Audio Library Programming Guide - Buffers


Buffer Formats

As mentioned earlier, data is transferred from a sound device to the AFSoundcard's delegate with:


- (void)inputReceivedFromSoundcard:(AFSoundcard*)soundcard buffers:(float**)buffers numberOfBuffers:(int)n samples:(int)m ;


An output AFSoundcard requests data from its delegate with:


- (Boolean)outputNeededForSoundcard:(AFSoundcard*)soundcard buffers:(float**)buffers numberOfBuffers:(int)n samples:(int)m ;


Aditionally, data can be pushed to an output AFSoundcard by calling it with:


- (void)pushBuffers:(float**)buffers numberOfBuffers:(int)n samples:(int)samples rateScalar:(Float64)rateScalar ;


If you are using the "push" model with -pushBuffers, as shown in the example here, you can either leave out the -outputNeededForSoundcard method, or you can make that method immediately return false.

When an
AFSoundcard calls its delegate with the -inputReceivedFromSoundcard or the -outputNeededForSoundcard method, it passes along its own reference in the first argument. This allows the delegate to serve more than one AFSoundcard.


Buffer Pointers

A list of buffer pointers are passed between the delegate and the
AFSoundcard. The number of pointers should agree with the value of numberOfBuffers and also to the number of channels that AFSoundcard is programmed to use. The number of channels can be less than the number of physical channels of the audio device. Which channels are represented by the buffers are determined by a channel mask.

In the case of the the
-inputReceivedFromSoundcard or the -outputNeededForSoundcard calls, the buffers are allocated by the AFSoundcard. The delegate should simply copy data from or to these buffers.

In the case of
-pushBuffers, the delegate is the one that owns the buffers. AFSoundcard will copy data that it needs from the buffers that are pushed to it.


Buffer Lengths

An
AFSoundcard transfers data to and from its delegate in fixed length buffers. The buffer lengths that is sent to the delegate can be set to a different value from the buffer lengths that the physical device uses, viz,


- (int)bufferLength ;
- (void)setBufferLength:(int)samples ;


AFSoundcard will manage any rebuffering for you if the bufferLength is different from the buffer length used by the device. The default length is 512 samples.