Scan MP3 for header, avoid multiple burst errors (#764)

MP3 comes in chunks of encoded data started by a header sequence.
Scan for it in the decoder and only pass in data that starts with
this header to avoid the burst errors

Avoids a avalanche of `lost synchronization` error reports when there
is a file error.

Also enable additional sketches on the RP2040/RP2350
This commit is contained in:
Earle F. Philhower, III authored and GitHub committed 2025-10-07 23:22:54 -07:00
1 parent ae685b19e9
commit 39188b81d6
17 files changed
+126 -113

No files matched your search

-1
View File
@@ -74,4 +74,3 @@ protected:
};
#endif
-4
View File
@@ -18,8 +18,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(ESP32) || defined(ESP8266)
#include "AudioFileSourceHTTPStream.h"
AudioFileSourceHTTPStream::AudioFileSourceHTTPStream() {
@@ -156,5 +154,3 @@ uint32_t AudioFileSourceHTTPStream::getSize() {
uint32_t AudioFileSourceHTTPStream::getPos() {
return pos;
}
#endif
+3 -8
View File
@@ -18,14 +18,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(ESP32) || defined(ESP8266)
#pragma once
#include <Arduino.h>
#ifdef ESP32
#include <HTTPClient.h>
#else
#ifdef ESP8266
#include <ESP8266HTTPClient.h>
#else
#include <HTTPClient.h>
#endif
#include "AudioFileSource.h"
@@ -70,7 +69,3 @@ private:
int reconnectDelayMs;
char saveURL[128];
};
#endif
-4
View File
@@ -18,8 +18,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(ESP32) || defined(ESP8266)
#ifdef _GNU_SOURCE
#undef _GNU_SOURCE
#endif
@@ -260,5 +258,3 @@ retry:
icyByteCount += ret;
return read;
}
#endif
+3 -6
View File
@@ -18,14 +18,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(ESP32) || defined(ESP8266)
#pragma once
#include <Arduino.h>
#ifdef ESP32
#include <HTTPClient.h>
#else
#ifdef ESP8266
#include <ESP8266HTTPClient.h>
#else
#include <HTTPClient.h>
#endif
#include "AudioFileSourceHTTPStream.h"
@@ -43,5 +42,3 @@ private:
int icyMetaInt;
int icyByteCount;
};
#endif
+41 -12
View File
@@ -136,19 +136,48 @@ enum mad_flow AudioGeneratorMP3::Input() {
unused = 0;
}
lastReadPos = file->getPos() - unused;
int len = buffLen - unused;
len = file->read(buff + unused, len);
if ((len == 0) && (unused == 0)) {
// Can't read any from the file, and we don't have anything left. It's done....
return MAD_FLOW_STOP;
}
if (len < 0) {
desync();
unused = 0;
}
bool foundHeader = false;
do {
lastReadPos = file->getPos() - unused;
int len = buffLen - unused;
len = file->read(buff + unused, len);
if ((len == 0) && (unused == 0)) {
// Can't read any from the file, and we don't have anything left. It's done....
return MAD_FLOW_STOP;
}
if (len < 0) {
desync();
unused = 0;
}
lastBuffLen = len + unused;
for (int i = 0; i < lastBuffLen; i++) {
if ((buff[i] == 0xff) && ((buff[i + 1] & 0xe0) == 0xe0)) {
// We have a header!
if (i) {
memmove(buff, buff + i, lastBuffLen - i);
lastBuffLen -= i;
}
foundHeader = true;
break;
}
}
if (!foundHeader) {
unused = 0;
} else {
// Try and fill rest of buffer in case there was some shiftage
len = file->read(buff + lastBuffLen, buffLen - lastBuffLen);
if (len < 0) {
desync();
unused = 0;
}
lastBuffLen += len;
if (lastBuffLen < 8) {
return MAD_FLOW_STOP;
}
}
} while (!foundHeader);
lastBuffLen = len + unused;
mad_stream_buffer(stream, buff, lastBuffLen);
return MAD_FLOW_CONTINUE;
+1
View File
@@ -69,6 +69,7 @@ bool AudioOutputPWM::SetOutputModeMono(bool mono) {
bool AudioOutputPWM::begin() {
if (!pwmOn) {
pwm.setPin(doutPin);
pwm.setBuffers(8, 128);
pwm.begin(hertz);
}
pwmOn = true;