3 Replies Latest reply on Sep 16, 2021 1:16 PM by scottiebabe

    MP3 Playback on PI Pico

    scottiebabe

      Woo Hoo! Three cheers for Adafruit's Circuit Python.

       

      A few months ago when I tried mp3 playback in circuit python I had no success, just a few seconds of gibberish audio (not even the length of the mp3). This was the original guide I followed: https://learn.adafruit.com/mp3-playback-rp2040 

       

      Whatever the issue was, they have pushed a fix into their alpha releases: https://github.com/adafruit/circuitpython/issues/4208

       

      Now, just in time for Halloween I can playback MP3 snippets on the Pico!

       

      I assume any recent CircuitPython 7.0 build will work, the one I grabbed happened to be:

      Adafruit CircuitPython 7.0.0-rc.1-70-g3e9cf8fa5

       

      This is the sample code I ran on the Pico:

      """
      CircuitPython single MP3 playback example for Raspberry Pi Pico.
      Plays a single MP3 once.
      """
      import board
      import audiomp3
      import audiopwmio
      import time
          
      audio = audiopwmio.PWMAudioOut(board.GP2)
      decoder = audiomp3.MP3Decoder(open("sineimd_24_32.mp3", "rb"))
      #decoder = audiomp3.MP3Decoder(open("noise.mp3", "rb"))
      audio.play(decoder)
      
      while audio.playing:
          pass
      print("Done playing!")
      

      I attached the 2 MP3 files I generated in Audacity. Both MP3 are using a 24 kHz sample rate and are encoded at constant bitrate of 32kbit/s.

       

      "sineimd_24_32.mp3" : 2 Sinusoids with frequencies of 1000 Hz and 1300 Hz.

      "noise.mp3": white noise

       

      For PWM audio they are generating a 10 bit PWM. The output frequency on the Pico ends up being 125MHz/1024 ~ 122 kHz.

       

      A scope shot of the PWM output pin:

       

      Here are a few frequency spectrum plots of just the unfiltered PWM DAC pin:

      You can also see the images offset from the 24 kHz sample frequency:

       

      After a 2nd order lowpass filter the results are quite reasonable (for low-fi sound effects at least):

       

       

      I'm not sure if this is audacity or the MP3 compression specification but when you use a sample rate of 24 kHz and a bitrate of 32kbit/s the spectrum is bandlimited to about 8.2 kHz.

       

      You can see this playing back white noise:

      This actually works out quite nicely as the sampling image is restricted to being above 16 kHz, this gives a 2nd order RC reconstruction filter at least a bit of a chance to rolloff a few dB.

       

      All in all, thank you Adafruit!

       

      noise.mp3