blob: 56e6c3695e1108d22b8d5f0ecf69e1f1529db504 [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
Christian Schuldtf4e99db2018-03-01 11:32:50 +010014#include <math.h>
15
peah522d71b2017-02-23 05:16:26 -080016#include <algorithm>
17#include <memory>
18#include <vector>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010021#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/optional.h"
23#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren3ab308f2018-02-21 08:46:03 +010024#include "modules/audio_processing/aec3/delay_estimate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/aec3/echo_path_variability.h"
26#include "modules/audio_processing/aec3/erl_estimator.h"
27#include "modules/audio_processing/aec3/erle_estimator.h"
28#include "modules/audio_processing/aec3/render_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080030
31namespace webrtc {
32
33class ApmDataDumper;
34
35// Handles the state and the conditions for the echo removal functionality.
36class AecState {
37 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020038 explicit AecState(const EchoCanceller3Config& config);
peah522d71b2017-02-23 05:16:26 -080039 ~AecState();
40
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010041 // Returns whether the echo subtractor can be used to determine the residual
42 // echo.
peah522d71b2017-02-23 05:16:26 -080043 bool UsableLinearEstimate() const { return usable_linear_estimate_; }
44
45 // Returns whether there has been echo leakage detected.
46 bool EchoLeakageDetected() const { return echo_leakage_detected_; }
47
peah522d71b2017-02-23 05:16:26 -080048 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010049 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
peahebe77782017-02-27 07:29:21 -080050
peah522d71b2017-02-23 05:16:26 -080051 // Returns the ERLE.
52 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
53 return erle_estimator_.Erle();
54 }
55
Gustaf Ullberg332150d2017-11-22 14:17:39 +010056 // Returns the time-domain ERLE.
57 float ErleTimeDomain() const { return erle_estimator_.ErleTimeDomain(); }
58
peah522d71b2017-02-23 05:16:26 -080059 // Returns the ERL.
60 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
61 return erl_estimator_.Erl();
62 }
63
Gustaf Ullberg332150d2017-11-22 14:17:39 +010064 // Returns the time-domain ERL.
65 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
66
peah522d71b2017-02-23 05:16:26 -080067 // Returns the delay estimate based on the linear filter.
Per Åhgren0e6d2f52017-12-20 22:19:56 +010068 int FilterDelay() const { return filter_delay_; }
peah522d71b2017-02-23 05:16:26 -080069
peah522d71b2017-02-23 05:16:26 -080070 // Returns whether the capture signal is saturated.
71 bool SaturatedCapture() const { return capture_signal_saturation_; }
72
peah86afe9d2017-04-06 15:45:32 -070073 // Returns whether the echo signal is saturated.
74 bool SaturatedEcho() const { return echo_saturation_; }
75
Per Åhgren7ddd4632017-10-25 02:59:45 +020076 // Returns whether the echo path can saturate.
77 bool SaturatingEchoPath() const { return saturating_echo_path_; }
78
peah522d71b2017-02-23 05:16:26 -080079 // Updates the capture signal saturation.
80 void UpdateCaptureSaturation(bool capture_signal_saturation) {
81 capture_signal_saturation_ = capture_signal_saturation;
82 }
83
Per Åhgren1b4059e2017-10-15 20:19:21 +020084 // Returns whether the transparent mode is active
85 bool TransparentMode() const { return transparent_mode_; }
peah522d71b2017-02-23 05:16:26 -080086
peah86afe9d2017-04-06 15:45:32 -070087 // Takes appropriate action at an echo path change.
88 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
89
peah89420452017-04-07 06:13:39 -070090 // Returns the decay factor for the echo reverberation.
peah29103572017-07-11 02:54:02 -070091 float ReverbDecay() const { return reverb_decay_; }
peah89420452017-04-07 06:13:39 -070092
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010093 // Returns the upper limit for the echo suppression gain.
94 float SuppressionGainLimit() const { return suppressor_gain_limit_; }
peah6d822ad2017-04-10 13:52:14 -070095
peah29103572017-07-11 02:54:02 -070096 // Returns whether the echo in the capture signal is audible.
97 bool InaudibleEcho() const { return echo_audibility_.InaudibleEcho(); }
98
99 // Updates the aec state with the AEC output signal.
100 void UpdateWithOutput(rtc::ArrayView<const float> e) {
101 echo_audibility_.UpdateWithOutput(e);
102 }
103
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100104 // Returns whether the linear filter should have been able to properly adapt.
105 bool FilterHasHadTimeToConverge() const {
106 return filter_has_had_time_to_converge_;
107 }
Per Åhgren1b4059e2017-10-15 20:19:21 +0200108
Per Åhgrena98c8072018-01-15 19:17:16 +0100109 // Returns whether the filter adaptation is still in the initial state.
110 bool InitialState() const { return initial_state_; }
111
peah522d71b2017-02-23 05:16:26 -0800112 // Updates the aec state.
Per Åhgren3ab308f2018-02-21 08:46:03 +0100113 void Update(const rtc::Optional<DelayEstimate>& delay_estimate,
114 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700115 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100116 const std::vector<float>& adaptive_filter_impulse_response,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200117 bool converged_filter,
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,
peah29103572017-07-11 02:54:02 -0700121 const std::array<float, kBlockSize>& s_main,
peah522d71b2017-02-23 05:16:26 -0800122 bool echo_leakage_detected);
123
124 private:
peah29103572017-07-11 02:54:02 -0700125 class EchoAudibility {
126 public:
127 void Update(rtc::ArrayView<const float> x,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200128 const std::array<float, kBlockSize>& s,
129 bool converged_filter);
peah29103572017-07-11 02:54:02 -0700130 void UpdateWithOutput(rtc::ArrayView<const float> e);
131 bool InaudibleEcho() const { return inaudible_echo_; }
132
133 private:
134 float max_nearend_ = 0.f;
135 size_t max_nearend_counter_ = 0;
136 size_t low_farend_counter_ = 0;
137 bool inaudible_echo_ = false;
138 };
139
Per Åhgren09a718a2017-12-11 22:28:45 +0100140 void UpdateReverb(const std::vector<float>& impulse_response);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100141 bool DetectActiveRender(rtc::ArrayView<const float> x) const;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100142 void UpdateSuppressorGainLimit(bool render_activity);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100143 bool DetectEchoSaturation(rtc::ArrayView<const float> x);
peah29103572017-07-11 02:54:02 -0700144
peah522d71b2017-02-23 05:16:26 -0800145 static int instance_count_;
146 std::unique_ptr<ApmDataDumper> data_dumper_;
147 ErlEstimator erl_estimator_;
148 ErleEstimator erle_estimator_;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200149 size_t capture_block_counter_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100150 size_t blocks_with_proper_filter_adaptation_ = 0;
151 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800152 bool usable_linear_estimate_ = false;
153 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800154 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700155 bool echo_saturation_ = false;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200156 bool transparent_mode_ = false;
Per Åhgren63b494d2017-12-06 11:32:38 +0100157 float previous_max_sample_ = 0.f;
peahe52a2032017-04-19 09:03:40 -0700158 bool render_received_ = false;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100159 int realignment_counter_ = 0;
160 float suppressor_gain_limit_ = 1.f;
161 bool active_render_seen_ = false;
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100162 int filter_delay_ = 0;
peah86afe9d2017-04-06 15:45:32 -0700163 size_t blocks_since_last_saturation_ = 1000;
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100164 float tail_energy_ = 0.f;
165 float accumulated_nz_ = 0.f;
166 float accumulated_nn_ = 0.f;
167 float accumulated_count_ = 0.f;
168 size_t current_reverb_decay_section_ = 0;
169 size_t num_reverb_decay_sections_ = 0;
170 size_t num_reverb_decay_sections_next_ = 0;
171 bool found_end_of_reverb_decay_ = false;
172 bool main_filter_is_adapting_ = true;
173 std::array<float, kMaxAdaptiveFilterLength> block_energies_;
peah29103572017-07-11 02:54:02 -0700174 EchoAudibility echo_audibility_;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200175 const EchoCanceller3Config config_;
Per Åhgren09a718a2017-12-11 22:28:45 +0100176 std::vector<float> max_render_;
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100177 float reverb_decay_ = fabsf(config_.ep_strength.default_len);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200178 bool saturating_echo_path_ = false;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100179 bool filter_has_had_time_to_converge_ = false;
Per Åhgrena98c8072018-01-15 19:17:16 +0100180 bool initial_state_ = true;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100181 const float gain_rampup_increase_;
peah29103572017-07-11 02:54:02 -0700182
peah8cee56f2017-08-24 22:36:53 -0700183 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
peah522d71b2017-02-23 05:16:26 -0800184};
185
186} // namespace webrtc
187
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200188#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_