xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/subband_erle_estimator.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_SUBBAND_ERLE_ESTIMATOR_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_SUBBAND_ERLE_ESTIMATOR_H_
13 
14 #include <stddef.h>
15 
16 #include <array>
17 #include <memory>
18 #include <vector>
19 
20 #include "api/array_view.h"
21 #include "api/audio/echo_canceller3_config.h"
22 #include "modules/audio_processing/aec3/aec3_common.h"
23 #include "modules/audio_processing/logging/apm_data_dumper.h"
24 
25 namespace webrtc {
26 
27 // Estimates the echo return loss enhancement for each frequency subband.
28 class SubbandErleEstimator {
29  public:
30   SubbandErleEstimator(const EchoCanceller3Config& config,
31                        size_t num_capture_channels);
32   ~SubbandErleEstimator();
33 
34   // Resets the ERLE estimator.
35   void Reset();
36 
37   // Updates the ERLE estimate.
38   void Update(rtc::ArrayView<const float, kFftLengthBy2Plus1> X2,
39               rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2,
40               rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2,
41               const std::vector<bool>& converged_filters);
42 
43   // Returns the ERLE estimate.
Erle(bool onset_compensated)44   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle(
45       bool onset_compensated) const {
46     return onset_compensated && use_onset_detection_ ? erle_onset_compensated_
47                                                      : erle_;
48   }
49 
50   // Returns the non-capped ERLE estimate.
ErleUnbounded()51   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleUnbounded()
52       const {
53     return erle_unbounded_;
54   }
55 
56   // Returns the ERLE estimate at onsets (only used for testing).
ErleDuringOnsets()57   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleDuringOnsets()
58       const {
59     return erle_during_onsets_;
60   }
61 
62   void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
63 
64  private:
65   struct AccumulatedSpectra {
AccumulatedSpectraAccumulatedSpectra66     explicit AccumulatedSpectra(size_t num_capture_channels)
67         : Y2(num_capture_channels),
68           E2(num_capture_channels),
69           low_render_energy(num_capture_channels),
70           num_points(num_capture_channels) {}
71     std::vector<std::array<float, kFftLengthBy2Plus1>> Y2;
72     std::vector<std::array<float, kFftLengthBy2Plus1>> E2;
73     std::vector<std::array<bool, kFftLengthBy2Plus1>> low_render_energy;
74     std::vector<int> num_points;
75   };
76 
77   void UpdateAccumulatedSpectra(
78       rtc::ArrayView<const float, kFftLengthBy2Plus1> X2,
79       rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2,
80       rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2,
81       const std::vector<bool>& converged_filters);
82 
83   void ResetAccumulatedSpectra();
84 
85   void UpdateBands(const std::vector<bool>& converged_filters);
86   void DecreaseErlePerBandForLowRenderSignals();
87 
88   const bool use_onset_detection_;
89   const float min_erle_;
90   const std::array<float, kFftLengthBy2Plus1> max_erle_;
91   const bool use_min_erle_during_onsets_;
92   AccumulatedSpectra accum_spectra_;
93   // ERLE without special handling of render onsets.
94   std::vector<std::array<float, kFftLengthBy2Plus1>> erle_;
95   // ERLE lowered during render onsets.
96   std::vector<std::array<float, kFftLengthBy2Plus1>> erle_onset_compensated_;
97   std::vector<std::array<float, kFftLengthBy2Plus1>> erle_unbounded_;
98   // Estimation of ERLE during render onsets.
99   std::vector<std::array<float, kFftLengthBy2Plus1>> erle_during_onsets_;
100   std::vector<std::array<bool, kFftLengthBy2Plus1>> coming_onset_;
101   std::vector<std::array<int, kFftLengthBy2Plus1>> hold_counters_;
102 };
103 
104 }  // namespace webrtc
105 
106 #endif  // MODULES_AUDIO_PROCESSING_AEC3_SUBBAND_ERLE_ESTIMATOR_H_
107