blob: 51158303a49ae0f553a1947dc9749d102cded0c3 [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
peah1d680892017-05-23 04:07:10 -070029// Adjust the gains according to the presence of known external filters.
30void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080031 // Limit the low frequency gains to avoid the impact of the high-pass filter
32 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070033 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080034
35 // Limit the high frequency gains to avoid the impact of the anti-aliasing
36 // filter on the upper-frequency gains influencing the overall achieved
37 // gain. TODO(peah): Update this when new anti-aliasing filters are
38 // implemented.
peah86afe9d2017-04-06 15:45:32 -070039 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070040 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
41 std::for_each(
42 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
43 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
44 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080045}
46
peah1d680892017-05-23 04:07:10 -070047// Computes the gain to apply for the bands beyond the first band.
48float UpperBandsGain(
peah14c11a42017-07-11 06:13:43 -070049 const rtc::Optional<int>& narrow_peak_band,
peah1d680892017-05-23 04:07:10 -070050 bool saturated_echo,
51 const std::vector<std::vector<float>>& render,
52 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) {
53 RTC_DCHECK_LT(0, render.size());
peah86afe9d2017-04-06 15:45:32 -070054 if (render.size() == 1) {
55 return 1.f;
56 }
57
peah14c11a42017-07-11 06:13:43 -070058 if (narrow_peak_band &&
59 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
60 return 0.001f;
61 }
62
peah1d680892017-05-23 04:07:10 -070063 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
64 const float gain_below_8_khz = *std::min_element(
65 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
66
peah86afe9d2017-04-06 15:45:32 -070067 // Always attenuate the upper bands when there is saturated echo.
68 if (saturated_echo) {
peah1d680892017-05-23 04:07:10 -070069 return std::min(0.001f, gain_below_8_khz);
peah86afe9d2017-04-06 15:45:32 -070070 }
71
72 // Compute the upper and lower band energies.
peah1d680892017-05-23 04:07:10 -070073 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
74 const float low_band_energy =
75 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
76 float high_band_energy = 0.f;
peah86afe9d2017-04-06 15:45:32 -070077 for (size_t k = 1; k < render.size(); ++k) {
peah1d680892017-05-23 04:07:10 -070078 const float energy = std::accumulate(render[k].begin(), render[k].end(),
79 0.f, sum_of_squares);
80 high_band_energy = std::max(high_band_energy, energy);
peah86afe9d2017-04-06 15:45:32 -070081 }
82
83 // If there is more power in the lower frequencies than the upper frequencies,
peah1d680892017-05-23 04:07:10 -070084 // or if the power in upper frequencies is low, do not bound the gain in the
peah86afe9d2017-04-06 15:45:32 -070085 // upper bands.
peah1d680892017-05-23 04:07:10 -070086 float anti_howling_gain;
Per Åhgren38e2d952017-11-17 14:54:28 +010087 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
peah1d680892017-05-23 04:07:10 -070088 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
89 anti_howling_gain = 1.f;
90 } else {
91 // In all other cases, bound the gain for upper frequencies.
92 RTC_DCHECK_LE(low_band_energy, high_band_energy);
93 RTC_DCHECK_NE(0.f, high_band_energy);
94 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
peah86afe9d2017-04-06 15:45:32 -070095 }
96
peah1d680892017-05-23 04:07:10 -070097 // Choose the gain as the minimum of the lower and upper gains.
98 return std::min(gain_below_8_khz, anti_howling_gain);
99}
100
Per Åhgrenb02644f2018-04-17 11:52:17 +0200101// Scales the echo according to assessed audibility at the other end.
102void WeightEchoForAudibility(const EchoCanceller3Config& config,
103 rtc::ArrayView<const float> echo,
104 rtc::ArrayView<float> weighted_echo,
105 rtc::ArrayView<float> one_by_weighted_echo) {
106 RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
107 RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
108 RTC_DCHECK_EQ(kFftLengthBy2Plus1, one_by_weighted_echo.size());
109
110 auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
111 rtc::ArrayView<const float> echo,
112 rtc::ArrayView<float> weighted_echo,
113 rtc::ArrayView<float> one_by_weighted_echo) {
114 for (size_t k = begin; k < end; ++k) {
115 if (echo[k] < threshold) {
116 float tmp = (threshold - echo[k]) * normalizer;
117 weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp);
118 } else {
119 weighted_echo[k] = echo[k];
120 }
121 one_by_weighted_echo[k] =
122 weighted_echo[k] > 0.f ? 1.f / weighted_echo[k] : 1.f;
123 }
124 };
125
126 float threshold = config.echo_audibility.floor_power *
127 config.echo_audibility.audibility_threshold_lf;
128 float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
129 weigh(threshold, normalizer, 0, 3, echo, weighted_echo, one_by_weighted_echo);
130
131 threshold = config.echo_audibility.floor_power *
132 config.echo_audibility.audibility_threshold_mf;
133 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
134 weigh(threshold, normalizer, 3, 7, echo, weighted_echo, one_by_weighted_echo);
135
136 threshold = config.echo_audibility.floor_power *
137 config.echo_audibility.audibility_threshold_hf;
138 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
139 weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo,
140 one_by_weighted_echo);
141}
142
peah1d680892017-05-23 04:07:10 -0700143// Computes the gain to reduce the echo to a non audible level.
144void GainToNoAudibleEcho(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200145 const EchoCanceller3Config& config,
peah1d680892017-05-23 04:07:10 -0700146 bool low_noise_render,
147 bool saturated_echo,
Per Åhgrenc65ce782017-10-09 13:01:39 +0200148 bool linear_echo_estimate,
peah1d680892017-05-23 04:07:10 -0700149 const std::array<float, kFftLengthBy2Plus1>& nearend,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200150 const std::array<float, kFftLengthBy2Plus1>& weighted_echo,
peah1d680892017-05-23 04:07:10 -0700151 const std::array<float, kFftLengthBy2Plus1>& masker,
152 const std::array<float, kFftLengthBy2Plus1>& min_gain,
153 const std::array<float, kFftLengthBy2Plus1>& max_gain,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200154 const std::array<float, kFftLengthBy2Plus1>& one_by_weighted_echo,
peah1d680892017-05-23 04:07:10 -0700155 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgrenc65ce782017-10-09 13:01:39 +0200156 float nearend_masking_margin = 0.f;
Per Åhgren63b494d2017-12-06 11:32:38 +0100157 if (linear_echo_estimate) {
158 nearend_masking_margin =
159 low_noise_render
160 ? config.gain_mask.m9
161 : (saturated_echo ? config.gain_mask.m2 : config.gain_mask.m3);
Per Åhgrenc65ce782017-10-09 13:01:39 +0200162 } else {
Per Åhgren63b494d2017-12-06 11:32:38 +0100163 nearend_masking_margin = config.gain_mask.m7;
Per Åhgrenc65ce782017-10-09 13:01:39 +0200164 }
Per Åhgren7ddd4632017-10-25 02:59:45 +0200165
Per Åhgrend309b002017-10-09 23:50:44 +0200166 RTC_DCHECK_LE(0.f, nearend_masking_margin);
167 RTC_DCHECK_GT(1.f, nearend_masking_margin);
Per Åhgrend309b002017-10-09 23:50:44 +0200168
Per Åhgren63b494d2017-12-06 11:32:38 +0100169 const float masker_margin =
170 linear_echo_estimate ? config.gain_mask.m1 : config.gain_mask.m8;
peah1d680892017-05-23 04:07:10 -0700171
172 for (size_t k = 0; k < gain->size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200173 const float unity_gain_masker = std::max(nearend[k], masker[k]);
174 RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker);
Per Åhgrenb02644f2018-04-17 11:52:17 +0200175 if (weighted_echo[k] <= nearend_masking_margin * unity_gain_masker ||
Per Åhgren7106d932017-10-09 08:25:18 +0200176 unity_gain_masker <= 0.f) {
peah1d680892017-05-23 04:07:10 -0700177 (*gain)[k] = 1.f;
178 } else {
Per Åhgrend309b002017-10-09 23:50:44 +0200179 RTC_DCHECK_LT(0.f, unity_gain_masker);
Per Åhgrend309b002017-10-09 23:50:44 +0200180 (*gain)[k] =
Per Åhgrenb02644f2018-04-17 11:52:17 +0200181 std::max(0.f, (1.f - config.gain_mask.gain_curve_slope *
182 weighted_echo[k] / unity_gain_masker) *
183 config.gain_mask.gain_curve_offset);
184 (*gain)[k] = std::max(masker_margin * masker[k] * one_by_weighted_echo[k],
185 (*gain)[k]);
peah1d680892017-05-23 04:07:10 -0700186 }
187
188 (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]);
189 }
190}
191
Per Åhgren85a11a32017-10-02 14:42:06 +0200192// TODO(peah): Make adaptive to take the actual filter error into account.
193constexpr size_t kUpperAccurateBandPlus1 = 29;
194
peah1d680892017-05-23 04:07:10 -0700195// Computes the signal output power that masks the echo signal.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200196void MaskingPower(const EchoCanceller3Config& config,
peah8cee56f2017-08-24 22:36:53 -0700197 const std::array<float, kFftLengthBy2Plus1>& nearend,
peah1d680892017-05-23 04:07:10 -0700198 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
199 const std::array<float, kFftLengthBy2Plus1>& last_masker,
200 const std::array<float, kFftLengthBy2Plus1>& gain,
201 std::array<float, kFftLengthBy2Plus1>* masker) {
Per Åhgrenb02644f2018-04-17 11:52:17 +0200202 // Apply masking over time.
203 float masking_factor = config.gain_mask.temporal_masking_lf;
204 auto limit = config.gain_mask.temporal_masking_lf_bands;
205 std::transform(
206 comfort_noise.begin(), comfort_noise.begin() + limit, last_masker.begin(),
207 masker->begin(),
208 [masking_factor](float a, float b) { return a + masking_factor * b; });
209 masking_factor = config.gain_mask.temporal_masking_hf;
210 std::transform(
211 comfort_noise.begin() + limit, comfort_noise.end(),
212 last_masker.begin() + limit, masker->begin() + limit,
213 [masking_factor](float a, float b) { return a + masking_factor * b; });
214
215 // Apply masking only between lower frequency bands.
peah1d680892017-05-23 04:07:10 -0700216 std::array<float, kFftLengthBy2Plus1> side_band_masker;
Per Åhgren7106d932017-10-09 08:25:18 +0200217 float max_nearend_after_gain = 0.f;
peah1d680892017-05-23 04:07:10 -0700218 for (size_t k = 0; k < gain.size(); ++k) {
Per Åhgren7106d932017-10-09 08:25:18 +0200219 const float nearend_after_gain = nearend[k] * gain[k];
220 max_nearend_after_gain =
221 std::max(max_nearend_after_gain, nearend_after_gain);
222 side_band_masker[k] = nearend_after_gain + comfort_noise[k];
peah1d680892017-05-23 04:07:10 -0700223 }
Per Åhgren85a11a32017-10-02 14:42:06 +0200224
Per Åhgren85a11a32017-10-02 14:42:06 +0200225 RTC_DCHECK_LT(kUpperAccurateBandPlus1, gain.size());
226 for (size_t k = 1; k < kUpperAccurateBandPlus1; ++k) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200227 (*masker)[k] += config.gain_mask.m5 *
Per Åhgren7106d932017-10-09 08:25:18 +0200228 (side_band_masker[k - 1] + side_band_masker[k + 1]);
peah1d680892017-05-23 04:07:10 -0700229 }
Per Åhgren7106d932017-10-09 08:25:18 +0200230
231 // Add full-band masking as a minimum value for the masker.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200232 const float min_masker = max_nearend_after_gain * config.gain_mask.m6;
Per Åhgren7106d932017-10-09 08:25:18 +0200233 std::for_each(masker->begin(), masker->end(),
234 [min_masker](float& a) { a = std::max(a, min_masker); });
peah1d680892017-05-23 04:07:10 -0700235}
236
Per Åhgren85a11a32017-10-02 14:42:06 +0200237// Limits the gain in the frequencies for which the adaptive filter has not
238// converged. Currently, these frequencies are not hardcoded to the frequencies
239// which are typically not excited by speech.
240// TODO(peah): Make adaptive to take the actual filter error into account.
241void AdjustNonConvergedFrequencies(
242 std::array<float, kFftLengthBy2Plus1>* gain) {
243 constexpr float oneByBandsInSum =
244 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
245 const float hf_gain_bound =
246 std::accumulate(gain->begin() + 20,
247 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
248 oneByBandsInSum;
249
250 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
251 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
252}
253
peah1d680892017-05-23 04:07:10 -0700254} // namespace
255
256// TODO(peah): Add further optimizations, in particular for the divisions.
257void SuppressionGain::LowerBandGain(
258 bool low_noise_render,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100259 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -0700260 const std::array<float, kFftLengthBy2Plus1>& nearend,
261 const std::array<float, kFftLengthBy2Plus1>& echo,
262 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
263 std::array<float, kFftLengthBy2Plus1>* gain) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100264 const bool saturated_echo = aec_state.SaturatedEcho();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100265 const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
266
Per Åhgrenb02644f2018-04-17 11:52:17 +0200267 // Weight echo power in terms of audibility. // Precompute 1/weighted echo
268 // (note that when the echo is zero, the precomputed value is never used).
269 std::array<float, kFftLengthBy2Plus1> weighted_echo;
270 std::array<float, kFftLengthBy2Plus1> one_by_weighted_echo;
271 WeightEchoForAudibility(config_, echo, weighted_echo, one_by_weighted_echo);
peah1d680892017-05-23 04:07:10 -0700272
273 // Compute the minimum gain as the attenuating gain to put the signal just
274 // above the zero sample values.
275 std::array<float, kFftLengthBy2Plus1> min_gain;
peah8cee56f2017-08-24 22:36:53 -0700276 const float min_echo_power =
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200277 low_noise_render ? config_.echo_audibility.low_render_limit
278 : config_.echo_audibility.normal_render_limit;
Per Åhgren31122d62018-04-10 16:33:55 +0200279 if (!saturated_echo) {
peah1d680892017-05-23 04:07:10 -0700280 for (size_t k = 0; k < nearend.size(); ++k) {
Per Åhgrenb02644f2018-04-17 11:52:17 +0200281 const float denom = std::min(nearend[k], weighted_echo[k]);
peah1d680892017-05-23 04:07:10 -0700282 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
283 min_gain[k] = std::min(min_gain[k], 1.f);
284 }
285 } else {
286 min_gain.fill(0.f);
287 }
288
289 // Compute the maximum gain by limiting the gain increase from the previous
290 // gain.
291 std::array<float, kFftLengthBy2Plus1> max_gain;
292 for (size_t k = 0; k < gain->size(); ++k) {
Per Åhgren63b494d2017-12-06 11:32:38 +0100293 max_gain[k] = std::min(std::max(last_gain_[k] * gain_increase_[k],
294 config_.gain_updates.floor_first_increase),
295 1.f);
peah1d680892017-05-23 04:07:10 -0700296 }
297
298 // Iteratively compute the gain required to attenuate the echo to a non
299 // noticeable level.
300 gain->fill(0.f);
301 for (int k = 0; k < 2; ++k) {
302 std::array<float, kFftLengthBy2Plus1> masker;
peah8cee56f2017-08-24 22:36:53 -0700303 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain, &masker);
Per Åhgren63b494d2017-12-06 11:32:38 +0100304 GainToNoAudibleEcho(config_, low_noise_render, saturated_echo,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200305 linear_echo_estimate, nearend, weighted_echo, masker,
306 min_gain, max_gain, one_by_weighted_echo, gain);
peah1d680892017-05-23 04:07:10 -0700307 AdjustForExternalFilters(gain);
308 }
309
Per Åhgren85a11a32017-10-02 14:42:06 +0200310 // Adjust the gain for frequencies which have not yet converged.
311 AdjustNonConvergedFrequencies(gain);
312
peah1d680892017-05-23 04:07:10 -0700313 // Update the allowed maximum gain increase.
Per Åhgren31122d62018-04-10 16:33:55 +0200314 UpdateGainIncrease(low_noise_render, linear_echo_estimate, saturated_echo,
Per Åhgrenb02644f2018-04-17 11:52:17 +0200315 weighted_echo, *gain);
Per Åhgren1f33a372017-10-11 02:36:53 +0200316
peah1d680892017-05-23 04:07:10 -0700317 // Store data required for the gain computation of the next block.
Per Åhgrenb02644f2018-04-17 11:52:17 +0200318 std::copy(weighted_echo.begin(), weighted_echo.end(), last_echo_.begin());
peah1d680892017-05-23 04:07:10 -0700319 std::copy(gain->begin(), gain->end(), last_gain_.begin());
peah8cee56f2017-08-24 22:36:53 -0700320 MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain,
321 &last_masker_);
peah1d680892017-05-23 04:07:10 -0700322 aec3::VectorMath(optimization_).Sqrt(*gain);
peah86afe9d2017-04-06 15:45:32 -0700323}
324
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200325SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200326 Aec3Optimization optimization,
327 int sample_rate_hz)
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100328 : optimization_(optimization),
329 config_(config),
330 state_change_duration_blocks_(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200331 static_cast<int>(config_.filter.config_change_duration_blocks)),
332 coherence_gain_(sample_rate_hz,
333 config_.suppressor.bands_with_reliable_coherence) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100334 RTC_DCHECK_LT(0, state_change_duration_blocks_);
335 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700336 last_gain_.fill(1.f);
337 last_masker_.fill(0.f);
338 gain_increase_.fill(1.f);
339 last_echo_.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800340}
341
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200342SuppressionGain::~SuppressionGain() = default;
343
peah522d71b2017-02-23 05:16:26 -0800344void SuppressionGain::GetGain(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200345 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
346 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
347 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
348 const FftData& linear_aec_fft,
349 const FftData& render_fft,
350 const FftData& capture_fft,
peah14c11a42017-07-11 06:13:43 -0700351 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200352 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700353 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700354 float* high_bands_gain,
355 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
356 RTC_DCHECK(high_bands_gain);
357 RTC_DCHECK(low_band_gain);
358
peah1d680892017-05-23 04:07:10 -0700359 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100360 bool low_noise_render = low_render_detector_.Detect(render);
peah14c11a42017-07-11 06:13:43 -0700361 const rtc::Optional<int> narrow_peak_band =
362 render_signal_analyzer.NarrowPeakBand();
Gustaf Ullberg5bb98972018-04-25 12:54:59 +0200363 LowerBandGain(low_noise_render, aec_state, nearend_spectrum, echo_spectrum,
364 comfort_noise_spectrum, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700365
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200366 // Adjust the gain for bands where the coherence indicates not echo.
367 if (config_.suppressor.bands_with_reliable_coherence > 0) {
368 std::array<float, kFftLengthBy2Plus1> G_coherence;
369 coherence_gain_.ComputeGain(linear_aec_fft, render_fft, capture_fft,
370 G_coherence);
371 for (size_t k = 0; k < config_.suppressor.bands_with_reliable_coherence;
372 ++k) {
373 (*low_band_gain)[k] = std::max((*low_band_gain)[k], G_coherence[k]);
374 }
375 }
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200376
377 // Limit the gain of the lower bands during start up and after resets.
378 const float gain_upper_bound = aec_state.SuppressionGainLimit();
379 if (gain_upper_bound < 1.f) {
380 for (size_t k = 0; k < low_band_gain->size(); ++k) {
381 (*low_band_gain)[k] = std::min((*low_band_gain)[k], gain_upper_bound);
382 }
383 }
384
385 // Compute the gain for the upper bands.
386 *high_bands_gain = UpperBandsGain(narrow_peak_band, aec_state.SaturatedEcho(),
387 render, *low_band_gain);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100388}
389
390void SuppressionGain::SetInitialState(bool state) {
391 initial_state_ = state;
392 if (state) {
393 initial_state_change_counter_ = state_change_duration_blocks_;
394 } else {
395 initial_state_change_counter_ = 0;
396 }
397}
398
399void SuppressionGain::UpdateGainIncrease(
400 bool low_noise_render,
401 bool linear_echo_estimate,
Per Åhgren31122d62018-04-10 16:33:55 +0200402 bool saturated_echo,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100403 const std::array<float, kFftLengthBy2Plus1>& echo,
404 const std::array<float, kFftLengthBy2Plus1>& new_gain) {
405 float max_inc;
406 float max_dec;
407 float rate_inc;
408 float rate_dec;
409 float min_inc;
410 float min_dec;
411
412 RTC_DCHECK_GE(state_change_duration_blocks_, initial_state_change_counter_);
413 if (initial_state_change_counter_ > 0) {
414 if (--initial_state_change_counter_ == 0) {
415 initial_state_ = false;
416 }
417 }
418 RTC_DCHECK_LE(0, initial_state_change_counter_);
419
420 // EchoCanceller3Config::GainUpdates
421 auto& p = config_.gain_updates;
422 if (!linear_echo_estimate) {
423 max_inc = p.nonlinear.max_inc;
424 max_dec = p.nonlinear.max_dec;
425 rate_inc = p.nonlinear.rate_inc;
426 rate_dec = p.nonlinear.rate_dec;
427 min_inc = p.nonlinear.min_inc;
428 min_dec = p.nonlinear.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200429 } else if (initial_state_ && !saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100430 if (initial_state_change_counter_ > 0) {
431 float change_factor =
432 initial_state_change_counter_ * one_by_state_change_duration_blocks_;
433
434 auto average = [](float from, float to, float from_weight) {
435 return from * from_weight + to * (1.f - from_weight);
436 };
437
438 max_inc = average(p.initial.max_inc, p.normal.max_inc, change_factor);
439 max_dec = average(p.initial.max_dec, p.normal.max_dec, change_factor);
440 rate_inc = average(p.initial.rate_inc, p.normal.rate_inc, change_factor);
441 rate_dec = average(p.initial.rate_dec, p.normal.rate_dec, change_factor);
442 min_inc = average(p.initial.min_inc, p.normal.min_inc, change_factor);
443 min_dec = average(p.initial.min_dec, p.normal.min_dec, change_factor);
444 } else {
445 max_inc = p.initial.max_inc;
446 max_dec = p.initial.max_dec;
447 rate_inc = p.initial.rate_inc;
448 rate_dec = p.initial.rate_dec;
449 min_inc = p.initial.min_inc;
450 min_dec = p.initial.min_dec;
451 }
452 } else if (low_noise_render) {
453 max_inc = p.low_noise.max_inc;
454 max_dec = p.low_noise.max_dec;
455 rate_inc = p.low_noise.rate_inc;
456 rate_dec = p.low_noise.rate_dec;
457 min_inc = p.low_noise.min_inc;
458 min_dec = p.low_noise.min_dec;
Per Åhgren31122d62018-04-10 16:33:55 +0200459 } else if (!saturated_echo) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100460 max_inc = p.normal.max_inc;
461 max_dec = p.normal.max_dec;
462 rate_inc = p.normal.rate_inc;
463 rate_dec = p.normal.rate_dec;
464 min_inc = p.normal.min_inc;
465 min_dec = p.normal.min_dec;
466 } else {
467 max_inc = p.saturation.max_inc;
468 max_dec = p.saturation.max_dec;
469 rate_inc = p.saturation.rate_inc;
470 rate_dec = p.saturation.rate_dec;
471 min_inc = p.saturation.min_inc;
472 min_dec = p.saturation.min_dec;
473 }
474
475 for (size_t k = 0; k < new_gain.size(); ++k) {
476 auto increase_update = [](float new_gain, float last_gain,
477 float current_inc, float max_inc, float min_inc,
478 float change_rate) {
479 return new_gain > last_gain ? std::min(max_inc, current_inc * change_rate)
480 : min_inc;
481 };
482
483 if (echo[k] > last_echo_[k]) {
484 gain_increase_[k] =
485 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
486 max_inc, min_inc, rate_inc);
487 } else {
488 gain_increase_[k] =
489 increase_update(new_gain[k], last_gain_[k], gain_increase_[k],
490 max_dec, min_dec, rate_dec);
491 }
492 }
peah1d680892017-05-23 04:07:10 -0700493}
peah86afe9d2017-04-06 15:45:32 -0700494
peah1d680892017-05-23 04:07:10 -0700495// Detects when the render signal can be considered to have low power and
496// consist of stationary noise.
497bool SuppressionGain::LowNoiseRenderDetector::Detect(
498 const std::vector<std::vector<float>>& render) {
499 float x2_sum = 0.f;
500 float x2_max = 0.f;
501 for (auto x_k : render[0]) {
502 const float x2 = x_k * x_k;
503 x2_sum += x2;
504 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800505 }
peah1d680892017-05-23 04:07:10 -0700506
507 constexpr float kThreshold = 50.f * 50.f * 64.f;
508 const bool low_noise_render =
509 average_power_ < kThreshold && x2_max < 3 * average_power_;
510 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
511 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800512}
513
514} // namespace webrtc