In the last Blogs I presented
A File Based Versatile MP3 Player
A Streaming MP3 Player
A Streaming AAC Player with Volume Control

All these examples were using my Arduino Audio Tools Library.

In this final Blog about this topic, I will demonstrate how easy it is to adapt the Sketch from the first Blog and send the output to a Bluetooth Speaker. Here is the Arduino Sketch which will work with a ESP32 Microcontroller:

#define USE_HELIX 
#define USE_A2DP
#define USE_SDFAT

#include "AudioTools.h"
#include "AudioA2DP.h"
#include "AudioCodecs/CodecMP3Helix.h"

using namespace audio_tools;  

const char *startFilePath="/";
const char* ext="mp3";
AudioSourceSdFat source(startFilePath, ext);
A2DPStream out = A2DPStream::instance();  // A2DP output - A2DPStream is a singleton!
MP3DecoderHelix decoder;
AudioPlayer player(source, out, decoder);


void setup() {
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Warning);

  // setup output - We send the output via A2DP to a "LEXON MINO L" Bluetooth Speaker
  auto cfg = out.defaultConfig(TX_MODE);
  cfg.name = "LEXON MINO L";
  out.begin(cfg);

  // setup player
  player.setVolume(0.1);
  player.begin();

}

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

The only difference is that we use the A2DPStream class as output and when we configure A2DP, we need to indicate the Bluetooth Speaker name.
Finally we limit the volume and set it to 0.1.

Audio Format

The Helix decoder provides 2 channels with 16bit data usually at a sample rate of 44100 and this is exactly what is needed by A2DP!

SD Card

Here is the information how to wire the SD card to the ESP32

SD ESP32
CS VSPI-CS0 (GPIO 05)
SCK VSPI-CLK (GPIO 18)
MOSI VSPI-MOSI (GPIO 23)
MISO VSPI-MISO (GPIO 19)
VCC VIN (5V)
GND GND

SD

Dependencies

You need to install the following libraries:

Source Code

The full source code can also be found on Github


5 Comments

Ayush · 28. July 2022 at 15:48

can you please tell how to implement this on an esp32cam? for the esp32cam i need to use the sd library since the pin definitions are completely different to what has been given

    pschatzmann · 28. July 2022 at 16:07

    You can define your custom SPI pins with the begin method: 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);

Pedro75 · 30. December 2021 at 12:05

Hello,
First of all many thanks for the great job you made with this set of libraries.
I tried to compile and run the example “SD MP3 to A2DP” it works well!
I search an example of stream MP3 from URL to A2DP in the examples folders and did not find anyone. Was my searching wrong?
If not could you give some pieces of code to do that?
Wish you a happy new year!

    pschatzmann · 30. December 2021 at 12:14

    https://github.com/pschatzmann/ESP32-A2DP/issues/19

      Pedro75 · 30. December 2021 at 12:57

      What’s a a pity !!!

      By the way I have one questions regarding A2DP:

      is it possible to set the BT hostname programmatically, for example:

      A2DPStream out = A2DPStream::instance(“hostname”);
      This will allow to have several BT sender devices with different name.

      Thanks by advance for your help

Leave a Reply

Avatar placeholder

Your email address will not be published.