blob: 49197f6ad964e264136c8825e9ce330ecc141f79 [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"
24#include "rtc_base/checks.h"
peahcf02cf12017-04-05 14:18:07 -070025
peah522d71b2017-02-23 05:16:26 -080026namespace webrtc {
27namespace {
28
peah14c11a42017-07-11 06:13:43 -070029// Reduce gain to avoid narrow band echo leakage.
30void NarrowBandAttenuation(int narrow_bin,
Per Åhgren5c532d32018-03-22 00:29:25 +010031 const std::array<float, kFftLengthBy2Plus1>& nearend,
32 const std::array<float, kFftLengthBy2Plus1>& echo,
peah14c11a42017-07-11 06:13:43 -070033 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5c532d32018-03-22 00:29:25 +010034 // TODO(peah): Verify that the condition below is not too conservative.
35 if (10.f * echo[narrow_bin] > nearend[narrow_bin]) {
36 const int upper_bin =
37 std::min(narrow_bin + 6, static_cast<int>(kFftLengthBy2Plus1 - 1));
38 for (int k = std::max(0, narrow_bin - 6); k <= upper_bin; ++k) {
39 (*gain)[k] = std::min((*gain)[k], 0.001f);
40 }
peah14c11a42017-07-11 06:13:43 -070041 }
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(
peah14c11a42017-07-11 06:13:43 -070064 const rtc::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
peah1d680892017-05-23 04:07:10 -0700116// Computes the gain to reduce the echo to a non audible level.
117void GainToNoAudibleEcho(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200118 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700119 bool low_noise_render,
120 bool saturated_echo,
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();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100227 const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
228
peah1d680892017-05-23 04:07:10 -0700229 // Precompute 1/echo (note that when the echo is zero, the precomputed value
230 // is never used).
231 std::array<float, kFftLengthBy2Plus1> one_by_echo;
232 std::transform(echo.begin(), echo.end(), one_by_echo.begin(),
233 [](float a) { return a > 0.f ? 1.f / a : 1.f; });
234
235 // Compute the minimum gain as the attenuating gain to put the signal just
236 // above the zero sample values.
237 std::array<float, kFftLengthBy2Plus1> min_gain;
peah8cee56f2017-08-24 22:36:53 -0700238 const float min_echo_power =
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200239 low_noise_render ? config_.echo_audibility.low_render_limit
240 : config_.echo_audibility.normal_render_limit;
Per Åhgren31122d62018-04-10 16:33:55 +0200241 if (!saturated_echo) {
peah1d680892017-05-23 04:07:10 -0700242 for (size_t k = 0; k < nearend.size(); ++k) {
243 const float denom = std::min(nearend[k], echo[k]);
244 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
245 min_gain[k] = std::min(min_gain[k], 1.f);
246 }
247 } else {
248 min_gain.fill(0.f);
249 }
250
251 // Compute the maximum gain by limiting the gain increase from the previous
252 // gain.
253 std::array<float, kFftLengthBy2Plus1> max_gain;
254 for (size_t k = 0; k < gain->size(); ++k) {
Per Åhgren63b494d2017-12-06 11:32:38 +0100255 max_gain[k] = std::min(std::max(last_gain_[k] * gain_increase_[k],
256 config_.gain_updates.floor_first_increase),
257 1.f);
peah1d680892017-05-23 04:07:10 -0700258 }
259
260 // Iteratively compute the gain required to attenuate the echo to a non
261 // noticeable level.
262 gain->fill(0.f);
263 for (int k = 0; k < 2; ++k) {
264 std::array<float, kFftLengthBy2Plus1> masker;
peah8cee56f2017-08-24 22:36:53 -0700265 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain, &masker);
Per Åhgren63b494d2017-12-06 11:32:38 +0100266 GainToNoAudibleEcho(config_, low_noise_render, saturated_echo,
Per Åhgren31122d62018-04-10 16:33:55 +0200267 linear_echo_estimate, nearend, echo, masker, min_gain,
268 max_gain, one_by_echo, gain);
peah1d680892017-05-23 04:07:10 -0700269 AdjustForExternalFilters(gain);
peah14c11a42017-07-11 06:13:43 -0700270 if (narrow_peak_band) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100271 NarrowBandAttenuation(*narrow_peak_band, nearend, echo, gain);
peah14c11a42017-07-11 06:13:43 -0700272 }
peah1d680892017-05-23 04:07:10 -0700273 }
274
Per Åhgren85a11a32017-10-02 14:42:06 +0200275 // Adjust the gain for frequencies which have not yet converged.
276 AdjustNonConvergedFrequencies(gain);
277
peah1d680892017-05-23 04:07:10 -0700278 // Update the allowed maximum gain increase.
Per Åhgren31122d62018-04-10 16:33:55 +0200279 UpdateGainIncrease(low_noise_render, linear_echo_estimate, saturated_echo,
280 echo, *gain);
peah1d680892017-05-23 04:07:10 -0700281
Per Åhgren1f33a372017-10-11 02:36:53 +0200282 // Adjust gain dynamics.
283 const float gain_bound =
284 std::max(0.001f, *std::min_element(gain->begin(), gain->end()) * 10000.f);
285 std::for_each(gain->begin(), gain->end(),
286 [gain_bound](float& a) { a = std::min(a, gain_bound); });
287
peah1d680892017-05-23 04:07:10 -0700288 // Store data required for the gain computation of the next block.
289 std::copy(echo.begin(), echo.end(), last_echo_.begin());
290 std::copy(gain->begin(), gain->end(), last_gain_.begin());
peah8cee56f2017-08-24 22:36:53 -0700291 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain,
292 &last_masker_);
peah1d680892017-05-23 04:07:10 -0700293 aec3::VectorMath(optimization_).Sqrt(*gain);
peah86afe9d2017-04-06 15:45:32 -0700294}
295
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200296SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
297 Aec3Optimization optimization)
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100298 : optimization_(optimization),
299 config_(config),
300 state_change_duration_blocks_(
301 static_cast<int>(config_.filter.config_change_duration_blocks)) {
302 RTC_DCHECK_LT(0, state_change_duration_blocks_);
303 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700304 last_gain_.fill(1.f);
305 last_masker_.fill(0.f);
306 gain_increase_.fill(1.f);
307 last_echo_.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800308}
309
310void SuppressionGain::GetGain(
peah1d680892017-05-23 04:07:10 -0700311 const std::array<float, kFftLengthBy2Plus1>& nearend,
312 const std::array<float, kFftLengthBy2Plus1>& echo,
313 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
peah14c11a42017-07-11 06:13:43 -0700314 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200315 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700316 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700317 float* high_bands_gain,
318 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
319 RTC_DCHECK(high_bands_gain);
320 RTC_DCHECK(low_band_gain);
321
peah1d680892017-05-23 04:07:10 -0700322 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100323 bool low_noise_render = low_render_detector_.Detect(render);
peah14c11a42017-07-11 06:13:43 -0700324 const rtc::Optional<int> narrow_peak_band =
325 render_signal_analyzer.NarrowPeakBand();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100326 LowerBandGain(low_noise_render, narrow_peak_band, aec_state, nearend, echo,
327 comfort_noise, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700328
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100329 const float gain_upper_bound = aec_state.SuppressionGainLimit();
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100330 if (gain_upper_bound < 1.f) {
331 for (size_t k = 0; k < low_band_gain->size(); ++k) {
332 (*low_band_gain)[k] = std::min((*low_band_gain)[k], gain_upper_bound);
333 }
334 }
335
peah1d680892017-05-23 04:07:10 -0700336 // Compute the gain for the upper bands.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100337 *high_bands_gain = UpperBandsGain(narrow_peak_band, aec_state.SaturatedEcho(),
338 render, *low_band_gain);
339}
340
341void SuppressionGain::SetInitialState(bool state) {
342 initial_state_ = state;
343 if (state) {
344 initial_state_change_counter_ = state_change_duration_blocks_;
345 } else {
346 initial_state_change_counter_ = 0;
347 }
348}
349
350void SuppressionGain::UpdateGainIncrease(
351 bool low_noise_render,
352 bool linear_echo_estimate,
Per Åhgren31122d62018-04-10 16:33:55 +0200353 bool saturated_echo,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100354 const std::array<float, kFftLengthBy2Plus1>& echo,
355 const std::array<float, kFftLengthBy2Plus1>& new_gain) {
356 float max_inc;
357 float max_dec;
358 float rate_inc;
359 float rate_dec;
360 float min_inc;
361 float min_dec;
362
363 RTC_DCHECK_GE(state_change_duration_blocks_, initial_state_change_counter_);
364 if (initial_state_change_counter_ > 0) {
365 if (--initial_state_change_counter_ == 0) {
366 initial_state_ = false;
367 }
368 }
369 RTC_DCHECK_LE(0, initial_state_change_counter_);
370
371 // EchoCanceller3Config::GainUpdates
372 auto& p = config_.gain_updates;
373 if (!linear_echo_estimate) {
374 max_inc = p.nonlinear.max_inc;
375 max_dec = p.nonlinear.max_dec;
376 rate_inc = p.nonlinear.rate_inc;
377 rate_dec = p.nonlinear.rate_dec;
378 min_inc = p.nonlinear.min_inc;
379 min_dec = p.nonlinear.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200380 } else if (initial_state_ && !saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100381 if (initial_state_change_counter_ > 0) {
382 float change_factor =
383 initial_state_change_counter_ * one_by_state_change_duration_blocks_;
384
385 auto average = [](float from, float to, float from_weight) {
386 return from * from_weight + to * (1.f - from_weight);
387 };
388
389 max_inc = average(p.initial.max_inc, p.normal.max_inc, change_factor);
390 max_dec = average(p.initial.max_dec, p.normal.max_dec, change_factor);
391 rate_inc = average(p.initial.rate_inc, p.normal.rate_inc, change_factor);
392 rate_dec = average(p.initial.rate_dec, p.normal.rate_dec, change_factor);
393 min_inc = average(p.initial.min_inc, p.normal.min_inc, change_factor);
394 min_dec = average(p.initial.min_dec, p.normal.min_dec, change_factor);
395 } else {
396 max_inc = p.initial.max_inc;
397 max_dec = p.initial.max_dec;
398 rate_inc = p.initial.rate_inc;
399 rate_dec = p.initial.rate_dec;
400 min_inc = p.initial.min_inc;
401 min_dec = p.initial.min_dec;
402 }
403 } else if (low_noise_render) {
404 max_inc = p.low_noise.max_inc;
405 max_dec = p.low_noise.max_dec;
406 rate_inc = p.low_noise.rate_inc;
407 rate_dec = p.low_noise.rate_dec;
408 min_inc = p.low_noise.min_inc;
409 min_dec = p.low_noise.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200410 } else if (!saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100411 max_inc = p.normal.max_inc;
412 max_dec = p.normal.max_dec;
413 rate_inc = p.normal.rate_inc;
414 rate_dec = p.normal.rate_dec;
415 min_inc = p.normal.min_inc;
416 min_dec = p.normal.min_dec;
417 } else {
418 max_inc = p.saturation.max_inc;
419 max_dec = p.saturation.max_dec;
420 rate_inc = p.saturation.rate_inc;
421 rate_dec = p.saturation.rate_dec;
422 min_inc = p.saturation.min_inc;
423 min_dec = p.saturation.min_dec;
424 }
425
426 for (size_t k = 0; k < new_gain.size(); ++k) {
427 auto increase_update = [](float new_gain, float last_gain,
428 float current_inc, float max_inc, float min_inc,
429 float change_rate) {
430 return new_gain > last_gain ? std::min(max_inc, current_inc * change_rate)
431 : min_inc;
432 };
433
434 if (echo[k] > last_echo_[k]) {
435 gain_increase_[k] =
436 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
437 max_inc, min_inc, rate_inc);
438 } else {
439 gain_increase_[k] =
440 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
441 max_dec, min_dec, rate_dec);
442 }
443 }
peah1d680892017-05-23 04:07:10 -0700444}
peah86afe9d2017-04-06 15:45:32 -0700445
peah1d680892017-05-23 04:07:10 -0700446// Detects when the render signal can be considered to have low power and
447// consist of stationary noise.
448bool SuppressionGain::LowNoiseRenderDetector::Detect(
449 const std::vector<std::vector<float>>& render) {
450 float x2_sum = 0.f;
451 float x2_max = 0.f;
452 for (auto x_k : render[0]) {
453 const float x2 = x_k * x_k;
454 x2_sum += x2;
455 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800456 }
peah1d680892017-05-23 04:07:10 -0700457
458 constexpr float kThreshold = 50.f * 50.f * 64.f;
459 const bool low_noise_render =
460 average_power_ < kThreshold && x2_max < 3 * average_power_;
461 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
462 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800463}
464
465} // namespace webrtc