How to get durationInSec for audio file?


#1

Hi, jules
How are you?
There is no data member for file duraiton in audioFormatReader.
There is int64 AudioFormatReader::lengthInSamples and numChannels.

Is it ok for calculating the audio file duration in the following calculation :
durationInSec = AudioFormatReader::lengthInSamples / (AudioFormatReader::numchannels * AudioFormatReader::sampleRate )

My questions is :
Can all the formats supported in juce lib be computed in this way for durationInSec?
Does AudioFormatReader::lengthInSamples contains all the sample from all the channels?

Cheers!
Leon


#2

You seem to be overcomplicating things… lengthInSamples has nothing to do with how many channels there are - it’s just the length of the file. So the length in seconds is just lengthInSamples/sampleRate.


#3

hi, jules
You are right.
I asked this because I meet an issue about inaccurate ogg file duration problem.
In your ogg code, you use ov_pcm_total API to get the ogg file duration in sample. I found the duration returned from this api is wrong. So it crashes the jucedemo.
The duration should be 1.86 secs, however, the ov_pcm_total api returns 4.8 sec.
The way of using ov_pcm_total to get the ogg duration is buggy, and can not be used for all the ogg files.

Do you have any ideas to solve that?

Thanks!
Leon


#4

I had no idea it had any problems, all the ogg files that I’ve tried have been fine. Maybe you could send me a file that crashes it?


#5

Hi, jules
How are you?
The issue is the part of ogg file is malformed, and can not be decoded properly. We need to handle it instead of crashing.
The first 1.86 secs are good, and after that the file is malformed. The return from ov_read_float() < 0 after 1.86 secs in read() function .
const int samps = ov_read_float (&ovFile, &dataIn, numToRead, &bitStream);
if (samps == 0)
break;

   issue is :  samps < 0;

Thanks!
Leon


#6

Ok, so I guess just changing it to

would sort it out?


#7