ios 如何在 iPhone 上控制硬件麦克风输入增益/电平?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10871231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:36:33  来源:igfitidea点击:

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

ioscore-audioremoteio

提问by buildsucceeded

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.

我的音频分析功能在 iPad (2) 上的响应比 iPhone (4) 更好。它似乎对 iPad 上的柔和声音很敏感,而 iPhone 需要更大声的输入才能正确响应。无论这是因为麦克风放置、不同的组件、不同的软件配置还是其他一些因素,我都希望能够在我的应用程序中对其进行控制。

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.

是否可以通过 iOS 中的软件控制麦克风的增益,类似于 MacOS 中的情况?我找不到任何关于此的文档,但我希望我只是以某种方式错过了它。

回答by foundry

On ios6+ you can use AVAudioSession properties

在 ios6+ 上,您可以使用 AVAudioSession 属性

        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

在 ios5 上,您可以使用 AudioSession 函数获取/设置音频输入增益属性

    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)

由于您对控制输入增益感兴趣,您可能还想通过将音频会话模式设置为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.

这些设置是特定于硬件的,因此无法假设可用性。例如,我可以更改 iPhone3GS/ios6 和 iPhone4S/ios5.1 上的增益,但不能更改 ipadMini/ios6.1。我可以在 iPhone3G 和 iPad mini 上禁用 AGC,但不能在 iPhone4S 上禁用。

回答by Jeremy Grenier