blob: 0ee95e03b93ad509c9e1f7d0cb3cc052acd5964f [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
11#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_BACKGROUND_NOISE_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_BACKGROUND_NOISE_H_
13
14#include <cstring> // size_t
15
16#include "webrtc/modules/audio_coding/neteq4/audio_multi_vector.h"
17#include "webrtc/system_wrappers/interface/constructor_magic.h"
18#include "webrtc/system_wrappers/interface/scoped_ptr.h"
19#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
23// Forward declarations.
24class PostDecodeVad;
25
26// This class handles estimation of background noise parameters.
27class BackgroundNoise {
28 public:
29 enum BackgroundNoiseMode {
30 kBgnOn, // Default behavior with eternal noise.
31 kBgnFade, // Noise fades to zero after some time.
32 kBgnOff // Background noise is always zero.
33 };
34
35 // TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10.
36 // Will work anyway, but probably sound a little worse.
37 static const int kMaxLpcOrder = 8; // 32000 / 8000 + 4.
38
39 explicit BackgroundNoise(size_t num_channels)
40 : num_channels_(num_channels),
41 channel_parameters_(new ChannelParameters[num_channels_]),
42 mode_(kBgnOn) {
43 Reset();
44 }
45
46 virtual ~BackgroundNoise() {
47 }
48
49 void Reset();
50
51 // Updates the parameter estimates based on the signal currently in the
52 // |sync_buffer|, and on the latest decision in |vad| if it is running.
53 void Update(const AudioMultiVector<int16_t>& sync_buffer,
54 const PostDecodeVad& vad);
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 |length| elements from |input| to the filter state. Will not copy
72 // more than |kMaxLpcOrder| elements.
73 void SetFilterState(size_t channel, const int16_t* input, size_t length);
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 BackgroundNoiseMode mode() const { return mode_; }
84
85 private:
86 static const int kThresholdIncrement = 229; // 0.0035 in Q16.
87 static const int kVecLen = 256;
88 static const int kLogVecLen = 8; // log2(kVecLen).
89 static const int kResidualLength = 64;
90 static const int kLogResidualLength = 6; // log2(kResidualLength)
91
92 struct ChannelParameters {
93 // Constructor.
94 ChannelParameters() {
95 Reset();
96 }
97
98 void Reset() {
99 energy = 2500;
100 max_energy = 0;
101 energy_update_threshold = 500000;
102 low_energy_update_threshold = 0;
103 memset(filter_state, 0, sizeof(filter_state));
104 memset(filter, 0, sizeof(filter));
105 filter[0] = 4096;
106 mute_factor = 0,
107 scale = 20000;
108 scale_shift = 24;
109 }
110
111 int32_t energy;
112 int32_t max_energy;
113 int32_t energy_update_threshold;
114 int32_t low_energy_update_threshold;
115 int16_t filter_state[kMaxLpcOrder];
116 int16_t filter[kMaxLpcOrder + 1];
117 int16_t mute_factor;
118 int16_t scale;
119 int16_t scale_shift;
120 };
121
122 int32_t CalculateAutoCorrelation(const int16_t* signal,
123 size_t length,
124 int32_t* auto_correlation) const;
125
126 // Increments the energy threshold by a factor 1 + |kThresholdIncrement|.
127 void IncrementEnergyThreshold(size_t channel, int32_t sample_energy);
128
129 // Updates the filter parameters.
130 void SaveParameters(size_t channel,
131 const int16_t* lpc_coefficients,
132 const int16_t* filter_state,
133 int32_t sample_energy,
134 int32_t residual_energy);
135
136 size_t num_channels_;
137 scoped_array<ChannelParameters> channel_parameters_;
138 bool initialized_;
139 BackgroundNoiseMode mode_;
140
141 DISALLOW_COPY_AND_ASSIGN(BackgroundNoise);
142};
143
144} // namespace webrtc
145#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_BACKGROUND_NOISE_H_