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/erle_estimator.h"
12 
13 #include "modules/audio_processing/aec3/aec3_common.h"
14 #include "rtc_base/checks.h"
15 
16 namespace webrtc {
17 
ErleEstimator(size_t startup_phase_length_blocks,const EchoCanceller3Config & config,size_t num_capture_channels)18 ErleEstimator::ErleEstimator(size_t startup_phase_length_blocks,
19                              const EchoCanceller3Config& config,
20                              size_t num_capture_channels)
21     : startup_phase_length_blocks_(startup_phase_length_blocks),
22       fullband_erle_estimator_(config.erle, num_capture_channels),
23       subband_erle_estimator_(config, num_capture_channels) {
24   if (config.erle.num_sections > 1) {
25     signal_dependent_erle_estimator_ =
26         std::make_unique<SignalDependentErleEstimator>(config,
27                                                        num_capture_channels);
28   }
29   Reset(true);
30 }
31 
32 ErleEstimator::~ErleEstimator() = default;
33 
Reset(bool delay_change)34 void ErleEstimator::Reset(bool delay_change) {
35   fullband_erle_estimator_.Reset();
36   subband_erle_estimator_.Reset();
37   if (signal_dependent_erle_estimator_) {
38     signal_dependent_erle_estimator_->Reset();
39   }
40   if (delay_change) {
41     blocks_since_reset_ = 0;
42   }
43 }
44 
Update(const RenderBuffer & render_buffer,rtc::ArrayView<const std::vector<std::array<float,kFftLengthBy2Plus1>>> filter_frequency_responses,rtc::ArrayView<const float,kFftLengthBy2Plus1> avg_render_spectrum_with_reverb,rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> capture_spectra,rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> subtractor_spectra,const std::vector<bool> & converged_filters)45 void ErleEstimator::Update(
46     const RenderBuffer& render_buffer,
47     rtc::ArrayView<const std::vector<std::array<float, kFftLengthBy2Plus1>>>
48         filter_frequency_responses,
49     rtc::ArrayView<const float, kFftLengthBy2Plus1>
50         avg_render_spectrum_with_reverb,
51     rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> capture_spectra,
52     rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
53         subtractor_spectra,
54     const std::vector<bool>& converged_filters) {
55   RTC_DCHECK_EQ(subband_erle_estimator_.Erle().size(), capture_spectra.size());
56   RTC_DCHECK_EQ(subband_erle_estimator_.Erle().size(),
57                 subtractor_spectra.size());
58   const auto& X2_reverb = avg_render_spectrum_with_reverb;
59   const auto& Y2 = capture_spectra;
60   const auto& E2 = subtractor_spectra;
61 
62   if (++blocks_since_reset_ < startup_phase_length_blocks_) {
63     return;
64   }
65 
66   subband_erle_estimator_.Update(X2_reverb, Y2, E2, converged_filters);
67 
68   if (signal_dependent_erle_estimator_) {
69     signal_dependent_erle_estimator_->Update(
70         render_buffer, filter_frequency_responses, X2_reverb, Y2, E2,
71         subband_erle_estimator_.Erle(), converged_filters);
72   }
73 
74   fullband_erle_estimator_.Update(X2_reverb, Y2, E2, converged_filters);
75 }
76 
Dump(const std::unique_ptr<ApmDataDumper> & data_dumper) const77 void ErleEstimator::Dump(
78     const std::unique_ptr<ApmDataDumper>& data_dumper) const {
79   fullband_erle_estimator_.Dump(data_dumper);
80   subband_erle_estimator_.Dump(data_dumper);
81   if (signal_dependent_erle_estimator_) {
82     signal_dependent_erle_estimator_->Dump(data_dumper);
83   }
84 }
85 
86 }  // namespace webrtc
87