blob: e2039ad4dbf5314404e4cdd5244fbca8564002ba [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:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020035 explicit AecState(const EchoCanceller3Config& config);
peah522d71b2017-02-23 05:16:26 -080036 ~AecState();
37
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010038 // Returns whether the echo subtractor can be used to determine the residual
39 // echo.
peah522d71b2017-02-23 05:16:26 -080040 bool UsableLinearEstimate() const { return usable_linear_estimate_; }
41
42 // Returns whether there has been echo leakage detected.
43 bool EchoLeakageDetected() const { return echo_leakage_detected_; }
44
peah522d71b2017-02-23 05:16:26 -080045 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010046 bool ActiveRender() const { return blocks_with_active_render_ > 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
Gustaf Ullberg332150d2017-11-22 14:17:39 +010053 // Returns the time-domain ERLE.
54 float ErleTimeDomain() const { return erle_estimator_.ErleTimeDomain(); }
55
peah522d71b2017-02-23 05:16:26 -080056 // Returns the ERL.
57 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
58 return erl_estimator_.Erl();
59 }
60
Gustaf Ullberg332150d2017-11-22 14:17:39 +010061 // Returns the time-domain ERL.
62 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
63
peah522d71b2017-02-23 05:16:26 -080064 // Returns the delay estimate based on the linear filter.
Per Åhgren0e6d2f52017-12-20 22:19:56 +010065 int FilterDelay() const { return filter_delay_; }
peah522d71b2017-02-23 05:16:26 -080066
peah522d71b2017-02-23 05:16:26 -080067 // Returns whether the capture signal is saturated.
68 bool SaturatedCapture() const { return capture_signal_saturation_; }
69
peah86afe9d2017-04-06 15:45:32 -070070 // Returns whether the echo signal is saturated.
71 bool SaturatedEcho() const { return echo_saturation_; }
72
Per Åhgren7ddd4632017-10-25 02:59:45 +020073 // Returns whether the echo path can saturate.
74 bool SaturatingEchoPath() const { return saturating_echo_path_; }
75
peah522d71b2017-02-23 05:16:26 -080076 // Updates the capture signal saturation.
77 void UpdateCaptureSaturation(bool capture_signal_saturation) {
78 capture_signal_saturation_ = capture_signal_saturation;
79 }
80
Per Åhgren1b4059e2017-10-15 20:19:21 +020081 // Returns whether the transparent mode is active
82 bool TransparentMode() const { return transparent_mode_; }
peah522d71b2017-02-23 05:16:26 -080083
peah86afe9d2017-04-06 15:45:32 -070084 // Takes appropriate action at an echo path change.
85 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
86
peah89420452017-04-07 06:13:39 -070087 // Returns the decay factor for the echo reverberation.
peah29103572017-07-11 02:54:02 -070088 float ReverbDecay() const { return reverb_decay_; }
peah89420452017-04-07 06:13:39 -070089
peah6d822ad2017-04-10 13:52:14 -070090 // Returns whether the echo suppression gain should be forced to zero.
91 bool ForcedZeroGain() const { return force_zero_gain_; }
92
peah29103572017-07-11 02:54:02 -070093 // Returns whether the echo in the capture signal is audible.
94 bool InaudibleEcho() const { return echo_audibility_.InaudibleEcho(); }
95
96 // Updates the aec state with the AEC output signal.
97 void UpdateWithOutput(rtc::ArrayView<const float> e) {
98 echo_audibility_.UpdateWithOutput(e);
99 }
100
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100101 // Returns whether the linear filter should have been able to properly adapt.
102 bool FilterHasHadTimeToConverge() const {
103 return filter_has_had_time_to_converge_;
104 }
Per Åhgren1b4059e2017-10-15 20:19:21 +0200105
Per Åhgrena98c8072018-01-15 19:17:16 +0100106 // Returns whether the filter adaptation is still in the initial state.
107 bool InitialState() const { return initial_state_; }
108
peah522d71b2017-02-23 05:16:26 -0800109 // Updates the aec state.
110 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700111 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100112 const std::vector<float>& adaptive_filter_impulse_response,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200113 bool converged_filter,
peah86afe9d2017-04-06 15:45:32 -0700114 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800115 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800116 const std::array<float, kFftLengthBy2Plus1>& Y2,
peah29103572017-07-11 02:54:02 -0700117 const std::array<float, kBlockSize>& s_main,
peah522d71b2017-02-23 05:16:26 -0800118 bool echo_leakage_detected);
119
120 private:
peah29103572017-07-11 02:54:02 -0700121 class EchoAudibility {
122 public:
123 void Update(rtc::ArrayView<const float> x,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200124 const std::array<float, kBlockSize>& s,
125 bool converged_filter);
peah29103572017-07-11 02:54:02 -0700126 void UpdateWithOutput(rtc::ArrayView<const float> e);
127 bool InaudibleEcho() const { return inaudible_echo_; }
128
129 private:
130 float max_nearend_ = 0.f;
131 size_t max_nearend_counter_ = 0;
132 size_t low_farend_counter_ = 0;
133 bool inaudible_echo_ = false;
134 };
135
Per Åhgren09a718a2017-12-11 22:28:45 +0100136 void UpdateReverb(const std::vector<float>& impulse_response);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100137 bool DetectActiveRender(rtc::ArrayView<const float> x) const;
138 bool DetectEchoSaturation(rtc::ArrayView<const float> x);
peah29103572017-07-11 02:54:02 -0700139
peah522d71b2017-02-23 05:16:26 -0800140 static int instance_count_;
141 std::unique_ptr<ApmDataDumper> data_dumper_;
142 ErlEstimator erl_estimator_;
143 ErleEstimator erle_estimator_;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200144 size_t capture_block_counter_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100145 size_t blocks_with_proper_filter_adaptation_ = 0;
146 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800147 bool usable_linear_estimate_ = false;
148 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800149 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700150 bool echo_saturation_ = false;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200151 bool transparent_mode_ = false;
Per Åhgren63b494d2017-12-06 11:32:38 +0100152 float previous_max_sample_ = 0.f;
peah6d822ad2017-04-10 13:52:14 -0700153 bool force_zero_gain_ = false;
peahe52a2032017-04-19 09:03:40 -0700154 bool render_received_ = false;
peah6d822ad2017-04-10 13:52:14 -0700155 size_t force_zero_gain_counter_ = 0;
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100156 int filter_delay_ = 0;
peah86afe9d2017-04-06 15:45:32 -0700157 size_t blocks_since_last_saturation_ = 1000;
peah29103572017-07-11 02:54:02 -0700158 float reverb_decay_to_test_ = 0.9f;
159 float reverb_decay_candidate_ = 0.f;
160 float reverb_decay_candidate_residual_ = -1.f;
161 EchoAudibility echo_audibility_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200162 const EchoCanceller3Config config_;
Per Åhgren09a718a2017-12-11 22:28:45 +0100163 std::vector<float> max_render_;
peah8cee56f2017-08-24 22:36:53 -0700164 float reverb_decay_;
Per Åhgren7ddd4632017-10-25 02:59:45 +0200165 bool saturating_echo_path_ = false;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100166 bool filter_has_had_time_to_converge_ = false;
Per Åhgrena98c8072018-01-15 19:17:16 +0100167 bool initial_state_ = true;
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_