blob: 4d86358781be4a2fb99d5b1a7140d5295f7c998d [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>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <utility>
peah522d71b2017-02-23 05:16:26 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/array_view.h"
Per Åhgrend4e69042019-09-05 15:55:58 +020017#include "modules/audio_processing/aec3/adaptive_fir_filter_erl.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "modules/audio_processing/aec3/fft_data.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/logging/apm_data_dumper.h"
20#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010021#include "rtc_base/numerics/safe_minmax.h"
peah522d71b2017-02-23 05:16:26 -080022
23namespace webrtc {
24
25namespace {
26
peah86afe9d2017-04-06 15:45:32 -070027void PredictionError(const Aec3Fft& fft,
28 const FftData& S,
29 rtc::ArrayView<const float> y,
30 std::array<float, kBlockSize>* e,
Per Åhgren45231be2019-05-16 14:43:57 +020031 std::array<float, kBlockSize>* s) {
Per Åhgren7634c162017-12-18 15:45:49 +010032 std::array<float, kFftLength> tmp;
33 fft.Ifft(S, &tmp);
peah522d71b2017-02-23 05:16:26 -080034 constexpr float kScale = 1.0f / kFftLengthBy2;
Per Åhgren7634c162017-12-18 15:45:49 +010035 std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(),
36 [&](float a, float b) { return a - b * kScale; });
peah29103572017-07-11 02:54:02 -070037
38 if (s) {
39 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010040 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070041 }
Per Åhgren9845a672018-01-15 13:09:02 +010042 }
peah522d71b2017-02-23 05:16:26 -080043}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010044
Per Åhgren7f5175a2018-07-25 16:30:54 +020045void ScaleFilterOutput(rtc::ArrayView<const float> y,
46 float factor,
47 rtc::ArrayView<float> e,
48 rtc::ArrayView<float> s) {
49 RTC_DCHECK_EQ(y.size(), e.size());
50 RTC_DCHECK_EQ(y.size(), s.size());
51 for (size_t k = 0; k < y.size(); ++k) {
52 s[k] *= factor;
53 e[k] = y[k] - s[k];
54 }
55}
56
peah522d71b2017-02-23 05:16:26 -080057} // namespace
58
Per Åhgren09a718a2017-12-11 22:28:45 +010059Subtractor::Subtractor(const EchoCanceller3Config& config,
Per Åhgrena33dc012019-09-03 23:59:52 +020060 size_t num_render_channels,
61 size_t num_capture_channels,
Per Åhgren09a718a2017-12-11 22:28:45 +010062 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080063 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080064 : fft_(),
65 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080066 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +010067 config_(config),
68 main_filter_(config_.filter.main.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010069 config_.filter.main_initial.length_blocks,
70 config.filter.config_change_duration_blocks,
Per Åhgrena33dc012019-09-03 23:59:52 +020071 num_render_channels,
72 num_capture_channels,
Per Åhgren08ea5892018-01-15 08:07:41 +010073 optimization,
74 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010075 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010076 config_.filter.shadow_initial.length_blocks,
77 config.filter.config_change_duration_blocks,
Per Åhgrena33dc012019-09-03 23:59:52 +020078 num_render_channels,
79 num_capture_channels,
Per Åhgren08ea5892018-01-15 08:07:41 +010080 optimization,
81 data_dumper_),
Per Åhgren5f1a31c2018-03-08 15:54:41 +010082 G_main_(config_.filter.main_initial,
83 config_.filter.config_change_duration_blocks),
84 G_shadow_(config_.filter.shadow_initial,
Per Åhgrend4e69042019-09-05 15:55:58 +020085 config.filter.config_change_duration_blocks),
86 main_frequency_response_(main_filter_.max_filter_size_partitions(),
87 std::array<float, kFftLengthBy2Plus1>()),
88 main_impulse_response_(
89 GetTimeDomainLength(main_filter_.max_filter_size_partitions()),
90 0.f) {
peah522d71b2017-02-23 05:16:26 -080091 RTC_DCHECK(data_dumper_);
Per Åhgrend4e69042019-09-05 15:55:58 +020092 for (auto& H2_k : main_frequency_response_) {
93 H2_k.fill(0.f);
94 }
peah522d71b2017-02-23 05:16:26 -080095}
96
peah29103572017-07-11 02:54:02 -070097Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080098
99void Subtractor::HandleEchoPathChange(
100 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100101 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -0800102 main_filter_.HandleEchoPathChange();
103 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +0100104 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -0700105 G_shadow_.HandleEchoPathChange();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100106 G_main_.SetConfig(config_.filter.main_initial, true);
107 G_shadow_.SetConfig(config_.filter.shadow_initial, true);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100108 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks,
109 true);
Per Åhgrena98c8072018-01-15 19:17:16 +0100110 shadow_filter_.SetSizePartitions(
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100111 config_.filter.shadow_initial.length_blocks, true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100112 };
113
Per Åhgren88cf0502018-07-16 17:08:41 +0200114 if (echo_path_variability.delay_change !=
115 EchoPathVariability::DelayAdjustment::kNone) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100116 full_reset();
Per Åhgren88cf0502018-07-16 17:08:41 +0200117 }
118
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100119 if (echo_path_variability.gain_change) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200120 G_main_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800121 }
122}
123
Per Åhgrena98c8072018-01-15 19:17:16 +0100124void Subtractor::ExitInitialState() {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100125 G_main_.SetConfig(config_.filter.main, false);
126 G_shadow_.SetConfig(config_.filter.shadow, false);
127 main_filter_.SetSizePartitions(config_.filter.main.length_blocks, false);
128 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks, false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100129}
130
peahcf02cf12017-04-05 14:18:07 -0700131void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800132 const rtc::ArrayView<const float> capture,
133 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700134 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800135 SubtractorOutput* output) {
136 RTC_DCHECK_EQ(kBlockSize, capture.size());
137 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800138 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -0700139 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800140 std::array<float, kBlockSize>& e_main = output->e_main;
141 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
142
143 FftData S;
144 FftData& G = S;
145
Per Åhgren7f5175a2018-07-25 16:30:54 +0200146 // Form the outputs of the main and shadow filters.
peah86afe9d2017-04-06 15:45:32 -0700147 main_filter_.Filter(render_buffer, &S);
Per Åhgren45231be2019-05-16 14:43:57 +0200148 PredictionError(fft_, S, y, &e_main, &output->s_main);
peah522d71b2017-02-23 05:16:26 -0800149
peah86afe9d2017-04-06 15:45:32 -0700150 shadow_filter_.Filter(render_buffer, &S);
Per Åhgren45231be2019-05-16 14:43:57 +0200151 PredictionError(fft_, S, y, &e_shadow, &output->s_shadow);
peah522d71b2017-02-23 05:16:26 -0800152
Per Åhgrene4db6a12018-07-26 15:32:24 +0200153 // Compute the signal powers in the subtractor output.
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200154 output->ComputeMetrics(y);
Per Åhgrene4db6a12018-07-26 15:32:24 +0200155
Per Åhgren7f5175a2018-07-25 16:30:54 +0200156 // Adjust the filter if needed.
157 bool main_filter_adjusted = false;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100158 filter_misadjustment_estimator_.Update(*output);
159 if (filter_misadjustment_estimator_.IsAdjustmentNeeded()) {
160 float scale = filter_misadjustment_estimator_.GetMisadjustment();
161 main_filter_.ScaleFilter(scale);
Per Åhgrend4e69042019-09-05 15:55:58 +0200162 for (auto& h_k : main_impulse_response_) {
163 h_k *= scale;
164 }
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100165 ScaleFilterOutput(y, scale, e_main, output->s_main);
166 filter_misadjustment_estimator_.Reset();
167 main_filter_adjusted = true;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200168 }
Per Åhgren7f5175a2018-07-25 16:30:54 +0200169
170 // Compute the FFts of the main and shadow filter outputs.
171 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
172 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
173
peah522d71b2017-02-23 05:16:26 -0800174 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100175 E_shadow.Spectrum(optimization_, output->E2_shadow);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100176 E_main.Spectrum(optimization_, output->E2_main);
177
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200178 // Compute the render powers.
179 std::array<float, kFftLengthBy2Plus1> X2_main;
180 std::array<float, kFftLengthBy2Plus1> X2_shadow_data;
181 std::array<float, kFftLengthBy2Plus1>& X2_shadow =
182 main_filter_.SizePartitions() == shadow_filter_.SizePartitions()
183 ? X2_main
184 : X2_shadow_data;
185 if (main_filter_.SizePartitions() == shadow_filter_.SizePartitions()) {
186 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2_main);
187 } else if (main_filter_.SizePartitions() > shadow_filter_.SizePartitions()) {
188 render_buffer.SpectralSums(shadow_filter_.SizePartitions(),
189 main_filter_.SizePartitions(), &X2_shadow,
190 &X2_main);
191 } else {
192 render_buffer.SpectralSums(main_filter_.SizePartitions(),
193 shadow_filter_.SizePartitions(), &X2_main,
194 &X2_shadow);
195 }
196
peah522d71b2017-02-23 05:16:26 -0800197 // Update the main filter.
Per Åhgren7f5175a2018-07-25 16:30:54 +0200198 if (!main_filter_adjusted) {
Per Åhgrend4e69042019-09-05 15:55:58 +0200199 std::array<float, kFftLengthBy2Plus1> erl;
200 ComputeErl(optimization_, main_frequency_response_, erl);
201 G_main_.Compute(X2_main, render_signal_analyzer, *output, erl,
202 main_filter_.SizePartitions(), aec_state.SaturatedCapture(),
203 &G);
Per Åhgren7f5175a2018-07-25 16:30:54 +0200204 } else {
205 G.re.fill(0.f);
206 G.im.fill(0.f);
207 }
Per Åhgrend4e69042019-09-05 15:55:58 +0200208 main_filter_.Adapt(render_buffer, G, &main_impulse_response_);
209 main_filter_.ComputeFrequencyResponse(&main_frequency_response_);
210
peah522d71b2017-02-23 05:16:26 -0800211 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
212 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
213
214 // Update the shadow filter.
Per Åhgrene4db6a12018-07-26 15:32:24 +0200215 poor_shadow_filter_counter_ =
216 output->e2_main < output->e2_shadow ? poor_shadow_filter_counter_ + 1 : 0;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100217 if (poor_shadow_filter_counter_ < 5) {
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200218 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_shadow,
Per Åhgrene4db6a12018-07-26 15:32:24 +0200219 shadow_filter_.SizePartitions(),
Per Åhgren45231be2019-05-16 14:43:57 +0200220 aec_state.SaturatedCapture(), &G);
Per Åhgrene4db6a12018-07-26 15:32:24 +0200221 } else {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200222 poor_shadow_filter_counter_ = 0;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100223 shadow_filter_.SetFilter(main_filter_.GetFilter());
224 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_main,
225 shadow_filter_.SizePartitions(),
Per Åhgren45231be2019-05-16 14:43:57 +0200226 aec_state.SaturatedCapture(), &G);
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100227 }
peah86afe9d2017-04-06 15:45:32 -0700228
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100229 shadow_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800230 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
231 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200232 filter_misadjustment_estimator_.Dump(data_dumper_);
Per Åhgren5c532d32018-03-22 00:29:25 +0100233 DumpFilters();
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200234
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100235 std::for_each(e_main.begin(), e_main.end(),
236 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
Per Åhgren71ebf992018-07-16 14:46:11 +0200237
238 data_dumper_->DumpWav("aec3_main_filter_output", kBlockSize, &e_main[0],
239 16000, 1);
240 data_dumper_->DumpWav("aec3_shadow_filter_output", kBlockSize, &e_shadow[0],
241 16000, 1);
peah522d71b2017-02-23 05:16:26 -0800242}
243
Per Åhgrenb20b9372018-07-13 00:22:54 +0200244void Subtractor::FilterMisadjustmentEstimator::Update(
Per Åhgrene4db6a12018-07-26 15:32:24 +0200245 const SubtractorOutput& output) {
246 e2_acum_ += output.e2_main;
247 y2_acum_ += output.y2;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200248 if (++n_blocks_acum_ == n_blocks_) {
249 if (y2_acum_ > n_blocks_ * 200.f * 200.f * kBlockSize) {
250 float update = (e2_acum_ / y2_acum_);
251 if (e2_acum_ > n_blocks_ * 7500.f * 7500.f * kBlockSize) {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200252 // Duration equal to blockSizeMs * n_blocks_ * 4.
253 overhang_ = 4;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200254 } else {
255 overhang_ = std::max(overhang_ - 1, 0);
256 }
257
258 if ((update < inv_misadjustment_) || (overhang_ > 0)) {
259 inv_misadjustment_ += 0.1f * (update - inv_misadjustment_);
260 }
261 }
262 e2_acum_ = 0.f;
263 y2_acum_ = 0.f;
264 n_blocks_acum_ = 0;
265 }
266}
267
268void Subtractor::FilterMisadjustmentEstimator::Reset() {
269 e2_acum_ = 0.f;
270 y2_acum_ = 0.f;
271 n_blocks_acum_ = 0;
272 inv_misadjustment_ = 0.f;
273 overhang_ = 0.f;
274}
275
276void Subtractor::FilterMisadjustmentEstimator::Dump(
277 ApmDataDumper* data_dumper) const {
278 data_dumper->DumpRaw("aec3_inv_misadjustment_factor", inv_misadjustment_);
279}
280
peah522d71b2017-02-23 05:16:26 -0800281} // namespace webrtc