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