Debian Codegraph @

Xpjproject/third_party/webrtc_aec3/src/modules/audio_processing/aec3/aec_state.h

raw symbols SCIP
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 MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
13
14 #include <stddef.h>
15
16 #include <array>
17 #include <memory>
18 #include <vector>
19
20 #include "absl/types/optional.h"
21 #include "api/array_view.h"
22 #include "api/audio/echo_canceller3_config.h"
23 #include "modules/audio_processing/aec3/aec3_common.h"
24 #include "modules/audio_processing/aec3/delay_estimate.h"
25 #include "modules/audio_processing/aec3/echo_audibility.h"
26 #include "modules/audio_processing/aec3/echo_path_variability.h"
27 #include "modules/audio_processing/aec3/erl_estimator.h"
28 #include "modules/audio_processing/aec3/erle_estimator.h"
29 #include "modules/audio_processing/aec3/filter_analyzer.h"
30 #include "modules/audio_processing/aec3/render_buffer.h"
31 #include "modules/audio_processing/aec3/reverb_model_estimator.h"
32 #include "modules/audio_processing/aec3/subtractor_output.h"
33 #include "modules/audio_processing/aec3/subtractor_output_analyzer.h"
34 #include "modules/audio_processing/aec3/transparent_mode.h"
35
36 namespace webrtc {
37
38 class ApmDataDumper;
39
40 // Handles the state and the conditions for the echo removal functionality.
41 class AecState {
42 public:
43 AecState(const EchoCanceller3Config& config, size_t num_capture_channels);
44 ~AecState();
45
46 // Returns whether the echo subtractor can be used to determine the residual
47 // echo.
48 bool UsableLinearEstimate() const {
49 return filter_quality_state_.LinearFilterUsable() &&
50 config_.filter.use_linear_filter;
51 }
52
53 // Returns whether the echo subtractor output should be used as output.
54 bool UseLinearFilterOutput() const {
55 return filter_quality_state_.LinearFilterUsable() &&
56 config_.filter.use_linear_filter;
57 }
58
59 // Returns whether the render signal is currently active.
60 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
61
62 // Returns the appropriate scaling of the residual echo to match the
63 // audibility.
64 void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const;
65
66 // Returns whether the stationary properties of the signals are used in the
67 // aec.
68 bool UseStationarityProperties() const {
69 return config_.echo_audibility.use_stationarity_properties;
70 }
71
72 // Returns the ERLE.
73 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle(
74 bool onset_compensated) const {
75 return erle_estimator_.Erle(onset_compensated);
76 }
77
78 // Returns the fullband ERLE estimate in log2 units.
79 float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
80
81 // Returns the ERL.
82 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
83 return erl_estimator_.Erl();
84 }
85
86 // Returns the time-domain ERL.
87 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
88
89 // Returns the delay estimate based on the linear filter.
90 int MinDirectPathFilterDelay() const {
91 return delay_state_.MinDirectPathFilterDelay();
92 }
93
94 // Returns whether the capture signal is saturated.
95 bool SaturatedCapture() const { return capture_signal_saturation_; }
96
97 // Returns whether the echo signal is saturated.
98 bool SaturatedEcho() const { return saturation_detector_.SaturatedEcho(); }
99
100 // Updates the capture signal saturation.
101 void UpdateCaptureSaturation(bool capture_signal_saturation) {
102 capture_signal_saturation_ = capture_signal_saturation;
103 }
104
105 // Returns whether the transparent mode is active
106 bool TransparentModeActive() const {
107 return transparent_state_ && transparent_state_->Active();
108 }
109
110 // Takes appropriate action at an echo path change.
111 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
112
113 // Returns the decay factor for the echo reverberation.
114 float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); }
115
116 // Return the frequency response of the reverberant echo.
117 rtc::ArrayView<const float> GetReverbFrequencyResponse() const {
118 return reverb_model_estimator_.GetReverbFrequencyResponse();
119 }
120
121 // Returns whether the transition for going out of the initial stated has
122 // been triggered.
123 bool TransitionTriggered() const {
124 return initial_state_.TransitionTriggered();
125 }
126
127 // Updates the aec state.
128 // TODO(bugs.webrtc.org/10913): Compute multi-channel ERL.
129 void Update(
130 const absl::optional<DelayEstimate>& external_delay,
131 rtc::ArrayView<const std::vector<std::array<float, kFftLengthBy2Plus1>>>
132 adaptive_filter_frequency_responses,
133 rtc::ArrayView<const std::vector<float>>
134 adaptive_filter_impulse_responses,
135 const RenderBuffer& render_buffer,
136 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2_refined,
137 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2,
138 rtc::ArrayView<const SubtractorOutput> subtractor_output);
139
140 // Returns filter length in blocks.
141 int FilterLengthBlocks() const {
142 // All filters have the same length, so arbitrarily return channel 0 length.
143 return filter_analyzer_.FilterLengthBlocks();
144 }
145
146 private:
147 static int instance_count_;
148 std::unique_ptr<ApmDataDumper> data_dumper_;
149 const EchoCanceller3Config config_;
150 const size_t num_capture_channels_;
151 const bool deactivate_initial_state_reset_at_echo_path_change_;
152 const bool full_reset_at_echo_path_change_;
153 const bool subtractor_analyzer_reset_at_echo_path_change_;
154
155 // Class for controlling the transition from the intial state, which in turn
156 // controls when the filter parameters for the initial state should be used.
157 class InitialState {
158 public:
159 explicit InitialState(const EchoCanceller3Config& config);
160 // Resets the state to again begin in the initial state.
161 void Reset();
162
163 // Updates the state based on new data.
164 void Update(bool active_render, bool saturated_capture);
165
166 // Returns whether the initial state is active or not.
167 bool InitialStateActive() const { return initial_state_; }
168
169 // Returns that the transition from the initial state has was started.
170 bool TransitionTriggered() const { return transition_triggered_; }
171
172 private:
173 const bool conservative_initial_phase_;
174 const float initial_state_seconds_;
175 bool transition_triggered_ = false;
176 bool initial_state_ = true;
177 size_t strong_not_saturated_render_blocks_ = 0;
178 } initial_state_;
179
180 // Class for choosing the direct-path delay relative to the beginning of the
181 // filter, as well as any other data related to the delay used within
182 // AecState.
183 class FilterDelay {
184 public:
185 FilterDelay(const EchoCanceller3Config& config,
186 size_t num_capture_channels);
187
188 // Returns whether an external delay has been reported to the AecState (from
189 // the delay estimator).
190 bool ExternalDelayReported() const { return external_delay_reported_; }
191
192 // Returns the delay in blocks relative to the beginning of the filter that
193 // corresponds to the direct path of the echo.
194 rtc::ArrayView<const int> DirectPathFilterDelays() const {
195 return filter_delays_blocks_;
196 }
197
198 // Returns the minimum delay among the direct path delays relative to the
199 // beginning of the filter
200 int MinDirectPathFilterDelay() const { return min_filter_delay_; }
201
202 // Updates the delay estimates based on new data.
203 void Update(
204 rtc::ArrayView<const int> analyzer_filter_delay_estimates_blocks,
205 const absl::optional<DelayEstimate>& external_delay,
206 size_t blocks_with_proper_filter_adaptation);
207
208 private:
209 const int delay_headroom_blocks_;
210 bool external_delay_reported_ = false;
211 std::vector<int> filter_delays_blocks_;
212 int min_filter_delay_;
213 absl::optional<DelayEstimate> external_delay_;
214 } delay_state_;
215
216 // Classifier for toggling transparent mode when there is no echo.
217 std::unique_ptr<TransparentMode> transparent_state_;
218
219 // Class for analyzing how well the linear filter is, and can be expected to,
220 // perform on the current signals. The purpose of this is for using to
221 // select the echo suppression functionality as well as the input to the echo
222 // suppressor.
223 class FilteringQualityAnalyzer {
224 public:
225 FilteringQualityAnalyzer(const EchoCanceller3Config& config,
226 size_t num_capture_channels);
227
228 // Returns whether the linear filter can be used for the echo
229 // canceller output.
230 bool LinearFilterUsable() const { return overall_usable_linear_estimates_; }
231
232 // Returns whether an individual filter output can be used for the echo
233 // canceller output.
234 const std::vector<bool>& UsableLinearFilterOutputs() const {
235 return usable_linear_filter_estimates_;
236 }
237
238 // Resets the state of the analyzer.
239 void Reset();
240
241 // Updates the analysis based on new data.
242 void Update(bool active_render,
243 bool transparent_mode,
244 bool saturated_capture,
245 const absl::optional<DelayEstimate>& external_delay,
246 bool any_filter_converged);
247
248 private:
249 const bool use_linear_filter_;
250 bool overall_usable_linear_estimates_ = false;
251 size_t filter_update_blocks_since_reset_ = 0;
252 size_t filter_update_blocks_since_start_ = 0;
253 bool convergence_seen_ = false;
254 std::vector<bool> usable_linear_filter_estimates_;
255 } filter_quality_state_;
256
257 // Class for detecting whether the echo is to be considered to be
258 // saturated.
259 class SaturationDetector {
260 public:
261 // Returns whether the echo is to be considered saturated.
262 bool SaturatedEcho() const { return saturated_echo_; }
263
264 // Updates the detection decision based on new data.
265 void Update(rtc::ArrayView<const std::vector<float>> x,
266 bool saturated_capture,
267 bool usable_linear_estimate,
268 rtc::ArrayView<const SubtractorOutput> subtractor_output,
269 float echo_path_gain);
270
271 private:
272 bool saturated_echo_ = false;
273 } saturation_detector_;
274
275 ErlEstimator erl_estimator_;
276 ErleEstimator erle_estimator_;
277 size_t strong_not_saturated_render_blocks_ = 0;
278 size_t blocks_with_active_render_ = 0;
279 bool capture_signal_saturation_ = false;
280 FilterAnalyzer filter_analyzer_;
281 EchoAudibility echo_audibility_;
282 ReverbModelEstimator reverb_model_estimator_;
283 ReverbModel avg_render_reverb_;
284 SubtractorOutputAnalyzer subtractor_output_analyzer_;
285 };
286
287 } // namespace webrtc
288
289 #endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_