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