--Starts Here!
This commit is contained in:
george 2026-04-13 11:49:38 +05:30
commit 3332460da3
4 changed files with 118 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
venv/

38
experimental.py Normal file
View File

@ -0,0 +1,38 @@
import alsaaudio
import numpy as np
import wave
import socket
# Network
UDP_IP = "172.16.11.27"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
inp = alsaaudio.PCM(
alsaaudio.PCM_CAPTURE, # capture mode
# alsaaudio.PCM_NONBLOCK, # non-blocking
alsaaudio.PCM_NORMAL,
channels=1, # mono
rate=48000, # sample rate (Hz)
format=alsaaudio.PCM_FORMAT_S16_LE, # 16-bit
periodsize=960, # frames per read
device='default'
)
# frames = []
try:
while True:
length, data = inp.read() # read audio chunk
if length > 0:
# convert raw bytes to numpy array
audio = np.frombuffer(data, dtype=np.int16)
sock.sendto(data, (UDP_IP, UDP_PORT))
print(f"Frames: {length} | Max amplitude: {np.max(np.abs(audio))}")
except KeyboardInterrupt:
print("Stopped")
finally:
sock.close()
print("Stopped")

40
main.py Normal file
View File

@ -0,0 +1,40 @@
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 150000
WAVE_OUTPUT_FILENAME = "output15.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

39
rec.py Normal file
View File

@ -0,0 +1,39 @@
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")