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