From 3332460da3116600f739c2e85ad23b10707dae77 Mon Sep 17 00:00:00 2001 From: george Date: Mon, 13 Apr 2026 11:49:38 +0530 Subject: [PATCH] feat: --Starts Here! --- .gitignore | 1 + experimental.py | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 40 ++++++++++++++++++++++++++++++++++++++++ rec.py | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 experimental.py create mode 100644 main.py create mode 100644 rec.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7275bb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ diff --git a/experimental.py b/experimental.py new file mode 100644 index 0000000..f7ad5be --- /dev/null +++ b/experimental.py @@ -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") \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..06bff91 --- /dev/null +++ b/main.py @@ -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() + diff --git a/rec.py b/rec.py new file mode 100644 index 0000000..e0d7e53 --- /dev/null +++ b/rec.py @@ -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") \ No newline at end of file