8

My audio-analysis function responds better on the iPad (2) than the iPhone (4). It seems sensitive to softer sounds on the iPad, whereas the iPhone requires much louder input to respond properly. Whether this is because of mic placement, different components, different software configurations or some other factor, I'd like to be able to control for it in my app.

Obviously I could just multiply all of my audio samples to programmatically apply gain. Of course that has a software cost too, so:

Is it possible to control the mic's gain from software in iOS, similarly to how it is in MacOS? I can't find any documentation on this but I'm hoping I'm just missing it somehow.

buildsucceeded
  • 4,203
  • 4
  • 34
  • 72

2 Answers2

17

On ios6+ you can use AVAudioSession properties

        CGFloat gain = sender.value;
        NSError* error;
        self.audioSession = [AVAudioSession sharedInstance];
        if (self.audioSession.isInputGainSettable) {
            BOOL success = [self.audioSession setInputGain:gain 
                                                     error:&error];
               if (!success){} //error handling
        } else {
            NSLog(@"ios6 - cannot set input gain");
        }               

On ios5 you can get/set audio input gain properties using AudioSession functions

    UInt32 ui32propSize = sizeof(UInt32);
    UInt32 f32propSize = sizeof(Float32);
    UInt32 inputGainAvailable = 0;
    Float32 inputGain = sender.value;


    OSStatus err = 
        AudioSessionGetProperty(kAudioSessionProperty_InputGainAvailable
                            , &ui32propSize
                            , &inputGainAvailable);

    if (inputGainAvailable) {
    OSStatus err = 
        AudioSessionSetProperty(kAudioSessionProperty_InputGainScalar
                             , sizeof(inputGain)
                             , &inputGain);
    } else {
        NSLog(@"ios5 - cannot set input gain");
    }
    OSStatus err = 
        AudioSessionGetProperty(kAudioSessionProperty_InputGainScalar
                              , &f32propSize
                              , &inputGain);
    NSLog(@"inputGain: %0.2f",inputGain);

(error handling omitted)

As you are interested in controlling input gain, you may also want to disable automatic gain control by setting the audio session mode to AVAudioSessionModeMeasurement (ios5+6)

[self.audioSession setMode:AVAudioSessionModeMeasurement
                     error:nil];
NSLog(@"mode:%@",self.audioSession.mode);

These settings are fairly hardware-specific so availability cannot be assumed. For example, I can alter the gain on iPhone3GS/ios6 and iPhone4S/ios5.1, but not on ipadMini/ios6.1. I can disable AGC on the iPhone3G and the iPad mini, but not the iPhone4S.

foundry
  • 31,615
  • 9
  • 90
  • 125
  • Thanks for the solution - do you know where in the code I should set this? Does it control the global microphone gain levels or just for a specific recording stream? – David Daudelin Apr 09 '14 at 15:27
  • 2
    @DavidDaudelin - it sets the mic gain levels for your app, and the settings remain in place for the entire runtime of your app until/unless you change them. Suitable places to set them would be the app delegate or a controller's `init` method. I usually make an audioController object and set up the audioSession there. Behaviour when your app is backgrounded depends on your settings and those of other apps that may compete for hardware control. – foundry Apr 09 '14 at 21:48
  • 1
    I know, long time have passed since this question is answered but I need a very big help. I need to increase the volume of the microphone in order to get a lauder sound while recording. while simulator microphone gets enough volume of input data, an iphone device gets the input audio very low volume.this creates problem for us. my question: can increasing the gain solve this problem? . I have tried your code, but 'isInputGainSettable' returns false. But somehow I am able to set the input gain using setInputGain method. why are these two of them conflicting with each other. any idea? Thanks – smoothumut Dec 31 '14 at 15:26
  • 1
    Ok, it solves my problem, thank you very very much, i am really happy now regards :) – smoothumut Dec 31 '14 at 16:31
  • @smoothumut How did you fix it? having the same problem.. – Teja Konjeti Jun 30 '20 at 13:47
  • @TejaKonjeti It has been very long time, I dont remember how I solved the problem. sorry for that. – smoothumut Jul 01 '20 at 05:23
  • @smoothumut That's ok :) 5 years is a long time – Teja Konjeti Jul 01 '20 at 13:41
-2

I think this can help you : http://www.stefanpopp.de/2011/capture-iphone-microphone/

Jeremy Grenier
  • 674
  • 1
  • 6
  • 8
  • Nice article, but it only describes software manipulation of the audio stream, which as I mentioned is what I'm already doing. – buildsucceeded Jun 03 '12 at 16:45
  • 2
    Link only answers aren't particularly helpful, and links have a tendency of dying, rendering the answer completely useless. – nhgrif Feb 05 '14 at 12:24