-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioStreamServer.py
More file actions
219 lines (182 loc) · 9.15 KB
/
AudioStreamServer.py
File metadata and controls
219 lines (182 loc) · 9.15 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import socket
import sys
import pyaudio
import threading
import tkinter as tk
from tkinter import messagebox, simpledialog
import json
import numpy as np
class AudioStreamServer:
def __init__(self):
self.clients = {}
self.sample_rate = 32000 # Sample rate in Hz
self.chunk_size = 256 # Number of audio frames per buffer
self.interface = "MacBook Air Microphone"#'artwalk'
self.audio = pyaudio.PyAudio()
# get device ID for named audio inteface
self.device_index,self.audio_channels = self.find_device_index(self.interface)
if self.device_index < 0:
print('No device found')
sys.exit(1)
# open the stream
self.stream = self.audio.open(format=pyaudio.paInt16,
channels=self.audio_channels,
rate=self.sample_rate,
input=True,
output=False,
input_device_index=self.device_index,
frames_per_buffer=self.chunk_size,
stream_callback=self.audio_callback)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.is_streaming = False
def find_device_index(self, device_name):
# find the index of audio device
found = -1
for i in range(self.audio.get_device_count()):
dev = self.audio.get_device_info_by_index(i)
name = dev['name']#.encode('utf-8')
print(i, name, dev['maxInputChannels'], dev['maxOutputChannels'])
if name.find(device_name) >= 0 and dev['maxInputChannels'] > 0:
found = i
n_ch = int(dev['maxInputChannels'])
break
return found,n_ch
def add_client(self, client_id, ip, port, channel):
self.clients[client_id] = {'ip': ip, 'port': port, 'channel': channel}
print(f"Added client {client_id} with IP {ip}, port {port}, channel {channel}")
def update_client(self, client_id, ip=None, port=None, channel=None):
if client_id in self.clients:
if ip:
self.clients[client_id]['ip'] = ip
if port:
self.clients[client_id]['port'] = port
if channel:
self.clients[client_id]['channel'] = channel
print(f"Updated client {client_id} to IP {self.clients[client_id]['ip']}, port {self.clients[client_id]['port']}, channel {self.clients[client_id]['channel']}")
def remove_client(self, client_id):
if client_id in self.clients:
del self.clients[client_id]
print(f"Removed client {client_id}")
def save_clients(self, filename):
with open(filename, 'w') as file:
json.dump(self.clients, file)
print(f"Saved clients to {filename}")
def load_clients(self, filename):
try:
with open(filename, 'r') as file:
self.clients = json.load(file)
print(f"Loaded clients from {filename}")
return True
except FileNotFoundError:
print(f"File {filename} not found")
return False
def stream_audio(self):
while self.is_streaming:
data = self.stream.read(self.chunk_size, exception_on_overflow=False)
for client_id, client_info in self.clients.items():
channel_data = data[client_info['channel']::self.audio_channels]
self.sock.sendto(channel_data, (client_info['ip'], client_info['port']))
# pyaudio call back function to send packets
def audio_callback(self,in_data, frame_count, time_info, status):
data_array = np.frombuffer(in_data, dtype='int16')
#for entity in CHANNEL_MAP:
for client_id, client_info in self.clients.items():
channel = data_array[client_info['channel']::self.audio_channels] # an error check here for ch<n_channels
data_str = channel.tobytes() # Use tobytes instead of tostring
try:
self.sock.sendto(data_str, (client_info['ip'], client_info['port']))
except OSError as error :
None
return (in_data, pyaudio.paContinue)
def start_streaming(self):
if not self.is_streaming:
self.is_streaming = True
self.stream.start_stream() #
#self.thread = threading.Thread(target=self.stream_audio)
#self.thread.start()
print("Audio streaming started")
def stop_streaming(self):
if self.is_streaming:
self.is_streaming = False
self.stream.stop_stream()
#self.thread.join()
print("Audio streaming stopped")
def close(self):
self.stop_streaming()
self.stream.stop_stream()
self.stream.close()
self.audio.terminate()
class AudioStreamServerGUI(tk.Tk):
def __init__(self, server):
super().__init__()
self.server = server
self.title("Audio Stream Server")
self.client_listbox = tk.Listbox(self, width=50, height=10)
self.client_listbox.pack(pady=10)
self.add_client_button = tk.Button(self, text="Add Client", command=self.add_client)
self.add_client_button.pack(pady=5)
self.update_client_button = tk.Button(self, text="Update Client", command=self.update_client)
self.update_client_button.pack(pady=5)
self.remove_client_button = tk.Button(self, text="Remove Client", command=self.remove_client)
self.remove_client_button.pack(pady=5)
self.save_clients_button = tk.Button(self, text="Save Clients", command=self.save_clients)
self.save_clients_button.pack(pady=5)
self.load_clients_button = tk.Button(self, text="Load Clients", command=self.load_clients)
self.load_clients_button.pack(pady=5)
self.start_button = tk.Button(self, text="Start Streaming", command=self.server.start_streaming)
self.start_button.pack(pady=5)
self.stop_button = tk.Button(self, text="Stop Streaming", command=self.server.stop_streaming)
self.stop_button.pack(pady=5)
def add_client(self):
client_id = simpledialog.askstring("Client ID", "Enter client ID:")
ip = simpledialog.askstring("IP Address", "Enter IP address:")
port = simpledialog.askinteger("Port", "Enter port number:")
channel = simpledialog.askinteger("Channel", "Enter channel number:")
if client_id and ip and port is not None and channel is not None:
self.server.add_client(client_id, ip, port, channel)
self.client_listbox.insert(tk.END, f"{client_id} - {ip}:{port} - Channel {channel}")
def update_client(self):
selected = self.client_listbox.curselection()
if selected:
client_info = self.client_listbox.get(selected[0])
client_id = client_info.split(" - ")[0]
new_ip = simpledialog.askstring("New IP Address", "Enter new IP address:")
new_port = simpledialog.askinteger("New Port", "Enter new port number:")
new_channel = simpledialog.askinteger("New Channel", "Enter new channel number (0 or 1):")
if new_ip and new_port is not None and new_channel is not None:
self.server.update_client(client_id, ip=new_ip, port=new_port, channel=new_channel)
self.client_listbox.delete(selected[0])
self.client_listbox.insert(selected[0], f"{client_id} - {new_ip}:{new_port} - Channel {new_channel}")
def remove_client(self):
selected = self.client_listbox.curselection()
if selected:
client_info = self.client_listbox.get(selected[0])
client_id = client_info.split(" - ")[0]
self.server.remove_client(client_id)
self.client_listbox.delete(selected[0])
def save_clients(self):
#filename = simpledialog.askstring("Save Clients", "Enter filename:")
filename="artwalkClients"
if filename:
self.server.save_clients(filename)
def load_clients(self):
filename = simpledialog.askstring("Load Clients", "Enter filename:")
if filename:
if self.server.load_clients(filename):
self.client_listbox.delete(0, tk.END)
for client_id, client_info in self.server.clients.items():
self.client_listbox.insert(tk.END, f"{client_id} - {client_info['ip']}:{client_info['port']} - Channel {client_info['channel']}")
def load_client_defaults(self):
if self.server.load_clients("artwalkClients"):
self.client_listbox.delete(0, tk.END)
for client_id, client_info in self.server.clients.items():
self.client_listbox.insert(tk.END, f"{client_id} - {client_info['ip']}:{client_info['port']} - Channel {client_info['channel']}")
def on_closing(self):
self.server.close()
self.destroy()
if __name__ == "__main__":
server = AudioStreamServer()
gui = AudioStreamServerGUI(server)
gui.load_client_defaults()
gui.protocol("WM_DELETE_WINDOW", gui.on_closing)
gui.mainloop()