Skip to content

Commit

Permalink
playsound -> nava (#79)
Browse files Browse the repository at this point in the history
* change : dependency changed.

* update : functions updated with nava.

* update : tests updated.

* log : changes logged.

* fix : drop Python 3.5

* update : `nava` version updated to `0.4`.

* doc : CHANGELOG.md updated

* fix : minor edit in run function

* fix : windows and macos version fixed

* doc : play_sound function docstring updated

* fix : unused imports removed

---------

Co-authored-by: sepandhaghighi <[email protected]>
  • Loading branch information
sadrasabouri and sepandhaghighi authored Jul 2, 2024
1 parent 4eacae1 commit 9d1ddd5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-20.04, windows-latest, macOS-latest]
python-version: [3.5, 3.6, 3.7, 3.8, 3.9, 3.10.0, 3.11.0]
os: [ubuntu-20.04, windows-2022, macOS-13]
python-version: [3.6, 3.7, 3.8, 3.9, 3.10.0, 3.11.0]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- `sound_check` function
### Changed
- `playsound` replaced with `nava`
- `nava` added to `requirements.txt`
- Test system modified
- `Python 3.11` added to `test.yml`
- CLI mode updated
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
art==5.9
playsound==1.3.0
nava==0.3
codecov>=2.0.15
pytest>=4.3.1
pytest-cov>=2.6.1
Expand Down
51 changes: 14 additions & 37 deletions nafas/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from nafas.params import NAFAS_DESCRIPTION, NAFAS_NOTICE, STANDARD_MENU, STANDARD_MENU_ORDER, STEP_MAP
from nafas.params import PROGRAMS, PROGRAM_DESCRIPTION, SOUND_MAP, STEP_TEMPLATE, CYCLE_TEMPLATE
from nafas.params import SOUND_WARNING_MESSAGE
import playsound
import threading
import nava
import os
from warnings import warn

Expand Down Expand Up @@ -132,7 +131,7 @@ def sound_check():
"""
sound_path = get_sound_path(SOUND_MAP['Silence'])
try:
playsound.playsound(sound_path)
nava.play(sound_path)
except Exception:
warn(SOUND_WARNING_MESSAGE, RuntimeWarning)

Expand Down Expand Up @@ -276,38 +275,15 @@ def graphic_counter(delay_time):
print()


def _playsound_async(sound_path, debug):
"""
Play sound asynchronous in a thread.
:param sound_path: sound path
:type sound_path: str
:param debug: debug mode flag
:type debug: bool
:return: None
"""
try:
playsound.playsound(sound_path)
except Exception as e:
if debug:
print(str(e))


def play_sound(sound_path, debug=False):
def play_sound(sound_path):
"""
Play inputted sound file.
:param sound_path: sound path
:type sound_path: str
:param debug: debug mode flag
:type debug: bool
:return: new thread as threading.Thread object
:return: sound id as int
"""
new_thread = threading.Thread(
target=_playsound_async, args=(
sound_path, debug,), daemon=True)
new_thread.start()
return new_thread
return nava.play(sound_path, async_mode=True)


def run(program_data):
Expand All @@ -324,15 +300,15 @@ def run(program_data):
unit = program_data["unit"]
pre = program_data["pre"]
print("Preparing ", end="", flush=True)
sound_thread = play_sound(get_sound_path(SOUND_MAP['Prepare']))
sid = play_sound(get_sound_path(SOUND_MAP['Prepare']))
graphic_counter(pre)
line()
time.sleep(1)
sound_thread.join()
sound_thread = play_sound(get_sound_path(SOUND_MAP['Start']))
nava.stop(sid)
sid = play_sound(get_sound_path(SOUND_MAP['Start']))
print("Start", flush=True)
time.sleep(1)
sound_thread.join()
nava.stop(sid)
line()
time.sleep(1)
for i in range(cycle):
Expand All @@ -341,15 +317,16 @@ def run(program_data):
for index, item in enumerate(ratio):
if item != 0:
item_name = STEP_MAP[index]
sound_thread = play_sound(get_sound_path(SOUND_MAP[item_name]))
sid = play_sound(get_sound_path(SOUND_MAP[item_name]))
print(
STEP_TEMPLATE.format(
item_name, str(
unit * item)), flush=True)
graphic_counter(item * unit)
sound_thread.join()
nava.stop(sid)
time.sleep(1)
line()
sound_thread = play_sound(get_sound_path(SOUND_MAP['End']))
sid = play_sound(get_sound_path(SOUND_MAP['End']))
print("Well done!", flush=True)
sound_thread.join()
time.sleep(2)
nava.stop(sid)
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
art>=1.8
playsound>=1.0.0

nava>=0.4
7 changes: 4 additions & 3 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@
. . . .
######################################################################
Well done!
>>> play_sound(1, debug=False).join()
>>> play_sound(1, debug=True).join()
ignore_this_message
>>> sid = play_sound(1)
Traceback (most recent call last):
...
nava.errors.NavaBaseError: Sound file's path should be a string.
>>> try:
... wave_path = os.path.join("nafas", "sounds", "silence.wav")
... temp_path = os.path.join("nafas", "sounds", "temp.wav")
Expand Down

0 comments on commit 9d1ddd5

Please sign in to comment.