blob: 56fee2cf8f52088e2478b9f4e5117f70a32f72b0 [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
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
18#include "webrtc/base/array_view.h"
19#include "webrtc/base/constructormagic.h"
20#include "webrtc/base/optional.h"
21#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
22#include "webrtc/modules/audio_processing/aec3/echo_path_variability.h"
23#include "webrtc/modules/audio_processing/aec3/erle_estimator.h"
24#include "webrtc/modules/audio_processing/aec3/erl_estimator.h"
25#include "webrtc/modules/audio_processing/aec3/fft_buffer.h"
26
27namespace webrtc {
28
29class ApmDataDumper;
30
31// Handles the state and the conditions for the echo removal functionality.
32class AecState {
33 public:
34 AecState();
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
43 // Returns whether it is possible at all to use the model based echo removal
44 // functionalities.
45 bool ModelBasedAecFeasible() const { return model_based_aec_feasible_; }
46
47 // Returns whether the render signal is currently active.
48 bool ActiveRender() const { return active_render_counter_ > 0; }
49
peahebe77782017-02-27 07:29:21 -080050 // Returns whether the number of active render blocks since an echo path
51 // change.
52 size_t ActiveRenderBlocks() const { return active_render_blocks_; }
53
peah522d71b2017-02-23 05:16:26 -080054 // Returns the ERLE.
55 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
56 return erle_estimator_.Erle();
57 }
58
59 // Returns the ERL.
60 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
61 return erl_estimator_.Erl();
62 }
63
64 // Returns the delay estimate based on the linear filter.
65 rtc::Optional<size_t> FilterDelay() const { return filter_delay_; }
66
67 // Returns the externally provided delay.
68 rtc::Optional<size_t> ExternalDelay() const { return external_delay_; }
69
70 // Returns the bands where the linear filter is reliable.
71 const std::array<bool, kFftLengthBy2Plus1>& BandsWithReliableFilter() const {
72 return bands_with_reliable_filter_;
73 }
74
75 // Reports whether the filter is poorly aligned.
76 bool PoorlyAlignedFilter() const {
77 return FilterDelay() ? *FilterDelay() > 0.75f * filter_length_ : false;
78 }
79
80 // Returns the strength of the filter.
81 const std::array<float, kFftLengthBy2Plus1>& FilterEstimateStrength() const {
82 return filter_estimate_strength_;
83 }
84
85 // Returns whether the capture signal is saturated.
86 bool SaturatedCapture() const { return capture_signal_saturation_; }
87
88 // Updates the capture signal saturation.
89 void UpdateCaptureSaturation(bool capture_signal_saturation) {
90 capture_signal_saturation_ = capture_signal_saturation;
91 }
92
93 // Returns whether a probable headset setup has been detected.
94 bool HeadsetDetected() const { return headset_detected_; }
95
96 // Updates the aec state.
97 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
98 filter_frequency_response,
99 const rtc::Optional<size_t>& external_delay_samples,
100 const FftBuffer& X_buffer,
101 const std::array<float, kFftLengthBy2Plus1>& E2_main,
102 const std::array<float, kFftLengthBy2Plus1>& E2_shadow,
103 const std::array<float, kFftLengthBy2Plus1>& Y2,
104 rtc::ArrayView<const float> x,
105 const EchoPathVariability& echo_path_variability,
106 bool echo_leakage_detected);
107
108 private:
109 static int instance_count_;
110 std::unique_ptr<ApmDataDumper> data_dumper_;
111 ErlEstimator erl_estimator_;
112 ErleEstimator erle_estimator_;
113 int echo_path_change_counter_;
114 int active_render_counter_;
peahebe77782017-02-27 07:29:21 -0800115 size_t active_render_blocks_ = 0;
peah522d71b2017-02-23 05:16:26 -0800116 bool usable_linear_estimate_ = false;
117 bool echo_leakage_detected_ = false;
118 bool model_based_aec_feasible_ = false;
119 bool capture_signal_saturation_ = false;
120 bool headset_detected_ = false;
121 rtc::Optional<size_t> filter_delay_;
122 rtc::Optional<size_t> external_delay_;
123 std::array<bool, kFftLengthBy2Plus1> bands_with_reliable_filter_;
124 std::array<float, kFftLengthBy2Plus1> filter_estimate_strength_;
125 size_t filter_length_;
126
127 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
128};
129
130} // namespace webrtc
131
132#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_