blob: 1cb165bbfcbf31d41a6d772cb0a5a1a6aa872629 [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
Per Åhgren7634c162017-12-18 15:45:49 +010025const float kHamming64[64] = {
26 0.08f, 0.08228584f, 0.08912066f, 0.10043651f, 0.11612094f,
27 0.13601808f, 0.15993016f, 0.18761956f, 0.21881106f, 0.25319469f,
28 0.29042872f, 0.3301431f, 0.37194313f, 0.41541338f, 0.46012184f,
29 0.50562416f, 0.55146812f, 0.5971981f, 0.64235963f, 0.68650386f,
30 0.72919207f, 0.77f, 0.80852209f, 0.84437549f, 0.87720386f,
31 0.90668095f, 0.93251381f, 0.95444568f, 0.97225861f, 0.98577555f,
32 0.99486218f, 0.99942818f, 0.99942818f, 0.99486218f, 0.98577555f,
33 0.97225861f, 0.95444568f, 0.93251381f, 0.90668095f, 0.87720386f,
34 0.84437549f, 0.80852209f, 0.77f, 0.72919207f, 0.68650386f,
35 0.64235963f, 0.5971981f, 0.55146812f, 0.50562416f, 0.46012184f,
36 0.41541338f, 0.37194313f, 0.3301431f, 0.29042872f, 0.25319469f,
37 0.21881106f, 0.18761956f, 0.15993016f, 0.13601808f, 0.11612094f,
38 0.10043651f, 0.08912066f, 0.08228584f, 0.08f};
39
peah86afe9d2017-04-06 15:45:32 -070040void PredictionError(const Aec3Fft& fft,
41 const FftData& S,
42 rtc::ArrayView<const float> y,
43 std::array<float, kBlockSize>* e,
peah29103572017-07-11 02:54:02 -070044 FftData* E,
45 std::array<float, kBlockSize>* s) {
Per Åhgren7634c162017-12-18 15:45:49 +010046 std::array<float, kFftLength> tmp;
47 fft.Ifft(S, &tmp);
peah522d71b2017-02-23 05:16:26 -080048 constexpr float kScale = 1.0f / kFftLengthBy2;
Per Åhgren7634c162017-12-18 15:45:49 +010049 std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(),
50 [&](float a, float b) { return a - b * kScale; });
peah29103572017-07-11 02:54:02 -070051
52 if (s) {
53 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010054 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070055 }
56 }
Per Åhgren7634c162017-12-18 15:45:49 +010057
58 std::for_each(e->begin(), e->end(),
59 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
60
61 RTC_DCHECK_EQ(64, e->size());
62 RTC_DCHECK_LE(64, tmp.size());
63 std::transform(e->begin(), e->end(), std::begin(kHamming64), tmp.begin(),
64 [](float a, float b) { return a * b; });
65
66 fft.ZeroPaddedFft(rtc::ArrayView<const float>(tmp.data(), 64), E);
peah522d71b2017-02-23 05:16:26 -080067}
68} // namespace
69
Per Åhgren09a718a2017-12-11 22:28:45 +010070Subtractor::Subtractor(const EchoCanceller3Config& config,
71 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080072 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080073 : fft_(),
74 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080075 optimization_(optimization),
Per Åhgren09a718a2017-12-11 22:28:45 +010076 main_filter_(config.filter.length_blocks, optimization, data_dumper_),
Per Åhgren477f2892017-12-11 23:29:44 +010077 shadow_filter_(config.filter.length_blocks, optimization, data_dumper_),
78 G_main_(config.filter.leakage_converged,
79 config.filter.leakage_diverged,
Per Åhgrenb6f9e6c2017-12-12 22:49:41 +010080 config.filter.main_noise_gate,
81 config.filter.error_floor),
Per Åhgren477f2892017-12-11 23:29:44 +010082 G_shadow_(config.filter.shadow_rate, config.filter.shadow_noise_gate) {
peah522d71b2017-02-23 05:16:26 -080083 RTC_DCHECK(data_dumper_);
84}
85
peah29103572017-07-11 02:54:02 -070086Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080087
88void Subtractor::HandleEchoPathChange(
89 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010090 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -080091 main_filter_.HandleEchoPathChange();
92 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +010093 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -070094 G_shadow_.HandleEchoPathChange();
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
peahcf02cf12017-04-05 14:18:07 -0700113void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800114 const rtc::ArrayView<const float> capture,
115 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700116 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800117 SubtractorOutput* output) {
118 RTC_DCHECK_EQ(kBlockSize, capture.size());
119 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800120 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -0700121 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800122 std::array<float, kBlockSize>& e_main = output->e_main;
123 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
124
125 FftData S;
126 FftData& G = S;
127
peah86afe9d2017-04-06 15:45:32 -0700128 // Form the output of the main filter.
129 main_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -0700130 PredictionError(fft_, S, y, &e_main, &E_main, &output->s_main);
peah522d71b2017-02-23 05:16:26 -0800131
peah86afe9d2017-04-06 15:45:32 -0700132 // Form the output of the shadow filter.
133 shadow_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -0700134 PredictionError(fft_, S, y, &e_shadow, &E_shadow, nullptr);
peah522d71b2017-02-23 05:16:26 -0800135
Per Åhgren7ddd4632017-10-25 02:59:45 +0200136 if (!converged_filter_) {
Per Åhgren63b494d2017-12-06 11:32:38 +0100137 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
138 const float e2_main =
139 std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
140 const float e2_shadow =
141 std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
142 const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
143
144 if (y2 > kBlockSize * 50.f * 50.f) {
145 converged_filter_ = (e2_main > 0.3 * y2 || e2_shadow > 0.1 * y2);
Per Åhgren1b4059e2017-10-15 20:19:21 +0200146 }
147 }
148
peah522d71b2017-02-23 05:16:26 -0800149 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100150 E_main.Spectrum(optimization_, output->E2_main);
151 E_shadow.Spectrum(optimization_, output->E2_shadow);
peah522d71b2017-02-23 05:16:26 -0800152
153 // Update the main filter.
peah86afe9d2017-04-06 15:45:32 -0700154 G_main_.Compute(render_buffer, render_signal_analyzer, *output, main_filter_,
155 aec_state.SaturatedCapture(), &G);
156 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800157 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
158 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
159
160 // Update the shadow filter.
peah86afe9d2017-04-06 15:45:32 -0700161 G_shadow_.Compute(render_buffer, render_signal_analyzer, E_shadow,
162 shadow_filter_.SizePartitions(),
163 aec_state.SaturatedCapture(), &G);
164 shadow_filter_.Adapt(render_buffer, G);
165
peah522d71b2017-02-23 05:16:26 -0800166 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
167 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
168
169 main_filter_.DumpFilter("aec3_subtractor_H_main");
170 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
171}
172
173} // namespace webrtc