2017-01-27 03:28:19 -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/echo_remover.h"
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
#include <math.h>
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <stddef.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
#include <algorithm>
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <array>
|
2019-03-15 07:39:02 +01:00
|
|
|
#include <cmath>
|
2017-02-23 05:16:26 -08:00
|
|
|
#include <memory>
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/array_view.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/aec3_common.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "modules/audio_processing/aec3/aec3_fft.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/aec_state.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/comfort_noise_generator.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/echo_path_variability.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/echo_remover_metrics.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/fft_data.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/render_buffer.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "modules/audio_processing/aec3/render_signal_analyzer.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/residual_echo_estimator.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/subtractor.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "modules/audio_processing/aec3/subtractor_output.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/aec3/suppression_filter.h"
|
|
|
|
|
#include "modules/audio_processing/aec3/suppression_gain.h"
|
|
|
|
|
#include "modules/audio_processing/logging/apm_data_dumper.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/atomic_ops.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2018-07-16 17:08:41 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2017-01-27 03:28:19 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2019-09-10 18:05:17 +02:00
|
|
|
// Maximum number of channels for which the capture channel data is stored on
|
|
|
|
|
// the stack. If the number of channels are larger than this, they are stored
|
|
|
|
|
// using scratch memory that is pre-allocated on the heap. The reason for this
|
|
|
|
|
// partitioning is not to waste heap space for handling the more common numbers
|
|
|
|
|
// of channels, while at the same time not limiting the support for higher
|
|
|
|
|
// numbers of channels by enforcing the capture channel data to be stored on the
|
|
|
|
|
// stack using a fixed maximum value.
|
|
|
|
|
constexpr size_t kMaxNumChannelsOnStack = 2;
|
|
|
|
|
|
|
|
|
|
// Chooses the number of channels to store on the heap when that is required due
|
|
|
|
|
// to the number of capture channels being larger than the pre-defined number
|
|
|
|
|
// of channels to store on the stack.
|
|
|
|
|
size_t NumChannelsOnHeap(size_t num_capture_channels) {
|
|
|
|
|
return num_capture_channels > kMaxNumChannelsOnStack ? num_capture_channels
|
|
|
|
|
: 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
void LinearEchoPower(const FftData& E,
|
|
|
|
|
const FftData& Y,
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1>* S2) {
|
|
|
|
|
for (size_t k = 0; k < E.re.size(); ++k) {
|
|
|
|
|
(*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) +
|
|
|
|
|
(Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-10 18:37:38 +02:00
|
|
|
// Fades between two input signals using a fix-sized transition.
|
|
|
|
|
void SignalTransition(rtc::ArrayView<const float> from,
|
|
|
|
|
rtc::ArrayView<const float> to,
|
|
|
|
|
rtc::ArrayView<float> out) {
|
2019-09-24 16:31:01 +02:00
|
|
|
if (from == to) {
|
|
|
|
|
RTC_DCHECK_EQ(to.size(), out.size());
|
|
|
|
|
std::copy(to.begin(), to.end(), out.begin());
|
|
|
|
|
} else {
|
|
|
|
|
constexpr size_t kTransitionSize = 30;
|
|
|
|
|
constexpr float kOneByTransitionSizePlusOne = 1.f / (kTransitionSize + 1);
|
2018-08-10 18:37:38 +02:00
|
|
|
|
2019-09-24 16:31:01 +02:00
|
|
|
RTC_DCHECK_EQ(from.size(), to.size());
|
|
|
|
|
RTC_DCHECK_EQ(from.size(), out.size());
|
|
|
|
|
RTC_DCHECK_LE(kTransitionSize, out.size());
|
2018-08-10 18:37:38 +02:00
|
|
|
|
2019-09-24 16:31:01 +02:00
|
|
|
for (size_t k = 0; k < kTransitionSize; ++k) {
|
|
|
|
|
float a = (k + 1) * kOneByTransitionSizePlusOne;
|
|
|
|
|
out[k] = a * to[k] + (1.f - a) * from[k];
|
|
|
|
|
}
|
2018-08-10 18:37:38 +02:00
|
|
|
|
2019-09-24 16:31:01 +02:00
|
|
|
std::copy(to.begin() + kTransitionSize, to.end(),
|
|
|
|
|
out.begin() + kTransitionSize);
|
|
|
|
|
}
|
2018-08-10 18:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-27 12:04:03 +02:00
|
|
|
// Computes a windowed (square root Hanning) padded FFT and updates the related
|
|
|
|
|
// memory.
|
|
|
|
|
void WindowedPaddedFft(const Aec3Fft& fft,
|
|
|
|
|
rtc::ArrayView<const float> v,
|
|
|
|
|
rtc::ArrayView<float> v_old,
|
|
|
|
|
FftData* V) {
|
|
|
|
|
fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V);
|
|
|
|
|
std::copy(v.begin(), v.end(), v_old.begin());
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Class for removing the echo from the capture signal.
|
2017-01-27 03:28:19 -08:00
|
|
|
class EchoRemoverImpl final : public EchoRemover {
|
|
|
|
|
public:
|
2019-09-02 17:01:19 +02:00
|
|
|
EchoRemoverImpl(const EchoCanceller3Config& config,
|
|
|
|
|
int sample_rate_hz,
|
|
|
|
|
size_t num_render_channels,
|
|
|
|
|
size_t num_capture_channels);
|
2017-01-27 03:28:19 -08:00
|
|
|
~EchoRemoverImpl() override;
|
2019-09-10 18:05:17 +02:00
|
|
|
EchoRemoverImpl(const EchoRemoverImpl&) = delete;
|
|
|
|
|
EchoRemoverImpl& operator=(const EchoRemoverImpl&) = delete;
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2017-11-22 14:17:39 +01:00
|
|
|
void GetMetrics(EchoControl::Metrics* metrics) const override;
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Removes the echo from a block of samples from the capture signal. The
|
|
|
|
|
// supplied render signal is assumed to be pre-aligned with the capture
|
|
|
|
|
// signal.
|
2019-09-02 17:01:19 +02:00
|
|
|
void ProcessCapture(
|
|
|
|
|
EchoPathVariability echo_path_variability,
|
|
|
|
|
bool capture_signal_saturation,
|
|
|
|
|
const absl::optional<DelayEstimate>& external_delay,
|
|
|
|
|
RenderBuffer* render_buffer,
|
|
|
|
|
std::vector<std::vector<std::vector<float>>>* capture) override;
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Updates the status on whether echo leakage is detected in the output of the
|
|
|
|
|
// echo remover.
|
|
|
|
|
void UpdateEchoLeakageStatus(bool leakage_detected) override {
|
|
|
|
|
echo_leakage_detected_ = leakage_detected;
|
|
|
|
|
}
|
2017-01-27 03:28:19 -08:00
|
|
|
|
|
|
|
|
private:
|
2018-08-01 16:24:08 +02:00
|
|
|
// Selects which of the shadow and main linear filter outputs that is most
|
2018-08-10 18:37:38 +02:00
|
|
|
// appropriate to pass to the suppressor and forms the linear filter output by
|
|
|
|
|
// smoothly transition between those.
|
2019-01-29 10:08:15 +01:00
|
|
|
void FormLinearFilterOutput(const SubtractorOutput& subtractor_output,
|
2018-08-10 18:37:38 +02:00
|
|
|
rtc::ArrayView<float> output);
|
2018-08-01 16:24:08 +02:00
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
static int instance_count_;
|
2017-10-18 12:32:42 +02:00
|
|
|
const EchoCanceller3Config config_;
|
2017-02-23 05:16:26 -08:00
|
|
|
const Aec3Fft fft_;
|
|
|
|
|
std::unique_ptr<ApmDataDumper> data_dumper_;
|
|
|
|
|
const Aec3Optimization optimization_;
|
2017-01-27 03:28:19 -08:00
|
|
|
const int sample_rate_hz_;
|
2019-09-02 17:01:19 +02:00
|
|
|
const size_t num_render_channels_;
|
|
|
|
|
const size_t num_capture_channels_;
|
2018-08-01 16:24:08 +02:00
|
|
|
const bool use_shadow_filter_output_;
|
2019-09-25 14:53:30 +02:00
|
|
|
Subtractor subtractor_;
|
2019-09-23 16:03:12 +02:00
|
|
|
std::vector<std::unique_ptr<SuppressionGain>> suppression_gains_;
|
|
|
|
|
std::vector<std::unique_ptr<ComfortNoiseGenerator>> cngs_;
|
2017-02-23 05:16:26 -08:00
|
|
|
SuppressionFilter suppression_filter_;
|
|
|
|
|
RenderSignalAnalyzer render_signal_analyzer_;
|
2019-09-23 16:03:12 +02:00
|
|
|
std::vector<std::unique_ptr<ResidualEchoEstimator>> residual_echo_estimators_;
|
2017-02-23 05:16:26 -08:00
|
|
|
bool echo_leakage_detected_ = false;
|
|
|
|
|
AecState aec_state_;
|
2017-02-28 22:08:53 -08:00
|
|
|
EchoRemoverMetrics metrics_;
|
2019-09-23 16:03:12 +02:00
|
|
|
std::vector<std::array<float, kFftLengthBy2>> e_old_;
|
|
|
|
|
std::vector<std::array<float, kFftLengthBy2>> y_old_;
|
2018-07-16 17:08:41 +02:00
|
|
|
size_t block_counter_ = 0;
|
|
|
|
|
int gain_change_hangover_ = 0;
|
2018-08-10 18:37:38 +02:00
|
|
|
bool main_filter_output_last_selected_ = true;
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2019-09-25 12:09:37 +02:00
|
|
|
std::vector<std::array<float, kFftLengthBy2>> e_heap_;
|
2019-09-10 18:05:17 +02:00
|
|
|
std::vector<std::array<float, kFftLengthBy2Plus1>> Y2_heap_;
|
|
|
|
|
std::vector<std::array<float, kFftLengthBy2Plus1>> E2_heap_;
|
|
|
|
|
std::vector<std::array<float, kFftLengthBy2Plus1>> R2_heap_;
|
|
|
|
|
std::vector<std::array<float, kFftLengthBy2Plus1>> S2_linear_heap_;
|
|
|
|
|
std::vector<FftData> Y_heap_;
|
|
|
|
|
std::vector<FftData> E_heap_;
|
|
|
|
|
std::vector<FftData> comfort_noise_heap_;
|
|
|
|
|
std::vector<FftData> high_band_comfort_noise_heap_;
|
|
|
|
|
std::vector<SubtractorOutput> subtractor_output_heap_;
|
2017-01-27 03:28:19 -08:00
|
|
|
};
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
int EchoRemoverImpl::instance_count_ = 0;
|
|
|
|
|
|
2017-10-18 12:32:42 +02:00
|
|
|
EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config,
|
2019-09-02 17:01:19 +02:00
|
|
|
int sample_rate_hz,
|
|
|
|
|
size_t num_render_channels,
|
|
|
|
|
size_t num_capture_channels)
|
2017-08-24 22:36:53 -07:00
|
|
|
: config_(config),
|
|
|
|
|
fft_(),
|
2017-02-23 06:27:03 -08:00
|
|
|
data_dumper_(
|
2017-02-23 05:16:26 -08:00
|
|
|
new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
|
|
|
|
|
optimization_(DetectOptimization()),
|
|
|
|
|
sample_rate_hz_(sample_rate_hz),
|
2019-09-02 17:01:19 +02:00
|
|
|
num_render_channels_(num_render_channels),
|
|
|
|
|
num_capture_channels_(num_capture_channels),
|
2018-08-31 07:34:29 +02:00
|
|
|
use_shadow_filter_output_(
|
|
|
|
|
config_.filter.enable_shadow_filter_output_usage),
|
2019-09-25 14:53:30 +02:00
|
|
|
subtractor_(config,
|
|
|
|
|
num_render_channels_,
|
|
|
|
|
num_capture_channels_,
|
|
|
|
|
data_dumper_.get(),
|
|
|
|
|
optimization_),
|
2019-09-23 16:03:12 +02:00
|
|
|
suppression_gains_(num_capture_channels_),
|
|
|
|
|
cngs_(num_capture_channels_),
|
2019-09-24 15:05:04 +02:00
|
|
|
suppression_filter_(optimization_,
|
|
|
|
|
sample_rate_hz_,
|
|
|
|
|
num_capture_channels_),
|
2018-03-14 23:23:47 +01:00
|
|
|
render_signal_analyzer_(config_),
|
2019-09-23 16:03:12 +02:00
|
|
|
residual_echo_estimators_(num_capture_channels_),
|
2019-09-10 18:05:17 +02:00
|
|
|
aec_state_(config_),
|
2019-09-23 16:03:12 +02:00
|
|
|
e_old_(num_capture_channels_),
|
|
|
|
|
y_old_(num_capture_channels_),
|
2019-09-25 12:09:37 +02:00
|
|
|
e_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
2019-09-10 18:05:17 +02:00
|
|
|
Y2_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
E2_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
R2_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
S2_linear_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
Y_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
E_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
high_band_comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
|
|
|
|
subtractor_output_heap_(NumChannelsOnHeap(num_capture_channels_)) {
|
2017-02-23 05:16:26 -08:00
|
|
|
RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
|
2019-09-25 12:09:37 +02:00
|
|
|
for (auto& e_k : e_heap_) {
|
|
|
|
|
e_k.fill(0.f);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-30 09:58:09 +02:00
|
|
|
uint32_t cng_seed = 42;
|
2019-09-23 16:03:12 +02:00
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
|
|
|
|
residual_echo_estimators_[ch] =
|
|
|
|
|
std::make_unique<ResidualEchoEstimator>(config_);
|
|
|
|
|
suppression_gains_[ch] = std::make_unique<SuppressionGain>(
|
|
|
|
|
config_, optimization_, sample_rate_hz);
|
2019-09-30 09:58:09 +02:00
|
|
|
cngs_[ch] =
|
|
|
|
|
std::make_unique<ComfortNoiseGenerator>(optimization_, cng_seed++);
|
2019-09-23 16:03:12 +02:00
|
|
|
e_old_[ch].fill(0.f);
|
|
|
|
|
y_old_[ch].fill(0.f);
|
|
|
|
|
}
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EchoRemoverImpl::~EchoRemoverImpl() = default;
|
|
|
|
|
|
2017-11-22 14:17:39 +01:00
|
|
|
void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const {
|
|
|
|
|
// Echo return loss (ERL) is inverted to go from gain to attenuation.
|
2019-03-15 07:39:02 +01:00
|
|
|
metrics->echo_return_loss = -10.0 * std::log10(aec_state_.ErlTimeDomain());
|
2017-11-22 14:17:39 +01:00
|
|
|
metrics->echo_return_loss_enhancement =
|
2018-09-27 11:49:39 +02:00
|
|
|
Log2TodB(aec_state_.FullBandErleLog2());
|
2017-11-22 14:17:39 +01:00
|
|
|
}
|
|
|
|
|
|
2017-04-05 14:18:07 -07:00
|
|
|
void EchoRemoverImpl::ProcessCapture(
|
2018-07-16 17:08:41 +02:00
|
|
|
EchoPathVariability echo_path_variability,
|
2017-01-27 03:28:19 -08:00
|
|
|
bool capture_signal_saturation,
|
2018-06-19 10:50:11 +02:00
|
|
|
const absl::optional<DelayEstimate>& external_delay,
|
2017-12-11 21:34:19 +01:00
|
|
|
RenderBuffer* render_buffer,
|
2019-09-02 17:01:19 +02:00
|
|
|
std::vector<std::vector<std::vector<float>>>* capture) {
|
2018-07-16 17:08:41 +02:00
|
|
|
++block_counter_;
|
2019-09-02 17:01:19 +02:00
|
|
|
const std::vector<std::vector<std::vector<float>>>& x =
|
|
|
|
|
render_buffer->Block(0);
|
|
|
|
|
std::vector<std::vector<std::vector<float>>>* y = capture;
|
2017-12-11 21:34:19 +01:00
|
|
|
RTC_DCHECK(render_buffer);
|
2017-02-23 05:16:26 -08:00
|
|
|
RTC_DCHECK(y);
|
|
|
|
|
RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_));
|
|
|
|
|
RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_));
|
2019-09-02 17:01:19 +02:00
|
|
|
RTC_DCHECK_EQ(x[0].size(), num_render_channels_);
|
|
|
|
|
RTC_DCHECK_EQ((*y)[0].size(), num_capture_channels_);
|
|
|
|
|
RTC_DCHECK_EQ(x[0][0].size(), kBlockSize);
|
|
|
|
|
RTC_DCHECK_EQ((*y)[0][0].size(), kBlockSize);
|
2019-09-10 18:05:17 +02:00
|
|
|
|
|
|
|
|
// Stack allocated data to use when the number of channels is low.
|
2019-09-25 12:09:37 +02:00
|
|
|
std::array<std::array<float, kFftLengthBy2>, kMaxNumChannelsOnStack> e_stack;
|
2019-09-10 18:05:17 +02:00
|
|
|
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
|
|
|
|
|
Y2_stack;
|
|
|
|
|
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
|
|
|
|
|
E2_stack;
|
|
|
|
|
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
|
|
|
|
|
R2_stack;
|
|
|
|
|
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
|
|
|
|
|
S2_linear_stack;
|
|
|
|
|
std::array<FftData, kMaxNumChannelsOnStack> Y_stack;
|
|
|
|
|
std::array<FftData, kMaxNumChannelsOnStack> E_stack;
|
|
|
|
|
std::array<FftData, kMaxNumChannelsOnStack> comfort_noise_stack;
|
|
|
|
|
std::array<FftData, kMaxNumChannelsOnStack> high_band_comfort_noise_stack;
|
|
|
|
|
std::array<SubtractorOutput, kMaxNumChannelsOnStack> subtractor_output_stack;
|
|
|
|
|
|
2019-09-25 12:09:37 +02:00
|
|
|
rtc::ArrayView<std::array<float, kFftLengthBy2>> e(e_stack.data(),
|
|
|
|
|
num_capture_channels_);
|
2019-09-10 18:05:17 +02:00
|
|
|
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> Y2(
|
|
|
|
|
Y2_stack.data(), num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> E2(
|
|
|
|
|
E2_stack.data(), num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> R2(
|
|
|
|
|
R2_stack.data(), num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> S2_linear(
|
|
|
|
|
S2_linear_stack.data(), num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<FftData> Y(Y_stack.data(), num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<FftData> E(E_stack.data(), num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<FftData> comfort_noise(comfort_noise_stack.data(),
|
|
|
|
|
num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<FftData> high_band_comfort_noise(
|
|
|
|
|
high_band_comfort_noise_stack.data(), num_capture_channels_);
|
|
|
|
|
rtc::ArrayView<SubtractorOutput> subtractor_output(
|
|
|
|
|
subtractor_output_stack.data(), num_capture_channels_);
|
|
|
|
|
if (NumChannelsOnHeap(num_capture_channels_) > 0) {
|
|
|
|
|
// If the stack-allocated space is too small, use the heap for storing the
|
|
|
|
|
// microphone data.
|
2019-09-25 12:09:37 +02:00
|
|
|
e = rtc::ArrayView<std::array<float, kFftLengthBy2>>(e_heap_.data(),
|
|
|
|
|
num_capture_channels_);
|
2019-09-10 18:05:17 +02:00
|
|
|
Y2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
|
|
|
|
|
Y2_heap_.data(), num_capture_channels_);
|
|
|
|
|
E2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
|
|
|
|
|
E2_heap_.data(), num_capture_channels_);
|
|
|
|
|
R2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
|
|
|
|
|
R2_heap_.data(), num_capture_channels_);
|
|
|
|
|
S2_linear = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
|
|
|
|
|
S2_linear_heap_.data(), num_capture_channels_);
|
|
|
|
|
Y = rtc::ArrayView<FftData>(Y_heap_.data(), num_capture_channels_);
|
|
|
|
|
E = rtc::ArrayView<FftData>(E_heap_.data(), num_capture_channels_);
|
|
|
|
|
comfort_noise = rtc::ArrayView<FftData>(comfort_noise_heap_.data(),
|
|
|
|
|
num_capture_channels_);
|
|
|
|
|
high_band_comfort_noise = rtc::ArrayView<FftData>(
|
|
|
|
|
high_band_comfort_noise_heap_.data(), num_capture_channels_);
|
|
|
|
|
subtractor_output = rtc::ArrayView<SubtractorOutput>(
|
|
|
|
|
subtractor_output_heap_.data(), num_capture_channels_);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 17:01:19 +02:00
|
|
|
const std::vector<float>& x0 = x[0][0];
|
|
|
|
|
std::vector<float>& y0 = (*y)[0][0];
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0],
|
2019-09-02 17:01:19 +02:00
|
|
|
16000, 1);
|
2017-04-06 15:45:32 -07:00
|
|
|
data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0],
|
2019-09-02 17:01:19 +02:00
|
|
|
16000, 1);
|
2017-07-11 02:54:02 -07:00
|
|
|
data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0);
|
|
|
|
|
data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
aec_state_.UpdateCaptureSaturation(capture_signal_saturation);
|
|
|
|
|
|
|
|
|
|
if (echo_path_variability.AudioPathChanged()) {
|
2018-07-16 17:08:41 +02:00
|
|
|
// Ensure that the gain change is only acted on once per frame.
|
|
|
|
|
if (echo_path_variability.gain_change) {
|
|
|
|
|
if (gain_change_hangover_ == 0) {
|
|
|
|
|
constexpr int kMaxBlocksPerFrame = 3;
|
|
|
|
|
gain_change_hangover_ = kMaxBlocksPerFrame;
|
2019-08-08 15:04:41 +02:00
|
|
|
RTC_LOG(LS_INFO) << "Gain change detected at block " << block_counter_;
|
2018-07-16 17:08:41 +02:00
|
|
|
} else {
|
|
|
|
|
echo_path_variability.gain_change = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 14:53:30 +02:00
|
|
|
subtractor_.HandleEchoPathChange(echo_path_variability);
|
2017-04-06 15:45:32 -07:00
|
|
|
aec_state_.HandleEchoPathChange(echo_path_variability);
|
2018-07-16 17:08:41 +02:00
|
|
|
|
|
|
|
|
if (echo_path_variability.delay_change !=
|
|
|
|
|
EchoPathVariability::DelayAdjustment::kNone) {
|
2019-09-23 16:03:12 +02:00
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
|
|
|
|
suppression_gains_[ch]->SetInitialState(true);
|
|
|
|
|
}
|
2018-07-16 17:08:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (gain_change_hangover_ > 0) {
|
|
|
|
|
--gain_change_hangover_;
|
2017-02-23 05:16:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Analyze the render signal.
|
2018-03-22 00:29:25 +01:00
|
|
|
render_signal_analyzer_.Update(*render_buffer,
|
|
|
|
|
aec_state_.FilterDelayBlocks());
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2019-09-25 14:53:30 +02:00
|
|
|
// State transition.
|
2018-08-29 13:34:07 +02:00
|
|
|
if (aec_state_.TransitionTriggered()) {
|
2019-09-25 14:53:30 +02:00
|
|
|
subtractor_.ExitInitialState();
|
2019-09-23 16:03:12 +02:00
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
|
|
|
|
suppression_gains_[ch]->SetInitialState(false);
|
|
|
|
|
}
|
2018-01-15 19:17:16 +01:00
|
|
|
}
|
2018-03-22 00:29:25 +01:00
|
|
|
|
2019-09-25 14:53:30 +02:00
|
|
|
// Perform linear echo cancellation.
|
|
|
|
|
subtractor_.Process(*render_buffer, (*y)[0], render_signal_analyzer_,
|
|
|
|
|
aec_state_, subtractor_output);
|
|
|
|
|
|
2019-09-23 16:03:12 +02:00
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
|
|
|
|
auto& y_low = (*y)[0][ch];
|
|
|
|
|
|
|
|
|
|
// Compute spectra.
|
2019-09-25 12:09:37 +02:00
|
|
|
FormLinearFilterOutput(subtractor_output[ch], e[ch]);
|
2019-09-23 16:03:12 +02:00
|
|
|
WindowedPaddedFft(fft_, y_low, y_old_[ch], &Y[ch]);
|
2019-09-25 12:09:37 +02:00
|
|
|
WindowedPaddedFft(fft_, e[ch], e_old_[ch], &E[ch]);
|
2019-09-23 16:03:12 +02:00
|
|
|
LinearEchoPower(E[ch], Y[ch], &S2_linear[ch]);
|
|
|
|
|
Y[ch].Spectrum(optimization_, Y2[ch]);
|
|
|
|
|
E[ch].Spectrum(optimization_, E2[ch]);
|
|
|
|
|
}
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
// Update the AEC state information.
|
2019-09-23 16:03:12 +02:00
|
|
|
// TODO(bugs.webrtc.org/10913): Take all subtractors into account.
|
2019-09-25 14:53:30 +02:00
|
|
|
aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponse(),
|
|
|
|
|
subtractor_.FilterImpulseResponse(), *render_buffer, E2[0],
|
|
|
|
|
Y2[0], subtractor_output[0], y0);
|
2018-04-27 12:04:03 +02:00
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Choose the linear output.
|
2019-09-24 15:05:04 +02:00
|
|
|
const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y;
|
2019-09-23 16:03:12 +02:00
|
|
|
|
2019-09-02 17:01:19 +02:00
|
|
|
data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0], 16000, 1);
|
2019-09-25 12:09:37 +02:00
|
|
|
data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0][0], 16000, 1);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2019-09-23 16:03:12 +02:00
|
|
|
float high_bands_gain = 1.f;
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1> G;
|
|
|
|
|
G.fill(1.f);
|
|
|
|
|
|
|
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
|
|
|
|
// Estimate the residual echo power.
|
|
|
|
|
residual_echo_estimators_[ch]->Estimate(aec_state_, *render_buffer,
|
|
|
|
|
S2_linear[ch], Y2[ch], &R2[ch]);
|
|
|
|
|
|
|
|
|
|
// Estimate the comfort noise.
|
|
|
|
|
cngs_[ch]->Compute(aec_state_, Y2[ch], &comfort_noise[ch],
|
|
|
|
|
&high_band_comfort_noise[ch]);
|
|
|
|
|
|
|
|
|
|
// Suppressor echo estimate.
|
|
|
|
|
const auto& echo_spectrum =
|
|
|
|
|
aec_state_.UsableLinearEstimate() ? S2_linear[ch] : R2[ch];
|
|
|
|
|
|
|
|
|
|
// Suppressor nearend estimate.
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1> nearend_spectrum_bounded;
|
|
|
|
|
if (aec_state_.UsableLinearEstimate()) {
|
|
|
|
|
std::transform(E2[ch].begin(), E2[ch].end(), Y2[ch].begin(),
|
|
|
|
|
nearend_spectrum_bounded.begin(),
|
|
|
|
|
[](float a, float b) { return std::min(a, b); });
|
|
|
|
|
}
|
|
|
|
|
const auto& nearend_spectrum =
|
|
|
|
|
aec_state_.UsableLinearEstimate() ? nearend_spectrum_bounded : Y2[ch];
|
|
|
|
|
|
|
|
|
|
// Compute preferred gains for each channel. The minimum gain determines the
|
|
|
|
|
// final gain.
|
|
|
|
|
float high_bands_gain_channel;
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1> G_channel;
|
|
|
|
|
suppression_gains_[ch]->GetGain(nearend_spectrum, echo_spectrum, R2[ch],
|
|
|
|
|
cngs_[ch]->NoiseSpectrum(),
|
|
|
|
|
render_signal_analyzer_, aec_state_, x,
|
|
|
|
|
&high_bands_gain_channel, &G_channel);
|
|
|
|
|
|
|
|
|
|
high_bands_gain = std::min(high_bands_gain, high_bands_gain_channel);
|
|
|
|
|
std::transform(G.begin(), G.end(), G_channel.begin(), G.begin(),
|
2019-04-15 17:15:37 +02:00
|
|
|
[](float a, float b) { return std::min(a, b); });
|
|
|
|
|
}
|
2018-04-24 12:44:29 +02:00
|
|
|
|
2019-09-24 15:05:04 +02:00
|
|
|
suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
|
2018-04-24 12:44:29 +02:00
|
|
|
high_bands_gain, Y_fft, y);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-02-28 22:08:53 -08:00
|
|
|
// Update the metrics.
|
2019-09-23 16:03:12 +02:00
|
|
|
metrics_.Update(aec_state_, cngs_[0]->NoiseSpectrum(), G);
|
2017-02-28 22:08:53 -08:00
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Debug outputs for the purpose of development and analysis.
|
2017-07-11 02:54:02 -07:00
|
|
|
data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize,
|
2019-09-10 18:05:17 +02:00
|
|
|
&subtractor_output[0].s_main[0], 16000, 1);
|
2017-07-11 02:54:02 -07:00
|
|
|
data_dumper_->DumpRaw("aec3_output", y0);
|
2017-07-11 06:13:43 -07:00
|
|
|
data_dumper_->DumpRaw("aec3_narrow_render",
|
|
|
|
|
render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
|
2019-09-23 16:03:12 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_N2", cngs_[0]->NoiseSpectrum());
|
2017-02-23 05:16:26 -08:00
|
|
|
data_dumper_->DumpRaw("aec3_suppressor_gain", G);
|
2019-09-02 17:01:19 +02:00
|
|
|
data_dumper_->DumpWav(
|
|
|
|
|
"aec3_output", rtc::ArrayView<const float>(&y0[0], kBlockSize), 16000, 1);
|
2019-09-10 18:05:17 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_using_subtractor_output[0]",
|
2018-03-22 00:29:25 +01:00
|
|
|
aec_state_.UseLinearFilterOutput() ? 1 : 0);
|
2019-09-10 18:05:17 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_E2", E2[0]);
|
|
|
|
|
data_dumper_->DumpRaw("aec3_S2_linear", S2_linear[0]);
|
|
|
|
|
data_dumper_->DumpRaw("aec3_Y2", Y2[0]);
|
Improves in the ERLE estimation for AEC3
The estimation on how well the linear filter in the AEC3 is performing
is done through an estimation of the ERLE. That estimation is then
used for knowing how much the suppressor needs to react in order to
cancel all the echoes.
In the current code, the ERLE is quite conservative during farend
inactivity and it is common that it goes to a minimum value during
those periods. Under highly varying conditions, that is probably the
right approach. However, in other scenarios where conditions does not
change that fast there is a loss in transparency that could be avoided
by means of a different ERLE estimation.
In the current CL, the ERLE estimation has been changed in the
following way:
- During farend activity the ERLE is estimated through a 1st order AR
smoother. This smoother goes faster toward lower ERLE values than to
larger ones in order to avoid overestimation of this
value. Furthermore, during the beginning of the farend burst, an
estimation of the ERLE is done that aim to represent the performance
of the linear filter during onsets. Under highly variant environments,
those quantities, the ERLE during onsets and the one computed during
the whole farend duration, would differ a lot. If the environment is
more stationary, those quantities would be much more similar.
- During nearend activity the ERLE estimation is decreased toward a
value of the ERLE during onsets.
Bug: webrtc:9040
Change-Id: Ieab86370a4333d2d0cd7041047d29651de4f6827
Reviewed-on: https://webrtc-review.googlesource.com/62342
Commit-Queue: Jesus de Vicente Pena <devicentepena@webrtc.org>
Reviewed-by: Per Åhgren <peah@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22568}
2018-03-22 14:53:23 +01:00
|
|
|
data_dumper_->DumpRaw(
|
2019-09-05 09:35:10 +02:00
|
|
|
"aec3_X2",
|
|
|
|
|
render_buffer->Spectrum(aec_state_.FilterDelayBlocks(), /*channel=*/0));
|
2019-09-10 18:05:17 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_R2", R2[0]);
|
2018-06-13 15:13:55 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_R2_reverb",
|
2019-09-23 16:03:12 +02:00
|
|
|
residual_echo_estimators_[0]->GetReverbPowerSpectrum());
|
2018-03-22 00:29:25 +01:00
|
|
|
data_dumper_->DumpRaw("aec3_filter_delay", aec_state_.FilterDelayBlocks());
|
2017-02-23 05:16:26 -08:00
|
|
|
data_dumper_->DumpRaw("aec3_capture_saturation",
|
|
|
|
|
aec_state_.SaturatedCapture() ? 1 : 0);
|
|
|
|
|
}
|
2017-01-27 03:28:19 -08:00
|
|
|
|
2018-08-10 18:37:38 +02:00
|
|
|
void EchoRemoverImpl::FormLinearFilterOutput(
|
|
|
|
|
const SubtractorOutput& subtractor_output,
|
|
|
|
|
rtc::ArrayView<float> output) {
|
|
|
|
|
RTC_DCHECK_EQ(subtractor_output.e_main.size(), output.size());
|
|
|
|
|
RTC_DCHECK_EQ(subtractor_output.e_shadow.size(), output.size());
|
|
|
|
|
bool use_main_output = true;
|
|
|
|
|
if (use_shadow_filter_output_) {
|
2018-08-29 13:34:07 +02:00
|
|
|
// As the output of the main adaptive filter generally should be better
|
|
|
|
|
// than the shadow filter output, add a margin and threshold for when
|
|
|
|
|
// choosing the shadow filter output.
|
2018-08-10 18:37:38 +02:00
|
|
|
if (subtractor_output.e2_shadow < 0.9f * subtractor_output.e2_main &&
|
|
|
|
|
subtractor_output.y2 > 30.f * 30.f * kBlockSize &&
|
|
|
|
|
(subtractor_output.s2_main > 60.f * 60.f * kBlockSize ||
|
|
|
|
|
subtractor_output.s2_shadow > 60.f * 60.f * kBlockSize)) {
|
|
|
|
|
use_main_output = false;
|
|
|
|
|
} else {
|
|
|
|
|
// If the main filter is diverged, choose the filter output that has the
|
|
|
|
|
// lowest power.
|
|
|
|
|
if (subtractor_output.e2_shadow < subtractor_output.e2_main &&
|
|
|
|
|
subtractor_output.y2 < subtractor_output.e2_main) {
|
|
|
|
|
use_main_output = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-24 16:31:01 +02:00
|
|
|
SignalTransition(
|
|
|
|
|
main_filter_output_last_selected_ ? subtractor_output.e_main
|
|
|
|
|
: subtractor_output.e_shadow,
|
|
|
|
|
use_main_output ? subtractor_output.e_main : subtractor_output.e_shadow,
|
|
|
|
|
output);
|
2018-08-10 18:37:38 +02:00
|
|
|
main_filter_output_last_selected_ = use_main_output;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
} // namespace
|
|
|
|
|
|
2017-10-18 12:32:42 +02:00
|
|
|
EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config,
|
2019-09-02 17:01:19 +02:00
|
|
|
int sample_rate_hz,
|
|
|
|
|
size_t num_render_channels,
|
|
|
|
|
size_t num_capture_channels) {
|
|
|
|
|
return new EchoRemoverImpl(config, sample_rate_hz, num_render_channels,
|
|
|
|
|
num_capture_channels);
|
2017-01-27 03:28:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|