i'm wanting to get the duration of .caf audio files using go. I found a few decoders but their Duration() methods just return 0 with comments perhaps suggesting ways of calculating the duration, does any know if these comments are legit and if so, how I might calculate the duration? I'll accept "it's not possible" as an answer if there's no easy solution.
func (d *Decoder) Duration() time.Duration {
//duration := time.Duration((float64(p.Size) / float64(p.AvgBytesPerSec)) * float64(time.Second))
//duration := time.Duration(float64(p.NumSampleFrames) / float64(p.SampleRate) * float64(time.Second))
return 0
}
one implementation example although i'm happy to use any implementation that's easy to install: https://github.com/mattetti/audio/blob/master/caf/decoder.go
The doc comments in that file you linked is taken directly from Apple's spec. In those docs, you'll find these two important things:
OK, cool, but how many valid frames are there? There are two possible ways to know:
That tells you the duration per packet, but because packets are a constant size, the number of packets is just the
audioDataSize / bytesPerPacket. The latter value is included in the Audio Description. The former is often embedded directly into the file, but it's permitted to be-1with the audio data as the last chunk, in which case its size istotalFileSize - startOfAudioDataIt breaks down like this:
seconds = validFrames / sampleRateframesPerByte = framesPerPacket / bytesPerPacketseconds = framesPerByte * audioDataSizeThe library you've got reads the Audio Description chunk, but I don't think it reads the Packet Table. Also, I'm not confident it calculates the audio data size if the chunk is -1. Maybe it does both/either, in which case, you can use the information above.
If not, you can just parse the file yourself, especially if you only care about the duration. The file starts with a short header, then is split into "chunks" (aka TLVs). Here's a sample implementation you can use as a starting point or to modify the library you linked: