0

I'm using MicroPython and I2S to play audio through a MAX98357A DAC board. I have multiple audio files (currently all were converted to WAV) on the SDCard and upon a user interaction I start play them randomly.

The relevant code in the play(...) function is more or less like this (exception handling and some end cases are ommitted):

i2s = machine.I2S(..., mode=I2S.TX, bits=16, format=I2S.MONO, rate=22050)  # values are based on the current files.
while True:
  num_read = wavfile.readinto(buf)
  if num_read == 0:
    break
  i2s.write(buf)

Ideally I'd like to keep the i2s object open (especially since it's allocating an internal buffer), but my files may vary in terms of sample rate or bits width. I couldn't understand from the docs if the init() method will actually reuse the buffer or not. Is there any existing package in MicroPython to help with resampling of the audio buffer so I can define the I2S object once according to the desired output and resample the input to match it?

And if we're at it, is there any existing Micropython implementation that supports other audio formats (mp3, mp4, ...). I'm currently dealing with hundreds of MBs on the SDCard. I'd like to read common formats and output audio samples at a fixed rate and size.

Thanks!

Zach Moshe
  • 2,782
  • 4
  • 24
  • 40
  • What about preprocessing the audio files before placing them on the sd card? I suspect that on-the-fly resampling of audio might be beyond most micropython boards. – larsks Aug 29 '22 at 12:01
  • That's what I currently do (convert everything to WAV 22050 16bit) but this is a bit cumbersome, especially if I want the user to upload files, but I was also hoping to save some space on the sdcard. Those WAVs are hundreds of MBs.. – Zach Moshe Aug 29 '22 at 12:43
  • Perhaps a hardware MP3 decoder (so you can convert all the audio to MP3 to save space)? – larsks Aug 29 '22 at 13:22
  • Ye, I'll probably take a look on that direction.. I'm a bit clueless, do they normally also do the amplification (so output is +/- directly to the speaker)? provide a weak analog signal (maybe just for headphones)? or they only generate a stream of digital data that I need to pass through the same DAC I already use? Thanks again! – Zach Moshe Aug 29 '22 at 16:15
  • 1
    I am also clueless; I just know that e.g. [this exists](https://www.adafruit.com/product/1381). – larsks Aug 29 '22 at 16:18

0 Answers0