Skip to content

Commit

Permalink
Fix async bugs - Update documentation and test script
Browse files Browse the repository at this point in the history
* Async bugs: call the play function async from test script
* Test script: more intuitive example of test script
  • Loading branch information
NimaSamadi007 committed Jul 10, 2023
1 parent 1310e1d commit 2a3c6b3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 189 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,3 @@ dmypy.json

# Cython debug symbols
cython_debug/

# Dev realated files
dev/
80 changes: 0 additions & 80 deletions dev/playsound.py

This file was deleted.

84 changes: 0 additions & 84 deletions dev/test.py

This file was deleted.

42 changes: 22 additions & 20 deletions nava/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,29 @@ def __play_win(sound_path):


@quote
def __play_linux(sound_path, loop=False, is_async=True):
async def __play_linux(sound_path, is_async=True):
"""
Play sound in Linux.
:param sound_path: sound path
:param sound_path: sound path to be played
:type sound_path: str
:param is_async: play async or not
:type is_async: bool
:return: None
"""
if is_async:
if loop:
while True:
asyncio.run(__play_async_linx(sound_path))
else:
asyncio.run(__play_async_linx(sound_path))
task = asyncio.create_task(__play_async_linux(sound_path))
await task
else:
if loop:
while True:
__play_sync_linux(sound_path)
else:
__play_sync_linux(sound_path)
__play_sync_linux(sound_path)


def __play_sync_linux(sound_path):
"""
Play sound synch in Linux
Play sound synchronously in Linux
:param sound_path: sound path to be played
:type sound_path: str
:return: None
"""
_ = subprocess.check_call(["aplay",
sound_path],
Expand All @@ -93,9 +92,13 @@ def __play_sync_linux(sound_path):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)

async def __play_async_linx(sound_path):

async def __play_async_linux(sound_path):
"""
Play sound async in Linux
Play sound asynchronously in Linux
:param sound_path: sound path to be played
:type sound_path: str
:return: None
"""
play_cmd = f"aplay {sound_path}"
proc = await asyncio.subprocess.create_subprocess_shell(
Expand Down Expand Up @@ -152,15 +155,13 @@ def path_checker(sound_path, *args, **kwargs):


@path_check
def play(sound_path, loop=False, is_async=True):
async def play(sound_path, is_async=True):
"""
Play sound.
:param sound_path: sound path
:type sound_path: str
:param loop: play sound on loop (False by default)
:type loop: bool
:param is_async: play synchronously or asynchronously (True by default)
:param is_async: play synchronously or asynchronously (async by default)
:type is_async: bool
:return: None
"""
Expand All @@ -171,6 +172,7 @@ def play(sound_path, loop=False, is_async=True):
elif sys_platform == "darwin":
__play_mac(sound_path)
else:
__play_linux(sound_path, loop, is_async)
task = asyncio.create_task(__play_linux(sound_path, is_async))
await task
except Exception:
raise NavaBaseError(SOUND_FILE_PLAY_ERROR)
47 changes: 45 additions & 2 deletions test/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
import nava.functions as nvf
import asyncio
import nava.functions as nv

nvf.play("test/clear.wav", is_async=False, loop=False)

async def test_print():
"""
Simple function printing hello world async
:return: None
"""
while True:
print("Hello, World!")
await asyncio.sleep(0.1)

async def test_play():
"""
Simple function playing sound async (must provide path to file)
:return: None
"""
play_task = asyncio.create_task(nv.play("test/clear.wav", is_async=True))
print_task = asyncio.create_task(test_print())

await play_task

if __name__ == "__main__":
asyncio.run(test_play())
"""
How to run? Make sure you're in the project root
directory. Then, pass the script to Python interpreter
like below:
> python -m test.main
The result in async mode with 0.1 second delay is as follows:
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
In the meantime you should be able to hear the sound. The actual
number of hello worlds depend on the voice length and the delay value.
"""

0 comments on commit 2a3c6b3

Please sign in to comment.