From 59de0fdad0490c3b67a6d0f3a7db156dca740749 Mon Sep 17 00:00:00 2001 From: Naoto Kondo Date: Sat, 19 Mar 2022 17:07:18 +0900 Subject: [PATCH] add example for the recv audio sd --- example/recv_audio_sd.py | 95 ++++++++++++++++++++++++++++++++++++++++ example/requirements.txt | 3 +- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 example/recv_audio_sd.py diff --git a/example/recv_audio_sd.py b/example/recv_audio_sd.py new file mode 100644 index 0000000..fd010d7 --- /dev/null +++ b/example/recv_audio_sd.py @@ -0,0 +1,95 @@ +import sys +import queue +import numpy as np +import sounddevice as sd +import NDIlib as ndi + + +def main(): + + if not ndi.initialize(): + print('Cannot run NDI.') + return 0 + + find_create_desc = ndi.FindCreate() + ndi_find = ndi.find_create_v2(find_create_desc) + + if ndi_find is None: + return 0 + + sources = [] + while not len(sources) > 0: + print('Looking for sources ...') + ndi.find_wait_for_sources(ndi_find, 1000) + sources = ndi.find_get_current_sources(ndi_find) + + if len(sources) < 1: + return 0 + + recv_create_desc = ndi.RecvCreateV3() + recv_create_desc.source_to_connect_to = sources[0] + recv_create_desc.ndi_recv_name = 'Example Audio Converter Receiver' + + ndi_recv = ndi.recv_create_v3(recv_create_desc) + + if ndi_recv is None: + ndi.find_destroy(ndi_find) + return 0 + + ndi.find_destroy(ndi_find) + + q = queue.Queue(maxsize=20) + + def audio_callback(outdata, frames, time, status): + try: + data = q.get_nowait() + if not status.output_underflow: + outdata[:] = data + except queue.Empty as e: + return + + stream = sd.RawOutputStream( + samplerate=48000, blocksize=1024, channels=2, dtype='int16', callback=audio_callback) + + with stream: + while True: + t, v, a, m = ndi.recv_capture_v2(ndi_recv, 1000) + + if t == ndi.FRAME_TYPE_NONE: + print('No data received.') + continue + + if t == ndi.FRAME_TYPE_VIDEO: + print('Video data received (%dx%d).' % (v.xres, v.yres)) + ndi.recv_free_video_v2(ndi_recv, v) + continue + + if t == ndi.FRAME_TYPE_AUDIO: + print('Audio data received (%d samples).' % a.no_samples) + data = np.zeros((a.no_channels, a.no_samples), np.int16) + interleaved = ndi.AudioFrameInterleaved16s() + interleaved.data = data + ndi.util_audio_to_interleaved_16s_v2(a, interleaved) + timeout = interleaved.no_samples * 20 / interleaved.sample_rate + q.put(data, timeout=timeout) + ndi.recv_free_audio_v2(ndi_recv, a) + continue + + if t == ndi.FRAME_TYPE_METADATA: + print('Meta data received.') + ndi.recv_free_metadata(ndi_recv, m) + continue + + if t == ndi.FRANE_TYPE_STATUS_CHANGE: + print('Receiver connection status changed.') + continue + + ndi.recv_destroy(ndi_recv) + + ndi.destroy() + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/example/requirements.txt b/example/requirements.txt index fc4586c..3ed3508 100644 --- a/example/requirements.txt +++ b/example/requirements.txt @@ -1,2 +1,3 @@ numpy -opencv-python \ No newline at end of file +opencv-python +sounddevice \ No newline at end of file