4

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.

Halle
  • 3,584
  • 1
  • 37
  • 53
  • Possible duplicate of [How to control hardware mic input gain/level on iPhone?](http://stackoverflow.com/questions/10871231/how-to-control-hardware-mic-input-gain-level-on-iphone) – buildsucceeded Oct 11 '15 at 14:19

4 Answers4

4

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

slf
  • 22,595
  • 11
  • 77
  • 101
  • 1
    OK, to be specific kAUVoiceIOProperty_VoiceProcessingEnableAGC only applies to the Voice-Processing I/O Audio Unit, but that might be an option for me, thank you. – Halle May 28 '10 at 22:10
  • Just to follow up on this, there is no gain control per se in any audio unit or audio session settings. But, some of the audio unit presets result in more perceived gain, or more signal and less noise, for voice applications specifically. – Halle May 26 '12 at 08:35
1

The gain control is automatic, the average will always be the level deemed 'optimal'. You could route the input thru a mixer to control the volume but i think it would probably clip.

This is a pretty good place to start with coreAudio on the iphone

http://www.subfurther.com/blog/?p=507

hooleyhoop
  • 9,128
  • 5
  • 37
  • 58
  • Interesting, could you point me to documentation/sample code regarding routing through a mixer? – Halle May 28 '10 at 19:26
  • Thank you - I'd seen that blog post in my searches, but I'm more interested in a specific example or document regarding gain/AGC. – Halle May 28 '10 at 22:03
  • Well, as i said.. there is no way to manipulate the microphone gain. But you can manipulate the volume of the signal prior to playback or recording by hooking up a remoteIO audio unit to a mixer audio unit in an AUGraph as described in the link. – hooleyhoop May 28 '10 at 22:29
  • Gotcha, I thought you were talking about a way to affect the gain levels using a mixer -- pushing the volume after acquisition probably isn't going to be good for this application since I'm hoping to improve s/n versus having louder playback, but I appreciate the suggestion. – Halle May 29 '10 at 01:47
0

For iOS 5.0 and later, you can now set AudioSession mode to kAudioSessionMode_Measurement.

kAudioSessionMode_Measurement

Appropriate for applications that wish to minimize the effect of system-supplied signal processing for input and/or output audio signals.

You can set the AudioSession mode in Core Audio like this:

UInt32 mode = kAudioSessionMode_Measurement;
AudioSessionSetProperty(kAudioSessionProperty_Mode, sizeof(mode), &mode)
elp
  • 8,021
  • 7
  • 61
  • 120
Rick Lam
  • 246
  • 1
  • 3
0

As of iOS 5, you can set the global analog input gain setting as follows

UInt32 uInt32Size = sizeof(UInt32);
UInt32 isGainAvaiable = 0;
OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_InputGainAvailable, &uInt32Size, &isGainAvaiable);
if (isGainAvaiable)
{
    Float32 gainFloat = 0.142857f; //for example...
    status = AudioSessionSetProperty(kAudioSessionProperty_InputGainScalar, sizeof(gainFloat), &gainFloat);
}
NSDestr0yer
  • 1,419
  • 16
  • 20