我有一个 MAX 9814 麦克风/放大器通过 ADC 连接到我的 Raspberry Pi Pico WH。我试图让它录制音频(特别是录制到 .wav 文件中)。我正在努力理解麦克风发送给我的数字信息,并且希望能帮助我分析我认为的 PCM 值。
顺便说一句:我通过串行端口连接将这些原始值从 Pi Pico 实时发送到我的计算机,因此我可以使用更多的计算能力来处理这些数据。
这是我的 Pi Pico 上的脚本(我已经取出了串行端口连接的东西,因为它工作正常)
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)
这是我电脑上的脚本:
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")
以下是“store_info.txt”的示例:
26342
25910
25382
25798
26278
26022
“store_info.wav”只是持续约一秒的高音嗡嗡声(请记住,我采样了 10 秒的麦克风数据,因此音频文件应持续 10 秒)。
我希望“store_info.wav”以录音形式出现——如果我对着它说话,我就能听到自己的声音。
麦克风读取模拟数据,然后通过麦克风所连接的模数转换器 (ADC) 引脚进行处理。
使用 store_info.txt 中获得的值,您必须使用参考电压和 pico 的比特率将其转换为电压读数(搜索如何执行此操作)。
看看这篇文章:
白噪声但没有声音 // 在 Raspberry Pi Pico (ADC) 上从驻极体麦克风前置放大器录制音频 // Micropython
这篇文章展示了一些使用正弦波值的测试代码以及如何将数据插入可读波形文件的正确格式。
需要注意的一件事是 pico 的内存非常差,因此要获得超过 2 秒的录音,需要您将给定持续时间内的记录值写入 SD 卡。
我希望您发现答案对您有所帮助,因为我对此类项目也比较陌生并且也在学习。