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_SUPPRESSION_GAIN_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_
13 
14 #include <array>
15 #include <vector>
16 
17 #include "modules/audio_processing/aec3/aec3_common.h"
18 #include "modules/audio_processing/aec3/aec_state.h"
19 #include "modules/audio_processing/aec3/render_signal_analyzer.h"
20 #include "modules/audio_processing/include/audio_processing.h"
21 #include "rtc_base/constructormagic.h"
22 
23 namespace webrtc {
24 
25 class SuppressionGain {
26  public:
27   SuppressionGain(const EchoCanceller3Config& config,
28                   Aec3Optimization optimization);
29   void GetGain(const std::array<float, kFftLengthBy2Plus1>& nearend,
30                const std::array<float, kFftLengthBy2Plus1>& echo,
31                const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
32                const RenderSignalAnalyzer& render_signal_analyzer,
33                const AecState& aec_state,
34                const std::vector<std::vector<float>>& render,
35                float* high_bands_gain,
36                std::array<float, kFftLengthBy2Plus1>* low_band_gain);
37 
38  private:
39   void LowerBandGain(bool stationary_with_low_power,
40                      const rtc::Optional<int>& narrow_peak_band,
41                      bool saturated_echo,
42                      bool saturating_echo_path,
43                      bool linear_echo_estimate,
44                      const std::array<float, kFftLengthBy2Plus1>& nearend,
45                      const std::array<float, kFftLengthBy2Plus1>& echo,
46                      const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
47                      std::array<float, kFftLengthBy2Plus1>* gain);
48 
49   class LowNoiseRenderDetector {
50    public:
51     bool Detect(const std::vector<std::vector<float>>& render);
52 
53    private:
54     float average_power_ = 32768.f * 32768.f;
55   };
56 
57   const Aec3Optimization optimization_;
58   std::array<float, kFftLengthBy2Plus1> last_gain_;
59   std::array<float, kFftLengthBy2Plus1> last_masker_;
60   std::array<float, kFftLengthBy2Plus1> gain_increase_;
61   std::array<float, kFftLengthBy2Plus1> last_echo_;
62 
63   LowNoiseRenderDetector low_render_detector_;
64   size_t no_saturation_counter_ = 0;
65   const EchoCanceller3Config config_;
66   RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain);
67 };
68 
69 }  // namespace webrtc
70 
71 #endif  // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_
72