/
r16auout.py
executable file
·55 lines (44 loc) · 1.18 KB
/
r16auout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# Pyaudio output device for 32-bit floating audio input from stdin
import pyaudio
import signal
import sys
import time
argvs = sys.argv
argc = len(argvs)
if argc == 2:
channels = int(argvs[1])
devidx = None
elif argc == 3:
channels = int(argvs[1])
devidx = int(argvs[2])
else:
print('Usage: ', argvs[0], 'channels [device-index]\n')
quit()
channels = int(argvs[1])
sample_rate = 48000
sample_width = 2 # 16bit int
def terminate():
stream.stop_stream()
stream.close()
p.terminate()
def signal_handler(signal, frame):
print('Terminated by CTRL/C')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
framesize = 768 # 16msec
readsize = framesize * sample_width * channels
p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.paInt16,
channels = channels,
rate = sample_rate,
output = True,
output_device_index = devidx,
frames_per_buffer = framesize
)
# print(stream.get_output_latency(), file=sys.stderr)
data = sys.stdin.buffer.read(readsize)
while data != '':
stream.write(data)
data = sys.stdin.buffer.read(readsize)
terminate()