如何计算X秒的数据包数量?

时间:2010-10-13 00:47:52

标签: iphone objective-c math iphone-sdk-3.0 audio

我想了解iPhone的Audio *内容

目前正在阅读:core audio overview

我有一个问题:

来自apple示例代码:

- (void) calculateSizesFor: (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];
}

我几乎了解了所有事情,但我只是不明白为什么这样: Float64 numPacketsForTime = audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds;

我觉得这样的事情会起作用: numPacketsForTime = seconds * packetsPerSecond

所以,packetsPerSecond = audioFormat.mSampleRate / audioFormat.mFramesPerPacket ????

你可以帮我解决数学问题吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

Apple的公式是:

audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds;

组合你的公式给出:

seconds * audioFormat.mSampleRate / audioFormat.mFramesPerPacket

与更改订单的公式完全相同。请注意:

seconds * audioFormat.mSampleRate / audioFormat.mFramesPerPacket
= seconds * (audioFormat.mSampleRate / audioFormat.mFramesPerPacket)
= (audioFormat.mSampleRate / audioFormat.mFramesPerPacket) * seconds
= audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds

你似乎从不同的方向理解它。 8)