Skip to content

A2DP Sink Optimizations

Phil Schatzmann edited this page Apr 14, 2024 · 21 revisions

Different Arduino/IDF versions are using up a different amount of RAM and PROGMEM. The more recent the version the more is needed.

If the sketch does not fit into the available PROGMEM you need to correct the Partition Scheme setting (that can be found in the Tools menu): I recommend to use Huge APP!

With the latest change Espressif is also recommending to use a separate ringbuffer and task to manage the output. This option is using additional RAM so I decide to make this optional.

You can try to fine tune the memory requirements by calling the following methods before the start():

BluetoothA2DPSink

The BluetoothA2DPSink provides the default implementation of a Bluetooth Speaker. The audio is directly written to I2S. You can try to adjust the following parameters

  • set_event_stack_size() - default value is 3K
  • set_event_queue_size() - default 20
  • set_task_priority() - default: configMAX_PRIORITIES - 10

BluetoothA2DPSinkQueued

You can use the queued approach with the BluetoothA2DPSinkQueued class. This way you will need about 33K more RAM.

This can potentially correct some lost audio depending of the combination of number of volume changes, the selected log level and the source Operating System (e.g. IOS seems to be potentially a source of trouble here).

  • set_event_stack_size() - default value is 3K
  • set_event_queue_size() - default 20
  • set_task_priority() - default: configMAX_PRIORITIES - 10
  • set_i2s_stack_size() - default value is 2K
  • set_i2s_ringbuffer_size() - default value is 32 * 1024
  • set_i2s_ringbuffer_prefetch_percent() - default 70
  • set_i2s_task_priority() - default configMAX_PRIORITIES - 3
  • set_i2s_write_size_upto() - default 240 * 6 = 1440
  • set_i2s_ticks() - default 20 (ms)

Legacy I2S

I2S is also providing a buffer that is defined with the help of the buffer size and buffer count. Increasing the buffer makes the signal more stable at the cost of RAM usage and additional lag.

AudioTools

    auto cfg = i2s.defaultConfig();
    cfg.buffer_size = 64;
    cfg.buffer_count = 8;
    //cfg.pin_bck = 14;
    //cfg.pin_ws = 15;
    //cfg.pin_data = 22;
    i2s.begin(cfg);

Legacy I2S

    BluetoothA2DPSink a2dp_sink;
    a2dp_sink.set_dma_buf_count(8);
    a2dp_sink.set_dma_buf_len(64);