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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/array_view.h" |
Gustaf Ullberg | 3646f97 | 2018-02-14 15:19:04 +0100 | [diff] [blame] | 21 | #include "api/audio/echo_canceller3_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/optional.h" |
| 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" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_processing/aec3/echo_path_variability.h" |
| 26 | #include "modules/audio_processing/aec3/erl_estimator.h" |
| 27 | #include "modules/audio_processing/aec3/erle_estimator.h" |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 28 | #include "modules/audio_processing/aec3/filter_analyzer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "modules/audio_processing/aec3/render_buffer.h" |
Per Åhgren | 12eb858 | 2018-03-06 10:40:51 +0100 | [diff] [blame] | 30 | #include "modules/audio_processing/aec3/suppression_gain_limiter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/constructormagic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
| 34 | |
| 35 | class ApmDataDumper; |
| 36 | |
| 37 | // Handles the state and the conditions for the echo removal functionality. |
| 38 | class AecState { |
| 39 | public: |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 40 | explicit AecState(const EchoCanceller3Config& config); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 41 | ~AecState(); |
| 42 | |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 43 | // Returns whether the echo subtractor can be used to determine the residual |
| 44 | // echo. |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 45 | bool UsableLinearEstimate() const { return usable_linear_estimate_; } |
| 46 | |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 47 | // Returns whether the echo subtractor output should be used as output. |
| 48 | bool UseLinearFilterOutput() const { return use_linear_filter_output_; } |
| 49 | |
| 50 | // Returns the estimated echo path gain. |
| 51 | bool EchoPathGain() const { return filter_analyzer_.Gain(); } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 52 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 53 | // Returns whether the render signal is currently active. |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 54 | bool ActiveRender() const { return blocks_with_active_render_ > 200; } |
peah | ebe7778 | 2017-02-27 07:29:21 -0800 | [diff] [blame] | 55 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 56 | // Returns the ERLE. |
| 57 | const std::array<float, kFftLengthBy2Plus1>& Erle() const { |
| 58 | return erle_estimator_.Erle(); |
| 59 | } |
| 60 | |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 61 | // Returns the time-domain ERLE. |
| 62 | float ErleTimeDomain() const { return erle_estimator_.ErleTimeDomain(); } |
| 63 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 64 | // Returns the ERL. |
| 65 | const std::array<float, kFftLengthBy2Plus1>& Erl() const { |
| 66 | return erl_estimator_.Erl(); |
| 67 | } |
| 68 | |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 69 | // Returns the time-domain ERL. |
| 70 | float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); } |
| 71 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 72 | // Returns the delay estimate based on the linear filter. |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 73 | int FilterDelayBlocks() const { return filter_delay_blocks_; } |
| 74 | |
| 75 | // Returns the internal delay estimate based on the linear filter. |
| 76 | rtc::Optional<int> InternalDelay() const { return internal_delay_; } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 77 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 78 | // Returns whether the capture signal is saturated. |
| 79 | bool SaturatedCapture() const { return capture_signal_saturation_; } |
| 80 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 81 | // Returns whether the echo signal is saturated. |
| 82 | bool SaturatedEcho() const { return echo_saturation_; } |
| 83 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 84 | // Updates the capture signal saturation. |
| 85 | void UpdateCaptureSaturation(bool capture_signal_saturation) { |
| 86 | capture_signal_saturation_ = capture_signal_saturation; |
| 87 | } |
| 88 | |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 89 | // Returns whether the transparent mode is active |
| 90 | bool TransparentMode() const { return transparent_mode_; } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 91 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 92 | // Takes appropriate action at an echo path change. |
| 93 | void HandleEchoPathChange(const EchoPathVariability& echo_path_variability); |
| 94 | |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 95 | // Returns the decay factor for the echo reverberation. |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 96 | float ReverbDecay() const { return reverb_decay_; } |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 97 | |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 98 | // Returns the upper limit for the echo suppression gain. |
Per Åhgren | 12eb858 | 2018-03-06 10:40:51 +0100 | [diff] [blame] | 99 | float SuppressionGainLimit() const { |
| 100 | return suppression_gain_limiter_.Limit(); |
| 101 | } |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 102 | |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 103 | // Returns whether the linear filter should have been able to properly adapt. |
| 104 | bool FilterHasHadTimeToConverge() const { |
| 105 | return filter_has_had_time_to_converge_; |
| 106 | } |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 107 | |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 108 | // Returns whether the filter adaptation is still in the initial state. |
| 109 | bool InitialState() const { return initial_state_; } |
| 110 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 111 | // Updates the aec state. |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 112 | void Update(const rtc::Optional<DelayEstimate>& external_delay, |
Per Åhgren | 3ab308f | 2018-02-21 08:46:03 +0100 | [diff] [blame] | 113 | const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 114 | adaptive_filter_frequency_response, |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 115 | const std::vector<float>& adaptive_filter_impulse_response, |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 116 | bool converged_filter, |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 117 | bool diverged_filter, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 118 | const RenderBuffer& render_buffer, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 119 | const std::array<float, kFftLengthBy2Plus1>& E2_main, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 120 | const std::array<float, kFftLengthBy2Plus1>& Y2, |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 121 | const std::array<float, kBlockSize>& s); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 122 | |
| 123 | private: |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 124 | void UpdateReverb(const std::vector<float>& impulse_response); |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 125 | bool DetectActiveRender(rtc::ArrayView<const float> x) const; |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 126 | void UpdateSuppressorGainLimit(bool render_activity); |
Per Åhgren | 31122d6 | 2018-04-10 16:33:55 +0200 | [diff] [blame^] | 127 | bool DetectEchoSaturation(rtc::ArrayView<const float> x, |
| 128 | float echo_path_gain); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 129 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 130 | static int instance_count_; |
| 131 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 132 | ErlEstimator erl_estimator_; |
| 133 | ErleEstimator erle_estimator_; |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 134 | size_t capture_block_counter_ = 0; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 135 | size_t blocks_since_reset_ = 0; |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 136 | size_t blocks_with_proper_filter_adaptation_ = 0; |
| 137 | size_t blocks_with_active_render_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 138 | bool usable_linear_estimate_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 139 | bool capture_signal_saturation_ = false; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 140 | bool echo_saturation_ = false; |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame] | 141 | bool transparent_mode_ = false; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 142 | bool render_received_ = false; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 143 | int filter_delay_blocks_ = 0; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 144 | size_t blocks_since_last_saturation_ = 1000; |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame] | 145 | float tail_energy_ = 0.f; |
| 146 | float accumulated_nz_ = 0.f; |
| 147 | float accumulated_nn_ = 0.f; |
| 148 | float accumulated_count_ = 0.f; |
| 149 | size_t current_reverb_decay_section_ = 0; |
| 150 | size_t num_reverb_decay_sections_ = 0; |
| 151 | size_t num_reverb_decay_sections_next_ = 0; |
| 152 | bool found_end_of_reverb_decay_ = false; |
| 153 | bool main_filter_is_adapting_ = true; |
| 154 | std::array<float, kMaxAdaptiveFilterLength> block_energies_; |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 155 | const EchoCanceller3Config config_; |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 156 | std::vector<float> max_render_; |
Christian Schuldt | f4e99db | 2018-03-01 11:32:50 +0100 | [diff] [blame] | 157 | float reverb_decay_ = fabsf(config_.ep_strength.default_len); |
Per Åhgren | 4b3bc0f | 2017-12-20 15:26:13 +0100 | [diff] [blame] | 158 | bool filter_has_had_time_to_converge_ = false; |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 159 | bool initial_state_ = true; |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 160 | const float gain_rampup_increase_; |
Per Åhgren | 12eb858 | 2018-03-06 10:40:51 +0100 | [diff] [blame] | 161 | SuppressionGainUpperLimiter suppression_gain_limiter_; |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 162 | FilterAnalyzer filter_analyzer_; |
| 163 | bool use_linear_filter_output_ = false; |
| 164 | rtc::Optional<int> internal_delay_; |
| 165 | size_t diverged_blocks_ = 0; |
| 166 | bool filter_should_have_converged_ = false; |
| 167 | size_t blocks_since_converged_filter_; |
| 168 | size_t active_blocks_since_consistent_filter_estimate_; |
| 169 | bool converged_filter_seen_ = false; |
| 170 | bool consistent_filter_seen_ = false; |
| 171 | bool external_delay_seen_ = false; |
Per Åhgren | f3e2bf1 | 2018-03-22 10:15:59 +0100 | [diff] [blame] | 172 | size_t converged_filter_count_ = 0; |
| 173 | bool finite_erl_ = false; |
Per Åhgren | 8131eb0 | 2018-03-28 18:13:41 +0200 | [diff] [blame] | 174 | size_t active_blocks_since_converged_filter_ = 0; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 175 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 176 | RTC_DISALLOW_COPY_AND_ASSIGN(AecState); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | } // namespace webrtc |
| 180 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 181 | #endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |