I have a MAX 9814 microphone/amplifier attached through ADC to my Raspberry Pi Pico WH. I am trying to get it record audio (specifically into a .wav file). I am struggling to understand the digital information that microphone is sending me, and would appreciate help on analyzing what I believe to be PCM values.
BTW: I am sending these raw values from the Pi Pico to my computer in real-time through serial port connection, so there is a lot more computing power that I can work with to process this data.
Here is the script on my Pi Pico (I've taken out the serial port connection stuff since it works fine)
import machine
from machine import ADC, Pin
import time
import uos
baseline = 0
variability = 0.5
SAMPLES_PER_SECOND = 44100
soundSensor = ADC(28) # Pin where sensor device (Microphone) is connected
led = Pin('LED', Pin.OUT)
while True:
start_time = time.ticks_ms()
print(soundSensor.read_u16())
# If we detect a spike in the waveform greater than a 10% deviation from our baseline, someone is probably talking.
if soundSensor.read_u16() > (baseline + baseline*variability) or soundSensor.read_u16() < (baseline - baseline*variability):
led.on() # Turn the light on if we're detecting a spike
else:
led.off() # Otherwise, keep the light off
elapsed_time = time.ticks_diff(time.ticks_ms(), start_time)
expected_interval = int(1000 / SAMPLES_PER_SECOND) # Convert to integer
sleep_time = max(0, expected_interval - elapsed_time) # Adjusted sleep time to maintain desired sample rate
time.sleep_ms(sleep_time)
Here is the script on my computer:
import wave
from pathlib import Path
def read_microphone_output(file_name):
p = Path(__file__).with_name(file_name)
with open(p.absolute(), 'rb') as pcmfile:
pcmdata = pcmfile.read()
with wave.open('store_info.wav', 'wb') as wavfile:
wavfile.setparams((1, 2, 44100, 0, 'NONE', 'NONE'))
wavfile.writeframes(pcmdata)
read_microphone_output("store_info.txt")
Here is an example of what "store_info.txt" looks like:
26342
25910
25382
25798
26278
26022
"store_info.wav" comes out as just a high pitched buzzing sound that lasts for about a second (keep in mind that I sample 10 seconds of microphone data, so the audio file should last for 10 seconds).
I am hoping for "store_info.wav" to come out as an audio recording--if I speak into it, I can hear my own voice.