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