views:

34

answers:

1

Hi guys,

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) 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];
}

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!

+1  A: 

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)

No one in particular
yep, i understand that A*B/C == B/C*A. what i do not understand is why this `audioFormat.mSampleRate / audioFormat.mFramesPerPacket`?, the mpsampleRate will give me the number of 'samples' for each second. the audioFormat.mFramesPerPacket will give me the number of framers per packet?, so what is the relation with packetsPerSecond ????
jhon
Try looking at the units. SampleRate would be frames/seconds. FramesPerPacket would be frames/packets. SampleRate / FramesPerPacket would be (frames/seconds)/(frames/packets). Cancelling the frames and rearranging gives packets/seconds or PacketsPerSecond.
No one in particular