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 "AudioFileSourceSD.h" #include "AudioGeneratorMP3.h" #include "AudioOutputWithCallback.h" #include "BluetoothA2DPSource.h" #include "AudioTools.h" using namespace audio_tools; 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

sd

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

Source Code

Both the project and the example can be found on Github.


0 Comments

Leave a Reply

Your email address will not be published.