blob: 39314ff645dda2818cf4d528c2a23cdbe1c6b305 [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_AEC_STATE_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
peah522d71b2017-02-23 05:16:26 -080013
Christian Schuldtf4e99db2018-03-01 11:32:50 +010014#include <math.h>
15
peah522d71b2017-02-23 05:16:26 -080016#include <algorithm>
17#include <memory>
18#include <vector>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010021#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/optional.h"
23#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren3ab308f2018-02-21 08:46:03 +010024#include "modules/audio_processing/aec3/delay_estimate.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020025#include "modules/audio_processing/aec3/echo_audibility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/aec3/echo_path_variability.h"
27#include "modules/audio_processing/aec3/erl_estimator.h"
28#include "modules/audio_processing/aec3/erle_estimator.h"
Per Åhgren5c532d32018-03-22 00:29:25 +010029#include "modules/audio_processing/aec3/filter_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_processing/aec3/render_buffer.h"
Per Åhgren12eb8582018-03-06 10:40:51 +010031#include "modules/audio_processing/aec3/suppression_gain_limiter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080033
34namespace webrtc {
35
36class ApmDataDumper;
37
38// Handles the state and the conditions for the echo removal functionality.
39class AecState {
40 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020041 explicit AecState(const EchoCanceller3Config& config);
peah522d71b2017-02-23 05:16:26 -080042 ~AecState();
43
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010044 // Returns whether the echo subtractor can be used to determine the residual
45 // echo.
peah522d71b2017-02-23 05:16:26 -080046 bool UsableLinearEstimate() const { return usable_linear_estimate_; }
47
Per Åhgren5c532d32018-03-22 00:29:25 +010048 // Returns whether the echo subtractor output should be used as output.
49 bool UseLinearFilterOutput() const { return use_linear_filter_output_; }
50
51 // Returns the estimated echo path gain.
Per Åhgrenced31ba2018-05-09 11:48:49 +020052 float EchoPathGain() const { return filter_analyzer_.Gain(); }
peah522d71b2017-02-23 05:16:26 -080053
peah522d71b2017-02-23 05:16:26 -080054 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010055 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
peahebe77782017-02-27 07:29:21 -080056
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020057 // Returns the appropriate scaling of the residual echo to match the
58 // audibility.
59 void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const {
60 echo_audibility_.GetResidualEchoScaling(residual_scaling);
61 }
62
63 // Returns whether the stationary properties of the signals are used in the
64 // aec.
Per Åhgren90e3fbd2018-05-16 15:25:04 +020065 bool UseStationaryProperties() const { return use_stationary_properties_; }
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020066
peah522d71b2017-02-23 05:16:26 -080067 // Returns the ERLE.
68 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
69 return erle_estimator_.Erle();
70 }
71
Gustaf Ullberg332150d2017-11-22 14:17:39 +010072 // Returns the time-domain ERLE.
73 float ErleTimeDomain() const { return erle_estimator_.ErleTimeDomain(); }
74
peah522d71b2017-02-23 05:16:26 -080075 // Returns the ERL.
76 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
77 return erl_estimator_.Erl();
78 }
79
Gustaf Ullberg332150d2017-11-22 14:17:39 +010080 // Returns the time-domain ERL.
81 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
82
peah522d71b2017-02-23 05:16:26 -080083 // Returns the delay estimate based on the linear filter.
Per Åhgren5c532d32018-03-22 00:29:25 +010084 int FilterDelayBlocks() const { return filter_delay_blocks_; }
85
86 // Returns the internal delay estimate based on the linear filter.
87 rtc::Optional<int> InternalDelay() const { return internal_delay_; }
peah522d71b2017-02-23 05:16:26 -080088
peah522d71b2017-02-23 05:16:26 -080089 // Returns whether the capture signal is saturated.
90 bool SaturatedCapture() const { return capture_signal_saturation_; }
91
peah86afe9d2017-04-06 15:45:32 -070092 // Returns whether the echo signal is saturated.
93 bool SaturatedEcho() const { return echo_saturation_; }
94
peah522d71b2017-02-23 05:16:26 -080095 // Updates the capture signal saturation.
96 void UpdateCaptureSaturation(bool capture_signal_saturation) {
97 capture_signal_saturation_ = capture_signal_saturation;
98 }
99
Per Åhgren1b4059e2017-10-15 20:19:21 +0200100 // Returns whether the transparent mode is active
101 bool TransparentMode() const { return transparent_mode_; }
peah522d71b2017-02-23 05:16:26 -0800102
peah86afe9d2017-04-06 15:45:32 -0700103 // Takes appropriate action at an echo path change.
104 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
105
peah89420452017-04-07 06:13:39 -0700106 // Returns the decay factor for the echo reverberation.
peah29103572017-07-11 02:54:02 -0700107 float ReverbDecay() const { return reverb_decay_; }
peah89420452017-04-07 06:13:39 -0700108
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100109 // Returns the upper limit for the echo suppression gain.
Per Åhgren12eb8582018-03-06 10:40:51 +0100110 float SuppressionGainLimit() const {
111 return suppression_gain_limiter_.Limit();
112 }
peah6d822ad2017-04-10 13:52:14 -0700113
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100114 // Returns whether the linear filter should have been able to properly adapt.
115 bool FilterHasHadTimeToConverge() const {
116 return filter_has_had_time_to_converge_;
117 }
Per Åhgren1b4059e2017-10-15 20:19:21 +0200118
Per Åhgrena98c8072018-01-15 19:17:16 +0100119 // Returns whether the filter adaptation is still in the initial state.
120 bool InitialState() const { return initial_state_; }
121
peah522d71b2017-02-23 05:16:26 -0800122 // Updates the aec state.
Per Åhgren5c532d32018-03-22 00:29:25 +0100123 void Update(const rtc::Optional<DelayEstimate>& external_delay,
Per Åhgren3ab308f2018-02-21 08:46:03 +0100124 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700125 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100126 const std::vector<float>& adaptive_filter_impulse_response,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200127 bool converged_filter,
Per Åhgren5c532d32018-03-22 00:29:25 +0100128 bool diverged_filter,
peah86afe9d2017-04-06 15:45:32 -0700129 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800130 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800131 const std::array<float, kFftLengthBy2Plus1>& Y2,
Per Åhgren5c532d32018-03-22 00:29:25 +0100132 const std::array<float, kBlockSize>& s);
peah522d71b2017-02-23 05:16:26 -0800133
134 private:
Per Åhgren09a718a2017-12-11 22:28:45 +0100135 void UpdateReverb(const std::vector<float>& impulse_response);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100136 bool DetectActiveRender(rtc::ArrayView<const float> x) const;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100137 void UpdateSuppressorGainLimit(bool render_activity);
Per Åhgren31122d62018-04-10 16:33:55 +0200138 bool DetectEchoSaturation(rtc::ArrayView<const float> x,
139 float echo_path_gain);
peah29103572017-07-11 02:54:02 -0700140
peah522d71b2017-02-23 05:16:26 -0800141 static int instance_count_;
142 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200143 const EchoCanceller3Config config_;
Per Åhgrend18e87e2018-05-09 12:07:26 +0200144 const bool allow_transparent_mode_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200145 const bool use_stationary_properties_;
peah522d71b2017-02-23 05:16:26 -0800146 ErlEstimator erl_estimator_;
147 ErleEstimator erle_estimator_;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200148 size_t capture_block_counter_ = 0;
Per Åhgren5c532d32018-03-22 00:29:25 +0100149 size_t blocks_since_reset_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100150 size_t blocks_with_proper_filter_adaptation_ = 0;
151 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800152 bool usable_linear_estimate_ = false;
peah522d71b2017-02-23 05:16:26 -0800153 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700154 bool echo_saturation_ = false;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200155 bool transparent_mode_ = false;
peahe52a2032017-04-19 09:03:40 -0700156 bool render_received_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100157 int filter_delay_blocks_ = 0;
peah86afe9d2017-04-06 15:45:32 -0700158 size_t blocks_since_last_saturation_ = 1000;
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100159 float tail_energy_ = 0.f;
160 float accumulated_nz_ = 0.f;
161 float accumulated_nn_ = 0.f;
162 float accumulated_count_ = 0.f;
163 size_t current_reverb_decay_section_ = 0;
164 size_t num_reverb_decay_sections_ = 0;
165 size_t num_reverb_decay_sections_next_ = 0;
166 bool found_end_of_reverb_decay_ = false;
167 bool main_filter_is_adapting_ = true;
168 std::array<float, kMaxAdaptiveFilterLength> block_energies_;
Per Åhgren09a718a2017-12-11 22:28:45 +0100169 std::vector<float> max_render_;
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100170 float reverb_decay_ = fabsf(config_.ep_strength.default_len);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100171 bool filter_has_had_time_to_converge_ = false;
Per Åhgrena98c8072018-01-15 19:17:16 +0100172 bool initial_state_ = true;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100173 const float gain_rampup_increase_;
Per Åhgren12eb8582018-03-06 10:40:51 +0100174 SuppressionGainUpperLimiter suppression_gain_limiter_;
Per Åhgren5c532d32018-03-22 00:29:25 +0100175 FilterAnalyzer filter_analyzer_;
176 bool use_linear_filter_output_ = false;
177 rtc::Optional<int> internal_delay_;
178 size_t diverged_blocks_ = 0;
179 bool filter_should_have_converged_ = false;
180 size_t blocks_since_converged_filter_;
181 size_t active_blocks_since_consistent_filter_estimate_;
182 bool converged_filter_seen_ = false;
183 bool consistent_filter_seen_ = false;
184 bool external_delay_seen_ = false;
Per Åhgrenf3e2bf12018-03-22 10:15:59 +0100185 size_t converged_filter_count_ = 0;
186 bool finite_erl_ = false;
Per Åhgren8131eb02018-03-28 18:13:41 +0200187 size_t active_blocks_since_converged_filter_ = 0;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200188 EchoAudibility echo_audibility_;
peah29103572017-07-11 02:54:02 -0700189
peah8cee56f2017-08-24 22:36:53 -0700190 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
peah522d71b2017-02-23 05:16:26 -0800191};
192
193} // namespace webrtc
194
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200195#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_