blob: efb79d42dc9468bd4807855cd4ad240ad862ed93 [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"
Yves Gerey988cc082018-10-23 12:03:01 +020017#include "modules/audio_processing/aec3/fft_data.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/logging/apm_data_dumper.h"
19#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010020#include "rtc_base/numerics/safe_minmax.h"
peah522d71b2017-02-23 05:16:26 -080021
22namespace webrtc {
23
24namespace {
25
peah86afe9d2017-04-06 15:45:32 -070026void PredictionError(const Aec3Fft& fft,
27 const FftData& S,
28 rtc::ArrayView<const float> y,
29 std::array<float, kBlockSize>* e,
Per Åhgren45231be2019-05-16 14:43:57 +020030 std::array<float, kBlockSize>* s) {
Per Åhgren7634c162017-12-18 15:45:49 +010031 std::array<float, kFftLength> tmp;
32 fft.Ifft(S, &tmp);
peah522d71b2017-02-23 05:16:26 -080033 constexpr float kScale = 1.0f / kFftLengthBy2;
Per Åhgren7634c162017-12-18 15:45:49 +010034 std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(),
35 [&](float a, float b) { return a - b * kScale; });
peah29103572017-07-11 02:54:02 -070036
37 if (s) {
38 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010039 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070040 }
Per Åhgren9845a672018-01-15 13:09:02 +010041 }
peah522d71b2017-02-23 05:16:26 -080042}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010043
Per Åhgren7f5175a2018-07-25 16:30:54 +020044void ScaleFilterOutput(rtc::ArrayView<const float> y,
45 float factor,
46 rtc::ArrayView<float> e,
47 rtc::ArrayView<float> s) {
48 RTC_DCHECK_EQ(y.size(), e.size());
49 RTC_DCHECK_EQ(y.size(), s.size());
50 for (size_t k = 0; k < y.size(); ++k) {
51 s[k] *= factor;
52 e[k] = y[k] - s[k];
53 }
54}
55
peah522d71b2017-02-23 05:16:26 -080056} // namespace
57
Per Åhgren09a718a2017-12-11 22:28:45 +010058Subtractor::Subtractor(const EchoCanceller3Config& config,
Per Åhgrena33dc012019-09-03 23:59:52 +020059 size_t num_render_channels,
60 size_t num_capture_channels,
Per Åhgren09a718a2017-12-11 22:28:45 +010061 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080062 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080063 : fft_(),
64 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080065 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +010066 config_(config),
67 main_filter_(config_.filter.main.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010068 config_.filter.main_initial.length_blocks,
69 config.filter.config_change_duration_blocks,
Per Åhgrena33dc012019-09-03 23:59:52 +020070 num_render_channels,
71 num_capture_channels,
Per Åhgren08ea5892018-01-15 08:07:41 +010072 optimization,
73 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010074 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010075 config_.filter.shadow_initial.length_blocks,
76 config.filter.config_change_duration_blocks,
Per Åhgrena33dc012019-09-03 23:59:52 +020077 num_render_channels,
78 num_capture_channels,
Per Åhgren08ea5892018-01-15 08:07:41 +010079 optimization,
80 data_dumper_),
Per Åhgren5f1a31c2018-03-08 15:54:41 +010081 G_main_(config_.filter.main_initial,
82 config_.filter.config_change_duration_blocks),
83 G_shadow_(config_.filter.shadow_initial,
84 config.filter.config_change_duration_blocks) {
peah522d71b2017-02-23 05:16:26 -080085 RTC_DCHECK(data_dumper_);
86}
87
peah29103572017-07-11 02:54:02 -070088Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080089
90void Subtractor::HandleEchoPathChange(
91 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010092 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -080093 main_filter_.HandleEchoPathChange();
94 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +010095 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -070096 G_shadow_.HandleEchoPathChange();
Per Åhgren5f1a31c2018-03-08 15:54:41 +010097 G_main_.SetConfig(config_.filter.main_initial, true);
98 G_shadow_.SetConfig(config_.filter.shadow_initial, true);
Per Åhgren5f1a31c2018-03-08 15:54:41 +010099 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks,
100 true);
Per Åhgrena98c8072018-01-15 19:17:16 +0100101 shadow_filter_.SetSizePartitions(
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100102 config_.filter.shadow_initial.length_blocks, true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100103 };
104
Per Åhgren88cf0502018-07-16 17:08:41 +0200105 if (echo_path_variability.delay_change !=
106 EchoPathVariability::DelayAdjustment::kNone) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100107 full_reset();
Per Åhgren88cf0502018-07-16 17:08:41 +0200108 }
109
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100110 if (echo_path_variability.gain_change) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200111 G_main_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800112 }
113}
114
Per Åhgrena98c8072018-01-15 19:17:16 +0100115void Subtractor::ExitInitialState() {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100116 G_main_.SetConfig(config_.filter.main, false);
117 G_shadow_.SetConfig(config_.filter.shadow, false);
118 main_filter_.SetSizePartitions(config_.filter.main.length_blocks, false);
119 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks, false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100120}
121
peahcf02cf12017-04-05 14:18:07 -0700122void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800123 const rtc::ArrayView<const float> capture,
124 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700125 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800126 SubtractorOutput* output) {
127 RTC_DCHECK_EQ(kBlockSize, capture.size());
128 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800129 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -0700130 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800131 std::array<float, kBlockSize>& e_main = output->e_main;
132 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
133
134 FftData S;
135 FftData& G = S;
136
Per Åhgren7f5175a2018-07-25 16:30:54 +0200137 // Form the outputs of the main and shadow filters.
peah86afe9d2017-04-06 15:45:32 -0700138 main_filter_.Filter(render_buffer, &S);
Per Åhgren45231be2019-05-16 14:43:57 +0200139 PredictionError(fft_, S, y, &e_main, &output->s_main);
peah522d71b2017-02-23 05:16:26 -0800140
peah86afe9d2017-04-06 15:45:32 -0700141 shadow_filter_.Filter(render_buffer, &S);
Per Åhgren45231be2019-05-16 14:43:57 +0200142 PredictionError(fft_, S, y, &e_shadow, &output->s_shadow);
peah522d71b2017-02-23 05:16:26 -0800143
Per Åhgrene4db6a12018-07-26 15:32:24 +0200144 // Compute the signal powers in the subtractor output.
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200145 output->ComputeMetrics(y);
Per Åhgrene4db6a12018-07-26 15:32:24 +0200146
Per Åhgren7f5175a2018-07-25 16:30:54 +0200147 // Adjust the filter if needed.
148 bool main_filter_adjusted = false;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100149 filter_misadjustment_estimator_.Update(*output);
150 if (filter_misadjustment_estimator_.IsAdjustmentNeeded()) {
151 float scale = filter_misadjustment_estimator_.GetMisadjustment();
152 main_filter_.ScaleFilter(scale);
153 ScaleFilterOutput(y, scale, e_main, output->s_main);
154 filter_misadjustment_estimator_.Reset();
155 main_filter_adjusted = true;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200156 }
Per Åhgren7f5175a2018-07-25 16:30:54 +0200157
158 // Compute the FFts of the main and shadow filter outputs.
159 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
160 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
161
peah522d71b2017-02-23 05:16:26 -0800162 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100163 E_shadow.Spectrum(optimization_, output->E2_shadow);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100164 E_main.Spectrum(optimization_, output->E2_main);
165
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200166 // Compute the render powers.
167 std::array<float, kFftLengthBy2Plus1> X2_main;
168 std::array<float, kFftLengthBy2Plus1> X2_shadow_data;
169 std::array<float, kFftLengthBy2Plus1>& X2_shadow =
170 main_filter_.SizePartitions() == shadow_filter_.SizePartitions()
171 ? X2_main
172 : X2_shadow_data;
173 if (main_filter_.SizePartitions() == shadow_filter_.SizePartitions()) {
174 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2_main);
175 } else if (main_filter_.SizePartitions() > shadow_filter_.SizePartitions()) {
176 render_buffer.SpectralSums(shadow_filter_.SizePartitions(),
177 main_filter_.SizePartitions(), &X2_shadow,
178 &X2_main);
179 } else {
180 render_buffer.SpectralSums(main_filter_.SizePartitions(),
181 shadow_filter_.SizePartitions(), &X2_main,
182 &X2_shadow);
183 }
184
peah522d71b2017-02-23 05:16:26 -0800185 // Update the main filter.
Per Åhgren7f5175a2018-07-25 16:30:54 +0200186 if (!main_filter_adjusted) {
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200187 G_main_.Compute(X2_main, render_signal_analyzer, *output, main_filter_,
Per Åhgren45231be2019-05-16 14:43:57 +0200188 aec_state.SaturatedCapture(), &G);
Per Åhgren7f5175a2018-07-25 16:30:54 +0200189 } else {
190 G.re.fill(0.f);
191 G.im.fill(0.f);
192 }
peah86afe9d2017-04-06 15:45:32 -0700193 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800194 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
195 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
196
197 // Update the shadow filter.
Per Åhgrene4db6a12018-07-26 15:32:24 +0200198 poor_shadow_filter_counter_ =
199 output->e2_main < output->e2_shadow ? poor_shadow_filter_counter_ + 1 : 0;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100200 if (poor_shadow_filter_counter_ < 5) {
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200201 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_shadow,
Per Åhgrene4db6a12018-07-26 15:32:24 +0200202 shadow_filter_.SizePartitions(),
Per Åhgren45231be2019-05-16 14:43:57 +0200203 aec_state.SaturatedCapture(), &G);
Per Åhgrene4db6a12018-07-26 15:32:24 +0200204 } else {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200205 poor_shadow_filter_counter_ = 0;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100206 shadow_filter_.SetFilter(main_filter_.GetFilter());
207 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_main,
208 shadow_filter_.SizePartitions(),
Per Åhgren45231be2019-05-16 14:43:57 +0200209 aec_state.SaturatedCapture(), &G);
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100210 }
peah86afe9d2017-04-06 15:45:32 -0700211
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100212 shadow_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800213 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
214 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200215 filter_misadjustment_estimator_.Dump(data_dumper_);
Per Åhgren5c532d32018-03-22 00:29:25 +0100216 DumpFilters();
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200217
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100218 std::for_each(e_main.begin(), e_main.end(),
219 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
Per Åhgren71ebf992018-07-16 14:46:11 +0200220
221 data_dumper_->DumpWav("aec3_main_filter_output", kBlockSize, &e_main[0],
222 16000, 1);
223 data_dumper_->DumpWav("aec3_shadow_filter_output", kBlockSize, &e_shadow[0],
224 16000, 1);
peah522d71b2017-02-23 05:16:26 -0800225}
226
Per Åhgrenb20b9372018-07-13 00:22:54 +0200227void Subtractor::FilterMisadjustmentEstimator::Update(
Per Åhgrene4db6a12018-07-26 15:32:24 +0200228 const SubtractorOutput& output) {
229 e2_acum_ += output.e2_main;
230 y2_acum_ += output.y2;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200231 if (++n_blocks_acum_ == n_blocks_) {
232 if (y2_acum_ > n_blocks_ * 200.f * 200.f * kBlockSize) {
233 float update = (e2_acum_ / y2_acum_);
234 if (e2_acum_ > n_blocks_ * 7500.f * 7500.f * kBlockSize) {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200235 // Duration equal to blockSizeMs * n_blocks_ * 4.
236 overhang_ = 4;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200237 } else {
238 overhang_ = std::max(overhang_ - 1, 0);
239 }
240
241 if ((update < inv_misadjustment_) || (overhang_ > 0)) {
242 inv_misadjustment_ += 0.1f * (update - inv_misadjustment_);
243 }
244 }
245 e2_acum_ = 0.f;
246 y2_acum_ = 0.f;
247 n_blocks_acum_ = 0;
248 }
249}
250
251void Subtractor::FilterMisadjustmentEstimator::Reset() {
252 e2_acum_ = 0.f;
253 y2_acum_ = 0.f;
254 n_blocks_acum_ = 0;
255 inv_misadjustment_ = 0.f;
256 overhang_ = 0.f;
257}
258
259void Subtractor::FilterMisadjustmentEstimator::Dump(
260 ApmDataDumper* data_dumper) const {
261 data_dumper->DumpRaw("aec3_inv_misadjustment_factor", inv_misadjustment_);
262}
263
peah522d71b2017-02-23 05:16:26 -0800264} // namespace webrtc