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(/*onset_compensated=*/true).size(),
56 capture_spectra.size());
57 RTC_DCHECK_EQ(subband_erle_estimator_.Erle(/*onset_compensated=*/true).size(),
58 subtractor_spectra.size());
59 const auto& X2_reverb = avg_render_spectrum_with_reverb;
60 const auto& Y2 = capture_spectra;
61 const auto& E2 = subtractor_spectra;
62
63 if (++blocks_since_reset_ < startup_phase_length_blocks_) {
64 return;
65 }
66
67 subband_erle_estimator_.Update(X2_reverb, Y2, E2, converged_filters);
68
69 if (signal_dependent_erle_estimator_) {
70 signal_dependent_erle_estimator_->Update(
71 render_buffer, filter_frequency_responses, X2_reverb, Y2, E2,
72 subband_erle_estimator_.Erle(/*onset_compensated=*/false),
73 subband_erle_estimator_.Erle(/*onset_compensated=*/true),
74 converged_filters);
75 }
76
77 fullband_erle_estimator_.Update(X2_reverb, Y2, E2, converged_filters);
78 }
79
Dump(const std::unique_ptr<ApmDataDumper> & data_dumper) const80 void ErleEstimator::Dump(
81 const std::unique_ptr<ApmDataDumper>& data_dumper) const {
82 fullband_erle_estimator_.Dump(data_dumper);
83 subband_erle_estimator_.Dump(data_dumper);
84 if (signal_dependent_erle_estimator_) {
85 signal_dependent_erle_estimator_->Dump(data_dumper);
86 }
87 }
88
89 } // namespace webrtc
90