Core Audio on iPhone - any way to change the microphone gain (either for speakerphone mic or headphone mic)?

Halle picture Halle · May 28, 2010 · Viewed 7.8k times · Source

After much searching the answer seems to be no, but I thought I'd ask here before giving up. For a project I'm working on that includes recording sound, the input levels sound a little quiet both when the route is external mic + speaker and when it's headphone mic + headphones. Does anyone know definitively whether it is possible to programmatically change mic gain levels on the iPhone in any part of Core Audio?

If not, is it possible that I'm not really in "speakerphone" mode (with the external mic at least) but only think I am? Here is my audio session init code:

OSStatus error = AudioSessionInitialize(NULL, NULL, audioQueueHelperInterruptionListener, r);

[...some error checking of the OSStatus...]

UInt32 category = kAudioSessionCategory_PlayAndRecord; // need to play out the speaker at full volume too so it is necessary to change default route below
error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
if (error) printf("couldn't set audio category!");

UInt32 doChangeDefaultRoute = 1;
error = AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);
if (error) printf("couldn't change default route!");

error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioQueueHelperPropListener, r);
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", (int)error);

UInt32 inputAvailable = 0;
UInt32 size = sizeof(inputAvailable);

error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", (int)error);

error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, audioQueueHelperPropListener, r);
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", (int)error);

error = AudioSessionSetActive(true); 
if (error) printf("AudioSessionSetActive (true) failed");

Thanks very much for any pointers.

Answer

slf picture slf · May 28, 2010

Gain is a property of the Audio Unit.

I haven't tried it, but you should be able to do AudioUnitSetProperty and turn off "Automatic Gain Control" or AGC for short with this property key: kAUVoiceIOProperty_VoiceProcessingEnableAGC

See also kAUVoiceIOProperty_BypassVoiceProcessing