blob: 7c4a1c9f7dada98b74290025d6f45195fe63c8b6 [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"
Gustaf Ullbergf534a642019-11-25 16:13:58 +010025#include "modules/audio_processing/aec3/nearend_detector.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/constructor_magic.h"
peah522d71b2017-02-23 05:16:26 -080029
30namespace webrtc {
peah522d71b2017-02-23 05:16:26 -080031
32class SuppressionGain {
33 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020034 SuppressionGain(const EchoCanceller3Config& config,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020035 Aec3Optimization optimization,
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010036 int sample_rate_hz,
37 size_t num_capture_channels);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020038 ~SuppressionGain();
39 void GetGain(
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010040 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>>
Gustaf Ullberga63d1522021-06-11 14:02:53 +020046 residual_echo_spectrum_unbounded,
47 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010048 comfort_noise_spectrum,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020049 const RenderSignalAnalyzer& render_signal_analyzer,
50 const AecState& aec_state,
Per Åhgrence202a02019-09-02 17:01:19 +020051 const std::vector<std::vector<std::vector<float>>>& render,
Gustaf Ullberg7e4ad822020-10-22 14:36:37 +020052 bool clock_drift,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020053 float* high_bands_gain,
54 std::array<float, kFftLengthBy2Plus1>* low_band_gain);
peah522d71b2017-02-23 05:16:26 -080055
Gustaf Ullberg437d1292021-04-20 13:48:57 +020056 bool IsDominantNearend() {
57 return dominant_nearend_detector_->IsNearendState();
58 }
59
Per Åhgren5f1a31c2018-03-08 15:54:41 +010060 // Toggles the usage of the initial state.
61 void SetInitialState(bool state);
62
peah522d71b2017-02-23 05:16:26 -080063 private:
Per Åhgrenfde4aa92018-08-27 14:19:35 +020064 // Computes the gain to apply for the bands beyond the first band.
65 float UpperBandsGain(
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010066 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum,
67 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
68 comfort_noise_spectrum,
Per Åhgrenfde4aa92018-08-27 14:19:35 +020069 const absl::optional<int>& narrow_peak_band,
70 bool saturated_echo,
Per Åhgrence202a02019-09-02 17:01:19 +020071 const std::vector<std::vector<std::vector<float>>>& render,
Per Åhgrenfde4aa92018-08-27 14:19:35 +020072 const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const;
73
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010074 void GainToNoAudibleEcho(const std::array<float, kFftLengthBy2Plus1>& nearend,
75 const std::array<float, kFftLengthBy2Plus1>& echo,
76 const std::array<float, kFftLengthBy2Plus1>& masker,
77 std::array<float, kFftLengthBy2Plus1>* gain) const;
Gustaf Ullbergec642172018-07-03 13:48:32 +020078
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020079 void LowerBandGain(
80 bool stationary_with_low_power,
81 const AecState& aec_state,
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010082 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
83 suppressor_input,
84 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> residual_echo,
85 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> comfort_noise,
Gustaf Ullberg7e4ad822020-10-22 14:36:37 +020086 bool clock_drift,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020087 std::array<float, kFftLengthBy2Plus1>* gain);
88
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +020089 void GetMinGain(rtc::ArrayView<const float> weighted_residual_echo,
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010090 rtc::ArrayView<const float> last_nearend,
91 rtc::ArrayView<const float> last_echo,
Jesús de Vicente Peña0faf0822018-09-24 12:48:28 +020092 bool low_noise_render,
93 bool saturated_echo,
94 rtc::ArrayView<float> min_gain) const;
95
96 void GetMaxGain(rtc::ArrayView<float> max_gain) const;
peah1d680892017-05-23 04:07:10 -070097
98 class LowNoiseRenderDetector {
99 public:
Per Åhgrence202a02019-09-02 17:01:19 +0200100 bool Detect(const std::vector<std::vector<std::vector<float>>>& render);
peah1d680892017-05-23 04:07:10 -0700101
102 private:
103 float average_power_ = 32768.f * 32768.f;
104 };
105
Per Åhgren524e8782018-08-24 22:48:49 +0200106 struct GainParameters {
107 explicit GainParameters(
Per Åhgrencbdbb8c2021-05-07 23:17:28 +0000108 int last_lf_band,
109 int first_hf_band,
Per Åhgren524e8782018-08-24 22:48:49 +0200110 const EchoCanceller3Config::Suppressor::Tuning& tuning);
111 const float max_inc_factor;
112 const float max_dec_factor_lf;
113 std::array<float, kFftLengthBy2Plus1> enr_transparent_;
114 std::array<float, kFftLengthBy2Plus1> enr_suppress_;
115 std::array<float, kFftLengthBy2Plus1> emr_transparent_;
116 };
117
Gustaf Ullberg216af842018-04-26 12:39:11 +0200118 static int instance_count_;
119 std::unique_ptr<ApmDataDumper> data_dumper_;
peah522d71b2017-02-23 05:16:26 -0800120 const Aec3Optimization optimization_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100121 const EchoCanceller3Config config_;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100122 const size_t num_capture_channels_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100123 const int state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -0700124 std::array<float, kFftLengthBy2Plus1> last_gain_;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100125 std::vector<std::array<float, kFftLengthBy2Plus1>> last_nearend_;
126 std::vector<std::array<float, kFftLengthBy2Plus1>> last_echo_;
peah1d680892017-05-23 04:07:10 -0700127 LowNoiseRenderDetector low_render_detector_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100128 bool initial_state_ = true;
129 int initial_state_change_counter_ = 0;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100130 std::vector<aec3::MovingAverage> nearend_smoothers_;
Per Åhgren524e8782018-08-24 22:48:49 +0200131 const GainParameters nearend_params_;
132 const GainParameters normal_params_;
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200133 // Determines if the dominant nearend detector uses the unbounded residual
134 // echo spectrum.
135 const bool use_unbounded_echo_spectrum_;
Gustaf Ullbergf534a642019-11-25 16:13:58 +0100136 std::unique_ptr<NearendDetector> dominant_nearend_detector_;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200137
peah8cee56f2017-08-24 22:36:53 -0700138 RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain);
peah522d71b2017-02-23 05:16:26 -0800139};
140
141} // namespace webrtc
142
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200143#endif // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_