serial_audio_catcher/wav_recorder.py

30 lines
755 B
Python

import wave
import numpy as np
FIFO_PATH = "/tmp/esp32_audio"
OUTPUT_FILE = "recorded_audio.wav"
SAMPLE_RATE = 16000
CHANNELS = 2 # stereo from ESP32
BYTES_PER_SAMPLE = 2
# configure WAV writer
out_wav = wave.open(OUTPUT_FILE, "wb")
out_wav.setnchannels(CHANNELS)
out_wav.setsampwidth(BYTES_PER_SAMPLE)
out_wav.setframerate(SAMPLE_RATE)
with open(FIFO_PATH, "rb") as f:
print(f"Recording from {FIFO_PATH} into {OUTPUT_FILE}...")
try:
while True:
data = f.read(8000) # ~0.125s stereo
if not data:
continue
out_wav.writeframes(data)
except KeyboardInterrupt:
print("\nStopping recording.")
finally:
out_wav.close()
print(f"Saved {OUTPUT_FILE}")