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()58   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle() const {
59     return signal_dependent_erle_estimator_
60                ? signal_dependent_erle_estimator_->Erle()
61                : subband_erle_estimator_.Erle();
62   }
63 
64   // Returns the subband ERLE that are estimated during onsets (only used for
65   // testing).
ErleOnsets()66   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleOnsets()
67       const {
68     return subband_erle_estimator_.ErleOnsets();
69   }
70 
71   // Returns the fullband ERLE estimate.
FullbandErleLog2()72   float FullbandErleLog2() const {
73     return fullband_erle_estimator_.FullbandErleLog2();
74   }
75 
76   // Returns an estimation of the current linear filter quality based on the
77   // current and past fullband ERLE estimates. The returned value is a float
78   // vector with content between 0 and 1 where 1 indicates that, at this current
79   // time instant, the linear filter is reaching its maximum subtraction
80   // performance.
GetInstLinearQualityEstimates()81   rtc::ArrayView<const absl::optional<float>> GetInstLinearQualityEstimates()
82       const {
83     return fullband_erle_estimator_.GetInstLinearQualityEstimates();
84   }
85 
86   void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
87 
88  private:
89   const size_t startup_phase_length_blocks_;
90   FullBandErleEstimator fullband_erle_estimator_;
91   SubbandErleEstimator subband_erle_estimator_;
92   std::unique_ptr<SignalDependentErleEstimator>
93       signal_dependent_erle_estimator_;
94   size_t blocks_since_reset_ = 0;
95 };
96 
97 }  // namespace webrtc
98 
99 #endif  // MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_
100