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 | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <cmath> |
| 15 | |
Jesús de Vicente Peña | c98849c | 2018-10-22 11:41:05 +0200 | [diff] [blame] | 16 | #include "api/array_view.h" |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/aec3/aec3_common.h" |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/aec3/matrix_buffer.h" |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/aec3/stationarity_estimator.h" |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/aec3/vector_buffer.h" |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
Jesús de Vicente Peña | 836a7a2 | 2018-08-31 15:03:04 +0200 | [diff] [blame] | 24 | EchoAudibility::EchoAudibility(bool use_render_stationarity_at_init) |
| 25 | : use_render_stationarity_at_init_(use_render_stationarity_at_init) { |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 26 | Reset(); |
| 27 | } |
| 28 | |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 29 | EchoAudibility::~EchoAudibility() = default; |
| 30 | |
Jesús de Vicente Peña | c98849c | 2018-10-22 11:41:05 +0200 | [diff] [blame] | 31 | void EchoAudibility::Update( |
| 32 | const RenderBuffer& render_buffer, |
| 33 | rtc::ArrayView<const float> render_reverb_contribution_spectrum, |
| 34 | int delay_blocks, |
| 35 | bool external_delay_seen) { |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 36 | UpdateRenderNoiseEstimator(render_buffer.GetSpectrumBuffer(), |
| 37 | render_buffer.GetBlockBuffer(), |
| 38 | external_delay_seen); |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 39 | |
Jesús de Vicente Peña | 836a7a2 | 2018-08-31 15:03:04 +0200 | [diff] [blame] | 40 | if (external_delay_seen || use_render_stationarity_at_init_) { |
Jesús de Vicente Peña | c98849c | 2018-10-22 11:41:05 +0200 | [diff] [blame] | 41 | UpdateRenderStationarityFlags( |
| 42 | render_buffer, render_reverb_contribution_spectrum, delay_blocks); |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 46 | void EchoAudibility::Reset() { |
| 47 | render_stationarity_.Reset(); |
| 48 | non_zero_render_seen_ = false; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 49 | render_spectrum_write_prev_ = absl::nullopt; |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void EchoAudibility::UpdateRenderStationarityFlags( |
| 53 | const RenderBuffer& render_buffer, |
Jesús de Vicente Peña | c98849c | 2018-10-22 11:41:05 +0200 | [diff] [blame] | 54 | rtc::ArrayView<const float> render_reverb_contribution_spectrum, |
| 55 | int delay_blocks) { |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 56 | const VectorBuffer& spectrum_buffer = render_buffer.GetSpectrumBuffer(); |
| 57 | int idx_at_delay = |
| 58 | spectrum_buffer.OffsetIndex(spectrum_buffer.read, delay_blocks); |
| 59 | |
| 60 | int num_lookahead = render_buffer.Headroom() - delay_blocks + 1; |
| 61 | num_lookahead = std::max(0, num_lookahead); |
| 62 | |
Jesús de Vicente Peña | c98849c | 2018-10-22 11:41:05 +0200 | [diff] [blame] | 63 | render_stationarity_.UpdateStationarityFlags( |
| 64 | spectrum_buffer, render_reverb_contribution_spectrum, idx_at_delay, |
| 65 | num_lookahead); |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void EchoAudibility::UpdateRenderNoiseEstimator( |
| 69 | const VectorBuffer& spectrum_buffer, |
| 70 | const MatrixBuffer& block_buffer, |
| 71 | bool external_delay_seen) { |
| 72 | if (!render_spectrum_write_prev_) { |
| 73 | render_spectrum_write_prev_ = spectrum_buffer.write; |
| 74 | render_block_write_prev_ = block_buffer.write; |
| 75 | return; |
| 76 | } |
| 77 | int render_spectrum_write_current = spectrum_buffer.write; |
| 78 | if (!non_zero_render_seen_ && !external_delay_seen) { |
| 79 | non_zero_render_seen_ = !IsRenderTooLow(block_buffer); |
| 80 | } |
| 81 | if (non_zero_render_seen_) { |
| 82 | for (int idx = render_spectrum_write_prev_.value(); |
| 83 | idx != render_spectrum_write_current; |
| 84 | idx = spectrum_buffer.DecIndex(idx)) { |
| 85 | render_stationarity_.UpdateNoiseEstimator(spectrum_buffer.buffer[idx]); |
| 86 | } |
| 87 | } |
| 88 | render_spectrum_write_prev_ = render_spectrum_write_current; |
| 89 | } |
| 90 | |
| 91 | bool EchoAudibility::IsRenderTooLow(const MatrixBuffer& block_buffer) { |
| 92 | bool too_low = false; |
| 93 | const int render_block_write_current = block_buffer.write; |
| 94 | if (render_block_write_current == render_block_write_prev_) { |
| 95 | too_low = true; |
| 96 | } else { |
| 97 | for (int idx = render_block_write_prev_; idx != render_block_write_current; |
| 98 | idx = block_buffer.IncIndex(idx)) { |
| 99 | auto block = block_buffer.buffer[idx][0]; |
| 100 | auto r = std::minmax_element(block.cbegin(), block.cend()); |
| 101 | float max_abs = std::max(std::fabs(*r.first), std::fabs(*r.second)); |
| 102 | if (max_abs < 10) { |
| 103 | too_low = true; // Discards all blocks if one of them is too low. |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | render_block_write_prev_ = render_block_write_current; |
| 109 | return too_low; |
| 110 | } |
| 111 | |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 112 | } // namespace webrtc |