blob: b8a8ac0fc3d4127f6bc0e2ca583656bc9f395195 [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"
25#include "modules/audio_processing/aec3/output_selector.h"
26#include "modules/audio_processing/aec3/render_buffer.h"
27#include "modules/audio_processing/aec3/render_delay_buffer.h"
28#include "modules/audio_processing/aec3/residual_echo_estimator.h"
29#include "modules/audio_processing/aec3/subtractor.h"
30#include "modules/audio_processing/aec3/suppression_filter.h"
31#include "modules/audio_processing/aec3/suppression_gain.h"
32#include "modules/audio_processing/logging/apm_data_dumper.h"
33#include "rtc_base/atomicops.h"
34#include "rtc_base/constructormagic.h"
peah69221db2017-01-27 03:28:19 -080035
36namespace webrtc {
37
38namespace {
peah522d71b2017-02-23 05:16:26 -080039
40void LinearEchoPower(const FftData& E,
41 const FftData& Y,
42 std::array<float, kFftLengthBy2Plus1>* S2) {
43 for (size_t k = 0; k < E.re.size(); ++k) {
44 (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) +
45 (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]);
46 }
47}
48
peah522d71b2017-02-23 05:16:26 -080049// Class for removing the echo from the capture signal.
peah69221db2017-01-27 03:28:19 -080050class EchoRemoverImpl final : public EchoRemover {
51 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020052 explicit EchoRemoverImpl(const EchoCanceller3Config& config,
53 int sample_rate_hz);
peah69221db2017-01-27 03:28:19 -080054 ~EchoRemoverImpl() override;
55
Gustaf Ullberg332150d2017-11-22 14:17:39 +010056 void GetMetrics(EchoControl::Metrics* metrics) const override;
57
peah522d71b2017-02-23 05:16:26 -080058 // Removes the echo from a block of samples from the capture signal. The
59 // supplied render signal is assumed to be pre-aligned with the capture
60 // signal.
Per Åhgrende22a172017-12-20 18:00:51 +010061 void ProcessCapture(const EchoPathVariability& echo_path_variability,
Alex Loiko890988c2017-08-31 10:25:48 +020062 bool capture_signal_saturation,
Per Åhgren3ab308f2018-02-21 08:46:03 +010063 const rtc::Optional<DelayEstimate>& delay_estimate,
Per Åhgrenc59a5762017-12-11 21:34:19 +010064 RenderBuffer* render_buffer,
Alex Loiko890988c2017-08-31 10:25:48 +020065 std::vector<std::vector<float>>* capture) override;
peah69221db2017-01-27 03:28:19 -080066
peah522d71b2017-02-23 05:16:26 -080067 // Updates the status on whether echo leakage is detected in the output of the
68 // echo remover.
69 void UpdateEchoLeakageStatus(bool leakage_detected) override {
70 echo_leakage_detected_ = leakage_detected;
71 }
peah69221db2017-01-27 03:28:19 -080072
73 private:
peah522d71b2017-02-23 05:16:26 -080074 static int instance_count_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020075 const EchoCanceller3Config config_;
peah522d71b2017-02-23 05:16:26 -080076 const Aec3Fft fft_;
77 std::unique_ptr<ApmDataDumper> data_dumper_;
78 const Aec3Optimization optimization_;
peah69221db2017-01-27 03:28:19 -080079 const int sample_rate_hz_;
peah522d71b2017-02-23 05:16:26 -080080 Subtractor subtractor_;
81 SuppressionGain suppression_gain_;
82 ComfortNoiseGenerator cng_;
83 SuppressionFilter suppression_filter_;
peah522d71b2017-02-23 05:16:26 -080084 RenderSignalAnalyzer render_signal_analyzer_;
85 OutputSelector output_selector_;
86 ResidualEchoEstimator residual_echo_estimator_;
87 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -080088 AecState aec_state_;
peahe985b3f2017-02-28 22:08:53 -080089 EchoRemoverMetrics metrics_;
Per Åhgrena98c8072018-01-15 19:17:16 +010090 bool initial_state_ = true;
peah69221db2017-01-27 03:28:19 -080091
92 RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl);
93};
94
peah522d71b2017-02-23 05:16:26 -080095int EchoRemoverImpl::instance_count_ = 0;
96
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020097EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config,
98 int sample_rate_hz)
peah8cee56f2017-08-24 22:36:53 -070099 : config_(config),
100 fft_(),
aleloi88b82b52017-02-23 06:27:03 -0800101 data_dumper_(
peah522d71b2017-02-23 05:16:26 -0800102 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
103 optimization_(DetectOptimization()),
104 sample_rate_hz_(sample_rate_hz),
Per Åhgren09a718a2017-12-11 22:28:45 +0100105 subtractor_(config, data_dumper_.get(), optimization_),
peah8cee56f2017-08-24 22:36:53 -0700106 suppression_gain_(config_, optimization_),
peah522d71b2017-02-23 05:16:26 -0800107 cng_(optimization_),
peah697a5902017-06-30 07:06:10 -0700108 suppression_filter_(sample_rate_hz_),
Per Åhgren971de072018-03-14 23:23:47 +0100109 render_signal_analyzer_(config_),
peah8cee56f2017-08-24 22:36:53 -0700110 residual_echo_estimator_(config_),
111 aec_state_(config_) {
peah522d71b2017-02-23 05:16:26 -0800112 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800113}
114
115EchoRemoverImpl::~EchoRemoverImpl() = default;
116
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100117void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const {
118 // Echo return loss (ERL) is inverted to go from gain to attenuation.
119 metrics->echo_return_loss = -10.0 * log10(aec_state_.ErlTimeDomain());
120 metrics->echo_return_loss_enhancement =
121 10.0 * log10(aec_state_.ErleTimeDomain());
122}
123
peahcf02cf12017-04-05 14:18:07 -0700124void EchoRemoverImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800125 const EchoPathVariability& echo_path_variability,
126 bool capture_signal_saturation,
Per Åhgren3ab308f2018-02-21 08:46:03 +0100127 const rtc::Optional<DelayEstimate>& delay_estimate,
Per Åhgrenc59a5762017-12-11 21:34:19 +0100128 RenderBuffer* render_buffer,
peah69221db2017-01-27 03:28:19 -0800129 std::vector<std::vector<float>>* capture) {
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100130 const std::vector<std::vector<float>>& x = render_buffer->Block(0);
peah522d71b2017-02-23 05:16:26 -0800131 std::vector<std::vector<float>>* y = capture;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100132 RTC_DCHECK(render_buffer);
peah522d71b2017-02-23 05:16:26 -0800133 RTC_DCHECK(y);
134 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
135 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
136 RTC_DCHECK_EQ(x[0].size(), kBlockSize);
137 RTC_DCHECK_EQ((*y)[0].size(), kBlockSize);
138 const std::vector<float>& x0 = x[0];
139 std::vector<float>& y0 = (*y)[0];
140
peah86afe9d2017-04-06 15:45:32 -0700141 data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0],
peah522d71b2017-02-23 05:16:26 -0800142 LowestBandRate(sample_rate_hz_), 1);
peah86afe9d2017-04-06 15:45:32 -0700143 data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0],
peah522d71b2017-02-23 05:16:26 -0800144 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700145 data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0);
146 data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0);
peah522d71b2017-02-23 05:16:26 -0800147
148 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
149
150 if (echo_path_variability.AudioPathChanged()) {
151 subtractor_.HandleEchoPathChange(echo_path_variability);
peah86afe9d2017-04-06 15:45:32 -0700152 aec_state_.HandleEchoPathChange(echo_path_variability);
Per Åhgrena98c8072018-01-15 19:17:16 +0100153 initial_state_ = true;
peah522d71b2017-02-23 05:16:26 -0800154 }
155
156 std::array<float, kFftLengthBy2Plus1> Y2;
peah522d71b2017-02-23 05:16:26 -0800157 std::array<float, kFftLengthBy2Plus1> R2;
158 std::array<float, kFftLengthBy2Plus1> S2_linear;
159 std::array<float, kFftLengthBy2Plus1> G;
peah86afe9d2017-04-06 15:45:32 -0700160 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -0800161 FftData Y;
162 FftData comfort_noise;
163 FftData high_band_comfort_noise;
164 SubtractorOutput subtractor_output;
Per Åhgrend20639f2018-01-11 10:29:49 +0100165 FftData& E_main_nonwindowed = subtractor_output.E_main_nonwindowed;
166 auto& E2_main = subtractor_output.E2_main_nonwindowed;
peah522d71b2017-02-23 05:16:26 -0800167 auto& E2_shadow = subtractor_output.E2_shadow;
168 auto& e_main = subtractor_output.e_main;
peah522d71b2017-02-23 05:16:26 -0800169
peah522d71b2017-02-23 05:16:26 -0800170 // Analyze the render signal.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100171 render_signal_analyzer_.Update(*render_buffer, aec_state_.FilterDelay());
peah522d71b2017-02-23 05:16:26 -0800172
173 // Perform linear echo cancellation.
Per Åhgrena98c8072018-01-15 19:17:16 +0100174 if (initial_state_ && !aec_state_.InitialState()) {
175 subtractor_.ExitInitialState();
176 initial_state_ = false;
177 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100178 subtractor_.Process(*render_buffer, y0, render_signal_analyzer_, aec_state_,
peah86afe9d2017-04-06 15:45:32 -0700179 &subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800180
181 // Compute spectra.
Per Åhgrend20639f2018-01-11 10:29:49 +0100182 // fft_.ZeroPaddedFft(y0, Aec3Fft::Window::kHanning, &Y);
183 fft_.ZeroPaddedFft(y0, Aec3Fft::Window::kRectangular, &Y);
184 LinearEchoPower(E_main_nonwindowed, Y, &S2_linear);
Per Åhgren8ba58612017-12-01 23:01:44 +0100185 Y.Spectrum(optimization_, Y2);
peah522d71b2017-02-23 05:16:26 -0800186
187 // Update the AEC state information.
Per Åhgren3ab308f2018-02-21 08:46:03 +0100188 aec_state_.Update(delay_estimate, subtractor_.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -0700189 subtractor_.FilterImpulseResponse(),
Per Åhgrende22a172017-12-20 18:00:51 +0100190 subtractor_.ConvergedFilter(), *render_buffer, E2_main, Y2,
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100191 subtractor_output.s_main, echo_leakage_detected_);
peah522d71b2017-02-23 05:16:26 -0800192
193 // Choose the linear output.
Per Åhgren1b4059e2017-10-15 20:19:21 +0200194 output_selector_.FormLinearOutput(!aec_state_.TransparentMode(), e_main, y0);
peah522d71b2017-02-23 05:16:26 -0800195 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0],
196 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700197 data_dumper_->DumpRaw("aec3_output_linear", y0);
peah522d71b2017-02-23 05:16:26 -0800198 const auto& E2 = output_selector_.UseSubtractorOutput() ? E2_main : Y2;
199
200 // Estimate the residual echo power.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100201 residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2,
peah86afe9d2017-04-06 15:45:32 -0700202 &R2);
peah522d71b2017-02-23 05:16:26 -0800203
204 // Estimate the comfort noise.
205 cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise);
206
peah522d71b2017-02-23 05:16:26 -0800207 // A choose and apply echo suppression gain.
Per Åhgren7ddd4632017-10-25 02:59:45 +0200208 suppression_gain_.GetGain(E2, R2, cng_.NoiseSpectrum(),
209 render_signal_analyzer_, aec_state_, x,
210 &high_bands_gain, &G);
peah86afe9d2017-04-06 15:45:32 -0700211 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
212 high_bands_gain, y);
peah522d71b2017-02-23 05:16:26 -0800213
peahe985b3f2017-02-28 22:08:53 -0800214 // Update the metrics.
215 metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G);
216
peah29103572017-07-11 02:54:02 -0700217 // Update the aec state with the aec output characteristics.
218 aec_state_.UpdateWithOutput(y0);
219
peah522d71b2017-02-23 05:16:26 -0800220 // Debug outputs for the purpose of development and analysis.
peah29103572017-07-11 02:54:02 -0700221 data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
222 &subtractor_output.s_main[0],
223 LowestBandRate(sample_rate_hz_), 1);
224 data_dumper_->DumpRaw("aec3_output", y0);
peah14c11a42017-07-11 06:13:43 -0700225 data_dumper_->DumpRaw("aec3_narrow_render",
226 render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800227 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum());
228 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
229 data_dumper_->DumpWav("aec3_output",
230 rtc::ArrayView<const float>(&y0[0], kBlockSize),
231 LowestBandRate(sample_rate_hz_), 1);
232 data_dumper_->DumpRaw("aec3_using_subtractor_output",
233 output_selector_.UseSubtractorOutput() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800234 data_dumper_->DumpRaw("aec3_E2", E2);
235 data_dumper_->DumpRaw("aec3_E2_main", E2_main);
236 data_dumper_->DumpRaw("aec3_E2_shadow", E2_shadow);
237 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear);
peah522d71b2017-02-23 05:16:26 -0800238 data_dumper_->DumpRaw("aec3_Y2", Y2);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100239 data_dumper_->DumpRaw("aec3_X2", render_buffer->Spectrum(0));
peah522d71b2017-02-23 05:16:26 -0800240 data_dumper_->DumpRaw("aec3_R2", R2);
241 data_dumper_->DumpRaw("aec3_erle", aec_state_.Erle());
242 data_dumper_->DumpRaw("aec3_erl", aec_state_.Erl());
peah522d71b2017-02-23 05:16:26 -0800243 data_dumper_->DumpRaw("aec3_usable_linear_estimate",
244 aec_state_.UsableLinearEstimate());
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100245 data_dumper_->DumpRaw("aec3_filter_delay", aec_state_.FilterDelay());
peah522d71b2017-02-23 05:16:26 -0800246 data_dumper_->DumpRaw("aec3_capture_saturation",
247 aec_state_.SaturatedCapture() ? 1 : 0);
248}
peah69221db2017-01-27 03:28:19 -0800249
250} // namespace
251
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200252EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config,
253 int sample_rate_hz) {
peah697a5902017-06-30 07:06:10 -0700254 return new EchoRemoverImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800255}
256
257} // namespace webrtc