pschatzmann / arduino-audio-tools

Arduino Audio Tools (a powerful Audio library not only for Arduino)
GNU General Public License v3.0
1.51k stars 233 forks source link

ESP32 I2S and A2DP not working with A2DP sample rate #1426

Closed duevil closed 8 months ago

duevil commented 8 months ago

Problem Description

I just tried your basic example A2DP to I2S, but my controller just crashes with an IntegerDivideByZero exception. However, when I comment out the line where the I2S sample rate is set to the A2DP sample rate, everything works just fine.

Monitor output:

configsip: 271414342, 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:13260
load:0x40080400,len:3028
entry 0x400805e4
[D] I2SStream.h : 55 - bool audio_tools::I2SStream::begin(audio_tools::I2SConfig)
[D] I2SESP32.h : 47 - bool audio_tools::I2SDriverESP32::begin(audio_tools::I2SConfigESP32)
[D] I2SESP32.h : 161 - bool audio_tools::I2SDriverESP32::begin(audio_tools::I2SConfigESP32, int, int)
[I] AudioTypes.h : 104 - sample_rate: 0
[I] AudioTypes.h : 105 - channels: 2
[I] AudioTypes.h : 106 - bits_per_sample: 16
[I] I2SConfigESP32.h : 90 - rx/tx mode: TX_MODE
[I] I2SConfigESP32.h : 91 - port_no: 0
[I] I2SConfigESP32.h : 92 - is_master: Master
[I] I2SConfigESP32.h : 93 - sample rate: 0
[I] I2SConfigESP32.h : 94 - bits per sample: 16
[I] I2SConfigESP32.h : 95 - number of channels: 2
[I] I2SConfigESP32.h : 96 - signal_type: Digital
[I] I2SConfigESP32.h : 98 - i2s_format: I2S_STD_FORMAT
[I] I2SConfigESP32.h : 100 - auto_clear: true
[I] I2SConfigESP32.h : 102 - use_apll: true
[I] I2SConfigESP32.h : 107 - buffer_count:8
[I] I2SConfigESP32.h : 108 - buffer_size:256
[I] I2SConfigESP32.h : 113 - pin_bck: 14
[I] I2SConfigESP32.h : 115 - pin_ws: 15
[I] I2SConfigESP32.h : 117 - pin_data: 23
[D] I2SESP32.h : 193 - i2s_driver_install
Guru Meditation Error: Core  1 panic'ed (IntegerDivideByZero). Exception was unhandled.

Core  1 register dump:
PC      : 0x400d9a50  PS      : 0x00060730  A0      : 0x800da750  A1      : 0x3ffcafb0  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3ffdf390  A5      : 0x00000000  
A6      : 0x3ffdf390  A7      : 0x3ffc3c0c  A8      : 0x00000100  A9      : 0x00000002  
A10     : 0x00000000  A11     : 0x00000001  A12     : 0x00000000  A13     : 0x3ffc3c0c  
A14     : 0xffffffbf  A15     : 0x00000008  SAR     : 0x00000020  EXCCAUSE: 0x00000006  
EXCVADDR: 0x00000000  LBEG    : 0x40092670  LEND    : 0x4009267b  LCOUNT  : 0x00000000  

Backtrace: 0x400d9a4d:0x3ffcafb0 0x400da74d:0x3ffcb000 0x400d32f2:0x3ffcb040 0x400d34c9:0x3ffcb0c0 0x400d35d5:0x3ffcb130 0x400d3fe9:0x3ffcb1a0 0x400d7936:0x3ffcb270

  #0  0x400d9a4d:0x3ffcafb0 in i2s_calculate_common_clock at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/driver/i2s.c:1172
      (inlined by) i2s_calculate_clock at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/driver/i2s.c:1230
      (inlined by) i2s_set_clk at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/driver/i2s.c:1701
  #1  0x400da74d:0x3ffcb000 in i2s_driver_install at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/driver/i2s.c:2027
  #2  0x400d32f2:0x3ffcb040 in audio_tools::I2SDriverESP32::begin(audio_tools::I2SConfigESP32, int, int) at .pio/libdeps/adafruit_feather_esp32_v2/audio-tools/src/AudioI2S/I2SESP32.h:194 (discriminator 3)
  #3  0x400d34c9:0x3ffcb0c0 in audio_tools::I2SDriverESP32::begin(audio_tools::I2SConfigESP32) at .pio/libdeps/adafruit_feather_esp32_v2/audio-tools/src/AudioI2S/I2SESP32.h:56
  #4  0x400d35d5:0x3ffcb130 in audio_tools::I2SStream::begin(audio_tools::I2SConfigESP32) at .pio/libdeps/adafruit_feather_esp32_v2/audio-tools/src/AudioI2S/I2SStream.h:56 (discriminator 3)
  #5  0x400d3fe9:0x3ffcb1a0 in setup() at src/main.cpp:40
  #6  0x400d7936:0x3ffcb270 in loopTask(void*) at C:/Users/Malte/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:42

Device Description

ESP32 Pico Mini D2 (Adafruit ESP32 Feather V2)

Sketch

/**
 * @file basic-a2dp-audioi2s.ino
 * @brief A2DP Sink with output to I2SStream. This example is of small value
 * since my Bluetooth Library already provides I2S output out of the box.
 *
 * @author Phil Schatzmann
 * @copyright GPLv3
 */

#include "BluetoothA2DPSink.h"
#include "AudioTools.h"

BluetoothA2DPSink a2dp_sink;
I2SStream i2s;

// Write data to I2S
void read_data_stream(const uint8_t *data, uint32_t length) {
    i2s.write(data, length);
}

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

    // 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-i2s");

    // setup output
    auto cfg = i2s.defaultConfig();
    cfg.pin_data = 23;
    cfg.sample_rate = a2dp_sink.sample_rate(); // <- when I comment out this line it's working
    cfg.channels = 2;
    cfg.bits_per_sample = 16;
    cfg.buffer_count = 8;
    cfg.buffer_size = 256;
    i2s.begin(cfg);
}

void loop() { delay(100); }

Other Steps to Reproduce

No response

What is your development environment

PlatformIO and CLion, with the following .ini-file:


platform = espressif32
board = adafruit_feather_esp32_v2
framework = arduino
monitor_speed = 115200
monitor_filters =
    esp32_exception_decoder
board_build.partitions = huge_app.csv
lib_deps =
    https://github.com/pschatzmann/ESP32-A2DP
    https://github.com/pschatzmann/arduino-audio-tools```

### I have checked existing issues, discussions and online documentation

- [X] I confirm I have checked existing issues, discussions and online documentation
pschatzmann commented 8 months ago

Yes, this happens because the sample rate has not been determined by A2DP yet. So the following would be needed to wait for A2DP to be ready. while(!a2dp_sink.is_connected()) delay(1);

I changed the A2DP library to report the default sample rate, if it has not been determined yet: This will prevent this error as well and I havn't seen any other sample rates in A2DP than 44100.