mp3mini (CodecMP3Mini) + A2DPSinkQueued = crash? #706
-
Symptoms: The audio cuts off prematurely while playing MP3. After that, trying to play audio through A2DP will cause the following error:
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/AudioCodecs/CodecMP3Mini.h"
#include "BluetoothA2DPSinkQueued.h"
#include "lamesound_l.h"
MemoryStream mp3;
AudioBoardStream kit(AudioKitEs8388V1);
BluetoothA2DPSinkQueued a2dp_sink(kit);
MP3DecoderMini mini;
EncodedAudioStream out(&kit, &mini);
StreamCopy copier(out, mp3);
void playSound(int index) {
const uint8_t* data = nullptr;
uint32_t data_len = 0;
ESP_LOGI("SoundPlay", "Playing sound %d", index);
switch (index) {
case 1: {
data = connected;
data_len = connected_len;
break;
}
case 2: {
data = disconnected;
data_len = disconnected_len;
break;
}
case 7: {
data = poweron;
data_len = poweron_len;
break;
}
default: {
data = poweron;
data_len = poweron_len;
break;
}
}
ESP_LOGW("SoundPlay", "Init data, len = %u", data_len);
if (data == nullptr || data_len == 0) {
ESP_LOGE("SoundPlay", "Something is wrong, wrong or empty data supplied.!!");
return;
}
mini.setOutput(kit);
mini.begin();
mp3.setValue(data, data_len);
ESP_LOGW("SoundPlay", "Start play...");
mp3.begin();
ESP_LOGW("SoundPlay", "Enter loop");
while (mp3.available()) {
copier.copy();
};
ESP_LOGW("SoundPlay", "Freeing reserved memory for MemoryStream...");
mp3.end();
ESP_LOGW("SoundPlay", "Exit loop, destroy Mini decoder...");
mini.end();
ESP_LOGW("SoundPlay", "Play sound %d ok...", index);
}
void setup() {
Serial.begin(115200);
auto cfg = kit.defaultConfig();
a2dp_sink.start("ESP-BT-DRG");
cfg.sample_rate = 44100;
cfg.sample_rate = a2dp_sink.sample_rate();
ESP_LOGW("Sound", "Sample Rate: %d", cfg.sample_rate);
cfg.channels = 2;
kit.begin(cfg);
out.begin(); // Potential issue?
playSound(7);
ESP_LOGI("System", "Ready");
}
void loop() {
yield();
} Please note: Helix decoder does not show this behavior, but due to the memory requirements it would fail to allocate memory and fail to play the prompt audio. With Helix it does not crash and Bluetooth audio plays normally. Using the regular kind of A2DP Sink is out of the question since it exhibits the issue of constantly dropping packets and playing back stuttering and low-pitched audio (this is not caused by the sampling rate, as confirmed by setting both to use 44100Hz sampling rate.) Enabling PSRAM causes the buffer overflow problem, which also results in the stuttering and low-pitched audio issue. (Perhaps there is a way to force certain tasks to not be handled through PSRAM?) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
mp3mini is not really suited for microcontrollers. |
Beta Was this translation helpful? Give feedback.
mp3mini is not really suited for microcontrollers.
I have already answered why BluetoothA2DPSink is not suited and BluetoothA2DPSinkQueued just makes it worse!