blob: 7818ff77a410b5ec737fb3d2276c3f649df1ce03 [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/aec_state.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <math.h>
Raphael Kubo da Costa07438142017-10-16 17:00:02 +020014
peah522d71b2017-02-23 05:16:26 -080015#include <numeric>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
19#include "modules/audio_processing/logging/apm_data_dumper.h"
20#include "rtc_base/atomicops.h"
21#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080022
23namespace webrtc {
24namespace {
25
peah86afe9d2017-04-06 15:45:32 -070026// Computes delay of the adaptive filter.
Per Åhgren40659c32017-10-17 12:56:21 +020027int EstimateFilterDelay(
peah522d71b2017-02-23 05:16:26 -080028 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -070029 adaptive_filter_frequency_response) {
30 const auto& H2 = adaptive_filter_frequency_response;
peah522d71b2017-02-23 05:16:26 -080031 constexpr size_t kUpperBin = kFftLengthBy2 - 5;
Per Åhgren09a718a2017-12-11 22:28:45 +010032 RTC_DCHECK_GE(kMaxAdaptiveFilterLength, H2.size());
33 std::array<int, kMaxAdaptiveFilterLength> delays;
Per Åhgren40659c32017-10-17 12:56:21 +020034 delays.fill(0);
peah522d71b2017-02-23 05:16:26 -080035 for (size_t k = 1; k < kUpperBin; ++k) {
peah86afe9d2017-04-06 15:45:32 -070036 // Find the maximum of H2[j].
Per Åhgren40659c32017-10-17 12:56:21 +020037 size_t peak = 0;
peah522d71b2017-02-23 05:16:26 -080038 for (size_t j = 0; j < H2.size(); ++j) {
39 if (H2[j][k] > H2[peak][k]) {
40 peak = j;
41 }
42 }
Per Åhgren40659c32017-10-17 12:56:21 +020043 ++delays[peak];
peah522d71b2017-02-23 05:16:26 -080044 }
peah522d71b2017-02-23 05:16:26 -080045
Per Åhgren40659c32017-10-17 12:56:21 +020046 return std::distance(delays.begin(),
47 std::max_element(delays.begin(), delays.end()));
peah522d71b2017-02-23 05:16:26 -080048}
49
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010050float ComputeGainRampupIncrease(const EchoCanceller3Config& config) {
51 const auto& c = config.echo_removal_control.gain_rampup;
52 return powf(1.f / c.first_non_zero_gain, 1.f / c.non_zero_gain_blocks);
53}
54
peah522d71b2017-02-23 05:16:26 -080055} // namespace
56
57int AecState::instance_count_ = 0;
58
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020059AecState::AecState(const EchoCanceller3Config& config)
peah522d71b2017-02-23 05:16:26 -080060 : data_dumper_(
61 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020062 erle_estimator_(config.erle.min, config.erle.max_l, config.erle.max_h),
peah8cee56f2017-08-24 22:36:53 -070063 config_(config),
Per Åhgren08ea5892018-01-15 08:07:41 +010064 max_render_(config_.filter.main.length_blocks, 0.f),
Christian Schuldtf4e99db2018-03-01 11:32:50 +010065 reverb_decay_(fabsf(config_.ep_strength.default_len)),
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010066 gain_rampup_increase_(ComputeGainRampupIncrease(config_)) {}
peah522d71b2017-02-23 05:16:26 -080067
68AecState::~AecState() = default;
69
peah86afe9d2017-04-06 15:45:32 -070070void AecState::HandleEchoPathChange(
71 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010072 const auto full_reset = [&]() {
Per Åhgren63b494d2017-12-06 11:32:38 +010073 blocks_since_last_saturation_ = 0;
peah86afe9d2017-04-06 15:45:32 -070074 usable_linear_estimate_ = false;
75 echo_leakage_detected_ = false;
76 capture_signal_saturation_ = false;
77 echo_saturation_ = false;
Per Åhgren63b494d2017-12-06 11:32:38 +010078 previous_max_sample_ = 0.f;
Per Åhgren09a718a2017-12-11 22:28:45 +010079 std::fill(max_render_.begin(), max_render_.end(), 0.f);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010080 blocks_with_proper_filter_adaptation_ = 0;
Per Åhgren8ba58612017-12-01 23:01:44 +010081 capture_block_counter_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010082 filter_has_had_time_to_converge_ = false;
Per Åhgren8ba58612017-12-01 23:01:44 +010083 render_received_ = false;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010084 blocks_with_active_render_ = 0;
Per Åhgrena98c8072018-01-15 19:17:16 +010085 initial_state_ = true;
Per Åhgren8ba58612017-12-01 23:01:44 +010086 };
peah6d822ad2017-04-10 13:52:14 -070087
Per Åhgren8ba58612017-12-01 23:01:44 +010088 // TODO(peah): Refine the reset scheme according to the type of gain and
89 // delay adjustment.
90 if (echo_path_variability.gain_change) {
91 full_reset();
92 }
93
94 if (echo_path_variability.delay_change !=
95 EchoPathVariability::DelayAdjustment::kBufferReadjustment) {
96 full_reset();
97 } else if (echo_path_variability.delay_change !=
98 EchoPathVariability::DelayAdjustment::kBufferFlush) {
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010099 active_render_seen_ = false;
Per Åhgren8ba58612017-12-01 23:01:44 +0100100 full_reset();
Per Åhgren8ba58612017-12-01 23:01:44 +0100101 } else if (echo_path_variability.delay_change !=
102 EchoPathVariability::DelayAdjustment::kDelayReset) {
103 full_reset();
104 } else if (echo_path_variability.delay_change !=
105 EchoPathVariability::DelayAdjustment::kNewDetectedDelay) {
106 full_reset();
107 } else if (echo_path_variability.gain_change) {
108 capture_block_counter_ = kNumBlocksPerSecond;
peah86afe9d2017-04-06 15:45:32 -0700109 }
110}
111
Per Åhgren09a718a2017-12-11 22:28:45 +0100112void AecState::Update(
Per Åhgren3ab308f2018-02-21 08:46:03 +0100113 const rtc::Optional<DelayEstimate>& delay_estimate,
Per Åhgren09a718a2017-12-11 22:28:45 +0100114 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
115 adaptive_filter_frequency_response,
116 const std::vector<float>& adaptive_filter_impulse_response,
117 bool converged_filter,
Per Åhgren09a718a2017-12-11 22:28:45 +0100118 const RenderBuffer& render_buffer,
119 const std::array<float, kFftLengthBy2Plus1>& E2_main,
120 const std::array<float, kFftLengthBy2Plus1>& Y2,
Per Åhgren09a718a2017-12-11 22:28:45 +0100121 const std::array<float, kBlockSize>& s,
122 bool echo_leakage_detected) {
peah86afe9d2017-04-06 15:45:32 -0700123 // Store input parameters.
124 echo_leakage_detected_ = echo_leakage_detected;
125
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100126 // Estimate the filter delay.
127 filter_delay_ = EstimateFilterDelay(adaptive_filter_frequency_response);
128 const std::vector<float>& x = render_buffer.Block(-filter_delay_)[0];
129
peah86afe9d2017-04-06 15:45:32 -0700130 // Update counters.
Per Åhgren1b4059e2017-10-15 20:19:21 +0200131 ++capture_block_counter_;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100132 const bool active_render_block = DetectActiveRender(x);
133 blocks_with_active_render_ += active_render_block ? 1 : 0;
134 blocks_with_proper_filter_adaptation_ +=
135 active_render_block && !SaturatedCapture() ? 1 : 0;
peah86afe9d2017-04-06 15:45:32 -0700136
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100137 // Update the limit on the echo suppression after an echo path change to avoid
138 // an initial echo burst.
139 UpdateSuppressorGainLimit(render_buffer.GetRenderActivity());
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100140
peah86afe9d2017-04-06 15:45:32 -0700141 // Update the ERL and ERLE measures.
Per Åhgren40659c32017-10-17 12:56:21 +0200142 if (converged_filter && capture_block_counter_ >= 2 * kNumBlocksPerSecond) {
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100143 const auto& X2 = render_buffer.Spectrum(filter_delay_);
peah522d71b2017-02-23 05:16:26 -0800144 erle_estimator_.Update(X2, Y2, E2_main);
145 erl_estimator_.Update(X2, Y2);
peah522d71b2017-02-23 05:16:26 -0800146 }
peah86afe9d2017-04-06 15:45:32 -0700147
Per Åhgren1b4059e2017-10-15 20:19:21 +0200148 // Update the echo audibility evaluator.
149 echo_audibility_.Update(x, s, converged_filter);
150
Per Åhgren63b494d2017-12-06 11:32:38 +0100151 // Detect and flag echo saturation.
152 // TODO(peah): Add the delay in this computation to ensure that the render and
153 // capture signals are properly aligned.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200154 if (config_.ep_strength.echo_can_saturate) {
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100155 echo_saturation_ = DetectEchoSaturation(x);
Per Åhgren1b4059e2017-10-15 20:19:21 +0200156 }
peah86afe9d2017-04-06 15:45:32 -0700157
Per Åhgren63b494d2017-12-06 11:32:38 +0100158 // TODO(peah): Move?
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100159 filter_has_had_time_to_converge_ =
Per Åhgren29f14322018-02-06 15:31:57 +0100160 blocks_with_proper_filter_adaptation_ >= 1.5f * kNumBlocksPerSecond;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100161
Per Åhgrena98c8072018-01-15 19:17:16 +0100162 initial_state_ =
163 blocks_with_proper_filter_adaptation_ < 5 * kNumBlocksPerSecond;
164
Per Åhgren63b494d2017-12-06 11:32:38 +0100165 // Flag whether the linear filter estimate is usable.
166 usable_linear_estimate_ =
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100167 !echo_saturation_ &&
Per Åhgren3f1c0622018-01-15 13:22:43 +0100168 (converged_filter && filter_has_had_time_to_converge_) &&
Per Åhgren0eef9c02018-01-22 20:24:06 +0100169 capture_block_counter_ >= 1.f * kNumBlocksPerSecond && !TransparentMode();
Per Åhgren63b494d2017-12-06 11:32:38 +0100170
171 // After an amount of active render samples for which an echo should have been
172 // detected in the capture signal if the ERL was not infinite, flag that a
173 // transparent mode should be entered.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100174 transparent_mode_ =
175 !converged_filter &&
176 (blocks_with_active_render_ == 0 ||
177 blocks_with_proper_filter_adaptation_ >= 5 * kNumBlocksPerSecond);
peah29103572017-07-11 02:54:02 -0700178}
179
Per Åhgren09a718a2017-12-11 22:28:45 +0100180void AecState::UpdateReverb(const std::vector<float>& impulse_response) {
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100181 // Echo tail estimation enabled if the below variable is set as negative.
182 if (config_.ep_strength.default_len > 0.f) {
183 return;
184 }
185
peah29103572017-07-11 02:54:02 -0700186 if ((!(filter_delay_ && usable_linear_estimate_)) ||
Per Åhgren08ea5892018-01-15 08:07:41 +0100187 (filter_delay_ >
188 static_cast<int>(config_.filter.main.length_blocks) - 4)) {
peah29103572017-07-11 02:54:02 -0700189 return;
190 }
191
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100192 constexpr float kOneByFftLengthBy2 = 1.f / kFftLengthBy2;
193
peah29103572017-07-11 02:54:02 -0700194 // Form the data to match against by squaring the impulse response
195 // coefficients.
Per Åhgren09a718a2017-12-11 22:28:45 +0100196 std::array<float, GetTimeDomainLength(kMaxAdaptiveFilterLength)>
197 matching_data_data;
Per Åhgren08ea5892018-01-15 08:07:41 +0100198 RTC_DCHECK_LE(GetTimeDomainLength(config_.filter.main.length_blocks),
Per Åhgren09a718a2017-12-11 22:28:45 +0100199 matching_data_data.size());
200 rtc::ArrayView<float> matching_data(
201 matching_data_data.data(),
Per Åhgren08ea5892018-01-15 08:07:41 +0100202 GetTimeDomainLength(config_.filter.main.length_blocks));
peah29103572017-07-11 02:54:02 -0700203 std::transform(impulse_response.begin(), impulse_response.end(),
204 matching_data.begin(), [](float a) { return a * a; });
205
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100206 if (current_reverb_decay_section_ < config_.filter.main.length_blocks) {
207 // Update accumulated variables for the current filter section.
peah29103572017-07-11 02:54:02 -0700208
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100209 const size_t start_index = current_reverb_decay_section_ * kFftLengthBy2;
peah29103572017-07-11 02:54:02 -0700210
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100211 RTC_DCHECK_GT(matching_data.size(), start_index);
212 RTC_DCHECK_GE(matching_data.size(), start_index + kFftLengthBy2);
213 float section_energy =
214 std::accumulate(matching_data.begin() + start_index,
215 matching_data.begin() + start_index + kFftLengthBy2,
216 0.f) *
217 kOneByFftLengthBy2;
218
219 section_energy = std::max(
220 section_energy, 1e-32f); // Regularization to avoid division by 0.
221
222 RTC_DCHECK_LT(current_reverb_decay_section_, block_energies_.size());
223 const float energy_ratio =
224 block_energies_[current_reverb_decay_section_] / section_energy;
225
226 main_filter_is_adapting_ = main_filter_is_adapting_ ||
227 (energy_ratio > 1.1f || energy_ratio < 0.9f);
228
229 // Count consecutive number of "good" filter sections, where "good" means:
230 // 1) energy is above noise floor.
231 // 2) energy of current section has not changed too much from last check.
232 if (!found_end_of_reverb_decay_ && section_energy > tail_energy_ &&
233 !main_filter_is_adapting_) {
234 ++num_reverb_decay_sections_next_;
235 } else {
236 found_end_of_reverb_decay_ = true;
237 }
238
239 block_energies_[current_reverb_decay_section_] = section_energy;
240
241 if (num_reverb_decay_sections_ > 0) {
242 // Linear regression of log squared magnitude of impulse response.
243 for (size_t i = 0; i < kFftLengthBy2; i++) {
244 auto fast_approx_log2f = [](const float in) {
245 RTC_DCHECK_GT(in, .0f);
246 // Read and interpret float as uint32_t and then cast to float.
247 // This is done to extract the exponent (bits 30 - 23).
248 // "Right shift" of the exponent is then performed by multiplying
249 // with the constant (1/2^23). Finally, we subtract a constant to
250 // remove the bias (https://en.wikipedia.org/wiki/Exponent_bias).
251 union {
252 float dummy;
253 uint32_t a;
254 } x = {in};
255 float out = x.a;
256 out *= 1.1920929e-7f; // 1/2^23
257 out -= 126.942695f; // Remove bias.
258 return out;
259 };
260 RTC_DCHECK_GT(matching_data.size(), start_index + i);
261 float z = fast_approx_log2f(matching_data[start_index + i]);
262 accumulated_nz_ += accumulated_count_ * z;
263 ++accumulated_count_;
peah29103572017-07-11 02:54:02 -0700264 }
peah29103572017-07-11 02:54:02 -0700265 }
266
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100267 num_reverb_decay_sections_ =
268 num_reverb_decay_sections_ > 0 ? num_reverb_decay_sections_ - 1 : 0;
269 ++current_reverb_decay_section_;
270
271 } else {
272 constexpr float kMaxDecay = 0.95f; // ~1 sec min RT60.
273 constexpr float kMinDecay = 0.02f; // ~15 ms max RT60.
274
275 // Accumulated variables throughout whole filter.
276
277 // Solve for decay rate.
278
279 float decay = reverb_decay_;
280
281 if (accumulated_nn_ != 0.f) {
282 const float exp_candidate = -accumulated_nz_ / accumulated_nn_;
283 decay = powf(2.0f, -exp_candidate * kFftLengthBy2);
284 decay = std::min(decay, kMaxDecay);
285 decay = std::max(decay, kMinDecay);
peah29103572017-07-11 02:54:02 -0700286 }
peah29103572017-07-11 02:54:02 -0700287
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100288 // Filter tail energy (assumed to be noise).
peah29103572017-07-11 02:54:02 -0700289
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100290 constexpr size_t kTailLength = kFftLength;
291 constexpr float k1ByTailLength = 1.f / kTailLength;
292 const size_t tail_index =
293 GetTimeDomainLength(config_.filter.main.length_blocks) - kTailLength;
peah29103572017-07-11 02:54:02 -0700294
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100295 RTC_DCHECK_GT(matching_data.size(), tail_index);
296 tail_energy_ = std::accumulate(matching_data.begin() + tail_index,
297 matching_data.end(), 0.f) *
298 k1ByTailLength;
299
300 // Update length of decay.
301 num_reverb_decay_sections_ = num_reverb_decay_sections_next_;
302 num_reverb_decay_sections_next_ = 0;
303 // Must have enough data (number of sections) in order
304 // to estimate decay rate.
305 if (num_reverb_decay_sections_ < 5) {
306 num_reverb_decay_sections_ = 0;
peah29103572017-07-11 02:54:02 -0700307 }
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100308
309 const float N = num_reverb_decay_sections_ * kFftLengthBy2;
310 accumulated_nz_ = 0.f;
311 const float k1By12 = 1.f / 12.f;
312 // Arithmetic sum $2 \sum_{i=0}^{(N-1)/2}i^2$ calculated directly.
313 accumulated_nn_ = N * (N * N - 1.0f) * k1By12;
314 accumulated_count_ = -N * 0.5f;
315 // Linear regression approach assumes symmetric index around 0.
316 accumulated_count_ += 0.5f;
317
318 // Identify the peak index of the impulse response.
319 const size_t peak_index = std::distance(
320 matching_data.begin(),
321 std::max_element(matching_data.begin(), matching_data.end()));
322
323 current_reverb_decay_section_ = peak_index * kOneByFftLengthBy2 + 3;
324 // Make sure we're not out of bounds.
325 if (current_reverb_decay_section_ + 1 >=
326 config_.filter.main.length_blocks) {
327 current_reverb_decay_section_ = config_.filter.main.length_blocks;
328 }
329 size_t start_index = current_reverb_decay_section_ * kFftLengthBy2;
330 float first_section_energy =
331 std::accumulate(matching_data.begin() + start_index,
332 matching_data.begin() + start_index + kFftLengthBy2,
333 0.f) *
334 kOneByFftLengthBy2;
335
336 // To estimate the reverb decay, the energy of the first filter section
337 // must be substantially larger than the last.
338 // Also, the first filter section energy must not deviate too much
339 // from the max peak.
340 bool main_filter_has_reverb = first_section_energy > 4.f * tail_energy_;
341 bool main_filter_is_sane = first_section_energy > 2.f * tail_energy_ &&
342 matching_data[peak_index] < 100.f;
343
344 // Not detecting any decay, but tail is over noise - assume max decay.
345 if (num_reverb_decay_sections_ == 0 && main_filter_is_sane &&
346 main_filter_has_reverb) {
347 decay = kMaxDecay;
348 }
349
350 if (!main_filter_is_adapting_ && main_filter_is_sane &&
351 num_reverb_decay_sections_ > 0) {
352 decay = std::max(.97f * reverb_decay_, decay);
353 reverb_decay_ -= .1f * (reverb_decay_ - decay);
354 }
355
356 found_end_of_reverb_decay_ =
357 !(main_filter_is_sane && main_filter_has_reverb);
358 main_filter_is_adapting_ = false;
peah29103572017-07-11 02:54:02 -0700359 }
360
peah29103572017-07-11 02:54:02 -0700361 data_dumper_->DumpRaw("aec3_reverb_decay", reverb_decay_);
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100362 data_dumper_->DumpRaw("aec3_reverb_tail_energy", tail_energy_);
peah29103572017-07-11 02:54:02 -0700363}
364
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100365bool AecState::DetectActiveRender(rtc::ArrayView<const float> x) const {
366 const float x_energy = std::inner_product(x.begin(), x.end(), x.begin(), 0.f);
367 return x_energy > (config_.render_levels.active_render_limit *
368 config_.render_levels.active_render_limit) *
369 kFftLengthBy2;
370}
371
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100372// Updates the suppressor gain limit.
373void AecState::UpdateSuppressorGainLimit(bool render_activity) {
374 const auto& rampup_conf = config_.echo_removal_control.gain_rampup;
375 if (!active_render_seen_ && render_activity) {
376 active_render_seen_ = true;
377 realignment_counter_ = rampup_conf.full_gain_blocks;
378 } else if (realignment_counter_ > 0) {
379 --realignment_counter_;
380 }
381
382 if (realignment_counter_ <= 0) {
383 suppressor_gain_limit_ = 1.f;
384 return;
385 }
386
387 if (realignment_counter_ > rampup_conf.non_zero_gain_blocks) {
388 suppressor_gain_limit_ = 0.f;
389 return;
390 }
391
392 if (realignment_counter_ == rampup_conf.non_zero_gain_blocks) {
393 suppressor_gain_limit_ = rampup_conf.first_non_zero_gain;
394 return;
395 }
396
397 RTC_DCHECK_LT(0.f, suppressor_gain_limit_);
398 suppressor_gain_limit_ =
399 std::min(1.f, suppressor_gain_limit_ * gain_rampup_increase_);
400 RTC_DCHECK_GE(1.f, suppressor_gain_limit_);
401}
402
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100403bool AecState::DetectEchoSaturation(rtc::ArrayView<const float> x) {
404 RTC_DCHECK_LT(0, x.size());
405 const float max_sample = fabs(*std::max_element(
406 x.begin(), x.end(), [](float a, float b) { return a * a < b * b; }));
407 previous_max_sample_ = max_sample;
408
409 // Set flag for potential presence of saturated echo
410 blocks_since_last_saturation_ =
411 previous_max_sample_ > 200.f && SaturatedCapture()
412 ? 0
413 : blocks_since_last_saturation_ + 1;
414
415 return blocks_since_last_saturation_ < 20;
416}
417
peah29103572017-07-11 02:54:02 -0700418void AecState::EchoAudibility::Update(rtc::ArrayView<const float> x,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200419 const std::array<float, kBlockSize>& s,
420 bool converged_filter) {
peah29103572017-07-11 02:54:02 -0700421 auto result_x = std::minmax_element(x.begin(), x.end());
422 auto result_s = std::minmax_element(s.begin(), s.end());
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100423 const float x_abs = std::max(fabsf(*result_x.first), fabsf(*result_x.second));
424 const float s_abs = std::max(fabsf(*result_s.first), fabsf(*result_s.second));
peah29103572017-07-11 02:54:02 -0700425
Per Åhgren1b4059e2017-10-15 20:19:21 +0200426 if (converged_filter) {
427 if (x_abs < 20.f) {
428 ++low_farend_counter_;
429 } else {
430 low_farend_counter_ = 0;
431 }
peah29103572017-07-11 02:54:02 -0700432 } else {
Per Åhgren1b4059e2017-10-15 20:19:21 +0200433 if (x_abs < 100.f) {
434 ++low_farend_counter_;
435 } else {
436 low_farend_counter_ = 0;
437 }
peah29103572017-07-11 02:54:02 -0700438 }
439
440 // The echo is deemed as not audible if the echo estimate is on the level of
441 // the quantization noise in the FFTs and the nearend level is sufficiently
442 // strong to mask that by ensuring that the playout and AGC gains do not boost
443 // any residual echo that is below the quantization noise level. Furthermore,
444 // cases where the render signal is very close to zero are also identified as
445 // not producing audible echo.
Per Åhgren1b4059e2017-10-15 20:19:21 +0200446 inaudible_echo_ = (max_nearend_ > 500 && s_abs < 30.f) ||
447 (!converged_filter && x_abs < 500);
peah29103572017-07-11 02:54:02 -0700448 inaudible_echo_ = inaudible_echo_ || low_farend_counter_ > 20;
449}
450
451void AecState::EchoAudibility::UpdateWithOutput(rtc::ArrayView<const float> e) {
452 const float e_max = *std::max_element(e.begin(), e.end());
453 const float e_min = *std::min_element(e.begin(), e.end());
Raphael Kubo da Costa07438142017-10-16 17:00:02 +0200454 const float e_abs = std::max(fabsf(e_max), fabsf(e_min));
peah29103572017-07-11 02:54:02 -0700455
456 if (max_nearend_ < e_abs) {
457 max_nearend_ = e_abs;
458 max_nearend_counter_ = 0;
459 } else {
460 if (++max_nearend_counter_ > 5 * kNumBlocksPerSecond) {
461 max_nearend_ *= 0.995f;
462 }
463 }
peah522d71b2017-02-23 05:16:26 -0800464}
465
466} // namespace webrtc