blob: b19bd647927254913397ece62d3fb921a2599873 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/subtractor.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
Per Åhgren1b4059e2017-10-15 20:19:21 +020014#include <numeric>
peah522d71b2017-02-23 05:16:26 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/array_view.h"
17#include "modules/audio_processing/logging/apm_data_dumper.h"
18#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010019#include "rtc_base/numerics/safe_minmax.h"
peah522d71b2017-02-23 05:16:26 -080020
21namespace webrtc {
22
23namespace {
24
peah86afe9d2017-04-06 15:45:32 -070025void PredictionError(const Aec3Fft& fft,
26 const FftData& S,
27 rtc::ArrayView<const float> y,
28 std::array<float, kBlockSize>* e,
peah29103572017-07-11 02:54:02 -070029 std::array<float, kBlockSize>* s) {
Per Åhgren7634c162017-12-18 15:45:49 +010030 std::array<float, kFftLength> tmp;
31 fft.Ifft(S, &tmp);
peah522d71b2017-02-23 05:16:26 -080032 constexpr float kScale = 1.0f / kFftLengthBy2;
Per Åhgren7634c162017-12-18 15:45:49 +010033 std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(),
34 [&](float a, float b) { return a - b * kScale; });
peah29103572017-07-11 02:54:02 -070035
36 if (s) {
37 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010038 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070039 }
40 }
Per Åhgren7634c162017-12-18 15:45:49 +010041
42 std::for_each(e->begin(), e->end(),
43 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
peah522d71b2017-02-23 05:16:26 -080044}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010045
peah522d71b2017-02-23 05:16:26 -080046} // namespace
47
Per Åhgren09a718a2017-12-11 22:28:45 +010048Subtractor::Subtractor(const EchoCanceller3Config& config,
49 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080050 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080051 : fft_(),
52 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080053 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +010054 config_(config),
55 main_filter_(config_.filter.main.length_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010056 optimization,
57 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010058 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010059 optimization,
60 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010061 G_main_(config_.filter.main_initial),
62 G_shadow_(config_.filter.shadow_initial) {
peah522d71b2017-02-23 05:16:26 -080063 RTC_DCHECK(data_dumper_);
Per Åhgren08ea5892018-01-15 08:07:41 +010064 // Currently, the rest of AEC3 requires the main and shadow filter lengths to
65 // be identical.
Per Åhgrena98c8072018-01-15 19:17:16 +010066 RTC_DCHECK_EQ(config_.filter.main.length_blocks,
67 config_.filter.shadow.length_blocks);
68 RTC_DCHECK_EQ(config_.filter.main_initial.length_blocks,
69 config_.filter.shadow_initial.length_blocks);
70
71 RTC_DCHECK_GE(config_.filter.main.length_blocks,
72 config_.filter.main_initial.length_blocks);
73 RTC_DCHECK_GE(config_.filter.shadow.length_blocks,
74 config_.filter.shadow_initial.length_blocks);
75
76 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks);
77 shadow_filter_.SetSizePartitions(config_.filter.shadow_initial.length_blocks);
peah522d71b2017-02-23 05:16:26 -080078}
79
peah29103572017-07-11 02:54:02 -070080Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080081
82void Subtractor::HandleEchoPathChange(
83 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010084 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -080085 main_filter_.HandleEchoPathChange();
86 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +010087 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -070088 G_shadow_.HandleEchoPathChange();
Per Åhgrena98c8072018-01-15 19:17:16 +010089 G_main_.SetConfig(config_.filter.main_initial);
90 G_shadow_.SetConfig(config_.filter.shadow_initial);
91 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks);
92 shadow_filter_.SetSizePartitions(
93 config_.filter.shadow_initial.length_blocks);
94
Per Åhgren1b4059e2017-10-15 20:19:21 +020095 converged_filter_ = false;
Per Åhgren8ba58612017-12-01 23:01:44 +010096 };
97
98 // TODO(peah): Add delay-change specific reset behavior.
99 if ((echo_path_variability.delay_change ==
100 EchoPathVariability::DelayAdjustment::kBufferFlush) ||
101 (echo_path_variability.delay_change ==
102 EchoPathVariability::DelayAdjustment::kDelayReset)) {
103 full_reset();
104 } else if (echo_path_variability.delay_change ==
105 EchoPathVariability::DelayAdjustment::kNewDetectedDelay) {
106 full_reset();
107 } else if (echo_path_variability.delay_change ==
108 EchoPathVariability::DelayAdjustment::kBufferReadjustment) {
109 full_reset();
peah522d71b2017-02-23 05:16:26 -0800110 }
111}
112
Per Åhgrena98c8072018-01-15 19:17:16 +0100113void Subtractor::ExitInitialState() {
114 G_main_.SetConfig(config_.filter.main);
115 G_shadow_.SetConfig(config_.filter.shadow);
116 main_filter_.SetSizePartitions(config_.filter.main.length_blocks);
117 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks);
118}
119
peahcf02cf12017-04-05 14:18:07 -0700120void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800121 const rtc::ArrayView<const float> capture,
122 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700123 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800124 SubtractorOutput* output) {
125 RTC_DCHECK_EQ(kBlockSize, capture.size());
126 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800127 FftData& E_main = output->E_main;
Per Åhgrend20639f2018-01-11 10:29:49 +0100128 FftData& E_main_nonwindowed = output->E_main_nonwindowed;
peah86afe9d2017-04-06 15:45:32 -0700129 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800130 std::array<float, kBlockSize>& e_main = output->e_main;
131 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
132
133 FftData S;
134 FftData& G = S;
135
peah86afe9d2017-04-06 15:45:32 -0700136 // Form the output of the main filter.
137 main_filter_.Filter(render_buffer, &S);
Per Åhgrend20639f2018-01-11 10:29:49 +0100138 PredictionError(fft_, S, y, &e_main, &output->s_main);
139 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
140 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kRectangular,
141 &E_main_nonwindowed);
peah522d71b2017-02-23 05:16:26 -0800142
peah86afe9d2017-04-06 15:45:32 -0700143 // Form the output of the shadow filter.
144 shadow_filter_.Filter(render_buffer, &S);
Per Åhgrend20639f2018-01-11 10:29:49 +0100145 PredictionError(fft_, S, y, &e_shadow, nullptr);
146 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
peah522d71b2017-02-23 05:16:26 -0800147
Per Åhgren7ddd4632017-10-25 02:59:45 +0200148 if (!converged_filter_) {
Per Åhgren63b494d2017-12-06 11:32:38 +0100149 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
150 const float e2_main =
151 std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
152 const float e2_shadow =
153 std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
154 const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
155
156 if (y2 > kBlockSize * 50.f * 50.f) {
157 converged_filter_ = (e2_main > 0.3 * y2 || e2_shadow > 0.1 * y2);
Per Åhgren1b4059e2017-10-15 20:19:21 +0200158 }
159 }
160
peah522d71b2017-02-23 05:16:26 -0800161 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100162 E_main.Spectrum(optimization_, output->E2_main);
Per Åhgrend20639f2018-01-11 10:29:49 +0100163 E_main_nonwindowed.Spectrum(optimization_, output->E2_main_nonwindowed);
Per Åhgren8ba58612017-12-01 23:01:44 +0100164 E_shadow.Spectrum(optimization_, output->E2_shadow);
peah522d71b2017-02-23 05:16:26 -0800165
166 // Update the main filter.
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100167 std::array<float, kFftLengthBy2Plus1> X2;
168 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2);
169 G_main_.Compute(X2, render_signal_analyzer, *output, main_filter_,
peah86afe9d2017-04-06 15:45:32 -0700170 aec_state.SaturatedCapture(), &G);
171 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800172 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
173 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
174
175 // Update the shadow filter.
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100176 if (shadow_filter_.SizePartitions() != main_filter_.SizePartitions()) {
177 render_buffer.SpectralSum(shadow_filter_.SizePartitions(), &X2);
178 }
179 G_shadow_.Compute(X2, render_signal_analyzer, E_shadow,
peah86afe9d2017-04-06 15:45:32 -0700180 shadow_filter_.SizePartitions(),
181 aec_state.SaturatedCapture(), &G);
182 shadow_filter_.Adapt(render_buffer, G);
183
peah522d71b2017-02-23 05:16:26 -0800184 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
185 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
186
187 main_filter_.DumpFilter("aec3_subtractor_H_main");
188 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
189}
190
191} // namespace webrtc