-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream-write_client_3.0.py
47 lines (35 loc) · 1.13 KB
/
stream-write_client_3.0.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
__author__ = 'Hagar Zemach'
#!/usr/bin/env python
import pyaudio
import socket
# # socket connection
# # Return a string containing the hostname of the machine where the Python interpreter is currently executing:
#HOST = socket.gethostname()
HOST = '127.0.0.1'
PORT = 5000
# Audio recording parameters
FORMAT = pyaudio.paInt16
RATE = 16000
CHUNK = int(RATE / 10) # 100ms
CHANNELS = 1
# instantiate PyAudio
audio_interface = pyaudio.PyAudio()
audio_stream = audio_interface.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK
)
def send_audio():
print('listening and connecting...')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as newSocket:
newSocket.connect((HOST, PORT))
while True:
data = audio_stream.read(CHUNK)
# Send data to the socket
# Unlike send(),
# sendall() continues to send data from bytes until either all data has been sent or an error occurs.
newSocket.sendall(data)
if __name__ == '__main__':
send_audio()