peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame] | 14 | #include <math.h> |
| 15 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 | |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "api/array_view.h" |
Gustaf Ullberg | 3646f97 | 2018-02-14 15:19:04 +0100 | [diff] [blame] | 22 | #include "api/audio/echo_canceller3_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/aec3_common.h" |
Per Åhgren | 3ab308f | 2018-02-21 08:46:03 +0100 | [diff] [blame] | 24 | #include "modules/audio_processing/aec3/delay_estimate.h" |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 25 | #include "modules/audio_processing/aec3/echo_audibility.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #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 Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 29 | #include "modules/audio_processing/aec3/filter_analyzer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "modules/audio_processing/aec3/render_buffer.h" |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame^] | 31 | #include "modules/audio_processing/aec3/reverb_model_estimator.h" |
Per Åhgren | 12eb858 | 2018-03-06 10:40:51 +0100 | [diff] [blame] | 32 | #include "modules/audio_processing/aec3/suppression_gain_limiter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "rtc_base/constructormagic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 34 | |
| 35 | namespace webrtc { |
| 36 | |
| 37 | class ApmDataDumper; |
| 38 | |
| 39 | // Handles the state and the conditions for the echo removal functionality. |
| 40 | class AecState { |
| 41 | public: |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 42 | explicit AecState(const EchoCanceller3Config& config); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 43 | ~AecState(); |
| 44 | |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 45 | // Returns whether the echo subtractor can be used to determine the residual |
| 46 | // echo. |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 47 | bool UsableLinearEstimate() const { return usable_linear_estimate_; } |
| 48 | |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 49 | // Returns whether the echo subtractor output should be used as output. |
| 50 | bool UseLinearFilterOutput() const { return use_linear_filter_output_; } |
| 51 | |
| 52 | // Returns the estimated echo path gain. |
Per Åhgren | ced31ba | 2018-05-09 11:48:49 +0200 | [diff] [blame] | 53 | float EchoPathGain() const { return filter_analyzer_.Gain(); } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 54 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | // Returns whether the render signal is currently active. |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 56 | bool ActiveRender() const { return blocks_with_active_render_ > 200; } |
peah | ebe7778 | 2017-02-27 07:29:21 -0800 | [diff] [blame] | 57 | |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 58 | // Returns the appropriate scaling of the residual echo to match the |
| 59 | // audibility. |
| 60 | void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const { |
| 61 | echo_audibility_.GetResidualEchoScaling(residual_scaling); |
| 62 | } |
| 63 | |
| 64 | // Returns whether the stationary properties of the signals are used in the |
| 65 | // aec. |
Per Åhgren | 90e3fbd | 2018-05-16 15:25:04 +0200 | [diff] [blame] | 66 | bool UseStationaryProperties() const { return use_stationary_properties_; } |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 67 | |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame^] | 68 | // Returns true if the current render block is estimated as stationary. |
| 69 | bool IsBlockStationary() const { |
| 70 | if (UseStationaryProperties()) { |
| 71 | return echo_audibility_.IsBlockStationary(); |
| 72 | } else { |
| 73 | // Assume that a non stationary block when the use of |
| 74 | // stationary properties are not enabled. |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 79 | // Returns the ERLE. |
| 80 | const std::array<float, kFftLengthBy2Plus1>& Erle() const { |
| 81 | return erle_estimator_.Erle(); |
| 82 | } |
| 83 | |
Gustaf Ullberg | 6c618c7 | 2018-06-28 14:21:16 +0200 | [diff] [blame] | 84 | // Returns any uncertainty in the ERLE estimate. |
| 85 | absl::optional<float> ErleUncertainty() const { |
| 86 | if (allow_linear_mode_with_diverged_filter_ && diverged_linear_filter_) { |
| 87 | return 10.f; |
| 88 | } |
| 89 | return absl::nullopt; |
| 90 | } |
| 91 | |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame^] | 92 | // Returns the time-domain ERLE in log2 units. |
| 93 | float ErleTimeDomainLog2() const { |
| 94 | return erle_estimator_.ErleTimeDomainLog2(); |
| 95 | } |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 96 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 97 | // Returns the ERL. |
| 98 | const std::array<float, kFftLengthBy2Plus1>& Erl() const { |
| 99 | return erl_estimator_.Erl(); |
| 100 | } |
| 101 | |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 102 | // Returns the time-domain ERL. |
| 103 | float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); } |
| 104 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 105 | // Returns the delay estimate based on the linear filter. |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 106 | int FilterDelayBlocks() const { return filter_delay_blocks_; } |
| 107 | |
| 108 | // Returns the internal delay estimate based on the linear filter. |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 109 | absl::optional<int> InternalDelay() const { return internal_delay_; } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 110 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 111 | // Returns whether the capture signal is saturated. |
| 112 | bool SaturatedCapture() const { return capture_signal_saturation_; } |
| 113 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 114 | // Returns whether the echo signal is saturated. |
| 115 | bool SaturatedEcho() const { return echo_saturation_; } |
| 116 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 117 | // Updates the capture signal saturation. |
| 118 | void UpdateCaptureSaturation(bool capture_signal_saturation) { |
| 119 | capture_signal_saturation_ = capture_signal_saturation; |
| 120 | } |
| 121 | |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 122 | // Returns whether the transparent mode is active |
| 123 | bool TransparentMode() const { return transparent_mode_; } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 124 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 125 | // Takes appropriate action at an echo path change. |
| 126 | void HandleEchoPathChange(const EchoPathVariability& echo_path_variability); |
| 127 | |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 128 | // Returns the decay factor for the echo reverberation. |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame^] | 129 | float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); } |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 130 | |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 131 | // Returns the upper limit for the echo suppression gain. |
Per Åhgren | 12eb858 | 2018-03-06 10:40:51 +0100 | [diff] [blame] | 132 | float SuppressionGainLimit() const { |
| 133 | return suppression_gain_limiter_.Limit(); |
| 134 | } |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 135 | |
Jesús de Vicente Peña | dd09287 | 2018-05-25 16:55:11 +0200 | [diff] [blame] | 136 | // Returns whether the suppression gain limiter is active. |
| 137 | bool IsSuppressionGainLimitActive() const { |
| 138 | return suppression_gain_limiter_.IsActive(); |
| 139 | } |
| 140 | |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 141 | // Returns whether the linear filter should have been able to properly adapt. |
| 142 | bool FilterHasHadTimeToConverge() const { |
| 143 | return filter_has_had_time_to_converge_; |
| 144 | } |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 145 | |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 146 | // Returns whether the filter adaptation is still in the initial state. |
| 147 | bool InitialState() const { return initial_state_; } |
| 148 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 149 | // Updates the aec state. |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 150 | void Update(const absl::optional<DelayEstimate>& external_delay, |
Per Åhgren | 3ab308f | 2018-02-21 08:46:03 +0100 | [diff] [blame] | 151 | const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 152 | adaptive_filter_frequency_response, |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 153 | const std::vector<float>& adaptive_filter_impulse_response, |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 154 | bool converged_filter, |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 155 | bool diverged_filter, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 156 | const RenderBuffer& render_buffer, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 157 | const std::array<float, kFftLengthBy2Plus1>& E2_main, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 158 | const std::array<float, kFftLengthBy2Plus1>& Y2, |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 159 | const std::array<float, kBlockSize>& s); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 160 | |
Jesús de Vicente Peña | e58bd8a | 2018-06-26 17:19:15 +0200 | [diff] [blame] | 161 | // Returns the tail freq. response of the linear filter. |
| 162 | rtc::ArrayView<const float> GetFreqRespTail() const { |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame^] | 163 | return reverb_model_estimator_.GetFreqRespTail(); |
Jesús de Vicente Peña | e58bd8a | 2018-06-26 17:19:15 +0200 | [diff] [blame] | 164 | } |
Jesús de Vicente Peña | 075cb2b | 2018-06-13 15:13:55 +0200 | [diff] [blame] | 165 | |
| 166 | // Returns filter length in blocks. |
| 167 | int FilterLengthBlocks() const { |
| 168 | return filter_analyzer_.FilterLengthBlocks(); |
| 169 | } |
| 170 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 171 | private: |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 172 | bool DetectActiveRender(rtc::ArrayView<const float> x) const; |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 173 | void UpdateSuppressorGainLimit(bool render_activity); |
Per Åhgren | 31122d6 | 2018-04-10 16:33:55 +0200 | [diff] [blame] | 174 | bool DetectEchoSaturation(rtc::ArrayView<const float> x, |
| 175 | float echo_path_gain); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 176 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 177 | static int instance_count_; |
| 178 | std::unique_ptr<ApmDataDumper> data_dumper_; |
Per Åhgren | 90e3fbd | 2018-05-16 15:25:04 +0200 | [diff] [blame] | 179 | const EchoCanceller3Config config_; |
Per Åhgren | d18e87e | 2018-05-09 12:07:26 +0200 | [diff] [blame] | 180 | const bool allow_transparent_mode_; |
Per Åhgren | 90e3fbd | 2018-05-16 15:25:04 +0200 | [diff] [blame] | 181 | const bool use_stationary_properties_; |
Per Åhgren | 05d8ee1 | 2018-06-07 15:59:59 +0200 | [diff] [blame] | 182 | const bool enforce_delay_after_realignment_; |
Gustaf Ullberg | 6c618c7 | 2018-06-28 14:21:16 +0200 | [diff] [blame] | 183 | const bool allow_linear_mode_with_diverged_filter_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 184 | ErlEstimator erl_estimator_; |
| 185 | ErleEstimator erle_estimator_; |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 186 | size_t capture_block_counter_ = 0; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 187 | size_t blocks_since_reset_ = 0; |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 188 | size_t blocks_with_proper_filter_adaptation_ = 0; |
| 189 | size_t blocks_with_active_render_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 190 | bool usable_linear_estimate_ = false; |
Gustaf Ullberg | 6c618c7 | 2018-06-28 14:21:16 +0200 | [diff] [blame] | 191 | bool diverged_linear_filter_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 192 | bool capture_signal_saturation_ = false; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 193 | bool echo_saturation_ = false; |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 194 | bool transparent_mode_ = false; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 195 | bool render_received_ = false; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 196 | int filter_delay_blocks_ = 0; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 197 | size_t blocks_since_last_saturation_ = 1000; |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame^] | 198 | |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 199 | std::vector<float> max_render_; |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 200 | bool filter_has_had_time_to_converge_ = false; |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 201 | bool initial_state_ = true; |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 202 | const float gain_rampup_increase_; |
Per Åhgren | 12eb858 | 2018-03-06 10:40:51 +0100 | [diff] [blame] | 203 | SuppressionGainUpperLimiter suppression_gain_limiter_; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 204 | FilterAnalyzer filter_analyzer_; |
| 205 | bool use_linear_filter_output_ = false; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 206 | absl::optional<int> internal_delay_; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 207 | size_t diverged_blocks_ = 0; |
| 208 | bool filter_should_have_converged_ = false; |
| 209 | size_t blocks_since_converged_filter_; |
| 210 | size_t active_blocks_since_consistent_filter_estimate_; |
| 211 | bool converged_filter_seen_ = false; |
| 212 | bool consistent_filter_seen_ = false; |
| 213 | bool external_delay_seen_ = false; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 214 | absl::optional<DelayEstimate> external_delay_; |
Per Åhgren | 05d8ee1 | 2018-06-07 15:59:59 +0200 | [diff] [blame] | 215 | size_t frames_since_external_delay_change_ = 0; |
Per Åhgren | f3e2bf1 | 2018-03-22 10:15:59 +0100 | [diff] [blame] | 216 | size_t converged_filter_count_ = 0; |
| 217 | bool finite_erl_ = false; |
Per Åhgren | 8131eb0 | 2018-03-28 18:13:41 +0200 | [diff] [blame] | 218 | size_t active_blocks_since_converged_filter_ = 0; |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 219 | EchoAudibility echo_audibility_; |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame^] | 220 | ReverbModelEstimator reverb_model_estimator_; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 221 | RTC_DISALLOW_COPY_AND_ASSIGN(AecState); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace webrtc |
| 225 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 226 | #endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |