Fixes when seeking in an MP3 file (#303)
* separate malloc structures * add desync() for user when randomly seeking in the input file fix corner cases leading to exceptions when seeking * additional test when trying to crash-shake everything with a rotary encoder seeking into the currently played file * use private instead of static * remove temporary debug code
This commit is contained in:
1 parent
93a540da9b
commit
2fb8d887b1
3 files changed
+87
-14
No files matched your search
@@ -35,6 +35,7 @@ class AudioGenerator
|
|||||||
virtual bool loop() { return false; };
|
virtual bool loop() { return false; };
|
||||||
virtual bool stop() { return false; };
|
virtual bool stop() { return false; };
|
||||||
virtual bool isRunning() { return false;};
|
virtual bool isRunning() { return false;};
|
||||||
|
virtual void desync () { };
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
|
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
|
||||||
|
|||||||
+67
-11
@@ -29,11 +29,23 @@ AudioGeneratorMP3::AudioGeneratorMP3()
|
|||||||
buff = NULL;
|
buff = NULL;
|
||||||
nsCountMax = 1152/32;
|
nsCountMax = 1152/32;
|
||||||
madInitted = false;
|
madInitted = false;
|
||||||
preallocateSpace = NULL;
|
|
||||||
preallocateSize = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size)
|
AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size): preallocateSpace(space), preallocateSize(size)
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
file = NULL;
|
||||||
|
output = NULL;
|
||||||
|
buff = NULL;
|
||||||
|
nsCountMax = 1152/32;
|
||||||
|
madInitted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
AudioGeneratorMP3::AudioGeneratorMP3(void *buff, int buffSize, void *stream, int streamSize, void *frame, int frameSize, void *synth, int synthSize):
|
||||||
|
preallocateSpace(buff), preallocateSize(buffSize),
|
||||||
|
preallocateStreamSpace(stream), preallocateStreamSize(streamSize),
|
||||||
|
preallocateFrameSpace(frame), preallocateFrameSize(frameSize),
|
||||||
|
preallocateSynthSpace(synth), preallocateSynthSize(synthSize)
|
||||||
{
|
{
|
||||||
running = false;
|
running = false;
|
||||||
file = NULL;
|
file = NULL;
|
||||||
@@ -41,8 +53,6 @@ AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size)
|
|||||||
buff = NULL;
|
buff = NULL;
|
||||||
nsCountMax = 1152/32;
|
nsCountMax = 1152/32;
|
||||||
madInitted = false;
|
madInitted = false;
|
||||||
preallocateSpace = space;
|
|
||||||
preallocateSize = size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioGeneratorMP3::~AudioGeneratorMP3()
|
AudioGeneratorMP3::~AudioGeneratorMP3()
|
||||||
@@ -109,7 +119,12 @@ enum mad_flow AudioGeneratorMP3::Input()
|
|||||||
|
|
||||||
if (stream->next_frame) {
|
if (stream->next_frame) {
|
||||||
unused = lastBuffLen - (stream->next_frame - buff);
|
unused = lastBuffLen - (stream->next_frame - buff);
|
||||||
memmove(buff, stream->next_frame, unused);
|
if (unused < 0) {
|
||||||
|
desync();
|
||||||
|
unused = 0;
|
||||||
|
} else {
|
||||||
|
memmove(buff, stream->next_frame, unused);
|
||||||
|
}
|
||||||
stream->next_frame = NULL;
|
stream->next_frame = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,6 +140,10 @@ enum mad_flow AudioGeneratorMP3::Input()
|
|||||||
// Can't read any from the file, and we don't have anything left. It's done....
|
// Can't read any from the file, and we don't have anything left. It's done....
|
||||||
return MAD_FLOW_STOP;
|
return MAD_FLOW_STOP;
|
||||||
}
|
}
|
||||||
|
if (len < 0) {
|
||||||
|
desync();
|
||||||
|
unused = 0;
|
||||||
|
}
|
||||||
|
|
||||||
lastBuffLen = len + unused;
|
lastBuffLen = len + unused;
|
||||||
mad_stream_buffer(stream, buff, lastBuffLen);
|
mad_stream_buffer(stream, buff, lastBuffLen);
|
||||||
@@ -132,6 +151,16 @@ enum mad_flow AudioGeneratorMP3::Input()
|
|||||||
return MAD_FLOW_CONTINUE;
|
return MAD_FLOW_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AudioGeneratorMP3::desync ()
|
||||||
|
{
|
||||||
|
audioLogger->printf_P(PSTR("MP3:desync\n"));
|
||||||
|
if (stream) {
|
||||||
|
stream->next_frame = nullptr;
|
||||||
|
stream->this_frame = nullptr;
|
||||||
|
stream->sync = 0;
|
||||||
|
}
|
||||||
|
lastBuffLen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
bool AudioGeneratorMP3::DecodeNextFrame()
|
bool AudioGeneratorMP3::DecodeNextFrame()
|
||||||
{
|
{
|
||||||
@@ -196,6 +225,17 @@ retry:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!DecodeNextFrame()) {
|
if (!DecodeNextFrame()) {
|
||||||
|
if (stream->error == MAD_ERROR_BUFLEN) {
|
||||||
|
// randomly seeking can lead to endless
|
||||||
|
// and unrecoverable "MAD_ERROR_BUFLEN" loop
|
||||||
|
audioLogger->printf_P(PSTR("MP3:ERROR_BUFLEN %d\n"), unrecoverable);
|
||||||
|
if (++unrecoverable >= 3) {
|
||||||
|
unrecoverable = 0;
|
||||||
|
return (running = false);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unrecoverable = 0;
|
||||||
|
}
|
||||||
goto retry;
|
goto retry;
|
||||||
}
|
}
|
||||||
samplePtr = 9999;
|
samplePtr = 9999;
|
||||||
@@ -243,16 +283,32 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
|
|||||||
lastBuffLen = 0;
|
lastBuffLen = 0;
|
||||||
|
|
||||||
// Allocate all large memory chunks
|
// Allocate all large memory chunks
|
||||||
if (preallocateSpace) {
|
if (preallocateStreamSize + preallocateFrameSize + preallocateSynthSize) {
|
||||||
|
if (preallocateSize >= preAllocBuffSize() &&
|
||||||
|
preallocateStreamSize >= preAllocStreamSize() &&
|
||||||
|
preallocateFrameSize >= preAllocFrameSize() &&
|
||||||
|
preallocateSynthSize >= preAllocSynthSize()) {
|
||||||
|
buff = reinterpret_cast<unsigned char *>(preallocateSpace);
|
||||||
|
stream = reinterpret_cast<struct mad_stream *>(preallocateStreamSpace);
|
||||||
|
frame = reinterpret_cast<struct mad_frame *>(preallocateFrameSpace);
|
||||||
|
synth = reinterpret_cast<struct mad_synth *>(preallocateSynthSpace);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
audioLogger->printf_P("OOM error in MP3: Want %d/%d/%d/%d bytes, have %d/%d/%d/%d bytes preallocated.\n",
|
||||||
|
preAllocBuffSize(), preAllocStreamSize(), preAllocFrameSize(), preAllocSynthSize(),
|
||||||
|
preallocateSize, preallocateStreamSize, preallocateFrameSize, preallocateSynthSize);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (preallocateSpace) {
|
||||||
uint8_t *p = reinterpret_cast<uint8_t *>(preallocateSpace);
|
uint8_t *p = reinterpret_cast<uint8_t *>(preallocateSpace);
|
||||||
buff = reinterpret_cast<unsigned char *>(p);
|
buff = reinterpret_cast<unsigned char *>(p);
|
||||||
p += (buffLen+7) & ~7;
|
p += preAllocBuffSize();
|
||||||
stream = reinterpret_cast<struct mad_stream *>(p);
|
stream = reinterpret_cast<struct mad_stream *>(p);
|
||||||
p += (sizeof(struct mad_stream)+7) & ~7;
|
p += preAllocStreamSize();
|
||||||
frame = reinterpret_cast<struct mad_frame *>(p);
|
frame = reinterpret_cast<struct mad_frame *>(p);
|
||||||
p += (sizeof(struct mad_frame)+7) & ~7;
|
p += preAllocFrameSize();
|
||||||
synth = reinterpret_cast<struct mad_synth *>(p);
|
synth = reinterpret_cast<struct mad_synth *>(p);
|
||||||
p += (sizeof(struct mad_synth)+7) & ~7;
|
p += preAllocSynthSize();
|
||||||
int neededBytes = p - reinterpret_cast<uint8_t *>(preallocateSpace);
|
int neededBytes = p - reinterpret_cast<uint8_t *>(preallocateSpace);
|
||||||
if (neededBytes > preallocateSize) {
|
if (neededBytes > preallocateSize) {
|
||||||
audioLogger->printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize);
|
audioLogger->printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize);
|
||||||
|
|||||||
+19
-3
@@ -30,17 +30,31 @@ class AudioGeneratorMP3 : public AudioGenerator
|
|||||||
public:
|
public:
|
||||||
AudioGeneratorMP3();
|
AudioGeneratorMP3();
|
||||||
AudioGeneratorMP3(void *preallocateSpace, int preallocateSize);
|
AudioGeneratorMP3(void *preallocateSpace, int preallocateSize);
|
||||||
|
AudioGeneratorMP3(void *buff, int buffSize, void *stream, int streamSize, void *frame, int frameSize, void *synth, int synthSize);
|
||||||
virtual ~AudioGeneratorMP3() override;
|
virtual ~AudioGeneratorMP3() override;
|
||||||
virtual bool begin(AudioFileSource *source, AudioOutput *output) override;
|
virtual bool begin(AudioFileSource *source, AudioOutput *output) override;
|
||||||
virtual bool loop() override;
|
virtual bool loop() override;
|
||||||
virtual bool stop() override;
|
virtual bool stop() override;
|
||||||
virtual bool isRunning() override;
|
virtual bool isRunning() override;
|
||||||
|
virtual void desync () override;
|
||||||
|
|
||||||
|
static constexpr int preAllocSize () { return preAllocBuffSize() + preAllocStreamSize() + preAllocFrameSize() + preAllocSynthSize(); }
|
||||||
|
static constexpr int preAllocBuffSize () { return ((buffLen + 7) & ~7); }
|
||||||
|
static constexpr int preAllocStreamSize () { return ((sizeof(struct mad_stream) + 7) & ~7); }
|
||||||
|
static constexpr int preAllocFrameSize () { return (sizeof(struct mad_frame) + 7) & ~7; }
|
||||||
|
static constexpr int preAllocSynthSize () { return (sizeof(struct mad_synth) + 7) & ~7; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void *preallocateSpace;
|
void *preallocateSpace = nullptr;
|
||||||
int preallocateSize;
|
int preallocateSize = 0;
|
||||||
|
void *preallocateStreamSpace = nullptr;
|
||||||
|
int preallocateStreamSize = 0;
|
||||||
|
void *preallocateFrameSpace = nullptr;
|
||||||
|
int preallocateFrameSize = 0;
|
||||||
|
void *preallocateSynthSpace = nullptr;
|
||||||
|
int preallocateSynthSize = 0;
|
||||||
|
|
||||||
const int buffLen = 0x600; // Slightly larger than largest MP3 frame
|
static constexpr int buffLen = 0x600; // Slightly larger than largest MP3 frame
|
||||||
unsigned char *buff;
|
unsigned char *buff;
|
||||||
int lastReadPos;
|
int lastReadPos;
|
||||||
int lastBuffLen;
|
int lastBuffLen;
|
||||||
@@ -62,6 +76,8 @@ class AudioGeneratorMP3 : public AudioGenerator
|
|||||||
bool DecodeNextFrame();
|
bool DecodeNextFrame();
|
||||||
bool GetOneSample(int16_t sample[2]);
|
bool GetOneSample(int16_t sample[2]);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int unrecoverable = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in new issue
Block a user