blob: 71e4526b9e93f5126aebf06d796d354481d20ae5 [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
12#include <algorithm>
peah522d71b2017-02-23 05:16:26 -080013#include <memory>
14#include <numeric>
15#include <string>
peah69221db2017-01-27 03:28:19 -080016
peah522d71b2017-02-23 05:16:26 -080017#include "webrtc/base/array_view.h"
18#include "webrtc/base/atomicops.h"
peah69221db2017-01-27 03:28:19 -080019#include "webrtc/base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080020#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
21#include "webrtc/modules/audio_processing/aec3/aec_state.h"
22#include "webrtc/modules/audio_processing/aec3/comfort_noise_generator.h"
23#include "webrtc/modules/audio_processing/aec3/echo_path_variability.h"
peahe985b3f2017-02-28 22:08:53 -080024#include "webrtc/modules/audio_processing/aec3/echo_remover_metrics.h"
peah522d71b2017-02-23 05:16:26 -080025#include "webrtc/modules/audio_processing/aec3/fft_data.h"
26#include "webrtc/modules/audio_processing/aec3/output_selector.h"
27#include "webrtc/modules/audio_processing/aec3/power_echo_model.h"
peahcf02cf12017-04-05 14:18:07 -070028#include "webrtc/modules/audio_processing/aec3/render_buffer.h"
peah522d71b2017-02-23 05:16:26 -080029#include "webrtc/modules/audio_processing/aec3/render_delay_buffer.h"
30#include "webrtc/modules/audio_processing/aec3/residual_echo_estimator.h"
31#include "webrtc/modules/audio_processing/aec3/subtractor.h"
32#include "webrtc/modules/audio_processing/aec3/suppression_filter.h"
33#include "webrtc/modules/audio_processing/aec3/suppression_gain.h"
34#include "webrtc/modules/audio_processing/logging/apm_data_dumper.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
49float BlockPower(const std::array<float, kBlockSize> x) {
50 return std::accumulate(x.begin(), x.end(), 0.f,
51 [](float a, float b) -> float { return a + b * b; });
52}
53
54// Class for removing the echo from the capture signal.
peah69221db2017-01-27 03:28:19 -080055class EchoRemoverImpl final : public EchoRemover {
56 public:
57 explicit EchoRemoverImpl(int sample_rate_hz);
58 ~EchoRemoverImpl() override;
59
peah522d71b2017-02-23 05:16:26 -080060 // Removes the echo from a block of samples from the capture signal. The
61 // supplied render signal is assumed to be pre-aligned with the capture
62 // signal.
peahcf02cf12017-04-05 14:18:07 -070063 void ProcessCapture(
peah522d71b2017-02-23 05:16:26 -080064 const rtc::Optional<size_t>& external_echo_path_delay_estimate,
65 const EchoPathVariability& echo_path_variability,
66 bool capture_signal_saturation,
peahcf02cf12017-04-05 14:18:07 -070067 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080068 std::vector<std::vector<float>>* capture) override;
peah69221db2017-01-27 03:28:19 -080069
peah522d71b2017-02-23 05:16:26 -080070 // Updates the status on whether echo leakage is detected in the output of the
71 // echo remover.
72 void UpdateEchoLeakageStatus(bool leakage_detected) override {
73 echo_leakage_detected_ = leakage_detected;
74 }
peah69221db2017-01-27 03:28:19 -080075
76 private:
peah522d71b2017-02-23 05:16:26 -080077 static int instance_count_;
78 const Aec3Fft fft_;
79 std::unique_ptr<ApmDataDumper> data_dumper_;
80 const Aec3Optimization optimization_;
peah69221db2017-01-27 03:28:19 -080081 const int sample_rate_hz_;
peah522d71b2017-02-23 05:16:26 -080082 Subtractor subtractor_;
83 SuppressionGain suppression_gain_;
84 ComfortNoiseGenerator cng_;
85 SuppressionFilter suppression_filter_;
86 PowerEchoModel power_echo_model_;
peahcf02cf12017-04-05 14:18:07 -070087 RenderBuffer X_buffer_;
peah522d71b2017-02-23 05:16:26 -080088 RenderSignalAnalyzer render_signal_analyzer_;
89 OutputSelector output_selector_;
90 ResidualEchoEstimator residual_echo_estimator_;
91 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -080092 AecState aec_state_;
peahe985b3f2017-02-28 22:08:53 -080093 EchoRemoverMetrics metrics_;
peah69221db2017-01-27 03:28:19 -080094
95 RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl);
96};
97
peah522d71b2017-02-23 05:16:26 -080098int EchoRemoverImpl::instance_count_ = 0;
99
peah69221db2017-01-27 03:28:19 -0800100EchoRemoverImpl::EchoRemoverImpl(int sample_rate_hz)
aleloi88b82b52017-02-23 06:27:03 -0800101 : fft_(),
102 data_dumper_(
peah522d71b2017-02-23 05:16:26 -0800103 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
104 optimization_(DetectOptimization()),
105 sample_rate_hz_(sample_rate_hz),
106 subtractor_(data_dumper_.get(), optimization_),
107 suppression_gain_(optimization_),
108 cng_(optimization_),
109 suppression_filter_(sample_rate_hz_),
110 X_buffer_(optimization_,
peahcf02cf12017-04-05 14:18:07 -0700111 NumBandsForRate(sample_rate_hz_),
peah522d71b2017-02-23 05:16:26 -0800112 std::max(subtractor_.MinFarendBufferLength(),
113 power_echo_model_.MinFarendBufferLength()),
114 subtractor_.NumBlocksInRenderSums()) {
115 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800116}
117
118EchoRemoverImpl::~EchoRemoverImpl() = default;
119
peahcf02cf12017-04-05 14:18:07 -0700120void EchoRemoverImpl::ProcessCapture(
peah69221db2017-01-27 03:28:19 -0800121 const rtc::Optional<size_t>& echo_path_delay_samples,
122 const EchoPathVariability& echo_path_variability,
123 bool capture_signal_saturation,
peahcf02cf12017-04-05 14:18:07 -0700124 const RenderBuffer& render_buffer,
peah69221db2017-01-27 03:28:19 -0800125 std::vector<std::vector<float>>* capture) {
peahcf02cf12017-04-05 14:18:07 -0700126 const std::vector<std::vector<float>>& x = render_buffer.MostRecentBlock();
peah522d71b2017-02-23 05:16:26 -0800127 std::vector<std::vector<float>>* y = capture;
peah69221db2017-01-27 03:28:19 -0800128
peah522d71b2017-02-23 05:16:26 -0800129 RTC_DCHECK(y);
130 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
131 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
132 RTC_DCHECK_EQ(x[0].size(), kBlockSize);
133 RTC_DCHECK_EQ((*y)[0].size(), kBlockSize);
134 const std::vector<float>& x0 = x[0];
135 std::vector<float>& y0 = (*y)[0];
136
137 data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize, &y0[0],
138 LowestBandRate(sample_rate_hz_), 1);
139 data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize, &x0[0],
140 LowestBandRate(sample_rate_hz_), 1);
141
142 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
143
144 if (echo_path_variability.AudioPathChanged()) {
145 subtractor_.HandleEchoPathChange(echo_path_variability);
peahebe77782017-02-27 07:29:21 -0800146 residual_echo_estimator_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800147 }
148
149 std::array<float, kFftLengthBy2Plus1> Y2;
150 std::array<float, kFftLengthBy2Plus1> S2_power;
151 std::array<float, kFftLengthBy2Plus1> R2;
152 std::array<float, kFftLengthBy2Plus1> S2_linear;
153 std::array<float, kFftLengthBy2Plus1> G;
peah522d71b2017-02-23 05:16:26 -0800154 FftData Y;
155 FftData comfort_noise;
156 FftData high_band_comfort_noise;
157 SubtractorOutput subtractor_output;
158 FftData& E_main = subtractor_output.E_main;
159 auto& E2_main = subtractor_output.E2_main;
160 auto& E2_shadow = subtractor_output.E2_shadow;
161 auto& e_main = subtractor_output.e_main;
162 auto& e_shadow = subtractor_output.e_shadow;
163
peah522d71b2017-02-23 05:16:26 -0800164 // Analyze the render signal.
peahcf02cf12017-04-05 14:18:07 -0700165 render_signal_analyzer_.Update(render_buffer, aec_state_.FilterDelay());
peah522d71b2017-02-23 05:16:26 -0800166
167 // Perform linear echo cancellation.
peahcf02cf12017-04-05 14:18:07 -0700168 subtractor_.Process(render_buffer, y0, render_signal_analyzer_,
peah522d71b2017-02-23 05:16:26 -0800169 aec_state_.SaturatedCapture(), &subtractor_output);
170
171 // Compute spectra.
172 fft_.ZeroPaddedFft(y0, &Y);
173 LinearEchoPower(E_main, Y, &S2_linear);
174 Y.Spectrum(optimization_, &Y2);
175
176 // Update the AEC state information.
177 aec_state_.Update(subtractor_.FilterFrequencyResponse(),
peahcf02cf12017-04-05 14:18:07 -0700178 echo_path_delay_samples, render_buffer, E2_main, E2_shadow,
179 Y2, x0, echo_path_variability, echo_leakage_detected_);
peah522d71b2017-02-23 05:16:26 -0800180
181 // Use the power model to estimate the echo.
peahcf02cf12017-04-05 14:18:07 -0700182 // TODO(peah): Remove in upcoming CL.
183 // power_echo_model_.EstimateEcho(render_buffer, Y2, aec_state_, &S2_power);
184 S2_power.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800185
186 // Choose the linear output.
187 output_selector_.FormLinearOutput(e_main, y0);
188 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0],
189 LowestBandRate(sample_rate_hz_), 1);
190 const auto& E2 = output_selector_.UseSubtractorOutput() ? E2_main : Y2;
191
192 // Estimate the residual echo power.
193 residual_echo_estimator_.Estimate(
peahcf02cf12017-04-05 14:18:07 -0700194 output_selector_.UseSubtractorOutput(), aec_state_, render_buffer,
peah522d71b2017-02-23 05:16:26 -0800195 subtractor_.FilterFrequencyResponse(), E2_main, E2_shadow, S2_linear,
196 S2_power, Y2, &R2);
197
198 // Estimate the comfort noise.
199 cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise);
200
201 // Detect basic doubletalk.
202 const bool doubletalk = BlockPower(e_shadow) < BlockPower(e_main);
203
204 // A choose and apply echo suppression gain.
205 suppression_gain_.GetGain(E2, R2, cng_.NoiseSpectrum(),
206 doubletalk ? 0.001f : 0.0001f, &G);
207 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G, y);
208
peahe985b3f2017-02-28 22:08:53 -0800209 // Update the metrics.
210 metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G);
211
peah522d71b2017-02-23 05:16:26 -0800212 // Debug outputs for the purpose of development and analysis.
213 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum());
214 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
215 data_dumper_->DumpWav("aec3_output",
216 rtc::ArrayView<const float>(&y0[0], kBlockSize),
217 LowestBandRate(sample_rate_hz_), 1);
218 data_dumper_->DumpRaw("aec3_using_subtractor_output",
219 output_selector_.UseSubtractorOutput() ? 1 : 0);
220 data_dumper_->DumpRaw("aec3_doubletalk", doubletalk ? 1 : 0);
221 data_dumper_->DumpRaw("aec3_E2", E2);
222 data_dumper_->DumpRaw("aec3_E2_main", E2_main);
223 data_dumper_->DumpRaw("aec3_E2_shadow", E2_shadow);
224 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear);
225 data_dumper_->DumpRaw("aec3_S2_power", S2_power);
226 data_dumper_->DumpRaw("aec3_Y2", Y2);
227 data_dumper_->DumpRaw("aec3_R2", R2);
228 data_dumper_->DumpRaw("aec3_erle", aec_state_.Erle());
229 data_dumper_->DumpRaw("aec3_erl", aec_state_.Erl());
230 data_dumper_->DumpRaw("aec3_reliable_filter_bands",
231 aec_state_.BandsWithReliableFilter());
232 data_dumper_->DumpRaw("aec3_active_render", aec_state_.ActiveRender());
233 data_dumper_->DumpRaw("aec3_model_based_aec_feasible",
234 aec_state_.ModelBasedAecFeasible());
235 data_dumper_->DumpRaw("aec3_usable_linear_estimate",
236 aec_state_.UsableLinearEstimate());
237 data_dumper_->DumpRaw(
238 "aec3_filter_delay",
239 aec_state_.FilterDelay() ? *aec_state_.FilterDelay() : -1);
240 data_dumper_->DumpRaw(
241 "aec3_external_delay",
242 aec_state_.ExternalDelay() ? *aec_state_.ExternalDelay() : -1);
243 data_dumper_->DumpRaw("aec3_capture_saturation",
244 aec_state_.SaturatedCapture() ? 1 : 0);
245}
peah69221db2017-01-27 03:28:19 -0800246
247} // namespace
248
249EchoRemover* EchoRemover::Create(int sample_rate_hz) {
250 return new EchoRemoverImpl(sample_rate_hz);
251}
252
253} // namespace webrtc