blob: 2098fc25f8c1a1265ead13c4d362ff2b45b2dc99 [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(
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020057 const absl::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) {
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200185 // TODO(devicentepena): Experiment by removing the reverberation estimation
186 // from the nearend signal before computing the gains.
Per Åhgren7106d932017-10-09 08:25:18 +0200187 const float unity_gain_masker = std::max(nearend[k], masker[k]);
188 RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker);
Per Åhgrenb02644f2018-04-17 11:52:17 +0200189 if (weighted_echo[k] <= nearend_masking_margin * unity_gain_masker ||
Per Åhgren7106d932017-10-09 08:25:18 +0200190 unity_gain_masker <= 0.f) {
peah1d680892017-05-23 04:07:10 -0700191 (*gain)[k] = 1.f;
192 } else {
Per Åhgrend309b002017-10-09 23:50:44 +0200193 RTC_DCHECK_LT(0.f, unity_gain_masker);
Per Åhgrend309b002017-10-09 23:50:44 +0200194 (*gain)[k] =
Per Åhgrenb02644f2018-04-17 11:52:17 +0200195 std::max(0.f, (1.f - config.gain_mask.gain_curve_slope *
196 weighted_echo[k] / unity_gain_masker) *
197 config.gain_mask.gain_curve_offset);
198 (*gain)[k] = std::max(masker_margin * masker[k] * one_by_weighted_echo[k],
199 (*gain)[k]);
peah1d680892017-05-23 04:07:10 -0700200 }
201
202 (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]);
203 }
204}
205
Per Åhgren85a11a32017-10-02 14:42:06 +0200206// TODO(peah): Make adaptive to take the actual filter error into account.
207constexpr size_t kUpperAccurateBandPlus1 = 29;
208
peah1d680892017-05-23 04:07:10 -0700209// Computes the signal output power that masks the echo signal.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200210void MaskingPower(const EchoCanceller3Config& config,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200211 bool enable_transparency_improvements,
peah8cee56f2017-08-24 22:36:53 -0700212 const std::array<float, kFftLengthBy2Plus1>& nearend,
peah1d680892017-05-23 04:07:10 -0700213 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
214 const std::array<float, kFftLengthBy2Plus1>& last_masker,
215 const std::array<float, kFftLengthBy2Plus1>& gain,
216 std::array<float, kFftLengthBy2Plus1>* masker) {
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200217 if (enable_transparency_improvements) {
218 std::copy(comfort_noise.begin(), comfort_noise.end(), masker->begin());
219 return;
220 }
221
Per Åhgrenb02644f2018-04-17 11:52:17 +0200222 // Apply masking over time.
223 float masking_factor = config.gain_mask.temporal_masking_lf;
224 auto limit = config.gain_mask.temporal_masking_lf_bands;
225 std::transform(
226 comfort_noise.begin(), comfort_noise.begin() + limit, last_masker.begin(),
227 masker->begin(),
228 [masking_factor](float a, float b) { return a + masking_factor * b; });
229 masking_factor = config.gain_mask.temporal_masking_hf;
230 std::transform(
231 comfort_noise.begin() + limit, comfort_noise.end(),
232 last_masker.begin() + limit, masker->begin() + limit,
233 [masking_factor](float a, float b) { return a + masking_factor * b; });
234
235 // Apply masking only between lower frequency bands.
peah1d680892017-05-23 04:07:10 -0700236 std::array<float, kFftLengthBy2Plus1> side_band_masker;
Per Åhgren7106d932017-10-09 08:25:18 +0200237 float max_nearend_after_gain = 0.f;
peah1d680892017-05-23 04:07:10 -0700238 for (size_t k = 0; k < gain.size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200239 const float nearend_after_gain = nearend[k] * gain[k];
240 max_nearend_after_gain =
241 std::max(max_nearend_after_gain, nearend_after_gain);
242 side_band_masker[k] = nearend_after_gain + comfort_noise[k];
peah1d680892017-05-23 04:07:10 -0700243 }
Per Åhgren85a11a32017-10-02 14:42:06 +0200244
Per Åhgren85a11a32017-10-02 14:42:06 +0200245 RTC_DCHECK_LT(kUpperAccurateBandPlus1, gain.size());
246 for (size_t k = 1; k < kUpperAccurateBandPlus1; ++k) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200247 (*masker)[k] += config.gain_mask.m5 *
Per Åhgren7106d932017-10-09 08:25:18 +0200248 (side_band_masker[k - 1] + side_band_masker[k + 1]);
peah1d680892017-05-23 04:07:10 -0700249 }
Per Åhgren7106d932017-10-09 08:25:18 +0200250
251 // Add full-band masking as a minimum value for the masker.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200252 const float min_masker = max_nearend_after_gain * config.gain_mask.m6;
Per Åhgren7106d932017-10-09 08:25:18 +0200253 std::for_each(masker->begin(), masker->end(),
254 [min_masker](float& a) { a = std::max(a, min_masker); });
peah1d680892017-05-23 04:07:10 -0700255}
256
Per Åhgren85a11a32017-10-02 14:42:06 +0200257// Limits the gain in the frequencies for which the adaptive filter has not
258// converged. Currently, these frequencies are not hardcoded to the frequencies
259// which are typically not excited by speech.
260// TODO(peah): Make adaptive to take the actual filter error into account.
261void AdjustNonConvergedFrequencies(
262 std::array<float, kFftLengthBy2Plus1>* gain) {
263 constexpr float oneByBandsInSum =
264 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
265 const float hf_gain_bound =
266 std::accumulate(gain->begin() + 20,
267 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
268 oneByBandsInSum;
269
270 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
271 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
272}
273
peah1d680892017-05-23 04:07:10 -0700274} // namespace
275
Gustaf Ullberg216af842018-04-26 12:39:11 +0200276int SuppressionGain::instance_count_ = 0;
277
peah1d680892017-05-23 04:07:10 -0700278// TODO(peah): Add further optimizations, in particular for the divisions.
279void SuppressionGain::LowerBandGain(
280 bool low_noise_render,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100281 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -0700282 const std::array<float, kFftLengthBy2Plus1>& nearend,
283 const std::array<float, kFftLengthBy2Plus1>& echo,
284 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
285 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100286 const bool saturated_echo = aec_state.SaturatedEcho();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100287 const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
288
Per Åhgrenb02644f2018-04-17 11:52:17 +0200289 // Weight echo power in terms of audibility. // Precompute 1/weighted echo
290 // (note that when the echo is zero, the precomputed value is never used).
291 std::array<float, kFftLengthBy2Plus1> weighted_echo;
292 std::array<float, kFftLengthBy2Plus1> one_by_weighted_echo;
293 WeightEchoForAudibility(config_, echo, weighted_echo, one_by_weighted_echo);
peah1d680892017-05-23 04:07:10 -0700294
295 // Compute the minimum gain as the attenuating gain to put the signal just
296 // above the zero sample values.
297 std::array<float, kFftLengthBy2Plus1> min_gain;
peah8cee56f2017-08-24 22:36:53 -0700298 const float min_echo_power =
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200299 low_noise_render ? config_.echo_audibility.low_render_limit
300 : config_.echo_audibility.normal_render_limit;
Per Åhgren31122d62018-04-10 16:33:55 +0200301 if (!saturated_echo) {
peah1d680892017-05-23 04:07:10 -0700302 for (size_t k = 0; k < nearend.size(); ++k) {
Per Åhgrenb02644f2018-04-17 11:52:17 +0200303 const float denom = std::min(nearend[k], weighted_echo[k]);
peah1d680892017-05-23 04:07:10 -0700304 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
305 min_gain[k] = std::min(min_gain[k], 1.f);
306 }
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200307 if (enable_transparency_improvements_) {
308 for (size_t k = 0; k < 6; ++k) {
309 // Make sure the gains of the low frequencies do not decrease too
310 // quickly after strong nearend.
311 if (last_nearend_[k] > last_echo_[k]) {
312 min_gain[k] =
313 std::max(min_gain[k],
314 last_gain_[k] * config_.gain_updates.max_dec_factor_lf);
315 min_gain[k] = std::min(min_gain[k], 1.f);
316 }
317 }
318 }
peah1d680892017-05-23 04:07:10 -0700319 } else {
320 min_gain.fill(0.f);
321 }
322
323 // Compute the maximum gain by limiting the gain increase from the previous
324 // gain.
325 std::array<float, kFftLengthBy2Plus1> max_gain;
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200326 if (enable_transparency_improvements_) {
327 for (size_t k = 0; k < gain->size(); ++k) {
328 max_gain[k] =
329 std::min(std::max(last_gain_[k] * config_.gain_updates.max_inc_factor,
330 config_.gain_updates.floor_first_increase),
331 1.f);
332 }
333 } else {
334 for (size_t k = 0; k < gain->size(); ++k) {
335 max_gain[k] =
336 std::min(std::max(last_gain_[k] * gain_increase_[k],
337 config_.gain_updates.floor_first_increase),
338 1.f);
339 }
peah1d680892017-05-23 04:07:10 -0700340 }
341
342 // Iteratively compute the gain required to attenuate the echo to a non
343 // noticeable level.
344 gain->fill(0.f);
Gustaf Ullberg216af842018-04-26 12:39:11 +0200345 std::array<float, kFftLengthBy2Plus1> masker;
peah1d680892017-05-23 04:07:10 -0700346 for (int k = 0; k < 2; ++k) {
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200347 MaskingPower(config_, enable_transparency_improvements_, nearend,
348 comfort_noise, last_masker_, *gain, &masker);
Per Åhgren63b494d2017-12-06 11:32:38 +0100349 GainToNoAudibleEcho(config_, low_noise_render, saturated_echo,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200350 linear_echo_estimate, enable_transparency_improvements_,
351 nearend, weighted_echo, masker, min_gain, max_gain,
352 one_by_weighted_echo, gain);
peah1d680892017-05-23 04:07:10 -0700353 AdjustForExternalFilters(gain);
354 }
355
Per Åhgren85a11a32017-10-02 14:42:06 +0200356 // Adjust the gain for frequencies which have not yet converged.
357 AdjustNonConvergedFrequencies(gain);
358
peah1d680892017-05-23 04:07:10 -0700359 // Update the allowed maximum gain increase.
Per Åhgren31122d62018-04-10 16:33:55 +0200360 UpdateGainIncrease(low_noise_render, linear_echo_estimate, saturated_echo,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200361 weighted_echo, *gain);
Per Åhgren1f33a372017-10-11 02:36:53 +0200362
peah1d680892017-05-23 04:07:10 -0700363 // Store data required for the gain computation of the next block.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200364 std::copy(nearend.begin(), nearend.end(), last_nearend_.begin());
Per Åhgrenb02644f2018-04-17 11:52:17 +0200365 std::copy(weighted_echo.begin(), weighted_echo.end(), last_echo_.begin());
peah1d680892017-05-23 04:07:10 -0700366 std::copy(gain->begin(), gain->end(), last_gain_.begin());
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200367 MaskingPower(config_, enable_transparency_improvements_, nearend,
368 comfort_noise, last_masker_, *gain, &last_masker_);
peah1d680892017-05-23 04:07:10 -0700369 aec3::VectorMath(optimization_).Sqrt(*gain);
Gustaf Ullberg216af842018-04-26 12:39:11 +0200370
371 // Debug outputs for the purpose of development and analysis.
372 data_dumper_->DumpRaw("aec3_suppressor_min_gain", min_gain);
373 data_dumper_->DumpRaw("aec3_suppressor_max_gain", max_gain);
374 data_dumper_->DumpRaw("aec3_suppressor_masker", masker);
375 data_dumper_->DumpRaw("aec3_suppressor_last_masker", last_masker_);
peah86afe9d2017-04-06 15:45:32 -0700376}
377
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200378SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200379 Aec3Optimization optimization,
380 int sample_rate_hz)
Gustaf Ullberg216af842018-04-26 12:39:11 +0200381 : data_dumper_(
382 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
383 optimization_(optimization),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100384 config_(config),
385 state_change_duration_blocks_(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200386 static_cast<int>(config_.filter.config_change_duration_blocks)),
387 coherence_gain_(sample_rate_hz,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200388 config_.suppressor.bands_with_reliable_coherence),
389 enable_transparency_improvements_(EnableTransparencyImprovements()) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100390 RTC_DCHECK_LT(0, state_change_duration_blocks_);
391 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700392 last_gain_.fill(1.f);
393 last_masker_.fill(0.f);
394 gain_increase_.fill(1.f);
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200395 last_nearend_.fill(0.f);
peah1d680892017-05-23 04:07:10 -0700396 last_echo_.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800397}
398
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200399SuppressionGain::~SuppressionGain() = default;
400
peah522d71b2017-02-23 05:16:26 -0800401void SuppressionGain::GetGain(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200402 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
403 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
404 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
405 const FftData& linear_aec_fft,
406 const FftData& render_fft,
407 const FftData& capture_fft,
peah14c11a42017-07-11 06:13:43 -0700408 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200409 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700410 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700411 float* high_bands_gain,
412 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
413 RTC_DCHECK(high_bands_gain);
414 RTC_DCHECK(low_band_gain);
415
peah1d680892017-05-23 04:07:10 -0700416 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100417 bool low_noise_render = low_render_detector_.Detect(render);
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200418 const absl::optional<int> narrow_peak_band =
peah14c11a42017-07-11 06:13:43 -0700419 render_signal_analyzer.NarrowPeakBand();
Gustaf Ullberg5bb98972018-04-25 12:54:59 +0200420 LowerBandGain(low_noise_render, aec_state, nearend_spectrum, echo_spectrum,
421 comfort_noise_spectrum, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700422
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200423 // Adjust the gain for bands where the coherence indicates not echo.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200424 if (config_.suppressor.bands_with_reliable_coherence > 0 &&
425 !enable_transparency_improvements_) {
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200426 std::array<float, kFftLengthBy2Plus1> G_coherence;
427 coherence_gain_.ComputeGain(linear_aec_fft, render_fft, capture_fft,
428 G_coherence);
429 for (size_t k = 0; k < config_.suppressor.bands_with_reliable_coherence;
430 ++k) {
431 (*low_band_gain)[k] = std::max((*low_band_gain)[k], G_coherence[k]);
432 }
433 }
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200434
435 // Limit the gain of the lower bands during start up and after resets.
436 const float gain_upper_bound = aec_state.SuppressionGainLimit();
437 if (gain_upper_bound < 1.f) {
438 for (size_t k = 0; k < low_band_gain->size(); ++k) {
439 (*low_band_gain)[k] = std::min((*low_band_gain)[k], gain_upper_bound);
440 }
441 }
442
443 // Compute the gain for the upper bands.
444 *high_bands_gain = UpperBandsGain(narrow_peak_band, aec_state.SaturatedEcho(),
445 render, *low_band_gain);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100446}
447
448void SuppressionGain::SetInitialState(bool state) {
449 initial_state_ = state;
450 if (state) {
451 initial_state_change_counter_ = state_change_duration_blocks_;
452 } else {
453 initial_state_change_counter_ = 0;
454 }
455}
456
457void SuppressionGain::UpdateGainIncrease(
458 bool low_noise_render,
459 bool linear_echo_estimate,
Per Åhgren31122d62018-04-10 16:33:55 +0200460 bool saturated_echo,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100461 const std::array<float, kFftLengthBy2Plus1>& echo,
462 const std::array<float, kFftLengthBy2Plus1>& new_gain) {
463 float max_inc;
464 float max_dec;
465 float rate_inc;
466 float rate_dec;
467 float min_inc;
468 float min_dec;
469
470 RTC_DCHECK_GE(state_change_duration_blocks_, initial_state_change_counter_);
471 if (initial_state_change_counter_ > 0) {
472 if (--initial_state_change_counter_ == 0) {
473 initial_state_ = false;
474 }
475 }
476 RTC_DCHECK_LE(0, initial_state_change_counter_);
477
478 // EchoCanceller3Config::GainUpdates
479 auto& p = config_.gain_updates;
480 if (!linear_echo_estimate) {
481 max_inc = p.nonlinear.max_inc;
482 max_dec = p.nonlinear.max_dec;
483 rate_inc = p.nonlinear.rate_inc;
484 rate_dec = p.nonlinear.rate_dec;
485 min_inc = p.nonlinear.min_inc;
486 min_dec = p.nonlinear.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200487 } else if (initial_state_ && !saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100488 if (initial_state_change_counter_ > 0) {
489 float change_factor =
490 initial_state_change_counter_ * one_by_state_change_duration_blocks_;
491
492 auto average = [](float from, float to, float from_weight) {
493 return from * from_weight + to * (1.f - from_weight);
494 };
495
496 max_inc = average(p.initial.max_inc, p.normal.max_inc, change_factor);
497 max_dec = average(p.initial.max_dec, p.normal.max_dec, change_factor);
498 rate_inc = average(p.initial.rate_inc, p.normal.rate_inc, change_factor);
499 rate_dec = average(p.initial.rate_dec, p.normal.rate_dec, change_factor);
500 min_inc = average(p.initial.min_inc, p.normal.min_inc, change_factor);
501 min_dec = average(p.initial.min_dec, p.normal.min_dec, change_factor);
502 } else {
503 max_inc = p.initial.max_inc;
504 max_dec = p.initial.max_dec;
505 rate_inc = p.initial.rate_inc;
506 rate_dec = p.initial.rate_dec;
507 min_inc = p.initial.min_inc;
508 min_dec = p.initial.min_dec;
509 }
510 } else if (low_noise_render) {
511 max_inc = p.low_noise.max_inc;
512 max_dec = p.low_noise.max_dec;
513 rate_inc = p.low_noise.rate_inc;
514 rate_dec = p.low_noise.rate_dec;
515 min_inc = p.low_noise.min_inc;
516 min_dec = p.low_noise.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200517 } else if (!saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100518 max_inc = p.normal.max_inc;
519 max_dec = p.normal.max_dec;
520 rate_inc = p.normal.rate_inc;
521 rate_dec = p.normal.rate_dec;
522 min_inc = p.normal.min_inc;
523 min_dec = p.normal.min_dec;
524 } else {
525 max_inc = p.saturation.max_inc;
526 max_dec = p.saturation.max_dec;
527 rate_inc = p.saturation.rate_inc;
528 rate_dec = p.saturation.rate_dec;
529 min_inc = p.saturation.min_inc;
530 min_dec = p.saturation.min_dec;
531 }
532
533 for (size_t k = 0; k < new_gain.size(); ++k) {
534 auto increase_update = [](float new_gain, float last_gain,
535 float current_inc, float max_inc, float min_inc,
536 float change_rate) {
537 return new_gain > last_gain ? std::min(max_inc, current_inc * change_rate)
538 : min_inc;
539 };
540
541 if (echo[k] > last_echo_[k]) {
542 gain_increase_[k] =
543 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
544 max_inc, min_inc, rate_inc);
545 } else {
546 gain_increase_[k] =
547 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
548 max_dec, min_dec, rate_dec);
549 }
550 }
peah1d680892017-05-23 04:07:10 -0700551}
peah86afe9d2017-04-06 15:45:32 -0700552
peah1d680892017-05-23 04:07:10 -0700553// Detects when the render signal can be considered to have low power and
554// consist of stationary noise.
555bool SuppressionGain::LowNoiseRenderDetector::Detect(
556 const std::vector<std::vector<float>>& render) {
557 float x2_sum = 0.f;
558 float x2_max = 0.f;
559 for (auto x_k : render[0]) {
560 const float x2 = x_k * x_k;
561 x2_sum += x2;
562 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800563 }
peah1d680892017-05-23 04:07:10 -0700564
565 constexpr float kThreshold = 50.f * 50.f * 64.f;
566 const bool low_noise_render =
567 average_power_ < kThreshold && x2_max < 3 * average_power_;
568 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
569 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800570}
571
572} // namespace webrtc