Search code examples
iosavaudiosessionavaudiorecorder

Cancel iOS microphone echo cancellation and noise suppression


Is there a way to cancel pre-processing like echo cancellation and noise suppression in audio recorder in iOS?

I'm using AVAudioRecorder with meteringEnabled=true, and I get the average decibel level using averagePowerForChannel (docs).

I am trying to measure ambient noise near the phone, and iPhone 8 seems to amplify low noises or cancel them out if I start to speak. For example, if background music has an absolute decibel level of 30 - iOS seems to amplify it. When I start to speak even quietly - the dB level drops significantly.

But since I want to measure ambient noise - I don't want this pre-processing.

I tried setInputGain (docs) but isInputGainSettable is always false - therefore, I can't take this approach.

Is there a way to cancel any amplification or pre-processing like echo cancellation and noise suppression?


Solution

  • What the app needs is access to unprocessed audio after disabling the AGC (Auto Gain Control) filters on the audio channel. To get access to raw and unprocessed audio, turn on Measurement mode in iOS.

    As appears in the iOS documentation here, "Measurement" mode is a mode that indicates that your app is performing measurement of audio input or output.

    This mode is intended for apps that need to minimize the amount of system-supplied signal processing to input and output signals. If recording on devices with more than one built-in microphone, the primary microphone is used.

    The javascript code I used to modify this (using nativescript), before recording, is this:

    // Disable AGC
    const avSession = AVAudioSession.sharedInstance();
    avSession.setCategoryModeOptionsError(AVAudioSessionCategoryRecord, AVAudioSessionModeMeasurement, null);