2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_BACKGROUND_NOISE_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_BACKGROUND_NOISE_H_
14 #include <string.h> // size_t
16 #include "webrtc/modules/audio_coding/neteq4/audio_multi_vector.h"
17 #include "webrtc/modules/audio_coding/neteq4/interface/neteq.h"
18 #include "webrtc/system_wrappers/interface/constructor_magic.h"
19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
20 #include "webrtc/typedefs.h"
24 // Forward declarations.
27 // This class handles estimation of background noise parameters.
28 class BackgroundNoise {
30 // TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10.
31 // Will work anyway, but probably sound a little worse.
32 static const int kMaxLpcOrder = 8; // 32000 / 8000 + 4.
34 explicit BackgroundNoise(size_t num_channels);
35 virtual ~BackgroundNoise();
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.
41 void Update(const AudioMultiVector& sync_buffer,
42 const PostDecodeVad& vad);
44 // Returns |energy_| for |channel|.
45 int32_t Energy(size_t channel) const;
47 // Sets the value of |mute_factor_| for |channel| to |value|.
48 void SetMuteFactor(size_t channel, int16_t value);
50 // Returns |mute_factor_| for |channel|.
51 int16_t MuteFactor(size_t channel) const;
53 // Returns a pointer to |filter_| for |channel|.
54 const int16_t* Filter(size_t channel) const;
56 // Returns a pointer to |filter_state_| for |channel|.
57 const int16_t* FilterState(size_t channel) const;
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);
63 // Returns |scale_| for |channel|.
64 int16_t Scale(size_t channel) const;
66 // Returns |scale_shift_| for |channel|.
67 int16_t ScaleShift(size_t channel) const;
70 bool initialized() const { return initialized_; }
71 NetEqBackgroundNoiseMode mode() const { return mode_; }
73 // Sets the mode of the background noise playout for cases when there is long
74 // duration of packet loss.
75 void set_mode(NetEqBackgroundNoiseMode mode) { mode_ = mode; }
78 static const int kThresholdIncrement = 229; // 0.0035 in Q16.
79 static const int kVecLen = 256;
80 static const int kLogVecLen = 8; // log2(kVecLen).
81 static const int kResidualLength = 64;
82 static const int kLogResidualLength = 6; // log2(kResidualLength)
84 struct ChannelParameters {
93 energy_update_threshold = 500000;
94 low_energy_update_threshold = 0;
95 memset(filter_state, 0, sizeof(filter_state));
96 memset(filter, 0, sizeof(filter));
105 int32_t energy_update_threshold;
106 int32_t low_energy_update_threshold;
107 int16_t filter_state[kMaxLpcOrder];
108 int16_t filter[kMaxLpcOrder + 1];
114 int32_t CalculateAutoCorrelation(const int16_t* signal,
116 int32_t* auto_correlation) const;
118 // Increments the energy threshold by a factor 1 + |kThresholdIncrement|.
119 void IncrementEnergyThreshold(size_t channel, int32_t sample_energy);
121 // Updates the filter parameters.
122 void SaveParameters(size_t channel,
123 const int16_t* lpc_coefficients,
124 const int16_t* filter_state,
125 int32_t sample_energy,
126 int32_t residual_energy);
128 size_t num_channels_;
129 scoped_ptr<ChannelParameters[]> channel_parameters_;
131 NetEqBackgroundNoiseMode mode_;
133 DISALLOW_COPY_AND_ASSIGN(BackgroundNoise);
136 } // namespace webrtc
137 #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_BACKGROUND_NOISE_H_