blob: 9e690f84917cd25f1cd6d30787a284b263837d71 [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,
peah29103572017-07-11 02:54:02 -070029 FftData* E,
30 std::array<float, kBlockSize>* s) {
31 std::array<float, kFftLength> s_scratch;
32 fft.Ifft(S, &s_scratch);
peah522d71b2017-02-23 05:16:26 -080033 constexpr float kScale = 1.0f / kFftLengthBy2;
peah29103572017-07-11 02:54:02 -070034 std::transform(y.begin(), y.end(), s_scratch.begin() + kFftLengthBy2,
35 e->begin(), [&](float a, float b) { return a - b * kScale; });
kwiberg07038562017-06-12 11:40:47 -070036 std::for_each(e->begin(), e->end(),
37 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
peah522d71b2017-02-23 05:16:26 -080038 fft.ZeroPaddedFft(*e, E);
peah29103572017-07-11 02:54:02 -070039
40 if (s) {
41 for (size_t k = 0; k < s->size(); ++k) {
42 (*s)[k] = kScale * s_scratch[k + kFftLengthBy2];
43 }
44 }
peah522d71b2017-02-23 05:16:26 -080045}
46} // namespace
47
Per Åhgren09a718a2017-12-11 22:28:45 +010048Subtractor::Subtractor(const EchoCanceller3Config& config,
49 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080050 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080051 : fft_(),
52 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080053 optimization_(optimization),
Per Åhgren09a718a2017-12-11 22:28:45 +010054 main_filter_(config.filter.length_blocks, optimization, data_dumper_),
55 shadow_filter_(config.filter.length_blocks, optimization, data_dumper_) {
peah522d71b2017-02-23 05:16:26 -080056 RTC_DCHECK(data_dumper_);
57}
58
peah29103572017-07-11 02:54:02 -070059Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080060
61void Subtractor::HandleEchoPathChange(
62 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010063 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -080064 main_filter_.HandleEchoPathChange();
65 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +010066 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -070067 G_shadow_.HandleEchoPathChange();
Per Åhgren1b4059e2017-10-15 20:19:21 +020068 converged_filter_ = false;
Per Åhgren8ba58612017-12-01 23:01:44 +010069 };
70
71 // TODO(peah): Add delay-change specific reset behavior.
72 if ((echo_path_variability.delay_change ==
73 EchoPathVariability::DelayAdjustment::kBufferFlush) ||
74 (echo_path_variability.delay_change ==
75 EchoPathVariability::DelayAdjustment::kDelayReset)) {
76 full_reset();
77 } else if (echo_path_variability.delay_change ==
78 EchoPathVariability::DelayAdjustment::kNewDetectedDelay) {
79 full_reset();
80 } else if (echo_path_variability.delay_change ==
81 EchoPathVariability::DelayAdjustment::kBufferReadjustment) {
82 full_reset();
peah522d71b2017-02-23 05:16:26 -080083 }
84}
85
peahcf02cf12017-04-05 14:18:07 -070086void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080087 const rtc::ArrayView<const float> capture,
88 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070089 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -080090 SubtractorOutput* output) {
91 RTC_DCHECK_EQ(kBlockSize, capture.size());
92 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -080093 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -070094 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -080095 std::array<float, kBlockSize>& e_main = output->e_main;
96 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
97
98 FftData S;
99 FftData& G = S;
100
peah86afe9d2017-04-06 15:45:32 -0700101 // Form the output of the main filter.
102 main_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -0700103 PredictionError(fft_, S, y, &e_main, &E_main, &output->s_main);
peah522d71b2017-02-23 05:16:26 -0800104
peah86afe9d2017-04-06 15:45:32 -0700105 // Form the output of the shadow filter.
106 shadow_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -0700107 PredictionError(fft_, S, y, &e_shadow, &E_shadow, nullptr);
peah522d71b2017-02-23 05:16:26 -0800108
Per Åhgren1b4059e2017-10-15 20:19:21 +0200109
Per Åhgren7ddd4632017-10-25 02:59:45 +0200110 if (!converged_filter_) {
Per Åhgren63b494d2017-12-06 11:32:38 +0100111 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
112 const float e2_main =
113 std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
114 const float e2_shadow =
115 std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
116 const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
117
118 if (y2 > kBlockSize * 50.f * 50.f) {
119 converged_filter_ = (e2_main > 0.3 * y2 || e2_shadow > 0.1 * y2);
Per Åhgren1b4059e2017-10-15 20:19:21 +0200120 }
121 }
122
peah522d71b2017-02-23 05:16:26 -0800123 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100124 E_main.Spectrum(optimization_, output->E2_main);
125 E_shadow.Spectrum(optimization_, output->E2_shadow);
peah522d71b2017-02-23 05:16:26 -0800126
127 // Update the main filter.
peah86afe9d2017-04-06 15:45:32 -0700128 G_main_.Compute(render_buffer, render_signal_analyzer, *output, main_filter_,
129 aec_state.SaturatedCapture(), &G);
130 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800131 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
132 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
133
134 // Update the shadow filter.
peah86afe9d2017-04-06 15:45:32 -0700135 G_shadow_.Compute(render_buffer, render_signal_analyzer, E_shadow,
136 shadow_filter_.SizePartitions(),
137 aec_state.SaturatedCapture(), &G);
138 shadow_filter_.Adapt(render_buffer, G);
139
peah522d71b2017-02-23 05:16:26 -0800140 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
141 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
142
143 main_filter_.DumpFilter("aec3_subtractor_H_main");
144 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
145}
146
147} // namespace webrtc