In my last post, I described that I have implemented the URLStream class using pure IDF functionality. So you will not be wrong if you expect a working MP3 Internat Player next that just relies on the IDF w/o Arduino.

The main.cpp Program

Here is the complete IDF program that mimicks the Arduino setup() and loop() methods. The only other difference to the usual Arduino sketches is, that we can’t use Serial and the related logging, but we rely on the logging functionality provided by the IDF.

In this example we use an AI Thinker AudioKit as output device. But you can easily replace the AudioBoardStream with any other output class (e.g. I2SStream).

#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h" // for AudioKit

URLStream url("ssid","password");  // or replace with ICYStream to get metadata
AudioBoardStream i2s(AudioKitEs8388V1); // final output of decoded stream
MP3DecoderHelix mp3;
EncodedAudioStream dec_stream(&i2s, &mp3); // Decoding stream
StreamCopy copier(dec_stream, url); // copy url to decoder

void setup(){
  // setup i2s
  auto config = i2s.defaultConfig(TX_MODE);
  i2s.begin(config);

  // setup I2S based on sampling rate provided by decoder
  dec_stream.begin();

  // mp3 radio
  if (!url.begin("http://stream.srg-ssr.ch/m/rsj/mp3_128","audio/mp3")){
    stop();
  }
}

void loop(){
  copier.copy();
}

extern "C" void app_main() {
    setup();
    while(true) loop();
}

So like in the Arduino Variant, we just copy the mp3 data from an URLStream to a EncodedAudioStream which decods the mp3 to pcm and sends the result to the AudioBoardStream.

The CMakeLists.txt

Next to the main.cpp we have the following CMakeLists.txt

# This file was automatically generated for projects
# without default 'CMakeLists.txt' file.

FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)

idf_component_register(SRCS ${app_sources})

# imporant defines
add_compile_definitions(-DESP32_CMAKE -DLOG_LOCAL_LEVEL=ESP_LOG_INFO )

The -DESP32_CMAKE tells all libraries that we use the ESP32 IDF framework w/o Arduino.
Note that we also define the log level here!

Dependencies

The components directory contains all dependencies:

Complete Source Code

You can find the complete source code on Github!

Categories: Machine Sound

0 Comments

Leave a Reply

Avatar placeholder

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