blob: bd7a3d68fb3231b2eb7db51959d5fb693ef76669 [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
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010028void PostprocessGains(std::array<float, kFftLengthBy2Plus1>* gain) {
29 // TODO(gustaf): Investigate if this can be relaxed to achieve higher
30 // transparency above 2 kHz.
31
peaha2376e72017-02-27 01:15:24 -080032 // Limit the low frequency gains to avoid the impact of the high-pass filter
33 // on the lower-frequency gain influencing the overall achieved gain.
peah1d680892017-05-23 04:07:10 -070034 (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]);
peaha2376e72017-02-27 01:15:24 -080035
36 // Limit the high frequency gains to avoid the impact of the anti-aliasing
37 // filter on the upper-frequency gains influencing the overall achieved
38 // gain. TODO(peah): Update this when new anti-aliasing filters are
39 // implemented.
peah86afe9d2017-04-06 15:45:32 -070040 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peah1d680892017-05-23 04:07:10 -070041 const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit];
42 std::for_each(
43 gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1,
44 [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); });
45 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1];
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010046
47 // Limits the gain in the frequencies for which the adaptive filter has not
48 // converged.
49 // TODO(peah): Make adaptive to take the actual filter error into account.
50 constexpr size_t kUpperAccurateBandPlus1 = 29;
51
52 constexpr float oneByBandsInSum =
53 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20);
54 const float hf_gain_bound =
55 std::accumulate(gain->begin() + 20,
56 gain->begin() + kUpperAccurateBandPlus1, 0.f) *
57 oneByBandsInSum;
58
59 std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(),
60 [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); });
peaha2376e72017-02-27 01:15:24 -080061}
62
Per Åhgrenb02644f2018-04-17 11:52:17 +020063// Scales the echo according to assessed audibility at the other end.
64void WeightEchoForAudibility(const EchoCanceller3Config& config,
65 rtc::ArrayView<const float> echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020066 rtc::ArrayView<float> weighted_echo) {
Per Åhgrenb02644f2018-04-17 11:52:17 +020067 RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
68 RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
Per Åhgrenb02644f2018-04-17 11:52:17 +020069
70 auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
71 rtc::ArrayView<const float> echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020072 rtc::ArrayView<float> weighted_echo) {
Per Åhgrenb02644f2018-04-17 11:52:17 +020073 for (size_t k = begin; k < end; ++k) {
74 if (echo[k] < threshold) {
75 float tmp = (threshold - echo[k]) * normalizer;
76 weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp);
77 } else {
78 weighted_echo[k] = echo[k];
79 }
Per Åhgrenb02644f2018-04-17 11:52:17 +020080 }
81 };
82
83 float threshold = config.echo_audibility.floor_power *
84 config.echo_audibility.audibility_threshold_lf;
85 float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020086 weigh(threshold, normalizer, 0, 3, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020087
88 threshold = config.echo_audibility.floor_power *
89 config.echo_audibility.audibility_threshold_mf;
90 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020091 weigh(threshold, normalizer, 3, 7, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020092
93 threshold = config.echo_audibility.floor_power *
94 config.echo_audibility.audibility_threshold_hf;
95 normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020096 weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo);
Per Åhgrenb02644f2018-04-17 11:52:17 +020097}
98
peah1d680892017-05-23 04:07:10 -070099} // namespace
100
Gustaf Ullberg216af842018-04-26 12:39:11 +0200101int SuppressionGain::instance_count_ = 0;
102
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200103float SuppressionGain::UpperBandsGain(
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100104 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum,
105 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
106 comfort_noise_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200107 const absl::optional<int>& narrow_peak_band,
108 bool saturated_echo,
Per Åhgrence202a02019-09-02 17:01:19 +0200109 const std::vector<std::vector<std::vector<float>>>& render,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200110 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const {
111 RTC_DCHECK_LT(0, render.size());
112 if (render.size() == 1) {
113 return 1.f;
114 }
Per Åhgren119e2192019-10-18 08:50:50 +0200115 const size_t num_render_channels = render[0].size();
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200116
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; };
Per Åhgren119e2192019-10-18 08:50:50 +0200133 float low_band_energy = 0.f;
134 for (size_t ch = 0; ch < num_render_channels; ++ch) {
135 const float channel_energy = std::accumulate(
136 render[0][0].begin(), render[0][0].end(), 0.f, sum_of_squares);
137 low_band_energy = std::max(low_band_energy, channel_energy);
138 }
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200139 float high_band_energy = 0.f;
140 for (size_t k = 1; k < render.size(); ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200141 for (size_t ch = 0; ch < num_render_channels; ++ch) {
142 const float energy = std::accumulate(
143 render[k][ch].begin(), render[k][ch].end(), 0.f, sum_of_squares);
144 high_band_energy = std::max(high_band_energy, energy);
145 }
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200146 }
147
148 // If there is more power in the lower frequencies than the upper frequencies,
149 // or if the power in upper frequencies is low, do not bound the gain in the
150 // upper bands.
151 float anti_howling_gain;
152 constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f;
153 if (high_band_energy < std::max(low_band_energy, kThreshold)) {
154 anti_howling_gain = 1.f;
155 } else {
156 // In all other cases, bound the gain for upper frequencies.
157 RTC_DCHECK_LE(low_band_energy, high_band_energy);
158 RTC_DCHECK_NE(0.f, high_band_energy);
159 anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy);
160 }
161
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200162 float gain_bound = 1.f;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100163 if (!dominant_nearend_detector_.IsNearendState()) {
164 // Bound the upper gain during significant echo activity.
165 const auto& cfg = config_.suppressor.high_bands_suppression;
166 auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) {
167 RTC_DCHECK_LE(16, spectrum.size());
168 return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f);
169 };
170 for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
171 const float echo_sum = low_frequency_energy(echo_spectrum[ch]);
172 const float noise_sum = low_frequency_energy(comfort_noise_spectrum[ch]);
173 if (echo_sum > cfg.enr_threshold * noise_sum) {
174 gain_bound = cfg.max_gain_during_echo;
175 break;
176 }
177 }
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200178 }
179
180 // Choose the gain as the minimum of the lower and upper gains.
181 return std::min(std::min(gain_below_8_khz, anti_howling_gain), gain_bound);
182}
183
Gustaf Ullbergec642172018-07-03 13:48:32 +0200184// Computes the gain to reduce the echo to a non audible level.
185void SuppressionGain::GainToNoAudibleEcho(
186 const std::array<float, kFftLengthBy2Plus1>& nearend,
187 const std::array<float, kFftLengthBy2Plus1>& echo,
188 const std::array<float, kFftLengthBy2Plus1>& masker,
Gustaf Ullbergec642172018-07-03 13:48:32 +0200189 std::array<float, kFftLengthBy2Plus1>* gain) const {
Per Åhgren524e8782018-08-24 22:48:49 +0200190 const auto& p = dominant_nearend_detector_.IsNearendState() ? nearend_params_
191 : normal_params_;
Gustaf Ullbergec642172018-07-03 13:48:32 +0200192 for (size_t k = 0; k < gain->size(); ++k) {
193 float enr = echo[k] / (nearend[k] + 1.f); // Echo-to-nearend ratio.
194 float emr = echo[k] / (masker[k] + 1.f); // Echo-to-masker (noise) ratio.
195 float g = 1.0f;
Per Åhgren524e8782018-08-24 22:48:49 +0200196 if (enr > p.enr_transparent_[k] && emr > p.emr_transparent_[k]) {
197 g = (p.enr_suppress_[k] - enr) /
198 (p.enr_suppress_[k] - p.enr_transparent_[k]);
199 g = std::max(g, p.emr_transparent_[k] / emr);
Gustaf Ullbergec642172018-07-03 13:48:32 +0200200 }
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100201 (*gain)[k] = g;
Gustaf Ullbergec642172018-07-03 13:48:32 +0200202 }
203}
204
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200205// Compute the minimum gain as the attenuating gain to put the signal just
206// above the zero sample values.
207void SuppressionGain::GetMinGain(
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200208 rtc::ArrayView<const float> weighted_residual_echo,
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100209 rtc::ArrayView<const float> last_nearend,
210 rtc::ArrayView<const float> last_echo,
peah1d680892017-05-23 04:07:10 -0700211 bool low_noise_render,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200212 bool saturated_echo,
213 rtc::ArrayView<float> min_gain) const {
Per Åhgren31122d62018-04-10 16:33:55 +0200214 if (!saturated_echo) {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200215 const float min_echo_power =
216 low_noise_render ? config_.echo_audibility.low_render_limit
217 : config_.echo_audibility.normal_render_limit;
218
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200219 for (size_t k = 0; k < min_gain.size(); ++k) {
220 min_gain[k] = weighted_residual_echo[k] > 0.f
221 ? min_echo_power / weighted_residual_echo[k]
222 : 1.f;
peah1d680892017-05-23 04:07:10 -0700223 min_gain[k] = std::min(min_gain[k], 1.f);
224 }
Gustaf Ullbergecb2d562018-08-23 15:11:38 +0200225 for (size_t k = 0; k < 6; ++k) {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200226 const auto& dec = dominant_nearend_detector_.IsNearendState()
227 ? nearend_params_.max_dec_factor_lf
228 : normal_params_.max_dec_factor_lf;
229
Gustaf Ullbergecb2d562018-08-23 15:11:38 +0200230 // Make sure the gains of the low frequencies do not decrease too
231 // quickly after strong nearend.
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100232 if (last_nearend[k] > last_echo[k]) {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200233 min_gain[k] = std::max(min_gain[k], last_gain_[k] * dec);
Gustaf Ullbergecb2d562018-08-23 15:11:38 +0200234 min_gain[k] = std::min(min_gain[k], 1.f);
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200235 }
236 }
peah1d680892017-05-23 04:07:10 -0700237 } else {
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200238 std::fill(min_gain.begin(), min_gain.end(), 0.f);
peah1d680892017-05-23 04:07:10 -0700239 }
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200240}
peah1d680892017-05-23 04:07:10 -0700241
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200242// Compute the maximum gain by limiting the gain increase from the previous
243// gain.
244void SuppressionGain::GetMaxGain(rtc::ArrayView<float> max_gain) const {
245 const auto& inc = dominant_nearend_detector_.IsNearendState()
246 ? nearend_params_.max_inc_factor
247 : normal_params_.max_inc_factor;
248 const auto& floor = config_.suppressor.floor_first_increase;
249 for (size_t k = 0; k < max_gain.size(); ++k) {
250 max_gain[k] = std::min(std::max(last_gain_[k] * inc, floor), 1.f);
peah1d680892017-05-23 04:07:10 -0700251 }
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200252}
253
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200254void SuppressionGain::LowerBandGain(
255 bool low_noise_render,
256 const AecState& aec_state,
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100257 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
258 suppressor_input,
259 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> residual_echo,
260 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> comfort_noise,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200261 std::array<float, kFftLengthBy2Plus1>* gain) {
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100262 gain->fill(1.f);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200263 const bool saturated_echo = aec_state.SaturatedEcho();
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200264 std::array<float, kFftLengthBy2Plus1> max_gain;
265 GetMaxGain(max_gain);
peah1d680892017-05-23 04:07:10 -0700266
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100267 for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
268 std::array<float, kFftLengthBy2Plus1> G;
269 std::array<float, kFftLengthBy2Plus1> nearend;
270 nearend_smoothers_[ch].Average(suppressor_input[ch], nearend);
peah1d680892017-05-23 04:07:10 -0700271
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100272 // Weight echo power in terms of audibility.
273 std::array<float, kFftLengthBy2Plus1> weighted_residual_echo;
274 WeightEchoForAudibility(config_, residual_echo[ch], weighted_residual_echo);
Per Åhgren85a11a32017-10-02 14:42:06 +0200275
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100276 std::array<float, kFftLengthBy2Plus1> min_gain;
277 GetMinGain(weighted_residual_echo, last_nearend_[ch], last_echo_[ch],
278 low_noise_render, saturated_echo, min_gain);
279
280 GainToNoAudibleEcho(nearend, weighted_residual_echo, comfort_noise[0], &G);
281
282 // Clamp gains.
283 for (size_t k = 0; k < gain->size(); ++k) {
284 G[k] = std::max(std::min(G[k], max_gain[k]), min_gain[k]);
285 (*gain)[k] = std::min((*gain)[k], G[k]);
286 }
287
288 // Store data required for the gain computation of the next block.
289 std::copy(nearend.begin(), nearend.end(), last_nearend_[ch].begin());
290 std::copy(weighted_residual_echo.begin(), weighted_residual_echo.end(),
291 last_echo_[ch].begin());
292 }
293
294 // Limit high-frequency gains.
295 PostprocessGains(gain);
296
297 // Store computed gains.
peah1d680892017-05-23 04:07:10 -0700298 std::copy(gain->begin(), gain->end(), last_gain_.begin());
Gustaf Ullberg216af842018-04-26 12:39:11 +0200299
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100300 // Transform gains to amplitude domain.
301 aec3::VectorMath(optimization_).Sqrt(*gain);
peah86afe9d2017-04-06 15:45:32 -0700302}
303
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200304SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200305 Aec3Optimization optimization,
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100306 int sample_rate_hz,
307 size_t num_capture_channels)
Gustaf Ullberg216af842018-04-26 12:39:11 +0200308 : data_dumper_(
309 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
310 optimization_(optimization),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100311 config_(config),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100312 num_capture_channels_(num_capture_channels),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100313 state_change_duration_blocks_(
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200314 static_cast<int>(config_.filter.config_change_duration_blocks)),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100315 last_nearend_(num_capture_channels_, {0}),
316 last_echo_(num_capture_channels_, {0}),
317 nearend_smoothers_(
318 num_capture_channels_,
319 aec3::MovingAverage(kFftLengthBy2Plus1,
320 config.suppressor.nearend_average_blocks)),
Per Åhgren524e8782018-08-24 22:48:49 +0200321 nearend_params_(config_.suppressor.nearend_tuning),
322 normal_params_(config_.suppressor.normal_tuning),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100323 dominant_nearend_detector_(config_.suppressor.dominant_nearend_detection,
324 num_capture_channels_) {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100325 RTC_DCHECK_LT(0, state_change_duration_blocks_);
peah1d680892017-05-23 04:07:10 -0700326 last_gain_.fill(1.f);
peah522d71b2017-02-23 05:16:26 -0800327}
328
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200329SuppressionGain::~SuppressionGain() = default;
330
peah522d71b2017-02-23 05:16:26 -0800331void SuppressionGain::GetGain(
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100332 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
333 nearend_spectrum,
334 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum,
335 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
336 residual_echo_spectrum,
337 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
338 comfort_noise_spectrum,
peah14c11a42017-07-11 06:13:43 -0700339 const RenderSignalAnalyzer& render_signal_analyzer,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200340 const AecState& aec_state,
Per Åhgrence202a02019-09-02 17:01:19 +0200341 const std::vector<std::vector<std::vector<float>>>& render,
peah86afe9d2017-04-06 15:45:32 -0700342 float* high_bands_gain,
343 std::array<float, kFftLengthBy2Plus1>* low_band_gain) {
344 RTC_DCHECK(high_bands_gain);
345 RTC_DCHECK(low_band_gain);
346
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100347 // Update the nearend state selection.
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200348 dominant_nearend_detector_.Update(nearend_spectrum, residual_echo_spectrum,
Per Åhgren700b4a42018-10-23 21:21:37 +0200349 comfort_noise_spectrum, initial_state_);
Per Åhgren524e8782018-08-24 22:48:49 +0200350
peah1d680892017-05-23 04:07:10 -0700351 // Compute gain for the lower band.
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100352 bool low_noise_render = low_render_detector_.Detect(render);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100353 LowerBandGain(low_noise_render, aec_state, nearend_spectrum,
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200354 residual_echo_spectrum, comfort_noise_spectrum, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700355
Gustaf Ullberg0cb4a252018-04-26 15:45:44 +0200356 // Compute the gain for the upper bands.
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200357 const absl::optional<int> narrow_peak_band =
358 render_signal_analyzer.NarrowPeakBand();
359
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200360 *high_bands_gain =
361 UpperBandsGain(echo_spectrum, comfort_noise_spectrum, narrow_peak_band,
362 aec_state.SaturatedEcho(), render, *low_band_gain);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100363}
364
365void SuppressionGain::SetInitialState(bool state) {
366 initial_state_ = state;
367 if (state) {
368 initial_state_change_counter_ = state_change_duration_blocks_;
369 } else {
370 initial_state_change_counter_ = 0;
371 }
372}
373
peah1d680892017-05-23 04:07:10 -0700374// Detects when the render signal can be considered to have low power and
375// consist of stationary noise.
376bool SuppressionGain::LowNoiseRenderDetector::Detect(
Per Åhgrence202a02019-09-02 17:01:19 +0200377 const std::vector<std::vector<std::vector<float>>>& render) {
peah1d680892017-05-23 04:07:10 -0700378 float x2_sum = 0.f;
379 float x2_max = 0.f;
Per Åhgren119e2192019-10-18 08:50:50 +0200380 for (auto x_ch : render[0]) {
381 for (auto x_k : x_ch) {
382 const float x2 = x_k * x_k;
383 x2_sum += x2;
384 x2_max = std::max(x2_max, x2);
385 }
peah522d71b2017-02-23 05:16:26 -0800386 }
Per Åhgren119e2192019-10-18 08:50:50 +0200387 const size_t num_render_channels = render[0].size();
388 x2_sum = x2_sum / num_render_channels;
389 ;
peah1d680892017-05-23 04:07:10 -0700390
391 constexpr float kThreshold = 50.f * 50.f * 64.f;
392 const bool low_noise_render =
393 average_power_ < kThreshold && x2_max < 3 * average_power_;
394 average_power_ = average_power_ * 0.9f + x2_sum * 0.1f;
395 return low_noise_render;
peah522d71b2017-02-23 05:16:26 -0800396}
397
Per Åhgren524e8782018-08-24 22:48:49 +0200398SuppressionGain::GainParameters::GainParameters(
399 const EchoCanceller3Config::Suppressor::Tuning& tuning)
400 : max_inc_factor(tuning.max_inc_factor),
401 max_dec_factor_lf(tuning.max_dec_factor_lf) {
402 // Compute per-band masking thresholds.
403 constexpr size_t kLastLfBand = 5;
404 constexpr size_t kFirstHfBand = 8;
405 RTC_DCHECK_LT(kLastLfBand, kFirstHfBand);
406 auto& lf = tuning.mask_lf;
407 auto& hf = tuning.mask_hf;
408 RTC_DCHECK_LT(lf.enr_transparent, lf.enr_suppress);
409 RTC_DCHECK_LT(hf.enr_transparent, hf.enr_suppress);
410 for (size_t k = 0; k < kFftLengthBy2Plus1; k++) {
411 float a;
412 if (k <= kLastLfBand) {
413 a = 0.f;
414 } else if (k < kFirstHfBand) {
415 a = (k - kLastLfBand) / static_cast<float>(kFirstHfBand - kLastLfBand);
416 } else {
417 a = 1.f;
418 }
419 enr_transparent_[k] = (1 - a) * lf.enr_transparent + a * hf.enr_transparent;
420 enr_suppress_[k] = (1 - a) * lf.enr_suppress + a * hf.enr_suppress;
421 emr_transparent_[k] = (1 - a) * lf.emr_transparent + a * hf.emr_transparent;
422 }
423}
424
peah522d71b2017-02-23 05:16:26 -0800425} // namespace webrtc