blob: c1909f3b90bc59fc85bdaee8470656b8136c2404 [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"
19#include "rtc_base/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
peah522d71b2017-02-23 05:16:26 -080048Subtractor::Subtractor(ApmDataDumper* data_dumper,
49 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080050 : fft_(),
51 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080052 optimization_(optimization),
peah86afe9d2017-04-06 15:45:32 -070053 main_filter_(kAdaptiveFilterLength, optimization, data_dumper_),
54 shadow_filter_(kAdaptiveFilterLength, optimization, data_dumper_) {
peah522d71b2017-02-23 05:16:26 -080055 RTC_DCHECK(data_dumper_);
56}
57
peah29103572017-07-11 02:54:02 -070058Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080059
60void Subtractor::HandleEchoPathChange(
61 const EchoPathVariability& echo_path_variability) {
62 if (echo_path_variability.delay_change) {
63 main_filter_.HandleEchoPathChange();
64 shadow_filter_.HandleEchoPathChange();
65 G_main_.HandleEchoPathChange();
peahdebaa442017-05-03 05:39:09 -070066 G_shadow_.HandleEchoPathChange();
Per Åhgren1b4059e2017-10-15 20:19:21 +020067 converged_filter_ = false;
peah522d71b2017-02-23 05:16:26 -080068 }
69}
70
peahcf02cf12017-04-05 14:18:07 -070071void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080072 const rtc::ArrayView<const float> capture,
73 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070074 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -080075 SubtractorOutput* output) {
76 RTC_DCHECK_EQ(kBlockSize, capture.size());
77 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -080078 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -070079 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -080080 std::array<float, kBlockSize>& e_main = output->e_main;
81 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
82
83 FftData S;
84 FftData& G = S;
85
peah86afe9d2017-04-06 15:45:32 -070086 // Form the output of the main filter.
87 main_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -070088 PredictionError(fft_, S, y, &e_main, &E_main, &output->s_main);
peah522d71b2017-02-23 05:16:26 -080089
peah86afe9d2017-04-06 15:45:32 -070090 // Form the output of the shadow filter.
91 shadow_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -070092 PredictionError(fft_, S, y, &e_shadow, &E_shadow, nullptr);
peah522d71b2017-02-23 05:16:26 -080093
Per Åhgren1b4059e2017-10-15 20:19:21 +020094 if (!converged_filter_) {
95 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
96 const float e2_main =
97 std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
98 const float e2_shadow =
99 std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
100 const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
101
102 if (y2 > kBlockSize * 50.f * 50.f) {
103 converged_filter_ = (e2_main > 0.3 * y2 || e2_shadow > 0.1 * y2);
104 }
105 }
106
peah522d71b2017-02-23 05:16:26 -0800107 // Compute spectra for future use.
108 E_main.Spectrum(optimization_, &output->E2_main);
109 E_shadow.Spectrum(optimization_, &output->E2_shadow);
110
111 // Update the main filter.
peah86afe9d2017-04-06 15:45:32 -0700112 G_main_.Compute(render_buffer, render_signal_analyzer, *output, main_filter_,
113 aec_state.SaturatedCapture(), &G);
114 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800115 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
116 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
117
118 // Update the shadow filter.
peah86afe9d2017-04-06 15:45:32 -0700119 G_shadow_.Compute(render_buffer, render_signal_analyzer, E_shadow,
120 shadow_filter_.SizePartitions(),
121 aec_state.SaturatedCapture(), &G);
122 shadow_filter_.Adapt(render_buffer, G);
123
peah522d71b2017-02-23 05:16:26 -0800124 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
125 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
126
127 main_filter_.DumpFilter("aec3_subtractor_H_main");
128 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
129}
130
131} // namespace webrtc