• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting duration of an audio file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting duration of an audio file


  • Subject: Re: Getting duration of an audio file
  • From: Doug Wyatt <email@hidden>
  • Date: Wed, 27 Jul 2005 16:05:19 -0700

On Jul 27, 2005, at 15:49, Peter Ammon wrote:

Here's the code I'm using to get the duration (in seconds) of an audio file:

SInt64 packetCount;
UInt32 dataSize = sizeof packetCount;
OSStatus result = AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataPacketCount, &dataSize, &packetCount);
if (result==noErr) {
double duration = ((double)packetCount * myASBD.mFramesPerPacket) / myASBD.mSampleRate;
}


Here, myASBD is the AudioStreamBasicDescription for audioFile. Is this code correct? If so, is it exact for variable-bit rate files, or is the calculated duration an estimate? It appears to be exact, but I'd rather be sure.

In our SDK, AudioFileTools project, there's the afinfo tool. It calculates the duration as follows:


------
UInt64 dataPacketCount;
UInt64 totalFrames;
totalFrames = 0;
propertySize = sizeof(dataPacketCount);
err = AudioFileGetProperty(afid, kAudioFilePropertyAudioDataPacketCount, &propertySize, &dataPacketCount);
if (err) {
fprintf(stderr, "AudioFileGetProperty kAudioFilePropertyAudioDataPacketCount failed for '%s'\n", argv[i]);
} else {
printf("audio packets: %llu\n", dataPacketCount);
if (asbd.mFramesPerPacket)
totalFrames = asbd.mFramesPerPacket * dataPacketCount;
}


------
So far it's identical to what you're doing ... but there's more. The above will fail for a format with variable frames per packet (we don't currently have codecs for such formats directly, but it's possible ...)
------
AudioFilePacketTableInfo pti;
propertySize = sizeof(pti);
err = AudioFileGetProperty(afid, kAudioFilePropertyPacketTableInfo, &propertySize, &pti);
if (err == noErr) {
totalFrames = pti.mNumberValidFrames;
printf("audio %qd valid frames + %ld priming + %ld remainder = %qd\n", pti.mNumberValidFrames, pti.mPrimingFrames, pti.mRemainderFrames, pti.mNumberValidFrames + pti.mPrimingFrames + pti.mRemainderFrames);
}


if (totalFrames != 0)
printf("duration: %.4f seconds\n", (double)totalFrames / (double)asbd.mSampleRate );
// otherwise, we don't know
------


kAudioFilePropertyPacketTableInfo is implemented by CAF files. The AAC encoder (to take one common example) generates more frames of audio than it is given as input, both in terms of a startup latency and rounding up to a packet size. The packet table info allows you to recover the true length of the file. If you create a CAF file containing AAC, using afconvert (also from the SDK), the packet table header will be filled out, and afinfo will show you that the AAC file has exactly the same number of valid frames (as opposed to leading/ remainder frames) as the source PCM file.

Doug

--
Doug Wyatt
Core Audio, Apple

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Getting duration of an audio file (From: Peter Ammon <email@hidden>)

  • Prev by Date: Getting duration of an audio file
  • Next by Date: Data Source Confusion
  • Previous by thread: Getting duration of an audio file
  • Next by thread: Data Source Confusion
  • Index(es):
    • Date
    • Thread