2013-01-29 12:09:21 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_
|
|
|
|
|
#define MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <string.h> // size_t
|
2016-02-14 09:28:33 -08:00
|
|
|
#include <memory>
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2019-04-15 12:32:23 +02:00
|
|
|
#include "api/array_view.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/constructor_magic.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
// Forward declarations.
|
2018-10-23 12:03:01 +02:00
|
|
|
class AudioMultiVector;
|
2013-01-29 12:09:21 +00:00
|
|
|
class PostDecodeVad;
|
|
|
|
|
|
|
|
|
|
// This class handles estimation of background noise parameters.
|
|
|
|
|
class BackgroundNoise {
|
|
|
|
|
public:
|
|
|
|
|
// TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10.
|
|
|
|
|
// Will work anyway, but probably sound a little worse.
|
2019-04-15 12:32:23 +02:00
|
|
|
static constexpr size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4.
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2013-07-31 15:54:00 +00:00
|
|
|
explicit BackgroundNoise(size_t num_channels);
|
|
|
|
|
virtual ~BackgroundNoise();
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
|
|
// Updates the parameter estimates based on the signal currently in the
|
|
|
|
|
// |sync_buffer|, and on the latest decision in |vad| if it is running.
|
2019-06-28 10:49:39 +02:00
|
|
|
// Returns true if the filter parameters are updated.
|
|
|
|
|
bool Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad);
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2019-04-15 12:32:23 +02:00
|
|
|
// Generates background noise given a random vector and writes the output to
|
|
|
|
|
// |buffer|.
|
|
|
|
|
void GenerateBackgroundNoise(rtc::ArrayView<const int16_t> random_vector,
|
|
|
|
|
size_t channel,
|
|
|
|
|
int mute_slope,
|
|
|
|
|
bool too_many_expands,
|
|
|
|
|
size_t num_noise_samples,
|
|
|
|
|
int16_t* buffer);
|
|
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
// Returns |energy_| for |channel|.
|
|
|
|
|
int32_t Energy(size_t channel) const;
|
|
|
|
|
|
|
|
|
|
// Sets the value of |mute_factor_| for |channel| to |value|.
|
|
|
|
|
void SetMuteFactor(size_t channel, int16_t value);
|
|
|
|
|
|
|
|
|
|
// Returns |mute_factor_| for |channel|.
|
|
|
|
|
int16_t MuteFactor(size_t channel) const;
|
|
|
|
|
|
|
|
|
|
// Returns a pointer to |filter_| for |channel|.
|
|
|
|
|
const int16_t* Filter(size_t channel) const;
|
|
|
|
|
|
|
|
|
|
// Returns a pointer to |filter_state_| for |channel|.
|
|
|
|
|
const int16_t* FilterState(size_t channel) const;
|
|
|
|
|
|
2019-04-15 12:32:23 +02:00
|
|
|
// Copies |input| to the filter state. Will not copy more than |kMaxLpcOrder|
|
|
|
|
|
// elements.
|
|
|
|
|
void SetFilterState(size_t channel, rtc::ArrayView<const int16_t> input);
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Returns |scale_| for |channel|.
|
|
|
|
|
int16_t Scale(size_t channel) const;
|
|
|
|
|
|
|
|
|
|
// Returns |scale_shift_| for |channel|.
|
|
|
|
|
int16_t ScaleShift(size_t channel) const;
|
|
|
|
|
|
|
|
|
|
// Accessors.
|
|
|
|
|
bool initialized() const { return initialized_; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static const int kThresholdIncrement = 229; // 0.0035 in Q16.
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
static const size_t kVecLen = 256;
|
2013-01-29 12:09:21 +00:00
|
|
|
static const int kLogVecLen = 8; // log2(kVecLen).
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
static const size_t kResidualLength = 64;
|
Match existing type usage better.
This makes a variety of small changes to synchronize bits of code using different types, remove useless code or casts, and add explicit casts in some places previously doing implicit ones. For example:
* Change a few type declarations to better match how the majority of code uses those objects.
* Eliminate "< 0" check for unsigned values.
* Replace "(float)sin(x)", where |x| is also a float, with "sinf(x)", and similar.
* Add casts to uint32_t in many places timestamps were used and the existing code stored signed values into the unsigned objects.
* Remove downcasts when the results would be passed to a larger type, e.g. calling "foo((int16_t)x)" with an int |x| when foo() takes an int instead of an int16_t.
* Similarly, add casts when passing a larger type to a function taking a smaller one.
* Add casts to int16_t when doing something like "int16_t = int16_t + int16_t" as the "+" operation would implicitly upconvert to int, and similar.
* Use "false" instead of "0" for setting a bool.
* Shift a few temp types when doing a multi-stage calculation involving typecasts, so as to put the most logical/semantically correct type possible into the temps. For example, when doing "int foo = int + int; size_t bar = (size_t)foo + size_t;", we might change |foo| to a size_t and move the cast if it makes more sense for |foo| to be represented as a size_t.
BUG=none
R=andrew@webrtc.org, asapersson@webrtc.org, henrika@webrtc.org, juberti@webrtc.org, kwiberg@webrtc.org
TBR=andrew, asapersson, henrika
Review URL: https://codereview.webrtc.org/1168753002
Cr-Commit-Position: refs/heads/master@{#9419}
2015-06-11 12:55:50 -07:00
|
|
|
static const int16_t kLogResidualLength = 6; // log2(kResidualLength)
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
struct ChannelParameters {
|
|
|
|
|
// Constructor.
|
|
|
|
|
ChannelParameters() { Reset(); }
|
|
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
|
energy = 2500;
|
|
|
|
|
max_energy = 0;
|
|
|
|
|
energy_update_threshold = 500000;
|
|
|
|
|
low_energy_update_threshold = 0;
|
|
|
|
|
memset(filter_state, 0, sizeof(filter_state));
|
|
|
|
|
memset(filter, 0, sizeof(filter));
|
|
|
|
|
filter[0] = 4096;
|
2018-03-13 17:14:10 +01:00
|
|
|
mute_factor = 0;
|
2013-01-29 12:09:21 +00:00
|
|
|
scale = 20000;
|
|
|
|
|
scale_shift = 24;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t energy;
|
|
|
|
|
int32_t max_energy;
|
|
|
|
|
int32_t energy_update_threshold;
|
|
|
|
|
int32_t low_energy_update_threshold;
|
|
|
|
|
int16_t filter_state[kMaxLpcOrder];
|
|
|
|
|
int16_t filter[kMaxLpcOrder + 1];
|
|
|
|
|
int16_t mute_factor;
|
|
|
|
|
int16_t scale;
|
|
|
|
|
int16_t scale_shift;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int32_t CalculateAutoCorrelation(const int16_t* signal,
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t length,
|
2013-01-29 12:09:21 +00:00
|
|
|
int32_t* auto_correlation) const;
|
|
|
|
|
|
|
|
|
|
// Increments the energy threshold by a factor 1 + |kThresholdIncrement|.
|
|
|
|
|
void IncrementEnergyThreshold(size_t channel, int32_t sample_energy);
|
|
|
|
|
|
|
|
|
|
// Updates the filter parameters.
|
|
|
|
|
void SaveParameters(size_t channel,
|
|
|
|
|
const int16_t* lpc_coefficients,
|
|
|
|
|
const int16_t* filter_state,
|
|
|
|
|
int32_t sample_energy,
|
|
|
|
|
int32_t residual_energy);
|
|
|
|
|
|
|
|
|
|
size_t num_channels_;
|
2016-02-14 09:28:33 -08:00
|
|
|
std::unique_ptr<ChannelParameters[]> channel_parameters_;
|
2013-01-29 12:09:21 +00:00
|
|
|
bool initialized_;
|
|
|
|
|
|
2015-09-16 05:37:44 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(BackgroundNoise);
|
2013-01-29 12:09:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_
|