Skip to content

Commit

Permalink
NavaThread engine (#51)
Browse files Browse the repository at this point in the history
* feat : engine parameter added to NavaThread class

* fix : NavaThread instances fixed

* doc : CHANGELOG.md updated

* fix : autopep8
  • Loading branch information
sepandhaghighi authored Aug 14, 2024
1 parent 0d81ddf commit 48a6881
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Changed
- `engine` parameter added to `play` function
- `engine` parameter added to `NavaThread` class
- `README.md` modified
- `__play_win` function renamed to `__play_winsound`
- `__play_mac` function renamed to `__play_afplay`
Expand Down
5 changes: 5 additions & 0 deletions nava/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __play_winsound(sound_path, async_mode=False, loop=False):

if async_mode:
sound_thread = NavaThread(loop,
engine=Engine.WINSOUND,
target=__play_winsound_flags,
args=(sound_path, play_flags),
daemon=True)
Expand Down Expand Up @@ -143,6 +144,7 @@ def __play_alsa(sound_path, async_mode=False, loop=False):
"""
if async_mode:
sound_thread = NavaThread(loop,
engine=Engine.ALSA,
target=__play_proc_alsa,
args=(sound_path,),
daemon=True)
Expand Down Expand Up @@ -190,6 +192,7 @@ def __play_afplay(sound_path, async_mode=False, loop=False):
"""
if async_mode:
sound_thread = NavaThread(loop,
engine=Engine.AFPLAY,
target=__play_proc_afplay,
args=(sound_path,),
daemon=True)
Expand Down Expand Up @@ -249,6 +252,7 @@ def path_checker(sound_path, *args, **kwargs):
return func(sound_path, *args, **kwargs)
return path_checker


def __play_auto(sound_path, async_mode=False, loop=False):
"""
Play sound in automatic mode.
Expand All @@ -269,6 +273,7 @@ def __play_auto(sound_path, async_mode=False, loop=False):
else:
return __play_alsa(sound_path, async_mode, loop)


@path_check
def play(sound_path, async_mode=False, loop=False, engine=Engine.AUTO):
"""
Expand Down
2 changes: 2 additions & 0 deletions nava/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""


class Engine(Enum):
"""
Nava engine class.
Expand All @@ -22,6 +23,7 @@ class Engine(Enum):
ALSA = "alsa"
AFPLAY = "afplay"


SOUND_FILE_PLAY_ERROR = "Sound can not play due to some issues."
SOUND_FILE_EXIST_ERROR = "Given sound file doesn't exist."
SOUND_FILE_PATH_TYPE_ERROR = "Sound file's path should be a string."
Expand Down
12 changes: 7 additions & 5 deletions nava/thread.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# -*- coding: utf-8 -*-
"""Nava thread."""

import sys
import threading
from .params import Engine


class NavaThread(threading.Thread):
"""Nava custom thread."""

def __init__(self, loop, *args, **kwargs):
def __init__(self, loop, engine, *args, **kwargs):
"""
Init method.
:param loop: sound thread loop flag
:type loop: bool
:param engine: play engine
:type engine: Engine enum
:param args: arguments
:type args: list
:param kwargs: keyword arguments
:type kwargs: dict
"""
super(NavaThread, self).__init__(*args, **kwargs)
self._sys_platform = sys.platform
self._play_process = None
self._loop = loop
self._engine = engine

def run(self):
"""
Expand All @@ -31,7 +33,7 @@ def run(self):
:return: None
"""
if self._target is not None:
if self._sys_platform == "win32":
if self._engine == Engine.WINSOUND:
self._play_process = self._target(*self._args, **self._kwargs)
else:
while True:
Expand All @@ -47,7 +49,7 @@ def stop(self):
:return: None
"""
self._loop = False
if self._sys_platform == "win32":
if self._engine == Engine.WINSOUND:
import winsound
winsound.PlaySound(None, winsound.SND_PURGE)
else:
Expand Down

0 comments on commit 48a6881

Please sign in to comment.