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 #ifndef MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_ 12 #define MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_ 13 14 #include <stddef.h> 15 16 #include <array> 17 #include <memory> 18 #include <vector> 19 20 #include "absl/types/optional.h" 21 #include "api/array_view.h" 22 #include "api/audio/echo_canceller3_config.h" 23 #include "modules/audio_processing/aec3/aec3_common.h" 24 #include "modules/audio_processing/aec3/fullband_erle_estimator.h" 25 #include "modules/audio_processing/aec3/render_buffer.h" 26 #include "modules/audio_processing/aec3/signal_dependent_erle_estimator.h" 27 #include "modules/audio_processing/aec3/subband_erle_estimator.h" 28 #include "modules/audio_processing/logging/apm_data_dumper.h" 29 30 namespace webrtc { 31 32 // Estimates the echo return loss enhancement. One estimate is done per subband 33 // and another one is done using the aggreation of energy over all the subbands. 34 class ErleEstimator { 35 public: 36 ErleEstimator(size_t startup_phase_length_blocks, 37 const EchoCanceller3Config& config, 38 size_t num_capture_channels); 39 ~ErleEstimator(); 40 41 // Resets the fullband ERLE estimator and the subbands ERLE estimators. 42 void Reset(bool delay_change); 43 44 // Updates the ERLE estimates. 45 void 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>> 52 capture_spectra, 53 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> 54 subtractor_spectra, 55 const std::vector<bool>& converged_filters); 56 57 // Returns the most recent subband ERLE estimates. Erle(bool onset_compensated)58 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle( 59 bool onset_compensated) const { 60 return signal_dependent_erle_estimator_ 61 ? signal_dependent_erle_estimator_->Erle(onset_compensated) 62 : subband_erle_estimator_.Erle(onset_compensated); 63 } 64 65 // Returns the non-capped subband ERLE. ErleUnbounded()66 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleUnbounded() 67 const { 68 // Unbounded ERLE is only used with the subband erle estimator where the 69 // ERLE is often capped at low values. When the signal dependent ERLE 70 // estimator is used the capped ERLE is returned. 71 return !signal_dependent_erle_estimator_ 72 ? subband_erle_estimator_.ErleUnbounded() 73 : signal_dependent_erle_estimator_->Erle( 74 /*onset_compensated=*/false); 75 } 76 77 // Returns the subband ERLE that are estimated during onsets (only used for 78 // testing). ErleDuringOnsets()79 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleDuringOnsets() 80 const { 81 return subband_erle_estimator_.ErleDuringOnsets(); 82 } 83 84 // Returns the fullband ERLE estimate. FullbandErleLog2()85 float FullbandErleLog2() const { 86 return fullband_erle_estimator_.FullbandErleLog2(); 87 } 88 89 // Returns an estimation of the current linear filter quality based on the 90 // current and past fullband ERLE estimates. The returned value is a float 91 // vector with content between 0 and 1 where 1 indicates that, at this current 92 // time instant, the linear filter is reaching its maximum subtraction 93 // performance. GetInstLinearQualityEstimates()94 rtc::ArrayView<const absl::optional<float>> GetInstLinearQualityEstimates() 95 const { 96 return fullband_erle_estimator_.GetInstLinearQualityEstimates(); 97 } 98 99 void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const; 100 101 private: 102 const size_t startup_phase_length_blocks_; 103 FullBandErleEstimator fullband_erle_estimator_; 104 SubbandErleEstimator subband_erle_estimator_; 105 std::unique_ptr<SignalDependentErleEstimator> 106 signal_dependent_erle_estimator_; 107 size_t blocks_since_reset_ = 0; 108 }; 109 110 } // namespace webrtc 111 112 #endif // MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_ 113