sbooth / SFBAudioEngine

A powerhouse of audio functionality for macOS and iOS
https://sbooth.github.io/SFBAudioEngine/
MIT License
552 stars 87 forks source link

OPUS file duration in milliseconds #248

Closed chess92 closed 1 year ago

chess92 commented 1 year ago

I am trying to get the duration of an OPUS file in milliseconds. This is what I have currently:

if let audioFile = try? SFBAudioEngine.AudioFile(readingPropertiesAndMetadataFrom: audioFileLocation) {
    let duration = audioFile.properties.duration
}

The result of audioFile.properties.duration is seconds. With other libraries, this is normally fine because it would be something like 546.342 which could be converted to milliseconds by multiplying by 1000.0. But the above code produces 546.0 which cannot be used to get milliseconds.

Is there another way to get duration for OPUS files?

sbooth commented 1 year ago

One alternative would be to use AudioDecoder and then calculate the duration using frameLength and sample rate.

However, it appears that Opus::Properties has a method lengthInMilliseconds() which is what you're after. I will update SFBOggOpusFile to use this property instead of length() which is just an alias for lengthInSeconds().

sbooth commented 1 year ago

Please try the code in #249 and see if it is more accurate.

chess92 commented 1 year ago

Just tested this and it works great! Thank you.