summaryrefslogtreecommitdiff
path: root/examples/asyncio_coroutines.py
diff options
context:
space:
mode:
authorMatthias Geier <Matthias.Geier@gmail.com>2019-01-14 13:12:16 +0100
committerMatthias Geier <Matthias.Geier@gmail.com>2019-02-09 16:30:33 +0100
commit53e001374aceb316de496234db4ce8e516491c6a (patch)
tree98d15b617bfd50cb9b63ae39d42b58ca2cc6c074 /examples/asyncio_coroutines.py
parent24c045a26080e5ba1fd7f6305e40b9eebf7f95ff (diff)
DOC: Add asyncio examples
Diffstat (limited to 'examples/asyncio_coroutines.py')
-rwxr-xr-xexamples/asyncio_coroutines.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/examples/asyncio_coroutines.py b/examples/asyncio_coroutines.py
new file mode 100755
index 0000000..af6d707
--- /dev/null
+++ b/examples/asyncio_coroutines.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python3
+"""An example for using a stream in an asyncio coroutine.
+
+This example shows how to create a stream in a coroutine and how to wait for
+the completion of the stream.
+
+You need Python 3.7 or newer to run this.
+
+"""
+import asyncio
+
+import numpy as np
+import sounddevice as sd
+
+
+async def record_buffer(buffer, **kwargs):
+ loop = asyncio.get_event_loop()
+ event = asyncio.Event()
+ idx = 0
+
+ def callback(indata, frame_count, time_info, status):
+ nonlocal idx
+ if status:
+ print(status)
+ remainder = len(buffer) - idx
+ if remainder == 0:
+ loop.call_soon_threadsafe(event.set)
+ raise sd.CallbackStop
+ indata = indata[:remainder]
+ buffer[idx:idx + len(indata)] = indata
+ idx += len(indata)
+
+ stream = sd.InputStream(callback=callback, dtype=buffer.dtype,
+ channels=buffer.shape[1], **kwargs)
+ with stream:
+ await event.wait()
+
+
+async def play_buffer(buffer, **kwargs):
+ loop = asyncio.get_event_loop()
+ event = asyncio.Event()
+ idx = 0
+
+ def callback(outdata, frame_count, time_info, status):
+ nonlocal idx
+ if status:
+ print(status)
+ remainder = len(buffer) - idx
+ if remainder == 0:
+ loop.call_soon_threadsafe(event.set)
+ raise sd.CallbackStop
+ valid_frames = frame_count if remainder >= frame_count else remainder
+ outdata[:valid_frames] = buffer[idx:idx + valid_frames]
+ outdata[valid_frames:] = 0
+ idx += valid_frames
+
+ stream = sd.OutputStream(callback=callback, dtype=buffer.dtype,
+ channels=buffer.shape[1], **kwargs)
+ with stream:
+ await event.wait()
+
+
+async def main(frames=150_000, channels=1, dtype='float32', **kwargs):
+ buffer = np.empty((frames, channels), dtype=dtype)
+ print('recording buffer ...')
+ await record_buffer(buffer, **kwargs)
+ print('playing buffer ...')
+ await play_buffer(buffer, **kwargs)
+ print('done')
+
+
+if __name__ == "__main__":
+ asyncio.run(main())