Skip to content

Commit

Permalink
Fix audio dropouts in BT-Source mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tueddy committed Dec 27, 2024
1 parent 218f245 commit d7c3579
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 4 additions & 20 deletions src/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
11 changes: 6 additions & 5 deletions src/Bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit d7c3579

Please sign in to comment.