From 60bfb3d4e3adc1a13b43c38668118c37ef8b5c2c Mon Sep 17 00:00:00 2001 From: Alessio Bazzica Date: Fri, 28 Jun 2019 10:49:39 +0200 Subject: [PATCH] NetEQ: BackgroundNoise::Update returns true when the filter is updated Bug: webrtc:10690 Change-Id: I17ff7dc1cffc8c46987d0a9ff8c6633ce9dcc8d3 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/144040 Commit-Queue: Alessio Bazzica Reviewed-by: Henrik Lundin Cr-Commit-Position: refs/heads/master@{#28411} --- .../audio_coding/neteq/background_noise.cc | 47 ++++++++++--------- modules/audio_coding/neteq/background_noise.h | 3 +- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/modules/audio_coding/neteq/background_noise.cc b/modules/audio_coding/neteq/background_noise.cc index 5bb9d7e026..c0dcc5e04d 100644 --- a/modules/audio_coding/neteq/background_noise.cc +++ b/modules/audio_coding/neteq/background_noise.cc @@ -45,12 +45,13 @@ void BackgroundNoise::Reset() { } } -void BackgroundNoise::Update(const AudioMultiVector& input, +bool BackgroundNoise::Update(const AudioMultiVector& input, const PostDecodeVad& vad) { + bool filter_params_saved = false; if (vad.running() && vad.active_speech()) { // Do not update the background noise parameters if we know that the signal // is active speech. - return; + return filter_params_saved; } int32_t auto_correlation[kMaxLpcOrder + 1]; @@ -62,6 +63,7 @@ void BackgroundNoise::Update(const AudioMultiVector& input, ChannelParameters& parameters = channel_parameters_[channel_ix]; int16_t temp_signal_array[kVecLen + kMaxLpcOrder] = {0}; int16_t* temp_signal = &temp_signal_array[kMaxLpcOrder]; + RTC_DCHECK_GE(input.Size(), kVecLen); input[channel_ix].CopyTo(kVecLen, input.Size() - kVecLen, temp_signal); int32_t sample_energy = CalculateAutoCorrelation(temp_signal, kVecLen, auto_correlation); @@ -70,26 +72,26 @@ void BackgroundNoise::Update(const AudioMultiVector& input, sample_energy < parameters.energy_update_threshold) || (vad.running() && !vad.active_speech())) { // Generate LPC coefficients. - if (auto_correlation[0] > 0) { - // Regardless of whether the filter is actually updated or not, - // update energy threshold levels, since we have in fact observed - // a low energy signal. - if (sample_energy < parameters.energy_update_threshold) { - // Never go under 1.0 in average sample energy. - parameters.energy_update_threshold = std::max(sample_energy, 1); - parameters.low_energy_update_threshold = 0; - } - - // Only update BGN if filter is stable, i.e., if return value from - // Levinson-Durbin function is 1. - if (WebRtcSpl_LevinsonDurbin(auto_correlation, lpc_coefficients, - reflection_coefficients, - kMaxLpcOrder) != 1) { - return; - } - } else { + if (auto_correlation[0] <= 0) { // Center value in auto-correlation is not positive. Do not update. - return; + return filter_params_saved; + } + + // Regardless of whether the filter is actually updated or not, + // update energy threshold levels, since we have in fact observed + // a low energy signal. + if (sample_energy < parameters.energy_update_threshold) { + // Never go under 1.0 in average sample energy. + parameters.energy_update_threshold = std::max(sample_energy, 1); + parameters.low_energy_update_threshold = 0; + } + + // Only update BGN if filter is stable, i.e., if return value from + // Levinson-Durbin function is 1. + if (WebRtcSpl_LevinsonDurbin(auto_correlation, lpc_coefficients, + reflection_coefficients, + kMaxLpcOrder) != 1) { + return filter_params_saved; } // Generate the CNG gain factor by looking at the energy of the residual. @@ -113,6 +115,7 @@ void BackgroundNoise::Update(const AudioMultiVector& input, SaveParameters(channel_ix, lpc_coefficients, temp_signal + kVecLen - kMaxLpcOrder, sample_energy, residual_energy); + filter_params_saved = true; } } else { // Will only happen if post-decode VAD is disabled and |sample_energy| is @@ -121,7 +124,7 @@ void BackgroundNoise::Update(const AudioMultiVector& input, IncrementEnergyThreshold(channel_ix, sample_energy); } } - return; + return filter_params_saved; } void BackgroundNoise::GenerateBackgroundNoise( diff --git a/modules/audio_coding/neteq/background_noise.h b/modules/audio_coding/neteq/background_noise.h index 0286320c22..51911793c2 100644 --- a/modules/audio_coding/neteq/background_noise.h +++ b/modules/audio_coding/neteq/background_noise.h @@ -37,7 +37,8 @@ class BackgroundNoise { // Updates the parameter estimates based on the signal currently in the // |sync_buffer|, and on the latest decision in |vad| if it is running. - void Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad); + // Returns true if the filter parameters are updated. + bool Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad); // Generates background noise given a random vector and writes the output to // |buffer|.