peah | 69221db | 2017-01-27 03:28:19 -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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "modules/audio_processing/aec3/echo_remover.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 11 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 12 | #include <math.h> |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 13 | #include <algorithm> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <numeric> |
| 16 | #include <string> |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
| 19 | #include "modules/audio_processing/aec3/aec3_common.h" |
| 20 | #include "modules/audio_processing/aec3/aec_state.h" |
| 21 | #include "modules/audio_processing/aec3/comfort_noise_generator.h" |
| 22 | #include "modules/audio_processing/aec3/echo_path_variability.h" |
| 23 | #include "modules/audio_processing/aec3/echo_remover_metrics.h" |
| 24 | #include "modules/audio_processing/aec3/fft_data.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_processing/aec3/render_buffer.h" |
| 26 | #include "modules/audio_processing/aec3/render_delay_buffer.h" |
| 27 | #include "modules/audio_processing/aec3/residual_echo_estimator.h" |
| 28 | #include "modules/audio_processing/aec3/subtractor.h" |
| 29 | #include "modules/audio_processing/aec3/suppression_filter.h" |
| 30 | #include "modules/audio_processing/aec3/suppression_gain.h" |
| 31 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
| 32 | #include "rtc_base/atomicops.h" |
| 33 | #include "rtc_base/constructormagic.h" |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 34 | #include "rtc_base/logging.h" |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 35 | #include "system_wrappers/include/field_trial.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 36 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | namespace { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 40 | |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 41 | bool UseShadowFilterOutput() { |
| 42 | return !field_trial::IsEnabled( |
| 43 | "WebRTC-Aec3UtilizeShadowFilterOutputKillSwitch"); |
| 44 | } |
| 45 | |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 46 | bool UseSmoothSignalTransitions() { |
| 47 | return !field_trial::IsEnabled( |
| 48 | "WebRTC-Aec3SmoothSignalTransitionsKillSwitch"); |
| 49 | } |
| 50 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 51 | bool EnableBoundedNearend() { |
| 52 | return !field_trial::IsEnabled("WebRTC-Aec3BoundedNearendKillSwitch"); |
| 53 | } |
| 54 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | void LinearEchoPower(const FftData& E, |
| 56 | const FftData& Y, |
| 57 | std::array<float, kFftLengthBy2Plus1>* S2) { |
| 58 | for (size_t k = 0; k < E.re.size(); ++k) { |
| 59 | (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) + |
| 60 | (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]); |
| 61 | } |
| 62 | } |
| 63 | |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 64 | // Fades between two input signals using a fix-sized transition. |
| 65 | void SignalTransition(rtc::ArrayView<const float> from, |
| 66 | rtc::ArrayView<const float> to, |
| 67 | rtc::ArrayView<float> out) { |
| 68 | constexpr size_t kTransitionSize = 30; |
Gustaf Ullberg | ddb82a6 | 2018-09-11 12:55:23 +0200 | [diff] [blame] | 69 | constexpr float kOneByTransitionSizePlusOne = 1.f / (kTransitionSize + 1); |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 70 | |
| 71 | RTC_DCHECK_EQ(from.size(), to.size()); |
| 72 | RTC_DCHECK_EQ(from.size(), out.size()); |
| 73 | RTC_DCHECK_LE(kTransitionSize, out.size()); |
| 74 | |
| 75 | for (size_t k = 0; k < kTransitionSize; ++k) { |
Gustaf Ullberg | ddb82a6 | 2018-09-11 12:55:23 +0200 | [diff] [blame] | 76 | float a = (k + 1) * kOneByTransitionSizePlusOne; |
| 77 | out[k] = a * to[k] + (1.f - a) * from[k]; |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | std::copy(to.begin() + kTransitionSize, to.end(), |
| 81 | out.begin() + kTransitionSize); |
| 82 | } |
| 83 | |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 84 | // Computes a windowed (square root Hanning) padded FFT and updates the related |
| 85 | // memory. |
| 86 | void WindowedPaddedFft(const Aec3Fft& fft, |
| 87 | rtc::ArrayView<const float> v, |
| 88 | rtc::ArrayView<float> v_old, |
| 89 | FftData* V) { |
| 90 | fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V); |
| 91 | std::copy(v.begin(), v.end(), v_old.begin()); |
| 92 | } |
| 93 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 94 | // Class for removing the echo from the capture signal. |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 95 | class EchoRemoverImpl final : public EchoRemover { |
| 96 | public: |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 97 | EchoRemoverImpl(const EchoCanceller3Config& config, int sample_rate_hz); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 98 | ~EchoRemoverImpl() override; |
| 99 | |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 100 | void GetMetrics(EchoControl::Metrics* metrics) const override; |
| 101 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 102 | // Removes the echo from a block of samples from the capture signal. The |
| 103 | // supplied render signal is assumed to be pre-aligned with the capture |
| 104 | // signal. |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 105 | void ProcessCapture(EchoPathVariability echo_path_variability, |
Alex Loiko | 890988c | 2017-08-31 10:25:48 +0200 | [diff] [blame] | 106 | bool capture_signal_saturation, |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 107 | const absl::optional<DelayEstimate>& external_delay, |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 108 | RenderBuffer* render_buffer, |
Alex Loiko | 890988c | 2017-08-31 10:25:48 +0200 | [diff] [blame] | 109 | std::vector<std::vector<float>>* capture) override; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 110 | |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 111 | // Returns the internal delay estimate in blocks. |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 112 | absl::optional<int> Delay() const override { |
Per Åhgren | e05c43c | 2018-05-09 12:26:51 +0200 | [diff] [blame] | 113 | // TODO(peah): Remove or reactivate this functionality. |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 114 | return absl::nullopt; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 115 | } |
| 116 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 117 | // Updates the status on whether echo leakage is detected in the output of the |
| 118 | // echo remover. |
| 119 | void UpdateEchoLeakageStatus(bool leakage_detected) override { |
| 120 | echo_leakage_detected_ = leakage_detected; |
| 121 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 122 | |
| 123 | private: |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 124 | // Selects which of the shadow and main linear filter outputs that is most |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 125 | // appropriate to pass to the suppressor and forms the linear filter output by |
| 126 | // smoothly transition between those. |
| 127 | void FormLinearFilterOutput(bool smooth_transition, |
| 128 | const SubtractorOutput& subtractor_output, |
| 129 | rtc::ArrayView<float> output); |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 130 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 131 | static int instance_count_; |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 132 | const EchoCanceller3Config config_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 133 | const Aec3Fft fft_; |
| 134 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 135 | const Aec3Optimization optimization_; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 136 | const int sample_rate_hz_; |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 137 | const bool use_shadow_filter_output_; |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 138 | const bool use_smooth_signal_transitions_; |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 139 | const bool enable_bounded_nearend_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 140 | Subtractor subtractor_; |
| 141 | SuppressionGain suppression_gain_; |
| 142 | ComfortNoiseGenerator cng_; |
| 143 | SuppressionFilter suppression_filter_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 144 | RenderSignalAnalyzer render_signal_analyzer_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 145 | ResidualEchoEstimator residual_echo_estimator_; |
| 146 | bool echo_leakage_detected_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 147 | AecState aec_state_; |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 148 | EchoRemoverMetrics metrics_; |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 149 | std::array<float, kFftLengthBy2> e_old_; |
| 150 | std::array<float, kFftLengthBy2> x_old_; |
| 151 | std::array<float, kFftLengthBy2> y_old_; |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 152 | size_t block_counter_ = 0; |
| 153 | int gain_change_hangover_ = 0; |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 154 | bool main_filter_output_last_selected_ = true; |
| 155 | bool linear_filter_output_last_selected_ = true; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 156 | |
| 157 | RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl); |
| 158 | }; |
| 159 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 160 | int EchoRemoverImpl::instance_count_ = 0; |
| 161 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 162 | EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config, |
| 163 | int sample_rate_hz) |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 164 | : config_(config), |
| 165 | fft_(), |
aleloi | 88b82b5 | 2017-02-23 06:27:03 -0800 | [diff] [blame] | 166 | data_dumper_( |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 167 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 168 | optimization_(DetectOptimization()), |
| 169 | sample_rate_hz_(sample_rate_hz), |
Per Åhgren | 2402154 | 2018-08-31 07:34:29 +0200 | [diff] [blame] | 170 | use_shadow_filter_output_( |
| 171 | UseShadowFilterOutput() && |
| 172 | config_.filter.enable_shadow_filter_output_usage), |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 173 | use_smooth_signal_transitions_(UseSmoothSignalTransitions()), |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 174 | enable_bounded_nearend_(EnableBoundedNearend()), |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 175 | subtractor_(config, data_dumper_.get(), optimization_), |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 176 | suppression_gain_(config_, optimization_, sample_rate_hz), |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 177 | cng_(optimization_), |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 178 | suppression_filter_(sample_rate_hz_), |
Per Åhgren | 971de07 | 2018-03-14 23:23:47 +0100 | [diff] [blame] | 179 | render_signal_analyzer_(config_), |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 180 | residual_echo_estimator_(config_), |
| 181 | aec_state_(config_) { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 182 | RTC_DCHECK(ValidFullBandRate(sample_rate_hz)); |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 183 | x_old_.fill(0.f); |
| 184 | y_old_.fill(0.f); |
| 185 | e_old_.fill(0.f); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | EchoRemoverImpl::~EchoRemoverImpl() = default; |
| 189 | |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 190 | void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const { |
| 191 | // Echo return loss (ERL) is inverted to go from gain to attenuation. |
| 192 | metrics->echo_return_loss = -10.0 * log10(aec_state_.ErlTimeDomain()); |
| 193 | metrics->echo_return_loss_enhancement = |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame] | 194 | Log2TodB(aec_state_.ErleTimeDomainLog2()); |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 195 | } |
| 196 | |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 197 | void EchoRemoverImpl::ProcessCapture( |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 198 | EchoPathVariability echo_path_variability, |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 199 | bool capture_signal_saturation, |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 200 | const absl::optional<DelayEstimate>& external_delay, |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 201 | RenderBuffer* render_buffer, |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 202 | std::vector<std::vector<float>>* capture) { |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 203 | ++block_counter_; |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 204 | const std::vector<std::vector<float>>& x = render_buffer->Block(0); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 205 | std::vector<std::vector<float>>* y = capture; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 206 | RTC_DCHECK(render_buffer); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 207 | RTC_DCHECK(y); |
| 208 | RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_)); |
| 209 | RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_)); |
| 210 | RTC_DCHECK_EQ(x[0].size(), kBlockSize); |
| 211 | RTC_DCHECK_EQ((*y)[0].size(), kBlockSize); |
| 212 | const std::vector<float>& x0 = x[0]; |
| 213 | std::vector<float>& y0 = (*y)[0]; |
| 214 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 215 | data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0], |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 216 | LowestBandRate(sample_rate_hz_), 1); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 217 | data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0], |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 218 | LowestBandRate(sample_rate_hz_), 1); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 219 | data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0); |
| 220 | data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 221 | |
| 222 | aec_state_.UpdateCaptureSaturation(capture_signal_saturation); |
| 223 | |
| 224 | if (echo_path_variability.AudioPathChanged()) { |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 225 | // Ensure that the gain change is only acted on once per frame. |
| 226 | if (echo_path_variability.gain_change) { |
| 227 | if (gain_change_hangover_ == 0) { |
| 228 | constexpr int kMaxBlocksPerFrame = 3; |
| 229 | gain_change_hangover_ = kMaxBlocksPerFrame; |
| 230 | RTC_LOG(LS_WARNING) |
| 231 | << "Gain change detected at block " << block_counter_; |
| 232 | } else { |
| 233 | echo_path_variability.gain_change = false; |
| 234 | } |
| 235 | } |
| 236 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 237 | subtractor_.HandleEchoPathChange(echo_path_variability); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 238 | aec_state_.HandleEchoPathChange(echo_path_variability); |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 239 | |
| 240 | if (echo_path_variability.delay_change != |
| 241 | EchoPathVariability::DelayAdjustment::kNone) { |
| 242 | suppression_gain_.SetInitialState(true); |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | if (gain_change_hangover_ > 0) { |
| 246 | --gain_change_hangover_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | std::array<float, kFftLengthBy2Plus1> Y2; |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 250 | std::array<float, kFftLengthBy2Plus1> E2; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 251 | std::array<float, kFftLengthBy2Plus1> R2; |
| 252 | std::array<float, kFftLengthBy2Plus1> S2_linear; |
| 253 | std::array<float, kFftLengthBy2Plus1> G; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 254 | float high_bands_gain; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 255 | FftData Y; |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 256 | FftData E; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 257 | FftData comfort_noise; |
| 258 | FftData high_band_comfort_noise; |
| 259 | SubtractorOutput subtractor_output; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 260 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 261 | // Analyze the render signal. |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 262 | render_signal_analyzer_.Update(*render_buffer, |
| 263 | aec_state_.FilterDelayBlocks()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 264 | |
| 265 | // Perform linear echo cancellation. |
Jesús de Vicente Peña | 02e9e44 | 2018-08-29 13:34:07 +0200 | [diff] [blame] | 266 | if (aec_state_.TransitionTriggered()) { |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 267 | subtractor_.ExitInitialState(); |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 268 | suppression_gain_.SetInitialState(false); |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 269 | } |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 270 | |
| 271 | // If the delay is known, use the echo subtractor. |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 272 | subtractor_.Process(*render_buffer, y0, render_signal_analyzer_, aec_state_, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 273 | &subtractor_output); |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 274 | std::array<float, kBlockSize> e; |
| 275 | FormLinearFilterOutput(use_smooth_signal_transitions_, subtractor_output, e); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 276 | |
| 277 | // Compute spectra. |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 278 | WindowedPaddedFft(fft_, y0, y_old_, &Y); |
| 279 | WindowedPaddedFft(fft_, e, e_old_, &E); |
| 280 | LinearEchoPower(E, Y, &S2_linear); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 281 | Y.Spectrum(optimization_, Y2); |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 282 | E.Spectrum(optimization_, E2); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 283 | |
| 284 | // Update the AEC state information. |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 285 | aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponse(), |
Per Åhgren | b20b937 | 2018-07-13 00:22:54 +0200 | [diff] [blame] | 286 | subtractor_.FilterImpulseResponse(), *render_buffer, E2, Y2, |
| 287 | subtractor_output, y0); |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 288 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 289 | // Choose the linear output. |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 290 | data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0], |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 291 | LowestBandRate(sample_rate_hz_), 1); |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 292 | if (aec_state_.UseLinearFilterOutput()) { |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 293 | if (!linear_filter_output_last_selected_ && |
| 294 | use_smooth_signal_transitions_) { |
| 295 | SignalTransition(y0, e, y0); |
| 296 | } else { |
| 297 | std::copy(e.begin(), e.end(), y0.begin()); |
| 298 | } |
| 299 | } else { |
| 300 | if (linear_filter_output_last_selected_ && use_smooth_signal_transitions_) { |
| 301 | SignalTransition(e, y0, y0); |
| 302 | } |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 303 | } |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 304 | linear_filter_output_last_selected_ = aec_state_.UseLinearFilterOutput(); |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 305 | const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y; |
| 306 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 307 | data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0], |
| 308 | LowestBandRate(sample_rate_hz_), 1); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 309 | |
| 310 | // Estimate the residual echo power. |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 311 | residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 312 | &R2); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 313 | |
| 314 | // Estimate the comfort noise. |
| 315 | cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise); |
| 316 | |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 317 | // Compute and apply the suppression gain. |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 318 | const auto& echo_spectrum = |
| 319 | aec_state_.UsableLinearEstimate() ? S2_linear : R2; |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 320 | |
| 321 | std::array<float, kFftLengthBy2Plus1> E2_bounded; |
| 322 | if (enable_bounded_nearend_) { |
| 323 | std::transform(E2.begin(), E2.end(), Y2.begin(), E2_bounded.begin(), |
| 324 | [](float a, float b) { return std::min(a, b); }); |
| 325 | } else { |
| 326 | std::copy(E2.begin(), E2.end(), E2_bounded.begin()); |
| 327 | } |
| 328 | |
| 329 | suppression_gain_.GetGain(E2, E2_bounded, echo_spectrum, R2, |
| 330 | cng_.NoiseSpectrum(), E, Y, render_signal_analyzer_, |
| 331 | aec_state_, x, &high_bands_gain, &G); |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 332 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 333 | suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 334 | high_bands_gain, Y_fft, y); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 335 | |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 336 | // Update the metrics. |
| 337 | metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G); |
| 338 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 339 | // Debug outputs for the purpose of development and analysis. |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 340 | data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize, |
| 341 | &subtractor_output.s_main[0], |
| 342 | LowestBandRate(sample_rate_hz_), 1); |
| 343 | data_dumper_->DumpRaw("aec3_output", y0); |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 344 | data_dumper_->DumpRaw("aec3_narrow_render", |
| 345 | render_signal_analyzer_.NarrowPeakBand() ? 1 : 0); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 346 | data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum()); |
| 347 | data_dumper_->DumpRaw("aec3_suppressor_gain", G); |
| 348 | data_dumper_->DumpWav("aec3_output", |
| 349 | rtc::ArrayView<const float>(&y0[0], kBlockSize), |
| 350 | LowestBandRate(sample_rate_hz_), 1); |
| 351 | data_dumper_->DumpRaw("aec3_using_subtractor_output", |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 352 | aec_state_.UseLinearFilterOutput() ? 1 : 0); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 353 | data_dumper_->DumpRaw("aec3_E2", E2); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 354 | data_dumper_->DumpRaw("aec3_S2_linear", S2_linear); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 355 | data_dumper_->DumpRaw("aec3_Y2", Y2); |
Jesús de Vicente Peña | 7682c6e | 2018-03-22 14:53:23 +0100 | [diff] [blame] | 356 | data_dumper_->DumpRaw( |
| 357 | "aec3_X2", render_buffer->Spectrum(aec_state_.FilterDelayBlocks())); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 358 | data_dumper_->DumpRaw("aec3_R2", R2); |
Jesús de Vicente Peña | 075cb2b | 2018-06-13 15:13:55 +0200 | [diff] [blame] | 359 | data_dumper_->DumpRaw("aec3_R2_reverb", |
| 360 | residual_echo_estimator_.GetReverbPowerSpectrum()); |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 361 | data_dumper_->DumpRaw("aec3_filter_delay", aec_state_.FilterDelayBlocks()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 362 | data_dumper_->DumpRaw("aec3_capture_saturation", |
| 363 | aec_state_.SaturatedCapture() ? 1 : 0); |
| 364 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 365 | |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 366 | void EchoRemoverImpl::FormLinearFilterOutput( |
| 367 | bool smooth_transition, |
| 368 | const SubtractorOutput& subtractor_output, |
| 369 | rtc::ArrayView<float> output) { |
| 370 | RTC_DCHECK_EQ(subtractor_output.e_main.size(), output.size()); |
| 371 | RTC_DCHECK_EQ(subtractor_output.e_shadow.size(), output.size()); |
| 372 | bool use_main_output = true; |
| 373 | if (use_shadow_filter_output_) { |
Jesús de Vicente Peña | 02e9e44 | 2018-08-29 13:34:07 +0200 | [diff] [blame] | 374 | // As the output of the main adaptive filter generally should be better |
| 375 | // than the shadow filter output, add a margin and threshold for when |
| 376 | // choosing the shadow filter output. |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 377 | if (subtractor_output.e2_shadow < 0.9f * subtractor_output.e2_main && |
| 378 | subtractor_output.y2 > 30.f * 30.f * kBlockSize && |
| 379 | (subtractor_output.s2_main > 60.f * 60.f * kBlockSize || |
| 380 | subtractor_output.s2_shadow > 60.f * 60.f * kBlockSize)) { |
| 381 | use_main_output = false; |
| 382 | } else { |
| 383 | // If the main filter is diverged, choose the filter output that has the |
| 384 | // lowest power. |
| 385 | if (subtractor_output.e2_shadow < subtractor_output.e2_main && |
| 386 | subtractor_output.y2 < subtractor_output.e2_main) { |
| 387 | use_main_output = false; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (use_main_output) { |
| 393 | if (!main_filter_output_last_selected_ && smooth_transition) { |
| 394 | SignalTransition(subtractor_output.e_shadow, subtractor_output.e_main, |
| 395 | output); |
| 396 | } else { |
| 397 | std::copy(subtractor_output.e_main.begin(), |
| 398 | subtractor_output.e_main.end(), output.begin()); |
| 399 | } |
| 400 | } else { |
| 401 | if (main_filter_output_last_selected_ && smooth_transition) { |
| 402 | SignalTransition(subtractor_output.e_main, subtractor_output.e_shadow, |
| 403 | output); |
| 404 | } else { |
| 405 | std::copy(subtractor_output.e_shadow.begin(), |
| 406 | subtractor_output.e_shadow.end(), output.begin()); |
| 407 | } |
| 408 | } |
| 409 | main_filter_output_last_selected_ = use_main_output; |
| 410 | } |
| 411 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 412 | } // namespace |
| 413 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 414 | EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config, |
| 415 | int sample_rate_hz) { |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 416 | return new EchoRemoverImpl(config, sample_rate_hz); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | } // namespace webrtc |