udp_experimental_recorder/rec.py
george 3332460da3 feat:
--Starts Here!
2026-04-13 11:49:38 +05:30

39 lines
882 B
Python

import alsaaudio
import socket
# Network
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", UDP_PORT))
# 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)
out.write(data) # play the 960 samples
except socket.timeout:
out.write(SILENCE) # packet lost → play silence
except KeyboardInterrupt:
sock.close()
print("Stopped")