blob: 72c629b7e8c1342c33c9081051e9aa477c1a4340 [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
peah522d71b2017-02-23 05:16:26 -080056 // Removes the echo from a block of samples from the capture signal. The
57 // supplied render signal is assumed to be pre-aligned with the capture
58 // signal.
Alex Loiko890988c2017-08-31 10:25:48 +020059 void ProcessCapture(const rtc::Optional<size_t>& echo_path_delay_samples,
60 const EchoPathVariability& echo_path_variability,
61 bool capture_signal_saturation,
62 const RenderBuffer& render_buffer,
63 std::vector<std::vector<float>>* capture) override;
peah69221db2017-01-27 03:28:19 -080064
peah522d71b2017-02-23 05:16:26 -080065 // Updates the status on whether echo leakage is detected in the output of the
66 // echo remover.
67 void UpdateEchoLeakageStatus(bool leakage_detected) override {
68 echo_leakage_detected_ = leakage_detected;
69 }
peah69221db2017-01-27 03:28:19 -080070
71 private:
peah522d71b2017-02-23 05:16:26 -080072 static int instance_count_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020073 const EchoCanceller3Config config_;
peah522d71b2017-02-23 05:16:26 -080074 const Aec3Fft fft_;
75 std::unique_ptr<ApmDataDumper> data_dumper_;
76 const Aec3Optimization optimization_;
peah69221db2017-01-27 03:28:19 -080077 const int sample_rate_hz_;
peah522d71b2017-02-23 05:16:26 -080078 Subtractor subtractor_;
79 SuppressionGain suppression_gain_;
80 ComfortNoiseGenerator cng_;
81 SuppressionFilter suppression_filter_;
peah522d71b2017-02-23 05:16:26 -080082 RenderSignalAnalyzer render_signal_analyzer_;
83 OutputSelector output_selector_;
84 ResidualEchoEstimator residual_echo_estimator_;
85 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -080086 AecState aec_state_;
peahe985b3f2017-02-28 22:08:53 -080087 EchoRemoverMetrics metrics_;
peah69221db2017-01-27 03:28:19 -080088
89 RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl);
90};
91
peah522d71b2017-02-23 05:16:26 -080092int EchoRemoverImpl::instance_count_ = 0;
93
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020094EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config,
95 int sample_rate_hz)
peah8cee56f2017-08-24 22:36:53 -070096 : config_(config),
97 fft_(),
aleloi88b82b52017-02-23 06:27:03 -080098 data_dumper_(
peah522d71b2017-02-23 05:16:26 -080099 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
100 optimization_(DetectOptimization()),
101 sample_rate_hz_(sample_rate_hz),
102 subtractor_(data_dumper_.get(), optimization_),
peah8cee56f2017-08-24 22:36:53 -0700103 suppression_gain_(config_, optimization_),
peah522d71b2017-02-23 05:16:26 -0800104 cng_(optimization_),
peah697a5902017-06-30 07:06:10 -0700105 suppression_filter_(sample_rate_hz_),
peah8cee56f2017-08-24 22:36:53 -0700106 residual_echo_estimator_(config_),
107 aec_state_(config_) {
peah522d71b2017-02-23 05:16:26 -0800108 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800109}
110
111EchoRemoverImpl::~EchoRemoverImpl() = default;
112
peahcf02cf12017-04-05 14:18:07 -0700113void EchoRemoverImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800114 const rtc::Optional<size_t>& echo_path_delay_samples,
115 const EchoPathVariability& echo_path_variability,
116 bool capture_signal_saturation,
peahcf02cf12017-04-05 14:18:07 -0700117 const RenderBuffer& render_buffer,
peah69221db2017-01-27 03:28:19 -0800118 std::vector<std::vector<float>>* capture) {
peahcf02cf12017-04-05 14:18:07 -0700119 const std::vector<std::vector<float>>& x = render_buffer.MostRecentBlock();
peah522d71b2017-02-23 05:16:26 -0800120 std::vector<std::vector<float>>* y = capture;
peah69221db2017-01-27 03:28:19 -0800121
peah522d71b2017-02-23 05:16:26 -0800122 RTC_DCHECK(y);
123 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
124 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
125 RTC_DCHECK_EQ(x[0].size(), kBlockSize);
126 RTC_DCHECK_EQ((*y)[0].size(), kBlockSize);
127 const std::vector<float>& x0 = x[0];
128 std::vector<float>& y0 = (*y)[0];
129
peah86afe9d2017-04-06 15:45:32 -0700130 data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0],
peah522d71b2017-02-23 05:16:26 -0800131 LowestBandRate(sample_rate_hz_), 1);
peah86afe9d2017-04-06 15:45:32 -0700132 data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0],
peah522d71b2017-02-23 05:16:26 -0800133 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700134 data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0);
135 data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0);
peah522d71b2017-02-23 05:16:26 -0800136
137 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
138
139 if (echo_path_variability.AudioPathChanged()) {
140 subtractor_.HandleEchoPathChange(echo_path_variability);
peah86afe9d2017-04-06 15:45:32 -0700141 aec_state_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800142 }
143
144 std::array<float, kFftLengthBy2Plus1> Y2;
peah522d71b2017-02-23 05:16:26 -0800145 std::array<float, kFftLengthBy2Plus1> R2;
146 std::array<float, kFftLengthBy2Plus1> S2_linear;
147 std::array<float, kFftLengthBy2Plus1> G;
peah86afe9d2017-04-06 15:45:32 -0700148 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -0800149 FftData Y;
150 FftData comfort_noise;
151 FftData high_band_comfort_noise;
152 SubtractorOutput subtractor_output;
153 FftData& E_main = subtractor_output.E_main;
154 auto& E2_main = subtractor_output.E2_main;
155 auto& E2_shadow = subtractor_output.E2_shadow;
156 auto& e_main = subtractor_output.e_main;
peah522d71b2017-02-23 05:16:26 -0800157
peah522d71b2017-02-23 05:16:26 -0800158 // Analyze the render signal.
peahcf02cf12017-04-05 14:18:07 -0700159 render_signal_analyzer_.Update(render_buffer, aec_state_.FilterDelay());
peah522d71b2017-02-23 05:16:26 -0800160
161 // Perform linear echo cancellation.
peah86afe9d2017-04-06 15:45:32 -0700162 subtractor_.Process(render_buffer, y0, render_signal_analyzer_, aec_state_,
163 &subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800164
165 // Compute spectra.
166 fft_.ZeroPaddedFft(y0, &Y);
167 LinearEchoPower(E_main, Y, &S2_linear);
168 Y.Spectrum(optimization_, &Y2);
169
170 // Update the AEC state information.
171 aec_state_.Update(subtractor_.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -0700172 subtractor_.FilterImpulseResponse(),
Per Åhgren1b4059e2017-10-15 20:19:21 +0200173 subtractor_.ConvergedFilter(), echo_path_delay_samples,
174 render_buffer, E2_main, Y2, x0, subtractor_output.s_main,
175 echo_leakage_detected_);
peah522d71b2017-02-23 05:16:26 -0800176
177 // Choose the linear output.
Per Åhgren1b4059e2017-10-15 20:19:21 +0200178 output_selector_.FormLinearOutput(!aec_state_.TransparentMode(), e_main, y0);
peah522d71b2017-02-23 05:16:26 -0800179 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0],
180 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700181 data_dumper_->DumpRaw("aec3_output_linear", y0);
peah522d71b2017-02-23 05:16:26 -0800182 const auto& E2 = output_selector_.UseSubtractorOutput() ? E2_main : Y2;
183
184 // Estimate the residual echo power.
Per Åhgrenc65ce782017-10-09 13:01:39 +0200185 residual_echo_estimator_.Estimate(aec_state_, render_buffer, S2_linear, Y2,
peah86afe9d2017-04-06 15:45:32 -0700186 &R2);
peah522d71b2017-02-23 05:16:26 -0800187
188 // Estimate the comfort noise.
189 cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise);
190
peah522d71b2017-02-23 05:16:26 -0800191 // A choose and apply echo suppression gain.
Per Åhgren7ddd4632017-10-25 02:59:45 +0200192 suppression_gain_.GetGain(E2, R2, cng_.NoiseSpectrum(),
193 render_signal_analyzer_, aec_state_, x,
194 &high_bands_gain, &G);
peah86afe9d2017-04-06 15:45:32 -0700195 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
196 high_bands_gain, y);
peah522d71b2017-02-23 05:16:26 -0800197
peahe985b3f2017-02-28 22:08:53 -0800198 // Update the metrics.
199 metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G);
200
peah29103572017-07-11 02:54:02 -0700201 // Update the aec state with the aec output characteristics.
202 aec_state_.UpdateWithOutput(y0);
203
peah522d71b2017-02-23 05:16:26 -0800204 // Debug outputs for the purpose of development and analysis.
peah29103572017-07-11 02:54:02 -0700205 data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
206 &subtractor_output.s_main[0],
207 LowestBandRate(sample_rate_hz_), 1);
208 data_dumper_->DumpRaw("aec3_output", y0);
peah14c11a42017-07-11 06:13:43 -0700209 data_dumper_->DumpRaw("aec3_narrow_render",
210 render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800211 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum());
212 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
213 data_dumper_->DumpWav("aec3_output",
214 rtc::ArrayView<const float>(&y0[0], kBlockSize),
215 LowestBandRate(sample_rate_hz_), 1);
216 data_dumper_->DumpRaw("aec3_using_subtractor_output",
217 output_selector_.UseSubtractorOutput() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800218 data_dumper_->DumpRaw("aec3_E2", E2);
219 data_dumper_->DumpRaw("aec3_E2_main", E2_main);
220 data_dumper_->DumpRaw("aec3_E2_shadow", E2_shadow);
221 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear);
peah522d71b2017-02-23 05:16:26 -0800222 data_dumper_->DumpRaw("aec3_Y2", Y2);
peah86afe9d2017-04-06 15:45:32 -0700223 data_dumper_->DumpRaw("aec3_X2", render_buffer.Spectrum(0));
peah522d71b2017-02-23 05:16:26 -0800224 data_dumper_->DumpRaw("aec3_R2", R2);
225 data_dumper_->DumpRaw("aec3_erle", aec_state_.Erle());
226 data_dumper_->DumpRaw("aec3_erl", aec_state_.Erl());
peah522d71b2017-02-23 05:16:26 -0800227 data_dumper_->DumpRaw("aec3_active_render", aec_state_.ActiveRender());
peah522d71b2017-02-23 05:16:26 -0800228 data_dumper_->DumpRaw("aec3_usable_linear_estimate",
229 aec_state_.UsableLinearEstimate());
230 data_dumper_->DumpRaw(
231 "aec3_filter_delay",
232 aec_state_.FilterDelay() ? *aec_state_.FilterDelay() : -1);
233 data_dumper_->DumpRaw(
234 "aec3_external_delay",
235 aec_state_.ExternalDelay() ? *aec_state_.ExternalDelay() : -1);
236 data_dumper_->DumpRaw("aec3_capture_saturation",
237 aec_state_.SaturatedCapture() ? 1 : 0);
238}
peah69221db2017-01-27 03:28:19 -0800239
240} // namespace
241
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200242EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config,
243 int sample_rate_hz) {
peah697a5902017-06-30 07:06:10 -0700244 return new EchoRemoverImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800245}
246
247} // namespace webrtc