MP3: Fix incomplete playback at end of stream

This commit is contained in:
Iliyas Jorio 2022-09-24 00:36:27 +02:00
parent 543b50c1eb
commit 38d2be66b6
1 changed files with 10 additions and 9 deletions

View File

@ -24,17 +24,13 @@ SndListHandle Pomme::Sound::LoadMP3AsResource(std::istream& stream)
int totalSamples = 0; int totalSamples = 0;
while (true) while (!stream.eof() || !fileBuf.empty())
{ {
// Refill buffer // Refill the buffer as long as data is available in the input stream.
if (fileBuf.size() < MINIMP3_BUF_SIZE) // 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 oldSize = fileBuf.size();
auto toRead = MINIMP3_BUF_SIZE - oldSize; auto toRead = MINIMP3_BUF_SIZE - oldSize;
@ -45,6 +41,11 @@ SndListHandle Pomme::Sound::LoadMP3AsResource(std::istream& stream)
fileBuf.resize(oldSize + didRead); fileBuf.resize(oldSize + didRead);
} }
if (fileBuf.empty())
{
break;
}
int numDecodedSamples = mp3dec_decode_frame(&context, fileBuf.data(), (int) fileBuf.size(), tempPCM.data(), &frameInfo); int numDecodedSamples = mp3dec_decode_frame(&context, fileBuf.data(), (int) fileBuf.size(), tempPCM.data(), &frameInfo);
if (numDecodedSamples > 0) if (numDecodedSamples > 0)