How to control hardware mic input gain/level on iPhone?

只愿长相守 提交于 2019-12-17 18:33:42

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10871231/how-to-control-hardware-mic-input-gain-level-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!