From cb157b5337096d77200462cd9fbc3e53ca99a724 Mon Sep 17 00:00:00 2001 From: Audrey Dutcher Date: Thu, 2 May 2024 17:30:08 -0700 Subject: [PATCH] add an aplay function --- sound/signal.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sound/signal.py b/sound/signal.py index 21abd1f..937a3ea 100644 --- a/sound/signal.py +++ b/sound/signal.py @@ -3,6 +3,7 @@ import numpy import struct import wave +import asyncio import progressbar @@ -53,6 +54,16 @@ def play(self, length: Optional[float]=None, progress=False): data = self.render(length, progress) sd.play(data, blocking=True) + async def aplay(self, length: Optional[float]=None): + """ + Play this signal. Block (but asyncio yield) until playback is complete. + If the given signal is infinitely long, default to three seconds of playback. + + :param length: The length to play, in seconds. Optional. + """ + data = await asyncio.get_event_loop().run_in_executor(None, self.render, length, False) + sd + def play_async(self): """ Play this signal asynchronously. Return the `sounddevice` stream object for this playback.