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!