blob: 89ba736a9ffcece2074987c0ecd95fa7595bef65 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
peah69221db2017-01-27 03:28:19 -080015#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <array>
Mirko Bonadeidbce0902019-03-15 07:39:02 +010017#include <cmath>
peah522d71b2017-02-23 05:16:26 -080018#include <memory>
peah69221db2017-01-27 03:28:19 -080019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/array_view.h"
21#include "modules/audio_processing/aec3/aec3_common.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "modules/audio_processing/aec3/aec3_fft.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/aec3/aec_state.h"
24#include "modules/audio_processing/aec3/comfort_noise_generator.h"
25#include "modules/audio_processing/aec3/echo_path_variability.h"
26#include "modules/audio_processing/aec3/echo_remover_metrics.h"
27#include "modules/audio_processing/aec3/fft_data.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/aec3/render_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020029#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_processing/aec3/residual_echo_estimator.h"
31#include "modules/audio_processing/aec3/subtractor.h"
Yves Gerey988cc082018-10-23 12:03:01 +020032#include "modules/audio_processing/aec3/subtractor_output.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "modules/audio_processing/aec3/suppression_filter.h"
34#include "modules/audio_processing/aec3/suppression_gain.h"
35#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/atomic_ops.h"
Yves Gerey988cc082018-10-23 12:03:01 +020037#include "rtc_base/checks.h"
Per Åhgren88cf0502018-07-16 17:08:41 +020038#include "rtc_base/logging.h"
peah69221db2017-01-27 03:28:19 -080039
40namespace webrtc {
41
42namespace {
peah522d71b2017-02-23 05:16:26 -080043
Per Åhgrenf6aa5722019-09-10 18:05:17 +020044// Maximum number of channels for which the capture channel data is stored on
45// the stack. If the number of channels are larger than this, they are stored
46// using scratch memory that is pre-allocated on the heap. The reason for this
47// partitioning is not to waste heap space for handling the more common numbers
48// of channels, while at the same time not limiting the support for higher
49// numbers of channels by enforcing the capture channel data to be stored on the
50// stack using a fixed maximum value.
51constexpr size_t kMaxNumChannelsOnStack = 2;
52
53// Chooses the number of channels to store on the heap when that is required due
54// to the number of capture channels being larger than the pre-defined number
55// of channels to store on the stack.
56size_t NumChannelsOnHeap(size_t num_capture_channels) {
57 return num_capture_channels > kMaxNumChannelsOnStack ? num_capture_channels
58 : 0;
59}
60
peah522d71b2017-02-23 05:16:26 -080061void LinearEchoPower(const FftData& E,
62 const FftData& Y,
63 std::array<float, kFftLengthBy2Plus1>* S2) {
64 for (size_t k = 0; k < E.re.size(); ++k) {
65 (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) +
66 (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]);
67 }
68}
69
Per Åhgren22754392018-08-10 18:37:38 +020070// Fades between two input signals using a fix-sized transition.
71void SignalTransition(rtc::ArrayView<const float> from,
72 rtc::ArrayView<const float> to,
73 rtc::ArrayView<float> out) {
Gustaf Ullberg7911d372019-09-24 16:31:01 +020074 if (from == to) {
75 RTC_DCHECK_EQ(to.size(), out.size());
76 std::copy(to.begin(), to.end(), out.begin());
77 } else {
78 constexpr size_t kTransitionSize = 30;
79 constexpr float kOneByTransitionSizePlusOne = 1.f / (kTransitionSize + 1);
Per Åhgren22754392018-08-10 18:37:38 +020080
Gustaf Ullberg7911d372019-09-24 16:31:01 +020081 RTC_DCHECK_EQ(from.size(), to.size());
82 RTC_DCHECK_EQ(from.size(), out.size());
83 RTC_DCHECK_LE(kTransitionSize, out.size());
Per Åhgren22754392018-08-10 18:37:38 +020084
Gustaf Ullberg7911d372019-09-24 16:31:01 +020085 for (size_t k = 0; k < kTransitionSize; ++k) {
86 float a = (k + 1) * kOneByTransitionSizePlusOne;
87 out[k] = a * to[k] + (1.f - a) * from[k];
88 }
89
90 std::copy(to.begin() + kTransitionSize, to.end(),
91 out.begin() + kTransitionSize);
Per Åhgren22754392018-08-10 18:37:38 +020092 }
Per Åhgren22754392018-08-10 18:37:38 +020093}
94
Per Åhgren169c7fd2018-04-27 12:04:03 +020095// Computes a windowed (square root Hanning) padded FFT and updates the related
96// memory.
97void WindowedPaddedFft(const Aec3Fft& fft,
98 rtc::ArrayView<const float> v,
99 rtc::ArrayView<float> v_old,
100 FftData* V) {
101 fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V);
102 std::copy(v.begin(), v.end(), v_old.begin());
103}
104
peah522d71b2017-02-23 05:16:26 -0800105// Class for removing the echo from the capture signal.
peah69221db2017-01-27 03:28:19 -0800106class EchoRemoverImpl final : public EchoRemover {
107 public:
Per Åhgrence202a02019-09-02 17:01:19 +0200108 EchoRemoverImpl(const EchoCanceller3Config& config,
109 int sample_rate_hz,
110 size_t num_render_channels,
111 size_t num_capture_channels);
peah69221db2017-01-27 03:28:19 -0800112 ~EchoRemoverImpl() override;
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200113 EchoRemoverImpl(const EchoRemoverImpl&) = delete;
114 EchoRemoverImpl& operator=(const EchoRemoverImpl&) = delete;
peah69221db2017-01-27 03:28:19 -0800115
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100116 void GetMetrics(EchoControl::Metrics* metrics) const override;
117
peah522d71b2017-02-23 05:16:26 -0800118 // Removes the echo from a block of samples from the capture signal. The
119 // supplied render signal is assumed to be pre-aligned with the capture
120 // signal.
Per Åhgrence202a02019-09-02 17:01:19 +0200121 void ProcessCapture(
122 EchoPathVariability echo_path_variability,
123 bool capture_signal_saturation,
124 const absl::optional<DelayEstimate>& external_delay,
125 RenderBuffer* render_buffer,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100126 std::vector<std::vector<std::vector<float>>>* linear_output,
Per Åhgrence202a02019-09-02 17:01:19 +0200127 std::vector<std::vector<std::vector<float>>>* capture) override;
peah69221db2017-01-27 03:28:19 -0800128
peah522d71b2017-02-23 05:16:26 -0800129 // Updates the status on whether echo leakage is detected in the output of the
130 // echo remover.
131 void UpdateEchoLeakageStatus(bool leakage_detected) override {
132 echo_leakage_detected_ = leakage_detected;
133 }
peah69221db2017-01-27 03:28:19 -0800134
135 private:
Per Åhgren78026752018-08-01 16:24:08 +0200136 // Selects which of the shadow and main linear filter outputs that is most
Per Åhgren22754392018-08-10 18:37:38 +0200137 // appropriate to pass to the suppressor and forms the linear filter output by
138 // smoothly transition between those.
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100139 void FormLinearFilterOutput(const SubtractorOutput& subtractor_output,
Per Åhgren22754392018-08-10 18:37:38 +0200140 rtc::ArrayView<float> output);
Per Åhgren78026752018-08-01 16:24:08 +0200141
peah522d71b2017-02-23 05:16:26 -0800142 static int instance_count_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200143 const EchoCanceller3Config config_;
peah522d71b2017-02-23 05:16:26 -0800144 const Aec3Fft fft_;
145 std::unique_ptr<ApmDataDumper> data_dumper_;
146 const Aec3Optimization optimization_;
peah69221db2017-01-27 03:28:19 -0800147 const int sample_rate_hz_;
Per Åhgrence202a02019-09-02 17:01:19 +0200148 const size_t num_render_channels_;
149 const size_t num_capture_channels_;
Per Åhgren78026752018-08-01 16:24:08 +0200150 const bool use_shadow_filter_output_;
Per Åhgren7bdf0732019-09-25 14:53:30 +0200151 Subtractor subtractor_;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100152 SuppressionGain suppression_gain_;
Gustaf Ullbergcaaa9e72019-10-31 14:10:24 +0100153 ComfortNoiseGenerator cng_;
peah522d71b2017-02-23 05:16:26 -0800154 SuppressionFilter suppression_filter_;
peah522d71b2017-02-23 05:16:26 -0800155 RenderSignalAnalyzer render_signal_analyzer_;
Per Åhgrenb4161d32019-10-08 12:35:47 +0200156 ResidualEchoEstimator residual_echo_estimator_;
peah522d71b2017-02-23 05:16:26 -0800157 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800158 AecState aec_state_;
peahe985b3f2017-02-28 22:08:53 -0800159 EchoRemoverMetrics metrics_;
Gustaf Ullberga99b89b2019-09-23 16:03:12 +0200160 std::vector<std::array<float, kFftLengthBy2>> e_old_;
161 std::vector<std::array<float, kFftLengthBy2>> y_old_;
Per Åhgren88cf0502018-07-16 17:08:41 +0200162 size_t block_counter_ = 0;
163 int gain_change_hangover_ = 0;
Per Åhgren22754392018-08-10 18:37:38 +0200164 bool main_filter_output_last_selected_ = true;
peah69221db2017-01-27 03:28:19 -0800165
Per Åhgren0e3b1ff2019-09-25 12:09:37 +0200166 std::vector<std::array<float, kFftLengthBy2>> e_heap_;
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200167 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2_heap_;
168 std::vector<std::array<float, kFftLengthBy2Plus1>> E2_heap_;
169 std::vector<std::array<float, kFftLengthBy2Plus1>> R2_heap_;
170 std::vector<std::array<float, kFftLengthBy2Plus1>> S2_linear_heap_;
171 std::vector<FftData> Y_heap_;
172 std::vector<FftData> E_heap_;
173 std::vector<FftData> comfort_noise_heap_;
174 std::vector<FftData> high_band_comfort_noise_heap_;
175 std::vector<SubtractorOutput> subtractor_output_heap_;
peah69221db2017-01-27 03:28:19 -0800176};
177
peah522d71b2017-02-23 05:16:26 -0800178int EchoRemoverImpl::instance_count_ = 0;
179
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200180EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config,
Per Åhgrence202a02019-09-02 17:01:19 +0200181 int sample_rate_hz,
182 size_t num_render_channels,
183 size_t num_capture_channels)
peah8cee56f2017-08-24 22:36:53 -0700184 : config_(config),
185 fft_(),
aleloi88b82b52017-02-23 06:27:03 -0800186 data_dumper_(
peah522d71b2017-02-23 05:16:26 -0800187 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
188 optimization_(DetectOptimization()),
189 sample_rate_hz_(sample_rate_hz),
Per Åhgrence202a02019-09-02 17:01:19 +0200190 num_render_channels_(num_render_channels),
191 num_capture_channels_(num_capture_channels),
Per Åhgren24021542018-08-31 07:34:29 +0200192 use_shadow_filter_output_(
Per Åhgren24021542018-08-31 07:34:29 +0200193 config_.filter.enable_shadow_filter_output_usage),
Per Åhgren7bdf0732019-09-25 14:53:30 +0200194 subtractor_(config,
195 num_render_channels_,
196 num_capture_channels_,
197 data_dumper_.get(),
198 optimization_),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100199 suppression_gain_(config_,
200 optimization_,
201 sample_rate_hz,
202 num_capture_channels),
Gustaf Ullbergcaaa9e72019-10-31 14:10:24 +0100203 cng_(optimization_, num_capture_channels_),
Gustaf Ullbergaf3fdc02019-09-24 15:05:04 +0200204 suppression_filter_(optimization_,
205 sample_rate_hz_,
206 num_capture_channels_),
Per Åhgren971de072018-03-14 23:23:47 +0100207 render_signal_analyzer_(config_),
Per Åhgrenb4161d32019-10-08 12:35:47 +0200208 residual_echo_estimator_(config_, num_render_channels),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200209 aec_state_(config_, num_capture_channels_),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100210 e_old_(num_capture_channels_, {0.f}),
211 y_old_(num_capture_channels_, {0.f}),
212 e_heap_(NumChannelsOnHeap(num_capture_channels_), {0.f}),
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200213 Y2_heap_(NumChannelsOnHeap(num_capture_channels_)),
214 E2_heap_(NumChannelsOnHeap(num_capture_channels_)),
215 R2_heap_(NumChannelsOnHeap(num_capture_channels_)),
216 S2_linear_heap_(NumChannelsOnHeap(num_capture_channels_)),
217 Y_heap_(NumChannelsOnHeap(num_capture_channels_)),
218 E_heap_(NumChannelsOnHeap(num_capture_channels_)),
219 comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)),
220 high_band_comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)),
221 subtractor_output_heap_(NumChannelsOnHeap(num_capture_channels_)) {
peah522d71b2017-02-23 05:16:26 -0800222 RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
peah69221db2017-01-27 03:28:19 -0800223}
224
225EchoRemoverImpl::~EchoRemoverImpl() = default;
226
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100227void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const {
228 // Echo return loss (ERL) is inverted to go from gain to attenuation.
Mirko Bonadeidbce0902019-03-15 07:39:02 +0100229 metrics->echo_return_loss = -10.0 * std::log10(aec_state_.ErlTimeDomain());
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100230 metrics->echo_return_loss_enhancement =
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200231 Log2TodB(aec_state_.FullBandErleLog2());
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100232}
233
peahcf02cf12017-04-05 14:18:07 -0700234void EchoRemoverImpl::ProcessCapture(
Per Åhgren88cf0502018-07-16 17:08:41 +0200235 EchoPathVariability echo_path_variability,
peah69221db2017-01-27 03:28:19 -0800236 bool capture_signal_saturation,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200237 const absl::optional<DelayEstimate>& external_delay,
Per Åhgrenc59a5762017-12-11 21:34:19 +0100238 RenderBuffer* render_buffer,
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100239 std::vector<std::vector<std::vector<float>>>* linear_output,
Per Åhgrence202a02019-09-02 17:01:19 +0200240 std::vector<std::vector<std::vector<float>>>* capture) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200241 ++block_counter_;
Per Åhgrence202a02019-09-02 17:01:19 +0200242 const std::vector<std::vector<std::vector<float>>>& x =
243 render_buffer->Block(0);
244 std::vector<std::vector<std::vector<float>>>* y = capture;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100245 RTC_DCHECK(render_buffer);
peah522d71b2017-02-23 05:16:26 -0800246 RTC_DCHECK(y);
247 RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
248 RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
Per Åhgrence202a02019-09-02 17:01:19 +0200249 RTC_DCHECK_EQ(x[0].size(), num_render_channels_);
250 RTC_DCHECK_EQ((*y)[0].size(), num_capture_channels_);
251 RTC_DCHECK_EQ(x[0][0].size(), kBlockSize);
252 RTC_DCHECK_EQ((*y)[0][0].size(), kBlockSize);
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200253
254 // Stack allocated data to use when the number of channels is low.
Per Åhgren0e3b1ff2019-09-25 12:09:37 +0200255 std::array<std::array<float, kFftLengthBy2>, kMaxNumChannelsOnStack> e_stack;
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200256 std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
257 Y2_stack;
258 std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
259 E2_stack;
260 std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
261 R2_stack;
262 std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
263 S2_linear_stack;
264 std::array<FftData, kMaxNumChannelsOnStack> Y_stack;
265 std::array<FftData, kMaxNumChannelsOnStack> E_stack;
266 std::array<FftData, kMaxNumChannelsOnStack> comfort_noise_stack;
267 std::array<FftData, kMaxNumChannelsOnStack> high_band_comfort_noise_stack;
268 std::array<SubtractorOutput, kMaxNumChannelsOnStack> subtractor_output_stack;
269
Per Åhgren0e3b1ff2019-09-25 12:09:37 +0200270 rtc::ArrayView<std::array<float, kFftLengthBy2>> e(e_stack.data(),
271 num_capture_channels_);
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200272 rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> Y2(
273 Y2_stack.data(), num_capture_channels_);
274 rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> E2(
275 E2_stack.data(), num_capture_channels_);
276 rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> R2(
277 R2_stack.data(), num_capture_channels_);
278 rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> S2_linear(
279 S2_linear_stack.data(), num_capture_channels_);
280 rtc::ArrayView<FftData> Y(Y_stack.data(), num_capture_channels_);
281 rtc::ArrayView<FftData> E(E_stack.data(), num_capture_channels_);
282 rtc::ArrayView<FftData> comfort_noise(comfort_noise_stack.data(),
283 num_capture_channels_);
284 rtc::ArrayView<FftData> high_band_comfort_noise(
285 high_band_comfort_noise_stack.data(), num_capture_channels_);
286 rtc::ArrayView<SubtractorOutput> subtractor_output(
287 subtractor_output_stack.data(), num_capture_channels_);
288 if (NumChannelsOnHeap(num_capture_channels_) > 0) {
289 // If the stack-allocated space is too small, use the heap for storing the
290 // microphone data.
Per Åhgren0e3b1ff2019-09-25 12:09:37 +0200291 e = rtc::ArrayView<std::array<float, kFftLengthBy2>>(e_heap_.data(),
292 num_capture_channels_);
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200293 Y2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
294 Y2_heap_.data(), num_capture_channels_);
295 E2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
296 E2_heap_.data(), num_capture_channels_);
297 R2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
298 R2_heap_.data(), num_capture_channels_);
299 S2_linear = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
300 S2_linear_heap_.data(), num_capture_channels_);
301 Y = rtc::ArrayView<FftData>(Y_heap_.data(), num_capture_channels_);
302 E = rtc::ArrayView<FftData>(E_heap_.data(), num_capture_channels_);
303 comfort_noise = rtc::ArrayView<FftData>(comfort_noise_heap_.data(),
304 num_capture_channels_);
305 high_band_comfort_noise = rtc::ArrayView<FftData>(
306 high_band_comfort_noise_heap_.data(), num_capture_channels_);
307 subtractor_output = rtc::ArrayView<SubtractorOutput>(
308 subtractor_output_heap_.data(), num_capture_channels_);
309 }
310
Per Åhgren119e2192019-10-18 08:50:50 +0200311 data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize,
312 &(*y)[0][0][0], 16000, 1);
313 data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize,
314 &x[0][0][0], 16000, 1);
315 data_dumper_->DumpRaw("aec3_echo_remover_capture_input", (*y)[0][0]);
316 data_dumper_->DumpRaw("aec3_echo_remover_render_input", x[0][0]);
peah522d71b2017-02-23 05:16:26 -0800317
318 aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
319
320 if (echo_path_variability.AudioPathChanged()) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200321 // Ensure that the gain change is only acted on once per frame.
322 if (echo_path_variability.gain_change) {
323 if (gain_change_hangover_ == 0) {
324 constexpr int kMaxBlocksPerFrame = 3;
325 gain_change_hangover_ = kMaxBlocksPerFrame;
Sam Zackrissonffc84522019-10-15 13:43:02 +0200326 rtc::LoggingSeverity log_level =
327 config_.delay.log_warning_on_delay_changes ? rtc::LS_WARNING
328 : rtc::LS_INFO;
329 RTC_LOG_V(log_level)
330 << "Gain change detected at block " << block_counter_;
Per Åhgren88cf0502018-07-16 17:08:41 +0200331 } else {
332 echo_path_variability.gain_change = false;
333 }
334 }
335
Per Åhgren7bdf0732019-09-25 14:53:30 +0200336 subtractor_.HandleEchoPathChange(echo_path_variability);
peah86afe9d2017-04-06 15:45:32 -0700337 aec_state_.HandleEchoPathChange(echo_path_variability);
Per Åhgren88cf0502018-07-16 17:08:41 +0200338
339 if (echo_path_variability.delay_change !=
340 EchoPathVariability::DelayAdjustment::kNone) {
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100341 suppression_gain_.SetInitialState(true);
Per Åhgren88cf0502018-07-16 17:08:41 +0200342 }
343 }
344 if (gain_change_hangover_ > 0) {
345 --gain_change_hangover_;
peah522d71b2017-02-23 05:16:26 -0800346 }
347
peah522d71b2017-02-23 05:16:26 -0800348 // Analyze the render signal.
Per Åhgren5c532d32018-03-22 00:29:25 +0100349 render_signal_analyzer_.Update(*render_buffer,
Per Åhgren8718afb2019-10-15 10:31:35 +0200350 aec_state_.MinDirectPathFilterDelay());
peah522d71b2017-02-23 05:16:26 -0800351
Per Åhgren7bdf0732019-09-25 14:53:30 +0200352 // State transition.
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200353 if (aec_state_.TransitionTriggered()) {
Per Åhgren7bdf0732019-09-25 14:53:30 +0200354 subtractor_.ExitInitialState();
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100355 suppression_gain_.SetInitialState(false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100356 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100357
Per Åhgren7bdf0732019-09-25 14:53:30 +0200358 // Perform linear echo cancellation.
359 subtractor_.Process(*render_buffer, (*y)[0], render_signal_analyzer_,
360 aec_state_, subtractor_output);
361
Per Åhgren119e2192019-10-18 08:50:50 +0200362 // Compute spectra.
Gustaf Ullberga99b89b2019-09-23 16:03:12 +0200363 for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
Per Åhgren0e3b1ff2019-09-25 12:09:37 +0200364 FormLinearFilterOutput(subtractor_output[ch], e[ch]);
Per Åhgren119e2192019-10-18 08:50:50 +0200365 WindowedPaddedFft(fft_, (*y)[0][ch], y_old_[ch], &Y[ch]);
Per Åhgren0e3b1ff2019-09-25 12:09:37 +0200366 WindowedPaddedFft(fft_, e[ch], e_old_[ch], &E[ch]);
Gustaf Ullberga99b89b2019-09-23 16:03:12 +0200367 LinearEchoPower(E[ch], Y[ch], &S2_linear[ch]);
368 Y[ch].Spectrum(optimization_, Y2[ch]);
369 E[ch].Spectrum(optimization_, E2[ch]);
370 }
peah522d71b2017-02-23 05:16:26 -0800371
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100372 // Optionally return the linear filter output.
373 if (linear_output) {
374 RTC_DCHECK_GE(1, linear_output->size());
375 RTC_DCHECK_EQ(num_capture_channels_, linear_output[0].size());
376 for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
377 RTC_DCHECK_EQ(kBlockSize, (*linear_output)[0][ch].size());
378 std::copy(e[ch].begin(), e[ch].end(), (*linear_output)[0][ch].begin());
379 }
380 }
381
peah522d71b2017-02-23 05:16:26 -0800382 // Update the AEC state information.
Per Åhgren119e2192019-10-18 08:50:50 +0200383 aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponses(),
384 subtractor_.FilterImpulseResponses(), *render_buffer, E2,
385 Y2, subtractor_output);
Per Åhgren169c7fd2018-04-27 12:04:03 +0200386
peah522d71b2017-02-23 05:16:26 -0800387 // Choose the linear output.
Gustaf Ullbergaf3fdc02019-09-24 15:05:04 +0200388 const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y;
Gustaf Ullberga99b89b2019-09-23 16:03:12 +0200389
Per Åhgren119e2192019-10-18 08:50:50 +0200390 data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &(*y)[0][0][0], 16000,
391 1);
Per Åhgren0e3b1ff2019-09-25 12:09:37 +0200392 data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0][0], 16000, 1);
peah522d71b2017-02-23 05:16:26 -0800393
Per Åhgrenb4161d32019-10-08 12:35:47 +0200394 // Estimate the residual echo power.
395 residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2,
396 R2);
peah522d71b2017-02-23 05:16:26 -0800397
Gustaf Ullbergcaaa9e72019-10-31 14:10:24 +0100398 // Estimate the comfort noise.
399 cng_.Compute(aec_state_.SaturatedCapture(), Y2, comfort_noise,
400 high_band_comfort_noise);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200401
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100402 // Suppressor nearend estimate.
403 if (aec_state_.UsableLinearEstimate()) {
404 // E2 is bound by Y2.
405 for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
Gustaf Ullberga99b89b2019-09-23 16:03:12 +0200406 std::transform(E2[ch].begin(), E2[ch].end(), Y2[ch].begin(),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100407 E2[ch].begin(),
Gustaf Ullberga99b89b2019-09-23 16:03:12 +0200408 [](float a, float b) { return std::min(a, b); });
409 }
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200410 }
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100411 const auto& nearend_spectrum = aec_state_.UsableLinearEstimate() ? E2 : Y2;
412
413 // Suppressor echo estimate.
414 const auto& echo_spectrum =
415 aec_state_.UsableLinearEstimate() ? S2_linear : R2;
416
417 // Compute preferred gains.
418 float high_bands_gain;
419 std::array<float, kFftLengthBy2Plus1> G;
420 suppression_gain_.GetGain(nearend_spectrum, echo_spectrum, R2,
421 cng_.NoiseSpectrum(), render_signal_analyzer_,
422 aec_state_, x, &high_bands_gain, &G);
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +0200423
Gustaf Ullbergaf3fdc02019-09-24 15:05:04 +0200424 suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200425 high_bands_gain, Y_fft, y);
peah522d71b2017-02-23 05:16:26 -0800426
peahe985b3f2017-02-28 22:08:53 -0800427 // Update the metrics.
Gustaf Ullbergcaaa9e72019-10-31 14:10:24 +0100428 metrics_.Update(aec_state_, cng_.NoiseSpectrum()[0], G);
peahe985b3f2017-02-28 22:08:53 -0800429
peah522d71b2017-02-23 05:16:26 -0800430 // Debug outputs for the purpose of development and analysis.
peah29103572017-07-11 02:54:02 -0700431 data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200432 &subtractor_output[0].s_main[0], 16000, 1);
Per Åhgren119e2192019-10-18 08:50:50 +0200433 data_dumper_->DumpRaw("aec3_output", (*y)[0][0]);
peah14c11a42017-07-11 06:13:43 -0700434 data_dumper_->DumpRaw("aec3_narrow_render",
435 render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
Gustaf Ullbergcaaa9e72019-10-31 14:10:24 +0100436 data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum()[0]);
peah522d71b2017-02-23 05:16:26 -0800437 data_dumper_->DumpRaw("aec3_suppressor_gain", G);
Per Åhgren119e2192019-10-18 08:50:50 +0200438 data_dumper_->DumpWav("aec3_output",
439 rtc::ArrayView<const float>(&(*y)[0][0][0], kBlockSize),
440 16000, 1);
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200441 data_dumper_->DumpRaw("aec3_using_subtractor_output[0]",
Per Åhgren5c532d32018-03-22 00:29:25 +0100442 aec_state_.UseLinearFilterOutput() ? 1 : 0);
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200443 data_dumper_->DumpRaw("aec3_E2", E2[0]);
444 data_dumper_->DumpRaw("aec3_S2_linear", S2_linear[0]);
445 data_dumper_->DumpRaw("aec3_Y2", Y2[0]);
Jesús de Vicente Peña7682c6e2018-03-22 14:53:23 +0100446 data_dumper_->DumpRaw(
Sam Zackrisson98872dc2019-10-18 08:20:09 +0200447 "aec3_X2", render_buffer->Spectrum(
448 aec_state_.MinDirectPathFilterDelay())[/*channel=*/0]);
Per Åhgrenf6aa5722019-09-10 18:05:17 +0200449 data_dumper_->DumpRaw("aec3_R2", R2[0]);
Per Åhgren8718afb2019-10-15 10:31:35 +0200450 data_dumper_->DumpRaw("aec3_filter_delay",
451 aec_state_.MinDirectPathFilterDelay());
peah522d71b2017-02-23 05:16:26 -0800452 data_dumper_->DumpRaw("aec3_capture_saturation",
453 aec_state_.SaturatedCapture() ? 1 : 0);
454}
peah69221db2017-01-27 03:28:19 -0800455
Per Åhgren22754392018-08-10 18:37:38 +0200456void EchoRemoverImpl::FormLinearFilterOutput(
Per Åhgren22754392018-08-10 18:37:38 +0200457 const SubtractorOutput& subtractor_output,
458 rtc::ArrayView<float> output) {
459 RTC_DCHECK_EQ(subtractor_output.e_main.size(), output.size());
460 RTC_DCHECK_EQ(subtractor_output.e_shadow.size(), output.size());
461 bool use_main_output = true;
462 if (use_shadow_filter_output_) {
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200463 // As the output of the main adaptive filter generally should be better
464 // than the shadow filter output, add a margin and threshold for when
465 // choosing the shadow filter output.
Per Åhgren22754392018-08-10 18:37:38 +0200466 if (subtractor_output.e2_shadow < 0.9f * subtractor_output.e2_main &&
467 subtractor_output.y2 > 30.f * 30.f * kBlockSize &&
468 (subtractor_output.s2_main > 60.f * 60.f * kBlockSize ||
469 subtractor_output.s2_shadow > 60.f * 60.f * kBlockSize)) {
470 use_main_output = false;
471 } else {
472 // If the main filter is diverged, choose the filter output that has the
473 // lowest power.
474 if (subtractor_output.e2_shadow < subtractor_output.e2_main &&
475 subtractor_output.y2 < subtractor_output.e2_main) {
476 use_main_output = false;
477 }
478 }
479 }
480
Gustaf Ullberg7911d372019-09-24 16:31:01 +0200481 SignalTransition(
482 main_filter_output_last_selected_ ? subtractor_output.e_main
483 : subtractor_output.e_shadow,
484 use_main_output ? subtractor_output.e_main : subtractor_output.e_shadow,
485 output);
Per Åhgren22754392018-08-10 18:37:38 +0200486 main_filter_output_last_selected_ = use_main_output;
487}
488
peah69221db2017-01-27 03:28:19 -0800489} // namespace
490
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200491EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config,
Per Åhgrence202a02019-09-02 17:01:19 +0200492 int sample_rate_hz,
493 size_t num_render_channels,
494 size_t num_capture_channels) {
495 return new EchoRemoverImpl(config, sample_rate_hz, num_render_channels,
496 num_capture_channels);
peah69221db2017-01-27 03:28:19 -0800497}
498
499} // namespace webrtc