private void initRecorder() { Encoding encoding = Encoding.Pcm16bit; shortsBuffer = new short[bufferSize]; buffer = new byte[bufferSize]; audioRecorder = new AudioRecord( // Hardware source of recording. AudioSource.VoiceCommunication, // Frequency sampleRate, // Mono or stereo ChannelIn.Mono, // Audio encoding encoding, // Length of the audio clip. bufferSize * 5 ); audioRecorder.StartRecording(); if (AcousticEchoCanceler.IsAvailable) { echoCanceller = AcousticEchoCanceler.Create(audioRecorder.AudioSessionId); } if (NoiseSuppressor.IsAvailable) { noiseSuppressor = NoiseSuppressor.Create(audioRecorder.AudioSessionId); } }
/// <summary> /// Constructs an instance of the WebRtcFilter. /// </summary> /// <param name="expectedAudioLatency">The expected audio latency in milliseconds</param> /// <param name="filterLength">The length of the echo cancellation filter in milliseconds</param> /// <param name="recordedAudioFormat">The audio format into which the recorded audio has been transformed prior to encoding (e.g., not the raw audio)</param> /// <param name="playedAudioFormat">The audio format which the audio must be transformed after decoding and before playing</param> /// <param name="enableAec">Whether to enable acoustic echo cancellation</param> /// <param name="enableDenoise">Whether to enable denoising</param> /// <param name="enableAgc">Whether to enable automatic gain control</param> /// <param name="playedResampler">The resampler which should be used on played audio</param> /// <param name="recordedResampler">The resampler which should be used on recorded audio</param> public WebRtcFilter(int expectedAudioLatency, int filterLength, AudioFormat recordedAudioFormat, AudioFormat playedAudioFormat, bool enableAec, bool enableDenoise, bool enableAgc, IAudioFilter playedResampler = null, IAudioFilter recordedResampler = null) : base(expectedAudioLatency, filterLength, recordedAudioFormat, playedAudioFormat, playedResampler, recordedResampler) { // Default settings. var aecConfig = new AecConfig(FilterLength, recordedAudioFormat.SamplesPerFrame, recordedAudioFormat.SamplesPerSecond) { NlpMode = AecNlpMode.KAecNlpModerate, SkewMode = false, MetricsMode = false }; _ns = new NoiseSuppressor(recordedAudioFormat); _aec = new AecCore(aecConfig); if (aecConfig.NlpMode != AecNlpMode.KAecNlpConservative && aecConfig.NlpMode != AecNlpMode.KAecNlpModerate && aecConfig.NlpMode != AecNlpMode.KAecNlpAggressive) { throw new ArgumentException(); } _aec.targetSupp = WebRtcConstants.targetSupp[(int)aecConfig.NlpMode]; _aec.minOverDrive = WebRtcConstants.minOverDrive[(int)aecConfig.NlpMode]; if (aecConfig.MetricsMode && aecConfig.MetricsMode != true) { throw new ArgumentException(); } _aec.metricsMode = aecConfig.MetricsMode; if (_aec.metricsMode) { _aec.InitMetrics(); } _enableAec = enableAec; _enableDenoise = enableDenoise; _enableAgc = enableAgc; _agc = new Agc(0, 255, Agc.AgcMode.AgcModeAdaptiveDigital, (uint)recordedAudioFormat.SamplesPerSecond); }
public void stop() { if (!running) { return; } running = false; if (echoCanceller != null) { try { echoCanceller.Release(); echoCanceller.Dispose(); } catch (Exception) { } echoCanceller = null; } if (noiseSuppressor != null) { try { noiseSuppressor.Release(); noiseSuppressor.Dispose(); } catch (Exception) { } noiseSuppressor = null; } if (audioRecorder != null) { try { audioRecorder.Stop(); audioRecorder.Release(); } catch (Exception) { } audioRecorder.Dispose(); audioRecorder = null; } if (audioEncoder != null) { audioEncoder.stop(); audioEncoder.Dispose(); audioEncoder = null; } buffer = null; shortsBuffer = null; bufferSize = 0; lock (outputBuffers) { outputBuffers.Clear(); } AudioManager am = (AudioManager)MainActivity.Instance.GetSystemService(Context.AudioService); if (Build.VERSION.SdkInt < BuildVersionCodes.O) { if (focusListener != null) { #pragma warning disable CS0618 // Type or member is obsolete am.AbandonAudioFocus(focusListener); #pragma warning restore CS0618 // Type or member is obsolete focusListener.Dispose(); focusListener = null; } } else { if (focusListener != null) { if (focusRequest != null) { am.AbandonAudioFocusRequest(focusRequest); focusRequest.Dispose(); focusRequest = null; } focusListener.Dispose(); focusListener = null; } } }