blob: b73e87efe09b77a817fe40c2cb3ec3dae67c492f [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,
Per Åhgren5c532d32018-03-22 00:29:25 +010030 const std::array<float, kFftLengthBy2Plus1>& nearend,
31 const std::array<float, kFftLengthBy2Plus1>& echo,
peah14c11a42017-07-11 06:13:43 -070032 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5c532d32018-03-22 00:29:25 +010033 // TODO(peah): Verify that the condition below is not too conservative.
34 if (10.f * echo[narrow_bin] > nearend[narrow_bin]) {
35 const int upper_bin =
36 std::min(narrow_bin + 6, static_cast<int>(kFftLengthBy2Plus1 - 1));
37 for (int k = std::max(0, narrow_bin - 6); k <= upper_bin; ++k) {
38 (*gain)[k] = std::min((*gain)[k], 0.001f);
39 }
peah14c11a42017-07-11 06:13:43 -070040 }
41}
42
peah1d680892017-05-23 04:07:10 -070043// Adjust the gains according to the presence of known external filters.
44void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080045 // Limit the low frequency gains to avoid the impact of the high-pass filter
46 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070047 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080048
49 // Limit the high frequency gains to avoid the impact of the anti-aliasing
50 // filter on the upper-frequency gains influencing the overall achieved
51 // gain. TODO(peah): Update this when new anti-aliasing filters are
52 // implemented.
peah86afe9d2017-04-06 15:45:32 -070053 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070054 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
55 std::for_each(
56 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
57 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
58 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080059}
60
peah1d680892017-05-23 04:07:10 -070061// Computes the gain to apply for the bands beyond the first band.
62float UpperBandsGain(
peah14c11a42017-07-11 06:13:43 -070063 const rtc::Optional<int>& narrow_peak_band,
peah1d680892017-05-23 04:07:10 -070064 bool saturated_echo,
65 const std::vector<std::vector<float>>& render,
66 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) {
67 RTC_DCHECK_LT(0, render.size());
peah86afe9d2017-04-06 15:45:32 -070068 if (render.size() == 1) {
69 return 1.f;
70 }
71
peah14c11a42017-07-11 06:13:43 -070072 if (narrow_peak_band &&
73 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
74 return 0.001f;
75 }
76
peah1d680892017-05-23 04:07:10 -070077 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
78 const float gain_below_8_khz = *std::min_element(
79 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
80
peah86afe9d2017-04-06 15:45:32 -070081 // Always attenuate the upper bands when there is saturated echo.
82 if (saturated_echo) {
peah1d680892017-05-23 04:07:10 -070083 return std::min(0.001f, gain_below_8_khz);
peah86afe9d2017-04-06 15:45:32 -070084 }
85
86 // Compute the upper and lower band energies.
peah1d680892017-05-23 04:07:10 -070087 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
88 const float low_band_energy =
89 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
90 float high_band_energy = 0.f;
peah86afe9d2017-04-06 15:45:32 -070091 for (size_t k = 1; k < render.size(); ++k) {
peah1d680892017-05-23 04:07:10 -070092 const float energy = std::accumulate(render[k].begin(), render[k].end(),
93 0.f, sum_of_squares);
94 high_band_energy = std::max(high_band_energy, energy);
peah86afe9d2017-04-06 15:45:32 -070095 }
96
97 // If there is more power in the lower frequencies than the upper frequencies,
peah1d680892017-05-23 04:07:10 -070098 // or if the power in upper frequencies is low, do not bound the gain in the
peah86afe9d2017-04-06 15:45:32 -070099 // upper bands.
peah1d680892017-05-23 04:07:10 -0700100 float anti_howling_gain;
Per Åhgren38e2d952017-11-17 14:54:28 +0100101 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
peah1d680892017-05-23 04:07:10 -0700102 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
103 anti_howling_gain = 1.f;
104 } else {
105 // In all other cases, bound the gain for upper frequencies.
106 RTC_DCHECK_LE(low_band_energy, high_band_energy);
107 RTC_DCHECK_NE(0.f, high_band_energy);
108 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
peah86afe9d2017-04-06 15:45:32 -0700109 }
110
peah1d680892017-05-23 04:07:10 -0700111 // Choose the gain as the minimum of the lower and upper gains.
112 return std::min(gain_below_8_khz, anti_howling_gain);
113}
114
peah1d680892017-05-23 04:07:10 -0700115// Computes the gain to reduce the echo to a non audible level.
116void GainToNoAudibleEcho(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200117 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700118 bool low_noise_render,
119 bool saturated_echo,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200120 bool saturating_echo_path,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200121 bool linear_echo_estimate,
peah1d680892017-05-23 04:07:10 -0700122 const std::array<float, kFftLengthBy2Plus1>& nearend,
123 const std::array<float, kFftLengthBy2Plus1>& echo,
124 const std::array<float, kFftLengthBy2Plus1>& masker,
125 const std::array<float, kFftLengthBy2Plus1>& min_gain,
126 const std::array<float, kFftLengthBy2Plus1>& max_gain,
127 const std::array<float, kFftLengthBy2Plus1>& one_by_echo,
128 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgrenc65ce782017-10-09 13:01:39 +0200129 float nearend_masking_margin = 0.f;
Per Åhgren63b494d2017-12-06 11:32:38 +0100130 if (linear_echo_estimate) {
131 nearend_masking_margin =
132 low_noise_render
133 ? config.gain_mask.m9
134 : (saturated_echo ? config.gain_mask.m2 : config.gain_mask.m3);
Per Åhgrenc65ce782017-10-09 13:01:39 +0200135 } else {
Per Åhgren63b494d2017-12-06 11:32:38 +0100136 nearend_masking_margin = config.gain_mask.m7;
Per Åhgrenc65ce782017-10-09 13:01:39 +0200137 }
Per Åhgren7ddd4632017-10-25 02:59:45 +0200138
Per Åhgrend309b002017-10-09 23:50:44 +0200139 RTC_DCHECK_LE(0.f, nearend_masking_margin);
140 RTC_DCHECK_GT(1.f, nearend_masking_margin);
141 const float one_by_one_minus_nearend_masking_margin =
142 1.f / (1.0f - nearend_masking_margin);
143
Per Åhgren63b494d2017-12-06 11:32:38 +0100144 const float masker_margin =
145 linear_echo_estimate ? config.gain_mask.m1 : config.gain_mask.m8;
peah1d680892017-05-23 04:07:10 -0700146
147 for (size_t k = 0; k < gain->size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200148 const float unity_gain_masker = std::max(nearend[k], masker[k]);
149 RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker);
150 if (echo[k] <= nearend_masking_margin * unity_gain_masker ||
151 unity_gain_masker <= 0.f) {
peah1d680892017-05-23 04:07:10 -0700152 (*gain)[k] = 1.f;
153 } else {
Per Åhgrend309b002017-10-09 23:50:44 +0200154 RTC_DCHECK_LT(0.f, unity_gain_masker);
Per Åhgren1f33a372017-10-11 02:36:53 +0200155 (*gain)[k] = std::max(0.f, (1.f - 5.f * echo[k] / unity_gain_masker) *
Per Åhgrend309b002017-10-09 23:50:44 +0200156 one_by_one_minus_nearend_masking_margin);
157 (*gain)[k] =
158 std::max(masker_margin * masker[k] * one_by_echo[k], (*gain)[k]);
peah1d680892017-05-23 04:07:10 -0700159 }
160
161 (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]);
162 }
163}
164
Per Åhgren85a11a32017-10-02 14:42:06 +0200165// TODO(peah): Make adaptive to take the actual filter error into account.
166constexpr size_t kUpperAccurateBandPlus1 = 29;
167
peah1d680892017-05-23 04:07:10 -0700168// Computes the signal output power that masks the echo signal.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200169void MaskingPower(const EchoCanceller3Config& config,
peah8cee56f2017-08-24 22:36:53 -0700170 const std::array<float, kFftLengthBy2Plus1>& nearend,
peah1d680892017-05-23 04:07:10 -0700171 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
172 const std::array<float, kFftLengthBy2Plus1>& last_masker,
173 const std::array<float, kFftLengthBy2Plus1>& gain,
174 std::array<float, kFftLengthBy2Plus1>* masker) {
175 std::array<float, kFftLengthBy2Plus1> side_band_masker;
Per Åhgren7106d932017-10-09 08:25:18 +0200176 float max_nearend_after_gain = 0.f;
peah1d680892017-05-23 04:07:10 -0700177 for (size_t k = 0; k < gain.size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200178 const float nearend_after_gain = nearend[k] * gain[k];
179 max_nearend_after_gain =
180 std::max(max_nearend_after_gain, nearend_after_gain);
181 side_band_masker[k] = nearend_after_gain + comfort_noise[k];
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200182 (*masker)[k] = comfort_noise[k] + config.gain_mask.m4 * last_masker[k];
peah1d680892017-05-23 04:07:10 -0700183 }
Per Åhgren85a11a32017-10-02 14:42:06 +0200184
185 // Apply masking only between lower frequency bands.
186 RTC_DCHECK_LT(kUpperAccurateBandPlus1, gain.size());
187 for (size_t k = 1; k < kUpperAccurateBandPlus1; ++k) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200188 (*masker)[k] += config.gain_mask.m5 *
Per Åhgren7106d932017-10-09 08:25:18 +0200189 (side_band_masker[k - 1] + side_band_masker[k + 1]);
peah1d680892017-05-23 04:07:10 -0700190 }
Per Åhgren7106d932017-10-09 08:25:18 +0200191
192 // Add full-band masking as a minimum value for the masker.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200193 const float min_masker = max_nearend_after_gain * config.gain_mask.m6;
Per Åhgren7106d932017-10-09 08:25:18 +0200194 std::for_each(masker->begin(), masker->end(),
195 [min_masker](float& a) { a = std::max(a, min_masker); });
peah1d680892017-05-23 04:07:10 -0700196}
197
Per Åhgren85a11a32017-10-02 14:42:06 +0200198// Limits the gain in the frequencies for which the adaptive filter has not
199// converged. Currently, these frequencies are not hardcoded to the frequencies
200// which are typically not excited by speech.
201// TODO(peah): Make adaptive to take the actual filter error into account.
202void AdjustNonConvergedFrequencies(
203 std::array<float, kFftLengthBy2Plus1>* gain) {
204 constexpr float oneByBandsInSum =
205 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
206 const float hf_gain_bound =
207 std::accumulate(gain->begin() + 20,
208 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
209 oneByBandsInSum;
210
211 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
212 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
213}
214
peah1d680892017-05-23 04:07:10 -0700215} // namespace
216
217// TODO(peah): Add further optimizations, in particular for the divisions.
218void SuppressionGain::LowerBandGain(
219 bool low_noise_render,
peah14c11a42017-07-11 06:13:43 -0700220 const rtc::Optional<int>& narrow_peak_band,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100221 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -0700222 const std::array<float, kFftLengthBy2Plus1>& nearend,
223 const std::array<float, kFftLengthBy2Plus1>& echo,
224 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
225 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100226 const bool saturated_echo = aec_state.SaturatedEcho();
227 const bool saturating_echo_path = aec_state.SaturatingEchoPath();
228 const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
229
peah1d680892017-05-23 04:07:10 -0700230 // Count the number of blocks since saturation.
231 no_saturation_counter_ = saturated_echo ? 0 : no_saturation_counter_ + 1;
232
233 // Precompute 1/echo (note that when the echo is zero, the precomputed value
234 // is never used).
235 std::array<float, kFftLengthBy2Plus1> one_by_echo;
236 std::transform(echo.begin(), echo.end(), one_by_echo.begin(),
237 [](float a) { return a > 0.f ? 1.f / a : 1.f; });
238
239 // Compute the minimum gain as the attenuating gain to put the signal just
240 // above the zero sample values.
241 std::array<float, kFftLengthBy2Plus1> min_gain;
peah8cee56f2017-08-24 22:36:53 -0700242 const float min_echo_power =
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200243 low_noise_render ? config_.echo_audibility.low_render_limit
244 : config_.echo_audibility.normal_render_limit;
Per Åhgren63b494d2017-12-06 11:32:38 +0100245 if (no_saturation_counter_ > 10) {
peah1d680892017-05-23 04:07:10 -0700246 for (size_t k = 0; k < nearend.size(); ++k) {
247 const float denom = std::min(nearend[k], echo[k]);
248 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
249 min_gain[k] = std::min(min_gain[k], 1.f);
250 }
251 } else {
252 min_gain.fill(0.f);
253 }
254
255 // Compute the maximum gain by limiting the gain increase from the previous
256 // gain.
257 std::array<float, kFftLengthBy2Plus1> max_gain;
258 for (size_t k = 0; k < gain->size(); ++k) {
Per Åhgren63b494d2017-12-06 11:32:38 +0100259 max_gain[k] = std::min(std::max(last_gain_[k] * gain_increase_[k],
260 config_.gain_updates.floor_first_increase),
261 1.f);
peah1d680892017-05-23 04:07:10 -0700262 }
263
264 // Iteratively compute the gain required to attenuate the echo to a non
265 // noticeable level.
266 gain->fill(0.f);
267 for (int k = 0; k < 2; ++k) {
268 std::array<float, kFftLengthBy2Plus1> masker;
peah8cee56f2017-08-24 22:36:53 -0700269 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain, &masker);
Per Åhgren63b494d2017-12-06 11:32:38 +0100270 GainToNoAudibleEcho(config_, low_noise_render, saturated_echo,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200271 saturating_echo_path, linear_echo_estimate, nearend,
272 echo, masker, min_gain, max_gain, one_by_echo, gain);
peah1d680892017-05-23 04:07:10 -0700273 AdjustForExternalFilters(gain);
peah14c11a42017-07-11 06:13:43 -0700274 if (narrow_peak_band) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100275 NarrowBandAttenuation(*narrow_peak_band, nearend, echo, gain);
peah14c11a42017-07-11 06:13:43 -0700276 }
peah1d680892017-05-23 04:07:10 -0700277 }
278
Per Åhgren85a11a32017-10-02 14:42:06 +0200279 // Adjust the gain for frequencies which have not yet converged.
280 AdjustNonConvergedFrequencies(gain);
281
peah1d680892017-05-23 04:07:10 -0700282 // Update the allowed maximum gain increase.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100283 UpdateGainIncrease(low_noise_render, linear_echo_estimate, echo, *gain);
peah1d680892017-05-23 04:07:10 -0700284
Per Åhgren1f33a372017-10-11 02:36:53 +0200285 // Adjust gain dynamics.
286 const float gain_bound =
287 std::max(0.001f, *std::min_element(gain->begin(), gain->end()) * 10000.f);
288 std::for_each(gain->begin(), gain->end(),
289 [gain_bound](float& a) { a = std::min(a, gain_bound); });
290
peah1d680892017-05-23 04:07:10 -0700291 // Store data required for the gain computation of the next block.
292 std::copy(echo.begin(), echo.end(), last_echo_.begin());
293 std::copy(gain->begin(), gain->end(), last_gain_.begin());
peah8cee56f2017-08-24 22:36:53 -0700294 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain,
295 &last_masker_);
peah1d680892017-05-23 04:07:10 -0700296 aec3::VectorMath(optimization_).Sqrt(*gain);
peah86afe9d2017-04-06 15:45:32 -0700297}
298
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200299SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
300 Aec3Optimization optimization)
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100301 : optimization_(optimization),
302 config_(config),
303 state_change_duration_blocks_(
304 static_cast<int>(config_.filter.config_change_duration_blocks)) {
305 RTC_DCHECK_LT(0, state_change_duration_blocks_);
306 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700307 last_gain_.fill(1.f);
308 last_masker_.fill(0.f);
309 gain_increase_.fill(1.f);
310 last_echo_.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800311}
312
313void SuppressionGain::GetGain(
peah1d680892017-05-23 04:07:10 -0700314 const std::array<float, kFftLengthBy2Plus1>& nearend,
315 const std::array<float, kFftLengthBy2Plus1>& echo,
316 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
peah14c11a42017-07-11 06:13:43 -0700317 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200318 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700319 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700320 float* high_bands_gain,
321 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
322 RTC_DCHECK(high_bands_gain);
323 RTC_DCHECK(low_band_gain);
324
peah1d680892017-05-23 04:07:10 -0700325 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100326 bool low_noise_render = low_render_detector_.Detect(render);
peah14c11a42017-07-11 06:13:43 -0700327 const rtc::Optional<int> narrow_peak_band =
328 render_signal_analyzer.NarrowPeakBand();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100329 LowerBandGain(low_noise_render, narrow_peak_band, aec_state, nearend, echo,
330 comfort_noise, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700331
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100332 const float gain_upper_bound = aec_state.SuppressionGainLimit();
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100333 if (gain_upper_bound < 1.f) {
334 for (size_t k = 0; k < low_band_gain->size(); ++k) {
335 (*low_band_gain)[k] = std::min((*low_band_gain)[k], gain_upper_bound);
336 }
337 }
338
peah1d680892017-05-23 04:07:10 -0700339 // Compute the gain for the upper bands.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100340 *high_bands_gain = UpperBandsGain(narrow_peak_band, aec_state.SaturatedEcho(),
341 render, *low_band_gain);
342}
343
344void SuppressionGain::SetInitialState(bool state) {
345 initial_state_ = state;
346 if (state) {
347 initial_state_change_counter_ = state_change_duration_blocks_;
348 } else {
349 initial_state_change_counter_ = 0;
350 }
351}
352
353void SuppressionGain::UpdateGainIncrease(
354 bool low_noise_render,
355 bool linear_echo_estimate,
356 const std::array<float, kFftLengthBy2Plus1>& echo,
357 const std::array<float, kFftLengthBy2Plus1>& new_gain) {
358 float max_inc;
359 float max_dec;
360 float rate_inc;
361 float rate_dec;
362 float min_inc;
363 float min_dec;
364
365 RTC_DCHECK_GE(state_change_duration_blocks_, initial_state_change_counter_);
366 if (initial_state_change_counter_ > 0) {
367 if (--initial_state_change_counter_ == 0) {
368 initial_state_ = false;
369 }
370 }
371 RTC_DCHECK_LE(0, initial_state_change_counter_);
372
373 // EchoCanceller3Config::GainUpdates
374 auto& p = config_.gain_updates;
375 if (!linear_echo_estimate) {
376 max_inc = p.nonlinear.max_inc;
377 max_dec = p.nonlinear.max_dec;
378 rate_inc = p.nonlinear.rate_inc;
379 rate_dec = p.nonlinear.rate_dec;
380 min_inc = p.nonlinear.min_inc;
381 min_dec = p.nonlinear.min_dec;
382 } else if (initial_state_ && no_saturation_counter_ > 10) {
383 if (initial_state_change_counter_ > 0) {
384 float change_factor =
385 initial_state_change_counter_ * one_by_state_change_duration_blocks_;
386
387 auto average = [](float from, float to, float from_weight) {
388 return from * from_weight + to * (1.f - from_weight);
389 };
390
391 max_inc = average(p.initial.max_inc, p.normal.max_inc, change_factor);
392 max_dec = average(p.initial.max_dec, p.normal.max_dec, change_factor);
393 rate_inc = average(p.initial.rate_inc, p.normal.rate_inc, change_factor);
394 rate_dec = average(p.initial.rate_dec, p.normal.rate_dec, change_factor);
395 min_inc = average(p.initial.min_inc, p.normal.min_inc, change_factor);
396 min_dec = average(p.initial.min_dec, p.normal.min_dec, change_factor);
397 } else {
398 max_inc = p.initial.max_inc;
399 max_dec = p.initial.max_dec;
400 rate_inc = p.initial.rate_inc;
401 rate_dec = p.initial.rate_dec;
402 min_inc = p.initial.min_inc;
403 min_dec = p.initial.min_dec;
404 }
405 } else if (low_noise_render) {
406 max_inc = p.low_noise.max_inc;
407 max_dec = p.low_noise.max_dec;
408 rate_inc = p.low_noise.rate_inc;
409 rate_dec = p.low_noise.rate_dec;
410 min_inc = p.low_noise.min_inc;
411 min_dec = p.low_noise.min_dec;
412 } else if (no_saturation_counter_ > 10) {
413 max_inc = p.normal.max_inc;
414 max_dec = p.normal.max_dec;
415 rate_inc = p.normal.rate_inc;
416 rate_dec = p.normal.rate_dec;
417 min_inc = p.normal.min_inc;
418 min_dec = p.normal.min_dec;
419 } else {
420 max_inc = p.saturation.max_inc;
421 max_dec = p.saturation.max_dec;
422 rate_inc = p.saturation.rate_inc;
423 rate_dec = p.saturation.rate_dec;
424 min_inc = p.saturation.min_inc;
425 min_dec = p.saturation.min_dec;
426 }
427
428 for (size_t k = 0; k < new_gain.size(); ++k) {
429 auto increase_update = [](float new_gain, float last_gain,
430 float current_inc, float max_inc, float min_inc,
431 float change_rate) {
432 return new_gain > last_gain ? std::min(max_inc, current_inc * change_rate)
433 : min_inc;
434 };
435
436 if (echo[k] > last_echo_[k]) {
437 gain_increase_[k] =
438 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
439 max_inc, min_inc, rate_inc);
440 } else {
441 gain_increase_[k] =
442 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
443 max_dec, min_dec, rate_dec);
444 }
445 }
peah1d680892017-05-23 04:07:10 -0700446}
peah86afe9d2017-04-06 15:45:32 -0700447
peah1d680892017-05-23 04:07:10 -0700448// Detects when the render signal can be considered to have low power and
449// consist of stationary noise.
450bool SuppressionGain::LowNoiseRenderDetector::Detect(
451 const std::vector<std::vector<float>>& render) {
452 float x2_sum = 0.f;
453 float x2_max = 0.f;
454 for (auto x_k : render[0]) {
455 const float x2 = x_k * x_k;
456 x2_sum += x2;
457 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800458 }
peah1d680892017-05-23 04:07:10 -0700459
460 constexpr float kThreshold = 50.f * 50.f * 64.f;
461 const bool low_noise_render =
462 average_power_ < kThreshold && x2_max < 3 * average_power_;
463 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
464 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800465}
466
467} // namespace webrtc