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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "webrtc/modules/audio_processing/aec3/aec_state.h"
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <numeric>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
#include "webrtc/base/array_view.h"
|
2017-02-23 05:16:26 -08:00
|
|
|
#include "webrtc/base/atomicops.h"
|
|
|
|
|
#include "webrtc/base/checks.h"
|
|
|
|
|
#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
constexpr size_t kEchoPathChangeConvergenceBlocks = 4 * kNumBlocksPerSecond;
|
|
|
|
|
constexpr size_t kSaturationLeakageBlocks = 20;
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Computes delay of the adaptive filter.
|
|
|
|
|
rtc::Optional<size_t> EstimateFilterDelay(
|
2017-02-23 05:16:26 -08:00
|
|
|
const std::vector<std::array<float, kFftLengthBy2Plus1>>&
|
2017-04-06 15:45:32 -07:00
|
|
|
adaptive_filter_frequency_response) {
|
|
|
|
|
const auto& H2 = adaptive_filter_frequency_response;
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
size_t reliable_delays_sum = 0;
|
|
|
|
|
size_t num_reliable_delays = 0;
|
|
|
|
|
|
|
|
|
|
constexpr size_t kUpperBin = kFftLengthBy2 - 5;
|
2017-04-06 15:45:32 -07:00
|
|
|
constexpr float kMinPeakMargin = 10.f;
|
|
|
|
|
const size_t kTailPartition = H2.size() - 1;
|
2017-02-23 05:16:26 -08:00
|
|
|
for (size_t k = 1; k < kUpperBin; ++k) {
|
2017-04-06 15:45:32 -07:00
|
|
|
// Find the maximum of H2[j].
|
2017-02-23 05:16:26 -08:00
|
|
|
int peak = 0;
|
|
|
|
|
for (size_t j = 0; j < H2.size(); ++j) {
|
|
|
|
|
if (H2[j][k] > H2[peak][k]) {
|
|
|
|
|
peak = j;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Count the peak as a delay only if the peak is sufficiently larger than
|
|
|
|
|
// the tail.
|
|
|
|
|
if (kMinPeakMargin * H2[kTailPartition][k] < H2[peak][k]) {
|
2017-02-23 05:16:26 -08:00
|
|
|
reliable_delays_sum += peak;
|
|
|
|
|
++num_reliable_delays;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-06 15:45:32 -07:00
|
|
|
|
|
|
|
|
// Return no delay if not sufficient delays have been found.
|
|
|
|
|
if (num_reliable_delays < 21) {
|
|
|
|
|
return rtc::Optional<size_t>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t delay = reliable_delays_sum / num_reliable_delays;
|
|
|
|
|
// Sanity check that the peak is not caused by a false strong DC-component in
|
|
|
|
|
// the filter.
|
|
|
|
|
for (size_t k = 1; k < kUpperBin; ++k) {
|
|
|
|
|
if (H2[delay][k] > H2[delay][0]) {
|
|
|
|
|
RTC_DCHECK_GT(H2.size(), delay);
|
|
|
|
|
return rtc::Optional<size_t>(delay);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rtc::Optional<size_t>();
|
2017-02-23 05:16:26 -08:00
|
|
|
}
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
constexpr int kEchoPathChangeCounterInitial = kNumBlocksPerSecond / 5;
|
2017-04-12 05:40:55 -07:00
|
|
|
constexpr int kEchoPathChangeCounterMax = 2 * kNumBlocksPerSecond;
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
int AecState::instance_count_ = 0;
|
|
|
|
|
|
|
|
|
|
AecState::AecState()
|
|
|
|
|
: data_dumper_(
|
|
|
|
|
new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
|
2017-04-06 15:45:32 -07:00
|
|
|
echo_path_change_counter_(kEchoPathChangeCounterInitial) {}
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
AecState::~AecState() = default;
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
void AecState::HandleEchoPathChange(
|
|
|
|
|
const EchoPathVariability& echo_path_variability) {
|
|
|
|
|
if (echo_path_variability.AudioPathChanged()) {
|
|
|
|
|
blocks_since_last_saturation_ = 0;
|
|
|
|
|
active_render_blocks_ = 0;
|
|
|
|
|
usable_linear_estimate_ = false;
|
|
|
|
|
echo_leakage_detected_ = false;
|
|
|
|
|
capture_signal_saturation_ = false;
|
|
|
|
|
echo_saturation_ = false;
|
|
|
|
|
previous_max_sample_ = 0.f;
|
2017-04-10 13:52:14 -07:00
|
|
|
|
|
|
|
|
if (echo_path_variability.delay_change) {
|
|
|
|
|
force_zero_gain_counter_ = 0;
|
|
|
|
|
force_zero_gain_ = true;
|
2017-04-12 05:40:55 -07:00
|
|
|
echo_path_change_counter_ = kEchoPathChangeCounterMax;
|
|
|
|
|
}
|
|
|
|
|
if (echo_path_variability.gain_change) {
|
|
|
|
|
echo_path_change_counter_ = kEchoPathChangeCounterInitial;
|
2017-04-10 13:52:14 -07:00
|
|
|
}
|
2017-04-06 15:45:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
void AecState::Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
|
2017-04-06 15:45:32 -07:00
|
|
|
adaptive_filter_frequency_response,
|
2017-02-23 05:16:26 -08:00
|
|
|
const rtc::Optional<size_t>& external_delay_samples,
|
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,
|
|
|
|
|
rtc::ArrayView<const float> x,
|
|
|
|
|
bool echo_leakage_detected) {
|
2017-04-06 15:45:32 -07:00
|
|
|
// Store input parameters.
|
|
|
|
|
echo_leakage_detected_ = echo_leakage_detected;
|
|
|
|
|
|
|
|
|
|
// Update counters.
|
|
|
|
|
const float x_energy = std::inner_product(x.begin(), x.end(), x.begin(), 0.f);
|
|
|
|
|
const bool active_render_block = x_energy > 10000.f * kFftLengthBy2;
|
|
|
|
|
active_render_blocks_ += active_render_block ? 1 : 0;
|
|
|
|
|
--echo_path_change_counter_;
|
|
|
|
|
|
2017-04-10 13:52:14 -07:00
|
|
|
// Force zero echo suppression gain after an echo path change to allow at
|
|
|
|
|
// least some render data to be collected in order to avoid an initial echo
|
|
|
|
|
// burst.
|
|
|
|
|
constexpr size_t kZeroGainBlocksAfterChange = kNumBlocksPerSecond / 5;
|
|
|
|
|
force_zero_gain_ = (++force_zero_gain_counter_) < kZeroGainBlocksAfterChange;
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Estimate delays.
|
|
|
|
|
filter_delay_ = EstimateFilterDelay(adaptive_filter_frequency_response);
|
2017-02-23 05:16:26 -08:00
|
|
|
external_delay_ =
|
|
|
|
|
external_delay_samples
|
|
|
|
|
? rtc::Optional<size_t>(*external_delay_samples / kBlockSize)
|
|
|
|
|
: rtc::Optional<size_t>();
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Update the ERL and ERLE measures.
|
|
|
|
|
if (filter_delay_ && echo_path_change_counter_ <= 0) {
|
|
|
|
|
const auto& X2 = render_buffer.Spectrum(*filter_delay_);
|
2017-02-23 05:16:26 -08:00
|
|
|
erle_estimator_.Update(X2, Y2, E2_main);
|
|
|
|
|
erl_estimator_.Update(X2, Y2);
|
|
|
|
|
}
|
2017-04-06 15:45:32 -07:00
|
|
|
|
|
|
|
|
// Detect and flag echo saturation.
|
|
|
|
|
RTC_DCHECK_LT(0, x.size());
|
|
|
|
|
const float max_sample = fabs(*std::max_element(
|
|
|
|
|
x.begin(), x.end(), [](float a, float b) { return a * a < b * b; }));
|
|
|
|
|
const bool saturated_echo =
|
|
|
|
|
previous_max_sample_ * kFixedEchoPathGain > 1600 && SaturatedCapture();
|
|
|
|
|
previous_max_sample_ = max_sample;
|
|
|
|
|
|
|
|
|
|
// Counts the blocks since saturation.
|
|
|
|
|
blocks_since_last_saturation_ =
|
|
|
|
|
saturated_echo ? 0 : blocks_since_last_saturation_ + 1;
|
|
|
|
|
echo_saturation_ = blocks_since_last_saturation_ < kSaturationLeakageBlocks;
|
|
|
|
|
|
|
|
|
|
// Flag whether the linear filter estimate is usable.
|
|
|
|
|
usable_linear_estimate_ =
|
|
|
|
|
(!echo_saturation_) &&
|
|
|
|
|
active_render_blocks_ > kEchoPathChangeConvergenceBlocks &&
|
|
|
|
|
filter_delay_ && echo_path_change_counter_ <= 0;
|
|
|
|
|
|
|
|
|
|
// After an amount of active render samples for which an echo should have been
|
|
|
|
|
// detected in the capture signal if the ERL was not infinite, flag that a
|
|
|
|
|
// headset is used.
|
|
|
|
|
headset_detected_ = !external_delay_ && !filter_delay_ &&
|
|
|
|
|
active_render_blocks_ >= kEchoPathChangeConvergenceBlocks;
|
2017-02-23 05:16:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|