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