blob: 935980f6f1ebab0921a8da1022a6342b54bb75bf [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>
peah522d71b2017-02-23 05:16:26 -080015#include <algorithm>
peah86afe9d2017-04-06 15:45:32 -070016#include <numeric>
peah522d71b2017-02-23 05:16:26 -080017
Gustaf Ullberg8406c432018-06-19 12:31:33 +020018#include "modules/audio_processing/aec3/moving_average.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/aec3/vector_math.h"
Gustaf Ullberg216af842018-04-26 12:39:11 +020020#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
peahcf02cf12017-04-05 14:18:07 -070023
peah522d71b2017-02-23 05:16:26 -080024namespace webrtc {
25namespace {
26
peah1d680892017-05-23 04:07:10 -070027// Adjust the gains according to the presence of known external filters.
28void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) {
peaha2376e72017-02-27 01:15:24 -080029 // Limit the low frequency gains to avoid the impact of the high-pass filter
30 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070031 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080032
33 // Limit the high frequency gains to avoid the impact of the anti-aliasing
34 // filter on the upper-frequency gains influencing the overall achieved
35 // gain. TODO(peah): Update this when new anti-aliasing filters are
36 // implemented.
peah86afe9d2017-04-06 15:45:32 -070037 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070038 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
39 std::for_each(
40 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
41 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
42 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
peaha2376e72017-02-27 01:15:24 -080043}
44
Per Åhgrenb02644f2018-04-17 11:52:17 +020045// Scales the echo according to assessed audibility at the other end.
46void WeightEchoForAudibility(const EchoCanceller3Config& config,
47 rtc::ArrayView<const float> echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020048 rtc::ArrayView<float> weighted_echo) {
Per Åhgrenb02644f2018-04-17 11:52:17 +020049 RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
50 RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
Per Åhgrenb02644f2018-04-17 11:52:17 +020051
52 auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
53 rtc::ArrayView<const float> echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020054 rtc::ArrayView<float> weighted_echo) {
Per Åhgrenb02644f2018-04-17 11:52:17 +020055 for (size_t k = begin; k < end; ++k) {
56 if (echo[k] < threshold) {
57 float tmp = (threshold - echo[k]) * normalizer;
58 weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp);
59 } else {
60 weighted_echo[k] = echo[k];
61 }
Per Åhgrenb02644f2018-04-17 11:52:17 +020062 }
63 };
64
65 float threshold = config.echo_audibility.floor_power *
66 config.echo_audibility.audibility_threshold_lf;
67 float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020068 weigh(threshold, normalizer, 0, 3, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020069
70 threshold = config.echo_audibility.floor_power *
71 config.echo_audibility.audibility_threshold_mf;
72 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020073 weigh(threshold, normalizer, 3, 7, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020074
75 threshold = config.echo_audibility.floor_power *
76 config.echo_audibility.audibility_threshold_hf;
77 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020078 weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020079}
80
Per Åhgren85a11a32017-10-02 14:42:06 +020081// TODO(peah): Make adaptive to take the actual filter error into account.
82constexpr size_t kUpperAccurateBandPlus1 = 29;
83
Per Åhgren85a11a32017-10-02 14:42:06 +020084// Limits the gain in the frequencies for which the adaptive filter has not
85// converged. Currently, these frequencies are not hardcoded to the frequencies
86// which are typically not excited by speech.
87// TODO(peah): Make adaptive to take the actual filter error into account.
88void AdjustNonConvergedFrequencies(
89 std::array<float, kFftLengthBy2Plus1>* gain) {
90 constexpr float oneByBandsInSum =
91 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
92 const float hf_gain_bound =
93 std::accumulate(gain->begin() + 20,
94 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
95 oneByBandsInSum;
96
97 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
98 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
99}
100
peah1d680892017-05-23 04:07:10 -0700101} // namespace
102
Gustaf Ullberg216af842018-04-26 12:39:11 +0200103int SuppressionGain::instance_count_ = 0;
104
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200105float SuppressionGain::UpperBandsGain(
106 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
107 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
108 const absl::optional<int>& narrow_peak_band,
109 bool saturated_echo,
110 const std::vector<std::vector<float>>& render,
111 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const {
112 RTC_DCHECK_LT(0, render.size());
113 if (render.size() == 1) {
114 return 1.f;
115 }
116
117 if (narrow_peak_band &&
118 (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) {
119 return 0.001f;
120 }
121
122 constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2;
123 const float gain_below_8_khz = *std::min_element(
124 low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end());
125
126 // Always attenuate the upper bands when there is saturated echo.
127 if (saturated_echo) {
128 return std::min(0.001f, gain_below_8_khz);
129 }
130
131 // Compute the upper and lower band energies.
132 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
133 const float low_band_energy =
134 std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares);
135 float high_band_energy = 0.f;
136 for (size_t k = 1; k < render.size(); ++k) {
137 const float energy = std::accumulate(render[k].begin(), render[k].end(),
138 0.f, sum_of_squares);
139 high_band_energy = std::max(high_band_energy, energy);
140 }
141
142 // If there is more power in the lower frequencies than the upper frequencies,
143 // or if the power in upper frequencies is low, do not bound the gain in the
144 // upper bands.
145 float anti_howling_gain;
146 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
147 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
148 anti_howling_gain = 1.f;
149 } else {
150 // In all other cases, bound the gain for upper frequencies.
151 RTC_DCHECK_LE(low_band_energy, high_band_energy);
152 RTC_DCHECK_NE(0.f, high_band_energy);
153 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
154 }
155
156 // Bound the upper gain during significant echo activity.
157 auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) {
158 RTC_DCHECK_LE(16, spectrum.size());
159 return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f);
160 };
161 const float echo_sum = low_frequency_energy(echo_spectrum);
162 const float noise_sum = low_frequency_energy(comfort_noise_spectrum);
163 const auto& cfg = config_.suppressor.high_bands_suppression;
164 float gain_bound = 1.f;
165 if (echo_sum > cfg.enr_threshold * noise_sum &&
166 !dominant_nearend_detector_.IsNearendState()) {
167 gain_bound = cfg.max_gain_during_echo;
168 }
169
170 // Choose the gain as the minimum of the lower and upper gains.
171 return std::min(std::min(gain_below_8_khz, anti_howling_gain), gain_bound);
172}
173
Gustaf Ullbergec642172018-07-03 13:48:32 +0200174// Computes the gain to reduce the echo to a non audible level.
175void SuppressionGain::GainToNoAudibleEcho(
176 const std::array<float, kFftLengthBy2Plus1>& nearend,
177 const std::array<float, kFftLengthBy2Plus1>& echo,
178 const std::array<float, kFftLengthBy2Plus1>& masker,
179 const std::array<float, kFftLengthBy2Plus1>& min_gain,
180 const std::array<float, kFftLengthBy2Plus1>& max_gain,
181 std::array<float, kFftLengthBy2Plus1>* gain) const {
Per Åhgren524e8782018-08-24 22:48:49 +0200182 const auto& p = dominant_nearend_detector_.IsNearendState() ? nearend_params_
183 : normal_params_;
Gustaf Ullbergec642172018-07-03 13:48:32 +0200184 for (size_t k = 0; k < gain->size(); ++k) {
185 float enr = echo[k] / (nearend[k] + 1.f); // Echo-to-nearend ratio.
186 float emr = echo[k] / (masker[k] + 1.f); // Echo-to-masker (noise) ratio.
187 float g = 1.0f;
Per Åhgren524e8782018-08-24 22:48:49 +0200188 if (enr > p.enr_transparent_[k] && emr > p.emr_transparent_[k]) {
189 g = (p.enr_suppress_[k] - enr) /
190 (p.enr_suppress_[k] - p.enr_transparent_[k]);
191 g = std::max(g, p.emr_transparent_[k] / emr);
Gustaf Ullbergec642172018-07-03 13:48:32 +0200192 }
193 (*gain)[k] = std::max(std::min(g, max_gain[k]), min_gain[k]);
194 }
195}
196
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200197// Compute the minimum gain as the attenuating gain to put the signal just
198// above the zero sample values.
199void SuppressionGain::GetMinGain(
200 rtc::ArrayView<const float> suppressor_input,
201 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
210 for (size_t k = 0; k < suppressor_input.size(); ++k) {
211 const float denom =
212 std::min(suppressor_input[k], weighted_residual_echo[k]);
peah1d680892017-05-23 04:07:10 -0700213 min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
214 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;
262 GetMinGain(suppressor_input, weighted_residual_echo, low_noise_render,
263 saturated_echo, min_gain);
264
265 std::array<float, kFftLengthBy2Plus1> max_gain;
266 GetMaxGain(max_gain);
peah1d680892017-05-23 04:07:10 -0700267
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200268 GainToNoAudibleEcho(nearend, weighted_residual_echo, comfort_noise,
269 min_gain, max_gain, gain);
peah1d680892017-05-23 04:07:10 -0700270 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(
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200314 const std::array<float, kFftLengthBy2Plus1>& suppressor_input_spectrum,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200315 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
316 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200317 const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200318 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
peah14c11a42017-07-11 06:13:43 -0700319 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200320 const AecState& aec_state,
peah86afe9d2017-04-06 15:45:32 -0700321 const std::vector<std::vector<float>>& render,
peah86afe9d2017-04-06 15:45:32 -0700322 float* high_bands_gain,
323 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
324 RTC_DCHECK(high_bands_gain);
325 RTC_DCHECK(low_band_gain);
Per Åhgren7343f562018-08-17 10:08:34 +0200326 const auto& cfg = config_.suppressor;
327
328 if (cfg.enforce_transparent) {
329 low_band_gain->fill(1.f);
330 *high_bands_gain = cfg.enforce_empty_higher_bands ? 0.f : 1.f;
331 return;
332 }
peah86afe9d2017-04-06 15:45:32 -0700333
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200334 std::array<float, kFftLengthBy2Plus1> nearend_average;
335 moving_average_.Average(nearend_spectrum, nearend_average);
336
Per Åhgren524e8782018-08-24 22:48:49 +0200337 // Update the state selection.
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200338 dominant_nearend_detector_.Update(nearend_spectrum, residual_echo_spectrum,
Per Åhgren700b4a42018-10-23 21:21:37 +0200339 comfort_noise_spectrum, initial_state_);
Per Åhgren524e8782018-08-24 22:48:49 +0200340
peah1d680892017-05-23 04:07:10 -0700341 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100342 bool low_noise_render = low_render_detector_.Detect(render);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200343 LowerBandGain(low_noise_render, aec_state, suppressor_input_spectrum,
344 nearend_average, residual_echo_spectrum, comfort_noise_spectrum,
345 low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700346
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200347 // Compute the gain for the upper bands.
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200348 const absl::optional<int> narrow_peak_band =
349 render_signal_analyzer.NarrowPeakBand();
350
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200351 *high_bands_gain =
352 UpperBandsGain(echo_spectrum, comfort_noise_spectrum, narrow_peak_band,
353 aec_state.SaturatedEcho(), render, *low_band_gain);
Per Åhgren7343f562018-08-17 10:08:34 +0200354 if (cfg.enforce_empty_higher_bands) {
355 *high_bands_gain = 0.f;
356 }
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100357}
358
359void SuppressionGain::SetInitialState(bool state) {
360 initial_state_ = state;
361 if (state) {
362 initial_state_change_counter_ = state_change_duration_blocks_;
363 } else {
364 initial_state_change_counter_ = 0;
365 }
366}
367
peah1d680892017-05-23 04:07:10 -0700368// Detects when the render signal can be considered to have low power and
369// consist of stationary noise.
370bool SuppressionGain::LowNoiseRenderDetector::Detect(
371 const std::vector<std::vector<float>>& render) {
372 float x2_sum = 0.f;
373 float x2_max = 0.f;
374 for (auto x_k : render[0]) {
375 const float x2 = x_k * x_k;
376 x2_sum += x2;
377 x2_max = std::max(x2_max, x2);
peah522d71b2017-02-23 05:16:26 -0800378 }
peah1d680892017-05-23 04:07:10 -0700379
380 constexpr float kThreshold = 50.f * 50.f * 64.f;
381 const bool low_noise_render =
382 average_power_ < kThreshold && x2_max < 3 * average_power_;
383 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
384 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800385}
386
Per Åhgren524e8782018-08-24 22:48:49 +0200387SuppressionGain::DominantNearendDetector::DominantNearendDetector(
388 const EchoCanceller3Config::Suppressor::DominantNearendDetection config)
389 : enr_threshold_(config.enr_threshold),
Gustaf Ullbergc9f9b872018-10-22 15:15:36 +0200390 enr_exit_threshold_(config.enr_exit_threshold),
Per Åhgren524e8782018-08-24 22:48:49 +0200391 snr_threshold_(config.snr_threshold),
392 hold_duration_(config.hold_duration),
Per Åhgren700b4a42018-10-23 21:21:37 +0200393 trigger_threshold_(config.trigger_threshold),
394 use_during_initial_phase_(config.use_during_initial_phase) {}
Per Åhgren524e8782018-08-24 22:48:49 +0200395
396void SuppressionGain::DominantNearendDetector::Update(
397 rtc::ArrayView<const float> nearend_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200398 rtc::ArrayView<const float> residual_echo_spectrum,
Per Åhgren700b4a42018-10-23 21:21:37 +0200399 rtc::ArrayView<const float> comfort_noise_spectrum,
400 bool initial_state) {
Per Åhgren524e8782018-08-24 22:48:49 +0200401 auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) {
402 RTC_DCHECK_LE(16, spectrum.size());
403 return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f);
404 };
405 const float ne_sum = low_frequency_energy(nearend_spectrum);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200406 const float echo_sum = low_frequency_energy(residual_echo_spectrum);
Per Åhgren524e8782018-08-24 22:48:49 +0200407 const float noise_sum = low_frequency_energy(comfort_noise_spectrum);
408
409 // Detect strong active nearend if the nearend is sufficiently stronger than
410 // the echo and the nearend noise.
Per Åhgren700b4a42018-10-23 21:21:37 +0200411 if ((!initial_state || use_during_initial_phase_) &&
Gustaf Ullbergde10eea2018-11-28 09:44:50 +0100412 echo_sum < enr_threshold_ * ne_sum &&
Per Åhgren524e8782018-08-24 22:48:49 +0200413 ne_sum > snr_threshold_ * noise_sum) {
414 if (++trigger_counter_ >= trigger_threshold_) {
415 // After a period of strong active nearend activity, flag nearend mode.
416 hold_counter_ = hold_duration_;
417 trigger_counter_ = trigger_threshold_;
418 }
419 } else {
420 // Forget previously detected strong active nearend activity.
421 trigger_counter_ = std::max(0, trigger_counter_ - 1);
422 }
423
Gustaf Ullbergc9f9b872018-10-22 15:15:36 +0200424 // Exit nearend-state early at strong echo.
Gustaf Ullbergde10eea2018-11-28 09:44:50 +0100425 if (echo_sum > enr_exit_threshold_ * ne_sum &&
Gustaf Ullbergc9f9b872018-10-22 15:15:36 +0200426 echo_sum > snr_threshold_ * noise_sum) {
427 hold_counter_ = 0;
428 }
429
Per Åhgren524e8782018-08-24 22:48:49 +0200430 // Remain in any nearend mode for a certain duration.
431 hold_counter_ = std::max(0, hold_counter_ - 1);
432 nearend_state_ = hold_counter_ > 0;
433}
434
435SuppressionGain::GainParameters::GainParameters(
436 const EchoCanceller3Config::Suppressor::Tuning& tuning)
437 : max_inc_factor(tuning.max_inc_factor),
438 max_dec_factor_lf(tuning.max_dec_factor_lf) {
439 // Compute per-band masking thresholds.
440 constexpr size_t kLastLfBand = 5;
441 constexpr size_t kFirstHfBand = 8;
442 RTC_DCHECK_LT(kLastLfBand, kFirstHfBand);
443 auto& lf = tuning.mask_lf;
444 auto& hf = tuning.mask_hf;
445 RTC_DCHECK_LT(lf.enr_transparent, lf.enr_suppress);
446 RTC_DCHECK_LT(hf.enr_transparent, hf.enr_suppress);
447 for (size_t k = 0; k < kFftLengthBy2Plus1; k++) {
448 float a;
449 if (k <= kLastLfBand) {
450 a = 0.f;
451 } else if (k < kFirstHfBand) {
452 a = (k - kLastLfBand) / static_cast<float>(kFirstHfBand - kLastLfBand);
453 } else {
454 a = 1.f;
455 }
456 enr_transparent_[k] = (1 - a) * lf.enr_transparent + a * hf.enr_transparent;
457 enr_suppress_[k] = (1 - a) * lf.enr_suppress + a * hf.enr_suppress;
458 emr_transparent_[k] = (1 - a) * lf.emr_transparent + a * hf.emr_transparent;
459 }
460}
461
peah522d71b2017-02-23 05:16:26 -0800462} // namespace webrtc