Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes when seeking in an MP3 file #303

Merged
merged 5 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AudioGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AudioGenerator
virtual bool loop() { return false; };
virtual bool stop() { return false; };
virtual bool isRunning() { return false;};
virtual void desync () { };

public:
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
Expand Down
78 changes: 67 additions & 11 deletions src/AudioGeneratorMP3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,30 @@ AudioGeneratorMP3::AudioGeneratorMP3()
buff = NULL;
nsCountMax = 1152/32;
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;
file = NULL;
output = NULL;
buff = NULL;
nsCountMax = 1152/32;
madInitted = false;
preallocateSpace = space;
preallocateSize = size;
}

AudioGeneratorMP3::~AudioGeneratorMP3()
Expand Down Expand Up @@ -109,7 +119,12 @@ enum mad_flow AudioGeneratorMP3::Input()

if (stream->next_frame) {
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;
}

Expand All @@ -125,13 +140,27 @@ enum mad_flow AudioGeneratorMP3::Input()
// 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;
mad_stream_buffer(stream, buff, lastBuffLen);

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()
{
Expand Down Expand Up @@ -196,6 +225,17 @@ bool AudioGeneratorMP3::loop()
}

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;
}
samplePtr = 9999;
Expand Down Expand Up @@ -243,16 +283,32 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
lastBuffLen = 0;

// 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);
buff = reinterpret_cast<unsigned char *>(p);
p += (buffLen+7) & ~7;
p += preAllocBuffSize();
stream = reinterpret_cast<struct mad_stream *>(p);
p += (sizeof(struct mad_stream)+7) & ~7;
p += preAllocStreamSize();
frame = reinterpret_cast<struct mad_frame *>(p);
p += (sizeof(struct mad_frame)+7) & ~7;
p += preAllocFrameSize();
synth = reinterpret_cast<struct mad_synth *>(p);
p += (sizeof(struct mad_synth)+7) & ~7;
p += preAllocSynthSize();
int neededBytes = p - reinterpret_cast<uint8_t *>(preallocateSpace);
if (neededBytes > preallocateSize) {
audioLogger->printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize);
Expand Down
24 changes: 20 additions & 4 deletions src/AudioGeneratorMP3.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@ class AudioGeneratorMP3 : public AudioGenerator
public:
AudioGeneratorMP3();
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 bool begin(AudioFileSource *source, AudioOutput *output) override;
virtual bool loop() override;
virtual bool stop() 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:
void *preallocateSpace;
int preallocateSize;
void *preallocateSpace = nullptr;
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;
int lastReadPos;
int lastBuffLen;
Expand All @@ -62,6 +76,8 @@ class AudioGeneratorMP3 : public AudioGenerator
bool DecodeNextFrame();
bool GetOneSample(int16_t sample[2]);

private:
int unrecoverable = 0;
};

#endif
Expand Down