blob: 846836de384d08ba0a2b02bf44902987cd6b0d82 [file] [log] [blame]
Per Åhgren31122d62018-04-10 16:33:55 +02001
peah522d71b2017-02-23 05:16:26 -08002/*
3 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
4 *
5 * Use of this source code is governed by a BSD-style license
6 * that can be found in the LICENSE file in the root of the source
7 * tree. An additional intellectual property rights grant can be found
8 * in the file PATENTS. All contributing project authors may
9 * be found in the AUTHORS file in the root of the source tree.
10 */
11
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "modules/audio_processing/aec3/suppression_gain.h"
peah522d71b2017-02-23 05:16:26 -080013
Mirko Bonadei71207422017-09-15 13:58:09 +020014#include "typedefs.h" // NOLINT(build/include)
peah522d71b2017-02-23 05:16:26 -080015#if defined(WEBRTC_ARCH_X86_FAMILY)
16#include <emmintrin.h>
17#endif
18#include <math.h>
19#include <algorithm>
20#include <functional>
peah86afe9d2017-04-06 15:45:32 -070021#include <numeric>
peah522d71b2017-02-23 05:16:26 -080022
Gustaf Ullberg8406c432018-06-19 12:31:33 +020023#include "modules/audio_processing/aec3/moving_average.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/aec3/vector_math.h"
Gustaf Ullberg216af842018-04-26 12:39:11 +020025#include "modules/audio_processing/logging/apm_data_dumper.h"
26#include "rtc_base/atomicops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +020028#include "system_wrappers/include/field_trial.h"
peahcf02cf12017-04-05 14:18:07 -070029
peah522d71b2017-02-23 05:16:26 -080030namespace webrtc {
31namespace {
32
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +020033bool EnableTransparencyImprovements() {
34 return !field_trial::IsEnabled(
35 "WebRTC-Aec3TransparencyImprovementsKillSwitch");
36}
37
Gustaf Ullbergec642172018-07-03 13:48:32 +020038bool EnableNewSuppression() {
39 return !field_trial::IsEnabled("WebRTC-Aec3NewSuppressionKillSwitch");
40}
41
peah1d680892017-05-23 04:07:10 -070042// Adjust the gains according to the presence of known external filters.
43void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080044 // Limit the low frequency gains to avoid the impact of the high-pass filter
45 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070046 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080047
48 // Limit the high frequency gains to avoid the impact of the anti-aliasing
49 // filter on the upper-frequency gains influencing the overall achieved
50 // gain. TODO(peah): Update this when new anti-aliasing filters are
51 // implemented.
peah86afe9d2017-04-06 15:45:32 -070052 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070053 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
54 std::for_each(
55 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
56 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
57 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080058}
59
peah1d680892017-05-23 04:07:10 -070060// Computes the gain to apply for the bands beyond the first band.
61float UpperBandsGain(
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020062 const absl::optional<int>& narrow_peak_band,
peah1d680892017-05-23 04:07:10 -070063 bool saturated_echo,
64 const std::vector<std::vector<float>>& render,
65 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) {
66 RTC_DCHECK_LT(0, render.size());
peah86afe9d2017-04-06 15:45:32 -070067 if (render.size() == 1) {
68 return 1.f;
69 }
70
peah14c11a42017-07-11 06:13:43 -070071 if (narrow_peak_band &&
72 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
73 return 0.001f;
74 }
75
peah1d680892017-05-23 04:07:10 -070076 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
77 const float gain_below_8_khz = *std::min_element(
78 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
79
peah86afe9d2017-04-06 15:45:32 -070080 // Always attenuate the upper bands when there is saturated echo.
81 if (saturated_echo) {
peah1d680892017-05-23 04:07:10 -070082 return std::min(0.001f, gain_below_8_khz);
peah86afe9d2017-04-06 15:45:32 -070083 }
84
85 // Compute the upper and lower band energies.
peah1d680892017-05-23 04:07:10 -070086 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
87 const float low_band_energy =
88 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
89 float high_band_energy = 0.f;
peah86afe9d2017-04-06 15:45:32 -070090 for (size_t k = 1; k < render.size(); ++k) {
peah1d680892017-05-23 04:07:10 -070091 const float energy = std::accumulate(render[k].begin(), render[k].end(),
92 0.f, sum_of_squares);
93 high_band_energy = std::max(high_band_energy, energy);
peah86afe9d2017-04-06 15:45:32 -070094 }
95
96 // If there is more power in the lower frequencies than the upper frequencies,
peah1d680892017-05-23 04:07:10 -070097 // or if the power in upper frequencies is low, do not bound the gain in the
peah86afe9d2017-04-06 15:45:32 -070098 // upper bands.
peah1d680892017-05-23 04:07:10 -070099 float anti_howling_gain;
Per Åhgren38e2d952017-11-17 14:54:28 +0100100 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
peah1d680892017-05-23 04:07:10 -0700101 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
102 anti_howling_gain = 1.f;
103 } else {
104 // In all other cases, bound the gain for upper frequencies.
105 RTC_DCHECK_LE(low_band_energy, high_band_energy);
106 RTC_DCHECK_NE(0.f, high_band_energy);
107 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
peah86afe9d2017-04-06 15:45:32 -0700108 }
109
peah1d680892017-05-23 04:07:10 -0700110 // Choose the gain as the minimum of the lower and upper gains.
111 return std::min(gain_below_8_khz, anti_howling_gain);
112}
113
Per Åhgrenb02644f2018-04-17 11:52:17 +0200114// Scales the echo according to assessed audibility at the other end.
115void WeightEchoForAudibility(const EchoCanceller3Config& config,
116 rtc::ArrayView<const float> echo,
117 rtc::ArrayView<float> weighted_echo,
118 rtc::ArrayView<float> one_by_weighted_echo) {
119 RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
120 RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
121 RTC_DCHECK_EQ(kFftLengthBy2Plus1, one_by_weighted_echo.size());
122
123 auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
124 rtc::ArrayView<const float> echo,
125 rtc::ArrayView<float> weighted_echo,
126 rtc::ArrayView<float> one_by_weighted_echo) {
127 for (size_t k = begin; k < end; ++k) {
128 if (echo[k] < threshold) {
129 float tmp = (threshold - echo[k]) * normalizer;
130 weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp);
131 } else {
132 weighted_echo[k] = echo[k];
133 }
134 one_by_weighted_echo[k] =
135 weighted_echo[k] > 0.f ? 1.f / weighted_echo[k] : 1.f;
136 }
137 };
138
139 float threshold = config.echo_audibility.floor_power *
140 config.echo_audibility.audibility_threshold_lf;
141 float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
142 weigh(threshold, normalizer, 0, 3, echo, weighted_echo, one_by_weighted_echo);
143
144 threshold = config.echo_audibility.floor_power *
145 config.echo_audibility.audibility_threshold_mf;
146 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
147 weigh(threshold, normalizer, 3, 7, echo, weighted_echo, one_by_weighted_echo);
148
149 threshold = config.echo_audibility.floor_power *
150 config.echo_audibility.audibility_threshold_hf;
151 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
152 weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo,
153 one_by_weighted_echo);
154}
155
peah1d680892017-05-23 04:07:10 -0700156// Computes the gain to reduce the echo to a non audible level.
Gustaf Ullbergec642172018-07-03 13:48:32 +0200157void GainToNoAudibleEchoFallback(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200158 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700159 bool low_noise_render,
160 bool saturated_echo,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200161 bool linear_echo_estimate,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200162 bool enable_transparency_improvements,
peah1d680892017-05-23 04:07:10 -0700163 const std::array<float, kFftLengthBy2Plus1>& nearend,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200164 const std::array<float, kFftLengthBy2Plus1>& weighted_echo,
peah1d680892017-05-23 04:07:10 -0700165 const std::array<float, kFftLengthBy2Plus1>& masker,
166 const std::array<float, kFftLengthBy2Plus1>& min_gain,
167 const std::array<float, kFftLengthBy2Plus1>& max_gain,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200168 const std::array<float, kFftLengthBy2Plus1>& one_by_weighted_echo,
peah1d680892017-05-23 04:07:10 -0700169 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgrenc65ce782017-10-09 13:01:39 +0200170 float nearend_masking_margin = 0.f;
Per Åhgren63b494d2017-12-06 11:32:38 +0100171 if (linear_echo_estimate) {
172 nearend_masking_margin =
173 low_noise_render
174 ? config.gain_mask.m9
175 : (saturated_echo ? config.gain_mask.m2 : config.gain_mask.m3);
Per Åhgrenc65ce782017-10-09 13:01:39 +0200176 } else {
Per Åhgren63b494d2017-12-06 11:32:38 +0100177 nearend_masking_margin = config.gain_mask.m7;
Per Åhgrenc65ce782017-10-09 13:01:39 +0200178 }
Per Åhgren7ddd4632017-10-25 02:59:45 +0200179
Per Åhgrend309b002017-10-09 23:50:44 +0200180 RTC_DCHECK_LE(0.f, nearend_masking_margin);
181 RTC_DCHECK_GT(1.f, nearend_masking_margin);
Per Åhgrend309b002017-10-09 23:50:44 +0200182
Per Åhgren63b494d2017-12-06 11:32:38 +0100183 const float masker_margin =
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200184 linear_echo_estimate
185 ? (enable_transparency_improvements ? config.gain_mask.m0
186 : config.gain_mask.m1)
187 : config.gain_mask.m8;
peah1d680892017-05-23 04:07:10 -0700188
189 for (size_t k = 0; k < gain->size(); ++k) {
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200190 // TODO(devicentepena): Experiment by removing the reverberation estimation
191 // from the nearend signal before computing the gains.
Per Åhgren7106d932017-10-09 08:25:18 +0200192 const float unity_gain_masker = std::max(nearend[k], masker[k]);
193 RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker);
Per Åhgrenb02644f2018-04-17 11:52:17 +0200194 if (weighted_echo[k] <= nearend_masking_margin * unity_gain_masker ||
Per Åhgren7106d932017-10-09 08:25:18 +0200195 unity_gain_masker <= 0.f) {
peah1d680892017-05-23 04:07:10 -0700196 (*gain)[k] = 1.f;
197 } else {
Per Åhgrend309b002017-10-09 23:50:44 +0200198 RTC_DCHECK_LT(0.f, unity_gain_masker);
Per Åhgrend309b002017-10-09 23:50:44 +0200199 (*gain)[k] =
Per Åhgrenb02644f2018-04-17 11:52:17 +0200200 std::max(0.f, (1.f - config.gain_mask.gain_curve_slope *
201 weighted_echo[k] / unity_gain_masker) *
202 config.gain_mask.gain_curve_offset);
203 (*gain)[k] = std::max(masker_margin * masker[k] * one_by_weighted_echo[k],
204 (*gain)[k]);
peah1d680892017-05-23 04:07:10 -0700205 }
206
207 (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]);
208 }
209}
210
Per Åhgren85a11a32017-10-02 14:42:06 +0200211// TODO(peah): Make adaptive to take the actual filter error into account.
212constexpr size_t kUpperAccurateBandPlus1 = 29;
213
peah1d680892017-05-23 04:07:10 -0700214// Computes the signal output power that masks the echo signal.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200215void MaskingPower(const EchoCanceller3Config& config,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200216 bool enable_transparency_improvements,
peah8cee56f2017-08-24 22:36:53 -0700217 const std::array<float, kFftLengthBy2Plus1>& nearend,
peah1d680892017-05-23 04:07:10 -0700218 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
219 const std::array<float, kFftLengthBy2Plus1>& last_masker,
220 const std::array<float, kFftLengthBy2Plus1>& gain,
221 std::array<float, kFftLengthBy2Plus1>* masker) {
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200222 if (enable_transparency_improvements) {
223 std::copy(comfort_noise.begin(), comfort_noise.end(), masker->begin());
224 return;
225 }
226
Per Åhgrenb02644f2018-04-17 11:52:17 +0200227 // Apply masking over time.
228 float masking_factor = config.gain_mask.temporal_masking_lf;
229 auto limit = config.gain_mask.temporal_masking_lf_bands;
230 std::transform(
231 comfort_noise.begin(), comfort_noise.begin() + limit, last_masker.begin(),
232 masker->begin(),
233 [masking_factor](float a, float b) { return a + masking_factor * b; });
234 masking_factor = config.gain_mask.temporal_masking_hf;
235 std::transform(
236 comfort_noise.begin() + limit, comfort_noise.end(),
237 last_masker.begin() + limit, masker->begin() + limit,
238 [masking_factor](float a, float b) { return a + masking_factor * b; });
239
240 // Apply masking only between lower frequency bands.
peah1d680892017-05-23 04:07:10 -0700241 std::array<float, kFftLengthBy2Plus1> side_band_masker;
Per Åhgren7106d932017-10-09 08:25:18 +0200242 float max_nearend_after_gain = 0.f;
peah1d680892017-05-23 04:07:10 -0700243 for (size_t k = 0; k < gain.size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200244 const float nearend_after_gain = nearend[k] * gain[k];
245 max_nearend_after_gain =
246 std::max(max_nearend_after_gain, nearend_after_gain);
247 side_band_masker[k] = nearend_after_gain + comfort_noise[k];
peah1d680892017-05-23 04:07:10 -0700248 }
Per Åhgren85a11a32017-10-02 14:42:06 +0200249
Per Åhgren85a11a32017-10-02 14:42:06 +0200250 RTC_DCHECK_LT(kUpperAccurateBandPlus1, gain.size());
251 for (size_t k = 1; k < kUpperAccurateBandPlus1; ++k) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200252 (*masker)[k] += config.gain_mask.m5 *
Per Åhgren7106d932017-10-09 08:25:18 +0200253 (side_band_masker[k - 1] + side_band_masker[k + 1]);
peah1d680892017-05-23 04:07:10 -0700254 }
Per Åhgren7106d932017-10-09 08:25:18 +0200255
256 // Add full-band masking as a minimum value for the masker.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200257 const float min_masker = max_nearend_after_gain * config.gain_mask.m6;
Per Åhgren7106d932017-10-09 08:25:18 +0200258 std::for_each(masker->begin(), masker->end(),
259 [min_masker](float& a) { a = std::max(a, min_masker); });
peah1d680892017-05-23 04:07:10 -0700260}
261
Per Åhgren85a11a32017-10-02 14:42:06 +0200262// Limits the gain in the frequencies for which the adaptive filter has not
263// converged. Currently, these frequencies are not hardcoded to the frequencies
264// which are typically not excited by speech.
265// TODO(peah): Make adaptive to take the actual filter error into account.
266void AdjustNonConvergedFrequencies(
267 std::array<float, kFftLengthBy2Plus1>* gain) {
268 constexpr float oneByBandsInSum =
269 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
270 const float hf_gain_bound =
271 std::accumulate(gain->begin() + 20,
272 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
273 oneByBandsInSum;
274
275 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
276 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
277}
278
peah1d680892017-05-23 04:07:10 -0700279} // namespace
280
Gustaf Ullberg216af842018-04-26 12:39:11 +0200281int SuppressionGain::instance_count_ = 0;
282
Gustaf Ullbergec642172018-07-03 13:48:32 +0200283// Computes the gain to reduce the echo to a non audible level.
284void SuppressionGain::GainToNoAudibleEcho(
285 const std::array<float, kFftLengthBy2Plus1>& nearend,
286 const std::array<float, kFftLengthBy2Plus1>& echo,
287 const std::array<float, kFftLengthBy2Plus1>& masker,
288 const std::array<float, kFftLengthBy2Plus1>& min_gain,
289 const std::array<float, kFftLengthBy2Plus1>& max_gain,
290 std::array<float, kFftLengthBy2Plus1>* gain) const {
291 for (size_t k = 0; k < gain->size(); ++k) {
292 float enr = echo[k] / (nearend[k] + 1.f); // Echo-to-nearend ratio.
293 float emr = echo[k] / (masker[k] + 1.f); // Echo-to-masker (noise) ratio.
294 float g = 1.0f;
295 if (enr > enr_transparent_[k] && emr > emr_transparent_[k]) {
296 g = (enr_suppress_[k] - enr) / (enr_suppress_[k] - enr_transparent_[k]);
297 g = std::max(g, emr_transparent_[k] / emr);
298 }
299 (*gain)[k] = std::max(std::min(g, max_gain[k]), min_gain[k]);
300 }
301}
302
peah1d680892017-05-23 04:07:10 -0700303// TODO(peah): Add further optimizations, in particular for the divisions.
304void SuppressionGain::LowerBandGain(
305 bool low_noise_render,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100306 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -0700307 const std::array<float, kFftLengthBy2Plus1>& nearend,
308 const std::array<float, kFftLengthBy2Plus1>& echo,
309 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
310 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100311 const bool saturated_echo = aec_state.SaturatedEcho();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100312 const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
313
Per Åhgrenb02644f2018-04-17 11:52:17 +0200314 // Weight echo power in terms of audibility. // Precompute 1/weighted echo
315 // (note that when the echo is zero, the precomputed value is never used).
316 std::array<float, kFftLengthBy2Plus1> weighted_echo;
317 std::array<float, kFftLengthBy2Plus1> one_by_weighted_echo;
318 WeightEchoForAudibility(config_, echo, weighted_echo, one_by_weighted_echo);
peah1d680892017-05-23 04:07:10 -0700319
320 // Compute the minimum gain as the attenuating gain to put the signal just
321 // above the zero sample values.
322 std::array<float, kFftLengthBy2Plus1> min_gain;
peah8cee56f2017-08-24 22:36:53 -0700323 const float min_echo_power =
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200324 low_noise_render ? config_.echo_audibility.low_render_limit
325 : config_.echo_audibility.normal_render_limit;
Per Åhgren31122d62018-04-10 16:33:55 +0200326 if (!saturated_echo) {
peah1d680892017-05-23 04:07:10 -0700327 for (size_t k = 0; k < nearend.size(); ++k) {
Per Åhgrenb02644f2018-04-17 11:52:17 +0200328 const float denom = std::min(nearend[k], weighted_echo[k]);
peah1d680892017-05-23 04:07:10 -0700329 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
330 min_gain[k] = std::min(min_gain[k], 1.f);
331 }
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200332 if (enable_transparency_improvements_) {
333 for (size_t k = 0; k < 6; ++k) {
334 // Make sure the gains of the low frequencies do not decrease too
335 // quickly after strong nearend.
336 if (last_nearend_[k] > last_echo_[k]) {
337 min_gain[k] =
338 std::max(min_gain[k],
339 last_gain_[k] * config_.gain_updates.max_dec_factor_lf);
340 min_gain[k] = std::min(min_gain[k], 1.f);
341 }
342 }
343 }
peah1d680892017-05-23 04:07:10 -0700344 } else {
345 min_gain.fill(0.f);
346 }
347
348 // Compute the maximum gain by limiting the gain increase from the previous
349 // gain.
350 std::array<float, kFftLengthBy2Plus1> max_gain;
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200351 if (enable_transparency_improvements_) {
352 for (size_t k = 0; k < gain->size(); ++k) {
353 max_gain[k] =
354 std::min(std::max(last_gain_[k] * config_.gain_updates.max_inc_factor,
355 config_.gain_updates.floor_first_increase),
356 1.f);
357 }
358 } else {
359 for (size_t k = 0; k < gain->size(); ++k) {
360 max_gain[k] =
361 std::min(std::max(last_gain_[k] * gain_increase_[k],
362 config_.gain_updates.floor_first_increase),
363 1.f);
364 }
peah1d680892017-05-23 04:07:10 -0700365 }
366
367 // Iteratively compute the gain required to attenuate the echo to a non
368 // noticeable level.
Gustaf Ullberg216af842018-04-26 12:39:11 +0200369 std::array<float, kFftLengthBy2Plus1> masker;
Gustaf Ullbergec642172018-07-03 13:48:32 +0200370 if (enable_new_suppression_) {
371 GainToNoAudibleEcho(nearend, weighted_echo, comfort_noise, min_gain,
372 max_gain, gain);
peah1d680892017-05-23 04:07:10 -0700373 AdjustForExternalFilters(gain);
Gustaf Ullbergec642172018-07-03 13:48:32 +0200374 } else {
375 gain->fill(0.f);
376 for (int k = 0; k < 2; ++k) {
377 MaskingPower(config_, enable_transparency_improvements_, nearend,
378 comfort_noise, last_masker_, *gain, &masker);
379 GainToNoAudibleEchoFallback(
380 config_, low_noise_render, saturated_echo, linear_echo_estimate,
381 enable_transparency_improvements_, nearend, weighted_echo, masker,
382 min_gain, max_gain, one_by_weighted_echo, gain);
383 AdjustForExternalFilters(gain);
384 }
peah1d680892017-05-23 04:07:10 -0700385 }
386
Per Åhgren85a11a32017-10-02 14:42:06 +0200387 // Adjust the gain for frequencies which have not yet converged.
388 AdjustNonConvergedFrequencies(gain);
389
peah1d680892017-05-23 04:07:10 -0700390 // Update the allowed maximum gain increase.
Per Åhgren31122d62018-04-10 16:33:55 +0200391 UpdateGainIncrease(low_noise_render, linear_echo_estimate, saturated_echo,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200392 weighted_echo, *gain);
Per Åhgren1f33a372017-10-11 02:36:53 +0200393
peah1d680892017-05-23 04:07:10 -0700394 // Store data required for the gain computation of the next block.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200395 std::copy(nearend.begin(), nearend.end(), last_nearend_.begin());
Per Åhgrenb02644f2018-04-17 11:52:17 +0200396 std::copy(weighted_echo.begin(), weighted_echo.end(), last_echo_.begin());
peah1d680892017-05-23 04:07:10 -0700397 std::copy(gain->begin(), gain->end(), last_gain_.begin());
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200398 MaskingPower(config_, enable_transparency_improvements_, nearend,
399 comfort_noise, last_masker_, *gain, &last_masker_);
peah1d680892017-05-23 04:07:10 -0700400 aec3::VectorMath(optimization_).Sqrt(*gain);
Gustaf Ullberg216af842018-04-26 12:39:11 +0200401
402 // Debug outputs for the purpose of development and analysis.
403 data_dumper_->DumpRaw("aec3_suppressor_min_gain", min_gain);
404 data_dumper_->DumpRaw("aec3_suppressor_max_gain", max_gain);
405 data_dumper_->DumpRaw("aec3_suppressor_masker", masker);
406 data_dumper_->DumpRaw("aec3_suppressor_last_masker", last_masker_);
peah86afe9d2017-04-06 15:45:32 -0700407}
408
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200409SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200410 Aec3Optimization optimization,
411 int sample_rate_hz)
Gustaf Ullberg216af842018-04-26 12:39:11 +0200412 : data_dumper_(
413 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
414 optimization_(optimization),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100415 config_(config),
416 state_change_duration_blocks_(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200417 static_cast<int>(config_.filter.config_change_duration_blocks)),
418 coherence_gain_(sample_rate_hz,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200419 config_.suppressor.bands_with_reliable_coherence),
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200420 enable_transparency_improvements_(EnableTransparencyImprovements()),
Gustaf Ullbergec642172018-07-03 13:48:32 +0200421 enable_new_suppression_(EnableNewSuppression()),
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200422 moving_average_(kFftLengthBy2Plus1,
423 config.suppressor.nearend_average_blocks) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100424 RTC_DCHECK_LT(0, state_change_duration_blocks_);
425 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700426 last_gain_.fill(1.f);
427 last_masker_.fill(0.f);
428 gain_increase_.fill(1.f);
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200429 last_nearend_.fill(0.f);
peah1d680892017-05-23 04:07:10 -0700430 last_echo_.fill(0.f);
Gustaf Ullbergec642172018-07-03 13:48:32 +0200431
432 // Compute per-band masking thresholds.
433 constexpr size_t kLastLfBand = 5;
434 constexpr size_t kFirstHfBand = 8;
435 RTC_DCHECK_LT(kLastLfBand, kFirstHfBand);
436 auto& lf = config.suppressor.mask_lf;
437 auto& hf = config.suppressor.mask_hf;
438 RTC_DCHECK_LT(lf.enr_transparent, lf.enr_suppress);
439 RTC_DCHECK_LT(hf.enr_transparent, hf.enr_suppress);
440 for (size_t k = 0; k < kFftLengthBy2Plus1; k++) {
441 float a;
442 if (k <= kLastLfBand) {
443 a = 0.f;
444 } else if (k < kFirstHfBand) {
445 a = (k - kLastLfBand) / static_cast<float>(kFirstHfBand - kLastLfBand);
446 } else {
447 a = 1.f;
448 }
449 enr_transparent_[k] = (1 - a) * lf.enr_transparent + a * hf.enr_transparent;
450 enr_suppress_[k] = (1 - a) * lf.enr_suppress + a * hf.enr_suppress;
451 emr_transparent_[k] = (1 - a) * lf.emr_transparent + a * hf.emr_transparent;
452 }
peah522d71b2017-02-23 05:16:26 -0800453}
454
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200455SuppressionGain::~SuppressionGain() = default;
456
peah522d71b2017-02-23 05:16:26 -0800457void SuppressionGain::GetGain(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200458 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
459 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
460 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
461 const FftData& linear_aec_fft,
462 const FftData& render_fft,
463 const FftData& capture_fft,
peah14c11a42017-07-11 06:13:43 -0700464 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200465 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700466 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700467 float* high_bands_gain,
468 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
469 RTC_DCHECK(high_bands_gain);
470 RTC_DCHECK(low_band_gain);
471
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200472 std::array<float, kFftLengthBy2Plus1> nearend_average;
473 moving_average_.Average(nearend_spectrum, nearend_average);
474
peah1d680892017-05-23 04:07:10 -0700475 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100476 bool low_noise_render = low_render_detector_.Detect(render);
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200477 const absl::optional<int> narrow_peak_band =
peah14c11a42017-07-11 06:13:43 -0700478 render_signal_analyzer.NarrowPeakBand();
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200479 LowerBandGain(low_noise_render, aec_state, nearend_average, echo_spectrum,
Gustaf Ullberg5bb98972018-04-25 12:54:59 +0200480 comfort_noise_spectrum, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700481
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200482 // Adjust the gain for bands where the coherence indicates not echo.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200483 if (config_.suppressor.bands_with_reliable_coherence > 0 &&
484 !enable_transparency_improvements_) {
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200485 std::array<float, kFftLengthBy2Plus1> G_coherence;
486 coherence_gain_.ComputeGain(linear_aec_fft, render_fft, capture_fft,
487 G_coherence);
488 for (size_t k = 0; k < config_.suppressor.bands_with_reliable_coherence;
489 ++k) {
490 (*low_band_gain)[k] = std::max((*low_band_gain)[k], G_coherence[k]);
491 }
492 }
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200493
494 // Limit the gain of the lower bands during start up and after resets.
495 const float gain_upper_bound = aec_state.SuppressionGainLimit();
496 if (gain_upper_bound < 1.f) {
497 for (size_t k = 0; k < low_band_gain->size(); ++k) {
498 (*low_band_gain)[k] = std::min((*low_band_gain)[k], gain_upper_bound);
499 }
500 }
501
502 // Compute the gain for the upper bands.
503 *high_bands_gain = UpperBandsGain(narrow_peak_band, aec_state.SaturatedEcho(),
504 render, *low_band_gain);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100505}
506
507void SuppressionGain::SetInitialState(bool state) {
508 initial_state_ = state;
509 if (state) {
510 initial_state_change_counter_ = state_change_duration_blocks_;
511 } else {
512 initial_state_change_counter_ = 0;
513 }
514}
515
516void SuppressionGain::UpdateGainIncrease(
517 bool low_noise_render,
518 bool linear_echo_estimate,
Per Åhgren31122d62018-04-10 16:33:55 +0200519 bool saturated_echo,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100520 const std::array<float, kFftLengthBy2Plus1>& echo,
521 const std::array<float, kFftLengthBy2Plus1>& new_gain) {
522 float max_inc;
523 float max_dec;
524 float rate_inc;
525 float rate_dec;
526 float min_inc;
527 float min_dec;
528
529 RTC_DCHECK_GE(state_change_duration_blocks_, initial_state_change_counter_);
530 if (initial_state_change_counter_ > 0) {
531 if (--initial_state_change_counter_ == 0) {
532 initial_state_ = false;
533 }
534 }
535 RTC_DCHECK_LE(0, initial_state_change_counter_);
536
537 // EchoCanceller3Config::GainUpdates
538 auto& p = config_.gain_updates;
539 if (!linear_echo_estimate) {
540 max_inc = p.nonlinear.max_inc;
541 max_dec = p.nonlinear.max_dec;
542 rate_inc = p.nonlinear.rate_inc;
543 rate_dec = p.nonlinear.rate_dec;
544 min_inc = p.nonlinear.min_inc;
545 min_dec = p.nonlinear.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200546 } else if (initial_state_ && !saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100547 if (initial_state_change_counter_ > 0) {
548 float change_factor =
549 initial_state_change_counter_ * one_by_state_change_duration_blocks_;
550
551 auto average = [](float from, float to, float from_weight) {
552 return from * from_weight + to * (1.f - from_weight);
553 };
554
555 max_inc = average(p.initial.max_inc, p.normal.max_inc, change_factor);
556 max_dec = average(p.initial.max_dec, p.normal.max_dec, change_factor);
557 rate_inc = average(p.initial.rate_inc, p.normal.rate_inc, change_factor);
558 rate_dec = average(p.initial.rate_dec, p.normal.rate_dec, change_factor);
559 min_inc = average(p.initial.min_inc, p.normal.min_inc, change_factor);
560 min_dec = average(p.initial.min_dec, p.normal.min_dec, change_factor);
561 } else {
562 max_inc = p.initial.max_inc;
563 max_dec = p.initial.max_dec;
564 rate_inc = p.initial.rate_inc;
565 rate_dec = p.initial.rate_dec;
566 min_inc = p.initial.min_inc;
567 min_dec = p.initial.min_dec;
568 }
569 } else if (low_noise_render) {
570 max_inc = p.low_noise.max_inc;
571 max_dec = p.low_noise.max_dec;
572 rate_inc = p.low_noise.rate_inc;
573 rate_dec = p.low_noise.rate_dec;
574 min_inc = p.low_noise.min_inc;
575 min_dec = p.low_noise.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200576 } else if (!saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100577 max_inc = p.normal.max_inc;
578 max_dec = p.normal.max_dec;
579 rate_inc = p.normal.rate_inc;
580 rate_dec = p.normal.rate_dec;
581 min_inc = p.normal.min_inc;
582 min_dec = p.normal.min_dec;
583 } else {
584 max_inc = p.saturation.max_inc;
585 max_dec = p.saturation.max_dec;
586 rate_inc = p.saturation.rate_inc;
587 rate_dec = p.saturation.rate_dec;
588 min_inc = p.saturation.min_inc;
589 min_dec = p.saturation.min_dec;
590 }
591
592 for (size_t k = 0; k < new_gain.size(); ++k) {
593 auto increase_update = [](float new_gain, float last_gain,
594 float current_inc, float max_inc, float min_inc,
595 float change_rate) {
596 return new_gain > last_gain ? std::min(max_inc, current_inc * change_rate)
597 : min_inc;
598 };
599
600 if (echo[k] > last_echo_[k]) {
601 gain_increase_[k] =
602 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
603 max_inc, min_inc, rate_inc);
604 } else {
605 gain_increase_[k] =
606 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
607 max_dec, min_dec, rate_dec);
608 }
609 }
peah1d680892017-05-23 04:07:10 -0700610}
peah86afe9d2017-04-06 15:45:32 -0700611
peah1d680892017-05-23 04:07:10 -0700612// Detects when the render signal can be considered to have low power and
613// consist of stationary noise.
614bool SuppressionGain::LowNoiseRenderDetector::Detect(
615 const std::vector<std::vector<float>>& render) {
616 float x2_sum = 0.f;
617 float x2_max = 0.f;
618 for (auto x_k : render[0]) {
619 const float x2 = x_k * x_k;
620 x2_sum += x2;
621 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800622 }
peah1d680892017-05-23 04:07:10 -0700623
624 constexpr float kThreshold = 50.f * 50.f * 64.f;
625 const bool low_noise_render =
626 average_power_ < kThreshold && x2_max < 3 * average_power_;
627 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
628 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800629}
630
631} // namespace webrtc