blob: 20ba5108e404ea9a00e350386f247f24f1ab88aa [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
11#include "webrtc/modules/audio_processing/aec3/subtractor.h"
12
13#include <algorithm>
14
peah522d71b2017-02-23 05:16:26 -080015#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020016#include "webrtc/rtc_base/array_view.h"
17#include "webrtc/rtc_base/checks.h"
18#include "webrtc/rtc_base/safe_minmax.h"
peah522d71b2017-02-23 05:16:26 -080019
20namespace webrtc {
21
22namespace {
23
peah86afe9d2017-04-06 15:45:32 -070024void PredictionError(const Aec3Fft& fft,
25 const FftData& S,
26 rtc::ArrayView<const float> y,
27 std::array<float, kBlockSize>* e,
peah29103572017-07-11 02:54:02 -070028 FftData* E,
29 std::array<float, kBlockSize>* s) {
30 std::array<float, kFftLength> s_scratch;
31 fft.Ifft(S, &s_scratch);
peah522d71b2017-02-23 05:16:26 -080032 constexpr float kScale = 1.0f / kFftLengthBy2;
peah29103572017-07-11 02:54:02 -070033 std::transform(y.begin(), y.end(), s_scratch.begin() + kFftLengthBy2,
34 e->begin(), [&](float a, float b) { return a - b * kScale; });
kwiberg07038562017-06-12 11:40:47 -070035 std::for_each(e->begin(), e->end(),
36 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
peah522d71b2017-02-23 05:16:26 -080037 fft.ZeroPaddedFft(*e, E);
peah29103572017-07-11 02:54:02 -070038
39 if (s) {
40 for (size_t k = 0; k < s->size(); ++k) {
41 (*s)[k] = kScale * s_scratch[k + kFftLengthBy2];
42 }
43 }
peah522d71b2017-02-23 05:16:26 -080044}
45} // namespace
46
peah522d71b2017-02-23 05:16:26 -080047Subtractor::Subtractor(ApmDataDumper* data_dumper,
48 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080049 : fft_(),
50 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080051 optimization_(optimization),
peah86afe9d2017-04-06 15:45:32 -070052 main_filter_(kAdaptiveFilterLength, optimization, data_dumper_),
53 shadow_filter_(kAdaptiveFilterLength, optimization, data_dumper_) {
peah522d71b2017-02-23 05:16:26 -080054 RTC_DCHECK(data_dumper_);
55}
56
peah29103572017-07-11 02:54:02 -070057Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -080058
59void Subtractor::HandleEchoPathChange(
60 const EchoPathVariability& echo_path_variability) {
61 if (echo_path_variability.delay_change) {
62 main_filter_.HandleEchoPathChange();
63 shadow_filter_.HandleEchoPathChange();
64 G_main_.HandleEchoPathChange();
peahdebaa442017-05-03 05:39:09 -070065 G_shadow_.HandleEchoPathChange();
peah522d71b2017-02-23 05:16:26 -080066 }
67}
68
peahcf02cf12017-04-05 14:18:07 -070069void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080070 const rtc::ArrayView<const float> capture,
71 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070072 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -080073 SubtractorOutput* output) {
74 RTC_DCHECK_EQ(kBlockSize, capture.size());
75 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -080076 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -070077 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -080078 std::array<float, kBlockSize>& e_main = output->e_main;
79 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
80
81 FftData S;
82 FftData& G = S;
83
peah86afe9d2017-04-06 15:45:32 -070084 // Form the output of the main filter.
85 main_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -070086 PredictionError(fft_, S, y, &e_main, &E_main, &output->s_main);
peah522d71b2017-02-23 05:16:26 -080087
peah86afe9d2017-04-06 15:45:32 -070088 // Form the output of the shadow filter.
89 shadow_filter_.Filter(render_buffer, &S);
peah29103572017-07-11 02:54:02 -070090 PredictionError(fft_, S, y, &e_shadow, &E_shadow, nullptr);
peah522d71b2017-02-23 05:16:26 -080091
92 // Compute spectra for future use.
93 E_main.Spectrum(optimization_, &output->E2_main);
94 E_shadow.Spectrum(optimization_, &output->E2_shadow);
95
96 // Update the main filter.
peah86afe9d2017-04-06 15:45:32 -070097 G_main_.Compute(render_buffer, render_signal_analyzer, *output, main_filter_,
98 aec_state.SaturatedCapture(), &G);
99 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800100 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
101 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
102
103 // Update the shadow filter.
peah86afe9d2017-04-06 15:45:32 -0700104 G_shadow_.Compute(render_buffer, render_signal_analyzer, E_shadow,
105 shadow_filter_.SizePartitions(),
106 aec_state.SaturatedCapture(), &G);
107 shadow_filter_.Adapt(render_buffer, G);
108
peah522d71b2017-02-23 05:16:26 -0800109 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
110 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
111
112 main_filter_.DumpFilter("aec3_subtractor_H_main");
113 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
114}
115
116} // namespace webrtc