mackron / dr_libs

Audio decoding libraries for C/C++, each in a single source file.
Other
1.24k stars 205 forks source link

dr_mp3: support getting Length in ms #235

Closed ericoporto closed 2 years ago

ericoporto commented 2 years ago

I could not figure how to get the length of the mp3 in ms, if this is already supported, disregard this. But if not, I think this is a new feature.

mackron commented 2 years ago

This is easy done. Use drmp3_get_pcm_frame_count() and derive it from the sample rate:

lengthInSeconds = drmp3_get_pcm_frame_count(&mp3) / mp3.sampleRate;

The example above calculates the length in seconds. From there it's a trivial conversion to milliseconds. The sample rate can be retrieved directly from the drmp3 struct.

ericoporto commented 2 years ago

I saw two additional cases, one in al_mp3, where is looks like a header exists with the information ready in xing header. The other is a case where it's a variable rate but this header it seems is not available or the person was not aware, which is reported here https://github.com/lieff/minimp3/issues/54. I was just looking for a method that could check all of these and give me the direct answer.

mackron commented 2 years ago

So dr_mp3 will always iterate over the entire file like in that minimp3 issue you mentioned. That will give you an accurate frame count, but it comes at the expense of a slow processing time. There's no header interrogation going on in dr_mp3, though I would like to support that eventually.

Another potential issue is when the sample rate changes between MP3 frames. The sample rate used by dr_mp3 is always the sample rate of the first MP3 frame. I think this is quite uncommon though? Regardless, I have little interest in supporting that scenario.

ericoporto commented 2 years ago

Thanks for the information. :)