Skip to content

Commit 65ddfbe

Browse files
committed
Add "wire" example
1 parent fb8ca29 commit 65ddfbe

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

doc/examples.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
:orphan:
22

3-
Example Program(s)
4-
==================
3+
Example Programs
4+
================
55

66
Play a Sound File
77
-----------------
88

99
:download:`play_file.py <../examples/play_file.py>`
1010

1111
.. literalinclude:: ../examples/play_file.py
12+
13+
Input to Output Pass-Through
14+
----------------------------
15+
16+
:download:`wire.py <../examples/wire.py>`
17+
18+
.. literalinclude:: ../examples/wire.py

examples/wire.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
"""Pass input directly to output.
3+
4+
See https://www.assembla.com/spaces/portaudio/subversion/source/HEAD/portaudio/trunk/test/patest_wire.c
5+
6+
"""
7+
import argparse
8+
9+
parser = argparse.ArgumentParser(description=__doc__)
10+
parser.add_argument("-i", "--input-device", type=int, help="input device ID")
11+
parser.add_argument("-o", "--output-device", type=int, help="output device ID")
12+
parser.add_argument("-c", "--channels", type=int, default=2,
13+
help="number of channels")
14+
parser.add_argument("-t", "--dtype", help="audio data type")
15+
parser.add_argument("-s", "--samplerate", type=float, help="sampling rate")
16+
parser.add_argument("-b", "--blocksize", type=int, help="block size")
17+
parser.add_argument("-l", "--latency", type=float, help="latency in seconds")
18+
args = parser.parse_args()
19+
20+
try:
21+
import sounddevice as sd
22+
23+
callback_status = sd.CallbackFlags()
24+
25+
def callback(indata, outdata, frames, time, status):
26+
global callback_status
27+
callback_status |= status
28+
outdata[:] = indata
29+
30+
with sd.Stream(device=(args.input_device, args.output_device),
31+
samplerate=args.samplerate, blocksize=args.blocksize,
32+
dtype=args.dtype, latency=args.latency,
33+
channels=args.channels, callback=callback):
34+
print("#" * 80)
35+
print("press Return to quit")
36+
print("#" * 80)
37+
input()
38+
39+
if callback_status:
40+
print(callback_status)
41+
except BaseException as e:
42+
# This avoids printing the traceback, especially if Ctrl-C is used.
43+
raise SystemExit(str(e))

0 commit comments

Comments
 (0)