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>
|
2022-06-27 09:47:02 +02:00
|
|
|
#include <atomic>
|
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"
|
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.
|
2022-05-23 10:39:53 +02:00
|
|
|
void ProcessCapture(EchoPathVariability echo_path_variability,
|
|
|
|
|
bool capture_signal_saturation,
|
|
|
|
|
const absl::optional<DelayEstimate>& external_delay,
|
|
|
|
|
RenderBuffer* render_buffer,
|
|
|
|
|
Block* linear_output,
|
|
|
|
|
Block* 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
|
|
|
|
2021-03-11 06:33:45 +00:00
|
|
|
void SetCaptureOutputUsage(bool capture_output_used) override {
|
|
|
|
|
capture_output_used_ = capture_output_used;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 03:28:19 -08:00
|
|
|
private:
|
2020-03-20 11:26:48 +01:00
|
|
|
// Selects which of the coarse and refined 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
|
|
|
|
2022-06-27 09:47:02 +02:00
|
|
|
static std::atomic<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_;
|
2020-03-20 11:26:48 +01:00
|
|
|
const bool use_coarse_filter_output_;
|
2019-09-25 14:53:30 +02:00
|
|
|
Subtractor subtractor_;
|
2019-11-05 15:19:02 +01:00
|
|
|
SuppressionGain suppression_gain_;
|
2019-10-31 14:10:24 +01:00
|
|
|
ComfortNoiseGenerator cng_;
|
2017-02-23 05:16:26 -08:00
|
|
|
SuppressionFilter suppression_filter_;
|
|
|
|
|
RenderSignalAnalyzer render_signal_analyzer_;
|
2019-10-08 12:35:47 +02:00
|
|
|
ResidualEchoEstimator residual_echo_estimator_;
|
2017-02-23 05:16:26 -08:00
|
|
|
bool echo_leakage_detected_ = false;
|
2021-03-11 06:33:45 +00:00
|
|
|
bool capture_output_used_ = true;
|
2017-02-23 05:16:26 -08:00
|
|
|
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;
|
2020-03-20 11:20:39 +01:00
|
|
|
bool refined_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_;
|
2021-06-11 14:02:53 +02:00
|
|
|
std::vector<std::array<float, kFftLengthBy2Plus1>> R2_unbounded_heap_;
|
2019-09-10 18:05:17 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2022-06-27 09:47:02 +02:00
|
|
|
std::atomic<int> EchoRemoverImpl::instance_count_(0);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
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_(),
|
2022-06-27 09:47:02 +02:00
|
|
|
data_dumper_(new ApmDataDumper(instance_count_.fetch_add(1) + 1)),
|
2017-02-23 05:16:26 -08:00
|
|
|
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),
|
2020-03-20 11:26:48 +01:00
|
|
|
use_coarse_filter_output_(
|
|
|
|
|
config_.filter.enable_coarse_filter_output_usage),
|
2019-09-25 14:53:30 +02:00
|
|
|
subtractor_(config,
|
|
|
|
|
num_render_channels_,
|
|
|
|
|
num_capture_channels_,
|
|
|
|
|
data_dumper_.get(),
|
|
|
|
|
optimization_),
|
2019-11-05 15:19:02 +01:00
|
|
|
suppression_gain_(config_,
|
|
|
|
|
optimization_,
|
|
|
|
|
sample_rate_hz,
|
|
|
|
|
num_capture_channels),
|
2020-03-25 07:31:47 +01:00
|
|
|
cng_(config_, optimization_, 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-10-08 12:35:47 +02:00
|
|
|
residual_echo_estimator_(config_, num_render_channels),
|
2019-10-01 12:47:53 +02:00
|
|
|
aec_state_(config_, num_capture_channels_),
|
2019-11-05 15:19:02 +01:00
|
|
|
e_old_(num_capture_channels_, {0.f}),
|
|
|
|
|
y_old_(num_capture_channels_, {0.f}),
|
|
|
|
|
e_heap_(NumChannelsOnHeap(num_capture_channels_), {0.f}),
|
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_)),
|
2021-06-11 14:02:53 +02:00
|
|
|
R2_unbounded_heap_(NumChannelsOnHeap(num_capture_channels_)),
|
2019-09-10 18:05:17 +02:00
|
|
|
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));
|
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,
|
2022-05-23 10:39:53 +02:00
|
|
|
Block* linear_output,
|
|
|
|
|
Block* capture) {
|
2018-07-16 17:08:41 +02:00
|
|
|
++block_counter_;
|
2022-05-24 10:49:50 +03:00
|
|
|
const Block& x = render_buffer->GetBlock(0);
|
2022-05-23 10:39:53 +02:00
|
|
|
Block* 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);
|
2022-05-23 10:39:53 +02:00
|
|
|
RTC_DCHECK_EQ(x.NumBands(), NumBandsForRate(sample_rate_hz_));
|
|
|
|
|
RTC_DCHECK_EQ(y->NumBands(), NumBandsForRate(sample_rate_hz_));
|
|
|
|
|
RTC_DCHECK_EQ(x.NumChannels(), num_render_channels_);
|
|
|
|
|
RTC_DCHECK_EQ(y->NumChannels(), num_capture_channels_);
|
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;
|
2021-06-11 14:02:53 +02:00
|
|
|
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack>
|
|
|
|
|
R2_unbounded_stack;
|
2019-09-10 18:05:17 +02:00
|
|
|
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_);
|
2021-06-11 14:02:53 +02:00
|
|
|
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> R2_unbounded(
|
|
|
|
|
R2_unbounded_stack.data(), num_capture_channels_);
|
2019-09-10 18:05:17 +02:00
|
|
|
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_);
|
2021-06-11 14:02:53 +02:00
|
|
|
R2_unbounded = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>(
|
|
|
|
|
R2_unbounded_heap_.data(), num_capture_channels_);
|
2019-09-10 18:05:17 +02:00
|
|
|
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_);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 10:39:53 +02:00
|
|
|
data_dumper_->DumpWav("aec3_echo_remover_capture_input",
|
|
|
|
|
y->View(/*band=*/0, /*channel=*/0), 16000, 1);
|
|
|
|
|
data_dumper_->DumpWav("aec3_echo_remover_render_input",
|
|
|
|
|
x.View(/*band=*/0, /*channel=*/0), 16000, 1);
|
|
|
|
|
data_dumper_->DumpRaw("aec3_echo_remover_capture_input",
|
|
|
|
|
y->View(/*band=*/0, /*channel=*/0));
|
|
|
|
|
data_dumper_->DumpRaw("aec3_echo_remover_render_input",
|
|
|
|
|
x.View(/*band=*/0, /*channel=*/0));
|
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-10-15 13:43:02 +02:00
|
|
|
rtc::LoggingSeverity log_level =
|
|
|
|
|
config_.delay.log_warning_on_delay_changes ? rtc::LS_WARNING
|
2020-01-14 12:58:12 +01:00
|
|
|
: rtc::LS_VERBOSE;
|
2019-10-15 13:43:02 +02:00
|
|
|
RTC_LOG_V(log_level)
|
|
|
|
|
<< "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-11-05 15:19:02 +01:00
|
|
|
suppression_gain_.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,
|
2019-10-15 10:31:35 +02:00
|
|
|
aec_state_.MinDirectPathFilterDelay());
|
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-11-05 15:19:02 +01:00
|
|
|
suppression_gain_.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.
|
2022-05-23 10:39:53 +02:00
|
|
|
subtractor_.Process(*render_buffer, *y, render_signal_analyzer_, aec_state_,
|
|
|
|
|
subtractor_output);
|
2019-09-25 14:53:30 +02:00
|
|
|
|
2019-10-18 08:50:50 +02:00
|
|
|
// Compute spectra.
|
2019-09-23 16:03:12 +02:00
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
2019-09-25 12:09:37 +02:00
|
|
|
FormLinearFilterOutput(subtractor_output[ch], e[ch]);
|
2022-05-23 10:39:53 +02:00
|
|
|
WindowedPaddedFft(fft_, y->View(/*band=*/0, ch), 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
|
|
|
|
2019-11-13 11:12:29 +01:00
|
|
|
// Optionally return the linear filter output.
|
|
|
|
|
if (linear_output) {
|
2022-05-23 10:39:53 +02:00
|
|
|
RTC_DCHECK_GE(1, linear_output->NumBands());
|
|
|
|
|
RTC_DCHECK_EQ(num_capture_channels_, linear_output->NumChannels());
|
2019-11-13 11:12:29 +01:00
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
2022-05-23 10:39:53 +02:00
|
|
|
std::copy(e[ch].begin(), e[ch].end(),
|
|
|
|
|
linear_output->begin(/*band=*/0, ch));
|
2019-11-13 11:12:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Update the AEC state information.
|
2019-10-18 08:50:50 +02:00
|
|
|
aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponses(),
|
|
|
|
|
subtractor_.FilterImpulseResponses(), *render_buffer, E2,
|
|
|
|
|
Y2, subtractor_output);
|
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
|
|
|
|
2022-05-23 10:39:53 +02:00
|
|
|
data_dumper_->DumpWav("aec3_output_linear",
|
|
|
|
|
y->View(/*band=*/0, /*channel=*/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-10-31 14:10:24 +01:00
|
|
|
// Estimate the comfort noise.
|
|
|
|
|
cng_.Compute(aec_state_.SaturatedCapture(), Y2, comfort_noise,
|
|
|
|
|
high_band_comfort_noise);
|
2019-09-23 16:03:12 +02:00
|
|
|
|
2021-03-11 06:33:45 +00:00
|
|
|
// Only do the below processing if the output of the audio processing module
|
|
|
|
|
// is used.
|
|
|
|
|
std::array<float, kFftLengthBy2Plus1> G;
|
|
|
|
|
if (capture_output_used_) {
|
|
|
|
|
// Estimate the residual echo power.
|
|
|
|
|
residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2,
|
2021-06-11 14:02:53 +02:00
|
|
|
suppression_gain_.IsDominantNearend(), R2,
|
|
|
|
|
R2_unbounded);
|
2021-03-11 06:33:45 +00:00
|
|
|
|
|
|
|
|
// Suppressor nearend estimate.
|
|
|
|
|
if (aec_state_.UsableLinearEstimate()) {
|
|
|
|
|
// E2 is bound by Y2.
|
|
|
|
|
for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
|
|
|
|
|
std::transform(E2[ch].begin(), E2[ch].end(), Y2[ch].begin(),
|
|
|
|
|
E2[ch].begin(),
|
|
|
|
|
[](float a, float b) { return std::min(a, b); });
|
|
|
|
|
}
|
2019-09-23 16:03:12 +02:00
|
|
|
}
|
2021-03-11 06:33:45 +00:00
|
|
|
const auto& nearend_spectrum = aec_state_.UsableLinearEstimate() ? E2 : Y2;
|
2019-11-05 15:19:02 +01:00
|
|
|
|
2021-03-11 06:33:45 +00:00
|
|
|
// Suppressor echo estimate.
|
|
|
|
|
const auto& echo_spectrum =
|
|
|
|
|
aec_state_.UsableLinearEstimate() ? S2_linear : R2;
|
2019-11-05 15:19:02 +01:00
|
|
|
|
2021-03-11 06:33:45 +00:00
|
|
|
// Determine if the suppressor should assume clock drift.
|
|
|
|
|
const bool clock_drift = config_.echo_removal_control.has_clock_drift ||
|
|
|
|
|
echo_path_variability.clock_drift;
|
2020-10-22 14:36:37 +02:00
|
|
|
|
2021-03-11 06:33:45 +00:00
|
|
|
// Compute preferred gains.
|
|
|
|
|
float high_bands_gain;
|
2021-06-11 14:02:53 +02:00
|
|
|
suppression_gain_.GetGain(nearend_spectrum, echo_spectrum, R2, R2_unbounded,
|
2021-03-11 06:33:45 +00:00
|
|
|
cng_.NoiseSpectrum(), render_signal_analyzer_,
|
|
|
|
|
aec_state_, x, clock_drift, &high_bands_gain, &G);
|
|
|
|
|
|
|
|
|
|
suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
|
|
|
|
|
high_bands_gain, Y_fft, y);
|
2018-04-24 12:44:29 +02:00
|
|
|
|
2021-03-11 06:33:45 +00:00
|
|
|
} else {
|
|
|
|
|
G.fill(0.f);
|
|
|
|
|
}
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-02-28 22:08:53 -08:00
|
|
|
// Update the metrics.
|
2019-10-31 14:10:24 +01:00
|
|
|
metrics_.Update(aec_state_, cng_.NoiseSpectrum()[0], 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,
|
2020-03-20 11:20:39 +01:00
|
|
|
&subtractor_output[0].s_refined[0], 16000, 1);
|
2022-05-23 10:39:53 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_output", y->View(/*band=*/0, /*channel=*/0));
|
2017-07-11 06:13:43 -07:00
|
|
|
data_dumper_->DumpRaw("aec3_narrow_render",
|
|
|
|
|
render_signal_analyzer_.NarrowPeakBand() ? 1 : 0);
|
2019-10-31 14:10:24 +01:00
|
|
|
data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum()[0]);
|
2017-02-23 05:16:26 -08:00
|
|
|
data_dumper_->DumpRaw("aec3_suppressor_gain", G);
|
2022-05-23 10:39:53 +02:00
|
|
|
data_dumper_->DumpWav("aec3_output", y->View(/*band=*/0, /*channel=*/0),
|
2019-10-18 08:50:50 +02:00
|
|
|
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-10-18 08:20:09 +02:00
|
|
|
"aec3_X2", render_buffer->Spectrum(
|
|
|
|
|
aec_state_.MinDirectPathFilterDelay())[/*channel=*/0]);
|
2019-09-10 18:05:17 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_R2", R2[0]);
|
2019-10-15 10:31:35 +02:00
|
|
|
data_dumper_->DumpRaw("aec3_filter_delay",
|
|
|
|
|
aec_state_.MinDirectPathFilterDelay());
|
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) {
|
2020-03-20 11:20:39 +01:00
|
|
|
RTC_DCHECK_EQ(subtractor_output.e_refined.size(), output.size());
|
2020-03-20 11:26:48 +01:00
|
|
|
RTC_DCHECK_EQ(subtractor_output.e_coarse.size(), output.size());
|
2020-03-20 11:20:39 +01:00
|
|
|
bool use_refined_output = true;
|
2020-03-20 11:26:48 +01:00
|
|
|
if (use_coarse_filter_output_) {
|
2020-03-20 11:20:39 +01:00
|
|
|
// As the output of the refined adaptive filter generally should be better
|
2020-03-20 11:26:48 +01:00
|
|
|
// than the coarse filter output, add a margin and threshold for when
|
|
|
|
|
// choosing the coarse filter output.
|
|
|
|
|
if (subtractor_output.e2_coarse < 0.9f * subtractor_output.e2_refined &&
|
2018-08-10 18:37:38 +02:00
|
|
|
subtractor_output.y2 > 30.f * 30.f * kBlockSize &&
|
2020-03-20 11:20:39 +01:00
|
|
|
(subtractor_output.s2_refined > 60.f * 60.f * kBlockSize ||
|
2020-03-20 11:26:48 +01:00
|
|
|
subtractor_output.s2_coarse > 60.f * 60.f * kBlockSize)) {
|
2020-03-20 11:20:39 +01:00
|
|
|
use_refined_output = false;
|
2018-08-10 18:37:38 +02:00
|
|
|
} else {
|
2020-03-20 11:20:39 +01:00
|
|
|
// If the refined filter is diverged, choose the filter output that has
|
|
|
|
|
// the lowest power.
|
2020-03-20 11:26:48 +01:00
|
|
|
if (subtractor_output.e2_coarse < subtractor_output.e2_refined &&
|
2020-03-20 11:20:39 +01:00
|
|
|
subtractor_output.y2 < subtractor_output.e2_refined) {
|
|
|
|
|
use_refined_output = false;
|
2018-08-10 18:37:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 11:20:39 +01:00
|
|
|
SignalTransition(refined_filter_output_last_selected_
|
|
|
|
|
? subtractor_output.e_refined
|
2020-03-20 11:26:48 +01:00
|
|
|
: subtractor_output.e_coarse,
|
2020-03-20 11:20:39 +01:00
|
|
|
use_refined_output ? subtractor_output.e_refined
|
2020-03-20 11:26:48 +01:00
|
|
|
: subtractor_output.e_coarse,
|
2020-03-20 11:20:39 +01:00
|
|
|
output);
|
|
|
|
|
refined_filter_output_last_selected_ = use_refined_output;
|
2018-08-10 18:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
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
|