Please note that a lot has changed since I published this Blog! Tought it might be possible to make this work again, you are advised to use the current API with this updated example!
The processing of files with the help of the ESP8266Audio is a little bit more involved. However it allows to process different audio file types from different sources. Please consult the project for further details.
Streaming of MP3 Files on a SD card
The Sketch
Here is the Arduino Sketch that you can use with an I2S audio source:
#include <SPI.h>
#include <SD.h>
#include "AudioTools.h"
#include "AudioLibs/AudioESP8266.h"
#include "AudioFileSourceSD.h"
#include "AudioGeneratorMP3.h"
#include "BluetoothA2DP.h"
const int sd_ss_pin = 5;
const char* fileName = "/audio.mp3";
BluetoothA2DPSource a2dp_source;
AudioFileSourceSD *file;
AudioGeneratorMP3 *mp3;
AudioOutputWithCallback *out;
// callback used by A2DP to provide the sound data
int32_t get_sound_data(Channels* data, int32_t len) {
return out == nullptr ? 0 : out->read(data, len);
}
// Arduino Setup
void setup(void) {
Serial.begin(115200);
audioLogger = &Serial;
// Setup Audio
file = new AudioFileSourceSD();
mp3 = new AudioGeneratorMP3();
out = new AudioOutputWithCallback(512,5);
// Open MP3 file and play it
SD.begin(sd_ss_pin);
if (file->open(fileName)) {
// start the bluetooth
Serial.println("starting A2DP...");
a2dp_source.start("MyMusic", get_sound_data);
// start to play the file
mp3->begin(file, out);
Serial.printf("Playback of '%s' begins...\n", fileName);
} else {
Serial.println("Can't find .mp3 file");
}
}
// Arduino loop - repeated processing
void loop() {
if (mp3->isRunning()) {
if (!mp3->loop()) mp3->stop();
} else {
Serial.println("MP3 done");
delay(10000);
}
}
In this Sketch we use the ESP8266Audio library to read the audio.mp3 file from a SD drive. The output is pushed into a temporary buffer. The A2DP Callback then just consumes this buffered data. This is done with the help of the AudioOutputWithCallback class.
The Device
The SD module is connected with the help of the SPI bus
Pins
SD | ESP32 |
---|---|
VCC | 5V |
GND | GND |
CS | CS GP5 |
SCK | SCK GP18 |
MOSI | MOSI GP23 |
MISO | MISO GP19 |
2 Comments
Anonymous · 27. September 2021 at 19:22
In your example, I had to replace “AudioOutputWithCallback.h” with “AudioESP8266.h”, otherwise AudioOutputWithCallback() can’t be fount
pschatzmann · 27. September 2021 at 20:54
Thanks for your feed back.