blob: b8519302bda51a2c9b756e0fba4cd3fd748b617d [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_
peah522d71b2017-02-23 05:16:26 -080013
14#include <array>
peah86afe9d2017-04-06 15:45:32 -070015#include <vector>
peah522d71b2017-02-23 05:16:26 -080016
Gustaf Ullberg3646f972018-02-14 15:19:04 +010017#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren7ddd4632017-10-25 02:59:45 +020019#include "modules/audio_processing/aec3/aec_state.h"
Gustaf Ullberg8406c432018-06-19 12:31:33 +020020#include "modules/audio_processing/aec3/moving_average.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080023
24namespace webrtc {
peah522d71b2017-02-23 05:16:26 -080025
26class SuppressionGain {
27 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020028 SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020029 Aec3Optimization optimization,
30 int sample_rate_hz);
31 ~SuppressionGain();
32 void GetGain(
33 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
34 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +020035 const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020036 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
37 const FftData& linear_aec_fft,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020038 const FftData& capture_fft,
39 const RenderSignalAnalyzer& render_signal_analyzer,
40 const AecState& aec_state,
41 const std::vector<std::vector<float>>& render,
42 float* high_bands_gain,
43 std::array<float, kFftLengthBy2Plus1>* low_band_gain);
peah522d71b2017-02-23 05:16:26 -080044
Per Åhgren5f1a31c2018-03-08 15:54:41 +010045 // Toggles the usage of the initial state.
46 void SetInitialState(bool state);
47
peah522d71b2017-02-23 05:16:26 -080048 private:
Per Åhgrenfde4aa92018-08-27 14:19:35 +020049 // Computes the gain to apply for the bands beyond the first band.
50 float UpperBandsGain(
51 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
52 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
53 const absl::optional<int>& narrow_peak_band,
54 bool saturated_echo,
55 const std::vector<std::vector<float>>& render,
56 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const;
57
Gustaf Ullbergec642172018-07-03 13:48:32 +020058 void GainToNoAudibleEcho(
59 const std::array<float, kFftLengthBy2Plus1>& nearend,
60 const std::array<float, kFftLengthBy2Plus1>& echo,
61 const std::array<float, kFftLengthBy2Plus1>& masker,
62 const std::array<float, kFftLengthBy2Plus1>& min_gain,
63 const std::array<float, kFftLengthBy2Plus1>& max_gain,
64 std::array<float, kFftLengthBy2Plus1>* gain) const;
65
peah1d680892017-05-23 04:07:10 -070066 void LowerBandGain(bool stationary_with_low_power,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010067 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -070068 const std::array<float, kFftLengthBy2Plus1>& nearend,
69 const std::array<float, kFftLengthBy2Plus1>& echo,
70 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
71 std::array<float, kFftLengthBy2Plus1>* gain);
72
73 class LowNoiseRenderDetector {
74 public:
75 bool Detect(const std::vector<std::vector<float>>& render);
76
77 private:
78 float average_power_ = 32768.f * 32768.f;
79 };
80
Per Åhgren524e8782018-08-24 22:48:49 +020081 // Class for selecting whether the suppressor is in the nearend or echo state.
82 class DominantNearendDetector {
83 public:
84 explicit DominantNearendDetector(
85 const EchoCanceller3Config::Suppressor::DominantNearendDetection
86 config);
87
88 // Returns whether the current state is the nearend state.
89 bool IsNearendState() const { return nearend_state_; }
90
91 // Updates the state selection based on latest spectral estimates.
92 void Update(rtc::ArrayView<const float> nearend_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +020093 rtc::ArrayView<const float> residual_echo_spectrum,
Per Åhgren524e8782018-08-24 22:48:49 +020094 rtc::ArrayView<const float> comfort_noise_spectrum);
95
96 private:
97 const float enr_threshold_;
98 const float snr_threshold_;
99 const int hold_duration_;
100 const int trigger_threshold_;
101
102 bool nearend_state_ = false;
103 int trigger_counter_ = 0;
104 int hold_counter_ = 0;
105 };
106
107 struct GainParameters {
108 explicit GainParameters(
109 const EchoCanceller3Config::Suppressor::Tuning& tuning);
110 const float max_inc_factor;
111 const float max_dec_factor_lf;
112 std::array<float, kFftLengthBy2Plus1> enr_transparent_;
113 std::array<float, kFftLengthBy2Plus1> enr_suppress_;
114 std::array<float, kFftLengthBy2Plus1> emr_transparent_;
115 };
116
Gustaf Ullberg216af842018-04-26 12:39:11 +0200117 static int instance_count_;
118 std::unique_ptr<ApmDataDumper> data_dumper_;
peah522d71b2017-02-23 05:16:26 -0800119 const Aec3Optimization optimization_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100120 const EchoCanceller3Config config_;
121 const int state_change_duration_blocks_;
122 float one_by_state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700123 std::array<float, kFftLengthBy2Plus1> last_gain_;
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200124 std::array<float, kFftLengthBy2Plus1> last_nearend_;
peah1d680892017-05-23 04:07:10 -0700125 std::array<float, kFftLengthBy2Plus1> last_echo_;
peah1d680892017-05-23 04:07:10 -0700126 LowNoiseRenderDetector low_render_detector_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100127 bool initial_state_ = true;
128 int initial_state_change_counter_ = 0;
Gustaf Ullbergec642172018-07-03 13:48:32 +0200129 const bool enable_new_suppression_;
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200130 aec3::MovingAverage moving_average_;
Per Åhgren524e8782018-08-24 22:48:49 +0200131 const GainParameters nearend_params_;
132 const GainParameters normal_params_;
133 DominantNearendDetector dominant_nearend_detector_;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200134
peah8cee56f2017-08-24 22:36:53 -0700135 RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain);
peah522d71b2017-02-23 05:16:26 -0800136};
137
138} // namespace webrtc
139
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200140#endif // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_