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 | |
| 11 | #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <memory> |
| 16 | #include <vector> |
| 17 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 19 | #include "webrtc/modules/audio_processing/aec3/echo_path_variability.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/aec3/erl_estimator.h" |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/aec3/erle_estimator.h" |
| 22 | #include "webrtc/modules/audio_processing/aec3/render_buffer.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 23 | #include "webrtc/rtc_base/array_view.h" |
| 24 | #include "webrtc/rtc_base/constructormagic.h" |
| 25 | #include "webrtc/rtc_base/optional.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class ApmDataDumper; |
| 30 | |
| 31 | // Handles the state and the conditions for the echo removal functionality. |
| 32 | class AecState { |
| 33 | public: |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 34 | explicit AecState(float reverb_decay); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 35 | ~AecState(); |
| 36 | |
| 37 | // Returns whether the linear filter estimate is usable. |
| 38 | bool UsableLinearEstimate() const { return usable_linear_estimate_; } |
| 39 | |
| 40 | // Returns whether there has been echo leakage detected. |
| 41 | bool EchoLeakageDetected() const { return echo_leakage_detected_; } |
| 42 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 43 | // Returns whether the render signal is currently active. |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 44 | // TODO(peah): Deprecate this in an upcoming CL. |
| 45 | bool ActiveRender() const { return blocks_with_filter_adaptation_ > 200; } |
peah | ebe7778 | 2017-02-27 07:29:21 -0800 | [diff] [blame] | 46 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 47 | // Returns the ERLE. |
| 48 | const std::array<float, kFftLengthBy2Plus1>& Erle() const { |
| 49 | return erle_estimator_.Erle(); |
| 50 | } |
| 51 | |
| 52 | // Returns the ERL. |
| 53 | const std::array<float, kFftLengthBy2Plus1>& Erl() const { |
| 54 | return erl_estimator_.Erl(); |
| 55 | } |
| 56 | |
| 57 | // Returns the delay estimate based on the linear filter. |
| 58 | rtc::Optional<size_t> FilterDelay() const { return filter_delay_; } |
| 59 | |
| 60 | // Returns the externally provided delay. |
| 61 | rtc::Optional<size_t> ExternalDelay() const { return external_delay_; } |
| 62 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 63 | // Returns whether the capture signal is saturated. |
| 64 | bool SaturatedCapture() const { return capture_signal_saturation_; } |
| 65 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 66 | // Returns whether the echo signal is saturated. |
| 67 | bool SaturatedEcho() const { return echo_saturation_; } |
| 68 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 69 | // Updates the capture signal saturation. |
| 70 | void UpdateCaptureSaturation(bool capture_signal_saturation) { |
| 71 | capture_signal_saturation_ = capture_signal_saturation; |
| 72 | } |
| 73 | |
| 74 | // Returns whether a probable headset setup has been detected. |
| 75 | bool HeadsetDetected() const { return headset_detected_; } |
| 76 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 77 | // Takes appropriate action at an echo path change. |
| 78 | void HandleEchoPathChange(const EchoPathVariability& echo_path_variability); |
| 79 | |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 80 | // Returns the decay factor for the echo reverberation. |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 81 | float ReverbDecay() const { return reverb_decay_; } |
peah | 8942045 | 2017-04-07 06:13:39 -0700 | [diff] [blame] | 82 | |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 83 | // Returns whether the echo suppression gain should be forced to zero. |
| 84 | bool ForcedZeroGain() const { return force_zero_gain_; } |
| 85 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 86 | // Returns whether the echo in the capture signal is audible. |
| 87 | bool InaudibleEcho() const { return echo_audibility_.InaudibleEcho(); } |
| 88 | |
| 89 | // Updates the aec state with the AEC output signal. |
| 90 | void UpdateWithOutput(rtc::ArrayView<const float> e) { |
| 91 | echo_audibility_.UpdateWithOutput(e); |
| 92 | } |
| 93 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 94 | // Updates the aec state. |
| 95 | void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 96 | adaptive_filter_frequency_response, |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 97 | const std::array<float, kAdaptiveFilterTimeDomainLength>& |
| 98 | adaptive_filter_impulse_response, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 99 | const rtc::Optional<size_t>& external_delay_samples, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 100 | const RenderBuffer& render_buffer, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 101 | const std::array<float, kFftLengthBy2Plus1>& E2_main, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 102 | const std::array<float, kFftLengthBy2Plus1>& Y2, |
| 103 | rtc::ArrayView<const float> x, |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 104 | const std::array<float, kBlockSize>& s_main, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 105 | bool echo_leakage_detected); |
| 106 | |
| 107 | private: |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 108 | class EchoAudibility { |
| 109 | public: |
| 110 | void Update(rtc::ArrayView<const float> x, |
| 111 | const std::array<float, kBlockSize>& s); |
| 112 | void UpdateWithOutput(rtc::ArrayView<const float> e); |
| 113 | bool InaudibleEcho() const { return inaudible_echo_; } |
| 114 | |
| 115 | private: |
| 116 | float max_nearend_ = 0.f; |
| 117 | size_t max_nearend_counter_ = 0; |
| 118 | size_t low_farend_counter_ = 0; |
| 119 | bool inaudible_echo_ = false; |
| 120 | }; |
| 121 | |
| 122 | void UpdateReverb(const std::array<float, kAdaptiveFilterTimeDomainLength>& |
| 123 | impulse_response); |
| 124 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 125 | static int instance_count_; |
| 126 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 127 | ErlEstimator erl_estimator_; |
| 128 | ErleEstimator erle_estimator_; |
| 129 | int echo_path_change_counter_; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 130 | size_t blocks_with_filter_adaptation_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 131 | bool usable_linear_estimate_ = false; |
| 132 | bool echo_leakage_detected_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 133 | bool capture_signal_saturation_ = false; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 134 | bool echo_saturation_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 135 | bool headset_detected_ = false; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 136 | float previous_max_sample_ = 0.f; |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 137 | bool force_zero_gain_ = false; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 138 | bool render_received_ = false; |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 139 | size_t force_zero_gain_counter_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 140 | rtc::Optional<size_t> filter_delay_; |
| 141 | rtc::Optional<size_t> external_delay_; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 142 | size_t blocks_since_last_saturation_ = 1000; |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 143 | float reverb_decay_; |
| 144 | float reverb_decay_to_test_ = 0.9f; |
| 145 | float reverb_decay_candidate_ = 0.f; |
| 146 | float reverb_decay_candidate_residual_ = -1.f; |
| 147 | EchoAudibility echo_audibility_; |
| 148 | |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 149 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AecState); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | } // namespace webrtc |
| 153 | |
| 154 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |