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 | #include "modules/audio_processing/aec3/suppression_gain.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 12 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | #include <math.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 15 | #include <algorithm> |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 16 | #include <numeric> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 17 | |
Gustaf Ullberg | 8406c43 | 2018-06-19 12:31:33 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/aec3/moving_average.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/aec3/vector_math.h" |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 23 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | namespace { |
| 26 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 27 | // Adjust the gains according to the presence of known external filters. |
| 28 | void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) { |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 29 | // Limit the low frequency gains to avoid the impact of the high-pass filter |
| 30 | // on the lower-frequency gain influencing the overall achieved gain. |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 31 | (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]); |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 32 | |
| 33 | // Limit the high frequency gains to avoid the impact of the anti-aliasing |
| 34 | // filter on the upper-frequency gains influencing the overall achieved |
| 35 | // gain. TODO(peah): Update this when new anti-aliasing filters are |
| 36 | // implemented. |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 37 | constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 38 | const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit]; |
| 39 | std::for_each( |
| 40 | gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1, |
| 41 | [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); }); |
| 42 | (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1]; |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 45 | // Scales the echo according to assessed audibility at the other end. |
| 46 | void WeightEchoForAudibility(const EchoCanceller3Config& config, |
| 47 | rtc::ArrayView<const float> echo, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 48 | rtc::ArrayView<float> weighted_echo) { |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 49 | RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size()); |
| 50 | RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size()); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 51 | |
| 52 | auto weigh = [](float threshold, float normalizer, size_t begin, size_t end, |
| 53 | rtc::ArrayView<const float> echo, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 54 | rtc::ArrayView<float> weighted_echo) { |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 55 | for (size_t k = begin; k < end; ++k) { |
| 56 | if (echo[k] < threshold) { |
| 57 | float tmp = (threshold - echo[k]) * normalizer; |
| 58 | weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp); |
| 59 | } else { |
| 60 | weighted_echo[k] = echo[k]; |
| 61 | } |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 62 | } |
| 63 | }; |
| 64 | |
| 65 | float threshold = config.echo_audibility.floor_power * |
| 66 | config.echo_audibility.audibility_threshold_lf; |
| 67 | float normalizer = 1.f / (threshold - config.echo_audibility.floor_power); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 68 | weigh(threshold, normalizer, 0, 3, echo, weighted_echo); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 69 | |
| 70 | threshold = config.echo_audibility.floor_power * |
| 71 | config.echo_audibility.audibility_threshold_mf; |
| 72 | normalizer = 1.f / (threshold - config.echo_audibility.floor_power); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 73 | weigh(threshold, normalizer, 3, 7, echo, weighted_echo); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 74 | |
| 75 | threshold = config.echo_audibility.floor_power * |
| 76 | config.echo_audibility.audibility_threshold_hf; |
| 77 | normalizer = 1.f / (threshold - config.echo_audibility.floor_power); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 78 | weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 81 | // TODO(peah): Make adaptive to take the actual filter error into account. |
| 82 | constexpr size_t kUpperAccurateBandPlus1 = 29; |
| 83 | |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 84 | // Limits the gain in the frequencies for which the adaptive filter has not |
| 85 | // converged. Currently, these frequencies are not hardcoded to the frequencies |
| 86 | // which are typically not excited by speech. |
| 87 | // TODO(peah): Make adaptive to take the actual filter error into account. |
| 88 | void AdjustNonConvergedFrequencies( |
| 89 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 90 | constexpr float oneByBandsInSum = |
| 91 | 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20); |
| 92 | const float hf_gain_bound = |
| 93 | std::accumulate(gain->begin() + 20, |
| 94 | gain->begin() + kUpperAccurateBandPlus1, 0.f) * |
| 95 | oneByBandsInSum; |
| 96 | |
| 97 | std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(), |
| 98 | [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); }); |
| 99 | } |
| 100 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 101 | } // namespace |
| 102 | |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 103 | int SuppressionGain::instance_count_ = 0; |
| 104 | |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 105 | float SuppressionGain::UpperBandsGain( |
| 106 | const std::array<float, kFftLengthBy2Plus1>& echo_spectrum, |
| 107 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum, |
| 108 | const absl::optional<int>& narrow_peak_band, |
| 109 | bool saturated_echo, |
| 110 | const std::vector<std::vector<float>>& render, |
| 111 | const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const { |
| 112 | RTC_DCHECK_LT(0, render.size()); |
| 113 | if (render.size() == 1) { |
| 114 | return 1.f; |
| 115 | } |
| 116 | |
| 117 | if (narrow_peak_band && |
| 118 | (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) { |
| 119 | return 0.001f; |
| 120 | } |
| 121 | |
| 122 | constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2; |
| 123 | const float gain_below_8_khz = *std::min_element( |
| 124 | low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end()); |
| 125 | |
| 126 | // Always attenuate the upper bands when there is saturated echo. |
| 127 | if (saturated_echo) { |
| 128 | return std::min(0.001f, gain_below_8_khz); |
| 129 | } |
| 130 | |
| 131 | // Compute the upper and lower band energies. |
| 132 | const auto sum_of_squares = [](float a, float b) { return a + b * b; }; |
| 133 | const float low_band_energy = |
| 134 | std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares); |
| 135 | float high_band_energy = 0.f; |
| 136 | for (size_t k = 1; k < render.size(); ++k) { |
| 137 | const float energy = std::accumulate(render[k].begin(), render[k].end(), |
| 138 | 0.f, sum_of_squares); |
| 139 | high_band_energy = std::max(high_band_energy, energy); |
| 140 | } |
| 141 | |
| 142 | // If there is more power in the lower frequencies than the upper frequencies, |
| 143 | // or if the power in upper frequencies is low, do not bound the gain in the |
| 144 | // upper bands. |
| 145 | float anti_howling_gain; |
| 146 | constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f; |
| 147 | if (high_band_energy < std::max(low_band_energy, kThreshold)) { |
| 148 | anti_howling_gain = 1.f; |
| 149 | } else { |
| 150 | // In all other cases, bound the gain for upper frequencies. |
| 151 | RTC_DCHECK_LE(low_band_energy, high_band_energy); |
| 152 | RTC_DCHECK_NE(0.f, high_band_energy); |
| 153 | anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy); |
| 154 | } |
| 155 | |
| 156 | // Bound the upper gain during significant echo activity. |
| 157 | auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) { |
| 158 | RTC_DCHECK_LE(16, spectrum.size()); |
| 159 | return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f); |
| 160 | }; |
| 161 | const float echo_sum = low_frequency_energy(echo_spectrum); |
| 162 | const float noise_sum = low_frequency_energy(comfort_noise_spectrum); |
| 163 | const auto& cfg = config_.suppressor.high_bands_suppression; |
| 164 | float gain_bound = 1.f; |
| 165 | if (echo_sum > cfg.enr_threshold * noise_sum && |
| 166 | !dominant_nearend_detector_.IsNearendState()) { |
| 167 | gain_bound = cfg.max_gain_during_echo; |
| 168 | } |
| 169 | |
| 170 | // Choose the gain as the minimum of the lower and upper gains. |
| 171 | return std::min(std::min(gain_below_8_khz, anti_howling_gain), gain_bound); |
| 172 | } |
| 173 | |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 174 | // Computes the gain to reduce the echo to a non audible level. |
| 175 | void SuppressionGain::GainToNoAudibleEcho( |
| 176 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 177 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 178 | const std::array<float, kFftLengthBy2Plus1>& masker, |
| 179 | const std::array<float, kFftLengthBy2Plus1>& min_gain, |
| 180 | const std::array<float, kFftLengthBy2Plus1>& max_gain, |
| 181 | std::array<float, kFftLengthBy2Plus1>* gain) const { |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 182 | const auto& p = dominant_nearend_detector_.IsNearendState() ? nearend_params_ |
| 183 | : normal_params_; |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 184 | for (size_t k = 0; k < gain->size(); ++k) { |
| 185 | float enr = echo[k] / (nearend[k] + 1.f); // Echo-to-nearend ratio. |
| 186 | float emr = echo[k] / (masker[k] + 1.f); // Echo-to-masker (noise) ratio. |
| 187 | float g = 1.0f; |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 188 | if (enr > p.enr_transparent_[k] && emr > p.emr_transparent_[k]) { |
| 189 | g = (p.enr_suppress_[k] - enr) / |
| 190 | (p.enr_suppress_[k] - p.enr_transparent_[k]); |
| 191 | g = std::max(g, p.emr_transparent_[k] / emr); |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 192 | } |
| 193 | (*gain)[k] = std::max(std::min(g, max_gain[k]), min_gain[k]); |
| 194 | } |
| 195 | } |
| 196 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 197 | // Compute the minimum gain as the attenuating gain to put the signal just |
| 198 | // above the zero sample values. |
| 199 | void SuppressionGain::GetMinGain( |
| 200 | rtc::ArrayView<const float> suppressor_input, |
| 201 | rtc::ArrayView<const float> weighted_residual_echo, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 202 | bool low_noise_render, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 203 | bool saturated_echo, |
| 204 | rtc::ArrayView<float> min_gain) const { |
Per Åhgren | 31122d6 | 2018-04-10 16:33:55 +0200 | [diff] [blame] | 205 | if (!saturated_echo) { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 206 | const float min_echo_power = |
| 207 | low_noise_render ? config_.echo_audibility.low_render_limit |
| 208 | : config_.echo_audibility.normal_render_limit; |
| 209 | |
| 210 | for (size_t k = 0; k < suppressor_input.size(); ++k) { |
| 211 | const float denom = |
| 212 | std::min(suppressor_input[k], weighted_residual_echo[k]); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 213 | min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f; |
| 214 | min_gain[k] = std::min(min_gain[k], 1.f); |
| 215 | } |
Gustaf Ullberg | ecb2d56 | 2018-08-23 15:11:38 +0200 | [diff] [blame] | 216 | for (size_t k = 0; k < 6; ++k) { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 217 | const auto& dec = dominant_nearend_detector_.IsNearendState() |
| 218 | ? nearend_params_.max_dec_factor_lf |
| 219 | : normal_params_.max_dec_factor_lf; |
| 220 | |
Gustaf Ullberg | ecb2d56 | 2018-08-23 15:11:38 +0200 | [diff] [blame] | 221 | // Make sure the gains of the low frequencies do not decrease too |
| 222 | // quickly after strong nearend. |
| 223 | if (last_nearend_[k] > last_echo_[k]) { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 224 | min_gain[k] = std::max(min_gain[k], last_gain_[k] * dec); |
Gustaf Ullberg | ecb2d56 | 2018-08-23 15:11:38 +0200 | [diff] [blame] | 225 | min_gain[k] = std::min(min_gain[k], 1.f); |
Gustaf Ullberg | 0e6375e | 2018-05-04 11:29:02 +0200 | [diff] [blame] | 226 | } |
| 227 | } |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 228 | } else { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 229 | std::fill(min_gain.begin(), min_gain.end(), 0.f); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 230 | } |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 231 | } |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 232 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 233 | // Compute the maximum gain by limiting the gain increase from the previous |
| 234 | // gain. |
| 235 | void SuppressionGain::GetMaxGain(rtc::ArrayView<float> max_gain) const { |
| 236 | const auto& inc = dominant_nearend_detector_.IsNearendState() |
| 237 | ? nearend_params_.max_inc_factor |
| 238 | : normal_params_.max_inc_factor; |
| 239 | const auto& floor = config_.suppressor.floor_first_increase; |
| 240 | for (size_t k = 0; k < max_gain.size(); ++k) { |
| 241 | max_gain[k] = std::min(std::max(last_gain_[k] * inc, floor), 1.f); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 242 | } |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // TODO(peah): Add further optimizations, in particular for the divisions. |
| 246 | void SuppressionGain::LowerBandGain( |
| 247 | bool low_noise_render, |
| 248 | const AecState& aec_state, |
| 249 | const std::array<float, kFftLengthBy2Plus1>& suppressor_input, |
| 250 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 251 | const std::array<float, kFftLengthBy2Plus1>& residual_echo, |
| 252 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise, |
| 253 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 254 | const bool saturated_echo = aec_state.SaturatedEcho(); |
| 255 | |
| 256 | // Weight echo power in terms of audibility. // Precompute 1/weighted echo |
| 257 | // (note that when the echo is zero, the precomputed value is never used). |
| 258 | std::array<float, kFftLengthBy2Plus1> weighted_residual_echo; |
| 259 | WeightEchoForAudibility(config_, residual_echo, weighted_residual_echo); |
| 260 | |
| 261 | std::array<float, kFftLengthBy2Plus1> min_gain; |
| 262 | GetMinGain(suppressor_input, weighted_residual_echo, low_noise_render, |
| 263 | saturated_echo, min_gain); |
| 264 | |
| 265 | std::array<float, kFftLengthBy2Plus1> max_gain; |
| 266 | GetMaxGain(max_gain); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 267 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 268 | GainToNoAudibleEcho(nearend, weighted_residual_echo, comfort_noise, |
| 269 | min_gain, max_gain, gain); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 270 | AdjustForExternalFilters(gain); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 271 | |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 272 | // Adjust the gain for frequencies which have not yet converged. |
| 273 | AdjustNonConvergedFrequencies(gain); |
| 274 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 275 | // Store data required for the gain computation of the next block. |
Gustaf Ullberg | 0e6375e | 2018-05-04 11:29:02 +0200 | [diff] [blame] | 276 | std::copy(nearend.begin(), nearend.end(), last_nearend_.begin()); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 277 | std::copy(weighted_residual_echo.begin(), weighted_residual_echo.end(), |
| 278 | last_echo_.begin()); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 279 | std::copy(gain->begin(), gain->end(), last_gain_.begin()); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 280 | aec3::VectorMath(optimization_).Sqrt(*gain); |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 281 | |
| 282 | // Debug outputs for the purpose of development and analysis. |
| 283 | data_dumper_->DumpRaw("aec3_suppressor_min_gain", min_gain); |
| 284 | data_dumper_->DumpRaw("aec3_suppressor_max_gain", max_gain); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 285 | data_dumper_->DumpRaw("aec3_dominant_nearend", |
| 286 | dominant_nearend_detector_.IsNearendState()); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 289 | SuppressionGain::SuppressionGain(const EchoCanceller3Config& config, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 290 | Aec3Optimization optimization, |
| 291 | int sample_rate_hz) |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 292 | : data_dumper_( |
| 293 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 294 | optimization_(optimization), |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 295 | config_(config), |
| 296 | state_change_duration_blocks_( |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 297 | static_cast<int>(config_.filter.config_change_duration_blocks)), |
Gustaf Ullberg | 8406c43 | 2018-06-19 12:31:33 +0200 | [diff] [blame] | 298 | moving_average_(kFftLengthBy2Plus1, |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 299 | config.suppressor.nearend_average_blocks), |
| 300 | nearend_params_(config_.suppressor.nearend_tuning), |
| 301 | normal_params_(config_.suppressor.normal_tuning), |
| 302 | dominant_nearend_detector_( |
| 303 | config_.suppressor.dominant_nearend_detection) { |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 304 | RTC_DCHECK_LT(0, state_change_duration_blocks_); |
| 305 | one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 306 | last_gain_.fill(1.f); |
Gustaf Ullberg | 0e6375e | 2018-05-04 11:29:02 +0200 | [diff] [blame] | 307 | last_nearend_.fill(0.f); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 308 | last_echo_.fill(0.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 311 | SuppressionGain::~SuppressionGain() = default; |
| 312 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 313 | void SuppressionGain::GetGain( |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 314 | const std::array<float, kFftLengthBy2Plus1>& suppressor_input_spectrum, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 315 | const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum, |
| 316 | const std::array<float, kFftLengthBy2Plus1>& echo_spectrum, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 317 | const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 318 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum, |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 319 | const RenderSignalAnalyzer& render_signal_analyzer, |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 320 | const AecState& aec_state, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 321 | const std::vector<std::vector<float>>& render, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 322 | float* high_bands_gain, |
| 323 | std::array<float, kFftLengthBy2Plus1>* low_band_gain) { |
| 324 | RTC_DCHECK(high_bands_gain); |
| 325 | RTC_DCHECK(low_band_gain); |
Per Åhgren | 7343f56 | 2018-08-17 10:08:34 +0200 | [diff] [blame] | 326 | const auto& cfg = config_.suppressor; |
| 327 | |
| 328 | if (cfg.enforce_transparent) { |
| 329 | low_band_gain->fill(1.f); |
| 330 | *high_bands_gain = cfg.enforce_empty_higher_bands ? 0.f : 1.f; |
| 331 | return; |
| 332 | } |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 333 | |
Gustaf Ullberg | 8406c43 | 2018-06-19 12:31:33 +0200 | [diff] [blame] | 334 | std::array<float, kFftLengthBy2Plus1> nearend_average; |
| 335 | moving_average_.Average(nearend_spectrum, nearend_average); |
| 336 | |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 337 | // Update the state selection. |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 338 | dominant_nearend_detector_.Update(nearend_spectrum, residual_echo_spectrum, |
Per Åhgren | 700b4a4 | 2018-10-23 21:21:37 +0200 | [diff] [blame] | 339 | comfort_noise_spectrum, initial_state_); |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 340 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 341 | // Compute gain for the lower band. |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 342 | bool low_noise_render = low_render_detector_.Detect(render); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 343 | LowerBandGain(low_noise_render, aec_state, suppressor_input_spectrum, |
| 344 | nearend_average, residual_echo_spectrum, comfort_noise_spectrum, |
| 345 | low_band_gain); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 346 | |
Gustaf Ullberg | 0cb4a25 | 2018-04-26 15:45:44 +0200 | [diff] [blame] | 347 | // Compute the gain for the upper bands. |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 348 | const absl::optional<int> narrow_peak_band = |
| 349 | render_signal_analyzer.NarrowPeakBand(); |
| 350 | |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 351 | *high_bands_gain = |
| 352 | UpperBandsGain(echo_spectrum, comfort_noise_spectrum, narrow_peak_band, |
| 353 | aec_state.SaturatedEcho(), render, *low_band_gain); |
Per Åhgren | 7343f56 | 2018-08-17 10:08:34 +0200 | [diff] [blame] | 354 | if (cfg.enforce_empty_higher_bands) { |
| 355 | *high_bands_gain = 0.f; |
| 356 | } |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void SuppressionGain::SetInitialState(bool state) { |
| 360 | initial_state_ = state; |
| 361 | if (state) { |
| 362 | initial_state_change_counter_ = state_change_duration_blocks_; |
| 363 | } else { |
| 364 | initial_state_change_counter_ = 0; |
| 365 | } |
| 366 | } |
| 367 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 368 | // Detects when the render signal can be considered to have low power and |
| 369 | // consist of stationary noise. |
| 370 | bool SuppressionGain::LowNoiseRenderDetector::Detect( |
| 371 | const std::vector<std::vector<float>>& render) { |
| 372 | float x2_sum = 0.f; |
| 373 | float x2_max = 0.f; |
| 374 | for (auto x_k : render[0]) { |
| 375 | const float x2 = x_k * x_k; |
| 376 | x2_sum += x2; |
| 377 | x2_max = std::max(x2_max, x2); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 378 | } |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 379 | |
| 380 | constexpr float kThreshold = 50.f * 50.f * 64.f; |
| 381 | const bool low_noise_render = |
| 382 | average_power_ < kThreshold && x2_max < 3 * average_power_; |
| 383 | average_power_ = average_power_ * 0.9f + x2_sum * 0.1f; |
| 384 | return low_noise_render; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 387 | SuppressionGain::DominantNearendDetector::DominantNearendDetector( |
| 388 | const EchoCanceller3Config::Suppressor::DominantNearendDetection config) |
| 389 | : enr_threshold_(config.enr_threshold), |
Gustaf Ullberg | c9f9b87 | 2018-10-22 15:15:36 +0200 | [diff] [blame] | 390 | enr_exit_threshold_(config.enr_exit_threshold), |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 391 | snr_threshold_(config.snr_threshold), |
| 392 | hold_duration_(config.hold_duration), |
Per Åhgren | 700b4a4 | 2018-10-23 21:21:37 +0200 | [diff] [blame] | 393 | trigger_threshold_(config.trigger_threshold), |
| 394 | use_during_initial_phase_(config.use_during_initial_phase) {} |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 395 | |
| 396 | void SuppressionGain::DominantNearendDetector::Update( |
| 397 | rtc::ArrayView<const float> nearend_spectrum, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 398 | rtc::ArrayView<const float> residual_echo_spectrum, |
Per Åhgren | 700b4a4 | 2018-10-23 21:21:37 +0200 | [diff] [blame] | 399 | rtc::ArrayView<const float> comfort_noise_spectrum, |
| 400 | bool initial_state) { |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 401 | auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) { |
| 402 | RTC_DCHECK_LE(16, spectrum.size()); |
| 403 | return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f); |
| 404 | }; |
| 405 | const float ne_sum = low_frequency_energy(nearend_spectrum); |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 406 | const float echo_sum = low_frequency_energy(residual_echo_spectrum); |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 407 | const float noise_sum = low_frequency_energy(comfort_noise_spectrum); |
| 408 | |
| 409 | // Detect strong active nearend if the nearend is sufficiently stronger than |
| 410 | // the echo and the nearend noise. |
Per Åhgren | 700b4a4 | 2018-10-23 21:21:37 +0200 | [diff] [blame] | 411 | if ((!initial_state || use_during_initial_phase_) && |
Gustaf Ullberg | de10eea | 2018-11-28 09:44:50 +0100 | [diff] [blame] | 412 | echo_sum < enr_threshold_ * ne_sum && |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 413 | ne_sum > snr_threshold_ * noise_sum) { |
| 414 | if (++trigger_counter_ >= trigger_threshold_) { |
| 415 | // After a period of strong active nearend activity, flag nearend mode. |
| 416 | hold_counter_ = hold_duration_; |
| 417 | trigger_counter_ = trigger_threshold_; |
| 418 | } |
| 419 | } else { |
| 420 | // Forget previously detected strong active nearend activity. |
| 421 | trigger_counter_ = std::max(0, trigger_counter_ - 1); |
| 422 | } |
| 423 | |
Gustaf Ullberg | c9f9b87 | 2018-10-22 15:15:36 +0200 | [diff] [blame] | 424 | // Exit nearend-state early at strong echo. |
Gustaf Ullberg | de10eea | 2018-11-28 09:44:50 +0100 | [diff] [blame] | 425 | if (echo_sum > enr_exit_threshold_ * ne_sum && |
Gustaf Ullberg | c9f9b87 | 2018-10-22 15:15:36 +0200 | [diff] [blame] | 426 | echo_sum > snr_threshold_ * noise_sum) { |
| 427 | hold_counter_ = 0; |
| 428 | } |
| 429 | |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 430 | // Remain in any nearend mode for a certain duration. |
| 431 | hold_counter_ = std::max(0, hold_counter_ - 1); |
| 432 | nearend_state_ = hold_counter_ > 0; |
| 433 | } |
| 434 | |
| 435 | SuppressionGain::GainParameters::GainParameters( |
| 436 | const EchoCanceller3Config::Suppressor::Tuning& tuning) |
| 437 | : max_inc_factor(tuning.max_inc_factor), |
| 438 | max_dec_factor_lf(tuning.max_dec_factor_lf) { |
| 439 | // Compute per-band masking thresholds. |
| 440 | constexpr size_t kLastLfBand = 5; |
| 441 | constexpr size_t kFirstHfBand = 8; |
| 442 | RTC_DCHECK_LT(kLastLfBand, kFirstHfBand); |
| 443 | auto& lf = tuning.mask_lf; |
| 444 | auto& hf = tuning.mask_hf; |
| 445 | RTC_DCHECK_LT(lf.enr_transparent, lf.enr_suppress); |
| 446 | RTC_DCHECK_LT(hf.enr_transparent, hf.enr_suppress); |
| 447 | for (size_t k = 0; k < kFftLengthBy2Plus1; k++) { |
| 448 | float a; |
| 449 | if (k <= kLastLfBand) { |
| 450 | a = 0.f; |
| 451 | } else if (k < kFirstHfBand) { |
| 452 | a = (k - kLastLfBand) / static_cast<float>(kFirstHfBand - kLastLfBand); |
| 453 | } else { |
| 454 | a = 1.f; |
| 455 | } |
| 456 | enr_transparent_[k] = (1 - a) * lf.enr_transparent + a * hf.enr_transparent; |
| 457 | enr_suppress_[k] = (1 - a) * lf.enr_suppress + a * hf.enr_suppress; |
| 458 | emr_transparent_[k] = (1 - a) * lf.emr_transparent + a * hf.emr_transparent; |
| 459 | } |
| 460 | } |
| 461 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 462 | } // namespace webrtc |