peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/aec_state.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 12 | |
| 13 | #include <math.h> |
Raphael Kubo da Costa | 0743814 | 2017-10-16 17:00:02 +0200 | [diff] [blame] | 14 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 15 | #include <numeric> |
| 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #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" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace { |
| 25 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 26 | // Computes delay of the adaptive filter. |
Per Åhgren | 40659c3 | 2017-10-17 12:56:21 +0200 | [diff] [blame] | 27 | int EstimateFilterDelay( |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 28 | const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 29 | adaptive_filter_frequency_response) { |
| 30 | const auto& H2 = adaptive_filter_frequency_response; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 31 | constexpr size_t kUpperBin = kFftLengthBy2 - 5; |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 32 | RTC_DCHECK_GE(kMaxAdaptiveFilterLength, H2.size()); |
| 33 | std::array<int, kMaxAdaptiveFilterLength> delays; |
Per Åhgren | 40659c3 | 2017-10-17 12:56:21 +0200 | [diff] [blame] | 34 | delays.fill(0); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 35 | for (size_t k = 1; k < kUpperBin; ++k) { |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 36 | // Find the maximum of H2[j]. |
Per Åhgren | 40659c3 | 2017-10-17 12:56:21 +0200 | [diff] [blame] | 37 | size_t peak = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 38 | for (size_t j = 0; j < H2.size(); ++j) { |
| 39 | if (H2[j][k] > H2[peak][k]) { |
| 40 | peak = j; |
| 41 | } |
| 42 | } |
Per Åhgren | 40659c3 | 2017-10-17 12:56:21 +0200 | [diff] [blame] | 43 | ++delays[peak]; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 44 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 45 | |
Per Åhgren | 40659c3 | 2017-10-17 12:56:21 +0200 | [diff] [blame] | 46 | return std::distance(delays.begin(), |
| 47 | std::max_element(delays.begin(), delays.end())); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 50 | float 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 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | } // namespace |
| 56 | |
| 57 | int AecState::instance_count_ = 0; |
| 58 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 59 | AecState::AecState(const EchoCanceller3Config& config) |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 60 | : data_dumper_( |
| 61 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 62 | erle_estimator_(config.erle.min, config.erle.max_l, config.erle.max_h), |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 63 | config_(config), |
Per Åhgren | 08ea589 | 2018-01-15 08:07:41 +0100 | [diff] [blame] | 64 | max_render_(config_.filter.main.length_blocks, 0.f), |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 65 | reverb_decay_(fabsf(config_.ep_strength.default_len)), |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 66 | gain_rampup_increase_(ComputeGainRampupIncrease(config_)) {} |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 67 | |
| 68 | AecState::~AecState() = default; |
| 69 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 70 | void AecState::HandleEchoPathChange( |
| 71 | const EchoPathVariability& echo_path_variability) { |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 72 | const auto full_reset = [&]() { |
Per Åhgren | 63b494d | 2017-12-06 11:32:38 +0100 | [diff] [blame] | 73 | blocks_since_last_saturation_ = 0; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 74 | usable_linear_estimate_ = false; |
| 75 | echo_leakage_detected_ = false; |
| 76 | capture_signal_saturation_ = false; |
| 77 | echo_saturation_ = false; |
Per Åhgren | 63b494d | 2017-12-06 11:32:38 +0100 | [diff] [blame] | 78 | previous_max_sample_ = 0.f; |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 79 | std::fill(max_render_.begin(), max_render_.end(), 0.f); |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 80 | blocks_with_proper_filter_adaptation_ = 0; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 81 | capture_block_counter_ = 0; |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 82 | filter_has_had_time_to_converge_ = false; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 83 | render_received_ = false; |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 84 | blocks_with_active_render_ = 0; |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 85 | initial_state_ = true; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 86 | }; |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 87 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 88 | // 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 Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 99 | active_render_seen_ = false; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 100 | full_reset(); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 101 | } 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; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 112 | void AecState::Update( |
Per Åhgren | 3ab308f | 2018-02-21 08:46:03 +0100 | [diff] [blame] | 113 | const rtc::Optional<DelayEstimate>& delay_estimate, |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 114 | 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 Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 118 | const RenderBuffer& render_buffer, |
| 119 | const std::array<float, kFftLengthBy2Plus1>& E2_main, |
| 120 | const std::array<float, kFftLengthBy2Plus1>& Y2, |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 121 | const std::array<float, kBlockSize>& s, |
| 122 | bool echo_leakage_detected) { |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 123 | // Store input parameters. |
| 124 | echo_leakage_detected_ = echo_leakage_detected; |
| 125 | |
Per Åhgren | 0e6d2f5 | 2017-12-20 22:19:56 +0100 | [diff] [blame] | 126 | // 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 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 130 | // Update counters. |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 131 | ++capture_block_counter_; |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 132 | 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; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 136 | |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 137 | // 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 Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 140 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 141 | // Update the ERL and ERLE measures. |
Per Åhgren | 40659c3 | 2017-10-17 12:56:21 +0200 | [diff] [blame] | 142 | if (converged_filter && capture_block_counter_ >= 2 * kNumBlocksPerSecond) { |
Per Åhgren | 0e6d2f5 | 2017-12-20 22:19:56 +0100 | [diff] [blame] | 143 | const auto& X2 = render_buffer.Spectrum(filter_delay_); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 144 | erle_estimator_.Update(X2, Y2, E2_main); |
| 145 | erl_estimator_.Update(X2, Y2); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 146 | } |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 147 | |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 148 | // Update the echo audibility evaluator. |
| 149 | echo_audibility_.Update(x, s, converged_filter); |
| 150 | |
Per Åhgren | 63b494d | 2017-12-06 11:32:38 +0100 | [diff] [blame] | 151 | // 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 Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 154 | if (config_.ep_strength.echo_can_saturate) { |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 155 | echo_saturation_ = DetectEchoSaturation(x); |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 156 | } |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 157 | |
Per Åhgren | 63b494d | 2017-12-06 11:32:38 +0100 | [diff] [blame] | 158 | // TODO(peah): Move? |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 159 | filter_has_had_time_to_converge_ = |
Per Åhgren | 29f1432 | 2018-02-06 15:31:57 +0100 | [diff] [blame] | 160 | blocks_with_proper_filter_adaptation_ >= 1.5f * kNumBlocksPerSecond; |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 161 | |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 162 | initial_state_ = |
| 163 | blocks_with_proper_filter_adaptation_ < 5 * kNumBlocksPerSecond; |
| 164 | |
Per Åhgren | 63b494d | 2017-12-06 11:32:38 +0100 | [diff] [blame] | 165 | // Flag whether the linear filter estimate is usable. |
| 166 | usable_linear_estimate_ = |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 167 | !echo_saturation_ && |
Per Åhgren | 3f1c062 | 2018-01-15 13:22:43 +0100 | [diff] [blame] | 168 | (converged_filter && filter_has_had_time_to_converge_) && |
Per Åhgren | 0eef9c0 | 2018-01-22 20:24:06 +0100 | [diff] [blame] | 169 | capture_block_counter_ >= 1.f * kNumBlocksPerSecond && !TransparentMode(); |
Per Åhgren | 63b494d | 2017-12-06 11:32:38 +0100 | [diff] [blame] | 170 | |
| 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 Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 174 | transparent_mode_ = |
| 175 | !converged_filter && |
| 176 | (blocks_with_active_render_ == 0 || |
| 177 | blocks_with_proper_filter_adaptation_ >= 5 * kNumBlocksPerSecond); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 180 | void AecState::UpdateReverb(const std::vector<float>& impulse_response) { |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 181 | // 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 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 186 | if ((!(filter_delay_ && usable_linear_estimate_)) || |
Per Åhgren | 08ea589 | 2018-01-15 08:07:41 +0100 | [diff] [blame] | 187 | (filter_delay_ > |
| 188 | static_cast<int>(config_.filter.main.length_blocks) - 4)) { |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 192 | constexpr float kOneByFftLengthBy2 = 1.f / kFftLengthBy2; |
| 193 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 194 | // Form the data to match against by squaring the impulse response |
| 195 | // coefficients. |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 196 | std::array<float, GetTimeDomainLength(kMaxAdaptiveFilterLength)> |
| 197 | matching_data_data; |
Per Åhgren | 08ea589 | 2018-01-15 08:07:41 +0100 | [diff] [blame] | 198 | RTC_DCHECK_LE(GetTimeDomainLength(config_.filter.main.length_blocks), |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 199 | matching_data_data.size()); |
| 200 | rtc::ArrayView<float> matching_data( |
| 201 | matching_data_data.data(), |
Per Åhgren | 08ea589 | 2018-01-15 08:07:41 +0100 | [diff] [blame] | 202 | GetTimeDomainLength(config_.filter.main.length_blocks)); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 203 | std::transform(impulse_response.begin(), impulse_response.end(), |
| 204 | matching_data.begin(), [](float a) { return a * a; }); |
| 205 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 206 | if (current_reverb_decay_section_ < config_.filter.main.length_blocks) { |
| 207 | // Update accumulated variables for the current filter section. |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 208 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 209 | const size_t start_index = current_reverb_decay_section_ * kFftLengthBy2; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 210 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 211 | 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_; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 264 | } |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 267 | 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); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 286 | } |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 287 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 288 | // Filter tail energy (assumed to be noise). |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 289 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 290 | 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; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 294 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 295 | 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; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 307 | } |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 308 | |
| 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; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 359 | } |
| 360 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 361 | data_dumper_->DumpRaw("aec3_reverb_decay", reverb_decay_); |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 362 | data_dumper_->DumpRaw("aec3_reverb_tail_energy", tail_energy_); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 365 | bool 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 Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 372 | // Updates the suppressor gain limit. |
| 373 | void 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 Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 403 | bool 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 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 418 | void AecState::EchoAudibility::Update(rtc::ArrayView<const float> x, |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 419 | const std::array<float, kBlockSize>& s, |
| 420 | bool converged_filter) { |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 421 | auto result_x = std::minmax_element(x.begin(), x.end()); |
| 422 | auto result_s = std::minmax_element(s.begin(), s.end()); |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame^] | 423 | 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)); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 425 | |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 426 | if (converged_filter) { |
| 427 | if (x_abs < 20.f) { |
| 428 | ++low_farend_counter_; |
| 429 | } else { |
| 430 | low_farend_counter_ = 0; |
| 431 | } |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 432 | } else { |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 433 | if (x_abs < 100.f) { |
| 434 | ++low_farend_counter_; |
| 435 | } else { |
| 436 | low_farend_counter_ = 0; |
| 437 | } |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 438 | } |
| 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 Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 446 | inaudible_echo_ = (max_nearend_ > 500 && s_abs < 30.f) || |
| 447 | (!converged_filter && x_abs < 500); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 448 | inaudible_echo_ = inaudible_echo_ || low_farend_counter_ > 20; |
| 449 | } |
| 450 | |
| 451 | void 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 Costa | 0743814 | 2017-10-16 17:00:02 +0200 | [diff] [blame] | 454 | const float e_abs = std::max(fabsf(e_max), fabsf(e_min)); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 455 | |
| 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 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | } // namespace webrtc |