AECircularBuffer.h File Reference
#import "TPCircularBuffer.h"
#import <AudioToolbox/AudioToolbox.h>

Go to the source code of this file.

Classes

struct  AECircularBuffer
 Circular buffer. More...
 

Macros

#define AECircularBufferCopyAll   UINT32_MAX
 

Functions

BOOL AECircularBufferInit (AECircularBuffer *buffer, UInt32 capacityInFrames, int channelCount, double sampleRate)
 Initialize buffer.
 
void AECircularBufferCleanup (AECircularBuffer *buffer)
 Cleanup buffer.
 
void AECircularBufferClear (AECircularBuffer *buffer)
 Clear buffer.
 
void AECircularBufferSetAtomic (AECircularBuffer *buffer, BOOL atomic)
 Set the atomicity.
 
void AECircularBufferSetChannelCountAndSampleRate (AECircularBuffer *buffer, int channelCount, double sampleRate)
 Change channel count and/or sample rate.
 
UInt32 AECircularBufferGetAvailableSpace (AECircularBuffer *buffer)
 Determine how many much space there is in the buffer.
 
BOOL AECircularBufferEnqueue (AECircularBuffer *buffer, const AudioBufferList *bufferList, const AudioTimeStamp *timestamp, UInt32 frames)
 Copy the audio buffer list onto the buffer.
 
AudioBufferList * AECircularBufferPrepareEmptyAudioBufferList (AECircularBuffer *buffer, UInt32 frameCount, const AudioTimeStamp *timestamp)
 Prepare an empty buffer list, stored on the circular buffer.
 
void AECircularBufferProduceAudioBufferList (AECircularBuffer *buffer)
 Mark next audio buffer list as ready for reading.
 
UInt32 AECircularBufferPeek (AECircularBuffer *buffer, AudioTimeStamp *outTimestamp)
 Determine how many frames of audio are buffered.
 
void AECircularBufferDequeue (AECircularBuffer *buffer, UInt32 *ioLengthInFrames, const AudioBufferList *outputBufferList, AudioTimeStamp *outTimestamp)
 Copy a certain number of frames from the buffer and dequeue.
 
AudioBufferList * AECircularBufferNextBufferList (AECircularBuffer *buffer, AudioTimeStamp *outTimestamp, const AudioBufferList *lastBufferList)
 Access the next stored buffer list.
 
void AECircularBufferConsumeNextBufferList (AECircularBuffer *buffer)
 Consume the next buffer list available for reading.
 
void AECircularBufferConsumeNextBufferListPartial (AECircularBuffer *buffer, UInt32 frames)
 Consume a portion of the next buffer list.
 

Class Documentation

struct AECircularBuffer

Circular buffer.

This utility provides a thread-safe and lock-free FIFO buffer that operates on AudioBufferLists and tracks the AudioTimeStamps corresponding to the audio.

It provides a convenience wrapper to TPCircularBuffer and its AudioBufferList utilities, by avoiding the need to provide an AudioStreamBasicDescription to the various functions, among other things.

It's generally preferable to use this interface instead of TPCircularBuffer if you're just using the TAAE audio format (non-interleaved float).

Class Members
TPCircularBuffer buffer
AudioStreamBasicDescription audioDescription

Macro Definition Documentation

#define AECircularBufferCopyAll   UINT32_MAX

Function Documentation

BOOL AECircularBufferInit ( AECircularBuffer buffer,
UInt32  capacityInFrames,
int  channelCount,
double  sampleRate 
)

Initialize buffer.

Note that the length is advisory only; the true buffer length will be multiples of the device page size (e.g. 4096 bytes)

Parameters
bufferCircular buffer
capacityInFramesAmount of audio frames you wish to store in the buffer
channelCountNumber of channels of audio you'll be working with
sampleRateSample rate of audio, used to work with AudioTimeStamps
Returns
YES on success, NO on buffer allocation failure
void AECircularBufferCleanup ( AECircularBuffer buffer)

Cleanup buffer.

Releases buffer resources.

void AECircularBufferClear ( AECircularBuffer buffer)

Clear buffer.

Resets buffer to original, empty state.

This is safe for use by consumer while producer is accessing the buffer.

void AECircularBufferSetAtomic ( AECircularBuffer buffer,
BOOL  atomic 
)

Set the atomicity.

If you set the atomiticy to false using this method, the buffer will not use atomic operations. This can be used to give the compiler a little more optimisation opportunities when the buffer is only used on one thread.

Important note: Only set this to false if you know what you're doing!

The default value is true (the buffer will use atomic operations)

Parameters
bufferCircular buffer
atomicWhether the buffer is atomic (default true)
void AECircularBufferSetChannelCountAndSampleRate ( AECircularBuffer buffer,
int  channelCount,
double  sampleRate 
)

Change channel count and/or sample rate.

This will cause the buffer to clear any existing audio, and reconfigure to use the new channel count and sample rate. Note that it will not alter the buffer's capacity; if you need to increase capacity to cater to a larger number of channels/frames, then you'll need to cleanup and re-initialize the buffer.

You should only use this on the consumer thread.

Parameters
bufferCircular buffer
channelCountNumber of channels of audio you'll be working with
sampleRateSample rate of audio, used to work with AudioTimeStamps
UInt32 AECircularBufferGetAvailableSpace ( AECircularBuffer buffer)

Determine how many much space there is in the buffer.

Determines the number of frames of audio that can be buffered.

Note: This function should only be used on the producer thread, not the consumer thread.

Parameters
bufferCircular buffer
Returns
The number of frames that can be stored in the buffer
BOOL AECircularBufferEnqueue ( AECircularBuffer buffer,
const AudioBufferList *  bufferList,
const AudioTimeStamp *  timestamp,
UInt32  frames 
)

Copy the audio buffer list onto the buffer.

Parameters
bufferCircular buffer
bufferListBuffer list containing audio to copy to buffer
timestampThe timestamp associated with the buffer, or NULL
framesLength of audio in frames, or AECircularBufferCopyAll to copy the whole buffer
Returns
YES if buffer list was successfully copied; NO if there was insufficient space
AudioBufferList* AECircularBufferPrepareEmptyAudioBufferList ( AECircularBuffer buffer,
UInt32  frameCount,
const AudioTimeStamp *  timestamp 
)

Prepare an empty buffer list, stored on the circular buffer.

Parameters
bufferCircular buffer
frameCountThe number of frames that will be stored
timestampThe timestamp associated with the buffer, or NULL.
Returns
The empty buffer list, or NULL if circular buffer has insufficient space
void AECircularBufferProduceAudioBufferList ( AECircularBuffer buffer)

Mark next audio buffer list as ready for reading.

This marks the audio buffer list prepared using AECircularBufferPrepareEmptyAudioBufferList as ready for reading. You must not call this function without first calling AECircularBufferPrepareEmptyAudioBufferList.

Parameters
bufferCircular buffer
UInt32 AECircularBufferPeek ( AECircularBuffer buffer,
AudioTimeStamp *  outTimestamp 
)

Determine how many frames of audio are buffered.

Note: This function should only be used on the consumer thread, not the producer thread.

Parameters
bufferCircular buffer
outTimestampOn output, if not NULL, the timestamp corresponding to the first audio frame
Returns
The number of frames queued in the buffer
void AECircularBufferDequeue ( AECircularBuffer buffer,
UInt32 *  ioLengthInFrames,
const AudioBufferList *  outputBufferList,
AudioTimeStamp *  outTimestamp 
)

Copy a certain number of frames from the buffer and dequeue.

Parameters
bufferCircular buffer
ioLengthInFramesOn input, the number of frames to consume; on output, the number of frames provided
outputBufferListThe buffer list to copy audio to, or NULL to discard audio.
outTimestampOn output, if not NULL, the timestamp corresponding to the first audio frame returned
AudioBufferList* AECircularBufferNextBufferList ( AECircularBuffer buffer,
AudioTimeStamp *  outTimestamp,
const AudioBufferList *  lastBufferList 
)

Access the next stored buffer list.

Parameters
bufferCircular buffer
outTimestampOn output, if not NULL, the timestamp corresponding to the buffer
lastBufferListIf not NULL, the preceding buffer list on the buffer. The next buffer list after this will be returned; use this to iterate through all queued buffers. If NULL, this function will return the first queued buffer.
Returns
Pointer to the next queued buffer list
void AECircularBufferConsumeNextBufferList ( AECircularBuffer buffer)

Consume the next buffer list available for reading.

Parameters
bufferCircular buffer
void AECircularBufferConsumeNextBufferListPartial ( AECircularBuffer buffer,
UInt32  frames 
)

Consume a portion of the next buffer list.

This will also increment the sample time and host time portions of the timestamp of the buffer list, if present.

Parameters
bufferCircular buffer
framesThe number of frames to consume from the buffer list