blob: c5ec6cc3e5662af1e5b6e6c3c89caa83d0305fda [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
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010022#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#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"
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020031#include "modules/audio_processing/aec3/reverb_model_estimator.h"
Per Åhgrenb20b9372018-07-13 00:22:54 +020032#include "modules/audio_processing/aec3/subtractor_output.h"
33#include "modules/audio_processing/aec3/subtractor_output_analyzer.h"
Per Åhgren12eb8582018-03-06 10:40:51 +010034#include "modules/audio_processing/aec3/suppression_gain_limiter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080036
37namespace webrtc {
38
39class ApmDataDumper;
40
41// Handles the state and the conditions for the echo removal functionality.
42class AecState {
43 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020044 explicit AecState(const EchoCanceller3Config& config);
peah522d71b2017-02-23 05:16:26 -080045 ~AecState();
46
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010047 // Returns whether the echo subtractor can be used to determine the residual
48 // echo.
peah522d71b2017-02-23 05:16:26 -080049 bool UsableLinearEstimate() const { return usable_linear_estimate_; }
50
Per Åhgren5c532d32018-03-22 00:29:25 +010051 // Returns whether the echo subtractor output should be used as output.
52 bool UseLinearFilterOutput() const { return use_linear_filter_output_; }
53
54 // Returns the estimated echo path gain.
Per Åhgrenced31ba2018-05-09 11:48:49 +020055 float EchoPathGain() const { return filter_analyzer_.Gain(); }
peah522d71b2017-02-23 05:16:26 -080056
peah522d71b2017-02-23 05:16:26 -080057 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010058 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
peahebe77782017-02-27 07:29:21 -080059
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020060 // Returns the appropriate scaling of the residual echo to match the
61 // audibility.
62 void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const {
Per Åhgrenb2d71162018-09-10 14:10:34 +020063 echo_audibility_.GetResidualEchoScaling(filter_has_had_time_to_converge_,
64 residual_scaling);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020065 }
66
67 // Returns whether the stationary properties of the signals are used in the
68 // aec.
Per Åhgren90e3fbd2018-05-16 15:25:04 +020069 bool UseStationaryProperties() const { return use_stationary_properties_; }
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020070
peah522d71b2017-02-23 05:16:26 -080071 // Returns the ERLE.
72 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
73 return erle_estimator_.Erle();
74 }
75
Gustaf Ullberg6c618c72018-06-28 14:21:16 +020076 // Returns any uncertainty in the ERLE estimate.
77 absl::optional<float> ErleUncertainty() const {
Per Åhgren22754392018-08-10 18:37:38 +020078 if (!filter_has_had_time_to_converge_ &&
79 use_uncertainty_until_sufficiently_adapted_) {
Per Åhgren6204adf2018-08-19 11:12:00 +020080 return uncertainty_before_convergence_;
Per Åhgren22754392018-08-10 18:37:38 +020081 }
Gustaf Ullberg6c618c72018-06-28 14:21:16 +020082 return absl::nullopt;
83 }
84
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020085 // Returns the fullband ERLE estimate in log2 units.
86 float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
Gustaf Ullberg332150d2017-11-22 14:17:39 +010087
peah522d71b2017-02-23 05:16:26 -080088 // Returns the ERL.
89 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
90 return erl_estimator_.Erl();
91 }
92
Gustaf Ullberg332150d2017-11-22 14:17:39 +010093 // Returns the time-domain ERL.
94 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
95
peah522d71b2017-02-23 05:16:26 -080096 // Returns the delay estimate based on the linear filter.
Per Åhgren5c532d32018-03-22 00:29:25 +010097 int FilterDelayBlocks() const { return filter_delay_blocks_; }
98
99 // Returns the internal delay estimate based on the linear filter.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200100 absl::optional<int> InternalDelay() const { return internal_delay_; }
peah522d71b2017-02-23 05:16:26 -0800101
peah522d71b2017-02-23 05:16:26 -0800102 // Returns whether the capture signal is saturated.
103 bool SaturatedCapture() const { return capture_signal_saturation_; }
104
peah86afe9d2017-04-06 15:45:32 -0700105 // Returns whether the echo signal is saturated.
106 bool SaturatedEcho() const { return echo_saturation_; }
107
peah522d71b2017-02-23 05:16:26 -0800108 // Updates the capture signal saturation.
109 void UpdateCaptureSaturation(bool capture_signal_saturation) {
110 capture_signal_saturation_ = capture_signal_saturation;
111 }
112
Per Åhgren1b4059e2017-10-15 20:19:21 +0200113 // Returns whether the transparent mode is active
114 bool TransparentMode() const { return transparent_mode_; }
peah522d71b2017-02-23 05:16:26 -0800115
peah86afe9d2017-04-06 15:45:32 -0700116 // Takes appropriate action at an echo path change.
117 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
118
peah89420452017-04-07 06:13:39 -0700119 // Returns the decay factor for the echo reverberation.
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200120 float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); }
peah89420452017-04-07 06:13:39 -0700121
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200122 // Return the frequency response of the reverberant echo.
123 rtc::ArrayView<const float> GetReverbFrequencyResponse() const {
124 return reverb_model_estimator_.GetReverbFrequencyResponse();
125 }
126
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100127 // Returns the upper limit for the echo suppression gain.
Per Åhgren12eb8582018-03-06 10:40:51 +0100128 float SuppressionGainLimit() const {
129 return suppression_gain_limiter_.Limit();
130 }
peah6d822ad2017-04-10 13:52:14 -0700131
Jesús de Vicente Peñadd092872018-05-25 16:55:11 +0200132 // Returns whether the suppression gain limiter is active.
133 bool IsSuppressionGainLimitActive() const {
134 return suppression_gain_limiter_.IsActive();
135 }
136
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100137 // Returns whether the linear filter should have been able to properly adapt.
138 bool FilterHasHadTimeToConverge() const {
139 return filter_has_had_time_to_converge_;
140 }
Per Åhgren1b4059e2017-10-15 20:19:21 +0200141
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200142 // Returns whether the transition for going out of the initial stated has
143 // been triggered.
144 bool TransitionTriggered() const { return transition_triggered_; }
Per Åhgrena98c8072018-01-15 19:17:16 +0100145
peah522d71b2017-02-23 05:16:26 -0800146 // Updates the aec state.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200147 void Update(const absl::optional<DelayEstimate>& external_delay,
Per Åhgren3ab308f2018-02-21 08:46:03 +0100148 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700149 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100150 const std::vector<float>& adaptive_filter_impulse_response,
peah86afe9d2017-04-06 15:45:32 -0700151 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800152 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800153 const std::array<float, kFftLengthBy2Plus1>& Y2,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200154 const SubtractorOutput& subtractor_output,
155 rtc::ArrayView<const float> y);
peah522d71b2017-02-23 05:16:26 -0800156
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200157 // Returns filter length in blocks.
158 int FilterLengthBlocks() const {
159 return filter_analyzer_.FilterLengthBlocks();
160 }
161
peah522d71b2017-02-23 05:16:26 -0800162 private:
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100163 bool DetectActiveRender(rtc::ArrayView<const float> x) const;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100164 void UpdateSuppressorGainLimit(bool render_activity);
Per Åhgren31122d62018-04-10 16:33:55 +0200165 bool DetectEchoSaturation(rtc::ArrayView<const float> x,
166 float echo_path_gain);
peah29103572017-07-11 02:54:02 -0700167
peah522d71b2017-02-23 05:16:26 -0800168 static int instance_count_;
169 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200170 const EchoCanceller3Config config_;
Per Åhgrend18e87e2018-05-09 12:07:26 +0200171 const bool allow_transparent_mode_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200172 const bool use_stationary_properties_;
Per Åhgren05d8ee12018-06-07 15:59:59 +0200173 const bool enforce_delay_after_realignment_;
Per Åhgrenf954ba52018-07-27 14:53:58 +0200174 const bool early_filter_usage_activated_;
175 const bool use_short_initial_state_;
Per Åhgren22754392018-08-10 18:37:38 +0200176 const bool convergence_trigger_linear_mode_;
177 const bool no_alignment_required_for_linear_mode_;
178 const bool use_uncertainty_until_sufficiently_adapted_;
Per Åhgren6204adf2018-08-19 11:12:00 +0200179 const float uncertainty_before_convergence_;
180 const bool early_entry_to_converged_mode_;
Per Åhgren6204adf2018-08-19 11:12:00 +0200181 const bool early_limiter_deactivation_;
Jesús de Vicente Peña7015bb42018-08-29 11:15:30 +0200182 const bool reset_erle_after_echo_path_changes_;
peah522d71b2017-02-23 05:16:26 -0800183 ErlEstimator erl_estimator_;
184 ErleEstimator erle_estimator_;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200185 size_t capture_block_counter_ = 0;
Per Åhgren5c532d32018-03-22 00:29:25 +0100186 size_t blocks_since_reset_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100187 size_t blocks_with_proper_filter_adaptation_ = 0;
188 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800189 bool usable_linear_estimate_ = false;
peah522d71b2017-02-23 05:16:26 -0800190 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700191 bool echo_saturation_ = false;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200192 bool transparent_mode_ = false;
peahe52a2032017-04-19 09:03:40 -0700193 bool render_received_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100194 int filter_delay_blocks_ = 0;
peah86afe9d2017-04-06 15:45:32 -0700195 size_t blocks_since_last_saturation_ = 1000;
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200196
Per Åhgren09a718a2017-12-11 22:28:45 +0100197 std::vector<float> max_render_;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100198 bool filter_has_had_time_to_converge_ = false;
Per Åhgrena98c8072018-01-15 19:17:16 +0100199 bool initial_state_ = true;
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200200 bool transition_triggered_ = false;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100201 const float gain_rampup_increase_;
Per Åhgren12eb8582018-03-06 10:40:51 +0100202 SuppressionGainUpperLimiter suppression_gain_limiter_;
Per Åhgren5c532d32018-03-22 00:29:25 +0100203 FilterAnalyzer filter_analyzer_;
204 bool use_linear_filter_output_ = false;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200205 absl::optional<int> internal_delay_;
Per Åhgren5c532d32018-03-22 00:29:25 +0100206 size_t diverged_blocks_ = 0;
207 bool filter_should_have_converged_ = false;
208 size_t blocks_since_converged_filter_;
209 size_t active_blocks_since_consistent_filter_estimate_;
210 bool converged_filter_seen_ = false;
211 bool consistent_filter_seen_ = false;
212 bool external_delay_seen_ = false;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200213 absl::optional<DelayEstimate> external_delay_;
Per Åhgren05d8ee12018-06-07 15:59:59 +0200214 size_t frames_since_external_delay_change_ = 0;
Per Åhgrenf3e2bf12018-03-22 10:15:59 +0100215 size_t converged_filter_count_ = 0;
216 bool finite_erl_ = false;
Per Åhgren8131eb02018-03-28 18:13:41 +0200217 size_t active_blocks_since_converged_filter_ = 0;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200218 EchoAudibility echo_audibility_;
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200219 ReverbModelEstimator reverb_model_estimator_;
Per Åhgrenb20b9372018-07-13 00:22:54 +0200220 SubtractorOutputAnalyzer subtractor_output_analyzer_;
peah8cee56f2017-08-24 22:36:53 -0700221 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
peah522d71b2017-02-23 05:16:26 -0800222};
223
224} // namespace webrtc
225
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200226#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_