The last couple of days I have spent to add SDIF audio output support to my Arduino AudioTools library and I would like to thank joba-1 for testing the solution.

Together with my ESP32-A2DP Bluetooth library we can build now a Bluetooth receiver that outputs the audio signals as SPDIF with just a few lines of code:

The Arduino Sketch

Here is the Arduino Sketch

#define USE_A2DP
#include "AudioConfigLocal.h"
#include "AudioTools.h"

BluetoothA2DPSink a2dp_sink;
SPDIFStream spdif;

// Write data to SPDIF in callback
void read_data_stream(const uint8_t *data, uint32_t length) {
    spdif.write(data, length);
}

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

  // register callback
  a2dp_sink.set_stream_reader(read_data_stream, false);

  // Start Bluetooth Audio Receiver
  a2dp_sink.set_auto_reconnect(false);
  a2dp_sink.start("a2dp-spdif");

  // setup SPDIF output
  auto cfg = spdif.defaultConfig();
  cfg.pin_data = 23;
  cfg.sample_rate = a2dp_sink.sample_rate();
  cfg.channels = 2;
  cfg.bits_per_sample = 16;
  spdif.begin(cfg);

}

void loop() {
  delay(100);
}

The regular output of the BluetoothA2DPSink goes to I2S, but we can alternatively send the output to a callback methods: This is done with the set_stream_reader(read_data_stream, false). In the callback method we just need to write the audio data to the SPDIFStream.

Before starting the Bluetooth Sink, I deactivate the automatic reconnect – so that I can easily switch my feeding devices.

With the #include “AudioConfigLocal.h” you have the possibility to define some sketch specific configuration. In my example I use this mechanism to define some specific I2S_BUFFER_COUNT and I2S_BUFFER_SIZE values.

Source Code

The (potentially updated) source code can be found on Github

Dependencies

The following libraries need to be installed


16 Comments

Vasilis · 6. July 2024 at 0:27

I want to ask if the Bluetooth receiver that outputs the audio signals as SPDIF, can haddle mp3 songs.

    pschatzmann · 6. July 2024 at 5:07

    I am not sure what you mean exactly: The ESP32 A2DP only supports SBC, but you can play any audio type (including mp3) that your phone’s audio player supports and this will be automatically converted by your phone to SBC.

      Vasilis · 6. July 2024 at 21:18

      Thank you for your answer.
      You got me covered.

jose · 17. July 2023 at 18:34

It would be extremely nice to have high resolution codecs too.
https://github.com/cfint/esp32-a2dp-sink

    jose · 19. July 2023 at 5:16

    Hello
    I have installed ESP-IDF but I haven’t any experience with it. I usually use VS Code + Platformio or Arduino IDE. i’m strugging trying to mix the modified branch with new codecs, platformio.ini and the arduino sketch but I’m far from make it work.
    Is some body interested in this option? Any help will be welcomed.

      pschatzmann · 19. July 2023 at 8:38

      In the Wiki there is a description how to use the A2DP-ESP32 project as a component

        jose · 19. July 2023 at 16:05

        Time to read.
        Thanks.

        jose · 23. July 2023 at 19:42

        I’m testing it in a DevKit v1.
        First of all: Phil, can you add #include “BluetoothA2DPSink.h” to the sketch above as in the sketch stored in the github repository? Thanks.
        “Luckily”, inside mi amp there is a socket with 3.3v and GND labels, but when I try to use them, I can’t find the Bluetooth device and the “ON” led shows a little glitch every second approx. Maybe that socket hasn’t power enough to the ESP32.
        Do you have any idea about to lower the power consumption on the ESP32? During compilation, I can see some references to WiFi @ 2.0.0 and WiFiClientSecure @ 2.0.0. Is WiFi on? Thanks again.

          pschatzmann · 23. July 2023 at 20:03

          Please use the updated examples from github. I never had any power issues by providing the power via usb…

          jose · 23. July 2023 at 20:56

          I forgot to mention: That glitch doesn’t appear when powered via USB.

Cristiano · 20. March 2023 at 4:44

Hello guys!
I was wondering if is possible to use SPDIF as INPUT so I could play my old fashioned Sony Playstation 2 with a Bluetooth Headset

    pschatzmann · 20. March 2023 at 12:01

    No, only output is supported for the time being…

Niklas Are · 22. September 2022 at 0:41

Hi, I have used your ESP audio tools for some time now, with several projects running well. But I have tried this for several days now and have no luck what so ever. The ESP get into a continuous “rebooting” state with the following message in the serial printout. :

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13160
load:0x40080400,len:3036
entry 0x400805e4

assert failed: vQueueDelete queue.c:2140 (pxQueue)

Backtrace:0x400838c1:0x3ffd6ae00x40093d5d:0x3ffd6b00 0x40099201:0x3ffd6b20 0x40094f16:0x3ffd6c50 0x4011443d:0x3ffd6c70 0x401136e0:0x3ffd6c90 0x4011372e:0x3ffd6cb0 0x4010ca7b:0x3ffd6cd0 0x4010506c:0x3ffd6d10 0x40105104:0x3ffd6d30 0x4010d7ef:0x3ffd6d50 0x4012b091:0x3ffd6d70 0x401238e6:0x3ffd6d90 0x401473dd:0x3ffd6de0 0x40145201:0x3ffd6e20 0x401153eb:0x3ffd6e50 0x40115346:0x3ffd6e70 0x40118a51:0x3ffd6e90 0x40114127:0x3ffd6eb0

ELF file SHA256: 0000000000000000

Rebooting…
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13160
load:0x40080400,len:3036
entry 0x400805e4

What have I done wrong?
PS: This is with the copied code from this page..

Best Regards,
Niklas

    pschatzmann · 25. September 2022 at 11:00

    I suggest to open a discussion on Github, but first learn how to use the ESP32 Exception Decoder!

      Jakub Kisielewski · 27. November 2022 at 21:02

      I wasn’t able to get Exception Decoder working (no support for Arduino IDE 2 apparently) but running the Serial Monitor shown little heap being available, somewhere around ~1500. I reduced the buffer constants (count=8, size=256) and it seems to work now.

      Also thanks for your work on the library!! It’s going to save me a lot of time modding my car.

tom · 12. July 2022 at 19:03

Thanks for this – I have been trying to make a Bluetooth device that I can change the Friendly Name of to identify connections for different rooms in a rehearsal suite with a few rooms.
I tried I2S DACs such as the PCM5102 but the audio quality was poor.. this could have been the DACs I was using though. I had some old SDPIF to Analogue devices from years ago, and hooking an ESP32 up to them using your sketch and letting the convertor do the SPDIF to Analogue works brilliantly.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *