blob: 9b6677ea4bd26f1cc193c4c478f5c583833de7e4 [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"
Per Åhgren88cf0502018-07-16 17:08:41 +020034#include "rtc_base/logging.h"
Per Åhgren78026752018-08-01 16:24:08 +020035#include "system_wrappers/include/field_trial.h"
peah69221db2017-01-27 03:28:19 -080036
37namespace webrtc {
38
39namespace {
peah522d71b2017-02-23 05:16:26 -080040
Per Åhgren78026752018-08-01 16:24:08 +020041bool UseShadowFilterOutput() {
42 return !field_trial::IsEnabled(
43 "WebRTC-Aec3UtilizeShadowFilterOutputKillSwitch");
44}
45
Per Åhgren22754392018-08-10 18:37:38 +020046bool UseSmoothSignalTransitions() {
47 return !field_trial::IsEnabled(
48 "WebRTC-Aec3SmoothSignalTransitionsKillSwitch");
49}
50
peah522d71b2017-02-23 05:16:26 -080051void LinearEchoPower(const FftData& E,
52 const FftData& Y,
53 std::array<float, kFftLengthBy2Plus1>* S2) {
54 for (size_t k = 0; k < E.re.size(); ++k) {
55 (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) +
56 (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]);
57 }
58}
59
Per Åhgren22754392018-08-10 18:37:38 +020060// Fades between two input signals using a fix-sized transition.
61void SignalTransition(rtc::ArrayView<const float> from,
62 rtc::ArrayView<const float> to,
63 rtc::ArrayView<float> out) {
64 constexpr size_t kTransitionSize = 30;
65 constexpr float kOneByTransitionSize = 1.f / kTransitionSize;
66
67 RTC_DCHECK_EQ(from.size(), to.size());
68 RTC_DCHECK_EQ(from.size(), out.size());
69 RTC_DCHECK_LE(kTransitionSize, out.size());
70
71 for (size_t k = 0; k < kTransitionSize; ++k) {
72 out[k] = k * kOneByTransitionSize * to[k];
73 out[k] += (kTransitionSize - k) * kOneByTransitionSize * to[k];
74 }
75
76 std::copy(to.begin() + kTransitionSize, to.end(),
77 out.begin() + kTransitionSize);
78}
79
Per Åhgren169c7fd2018-04-27 12:04:03 +020080// Computes a windowed (square root Hanning) padded FFT and updates the related
81// memory.
82void WindowedPaddedFft(const Aec3Fft& fft,
83 rtc::ArrayView<const float> v,
84 rtc::ArrayView<float> v_old,
85 FftData* V) {
86 fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V);
87 std::copy(v.begin(), v.end(), v_old.begin());
88}
89
peah522d71b2017-02-23 05:16:26 -080090// Class for removing the echo from the capture signal.
peah69221db2017-01-27 03:28:19 -080091class EchoRemoverImpl final : public EchoRemover {
92 public:
Per Åhgren5c532d32018-03-22 00:29:25 +010093 EchoRemoverImpl(const EchoCanceller3Config& config, int sample_rate_hz);
peah69221db2017-01-27 03:28:19 -080094 ~EchoRemoverImpl() override;
95
Gustaf Ullberg332150d2017-11-22 14:17:39 +010096 void GetMetrics(EchoControl::Metrics* metrics) const override;
97
peah522d71b2017-02-23 05:16:26 -080098 // Removes the echo from a block of samples from the capture signal. The
99 // supplied render signal is assumed to be pre-aligned with the capture
100 // signal.
Per Åhgren88cf0502018-07-16 17:08:41 +0200101 void ProcessCapture(EchoPathVariability echo_path_variability,
Alex Loiko890988c2017-08-31 10:25:48 +0200102 bool capture_signal_saturation,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200103 const absl::optional<DelayEstimate>& external_delay,
Per Åhgrenc59a5762017-12-11 21:34:19 +0100104 RenderBuffer* render_buffer,
Alex Loiko890988c2017-08-31 10:25:48 +0200105 std::vector<std::vector<float>>* capture) override;
peah69221db2017-01-27 03:28:19 -0800106
Per Åhgren5c532d32018-03-22 00:29:25 +0100107 // Returns the internal delay estimate in blocks.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200108 absl::optional<int> Delay() const override {
Per Åhgrene05c43c2018-05-09 12:26:51 +0200109 // TODO(peah): Remove or reactivate this functionality.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200110 return absl::nullopt;
Per Åhgren5c532d32018-03-22 00:29:25 +0100111 }
112
peah522d71b2017-02-23 05:16:26 -0800113 // Updates the status on whether echo leakage is detected in the output of the
114 // echo remover.
115 void UpdateEchoLeakageStatus(bool leakage_detected) override {
116 echo_leakage_detected_ = leakage_detected;
117 }
peah69221db2017-01-27 03:28:19 -0800118
119 private:
Per Åhgren78026752018-08-01 16:24:08 +0200120 // Selects which of the shadow and main linear filter outputs that is most
Per Åhgren22754392018-08-10 18:37:38 +0200121 // appropriate to pass to the suppressor and forms the linear filter output by
122 // smoothly transition between those.
123 void FormLinearFilterOutput(bool smooth_transition,
124 const SubtractorOutput& subtractor_output,
125 rtc::ArrayView<float> output);
Per Åhgren78026752018-08-01 16:24:08 +0200126
peah522d71b2017-02-23 05:16:26 -0800127 static int instance_count_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200128 const EchoCanceller3Config config_;
peah522d71b2017-02-23 05:16:26 -0800129 const Aec3Fft fft_;
130 std::unique_ptr<ApmDataDumper> data_dumper_;
131 const Aec3Optimization optimization_;
peah69221db2017-01-27 03:28:19 -0800132 const int sample_rate_hz_;
Per Åhgren78026752018-08-01 16:24:08 +0200133 const bool use_shadow_filter_output_;
Per Åhgren22754392018-08-10 18:37:38 +0200134 const bool use_smooth_signal_transitions_;
peah522d71b2017-02-23 05:16:26 -0800135 Subtractor subtractor_;
136 SuppressionGain suppression_gain_;
137 ComfortNoiseGenerator cng_;
138 SuppressionFilter suppression_filter_;
peah522d71b2017-02-23 05:16:26 -0800139 RenderSignalAnalyzer render_signal_analyzer_;
peah522d71b2017-02-23 05:16:26 -0800140 ResidualEchoEstimator residual_echo_estimator_;
141 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800142 AecState aec_state_;
peahe985b3f2017-02-28 22:08:53 -0800143 EchoRemoverMetrics metrics_;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200144 std::array<float, kFftLengthBy2> e_old_;
145 std::array<float, kFftLengthBy2> x_old_;
146 std::array<float, kFftLengthBy2> y_old_;
Per Åhgren88cf0502018-07-16 17:08:41 +0200147 size_t block_counter_ = 0;
148 int gain_change_hangover_ = 0;
Per Åhgren22754392018-08-10 18:37:38 +0200149 bool main_filter_output_last_selected_ = true;
150 bool linear_filter_output_last_selected_ = true;
peah69221db2017-01-27 03:28:19 -0800151
152 RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl);
153};
154
peah522d71b2017-02-23 05:16:26 -0800155int EchoRemoverImpl::instance_count_ = 0;
156
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200157EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config,
158 int sample_rate_hz)
peah8cee56f2017-08-24 22:36:53 -0700159 : config_(config),
160 fft_(),
aleloi88b82b52017-02-23 06:27:03 -0800161 data_dumper_(
peah522d71b2017-02-23 05:16:26 -0800162 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
163 optimization_(DetectOptimization()),
164 sample_rate_hz_(sample_rate_hz),
Per Åhgren24021542018-08-31 07:34:29 +0200165 use_shadow_filter_output_(
166 UseShadowFilterOutput() &&
167 config_.filter.enable_shadow_filter_output_usage),
Per Åhgren22754392018-08-10 18:37:38 +0200168 use_smooth_signal_transitions_(UseSmoothSignalTransitions()),
Per Åhgren09a718a2017-12-11 22:28:45 +0100169 subtractor_(config, data_dumper_.get(), optimization_),
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200170 suppression_gain_(config_, optimization_, sample_rate_hz),
peah522d71b2017-02-23 05:16:26 -0800171 cng_(optimization_),
peah697a5902017-06-30 07:06:10 -0700172 suppression_filter_(sample_rate_hz_),
Per Åhgren971de072018-03-14 23:23:47 +0100173 render_signal_analyzer_(config_),
peah8cee56f2017-08-24 22:36:53 -0700174 residual_echo_estimator_(config_),
175 aec_state_(config_) {
peah522d71b2017-02-23 05:16:26 -0800176 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200177 x_old_.fill(0.f);
178 y_old_.fill(0.f);
179 e_old_.fill(0.f);
peah69221db2017-01-27 03:28:19 -0800180}
181
182EchoRemoverImpl::~EchoRemoverImpl() = default;
183
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100184void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const {
185 // Echo return loss (ERL) is inverted to go from gain to attenuation.
186 metrics->echo_return_loss = -10.0 * log10(aec_state_.ErlTimeDomain());
187 metrics->echo_return_loss_enhancement =
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200188 Log2TodB(aec_state_.ErleTimeDomainLog2());
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100189}
190
peahcf02cf12017-04-05 14:18:07 -0700191void EchoRemoverImpl::ProcessCapture(
Per Åhgren88cf0502018-07-16 17:08:41 +0200192 EchoPathVariability echo_path_variability,
peah69221db2017-01-27 03:28:19 -0800193 bool capture_signal_saturation,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200194 const absl::optional<DelayEstimate>& external_delay,
Per Åhgrenc59a5762017-12-11 21:34:19 +0100195 RenderBuffer* render_buffer,
peah69221db2017-01-27 03:28:19 -0800196 std::vector<std::vector<float>>* capture) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200197 ++block_counter_;
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100198 const std::vector<std::vector<float>>& x = render_buffer->Block(0);
peah522d71b2017-02-23 05:16:26 -0800199 std::vector<std::vector<float>>* y = capture;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100200 RTC_DCHECK(render_buffer);
peah522d71b2017-02-23 05:16:26 -0800201 RTC_DCHECK(y);
202 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
203 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
204 RTC_DCHECK_EQ(x[0].size(), kBlockSize);
205 RTC_DCHECK_EQ((*y)[0].size(), kBlockSize);
206 const std::vector<float>& x0 = x[0];
207 std::vector<float>& y0 = (*y)[0];
208
peah86afe9d2017-04-06 15:45:32 -0700209 data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0],
peah522d71b2017-02-23 05:16:26 -0800210 LowestBandRate(sample_rate_hz_), 1);
peah86afe9d2017-04-06 15:45:32 -0700211 data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0],
peah522d71b2017-02-23 05:16:26 -0800212 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700213 data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0);
214 data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0);
peah522d71b2017-02-23 05:16:26 -0800215
216 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
217
218 if (echo_path_variability.AudioPathChanged()) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200219 // Ensure that the gain change is only acted on once per frame.
220 if (echo_path_variability.gain_change) {
221 if (gain_change_hangover_ == 0) {
222 constexpr int kMaxBlocksPerFrame = 3;
223 gain_change_hangover_ = kMaxBlocksPerFrame;
224 RTC_LOG(LS_WARNING)
225 << "Gain change detected at block " << block_counter_;
226 } else {
227 echo_path_variability.gain_change = false;
228 }
229 }
230
peah522d71b2017-02-23 05:16:26 -0800231 subtractor_.HandleEchoPathChange(echo_path_variability);
peah86afe9d2017-04-06 15:45:32 -0700232 aec_state_.HandleEchoPathChange(echo_path_variability);
Per Åhgren88cf0502018-07-16 17:08:41 +0200233
234 if (echo_path_variability.delay_change !=
235 EchoPathVariability::DelayAdjustment::kNone) {
236 suppression_gain_.SetInitialState(true);
Per Åhgren88cf0502018-07-16 17:08:41 +0200237 }
238 }
239 if (gain_change_hangover_ > 0) {
240 --gain_change_hangover_;
peah522d71b2017-02-23 05:16:26 -0800241 }
242
243 std::array<float, kFftLengthBy2Plus1> Y2;
Per Åhgren169c7fd2018-04-27 12:04:03 +0200244 std::array<float, kFftLengthBy2Plus1> E2;
peah522d71b2017-02-23 05:16:26 -0800245 std::array<float, kFftLengthBy2Plus1> R2;
246 std::array<float, kFftLengthBy2Plus1> S2_linear;
247 std::array<float, kFftLengthBy2Plus1> G;
peah86afe9d2017-04-06 15:45:32 -0700248 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -0800249 FftData Y;
Per Åhgren169c7fd2018-04-27 12:04:03 +0200250 FftData E;
peah522d71b2017-02-23 05:16:26 -0800251 FftData comfort_noise;
252 FftData high_band_comfort_noise;
253 SubtractorOutput subtractor_output;
peah522d71b2017-02-23 05:16:26 -0800254
peah522d71b2017-02-23 05:16:26 -0800255 // Analyze the render signal.
Per Åhgren5c532d32018-03-22 00:29:25 +0100256 render_signal_analyzer_.Update(*render_buffer,
257 aec_state_.FilterDelayBlocks());
peah522d71b2017-02-23 05:16:26 -0800258
259 // Perform linear echo cancellation.
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200260 if (aec_state_.TransitionTriggered()) {
Per Åhgrena98c8072018-01-15 19:17:16 +0100261 subtractor_.ExitInitialState();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100262 suppression_gain_.SetInitialState(false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100263 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100264
265 // If the delay is known, use the echo subtractor.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100266 subtractor_.Process(*render_buffer, y0, render_signal_analyzer_, aec_state_,
peah86afe9d2017-04-06 15:45:32 -0700267 &subtractor_output);
Per Åhgren22754392018-08-10 18:37:38 +0200268 std::array<float, kBlockSize> e;
269 FormLinearFilterOutput(use_smooth_signal_transitions_, subtractor_output, e);
peah522d71b2017-02-23 05:16:26 -0800270
271 // Compute spectra.
Per Åhgren169c7fd2018-04-27 12:04:03 +0200272 WindowedPaddedFft(fft_, y0, y_old_, &Y);
273 WindowedPaddedFft(fft_, e, e_old_, &E);
274 LinearEchoPower(E, Y, &S2_linear);
Per Åhgren8ba58612017-12-01 23:01:44 +0100275 Y.Spectrum(optimization_, Y2);
Per Åhgren169c7fd2018-04-27 12:04:03 +0200276 E.Spectrum(optimization_, E2);
peah522d71b2017-02-23 05:16:26 -0800277
278 // Update the AEC state information.
Per Åhgren5c532d32018-03-22 00:29:25 +0100279 aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponse(),
Per Åhgrenb20b9372018-07-13 00:22:54 +0200280 subtractor_.FilterImpulseResponse(), *render_buffer, E2, Y2,
281 subtractor_output, y0);
Per Åhgren169c7fd2018-04-27 12:04:03 +0200282
peah522d71b2017-02-23 05:16:26 -0800283 // Choose the linear output.
Per Åhgren169c7fd2018-04-27 12:04:03 +0200284 data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0],
Per Åhgren5c532d32018-03-22 00:29:25 +0100285 LowestBandRate(sample_rate_hz_), 1);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200286 if (aec_state_.UseLinearFilterOutput()) {
Per Åhgren22754392018-08-10 18:37:38 +0200287 if (!linear_filter_output_last_selected_ &&
288 use_smooth_signal_transitions_) {
289 SignalTransition(y0, e, y0);
290 } else {
291 std::copy(e.begin(), e.end(), y0.begin());
292 }
293 } else {
294 if (linear_filter_output_last_selected_ && use_smooth_signal_transitions_) {
295 SignalTransition(e, y0, y0);
296 }
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200297 }
Per Åhgren22754392018-08-10 18:37:38 +0200298 linear_filter_output_last_selected_ = aec_state_.UseLinearFilterOutput();
Per Åhgren169c7fd2018-04-27 12:04:03 +0200299 const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y;
300
peah522d71b2017-02-23 05:16:26 -0800301 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0],
302 LowestBandRate(sample_rate_hz_), 1);
peah522d71b2017-02-23 05:16:26 -0800303
304 // Estimate the residual echo power.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100305 residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2,
peah86afe9d2017-04-06 15:45:32 -0700306 &R2);
peah522d71b2017-02-23 05:16:26 -0800307
308 // Estimate the comfort noise.
309 cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise);
310
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200311 // Compute and apply the suppression gain.
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200312 const auto& echo_spectrum =
313 aec_state_.UsableLinearEstimate() ? S2_linear : R2;
314 suppression_gain_.GetGain(E2, echo_spectrum, R2, cng_.NoiseSpectrum(), E, Y,
Per Åhgren7ddd4632017-10-25 02:59:45 +0200315 render_signal_analyzer_, aec_state_, x,
316 &high_bands_gain, &G);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200317
peah86afe9d2017-04-06 15:45:32 -0700318 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200319 high_bands_gain, Y_fft, y);
peah522d71b2017-02-23 05:16:26 -0800320
peahe985b3f2017-02-28 22:08:53 -0800321 // Update the metrics.
322 metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G);
323
peah522d71b2017-02-23 05:16:26 -0800324 // Debug outputs for the purpose of development and analysis.
peah29103572017-07-11 02:54:02 -0700325 data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
326 &subtractor_output.s_main[0],
327 LowestBandRate(sample_rate_hz_), 1);
328 data_dumper_->DumpRaw("aec3_output", y0);
peah14c11a42017-07-11 06:13:43 -0700329 data_dumper_->DumpRaw("aec3_narrow_render",
330 render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800331 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum());
332 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
333 data_dumper_->DumpWav("aec3_output",
334 rtc::ArrayView<const float>(&y0[0], kBlockSize),
335 LowestBandRate(sample_rate_hz_), 1);
336 data_dumper_->DumpRaw("aec3_using_subtractor_output",
Per Åhgren5c532d32018-03-22 00:29:25 +0100337 aec_state_.UseLinearFilterOutput() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800338 data_dumper_->DumpRaw("aec3_E2", E2);
peah522d71b2017-02-23 05:16:26 -0800339 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear);
peah522d71b2017-02-23 05:16:26 -0800340 data_dumper_->DumpRaw("aec3_Y2", Y2);
Jesús de Vicente Peña7682c6e2018-03-22 14:53:23 +0100341 data_dumper_->DumpRaw(
342 "aec3_X2", render_buffer->Spectrum(aec_state_.FilterDelayBlocks()));
peah522d71b2017-02-23 05:16:26 -0800343 data_dumper_->DumpRaw("aec3_R2", R2);
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200344 data_dumper_->DumpRaw("aec3_R2_reverb",
345 residual_echo_estimator_.GetReverbPowerSpectrum());
Per Åhgren5c532d32018-03-22 00:29:25 +0100346 data_dumper_->DumpRaw("aec3_filter_delay", aec_state_.FilterDelayBlocks());
peah522d71b2017-02-23 05:16:26 -0800347 data_dumper_->DumpRaw("aec3_capture_saturation",
348 aec_state_.SaturatedCapture() ? 1 : 0);
349}
peah69221db2017-01-27 03:28:19 -0800350
Per Åhgren22754392018-08-10 18:37:38 +0200351void EchoRemoverImpl::FormLinearFilterOutput(
352 bool smooth_transition,
353 const SubtractorOutput& subtractor_output,
354 rtc::ArrayView<float> output) {
355 RTC_DCHECK_EQ(subtractor_output.e_main.size(), output.size());
356 RTC_DCHECK_EQ(subtractor_output.e_shadow.size(), output.size());
357 bool use_main_output = true;
358 if (use_shadow_filter_output_) {
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200359 // As the output of the main adaptive filter generally should be better
360 // than the shadow filter output, add a margin and threshold for when
361 // choosing the shadow filter output.
Per Åhgren22754392018-08-10 18:37:38 +0200362 if (subtractor_output.e2_shadow < 0.9f * subtractor_output.e2_main &&
363 subtractor_output.y2 > 30.f * 30.f * kBlockSize &&
364 (subtractor_output.s2_main > 60.f * 60.f * kBlockSize ||
365 subtractor_output.s2_shadow > 60.f * 60.f * kBlockSize)) {
366 use_main_output = false;
367 } else {
368 // If the main filter is diverged, choose the filter output that has the
369 // lowest power.
370 if (subtractor_output.e2_shadow < subtractor_output.e2_main &&
371 subtractor_output.y2 < subtractor_output.e2_main) {
372 use_main_output = false;
373 }
374 }
375 }
376
377 if (use_main_output) {
378 if (!main_filter_output_last_selected_ && smooth_transition) {
379 SignalTransition(subtractor_output.e_shadow, subtractor_output.e_main,
380 output);
381 } else {
382 std::copy(subtractor_output.e_main.begin(),
383 subtractor_output.e_main.end(), output.begin());
384 }
385 } else {
386 if (main_filter_output_last_selected_ && smooth_transition) {
387 SignalTransition(subtractor_output.e_main, subtractor_output.e_shadow,
388 output);
389 } else {
390 std::copy(subtractor_output.e_shadow.begin(),
391 subtractor_output.e_shadow.end(), output.begin());
392 }
393 }
394 main_filter_output_last_selected_ = use_main_output;
395}
396
peah69221db2017-01-27 03:28:19 -0800397} // namespace
398
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200399EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config,
400 int sample_rate_hz) {
peah697a5902017-06-30 07:06:10 -0700401 return new EchoRemoverImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800402}
403
404} // namespace webrtc