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
|
|
|
#include "modules/audio_processing/aec3/residual_echo_estimator.h"
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <algorithm>
|
2017-02-23 05:16:26 -08:00
|
|
|
#include <vector>
|
|
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "api/array_view.h"
|
2018-06-13 15:13:55 +02:00
|
|
|
#include "modules/audio_processing/aec3/reverb_model.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/reverb_model_fallback.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2018-05-06 21:17:20 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2018-05-25 16:55:11 +02:00
|
|
|
// Computes the indexes that will be used for computing spectral power over
|
|
|
|
|
// the blocks surrounding the delay.
|
|
|
|
|
void GetRenderIndexesToAnalyze(
|
|
|
|
|
const VectorBuffer& spectrum_buffer,
|
|
|
|
|
const EchoCanceller3Config::EchoModel& echo_model,
|
|
|
|
|
int filter_delay_blocks,
|
|
|
|
|
int* idx_start,
|
|
|
|
|
int* idx_stop) {
|
|
|
|
|
RTC_DCHECK(idx_start);
|
|
|
|
|
RTC_DCHECK(idx_stop);
|
2019-01-29 10:08:15 +01:00
|
|
|
size_t window_start;
|
|
|
|
|
size_t window_end;
|
|
|
|
|
window_start =
|
|
|
|
|
std::max(0, filter_delay_blocks -
|
|
|
|
|
static_cast<int>(echo_model.render_pre_window_size));
|
|
|
|
|
window_end = filter_delay_blocks +
|
|
|
|
|
static_cast<int>(echo_model.render_post_window_size);
|
|
|
|
|
*idx_start = spectrum_buffer.OffsetIndex(spectrum_buffer.read, window_start);
|
|
|
|
|
*idx_stop = spectrum_buffer.OffsetIndex(spectrum_buffer.read, window_end + 1);
|
2018-05-25 16:55:11 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-06 21:17:20 +02:00
|
|
|
} // namespace
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-10-18 12:32:42 +02:00
|
|
|
ResidualEchoEstimator::ResidualEchoEstimator(const EchoCanceller3Config& config)
|
2019-01-29 10:08:15 +01:00
|
|
|
: config_(config) {
|
2018-06-13 15:13:55 +02:00
|
|
|
if (config_.ep_strength.reverb_based_on_render) {
|
|
|
|
|
echo_reverb_.reset(new ReverbModel());
|
|
|
|
|
} else {
|
|
|
|
|
echo_reverb_fallback.reset(
|
|
|
|
|
new ReverbModelFallback(config_.filter.main.length_blocks));
|
|
|
|
|
}
|
2017-04-07 06:13:39 -07:00
|
|
|
Reset();
|
2017-02-23 05:16:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResidualEchoEstimator::~ResidualEchoEstimator() = default;
|
|
|
|
|
|
|
|
|
|
void ResidualEchoEstimator::Estimate(
|
|
|
|
|
const AecState& aec_state,
|
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>& S2_linear,
|
|
|
|
|
const std::array<float, kFftLengthBy2Plus1>& Y2,
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1>* R2) {
|
|
|
|
|
RTC_DCHECK(R2);
|
2017-04-06 15:45:32 -07:00
|
|
|
|
2017-04-19 09:03:40 -07:00
|
|
|
// Estimate the power of the stationary noise in the render signal.
|
|
|
|
|
RenderNoisePower(render_buffer, &X2_noise_floor_, &X2_noise_floor_counter_);
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Estimate the residual echo power.
|
2017-12-20 15:26:13 +01:00
|
|
|
if (aec_state.UsableLinearEstimate()) {
|
2018-06-28 14:21:16 +02:00
|
|
|
LinearEstimate(S2_linear, aec_state.Erle(), aec_state.ErleUncertainty(),
|
|
|
|
|
R2);
|
2018-10-16 14:38:10 +02:00
|
|
|
|
|
|
|
|
// When there is saturated echo, assume the same spectral content as is
|
2019-04-15 10:21:56 +02:00
|
|
|
// present in the microphone signal.
|
2018-10-16 14:38:10 +02:00
|
|
|
if (aec_state.SaturatedEcho()) {
|
|
|
|
|
std::copy(Y2.begin(), Y2.end(), R2->begin());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 15:13:55 +02:00
|
|
|
// Adds the estimated unmodelled echo power to the residual echo power
|
|
|
|
|
// estimate.
|
|
|
|
|
if (echo_reverb_) {
|
|
|
|
|
echo_reverb_->AddReverb(
|
|
|
|
|
render_buffer.Spectrum(aec_state.FilterLengthBlocks() + 1),
|
2018-07-31 00:03:46 +02:00
|
|
|
aec_state.GetReverbFrequencyResponse(), aec_state.ReverbDecay(), *R2);
|
2018-06-26 17:19:15 +02:00
|
|
|
|
2018-06-13 15:13:55 +02:00
|
|
|
} else {
|
|
|
|
|
RTC_DCHECK(echo_reverb_fallback);
|
|
|
|
|
echo_reverb_fallback->AddEchoReverb(S2_linear,
|
|
|
|
|
aec_state.FilterDelayBlocks(),
|
|
|
|
|
aec_state.ReverbDecay(), R2);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
} else {
|
2017-04-07 06:13:39 -07:00
|
|
|
// Estimate the echo generating signal power.
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1> X2;
|
2017-12-20 22:19:56 +01:00
|
|
|
|
2018-05-25 16:55:11 +02:00
|
|
|
EchoGeneratingPower(render_buffer.GetSpectrumBuffer(), config_.echo_model,
|
2019-04-15 10:21:56 +02:00
|
|
|
aec_state.FilterDelayBlocks(),
|
2018-05-02 14:28:30 +02:00
|
|
|
!aec_state.UseStationaryProperties(), &X2);
|
2017-04-07 06:13:39 -07:00
|
|
|
|
2017-04-19 09:03:40 -07:00
|
|
|
// Subtract the stationary noise power to avoid stationary noise causing
|
|
|
|
|
// excessive echo suppression.
|
2018-03-28 16:31:57 +02:00
|
|
|
std::transform(X2.begin(), X2.end(), X2_noise_floor_.begin(), X2.begin(),
|
|
|
|
|
[&](float a, float b) {
|
|
|
|
|
return std::max(
|
|
|
|
|
0.f, a - config_.echo_model.stationary_gate_slope * b);
|
|
|
|
|
});
|
2017-04-19 09:03:40 -07:00
|
|
|
|
2018-05-09 11:48:49 +02:00
|
|
|
float echo_path_gain;
|
2019-01-29 10:08:15 +01:00
|
|
|
echo_path_gain =
|
2019-03-14 11:29:39 +01:00
|
|
|
aec_state.TransparentMode() ? 0.01f : config_.ep_strength.default_gain;
|
2019-04-15 10:21:56 +02:00
|
|
|
NonLinearEstimate(echo_path_gain, X2, R2);
|
2017-12-06 11:32:38 +01:00
|
|
|
|
2018-10-16 14:38:10 +02:00
|
|
|
// When there is saturated echo, assume the same spectral content as is
|
2019-04-15 10:21:56 +02:00
|
|
|
// present in the microphone signal.
|
2017-12-20 22:19:56 +01:00
|
|
|
if (aec_state.SaturatedEcho()) {
|
2018-10-16 14:38:10 +02:00
|
|
|
std::copy(Y2.begin(), Y2.end(), R2->begin());
|
2017-12-06 11:32:38 +01:00
|
|
|
}
|
2018-04-10 16:33:55 +02:00
|
|
|
|
2019-01-29 10:08:15 +01:00
|
|
|
if (!(aec_state.TransparentMode())) {
|
2018-06-13 15:13:55 +02:00
|
|
|
if (echo_reverb_) {
|
2018-06-26 17:19:15 +02:00
|
|
|
echo_reverb_->AddReverbNoFreqShaping(
|
2018-06-13 15:13:55 +02:00
|
|
|
render_buffer.Spectrum(aec_state.FilterDelayBlocks() + 1),
|
|
|
|
|
echo_path_gain * echo_path_gain, aec_state.ReverbDecay(), *R2);
|
|
|
|
|
} else {
|
|
|
|
|
RTC_DCHECK(echo_reverb_fallback);
|
|
|
|
|
echo_reverb_fallback->AddEchoReverb(*R2,
|
|
|
|
|
config_.filter.main.length_blocks,
|
|
|
|
|
aec_state.ReverbDecay(), R2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-11 02:54:02 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 13:58:45 +02:00
|
|
|
if (aec_state.UseStationaryProperties()) {
|
|
|
|
|
// Scale the echo according to echo audibility.
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1> residual_scaling;
|
|
|
|
|
aec_state.GetResidualEchoScaling(residual_scaling);
|
|
|
|
|
for (size_t k = 0; k < R2->size(); ++k) {
|
|
|
|
|
(*R2)[k] *= residual_scaling[k];
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-27 07:29:21 -08:00
|
|
|
}
|
|
|
|
|
|
2017-04-07 06:13:39 -07:00
|
|
|
void ResidualEchoEstimator::Reset() {
|
2018-06-13 15:13:55 +02:00
|
|
|
if (echo_reverb_) {
|
|
|
|
|
echo_reverb_->Reset();
|
|
|
|
|
} else {
|
|
|
|
|
RTC_DCHECK(echo_reverb_fallback);
|
|
|
|
|
echo_reverb_fallback->Reset();
|
|
|
|
|
}
|
2018-03-28 16:31:57 +02:00
|
|
|
X2_noise_floor_counter_.fill(config_.echo_model.noise_floor_hold);
|
|
|
|
|
X2_noise_floor_.fill(config_.echo_model.min_noise_floor_power);
|
2017-04-07 06:13:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResidualEchoEstimator::LinearEstimate(
|
|
|
|
|
const std::array<float, kFftLengthBy2Plus1>& S2_linear,
|
|
|
|
|
const std::array<float, kFftLengthBy2Plus1>& erle,
|
2018-06-28 14:21:16 +02:00
|
|
|
absl::optional<float> erle_uncertainty,
|
2017-04-07 06:13:39 -07:00
|
|
|
std::array<float, kFftLengthBy2Plus1>* R2) {
|
2018-06-28 14:21:16 +02:00
|
|
|
if (erle_uncertainty) {
|
|
|
|
|
for (size_t k = 0; k < R2->size(); ++k) {
|
|
|
|
|
(*R2)[k] = S2_linear[k] * *erle_uncertainty;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
std::transform(erle.begin(), erle.end(), S2_linear.begin(), R2->begin(),
|
|
|
|
|
[](float a, float b) {
|
|
|
|
|
RTC_DCHECK_LT(0.f, a);
|
|
|
|
|
return b / a;
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-04-07 06:13:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResidualEchoEstimator::NonLinearEstimate(
|
2018-03-22 00:29:25 +01:00
|
|
|
float echo_path_gain,
|
2017-04-07 06:13:39 -07:00
|
|
|
const std::array<float, kFftLengthBy2Plus1>& X2,
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1>* R2) {
|
|
|
|
|
// Compute preliminary residual echo.
|
2018-04-10 16:33:55 +02:00
|
|
|
std::transform(X2.begin(), X2.end(), R2->begin(), [echo_path_gain](float a) {
|
|
|
|
|
return a * echo_path_gain * echo_path_gain;
|
|
|
|
|
});
|
2017-04-07 06:13:39 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-28 16:31:57 +02:00
|
|
|
void ResidualEchoEstimator::EchoGeneratingPower(
|
2018-05-25 16:55:11 +02:00
|
|
|
const VectorBuffer& spectrum_buffer,
|
|
|
|
|
const EchoCanceller3Config::EchoModel& echo_model,
|
|
|
|
|
int filter_delay_blocks,
|
2018-05-02 14:28:30 +02:00
|
|
|
bool apply_noise_gating,
|
2018-03-28 16:31:57 +02:00
|
|
|
std::array<float, kFftLengthBy2Plus1>* X2) const {
|
2018-05-25 16:55:11 +02:00
|
|
|
int idx_stop, idx_start;
|
|
|
|
|
|
|
|
|
|
RTC_DCHECK(X2);
|
|
|
|
|
GetRenderIndexesToAnalyze(spectrum_buffer, config_.echo_model,
|
2019-04-15 10:21:56 +02:00
|
|
|
filter_delay_blocks, &idx_start, &idx_stop);
|
2018-05-25 16:55:11 +02:00
|
|
|
|
2018-03-28 16:31:57 +02:00
|
|
|
X2->fill(0.f);
|
2018-05-25 16:55:11 +02:00
|
|
|
for (int k = idx_start; k != idx_stop; k = spectrum_buffer.IncIndex(k)) {
|
|
|
|
|
std::transform(X2->begin(), X2->end(), spectrum_buffer.buffer[k].begin(),
|
2018-03-28 16:31:57 +02:00
|
|
|
X2->begin(),
|
|
|
|
|
[](float a, float b) { return std::max(a, b); });
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 14:28:30 +02:00
|
|
|
if (apply_noise_gating) {
|
|
|
|
|
// Apply soft noise gate.
|
|
|
|
|
std::for_each(X2->begin(), X2->end(), [&](float& a) {
|
|
|
|
|
if (config_.echo_model.noise_gate_power > a) {
|
|
|
|
|
a = std::max(0.f, a - config_.echo_model.noise_gate_slope *
|
|
|
|
|
(config_.echo_model.noise_gate_power - a));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-03-28 16:31:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResidualEchoEstimator::RenderNoisePower(
|
|
|
|
|
const RenderBuffer& render_buffer,
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1>* X2_noise_floor,
|
|
|
|
|
std::array<int, kFftLengthBy2Plus1>* X2_noise_floor_counter) const {
|
|
|
|
|
RTC_DCHECK(X2_noise_floor);
|
|
|
|
|
RTC_DCHECK(X2_noise_floor_counter);
|
|
|
|
|
|
|
|
|
|
const auto render_power = render_buffer.Spectrum(0);
|
|
|
|
|
RTC_DCHECK_EQ(X2_noise_floor->size(), render_power.size());
|
|
|
|
|
RTC_DCHECK_EQ(X2_noise_floor_counter->size(), render_power.size());
|
|
|
|
|
|
|
|
|
|
// Estimate the stationary noise power in a minimum statistics manner.
|
|
|
|
|
for (size_t k = 0; k < render_power.size(); ++k) {
|
|
|
|
|
// Decrease rapidly.
|
|
|
|
|
if (render_power[k] < (*X2_noise_floor)[k]) {
|
|
|
|
|
(*X2_noise_floor)[k] = render_power[k];
|
|
|
|
|
(*X2_noise_floor_counter)[k] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
// Increase in a delayed, leaky manner.
|
|
|
|
|
if ((*X2_noise_floor_counter)[k] >=
|
|
|
|
|
static_cast<int>(config_.echo_model.noise_floor_hold)) {
|
|
|
|
|
(*X2_noise_floor)[k] =
|
|
|
|
|
std::max((*X2_noise_floor)[k] * 1.1f,
|
|
|
|
|
config_.echo_model.min_noise_floor_power);
|
|
|
|
|
} else {
|
|
|
|
|
++(*X2_noise_floor_counter)[k];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
} // namespace webrtc
|