ADC Home > Reference Library > Technical Q&As > Audio > Core Audio >

AudioDevice sample rates




Getting and setting sample rates

Q: How do I get and set the sample rate for an AudioDevice?

A: The code below was taken from the CoreAudio SDK(/Developer/Examples/CoreAudio/PublicUtility/CAAudioHardwareDevice.*) . It can be used to get and set available sample rate ranges for a given AudioDevice .



Listing 1. How to get and set the available sample rate ranges


Float64 CAAudioHardwareDevice::GetNominalSampleRate() const
{
    Float64 theAnswer = 0;
    UInt32 theSize = sizeof(Float64);
    GetPropertyData(0, 
                               kAudioDeviceSectionGlobal, 
                               kAudioDevicePropertyNominalSampleRate, 
                               theSize, 
                               &theAnswer);
    return theAnswer;
}

void CAAudioHardwareDevice::SetNominalSampleRate(Float64 inSampleRate)
{
    UInt32 theSize = sizeof(Float64);
    SetPropertyData(0, 
                              kAudioDeviceSectionGlobal, 
                              kAudioDevicePropertyNominalSampleRate, 
                              theSize, 
                              &inSampleRate);
}

UInt32 CAAudioHardwareDevice::GetNumberNominalSampleRateRanges() const
{
    UInt32 theSize = GetPropertyDataSize(0, 
                        kAudioDeviceSectionGlobal,
                        kAudioDevicePropertyAvailableNominalSampleRates);
       
    return (theSize / sizeof(AudioValueRange) );
}

void    GetNominalSampleRateRanges(UInt32& ioNumberRanges,
                                   AudioValueRange* outRanges) const
{
    UInt32 theSize = ioNumberRanges * sizeof(AudioValueRange);
    GetPropertyData(0, 
                   kAudioDeviceSectionGlobal,
                   kAudioDevicePropertyAvailableNominalSampleRates, 
                   theSize,
                   outRanges);

    ioNumberRanges = theSize / sizeof(AudioValueRange);
}

UInt32 CAAudioHardwareDevice::GetPropertyDataSize(UInt32 inChannel,
                 CAAudioHardwareDeviceSectionID inSection, 
                 AudioHardwarePropertyID inPropertyID) const
{
    UInt32 theSize = 0;
    OSStatus theError;
    theError = AudioDeviceGetPropertyInfo(mAudioDeviceID, 
                                 inChannel, 
                                 inSection, 
                                 inPropertyID, 
                                 &theSize,
                                  NULL);
      ThrowIfError(theError,
                    CAException(theError), 
                    "error getting info about a property");
    return theSize;
}

void    CAAudioHardwareDevice::GetPropertyData(UInt32 inChannel,
                                  CAAudioHardwareDeviceSectionID inSection,
                                  AudioHardwarePropertyID inPropertyID,
                                  UInt32& ioDataSize, 
                                  void* outData) const
{
    OSStatus theError;
    theError = AudioDeviceGetProperty(mAudioDeviceID, 
                                 inChannel, 
                                 inSection, 
                                 inPropertyID, 
                                 &ioDataSize, 
                                 outData);
     ThrowIfError(theError,
                  CAException(theError), 
                  "error getting the value of a property");
}

void    CAAudioHardwareDevice::SetPropertyData(UInt32 inChannel,    
                                 CAAudioHardwareDeviceSectionID inSection, 
                                 AudioHardwarePropertyID inPropertyID, 
                                 UInt32 inDataSize, 
                                 const void* inData, 
                                 const AudioTimeStamp* inWhen)
{
    OSStatus theError;
    theError = AudioDeviceSetProperty(mAudioDeviceID,
                                               inWhen,
                                               inChannel, 
                                               inSection, 
                                               inPropertyID,
                                               inDataSize, 
                                               inData);
       ThrowIfError(theError, 
                     CAException(theError),
                     "error setting the value of a property");
}





Back to top



Sample Rate Conversions

Q :My input audio device has a different sample rate than the output device. How can I handle conversions between the two?

A: There are two ways to meet your goals.

(1) You can use an AudioConverter to handle the sample rate conversions between the devices. An AudioConverter can be created with the input and output stream formats. Then you must use AudioConverterFillBuffer to convert a buffer of data.

(2) Use the DefaultOutputUnit. This will handle sample rate converstion between your input device and the default output device automatically. See sample code DefaultOutputUnit ( /Developer/Examples/CoreAudio/Services/DefaultOutputUnit ) for more info.


[Dec 09, 2003]


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.