background_noise.h (4509B)
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 <cstdint> 17 #include <memory> 18 19 #include "api/array_view.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 BackgroundNoise(const BackgroundNoise&) = delete; 38 BackgroundNoise& operator=(const BackgroundNoise&) = delete; 39 40 void Reset(); 41 42 // Updates the parameter estimates based on the signal currently in the 43 // `sync_buffer`. 44 // Returns true if the filter parameters are updated. 45 bool Update(const AudioMultiVector& sync_buffer); 46 47 // Generates background noise given a random vector and writes the output to 48 // `buffer`. 49 void GenerateBackgroundNoise(ArrayView<const int16_t> random_vector, 50 size_t channel, 51 int mute_slope, 52 bool too_many_expands, 53 size_t num_noise_samples, 54 int16_t* buffer); 55 56 // Returns `energy_` for `channel`. 57 int32_t Energy(size_t channel) const; 58 59 // Sets the value of `mute_factor_` for `channel` to `value`. 60 void SetMuteFactor(size_t channel, int16_t value); 61 62 // Returns `mute_factor_` for `channel`. 63 int16_t MuteFactor(size_t channel) const; 64 65 // Returns a pointer to `filter_` for `channel`. 66 const int16_t* Filter(size_t channel) const; 67 68 // Returns a pointer to `filter_state_` for `channel`. 69 const int16_t* FilterState(size_t channel) const; 70 71 // Copies `input` to the filter state. Will not copy more than `kMaxLpcOrder` 72 // elements. 73 void SetFilterState(size_t channel, ArrayView<const int16_t> input); 74 75 // Returns `scale_` for `channel`. 76 int16_t Scale(size_t channel) const; 77 78 // Returns `scale_shift_` for `channel`. 79 int16_t ScaleShift(size_t channel) const; 80 81 // Accessors. 82 bool initialized() const { return initialized_; } 83 84 private: 85 static const int kThresholdIncrement = 229; // 0.0035 in Q16. 86 static const size_t kVecLen = 256; 87 static const int kLogVecLen = 8; // log2(kVecLen). 88 static const size_t kResidualLength = 64; 89 static const int16_t kLogResidualLength = 6; // log2(kResidualLength) 90 91 struct ChannelParameters { 92 // Constructor. 93 ChannelParameters() { Reset(); } 94 95 void Reset() { 96 energy = 2500; 97 max_energy = 0; 98 energy_update_threshold = 500000; 99 low_energy_update_threshold = 0; 100 memset(filter_state, 0, sizeof(filter_state)); 101 memset(filter, 0, sizeof(filter)); 102 filter[0] = 4096; 103 mute_factor = 0; 104 scale = 20000; 105 scale_shift = 24; 106 } 107 108 int32_t energy; 109 int32_t max_energy; 110 int32_t energy_update_threshold; 111 int32_t low_energy_update_threshold; 112 int16_t filter_state[kMaxLpcOrder]; 113 int16_t filter[kMaxLpcOrder + 1]; 114 int16_t mute_factor; 115 int16_t scale; 116 int16_t scale_shift; 117 }; 118 119 int32_t CalculateAutoCorrelation(const int16_t* signal, 120 size_t length, 121 int32_t* auto_correlation) const; 122 123 // Increments the energy threshold by a factor 1 + `kThresholdIncrement`. 124 void IncrementEnergyThreshold(size_t channel, int32_t sample_energy); 125 126 // Updates the filter parameters. 127 void SaveParameters(size_t channel, 128 const int16_t* lpc_coefficients, 129 const int16_t* filter_state, 130 int32_t sample_energy, 131 int32_t residual_energy); 132 133 size_t num_channels_; 134 std::unique_ptr<ChannelParameters[]> channel_parameters_; 135 bool initialized_; 136 }; 137 138 } // namespace webrtc 139 #endif // MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_