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_RESIDUAL_ECHO_ESTIMATOR_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_RESIDUAL_ECHO_ESTIMATOR_H_
13 
14 #include <algorithm>
15 #include <array>
16 #include <vector>
17 
18 #include "api/array_view.h"
19 #include "modules/audio_processing/aec3/aec3_common.h"
20 #include "modules/audio_processing/aec3/aec_state.h"
21 #include "modules/audio_processing/aec3/render_buffer.h"
22 #include "modules/audio_processing/include/audio_processing.h"
23 #include "rtc_base/constructormagic.h"
24 
25 namespace webrtc {
26 
27 class ResidualEchoEstimator {
28  public:
29   explicit ResidualEchoEstimator(const EchoCanceller3Config& config);
30   ~ResidualEchoEstimator();
31 
32   void Estimate(const AecState& aec_state,
33                 const RenderBuffer& render_buffer,
34                 const std::array<float, kFftLengthBy2Plus1>& S2_linear,
35                 const std::array<float, kFftLengthBy2Plus1>& Y2,
36                 std::array<float, kFftLengthBy2Plus1>* R2);
37 
38  private:
39   // Resets the state.
40   void Reset();
41 
42   // Estimates the residual echo power based on the echo return loss enhancement
43   // (ERLE) and the linear power estimate.
44   void LinearEstimate(const std::array<float, kFftLengthBy2Plus1>& S2_linear,
45                       const std::array<float, kFftLengthBy2Plus1>& erle,
46                       size_t delay,
47                       std::array<float, kFftLengthBy2Plus1>* R2);
48 
49   // Estimates the residual echo power based on the estimate of the echo path
50   // gain.
51   void NonLinearEstimate(bool sufficient_filter_updates,
52                          bool saturated_echo,
53                          bool bounded_erl,
54                          bool transparent_mode,
55                          bool initial_state,
56                          const std::array<float, kFftLengthBy2Plus1>& X2,
57                          const std::array<float, kFftLengthBy2Plus1>& Y2,
58                          std::array<float, kFftLengthBy2Plus1>* R2);
59 
60   // Adds the estimated unmodelled echo power to the residual echo power
61   // estimate.
62   void AddEchoReverb(const std::array<float, kFftLengthBy2Plus1>& S2,
63                      bool saturated_echo,
64                      size_t delay,
65                      float reverb_decay_factor,
66                      std::array<float, kFftLengthBy2Plus1>* R2);
67 
68   std::array<float, kFftLengthBy2Plus1> R2_old_;
69   std::array<int, kFftLengthBy2Plus1> R2_hold_counter_;
70   std::array<float, kFftLengthBy2Plus1> R2_reverb_;
71   int S2_old_index_ = 0;
72   std::array<std::array<float, kFftLengthBy2Plus1>, kAdaptiveFilterLength>
73       S2_old_;
74   std::array<float, kFftLengthBy2Plus1> X2_noise_floor_;
75   std::array<int, kFftLengthBy2Plus1> X2_noise_floor_counter_;
76   const EchoCanceller3Config config_;
77 
78   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(ResidualEchoEstimator);
79 };
80 
81 }  // namespace webrtc
82 
83 #endif  // MODULES_AUDIO_PROCESSING_AEC3_RESIDUAL_ECHO_ESTIMATOR_H_
84