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:

#define USE_HELIX 

#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

The sketch is using the following dependencies:

Source code

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

Final Notes

This example works with Arduino ESP32 Release 1.0.6 and with PlatformIO. It seems that Arduino ESP Release 2.0.0 has still some bugs.

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


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.