blob: 5b3740f6bb71fc20561fadd6ca7e5c2dd9ee2063 [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>
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stddef.h>
peah69221db2017-01-27 03:28:19 -080014#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <array>
peah522d71b2017-02-23 05:16:26 -080016#include <memory>
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"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "modules/audio_processing/aec3/aec3_fft.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/aec3/aec_state.h"
22#include "modules/audio_processing/aec3/comfort_noise_generator.h"
23#include "modules/audio_processing/aec3/echo_path_variability.h"
24#include "modules/audio_processing/aec3/echo_remover_metrics.h"
25#include "modules/audio_processing/aec3/fft_data.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/aec3/render_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/aec3/residual_echo_estimator.h"
29#include "modules/audio_processing/aec3/subtractor.h"
Yves Gerey988cc082018-10-23 12:03:01 +020030#include "modules/audio_processing/aec3/subtractor_output.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_processing/aec3/suppression_filter.h"
32#include "modules/audio_processing/aec3/suppression_gain.h"
33#include "modules/audio_processing/logging/apm_data_dumper.h"
34#include "rtc_base/atomicops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020035#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/constructormagic.h"
Per Åhgren88cf0502018-07-16 17:08:41 +020037#include "rtc_base/logging.h"
Per Åhgren78026752018-08-01 16:24:08 +020038#include "system_wrappers/include/field_trial.h"
peah69221db2017-01-27 03:28:19 -080039
40namespace webrtc {
41
42namespace {
peah522d71b2017-02-23 05:16:26 -080043
Per Åhgren78026752018-08-01 16:24:08 +020044bool UseShadowFilterOutput() {
45 return !field_trial::IsEnabled(
46 "WebRTC-Aec3UtilizeShadowFilterOutputKillSwitch");
47}
48
Per Åhgren22754392018-08-10 18:37:38 +020049bool UseSmoothSignalTransitions() {
50 return !field_trial::IsEnabled(
51 "WebRTC-Aec3SmoothSignalTransitionsKillSwitch");
52}
53
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020054bool EnableBoundedNearend() {
55 return !field_trial::IsEnabled("WebRTC-Aec3BoundedNearendKillSwitch");
56}
57
peah522d71b2017-02-23 05:16:26 -080058void LinearEchoPower(const FftData& E,
59 const FftData& Y,
60 std::array<float, kFftLengthBy2Plus1>* S2) {
61 for (size_t k = 0; k < E.re.size(); ++k) {
62 (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) +
63 (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]);
64 }
65}
66
Per Åhgren22754392018-08-10 18:37:38 +020067// Fades between two input signals using a fix-sized transition.
68void SignalTransition(rtc::ArrayView<const float> from,
69 rtc::ArrayView<const float> to,
70 rtc::ArrayView<float> out) {
71 constexpr size_t kTransitionSize = 30;
Gustaf Ullbergddb82a62018-09-11 12:55:23 +020072 constexpr float kOneByTransitionSizePlusOne = 1.f / (kTransitionSize + 1);
Per Åhgren22754392018-08-10 18:37:38 +020073
74 RTC_DCHECK_EQ(from.size(), to.size());
75 RTC_DCHECK_EQ(from.size(), out.size());
76 RTC_DCHECK_LE(kTransitionSize, out.size());
77
78 for (size_t k = 0; k < kTransitionSize; ++k) {
Gustaf Ullbergddb82a62018-09-11 12:55:23 +020079 float a = (k + 1) * kOneByTransitionSizePlusOne;
80 out[k] = a * to[k] + (1.f - a) * from[k];
Per Åhgren22754392018-08-10 18:37:38 +020081 }
82
83 std::copy(to.begin() + kTransitionSize, to.end(),
84 out.begin() + kTransitionSize);
85}
86
Per Åhgren169c7fd2018-04-27 12:04:03 +020087// Computes a windowed (square root Hanning) padded FFT and updates the related
88// memory.
89void WindowedPaddedFft(const Aec3Fft& fft,
90 rtc::ArrayView<const float> v,
91 rtc::ArrayView<float> v_old,
92 FftData* V) {
93 fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V);
94 std::copy(v.begin(), v.end(), v_old.begin());
95}
96
peah522d71b2017-02-23 05:16:26 -080097// Class for removing the echo from the capture signal.
peah69221db2017-01-27 03:28:19 -080098class EchoRemoverImpl final : public EchoRemover {
99 public:
Per Åhgren5c532d32018-03-22 00:29:25 +0100100 EchoRemoverImpl(const EchoCanceller3Config& config, int sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800101 ~EchoRemoverImpl() override;
102
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100103 void GetMetrics(EchoControl::Metrics* metrics) const override;
104
peah522d71b2017-02-23 05:16:26 -0800105 // Removes the echo from a block of samples from the capture signal. The
106 // supplied render signal is assumed to be pre-aligned with the capture
107 // signal.
Per Åhgren88cf0502018-07-16 17:08:41 +0200108 void ProcessCapture(EchoPathVariability echo_path_variability,
Alex Loiko890988c2017-08-31 10:25:48 +0200109 bool capture_signal_saturation,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200110 const absl::optional<DelayEstimate>& external_delay,
Per Åhgrenc59a5762017-12-11 21:34:19 +0100111 RenderBuffer* render_buffer,
Alex Loiko890988c2017-08-31 10:25:48 +0200112 std::vector<std::vector<float>>* capture) override;
peah69221db2017-01-27 03:28:19 -0800113
Per Åhgren5c532d32018-03-22 00:29:25 +0100114 // Returns the internal delay estimate in blocks.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200115 absl::optional<int> Delay() const override {
Per Åhgrene05c43c2018-05-09 12:26:51 +0200116 // TODO(peah): Remove or reactivate this functionality.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200117 return absl::nullopt;
Per Åhgren5c532d32018-03-22 00:29:25 +0100118 }
119
peah522d71b2017-02-23 05:16:26 -0800120 // Updates the status on whether echo leakage is detected in the output of the
121 // echo remover.
122 void UpdateEchoLeakageStatus(bool leakage_detected) override {
123 echo_leakage_detected_ = leakage_detected;
124 }
peah69221db2017-01-27 03:28:19 -0800125
126 private:
Per Åhgren78026752018-08-01 16:24:08 +0200127 // Selects which of the shadow and main linear filter outputs that is most
Per Åhgren22754392018-08-10 18:37:38 +0200128 // appropriate to pass to the suppressor and forms the linear filter output by
129 // smoothly transition between those.
130 void FormLinearFilterOutput(bool smooth_transition,
131 const SubtractorOutput& subtractor_output,
132 rtc::ArrayView<float> output);
Per Åhgren78026752018-08-01 16:24:08 +0200133
peah522d71b2017-02-23 05:16:26 -0800134 static int instance_count_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200135 const EchoCanceller3Config config_;
peah522d71b2017-02-23 05:16:26 -0800136 const Aec3Fft fft_;
137 std::unique_ptr<ApmDataDumper> data_dumper_;
138 const Aec3Optimization optimization_;
peah69221db2017-01-27 03:28:19 -0800139 const int sample_rate_hz_;
Per Åhgren78026752018-08-01 16:24:08 +0200140 const bool use_shadow_filter_output_;
Per Åhgren22754392018-08-10 18:37:38 +0200141 const bool use_smooth_signal_transitions_;
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200142 const bool enable_bounded_nearend_;
peah522d71b2017-02-23 05:16:26 -0800143 Subtractor subtractor_;
144 SuppressionGain suppression_gain_;
145 ComfortNoiseGenerator cng_;
146 SuppressionFilter suppression_filter_;
peah522d71b2017-02-23 05:16:26 -0800147 RenderSignalAnalyzer render_signal_analyzer_;
peah522d71b2017-02-23 05:16:26 -0800148 ResidualEchoEstimator residual_echo_estimator_;
149 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800150 AecState aec_state_;
peahe985b3f2017-02-28 22:08:53 -0800151 EchoRemoverMetrics metrics_;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200152 std::array<float, kFftLengthBy2> e_old_;
153 std::array<float, kFftLengthBy2> x_old_;
154 std::array<float, kFftLengthBy2> y_old_;
Per Åhgren88cf0502018-07-16 17:08:41 +0200155 size_t block_counter_ = 0;
156 int gain_change_hangover_ = 0;
Per Åhgren22754392018-08-10 18:37:38 +0200157 bool main_filter_output_last_selected_ = true;
158 bool linear_filter_output_last_selected_ = true;
peah69221db2017-01-27 03:28:19 -0800159
160 RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl);
161};
162
peah522d71b2017-02-23 05:16:26 -0800163int EchoRemoverImpl::instance_count_ = 0;
164
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200165EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config,
166 int sample_rate_hz)
peah8cee56f2017-08-24 22:36:53 -0700167 : config_(config),
168 fft_(),
aleloi88b82b52017-02-23 06:27:03 -0800169 data_dumper_(
peah522d71b2017-02-23 05:16:26 -0800170 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
171 optimization_(DetectOptimization()),
172 sample_rate_hz_(sample_rate_hz),
Per Åhgren24021542018-08-31 07:34:29 +0200173 use_shadow_filter_output_(
174 UseShadowFilterOutput() &&
175 config_.filter.enable_shadow_filter_output_usage),
Per Åhgren22754392018-08-10 18:37:38 +0200176 use_smooth_signal_transitions_(UseSmoothSignalTransitions()),
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200177 enable_bounded_nearend_(EnableBoundedNearend()),
Per Åhgren09a718a2017-12-11 22:28:45 +0100178 subtractor_(config, data_dumper_.get(), optimization_),
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200179 suppression_gain_(config_, optimization_, sample_rate_hz),
peah522d71b2017-02-23 05:16:26 -0800180 cng_(optimization_),
peah697a5902017-06-30 07:06:10 -0700181 suppression_filter_(sample_rate_hz_),
Per Åhgren971de072018-03-14 23:23:47 +0100182 render_signal_analyzer_(config_),
peah8cee56f2017-08-24 22:36:53 -0700183 residual_echo_estimator_(config_),
184 aec_state_(config_) {
peah522d71b2017-02-23 05:16:26 -0800185 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200186 x_old_.fill(0.f);
187 y_old_.fill(0.f);
188 e_old_.fill(0.f);
peah69221db2017-01-27 03:28:19 -0800189}
190
191EchoRemoverImpl::~EchoRemoverImpl() = default;
192
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100193void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const {
194 // Echo return loss (ERL) is inverted to go from gain to attenuation.
195 metrics->echo_return_loss = -10.0 * log10(aec_state_.ErlTimeDomain());
196 metrics->echo_return_loss_enhancement =
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200197 Log2TodB(aec_state_.FullBandErleLog2());
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100198}
199
peahcf02cf12017-04-05 14:18:07 -0700200void EchoRemoverImpl::ProcessCapture(
Per Åhgren88cf0502018-07-16 17:08:41 +0200201 EchoPathVariability echo_path_variability,
peah69221db2017-01-27 03:28:19 -0800202 bool capture_signal_saturation,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200203 const absl::optional<DelayEstimate>& external_delay,
Per Åhgrenc59a5762017-12-11 21:34:19 +0100204 RenderBuffer* render_buffer,
peah69221db2017-01-27 03:28:19 -0800205 std::vector<std::vector<float>>* capture) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200206 ++block_counter_;
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100207 const std::vector<std::vector<float>>& x = render_buffer->Block(0);
peah522d71b2017-02-23 05:16:26 -0800208 std::vector<std::vector<float>>* y = capture;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100209 RTC_DCHECK(render_buffer);
peah522d71b2017-02-23 05:16:26 -0800210 RTC_DCHECK(y);
211 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
212 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
213 RTC_DCHECK_EQ(x[0].size(), kBlockSize);
214 RTC_DCHECK_EQ((*y)[0].size(), kBlockSize);
215 const std::vector<float>& x0 = x[0];
216 std::vector<float>& y0 = (*y)[0];
217
peah86afe9d2017-04-06 15:45:32 -0700218 data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0],
peah522d71b2017-02-23 05:16:26 -0800219 LowestBandRate(sample_rate_hz_), 1);
peah86afe9d2017-04-06 15:45:32 -0700220 data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0],
peah522d71b2017-02-23 05:16:26 -0800221 LowestBandRate(sample_rate_hz_), 1);
peah29103572017-07-11 02:54:02 -0700222 data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0);
223 data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0);
peah522d71b2017-02-23 05:16:26 -0800224
225 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
226
227 if (echo_path_variability.AudioPathChanged()) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200228 // Ensure that the gain change is only acted on once per frame.
229 if (echo_path_variability.gain_change) {
230 if (gain_change_hangover_ == 0) {
231 constexpr int kMaxBlocksPerFrame = 3;
232 gain_change_hangover_ = kMaxBlocksPerFrame;
233 RTC_LOG(LS_WARNING)
234 << "Gain change detected at block " << block_counter_;
235 } else {
236 echo_path_variability.gain_change = false;
237 }
238 }
239
peah522d71b2017-02-23 05:16:26 -0800240 subtractor_.HandleEchoPathChange(echo_path_variability);
peah86afe9d2017-04-06 15:45:32 -0700241 aec_state_.HandleEchoPathChange(echo_path_variability);
Per Åhgren88cf0502018-07-16 17:08:41 +0200242
243 if (echo_path_variability.delay_change !=
244 EchoPathVariability::DelayAdjustment::kNone) {
245 suppression_gain_.SetInitialState(true);
Per Åhgren88cf0502018-07-16 17:08:41 +0200246 }
247 }
248 if (gain_change_hangover_ > 0) {
249 --gain_change_hangover_;
peah522d71b2017-02-23 05:16:26 -0800250 }
251
252 std::array<float, kFftLengthBy2Plus1> Y2;
Per Åhgren169c7fd2018-04-27 12:04:03 +0200253 std::array<float, kFftLengthBy2Plus1> E2;
peah522d71b2017-02-23 05:16:26 -0800254 std::array<float, kFftLengthBy2Plus1> R2;
255 std::array<float, kFftLengthBy2Plus1> S2_linear;
256 std::array<float, kFftLengthBy2Plus1> G;
peah86afe9d2017-04-06 15:45:32 -0700257 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -0800258 FftData Y;
Per Åhgren169c7fd2018-04-27 12:04:03 +0200259 FftData E;
peah522d71b2017-02-23 05:16:26 -0800260 FftData comfort_noise;
261 FftData high_band_comfort_noise;
262 SubtractorOutput subtractor_output;
peah522d71b2017-02-23 05:16:26 -0800263
peah522d71b2017-02-23 05:16:26 -0800264 // Analyze the render signal.
Per Åhgren5c532d32018-03-22 00:29:25 +0100265 render_signal_analyzer_.Update(*render_buffer,
266 aec_state_.FilterDelayBlocks());
peah522d71b2017-02-23 05:16:26 -0800267
268 // Perform linear echo cancellation.
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200269 if (aec_state_.TransitionTriggered()) {
Per Åhgrena98c8072018-01-15 19:17:16 +0100270 subtractor_.ExitInitialState();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100271 suppression_gain_.SetInitialState(false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100272 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100273
274 // If the delay is known, use the echo subtractor.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100275 subtractor_.Process(*render_buffer, y0, render_signal_analyzer_, aec_state_,
peah86afe9d2017-04-06 15:45:32 -0700276 &subtractor_output);
Per Åhgren22754392018-08-10 18:37:38 +0200277 std::array<float, kBlockSize> e;
278 FormLinearFilterOutput(use_smooth_signal_transitions_, subtractor_output, e);
peah522d71b2017-02-23 05:16:26 -0800279
280 // Compute spectra.
Per Åhgren169c7fd2018-04-27 12:04:03 +0200281 WindowedPaddedFft(fft_, y0, y_old_, &Y);
282 WindowedPaddedFft(fft_, e, e_old_, &E);
283 LinearEchoPower(E, Y, &S2_linear);
Per Åhgren8ba58612017-12-01 23:01:44 +0100284 Y.Spectrum(optimization_, Y2);
Per Åhgren169c7fd2018-04-27 12:04:03 +0200285 E.Spectrum(optimization_, E2);
peah522d71b2017-02-23 05:16:26 -0800286
287 // Update the AEC state information.
Per Åhgren5c532d32018-03-22 00:29:25 +0100288 aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponse(),
Per Åhgrenb20b9372018-07-13 00:22:54 +0200289 subtractor_.FilterImpulseResponse(), *render_buffer, E2, Y2,
290 subtractor_output, y0);
Per Åhgren169c7fd2018-04-27 12:04:03 +0200291
peah522d71b2017-02-23 05:16:26 -0800292 // Choose the linear output.
Per Åhgren169c7fd2018-04-27 12:04:03 +0200293 data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0],
Per Åhgren5c532d32018-03-22 00:29:25 +0100294 LowestBandRate(sample_rate_hz_), 1);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200295 if (aec_state_.UseLinearFilterOutput()) {
Per Åhgren22754392018-08-10 18:37:38 +0200296 if (!linear_filter_output_last_selected_ &&
297 use_smooth_signal_transitions_) {
298 SignalTransition(y0, e, y0);
299 } else {
300 std::copy(e.begin(), e.end(), y0.begin());
301 }
302 } else {
303 if (linear_filter_output_last_selected_ && use_smooth_signal_transitions_) {
304 SignalTransition(e, y0, y0);
305 }
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200306 }
Per Åhgren22754392018-08-10 18:37:38 +0200307 linear_filter_output_last_selected_ = aec_state_.UseLinearFilterOutput();
Per Åhgren169c7fd2018-04-27 12:04:03 +0200308 const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y;
309
peah522d71b2017-02-23 05:16:26 -0800310 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0],
311 LowestBandRate(sample_rate_hz_), 1);
peah522d71b2017-02-23 05:16:26 -0800312
313 // Estimate the residual echo power.
Per Åhgrenc59a5762017-12-11 21:34:19 +0100314 residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2,
peah86afe9d2017-04-06 15:45:32 -0700315 &R2);
peah522d71b2017-02-23 05:16:26 -0800316
317 // Estimate the comfort noise.
318 cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise);
319
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200320 // Compute and apply the suppression gain.
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200321 const auto& echo_spectrum =
322 aec_state_.UsableLinearEstimate() ? S2_linear : R2;
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200323
324 std::array<float, kFftLengthBy2Plus1> E2_bounded;
325 if (enable_bounded_nearend_) {
326 std::transform(E2.begin(), E2.end(), Y2.begin(), E2_bounded.begin(),
327 [](float a, float b) { return std::min(a, b); });
328 } else {
329 std::copy(E2.begin(), E2.end(), E2_bounded.begin());
330 }
331
332 suppression_gain_.GetGain(E2, E2_bounded, echo_spectrum, R2,
333 cng_.NoiseSpectrum(), E, Y, render_signal_analyzer_,
334 aec_state_, x, &high_bands_gain, &G);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200335
peah86afe9d2017-04-06 15:45:32 -0700336 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200337 high_bands_gain, Y_fft, y);
peah522d71b2017-02-23 05:16:26 -0800338
peahe985b3f2017-02-28 22:08:53 -0800339 // Update the metrics.
340 metrics_.Update(aec_state_, cng_.NoiseSpectrum(), G);
341
peah522d71b2017-02-23 05:16:26 -0800342 // Debug outputs for the purpose of development and analysis.
peah29103572017-07-11 02:54:02 -0700343 data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
344 &subtractor_output.s_main[0],
345 LowestBandRate(sample_rate_hz_), 1);
346 data_dumper_->DumpRaw("aec3_output", y0);
peah14c11a42017-07-11 06:13:43 -0700347 data_dumper_->DumpRaw("aec3_narrow_render",
348 render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800349 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum());
350 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
351 data_dumper_->DumpWav("aec3_output",
352 rtc::ArrayView<const float>(&y0[0], kBlockSize),
353 LowestBandRate(sample_rate_hz_), 1);
354 data_dumper_->DumpRaw("aec3_using_subtractor_output",
Per Åhgren5c532d32018-03-22 00:29:25 +0100355 aec_state_.UseLinearFilterOutput() ? 1 : 0);
peah522d71b2017-02-23 05:16:26 -0800356 data_dumper_->DumpRaw("aec3_E2", E2);
peah522d71b2017-02-23 05:16:26 -0800357 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear);
peah522d71b2017-02-23 05:16:26 -0800358 data_dumper_->DumpRaw("aec3_Y2", Y2);
Jesús de Vicente Peña7682c6e2018-03-22 14:53:23 +0100359 data_dumper_->DumpRaw(
360 "aec3_X2", render_buffer->Spectrum(aec_state_.FilterDelayBlocks()));
peah522d71b2017-02-23 05:16:26 -0800361 data_dumper_->DumpRaw("aec3_R2", R2);
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200362 data_dumper_->DumpRaw("aec3_R2_reverb",
363 residual_echo_estimator_.GetReverbPowerSpectrum());
Per Åhgren5c532d32018-03-22 00:29:25 +0100364 data_dumper_->DumpRaw("aec3_filter_delay", aec_state_.FilterDelayBlocks());
peah522d71b2017-02-23 05:16:26 -0800365 data_dumper_->DumpRaw("aec3_capture_saturation",
366 aec_state_.SaturatedCapture() ? 1 : 0);
367}
peah69221db2017-01-27 03:28:19 -0800368
Per Åhgren22754392018-08-10 18:37:38 +0200369void EchoRemoverImpl::FormLinearFilterOutput(
370 bool smooth_transition,
371 const SubtractorOutput& subtractor_output,
372 rtc::ArrayView<float> output) {
373 RTC_DCHECK_EQ(subtractor_output.e_main.size(), output.size());
374 RTC_DCHECK_EQ(subtractor_output.e_shadow.size(), output.size());
375 bool use_main_output = true;
376 if (use_shadow_filter_output_) {
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200377 // As the output of the main adaptive filter generally should be better
378 // than the shadow filter output, add a margin and threshold for when
379 // choosing the shadow filter output.
Per Åhgren22754392018-08-10 18:37:38 +0200380 if (subtractor_output.e2_shadow < 0.9f * subtractor_output.e2_main &&
381 subtractor_output.y2 > 30.f * 30.f * kBlockSize &&
382 (subtractor_output.s2_main > 60.f * 60.f * kBlockSize ||
383 subtractor_output.s2_shadow > 60.f * 60.f * kBlockSize)) {
384 use_main_output = false;
385 } else {
386 // If the main filter is diverged, choose the filter output that has the
387 // lowest power.
388 if (subtractor_output.e2_shadow < subtractor_output.e2_main &&
389 subtractor_output.y2 < subtractor_output.e2_main) {
390 use_main_output = false;
391 }
392 }
393 }
394
395 if (use_main_output) {
396 if (!main_filter_output_last_selected_ && smooth_transition) {
397 SignalTransition(subtractor_output.e_shadow, subtractor_output.e_main,
398 output);
399 } else {
400 std::copy(subtractor_output.e_main.begin(),
401 subtractor_output.e_main.end(), output.begin());
402 }
403 } else {
404 if (main_filter_output_last_selected_ && smooth_transition) {
405 SignalTransition(subtractor_output.e_main, subtractor_output.e_shadow,
406 output);
407 } else {
408 std::copy(subtractor_output.e_shadow.begin(),
409 subtractor_output.e_shadow.end(), output.begin());
410 }
411 }
412 main_filter_output_last_selected_ = use_main_output;
413}
414
peah69221db2017-01-27 03:28:19 -0800415} // namespace
416
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200417EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config,
418 int sample_rate_hz) {
peah697a5902017-06-30 07:06:10 -0700419 return new EchoRemoverImpl(config, sample_rate_hz);
peah69221db2017-01-27 03:28:19 -0800420}
421
422} // namespace webrtc