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()44   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle() const {
45     return erle_;
46   }
47 
48   // Returns the ERLE estimate at onsets (only used for testing).
ErleOnsets()49   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleOnsets()
50       const {
51     return erle_onsets_;
52   }
53 
54   void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
55 
56  private:
57   struct AccumulatedSpectra {
AccumulatedSpectraAccumulatedSpectra58     explicit AccumulatedSpectra(size_t num_capture_channels)
59         : Y2(num_capture_channels),
60           E2(num_capture_channels),
61           low_render_energy(num_capture_channels),
62           num_points(num_capture_channels) {}
63     std::vector<std::array<float, kFftLengthBy2Plus1>> Y2;
64     std::vector<std::array<float, kFftLengthBy2Plus1>> E2;
65     std::vector<std::array<bool, kFftLengthBy2Plus1>> low_render_energy;
66     std::vector<int> num_points;
67   };
68 
69   void UpdateAccumulatedSpectra(
70       rtc::ArrayView<const float, kFftLengthBy2Plus1> X2,
71       rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2,
72       rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2,
73       const std::vector<bool>& converged_filters);
74 
75   void ResetAccumulatedSpectra();
76 
77   void UpdateBands(const std::vector<bool>& converged_filters);
78   void DecreaseErlePerBandForLowRenderSignals();
79 
80   const bool use_onset_detection_;
81   const float min_erle_;
82   const std::array<float, kFftLengthBy2Plus1> max_erle_;
83   const bool use_min_erle_during_onsets_;
84   AccumulatedSpectra accum_spectra_;
85   std::vector<std::array<float, kFftLengthBy2Plus1>> erle_;
86   std::vector<std::array<float, kFftLengthBy2Plus1>> erle_onsets_;
87   std::vector<std::array<bool, kFftLengthBy2Plus1>> coming_onset_;
88   std::vector<std::array<int, kFftLengthBy2Plus1>> hold_counters_;
89 };
90 
91 }  // namespace webrtc
92 
93 #endif  // MODULES_AUDIO_PROCESSING_AEC3_SUBBAND_ERLE_ESTIMATOR_H_
94