blob: 4831b7163f7e97419ce485ab768ec2b6b0d3e820 [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
peah522d71b2017-02-23 05:16:26 -080013#include <math.h>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
peah522d71b2017-02-23 05:16:26 -080016#include <algorithm>
peah86afe9d2017-04-06 15:45:32 -070017#include <numeric>
peah522d71b2017-02-23 05:16:26 -080018
Gustaf Ullberg8406c432018-06-19 12:31:33 +020019#include "modules/audio_processing/aec3/moving_average.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_processing/aec3/vector_math.h"
Gustaf Ullberg216af842018-04-26 12:39:11 +020021#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
peahcf02cf12017-04-05 14:18:07 -070024
peah522d71b2017-02-23 05:16:26 -080025namespace webrtc {
26namespace {
27
peah1d680892017-05-23 04:07:10 -070028// Adjust the gains according to the presence of known external filters.
29void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080030 // Limit the low frequency gains to avoid the impact of the high-pass filter
31 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070032 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080033
34 // Limit the high frequency gains to avoid the impact of the anti-aliasing
35 // filter on the upper-frequency gains influencing the overall achieved
36 // gain. TODO(peah): Update this when new anti-aliasing filters are
37 // implemented.
peah86afe9d2017-04-06 15:45:32 -070038 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070039 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
40 std::for_each(
41 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
42 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
43 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080044}
45
Per Åhgrenb02644f2018-04-17 11:52:17 +020046// Scales the echo according to assessed audibility at the other end.
47void WeightEchoForAudibility(const EchoCanceller3Config& config,
48 rtc::ArrayView<const float> echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020049 rtc::ArrayView<float> weighted_echo) {
Per Åhgrenb02644f2018-04-17 11:52:17 +020050 RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
51 RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
Per Åhgrenb02644f2018-04-17 11:52:17 +020052
53 auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
54 rtc::ArrayView<const float> echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020055 rtc::ArrayView<float> weighted_echo) {
Per Åhgrenb02644f2018-04-17 11:52:17 +020056 for (size_t k = begin; k < end; ++k) {
57 if (echo[k] < threshold) {
58 float tmp = (threshold - echo[k]) * normalizer;
59 weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp);
60 } else {
61 weighted_echo[k] = echo[k];
62 }
Per Åhgrenb02644f2018-04-17 11:52:17 +020063 }
64 };
65
66 float threshold = config.echo_audibility.floor_power *
67 config.echo_audibility.audibility_threshold_lf;
68 float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020069 weigh(threshold, normalizer, 0, 3, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020070
71 threshold = config.echo_audibility.floor_power *
72 config.echo_audibility.audibility_threshold_mf;
73 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020074 weigh(threshold, normalizer, 3, 7, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020075
76 threshold = config.echo_audibility.floor_power *
77 config.echo_audibility.audibility_threshold_hf;
78 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020079 weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020080}
81
Per Åhgren85a11a32017-10-02 14:42:06 +020082// TODO(peah): Make adaptive to take the actual filter error into account.
83constexpr size_t kUpperAccurateBandPlus1 = 29;
84
Per Åhgren85a11a32017-10-02 14:42:06 +020085// Limits the gain in the frequencies for which the adaptive filter has not
86// converged. Currently, these frequencies are not hardcoded to the frequencies
87// which are typically not excited by speech.
88// TODO(peah): Make adaptive to take the actual filter error into account.
89void AdjustNonConvergedFrequencies(
90 std::array<float, kFftLengthBy2Plus1>* gain) {
91 constexpr float oneByBandsInSum =
92 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
93 const float hf_gain_bound =
94 std::accumulate(gain->begin() + 20,
95 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
96 oneByBandsInSum;
97
98 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
99 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
100}
101
peah1d680892017-05-23 04:07:10 -0700102} // namespace
103
Gustaf Ullberg216af842018-04-26 12:39:11 +0200104int SuppressionGain::instance_count_ = 0;
105
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200106float SuppressionGain::UpperBandsGain(
107 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
108 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
109 const absl::optional<int>& narrow_peak_band,
110 bool saturated_echo,
Per Åhgrend112c752019-09-02 13:56:56 +0000111 const std::vector<std::vector<float>>& render,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200112 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const {
113 RTC_DCHECK_LT(0, render.size());
114 if (render.size() == 1) {
115 return 1.f;
116 }
117
118 if (narrow_peak_band &&
119 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
120 return 0.001f;
121 }
122
123 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
124 const float gain_below_8_khz = *std::min_element(
125 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
126
127 // Always attenuate the upper bands when there is saturated echo.
128 if (saturated_echo) {
129 return std::min(0.001f, gain_below_8_khz);
130 }
131
132 // Compute the upper and lower band energies.
133 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
Per Åhgrend112c752019-09-02 13:56:56 +0000134 const float low_band_energy =
135 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200136 float high_band_energy = 0.f;
137 for (size_t k = 1; k < render.size(); ++k) {
Per Åhgrend112c752019-09-02 13:56:56 +0000138 const float energy = std::accumulate(render[k].begin(), render[k].end(),
139 0.f, sum_of_squares);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200140 high_band_energy = std::max(high_band_energy, energy);
141 }
142
143 // If there is more power in the lower frequencies than the upper frequencies,
144 // or if the power in upper frequencies is low, do not bound the gain in the
145 // upper bands.
146 float anti_howling_gain;
147 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
148 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
149 anti_howling_gain = 1.f;
150 } else {
151 // In all other cases, bound the gain for upper frequencies.
152 RTC_DCHECK_LE(low_band_energy, high_band_energy);
153 RTC_DCHECK_NE(0.f, high_band_energy);
154 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
155 }
156
157 // Bound the upper gain during significant echo activity.
158 auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) {
159 RTC_DCHECK_LE(16, spectrum.size());
160 return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f);
161 };
162 const float echo_sum = low_frequency_energy(echo_spectrum);
163 const float noise_sum = low_frequency_energy(comfort_noise_spectrum);
164 const auto& cfg = config_.suppressor.high_bands_suppression;
165 float gain_bound = 1.f;
166 if (echo_sum > cfg.enr_threshold * noise_sum &&
167 !dominant_nearend_detector_.IsNearendState()) {
168 gain_bound = cfg.max_gain_during_echo;
169 }
170
171 // Choose the gain as the minimum of the lower and upper gains.
172 return std::min(std::min(gain_below_8_khz, anti_howling_gain), gain_bound);
173}
174
Gustaf Ullbergec642172018-07-03 13:48:32 +0200175// Computes the gain to reduce the echo to a non audible level.
176void SuppressionGain::GainToNoAudibleEcho(
177 const std::array<float, kFftLengthBy2Plus1>& nearend,
178 const std::array<float, kFftLengthBy2Plus1>& echo,
179 const std::array<float, kFftLengthBy2Plus1>& masker,
180 const std::array<float, kFftLengthBy2Plus1>& min_gain,
181 const std::array<float, kFftLengthBy2Plus1>& max_gain,
182 std::array<float, kFftLengthBy2Plus1>* gain) const {
Per Åhgren524e8782018-08-24 22:48:49 +0200183 const auto& p = dominant_nearend_detector_.IsNearendState() ? nearend_params_
184 : normal_params_;
Gustaf Ullbergec642172018-07-03 13:48:32 +0200185 for (size_t k = 0; k < gain->size(); ++k) {
186 float enr = echo[k] / (nearend[k] + 1.f); // Echo-to-nearend ratio.
187 float emr = echo[k] / (masker[k] + 1.f); // Echo-to-masker (noise) ratio.
188 float g = 1.0f;
Per Åhgren524e8782018-08-24 22:48:49 +0200189 if (enr > p.enr_transparent_[k] && emr > p.emr_transparent_[k]) {
190 g = (p.enr_suppress_[k] - enr) /
191 (p.enr_suppress_[k] - p.enr_transparent_[k]);
192 g = std::max(g, p.emr_transparent_[k] / emr);
Gustaf Ullbergec642172018-07-03 13:48:32 +0200193 }
194 (*gain)[k] = std::max(std::min(g, max_gain[k]), min_gain[k]);
195 }
196}
197
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200198// Compute the minimum gain as the attenuating gain to put the signal just
199// above the zero sample values.
200void SuppressionGain::GetMinGain(
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200201 rtc::ArrayView<const float> weighted_residual_echo,
peah1d680892017-05-23 04:07:10 -0700202 bool low_noise_render,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200203 bool saturated_echo,
204 rtc::ArrayView<float> min_gain) const {
Per Åhgren31122d62018-04-10 16:33:55 +0200205 if (!saturated_echo) {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200206 const float min_echo_power =
207 low_noise_render ? config_.echo_audibility.low_render_limit
208 : config_.echo_audibility.normal_render_limit;
209
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200210 for (size_t k = 0; k < min_gain.size(); ++k) {
211 min_gain[k] = weighted_residual_echo[k] > 0.f
212 ? min_echo_power / weighted_residual_echo[k]
213 : 1.f;
peah1d680892017-05-23 04:07:10 -0700214 min_gain[k] = std::min(min_gain[k], 1.f);
215 }
Gustaf Ullbergecb2d562018-08-23 15:11:38 +0200216 for (size_t k = 0; k < 6; ++k) {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200217 const auto& dec = dominant_nearend_detector_.IsNearendState()
218 ? nearend_params_.max_dec_factor_lf
219 : normal_params_.max_dec_factor_lf;
220
Gustaf Ullbergecb2d562018-08-23 15:11:38 +0200221 // Make sure the gains of the low frequencies do not decrease too
222 // quickly after strong nearend.
223 if (last_nearend_[k] > last_echo_[k]) {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200224 min_gain[k] = std::max(min_gain[k], last_gain_[k] * dec);
Gustaf Ullbergecb2d562018-08-23 15:11:38 +0200225 min_gain[k] = std::min(min_gain[k], 1.f);
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200226 }
227 }
peah1d680892017-05-23 04:07:10 -0700228 } else {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200229 std::fill(min_gain.begin(), min_gain.end(), 0.f);
peah1d680892017-05-23 04:07:10 -0700230 }
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200231}
peah1d680892017-05-23 04:07:10 -0700232
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200233// Compute the maximum gain by limiting the gain increase from the previous
234// gain.
235void SuppressionGain::GetMaxGain(rtc::ArrayView<float> max_gain) const {
236 const auto& inc = dominant_nearend_detector_.IsNearendState()
237 ? nearend_params_.max_inc_factor
238 : normal_params_.max_inc_factor;
239 const auto& floor = config_.suppressor.floor_first_increase;
240 for (size_t k = 0; k < max_gain.size(); ++k) {
241 max_gain[k] = std::min(std::max(last_gain_[k] * inc, floor), 1.f);
peah1d680892017-05-23 04:07:10 -0700242 }
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200243}
244
245// TODO(peah): Add further optimizations, in particular for the divisions.
246void SuppressionGain::LowerBandGain(
247 bool low_noise_render,
248 const AecState& aec_state,
249 const std::array<float, kFftLengthBy2Plus1>& suppressor_input,
250 const std::array<float, kFftLengthBy2Plus1>& nearend,
251 const std::array<float, kFftLengthBy2Plus1>& residual_echo,
252 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
253 std::array<float, kFftLengthBy2Plus1>* gain) {
254 const bool saturated_echo = aec_state.SaturatedEcho();
255
256 // Weight echo power in terms of audibility. // Precompute 1/weighted echo
257 // (note that when the echo is zero, the precomputed value is never used).
258 std::array<float, kFftLengthBy2Plus1> weighted_residual_echo;
259 WeightEchoForAudibility(config_, residual_echo, weighted_residual_echo);
260
261 std::array<float, kFftLengthBy2Plus1> min_gain;
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200262 GetMinGain(weighted_residual_echo, low_noise_render, saturated_echo,
263 min_gain);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200264
265 std::array<float, kFftLengthBy2Plus1> max_gain;
266 GetMaxGain(max_gain);
peah1d680892017-05-23 04:07:10 -0700267
Jonas Olssona4d87372019-07-05 19:08:33 +0200268 GainToNoAudibleEcho(nearend, weighted_residual_echo, comfort_noise, min_gain,
269 max_gain, gain);
270 AdjustForExternalFilters(gain);
peah1d680892017-05-23 04:07:10 -0700271
Per Åhgren85a11a32017-10-02 14:42:06 +0200272 // Adjust the gain for frequencies which have not yet converged.
273 AdjustNonConvergedFrequencies(gain);
274
peah1d680892017-05-23 04:07:10 -0700275 // Store data required for the gain computation of the next block.
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200276 std::copy(nearend.begin(), nearend.end(), last_nearend_.begin());
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200277 std::copy(weighted_residual_echo.begin(), weighted_residual_echo.end(),
278 last_echo_.begin());
peah1d680892017-05-23 04:07:10 -0700279 std::copy(gain->begin(), gain->end(), last_gain_.begin());
peah1d680892017-05-23 04:07:10 -0700280 aec3::VectorMath(optimization_).Sqrt(*gain);
Gustaf Ullberg216af842018-04-26 12:39:11 +0200281
282 // Debug outputs for the purpose of development and analysis.
283 data_dumper_->DumpRaw("aec3_suppressor_min_gain", min_gain);
284 data_dumper_->DumpRaw("aec3_suppressor_max_gain", max_gain);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200285 data_dumper_->DumpRaw("aec3_dominant_nearend",
286 dominant_nearend_detector_.IsNearendState());
peah86afe9d2017-04-06 15:45:32 -0700287}
288
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200289SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200290 Aec3Optimization optimization,
291 int sample_rate_hz)
Gustaf Ullberg216af842018-04-26 12:39:11 +0200292 : data_dumper_(
293 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
294 optimization_(optimization),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100295 config_(config),
296 state_change_duration_blocks_(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200297 static_cast<int>(config_.filter.config_change_duration_blocks)),
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200298 moving_average_(kFftLengthBy2Plus1,
Per Åhgren524e8782018-08-24 22:48:49 +0200299 config.suppressor.nearend_average_blocks),
300 nearend_params_(config_.suppressor.nearend_tuning),
301 normal_params_(config_.suppressor.normal_tuning),
302 dominant_nearend_detector_(
303 config_.suppressor.dominant_nearend_detection) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100304 RTC_DCHECK_LT(0, state_change_duration_blocks_);
305 one_by_state_change_duration_blocks_ = 1.f / state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700306 last_gain_.fill(1.f);
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200307 last_nearend_.fill(0.f);
peah1d680892017-05-23 04:07:10 -0700308 last_echo_.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800309}
310
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200311SuppressionGain::~SuppressionGain() = default;
312
peah522d71b2017-02-23 05:16:26 -0800313void SuppressionGain::GetGain(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200314 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
315 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200316 const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200317 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
peah14c11a42017-07-11 06:13:43 -0700318 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200319 const AecState& aec_state,
Per Åhgrend112c752019-09-02 13:56:56 +0000320 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700321 float* high_bands_gain,
322 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
323 RTC_DCHECK(high_bands_gain);
324 RTC_DCHECK(low_band_gain);
Per Åhgren7343f562018-08-17 10:08:34 +0200325 const auto& cfg = config_.suppressor;
326
327 if (cfg.enforce_transparent) {
328 low_band_gain->fill(1.f);
329 *high_bands_gain = cfg.enforce_empty_higher_bands ? 0.f : 1.f;
330 return;
331 }
peah86afe9d2017-04-06 15:45:32 -0700332
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200333 std::array<float, kFftLengthBy2Plus1> nearend_average;
334 moving_average_.Average(nearend_spectrum, nearend_average);
335
Per Åhgren524e8782018-08-24 22:48:49 +0200336 // Update the state selection.
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200337 dominant_nearend_detector_.Update(nearend_spectrum, residual_echo_spectrum,
Per Åhgren700b4a42018-10-23 21:21:37 +0200338 comfort_noise_spectrum, initial_state_);
Per Åhgren524e8782018-08-24 22:48:49 +0200339
peah1d680892017-05-23 04:07:10 -0700340 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100341 bool low_noise_render = low_render_detector_.Detect(render);
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200342 LowerBandGain(low_noise_render, aec_state, nearend_spectrum, nearend_average,
343 residual_echo_spectrum, comfort_noise_spectrum, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700344
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200345 // Compute the gain for the upper bands.
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200346 const absl::optional<int> narrow_peak_band =
347 render_signal_analyzer.NarrowPeakBand();
348
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200349 *high_bands_gain =
350 UpperBandsGain(echo_spectrum, comfort_noise_spectrum, narrow_peak_band,
351 aec_state.SaturatedEcho(), render, *low_band_gain);
Per Åhgren7343f562018-08-17 10:08:34 +0200352 if (cfg.enforce_empty_higher_bands) {
353 *high_bands_gain = 0.f;
354 }
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100355}
356
357void SuppressionGain::SetInitialState(bool state) {
358 initial_state_ = state;
359 if (state) {
360 initial_state_change_counter_ = state_change_duration_blocks_;
361 } else {
362 initial_state_change_counter_ = 0;
363 }
364}
365
peah1d680892017-05-23 04:07:10 -0700366// Detects when the render signal can be considered to have low power and
367// consist of stationary noise.
368bool SuppressionGain::LowNoiseRenderDetector::Detect(
Per Åhgrend112c752019-09-02 13:56:56 +0000369 const std::vector<std::vector<float>>& render) {
peah1d680892017-05-23 04:07:10 -0700370 float x2_sum = 0.f;
371 float x2_max = 0.f;
Per Åhgrend112c752019-09-02 13:56:56 +0000372 for (auto x_k : render[0]) {
peah1d680892017-05-23 04:07:10 -0700373 const float x2 = x_k * x_k;
374 x2_sum += x2;
375 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800376 }
peah1d680892017-05-23 04:07:10 -0700377
378 constexpr float kThreshold = 50.f * 50.f * 64.f;
379 const bool low_noise_render =
380 average_power_ < kThreshold && x2_max < 3 * average_power_;
381 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
382 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800383}
384
Per Åhgren524e8782018-08-24 22:48:49 +0200385SuppressionGain::DominantNearendDetector::DominantNearendDetector(
386 const EchoCanceller3Config::Suppressor::DominantNearendDetection config)
387 : enr_threshold_(config.enr_threshold),
Gustaf Ullbergc9f9b872018-10-22 15:15:36 +0200388 enr_exit_threshold_(config.enr_exit_threshold),
Per Åhgren524e8782018-08-24 22:48:49 +0200389 snr_threshold_(config.snr_threshold),
390 hold_duration_(config.hold_duration),
Per Åhgren700b4a42018-10-23 21:21:37 +0200391 trigger_threshold_(config.trigger_threshold),
392 use_during_initial_phase_(config.use_during_initial_phase) {}
Per Åhgren524e8782018-08-24 22:48:49 +0200393
394void SuppressionGain::DominantNearendDetector::Update(
395 rtc::ArrayView<const float> nearend_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200396 rtc::ArrayView<const float> residual_echo_spectrum,
Per Åhgren700b4a42018-10-23 21:21:37 +0200397 rtc::ArrayView<const float> comfort_noise_spectrum,
398 bool initial_state) {
Per Åhgren524e8782018-08-24 22:48:49 +0200399 auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) {
400 RTC_DCHECK_LE(16, spectrum.size());
401 return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f);
402 };
403 const float ne_sum = low_frequency_energy(nearend_spectrum);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200404 const float echo_sum = low_frequency_energy(residual_echo_spectrum);
Per Åhgren524e8782018-08-24 22:48:49 +0200405 const float noise_sum = low_frequency_energy(comfort_noise_spectrum);
406
407 // Detect strong active nearend if the nearend is sufficiently stronger than
408 // the echo and the nearend noise.
Per Åhgren700b4a42018-10-23 21:21:37 +0200409 if ((!initial_state || use_during_initial_phase_) &&
Gustaf Ullbergde10eea2018-11-28 09:44:50 +0100410 echo_sum < enr_threshold_ * ne_sum &&
Per Åhgren524e8782018-08-24 22:48:49 +0200411 ne_sum > snr_threshold_ * noise_sum) {
412 if (++trigger_counter_ >= trigger_threshold_) {
413 // After a period of strong active nearend activity, flag nearend mode.
414 hold_counter_ = hold_duration_;
415 trigger_counter_ = trigger_threshold_;
416 }
417 } else {
418 // Forget previously detected strong active nearend activity.
419 trigger_counter_ = std::max(0, trigger_counter_ - 1);
420 }
421
Gustaf Ullbergc9f9b872018-10-22 15:15:36 +0200422 // Exit nearend-state early at strong echo.
Gustaf Ullbergde10eea2018-11-28 09:44:50 +0100423 if (echo_sum > enr_exit_threshold_ * ne_sum &&
Gustaf Ullbergc9f9b872018-10-22 15:15:36 +0200424 echo_sum > snr_threshold_ * noise_sum) {
425 hold_counter_ = 0;
426 }
427
Per Åhgren524e8782018-08-24 22:48:49 +0200428 // Remain in any nearend mode for a certain duration.
429 hold_counter_ = std::max(0, hold_counter_ - 1);
430 nearend_state_ = hold_counter_ > 0;
431}
432
433SuppressionGain::GainParameters::GainParameters(
434 const EchoCanceller3Config::Suppressor::Tuning& tuning)
435 : max_inc_factor(tuning.max_inc_factor),
436 max_dec_factor_lf(tuning.max_dec_factor_lf) {
437 // Compute per-band masking thresholds.
438 constexpr size_t kLastLfBand = 5;
439 constexpr size_t kFirstHfBand = 8;
440 RTC_DCHECK_LT(kLastLfBand, kFirstHfBand);
441 auto& lf = tuning.mask_lf;
442 auto& hf = tuning.mask_hf;
443 RTC_DCHECK_LT(lf.enr_transparent, lf.enr_suppress);
444 RTC_DCHECK_LT(hf.enr_transparent, hf.enr_suppress);
445 for (size_t k = 0; k < kFftLengthBy2Plus1; k++) {
446 float a;
447 if (k <= kLastLfBand) {
448 a = 0.f;
449 } else if (k < kFirstHfBand) {
450 a = (k - kLastLfBand) / static_cast<float>(kFirstHfBand - kLastLfBand);
451 } else {
452 a = 1.f;
453 }
454 enr_transparent_[k] = (1 - a) * lf.enr_transparent + a * hf.enr_transparent;
455 enr_suppress_[k] = (1 - a) * lf.enr_suppress + a * hf.enr_suppress;
456 emr_transparent_[k] = (1 - a) * lf.emr_transparent + a * hf.emr_transparent;
457 }
458}
459
peah522d71b2017-02-23 05:16:26 -0800460} // namespace webrtc