Inherits from NSObject
Declared in SPTCircularBuffer.h

Overview

This class is a simple implementation of a circular buffer, designed to match the behaviour of the iOS SDK.

This class gets around the problem of filling the buffer too far ahead by having a maximum size. Once that size is reached, you cannot add more data without reading some out or clearing it and starting again. When used with the iOS SDK, this isn’t a problem as we can ask the library to re-deliver audio data at a later time.

Properties

length

Returns the amount of data currently in the buffer, in bytes.

@property (readonly) NSUInteger length

Declared In

SPTCircularBuffer.h

maximumLength

Returns the maximum amount of data that the buffer can hold, in bytes.

@property (readonly, nonatomic) NSUInteger maximumLength

Declared In

SPTCircularBuffer.h

Instance Methods

attemptAppendData:ofLength:

Attempt to copy new data into the buffer.

- (NSUInteger)attemptAppendData:(const void *)data ofLength:(NSUInteger)dataLength

Parameters

data

A buffer containing the data to be copied in.

dataLength

The length of the data, in bytes.

Return Value

Returns the amount of data copied into the buffer, in bytes. If this number is smaller than dataLength, only this number of bytes was copied in from the start of the given buffer.

Discussion

Data is copied using the following heuristic:

Declared In

SPTCircularBuffer.h

attemptAppendData:ofLength:chunkSize:

Attempt to copy new data into the buffer.

- (NSUInteger)attemptAppendData:(const void *)data ofLength:(NSUInteger)dataLength chunkSize:(NSUInteger)chunkSize

Parameters

data

A buffer containing the data to be copied in.

dataLength

The length of the data, in bytes.

chunkSize

Ensures the number of bytes copies in is a multiple of this number.

Return Value

Returns the amount of data copied into the buffer, in bytes. If this number is smaller than dataLength, only this number of bytes was copied in from the start of the given buffer.

Discussion

Data is copied using the following heuristic:

  • If dataLength <= (maximumLength - length), copy all data.
  • Otherwise, copy (maximumLength - length) bytes.
  • Number of bytes copied will be rounded to the largest number less than dataLength that can be integrally be divided by chunkSize.

Declared In

SPTCircularBuffer.h

clear

Clears all data from the buffer.

- (void)clear

Declared In

SPTCircularBuffer.h

initWithMaximumLength:

Initialize a new buffer.

- (id)initWithMaximumLength:(NSUInteger)size

Parameters

size

The maximum size of the buffer, in bytes.

Return Value

Returns the newly created SPTCircularBuffer.

Discussion

Initial size will be zero, with a maximum size as provided.

Declared In

SPTCircularBuffer.h

readDataOfLength:intoAllocatedBuffer:

Read data out of the buffer into a pre-allocated buffer.

- (NSUInteger)readDataOfLength:(NSUInteger)desiredLength intoAllocatedBuffer:(void **)outBuffer

Parameters

desiredLength

The desired number of bytes to copy out.

outBuffer

A pointer to a buffer, which must be malloc'ed with at least desiredLength bytes.

Return Value

Returns the amount of data copied into the given buffer, in bytes.

Declared In

SPTCircularBuffer.h