peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_ |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | |
| 14 | #include <array> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include <memory> |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 16 | #include <vector> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 17 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
| 19 | #include "api/array_view.h" |
Gustaf Ullberg | 3646f97 | 2018-02-14 15:19:04 +0100 | [diff] [blame] | 20 | #include "api/audio/echo_canceller3_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/audio_processing/aec3/aec3_common.h" |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/aec3/aec_state.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/fft_data.h" |
Gustaf Ullberg | 8406c43 | 2018-06-19 12:31:33 +0200 | [diff] [blame] | 24 | #include "modules/audio_processing/aec3/moving_average.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_processing/aec3/render_signal_analyzer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 26 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/constructormagic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 30 | |
| 31 | class SuppressionGain { |
| 32 | public: |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 33 | SuppressionGain(const EchoCanceller3Config& config, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 34 | Aec3Optimization optimization, |
| 35 | int sample_rate_hz); |
| 36 | ~SuppressionGain(); |
| 37 | void GetGain( |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 38 | const std::array<float, kFftLengthBy2Plus1>& suppressor_input_spectrum, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 39 | const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum, |
| 40 | const std::array<float, kFftLengthBy2Plus1>& echo_spectrum, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 41 | const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 42 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum, |
| 43 | const FftData& linear_aec_fft, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 44 | const FftData& capture_fft, |
| 45 | const RenderSignalAnalyzer& render_signal_analyzer, |
| 46 | const AecState& aec_state, |
| 47 | const std::vector<std::vector<float>>& render, |
| 48 | float* high_bands_gain, |
| 49 | std::array<float, kFftLengthBy2Plus1>* low_band_gain); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 50 | |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 51 | // Toggles the usage of the initial state. |
| 52 | void SetInitialState(bool state); |
| 53 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 54 | private: |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 55 | // Computes the gain to apply for the bands beyond the first band. |
| 56 | float UpperBandsGain( |
| 57 | const std::array<float, kFftLengthBy2Plus1>& echo_spectrum, |
| 58 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum, |
| 59 | const absl::optional<int>& narrow_peak_band, |
| 60 | bool saturated_echo, |
| 61 | const std::vector<std::vector<float>>& render, |
| 62 | const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const; |
| 63 | |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 64 | void GainToNoAudibleEcho( |
| 65 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 66 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 67 | const std::array<float, kFftLengthBy2Plus1>& masker, |
| 68 | const std::array<float, kFftLengthBy2Plus1>& min_gain, |
| 69 | const std::array<float, kFftLengthBy2Plus1>& max_gain, |
| 70 | std::array<float, kFftLengthBy2Plus1>* gain) const; |
| 71 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 72 | void LowerBandGain( |
| 73 | bool stationary_with_low_power, |
| 74 | const AecState& aec_state, |
| 75 | const std::array<float, kFftLengthBy2Plus1>& suppressor_input, |
| 76 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 77 | const std::array<float, kFftLengthBy2Plus1>& residual_echo, |
| 78 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise, |
| 79 | std::array<float, kFftLengthBy2Plus1>* gain); |
| 80 | |
| 81 | void GetMinGain(rtc::ArrayView<const float> suppressor_input, |
| 82 | rtc::ArrayView<const float> weighted_residual_echo, |
| 83 | bool low_noise_render, |
| 84 | bool saturated_echo, |
| 85 | rtc::ArrayView<float> min_gain) const; |
| 86 | |
| 87 | void GetMaxGain(rtc::ArrayView<float> max_gain) const; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 88 | |
| 89 | class LowNoiseRenderDetector { |
| 90 | public: |
| 91 | bool Detect(const std::vector<std::vector<float>>& render); |
| 92 | |
| 93 | private: |
| 94 | float average_power_ = 32768.f * 32768.f; |
| 95 | }; |
| 96 | |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 97 | // Class for selecting whether the suppressor is in the nearend or echo state. |
| 98 | class DominantNearendDetector { |
| 99 | public: |
| 100 | explicit DominantNearendDetector( |
| 101 | const EchoCanceller3Config::Suppressor::DominantNearendDetection |
| 102 | config); |
| 103 | |
| 104 | // Returns whether the current state is the nearend state. |
| 105 | bool IsNearendState() const { return nearend_state_; } |
| 106 | |
| 107 | // Updates the state selection based on latest spectral estimates. |
| 108 | void Update(rtc::ArrayView<const float> nearend_spectrum, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 109 | rtc::ArrayView<const float> residual_echo_spectrum, |
Per Åhgren | 700b4a4 | 2018-10-23 21:21:37 +0200 | [diff] [blame] | 110 | rtc::ArrayView<const float> comfort_noise_spectrum, |
| 111 | bool initial_state); |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | const float enr_threshold_; |
Gustaf Ullberg | c9f9b87 | 2018-10-22 15:15:36 +0200 | [diff] [blame] | 115 | const float enr_exit_threshold_; |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 116 | const float snr_threshold_; |
| 117 | const int hold_duration_; |
| 118 | const int trigger_threshold_; |
Per Åhgren | 700b4a4 | 2018-10-23 21:21:37 +0200 | [diff] [blame] | 119 | const bool use_during_initial_phase_; |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 120 | |
| 121 | bool nearend_state_ = false; |
| 122 | int trigger_counter_ = 0; |
| 123 | int hold_counter_ = 0; |
| 124 | }; |
| 125 | |
| 126 | struct GainParameters { |
| 127 | explicit GainParameters( |
| 128 | const EchoCanceller3Config::Suppressor::Tuning& tuning); |
| 129 | const float max_inc_factor; |
| 130 | const float max_dec_factor_lf; |
| 131 | std::array<float, kFftLengthBy2Plus1> enr_transparent_; |
| 132 | std::array<float, kFftLengthBy2Plus1> enr_suppress_; |
| 133 | std::array<float, kFftLengthBy2Plus1> emr_transparent_; |
| 134 | }; |
| 135 | |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 136 | static int instance_count_; |
| 137 | std::unique_ptr<ApmDataDumper> data_dumper_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 138 | const Aec3Optimization optimization_; |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 139 | const EchoCanceller3Config config_; |
| 140 | const int state_change_duration_blocks_; |
| 141 | float one_by_state_change_duration_blocks_; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 142 | std::array<float, kFftLengthBy2Plus1> last_gain_; |
Gustaf Ullberg | 0e6375e | 2018-05-04 11:29:02 +0200 | [diff] [blame] | 143 | std::array<float, kFftLengthBy2Plus1> last_nearend_; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 144 | std::array<float, kFftLengthBy2Plus1> last_echo_; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 145 | LowNoiseRenderDetector low_render_detector_; |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 146 | bool initial_state_ = true; |
| 147 | int initial_state_change_counter_ = 0; |
Gustaf Ullberg | 8406c43 | 2018-06-19 12:31:33 +0200 | [diff] [blame] | 148 | aec3::MovingAverage moving_average_; |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 149 | const GainParameters nearend_params_; |
| 150 | const GainParameters normal_params_; |
| 151 | DominantNearendDetector dominant_nearend_detector_; |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 152 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 153 | RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } // namespace webrtc |
| 157 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 158 | #endif // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_ |