| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 41 |
class AecState { |
| 42 |
public: |
| 43 |
AecState(const EchoCanceller3Config& config, size_t num_capture_channels); |
| 44 |
~AecState(); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
bool UsableLinearEstimate() const { |
| 49 |
return filter_quality_state_.LinearFilterUsable() && |
| 50 |
config_.filter.use_linear_filter; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
bool UseLinearFilterOutput() const { |
| 55 |
return filter_quality_state_.LinearFilterUsable() && |
| 56 |
config_.filter.use_linear_filter; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
bool ActiveRender() const { return blocks_with_active_render_ > 200; } |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
bool UseStationarityProperties() const { |
| 69 |
return config_.echo_audibility.use_stationarity_properties; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle( |
| 74 |
bool onset_compensated) const { |
| 75 |
return erle_estimator_.Erle(onset_compensated); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); } |
| 80 |
|
| 81 |
|
| 82 |
const std::array<float, kFftLengthBy2Plus1>& Erl() const { |
| 83 |
return erl_estimator_.Erl(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); } |
| 88 |
|
| 89 |
|
| 90 |
int MinDirectPathFilterDelay() const { |
| 91 |
return delay_state_.MinDirectPathFilterDelay(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
bool SaturatedCapture() const { return capture_signal_saturation_; } |
| 96 |
|
| 97 |
|
| 98 |
bool SaturatedEcho() const { return saturation_detector_.SaturatedEcho(); } |
| 99 |
|
| 100 |
|
| 101 |
void UpdateCaptureSaturation(bool capture_signal_saturation) { |
| 102 |
capture_signal_saturation_ = capture_signal_saturation; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
bool TransparentModeActive() const { |
| 107 |
return transparent_state_ && transparent_state_->Active(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
void HandleEchoPathChange(const EchoPathVariability& echo_path_variability); |
| 112 |
|
| 113 |
|
| 114 |
float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); } |
| 115 |
|
| 116 |
|
| 117 |
rtc::ArrayView<const float> GetReverbFrequencyResponse() const { |
| 118 |
return reverb_model_estimator_.GetReverbFrequencyResponse(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
bool TransitionTriggered() const { |
| 124 |
return initial_state_.TransitionTriggered(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 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 |
|
| 141 |
int FilterLengthBlocks() const { |
| 142 |
|
| 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 |
|
| 156 |
|
| 157 |
class InitialState { |
| 158 |
public: |
| 159 |
explicit InitialState(const EchoCanceller3Config& config); |
| 160 |
|
| 161 |
void Reset(); |
| 162 |
|
| 163 |
|
| 164 |
void Update(bool active_render, bool saturated_capture); |
| 165 |
|
| 166 |
|
| 167 |
bool InitialStateActive() const { return initial_state_; } |
| 168 |
|
| 169 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
class FilterDelay { |
| 184 |
public: |
| 185 |
FilterDelay(const EchoCanceller3Config& config, |
| 186 |
size_t num_capture_channels); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
bool ExternalDelayReported() const { return external_delay_reported_; } |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
rtc::ArrayView<const int> DirectPathFilterDelays() const { |
| 195 |
return filter_delays_blocks_; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
int MinDirectPathFilterDelay() const { return min_filter_delay_; } |
| 201 |
|
| 202 |
|
| 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 |
|
| 217 |
std::unique_ptr<TransparentMode> transparent_state_; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
class FilteringQualityAnalyzer { |
| 224 |
public: |
| 225 |
FilteringQualityAnalyzer(const EchoCanceller3Config& config, |
| 226 |
size_t num_capture_channels); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
bool LinearFilterUsable() const { return overall_usable_linear_estimates_; } |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
const std::vector<bool>& UsableLinearFilterOutputs() const { |
| 235 |
return usable_linear_filter_estimates_; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
void Reset(); |
| 240 |
|
| 241 |
|
| 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 |
|
| 258 |
|
| 259 |
class SaturationDetector { |
| 260 |
public: |
| 261 |
|
| 262 |
bool SaturatedEcho() const { return saturated_echo_; } |
| 263 |
|
| 264 |
|
| 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 |
} |
| 288 |
|
| 289 |
#endif |