xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/subtractor_output_analyzer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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_output_analyzer.h"
12 
13 #include <algorithm>
14 
15 #include "modules/audio_processing/aec3/aec3_common.h"
16 
17 namespace webrtc {
18 
SubtractorOutputAnalyzer(size_t num_capture_channels)19 SubtractorOutputAnalyzer::SubtractorOutputAnalyzer(size_t num_capture_channels)
20     : filters_converged_(num_capture_channels, false) {}
21 
Update(rtc::ArrayView<const SubtractorOutput> subtractor_output,bool * any_filter_converged,bool * any_coarse_filter_converged,bool * all_filters_diverged)22 void SubtractorOutputAnalyzer::Update(
23     rtc::ArrayView<const SubtractorOutput> subtractor_output,
24     bool* any_filter_converged,
25     bool* any_coarse_filter_converged,
26     bool* all_filters_diverged) {
27   RTC_DCHECK(any_filter_converged);
28   RTC_DCHECK(all_filters_diverged);
29   RTC_DCHECK_EQ(subtractor_output.size(), filters_converged_.size());
30 
31   *any_filter_converged = false;
32   *any_coarse_filter_converged = false;
33   *all_filters_diverged = true;
34 
35   for (size_t ch = 0; ch < subtractor_output.size(); ++ch) {
36     const float y2 = subtractor_output[ch].y2;
37     const float e2_refined = subtractor_output[ch].e2_refined;
38     const float e2_coarse = subtractor_output[ch].e2_coarse;
39 
40     constexpr float kConvergenceThreshold = 50 * 50 * kBlockSize;
41     constexpr float kConvergenceThresholdLowLevel = 20 * 20 * kBlockSize;
42     bool refined_filter_converged =
43         e2_refined < 0.5f * y2 && y2 > kConvergenceThreshold;
44     bool coarse_filter_converged_strict =
45         e2_coarse < 0.05f * y2 && y2 > kConvergenceThreshold;
46     bool coarse_filter_converged_relaxed =
47         e2_coarse < 0.2f * y2 && y2 > kConvergenceThresholdLowLevel;
48     float min_e2 = std::min(e2_refined, e2_coarse);
49     bool filter_diverged = min_e2 > 1.5f * y2 && y2 > 30.f * 30.f * kBlockSize;
50     filters_converged_[ch] =
51         refined_filter_converged || coarse_filter_converged_strict;
52 
53     *any_filter_converged = *any_filter_converged || filters_converged_[ch];
54     *any_coarse_filter_converged =
55         *any_coarse_filter_converged || coarse_filter_converged_relaxed;
56     *all_filters_diverged = *all_filters_diverged && filter_diverged;
57   }
58 }
59 
HandleEchoPathChange()60 void SubtractorOutputAnalyzer::HandleEchoPathChange() {
61   std::fill(filters_converged_.begin(), filters_converged_.end(), false);
62 }
63 
64 }  // namespace webrtc
65