My little CoreAudio playback app seems to not be able to correctly calculate the total samples in a file sometimes. Some of the files that I play that have, say, 24000 Hz sample rate have actually around double the number of samples and would be truncated in playback. Sometimes 44100 Hz files, like the one below will have a total sample count that is too low. Any ideas why?
On a related note, is there a better way to find out when the playback of a file is complete(other than checking every 0.1 seconds to see if the playback sample count exceeds the total sample count).
UInt64 nPackets; UInt32 propsize = sizeof(nPackets); XThrowIfError (AudioFileGetProperty(fAudioFile, kAudioFilePropertyAudioDataPacketCount, &propsize, &nPackets), "kAudioFilePropertyAudioDataPacketCount");
double fileDuration = (nPackets * fFileFormat.mFramesPerPacket) / fFileFormat.mSampleRate; NSLog(@"fileDuration %f", fileDuration); NSLog(@"nPackets %d", nPackets);
fTotalSamples = (nPackets * fFileFormat.mFramesPerPacket); printf("nPackets * fFileFormat.mFramesPerPacket = %d * %d\n", nPackets, fFileFormat.mFramesPerPacket); printf("fFileFormat.mSampleRate = %f\n", fFileFormat.mSampleRate); NSLog(@"fTotalSamples = %f", fTotalSamples);
2006-08-21 21:41:31.734 SimplePlayFile[7784] fileDuration 5.836984 2006-08-21 21:41:31.734 SimplePlayFile[7784] nPackets 0 nPackets * fFileFormat.mFramesPerPacket = 0 * 257411 fFileFormat.mSampleRate = 44100.000000 2006-08-21 21:41:31.734 SimplePlayFile[7784] fTotalSamples = 257411.000000
regards, Mike
|