blob: 2b34dbe46bfaf30f58de86aa0ce4cea5f1fbfd22 [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>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <memory>
peah86afe9d2017-04-06 15:45:32 -070016#include <vector>
peah522d71b2017-02-23 05:16:26 -080017
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "absl/types/optional.h"
19#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010020#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren7ddd4632017-10-25 02:59:45 +020022#include "modules/audio_processing/aec3/aec_state.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "modules/audio_processing/aec3/fft_data.h"
Gustaf Ullberg8406c432018-06-19 12:31:33 +020024#include "modules/audio_processing/aec3/moving_average.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020026#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/constructor_magic.h"
peah522d71b2017-02-23 05:16:26 -080028
29namespace webrtc {
peah522d71b2017-02-23 05:16:26 -080030
31class SuppressionGain {
32 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020033 SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020034 Aec3Optimization optimization,
35 int sample_rate_hz);
36 ~SuppressionGain();
37 void GetGain(
38 const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
39 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +020040 const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020041 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020042 const RenderSignalAnalyzer& render_signal_analyzer,
43 const AecState& aec_state,
44 const std::vector<std::vector<float>>& render,
45 float* high_bands_gain,
46 std::array<float, kFftLengthBy2Plus1>* low_band_gain);
peah522d71b2017-02-23 05:16:26 -080047
Per Åhgren5f1a31c2018-03-08 15:54:41 +010048 // Toggles the usage of the initial state.
49 void SetInitialState(bool state);
50
peah522d71b2017-02-23 05:16:26 -080051 private:
Per Åhgrenfde4aa92018-08-27 14:19:35 +020052 // Computes the gain to apply for the bands beyond the first band.
53 float UpperBandsGain(
54 const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
55 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
56 const absl::optional<int>& narrow_peak_band,
57 bool saturated_echo,
58 const std::vector<std::vector<float>>& render,
59 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const;
60
Gustaf Ullbergec642172018-07-03 13:48:32 +020061 void GainToNoAudibleEcho(
62 const std::array<float, kFftLengthBy2Plus1>& nearend,
63 const std::array<float, kFftLengthBy2Plus1>& echo,
64 const std::array<float, kFftLengthBy2Plus1>& masker,
65 const std::array<float, kFftLengthBy2Plus1>& min_gain,
66 const std::array<float, kFftLengthBy2Plus1>& max_gain,
67 std::array<float, kFftLengthBy2Plus1>* gain) const;
68
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020069 void LowerBandGain(
70 bool stationary_with_low_power,
71 const AecState& aec_state,
72 const std::array<float, kFftLengthBy2Plus1>& suppressor_input,
73 const std::array<float, kFftLengthBy2Plus1>& nearend,
74 const std::array<float, kFftLengthBy2Plus1>& residual_echo,
75 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
76 std::array<float, kFftLengthBy2Plus1>* gain);
77
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +020078 void GetMinGain(rtc::ArrayView<const float> weighted_residual_echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020079 bool low_noise_render,
80 bool saturated_echo,
81 rtc::ArrayView<float> min_gain) const;
82
83 void GetMaxGain(rtc::ArrayView<float> max_gain) const;
peah1d680892017-05-23 04:07:10 -070084
85 class LowNoiseRenderDetector {
86 public:
87 bool Detect(const std::vector<std::vector<float>>& render);
88
89 private:
90 float average_power_ = 32768.f * 32768.f;
91 };
92
Per Åhgren524e8782018-08-24 22:48:49 +020093 // Class for selecting whether the suppressor is in the nearend or echo state.
94 class DominantNearendDetector {
95 public:
96 explicit DominantNearendDetector(
97 const EchoCanceller3Config::Suppressor::DominantNearendDetection
98 config);
99
100 // Returns whether the current state is the nearend state.
101 bool IsNearendState() const { return nearend_state_; }
102
103 // Updates the state selection based on latest spectral estimates.
104 void Update(rtc::ArrayView<const float> nearend_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200105 rtc::ArrayView<const float> residual_echo_spectrum,
Per Åhgren700b4a42018-10-23 21:21:37 +0200106 rtc::ArrayView<const float> comfort_noise_spectrum,
107 bool initial_state);
Per Åhgren524e8782018-08-24 22:48:49 +0200108
109 private:
110 const float enr_threshold_;
Gustaf Ullbergc9f9b872018-10-22 15:15:36 +0200111 const float enr_exit_threshold_;
Per Åhgren524e8782018-08-24 22:48:49 +0200112 const float snr_threshold_;
113 const int hold_duration_;
114 const int trigger_threshold_;
Per Åhgren700b4a42018-10-23 21:21:37 +0200115 const bool use_during_initial_phase_;
Per Åhgren524e8782018-08-24 22:48:49 +0200116
117 bool nearend_state_ = false;
118 int trigger_counter_ = 0;
119 int hold_counter_ = 0;
120 };
121
122 struct GainParameters {
123 explicit GainParameters(
124 const EchoCanceller3Config::Suppressor::Tuning& tuning);
125 const float max_inc_factor;
126 const float max_dec_factor_lf;
127 std::array<float, kFftLengthBy2Plus1> enr_transparent_;
128 std::array<float, kFftLengthBy2Plus1> enr_suppress_;
129 std::array<float, kFftLengthBy2Plus1> emr_transparent_;
130 };
131
Gustaf Ullberg216af842018-04-26 12:39:11 +0200132 static int instance_count_;
133 std::unique_ptr<ApmDataDumper> data_dumper_;
peah522d71b2017-02-23 05:16:26 -0800134 const Aec3Optimization optimization_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100135 const EchoCanceller3Config config_;
136 const int state_change_duration_blocks_;
137 float one_by_state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700138 std::array<float, kFftLengthBy2Plus1> last_gain_;
Gustaf Ullberg0e6375e2018-05-04 11:29:02 +0200139 std::array<float, kFftLengthBy2Plus1> last_nearend_;
peah1d680892017-05-23 04:07:10 -0700140 std::array<float, kFftLengthBy2Plus1> last_echo_;
peah1d680892017-05-23 04:07:10 -0700141 LowNoiseRenderDetector low_render_detector_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100142 bool initial_state_ = true;
143 int initial_state_change_counter_ = 0;
Gustaf Ullberg8406c432018-06-19 12:31:33 +0200144 aec3::MovingAverage moving_average_;
Per Åhgren524e8782018-08-24 22:48:49 +0200145 const GainParameters nearend_params_;
146 const GainParameters normal_params_;
147 DominantNearendDetector dominant_nearend_detector_;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200148
peah8cee56f2017-08-24 22:36:53 -0700149 RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain);
peah522d71b2017-02-23 05:16:26 -0800150};
151
152} // namespace webrtc
153
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200154#endif // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_