blob: aa38a34bec7a137f4c6bf42d3fa3869610176f0a [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 Åhgren9845a672018-01-15 13:09:02 +010030 std::array<float, kBlockSize>* s,
31 bool* saturation) {
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
Per Åhgren9845a672018-01-15 13:09:02 +010038 *saturation = false;
39
peah29103572017-07-11 02:54:02 -070040 if (s) {
41 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010042 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070043 }
Per Åhgren9845a672018-01-15 13:09:02 +010044 auto result = std::minmax_element(s->begin(), s->end());
45 *saturation = *result.first <= -32768 || *result.first >= 32767;
46 }
47 if (!(*saturation)) {
48 auto result = std::minmax_element(e->begin(), e->end());
49 *saturation = *result.first <= -32768 || *result.first >= 32767;
peah29103572017-07-11 02:54:02 -070050 }
Per Åhgren7634c162017-12-18 15:45:49 +010051
Gustaf Ullberg68d6d442019-01-29 10:08:15 +010052 *saturation = false;
peah522d71b2017-02-23 05:16:26 -080053}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010054
Per Åhgren7f5175a2018-07-25 16:30:54 +020055void ScaleFilterOutput(rtc::ArrayView<const float> y,
56 float factor,
57 rtc::ArrayView<float> e,
58 rtc::ArrayView<float> s) {
59 RTC_DCHECK_EQ(y.size(), e.size());
60 RTC_DCHECK_EQ(y.size(), s.size());
61 for (size_t k = 0; k < y.size(); ++k) {
62 s[k] *= factor;
63 e[k] = y[k] - s[k];
64 }
65}
66
peah522d71b2017-02-23 05:16:26 -080067} // namespace
68
Per Åhgren09a718a2017-12-11 22:28:45 +010069Subtractor::Subtractor(const EchoCanceller3Config& config,
70 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080071 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080072 : fft_(),
73 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080074 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +010075 config_(config),
76 main_filter_(config_.filter.main.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010077 config_.filter.main_initial.length_blocks,
78 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010079 optimization,
80 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010081 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010082 config_.filter.shadow_initial.length_blocks,
83 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010084 optimization,
85 data_dumper_),
Per Åhgren5f1a31c2018-03-08 15:54:41 +010086 G_main_(config_.filter.main_initial,
87 config_.filter.config_change_duration_blocks),
88 G_shadow_(config_.filter.shadow_initial,
89 config.filter.config_change_duration_blocks) {
peah522d71b2017-02-23 05:16:26 -080090 RTC_DCHECK(data_dumper_);
91}
92
peah29103572017-07-11 02:54:02 -070093Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080094
95void Subtractor::HandleEchoPathChange(
96 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010097 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -080098 main_filter_.HandleEchoPathChange();
99 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +0100100 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -0700101 G_shadow_.HandleEchoPathChange();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100102 G_main_.SetConfig(config_.filter.main_initial, true);
103 G_shadow_.SetConfig(config_.filter.shadow_initial, true);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100104 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks,
105 true);
Per Åhgrena98c8072018-01-15 19:17:16 +0100106 shadow_filter_.SetSizePartitions(
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100107 config_.filter.shadow_initial.length_blocks, true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100108 };
109
Per Åhgren88cf0502018-07-16 17:08:41 +0200110 if (echo_path_variability.delay_change !=
111 EchoPathVariability::DelayAdjustment::kNone) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100112 full_reset();
Per Åhgren88cf0502018-07-16 17:08:41 +0200113 }
114
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100115 if (echo_path_variability.gain_change) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200116 G_main_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800117 }
118}
119
Per Åhgrena98c8072018-01-15 19:17:16 +0100120void Subtractor::ExitInitialState() {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100121 G_main_.SetConfig(config_.filter.main, false);
122 G_shadow_.SetConfig(config_.filter.shadow, false);
123 main_filter_.SetSizePartitions(config_.filter.main.length_blocks, false);
124 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks, false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100125}
126
peahcf02cf12017-04-05 14:18:07 -0700127void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800128 const rtc::ArrayView<const float> capture,
129 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700130 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800131 SubtractorOutput* output) {
132 RTC_DCHECK_EQ(kBlockSize, capture.size());
133 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800134 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -0700135 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800136 std::array<float, kBlockSize>& e_main = output->e_main;
137 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
138
139 FftData S;
140 FftData& G = S;
141
Per Åhgren7f5175a2018-07-25 16:30:54 +0200142 // Form the outputs of the main and shadow filters.
peah86afe9d2017-04-06 15:45:32 -0700143 main_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100144 bool main_saturation = false;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100145 PredictionError(fft_, S, y, &e_main, &output->s_main, &main_saturation);
peah522d71b2017-02-23 05:16:26 -0800146
peah86afe9d2017-04-06 15:45:32 -0700147 shadow_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100148 bool shadow_saturation = false;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100149 PredictionError(fft_, S, y, &e_shadow, &output->s_shadow, &shadow_saturation);
peah522d71b2017-02-23 05:16:26 -0800150
Per Åhgrene4db6a12018-07-26 15:32:24 +0200151 // Compute the signal powers in the subtractor output.
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200152 output->ComputeMetrics(y);
Per Åhgrene4db6a12018-07-26 15:32:24 +0200153
Per Åhgren7f5175a2018-07-25 16:30:54 +0200154 // Adjust the filter if needed.
155 bool main_filter_adjusted = false;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100156 filter_misadjustment_estimator_.Update(*output);
157 if (filter_misadjustment_estimator_.IsAdjustmentNeeded()) {
158 float scale = filter_misadjustment_estimator_.GetMisadjustment();
159 main_filter_.ScaleFilter(scale);
160 ScaleFilterOutput(y, scale, e_main, output->s_main);
161 filter_misadjustment_estimator_.Reset();
162 main_filter_adjusted = true;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200163 }
Per Åhgren7f5175a2018-07-25 16:30:54 +0200164
165 // Compute the FFts of the main and shadow filter outputs.
166 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
167 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
168
peah522d71b2017-02-23 05:16:26 -0800169 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100170 E_shadow.Spectrum(optimization_, output->E2_shadow);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100171 E_main.Spectrum(optimization_, output->E2_main);
172
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200173 // Compute the render powers.
174 std::array<float, kFftLengthBy2Plus1> X2_main;
175 std::array<float, kFftLengthBy2Plus1> X2_shadow_data;
176 std::array<float, kFftLengthBy2Plus1>& X2_shadow =
177 main_filter_.SizePartitions() == shadow_filter_.SizePartitions()
178 ? X2_main
179 : X2_shadow_data;
180 if (main_filter_.SizePartitions() == shadow_filter_.SizePartitions()) {
181 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2_main);
182 } else if (main_filter_.SizePartitions() > shadow_filter_.SizePartitions()) {
183 render_buffer.SpectralSums(shadow_filter_.SizePartitions(),
184 main_filter_.SizePartitions(), &X2_shadow,
185 &X2_main);
186 } else {
187 render_buffer.SpectralSums(main_filter_.SizePartitions(),
188 shadow_filter_.SizePartitions(), &X2_main,
189 &X2_shadow);
190 }
191
peah522d71b2017-02-23 05:16:26 -0800192 // Update the main filter.
Per Åhgren7f5175a2018-07-25 16:30:54 +0200193 if (!main_filter_adjusted) {
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200194 G_main_.Compute(X2_main, render_signal_analyzer, *output, main_filter_,
Per Åhgren7f5175a2018-07-25 16:30:54 +0200195 aec_state.SaturatedCapture() || main_saturation, &G);
196 } else {
197 G.re.fill(0.f);
198 G.im.fill(0.f);
199 }
peah86afe9d2017-04-06 15:45:32 -0700200 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800201 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
202 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
203
204 // Update the shadow filter.
Per Åhgrene4db6a12018-07-26 15:32:24 +0200205 poor_shadow_filter_counter_ =
206 output->e2_main < output->e2_shadow ? poor_shadow_filter_counter_ + 1 : 0;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100207 if (poor_shadow_filter_counter_ < 5) {
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200208 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_shadow,
Per Åhgrene4db6a12018-07-26 15:32:24 +0200209 shadow_filter_.SizePartitions(),
210 aec_state.SaturatedCapture() || shadow_saturation, &G);
Per Åhgrene4db6a12018-07-26 15:32:24 +0200211 } else {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200212 poor_shadow_filter_counter_ = 0;
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100213 shadow_filter_.SetFilter(main_filter_.GetFilter());
214 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_main,
215 shadow_filter_.SizePartitions(),
216 aec_state.SaturatedCapture() || main_saturation, &G);
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100217 }
peah86afe9d2017-04-06 15:45:32 -0700218
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100219 shadow_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800220 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
221 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200222 filter_misadjustment_estimator_.Dump(data_dumper_);
Per Åhgren5c532d32018-03-22 00:29:25 +0100223 DumpFilters();
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200224
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100225 std::for_each(e_main.begin(), e_main.end(),
226 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
Per Åhgren71ebf992018-07-16 14:46:11 +0200227
228 data_dumper_->DumpWav("aec3_main_filter_output", kBlockSize, &e_main[0],
229 16000, 1);
230 data_dumper_->DumpWav("aec3_shadow_filter_output", kBlockSize, &e_shadow[0],
231 16000, 1);
peah522d71b2017-02-23 05:16:26 -0800232}
233
Per Åhgrenb20b9372018-07-13 00:22:54 +0200234void Subtractor::FilterMisadjustmentEstimator::Update(
Per Åhgrene4db6a12018-07-26 15:32:24 +0200235 const SubtractorOutput& output) {
236 e2_acum_ += output.e2_main;
237 y2_acum_ += output.y2;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200238 if (++n_blocks_acum_ == n_blocks_) {
239 if (y2_acum_ > n_blocks_ * 200.f * 200.f * kBlockSize) {
240 float update = (e2_acum_ / y2_acum_);
241 if (e2_acum_ > n_blocks_ * 7500.f * 7500.f * kBlockSize) {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200242 // Duration equal to blockSizeMs * n_blocks_ * 4.
243 overhang_ = 4;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200244 } else {
245 overhang_ = std::max(overhang_ - 1, 0);
246 }
247
248 if ((update < inv_misadjustment_) || (overhang_ > 0)) {
249 inv_misadjustment_ += 0.1f * (update - inv_misadjustment_);
250 }
251 }
252 e2_acum_ = 0.f;
253 y2_acum_ = 0.f;
254 n_blocks_acum_ = 0;
255 }
256}
257
258void Subtractor::FilterMisadjustmentEstimator::Reset() {
259 e2_acum_ = 0.f;
260 y2_acum_ = 0.f;
261 n_blocks_acum_ = 0;
262 inv_misadjustment_ = 0.f;
263 overhang_ = 0.f;
264}
265
266void Subtractor::FilterMisadjustmentEstimator::Dump(
267 ApmDataDumper* data_dumper) const {
268 data_dumper->DumpRaw("aec3_inv_misadjustment_factor", inv_misadjustment_);
269}
270
peah522d71b2017-02-23 05:16:26 -0800271} // namespace webrtc