2017-02-23 05:16:26 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
|
|
|
|
|
#define MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2018-03-01 11:32:50 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2018-06-19 10:50:11 +02:00
|
|
|
#include "absl/types/optional.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/array_view.h"
|
2018-02-14 15:19:04 +01:00
|
|
|
#include "api/audio/echo_canceller3_config.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/aec3_common.h"
|
2018-02-21 08:46:03 +01:00
|
|
|
#include "modules/audio_processing/aec3/delay_estimate.h"
|
2018-04-25 13:58:45 +02:00
|
|
|
#include "modules/audio_processing/aec3/echo_audibility.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/echo_path_variability.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/erl_estimator.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/erle_estimator.h"
|
2018-03-22 00:29:25 +01:00
|
|
|
#include "modules/audio_processing/aec3/filter_analyzer.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/render_buffer.h"
|
2018-03-06 10:40:51 +01:00
|
|
|
#include "modules/audio_processing/aec3/suppression_gain_limiter.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/constructormagic.h"
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
class ApmDataDumper;
|
|
|
|
|
|
|
|
|
|
// Handles the state and the conditions for the echo removal functionality.
|
|
|
|
|
class AecState {
|
|
|
|
|
public:
|
2017-10-18 12:32:42 +02:00
|
|
|
explicit AecState(const EchoCanceller3Config& config);
|
2017-02-23 05:16:26 -08:00
|
|
|
~AecState();
|
|
|
|
|
|
2017-12-20 15:26:13 +01:00
|
|
|
// Returns whether the echo subtractor can be used to determine the residual
|
|
|
|
|
// echo.
|
2017-02-23 05:16:26 -08:00
|
|
|
bool UsableLinearEstimate() const { return usable_linear_estimate_; }
|
|
|
|
|
|
2018-03-22 00:29:25 +01:00
|
|
|
// Returns whether the echo subtractor output should be used as output.
|
|
|
|
|
bool UseLinearFilterOutput() const { return use_linear_filter_output_; }
|
|
|
|
|
|
|
|
|
|
// Returns the estimated echo path gain.
|
2018-05-09 11:48:49 +02:00
|
|
|
float EchoPathGain() const { return filter_analyzer_.Gain(); }
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
// Returns whether the render signal is currently active.
|
2017-12-20 15:26:13 +01:00
|
|
|
bool ActiveRender() const { return blocks_with_active_render_ > 200; }
|
2017-02-27 07:29:21 -08:00
|
|
|
|
2018-04-25 13:58:45 +02:00
|
|
|
// Returns the appropriate scaling of the residual echo to match the
|
|
|
|
|
// audibility.
|
|
|
|
|
void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const {
|
|
|
|
|
echo_audibility_.GetResidualEchoScaling(residual_scaling);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns whether the stationary properties of the signals are used in the
|
|
|
|
|
// aec.
|
2018-05-16 15:25:04 +02:00
|
|
|
bool UseStationaryProperties() const { return use_stationary_properties_; }
|
2018-04-25 13:58:45 +02:00
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Returns the ERLE.
|
|
|
|
|
const std::array<float, kFftLengthBy2Plus1>& Erle() const {
|
|
|
|
|
return erle_estimator_.Erle();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 14:21:16 +02:00
|
|
|
// Returns any uncertainty in the ERLE estimate.
|
|
|
|
|
absl::optional<float> ErleUncertainty() const {
|
|
|
|
|
if (allow_linear_mode_with_diverged_filter_ && diverged_linear_filter_) {
|
|
|
|
|
return 10.f;
|
|
|
|
|
}
|
|
|
|
|
return absl::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 14:17:39 +01:00
|
|
|
// Returns the time-domain ERLE.
|
|
|
|
|
float ErleTimeDomain() const { return erle_estimator_.ErleTimeDomain(); }
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Returns the ERL.
|
|
|
|
|
const std::array<float, kFftLengthBy2Plus1>& Erl() const {
|
|
|
|
|
return erl_estimator_.Erl();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 14:17:39 +01:00
|
|
|
// Returns the time-domain ERL.
|
|
|
|
|
float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Returns the delay estimate based on the linear filter.
|
2018-03-22 00:29:25 +01:00
|
|
|
int FilterDelayBlocks() const { return filter_delay_blocks_; }
|
|
|
|
|
|
|
|
|
|
// Returns the internal delay estimate based on the linear filter.
|
2018-06-19 10:50:11 +02:00
|
|
|
absl::optional<int> InternalDelay() const { return internal_delay_; }
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
// Returns whether the capture signal is saturated.
|
|
|
|
|
bool SaturatedCapture() const { return capture_signal_saturation_; }
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Returns whether the echo signal is saturated.
|
|
|
|
|
bool SaturatedEcho() const { return echo_saturation_; }
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Updates the capture signal saturation.
|
|
|
|
|
void UpdateCaptureSaturation(bool capture_signal_saturation) {
|
|
|
|
|
capture_signal_saturation_ = capture_signal_saturation;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 20:19:21 +02:00
|
|
|
// Returns whether the transparent mode is active
|
|
|
|
|
bool TransparentMode() const { return transparent_mode_; }
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Takes appropriate action at an echo path change.
|
|
|
|
|
void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
|
|
|
|
|
|
2017-04-07 06:13:39 -07:00
|
|
|
// Returns the decay factor for the echo reverberation.
|
2017-07-11 02:54:02 -07:00
|
|
|
float ReverbDecay() const { return reverb_decay_; }
|
2017-04-07 06:13:39 -07:00
|
|
|
|
2018-02-20 22:18:27 +01:00
|
|
|
// Returns the upper limit for the echo suppression gain.
|
2018-03-06 10:40:51 +01:00
|
|
|
float SuppressionGainLimit() const {
|
|
|
|
|
return suppression_gain_limiter_.Limit();
|
|
|
|
|
}
|
2017-04-10 13:52:14 -07:00
|
|
|
|
2018-05-25 16:55:11 +02:00
|
|
|
// Returns whether the suppression gain limiter is active.
|
|
|
|
|
bool IsSuppressionGainLimitActive() const {
|
|
|
|
|
return suppression_gain_limiter_.IsActive();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 15:26:13 +01:00
|
|
|
// Returns whether the linear filter should have been able to properly adapt.
|
|
|
|
|
bool FilterHasHadTimeToConverge() const {
|
|
|
|
|
return filter_has_had_time_to_converge_;
|
|
|
|
|
}
|
2017-10-15 20:19:21 +02:00
|
|
|
|
2018-01-15 19:17:16 +01:00
|
|
|
// Returns whether the filter adaptation is still in the initial state.
|
|
|
|
|
bool InitialState() const { return initial_state_; }
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Updates the aec state.
|
2018-06-19 10:50:11 +02:00
|
|
|
void Update(const absl::optional<DelayEstimate>& external_delay,
|
2018-02-21 08:46:03 +01:00
|
|
|
const std::vector<std::array<float, kFftLengthBy2Plus1>>&
|
2017-04-06 15:45:32 -07:00
|
|
|
adaptive_filter_frequency_response,
|
2017-12-11 22:28:45 +01:00
|
|
|
const std::vector<float>& adaptive_filter_impulse_response,
|
2017-10-15 20:19:21 +02:00
|
|
|
bool converged_filter,
|
2018-03-22 00:29:25 +01:00
|
|
|
bool diverged_filter,
|
2017-04-06 15:45:32 -07:00
|
|
|
const RenderBuffer& render_buffer,
|
2017-02-23 05:16:26 -08:00
|
|
|
const std::array<float, kFftLengthBy2Plus1>& E2_main,
|
|
|
|
|
const std::array<float, kFftLengthBy2Plus1>& Y2,
|
2018-03-22 00:29:25 +01:00
|
|
|
const std::array<float, kBlockSize>& s);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2018-06-26 17:19:15 +02:00
|
|
|
// Returns the tail freq. response of the linear filter.
|
|
|
|
|
rtc::ArrayView<const float> GetFreqRespTail() const {
|
|
|
|
|
return filter_analyzer_.GetFreqRespTail();
|
|
|
|
|
}
|
2018-06-13 15:13:55 +02:00
|
|
|
|
|
|
|
|
// Returns filter length in blocks.
|
|
|
|
|
int FilterLengthBlocks() const {
|
|
|
|
|
return filter_analyzer_.FilterLengthBlocks();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
private:
|
2017-12-11 22:28:45 +01:00
|
|
|
void UpdateReverb(const std::vector<float>& impulse_response);
|
2017-12-20 15:26:13 +01:00
|
|
|
bool DetectActiveRender(rtc::ArrayView<const float> x) const;
|
2018-02-20 22:18:27 +01:00
|
|
|
void UpdateSuppressorGainLimit(bool render_activity);
|
2018-04-10 16:33:55 +02:00
|
|
|
bool DetectEchoSaturation(rtc::ArrayView<const float> x,
|
|
|
|
|
float echo_path_gain);
|
2017-07-11 02:54:02 -07:00
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
static int instance_count_;
|
|
|
|
|
std::unique_ptr<ApmDataDumper> data_dumper_;
|
2018-05-16 15:25:04 +02:00
|
|
|
const EchoCanceller3Config config_;
|
2018-05-09 12:07:26 +02:00
|
|
|
const bool allow_transparent_mode_;
|
2018-05-16 15:25:04 +02:00
|
|
|
const bool use_stationary_properties_;
|
2018-06-07 15:59:59 +02:00
|
|
|
const bool enforce_delay_after_realignment_;
|
2018-06-28 14:21:16 +02:00
|
|
|
const bool allow_linear_mode_with_diverged_filter_;
|
2017-02-23 05:16:26 -08:00
|
|
|
ErlEstimator erl_estimator_;
|
|
|
|
|
ErleEstimator erle_estimator_;
|
2017-10-15 20:19:21 +02:00
|
|
|
size_t capture_block_counter_ = 0;
|
2018-03-22 00:29:25 +01:00
|
|
|
size_t blocks_since_reset_ = 0;
|
2017-12-20 15:26:13 +01:00
|
|
|
size_t blocks_with_proper_filter_adaptation_ = 0;
|
|
|
|
|
size_t blocks_with_active_render_ = 0;
|
2017-02-23 05:16:26 -08:00
|
|
|
bool usable_linear_estimate_ = false;
|
2018-06-28 14:21:16 +02:00
|
|
|
bool diverged_linear_filter_ = false;
|
2017-02-23 05:16:26 -08:00
|
|
|
bool capture_signal_saturation_ = false;
|
2017-04-06 15:45:32 -07:00
|
|
|
bool echo_saturation_ = false;
|
2017-10-15 20:19:21 +02:00
|
|
|
bool transparent_mode_ = false;
|
2017-04-19 09:03:40 -07:00
|
|
|
bool render_received_ = false;
|
2018-03-22 00:29:25 +01:00
|
|
|
int filter_delay_blocks_ = 0;
|
2017-04-06 15:45:32 -07:00
|
|
|
size_t blocks_since_last_saturation_ = 1000;
|
2018-03-01 11:32:50 +01:00
|
|
|
float tail_energy_ = 0.f;
|
|
|
|
|
float accumulated_nz_ = 0.f;
|
|
|
|
|
float accumulated_nn_ = 0.f;
|
|
|
|
|
float accumulated_count_ = 0.f;
|
|
|
|
|
size_t current_reverb_decay_section_ = 0;
|
|
|
|
|
size_t num_reverb_decay_sections_ = 0;
|
|
|
|
|
size_t num_reverb_decay_sections_next_ = 0;
|
|
|
|
|
bool found_end_of_reverb_decay_ = false;
|
|
|
|
|
bool main_filter_is_adapting_ = true;
|
|
|
|
|
std::array<float, kMaxAdaptiveFilterLength> block_energies_;
|
2017-12-11 22:28:45 +01:00
|
|
|
std::vector<float> max_render_;
|
2018-03-01 11:32:50 +01:00
|
|
|
float reverb_decay_ = fabsf(config_.ep_strength.default_len);
|
2017-12-20 15:26:13 +01:00
|
|
|
bool filter_has_had_time_to_converge_ = false;
|
2018-01-15 19:17:16 +01:00
|
|
|
bool initial_state_ = true;
|
2018-02-20 22:18:27 +01:00
|
|
|
const float gain_rampup_increase_;
|
2018-03-06 10:40:51 +01:00
|
|
|
SuppressionGainUpperLimiter suppression_gain_limiter_;
|
2018-03-22 00:29:25 +01:00
|
|
|
FilterAnalyzer filter_analyzer_;
|
|
|
|
|
bool use_linear_filter_output_ = false;
|
2018-06-19 10:50:11 +02:00
|
|
|
absl::optional<int> internal_delay_;
|
2018-03-22 00:29:25 +01:00
|
|
|
size_t diverged_blocks_ = 0;
|
|
|
|
|
bool filter_should_have_converged_ = false;
|
|
|
|
|
size_t blocks_since_converged_filter_;
|
|
|
|
|
size_t active_blocks_since_consistent_filter_estimate_;
|
|
|
|
|
bool converged_filter_seen_ = false;
|
|
|
|
|
bool consistent_filter_seen_ = false;
|
|
|
|
|
bool external_delay_seen_ = false;
|
2018-06-19 10:50:11 +02:00
|
|
|
absl::optional<DelayEstimate> external_delay_;
|
2018-06-07 15:59:59 +02:00
|
|
|
size_t frames_since_external_delay_change_ = 0;
|
2018-03-22 10:15:59 +01:00
|
|
|
size_t converged_filter_count_ = 0;
|
|
|
|
|
bool finite_erl_ = false;
|
2018-03-28 18:13:41 +02:00
|
|
|
size_t active_blocks_since_converged_filter_ = 0;
|
2018-04-25 13:58:45 +02:00
|
|
|
EchoAudibility echo_audibility_;
|
2017-08-24 22:36:53 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
|
2017-02-23 05:16:26 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
|