ADC Home > Reference Library > Reference > Audio > Core Audio > Audio Toolbox Framework Reference

 


AudioConverter.h

Includes:

Introduction

Functions for performing audio format conversions.

Discussion

AudioConverters convert between various linear PCM and compressed audio formats. Supported transformations include:



A single AudioConverter may perform more than one of the above transformations.



Functions

AudioConverterConvertBuffer
Converts data from an input buffer to an output buffer.
AudioConverterDispose
Destroy an AudioConverter.
AudioConverterFillBuffer
Converts data supplied by an input callback function.
AudioConverterFillComplexBuffer
Converts data supplied by an input callback function, supporting non-interleaved and packetized formats.
AudioConverterGetProperty
Returns an AudioConverter property value.
AudioConverterGetPropertyInfo
Returns information about an AudioConverter property.
AudioConverterNew
Create a new AudioConverter.
AudioConverterNewSpecific
Create a new AudioConverter using specific codecs.
AudioConverterReset
Reset an AudioConverter
AudioConverterSetProperty
Sets an AudioConverter property value.

AudioConverterConvertBuffer


Converts data from an input buffer to an output buffer.

extern OSStatus AudioConverterConvertBuffer(
    AudioConverterRef inAudioConverter, 
    UInt32 inInputDataSize, 
    const void*inInputData, 
    UInt32*ioOutputDataSize, 
    void*outOutputData);  
Parameters
inAudioConverter
The AudioConverter to use.
inInputDataSize
The size of the buffer inInputData.
inInputData
The input audio data buffer.
ioOutputDataSize
On entry, the size of the buffer outOutputData. On exit, the number of bytes written to outOutputData.
outOutputData
The output data buffer.
Return Value

Produces a buffer of output data from an AudioConverter, using the supplied input buffer.

WARNING: this function will fail for any conversion where there is a variable relationship between the input and output data buffer sizes. This includes sample rate conversions and most compressed formats. In these cases, use AudioConverterFillBuffer. Generally this function is only appropriate for PCM-to-PCM conversions where there is no sample rate conversion.

Availability
Introduced in Mac OS X v10.1.

AudioConverterDispose


Destroy an AudioConverter.

extern OSStatus AudioConverterDispose(
    AudioConverterRef inAudioConverter);  
Parameters
inAudioConverter
The AudioConverter to dispose.
Return Value

An OSStatus result code.

Availability
Introduced in Mac OS X v10.1.

AudioConverterFillBuffer


Converts data supplied by an input callback function.

extern OSStatus AudioConverterFillBuffer(
    AudioConverterRef inAudioConverter, 
    AudioConverterInputDataProc inInputDataProc, 
    void*inInputDataProcUserData, 
    UInt32*ioOutputDataSize, 
    void*outOutputData);  
Parameters
inAudioConverter
The AudioConverter to use.
inInputDataProc
A callback function which supplies the input data.
inInputDataProcUserData
A value for the use of the callback function.
ioOutputDataSize
On entry, the size of the buffer pointed to by outOutputData. On exit, the number of bytes written to outOutputData
outOutputData
The buffer into which the converted data is written.
Return Value

An OSStatus result code.

Discussion

Produces a buffer of output data from an AudioConverter. The supplied input callback function is called whenever necessary.

NOTE: AudioConverterFillBuffer is limited to simpler conversions that do not involve multiple deinterleaved buffers or packetized formats. Use AudioConverterFillComplexBuffer for those cases.

Availability
Introduced in Mac OS X v10.1.

AudioConverterFillComplexBuffer


Converts data supplied by an input callback function, supporting non-interleaved and packetized formats.

extern OSStatus AudioConverterFillComplexBuffer(
    AudioConverterRef inAudioConverter, 
    AudioConverterComplexInputDataProc inInputDataProc, 
    void*inInputDataProcUserData, 
    UInt32*ioOutputDataPacketSize, 
    AudioBufferList*outOutputData, 
    AudioStreamPacketDescription*outPacketDescription) ;  
Parameters
inAudioConverter
The AudioConverter to use.
inInputDataProc
A callback function which supplies the input data.
inInputDataProcUserData
A value for the use of the callback function.
ioOutputDataPacketSize
On entry, the capacity of outOutputData expressed in packets in the converter's output format. On exit, the number of packets of converted data that were written to outOutputData.
outOutputData
The converted output data is written to this buffer.
outPacketDescription
If non-null, and the converter's output uses packet descriptions, then packet descriptions are written to this array. It must point to a memory block capable of holding *ioOutputDataPacketSize packet descriptions. (See AudioFormat.h for ways to determine whether an audio format uses packet descriptions).
Return Value

An OSStatus result code.

Discussion

Produces a buffer list of output data from an AudioConverter. The supplied input callback function is called whenever necessary.

Availability
Introduced in Mac OS X v10.2

AudioConverterGetProperty


Returns an AudioConverter property value.

extern OSStatus AudioConverterGetProperty(
    AudioConverterRef inAudioConverter, 
    AudioConverterPropertyID inPropertyID, 
    UInt32*ioPropertyDataSize, 
    void*outPropertyData);  
Parameters
inAudioConverter
The AudioConverter to query.
inPropertyID
The property to fetch.
ioPropertyDataSize
On entry, the size of the memory pointed to by outPropertyData. On successful exit, the size of the property value.
outPropertyData
On exit, the property value.
Return Value

An OSStatus result code.

Availability
Introduced in Mac OS X v10.1.

AudioConverterGetPropertyInfo


Returns information about an AudioConverter property.

extern OSStatus AudioConverterGetPropertyInfo(
    AudioConverterRef inAudioConverter, 
    AudioConverterPropertyID inPropertyID, 
    UInt32*outSize, 
    Boolean*outWritable);  
Parameters
inAudioConverter
The AudioConverter to query.
inPropertyID
The property to query.
outSize
If non-null, on exit, the size of the property value in bytes.
outWritable
If non-null, on exit, indicates whether the property value is writable.
Return Value

An OSStatus result code.

Availability
Introduced in Mac OS X v10.1.

AudioConverterNew


Create a new AudioConverter.

extern OSStatus AudioConverterNew(
    const AudioStreamBasicDescription*inSourceFormat, 
    const AudioStreamBasicDescription*inDestinationFormat, 
    AudioConverterRef*outAudioConverter);  
Parameters
inSourceFormat
The format of the source audio to be converted.
inDestinationFormat
The destination format to which the audio is to be converter.
outAudioConverter
On successful return, points to a new AudioConverter instance.
Return Value

An OSStatus result code.

Discussion

For a pair of linear PCM formats, the following conversions are supported:



Also, encoding and decoding between linear PCM and compressed formats is supported. Functions in AudioToolbox/AudioFormat.h return information about the supported formats. When using a codec, you can use any supported PCM format (as above); the converter will perform any necessary additional conversion between your PCM format and the one created or consumed by the codec.

Availability
Introduced in Mac OS X v10.1.

AudioConverterNewSpecific


Create a new AudioConverter using specific codecs.

extern OSStatus AudioConverterNewSpecific(
    const AudioStreamBasicDescription*inSourceFormat, 
    const AudioStreamBasicDescription*inDestinationFormat, 
    UInt32 inNumberClassDescriptions, 
    AudioClassDescription*inClassDescriptions, 
    AudioConverterRef*outAudioConverter) ;  
Parameters
inSourceFormat
The format of the source audio to be converted.
inDestinationFormat
The destination format to which the audio is to be converter.
inNumberClassDescriptions
The number of class descriptions.
inClassDescriptions
AudioClassDescriptions specifiying the codec to instantiate.
outAudioConverter
On successful return, points to a new AudioConverter instance.
Return Value

An OSStatus result code.

Discussion

This function is identical to AudioConverterNew(), except that the client may explicitly choose which codec to instantiate if there is more than one choice.

Availability
Introduced in Mac OS X v10.4.

AudioConverterReset


Reset an AudioConverter

extern OSStatus AudioConverterReset(
    AudioConverterRef inAudioConverter);  
Parameters
inAudioConverter
The AudioConverter to reset.
Return Value

An OSStatus result code.

Discussion

Should be called whenever there is a discontinuity in the source audio stream being provided to the converter. This will flush any internal buffers in the converter.

Availability
Introduced in Mac OS X v10.1.

AudioConverterSetProperty


Sets an AudioConverter property value.

extern OSStatus AudioConverterSetProperty(
    AudioConverterRef inAudioConverter, 
    AudioConverterPropertyID inPropertyID, 
    UInt32 inPropertyDataSize, 
    const void*inPropertyData);  
Parameters
inAudioConverter
The AudioConverter to modify.
inPropertyID
The property to set.
inPropertyDataSize
The size in bytes of the property value.
inPropertyData
Points to the new property value.
Return Value

An OSStatus result code.

Availability
Introduced in Mac OS X v10.1.

Typedefs


AudioConverterComplexInputDataProc


Callback function for supplying input data to AudioConverterFillComplexBuffer.

typedef OSStatus (*AudioConverterComplexInputDataProc)(
    AudioConverterRef inAudioConverter, 
    UInt32*ioNumberDataPackets, 
    AudioBufferList*ioData, 
    AudioStreamPacketDescription** outDataPacketDescription, 
    void*inUserData);  
Parameters
inAudioConverter
The AudioConverter requesting input.
ioNumberDataPackets
On entry, the minimum number of packets of input audio data the converter would like in order to fulfill its current FillBuffer request. On exit, the number of packets of audio data actually being provided for input, or 0 if there is no more input.
ioData
On exit, the members of ioData should be set to point to the audio data being provided for input.
outDataPacketDescription
If non-null, on exit, the callback is expected to fill this in with an AudioStreamPacketDescription for each packet of input data being provided.
inUserData
The inInputDataProcUserData parameter passed to AudioConverterFillBuffer().
Return Value

An OSStatus result code.

Discussion

This callback function supplies input to AudioConverterFillComplexBuffer.

The AudioConverter requests a minimum number of packets (*ioNumberDataPackets). The callback may return one or more packets. If this is less than than the minimum, the callback will simply be called again in the near future.

The callback manipulates the members of ioData to point to one or more buffers of audio data (multiple buffers are used with non-interleaved PCM data). The callback is responsible for not freeing or altering this buffer until it is called again.

If the callback returns an error, it must return zero packets of data. AudioConverterFillComplexBuffer will stop producing output and return whatever output has already been produced to its caller, along with the error code. This mechanism can be used when an input proc has temporarily run out of data, but has not yet reached end of stream.


AudioConverterInputDataProc


Callback function for supplying input data to AudioConverterFillBuffer.

typedef OSStatus (*AudioConverterInputDataProc)(
    AudioConverterRef inAudioConverter, 
    UInt32*ioDataSize, 
    void** outData, 
    void*inUserData);  
Parameters
inAudioConverter
The AudioConverter requesting input.
ioDataSize
On entry, the minimum number of bytes of audio data the converter would like in order to fulfill its current FillBuffer request. On exit, the number of bytes of audio data actually being provided for input, or 0 if there is no more input.
outData
On exit, *outData should point to the audio data being provided for input.
inUserData
The inInputDataProcUserData parameter passed to AudioConverterFillBuffer().
Return Value

An OSStatus result code.

Discussion

This callback function supplies input to AudioConverterFillBuffer.

The AudioConverter requests a minimum amount of data (*ioDataSize). The callback may return any amount of data. If it is less than than the minimum, the callback will simply be called again in the near future.

The callback supplies a pointer to a buffer of audio data. The callback is responsible for not freeing or altering this buffer until it is called again.

If the callback returns an error, it must return zero bytes of data. AudioConverterFillBuffer will stop producing output and return whatever output has already been produced to its caller, along with the error code. This mechanism can be used when an input proc has temporarily run out of data, but has not yet reached end of stream.


AudioConverterRef


A reference to an AudioConverter object.

typedef struct OpaqueAudioConverter * AudioConverterRef;  

Structs and Unions


AudioConverterPrimeInfo


Specifies priming information.

struct AudioConverterPrimeInfo { 
    UInt32 leadingFrames; 
    UInt32 trailingFrames; 
};  
Fields
leadingFrames
Specifies the number of leading (previous) input frames, relative to the normal/desired start input frame, required by the converter to perform a high quality conversion. If using kConverterPrimeMethod_Pre, the client should "pre-seek" the input stream provided through the input proc by leadingFrames. If no frames are available previous to the desired input start frame (because, for example, the desired start frame is at the very beginning of available audio), then provide "leadingFrames" worth of initial zero frames in the input proc. Do not "pre-seek" in the default case of kConverterPrimeMethod_Normal or when using kConverterPrimeMethod_None.

trailingFrames
Specifies the number of trailing input frames (past the normal/expected end input frame) required by the converter to perform a high quality conversion. The client should be prepared to provide this number of additional input frames except when using kConverterPrimeMethod_None. If no more frames of input are available in the input stream (because, for example, the desired end frame is at the end of an audio file), then zero (silent) trailing frames will be synthesized for the client.

Discussion

When using AudioConverterFillBuffer() (either a single call or a series of calls), some conversions, particularly involving sample-rate conversion, ideally require a certain number of input frames previous to the normal start input frame and beyond the end of the last expected input frame in order to yield high-quality results.

These are expressed in the leadingFrames and trailingFrames members of the structure.

The very first call to AudioConverterFillBuffer(), or first call after AudioConverterReset(), will request additional input frames beyond those normally expected in the input proc callback to fulfill this first AudioConverterFillBuffer() request. The number of additional frames requested, depending on the prime method, will be approximately:

            kConverterPrimeMethod_Pre       leadingFrames + trailingFrames
            kConverterPrimeMethod_Normal    trailingFrames
            kConverterPrimeMethod_None      0


Thus, in effect, the first input proc callback(s) may provide not only the leading frames, but also may "read ahead" by an additional number of trailing frames depending on the prime method.

kConverterPrimeMethod_None is useful in a real-time application processing live input, in which case trailingFrames (relative to input sample rate) of through latency will be seen at the beginning of the output of the AudioConverter. In other real-time applications such as DAW systems, it may be possible to provide these initial extra audio frames since they are stored on disk or in memory somewhere and kConverterPrimeMethod_Pre may be preferable. The default method is kConverterPrimeMethod_Normal, which requires no pre-seeking of the input stream and generates no latency at the output.

Enumerations


AudioConverterPropertyID


The properties of an AudioConverter, accessible via AudioConverterGetProperty() and AudioConverterSetProperty().

enum // typedef UInt32 AudioConverterPropertyID 
{ 
    kAudioConverterPropertyMinimumInputBufferSize = 'mibs', 
    kAudioConverterPropertyMinimumOutputBufferSize = 'mobs', 
    kAudioConverterPropertyMaximumInputBufferSize = 'xibs', 
    kAudioConverterPropertyMaximumInputPacketSize = 'xips', 
    kAudioConverterPropertyMaximumOutputPacketSize = 'xops', 
    kAudioConverterPropertyCalculateInputBufferSize = 'cibs', 
    kAudioConverterPropertyCalculateOutputBufferSize = 'cobs', 
    kAudioConverterPropertyInputCodecParameters = 'icdp', 
    kAudioConverterPropertyOutputCodecParameters = 'ocdp', 
    kAudioConverterSampleRateConverterAlgorithm = 'srci', 
    kAudioConverterSampleRateConverterQuality = 'srcq', 
    kAudioConverterCodecQuality = 'cdqu', 
    kAudioConverterPrimeMethod = 'prmm', 
    kAudioConverterPrimeInfo = 'prim', 
    kAudioConverterChannelMap = 'chmp', 
    kAudioConverterDecompressionMagicCookie = 'dmgc', 
    kAudioConverterCompressionMagicCookie = 'cmgc', 
    kAudioConverterEncodeBitRate = 'brat', 
    kAudioConverterEncodeAdjustableSampleRate = 'ajsr', 
    kAudioConverterInputChannelLayout = 'icl ', 
    kAudioConverterOutputChannelLayout = 'ocl ', 
    kAudioConverterApplicableEncodeBitRates = 'aebr', 
    kAudioConverterAvailableEncodeBitRates = 'vebr', 
    kAudioConverterApplicableEncodeSampleRates = 'aesr', 
    kAudioConverterAvailableEncodeSampleRates = 'vesr', 
    kAudioConverterAvailableEncodeChannelLayoutTags = 'aecl', 
    kAudioConverterCurrentOutputStreamDescription = 'acod', 
    kAudioConverterCurrentInputStreamDescription = 'acid', 
    kAudioConverterPropertySettings = 'acps', 
    kAudioConverterPropertyBitDepthHint = 'acbd' 
};  
Constants
kAudioConverterPropertyMinimumInputBufferSize
a UInt32 that indicates the size in bytes of the smallest buffer of input data that can be supplied via the AudioConverterInputProc or as the input to AudioConverterConvertBuffer
kAudioConverterPropertyMinimumOutputBufferSize
a UInt32 that indicates the size in bytes of the smallest buffer of output data that can be supplied to AudioConverterFillBuffer or as the output to AudioConverterConvertBuffer
kAudioConverterPropertyMaximumInputBufferSize
a UInt32 that indicates the size in bytes of the largest buffer of input data that will be requested from the AudioConverterInputProc. This is mostly useful for variable bit rate compressed data. This will be equal to 0xFFFFFFFF if the maximum value depends on what is requested from the output, which is usually the case for constant bit rate formats.
kAudioConverterPropertyMaximumInputPacketSize
a UInt32 that indicates the size in bytes of the largest single packet of data in the input format. This is mostly useful for variable bit rate compressed data (decoders).
kAudioConverterPropertyMaximumOutputPacketSize
a UInt32 that indicates the size in bytes of the largest single packet of data in the output format. This is mostly useful for variable bit rate compressed data (encoders).
kAudioConverterPropertyCalculateInputBufferSize
a UInt32 that on input holds a size in bytes that is desired for the output data. On output, it will hold the size in bytes of the input buffer required to generate that much output data. Note that some converters cannot do this calculation.
kAudioConverterPropertyCalculateOutputBufferSize
a UInt32 that on input holds a size in bytes that is desired for the input data. On output, it will hold the size in bytes of the output buffer required to hold the output data that will be generated. Note that some converters cannot do this calculation.
kAudioConverterPropertyInputCodecParameters
The value of this property varies from format to format and is considered private to the format. It is treated as a buffer of untyped data.
kAudioConverterPropertyOutputCodecParameters
The value of this property varies from format to format and is considered private to the format. It is treated as a buffer of untyped data.
kAudioConverterSampleRateConverterAlgorithm
DEPRECATED: please use kAudioConverterSampleRateConverterQuality instead

An OSType that specifies the sample rate converter to use (as defined in AudioUnit/AudioUnitProperties.h -- for now only Apple SRC's can be used)
kAudioConverterSampleRateConverterQuality
A UInt32 that specifies rendering quality of the sample rate converter (see enum constants below)
kAudioConverterCodecQuality
A UInt32 that specifies rendering quality of a codec (see enum constants below)
kAudioConverterPrimeMethod
a UInt32 specifying priming method (usually for sample-rate converter) see explanation for struct AudioConverterPrimeInfo below along with enum constants
kAudioConverterPrimeInfo
A pointer to AudioConverterPrimeInfo (see explanation for struct AudioConverterPrimeInfo below)
kAudioConverterChannelMap
An array of SInt32's. The size of the array is the number of output channels, and each element specifies which input channel's data is routed to that output channel (using a 0-based index of the input channels), or -1 if no input channel is to be routed to that output channel. The default behavior is as follows. I = number of input channels, O = number of output channels. When I > O, the first O inputs are routed to the first O outputs, and the remaining puts discarded. When O > I, the first I inputs are routed to the first O outputs, and the remaining outputs are zeroed.

A simple example for splitting mono input to stereo output (instead of routing the input to only the first output channel):

   // this should be as large as the number of output channels:
  SInt32 channelMap[2] = { 0, 0 };
  AudioConverterSetProperty(theConverter, kAudioConverterChannelMap, 
    sizeof(channelMap), channelMap);
kAudioConverterDecompressionMagicCookie
A void * pointing to memory set up by the caller. Required by some formats in order to decompress the input data.
kAudioConverterCompressionMagicCookie
A void * pointing to memory set up by the caller. Returned by the converter so that it may be stored along with the output data. It can then be passed back to the converter for decompression at a later time.
kAudioConverterEncodeBitRate
A UInt32 containing the number of bits per second to aim for when encoding data. This property is only relevant to encoders.
kAudioConverterEncodeAdjustableSampleRate
For encoders where the AudioConverter was created with an output sample rate of zero, and the codec can do rate conversion on its input, this provides a way to set the output sample rate. The property value is a Float64.
kAudioConverterInputChannelLayout
The property value is an AudioChannelLayout.
kAudioConverterOutputChannelLayout
The property value is an AudioChannelLayout.
kAudioConverterApplicableEncodeBitRates
The property value is an array of AudioValueRange describing applicable bit rates based on current settings.
kAudioConverterAvailableEncodeBitRates
The property value is an array of AudioValueRange describing available bit rates based on the input format. You can get all available bit rates from the AudioFormat API.
kAudioConverterApplicableEncodeSampleRates
The property value is an array of AudioValueRange describing applicable sample rates based on current settings.
kAudioConverterAvailableEncodeSampleRates
The property value is an array of AudioValueRange describing available sample rates based on the input format. You can get all available sample rates from the AudioFormat API.
kAudioConverterAvailableEncodeChannelLayoutTags
The property value is an array of AudioChannelLayoutTags for the format and number of channels specified in the input format going to the encoder.
kAudioConverterCurrentOutputStreamDescription
Returns the current completely specified output AudioStreamBasicDescription. For example when encoding to AAC, your original output stream description will not have been completely filled out.
kAudioConverterCurrentInputStreamDescription
Returns the current completely specified input AudioStreamBasicDescription.
kAudioConverterPropertySettings
Returns the a CFArray of property settings for converters.
kAudioConverterPropertyBitDepthHint
A UInt32 of the source bit depth to preserve. This is a hint to some encoders like lossless about how many bits to preserve in the input. The converter usually tries to preserve as many as possible, but a lossless encoder will do poorly if more bits are supplied than are desired in the output.

Prime method constants


Constants to be used with kAudioConverterPrimeMethod.

enum { 
    kConverterPrimeMethod_Pre = 0, 
    kConverterPrimeMethod_Normal = 1, 
    kConverterPrimeMethod_None = 2 
};  
Constants
kConverterPrimeMethod_Pre
Primes with leading + trailing input frames.
kConverterPrimeMethod_Normal
Only primes with trailing (zero latency). Leading frames are assumed to be silence.
kConverterPrimeMethod_None
Acts in "latency" mode. Both leading and trailing frames assumed to be silence.

Quality constants


Constants to be used with kAudioConverterSampleRateConverterQuality.

enum { 
    kAudioConverterQuality_Max = 0x7F, 
    kAudioConverterQuality_High = 0x60, 
    kAudioConverterQuality_Medium = 0x40, 
    kAudioConverterQuality_Low = 0x20, 
    kAudioConverterQuality_Min = 0 
};  
Constants
kAudioConverterQuality_Max
maximum quality
kAudioConverterQuality_High
high quality
kAudioConverterQuality_Medium
medium quality
kAudioConverterQuality_Low
low quality
kAudioConverterQuality_Min
minimum quality


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.
Last Updated: 2006-09-07