blob: a32e8a0812ec78fe76471f5b97f9ae6659ff8cda [file] [log] [blame]
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +02001/*
2 * Copyright (c) 2018 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
11#include "modules/audio_processing/aec3/echo_audibility.h"
12
13#include <algorithm>
14
15#include "modules/audio_processing/aec3/aec3_common.h"
16#include "modules/audio_processing/aec3/stationarity_estimator.h"
17
18namespace webrtc {
19
20EchoAudibility::EchoAudibility() {
21 Reset();
22}
23
24void EchoAudibility::Reset() {
25 render_stationarity_.Reset();
26}
27
28EchoAudibility::~EchoAudibility() = default;
29
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020030void EchoAudibility::UpdateAfterStableDelay(const RenderBuffer& render_buffer,
31 size_t delay_blocks,
32 size_t capture_block_counter) {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020033 RTC_DCHECK_GT(capture_block_counter, delay_blocks);
34
35 size_t num_lookahead = std::min(StationarityEstimator::GetMaxNumLookAhead(),
36 render_buffer.Headroom() - delay_blocks + 1);
37 int render_block_number = capture_block_counter - delay_blocks;
38
39 for (size_t k = 0; k < (num_lookahead + 1); ++k) {
40 // Delay changes can potentially make that not all the farend blocks
41 // are seen. That effect is assumed to have a minimum effect in the
42 // estimation.
43 render_stationarity_.Update(render_buffer.Spectrum(delay_blocks - k),
44 render_block_number + k);
45 }
46 render_stationarity_.UpdateStationarityFlags(render_block_number,
47 num_lookahead);
48}
49
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020050void EchoAudibility::UpdateBeforeStableDelay(
51 const RenderBuffer& render_buffer) {
52 // If the delay is not set, the read position in the buffer cannot be trust
53 // and the write position in the render buffer should be used instead
54
55 if (first_update_) {
56 render_write_prev_ = render_buffer.GetWritePositionSpectrum();
57 first_update_ = false;
58 return;
59 }
60 int render_write_current = render_buffer.GetWritePositionSpectrum();
61
62 for (int idx = render_write_prev_; idx != render_write_current;
63 idx = render_buffer.DecIdx(idx)) {
64 render_stationarity_.UpdateNoiseEstimator(
65 render_buffer.SpectrumFromPosition(idx));
66 }
67
68 render_write_prev_ = render_write_current;
69}
70
71void EchoAudibility::Update(const RenderBuffer& render_buffer,
72 size_t delay_blocks,
73 size_t capture_block_counter,
74 bool external_delay_seen) {
75 if (external_delay_seen) {
76 UpdateAfterStableDelay(render_buffer, delay_blocks, capture_block_counter);
77 } else {
78 UpdateBeforeStableDelay(render_buffer);
79 }
80}
81
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020082} // namespace webrtc