blob: 90e0ab8906487950390570813993cc072ca10a4f [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/suppression_gain.h"
peah522d71b2017-02-23 05:16:26 -080012
Mirko Bonadei71207422017-09-15 13:58:09 +020013#include "typedefs.h" // NOLINT(build/include)
peah522d71b2017-02-23 05:16:26 -080014#if defined(WEBRTC_ARCH_X86_FAMILY)
15#include <emmintrin.h>
16#endif
17#include <math.h>
18#include <algorithm>
19#include <functional>
peah86afe9d2017-04-06 15:45:32 -070020#include <numeric>
peah522d71b2017-02-23 05:16:26 -080021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/aec3/vector_math.h"
23#include "rtc_base/checks.h"
peahcf02cf12017-04-05 14:18:07 -070024
peah522d71b2017-02-23 05:16:26 -080025namespace webrtc {
26namespace {
27
peah14c11a42017-07-11 06:13:43 -070028// Reduce gain to avoid narrow band echo leakage.
29void NarrowBandAttenuation(int narrow_bin,
30 std::array<float, kFftLengthBy2Plus1>* gain) {
31 const int upper_bin =
32 std::min(narrow_bin + 6, static_cast<int>(kFftLengthBy2Plus1 - 1));
33 for (int k = std::max(0, narrow_bin - 6); k <= upper_bin; ++k) {
34 (*gain)[k] = std::min((*gain)[k], 0.001f);
35 }
36}
37
peah1d680892017-05-23 04:07:10 -070038// Adjust the gains according to the presence of known external filters.
39void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080040 // Limit the low frequency gains to avoid the impact of the high-pass filter
41 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070042 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080043
44 // Limit the high frequency gains to avoid the impact of the anti-aliasing
45 // filter on the upper-frequency gains influencing the overall achieved
46 // gain. TODO(peah): Update this when new anti-aliasing filters are
47 // implemented.
peah86afe9d2017-04-06 15:45:32 -070048 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070049 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
50 std::for_each(
51 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
52 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
53 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080054}
55
peah1d680892017-05-23 04:07:10 -070056// Computes the gain to apply for the bands beyond the first band.
57float UpperBandsGain(
peah14c11a42017-07-11 06:13:43 -070058 const rtc::Optional<int>& narrow_peak_band,
peah1d680892017-05-23 04:07:10 -070059 bool saturated_echo,
60 const std::vector<std::vector<float>>& render,
61 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) {
62 RTC_DCHECK_LT(0, render.size());
peah86afe9d2017-04-06 15:45:32 -070063 if (render.size() == 1) {
64 return 1.f;
65 }
66
peah14c11a42017-07-11 06:13:43 -070067 if (narrow_peak_band &&
68 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
69 return 0.001f;
70 }
71
peah1d680892017-05-23 04:07:10 -070072 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
73 const float gain_below_8_khz = *std::min_element(
74 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
75
peah86afe9d2017-04-06 15:45:32 -070076 // Always attenuate the upper bands when there is saturated echo.
77 if (saturated_echo) {
peah1d680892017-05-23 04:07:10 -070078 return std::min(0.001f, gain_below_8_khz);
peah86afe9d2017-04-06 15:45:32 -070079 }
80
81 // Compute the upper and lower band energies.
peah1d680892017-05-23 04:07:10 -070082 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
83 const float low_band_energy =
84 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
85 float high_band_energy = 0.f;
peah86afe9d2017-04-06 15:45:32 -070086 for (size_t k = 1; k < render.size(); ++k) {
peah1d680892017-05-23 04:07:10 -070087 const float energy = std::accumulate(render[k].begin(), render[k].end(),
88 0.f, sum_of_squares);
89 high_band_energy = std::max(high_band_energy, energy);
peah86afe9d2017-04-06 15:45:32 -070090 }
91
92 // If there is more power in the lower frequencies than the upper frequencies,
peah1d680892017-05-23 04:07:10 -070093 // or if the power in upper frequencies is low, do not bound the gain in the
peah86afe9d2017-04-06 15:45:32 -070094 // upper bands.
peah1d680892017-05-23 04:07:10 -070095 float anti_howling_gain;
96 constexpr float kThreshold = kSubBlockSize * 10.f * 10.f;
97 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
98 anti_howling_gain = 1.f;
99 } else {
100 // In all other cases, bound the gain for upper frequencies.
101 RTC_DCHECK_LE(low_band_energy, high_band_energy);
102 RTC_DCHECK_NE(0.f, high_band_energy);
103 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
peah86afe9d2017-04-06 15:45:32 -0700104 }
105
peah1d680892017-05-23 04:07:10 -0700106 // Choose the gain as the minimum of the lower and upper gains.
107 return std::min(gain_below_8_khz, anti_howling_gain);
108}
109
110// Limits the gain increase.
111void UpdateMaxGainIncrease(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200112 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700113 size_t no_saturation_counter,
114 bool low_noise_render,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200115 bool linear_echo_estimate,
peah1d680892017-05-23 04:07:10 -0700116 const std::array<float, kFftLengthBy2Plus1>& last_echo,
117 const std::array<float, kFftLengthBy2Plus1>& echo,
118 const std::array<float, kFftLengthBy2Plus1>& last_gain,
119 const std::array<float, kFftLengthBy2Plus1>& new_gain,
120 std::array<float, kFftLengthBy2Plus1>* gain_increase) {
121 float max_increasing;
122 float max_decreasing;
123 float rate_increasing;
124 float rate_decreasing;
125 float min_increasing;
126 float min_decreasing;
127
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200128 auto& param = config.gain_updates;
Per Åhgrenc65ce782017-10-09 13:01:39 +0200129 if (!linear_echo_estimate) {
130 max_increasing = param.nonlinear.max_inc;
131 max_decreasing = param.nonlinear.max_dec;
132 rate_increasing = param.nonlinear.rate_inc;
133 rate_decreasing = param.nonlinear.rate_dec;
134 min_increasing = param.nonlinear.min_inc;
135 min_decreasing = param.nonlinear.min_dec;
136 } else if (low_noise_render) {
peah8cee56f2017-08-24 22:36:53 -0700137 max_increasing = param.low_noise.max_inc;
138 max_decreasing = param.low_noise.max_dec;
139 rate_increasing = param.low_noise.rate_inc;
140 rate_decreasing = param.low_noise.rate_dec;
141 min_increasing = param.low_noise.min_inc;
142 min_decreasing = param.low_noise.min_dec;
peah1d680892017-05-23 04:07:10 -0700143 } else if (no_saturation_counter > 10) {
peah8cee56f2017-08-24 22:36:53 -0700144 max_increasing = param.normal.max_inc;
145 max_decreasing = param.normal.max_dec;
146 rate_increasing = param.normal.rate_inc;
147 rate_decreasing = param.normal.rate_dec;
148 min_increasing = param.normal.min_inc;
149 min_decreasing = param.normal.min_dec;
peah1d680892017-05-23 04:07:10 -0700150 } else {
peah8cee56f2017-08-24 22:36:53 -0700151 max_increasing = param.saturation.max_inc;
152 max_decreasing = param.saturation.max_dec;
153 rate_increasing = param.saturation.rate_inc;
154 rate_decreasing = param.saturation.rate_dec;
155 min_increasing = param.saturation.min_inc;
156 min_decreasing = param.saturation.min_dec;
peah1d680892017-05-23 04:07:10 -0700157 }
158
159 for (size_t k = 0; k < new_gain.size(); ++k) {
160 if (echo[k] > last_echo[k]) {
161 (*gain_increase)[k] =
162 new_gain[k] > last_gain[k]
163 ? std::min(max_increasing, (*gain_increase)[k] * rate_increasing)
164 : min_increasing;
165 } else {
166 (*gain_increase)[k] =
167 new_gain[k] > last_gain[k]
168 ? std::min(max_decreasing, (*gain_increase)[k] * rate_decreasing)
169 : min_decreasing;
170 }
171 }
172}
173
174// Computes the gain to reduce the echo to a non audible level.
175void GainToNoAudibleEcho(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200176 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700177 bool low_noise_render,
178 bool saturated_echo,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200179 bool linear_echo_estimate,
peah1d680892017-05-23 04:07:10 -0700180 const std::array<float, kFftLengthBy2Plus1>& nearend,
181 const std::array<float, kFftLengthBy2Plus1>& echo,
182 const std::array<float, kFftLengthBy2Plus1>& masker,
183 const std::array<float, kFftLengthBy2Plus1>& min_gain,
184 const std::array<float, kFftLengthBy2Plus1>& max_gain,
185 const std::array<float, kFftLengthBy2Plus1>& one_by_echo,
186 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgrenc65ce782017-10-09 13:01:39 +0200187 float nearend_masking_margin = 0.f;
188 if (linear_echo_estimate) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200189 nearend_masking_margin =
190 low_noise_render
191 ? config.gain_mask.m9
192 : (saturated_echo ? config.gain_mask.m2 : config.gain_mask.m3);
Per Åhgrenc65ce782017-10-09 13:01:39 +0200193 } else {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200194 nearend_masking_margin = config.gain_mask.m7;
Per Åhgrenc65ce782017-10-09 13:01:39 +0200195 }
Per Åhgrend309b002017-10-09 23:50:44 +0200196 RTC_DCHECK_LE(0.f, nearend_masking_margin);
197 RTC_DCHECK_GT(1.f, nearend_masking_margin);
198 const float one_by_one_minus_nearend_masking_margin =
199 1.f / (1.0f - nearend_masking_margin);
200
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200201 const float masker_margin =
202 linear_echo_estimate ? config.gain_mask.m1 : config.gain_mask.m8;
peah1d680892017-05-23 04:07:10 -0700203
204 for (size_t k = 0; k < gain->size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200205 const float unity_gain_masker = std::max(nearend[k], masker[k]);
206 RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker);
207 if (echo[k] <= nearend_masking_margin * unity_gain_masker ||
208 unity_gain_masker <= 0.f) {
peah1d680892017-05-23 04:07:10 -0700209 (*gain)[k] = 1.f;
210 } else {
Per Åhgrend309b002017-10-09 23:50:44 +0200211 RTC_DCHECK_LT(0.f, unity_gain_masker);
Per Åhgren1f33a372017-10-11 02:36:53 +0200212 (*gain)[k] = std::max(0.f, (1.f - 5.f * echo[k] / unity_gain_masker) *
Per Åhgrend309b002017-10-09 23:50:44 +0200213 one_by_one_minus_nearend_masking_margin);
214 (*gain)[k] =
215 std::max(masker_margin * masker[k] * one_by_echo[k], (*gain)[k]);
peah1d680892017-05-23 04:07:10 -0700216 }
217
218 (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]);
219 }
220}
221
Per Åhgren85a11a32017-10-02 14:42:06 +0200222// TODO(peah): Make adaptive to take the actual filter error into account.
223constexpr size_t kUpperAccurateBandPlus1 = 29;
224
peah1d680892017-05-23 04:07:10 -0700225// Computes the signal output power that masks the echo signal.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200226void MaskingPower(const EchoCanceller3Config& config,
peah8cee56f2017-08-24 22:36:53 -0700227 const std::array<float, kFftLengthBy2Plus1>& nearend,
peah1d680892017-05-23 04:07:10 -0700228 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
229 const std::array<float, kFftLengthBy2Plus1>& last_masker,
230 const std::array<float, kFftLengthBy2Plus1>& gain,
231 std::array<float, kFftLengthBy2Plus1>* masker) {
232 std::array<float, kFftLengthBy2Plus1> side_band_masker;
Per Åhgren7106d932017-10-09 08:25:18 +0200233 float max_nearend_after_gain = 0.f;
peah1d680892017-05-23 04:07:10 -0700234 for (size_t k = 0; k < gain.size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200235 const float nearend_after_gain = nearend[k] * gain[k];
236 max_nearend_after_gain =
237 std::max(max_nearend_after_gain, nearend_after_gain);
238 side_band_masker[k] = nearend_after_gain + comfort_noise[k];
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200239 (*masker)[k] = comfort_noise[k] + config.gain_mask.m4 * last_masker[k];
peah1d680892017-05-23 04:07:10 -0700240 }
Per Åhgren85a11a32017-10-02 14:42:06 +0200241
242 // Apply masking only between lower frequency bands.
243 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
274// TODO(peah): Add further optimizations, in particular for the divisions.
275void SuppressionGain::LowerBandGain(
276 bool low_noise_render,
peah14c11a42017-07-11 06:13:43 -0700277 const rtc::Optional<int>& narrow_peak_band,
peah1d680892017-05-23 04:07:10 -0700278 bool saturated_echo,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200279 bool linear_echo_estimate,
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) {
284 // Count the number of blocks since saturation.
285 no_saturation_counter_ = saturated_echo ? 0 : no_saturation_counter_ + 1;
286
287 // Precompute 1/echo (note that when the echo is zero, the precomputed value
288 // is never used).
289 std::array<float, kFftLengthBy2Plus1> one_by_echo;
290 std::transform(echo.begin(), echo.end(), one_by_echo.begin(),
291 [](float a) { return a > 0.f ? 1.f / a : 1.f; });
292
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;
peah1d680892017-05-23 04:07:10 -0700299 if (no_saturation_counter_ > 10) {
300 for (size_t k = 0; k < nearend.size(); ++k) {
301 const float denom = std::min(nearend[k], echo[k]);
302 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
303 min_gain[k] = std::min(min_gain[k], 1.f);
304 }
305 } else {
306 min_gain.fill(0.f);
307 }
308
309 // Compute the maximum gain by limiting the gain increase from the previous
310 // gain.
311 std::array<float, kFftLengthBy2Plus1> max_gain;
312 for (size_t k = 0; k < gain->size(); ++k) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200313 max_gain[k] = std::min(std::max(last_gain_[k] * gain_increase_[k],
314 config_.gain_updates.floor_first_increase),
315 1.f);
peah1d680892017-05-23 04:07:10 -0700316 }
317
318 // Iteratively compute the gain required to attenuate the echo to a non
319 // noticeable level.
320 gain->fill(0.f);
321 for (int k = 0; k < 2; ++k) {
322 std::array<float, kFftLengthBy2Plus1> masker;
peah8cee56f2017-08-24 22:36:53 -0700323 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain, &masker);
Per Åhgrenc65ce782017-10-09 13:01:39 +0200324 GainToNoAudibleEcho(config_, low_noise_render, saturated_echo,
325 linear_echo_estimate, nearend, echo, masker, min_gain,
326 max_gain, one_by_echo, gain);
peah1d680892017-05-23 04:07:10 -0700327 AdjustForExternalFilters(gain);
peah14c11a42017-07-11 06:13:43 -0700328 if (narrow_peak_band) {
329 NarrowBandAttenuation(*narrow_peak_band, gain);
330 }
peah1d680892017-05-23 04:07:10 -0700331 }
332
Per Åhgren85a11a32017-10-02 14:42:06 +0200333 // Adjust the gain for frequencies which have not yet converged.
334 AdjustNonConvergedFrequencies(gain);
335
peah1d680892017-05-23 04:07:10 -0700336 // Update the allowed maximum gain increase.
peah8cee56f2017-08-24 22:36:53 -0700337 UpdateMaxGainIncrease(config_, no_saturation_counter_, low_noise_render,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200338 linear_echo_estimate, last_echo_, echo, last_gain_,
339 *gain, &gain_increase_);
peah1d680892017-05-23 04:07:10 -0700340
Per Åhgren1f33a372017-10-11 02:36:53 +0200341 // Adjust gain dynamics.
342 const float gain_bound =
343 std::max(0.001f, *std::min_element(gain->begin(), gain->end()) * 10000.f);
344 std::for_each(gain->begin(), gain->end(),
345 [gain_bound](float& a) { a = std::min(a, gain_bound); });
346
peah1d680892017-05-23 04:07:10 -0700347 // Store data required for the gain computation of the next block.
348 std::copy(echo.begin(), echo.end(), last_echo_.begin());
349 std::copy(gain->begin(), gain->end(), last_gain_.begin());
peah8cee56f2017-08-24 22:36:53 -0700350 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain,
351 &last_masker_);
peah1d680892017-05-23 04:07:10 -0700352 aec3::VectorMath(optimization_).Sqrt(*gain);
peah86afe9d2017-04-06 15:45:32 -0700353}
354
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200355SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
356 Aec3Optimization optimization)
peah8cee56f2017-08-24 22:36:53 -0700357 : optimization_(optimization), config_(config) {
peah1d680892017-05-23 04:07:10 -0700358 last_gain_.fill(1.f);
359 last_masker_.fill(0.f);
360 gain_increase_.fill(1.f);
361 last_echo_.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800362}
363
364void SuppressionGain::GetGain(
peah1d680892017-05-23 04:07:10 -0700365 const std::array<float, kFftLengthBy2Plus1>& nearend,
366 const std::array<float, kFftLengthBy2Plus1>& echo,
367 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
peah14c11a42017-07-11 06:13:43 -0700368 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700369 bool saturated_echo,
370 const std::vector<std::vector<float>>& render,
peah6d822ad2017-04-10 13:52:14 -0700371 bool force_zero_gain,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200372 bool linear_echo_estimate,
peah86afe9d2017-04-06 15:45:32 -0700373 float* high_bands_gain,
374 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
375 RTC_DCHECK(high_bands_gain);
376 RTC_DCHECK(low_band_gain);
377
peah6d822ad2017-04-10 13:52:14 -0700378 if (force_zero_gain) {
peah1d680892017-05-23 04:07:10 -0700379 last_gain_.fill(0.f);
380 std::copy(comfort_noise.begin(), comfort_noise.end(), last_masker_.begin());
peah6d822ad2017-04-10 13:52:14 -0700381 low_band_gain->fill(0.f);
peah1d680892017-05-23 04:07:10 -0700382 gain_increase_.fill(1.f);
peah6d822ad2017-04-10 13:52:14 -0700383 *high_bands_gain = 0.f;
384 return;
385 }
386
peah1d680892017-05-23 04:07:10 -0700387 bool low_noise_render = low_render_detector_.Detect(render);
peah86afe9d2017-04-06 15:45:32 -0700388
peah1d680892017-05-23 04:07:10 -0700389 // Compute gain for the lower band.
peah14c11a42017-07-11 06:13:43 -0700390 const rtc::Optional<int> narrow_peak_band =
391 render_signal_analyzer.NarrowPeakBand();
Per Åhgrenc65ce782017-10-09 13:01:39 +0200392 LowerBandGain(low_noise_render, narrow_peak_band, saturated_echo,
393 linear_echo_estimate, nearend, echo, comfort_noise,
394 low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700395
peah1d680892017-05-23 04:07:10 -0700396 // Compute the gain for the upper bands.
peah14c11a42017-07-11 06:13:43 -0700397 *high_bands_gain =
398 UpperBandsGain(narrow_peak_band, saturated_echo, render, *low_band_gain);
peah1d680892017-05-23 04:07:10 -0700399}
peah86afe9d2017-04-06 15:45:32 -0700400
peah1d680892017-05-23 04:07:10 -0700401// Detects when the render signal can be considered to have low power and
402// consist of stationary noise.
403bool SuppressionGain::LowNoiseRenderDetector::Detect(
404 const std::vector<std::vector<float>>& render) {
405 float x2_sum = 0.f;
406 float x2_max = 0.f;
407 for (auto x_k : render[0]) {
408 const float x2 = x_k * x_k;
409 x2_sum += x2;
410 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800411 }
peah1d680892017-05-23 04:07:10 -0700412
413 constexpr float kThreshold = 50.f * 50.f * 64.f;
414 const bool low_noise_render =
415 average_power_ < kThreshold && x2_max < 3 * average_power_;
416 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
417 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800418}
419
420} // namespace webrtc