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 #ifndef MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_ 12 #define MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_ 13 14 #include <memory> 15 #include <vector> 16 17 #include "absl/types/optional.h" 18 #include "api/array_view.h" 19 #include "api/audio/echo_canceller3_config.h" 20 #include "modules/audio_processing/aec3/aec3_common.h" 21 #include "modules/audio_processing/logging/apm_data_dumper.h" 22 23 namespace webrtc { 24 25 // Estimates the echo return loss enhancement using the energy of all the 26 // freuquency bands. 27 class FullBandErleEstimator { 28 public: 29 FullBandErleEstimator(const EchoCanceller3Config::Erle& config, 30 size_t num_capture_channels); 31 ~FullBandErleEstimator(); 32 // Resets the ERLE estimator. 33 void Reset(); 34 35 // Updates the ERLE estimator. 36 void Update(rtc::ArrayView<const float> X2, 37 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2, 38 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2, 39 const std::vector<bool>& converged_filters); 40 41 // Returns the fullband ERLE estimates in log2 units. FullbandErleLog2()42 float FullbandErleLog2() const { 43 float min_erle = erle_time_domain_log2_[0]; 44 for (size_t ch = 1; ch < erle_time_domain_log2_.size(); ++ch) { 45 min_erle = std::min(min_erle, erle_time_domain_log2_[ch]); 46 } 47 return min_erle; 48 } 49 50 // Returns an estimation of the current linear filter quality. It returns a 51 // float number between 0 and 1 mapping 1 to the highest possible quality. GetInstLinearQualityEstimates()52 rtc::ArrayView<const absl::optional<float>> GetInstLinearQualityEstimates() 53 const { 54 return linear_filters_qualities_; 55 } 56 57 void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const; 58 59 private: 60 void UpdateQualityEstimates(); 61 62 class ErleInstantaneous { 63 public: 64 explicit ErleInstantaneous(const EchoCanceller3Config::Erle& config); 65 ~ErleInstantaneous(); 66 67 // Updates the estimator with a new point, returns true 68 // if the instantaneous ERLE was updated due to having enough 69 // points for performing the estimate. 70 bool Update(float Y2_sum, float E2_sum); 71 // Resets the instantaneous ERLE estimator to its initial state. 72 void Reset(); 73 // Resets the members related with an instantaneous estimate. 74 void ResetAccumulators(); 75 // Returns the instantaneous ERLE in log2 units. GetInstErleLog2()76 absl::optional<float> GetInstErleLog2() const { return erle_log2_; } 77 // Gets an indication between 0 and 1 of the performance of the linear 78 // filter for the current time instant. GetQualityEstimate()79 absl::optional<float> GetQualityEstimate() const { 80 if (erle_log2_) { 81 float value = inst_quality_estimate_; 82 if (clamp_inst_quality_to_zero_) { 83 value = std::max(0.f, value); 84 } 85 if (clamp_inst_quality_to_one_) { 86 value = std::min(1.f, value); 87 } 88 return absl::optional<float>(value); 89 } 90 return absl::nullopt; 91 } 92 void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const; 93 94 private: 95 void UpdateMaxMin(); 96 void UpdateQualityEstimate(); 97 const bool clamp_inst_quality_to_zero_; 98 const bool clamp_inst_quality_to_one_; 99 absl::optional<float> erle_log2_; 100 float inst_quality_estimate_; 101 float max_erle_log2_; 102 float min_erle_log2_; 103 float Y2_acum_; 104 float E2_acum_; 105 int num_points_; 106 }; 107 108 const float min_erle_log2_; 109 const float max_erle_lf_log2_; 110 std::vector<int> hold_counters_instantaneous_erle_; 111 std::vector<float> erle_time_domain_log2_; 112 std::vector<ErleInstantaneous> instantaneous_erle_; 113 std::vector<absl::optional<float>> linear_filters_qualities_; 114 }; 115 116 } // namespace webrtc 117 118 #endif // MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_ 119