diff options
| -rw-r--r-- | doc/examples.rst | 11 | ||||
| -rwxr-xr-x | examples/wire.py | 43 |
2 files changed, 52 insertions, 2 deletions
diff --git a/doc/examples.rst b/doc/examples.rst index 61d80d2..616543f 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -1,7 +1,7 @@ :orphan: -Example Program(s) -================== +Example Programs +================ Play a Sound File ----------------- @@ -9,3 +9,10 @@ Play a Sound File :download:`play_file.py <../examples/play_file.py>` .. literalinclude:: ../examples/play_file.py + +Input to Output Pass-Through +---------------------------- + +:download:`wire.py <../examples/wire.py>` + +.. literalinclude:: ../examples/wire.py diff --git a/examples/wire.py b/examples/wire.py new file mode 100755 index 0000000..3c4edcb --- /dev/null +++ b/examples/wire.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""Pass input directly to output. + +See https://www.assembla.com/spaces/portaudio/subversion/source/HEAD/portaudio/trunk/test/patest_wire.c + +""" +import argparse + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument("-i", "--input-device", type=int, help="input device ID") +parser.add_argument("-o", "--output-device", type=int, help="output device ID") +parser.add_argument("-c", "--channels", type=int, default=2, + help="number of channels") +parser.add_argument("-t", "--dtype", help="audio data type") +parser.add_argument("-s", "--samplerate", type=float, help="sampling rate") +parser.add_argument("-b", "--blocksize", type=int, help="block size") +parser.add_argument("-l", "--latency", type=float, help="latency in seconds") +args = parser.parse_args() + +try: + import sounddevice as sd + + callback_status = sd.CallbackFlags() + + def callback(indata, outdata, frames, time, status): + global callback_status + callback_status |= status + outdata[:] = indata + + with sd.Stream(device=(args.input_device, args.output_device), + samplerate=args.samplerate, blocksize=args.blocksize, + dtype=args.dtype, latency=args.latency, + channels=args.channels, callback=callback): + print("#" * 80) + print("press Return to quit") + print("#" * 80) + input() + + if callback_status: + print(callback_status) +except BaseException as e: + # This avoids printing the traceback, especially if Ctrl-C is used. + raise SystemExit(str(e)) |
