blob: a9f564a6869b36089ef517087683059794284f0e [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/aec3/vector_math.h"
Gustaf Ullberg216af842018-04-26 12:39:11 +020024#include "modules/audio_processing/logging/apm_data_dumper.h"
25#include "rtc_base/atomicops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +020027#include "system_wrappers/include/field_trial.h"
peahcf02cf12017-04-05 14:18:07 -070028
peah522d71b2017-02-23 05:16:26 -080029namespace webrtc {
30namespace {
31
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +020032bool EnableTransparencyImprovements() {
33 return !field_trial::IsEnabled(
34 "WebRTC-Aec3TransparencyImprovementsKillSwitch");
35}
36
peah1d680892017-05-23 04:07:10 -070037// Adjust the gains according to the presence of known external filters.
38void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080039 // Limit the low frequency gains to avoid the impact of the high-pass filter
40 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070041 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080042
43 // Limit the high frequency gains to avoid the impact of the anti-aliasing
44 // filter on the upper-frequency gains influencing the overall achieved
45 // gain. TODO(peah): Update this when new anti-aliasing filters are
46 // implemented.
peah86afe9d2017-04-06 15:45:32 -070047 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070048 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
49 std::for_each(
50 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
51 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
52 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080053}
54
peah1d680892017-05-23 04:07:10 -070055// Computes the gain to apply for the bands beyond the first band.
56float UpperBandsGain(
peah14c11a42017-07-11 06:13:43 -070057 const rtc::Optional<int>& narrow_peak_band,
peah1d680892017-05-23 04:07:10 -070058 bool saturated_echo,
59 const std::vector<std::vector<float>>& render,
60 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) {
61 RTC_DCHECK_LT(0, render.size());
peah86afe9d2017-04-06 15:45:32 -070062 if (render.size() == 1) {
63 return 1.f;
64 }
65
peah14c11a42017-07-11 06:13:43 -070066 if (narrow_peak_band &&
67 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
68 return 0.001f;
69 }
70
peah1d680892017-05-23 04:07:10 -070071 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
72 const float gain_below_8_khz = *std::min_element(
73 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
74
peah86afe9d2017-04-06 15:45:32 -070075 // Always attenuate the upper bands when there is saturated echo.
76 if (saturated_echo) {
peah1d680892017-05-23 04:07:10 -070077 return std::min(0.001f, gain_below_8_khz);
peah86afe9d2017-04-06 15:45:32 -070078 }
79
80 // Compute the upper and lower band energies.
peah1d680892017-05-23 04:07:10 -070081 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
82 const float low_band_energy =
83 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
84 float high_band_energy = 0.f;
peah86afe9d2017-04-06 15:45:32 -070085 for (size_t k = 1; k < render.size(); ++k) {
peah1d680892017-05-23 04:07:10 -070086 const float energy = std::accumulate(render[k].begin(), render[k].end(),
87 0.f, sum_of_squares);
88 high_band_energy = std::max(high_band_energy, energy);
peah86afe9d2017-04-06 15:45:32 -070089 }
90
91 // If there is more power in the lower frequencies than the upper frequencies,
peah1d680892017-05-23 04:07:10 -070092 // or if the power in upper frequencies is low, do not bound the gain in the
peah86afe9d2017-04-06 15:45:32 -070093 // upper bands.
peah1d680892017-05-23 04:07:10 -070094 float anti_howling_gain;
Per Åhgren38e2d952017-11-17 14:54:28 +010095 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
peah1d680892017-05-23 04:07:10 -070096 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
97 anti_howling_gain = 1.f;
98 } else {
99 // In all other cases, bound the gain for upper frequencies.
100 RTC_DCHECK_LE(low_band_energy, high_band_energy);
101 RTC_DCHECK_NE(0.f, high_band_energy);
102 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
peah86afe9d2017-04-06 15:45:32 -0700103 }
104
peah1d680892017-05-23 04:07:10 -0700105 // Choose the gain as the minimum of the lower and upper gains.
106 return std::min(gain_below_8_khz, anti_howling_gain);
107}
108
Per Åhgrenb02644f2018-04-17 11:52:17 +0200109// Scales the echo according to assessed audibility at the other end.
110void WeightEchoForAudibility(const EchoCanceller3Config& config,
111 rtc::ArrayView<const float> echo,
112 rtc::ArrayView<float> weighted_echo,
113 rtc::ArrayView<float> one_by_weighted_echo) {
114 RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
115 RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
116 RTC_DCHECK_EQ(kFftLengthBy2Plus1, one_by_weighted_echo.size());
117
118 auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
119 rtc::ArrayView<const float> echo,
120 rtc::ArrayView<float> weighted_echo,
121 rtc::ArrayView<float> one_by_weighted_echo) {
122 for (size_t k = begin; k < end; ++k) {
123 if (echo[k] < threshold) {
124 float tmp = (threshold - echo[k]) * normalizer;
125 weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp);
126 } else {
127 weighted_echo[k] = echo[k];
128 }
129 one_by_weighted_echo[k] =
130 weighted_echo[k] > 0.f ? 1.f / weighted_echo[k] : 1.f;
131 }
132 };
133
134 float threshold = config.echo_audibility.floor_power *
135 config.echo_audibility.audibility_threshold_lf;
136 float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
137 weigh(threshold, normalizer, 0, 3, echo, weighted_echo, one_by_weighted_echo);
138
139 threshold = config.echo_audibility.floor_power *
140 config.echo_audibility.audibility_threshold_mf;
141 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
142 weigh(threshold, normalizer, 3, 7, echo, weighted_echo, one_by_weighted_echo);
143
144 threshold = config.echo_audibility.floor_power *
145 config.echo_audibility.audibility_threshold_hf;
146 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
147 weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo,
148 one_by_weighted_echo);
149}
150
peah1d680892017-05-23 04:07:10 -0700151// Computes the gain to reduce the echo to a non audible level.
152void GainToNoAudibleEcho(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200153 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700154 bool low_noise_render,
155 bool saturated_echo,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200156 bool linear_echo_estimate,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200157 bool enable_transparency_improvements,
peah1d680892017-05-23 04:07:10 -0700158 const std::array<float, kFftLengthBy2Plus1>& nearend,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200159 const std::array<float, kFftLengthBy2Plus1>& weighted_echo,
peah1d680892017-05-23 04:07:10 -0700160 const std::array<float, kFftLengthBy2Plus1>& masker,
161 const std::array<float, kFftLengthBy2Plus1>& min_gain,
162 const std::array<float, kFftLengthBy2Plus1>& max_gain,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200163 const std::array<float, kFftLengthBy2Plus1>& one_by_weighted_echo,
peah1d680892017-05-23 04:07:10 -0700164 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgrenc65ce782017-10-09 13:01:39 +0200165 float nearend_masking_margin = 0.f;
Per Åhgren63b494d2017-12-06 11:32:38 +0100166 if (linear_echo_estimate) {
167 nearend_masking_margin =
168 low_noise_render
169 ? config.gain_mask.m9
170 : (saturated_echo ? config.gain_mask.m2 : config.gain_mask.m3);
Per Åhgrenc65ce782017-10-09 13:01:39 +0200171 } else {
Per Åhgren63b494d2017-12-06 11:32:38 +0100172 nearend_masking_margin = config.gain_mask.m7;
Per Åhgrenc65ce782017-10-09 13:01:39 +0200173 }
Per Åhgren7ddd4632017-10-25 02:59:45 +0200174
Per Åhgrend309b002017-10-09 23:50:44 +0200175 RTC_DCHECK_LE(0.f, nearend_masking_margin);
176 RTC_DCHECK_GT(1.f, nearend_masking_margin);
Per Åhgrend309b002017-10-09 23:50:44 +0200177
Per Åhgren63b494d2017-12-06 11:32:38 +0100178 const float masker_margin =
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200179 linear_echo_estimate
180 ? (enable_transparency_improvements ? config.gain_mask.m0
181 : config.gain_mask.m1)
182 : config.gain_mask.m8;
peah1d680892017-05-23 04:07:10 -0700183
184 for (size_t k = 0; k < gain->size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200185 const float unity_gain_masker = std::max(nearend[k], masker[k]);
186 RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker);
Per Åhgrenb02644f2018-04-17 11:52:17 +0200187 if (weighted_echo[k] <= nearend_masking_margin * unity_gain_masker ||
Per Åhgren7106d932017-10-09 08:25:18 +0200188 unity_gain_masker <= 0.f) {
peah1d680892017-05-23 04:07:10 -0700189 (*gain)[k] = 1.f;
190 } else {
Per Åhgrend309b002017-10-09 23:50:44 +0200191 RTC_DCHECK_LT(0.f, unity_gain_masker);
Per Åhgrend309b002017-10-09 23:50:44 +0200192 (*gain)[k] =
Per Åhgrenb02644f2018-04-17 11:52:17 +0200193 std::max(0.f, (1.f - config.gain_mask.gain_curve_slope *
194 weighted_echo[k] / unity_gain_masker) *
195 config.gain_mask.gain_curve_offset);
196 (*gain)[k] = std::max(masker_margin * masker[k] * one_by_weighted_echo[k],
197 (*gain)[k]);
peah1d680892017-05-23 04:07:10 -0700198 }
199
200 (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]);
201 }
202}
203
Per Åhgren85a11a32017-10-02 14:42:06 +0200204// TODO(peah): Make adaptive to take the actual filter error into account.
205constexpr size_t kUpperAccurateBandPlus1 = 29;
206
peah1d680892017-05-23 04:07:10 -0700207// Computes the signal output power that masks the echo signal.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200208void MaskingPower(const EchoCanceller3Config& config,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200209 bool enable_transparency_improvements,
peah8cee56f2017-08-24 22:36:53 -0700210 const std::array<float, kFftLengthBy2Plus1>& nearend,
peah1d680892017-05-23 04:07:10 -0700211 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
212 const std::array<float, kFftLengthBy2Plus1>& last_masker,
213 const std::array<float, kFftLengthBy2Plus1>& gain,
214 std::array<float, kFftLengthBy2Plus1>* masker) {
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200215 if (enable_transparency_improvements) {
216 std::copy(comfort_noise.begin(), comfort_noise.end(), masker->begin());
217 return;
218 }
219
Per Åhgrenb02644f2018-04-17 11:52:17 +0200220 // Apply masking over time.
221 float masking_factor = config.gain_mask.temporal_masking_lf;
222 auto limit = config.gain_mask.temporal_masking_lf_bands;
223 std::transform(
224 comfort_noise.begin(), comfort_noise.begin() + limit, last_masker.begin(),
225 masker->begin(),
226 [masking_factor](float a, float b) { return a + masking_factor * b; });
227 masking_factor = config.gain_mask.temporal_masking_hf;
228 std::transform(
229 comfort_noise.begin() + limit, comfort_noise.end(),
230 last_masker.begin() + limit, masker->begin() + limit,
231 [masking_factor](float a, float b) { return a + masking_factor * b; });
232
233 // Apply masking only between lower frequency bands.
peah1d680892017-05-23 04:07:10 -0700234 std::array<float, kFftLengthBy2Plus1> side_band_masker;
Per Åhgren7106d932017-10-09 08:25:18 +0200235 float max_nearend_after_gain = 0.f;
peah1d680892017-05-23 04:07:10 -0700236 for (size_t k = 0; k < gain.size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200237 const float nearend_after_gain = nearend[k] * gain[k];
238 max_nearend_after_gain =
239 std::max(max_nearend_after_gain, nearend_after_gain);
240 side_band_masker[k] = nearend_after_gain + comfort_noise[k];
peah1d680892017-05-23 04:07:10 -0700241 }
Per Åhgren85a11a32017-10-02 14:42:06 +0200242
Per Åhgren85a11a32017-10-02 14:42:06 +0200243 RTC_DCHECK_LT(kUpperAccurateBandPlus1, gain.size());
244 for (size_t k = 1; k < kUpperAccurateBandPlus1; ++k) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200245 (*masker)[k] += config.gain_mask.m5 *
Per Åhgren7106d932017-10-09 08:25:18 +0200246 (side_band_masker[k - 1] + side_band_masker[k + 1]);
peah1d680892017-05-23 04:07:10 -0700247 }
Per Åhgren7106d932017-10-09 08:25:18 +0200248
249 // Add full-band masking as a minimum value for the masker.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200250 const float min_masker = max_nearend_after_gain * config.gain_mask.m6;
Per Åhgren7106d932017-10-09 08:25:18 +0200251 std::for_each(masker->begin(), masker->end(),
252 [min_masker](float& a) { a = std::max(a, min_masker); });
peah1d680892017-05-23 04:07:10 -0700253}
254
Per Åhgren85a11a32017-10-02 14:42:06 +0200255// Limits the gain in the frequencies for which the adaptive filter has not
256// converged. Currently, these frequencies are not hardcoded to the frequencies
257// which are typically not excited by speech.
258// TODO(peah): Make adaptive to take the actual filter error into account.
259void AdjustNonConvergedFrequencies(
260 std::array<float, kFftLengthBy2Plus1>* gain) {
261 constexpr float oneByBandsInSum =
262 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
263 const float hf_gain_bound =
264 std::accumulate(gain->begin() + 20,
265 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
266 oneByBandsInSum;
267
268 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
269 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
270}
271
peah1d680892017-05-23 04:07:10 -0700272} // namespace
273
Gustaf Ullberg216af842018-04-26 12:39:11 +0200274int SuppressionGain::instance_count_ = 0;
275
peah1d680892017-05-23 04:07:10 -0700276// TODO(peah): Add further optimizations, in particular for the divisions.
277void SuppressionGain::LowerBandGain(
278 bool low_noise_render,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100279 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -0700280 const std::array<float, kFftLengthBy2Plus1>& nearend,
281 const std::array<float, kFftLengthBy2Plus1>& echo,
282 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
283 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100284 const bool saturated_echo = aec_state.SaturatedEcho();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100285 const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
286
Per Åhgrenb02644f2018-04-17 11:52:17 +0200287 // Weight echo power in terms of audibility. // Precompute 1/weighted echo
288 // (note that when the echo is zero, the precomputed value is never used).
289 std::array<float, kFftLengthBy2Plus1> weighted_echo;
290 std::array<float, kFftLengthBy2Plus1> one_by_weighted_echo;
291 WeightEchoForAudibility(config_, echo, weighted_echo, one_by_weighted_echo);
peah1d680892017-05-23 04:07:10 -0700292
293 // Compute the minimum gain as the attenuating gain to put the signal just
294 // above the zero sample values.
295 std::array<float, kFftLengthBy2Plus1> min_gain;
peah8cee56f2017-08-24 22:36:53 -0700296 const float min_echo_power =
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200297 low_noise_render ? config_.echo_audibility.low_render_limit
298 : config_.echo_audibility.normal_render_limit;
Per Åhgren31122d62018-04-10 16:33:55 +0200299 if (!saturated_echo) {
peah1d680892017-05-23 04:07:10 -0700300 for (size_t k = 0; k < nearend.size(); ++k) {
Per Åhgrenb02644f2018-04-17 11:52:17 +0200301 const float denom = std::min(nearend[k], weighted_echo[k]);
peah1d680892017-05-23 04:07:10 -0700302 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
303 min_gain[k] = std::min(min_gain[k], 1.f);
304 }
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200305 if (enable_transparency_improvements_) {
306 for (size_t k = 0; k < 6; ++k) {
307 // Make sure the gains of the low frequencies do not decrease too
308 // quickly after strong nearend.
309 if (last_nearend_[k] > last_echo_[k]) {
310 min_gain[k] =
311 std::max(min_gain[k],
312 last_gain_[k] * config_.gain_updates.max_dec_factor_lf);
313 min_gain[k] = std::min(min_gain[k], 1.f);
314 }
315 }
316 }
peah1d680892017-05-23 04:07:10 -0700317 } else {
318 min_gain.fill(0.f);
319 }
320
321 // Compute the maximum gain by limiting the gain increase from the previous
322 // gain.
323 std::array<float, kFftLengthBy2Plus1> max_gain;
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200324 if (enable_transparency_improvements_) {
325 for (size_t k = 0; k < gain->size(); ++k) {
326 max_gain[k] =
327 std::min(std::max(last_gain_[k] * config_.gain_updates.max_inc_factor,
328 config_.gain_updates.floor_first_increase),
329 1.f);
330 }
331 } else {
332 for (size_t k = 0; k < gain->size(); ++k) {
333 max_gain[k] =
334 std::min(std::max(last_gain_[k] * gain_increase_[k],
335 config_.gain_updates.floor_first_increase),
336 1.f);
337 }
peah1d680892017-05-23 04:07:10 -0700338 }
339
340 // Iteratively compute the gain required to attenuate the echo to a non
341 // noticeable level.
342 gain->fill(0.f);
Gustaf Ullberg216af842018-04-26 12:39:11 +0200343 std::array<float, kFftLengthBy2Plus1> masker;
peah1d680892017-05-23 04:07:10 -0700344 for (int k = 0; k < 2; ++k) {
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200345 MaskingPower(config_, enable_transparency_improvements_, nearend,
346 comfort_noise, last_masker_, *gain, &masker);
Per Åhgren63b494d2017-12-06 11:32:38 +0100347 GainToNoAudibleEcho(config_, low_noise_render, saturated_echo,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200348 linear_echo_estimate, enable_transparency_improvements_,
349 nearend, weighted_echo, masker, min_gain, max_gain,
350 one_by_weighted_echo, gain);
peah1d680892017-05-23 04:07:10 -0700351 AdjustForExternalFilters(gain);
352 }
353
Per Åhgren85a11a32017-10-02 14:42:06 +0200354 // Adjust the gain for frequencies which have not yet converged.
355 AdjustNonConvergedFrequencies(gain);
356
peah1d680892017-05-23 04:07:10 -0700357 // Update the allowed maximum gain increase.
Per Åhgren31122d62018-04-10 16:33:55 +0200358 UpdateGainIncrease(low_noise_render, linear_echo_estimate, saturated_echo,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200359 weighted_echo, *gain);
Per Åhgren1f33a372017-10-11 02:36:53 +0200360
peah1d680892017-05-23 04:07:10 -0700361 // Store data required for the gain computation of the next block.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200362 std::copy(nearend.begin(), nearend.end(), last_nearend_.begin());
Per Åhgrenb02644f2018-04-17 11:52:17 +0200363 std::copy(weighted_echo.begin(), weighted_echo.end(), last_echo_.begin());
peah1d680892017-05-23 04:07:10 -0700364 std::copy(gain->begin(), gain->end(), last_gain_.begin());
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200365 MaskingPower(config_, enable_transparency_improvements_, nearend,
366 comfort_noise, last_masker_, *gain, &last_masker_);
peah1d680892017-05-23 04:07:10 -0700367 aec3::VectorMath(optimization_).Sqrt(*gain);
Gustaf Ullberg216af842018-04-26 12:39:11 +0200368
369 // Debug outputs for the purpose of development and analysis.
370 data_dumper_->DumpRaw("aec3_suppressor_min_gain", min_gain);
371 data_dumper_->DumpRaw("aec3_suppressor_max_gain", max_gain);
372 data_dumper_->DumpRaw("aec3_suppressor_masker", masker);
373 data_dumper_->DumpRaw("aec3_suppressor_last_masker", last_masker_);
peah86afe9d2017-04-06 15:45:32 -0700374}
375
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200376SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200377 Aec3Optimization optimization,
378 int sample_rate_hz)
Gustaf Ullberg216af842018-04-26 12:39:11 +0200379 : data_dumper_(
380 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
381 optimization_(optimization),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100382 config_(config),
383 state_change_duration_blocks_(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200384 static_cast<int>(config_.filter.config_change_duration_blocks)),
385 coherence_gain_(sample_rate_hz,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200386 config_.suppressor.bands_with_reliable_coherence),
387 enable_transparency_improvements_(EnableTransparencyImprovements()) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100388 RTC_DCHECK_LT(0, state_change_duration_blocks_);
389 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700390 last_gain_.fill(1.f);
391 last_masker_.fill(0.f);
392 gain_increase_.fill(1.f);
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200393 last_nearend_.fill(0.f);
peah1d680892017-05-23 04:07:10 -0700394 last_echo_.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800395}
396
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200397SuppressionGain::~SuppressionGain() = default;
398
peah522d71b2017-02-23 05:16:26 -0800399void SuppressionGain::GetGain(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200400 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
401 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
402 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
403 const FftData& linear_aec_fft,
404 const FftData& render_fft,
405 const FftData& capture_fft,
peah14c11a42017-07-11 06:13:43 -0700406 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200407 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700408 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700409 float* high_bands_gain,
410 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
411 RTC_DCHECK(high_bands_gain);
412 RTC_DCHECK(low_band_gain);
413
peah1d680892017-05-23 04:07:10 -0700414 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100415 bool low_noise_render = low_render_detector_.Detect(render);
peah14c11a42017-07-11 06:13:43 -0700416 const rtc::Optional<int> narrow_peak_band =
417 render_signal_analyzer.NarrowPeakBand();
Gustaf Ullberg5bb98972018-04-25 12:54:59 +0200418 LowerBandGain(low_noise_render, aec_state, nearend_spectrum, echo_spectrum,
419 comfort_noise_spectrum, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700420
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200421 // Adjust the gain for bands where the coherence indicates not echo.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200422 if (config_.suppressor.bands_with_reliable_coherence > 0 &&
423 !enable_transparency_improvements_) {
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200424 std::array<float, kFftLengthBy2Plus1> G_coherence;
425 coherence_gain_.ComputeGain(linear_aec_fft, render_fft, capture_fft,
426 G_coherence);
427 for (size_t k = 0; k < config_.suppressor.bands_with_reliable_coherence;
428 ++k) {
429 (*low_band_gain)[k] = std::max((*low_band_gain)[k], G_coherence[k]);
430 }
431 }
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200432
433 // Limit the gain of the lower bands during start up and after resets.
434 const float gain_upper_bound = aec_state.SuppressionGainLimit();
435 if (gain_upper_bound < 1.f) {
436 for (size_t k = 0; k < low_band_gain->size(); ++k) {
437 (*low_band_gain)[k] = std::min((*low_band_gain)[k], gain_upper_bound);
438 }
439 }
440
441 // Compute the gain for the upper bands.
442 *high_bands_gain = UpperBandsGain(narrow_peak_band, aec_state.SaturatedEcho(),
443 render, *low_band_gain);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100444}
445
446void SuppressionGain::SetInitialState(bool state) {
447 initial_state_ = state;
448 if (state) {
449 initial_state_change_counter_ = state_change_duration_blocks_;
450 } else {
451 initial_state_change_counter_ = 0;
452 }
453}
454
455void SuppressionGain::UpdateGainIncrease(
456 bool low_noise_render,
457 bool linear_echo_estimate,
Per Åhgren31122d62018-04-10 16:33:55 +0200458 bool saturated_echo,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100459 const std::array<float, kFftLengthBy2Plus1>& echo,
460 const std::array<float, kFftLengthBy2Plus1>& new_gain) {
461 float max_inc;
462 float max_dec;
463 float rate_inc;
464 float rate_dec;
465 float min_inc;
466 float min_dec;
467
468 RTC_DCHECK_GE(state_change_duration_blocks_, initial_state_change_counter_);
469 if (initial_state_change_counter_ > 0) {
470 if (--initial_state_change_counter_ == 0) {
471 initial_state_ = false;
472 }
473 }
474 RTC_DCHECK_LE(0, initial_state_change_counter_);
475
476 // EchoCanceller3Config::GainUpdates
477 auto& p = config_.gain_updates;
478 if (!linear_echo_estimate) {
479 max_inc = p.nonlinear.max_inc;
480 max_dec = p.nonlinear.max_dec;
481 rate_inc = p.nonlinear.rate_inc;
482 rate_dec = p.nonlinear.rate_dec;
483 min_inc = p.nonlinear.min_inc;
484 min_dec = p.nonlinear.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200485 } else if (initial_state_ && !saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100486 if (initial_state_change_counter_ > 0) {
487 float change_factor =
488 initial_state_change_counter_ * one_by_state_change_duration_blocks_;
489
490 auto average = [](float from, float to, float from_weight) {
491 return from * from_weight + to * (1.f - from_weight);
492 };
493
494 max_inc = average(p.initial.max_inc, p.normal.max_inc, change_factor);
495 max_dec = average(p.initial.max_dec, p.normal.max_dec, change_factor);
496 rate_inc = average(p.initial.rate_inc, p.normal.rate_inc, change_factor);
497 rate_dec = average(p.initial.rate_dec, p.normal.rate_dec, change_factor);
498 min_inc = average(p.initial.min_inc, p.normal.min_inc, change_factor);
499 min_dec = average(p.initial.min_dec, p.normal.min_dec, change_factor);
500 } else {
501 max_inc = p.initial.max_inc;
502 max_dec = p.initial.max_dec;
503 rate_inc = p.initial.rate_inc;
504 rate_dec = p.initial.rate_dec;
505 min_inc = p.initial.min_inc;
506 min_dec = p.initial.min_dec;
507 }
508 } else if (low_noise_render) {
509 max_inc = p.low_noise.max_inc;
510 max_dec = p.low_noise.max_dec;
511 rate_inc = p.low_noise.rate_inc;
512 rate_dec = p.low_noise.rate_dec;
513 min_inc = p.low_noise.min_inc;
514 min_dec = p.low_noise.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200515 } else if (!saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100516 max_inc = p.normal.max_inc;
517 max_dec = p.normal.max_dec;
518 rate_inc = p.normal.rate_inc;
519 rate_dec = p.normal.rate_dec;
520 min_inc = p.normal.min_inc;
521 min_dec = p.normal.min_dec;
522 } else {
523 max_inc = p.saturation.max_inc;
524 max_dec = p.saturation.max_dec;
525 rate_inc = p.saturation.rate_inc;
526 rate_dec = p.saturation.rate_dec;
527 min_inc = p.saturation.min_inc;
528 min_dec = p.saturation.min_dec;
529 }
530
531 for (size_t k = 0; k < new_gain.size(); ++k) {
532 auto increase_update = [](float new_gain, float last_gain,
533 float current_inc, float max_inc, float min_inc,
534 float change_rate) {
535 return new_gain > last_gain ? std::min(max_inc, current_inc * change_rate)
536 : min_inc;
537 };
538
539 if (echo[k] > last_echo_[k]) {
540 gain_increase_[k] =
541 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
542 max_inc, min_inc, rate_inc);
543 } else {
544 gain_increase_[k] =
545 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
546 max_dec, min_dec, rate_dec);
547 }
548 }
peah1d680892017-05-23 04:07:10 -0700549}
peah86afe9d2017-04-06 15:45:32 -0700550
peah1d680892017-05-23 04:07:10 -0700551// Detects when the render signal can be considered to have low power and
552// consist of stationary noise.
553bool SuppressionGain::LowNoiseRenderDetector::Detect(
554 const std::vector<std::vector<float>>& render) {
555 float x2_sum = 0.f;
556 float x2_max = 0.f;
557 for (auto x_k : render[0]) {
558 const float x2 = x_k * x_k;
559 x2_sum += x2;
560 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800561 }
peah1d680892017-05-23 04:07:10 -0700562
563 constexpr float kThreshold = 50.f * 50.f * 64.f;
564 const bool low_noise_render =
565 average_power_ < kThreshold && x2_max < 3 * average_power_;
566 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
567 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800568}
569
570} // namespace webrtc