-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathaudio_debug_server.py
More file actions
54 lines (42 loc) · 1.76 KB
/
audio_debug_server.py
File metadata and controls
54 lines (42 loc) · 1.76 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
import socket
import wave
import argparse
'''
Create a UDP socket and bind it to the server's IP:8000.
Listen for incoming messages and print them to the console.
Save the audio to a WAV file.
'''
def main(samplerate, channels):
# Create a UDP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('0.0.0.0', 8000))
# Create WAV file with parameters
filename = f"{samplerate}_{channels}.wav"
wav_file = wave.open(filename, "wb")
wav_file.setnchannels(channels) # channels parameter
wav_file.setsampwidth(2) # 2 bytes per sample (16-bit)
wav_file.setframerate(samplerate) # samplerate parameter
print(f"Start saving audio from 0.0.0.0:8000 to {filename}...")
try:
while True:
# Receive a message from the client
message, address = server_socket.recvfrom(8000)
# Write PCM data to WAV file
wav_file.writeframes(message)
# Print length of the message
print(f"Received {len(message)} bytes from {address}")
except KeyboardInterrupt:
print("\nStopping recording...")
finally:
# Close files and socket
wav_file.close()
server_socket.close()
print(f"WAV file '{filename}' saved successfully")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='UDP音频数据接收器,保存为WAV文件')
parser.add_argument('--samplerate', '-s', type=int, default=16000,
help='采样率 (默认: 16000)')
parser.add_argument('--channels', '-c', type=int, default=2,
help='声道数 (默认: 2)')
args = parser.parse_args()
main(args.samplerate, args.channels)