Skip to content

Commit 0f315f5

Browse files
committed
Fix mp3 length
1 parent 0f93ca3 commit 0f315f5

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

modules/yup_audio_formats/formats/yup_Mp3AudioFormat.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,27 @@ Mp3AudioFormatReader::Mp3AudioFormatReader (InputStream* sourceStream)
192192
{
193193
sampleRate = mp3.sampleRate;
194194
bitsPerSample = 16; // MP3 always outputs 16-bit samples
195-
lengthInSamples = mp3.totalPCMFrameCount;
196195
numChannels = mp3.channels;
197196
usesFloatingPointData = false; // MP3 outputs 16-bit integer samples
198197

198+
const drmp3_uint64 unknownFrameCount = (drmp3_uint64) -1;
199+
drmp3_uint64 totalFrames = mp3.totalPCMFrameCount;
200+
201+
if (totalFrames == 0 || totalFrames == unknownFrameCount)
202+
{
203+
const auto scannedFrames = drmp3_get_pcm_frame_count (&mp3);
204+
if (scannedFrames != 0 && scannedFrames != unknownFrameCount)
205+
totalFrames = scannedFrames;
206+
207+
drmp3_seek_to_pcm_frame (&mp3, 0);
208+
currentPCMFrame = 0;
209+
}
210+
211+
if (totalFrames == 0 || totalFrames == unknownFrameCount || totalFrames > 0x7fffffffffffffffULL)
212+
lengthInSamples = 0;
213+
else
214+
lengthInSamples = (int64) totalFrames;
215+
199216
// Allocate temp buffer for reading
200217
const auto bytesPerFrame = numChannels * (bitsPerSample / 8);
201218
tempBufferSize = bytesPerFrame * 4096;
@@ -450,7 +467,14 @@ const String& Mp3AudioFormat::getFormatName() const
450467

451468
Array<String> Mp3AudioFormat::getFileExtensions ([[maybe_unused]] Mode handleMode) const
452469
{
470+
if (handleMode == Mode::forReading)
471+
return { ".mp3" };
472+
473+
#if YUP_MODULE_AVAILABLE_hmp3_library
453474
return { ".mp3" };
475+
#else
476+
return {};
477+
#endif
454478
}
455479

456480
std::unique_ptr<AudioFormatReader> Mp3AudioFormat::createReaderFor (InputStream* sourceStream)

0 commit comments

Comments
 (0)