Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 13 | #include "modules/audio_processing/aec3/aec3_common.h" |
| 14 | #include "modules/audio_processing/aec3/stationarity_estimator.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 18 | namespace { |
| 19 | constexpr int kUnitializedIndex = -1; |
| 20 | } // namespace |
| 21 | |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 22 | EchoAudibility::EchoAudibility() { |
| 23 | Reset(); |
| 24 | } |
| 25 | |
| 26 | void EchoAudibility::Reset() { |
| 27 | render_stationarity_.Reset(); |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 28 | render_write_prev_ = kUnitializedIndex; |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | EchoAudibility::~EchoAudibility() = default; |
| 32 | |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 33 | void EchoAudibility::UpdateRenderStationarityFlags( |
| 34 | const RenderBuffer& render_buffer, |
| 35 | size_t delay_blocks) { |
| 36 | int idx_at_delay = |
| 37 | render_buffer.OffsetSpectrumIndex(render_buffer.Position(), delay_blocks); |
| 38 | size_t num_lookahead = render_buffer.Headroom() - delay_blocks + 1; |
| 39 | render_stationarity_.UpdateStationarityFlags( |
| 40 | render_buffer.GetSpectrumBuffer(), idx_at_delay, num_lookahead); |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 43 | void EchoAudibility::UpdateRenderNoiseEstimator( |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 44 | const RenderBuffer& render_buffer) { |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 45 | if (render_write_prev_ == kUnitializedIndex) { |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 46 | render_write_prev_ = render_buffer.GetWritePositionSpectrum(); |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | int render_write_current = render_buffer.GetWritePositionSpectrum(); |
| 50 | |
| 51 | for (int idx = render_write_prev_; idx != render_write_current; |
| 52 | idx = render_buffer.DecIdx(idx)) { |
| 53 | render_stationarity_.UpdateNoiseEstimator( |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 54 | render_buffer.SpectrumAtIndex(idx)); |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | render_write_prev_ = render_write_current; |
| 58 | } |
| 59 | |
| 60 | void EchoAudibility::Update(const RenderBuffer& render_buffer, |
| 61 | size_t delay_blocks, |
| 62 | size_t capture_block_counter, |
| 63 | bool external_delay_seen) { |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 64 | UpdateRenderNoiseEstimator(render_buffer); |
| 65 | |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 66 | if (external_delay_seen) { |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 67 | UpdateRenderStationarityFlags(render_buffer, delay_blocks); |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 71 | } // namespace webrtc |