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