This week I was looking into the FAAD AAC Decoder and tried to make it work on the ESP32.
I was struggling quite a bit with the memory requirements and I could only make it work with a stack of 60k and by using PSRAM. So to fulfill the stack requirement we need to run the decoder in a separate freertos task.
The Arduino Sketch
Here is a working sketch that streams AAC audio from the internet and decodes it using FAAT. I was testing on an AudioKit, but you are free to replace the AudioKitStream with any other supported output stream (e.g. I2SStream):
#include "AudioTools.h"
#include "AudioCodecs/CodecAACFAAD.h"
#include "AudioLibs/AudioKit.h"
#include "freertos-all.h"
#include "esp_heap_caps.h"
URLStream url("ssid","your password"); // or replace with ICYStream to get metadata
AudioKitStream i2s; // final output of decoded stream
EncodedAudioStream dec(&i2s, new AACDecoderFAAD()); // Decoding stream
StreamCopy copier(dec, url); // copy url to decoder
void playAAC();
Task task("play", 60000, 10, playAAC);
void playAAC(){
// setup i2s
auto config = i2s.defaultConfig(TX_MODE);
i2s.begin(config);
// setup I2S based on sampling rate provided by decoder
dec.begin();
// aac radio
url.begin("http://peacefulpiano.stream.publicradio.org/peacefulpiano.aac","audio/aac");
while(true){
copier.copy();
}
}
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
// alloc > 1024 bytes in PSRAM
const int limit = 1024;
heap_caps_malloc_extmem_enable(limit);
// start on core 1
task.Start(1);
}
void loop(){
delay(1000);
}
Don’t forget to activate PSRAM in the Arduino IDE!
Dependencies
You need to install the following libraries
- arduino-audio-tools
- arduino-libfaad
- arduino-freertos-addons
- arduino-audiokit (when using an AudioKit)
Conclusions
It’s nice to have another option for AAC Decoding, but in the light of the memory requirements, I rather stick to using libhelix.
0 Comments