blob: 3b6548b7e7d24893971b92317531c388256e1608 [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
Niels Möllera12c42a2018-07-25 16:05:48 +020014// Defines WEBRTC_ARCH_X86_FAMILY, used below.
15#include "rtc_base/system/arch.h"
16
peah522d71b2017-02-23 05:16:26 -080017#if defined(WEBRTC_ARCH_X86_FAMILY)
18#include <emmintrin.h>
19#endif
20#include <math.h>
21#include <algorithm>
22#include <functional>
peah86afe9d2017-04-06 15:45:32 -070023#include <numeric>
peah522d71b2017-02-23 05:16:26 -080024
Gustaf Ullberg8406c432018-06-19 12:31:33 +020025#include "modules/audio_processing/aec3/moving_average.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/aec3/vector_math.h"
Gustaf Ullberg216af842018-04-26 12:39:11 +020027#include "modules/audio_processing/logging/apm_data_dumper.h"
28#include "rtc_base/atomicops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/checks.h"
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +020030#include "system_wrappers/include/field_trial.h"
peahcf02cf12017-04-05 14:18:07 -070031
peah522d71b2017-02-23 05:16:26 -080032namespace webrtc {
33namespace {
34
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +020035bool EnableTransparencyImprovements() {
36 return !field_trial::IsEnabled(
37 "WebRTC-Aec3TransparencyImprovementsKillSwitch");
38}
39
Gustaf Ullbergec642172018-07-03 13:48:32 +020040bool EnableNewSuppression() {
41 return !field_trial::IsEnabled("WebRTC-Aec3NewSuppressionKillSwitch");
42}
43
peah1d680892017-05-23 04:07:10 -070044// Adjust the gains according to the presence of known external filters.
45void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080046 // Limit the low frequency gains to avoid the impact of the high-pass filter
47 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070048 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080049
50 // Limit the high frequency gains to avoid the impact of the anti-aliasing
51 // filter on the upper-frequency gains influencing the overall achieved
52 // gain. TODO(peah): Update this when new anti-aliasing filters are
53 // implemented.
peah86afe9d2017-04-06 15:45:32 -070054 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070055 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
56 std::for_each(
57 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
58 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
59 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080060}
61
peah1d680892017-05-23 04:07:10 -070062// Computes the gain to apply for the bands beyond the first band.
63float UpperBandsGain(
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020064 const absl::optional<int>& narrow_peak_band,
peah1d680892017-05-23 04:07:10 -070065 bool saturated_echo,
66 const std::vector<std::vector<float>>& render,
67 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) {
68 RTC_DCHECK_LT(0, render.size());
peah86afe9d2017-04-06 15:45:32 -070069 if (render.size() == 1) {
70 return 1.f;
71 }
72
peah14c11a42017-07-11 06:13:43 -070073 if (narrow_peak_band &&
74 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
75 return 0.001f;
76 }
77
peah1d680892017-05-23 04:07:10 -070078 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
79 const float gain_below_8_khz = *std::min_element(
80 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
81
peah86afe9d2017-04-06 15:45:32 -070082 // Always attenuate the upper bands when there is saturated echo.
83 if (saturated_echo) {
peah1d680892017-05-23 04:07:10 -070084 return std::min(0.001f, gain_below_8_khz);
peah86afe9d2017-04-06 15:45:32 -070085 }
86
87 // Compute the upper and lower band energies.
peah1d680892017-05-23 04:07:10 -070088 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
89 const float low_band_energy =
90 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
91 float high_band_energy = 0.f;
peah86afe9d2017-04-06 15:45:32 -070092 for (size_t k = 1; k < render.size(); ++k) {
peah1d680892017-05-23 04:07:10 -070093 const float energy = std::accumulate(render[k].begin(), render[k].end(),
94 0.f, sum_of_squares);
95 high_band_energy = std::max(high_band_energy, energy);
peah86afe9d2017-04-06 15:45:32 -070096 }
97
98 // If there is more power in the lower frequencies than the upper frequencies,
peah1d680892017-05-23 04:07:10 -070099 // or if the power in upper frequencies is low, do not bound the gain in the
peah86afe9d2017-04-06 15:45:32 -0700100 // upper bands.
peah1d680892017-05-23 04:07:10 -0700101 float anti_howling_gain;
Per Åhgren38e2d952017-11-17 14:54:28 +0100102 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
peah1d680892017-05-23 04:07:10 -0700103 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
104 anti_howling_gain = 1.f;
105 } else {
106 // In all other cases, bound the gain for upper frequencies.
107 RTC_DCHECK_LE(low_band_energy, high_band_energy);
108 RTC_DCHECK_NE(0.f, high_band_energy);
109 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
peah86afe9d2017-04-06 15:45:32 -0700110 }
111
peah1d680892017-05-23 04:07:10 -0700112 // Choose the gain as the minimum of the lower and upper gains.
113 return std::min(gain_below_8_khz, anti_howling_gain);
114}
115
Per Åhgrenb02644f2018-04-17 11:52:17 +0200116// Scales the echo according to assessed audibility at the other end.
117void WeightEchoForAudibility(const EchoCanceller3Config& config,
118 rtc::ArrayView<const float> echo,
119 rtc::ArrayView<float> weighted_echo,
120 rtc::ArrayView<float> one_by_weighted_echo) {
121 RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
122 RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
123 RTC_DCHECK_EQ(kFftLengthBy2Plus1, one_by_weighted_echo.size());
124
125 auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
126 rtc::ArrayView<const float> echo,
127 rtc::ArrayView<float> weighted_echo,
128 rtc::ArrayView<float> one_by_weighted_echo) {
129 for (size_t k = begin; k < end; ++k) {
130 if (echo[k] < threshold) {
131 float tmp = (threshold - echo[k]) * normalizer;
132 weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp);
133 } else {
134 weighted_echo[k] = echo[k];
135 }
136 one_by_weighted_echo[k] =
137 weighted_echo[k] > 0.f ? 1.f / weighted_echo[k] : 1.f;
138 }
139 };
140
141 float threshold = config.echo_audibility.floor_power *
142 config.echo_audibility.audibility_threshold_lf;
143 float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
144 weigh(threshold, normalizer, 0, 3, echo, weighted_echo, one_by_weighted_echo);
145
146 threshold = config.echo_audibility.floor_power *
147 config.echo_audibility.audibility_threshold_mf;
148 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
149 weigh(threshold, normalizer, 3, 7, echo, weighted_echo, one_by_weighted_echo);
150
151 threshold = config.echo_audibility.floor_power *
152 config.echo_audibility.audibility_threshold_hf;
153 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
154 weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo,
155 one_by_weighted_echo);
156}
157
peah1d680892017-05-23 04:07:10 -0700158// Computes the gain to reduce the echo to a non audible level.
Gustaf Ullbergec642172018-07-03 13:48:32 +0200159void GainToNoAudibleEchoFallback(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200160 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700161 bool low_noise_render,
162 bool saturated_echo,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200163 bool linear_echo_estimate,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200164 bool enable_transparency_improvements,
peah1d680892017-05-23 04:07:10 -0700165 const std::array<float, kFftLengthBy2Plus1>& nearend,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200166 const std::array<float, kFftLengthBy2Plus1>& weighted_echo,
peah1d680892017-05-23 04:07:10 -0700167 const std::array<float, kFftLengthBy2Plus1>& masker,
168 const std::array<float, kFftLengthBy2Plus1>& min_gain,
169 const std::array<float, kFftLengthBy2Plus1>& max_gain,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200170 const std::array<float, kFftLengthBy2Plus1>& one_by_weighted_echo,
peah1d680892017-05-23 04:07:10 -0700171 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgrenc65ce782017-10-09 13:01:39 +0200172 float nearend_masking_margin = 0.f;
Per Åhgren63b494d2017-12-06 11:32:38 +0100173 if (linear_echo_estimate) {
174 nearend_masking_margin =
175 low_noise_render
176 ? config.gain_mask.m9
177 : (saturated_echo ? config.gain_mask.m2 : config.gain_mask.m3);
Per Åhgrenc65ce782017-10-09 13:01:39 +0200178 } else {
Per Åhgren63b494d2017-12-06 11:32:38 +0100179 nearend_masking_margin = config.gain_mask.m7;
Per Åhgrenc65ce782017-10-09 13:01:39 +0200180 }
Per Åhgren7ddd4632017-10-25 02:59:45 +0200181
Per Åhgrend309b002017-10-09 23:50:44 +0200182 RTC_DCHECK_LE(0.f, nearend_masking_margin);
183 RTC_DCHECK_GT(1.f, nearend_masking_margin);
Per Åhgrend309b002017-10-09 23:50:44 +0200184
Per Åhgren63b494d2017-12-06 11:32:38 +0100185 const float masker_margin =
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200186 linear_echo_estimate
187 ? (enable_transparency_improvements ? config.gain_mask.m0
188 : config.gain_mask.m1)
189 : config.gain_mask.m8;
peah1d680892017-05-23 04:07:10 -0700190
191 for (size_t k = 0; k < gain->size(); ++k) {
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200192 // TODO(devicentepena): Experiment by removing the reverberation estimation
193 // from the nearend signal before computing the gains.
Per Åhgren7106d932017-10-09 08:25:18 +0200194 const float unity_gain_masker = std::max(nearend[k], masker[k]);
195 RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker);
Per Åhgrenb02644f2018-04-17 11:52:17 +0200196 if (weighted_echo[k] <= nearend_masking_margin * unity_gain_masker ||
Per Åhgren7106d932017-10-09 08:25:18 +0200197 unity_gain_masker <= 0.f) {
peah1d680892017-05-23 04:07:10 -0700198 (*gain)[k] = 1.f;
199 } else {
Per Åhgrend309b002017-10-09 23:50:44 +0200200 RTC_DCHECK_LT(0.f, unity_gain_masker);
Per Åhgrend309b002017-10-09 23:50:44 +0200201 (*gain)[k] =
Per Åhgrenb02644f2018-04-17 11:52:17 +0200202 std::max(0.f, (1.f - config.gain_mask.gain_curve_slope *
203 weighted_echo[k] / unity_gain_masker) *
204 config.gain_mask.gain_curve_offset);
205 (*gain)[k] = std::max(masker_margin * masker[k] * one_by_weighted_echo[k],
206 (*gain)[k]);
peah1d680892017-05-23 04:07:10 -0700207 }
208
209 (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]);
210 }
211}
212
Per Åhgren85a11a32017-10-02 14:42:06 +0200213// TODO(peah): Make adaptive to take the actual filter error into account.
214constexpr size_t kUpperAccurateBandPlus1 = 29;
215
peah1d680892017-05-23 04:07:10 -0700216// Computes the signal output power that masks the echo signal.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200217void MaskingPower(const EchoCanceller3Config& config,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200218 bool enable_transparency_improvements,
peah8cee56f2017-08-24 22:36:53 -0700219 const std::array<float, kFftLengthBy2Plus1>& nearend,
peah1d680892017-05-23 04:07:10 -0700220 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
221 const std::array<float, kFftLengthBy2Plus1>& last_masker,
222 const std::array<float, kFftLengthBy2Plus1>& gain,
223 std::array<float, kFftLengthBy2Plus1>* masker) {
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200224 if (enable_transparency_improvements) {
225 std::copy(comfort_noise.begin(), comfort_noise.end(), masker->begin());
226 return;
227 }
228
Per Åhgrenb02644f2018-04-17 11:52:17 +0200229 // Apply masking over time.
230 float masking_factor = config.gain_mask.temporal_masking_lf;
231 auto limit = config.gain_mask.temporal_masking_lf_bands;
232 std::transform(
233 comfort_noise.begin(), comfort_noise.begin() + limit, last_masker.begin(),
234 masker->begin(),
235 [masking_factor](float a, float b) { return a + masking_factor * b; });
236 masking_factor = config.gain_mask.temporal_masking_hf;
237 std::transform(
238 comfort_noise.begin() + limit, comfort_noise.end(),
239 last_masker.begin() + limit, masker->begin() + limit,
240 [masking_factor](float a, float b) { return a + masking_factor * b; });
241
242 // Apply masking only between lower frequency bands.
peah1d680892017-05-23 04:07:10 -0700243 std::array<float, kFftLengthBy2Plus1> side_band_masker;
Per Åhgren7106d932017-10-09 08:25:18 +0200244 float max_nearend_after_gain = 0.f;
peah1d680892017-05-23 04:07:10 -0700245 for (size_t k = 0; k < gain.size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200246 const float nearend_after_gain = nearend[k] * gain[k];
247 max_nearend_after_gain =
248 std::max(max_nearend_after_gain, nearend_after_gain);
249 side_band_masker[k] = nearend_after_gain + comfort_noise[k];
peah1d680892017-05-23 04:07:10 -0700250 }
Per Åhgren85a11a32017-10-02 14:42:06 +0200251
Per Åhgren85a11a32017-10-02 14:42:06 +0200252 RTC_DCHECK_LT(kUpperAccurateBandPlus1, gain.size());
253 for (size_t k = 1; k < kUpperAccurateBandPlus1; ++k) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200254 (*masker)[k] += config.gain_mask.m5 *
Per Åhgren7106d932017-10-09 08:25:18 +0200255 (side_band_masker[k - 1] + side_band_masker[k + 1]);
peah1d680892017-05-23 04:07:10 -0700256 }
Per Åhgren7106d932017-10-09 08:25:18 +0200257
258 // Add full-band masking as a minimum value for the masker.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200259 const float min_masker = max_nearend_after_gain * config.gain_mask.m6;
Per Åhgren7106d932017-10-09 08:25:18 +0200260 std::for_each(masker->begin(), masker->end(),
261 [min_masker](float& a) { a = std::max(a, min_masker); });
peah1d680892017-05-23 04:07:10 -0700262}
263
Per Åhgren85a11a32017-10-02 14:42:06 +0200264// Limits the gain in the frequencies for which the adaptive filter has not
265// converged. Currently, these frequencies are not hardcoded to the frequencies
266// which are typically not excited by speech.
267// TODO(peah): Make adaptive to take the actual filter error into account.
268void AdjustNonConvergedFrequencies(
269 std::array<float, kFftLengthBy2Plus1>* gain) {
270 constexpr float oneByBandsInSum =
271 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
272 const float hf_gain_bound =
273 std::accumulate(gain->begin() + 20,
274 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
275 oneByBandsInSum;
276
277 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
278 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
279}
280
peah1d680892017-05-23 04:07:10 -0700281} // namespace
282
Gustaf Ullberg216af842018-04-26 12:39:11 +0200283int SuppressionGain::instance_count_ = 0;
284
Gustaf Ullbergec642172018-07-03 13:48:32 +0200285// Computes the gain to reduce the echo to a non audible level.
286void SuppressionGain::GainToNoAudibleEcho(
287 const std::array<float, kFftLengthBy2Plus1>& nearend,
288 const std::array<float, kFftLengthBy2Plus1>& echo,
289 const std::array<float, kFftLengthBy2Plus1>& masker,
290 const std::array<float, kFftLengthBy2Plus1>& min_gain,
291 const std::array<float, kFftLengthBy2Plus1>& max_gain,
292 std::array<float, kFftLengthBy2Plus1>* gain) const {
293 for (size_t k = 0; k < gain->size(); ++k) {
294 float enr = echo[k] / (nearend[k] + 1.f); // Echo-to-nearend ratio.
295 float emr = echo[k] / (masker[k] + 1.f); // Echo-to-masker (noise) ratio.
296 float g = 1.0f;
297 if (enr > enr_transparent_[k] && emr > emr_transparent_[k]) {
298 g = (enr_suppress_[k] - enr) / (enr_suppress_[k] - enr_transparent_[k]);
299 g = std::max(g, emr_transparent_[k] / emr);
300 }
301 (*gain)[k] = std::max(std::min(g, max_gain[k]), min_gain[k]);
302 }
303}
304
peah1d680892017-05-23 04:07:10 -0700305// TODO(peah): Add further optimizations, in particular for the divisions.
306void SuppressionGain::LowerBandGain(
307 bool low_noise_render,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100308 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -0700309 const std::array<float, kFftLengthBy2Plus1>& nearend,
310 const std::array<float, kFftLengthBy2Plus1>& echo,
311 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
312 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100313 const bool saturated_echo = aec_state.SaturatedEcho();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100314 const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
315
Per Åhgrenb02644f2018-04-17 11:52:17 +0200316 // Weight echo power in terms of audibility. // Precompute 1/weighted echo
317 // (note that when the echo is zero, the precomputed value is never used).
318 std::array<float, kFftLengthBy2Plus1> weighted_echo;
319 std::array<float, kFftLengthBy2Plus1> one_by_weighted_echo;
320 WeightEchoForAudibility(config_, echo, weighted_echo, one_by_weighted_echo);
peah1d680892017-05-23 04:07:10 -0700321
322 // Compute the minimum gain as the attenuating gain to put the signal just
323 // above the zero sample values.
324 std::array<float, kFftLengthBy2Plus1> min_gain;
peah8cee56f2017-08-24 22:36:53 -0700325 const float min_echo_power =
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200326 low_noise_render ? config_.echo_audibility.low_render_limit
327 : config_.echo_audibility.normal_render_limit;
Per Åhgren31122d62018-04-10 16:33:55 +0200328 if (!saturated_echo) {
peah1d680892017-05-23 04:07:10 -0700329 for (size_t k = 0; k < nearend.size(); ++k) {
Per Åhgrenb02644f2018-04-17 11:52:17 +0200330 const float denom = std::min(nearend[k], weighted_echo[k]);
peah1d680892017-05-23 04:07:10 -0700331 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
332 min_gain[k] = std::min(min_gain[k], 1.f);
333 }
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200334 if (enable_transparency_improvements_) {
335 for (size_t k = 0; k < 6; ++k) {
336 // Make sure the gains of the low frequencies do not decrease too
337 // quickly after strong nearend.
338 if (last_nearend_[k] > last_echo_[k]) {
339 min_gain[k] =
340 std::max(min_gain[k],
341 last_gain_[k] * config_.gain_updates.max_dec_factor_lf);
342 min_gain[k] = std::min(min_gain[k], 1.f);
343 }
344 }
345 }
peah1d680892017-05-23 04:07:10 -0700346 } else {
347 min_gain.fill(0.f);
348 }
349
350 // Compute the maximum gain by limiting the gain increase from the previous
351 // gain.
352 std::array<float, kFftLengthBy2Plus1> max_gain;
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200353 if (enable_transparency_improvements_) {
354 for (size_t k = 0; k < gain->size(); ++k) {
355 max_gain[k] =
356 std::min(std::max(last_gain_[k] * config_.gain_updates.max_inc_factor,
357 config_.gain_updates.floor_first_increase),
358 1.f);
359 }
360 } else {
361 for (size_t k = 0; k < gain->size(); ++k) {
362 max_gain[k] =
363 std::min(std::max(last_gain_[k] * gain_increase_[k],
364 config_.gain_updates.floor_first_increase),
365 1.f);
366 }
peah1d680892017-05-23 04:07:10 -0700367 }
368
369 // Iteratively compute the gain required to attenuate the echo to a non
370 // noticeable level.
Gustaf Ullberg216af842018-04-26 12:39:11 +0200371 std::array<float, kFftLengthBy2Plus1> masker;
Gustaf Ullbergec642172018-07-03 13:48:32 +0200372 if (enable_new_suppression_) {
373 GainToNoAudibleEcho(nearend, weighted_echo, comfort_noise, min_gain,
374 max_gain, gain);
peah1d680892017-05-23 04:07:10 -0700375 AdjustForExternalFilters(gain);
Gustaf Ullbergec642172018-07-03 13:48:32 +0200376 } else {
377 gain->fill(0.f);
378 for (int k = 0; k < 2; ++k) {
379 MaskingPower(config_, enable_transparency_improvements_, nearend,
380 comfort_noise, last_masker_, *gain, &masker);
381 GainToNoAudibleEchoFallback(
382 config_, low_noise_render, saturated_echo, linear_echo_estimate,
383 enable_transparency_improvements_, nearend, weighted_echo, masker,
384 min_gain, max_gain, one_by_weighted_echo, gain);
385 AdjustForExternalFilters(gain);
386 }
peah1d680892017-05-23 04:07:10 -0700387 }
388
Per Åhgren85a11a32017-10-02 14:42:06 +0200389 // Adjust the gain for frequencies which have not yet converged.
390 AdjustNonConvergedFrequencies(gain);
391
peah1d680892017-05-23 04:07:10 -0700392 // Update the allowed maximum gain increase.
Per Åhgren31122d62018-04-10 16:33:55 +0200393 UpdateGainIncrease(low_noise_render, linear_echo_estimate, saturated_echo,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200394 weighted_echo, *gain);
Per Åhgren1f33a372017-10-11 02:36:53 +0200395
peah1d680892017-05-23 04:07:10 -0700396 // Store data required for the gain computation of the next block.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200397 std::copy(nearend.begin(), nearend.end(), last_nearend_.begin());
Per Åhgrenb02644f2018-04-17 11:52:17 +0200398 std::copy(weighted_echo.begin(), weighted_echo.end(), last_echo_.begin());
peah1d680892017-05-23 04:07:10 -0700399 std::copy(gain->begin(), gain->end(), last_gain_.begin());
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200400 MaskingPower(config_, enable_transparency_improvements_, nearend,
401 comfort_noise, last_masker_, *gain, &last_masker_);
peah1d680892017-05-23 04:07:10 -0700402 aec3::VectorMath(optimization_).Sqrt(*gain);
Gustaf Ullberg216af842018-04-26 12:39:11 +0200403
404 // Debug outputs for the purpose of development and analysis.
405 data_dumper_->DumpRaw("aec3_suppressor_min_gain", min_gain);
406 data_dumper_->DumpRaw("aec3_suppressor_max_gain", max_gain);
407 data_dumper_->DumpRaw("aec3_suppressor_masker", masker);
408 data_dumper_->DumpRaw("aec3_suppressor_last_masker", last_masker_);
peah86afe9d2017-04-06 15:45:32 -0700409}
410
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200411SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200412 Aec3Optimization optimization,
413 int sample_rate_hz)
Gustaf Ullberg216af842018-04-26 12:39:11 +0200414 : data_dumper_(
415 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
416 optimization_(optimization),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100417 config_(config),
418 state_change_duration_blocks_(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200419 static_cast<int>(config_.filter.config_change_duration_blocks)),
420 coherence_gain_(sample_rate_hz,
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200421 config_.suppressor.bands_with_reliable_coherence),
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200422 enable_transparency_improvements_(EnableTransparencyImprovements()),
Gustaf Ullbergec642172018-07-03 13:48:32 +0200423 enable_new_suppression_(EnableNewSuppression()),
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200424 moving_average_(kFftLengthBy2Plus1,
425 config.suppressor.nearend_average_blocks) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100426 RTC_DCHECK_LT(0, state_change_duration_blocks_);
427 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700428 last_gain_.fill(1.f);
429 last_masker_.fill(0.f);
430 gain_increase_.fill(1.f);
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200431 last_nearend_.fill(0.f);
peah1d680892017-05-23 04:07:10 -0700432 last_echo_.fill(0.f);
Gustaf Ullbergec642172018-07-03 13:48:32 +0200433
434 // Compute per-band masking thresholds.
435 constexpr size_t kLastLfBand = 5;
436 constexpr size_t kFirstHfBand = 8;
437 RTC_DCHECK_LT(kLastLfBand, kFirstHfBand);
438 auto& lf = config.suppressor.mask_lf;
439 auto& hf = config.suppressor.mask_hf;
440 RTC_DCHECK_LT(lf.enr_transparent, lf.enr_suppress);
441 RTC_DCHECK_LT(hf.enr_transparent, hf.enr_suppress);
442 for (size_t k = 0; k < kFftLengthBy2Plus1; k++) {
443 float a;
444 if (k <= kLastLfBand) {
445 a = 0.f;
446 } else if (k < kFirstHfBand) {
447 a = (k - kLastLfBand) / static_cast<float>(kFirstHfBand - kLastLfBand);
448 } else {
449 a = 1.f;
450 }
451 enr_transparent_[k] = (1 - a) * lf.enr_transparent + a * hf.enr_transparent;
452 enr_suppress_[k] = (1 - a) * lf.enr_suppress + a * hf.enr_suppress;
453 emr_transparent_[k] = (1 - a) * lf.emr_transparent + a * hf.emr_transparent;
454 }
peah522d71b2017-02-23 05:16:26 -0800455}
456
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200457SuppressionGain::~SuppressionGain() = default;
458
peah522d71b2017-02-23 05:16:26 -0800459void SuppressionGain::GetGain(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200460 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
461 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
462 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
463 const FftData& linear_aec_fft,
464 const FftData& render_fft,
465 const FftData& capture_fft,
peah14c11a42017-07-11 06:13:43 -0700466 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200467 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700468 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700469 float* high_bands_gain,
470 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
471 RTC_DCHECK(high_bands_gain);
472 RTC_DCHECK(low_band_gain);
Per Åhgren7343f562018-08-17 10:08:34 +0200473 const auto& cfg = config_.suppressor;
474
475 if (cfg.enforce_transparent) {
476 low_band_gain->fill(1.f);
477 *high_bands_gain = cfg.enforce_empty_higher_bands ? 0.f : 1.f;
478 return;
479 }
peah86afe9d2017-04-06 15:45:32 -0700480
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200481 std::array<float, kFftLengthBy2Plus1> nearend_average;
482 moving_average_.Average(nearend_spectrum, nearend_average);
483
peah1d680892017-05-23 04:07:10 -0700484 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100485 bool low_noise_render = low_render_detector_.Detect(render);
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200486 const absl::optional<int> narrow_peak_band =
peah14c11a42017-07-11 06:13:43 -0700487 render_signal_analyzer.NarrowPeakBand();
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200488 LowerBandGain(low_noise_render, aec_state, nearend_average, echo_spectrum,
Gustaf Ullberg5bb98972018-04-25 12:54:59 +0200489 comfort_noise_spectrum, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700490
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200491 // Adjust the gain for bands where the coherence indicates not echo.
Per Åhgren7343f562018-08-17 10:08:34 +0200492 if (cfg.bands_with_reliable_coherence > 0 &&
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200493 !enable_transparency_improvements_) {
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200494 std::array<float, kFftLengthBy2Plus1> G_coherence;
495 coherence_gain_.ComputeGain(linear_aec_fft, render_fft, capture_fft,
496 G_coherence);
Per Åhgren7343f562018-08-17 10:08:34 +0200497 for (size_t k = 0; k < cfg.bands_with_reliable_coherence; ++k) {
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200498 (*low_band_gain)[k] = std::max((*low_band_gain)[k], G_coherence[k]);
499 }
500 }
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200501
502 // Limit the gain of the lower bands during start up and after resets.
503 const float gain_upper_bound = aec_state.SuppressionGainLimit();
504 if (gain_upper_bound < 1.f) {
505 for (size_t k = 0; k < low_band_gain->size(); ++k) {
506 (*low_band_gain)[k] = std::min((*low_band_gain)[k], gain_upper_bound);
507 }
508 }
509
510 // Compute the gain for the upper bands.
511 *high_bands_gain = UpperBandsGain(narrow_peak_band, aec_state.SaturatedEcho(),
512 render, *low_band_gain);
Per Åhgren7343f562018-08-17 10:08:34 +0200513 if (cfg.enforce_empty_higher_bands) {
514 *high_bands_gain = 0.f;
515 }
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100516}
517
518void SuppressionGain::SetInitialState(bool state) {
519 initial_state_ = state;
520 if (state) {
521 initial_state_change_counter_ = state_change_duration_blocks_;
522 } else {
523 initial_state_change_counter_ = 0;
524 }
525}
526
527void SuppressionGain::UpdateGainIncrease(
528 bool low_noise_render,
529 bool linear_echo_estimate,
Per Åhgren31122d62018-04-10 16:33:55 +0200530 bool saturated_echo,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100531 const std::array<float, kFftLengthBy2Plus1>& echo,
532 const std::array<float, kFftLengthBy2Plus1>& new_gain) {
533 float max_inc;
534 float max_dec;
535 float rate_inc;
536 float rate_dec;
537 float min_inc;
538 float min_dec;
539
540 RTC_DCHECK_GE(state_change_duration_blocks_, initial_state_change_counter_);
541 if (initial_state_change_counter_ > 0) {
542 if (--initial_state_change_counter_ == 0) {
543 initial_state_ = false;
544 }
545 }
546 RTC_DCHECK_LE(0, initial_state_change_counter_);
547
548 // EchoCanceller3Config::GainUpdates
549 auto& p = config_.gain_updates;
550 if (!linear_echo_estimate) {
551 max_inc = p.nonlinear.max_inc;
552 max_dec = p.nonlinear.max_dec;
553 rate_inc = p.nonlinear.rate_inc;
554 rate_dec = p.nonlinear.rate_dec;
555 min_inc = p.nonlinear.min_inc;
556 min_dec = p.nonlinear.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200557 } else if (initial_state_ && !saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100558 if (initial_state_change_counter_ > 0) {
559 float change_factor =
560 initial_state_change_counter_ * one_by_state_change_duration_blocks_;
561
562 auto average = [](float from, float to, float from_weight) {
563 return from * from_weight + to * (1.f - from_weight);
564 };
565
566 max_inc = average(p.initial.max_inc, p.normal.max_inc, change_factor);
567 max_dec = average(p.initial.max_dec, p.normal.max_dec, change_factor);
568 rate_inc = average(p.initial.rate_inc, p.normal.rate_inc, change_factor);
569 rate_dec = average(p.initial.rate_dec, p.normal.rate_dec, change_factor);
570 min_inc = average(p.initial.min_inc, p.normal.min_inc, change_factor);
571 min_dec = average(p.initial.min_dec, p.normal.min_dec, change_factor);
572 } else {
573 max_inc = p.initial.max_inc;
574 max_dec = p.initial.max_dec;
575 rate_inc = p.initial.rate_inc;
576 rate_dec = p.initial.rate_dec;
577 min_inc = p.initial.min_inc;
578 min_dec = p.initial.min_dec;
579 }
580 } else if (low_noise_render) {
581 max_inc = p.low_noise.max_inc;
582 max_dec = p.low_noise.max_dec;
583 rate_inc = p.low_noise.rate_inc;
584 rate_dec = p.low_noise.rate_dec;
585 min_inc = p.low_noise.min_inc;
586 min_dec = p.low_noise.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200587 } else if (!saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100588 max_inc = p.normal.max_inc;
589 max_dec = p.normal.max_dec;
590 rate_inc = p.normal.rate_inc;
591 rate_dec = p.normal.rate_dec;
592 min_inc = p.normal.min_inc;
593 min_dec = p.normal.min_dec;
594 } else {
595 max_inc = p.saturation.max_inc;
596 max_dec = p.saturation.max_dec;
597 rate_inc = p.saturation.rate_inc;
598 rate_dec = p.saturation.rate_dec;
599 min_inc = p.saturation.min_inc;
600 min_dec = p.saturation.min_dec;
601 }
602
603 for (size_t k = 0; k < new_gain.size(); ++k) {
604 auto increase_update = [](float new_gain, float last_gain,
605 float current_inc, float max_inc, float min_inc,
606 float change_rate) {
607 return new_gain > last_gain ? std::min(max_inc, current_inc * change_rate)
608 : min_inc;
609 };
610
611 if (echo[k] > last_echo_[k]) {
612 gain_increase_[k] =
613 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
614 max_inc, min_inc, rate_inc);
615 } else {
616 gain_increase_[k] =
617 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
618 max_dec, min_dec, rate_dec);
619 }
620 }
peah1d680892017-05-23 04:07:10 -0700621}
peah86afe9d2017-04-06 15:45:32 -0700622
peah1d680892017-05-23 04:07:10 -0700623// Detects when the render signal can be considered to have low power and
624// consist of stationary noise.
625bool SuppressionGain::LowNoiseRenderDetector::Detect(
626 const std::vector<std::vector<float>>& render) {
627 float x2_sum = 0.f;
628 float x2_max = 0.f;
629 for (auto x_k : render[0]) {
630 const float x2 = x_k * x_k;
631 x2_sum += x2;
632 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800633 }
peah1d680892017-05-23 04:07:10 -0700634
635 constexpr float kThreshold = 50.f * 50.f * 64.f;
636 const bool low_noise_render =
637 average_power_ < kThreshold && x2_max < 3 * average_power_;
638 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
639 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800640}
641
642} // namespace webrtc