-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
53 lines (41 loc) · 1.56 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
import socket
import threading
import pyaudio
class Client:
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while 1:
try:
self.target_ip = input('Enter IP address of server --> ')
self.target_port = int(input('Enter target port of server --> '))
self.s.connect((self.target_ip, self.target_port))
break
except:
print("Couldn't connect to server")
chunk_size = 1024 # 512
audio_format = pyaudio.paInt16
channels = 1
rate = 20000
# initialise microphone recording
self.p = pyaudio.PyAudio()
self.playing_stream = self.p.open(format=audio_format, channels=channels, rate=rate, output=True, frames_per_buffer=chunk_size)
self.recording_stream = self.p.open(format=audio_format, channels=channels, rate=rate, input=True, frames_per_buffer=chunk_size)
print("Connected to Server")
# start threads
receive_thread = threading.Thread(target=self.receive_server_data).start()
self.send_data_to_server()
def receive_server_data(self):
while True:
try:
data = self.s.recv(1024)
self.playing_stream.write(data)
except:
pass
def send_data_to_server(self):
while True:
try:
data = self.recording_stream.read(1024)
self.s.sendall(data)
except:
pass
client = Client()