Files
alsa_latency_test/sd_ioutput.py
2025-11-17 13:33:17 +01:00

69 lines
1.6 KiB
Python

import os
import sounddevice as sd
def main() -> None:
in_device = 1
out_device = 0
sample_rate = 48000
frame_size = 480
indev = int(in_device)
outdev = int(out_device)
dinfo = sd.query_devices(indev)
doutfo = sd.query_devices(outdev)
print(f"Input device {indev} has no input channels: {dinfo}")
inputs = [
(d['index'], d['name'], d['max_input_channels'])
for d in sd.query_devices()
if d.get('max_input_channels', 0) > 0
]
print('Input-capable devices:', inputs)
print(f"Output device {outdev} has no output channels: {doutfo}")
outputs = [
(d['index'], d['name'], d['max_output_channels'])
for d in sd.query_devices()
if d.get('max_output_channels', 0) > 0
]
print('Output-capable devices:', outputs)
istream = sd.RawInputStream(
samplerate=sample_rate,
device=indev,
channels=1,
dtype='int16',
blocksize=frame_size,
)
ostream = sd.RawOutputStream(
samplerate=sample_rate,
device=outdev,
channels=1,
dtype='int16',
blocksize=frame_size,
)
istream.start()
ostream.start()
try:
while True:
data, overflowed = istream.read(frame_size)
if overflowed:
print("an overflow happened")
ostream.write(data)
except KeyboardInterrupt:
pass
finally:
try:
istream.stop(); istream.close()
except Exception:
pass
try:
ostream.stop(); ostream.close()
except Exception:
pass
if __name__ == '__main__':
main()