blob: 59eeb358576456efc751b0754c653f0add0d2aac [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"
Per Åhgren47d7fbd2018-04-24 12:44:29 +020020#include "modules/audio_processing/aec3/coherence_gain.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,
35 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
36 const FftData& linear_aec_fft,
37 const FftData& render_fft,
38 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:
peah1d680892017-05-23 04:07:10 -070049 void LowerBandGain(bool stationary_with_low_power,
peah14c11a42017-07-11 06:13:43 -070050 const rtc::Optional<int>& narrow_peak_band,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010051 const AecState& aec_state,
peah1d680892017-05-23 04:07:10 -070052 const std::array<float, kFftLengthBy2Plus1>& nearend,
53 const std::array<float, kFftLengthBy2Plus1>& echo,
54 const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
55 std::array<float, kFftLengthBy2Plus1>* gain);
56
Per Åhgren5f1a31c2018-03-08 15:54:41 +010057 // Limits the gain increase.
58 void UpdateGainIncrease(
59 bool low_noise_render,
60 bool linear_echo_estimate,
Per Åhgren31122d62018-04-10 16:33:55 +020061 bool saturated_echo,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010062 const std::array<float, kFftLengthBy2Plus1>& echo,
63 const std::array<float, kFftLengthBy2Plus1>& new_gain);
64
peah1d680892017-05-23 04:07:10 -070065 class LowNoiseRenderDetector {
66 public:
67 bool Detect(const std::vector<std::vector<float>>& render);
68
69 private:
70 float average_power_ = 32768.f * 32768.f;
71 };
72
peah522d71b2017-02-23 05:16:26 -080073 const Aec3Optimization optimization_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +010074 const EchoCanceller3Config config_;
75 const int state_change_duration_blocks_;
76 float one_by_state_change_duration_blocks_;
peah1d680892017-05-23 04:07:10 -070077 std::array<float, kFftLengthBy2Plus1> last_gain_;
78 std::array<float, kFftLengthBy2Plus1> last_masker_;
79 std::array<float, kFftLengthBy2Plus1> gain_increase_;
80 std::array<float, kFftLengthBy2Plus1> last_echo_;
81
82 LowNoiseRenderDetector low_render_detector_;
Per Åhgren5f1a31c2018-03-08 15:54:41 +010083 bool initial_state_ = true;
84 int initial_state_change_counter_ = 0;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020085 CoherenceGain coherence_gain_;
86
peah8cee56f2017-08-24 22:36:53 -070087 RTC_DISALLOW_COPY_AND_ASSIGN(SuppressionGain);
peah522d71b2017-02-23 05:16:26 -080088};
89
90} // namespace webrtc
91
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020092#endif // MODULES_AUDIO_PROCESSING_AEC3_SUPPRESSION_GAIN_H_