1 /* 2 * Copyright (c) 2012 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 #ifndef MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_ 13 14 #include <string.h> // size_t 15 16 #include <memory> 17 18 #include "api/array_view.h" 19 #include "rtc_base/constructor_magic.h" 20 21 namespace webrtc { 22 23 // Forward declarations. 24 class AudioMultiVector; 25 class PostDecodeVad; 26 27 // This class handles estimation of background noise parameters. 28 class BackgroundNoise { 29 public: 30 // TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10. 31 // Will work anyway, but probably sound a little worse. 32 static constexpr size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4. 33 34 explicit BackgroundNoise(size_t num_channels); 35 virtual ~BackgroundNoise(); 36 37 void Reset(); 38 39 // Updates the parameter estimates based on the signal currently in the 40 // |sync_buffer|, and on the latest decision in |vad| if it is running. 41 // Returns true if the filter parameters are updated. 42 bool Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad); 43 44 // Generates background noise given a random vector and writes the output to 45 // |buffer|. 46 void GenerateBackgroundNoise(rtc::ArrayView<const int16_t> random_vector, 47 size_t channel, 48 int mute_slope, 49 bool too_many_expands, 50 size_t num_noise_samples, 51 int16_t* buffer); 52 53 // Returns |energy_| for |channel|. 54 int32_t Energy(size_t channel) const; 55 56 // Sets the value of |mute_factor_| for |channel| to |value|. 57 void SetMuteFactor(size_t channel, int16_t value); 58 59 // Returns |mute_factor_| for |channel|. 60 int16_t MuteFactor(size_t channel) const; 61 62 // Returns a pointer to |filter_| for |channel|. 63 const int16_t* Filter(size_t channel) const; 64 65 // Returns a pointer to |filter_state_| for |channel|. 66 const int16_t* FilterState(size_t channel) const; 67 68 // Copies |input| to the filter state. Will not copy more than |kMaxLpcOrder| 69 // elements. 70 void SetFilterState(size_t channel, rtc::ArrayView<const int16_t> input); 71 72 // Returns |scale_| for |channel|. 73 int16_t Scale(size_t channel) const; 74 75 // Returns |scale_shift_| for |channel|. 76 int16_t ScaleShift(size_t channel) const; 77 78 // Accessors. initialized()79 bool initialized() const { return initialized_; } 80 81 private: 82 static const int kThresholdIncrement = 229; // 0.0035 in Q16. 83 static const size_t kVecLen = 256; 84 static const int kLogVecLen = 8; // log2(kVecLen). 85 static const size_t kResidualLength = 64; 86 static const int16_t kLogResidualLength = 6; // log2(kResidualLength) 87 88 struct ChannelParameters { 89 // Constructor. ChannelParametersChannelParameters90 ChannelParameters() { Reset(); } 91 ResetChannelParameters92 void Reset() { 93 energy = 2500; 94 max_energy = 0; 95 energy_update_threshold = 500000; 96 low_energy_update_threshold = 0; 97 memset(filter_state, 0, sizeof(filter_state)); 98 memset(filter, 0, sizeof(filter)); 99 filter[0] = 4096; 100 mute_factor = 0; 101 scale = 20000; 102 scale_shift = 24; 103 } 104 105 int32_t energy; 106 int32_t max_energy; 107 int32_t energy_update_threshold; 108 int32_t low_energy_update_threshold; 109 int16_t filter_state[kMaxLpcOrder]; 110 int16_t filter[kMaxLpcOrder + 1]; 111 int16_t mute_factor; 112 int16_t scale; 113 int16_t scale_shift; 114 }; 115 116 int32_t CalculateAutoCorrelation(const int16_t* signal, 117 size_t length, 118 int32_t* auto_correlation) const; 119 120 // Increments the energy threshold by a factor 1 + |kThresholdIncrement|. 121 void IncrementEnergyThreshold(size_t channel, int32_t sample_energy); 122 123 // Updates the filter parameters. 124 void SaveParameters(size_t channel, 125 const int16_t* lpc_coefficients, 126 const int16_t* filter_state, 127 int32_t sample_energy, 128 int32_t residual_energy); 129 130 size_t num_channels_; 131 std::unique_ptr<ChannelParameters[]> channel_parameters_; 132 bool initialized_; 133 134 RTC_DISALLOW_COPY_AND_ASSIGN(BackgroundNoise); 135 }; 136 137 } // namespace webrtc 138 #endif // MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_ 139