I'm trying to get the time length of an AVAudioPCMBuffer, but I can't seem to do it. I tried doing the following:
func getLength(buffer: AVAudioPCMBuffer) -> TimeInterval {
let framecount = buffer.frameCapacity
let bytesperframe = buffer.format.streamDescription.pointee.mBytesPerFrame
return TimeInterval(bytesperframe * framecount)
}
But this gives a huge number that doesn't seem to be what I'm after.
EDIT: I changed my code to this:
func getLength(buffer: AVAudioPCMBuffer) -> TimeInterval {
let framecount = Float64(buffer.frameCapacity)
let samplerate = buffer.format.streamDescription.pointee.mSampleRate
return TimeInterval(framecount / samplerate)
}
Which seems to work, but seems a bit complicated. Is there another way?
Thanks for the idea Rhythmic Fistman! I changed my code to this now:
func getLength(buffer: AVAudioPCMBuffer) -> TimeInterval {
let framecount = Double(buffer.frameLength)
let samplerate = buffer.format.sampleRate
return TimeInterval(framecount / samplerate)
}
It seems a lot simpler now, and it works. Hopefully I'm doing everything right.