Yesterday I was finally resolving some stability issues in my URLStream class and I wanted to take the opportunity to highlight that it is pretty simple to build a simple mp3 streaming audio player using the Stream API of my Arduino Audio Tools library. You just need to copy the result from an URLStream to your output stream:

The Arduino Sketch

In this example Arduino sketch I am streaming BBC World Service to the analog output pins using an ESP32 Microcontroller:


#include "AudioTools.h" #include "AudioCodecs/CodecMP3Helix.h" using namespace audio_tools; URLStream url("SSID","password"); AnalogAudioStream out; // final output of decoded stream EncodedAudioStream dec(out, new MP3DecoderHelix()); // Decoding stream StreamCopy copier(dec, url); // copy url to decoder void setup(){ Serial.begin(115200); AudioLogger::instance().begin(Serial, AudioLogger::Info); // setup out out.begin(out.defaultConfig(TX_MODE);); // setup out based on sampling rate provided by decoder dec.setNotifyAudioChange(out); dec.begin(); // BBC World - mp3 radio url.begin("https://stream.live.vc.bbcmedia.co.uk/bbc_world_service","audio/mp3"); Serial.println("starting to play..."); } void loop(){ copier.copy(); }

I think the overall logic is quite easy to understand: we use the EncodedAudioStream with a Decoder as output which sends the decoded result to the final output AnalogAudioStream device. The only specific thing to mention here is, that the AnalogAudioStream output device needs to know the number of channels, the bit size and the sample rate to play back the audio correctly and we do not know this information when we setup the output yet, so we just start with some default settings. Fortunately, the decoder has some automatic notification mechanism and with the line dec.setNotifyAudioChange(out);, we ask the decoder to notify the output whenever the audio information is changing!

The Output

On the ESP32 the analog output is available on GPIO25 (Channel 1) and GPIO26 (Channel 2). You can use a piezo electric element or connect some earphones:
Earphones

Dependencies

You need to install the following libraries:

Source code

You can find the source code for this and other streaming examples on Github.

Final Notes

This example works with Arduino ESP32 and with PlatformIO. You can replace the helix decoder with any other supported decoder. In the example we used the AnalogAudioStream output class but you can replace it with any other supported Audio Sink. For details please consult the Readme of the project.

In the next Blog, I will demonstrate how to set up streaming with the AudioPlayer class….


6 Comments

Bromium · 10. January 2023 at 22:41

“On the ESP32 the analog output is available on GPIO25 (Channel 1) and GPIO26 (Channel 2).”

As these are the two DAC channels of the ESP32, how does the analog output compare in terms of fidelity to a dedicated music chip such as the VS1053?

Thanks,
Bromium

    pschatzmann · 11. January 2023 at 3:33

    If I remember right they have only 8 bit resolution and usually you get a lot of noise. So they do not compare!
    I usually just use them for testing

      Bromium · 11. January 2023 at 4:15

      Thank you for your fast answer, Phil.

Giak · 7. August 2022 at 13:58

I cannot compile the sketch because MP3DecoderHelix.h file is missing….

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *