reverb_decay_estimator.h (3738B)
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_REVERB_DECAY_ESTIMATOR_H_ 12 #define MODULES_AUDIO_PROCESSING_AEC3_REVERB_DECAY_ESTIMATOR_H_ 13 14 #include <optional> 15 #include <vector> 16 17 #include "api/array_view.h" 18 19 namespace webrtc { 20 21 class ApmDataDumper; 22 struct EchoCanceller3Config; 23 24 // Class for estimating the decay of the late reverb. 25 class ReverbDecayEstimator { 26 public: 27 explicit ReverbDecayEstimator(const EchoCanceller3Config& config); 28 ~ReverbDecayEstimator(); 29 // Updates the decay estimate. 30 void Update(ArrayView<const float> filter, 31 const std::optional<float>& filter_quality, 32 int filter_delay_blocks, 33 bool usable_linear_filter, 34 bool stationary_signal); 35 // Returns the decay for the exponential model. The parameter `mild` indicates 36 // which exponential decay to return, the default one or a milder one. 37 float Decay(bool mild) const { 38 if (use_adaptive_echo_decay_) { 39 return decay_; 40 } else { 41 return mild ? mild_decay_ : decay_; 42 } 43 } 44 // Dumps debug data. 45 void Dump(ApmDataDumper* data_dumper) const; 46 47 private: 48 void EstimateDecay(ArrayView<const float> filter, int peak_block); 49 void AnalyzeFilter(ArrayView<const float> filter); 50 51 void ResetDecayEstimation(); 52 53 // Class for estimating the decay of the late reverb from the linear filter. 54 class LateReverbLinearRegressor { 55 public: 56 // Resets the estimator to receive a specified number of data points. 57 void Reset(int num_data_points); 58 // Accumulates estimation data. 59 void Accumulate(float z); 60 // Estimates the decay. 61 float Estimate(); 62 // Returns whether an estimate is available. 63 bool EstimateAvailable() const { return n_ == N_ && N_ != 0; } 64 65 public: 66 float nz_ = 0.f; 67 float nn_ = 0.f; 68 float count_ = 0.f; 69 int N_ = 0; 70 int n_ = 0; 71 }; 72 73 // Class for identifying the length of the early reverb from the linear 74 // filter. For identifying the early reverberations, the impulse response is 75 // divided in sections and the tilt of each section is computed by a linear 76 // regressor. 77 class EarlyReverbLengthEstimator { 78 public: 79 explicit EarlyReverbLengthEstimator(int max_blocks); 80 ~EarlyReverbLengthEstimator(); 81 82 // Resets the estimator. 83 void Reset(); 84 // Accumulates estimation data. 85 void Accumulate(float value, float smoothing); 86 // Estimates the size in blocks of the early reverb. 87 int Estimate(); 88 // Dumps debug data. 89 void Dump(ApmDataDumper* data_dumper) const; 90 91 private: 92 std::vector<float> numerators_smooth_; 93 std::vector<float> numerators_; 94 int coefficients_counter_; 95 int block_counter_ = 0; 96 int n_sections_ = 0; 97 }; 98 99 const int filter_length_blocks_; 100 const int filter_length_coefficients_; 101 const bool use_adaptive_echo_decay_; 102 LateReverbLinearRegressor late_reverb_decay_estimator_; 103 EarlyReverbLengthEstimator early_reverb_estimator_; 104 int late_reverb_start_; 105 int late_reverb_end_; 106 int block_to_analyze_ = 0; 107 int estimation_region_candidate_size_ = 0; 108 bool estimation_region_identified_ = false; 109 std::vector<float> previous_gains_; 110 float decay_; 111 float mild_decay_; 112 float tail_gain_ = 0.f; 113 float smoothing_constant_ = 0.f; 114 }; 115 116 } // namespace webrtc 117 118 #endif // MODULES_AUDIO_PROCESSING_AEC3_REVERB_DECAY_ESTIMATOR_H_