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/subtractor.h"
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
2017-10-15 20:19:21 +02:00
|
|
|
#include <numeric>
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/array_view.h"
|
|
|
|
|
#include "modules/audio_processing/logging/apm_data_dumper.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
2017-11-22 10:42:26 +01:00
|
|
|
#include "rtc_base/numerics/safe_minmax.h"
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
void PredictionError(const Aec3Fft& fft,
|
|
|
|
|
const FftData& S,
|
|
|
|
|
rtc::ArrayView<const float> y,
|
|
|
|
|
std::array<float, kBlockSize>* e,
|
2017-07-11 02:54:02 -07:00
|
|
|
FftData* E,
|
|
|
|
|
std::array<float, kBlockSize>* s) {
|
|
|
|
|
std::array<float, kFftLength> s_scratch;
|
|
|
|
|
fft.Ifft(S, &s_scratch);
|
2017-02-23 05:16:26 -08:00
|
|
|
constexpr float kScale = 1.0f / kFftLengthBy2;
|
2017-07-11 02:54:02 -07:00
|
|
|
std::transform(y.begin(), y.end(), s_scratch.begin() + kFftLengthBy2,
|
|
|
|
|
e->begin(), [&](float a, float b) { return a - b * kScale; });
|
2017-06-12 11:40:47 -07:00
|
|
|
std::for_each(e->begin(), e->end(),
|
|
|
|
|
[](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
|
2017-02-23 05:16:26 -08:00
|
|
|
fft.ZeroPaddedFft(*e, E);
|
2017-07-11 02:54:02 -07:00
|
|
|
|
|
|
|
|
if (s) {
|
|
|
|
|
for (size_t k = 0; k < s->size(); ++k) {
|
|
|
|
|
(*s)[k] = kScale * s_scratch[k + kFftLengthBy2];
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-23 05:16:26 -08:00
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2017-12-11 22:28:45 +01:00
|
|
|
Subtractor::Subtractor(const EchoCanceller3Config& config,
|
|
|
|
|
ApmDataDumper* data_dumper,
|
2017-02-23 05:16:26 -08:00
|
|
|
Aec3Optimization optimization)
|
2017-02-23 06:27:03 -08:00
|
|
|
: fft_(),
|
|
|
|
|
data_dumper_(data_dumper),
|
2017-02-23 05:16:26 -08:00
|
|
|
optimization_(optimization),
|
2017-12-11 22:28:45 +01:00
|
|
|
main_filter_(config.filter.length_blocks, optimization, data_dumper_),
|
2017-12-11 23:29:44 +01:00
|
|
|
shadow_filter_(config.filter.length_blocks, optimization, data_dumper_),
|
|
|
|
|
G_main_(config.filter.leakage_converged,
|
|
|
|
|
config.filter.leakage_diverged,
|
2017-12-12 22:49:41 +01:00
|
|
|
config.filter.main_noise_gate,
|
|
|
|
|
config.filter.error_floor),
|
2017-12-11 23:29:44 +01:00
|
|
|
G_shadow_(config.filter.shadow_rate, config.filter.shadow_noise_gate) {
|
2017-02-23 05:16:26 -08:00
|
|
|
RTC_DCHECK(data_dumper_);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 02:54:02 -07:00
|
|
|
Subtractor::~Subtractor() = default;
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
void Subtractor::HandleEchoPathChange(
|
|
|
|
|
const EchoPathVariability& echo_path_variability) {
|
2017-12-01 23:01:44 +01:00
|
|
|
const auto full_reset = [&]() {
|
2017-02-23 05:16:26 -08:00
|
|
|
main_filter_.HandleEchoPathChange();
|
|
|
|
|
shadow_filter_.HandleEchoPathChange();
|
2017-12-01 23:01:44 +01:00
|
|
|
G_main_.HandleEchoPathChange(echo_path_variability);
|
2017-05-03 05:39:09 -07:00
|
|
|
G_shadow_.HandleEchoPathChange();
|
2017-10-15 20:19:21 +02:00
|
|
|
converged_filter_ = false;
|
2017-12-01 23:01:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO(peah): Add delay-change specific reset behavior.
|
|
|
|
|
if ((echo_path_variability.delay_change ==
|
|
|
|
|
EchoPathVariability::DelayAdjustment::kBufferFlush) ||
|
|
|
|
|
(echo_path_variability.delay_change ==
|
|
|
|
|
EchoPathVariability::DelayAdjustment::kDelayReset)) {
|
|
|
|
|
full_reset();
|
|
|
|
|
} else if (echo_path_variability.delay_change ==
|
|
|
|
|
EchoPathVariability::DelayAdjustment::kNewDetectedDelay) {
|
|
|
|
|
full_reset();
|
|
|
|
|
} else if (echo_path_variability.delay_change ==
|
|
|
|
|
EchoPathVariability::DelayAdjustment::kBufferReadjustment) {
|
|
|
|
|
full_reset();
|
2017-02-23 05:16:26 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 14:18:07 -07:00
|
|
|
void Subtractor::Process(const RenderBuffer& render_buffer,
|
2017-02-23 05:16:26 -08:00
|
|
|
const rtc::ArrayView<const float> capture,
|
|
|
|
|
const RenderSignalAnalyzer& render_signal_analyzer,
|
2017-04-06 15:45:32 -07:00
|
|
|
const AecState& aec_state,
|
2017-02-23 05:16:26 -08:00
|
|
|
SubtractorOutput* output) {
|
|
|
|
|
RTC_DCHECK_EQ(kBlockSize, capture.size());
|
|
|
|
|
rtc::ArrayView<const float> y = capture;
|
|
|
|
|
FftData& E_main = output->E_main;
|
2017-04-06 15:45:32 -07:00
|
|
|
FftData E_shadow;
|
2017-02-23 05:16:26 -08:00
|
|
|
std::array<float, kBlockSize>& e_main = output->e_main;
|
|
|
|
|
std::array<float, kBlockSize>& e_shadow = output->e_shadow;
|
|
|
|
|
|
|
|
|
|
FftData S;
|
|
|
|
|
FftData& G = S;
|
|
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Form the output of the main filter.
|
|
|
|
|
main_filter_.Filter(render_buffer, &S);
|
2017-07-11 02:54:02 -07:00
|
|
|
PredictionError(fft_, S, y, &e_main, &E_main, &output->s_main);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-04-06 15:45:32 -07:00
|
|
|
// Form the output of the shadow filter.
|
|
|
|
|
shadow_filter_.Filter(render_buffer, &S);
|
2017-07-11 02:54:02 -07:00
|
|
|
PredictionError(fft_, S, y, &e_shadow, &E_shadow, nullptr);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
2017-10-25 02:59:45 +02:00
|
|
|
|
2017-10-15 20:19:21 +02:00
|
|
|
if (!converged_filter_) {
|
2017-12-06 11:32:38 +01:00
|
|
|
const auto sum_of_squares = [](float a, float b) { return a + b * b; };
|
|
|
|
|
const float e2_main =
|
|
|
|
|
std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
|
|
|
|
|
const float e2_shadow =
|
|
|
|
|
std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
|
|
|
|
|
const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
|
|
|
|
|
|
|
|
|
|
if (y2 > kBlockSize * 50.f * 50.f) {
|
|
|
|
|
converged_filter_ = (e2_main > 0.3 * y2 || e2_shadow > 0.1 * y2);
|
2017-10-15 20:19:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
// Compute spectra for future use.
|
2017-12-01 23:01:44 +01:00
|
|
|
E_main.Spectrum(optimization_, output->E2_main);
|
|
|
|
|
E_shadow.Spectrum(optimization_, output->E2_shadow);
|
2017-02-23 05:16:26 -08:00
|
|
|
|
|
|
|
|
// Update the main filter.
|
2017-04-06 15:45:32 -07:00
|
|
|
G_main_.Compute(render_buffer, render_signal_analyzer, *output, main_filter_,
|
|
|
|
|
aec_state.SaturatedCapture(), &G);
|
|
|
|
|
main_filter_.Adapt(render_buffer, G);
|
2017-02-23 05:16:26 -08:00
|
|
|
data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
|
|
|
|
|
data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
|
|
|
|
|
|
|
|
|
|
// Update the shadow filter.
|
2017-04-06 15:45:32 -07:00
|
|
|
G_shadow_.Compute(render_buffer, render_signal_analyzer, E_shadow,
|
|
|
|
|
shadow_filter_.SizePartitions(),
|
|
|
|
|
aec_state.SaturatedCapture(), &G);
|
|
|
|
|
shadow_filter_.Adapt(render_buffer, G);
|
|
|
|
|
|
2017-02-23 05:16:26 -08:00
|
|
|
data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
|
|
|
|
|
data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
|
|
|
|
|
|
|
|
|
|
main_filter_.DumpFilter("aec3_subtractor_H_main");
|
|
|
|
|
shadow_filter_.DumpFilter("aec3_subtractor_H_shadow");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|