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