serial_audio_catcher/main.py

38 lines
788 B
Python
Raw Normal View History

import serial
import pyaudio
import numpy as np
SERIAL_PORT = '/dev/ttyACM0' # Change this to your serial port
BAUD_RATE = 1500000
CHUNK_SIZE = 256 # Number of frames in each block
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=2,
rate=16000,
output=True)
ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
print("Streaming audio...")
try:
while True:
data = ser.read(CHUNK_SIZE * 4) # 2 channels × 2 bytes
if not data:
break
audio_data = np.frombuffer(data, dtype=np.int16)
stream.write(audio_data.tobytes())
except KeyboardInterrupt:
print("Stopping audio stream...")
finally:
stream.stop_stream()
stream.close()
p.terminate()
ser.close()