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