From d7c35799648ebcc01b12613e382db270422d47a5 Mon Sep 17 00:00:00 2001 From: tueddy Date: Fri, 27 Dec 2024 18:16:10 +0100 Subject: [PATCH] Fix audio dropouts in BT-Source mode avoid WDT reset & give audio/other tasks more CPU time Send all the audio data to the ring buffer in a single call, not every single sample. - arduino-audio-tools v1.0.1 - ESP32-A2DP v1.8.5 --- platformio.ini | 4 ++-- src/AudioPlayer.cpp | 24 ++++-------------------- src/Bluetooth.cpp | 11 ++++++----- src/Bluetooth.h | 2 +- 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/platformio.ini b/platformio.ini index ef46a352..816f24a4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,8 +37,8 @@ lib_deps = https://github.com/FastLED/FastLED.git#3b63b17 ; v3.9.7 https://github.com/mathieucarbou/ESPAsyncWebServer.git#079446b ; v3.4.5 https://github.com/bblanchon/ArduinoJson.git#f9fe855 ; v7.2.1 - https://github.com/pschatzmann/arduino-audio-tools.git#9d63174 ; v0.9.8 - https://github.com/pschatzmann/ESP32-A2DP.git#bb5bc2f + https://github.com/pschatzmann/arduino-audio-tools.git#2d5d09a ; v1.0.1 + https://github.com/pschatzmann/ESP32-A2DP.git#5b50fe0 ; v1.8.5 https://github.com/Arduino-IRremote/Arduino-IRremote.git#610d116 ; v4.4.1 https://github.com/kkloesener/MFRC522_I2C.git#121a27e https://github.com/tueddy/rfid.git#caa3e6d ; avoid warnings, fork from https://github.com/miguelbalboa/rfid.git#0ff12a1 diff --git a/src/AudioPlayer.cpp b/src/AudioPlayer.cpp index 952b8a9a..a33b028e 100644 --- a/src/AudioPlayer.cpp +++ b/src/AudioPlayer.cpp @@ -900,12 +900,7 @@ void AudioPlayer_Task(void *parameter) { // we are idle, update timeout so that we do not get a spurious error when launching into a playlist playbackTimeoutStart = millis(); } - if ((System_GetOperationMode() == OPMODE_BLUETOOTH_SOURCE) && audio->isRunning()) { - // do not delay here, audio task is time critical in BT-Source mode - } else { - vTaskDelay(portTICK_PERIOD_MS * 1); - } - // esp_task_wdt_reset(); // Don't forget to feed the dog! + vTaskDelay(portTICK_PERIOD_MS * 1); #ifdef DONT_ACCEPT_SAME_RFID_TWICE_ENABLE static uint8_t resetOnNextIdle = false; @@ -1446,19 +1441,8 @@ void audio_eof_speech(const char *info) { gPlayProperties.currentSpeechActive = false; } +// bitsPerSample always 16 +// channels always 2 void audio_process_i2s(int16_t *outBuff, uint16_t validSamples, uint8_t bitsPerSample, uint8_t channels, bool *continueI2S) { - - uint32_t sample; - for (int i = 0; i < validSamples; i++) { - if (channels == 2) { - // stereo - sample = (uint16_t(outBuff[i * 2]) << 16) | uint16_t(outBuff[i * 2 + 1]); - *continueI2S = !Bluetooth_Source_SendAudioData(&sample); - } - if (channels == 1) { - // mono - sample = (uint16_t(outBuff[i]) << 16) | uint16_t(outBuff[i]); - *continueI2S = !Bluetooth_Source_SendAudioData(&sample); - } - } + *continueI2S = !Bluetooth_Source_SendAudioData(outBuff, validSamples); } diff --git a/src/Bluetooth.cpp b/src/Bluetooth.cpp index ce27f1d8..9533e519 100644 --- a/src/Bluetooth.cpp +++ b/src/Bluetooth.cpp @@ -132,6 +132,8 @@ int32_t get_data_channels(Frame *frame, int32_t channel_len) { }; vRingbufferReturnItem(audioSourceRingBuffer, (void *) sampleBuff); }; + // avoid WDT reset & give audio/other tasks some CPU time + vTaskDelay(portTICK_PERIOD_MS * 1); return channel_len; }; #endif @@ -186,7 +188,6 @@ void Bluetooth_Init(void) { ; // do nothing } a2dp_sink = new BluetoothA2DPSink(i2s); - a2dp_sink->set_rssi_calldoxback(rssi); #else a2dp_sink = new BluetoothA2DPSink(); i2s_pin_config_t pin_config = { @@ -199,8 +200,8 @@ void Bluetooth_Init(void) { .data_in_num = I2S_PIN_NO_CHANGE }; a2dp_sink->set_pin_config(pin_config); - a2dp_sink->set_rssi_callback(rssi); #endif + a2dp_sink->set_rssi_callback(rssi); a2dp_sink->activate_pin_code(false); if (gPrefsSettings.getBool("playMono", false)) { a2dp_sink->set_mono_downmix(true); @@ -325,11 +326,11 @@ void Bluetooth_SetVolume(const int32_t _newVolume, bool reAdjustRotary) { #endif } -bool Bluetooth_Source_SendAudioData(uint32_t *sample) { +bool Bluetooth_Source_SendAudioData(int16_t *outBuff, uint16_t validSamples) { #ifdef BLUETOOTH_ENABLE // send audio data to ringbuffer - if ((System_GetOperationMode() == OPMODE_BLUETOOTH_SOURCE) && (a2dp_source) && a2dp_source->is_connected()) { - return (pdTRUE == xRingbufferSend(audioSourceRingBuffer, sample, sizeof(uint32_t), (TickType_t) portMAX_DELAY)); + if ((System_GetOperationMode() == OPMODE_BLUETOOTH_SOURCE) && (a2dp_source) && (validSamples > 0) && a2dp_source->is_connected()) { + return (pdTRUE == xRingbufferSend(audioSourceRingBuffer, outBuff, sizeof(uint32_t) * validSamples, (TickType_t) portMAX_DELAY)); } else { return false; } diff --git a/src/Bluetooth.h b/src/Bluetooth.h index c6d6d55b..020b835e 100644 --- a/src/Bluetooth.h +++ b/src/Bluetooth.h @@ -13,5 +13,5 @@ void Bluetooth_PreviousTrack(void); // Support for AVRC Commands starting from ESP32 Release 2.0.0 void Bluetooth_SetVolume(const int32_t _newVolume, bool reAdjustRotary); -bool Bluetooth_Source_SendAudioData(uint32_t *sample); +bool Bluetooth_Source_SendAudioData(int16_t *outBuff, uint16_t validSamples); bool Bluetooth_Device_Connected();