In all my demo sketches so far, I have used just one decoder only, but what if we want to play multiple audio file types ?
To support this scenario, I decided to provide the MultiDecoder class, that lets you register your codecs with the corresponding mime type.
This class works together with my MimeDetector which detects the file type based on the first couple of bytes.
Here is an example sketch that can play aac, mp3 and wav files from the SD drive:
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/AudioLibs/AudioSourceSD.h" // or AudioSourceIdxSD.h
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
#include "AudioTools/AudioCodecs/CodecAACHelix.h"
const char *startFilePath="/multi";
const char* ext="";
AudioSourceSD source(startFilePath, ext, SS);
I2SStream i2s;
MultiDecoder multi;
MP3DecoderHelix mp3;
AACDecoderHelix aac;
WAVDecoder wav;
AudioPlayer player(source, i2s, multi);
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// optionally define you custom pins for the SD drive
// SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
// register supported codecs with their mime type
multi.addDecoder(mp3, "audio/mpeg");
multi.addDecoder(aac, "audio/aac");
multi.addDecoder(wav, "audio/vnd.wave");
// setup output
auto cfg = i2s.defaultConfig(TX_MODE);
// sd_active is setting up SPI with the right SD pins by calling
i2s.begin(cfg);
// setup player
player.setVolume(0.7);
player.begin();
}
void loop() {
player.copy();
}
Dependencies
You need to install the following Arduino libraries:
4 Comments
NIlesh · 28. March 2025 at 12:33
Expected Logic for multi :EncodedAudioStream decoder(i2s, multi); // Decoding stream
MetaDataFilter filter(decoder);
Want similar logic for above would you help to implement not exact but similar like
AudioPlayer player(source, i2s, multi);
MetaDataFilter filter(player);
pschatzmann · 29. March 2025 at 17:55
I don’t quite understand your challange: You can e.g. wrap the MultiDecoder in a MetaDataFilterDecoder…
nilesh darawade · 1. March 2025 at 19:48
Internet radio work fantastic with MultiDecoder multi and over (ESP32S3 I2S(TX)- Esp32 I2S(RX)-Bluetoothspeaker ) with AudioInfo finalAudioPropsInfo(SAMPLE_RATE=44100, CHANNEL_COUNT=1, BITS_PER_SAMPLE=16)
nilesh Darawade · 1. March 2025 at 19:29
As usual its amazing work ; though i would like to inform that the specific “MP3DecoderHelix decoder;AudioPlayer player(source, i2s, decoder); player working with #define SAMPLE_RATE 44100 #define CHANNEL_COUNT 1 #define BITS_PER_SAMPLE 16
const char *startFilePath = “/Music/”;
const char *ext = ” “;//won’t work need to specify “mp3″ with multi
AudioInfo finalAudioPropsInfo(SAMPLE_RATE, CHANNEL_COUNT, BITS_PER_SAMPLE); was fine tuned . 1) but with multi not getting tone properly as it extends over period .2)const char *ext = ” “;//won’t work need to specify “mp3” with multi