udp_experimental_recorder/rec.py
George 7b93a4fce9 feat:
--ADDED MUMBLE server cap
2026-05-21 17:14:58 +05:30

40 lines
984 B
Python

import alsaaudio
import socket
import opuslib
# Network
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", UDP_PORT))
decoder = opuslib.Decoder(48000, 1)
# Audio
RATE = 48000
CHANNELS = 1
CHUNKSIZE = 960
out = alsaaudio.PCM(
alsaaudio.PCM_PLAYBACK,
alsaaudio.PCM_NORMAL,
channels=CHANNELS,
rate=RATE,
format=alsaaudio.PCM_FORMAT_S16_LE,
periodsize=CHUNKSIZE, # must match sender
device='default'
)
print("Receiving...")
SILENCE = b'\x00' * (CHUNKSIZE * 2) # 1920 bytes of silence
sock.settimeout(0.05) # 50ms timeout
try:
while True:
try:
data, addr = sock.recvfrom(4096)
pcm_data = decoder.decode(data, 960)
out.write(pcm_data) # play the 960 samples
except socket.timeout:
out.write(SILENCE) # packet lost → play silence
except KeyboardInterrupt:
sock.close()
print("Stopped")