blob: 8e6d5890a07b3b7414cd89dfe66f625a7b41d03a [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
Jonas Olssona4d87372019-07-05 19:08:33 +020015
kwiberg2d0c3322016-02-14 09:28:33 -080016#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017
Alessio Bazzica7e53be02019-04-15 12:32:23 +020018#include "api/array_view.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
20namespace webrtc {
21
22// Forward declarations.
Yves Gerey988cc082018-10-23 12:03:01 +020023class AudioMultiVector;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024class PostDecodeVad;
25
26// This class handles estimation of background noise parameters.
27class BackgroundNoise {
28 public:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029 // TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10.
30 // Will work anyway, but probably sound a little worse.
Alessio Bazzica7e53be02019-04-15 12:32:23 +020031 static constexpr size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000033 explicit BackgroundNoise(size_t num_channels);
34 virtual ~BackgroundNoise();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090036 BackgroundNoise(const BackgroundNoise&) = delete;
37 BackgroundNoise& operator=(const BackgroundNoise&) = delete;
38
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 void Reset();
40
41 // Updates the parameter estimates based on the signal currently in the
Artem Titovd00ce742021-07-28 20:00:17 +020042 // `sync_buffer`, and on the latest decision in `vad` if it is running.
Alessio Bazzica60bfb3d2019-06-28 10:49:39 +020043 // Returns true if the filter parameters are updated.
44 bool Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045
Alessio Bazzica7e53be02019-04-15 12:32:23 +020046 // Generates background noise given a random vector and writes the output to
Artem Titovd00ce742021-07-28 20:00:17 +020047 // `buffer`.
Alessio Bazzica7e53be02019-04-15 12:32:23 +020048 void GenerateBackgroundNoise(rtc::ArrayView<const int16_t> random_vector,
49 size_t channel,
50 int mute_slope,
51 bool too_many_expands,
52 size_t num_noise_samples,
53 int16_t* buffer);
54
Artem Titovd00ce742021-07-28 20:00:17 +020055 // Returns `energy_` for `channel`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056 int32_t Energy(size_t channel) const;
57
Artem Titovd00ce742021-07-28 20:00:17 +020058 // Sets the value of `mute_factor_` for `channel` to `value`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 void SetMuteFactor(size_t channel, int16_t value);
60
Artem Titovd00ce742021-07-28 20:00:17 +020061 // Returns `mute_factor_` for `channel`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062 int16_t MuteFactor(size_t channel) const;
63
Artem Titovd00ce742021-07-28 20:00:17 +020064 // Returns a pointer to `filter_` for `channel`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 const int16_t* Filter(size_t channel) const;
66
Artem Titovd00ce742021-07-28 20:00:17 +020067 // Returns a pointer to `filter_state_` for `channel`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 const int16_t* FilterState(size_t channel) const;
69
Artem Titovd00ce742021-07-28 20:00:17 +020070 // Copies `input` to the filter state. Will not copy more than `kMaxLpcOrder`
Alessio Bazzica7e53be02019-04-15 12:32:23 +020071 // elements.
72 void SetFilterState(size_t channel, rtc::ArrayView<const int16_t> input);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073
Artem Titovd00ce742021-07-28 20:00:17 +020074 // Returns `scale_` for `channel`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 int16_t Scale(size_t channel) const;
76
Artem Titovd00ce742021-07-28 20:00:17 +020077 // Returns `scale_shift_` for `channel`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078 int16_t ScaleShift(size_t channel) const;
79
80 // Accessors.
81 bool initialized() const { return initialized_; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082
83 private:
84 static const int kThresholdIncrement = 229; // 0.0035 in Q16.
Peter Kastingdce40cf2015-08-24 14:52:23 -070085 static const size_t kVecLen = 256;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086 static const int kLogVecLen = 8; // log2(kVecLen).
Peter Kastingdce40cf2015-08-24 14:52:23 -070087 static const size_t kResidualLength = 64;
Peter Kastingb7e50542015-06-11 12:55:50 -070088 static const int16_t kLogResidualLength = 6; // log2(kResidualLength)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089
90 struct ChannelParameters {
91 // Constructor.
Yves Gerey665174f2018-06-19 15:03:05 +020092 ChannelParameters() { Reset(); }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093
94 void Reset() {
95 energy = 2500;
96 max_energy = 0;
97 energy_update_threshold = 500000;
98 low_energy_update_threshold = 0;
99 memset(filter_state, 0, sizeof(filter_state));
100 memset(filter, 0, sizeof(filter));
101 filter[0] = 4096;
Henrik Lundine5531392018-03-13 17:14:10 +0100102 mute_factor = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000103 scale = 20000;
104 scale_shift = 24;
105 }
106
107 int32_t energy;
108 int32_t max_energy;
109 int32_t energy_update_threshold;
110 int32_t low_energy_update_threshold;
111 int16_t filter_state[kMaxLpcOrder];
112 int16_t filter[kMaxLpcOrder + 1];
113 int16_t mute_factor;
114 int16_t scale;
115 int16_t scale_shift;
116 };
117
118 int32_t CalculateAutoCorrelation(const int16_t* signal,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 size_t length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120 int32_t* auto_correlation) const;
121
Artem Titovd00ce742021-07-28 20:00:17 +0200122 // Increments the energy threshold by a factor 1 + `kThresholdIncrement`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123 void IncrementEnergyThreshold(size_t channel, int32_t sample_energy);
124
125 // Updates the filter parameters.
126 void SaveParameters(size_t channel,
127 const int16_t* lpc_coefficients,
128 const int16_t* filter_state,
129 int32_t sample_energy,
130 int32_t residual_energy);
131
132 size_t num_channels_;
kwiberg2d0c3322016-02-14 09:28:33 -0800133 std::unique_ptr<ChannelParameters[]> channel_parameters_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000134 bool initialized_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135};
136
137} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200138#endif // MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_