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