| 1 |
/* |
| 2 |
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license |
| 5 |
* that can be found in the LICENSE file in the root of the source |
| 6 |
* tree. An additional intellectual property rights grant can be found |
| 7 |
* in the file PATENTS. All contributing project authors may |
| 8 |
* be found in the AUTHORS file in the root of the source tree. |
| 9 |
*/ |
| 10 |
#include "modules/audio_processing/aec3/echo_remover.h" |
| 11 |
|
| 12 |
#include <math.h> |
| 13 |
#include <stddef.h> |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
#include <array> |
| 17 |
#include <cmath> |
| 18 |
#include <memory> |
| 19 |
|
| 20 |
#include "api/array_view.h" |
| 21 |
#include "modules/audio_processing/aec3/aec3_common.h" |
| 22 |
#include "modules/audio_processing/aec3/aec3_fft.h" |
| 23 |
#include "modules/audio_processing/aec3/aec_state.h" |
| 24 |
#include "modules/audio_processing/aec3/comfort_noise_generator.h" |
| 25 |
#include "modules/audio_processing/aec3/echo_path_variability.h" |
| 26 |
#include "modules/audio_processing/aec3/echo_remover_metrics.h" |
| 27 |
#include "modules/audio_processing/aec3/fft_data.h" |
| 28 |
#include "modules/audio_processing/aec3/render_buffer.h" |
| 29 |
#include "modules/audio_processing/aec3/render_signal_analyzer.h" |
| 30 |
#include "modules/audio_processing/aec3/residual_echo_estimator.h" |
| 31 |
#include "modules/audio_processing/aec3/subtractor.h" |
| 32 |
#include "modules/audio_processing/aec3/subtractor_output.h" |
| 33 |
#include "modules/audio_processing/aec3/suppression_filter.h" |
| 34 |
#include "modules/audio_processing/aec3/suppression_gain.h" |
| 35 |
#include "modules/audio_processing/logging/apm_data_dumper.h" |
| 36 |
#include "rtc_base/atomic_ops.h" |
| 37 |
#include "rtc_base/checks.h" |
| 38 |
#include "rtc_base/logging.h" |
| 39 |
|
| 40 |
namespace webrtc { |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
// Maximum number of channels for which the capture channel data is stored on |
| 45 |
// the stack. If the number of channels are larger than this, they are stored |
| 46 |
// using scratch memory that is pre-allocated on the heap. The reason for this |
| 47 |
// partitioning is not to waste heap space for handling the more common numbers |
| 48 |
// of channels, while at the same time not limiting the support for higher |
| 49 |
// numbers of channels by enforcing the capture channel data to be stored on the |
| 50 |
// stack using a fixed maximum value. |
| 51 |
constexpr size_t kMaxNumChannelsOnStack = 2; |
| 52 |
|
| 53 |
// Chooses the number of channels to store on the heap when that is required due |
| 54 |
// to the number of capture channels being larger than the pre-defined number |
| 55 |
// of channels to store on the stack. |
| 56 |
size_t NumChannelsOnHeap(size_t num_capture_channels) { |
| 57 |
return num_capture_channels > kMaxNumChannelsOnStack ? num_capture_channels |
| 58 |
: 0; |
| 59 |
} |
| 60 |
|
| 61 |
void LinearEchoPower(const FftData& E, |
| 62 |
const FftData& Y, |
| 63 |
std::array<float, kFftLengthBy2Plus1>* S2) { |
| 64 |
for (size_t k = 0; k < E.re.size(); ++k) { |
| 65 |
(*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) + |
| 66 |
(Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// Fades between two input signals using a fix-sized transition. |
| 71 |
void SignalTransition(rtc::ArrayView<const float> from, |
| 72 |
rtc::ArrayView<const float> to, |
| 73 |
rtc::ArrayView<float> out) { |
| 74 |
if (from == to) { |
| 75 |
RTC_DCHECK_EQ(to.size(), out.size()); |
| 76 |
std::copy(to.begin(), to.end(), out.begin()); |
| 77 |
} else { |
| 78 |
constexpr size_t kTransitionSize = 30; |
| 79 |
constexpr float kOneByTransitionSizePlusOne = 1.f / (kTransitionSize + 1); |
| 80 |
|
| 81 |
RTC_DCHECK_EQ(from.size(), to.size()); |
| 82 |
RTC_DCHECK_EQ(from.size(), out.size()); |
| 83 |
RTC_DCHECK_LE(kTransitionSize, out.size()); |
| 84 |
|
| 85 |
for (size_t k = 0; k < kTransitionSize; ++k) { |
| 86 |
float a = (k + 1) * kOneByTransitionSizePlusOne; |
| 87 |
out[k] = a * to[k] + (1.f - a) * from[k]; |
| 88 |
} |
| 89 |
|
| 90 |
std::copy(to.begin() + kTransitionSize, to.end(), |
| 91 |
out.begin() + kTransitionSize); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Computes a windowed (square root Hanning) padded FFT and updates the related |
| 96 |
// memory. |
| 97 |
void WindowedPaddedFft(const Aec3Fft& fft, |
| 98 |
rtc::ArrayView<const float> v, |
| 99 |
rtc::ArrayView<float> v_old, |
| 100 |
FftData* V) { |
| 101 |
fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V); |
| 102 |
std::copy(v.begin(), v.end(), v_old.begin()); |
| 103 |
} |
| 104 |
|
| 105 |
// Class for removing the echo from the capture signal. |
| 106 |
class EchoRemoverImpl final : public EchoRemover { |
| 107 |
public: |
| 108 |
EchoRemoverImpl(const EchoCanceller3Config& config, |
| 109 |
int sample_rate_hz, |
| 110 |
size_t num_render_channels, |
| 111 |
size_t num_capture_channels); |
| 112 |
~EchoRemoverImpl() override; |
| 113 |
EchoRemoverImpl(const EchoRemoverImpl&) = delete; |
| 114 |
EchoRemoverImpl& operator=(const EchoRemoverImpl&) = delete; |
| 115 |
|
| 116 |
void GetMetrics(EchoControl::Metrics* metrics) const override; |
| 117 |
|
| 118 |
// Removes the echo from a block of samples from the capture signal. The |
| 119 |
// supplied render signal is assumed to be pre-aligned with the capture |
| 120 |
// signal. |
| 121 |
void ProcessCapture( |
| 122 |
EchoPathVariability echo_path_variability, |
| 123 |
bool capture_signal_saturation, |
| 124 |
const absl::optional<DelayEstimate>& external_delay, |
| 125 |
RenderBuffer* render_buffer, |
| 126 |
std::vector<std::vector<std::vector<float>>>* linear_output, |
| 127 |
std::vector<std::vector<std::vector<float>>>* capture) override; |
| 128 |
|
| 129 |
// Updates the status on whether echo leakage is detected in the output of the |
| 130 |
// echo remover. |
| 131 |
void UpdateEchoLeakageStatus(bool leakage_detected) override { |
| 132 |
echo_leakage_detected_ = leakage_detected; |
| 133 |
} |
| 134 |
|
| 135 |
void SetCaptureOutputUsage(bool capture_output_used) override { |
| 136 |
capture_output_used_ = capture_output_used; |
| 137 |
} |
| 138 |
|
| 139 |
private: |
| 140 |
// Selects which of the coarse and refined linear filter outputs that is most |
| 141 |
// appropriate to pass to the suppressor and forms the linear filter output by |
| 142 |
// smoothly transition between those. |
| 143 |
void FormLinearFilterOutput(const SubtractorOutput& subtractor_output, |
| 144 |
rtc::ArrayView<float> output); |
| 145 |
|
| 146 |
static int instance_count_; |
| 147 |
const EchoCanceller3Config config_; |
| 148 |
const Aec3Fft fft_; |
| 149 |
std::unique_ptr<ApmDataDumper> data_dumper_; |
| 150 |
const Aec3Optimization optimization_; |
| 151 |
const int sample_rate_hz_; |
| 152 |
const size_t num_render_channels_; |
| 153 |
const size_t num_capture_channels_; |
| 154 |
const bool use_coarse_filter_output_; |
| 155 |
Subtractor subtractor_; |
| 156 |
SuppressionGain suppression_gain_; |
| 157 |
ComfortNoiseGenerator cng_; |
| 158 |
SuppressionFilter suppression_filter_; |
| 159 |
RenderSignalAnalyzer render_signal_analyzer_; |
| 160 |
ResidualEchoEstimator residual_echo_estimator_; |
| 161 |
bool echo_leakage_detected_ = false; |
| 162 |
bool capture_output_used_ = true; |
| 163 |
AecState aec_state_; |
| 164 |
EchoRemoverMetrics metrics_; |
| 165 |
std::vector<std::array<float, kFftLengthBy2>> e_old_; |
| 166 |
std::vector<std::array<float, kFftLengthBy2>> y_old_; |
| 167 |
size_t block_counter_ = 0; |
| 168 |
int gain_change_hangover_ = 0; |
| 169 |
bool refined_filter_output_last_selected_ = true; |
| 170 |
|
| 171 |
std::vector<std::array<float, kFftLengthBy2>> e_heap_; |
| 172 |
std::vector<std::array<float, kFftLengthBy2Plus1>> Y2_heap_; |
| 173 |
std::vector<std::array<float, kFftLengthBy2Plus1>> E2_heap_; |
| 174 |
std::vector<std::array<float, kFftLengthBy2Plus1>> R2_heap_; |
| 175 |
std::vector<std::array<float, kFftLengthBy2Plus1>> S2_linear_heap_; |
| 176 |
std::vector<FftData> Y_heap_; |
| 177 |
std::vector<FftData> E_heap_; |
| 178 |
std::vector<FftData> comfort_noise_heap_; |
| 179 |
std::vector<FftData> high_band_comfort_noise_heap_; |
| 180 |
std::vector<SubtractorOutput> subtractor_output_heap_; |
| 181 |
}; |
| 182 |
|
| 183 |
int EchoRemoverImpl::instance_count_ = 0; |
| 184 |
|
| 185 |
EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config, |
| 186 |
int sample_rate_hz, |
| 187 |
size_t num_render_channels, |
| 188 |
size_t num_capture_channels) |
| 189 |
: config_(config), |
| 190 |
fft_(), |
| 191 |
data_dumper_( |
| 192 |
new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 193 |
optimization_(DetectOptimization()), |
| 194 |
sample_rate_hz_(sample_rate_hz), |
| 195 |
num_render_channels_(num_render_channels), |
| 196 |
num_capture_channels_(num_capture_channels), |
| 197 |
use_coarse_filter_output_( |
| 198 |
config_.filter.enable_coarse_filter_output_usage), |
| 199 |
subtractor_(config, |
| 200 |
num_render_channels_, |
| 201 |
num_capture_channels_, |
| 202 |
data_dumper_.get(), |
| 203 |
optimization_), |
| 204 |
suppression_gain_(config_, |
| 205 |
optimization_, |
| 206 |
sample_rate_hz, |
| 207 |
num_capture_channels), |
| 208 |
cng_(config_, optimization_, num_capture_channels_), |
| 209 |
suppression_filter_(optimization_, |
| 210 |
sample_rate_hz_, |
| 211 |
num_capture_channels_), |
| 212 |
render_signal_analyzer_(config_), |
| 213 |
residual_echo_estimator_(config_, num_render_channels), |
| 214 |
aec_state_(config_, num_capture_channels_), |
| 215 |
e_old_(num_capture_channels_, {0.f}), |
| 216 |
y_old_(num_capture_channels_, {0.f}), |
| 217 |
e_heap_(NumChannelsOnHeap(num_capture_channels_), {0.f}), |
| 218 |
Y2_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 219 |
E2_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 220 |
R2_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 221 |
S2_linear_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 222 |
Y_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 223 |
E_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 224 |
comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 225 |
high_band_comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 226 |
subtractor_output_heap_(NumChannelsOnHeap(num_capture_channels_)) { |
| 227 |
RTC_DCHECK(ValidFullBandRate(sample_rate_hz)); |
| 228 |
} |
| 229 |
|
| 230 |
EchoRemoverImpl::~EchoRemoverImpl() = default; |
| 231 |
|
| 232 |
void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const { |
| 233 |
// Echo return loss (ERL) is inverted to go from gain to attenuation. |
| 234 |
metrics->echo_return_loss = -10.0 * std::log10(aec_state_.ErlTimeDomain()); |
| 235 |
metrics->echo_return_loss_enhancement = |
| 236 |
Log2TodB(aec_state_.FullBandErleLog2()); |
| 237 |
} |
| 238 |
|
| 239 |
void EchoRemoverImpl::ProcessCapture( |
| 240 |
EchoPathVariability echo_path_variability, |
| 241 |
bool capture_signal_saturation, |
| 242 |
const absl::optional<DelayEstimate>& external_delay, |
| 243 |
RenderBuffer* render_buffer, |
| 244 |
std::vector<std::vector<std::vector<float>>>* linear_output, |
| 245 |
std::vector<std::vector<std::vector<float>>>* capture) { |
| 246 |
++block_counter_; |
| 247 |
const std::vector<std::vector<std::vector<float>>>& x = |
| 248 |
render_buffer->Block(0); |
| 249 |
std::vector<std::vector<std::vector<float>>>* y = capture; |
| 250 |
RTC_DCHECK(render_buffer); |
| 251 |
RTC_DCHECK(y); |
| 252 |
RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_)); |
| 253 |
RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_)); |
| 254 |
RTC_DCHECK_EQ(x[0].size(), num_render_channels_); |
| 255 |
RTC_DCHECK_EQ((*y)[0].size(), num_capture_channels_); |
| 256 |
RTC_DCHECK_EQ(x[0][0].size(), kBlockSize); |
| 257 |
RTC_DCHECK_EQ((*y)[0][0].size(), kBlockSize); |
| 258 |
|
| 259 |
// Stack allocated data to use when the number of channels is low. |
| 260 |
std::array<std::array<float, kFftLengthBy2>, kMaxNumChannelsOnStack> e_stack; |
| 261 |
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 262 |
Y2_stack; |
| 263 |
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 264 |
E2_stack; |
| 265 |
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 266 |
R2_stack; |
| 267 |
std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 268 |
S2_linear_stack; |
| 269 |
std::array<FftData, kMaxNumChannelsOnStack> Y_stack; |
| 270 |
std::array<FftData, kMaxNumChannelsOnStack> E_stack; |
| 271 |
std::array<FftData, kMaxNumChannelsOnStack> comfort_noise_stack; |
| 272 |
std::array<FftData, kMaxNumChannelsOnStack> high_band_comfort_noise_stack; |
| 273 |
std::array<SubtractorOutput, kMaxNumChannelsOnStack> subtractor_output_stack; |
| 274 |
|
| 275 |
rtc::ArrayView<std::array<float, kFftLengthBy2>> e(e_stack.data(), |
| 276 |
num_capture_channels_); |
| 277 |
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> Y2( |
| 278 |
Y2_stack.data(), num_capture_channels_); |
| 279 |
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> E2( |
| 280 |
E2_stack.data(), num_capture_channels_); |
| 281 |
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> R2( |
| 282 |
R2_stack.data(), num_capture_channels_); |
| 283 |
rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> S2_linear( |
| 284 |
S2_linear_stack.data(), num_capture_channels_); |
| 285 |
rtc::ArrayView<FftData> Y(Y_stack.data(), num_capture_channels_); |
| 286 |
rtc::ArrayView<FftData> E(E_stack.data(), num_capture_channels_); |
| 287 |
rtc::ArrayView<FftData> comfort_noise(comfort_noise_stack.data(), |
| 288 |
num_capture_channels_); |
| 289 |
rtc::ArrayView<FftData> high_band_comfort_noise( |
| 290 |
high_band_comfort_noise_stack.data(), num_capture_channels_); |
| 291 |
rtc::ArrayView<SubtractorOutput> subtractor_output( |
| 292 |
subtractor_output_stack.data(), num_capture_channels_); |
| 293 |
if (NumChannelsOnHeap(num_capture_channels_) > 0) { |
| 294 |
// If the stack-allocated space is too small, use the heap for storing the |
| 295 |
// microphone data. |
| 296 |
e = rtc::ArrayView<std::array<float, kFftLengthBy2>>(e_heap_.data(), |
| 297 |
num_capture_channels_); |
| 298 |
Y2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 299 |
Y2_heap_.data(), num_capture_channels_); |
| 300 |
E2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 301 |
E2_heap_.data(), num_capture_channels_); |
| 302 |
R2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 303 |
R2_heap_.data(), num_capture_channels_); |
| 304 |
S2_linear = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 305 |
S2_linear_heap_.data(), num_capture_channels_); |
| 306 |
Y = rtc::ArrayView<FftData>(Y_heap_.data(), num_capture_channels_); |
| 307 |
E = rtc::ArrayView<FftData>(E_heap_.data(), num_capture_channels_); |
| 308 |
comfort_noise = rtc::ArrayView<FftData>(comfort_noise_heap_.data(), |
| 309 |
num_capture_channels_); |
| 310 |
high_band_comfort_noise = rtc::ArrayView<FftData>( |
| 311 |
high_band_comfort_noise_heap_.data(), num_capture_channels_); |
| 312 |
subtractor_output = rtc::ArrayView<SubtractorOutput>( |
| 313 |
subtractor_output_heap_.data(), num_capture_channels_); |
| 314 |
} |
| 315 |
|
| 316 |
data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, |
| 317 |
&(*y)[0][0][0], 16000, 1); |
| 318 |
data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, |
| 319 |
&x[0][0][0], 16000, 1); |
| 320 |
data_dumper_->DumpRaw("aec3_echo_remover_capture_input", (*y)[0][0]); |
| 321 |
data_dumper_->DumpRaw("aec3_echo_remover_render_input", x[0][0]); |
| 322 |
|
| 323 |
aec_state_.UpdateCaptureSaturation(capture_signal_saturation); |
| 324 |
|
| 325 |
if (echo_path_variability.AudioPathChanged()) { |
| 326 |
// Ensure that the gain change is only acted on once per frame. |
| 327 |
if (echo_path_variability.gain_change) { |
| 328 |
if (gain_change_hangover_ == 0) { |
| 329 |
constexpr int kMaxBlocksPerFrame = 3; |
| 330 |
gain_change_hangover_ = kMaxBlocksPerFrame; |
| 331 |
rtc::LoggingSeverity log_level = |
| 332 |
config_.delay.log_warning_on_delay_changes ? rtc::LS_WARNING |
| 333 |
: rtc::LS_VERBOSE; |
| 334 |
RTC_LOG_V(log_level) |
| 335 |
<< "Gain change detected at block " << block_counter_; |
| 336 |
} else { |
| 337 |
echo_path_variability.gain_change = false; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
subtractor_.HandleEchoPathChange(echo_path_variability); |
| 342 |
aec_state_.HandleEchoPathChange(echo_path_variability); |
| 343 |
|
| 344 |
if (echo_path_variability.delay_change != |
| 345 |
EchoPathVariability::DelayAdjustment::kNone) { |
| 346 |
suppression_gain_.SetInitialState(true); |
| 347 |
} |
| 348 |
} |
| 349 |
if (gain_change_hangover_ > 0) { |
| 350 |
--gain_change_hangover_; |
| 351 |
} |
| 352 |
|
| 353 |
// Analyze the render signal. |
| 354 |
render_signal_analyzer_.Update(*render_buffer, |
| 355 |
aec_state_.MinDirectPathFilterDelay()); |
| 356 |
|
| 357 |
// State transition. |
| 358 |
if (aec_state_.TransitionTriggered()) { |
| 359 |
subtractor_.ExitInitialState(); |
| 360 |
suppression_gain_.SetInitialState(false); |
| 361 |
} |
| 362 |
|
| 363 |
// Perform linear echo cancellation. |
| 364 |
subtractor_.Process(*render_buffer, (*y)[0], render_signal_analyzer_, |
| 365 |
aec_state_, subtractor_output); |
| 366 |
|
| 367 |
// Compute spectra. |
| 368 |
for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 369 |
FormLinearFilterOutput(subtractor_output[ch], e[ch]); |
| 370 |
WindowedPaddedFft(fft_, (*y)[0][ch], y_old_[ch], &Y[ch]); |
| 371 |
WindowedPaddedFft(fft_, e[ch], e_old_[ch], &E[ch]); |
| 372 |
LinearEchoPower(E[ch], Y[ch], &S2_linear[ch]); |
| 373 |
Y[ch].Spectrum(optimization_, Y2[ch]); |
| 374 |
E[ch].Spectrum(optimization_, E2[ch]); |
| 375 |
} |
| 376 |
|
| 377 |
// Optionally return the linear filter output. |
| 378 |
if (linear_output) { |
| 379 |
RTC_DCHECK_GE(1, linear_output->size()); |
| 380 |
RTC_DCHECK_EQ(num_capture_channels_, linear_output[0].size()); |
| 381 |
for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 382 |
RTC_DCHECK_EQ(kBlockSize, (*linear_output)[0][ch].size()); |
| 383 |
std::copy(e[ch].begin(), e[ch].end(), (*linear_output)[0][ch].begin()); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
// Update the AEC state information. |
| 388 |
aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponses(), |
| 389 |
subtractor_.FilterImpulseResponses(), *render_buffer, E2, |
| 390 |
Y2, subtractor_output); |
| 391 |
|
| 392 |
// Choose the linear output. |
| 393 |
const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y; |
| 394 |
|
| 395 |
data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &(*y)[0][0][0], 16000, |
| 396 |
1); |
| 397 |
data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0][0], 16000, 1); |
| 398 |
|
| 399 |
// Estimate the comfort noise. |
| 400 |
cng_.Compute(aec_state_.SaturatedCapture(), Y2, comfort_noise, |
| 401 |
high_band_comfort_noise); |
| 402 |
|
| 403 |
// Only do the below processing if the output of the audio processing module |
| 404 |
// is used. |
| 405 |
std::array<float, kFftLengthBy2Plus1> G; |
| 406 |
if (capture_output_used_) { |
| 407 |
// Estimate the residual echo power. |
| 408 |
residual_echo_estimator_.Estimate(aec_state_, *render_buffer, S2_linear, Y2, |
| 409 |
suppression_gain_.IsDominantNearend(), |
| 410 |
R2); |
| 411 |
|
| 412 |
// Suppressor nearend estimate. |
| 413 |
if (aec_state_.UsableLinearEstimate()) { |
| 414 |
// E2 is bound by Y2. |
| 415 |
for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 416 |
std::transform(E2[ch].begin(), E2[ch].end(), Y2[ch].begin(), |
| 417 |
E2[ch].begin(), |
| 418 |
[](float a, float b) { return std::min(a, b); }); |
| 419 |
} |
| 420 |
} |
| 421 |
const auto& nearend_spectrum = aec_state_.UsableLinearEstimate() ? E2 : Y2; |
| 422 |
|
| 423 |
// Suppressor echo estimate. |
| 424 |
const auto& echo_spectrum = |
| 425 |
aec_state_.UsableLinearEstimate() ? S2_linear : R2; |
| 426 |
|
| 427 |
// Determine if the suppressor should assume clock drift. |
| 428 |
const bool clock_drift = config_.echo_removal_control.has_clock_drift || |
| 429 |
echo_path_variability.clock_drift; |
| 430 |
|
| 431 |
// Compute preferred gains. |
| 432 |
float high_bands_gain; |
| 433 |
suppression_gain_.GetGain(nearend_spectrum, echo_spectrum, R2, |
| 434 |
cng_.NoiseSpectrum(), render_signal_analyzer_, |
| 435 |
aec_state_, x, clock_drift, &high_bands_gain, &G); |
| 436 |
|
| 437 |
suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G, |
| 438 |
high_bands_gain, Y_fft, y); |
| 439 |
|
| 440 |
} else { |
| 441 |
G.fill(0.f); |
| 442 |
} |
| 443 |
|
| 444 |
// Update the metrics. |
| 445 |
metrics_.Update(aec_state_, cng_.NoiseSpectrum()[0], G); |
| 446 |
|
| 447 |
// Debug outputs for the purpose of development and analysis. |
| 448 |
data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize, |
| 449 |
&subtractor_output[0].s_refined[0], 16000, 1); |
| 450 |
data_dumper_->DumpRaw("aec3_output", (*y)[0][0]); |
| 451 |
data_dumper_->DumpRaw("aec3_narrow_render", |
| 452 |
render_signal_analyzer_.NarrowPeakBand() ? 1 : 0); |
| 453 |
data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum()[0]); |
| 454 |
data_dumper_->DumpRaw("aec3_suppressor_gain", G); |
| 455 |
data_dumper_->DumpWav("aec3_output", |
| 456 |
rtc::ArrayView<const float>(&(*y)[0][0][0], kBlockSize), |
| 457 |
16000, 1); |
| 458 |
data_dumper_->DumpRaw("aec3_using_subtractor_output[0]", |
| 459 |
aec_state_.UseLinearFilterOutput() ? 1 : 0); |
| 460 |
data_dumper_->DumpRaw("aec3_E2", E2[0]); |
| 461 |
data_dumper_->DumpRaw("aec3_S2_linear", S2_linear[0]); |
| 462 |
data_dumper_->DumpRaw("aec3_Y2", Y2[0]); |
| 463 |
data_dumper_->DumpRaw( |
| 464 |
"aec3_X2", render_buffer->Spectrum( |
| 465 |
aec_state_.MinDirectPathFilterDelay())[/*channel=*/0]); |
| 466 |
data_dumper_->DumpRaw("aec3_R2", R2[0]); |
| 467 |
data_dumper_->DumpRaw("aec3_filter_delay", |
| 468 |
aec_state_.MinDirectPathFilterDelay()); |
| 469 |
data_dumper_->DumpRaw("aec3_capture_saturation", |
| 470 |
aec_state_.SaturatedCapture() ? 1 : 0); |
| 471 |
} |
| 472 |
|
| 473 |
void EchoRemoverImpl::FormLinearFilterOutput( |
| 474 |
const SubtractorOutput& subtractor_output, |
| 475 |
rtc::ArrayView<float> output) { |
| 476 |
RTC_DCHECK_EQ(subtractor_output.e_refined.size(), output.size()); |
| 477 |
RTC_DCHECK_EQ(subtractor_output.e_coarse.size(), output.size()); |
| 478 |
bool use_refined_output = true; |
| 479 |
if (use_coarse_filter_output_) { |
| 480 |
// As the output of the refined adaptive filter generally should be better |
| 481 |
// than the coarse filter output, add a margin and threshold for when |
| 482 |
// choosing the coarse filter output. |
| 483 |
if (subtractor_output.e2_coarse < 0.9f * subtractor_output.e2_refined && |
| 484 |
subtractor_output.y2 > 30.f * 30.f * kBlockSize && |
| 485 |
(subtractor_output.s2_refined > 60.f * 60.f * kBlockSize || |
| 486 |
subtractor_output.s2_coarse > 60.f * 60.f * kBlockSize)) { |
| 487 |
use_refined_output = false; |
| 488 |
} else { |
| 489 |
// If the refined filter is diverged, choose the filter output that has |
| 490 |
// the lowest power. |
| 491 |
if (subtractor_output.e2_coarse < subtractor_output.e2_refined && |
| 492 |
subtractor_output.y2 < subtractor_output.e2_refined) { |
| 493 |
use_refined_output = false; |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
SignalTransition(refined_filter_output_last_selected_ |
| 499 |
? subtractor_output.e_refined |
| 500 |
: subtractor_output.e_coarse, |
| 501 |
use_refined_output ? subtractor_output.e_refined |
| 502 |
: subtractor_output.e_coarse, |
| 503 |
output); |
| 504 |
refined_filter_output_last_selected_ = use_refined_output; |
| 505 |
} |
| 506 |
|
| 507 |
} // namespace |
| 508 |
|
| 509 |
EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config, |
| 510 |
int sample_rate_hz, |
| 511 |
size_t num_render_channels, |
| 512 |
size_t num_capture_channels) { |
| 513 |
return new EchoRemoverImpl(config, sample_rate_hz, num_render_channels, |
| 514 |
num_capture_channels); |
| 515 |
} |
| 516 |
|
| 517 |
} // namespace webrtc |