blob: e3b4f87c0fe58c7ce984a2a2bc26675f5616f816 [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,
Per Åhgren9845a672018-01-15 13:09:02 +010029 std::array<float, kBlockSize>* s,
30 bool* saturation) {
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
Per Åhgren9845a672018-01-15 13:09:02 +010037 *saturation = false;
38
peah29103572017-07-11 02:54:02 -070039 if (s) {
40 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010041 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070042 }
Per Åhgren9845a672018-01-15 13:09:02 +010043 auto result = std::minmax_element(s->begin(), s->end());
44 *saturation = *result.first <= -32768 || *result.first >= 32767;
45 }
46 if (!(*saturation)) {
47 auto result = std::minmax_element(e->begin(), e->end());
48 *saturation = *result.first <= -32768 || *result.first >= 32767;
peah29103572017-07-11 02:54:02 -070049 }
Per Åhgren7634c162017-12-18 15:45:49 +010050
51 std::for_each(e->begin(), e->end(),
52 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
peah522d71b2017-02-23 05:16:26 -080053}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010054
peah522d71b2017-02-23 05:16:26 -080055} // namespace
56
Per Åhgren09a718a2017-12-11 22:28:45 +010057Subtractor::Subtractor(const EchoCanceller3Config& config,
58 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080059 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080060 : fft_(),
61 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080062 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +010063 config_(config),
64 main_filter_(config_.filter.main.length_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010065 optimization,
66 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010067 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010068 optimization,
69 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010070 G_main_(config_.filter.main_initial),
71 G_shadow_(config_.filter.shadow_initial) {
peah522d71b2017-02-23 05:16:26 -080072 RTC_DCHECK(data_dumper_);
Per Åhgren08ea5892018-01-15 08:07:41 +010073 // Currently, the rest of AEC3 requires the main and shadow filter lengths to
74 // be identical.
Per Åhgrena98c8072018-01-15 19:17:16 +010075 RTC_DCHECK_EQ(config_.filter.main.length_blocks,
76 config_.filter.shadow.length_blocks);
77 RTC_DCHECK_EQ(config_.filter.main_initial.length_blocks,
78 config_.filter.shadow_initial.length_blocks);
79
80 RTC_DCHECK_GE(config_.filter.main.length_blocks,
81 config_.filter.main_initial.length_blocks);
82 RTC_DCHECK_GE(config_.filter.shadow.length_blocks,
83 config_.filter.shadow_initial.length_blocks);
84
85 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks);
86 shadow_filter_.SetSizePartitions(config_.filter.shadow_initial.length_blocks);
peah522d71b2017-02-23 05:16:26 -080087}
88
peah29103572017-07-11 02:54:02 -070089Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080090
91void Subtractor::HandleEchoPathChange(
92 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010093 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -080094 main_filter_.HandleEchoPathChange();
95 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +010096 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -070097 G_shadow_.HandleEchoPathChange();
Per Åhgrena98c8072018-01-15 19:17:16 +010098 G_main_.SetConfig(config_.filter.main_initial);
99 G_shadow_.SetConfig(config_.filter.shadow_initial);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100100 main_filter_converged_ = false;
101 shadow_filter_converged_ = false;
Per Åhgrena98c8072018-01-15 19:17:16 +0100102 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks);
103 shadow_filter_.SetSizePartitions(
104 config_.filter.shadow_initial.length_blocks);
Per Åhgren8ba58612017-12-01 23:01:44 +0100105 };
106
107 // TODO(peah): Add delay-change specific reset behavior.
108 if ((echo_path_variability.delay_change ==
109 EchoPathVariability::DelayAdjustment::kBufferFlush) ||
110 (echo_path_variability.delay_change ==
111 EchoPathVariability::DelayAdjustment::kDelayReset)) {
112 full_reset();
113 } else if (echo_path_variability.delay_change ==
114 EchoPathVariability::DelayAdjustment::kNewDetectedDelay) {
115 full_reset();
116 } else if (echo_path_variability.delay_change ==
117 EchoPathVariability::DelayAdjustment::kBufferReadjustment) {
118 full_reset();
peah522d71b2017-02-23 05:16:26 -0800119 }
120}
121
Per Åhgrena98c8072018-01-15 19:17:16 +0100122void Subtractor::ExitInitialState() {
123 G_main_.SetConfig(config_.filter.main);
124 G_shadow_.SetConfig(config_.filter.shadow);
125 main_filter_.SetSizePartitions(config_.filter.main.length_blocks);
126 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks);
127}
128
peahcf02cf12017-04-05 14:18:07 -0700129void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800130 const rtc::ArrayView<const float> capture,
131 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700132 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800133 SubtractorOutput* output) {
134 RTC_DCHECK_EQ(kBlockSize, capture.size());
135 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800136 FftData& E_main = output->E_main;
Per Åhgrend20639f2018-01-11 10:29:49 +0100137 FftData& E_main_nonwindowed = output->E_main_nonwindowed;
peah86afe9d2017-04-06 15:45:32 -0700138 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800139 std::array<float, kBlockSize>& e_main = output->e_main;
140 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
141
142 FftData S;
143 FftData& G = S;
144
peah86afe9d2017-04-06 15:45:32 -0700145 // Form the output of the main filter.
146 main_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100147 bool main_saturation = false;
148 PredictionError(fft_, S, y, &e_main, &output->s_main, &main_saturation);
Per Åhgrend20639f2018-01-11 10:29:49 +0100149 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
peah522d71b2017-02-23 05:16:26 -0800150
peah86afe9d2017-04-06 15:45:32 -0700151 // Form the output of the shadow filter.
152 shadow_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100153 bool shadow_saturation = false;
154 PredictionError(fft_, S, y, &e_shadow, nullptr, &shadow_saturation);
Per Åhgrend20639f2018-01-11 10:29:49 +0100155 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
peah522d71b2017-02-23 05:16:26 -0800156
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100157 if (!(main_filter_converged_ || shadow_filter_converged_)) {
Per Åhgren63b494d2017-12-06 11:32:38 +0100158 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
Per Åhgren63b494d2017-12-06 11:32:38 +0100159 const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
160
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100161 if (!main_filter_converged_) {
162 const float e2_main =
163 std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
164 main_filter_converged_ = e2_main > 0.1 * y2;
165 }
166
167 if (!shadow_filter_converged_) {
168 const float e2_shadow = std::accumulate(e_shadow.begin(), e_shadow.end(),
169 0.f, sum_of_squares);
170 shadow_filter_converged_ = e2_shadow > 0.1 * y2;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200171 }
172 }
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
178 if (main_filter_converged_ || !shadow_filter_converged_) {
179 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kRectangular,
180 &E_main_nonwindowed);
181 E_main_nonwindowed.Spectrum(optimization_, output->E2_main_nonwindowed);
182 } else {
183 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kRectangular,
184 &E_main_nonwindowed);
185 E_main_nonwindowed.Spectrum(optimization_, output->E2_main_nonwindowed);
186 }
peah522d71b2017-02-23 05:16:26 -0800187
188 // Update the main filter.
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100189 std::array<float, kFftLengthBy2Plus1> X2;
190 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2);
191 G_main_.Compute(X2, render_signal_analyzer, *output, main_filter_,
Per Åhgren9845a672018-01-15 13:09:02 +0100192 aec_state.SaturatedCapture() || main_saturation, &G);
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 Åhgrenec22e3f2017-12-20 15:20:37 +0100198 if (shadow_filter_.SizePartitions() != main_filter_.SizePartitions()) {
199 render_buffer.SpectralSum(shadow_filter_.SizePartitions(), &X2);
200 }
201 G_shadow_.Compute(X2, render_signal_analyzer, E_shadow,
peah86afe9d2017-04-06 15:45:32 -0700202 shadow_filter_.SizePartitions(),
Per Åhgren9845a672018-01-15 13:09:02 +0100203 aec_state.SaturatedCapture() || shadow_saturation, &G);
peah86afe9d2017-04-06 15:45:32 -0700204 shadow_filter_.Adapt(render_buffer, G);
205
peah522d71b2017-02-23 05:16:26 -0800206 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
207 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
208
209 main_filter_.DumpFilter("aec3_subtractor_H_main");
210 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
211}
212
213} // namespace webrtc