blob: 32ecc7392689b5e88907697fa94b1db88f7c65cc [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 */
10#include "webrtc/modules/audio_processing/aec3/echo_remover.h"
11
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
peah522d71b2017-02-23 05:16:26 -080018#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
19#include "webrtc/modules/audio_processing/aec3/aec_state.h"
20#include "webrtc/modules/audio_processing/aec3/comfort_noise_generator.h"
21#include "webrtc/modules/audio_processing/aec3/echo_path_variability.h"
peahe985b3f2017-02-28 22:08:53 -080022#include "webrtc/modules/audio_processing/aec3/echo_remover_metrics.h"
peah522d71b2017-02-23 05:16:26 -080023#include "webrtc/modules/audio_processing/aec3/fft_data.h"
24#include "webrtc/modules/audio_processing/aec3/output_selector.h"
peahcf02cf12017-04-05 14:18:07 -070025#include "webrtc/modules/audio_processing/aec3/render_buffer.h"
peah522d71b2017-02-23 05:16:26 -080026#include "webrtc/modules/audio_processing/aec3/render_delay_buffer.h"
27#include "webrtc/modules/audio_processing/aec3/residual_echo_estimator.h"
28#include "webrtc/modules/audio_processing/aec3/subtractor.h"
29#include "webrtc/modules/audio_processing/aec3/suppression_filter.h"
30#include "webrtc/modules/audio_processing/aec3/suppression_gain.h"
31#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020032#include "webrtc/rtc_base/array_view.h"
33#include "webrtc/rtc_base/atomicops.h"
34#include "webrtc/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:
peah697a5902017-06-30 07:06:10 -070052 explicit EchoRemoverImpl(
53 const AudioProcessing::Config::EchoCanceller3& config,
54 int sample_rate_hz);
peah69221db2017-01-27 03:28:19 -080055 ~EchoRemoverImpl() override;
56
peah522d71b2017-02-23 05:16:26 -080057 // Removes the echo from a block of samples from the capture signal. The
58 // supplied render signal is assumed to be pre-aligned with the capture
59 // signal.
peahcf02cf12017-04-05 14:18:07 -070060 void ProcessCapture(
peah522d71b2017-02-23 05:16:26 -080061 const rtc::Optional<size_t>& external_echo_path_delay_estimate,
62 const EchoPathVariability& echo_path_variability,
63 bool capture_signal_saturation,
peahcf02cf12017-04-05 14:18:07 -070064 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080065 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_;
peah8cee56f2017-08-24 22:36:53 -070075 const AudioProcessing::Config::EchoCanceller3 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_;
peah69221db2017-01-27 03:28:19 -080090
91 RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl);
92};
93
peah522d71b2017-02-23 05:16:26 -080094int EchoRemoverImpl::instance_count_ = 0;
95
peah697a5902017-06-30 07:06:10 -070096EchoRemoverImpl::EchoRemoverImpl(
97 const AudioProcessing::Config::EchoCanceller3& 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),
105 subtractor_(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_),
peah8cee56f2017-08-24 22:36:53 -0700109 residual_echo_estimator_(config_),
110 aec_state_(config_) {
peah522d71b2017-02-23 05:16:26 -0800111 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800112}
113
114EchoRemoverImpl::~EchoRemoverImpl() = default;
115
peahcf02cf12017-04-05 14:18:07 -0700116void EchoRemoverImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800117 const rtc::Optional<size_t>& echo_path_delay_samples,
118 const EchoPathVariability& echo_path_variability,
119 bool capture_signal_saturation,
peahcf02cf12017-04-05 14:18:07 -0700120 const RenderBuffer& render_buffer,
peah69221db2017-01-27 03:28:19 -0800121 std::vector<std::vector<float>>* capture) {
peahcf02cf12017-04-05 14:18:07 -0700122 const std::vector<std::vector<float>>& x = render_buffer.MostRecentBlock();
peah522d71b2017-02-23 05:16:26 -0800123 std::vector<std::vector<float>>* y = capture;
peah69221db2017-01-27 03:28:19 -0800124
peah522d71b2017-02-23 05:16:26 -0800125 RTC_DCHECK(y);
126 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
127 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
128 RTC_DCHECK_EQ(x[0].size(), kBlockSize);
129 RTC_DCHECK_EQ((*y)[0].size(), kBlockSize);
130 const std::vector<float>& x0 = x[0];
131 std::vector<float>& y0 = (*y)[0];
132
peah86afe9d2017-04-06 15:45:32 -0700133 data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0],
peah522d71b2017-02-23 05:16:26 -0800134 LowestBandRate(sample_rate_hz_), 1);
peah86afe9d2017-04-06 15:45:32 -0700135 data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0],
peah522d71b2017-02-23 05:16:26 -0800136 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700137 data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0);
138 data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0);
peah522d71b2017-02-23 05:16:26 -0800139
140 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
141
142 if (echo_path_variability.AudioPathChanged()) {
143 subtractor_.HandleEchoPathChange(echo_path_variability);
peah86afe9d2017-04-06 15:45:32 -0700144 aec_state_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800145 }
146
147 std::array<float, kFftLengthBy2Plus1> Y2;
peah522d71b2017-02-23 05:16:26 -0800148 std::array<float, kFftLengthBy2Plus1> R2;
149 std::array<float, kFftLengthBy2Plus1> S2_linear;
150 std::array<float, kFftLengthBy2Plus1> G;
peah86afe9d2017-04-06 15:45:32 -0700151 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -0800152 FftData Y;
153 FftData comfort_noise;
154 FftData high_band_comfort_noise;
155 SubtractorOutput subtractor_output;
156 FftData& E_main = subtractor_output.E_main;
157 auto& E2_main = subtractor_output.E2_main;
158 auto& E2_shadow = subtractor_output.E2_shadow;
159 auto& e_main = subtractor_output.e_main;
peah522d71b2017-02-23 05:16:26 -0800160
peah522d71b2017-02-23 05:16:26 -0800161 // Analyze the render signal.
peahcf02cf12017-04-05 14:18:07 -0700162 render_signal_analyzer_.Update(render_buffer, aec_state_.FilterDelay());
peah522d71b2017-02-23 05:16:26 -0800163
164 // Perform linear echo cancellation.
peah86afe9d2017-04-06 15:45:32 -0700165 subtractor_.Process(render_buffer, y0, render_signal_analyzer_, aec_state_,
166 &subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800167
168 // Compute spectra.
169 fft_.ZeroPaddedFft(y0, &Y);
170 LinearEchoPower(E_main, Y, &S2_linear);
171 Y.Spectrum(optimization_, &Y2);
172
173 // Update the AEC state information.
174 aec_state_.Update(subtractor_.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -0700175 subtractor_.FilterImpulseResponse(),
peah86afe9d2017-04-06 15:45:32 -0700176 echo_path_delay_samples, render_buffer, E2_main, Y2, x0,
peah29103572017-07-11 02:54:02 -0700177 subtractor_output.s_main, echo_leakage_detected_);
peah522d71b2017-02-23 05:16:26 -0800178
179 // Choose the linear output.
peah86afe9d2017-04-06 15:45:32 -0700180 output_selector_.FormLinearOutput(!aec_state_.HeadsetDetected(), e_main, y0);
peah522d71b2017-02-23 05:16:26 -0800181 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0],
182 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700183 data_dumper_->DumpRaw("aec3_output_linear", y0);
peah522d71b2017-02-23 05:16:26 -0800184 const auto& E2 = output_selector_.UseSubtractorOutput() ? E2_main : Y2;
185
186 // Estimate the residual echo power.
peah86afe9d2017-04-06 15:45:32 -0700187 residual_echo_estimator_.Estimate(output_selector_.UseSubtractorOutput(),
188 aec_state_, render_buffer, S2_linear, Y2,
189 &R2);
peah522d71b2017-02-23 05:16:26 -0800190
191 // Estimate the comfort noise.
192 cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise);
193
peah522d71b2017-02-23 05:16:26 -0800194 // A choose and apply echo suppression gain.
195 suppression_gain_.GetGain(E2, R2, cng_.NoiseSpectrum(),
peah14c11a42017-07-11 06:13:43 -0700196 render_signal_analyzer_, aec_state_.SaturatedEcho(),
197 x, aec_state_.ForcedZeroGain(), &high_bands_gain,
198 &G);
peah86afe9d2017-04-06 15:45:32 -0700199 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
200 high_bands_gain, y);
peah522d71b2017-02-23 05:16:26 -0800201
peahe985b3f2017-02-28 22:08:53 -0800202 // Update the metrics.
203 metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G);
204
peah29103572017-07-11 02:54:02 -0700205 // Update the aec state with the aec output characteristics.
206 aec_state_.UpdateWithOutput(y0);
207
peah522d71b2017-02-23 05:16:26 -0800208 // Debug outputs for the purpose of development and analysis.
peah29103572017-07-11 02:54:02 -0700209 data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
210 &subtractor_output.s_main[0],
211 LowestBandRate(sample_rate_hz_), 1);
212 data_dumper_->DumpRaw("aec3_output", y0);
peah14c11a42017-07-11 06:13:43 -0700213 data_dumper_->DumpRaw("aec3_narrow_render",
214 render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800215 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum());
216 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
217 data_dumper_->DumpWav("aec3_output",
218 rtc::ArrayView<const float>(&y0[0], kBlockSize),
219 LowestBandRate(sample_rate_hz_), 1);
220 data_dumper_->DumpRaw("aec3_using_subtractor_output",
221 output_selector_.UseSubtractorOutput() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800222 data_dumper_->DumpRaw("aec3_E2", E2);
223 data_dumper_->DumpRaw("aec3_E2_main", E2_main);
224 data_dumper_->DumpRaw("aec3_E2_shadow", E2_shadow);
225 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear);
peah522d71b2017-02-23 05:16:26 -0800226 data_dumper_->DumpRaw("aec3_Y2", Y2);
peah86afe9d2017-04-06 15:45:32 -0700227 data_dumper_->DumpRaw("aec3_X2", render_buffer.Spectrum(0));
peah522d71b2017-02-23 05:16:26 -0800228 data_dumper_->DumpRaw("aec3_R2", R2);
229 data_dumper_->DumpRaw("aec3_erle", aec_state_.Erle());
230 data_dumper_->DumpRaw("aec3_erl", aec_state_.Erl());
peah522d71b2017-02-23 05:16:26 -0800231 data_dumper_->DumpRaw("aec3_active_render", aec_state_.ActiveRender());
peah522d71b2017-02-23 05:16:26 -0800232 data_dumper_->DumpRaw("aec3_usable_linear_estimate",
233 aec_state_.UsableLinearEstimate());
234 data_dumper_->DumpRaw(
235 "aec3_filter_delay",
236 aec_state_.FilterDelay() ? *aec_state_.FilterDelay() : -1);
237 data_dumper_->DumpRaw(
238 "aec3_external_delay",
239 aec_state_.ExternalDelay() ? *aec_state_.ExternalDelay() : -1);
240 data_dumper_->DumpRaw("aec3_capture_saturation",
241 aec_state_.SaturatedCapture() ? 1 : 0);
242}
peah69221db2017-01-27 03:28:19 -0800243
244} // namespace
245
peah697a5902017-06-30 07:06:10 -0700246EchoRemover* EchoRemover::Create(
247 const AudioProcessing::Config::EchoCanceller3& config,
248 int sample_rate_hz) {
249 return new EchoRemoverImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800250}
251
252} // namespace webrtc