peah | c18cc3a | 2017-02-23 13:16:26 | [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 | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 19 | #include "webrtc/modules/audio_processing/aec3/echo_path_variability.h" |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/aec3/erl_estimator.h" |
peah | 22c5fa5 | 2017-04-05 21:18:07 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/aec3/erle_estimator.h" |
| 22 | #include "webrtc/modules/audio_processing/aec3/render_buffer.h" |
peah | 47c96e5 | 2017-08-25 05:36:53 | [diff] [blame] | 23 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 24 | #include "webrtc/rtc_base/array_view.h" |
| 25 | #include "webrtc/rtc_base/constructormagic.h" |
| 26 | #include "webrtc/rtc_base/optional.h" |
peah | c18cc3a | 2017-02-23 13:16:26 | [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 | 47c96e5 | 2017-08-25 05:36:53 | [diff] [blame] | 35 | explicit AecState(const AudioProcessing::Config::EchoCanceller3& config); |
peah | c18cc3a | 2017-02-23 13:16:26 | [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 | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 44 | // Returns whether the render signal is currently active. |
peah | 2a9d40c | 2017-04-19 16:03:40 | [diff] [blame] | 45 | // TODO(peah): Deprecate this in an upcoming CL. |
| 46 | bool ActiveRender() const { return blocks_with_filter_adaptation_ > 200; } |
peah | 50fb993 | 2017-02-27 15:29:21 | [diff] [blame] | 47 | |
peah | c18cc3a | 2017-02-23 13:16:26 | [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 | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 64 | // Returns whether the capture signal is saturated. |
| 65 | bool SaturatedCapture() const { return capture_signal_saturation_; } |
| 66 | |
peah | d149fca | 2017-04-06 22:45:32 | [diff] [blame] | 67 | // Returns whether the echo signal is saturated. |
| 68 | bool SaturatedEcho() const { return echo_saturation_; } |
| 69 | |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 70 | // Updates the capture signal saturation. |
| 71 | void UpdateCaptureSaturation(bool capture_signal_saturation) { |
| 72 | capture_signal_saturation_ = capture_signal_saturation; |
| 73 | } |
| 74 | |
| 75 | // Returns whether a probable headset setup has been detected. |
| 76 | bool HeadsetDetected() const { return headset_detected_; } |
| 77 | |
peah | d149fca | 2017-04-06 22:45:32 | [diff] [blame] | 78 | // Takes appropriate action at an echo path change. |
| 79 | void HandleEchoPathChange(const EchoPathVariability& echo_path_variability); |
| 80 | |
peah | 05fb319 | 2017-04-07 13:13:39 | [diff] [blame] | 81 | // Returns the decay factor for the echo reverberation. |
peah | 6552e11 | 2017-07-11 09:54:02 | [diff] [blame] | 82 | float ReverbDecay() const { return reverb_decay_; } |
peah | 05fb319 | 2017-04-07 13:13:39 | [diff] [blame] | 83 | |
peah | 4f6d570 | 2017-04-10 20:52:14 | [diff] [blame] | 84 | // Returns whether the echo suppression gain should be forced to zero. |
| 85 | bool ForcedZeroGain() const { return force_zero_gain_; } |
| 86 | |
peah | 6552e11 | 2017-07-11 09:54:02 | [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 | |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 95 | // Updates the aec state. |
| 96 | void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
peah | d149fca | 2017-04-06 22:45:32 | [diff] [blame] | 97 | adaptive_filter_frequency_response, |
peah | 6552e11 | 2017-07-11 09:54:02 | [diff] [blame] | 98 | const std::array<float, kAdaptiveFilterTimeDomainLength>& |
| 99 | adaptive_filter_impulse_response, |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 100 | const rtc::Optional<size_t>& external_delay_samples, |
peah | d149fca | 2017-04-06 22:45:32 | [diff] [blame] | 101 | const RenderBuffer& render_buffer, |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 102 | const std::array<float, kFftLengthBy2Plus1>& E2_main, |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 103 | const std::array<float, kFftLengthBy2Plus1>& Y2, |
| 104 | rtc::ArrayView<const float> x, |
peah | 6552e11 | 2017-07-11 09:54:02 | [diff] [blame] | 105 | const std::array<float, kBlockSize>& s_main, |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 106 | bool echo_leakage_detected); |
| 107 | |
| 108 | private: |
peah | 6552e11 | 2017-07-11 09:54:02 | [diff] [blame] | 109 | class EchoAudibility { |
| 110 | public: |
| 111 | void Update(rtc::ArrayView<const float> x, |
| 112 | const std::array<float, kBlockSize>& s); |
| 113 | void UpdateWithOutput(rtc::ArrayView<const float> e); |
| 114 | bool InaudibleEcho() const { return inaudible_echo_; } |
| 115 | |
| 116 | private: |
| 117 | float max_nearend_ = 0.f; |
| 118 | size_t max_nearend_counter_ = 0; |
| 119 | size_t low_farend_counter_ = 0; |
| 120 | bool inaudible_echo_ = false; |
| 121 | }; |
| 122 | |
| 123 | void UpdateReverb(const std::array<float, kAdaptiveFilterTimeDomainLength>& |
| 124 | impulse_response); |
| 125 | |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 126 | static int instance_count_; |
| 127 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 128 | ErlEstimator erl_estimator_; |
| 129 | ErleEstimator erle_estimator_; |
| 130 | int echo_path_change_counter_; |
peah | 2a9d40c | 2017-04-19 16:03:40 | [diff] [blame] | 131 | size_t blocks_with_filter_adaptation_ = 0; |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 132 | bool usable_linear_estimate_ = false; |
| 133 | bool echo_leakage_detected_ = false; |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 134 | bool capture_signal_saturation_ = false; |
peah | d149fca | 2017-04-06 22:45:32 | [diff] [blame] | 135 | bool echo_saturation_ = false; |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 136 | bool headset_detected_ = false; |
peah | d149fca | 2017-04-06 22:45:32 | [diff] [blame] | 137 | float previous_max_sample_ = 0.f; |
peah | 4f6d570 | 2017-04-10 20:52:14 | [diff] [blame] | 138 | bool force_zero_gain_ = false; |
peah | 2a9d40c | 2017-04-19 16:03:40 | [diff] [blame] | 139 | bool render_received_ = false; |
peah | 4f6d570 | 2017-04-10 20:52:14 | [diff] [blame] | 140 | size_t force_zero_gain_counter_ = 0; |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 141 | rtc::Optional<size_t> filter_delay_; |
| 142 | rtc::Optional<size_t> external_delay_; |
peah | d149fca | 2017-04-06 22:45:32 | [diff] [blame] | 143 | size_t blocks_since_last_saturation_ = 1000; |
peah | 6552e11 | 2017-07-11 09:54:02 | [diff] [blame] | 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_; |
peah | 47c96e5 | 2017-08-25 05:36:53 | [diff] [blame] | 148 | const AudioProcessing::Config::EchoCanceller3 config_; |
| 149 | float reverb_decay_; |
peah | 6552e11 | 2017-07-11 09:54:02 | [diff] [blame] | 150 | |
peah | 47c96e5 | 2017-08-25 05:36:53 | [diff] [blame] | 151 | RTC_DISALLOW_COPY_AND_ASSIGN(AecState); |
peah | c18cc3a | 2017-02-23 13:16:26 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | } // namespace webrtc |
| 155 | |
| 156 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |