henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <string.h> // size_t |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 15 | #include <memory> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 17 | #include "rtc_base/constructor_magic.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // Forward declarations. |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | class AudioMultiVector; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 23 | class PostDecodeVad; |
| 24 | |
| 25 | // This class handles estimation of background noise parameters. |
| 26 | class BackgroundNoise { |
| 27 | public: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 28 | // TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10. |
| 29 | // Will work anyway, but probably sound a little worse. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 30 | static const size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 31 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 32 | explicit BackgroundNoise(size_t num_channels); |
| 33 | virtual ~BackgroundNoise(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 34 | |
| 35 | void Reset(); |
| 36 | |
| 37 | // Updates the parameter estimates based on the signal currently in the |
| 38 | // |sync_buffer|, and on the latest decision in |vad| if it is running. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | void Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 40 | |
| 41 | // Returns |energy_| for |channel|. |
| 42 | int32_t Energy(size_t channel) const; |
| 43 | |
| 44 | // Sets the value of |mute_factor_| for |channel| to |value|. |
| 45 | void SetMuteFactor(size_t channel, int16_t value); |
| 46 | |
| 47 | // Returns |mute_factor_| for |channel|. |
| 48 | int16_t MuteFactor(size_t channel) const; |
| 49 | |
| 50 | // Returns a pointer to |filter_| for |channel|. |
| 51 | const int16_t* Filter(size_t channel) const; |
| 52 | |
| 53 | // Returns a pointer to |filter_state_| for |channel|. |
| 54 | const int16_t* FilterState(size_t channel) const; |
| 55 | |
| 56 | // Copies |length| elements from |input| to the filter state. Will not copy |
| 57 | // more than |kMaxLpcOrder| elements. |
| 58 | void SetFilterState(size_t channel, const int16_t* input, size_t length); |
| 59 | |
| 60 | // Returns |scale_| for |channel|. |
| 61 | int16_t Scale(size_t channel) const; |
| 62 | |
| 63 | // Returns |scale_shift_| for |channel|. |
| 64 | int16_t ScaleShift(size_t channel) const; |
| 65 | |
| 66 | // Accessors. |
| 67 | bool initialized() const { return initialized_; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | static const int kThresholdIncrement = 229; // 0.0035 in Q16. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 71 | static const size_t kVecLen = 256; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 72 | static const int kLogVecLen = 8; // log2(kVecLen). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 73 | static const size_t kResidualLength = 64; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 74 | static const int16_t kLogResidualLength = 6; // log2(kResidualLength) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | |
| 76 | struct ChannelParameters { |
| 77 | // Constructor. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | ChannelParameters() { Reset(); } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 79 | |
| 80 | void Reset() { |
| 81 | energy = 2500; |
| 82 | max_energy = 0; |
| 83 | energy_update_threshold = 500000; |
| 84 | low_energy_update_threshold = 0; |
| 85 | memset(filter_state, 0, sizeof(filter_state)); |
| 86 | memset(filter, 0, sizeof(filter)); |
| 87 | filter[0] = 4096; |
Henrik Lundin | e553139 | 2018-03-13 17:14:10 +0100 | [diff] [blame] | 88 | mute_factor = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 89 | scale = 20000; |
| 90 | scale_shift = 24; |
| 91 | } |
| 92 | |
| 93 | int32_t energy; |
| 94 | int32_t max_energy; |
| 95 | int32_t energy_update_threshold; |
| 96 | int32_t low_energy_update_threshold; |
| 97 | int16_t filter_state[kMaxLpcOrder]; |
| 98 | int16_t filter[kMaxLpcOrder + 1]; |
| 99 | int16_t mute_factor; |
| 100 | int16_t scale; |
| 101 | int16_t scale_shift; |
| 102 | }; |
| 103 | |
| 104 | int32_t CalculateAutoCorrelation(const int16_t* signal, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 105 | size_t length, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 106 | int32_t* auto_correlation) const; |
| 107 | |
| 108 | // Increments the energy threshold by a factor 1 + |kThresholdIncrement|. |
| 109 | void IncrementEnergyThreshold(size_t channel, int32_t sample_energy); |
| 110 | |
| 111 | // Updates the filter parameters. |
| 112 | void SaveParameters(size_t channel, |
| 113 | const int16_t* lpc_coefficients, |
| 114 | const int16_t* filter_state, |
| 115 | int32_t sample_energy, |
| 116 | int32_t residual_energy); |
| 117 | |
| 118 | size_t num_channels_; |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 119 | std::unique_ptr<ChannelParameters[]> channel_parameters_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 120 | bool initialized_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 121 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 122 | RTC_DISALLOW_COPY_AND_ASSIGN(BackgroundNoise); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 126 | #endif // MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_ |