To set up a 3-mic beamforming unit using ESP32 modules with Micropython and Python on a Windows 10 workstation with an NVIDIA RTX 4090 and 128GB RAM, follow these steps:
- Connect the ESP32 modules to your computer via COM3, COM4, and COM5.
- Install the required libraries for the ESP32 modules.
- Install the Python library for audio processing and beamforming:
- Set up the Micropython firmware on the ESP32 modules.
- Write a Python script to handle communication and audio processing with the ESP32 modules.
Here's an example of a Python script for the beamforming process:
import serial
import pyaudio
import audioop
import numpy as np
import time
# Serial ports for ESP32 modules
ports = ['COM3', 'COM4', 'COM5']
baud_rate = 115200
# Record audio for a specific duration
record_duration = 1 # seconds
# Buffer size ( gloves this the number of samples per second )
buffer_size = 2048
frame_size = 256
sample_rate = int(buffer_size * frame_size * 2 ** 0.5)
# Initialize PyAudio
audio_stream = pyaudio.PyAudio()
# Initialize class for streaming and processing audio
class AudioStream:
def __init__(self, ser, sample_rate, frame_size):
self.frame_size = frame_size
self.buffer_size = frame_size * sample_rate // 1000
self.sampling_rate = sample_rate
self.stream = pyaudio.PyAudio()
self.beamformer = np.eye(3) # 3-element beamformer
self.mics = [ser] * 3
def process(self):
# Read audio from the microphones
mic_data = self.mics[0].read(self.buffer_size)
mic_data = np.frombuffer(mic_data, dtype=np.int16)
mic_data = mic_data.astype(np.float32)
# Process audio data with the beamformer
new_data = np.dot(self.beamformer, mic_data)
return new_data
def cleanup(self):
for mic in self.mics:
mic.close()
# Connect to ESP32 modules
stream = None
for i, port in enumerate(ports):
ser = serial.Serial(port, baud_rate)
mic = SerialBuffer(ser, buffer_size)
stream = AudioStream(mic, sample_rate, frame_size) if not stream else stream.append(AudioStream(mic, sample_rate, frame_size))
# Main audio processing loop
for _ in range(int(record_duration * sample_rate / buffer_size)):
audio_data = stream.process()
volume = audioop.avg(audio_data)
print(f"Volume: {volume:.2f}")
# Clean up
stream.cleanup()
This script initializes the audio stream and processes it with a 3-element beamformer. You can adjust the beamformer weights and frame size according to your requirements.