I am using the Audio Unit Framework to develop a VOIP application on mac os x. In my program, I configured the AUHAL input and used the default stream format (44.1 kHz, 32 bit / channel) to capture the sound from the microphone. In this case, my program works fine.
Here is the code:
//The default setting in my program CheckError(AudioUnitGetProperty(m_audCapUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, //the value is 0 inputBus, //the value is 1 &m_audCapUnitOutputStreamFormat, &propertySize), "Couldn't get OutputSample ASBD from input unit") ; //the inOutputSampleRate is 44100.0 m_audCapUnitOutputStreamFormat.mSampleRate = inOutputSampleRate ; CheckError(AudioUnitSetProperty(m_audCapUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, inputBus, &m_audCapUnitOutputStreamFormat, propertySize), "Couldn't set OutputSample ASBD on input unit"); //
Since I am developing a VOIP application, the default format (44.1 kHz, 32 bit / channel) is not suitable for my program, so I want to change the sampling frequency to 8 kHz. And I wrote this code to change the format in my program:
//...... inOutputFormat.mSampleRate = 8000. ; inOutputFormat.mFormatID = kAudioFormatLinearPCM ; inOutputFormat.mChannelsPerFrame = 2 ; inOutputFormat.mBitsPerChannel = 16 ; inOutputFormat.mBytesPerFrame = 2 ; inOutputFormat.mBytesPerPacket = 2 ; inOutputFormat.mFramesPerPacket = 1 ; inOutputFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical ; inOutputFormat.mReserved = 0 ; CheckError(AudioUnitSetProperty(m_audCapUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, inputBus, &inOutputFormat, ui32PropSize), "Couldn't set AUHAL Unit Output Format") ; //.......
In this case, the program runs fine until my program calls AudioUnitRender in the callback function; it cannot call AudioUnitRender with error code -10876 , which means kAudioUnitErr_NoConnection in the documentation. The error code puzzled me so much, so I searched for it, but I did not find any useful information. Can someone tell me what the error really means?
This is not the end, I changed the format using this code:
//..... inOutputFormat.mSampleRate = 8000. ; inOutputFormat.mFormatID = kAudioFormatLinearPCM ; inOutputFormat.mChannelsPerFrame = 2 ; inOutputFormat.mBitsPerChannel = 32 ; inOutputFormat.mBytesPerFrame = 4 ; inOutputFormat.mBytesPerPacket = 4 ; inOutputFormat.mFramesPerPacket = 1 ; inOutputFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical ; inOutputFormat.mReserved = 0 ; CheckError(AudioUnitSetProperty(m_audCapUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, inputBus, &inOutputFormat, ui32PropSize), "Couldn't set AUHAL Unit Output Format") ; //........
In this case, the program failed to call AudioUnitRender again and returned another error code -10863(kAudioUnitErr_CannotDoInCurrentContext) . I googled it, but I found something useful . After reading the information there, I think the sampling rate or the format that I set to AUHAL may not be supported by the hardware.
So, I wrote code to check the available sampling rates on the default input device:
//.......... UInt32 propertySize = sizeof(AudioDeviceID) ; Boolean isWritable = false ; CheckError(AudioDeviceGetPropertyInfo(inDeviceID, //the inDeviceID is the default input device 0, true, kAudioDevicePropertyAvailableNominalSampleRates, &propertySize, &isWritable), "Get the Available Sample Rate Count Failed") ; m_valueCount = propertySize / sizeof(AudioValueRange) ; printf("Available %d Sample Rate\n",m_valueCount) ; CheckError(AudioDeviceGetProperty(inDeviceID, 0, false, kAudioDevicePropertyAvailableNominalSampleRates, &propertySize, m_valueTabe), "Get the Available Sample Rate Count Failed") ; for(UInt32 i = 0 ; i < m_valueCount ; ++i) { printf("Available Sample Rate value : %ld\n",(long)m_valueTabe[i].mMinimum) ; } //..............
And then I discovered that the available sampling rates are 8000, 16000, 32000, 44100, 48000, 88200 and 96000.
The 8000 sample rate is what I installed before, but I get an error code by calling AudioUnitRender , I just want to say why?
I had a lot of Google and also read a lot of documents, but I canβt get an answer, can someone solve this problem, what have I encountered?
In other words; How to change the sampling rate or format on the input AUHAL?