From 48a68818bc7b66558a77b1df9d9be67e8ebb9ea0 Mon Sep 17 00:00:00 2001 From: Sepand Haghighi Date: Thu, 15 Aug 2024 02:17:09 +0330 Subject: [PATCH] `NavaThread` engine (#51) * feat : engine parameter added to NavaThread class * fix : NavaThread instances fixed * doc : CHANGELOG.md updated * fix : autopep8 --- CHANGELOG.md | 1 + nava/functions.py | 5 +++++ nava/params.py | 2 ++ nava/thread.py | 12 +++++++----- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba26a21..511754d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/nava/functions.py b/nava/functions.py index 231c885..426d4ee 100644 --- a/nava/functions.py +++ b/nava/functions.py @@ -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) @@ -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) @@ -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) @@ -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. @@ -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): """ diff --git a/nava/params.py b/nava/params.py index 111600c..98bcfde 100644 --- a/nava/params.py +++ b/nava/params.py @@ -9,6 +9,7 @@ """ + class Engine(Enum): """ Nava engine class. @@ -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." diff --git a/nava/thread.py b/nava/thread.py index 55a8ca9..f281d74 100644 --- a/nava/thread.py +++ b/nava/thread.py @@ -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): """ @@ -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: @@ -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: