blob: a6f139573fe2a512180d58863b9fb832a844cb24 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_
12#define MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <string.h> // size_t
kwiberg2d0c3322016-02-14 09:28:33 -080015#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/audio_multi_vector.h"
18#include "modules/audio_coding/neteq/include/neteq.h"
19#include "rtc_base/constructormagic.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021
22namespace webrtc {
23
24// Forward declarations.
25class PostDecodeVad;
26
27// This class handles estimation of background noise parameters.
28class BackgroundNoise {
29 public:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030 // TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10.
31 // Will work anyway, but probably sound a little worse.
Peter Kastingdce40cf2015-08-24 14:52:23 -070032 static const size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000034 explicit BackgroundNoise(size_t num_channels);
35 virtual ~BackgroundNoise();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036
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.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000041 void Update(const AudioMultiVector& sync_buffer,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042 const PostDecodeVad& vad);
43
44 // Returns |energy_| for |channel|.
45 int32_t Energy(size_t channel) const;
46
47 // Sets the value of |mute_factor_| for |channel| to |value|.
48 void SetMuteFactor(size_t channel, int16_t value);
49
50 // Returns |mute_factor_| for |channel|.
51 int16_t MuteFactor(size_t channel) const;
52
53 // Returns a pointer to |filter_| for |channel|.
54 const int16_t* Filter(size_t channel) const;
55
56 // Returns a pointer to |filter_state_| for |channel|.
57 const int16_t* FilterState(size_t channel) const;
58
59 // Copies |length| elements from |input| to the filter state. Will not copy
60 // more than |kMaxLpcOrder| elements.
61 void SetFilterState(size_t channel, const int16_t* input, size_t length);
62
63 // Returns |scale_| for |channel|.
64 int16_t Scale(size_t channel) const;
65
66 // Returns |scale_shift_| for |channel|.
67 int16_t ScaleShift(size_t channel) const;
68
69 // Accessors.
70 bool initialized() const { return initialized_; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071
72 private:
73 static const int kThresholdIncrement = 229; // 0.0035 in Q16.
Peter Kastingdce40cf2015-08-24 14:52:23 -070074 static const size_t kVecLen = 256;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 static const int kLogVecLen = 8; // log2(kVecLen).
Peter Kastingdce40cf2015-08-24 14:52:23 -070076 static const size_t kResidualLength = 64;
Peter Kastingb7e50542015-06-11 12:55:50 -070077 static const int16_t kLogResidualLength = 6; // log2(kResidualLength)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078
79 struct ChannelParameters {
80 // Constructor.
81 ChannelParameters() {
82 Reset();
83 }
84
85 void Reset() {
86 energy = 2500;
87 max_energy = 0;
88 energy_update_threshold = 500000;
89 low_energy_update_threshold = 0;
90 memset(filter_state, 0, sizeof(filter_state));
91 memset(filter, 0, sizeof(filter));
92 filter[0] = 4096;
Henrik Lundine5531392018-03-13 17:14:10 +010093 mute_factor = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094 scale = 20000;
95 scale_shift = 24;
96 }
97
98 int32_t energy;
99 int32_t max_energy;
100 int32_t energy_update_threshold;
101 int32_t low_energy_update_threshold;
102 int16_t filter_state[kMaxLpcOrder];
103 int16_t filter[kMaxLpcOrder + 1];
104 int16_t mute_factor;
105 int16_t scale;
106 int16_t scale_shift;
107 };
108
109 int32_t CalculateAutoCorrelation(const int16_t* signal,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700110 size_t length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111 int32_t* auto_correlation) const;
112
113 // Increments the energy threshold by a factor 1 + |kThresholdIncrement|.
114 void IncrementEnergyThreshold(size_t channel, int32_t sample_energy);
115
116 // Updates the filter parameters.
117 void SaveParameters(size_t channel,
118 const int16_t* lpc_coefficients,
119 const int16_t* filter_state,
120 int32_t sample_energy,
121 int32_t residual_energy);
122
123 size_t num_channels_;
kwiberg2d0c3322016-02-14 09:28:33 -0800124 std::unique_ptr<ChannelParameters[]> channel_parameters_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125 bool initialized_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000126
henrikg3c089d72015-09-16 05:37:44 -0700127 RTC_DISALLOW_COPY_AND_ASSIGN(BackgroundNoise);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000128};
129
130} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_