-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketstream.py
68 lines (55 loc) · 1.92 KB
/
socketstream.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
63
64
65
66
67
68
import pyaudio
import socket
import select
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
dev = p.get_device_info_by_index(i)
print((i,dev['name'],dev['maxInputChannels']))
form_1 = pyaudio.paInt16
chans = 1
samp_rate = 44100
chunk = 4096
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
try:
if (p.get_device_info_by_host_api_device_index(0,i).get("maxInputChannels") > 0):
dev_index = i
print("\n")
print ("Recording device index: {} was automatically selected:{}".format(i, p.get_device_info_by_host_api_device_index(0,i).get("name") + "\n"))
break
else:
dev_index = 2
except Exception:
print("\n Exception in channel")
pass
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(('', 4444))
serversocket.listen(5)
def callback(in_data, frame_count, time_info, status):
for s in read_list[1:]:
s.send(in_data)
return (None, pyaudio.paContinue)
# start Recording
stream = p.open(format=form_1, channels=chans, rate=samp_rate, input=True,input_device_index = dev_index, frames_per_buffer=chunk, stream_callback=callback)
read_list = [serversocket]
print ("Socket created")
try:
while True:
readable, writable, errored = select.select(read_list, [], [])
for s in readable:
if s is serversocket:
(clientsocket, address) = serversocket.accept()
read_list.append(clientsocket)
print ("Connection from", address)
else:
data = s.recv(1024)
if not data:
read_list.remove(s)
except (OSError,KeyboardInterrupt,SystemExit):
print ("\nEnded socket")
serversocket.close()
stream.stop_stream()
stream.close()
p.terminate()
pass