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" |
Gustaf Ullberg | f534a64 | 2019-11-25 16:13:58 +0100 | [diff] [blame] | 25 | #include "modules/audio_processing/aec3/nearend_detector.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "modules/audio_processing/aec3/render_signal_analyzer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 27 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/constructor_magic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 31 | |
| 32 | class SuppressionGain { |
| 33 | public: |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 34 | SuppressionGain(const EchoCanceller3Config& config, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 35 | Aec3Optimization optimization, |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 36 | int sample_rate_hz, |
| 37 | size_t num_capture_channels); |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 38 | ~SuppressionGain(); |
| 39 | void GetGain( |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 40 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 41 | nearend_spectrum, |
| 42 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum, |
| 43 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 44 | residual_echo_spectrum, |
| 45 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 46 | comfort_noise_spectrum, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 47 | const RenderSignalAnalyzer& render_signal_analyzer, |
| 48 | const AecState& aec_state, |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 49 | const std::vector<std::vector<std::vector<float>>>& render, |
Gustaf Ullberg | 7e4ad82 | 2020-10-22 14:36:37 +0200 | [diff] [blame] | 50 | bool clock_drift, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 51 | float* high_bands_gain, |
| 52 | std::array<float, kFftLengthBy2Plus1>* low_band_gain); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 53 | |
Gustaf Ullberg | 437d129 | 2021-04-20 13:48:57 +0200 | [diff] [blame^] | 54 | bool IsDominantNearend() { |
| 55 | return dominant_nearend_detector_->IsNearendState(); |
| 56 | } |
| 57 | |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 58 | // Toggles the usage of the initial state. |
| 59 | void SetInitialState(bool state); |
| 60 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 61 | private: |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 62 | // Computes the gain to apply for the bands beyond the first band. |
| 63 | float UpperBandsGain( |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 64 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum, |
| 65 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 66 | comfort_noise_spectrum, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 67 | const absl::optional<int>& narrow_peak_band, |
| 68 | bool saturated_echo, |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 69 | const std::vector<std::vector<std::vector<float>>>& render, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 70 | const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const; |
| 71 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 72 | void GainToNoAudibleEcho(const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 73 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 74 | const std::array<float, kFftLengthBy2Plus1>& masker, |
| 75 | std::array<float, kFftLengthBy2Plus1>* gain) const; |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 76 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 77 | void LowerBandGain( |
| 78 | bool stationary_with_low_power, |
| 79 | const AecState& aec_state, |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 80 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 81 | suppressor_input, |
| 82 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> residual_echo, |
| 83 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> comfort_noise, |
Gustaf Ullberg | 7e4ad82 | 2020-10-22 14:36:37 +0200 | [diff] [blame] | 84 | bool clock_drift, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 85 | std::array<float, kFftLengthBy2Plus1>* gain); |
| 86 | |
Gustaf Ullberg | 2bab5ad | 2019-04-15 17:15:37 +0200 | [diff] [blame] | 87 | void GetMinGain(rtc::ArrayView<const float> weighted_residual_echo, |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 88 | rtc::ArrayView<const float> last_nearend, |
| 89 | rtc::ArrayView<const float> last_echo, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 90 | bool low_noise_render, |
| 91 | bool saturated_echo, |
| 92 | rtc::ArrayView<float> min_gain) const; |
| 93 | |
| 94 | void GetMaxGain(rtc::ArrayView<float> max_gain) const; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 95 | |
| 96 | class LowNoiseRenderDetector { |
| 97 | public: |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 98 | bool Detect(const std::vector<std::vector<std::vector<float>>>& render); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | float average_power_ = 32768.f * 32768.f; |
| 102 | }; |
| 103 | |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 104 | struct GainParameters { |
| 105 | explicit GainParameters( |
| 106 | const EchoCanceller3Config::Suppressor::Tuning& tuning); |
| 107 | const float max_inc_factor; |
| 108 | const float max_dec_factor_lf; |
| 109 | std::array<float, kFftLengthBy2Plus1> enr_transparent_; |
| 110 | std::array<float, kFftLengthBy2Plus1> enr_suppress_; |
| 111 | std::array<float, kFftLengthBy2Plus1> emr_transparent_; |
| 112 | }; |
| 113 | |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 114 | static int instance_count_; |
| 115 | std::unique_ptr<ApmDataDumper> data_dumper_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 116 | const Aec3Optimization optimization_; |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 117 | const EchoCanceller3Config config_; |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 118 | const size_t num_capture_channels_; |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 119 | const int state_change_duration_blocks_; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 120 | std::array<float, kFftLengthBy2Plus1> last_gain_; |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 121 | std::vector<std::array<float, kFftLengthBy2Plus1>> last_nearend_; |
| 122 | std::vector<std::array<float, kFftLengthBy2Plus1>> last_echo_; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 123 | LowNoiseRenderDetector low_render_detector_; |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 124 | bool initial_state_ = true; |
| 125 | int initial_state_change_counter_ = 0; |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 126 | std::vector<aec3::MovingAverage> nearend_smoothers_; |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 127 | const GainParameters nearend_params_; |
| 128 | const GainParameters normal_params_; |
Gustaf Ullberg | f534a64 | 2019-11-25 16:13:58 +0100 | [diff] [blame] | 129 | std::unique_ptr<NearendDetector> dominant_nearend_detector_; |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 130 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 131 | RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace webrtc |
| 135 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 136 | #endif // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_ |