1 /*
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 "modules/audio_processing/aec3/subtractor.h"
12 
13 #include <algorithm>
14 #include <numeric>
15 
16 #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/numerics/safe_minmax.h"
20 
21 namespace webrtc {
22 
23 namespace {
24 
PredictionError(const Aec3Fft & fft,const FftData & S,rtc::ArrayView<const float> y,std::array<float,kBlockSize> * e,FftData * E,std::array<float,kBlockSize> * s)25 void PredictionError(const Aec3Fft& fft,
26                      const FftData& S,
27                      rtc::ArrayView<const float> y,
28                      std::array<float, kBlockSize>* e,
29                      FftData* E,
30                      std::array<float, kBlockSize>* s) {
31   std::array<float, kFftLength> s_scratch;
32   fft.Ifft(S, &s_scratch);
33   constexpr float kScale = 1.0f / kFftLengthBy2;
34   std::transform(y.begin(), y.end(), s_scratch.begin() + kFftLengthBy2,
35                  e->begin(), [&](float a, float b) { return a - b * kScale; });
36   std::for_each(e->begin(), e->end(),
37                 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
38   fft.ZeroPaddedFft(*e, E);
39 
40   if (s) {
41     for (size_t k = 0; k < s->size(); ++k) {
42       (*s)[k] = kScale * s_scratch[k + kFftLengthBy2];
43     }
44   }
45 }
46 }  // namespace
47 
Subtractor(ApmDataDumper * data_dumper,Aec3Optimization optimization)48 Subtractor::Subtractor(ApmDataDumper* data_dumper,
49                        Aec3Optimization optimization)
50     : fft_(),
51       data_dumper_(data_dumper),
52       optimization_(optimization),
53       main_filter_(kAdaptiveFilterLength, optimization, data_dumper_),
54       shadow_filter_(kAdaptiveFilterLength, optimization, data_dumper_) {
55   RTC_DCHECK(data_dumper_);
56 }
57 
58 Subtractor::~Subtractor() = default;
59 
HandleEchoPathChange(const EchoPathVariability & echo_path_variability)60 void 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();
66     G_shadow_.HandleEchoPathChange();
67     converged_filter_ = false;
68   }
69 }
70 
Process(const RenderBuffer & render_buffer,const rtc::ArrayView<const float> capture,const RenderSignalAnalyzer & render_signal_analyzer,const AecState & aec_state,SubtractorOutput * output)71 void Subtractor::Process(const RenderBuffer& render_buffer,
72                          const rtc::ArrayView<const float> capture,
73                          const RenderSignalAnalyzer& render_signal_analyzer,
74                          const AecState& aec_state,
75                          SubtractorOutput* output) {
76   RTC_DCHECK_EQ(kBlockSize, capture.size());
77   rtc::ArrayView<const float> y = capture;
78   FftData& E_main = output->E_main;
79   FftData E_shadow;
80   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 
86   // Form the output of the main filter.
87   main_filter_.Filter(render_buffer, &S);
88   PredictionError(fft_, S, y, &e_main, &E_main, &output->s_main);
89 
90   // Form the output of the shadow filter.
91   shadow_filter_.Filter(render_buffer, &S);
92   PredictionError(fft_, S, y, &e_shadow, &E_shadow, nullptr);
93 
94 
95   if (!converged_filter_) {
96     const auto sum_of_squares = [](float a, float b) { return a + b * b; };
97     const float e2_main =
98         std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
99     const float e2_shadow =
100         std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
101     const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
102 
103     if (y2 > kBlockSize * 50.f * 50.f) {
104       converged_filter_ = (e2_main > 0.3 * y2 || e2_shadow > 0.1 * y2);
105     }
106   }
107 
108   // Compute spectra for future use.
109   E_main.Spectrum(optimization_, &output->E2_main);
110   E_shadow.Spectrum(optimization_, &output->E2_shadow);
111 
112   // Update the main filter.
113   G_main_.Compute(render_buffer, render_signal_analyzer, *output, main_filter_,
114                   aec_state.SaturatedCapture(), &G);
115   main_filter_.Adapt(render_buffer, G);
116   data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
117   data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
118 
119   // Update the shadow filter.
120   G_shadow_.Compute(render_buffer, render_signal_analyzer, E_shadow,
121                     shadow_filter_.SizePartitions(),
122                     aec_state.SaturatedCapture(), &G);
123   shadow_filter_.Adapt(render_buffer, G);
124 
125   data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
126   data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
127 
128   main_filter_.DumpFilter("aec3_subtractor_H_main");
129   shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
130 }
131 
132 }  // namespace webrtc
133