-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioStreamClient.py
More file actions
79 lines (69 loc) · 2.71 KB
/
AudioStreamClient.py
File metadata and controls
79 lines (69 loc) · 2.71 KB
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
69
70
71
72
73
74
75
76
77
78
79
import socket
import pyaudio
import threading
import time, queue
class AudioStreamClient:
def __init__(self, ip, port, channels, rate, chunk_size):
self.ip = ip
self.port = port
self.channels = channels
self.rate = rate
self.chunk_size = chunk_size
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((self.ip, self.port))
self.audio = pyaudio.PyAudio()
self.stream = self.audio.open(format=pyaudio.paInt16,
channels=self.channels,
rate=self.rate,
output=True,
frames_per_buffer=self.chunk_size)
self.is_receiving = False
self.frame_q = queue.Queue(maxsize=2000)
self.cframe = '' # current frame
self.pframe = '' # previous frame
self.emptyFrame = [0] * (self.chunk_size * self.channels * 2) # silence
def start_receiving(self):
self.is_receiving = True
self.thread = threading.Thread(target=self.receive_audio)
self.thread.start()
time.sleep(0.025)
print("Started receiving audio")
while True:
# handle underflow errors
if self.frame_q.empty():
self.frame = self.emptyFrame # silence or pframe if repeat
#print('empty queue')
else:
if self.frame_q.qsize()>100:
self.frame_q.queue.clear()
self.frame = self.frame_q.get()
self.pframe = self.frame
self.stream.write(self.frame, exception_on_underflow=False)
def stop_receiving(self):
self.is_receiving = False
self.thread.join()
print("Stopped receiving audio")
def receive_audio(self):
while self.is_receiving:
data, _ = self.sock.recvfrom(self.chunk_size * self.channels * 2)
self.frame_q.put(data)
#self.stream.write(data)
def close(self):
self.stop_receiving()
self.stream.stop_stream()
self.stream.close()
self.audio.terminate()
self.sock.close()
if __name__ == "__main__":
client_ip = "0.0.0.0" # Client's IP address
client_port = 57001 # Client's port to receive audio data
channels = 1 # Number of audio channels (e.g., mono)
rate = 32000 # Sample rate in Hz
chunk_size = 256 # Number of audio frames per buffer
client = AudioStreamClient(client_ip, client_port, channels, rate, chunk_size)
client.start_receiving()
try:
while True:
pass
except KeyboardInterrupt:
client.close()