2

i wanted to record audio using max9814 and raspberry pi pico with micropython/circuitpython but when i recorded the data, some parts of it lose because it's trying to read the data and write the data into a .wav file and it can not hadle it simultaneously.

the circuit is like below:

RPI PICO MAX9814
GP26 OUT
3v3 VDD
GND GND

my code is below:


import board
import analogio
import time
import adafruit_wave
import struct
import storage

# storage.remount("/", readonly=False)

adc = analogio.AnalogIn(board.A0)
conversion_factor = 3.3/4096
buffer = []

f = adafruit_wave.open("audio.wav", "w")
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(8000)

def save(data):
    global f
    f.writeframes(bytearray(data))
    print("done")

try:
    while True:
        sample = adc.value * conversion_factor
        frame = bytearray(struct.pack('>H', int(sample)))
        buffer.extend(frame)
        
        if len(buffer) > 16000:
            save(buffer)
            buffer = []
            print("clear buffer")
except KeyboardInterrupt:
    print("closing")
    f.close()

How can i handle it?

11
  • Sadly, you are unlikely to be able to meet the deadlines required to read audio, process it and write it to disk in python with a single processing unit
    – fdcpp
    Commented Jan 20 at 19:05
  • I used async but the result was the same, can you explain what is the dealine? Commented Jan 20 at 21:29
  • Your audio should be coming in at a regular rate so you only have the time between one sample and the next to do all the work that isn’t reading samples. If you had a separate processor or core you could offload the work of filing a buffer, but then you would be on a buffer deadline. Likely your best bet is to just record what audio you can into a static memory block then write to disk once you are finished
    – fdcpp
    Commented Jan 20 at 21:40
  • The problem is that the memory of the raspberry pi pico is very shallow and i can't save more than 1 seconds on the buffer and im looking for a way which i can run the reading and the saving part on separated cores. Commented Jan 20 at 21:49
  • 1
    This seems similar to this recent question, where I made a couple of comments.
    – nekomatic
    Commented Jan 23 at 10:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.