blob: 358c74d8e0b3e6f4522b02eea57132304af36990 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
peah522d71b2017-02-23 05:16:26 -080013
14#include <algorithm>
15#include <memory>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#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"
peah522d71b2017-02-23 05:16:26 -080027
28namespace webrtc {
29
30class ApmDataDumper;
31
32// Handles the state and the conditions for the echo removal functionality.
33class AecState {
34 public:
peah8cee56f2017-08-24 22:36:53 -070035 explicit AecState(const AudioProcessing::Config::EchoCanceller3& config);
peah522d71b2017-02-23 05:16:26 -080036 ~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
peah522d71b2017-02-23 05:16:26 -080044 // Returns whether the render signal is currently active.
peahe52a2032017-04-19 09:03:40 -070045 // TODO(peah): Deprecate this in an upcoming CL.
46 bool ActiveRender() const { return blocks_with_filter_adaptation_ > 200; }
peahebe77782017-02-27 07:29:21 -080047
peah522d71b2017-02-23 05:16:26 -080048 // 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
peah522d71b2017-02-23 05:16:26 -080064 // Returns whether the capture signal is saturated.
65 bool SaturatedCapture() const { return capture_signal_saturation_; }
66
peah86afe9d2017-04-06 15:45:32 -070067 // Returns whether the echo signal is saturated.
68 bool SaturatedEcho() const { return echo_saturation_; }
69
peah522d71b2017-02-23 05:16:26 -080070 // Updates the capture signal saturation.
71 void UpdateCaptureSaturation(bool capture_signal_saturation) {
72 capture_signal_saturation_ = capture_signal_saturation;
73 }
74
Per Åhgren1b4059e2017-10-15 20:19:21 +020075 // Returns whether the transparent mode is active
76 bool TransparentMode() const { return transparent_mode_; }
peah522d71b2017-02-23 05:16:26 -080077
peah86afe9d2017-04-06 15:45:32 -070078 // Takes appropriate action at an echo path change.
79 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
80
peah89420452017-04-07 06:13:39 -070081 // Returns the decay factor for the echo reverberation.
peah29103572017-07-11 02:54:02 -070082 float ReverbDecay() const { return reverb_decay_; }
peah89420452017-04-07 06:13:39 -070083
peah6d822ad2017-04-10 13:52:14 -070084 // Returns whether the echo suppression gain should be forced to zero.
85 bool ForcedZeroGain() const { return force_zero_gain_; }
86
peah29103572017-07-11 02:54:02 -070087 // 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 Åhgren1b4059e2017-10-15 20:19:21 +020095 // 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 Åhgrenc65ce782017-10-09 13:01:39 +0200100 // Returns whether the echo subtractor can be used to determine the residual
101 // echo.
102 bool LinearEchoEstimate() const {
Per Åhgren1b4059e2017-10-15 20:19:21 +0200103 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 Åhgrenc65ce782017-10-09 13:01:39 +0200109 }
110
peah522d71b2017-02-23 05:16:26 -0800111 // Updates the aec state.
112 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700113 adaptive_filter_frequency_response,
peah29103572017-07-11 02:54:02 -0700114 const std::array<float, kAdaptiveFilterTimeDomainLength>&
115 adaptive_filter_impulse_response,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200116 bool converged_filter,
peah522d71b2017-02-23 05:16:26 -0800117 const rtc::Optional<size_t>& external_delay_samples,
peah86afe9d2017-04-06 15:45:32 -0700118 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800119 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800120 const std::array<float, kFftLengthBy2Plus1>& Y2,
121 rtc::ArrayView<const float> x,
peah29103572017-07-11 02:54:02 -0700122 const std::array<float, kBlockSize>& s_main,
peah522d71b2017-02-23 05:16:26 -0800123 bool echo_leakage_detected);
124
125 private:
peah29103572017-07-11 02:54:02 -0700126 class EchoAudibility {
127 public:
128 void Update(rtc::ArrayView<const float> x,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200129 const std::array<float, kBlockSize>& s,
130 bool converged_filter);
peah29103572017-07-11 02:54:02 -0700131 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
peah522d71b2017-02-23 05:16:26 -0800144 static int instance_count_;
145 std::unique_ptr<ApmDataDumper> data_dumper_;
146 ErlEstimator erl_estimator_;
147 ErleEstimator erle_estimator_;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200148 size_t capture_block_counter_ = 0;
peahe52a2032017-04-19 09:03:40 -0700149 size_t blocks_with_filter_adaptation_ = 0;
peah522d71b2017-02-23 05:16:26 -0800150 bool usable_linear_estimate_ = false;
151 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800152 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700153 bool echo_saturation_ = false;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200154 bool transparent_mode_ = false;
peah86afe9d2017-04-06 15:45:32 -0700155 float previous_max_sample_ = 0.f;
peah6d822ad2017-04-10 13:52:14 -0700156 bool force_zero_gain_ = false;
peahe52a2032017-04-19 09:03:40 -0700157 bool render_received_ = false;
peah6d822ad2017-04-10 13:52:14 -0700158 size_t force_zero_gain_counter_ = 0;
peah522d71b2017-02-23 05:16:26 -0800159 rtc::Optional<size_t> filter_delay_;
160 rtc::Optional<size_t> external_delay_;
peah86afe9d2017-04-06 15:45:32 -0700161 size_t blocks_since_last_saturation_ = 1000;
peah29103572017-07-11 02:54:02 -0700162 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_;
peah8cee56f2017-08-24 22:36:53 -0700166 const AudioProcessing::Config::EchoCanceller3 config_;
167 float reverb_decay_;
peah29103572017-07-11 02:54:02 -0700168
peah8cee56f2017-08-24 22:36:53 -0700169 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
peah522d71b2017-02-23 05:16:26 -0800170};
171
172} // namespace webrtc
173
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200174#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_