I am trying to record audio from PDM Microphone MP34DT05-A, and save it in .wav file format on the micro SD Card through SPI, using Circuitpython.
The microcontroller I used is Raspberry Pi Pico W.
Currently, I could not fine a way to save audio data to .wav file.
Here my code but I stuck at saving the audio:
import time
import array
import math
import board
import audiobusio
import digitalio
import busio
import adafruit_sdcard
import storage
import audiocore
# Main program
mic = audiobusio.PDMIn(board.GP3, board.GP2, sample_rate=16000, bit_depth=16)
samples = array.array('H', [0] * 160)
filename = "/sd/recording1.wav"
record_time = 15 # seconds
sample_rate = 16000
# Initialize SPI bus and SD card.
# Initialize micro SD Card SPI pins
SCK_PIN = board.GP18
MOSI_PIN = board.GP19
MISO_PIN = board.GP16
CS_PIN = board.GP17
spi = busio.SPI(SCK_PIN, MOSI=MOSI_PIN, MISO=MISO_PIN)
cs = digitalio.DigitalInOut(CS_PIN)
sdcard = adafruit_sdcard.SDCard(spi, cs)
# Mount SD card filesystem.
vfs = storage.VfsFat(sdcard)
mount_result = None
try:
storage.mount(vfs, "/sd")
except Exception as e:
print("Error mounting filesystem:", e)
time.sleep(1)
# Create a WaveWrite object
with audiocore.WaveFile(open(filename, "wb")) as f:
while record_time > 0:
mic.record(samples, len(samples))
print(samples)
f.write(bytes(samples))
record_time -= 1
print("Recording complete!")
Any suggestion or ideas would be helpful. Thank you.