From 38d2be66b6d8b0f17bddfb815ce60a307bed7069 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Sat, 24 Sep 2022 00:36:27 +0200 Subject: [PATCH] MP3: Fix incomplete playback at end of stream --- src/SoundFormats/mp3.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/SoundFormats/mp3.cpp b/src/SoundFormats/mp3.cpp index 962e81c..d1cc5ab 100644 --- a/src/SoundFormats/mp3.cpp +++ b/src/SoundFormats/mp3.cpp @@ -24,17 +24,13 @@ SndListHandle Pomme::Sound::LoadMP3AsResource(std::istream& stream) int totalSamples = 0; - while (true) + while (!stream.eof() || !fileBuf.empty()) { - // Refill buffer - if (fileBuf.size() < MINIMP3_BUF_SIZE) + // Refill the buffer as long as data is available in the input stream. + // Once the stream is depleted, keep feeding the buffer to mp3dec until the buffer is empty. + if (fileBuf.size() < MINIMP3_BUF_SIZE + && !stream.eof()) { - if (stream.eof()) - { - // bail - break; - } - auto oldSize = fileBuf.size(); auto toRead = MINIMP3_BUF_SIZE - oldSize; @@ -45,6 +41,11 @@ SndListHandle Pomme::Sound::LoadMP3AsResource(std::istream& stream) fileBuf.resize(oldSize + didRead); } + if (fileBuf.empty()) + { + break; + } + int numDecodedSamples = mp3dec_decode_frame(&context, fileBuf.data(), (int) fileBuf.size(), tempPCM.data(), &frameInfo); if (numDecodedSamples > 0)