blob: f1a9b80fca381ca8206b011ad782b67e01925845 [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) {
Per Åhgren7ddd4632017-10-25 02:59:45 +020062 use_shadow_filter_frequency_response_ = false;
peah522d71b2017-02-23 05:16:26 -080063 if (echo_path_variability.delay_change) {
64 main_filter_.HandleEchoPathChange();
65 shadow_filter_.HandleEchoPathChange();
66 G_main_.HandleEchoPathChange();
peahdebaa442017-05-03 05:39:09 -070067 G_shadow_.HandleEchoPathChange();
Per Åhgren1b4059e2017-10-15 20:19:21 +020068 converged_filter_ = false;
Per Åhgren7ddd4632017-10-25 02:59:45 +020069 converged_filter_counter_ = 0;
peah522d71b2017-02-23 05:16:26 -080070 }
71}
72
peahcf02cf12017-04-05 14:18:07 -070073void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080074 const rtc::ArrayView<const float> capture,
75 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070076 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -080077 SubtractorOutput* output) {
78 RTC_DCHECK_EQ(kBlockSize, capture.size());
79 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -080080 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -070081 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -080082 std::array<float, kBlockSize>& e_main = output->e_main;
83 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
84
85 FftData S;
86 FftData& G = S;
87
peah86afe9d2017-04-06 15:45:32 -070088 // Form the output of the main filter.
89 main_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -070090 PredictionError(fft_, S, y, &e_main, &E_main, &output->s_main);
peah522d71b2017-02-23 05:16:26 -080091
peah86afe9d2017-04-06 15:45:32 -070092 // Form the output of the shadow filter.
93 shadow_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -070094 PredictionError(fft_, S, y, &e_shadow, &E_shadow, nullptr);
peah522d71b2017-02-23 05:16:26 -080095
Per Åhgren7ddd4632017-10-25 02:59:45 +020096 // Determine which frequency response should be used.
97 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
98 const float e2_main =
99 std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
100 const float e2_shadow =
101 std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
102 const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
Per Åhgren1b4059e2017-10-15 20:19:21 +0200103
Per Åhgren7ddd4632017-10-25 02:59:45 +0200104 if (e2_main < e2_shadow && e2_main < 0.1 * y2) {
105 use_shadow_filter_frequency_response_ = false;
106 } else if (e2_shadow < e2_main && e2_shadow < 0.01 * y2) {
107 use_shadow_filter_frequency_response_ = true;
108 }
109
110 // Flag whether the filter has at some point converged.
111 // TODO(peah): Consider using a timeout for this.
112 if (!converged_filter_) {
113 if (y2 > kBlockSize * 100.f * 100.f) {
114 if (e2_main < 0.3 * y2) {
115 converged_filter_ = (++converged_filter_counter_) > 10;
116 } else {
117 converged_filter_counter_ = 0;
118 }
Per Åhgren1b4059e2017-10-15 20:19:21 +0200119 }
120 }
121
peah522d71b2017-02-23 05:16:26 -0800122 // Compute spectra for future use.
123 E_main.Spectrum(optimization_, &output->E2_main);
124 E_shadow.Spectrum(optimization_, &output->E2_shadow);
125
126 // Update the main filter.
peah86afe9d2017-04-06 15:45:32 -0700127 G_main_.Compute(render_buffer, render_signal_analyzer, *output, main_filter_,
128 aec_state.SaturatedCapture(), &G);
129 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800130 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
131 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
132
133 // Update the shadow filter.
peah86afe9d2017-04-06 15:45:32 -0700134 G_shadow_.Compute(render_buffer, render_signal_analyzer, E_shadow,
135 shadow_filter_.SizePartitions(),
136 aec_state.SaturatedCapture(), &G);
137 shadow_filter_.Adapt(render_buffer, G);
138
peah522d71b2017-02-23 05:16:26 -0800139 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
140 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
141
142 main_filter_.DumpFilter("aec3_subtractor_H_main");
143 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
144}
145
146} // namespace webrtc