NetEQ: GenerateBackgroundNoise moved to BackgrounNoise
Bug: webrtc:10548
Change-Id: Ie9da0755793078b81c60c3751abcbff13da40ede
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/132788
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27619}
diff --git a/modules/audio_coding/neteq/background_noise.cc b/modules/audio_coding/neteq/background_noise.cc
index 08c278e..5bb9d7e 100644
--- a/modules/audio_coding/neteq/background_noise.cc
+++ b/modules/audio_coding/neteq/background_noise.cc
@@ -21,9 +21,14 @@
#include "modules/audio_coding/neteq/post_decode_vad.h"
namespace webrtc {
+namespace {
+
+constexpr size_t kMaxSampleRate = 48000;
+
+} // namespace
// static
-const size_t BackgroundNoise::kMaxLpcOrder;
+constexpr size_t BackgroundNoise::kMaxLpcOrder;
BackgroundNoise::BackgroundNoise(size_t num_channels)
: num_channels_(num_channels),
@@ -119,6 +124,56 @@
return;
}
+void BackgroundNoise::GenerateBackgroundNoise(
+ rtc::ArrayView<const int16_t> random_vector,
+ size_t channel,
+ int mute_slope,
+ bool too_many_expands,
+ size_t num_noise_samples,
+ int16_t* buffer) {
+ constexpr size_t kNoiseLpcOrder = kMaxLpcOrder;
+ int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125];
+ assert(num_noise_samples <= (kMaxSampleRate / 8000 * 125));
+ RTC_DCHECK_GE(random_vector.size(), num_noise_samples);
+ int16_t* noise_samples = &buffer[kNoiseLpcOrder];
+ if (initialized()) {
+ // Use background noise parameters.
+ memcpy(noise_samples - kNoiseLpcOrder, FilterState(channel),
+ sizeof(int16_t) * kNoiseLpcOrder);
+
+ int dc_offset = 0;
+ if (ScaleShift(channel) > 1) {
+ dc_offset = 1 << (ScaleShift(channel) - 1);
+ }
+
+ // Scale random vector to correct energy level.
+ WebRtcSpl_AffineTransformVector(scaled_random_vector, random_vector.data(),
+ Scale(channel), dc_offset,
+ ScaleShift(channel), num_noise_samples);
+
+ WebRtcSpl_FilterARFastQ12(scaled_random_vector, noise_samples,
+ Filter(channel), kNoiseLpcOrder + 1,
+ num_noise_samples);
+
+ SetFilterState(
+ channel,
+ {&(noise_samples[num_noise_samples - kNoiseLpcOrder]), kNoiseLpcOrder});
+
+ // Unmute the background noise.
+ int16_t bgn_mute_factor = MuteFactor(channel);
+ if (bgn_mute_factor < 16384) {
+ WebRtcSpl_AffineTransformVector(noise_samples, noise_samples,
+ bgn_mute_factor, 8192, 14,
+ num_noise_samples);
+ }
+ // Update mute_factor in BackgroundNoise class.
+ SetMuteFactor(channel, bgn_mute_factor);
+ } else {
+ // BGN parameters have not been initialized; use zero noise.
+ memset(noise_samples, 0, sizeof(int16_t) * num_noise_samples);
+ }
+}
+
int32_t BackgroundNoise::Energy(size_t channel) const {
assert(channel < num_channels_);
return channel_parameters_[channel].energy;
@@ -145,11 +200,10 @@
}
void BackgroundNoise::SetFilterState(size_t channel,
- const int16_t* input,
- size_t length) {
+ rtc::ArrayView<const int16_t> input) {
assert(channel < num_channels_);
- length = std::min(length, kMaxLpcOrder);
- memcpy(channel_parameters_[channel].filter_state, input,
+ size_t length = std::min(input.size(), kMaxLpcOrder);
+ memcpy(channel_parameters_[channel].filter_state, input.data(),
length * sizeof(int16_t));
}
diff --git a/modules/audio_coding/neteq/background_noise.h b/modules/audio_coding/neteq/background_noise.h
index d047942..0286320 100644
--- a/modules/audio_coding/neteq/background_noise.h
+++ b/modules/audio_coding/neteq/background_noise.h
@@ -14,6 +14,7 @@
#include <string.h> // size_t
#include <memory>
+#include "api/array_view.h"
#include "rtc_base/constructor_magic.h"
namespace webrtc {
@@ -27,7 +28,7 @@
public:
// TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10.
// Will work anyway, but probably sound a little worse.
- static const size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4.
+ static constexpr size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4.
explicit BackgroundNoise(size_t num_channels);
virtual ~BackgroundNoise();
@@ -38,6 +39,15 @@
// |sync_buffer|, and on the latest decision in |vad| if it is running.
void Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad);
+ // Generates background noise given a random vector and writes the output to
+ // |buffer|.
+ void GenerateBackgroundNoise(rtc::ArrayView<const int16_t> random_vector,
+ size_t channel,
+ int mute_slope,
+ bool too_many_expands,
+ size_t num_noise_samples,
+ int16_t* buffer);
+
// Returns |energy_| for |channel|.
int32_t Energy(size_t channel) const;
@@ -53,9 +63,9 @@
// Returns a pointer to |filter_state_| for |channel|.
const int16_t* FilterState(size_t channel) const;
- // Copies |length| elements from |input| to the filter state. Will not copy
- // more than |kMaxLpcOrder| elements.
- void SetFilterState(size_t channel, const int16_t* input, size_t length);
+ // Copies |input| to the filter state. Will not copy more than |kMaxLpcOrder|
+ // elements.
+ void SetFilterState(size_t channel, rtc::ArrayView<const int16_t> input);
// Returns |scale_| for |channel|.
int16_t Scale(size_t channel) const;
diff --git a/modules/audio_coding/neteq/expand.cc b/modules/audio_coding/neteq/expand.cc
index 4a06d09..acf7f28 100644
--- a/modules/audio_coding/neteq/expand.cc
+++ b/modules/audio_coding/neteq/expand.cc
@@ -292,7 +292,7 @@
}
// Background noise part.
- GenerateBackgroundNoise(
+ background_noise_->GenerateBackgroundNoise(
random_vector, channel_ix, channel_parameters_[channel_ix].mute_slope,
TooManyExpands(), current_lag, unvoiced_array_memory);
@@ -860,56 +860,6 @@
fs, num_channels);
}
-// TODO(turajs): This can be moved to BackgroundNoise class.
-void Expand::GenerateBackgroundNoise(int16_t* random_vector,
- size_t channel,
- int mute_slope,
- bool too_many_expands,
- size_t num_noise_samples,
- int16_t* buffer) {
- static const size_t kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder;
- int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125];
- assert(num_noise_samples <= (kMaxSampleRate / 8000 * 125));
- int16_t* noise_samples = &buffer[kNoiseLpcOrder];
- if (background_noise_->initialized()) {
- // Use background noise parameters.
- memcpy(noise_samples - kNoiseLpcOrder,
- background_noise_->FilterState(channel),
- sizeof(int16_t) * kNoiseLpcOrder);
-
- int dc_offset = 0;
- if (background_noise_->ScaleShift(channel) > 1) {
- dc_offset = 1 << (background_noise_->ScaleShift(channel) - 1);
- }
-
- // Scale random vector to correct energy level.
- WebRtcSpl_AffineTransformVector(
- scaled_random_vector, random_vector, background_noise_->Scale(channel),
- dc_offset, background_noise_->ScaleShift(channel), num_noise_samples);
-
- WebRtcSpl_FilterARFastQ12(scaled_random_vector, noise_samples,
- background_noise_->Filter(channel),
- kNoiseLpcOrder + 1, num_noise_samples);
-
- background_noise_->SetFilterState(
- channel, &(noise_samples[num_noise_samples - kNoiseLpcOrder]),
- kNoiseLpcOrder);
-
- // Unmute the background noise.
- int16_t bgn_mute_factor = background_noise_->MuteFactor(channel);
- if (bgn_mute_factor < 16384) {
- WebRtcSpl_AffineTransformVector(noise_samples, noise_samples,
- bgn_mute_factor, 8192, 14,
- num_noise_samples);
- }
- // Update mute_factor in BackgroundNoise class.
- background_noise_->SetMuteFactor(channel, bgn_mute_factor);
- } else {
- // BGN parameters have not been initialized; use zero noise.
- memset(noise_samples, 0, sizeof(int16_t) * num_noise_samples);
- }
-}
-
void Expand::GenerateRandomVector(int16_t seed_increment,
size_t length,
int16_t* random_vector) {
diff --git a/modules/audio_coding/neteq/expand.h b/modules/audio_coding/neteq/expand.h
index 7c041b8..9fc11eb 100644
--- a/modules/audio_coding/neteq/expand.h
+++ b/modules/audio_coding/neteq/expand.h
@@ -76,13 +76,6 @@
size_t length,
int16_t* random_vector);
- void GenerateBackgroundNoise(int16_t* random_vector,
- size_t channel,
- int mute_slope,
- bool too_many_expands,
- size_t num_noise_samples,
- int16_t* buffer);
-
// Initializes member variables at the beginning of an expand period.
void InitializeForAnExpandPeriod();