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 | |
Henrik Kjellander | dca1e09 | 2017-07-01 16:42:22 +0200 | [diff] [blame] | 18 | #include "webrtc/base/array_view.h" |
| 19 | #include "webrtc/base/constructormagic.h" |
| 20 | #include "webrtc/base/optional.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 22 | #include "webrtc/modules/audio_processing/aec3/echo_path_variability.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 23 | #include "webrtc/modules/audio_processing/aec3/erl_estimator.h" |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 24 | #include "webrtc/modules/audio_processing/aec3/erle_estimator.h" |
| 25 | #include "webrtc/modules/audio_processing/aec3/render_buffer.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 | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 34 | explicit AecState(float echo_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. |
| 81 | // TODO(peah): Make this adaptive. |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 82 | float ReverbDecayFactor() const { return echo_decay_factor_; } |
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 | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 87 | // Updates the aec state. |
| 88 | void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 89 | adaptive_filter_frequency_response, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 90 | const rtc::Optional<size_t>& external_delay_samples, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 91 | const RenderBuffer& render_buffer, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 92 | const std::array<float, kFftLengthBy2Plus1>& E2_main, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 93 | const std::array<float, kFftLengthBy2Plus1>& Y2, |
| 94 | rtc::ArrayView<const float> x, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 95 | bool echo_leakage_detected); |
| 96 | |
| 97 | private: |
| 98 | static int instance_count_; |
| 99 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 100 | ErlEstimator erl_estimator_; |
| 101 | ErleEstimator erle_estimator_; |
| 102 | int echo_path_change_counter_; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 103 | size_t blocks_with_filter_adaptation_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 104 | bool usable_linear_estimate_ = false; |
| 105 | bool echo_leakage_detected_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 106 | bool capture_signal_saturation_ = false; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 107 | bool echo_saturation_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 108 | bool headset_detected_ = false; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 109 | float previous_max_sample_ = 0.f; |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 110 | bool force_zero_gain_ = false; |
peah | e52a203 | 2017-04-19 09:03:40 -0700 | [diff] [blame] | 111 | bool render_received_ = false; |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 112 | size_t force_zero_gain_counter_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 113 | rtc::Optional<size_t> filter_delay_; |
| 114 | rtc::Optional<size_t> external_delay_; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 115 | size_t blocks_since_last_saturation_ = 1000; |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 116 | const float echo_decay_factor_; |
| 117 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AecState); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace webrtc |
| 121 | |
| 122 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_ |