how to calculate the number of Packets for X seconds?
i'm tring to understand the Audio * things for iPhone
currently i'm reading: core audio overview
here i got a question:
from apple example codes:
- (void) cal开发者_JS百科culateSizesFor: (Float64) seconds {
UInt32 maxPacketSize;
UInt32 propertySize = sizeof (maxPacketSize);
AudioFileGetProperty (
audioFileID,
kAudioFilePropertyPacketSizeUpperBound,
&propertySize,
&maxPacketSize
);
static const int maxBufferSize = 0x10000; // limit maximum size to 64K
static const int minBufferSize = 0x4000; // limit minimum size to 16K
if (audioFormat.mFramesPerPacket) {
Float64 numPacketsForTime =
audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds;
[self setBufferByteSize: numPacketsForTime * maxPacketSize];
} else {
// if frames per packet is zero, then the codec doesn't know the
// relationship between packets and time. Return a default buffer size
[self setBufferByteSize:
maxBufferSize > maxPacketSize ? maxBufferSize : maxPacketSize];
}
// clamp buffer size to our specified range
if (bufferByteSize > maxBufferSize && bufferByteSize > maxPacketSize) {
[self setBufferByteSize: maxBufferSize];
} else {
if (bufferByteSize < minBufferSize) {
[self setBufferByteSize: minBufferSize];
}
}
[self setNumPacketsToRead: self.bufferByteSize / maxPacketSize];
}
i understood almost everything, but i just did not get WHY this:
Float64 numPacketsForTime = audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds;
i thought something like this would work:
numPacketsForTime = seconds * packetsPerSecond
so, packetsPerSecond = audioFormat.mSampleRate / audioFormat.mFramesPerPacket
????
could you help me with the maths?
thanks!
Apple's formula is:
audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds;
combining your formulas gives:
seconds * audioFormat.mSampleRate / audioFormat.mFramesPerPacket
which is exactly the same formula with the order changed. Note that:
seconds * audioFormat.mSampleRate / audioFormat.mFramesPerPacket
= seconds * (audioFormat.mSampleRate / audioFormat.mFramesPerPacket)
= (audioFormat.mSampleRate / audioFormat.mFramesPerPacket) * seconds
= audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds
You seem to understand it but from different directions. 8)
精彩评论