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" |
| 19 | #include "api/optional.h" |
| 20 | #include "modules/audio_processing/aec3/aec3_common.h" |
| 21 | #include "modules/audio_processing/aec3/echo_path_variability.h" |
| 22 | #include "modules/audio_processing/aec3/erl_estimator.h" |
| 23 | #include "modules/audio_processing/aec3/erle_estimator.h" |
| 24 | #include "modules/audio_processing/aec3/render_buffer.h" |
| 25 | #include "modules/audio_processing/include/audio_processing.h" |
| 26 | #include "rtc_base/constructormagic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | class ApmDataDumper; |
| 31 | |
| 32 | // Handles the state and the conditions for the echo removal functionality. |
| 33 | class AecState { |
| 34 | public: |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 35 | explicit AecState(const AudioProcessing::Config::EchoCanceller3& config); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 36 | ~AecState(); |
| 37 | |
| 38 | // Returns whether the linear filter estimate is usable. |
| 39 | bool UsableLinearEstimate() const { return usable_linear_estimate_; } |
| 40 | |
| 41 | // Returns whether there has been echo leakage detected. |
| 42 | bool EchoLeakageDetected() const { return echo_leakage_detected_; } |
| 43 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 44 | // Returns whether the render signal is currently active. |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 45 | // TODO(peah): Deprecate this in an upcoming CL. |
| 46 | bool ActiveRender() const { return blocks_with_filter_adaptation_ > 200; } |
peah | ebe7778 | 2017-02-27 07:29:21 -0800 | [diff] [blame] | 47 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 48 | // Returns the ERLE. |
| 49 | const std::array<float, kFftLengthBy2Plus1>& Erle() const { |
| 50 | return erle_estimator_.Erle(); |
| 51 | } |
| 52 | |
| 53 | // Returns the ERL. |
| 54 | const std::array<float, kFftLengthBy2Plus1>& Erl() const { |
| 55 | return erl_estimator_.Erl(); |
| 56 | } |
| 57 | |
| 58 | // Returns the delay estimate based on the linear filter. |
| 59 | rtc::Optional<size_t> FilterDelay() const { return filter_delay_; } |
| 60 | |
| 61 | // Returns the externally provided delay. |
| 62 | rtc::Optional<size_t> ExternalDelay() const { return external_delay_; } |
| 63 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 64 | // Returns whether the capture signal is saturated. |
| 65 | bool SaturatedCapture() const { return capture_signal_saturation_; } |
| 66 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 67 | // Returns whether the echo signal is saturated. |
| 68 | bool SaturatedEcho() const { return echo_saturation_; } |
| 69 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 70 | // Updates the capture signal saturation. |
| 71 | void UpdateCaptureSaturation(bool capture_signal_saturation) { |
| 72 | capture_signal_saturation_ = capture_signal_saturation; |
| 73 | } |
| 74 | |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame^] | 75 | // Returns whether the transparent mode is active |
| 76 | bool TransparentMode() const { return transparent_mode_; } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 77 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 78 | // Takes appropriate action at an echo path change. |
| 79 | void HandleEchoPathChange(const EchoPathVariability& echo_path_variability); |
| 80 | |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 81 | // Returns the decay factor for the echo reverberation. |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 82 | float ReverbDecay() const { return reverb_decay_; } |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 83 | |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 84 | // Returns whether the echo suppression gain should be forced to zero. |
| 85 | bool ForcedZeroGain() const { return force_zero_gain_; } |
| 86 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 87 | // Returns whether the echo in the capture signal is audible. |
| 88 | bool InaudibleEcho() const { return echo_audibility_.InaudibleEcho(); } |
| 89 | |
| 90 | // Updates the aec state with the AEC output signal. |
| 91 | void UpdateWithOutput(rtc::ArrayView<const float> e) { |
| 92 | echo_audibility_.UpdateWithOutput(e); |
| 93 | } |
| 94 | |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame^] | 95 | // Returns whether the linear filter should have been able to adapt properly. |
| 96 | bool SufficientFilterUpdates() const { |
| 97 | return blocks_with_filter_adaptation_ >= kEchoPathChangeConvergenceBlocks; |
| 98 | } |
| 99 | |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 100 | // Returns whether the echo subtractor can be used to determine the residual |
| 101 | // echo. |
| 102 | bool LinearEchoEstimate() const { |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame^] | 103 | return UsableLinearEstimate() && !TransparentMode(); |
| 104 | } |
| 105 | |
| 106 | // Returns whether the AEC is in an initial state. |
| 107 | bool InitialState() const { |
| 108 | return capture_block_counter_ < 3 * kNumBlocksPerSecond; |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 109 | } |
| 110 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 111 | // Updates the aec state. |
| 112 | void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 113 | adaptive_filter_frequency_response, |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 114 | const std::array<float, kAdaptiveFilterTimeDomainLength>& |
| 115 | adaptive_filter_impulse_response, |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame^] | 116 | bool converged_filter, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 117 | const rtc::Optional<size_t>& external_delay_samples, |
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, |
| 121 | rtc::ArrayView<const float> x, |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 122 | const std::array<float, kBlockSize>& s_main, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 123 | bool echo_leakage_detected); |
| 124 | |
| 125 | private: |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 126 | class EchoAudibility { |
| 127 | public: |
| 128 | void Update(rtc::ArrayView<const float> x, |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame^] | 129 | const std::array<float, kBlockSize>& s, |
| 130 | bool converged_filter); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 131 | void UpdateWithOutput(rtc::ArrayView<const float> e); |
| 132 | bool InaudibleEcho() const { return inaudible_echo_; } |
| 133 | |
| 134 | private: |
| 135 | float max_nearend_ = 0.f; |
| 136 | size_t max_nearend_counter_ = 0; |
| 137 | size_t low_farend_counter_ = 0; |
| 138 | bool inaudible_echo_ = false; |
| 139 | }; |
| 140 | |
| 141 | void UpdateReverb(const std::array<float, kAdaptiveFilterTimeDomainLength>& |
| 142 | impulse_response); |
| 143 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 144 | static int instance_count_; |
| 145 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 146 | ErlEstimator erl_estimator_; |
| 147 | ErleEstimator erle_estimator_; |
Per Åhgren | 1b4059e | 2017-10-15 20:19:21 +0200 | [diff] [blame^] | 148 | size_t capture_block_counter_ = 0; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 149 | size_t blocks_with_filter_adaptation_ = 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; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 155 | float previous_max_sample_ = 0.f; |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 156 | bool force_zero_gain_ = false; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 157 | bool render_received_ = false; |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 158 | size_t force_zero_gain_counter_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 159 | rtc::Optional<size_t> filter_delay_; |
| 160 | rtc::Optional<size_t> external_delay_; |
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_; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 166 | const AudioProcessing::Config::EchoCanceller3 config_; |
| 167 | float reverb_decay_; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 168 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 169 | RTC_DISALLOW_COPY_AND_ASSIGN(AecState); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } // namespace webrtc |
| 173 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 174 | #endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |