blob: 96887fe38bb2240af112ac593bad7e867f4dbca6 [file] [log] [blame]
peah69221db2017-01-27 03:28:19 -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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/audio_processing/aec3/echo_remover.h"
peah69221db2017-01-27 03:28:19 -080011
peah86afe9d2017-04-06 15:45:32 -070012#include <math.h>
peah69221db2017-01-27 03:28:19 -080013#include <algorithm>
peah522d71b2017-02-23 05:16:26 -080014#include <memory>
15#include <numeric>
16#include <string>
peah69221db2017-01-27 03:28:19 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#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 Bonadei92ea95e2017-09-15 06:47:31 +020025#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"
peah69221db2017-01-27 03:28:19 -080034
35namespace webrtc {
36
37namespace {
peah522d71b2017-02-23 05:16:26 -080038
39void LinearEchoPower(const FftData& E,
40 const FftData& Y,
41 std::array<float, kFftLengthBy2Plus1>* S2) {
42 for (size_t k = 0; k < E.re.size(); ++k) {
43 (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) +
44 (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]);
45 }
46}
47
Per Åhgren169c7fd2018-04-27 12:04:03 +020048// Computes a windowed (square root Hanning) padded FFT and updates the related
49// memory.
50void WindowedPaddedFft(const Aec3Fft& fft,
51 rtc::ArrayView<const float> v,
52 rtc::ArrayView<float> v_old,
53 FftData* V) {
54 fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V);
55 std::copy(v.begin(), v.end(), v_old.begin());
56}
57
peah522d71b2017-02-23 05:16:26 -080058// Class for removing the echo from the capture signal.
peah69221db2017-01-27 03:28:19 -080059class EchoRemoverImpl final : public EchoRemover {
60 public:
Per Åhgren5c532d32018-03-22 00:29:25 +010061 EchoRemoverImpl(const EchoCanceller3Config& config, int sample_rate_hz);
peah69221db2017-01-27 03:28:19 -080062 ~EchoRemoverImpl() override;
63
Gustaf Ullberg332150d2017-11-22 14:17:39 +010064 void GetMetrics(EchoControl::Metrics* metrics) const override;
65
peah522d71b2017-02-23 05:16:26 -080066 // Removes the echo from a block of samples from the capture signal. The
67 // supplied render signal is assumed to be pre-aligned with the capture
68 // signal.
Per Åhgrende22a172017-12-20 18:00:51 +010069 void ProcessCapture(const EchoPathVariability& echo_path_variability,
Alex Loiko890988c2017-08-31 10:25:48 +020070 bool capture_signal_saturation,
Per Åhgren5c532d32018-03-22 00:29:25 +010071 const rtc::Optional<DelayEstimate>& external_delay,
Per Åhgrenc59a5762017-12-11 21:34:19 +010072 RenderBuffer* render_buffer,
Alex Loiko890988c2017-08-31 10:25:48 +020073 std::vector<std::vector<float>>* capture) override;
peah69221db2017-01-27 03:28:19 -080074
Per Åhgren5c532d32018-03-22 00:29:25 +010075 // Returns the internal delay estimate in blocks.
76 rtc::Optional<int> Delay() const override {
77 return aec_state_.InternalDelay();
78 }
79
peah522d71b2017-02-23 05:16:26 -080080 // Updates the status on whether echo leakage is detected in the output of the
81 // echo remover.
82 void UpdateEchoLeakageStatus(bool leakage_detected) override {
83 echo_leakage_detected_ = leakage_detected;
84 }
peah69221db2017-01-27 03:28:19 -080085
86 private:
peah522d71b2017-02-23 05:16:26 -080087 static int instance_count_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020088 const EchoCanceller3Config config_;
peah522d71b2017-02-23 05:16:26 -080089 const Aec3Fft fft_;
90 std::unique_ptr<ApmDataDumper> data_dumper_;
91 const Aec3Optimization optimization_;
peah69221db2017-01-27 03:28:19 -080092 const int sample_rate_hz_;
peah522d71b2017-02-23 05:16:26 -080093 Subtractor subtractor_;
94 SuppressionGain suppression_gain_;
95 ComfortNoiseGenerator cng_;
96 SuppressionFilter suppression_filter_;
peah522d71b2017-02-23 05:16:26 -080097 RenderSignalAnalyzer render_signal_analyzer_;
peah522d71b2017-02-23 05:16:26 -080098 ResidualEchoEstimator residual_echo_estimator_;
99 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800100 AecState aec_state_;
peahe985b3f2017-02-28 22:08:53 -0800101 EchoRemoverMetrics metrics_;
Per Åhgrena98c8072018-01-15 19:17:16 +0100102 bool initial_state_ = true;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200103 std::array<float, kFftLengthBy2> e_old_;
104 std::array<float, kFftLengthBy2> x_old_;
105 std::array<float, kFftLengthBy2> y_old_;
peah69221db2017-01-27 03:28:19 -0800106
107 RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl);
108};
109
peah522d71b2017-02-23 05:16:26 -0800110int EchoRemoverImpl::instance_count_ = 0;
111
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200112EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config,
113 int sample_rate_hz)
peah8cee56f2017-08-24 22:36:53 -0700114 : config_(config),
115 fft_(),
aleloi88b82b52017-02-23 06:27:03 -0800116 data_dumper_(
peah522d71b2017-02-23 05:16:26 -0800117 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
118 optimization_(DetectOptimization()),
119 sample_rate_hz_(sample_rate_hz),
Per Åhgren09a718a2017-12-11 22:28:45 +0100120 subtractor_(config, data_dumper_.get(), optimization_),
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200121 suppression_gain_(config_, optimization_, sample_rate_hz),
peah522d71b2017-02-23 05:16:26 -0800122 cng_(optimization_),
peah697a5902017-06-30 07:06:10 -0700123 suppression_filter_(sample_rate_hz_),
Per Åhgren971de072018-03-14 23:23:47 +0100124 render_signal_analyzer_(config_),
peah8cee56f2017-08-24 22:36:53 -0700125 residual_echo_estimator_(config_),
126 aec_state_(config_) {
peah522d71b2017-02-23 05:16:26 -0800127 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200128 x_old_.fill(0.f);
129 y_old_.fill(0.f);
130 e_old_.fill(0.f);
peah69221db2017-01-27 03:28:19 -0800131}
132
133EchoRemoverImpl::~EchoRemoverImpl() = default;
134
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100135void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const {
136 // Echo return loss (ERL) is inverted to go from gain to attenuation.
137 metrics->echo_return_loss = -10.0 * log10(aec_state_.ErlTimeDomain());
138 metrics->echo_return_loss_enhancement =
139 10.0 * log10(aec_state_.ErleTimeDomain());
140}
141
peahcf02cf12017-04-05 14:18:07 -0700142void EchoRemoverImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800143 const EchoPathVariability& echo_path_variability,
144 bool capture_signal_saturation,
Per Åhgren5c532d32018-03-22 00:29:25 +0100145 const rtc::Optional<DelayEstimate>& external_delay,
Per Åhgrenc59a5762017-12-11 21:34:19 +0100146 RenderBuffer* render_buffer,
peah69221db2017-01-27 03:28:19 -0800147 std::vector<std::vector<float>>* capture) {
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100148 const std::vector<std::vector<float>>& x = render_buffer->Block(0);
peah522d71b2017-02-23 05:16:26 -0800149 std::vector<std::vector<float>>* y = capture;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100150 RTC_DCHECK(render_buffer);
peah522d71b2017-02-23 05:16:26 -0800151 RTC_DCHECK(y);
152 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
153 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
154 RTC_DCHECK_EQ(x[0].size(), kBlockSize);
155 RTC_DCHECK_EQ((*y)[0].size(), kBlockSize);
156 const std::vector<float>& x0 = x[0];
157 std::vector<float>& y0 = (*y)[0];
158
peah86afe9d2017-04-06 15:45:32 -0700159 data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0],
peah522d71b2017-02-23 05:16:26 -0800160 LowestBandRate(sample_rate_hz_), 1);
peah86afe9d2017-04-06 15:45:32 -0700161 data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0],
peah522d71b2017-02-23 05:16:26 -0800162 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700163 data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0);
164 data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0);
peah522d71b2017-02-23 05:16:26 -0800165
166 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
167
168 if (echo_path_variability.AudioPathChanged()) {
169 subtractor_.HandleEchoPathChange(echo_path_variability);
peah86afe9d2017-04-06 15:45:32 -0700170 aec_state_.HandleEchoPathChange(echo_path_variability);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100171 suppression_gain_.SetInitialState(true);
Per Åhgrena98c8072018-01-15 19:17:16 +0100172 initial_state_ = true;
peah522d71b2017-02-23 05:16:26 -0800173 }
174
175 std::array<float, kFftLengthBy2Plus1> Y2;
Per Åhgren169c7fd2018-04-27 12:04:03 +0200176 std::array<float, kFftLengthBy2Plus1> E2;
peah522d71b2017-02-23 05:16:26 -0800177 std::array<float, kFftLengthBy2Plus1> R2;
178 std::array<float, kFftLengthBy2Plus1> S2_linear;
179 std::array<float, kFftLengthBy2Plus1> G;
peah86afe9d2017-04-06 15:45:32 -0700180 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -0800181 FftData Y;
Per Åhgren169c7fd2018-04-27 12:04:03 +0200182 FftData E;
peah522d71b2017-02-23 05:16:26 -0800183 FftData comfort_noise;
184 FftData high_band_comfort_noise;
185 SubtractorOutput subtractor_output;
peah522d71b2017-02-23 05:16:26 -0800186
peah522d71b2017-02-23 05:16:26 -0800187 // Analyze the render signal.
Per Åhgren5c532d32018-03-22 00:29:25 +0100188 render_signal_analyzer_.Update(*render_buffer,
189 aec_state_.FilterDelayBlocks());
peah522d71b2017-02-23 05:16:26 -0800190
191 // Perform linear echo cancellation.
Per Åhgrena98c8072018-01-15 19:17:16 +0100192 if (initial_state_ && !aec_state_.InitialState()) {
193 subtractor_.ExitInitialState();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100194 suppression_gain_.SetInitialState(false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100195 initial_state_ = false;
196 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100197
198 // If the delay is known, use the echo subtractor.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100199 subtractor_.Process(*render_buffer, y0, render_signal_analyzer_, aec_state_,
peah86afe9d2017-04-06 15:45:32 -0700200 &subtractor_output);
Per Åhgren169c7fd2018-04-27 12:04:03 +0200201 const auto& e = subtractor_output.e_main;
peah522d71b2017-02-23 05:16:26 -0800202
203 // Compute spectra.
Per Åhgren169c7fd2018-04-27 12:04:03 +0200204 WindowedPaddedFft(fft_, y0, y_old_, &Y);
205 WindowedPaddedFft(fft_, e, e_old_, &E);
206 LinearEchoPower(E, Y, &S2_linear);
Per Åhgren8ba58612017-12-01 23:01:44 +0100207 Y.Spectrum(optimization_, Y2);
Per Åhgren169c7fd2018-04-27 12:04:03 +0200208 E.Spectrum(optimization_, E2);
peah522d71b2017-02-23 05:16:26 -0800209
210 // Update the AEC state information.
Per Åhgren5c532d32018-03-22 00:29:25 +0100211 aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -0700212 subtractor_.FilterImpulseResponse(),
Per Åhgren5c532d32018-03-22 00:29:25 +0100213 subtractor_.ConvergedFilter(), subtractor_.DivergedFilter(),
Per Åhgren169c7fd2018-04-27 12:04:03 +0200214 *render_buffer, E2, Y2, subtractor_output.s_main);
215
216 // Compute spectra.
217 const bool suppression_gain_uses_ffts =
218 config_.suppressor.bands_with_reliable_coherence > 0;
219 FftData X;
220 if (suppression_gain_uses_ffts) {
221 auto& x_aligned = render_buffer->Block(-aec_state_.FilterDelayBlocks())[0];
222 WindowedPaddedFft(fft_, x_aligned, x_old_, &X);
223 } else {
224 X.Clear();
225 }
peah522d71b2017-02-23 05:16:26 -0800226
227 // Choose the linear output.
Per Åhgren169c7fd2018-04-27 12:04:03 +0200228 data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0],
Per Åhgren5c532d32018-03-22 00:29:25 +0100229 LowestBandRate(sample_rate_hz_), 1);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200230 if (aec_state_.UseLinearFilterOutput()) {
Per Åhgren169c7fd2018-04-27 12:04:03 +0200231 std::copy(e.begin(), e.end(), y0.begin());
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200232 }
Per Åhgren169c7fd2018-04-27 12:04:03 +0200233 const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y;
234
peah522d71b2017-02-23 05:16:26 -0800235 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0],
236 LowestBandRate(sample_rate_hz_), 1);
peah522d71b2017-02-23 05:16:26 -0800237
238 // Estimate the residual echo power.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100239 residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2,
peah86afe9d2017-04-06 15:45:32 -0700240 &R2);
peah522d71b2017-02-23 05:16:26 -0800241
242 // Estimate the comfort noise.
243 cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise);
244
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200245
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200246
247 // Compute and apply the suppression gain.
248 suppression_gain_.GetGain(E2, R2, cng_.NoiseSpectrum(), E, X, Y,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200249 render_signal_analyzer_, aec_state_, x,
250 &high_bands_gain, &G);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200251
peah86afe9d2017-04-06 15:45:32 -0700252 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200253 high_bands_gain, Y_fft, y);
peah522d71b2017-02-23 05:16:26 -0800254
peahe985b3f2017-02-28 22:08:53 -0800255 // Update the metrics.
256 metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G);
257
peah522d71b2017-02-23 05:16:26 -0800258 // Debug outputs for the purpose of development and analysis.
peah29103572017-07-11 02:54:02 -0700259 data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
260 &subtractor_output.s_main[0],
261 LowestBandRate(sample_rate_hz_), 1);
262 data_dumper_->DumpRaw("aec3_output", y0);
peah14c11a42017-07-11 06:13:43 -0700263 data_dumper_->DumpRaw("aec3_narrow_render",
264 render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800265 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum());
266 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
267 data_dumper_->DumpWav("aec3_output",
268 rtc::ArrayView<const float>(&y0[0], kBlockSize),
269 LowestBandRate(sample_rate_hz_), 1);
270 data_dumper_->DumpRaw("aec3_using_subtractor_output",
Per Åhgren5c532d32018-03-22 00:29:25 +0100271 aec_state_.UseLinearFilterOutput() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800272 data_dumper_->DumpRaw("aec3_E2", E2);
peah522d71b2017-02-23 05:16:26 -0800273 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear);
peah522d71b2017-02-23 05:16:26 -0800274 data_dumper_->DumpRaw("aec3_Y2", Y2);
Jesús de Vicente Peña7682c6e2018-03-22 14:53:23 +0100275 data_dumper_->DumpRaw(
276 "aec3_X2", render_buffer->Spectrum(aec_state_.FilterDelayBlocks()));
peah522d71b2017-02-23 05:16:26 -0800277 data_dumper_->DumpRaw("aec3_R2", R2);
Per Åhgren5c532d32018-03-22 00:29:25 +0100278 data_dumper_->DumpRaw("aec3_filter_delay", aec_state_.FilterDelayBlocks());
peah522d71b2017-02-23 05:16:26 -0800279 data_dumper_->DumpRaw("aec3_capture_saturation",
280 aec_state_.SaturatedCapture() ? 1 : 0);
281}
peah69221db2017-01-27 03:28:19 -0800282
283} // namespace
284
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200285EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config,
286 int sample_rate_hz) {
peah697a5902017-06-30 07:06:10 -0700287 return new EchoRemoverImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800288}
289
290} // namespace webrtc