SndAudioBuffer Class Reference

An in-memory audio buffer. More...

#import <SndAudioBuffer.h>

List of all members.

Public Member Functions

(id) - initWithFormat:data:
 Initialization method from a SndFormat and the data it describes.
(id) - initWithBuffer:
 Initialize a buffer with a matching format to the supplied buffer.
(id) - initWithBuffer:range:
 Initialize a buffer with a matching format to the supplied buffer, for a subset range of that audio buffer.
(id) - initWithDataFormat:channelCount:samplingRate:frameCount:
 Initializes the instance to the given format.
(id) - initWithDataFormat:channelCount:samplingRate:duration:
 Initializes this audio buffer with sound data of the given format, channels, sample rate and duration.
(long) - mixWithBuffer:fromStart:toEnd:canExpand:
 Mixes supplied SndAudioBuffer instance with the receiving instance, modifying it.
(long) - mixWithBuffer:
 Mixes supplied SndAudioBuffer instance with the receiving instance, modifying it.
(id) - copyWithZone:
 SndAudioBuffer object copying.
(id) - copyDataFromBuffer:
 Copies the audio buffer NSData instance from the given buffer to the receiver.
(id) - copyBytes:count:format:
 Copies bytes from the void * array given.
(id) - copyBytes:intoRange:format:
 Copies bytes from the void * array given into a sub region of the buffer.
(id) - copyFromBuffer:intoRange:
 Copies from the start of the given buffer into a sub region of the receiving buffer.
(long) - copyFromBuffer:intoFrameRange:fromFrameRange:
 Copies from the given region of the given buffer into a sub region of the receiving buffer.
(void) - fillSNDStreamBuffer:
 Fills the given stream buffer from the receiving audio buffer.
(SndAudioBuffer *) - audioBufferOfChannel:
 Returns a mono SndAudioBuffer instance extracting out the given audio channel of the receiving buffer.
(unsigned long) - lengthInSampleFrames
 Returns the number of sample frames in the audio buffer.
(id) - setLengthInSampleFrames:
 Changes the length of the buffer to newSampleFrameCount sample frames.
(long) - lengthInBytes
 Returns the length of the buffer's sample data in bytes.
(double) - duration
 Returns the length of the audio buffer in seconds.
(double) - samplingRate
 Returns the sampling rate of the audio buffer in Hertz.
(int) - channelCount
 Returns the number of channels in the audio buffer.
(SndSampleFormat) - dataFormat
 Returns the format of the sample data as a SndSampleFormat enumerated type.
(SndFormat- format
 Returns the format (number of frames, channels, dataFormat) of the audio buffer as a SndFormat structure.
(void *) - bytes
 Returns a C pointer to the sample data.
(BOOL) - hasSameFormatAsBuffer:
 compares the data format and length of this buffer to a second buffer.
(id) - zero
 Sets buffer data to zero. Silence.
(id) - zeroFrameRange:
 Zeros (silences) a given range of frames.
(int) - frameSizeInBytes
 Returns the size of a sample frame in bytes.
(NSString *) - description
 Returns a description of the instance as an NSString.
(void) - findMin:max:
 Finds the maximum and minimum sample values in the audio buffer and returns them as floats.
(double) - maximumAmplitude
 Returns the maximum amplitude of the format, that is, the maximum positive value of a sample.
(void) - normalise
 Scale signal to maximum dynamic range of data format.
(void) - scaleBy:
 Scale signal by a given factor.
(float) - sampleAtFrameIndex:channel:
 Retrieves a normalised sample given the frame number (time position) and channel number.
(void) - stereoChannels:
 Retrieve the channels used for stereo presentation (left and right).

Static Public Member Functions

(id) + audioBufferWithDataFormat:channelCount:samplingRate:duration:
 Factory method creating an instance from a set of parameters individually specified.
(id) + audioBufferWithDataFormat:channelCount:samplingRate:frameCount:
 Factory method creating an instance from a set of parameters individually specified.
(id) + audioBufferWithFormat:
 Factory method creating a zeroed buffer of the given format and length.
(id) + audioBufferWithFormat:data:
 Factory method creating an audio buffer in the given format with the audio data.
(id) + audioBufferWithSNDStreamBuffer:
 Factory method creating an SndAudioBuffer instance from a SNDStreamBuffer.
(id) + audioBufferWithSnd:inRange:
 Factory method creating audioBuffers from a region of the given Snd.

Protected Attributes

SndFormat format
NSMutableData * data
signed char * speakerConfiguration


Detailed Description

An in-memory audio buffer.

A SndAudioBuffer represents sound data in contiguous memory. As distinct from a Snd class, it holds typically small chunks of sound data ready for signal processing or performance. Using classes such as SndAudioBufferQueue enables a fragmented arrangement of buffers across memory, typically for processing constraints. SndAudioBuffers are the closest SndKit match to the underlying audio hardware buffer. In addition to holding the sample data, SndAudioBuffer encapsulates sampling rate, number of channels, number of frames and the format of the sample data. A SndAudioBuffer is guaranteed to be uncompressed, therefore it can be processed as a linear buffer of memory.


Member Function Documentation

+ (id) audioBufferWithDataFormat: (SndSampleFormat)  dataFormat
channelCount: (int)  channelCount
samplingRate: (double)  sampleRate
duration: (double)  timeInSeconds 

Factory method creating an instance from a set of parameters individually specified.

Parameters:
dataFormat A SndSampleFormat.
channelCount The number of sound channels per frame.
sampleRate The sampling rate specified in Hertz (Hz).
timeInSeconds Duration is specified in seconds.
Returns:
Returns an autoreleased SndAudioBuffer instance.

+ (id) audioBufferWithDataFormat: (SndSampleFormat)  newDataFormat
channelCount: (int)  newChannelCount
samplingRate: (double)  newSamplingRate
frameCount: (long)  newFrameCount 

Factory method creating an instance from a set of parameters individually specified.

Parameters:
newDataFormat A SndSampleFormat.
newChannelCount The number of sound channels per frame.
newSamplingRate The sampling rate specified in Hertz (Hz).
newFrameCount Duration is specified in frames.
Returns:
Returns an autoreleased SndAudioBuffer instance.

+ (id) audioBufferWithFormat: (SndFormat format  

Factory method creating a zeroed buffer of the given format and length.

Parameters:
format A SndFormat describing the format of the buffer to be created.
Returns:
Returns an autoreleased SndAudioBuffer instance.

+ (id) audioBufferWithFormat: (SndFormat *)  format
data: (void *)  dataPointer 

Factory method creating an audio buffer in the given format with the audio data.

The frameCount member of format MUST match the length of d (in bytes)!

Parameters:
format A pointer to a SndFormat.
dataPointer 
Returns:
Returns an autoreleased SndAudioBuffer instance.

+ (id) audioBufferWithSNDStreamBuffer: (SNDStreamBuffer *)  streamBuffer  

Factory method creating an SndAudioBuffer instance from a SNDStreamBuffer.

Parameters:
streamBuffer A fully populated SNDStreamBuffer structure.
Returns:
Returns an autoreleased SndAudioBuffer instance.

+ (id) audioBufferWithSnd: (Snd *)  snd
inRange: (NSRange)  rangeInFrames 

Factory method creating audioBuffers from a region of the given Snd.

Parameters:
snd 
rangeInFrames An NSRange structure indicating the start and end of the region in samples.
Returns:
Returns an autoreleased SndAudioBuffer instance.

- (id) initWithFormat: (SndFormat *)  format
data: (void *)  sampleData 

Initialization method from a SndFormat and the data it describes.

Can be used to initialize an instance with a buffer of data or with empty (i.e zeroed) data of the length given by format.frameCount if sampleData is NULL.

Parameters:
format A SndFormat. All fields must be valid before calling this method.
sampleData A pointer to the memory holding the sample data in the format described by format.
Returns:
Returns self.

- (id) initWithBuffer: (SndAudioBuffer *)  sndBuffer  

Initialize a buffer with a matching format to the supplied buffer.

Creates a duplicated buffer (with a shallow copy, the data is referenced)

Parameters:
sndBuffer An existing initialised SndAudioBuffer.
Returns:
Returns self.

- (id) initWithBuffer: (SndAudioBuffer *)  sndBuffer
range: (NSRange)  rangeInFrames 

Initialize a buffer with a matching format to the supplied buffer, for a subset range of that audio buffer.

Parameters:
sndBuffer An existing initialised SndAudioBuffer.
rangeInFrames The NSRange of the supplied sndBuffer to initialise the new instance with.
Returns:
Returns self.

- (id) initWithDataFormat: (SndSampleFormat)  dataFormat
channelCount: (int)  channelCount
samplingRate: (double)  samplingRate
frameCount: (long)  newFrameCount 

Initializes the instance to the given format.

Initializes the instance to the given sample data format, number of sound channels, sample rate, and frames (channel independent samples).

Parameters:
dataFormat A SndSampleFormat.
channelCount The number of sound channels.
samplingRate Sample rate in Hertz.
newFrameCount The number of frames, each frame consists of channelCount number of samples.
Returns:
Returns self.

- (id) initWithDataFormat: (SndSampleFormat)  dataFormat
channelCount: (int)  channelCount
samplingRate: (double)  samplingRate
duration: (double)  timeInSeconds 

Initializes this audio buffer with sound data of the given format, channels, sample rate and duration.

The samples are all silence.

Parameters:
dataFormat A SndSampleFormat.
channelCount Number of audio channels, 1 for mono, 2 for stereo, 4 for quad etc..
samplingRate Sample rate in Hertz.
timeInSeconds The size of the buffer in seconds.
Returns:
Returns self.

- (long) mixWithBuffer: (SndAudioBuffer *)  buff
fromStart: (unsigned long)  start
toEnd: (unsigned long)  end
canExpand: (BOOL)  expand 

Mixes supplied SndAudioBuffer instance with the receiving instance, modifying it.

Mixes over the given range of the buffer.

Parameters:
buff The SndAudioBuffer instance to mix.
start The sample frame to begin mixing from.
end The sample frame to end mixing at.
expand If TRUE, receiver is allowed to expand buff in place if required to change data format before mixing, (not sample rate).
Returns:
Returns the number of frames mixed.

- (long) mixWithBuffer: (SndAudioBuffer *)  buff  

Mixes supplied SndAudioBuffer instance with the receiving instance, modifying it.

Mixes the entire buffer onto the original buffer.

Parameters:
buff The SndAudioBuffer instance to mix.
Returns:
Returns the number of frames mixed.

- (id) copyWithZone: (NSZone *)  zone  

SndAudioBuffer object copying.

Returns:
A duplicate SndAudioBuffer with its own, identical data.

- (id) copyDataFromBuffer: (SndAudioBuffer *)  audioBufferToCopyFrom  

Copies the audio buffer NSData instance from the given buffer to the receiver.

Audio buffers must be the same format.

Parameters:
audioBufferToCopyFrom The audio buffer instance to copy the data from.
Returns:
Returns self.

- (id) copyBytes: (void *)  bytes
count: (unsigned int)  count
format: (SndFormat format 

Copies bytes from the void * array given.

Grows the internal NSMutableData object as necessary

Parameters:
bytes The void * array to copy from.
count The number of bytes to copy from the array.
format A SndFormat containing valid channelCount, samplingRate and dataFormat variables.
Returns:
Returns self.

- (id) copyBytes: (void *)  bytes
intoRange: (NSRange)  range
format: (SndFormat format 

Copies bytes from the void * array given into a sub region of the buffer.

Grows the internal NSMutableData object as necessary

Parameters:
bytes The void * array to copy from.
range The start location and number of bytes to copy from the array.
format A SndFormat containing valid channelCount, samplingRate and dataFormat variables.
Returns:
Returns self.

- (id) copyFromBuffer: (SndAudioBuffer *)  sourceBuffer
intoRange: (NSRange)  rangeInSamples 

Copies from the start of the given buffer into a sub region of the receiving buffer.

Grows the internal NSMutableData object as necessary

Parameters:
sourceBuffer The audio buffer to copy from.
rangeInSamples The start location and number of samples to copy to the receiving buffer.
Returns:
Returns self.

- (long) copyFromBuffer: (SndAudioBuffer *)  fromBuffer
intoFrameRange: (NSRange)  bufferRange
fromFrameRange: (NSRange)  fromFrameRange 

Copies from the given region of the given buffer into a sub region of the receiving buffer.

Grows the internal NSMutableData object as necessary

Parameters:
fromBuffer The audio buffer to copy from.
bufferRange The range of fromBuffer in sample frames to copy into.
fromFrameRange The start location and number of samples to copy to the receiving buffer.
Returns:
Returns the number of frames actually copied.

- (void) fillSNDStreamBuffer: (SNDStreamBuffer *)  streamBuffer  

Fills the given stream buffer from the receiving audio buffer.

Manages any conversion from interleaved SndAudioBuffer format to possibly non-interleaved SNDStreamBuffers.

Parameters:
streamBuffer Pointer to the structure that describes the underlying audio API format.

- (SndAudioBuffer *) audioBufferOfChannel: (int)  channel  

Returns a mono SndAudioBuffer instance extracting out the given audio channel of the receiving buffer.

Use this to retrieve a single buffer from a multichannel buffer. Sending this to a mono buffer returns the buffer verbatim.

Parameters:
channel The channel to extract. Must be in the range {0..[self channelCount] - 1}.
Returns:
Returns an autoreleased SndAudioBuffer instance.

- (unsigned long) lengthInSampleFrames  

Returns the number of sample frames in the audio buffer.

A sample frame is a channel independent time position duration, it's duration is the reciprocal of the sample rate.

Returns:
Returns the buffer length in sample frames.

- (id) setLengthInSampleFrames: (unsigned long)  newSampleFrameCount  

Changes the length of the buffer to newSampleFrameCount sample frames.

TODO Need to explain the consequences of setting the length longer or shorter than current. See the source code.

- (long) lengthInBytes  

Returns the length of the buffer's sample data in bytes.

Returns:
buffer length in bytes

- (double) duration  

Returns the length of the audio buffer in seconds.

Returns:
Duration in seconds (as determined by format sampling rate).

- (double) samplingRate  

Returns the sampling rate of the audio buffer in Hertz.

Returns:
sampling rate

- (int) channelCount  

Returns the number of channels in the audio buffer.

Returns:
Number of channels

- (SndSampleFormat) dataFormat  

Returns the format of the sample data as a SndSampleFormat enumerated type.

Returns:
Data format enumerated type.

- (SndFormat) format  

Returns the format (number of frames, channels, dataFormat) of the audio buffer as a SndFormat structure.

Returns:
Returns a SndFormat.

- (void *) bytes  

Returns a C pointer to the sample data.

The user of this method will need to have determined the format of the data in order to correctly traverse it.

Returns:
Pointer to the audio data.

- (BOOL) hasSameFormatAsBuffer: (SndAudioBuffer *)  buff  

compares the data format and length of this buffer to a second buffer.

The number of frames (frameCount) are compared.

Parameters:
buff The SndAudioBuffer to compare to.
Returns:
YES if the buffers have the same format and length, NO if there are any differences in format between buffers.

- (id) zero  

Sets buffer data to zero. Silence.

Returns:
Returns self.

- (id) zeroFrameRange: (NSRange)  frameRange  

Zeros (silences) a given range of frames.

The range must be between 0 and the buffers frame length.

Returns:
Returns self.

- (int) frameSizeInBytes  

Returns the size of a sample frame in bytes.

Returns:
Integer size of sample frame (channels * sample size in bytes)

- (NSString *) description  

Returns a description of the instance as an NSString.

Returns:
NSString describing the audio buffer.

- (void) findMin: (float *)  pMin
max: (float *)  pMax 

Finds the maximum and minimum sample values in the audio buffer and returns them as floats.

Parameters:
pMin Points to a float to store the minimum sample value (between -1.0 and 1.0).
pMax Points to a float to store the maximum sample value (between -1.0 and 1.0).

- (double) maximumAmplitude  

Returns the maximum amplitude of the format, that is, the maximum positive value of a sample.

Returns:
Returns the maximum value of a sample.

- (void) normalise  

Scale signal to maximum dynamic range of data format.

Manages both signals below the dynamic range and in the case of floating point format, exceeding the normalised dynamic range (-1.0 to 1.0). Scales such that no D.C shift occurs across all channels.

- (void) scaleBy: (float)  scaleFactor  

Scale signal by a given factor.

Scales the sample values in the buffer by the given multiplier across all channels.

- (float) sampleAtFrameIndex: (unsigned long)  frameIndex
channel: (int)  channel 

Retrieves a normalised sample given the frame number (time position) and channel number.

Parameters:
frameIndex The frame index, between 0 and the value returned by lengthInSampleFrames less one, inclusive.
channel The channel index, between 0 and the number of channels in the buffer (channelCount - 1) to retrieve a single channel, channelCount to average all channels to a single value.
Returns:
Returns a normalised sample value as a float regardless of the data format.

- (void) stereoChannels: (int *)  leftAndRightChannels  

Retrieve the channels used for stereo presentation (left and right).

This allows multichannel sound buffers to have a stereo version.

Parameters:
leftAndRightChannels An array of at least two integers which will be assigned the channel number of the left speaker in the zeroth element of the array, the channel number of the right speaker in the first element of the array.


Member Data Documentation

- (SndFormat) format [protected]

Holds sound parameters (sample rate, data format, channel count, frame count).

- (NSMutableData*) data [protected]

The audio sample data.

- (signed char*) speakerConfiguration [protected]

Holds the association of channels to speakers. Each element holds a channel number (0 - format.channelCount), -1 for unused and silent. Elements are arranged as described by SndSpeakerPosition above.


The documentation for this class was generated from the following file:

Generated on Sat Dec 5 17:01:16 2009 for MusicKit by  doxygen 1.5.6