-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
62 lines (47 loc) · 1.33 KB
/
client.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
56
57
58
59
60
61
62
'''
A script that connects to a network machine and attempts to play incoming
audio data. Uses PyAudio (Python bindings for PortAudio) for access to the
audio device, so it should be fairly portable.
Sample rate, number of channels, and sample format must all be edited
accordingly.
Alex Spitzer 2013
'''
import queue
import signal
import socket
import sys
import threading
import pyaudio
HOST = "localhost" if len(sys.argv) <= 1 else sys.argv[1]
PORT = 1234 if len(sys.argv) <= 2 else int(sys.argv[2])
def acceptData(s, buff):
while 1:
data = s.recv(128)
if not data:
break
buff.put(data)
s.close()
s = socket.socket()
s.connect((HOST, PORT))
buff = queue.Queue()
t = threading.Thread(target=acceptData, args = (s, buff))
t.daemon = True
t.start() # begin receiving data
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=48000, output=True)
def interrupt_handler(signal, frame):
print("Quitting...")
stream.stop_stream()
stream.close()
p.terminate()
s.close()
sys.exit(0)
signal.signal(signal.SIGINT, interrupt_handler)
signal.signal(signal.SIGTERM, interrupt_handler)
while 1:
try:
frame = buff.get(True, 5) # blocking get with 5 second timeout
except queue.Empty:
print("End of stream")
break
stream.write(frame)