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 <memory> 16 #include <vector> 17 18 #include "absl/types/optional.h" 19 #include "api/array_view.h" 20 #include "api/audio/echo_canceller3_config.h" 21 #include "modules/audio_processing/aec3/aec3_common.h" 22 #include "modules/audio_processing/aec3/aec_state.h" 23 #include "modules/audio_processing/aec3/fft_data.h" 24 #include "modules/audio_processing/aec3/moving_average.h" 25 #include "modules/audio_processing/aec3/nearend_detector.h" 26 #include "modules/audio_processing/aec3/render_signal_analyzer.h" 27 #include "modules/audio_processing/logging/apm_data_dumper.h" 28 #include "rtc_base/constructor_magic.h" 29 30 namespace webrtc { 31 32 class SuppressionGain { 33 public: 34 SuppressionGain(const EchoCanceller3Config& config, 35 Aec3Optimization optimization, 36 int sample_rate_hz, 37 size_t num_capture_channels); 38 ~SuppressionGain(); 39 void GetGain( 40 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> 41 nearend_spectrum, 42 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum, 43 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> 44 residual_echo_spectrum, 45 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> 46 comfort_noise_spectrum, 47 const RenderSignalAnalyzer& render_signal_analyzer, 48 const AecState& aec_state, 49 const std::vector<std::vector<std::vector<float>>>& render, 50 float* high_bands_gain, 51 std::array<float, kFftLengthBy2Plus1>* low_band_gain); 52 53 // Toggles the usage of the initial state. 54 void SetInitialState(bool state); 55 56 private: 57 // Computes the gain to apply for the bands beyond the first band. 58 float UpperBandsGain( 59 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum, 60 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> 61 comfort_noise_spectrum, 62 const absl::optional<int>& narrow_peak_band, 63 bool saturated_echo, 64 const std::vector<std::vector<std::vector<float>>>& render, 65 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const; 66 67 void GainToNoAudibleEcho(const std::array<float, kFftLengthBy2Plus1>& nearend, 68 const std::array<float, kFftLengthBy2Plus1>& echo, 69 const std::array<float, kFftLengthBy2Plus1>& masker, 70 std::array<float, kFftLengthBy2Plus1>* gain) const; 71 72 void LowerBandGain( 73 bool stationary_with_low_power, 74 const AecState& aec_state, 75 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> 76 suppressor_input, 77 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> residual_echo, 78 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> comfort_noise, 79 std::array<float, kFftLengthBy2Plus1>* gain); 80 81 void GetMinGain(rtc::ArrayView<const float> weighted_residual_echo, 82 rtc::ArrayView<const float> last_nearend, 83 rtc::ArrayView<const float> last_echo, 84 bool low_noise_render, 85 bool saturated_echo, 86 rtc::ArrayView<float> min_gain) const; 87 88 void GetMaxGain(rtc::ArrayView<float> max_gain) const; 89 90 class LowNoiseRenderDetector { 91 public: 92 bool Detect(const std::vector<std::vector<std::vector<float>>>& render); 93 94 private: 95 float average_power_ = 32768.f * 32768.f; 96 }; 97 98 struct GainParameters { 99 explicit GainParameters( 100 const EchoCanceller3Config::Suppressor::Tuning& tuning); 101 const float max_inc_factor; 102 const float max_dec_factor_lf; 103 std::array<float, kFftLengthBy2Plus1> enr_transparent_; 104 std::array<float, kFftLengthBy2Plus1> enr_suppress_; 105 std::array<float, kFftLengthBy2Plus1> emr_transparent_; 106 }; 107 108 static int instance_count_; 109 std::unique_ptr<ApmDataDumper> data_dumper_; 110 const Aec3Optimization optimization_; 111 const EchoCanceller3Config config_; 112 const size_t num_capture_channels_; 113 const int state_change_duration_blocks_; 114 std::array<float, kFftLengthBy2Plus1> last_gain_; 115 std::vector<std::array<float, kFftLengthBy2Plus1>> last_nearend_; 116 std::vector<std::array<float, kFftLengthBy2Plus1>> last_echo_; 117 LowNoiseRenderDetector low_render_detector_; 118 bool initial_state_ = true; 119 int initial_state_change_counter_ = 0; 120 std::vector<aec3::MovingAverage> nearend_smoothers_; 121 const GainParameters nearend_params_; 122 const GainParameters normal_params_; 123 std::unique_ptr<NearendDetector> dominant_nearend_detector_; 124 125 RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain); 126 }; 127 128 } // namespace webrtc 129 130 #endif // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_ 131