Update to current webrtc library
This is from the upstream library commit id 3326535126e435f1ba647885ce43a8f0f3d317eb, corresponding to Chromium 88.0.4290.1.
This commit is contained in:
@ -8,54 +8,121 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "webrtc/modules/audio_processing/rms_level.h"
|
||||
#include "modules/audio_processing/rms_level.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
static constexpr float kMaxSquaredLevel = 32768 * 32768;
|
||||
// kMinLevel is the level corresponding to kMinLevelDb, that is 10^(-127/10).
|
||||
static constexpr float kMinLevel = 1.995262314968883e-13f;
|
||||
|
||||
static const float kMaxSquaredLevel = 32768 * 32768;
|
||||
|
||||
RMSLevel::RMSLevel()
|
||||
: sum_square_(0),
|
||||
sample_count_(0) {}
|
||||
|
||||
RMSLevel::~RMSLevel() {}
|
||||
|
||||
void RMSLevel::Reset() {
|
||||
sum_square_ = 0;
|
||||
sample_count_ = 0;
|
||||
}
|
||||
|
||||
void RMSLevel::Process(const int16_t* data, size_t length) {
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
sum_square_ += data[i] * data[i];
|
||||
// Calculates the normalized RMS value from a mean square value. The input
|
||||
// should be the sum of squared samples divided by the number of samples. The
|
||||
// value will be normalized to full range before computing the RMS, wich is
|
||||
// returned as a negated dBfs. That is, 0 is full amplitude while 127 is very
|
||||
// faint.
|
||||
int ComputeRms(float mean_square) {
|
||||
if (mean_square <= kMinLevel * kMaxSquaredLevel) {
|
||||
// Very faint; simply return the minimum value.
|
||||
return RmsLevel::kMinLevelDb;
|
||||
}
|
||||
sample_count_ += length;
|
||||
}
|
||||
|
||||
void RMSLevel::ProcessMuted(size_t length) {
|
||||
sample_count_ += length;
|
||||
}
|
||||
|
||||
int RMSLevel::RMS() {
|
||||
if (sample_count_ == 0 || sum_square_ == 0) {
|
||||
Reset();
|
||||
return kMinLevel;
|
||||
}
|
||||
|
||||
// Normalize by the max level.
|
||||
float rms = sum_square_ / (sample_count_ * kMaxSquaredLevel);
|
||||
const float mean_square_norm = mean_square / kMaxSquaredLevel;
|
||||
RTC_DCHECK_GT(mean_square_norm, kMinLevel);
|
||||
// 20log_10(x^0.5) = 10log_10(x)
|
||||
rms = 10 * log10(rms);
|
||||
assert(rms <= 0);
|
||||
if (rms < -kMinLevel)
|
||||
rms = -kMinLevel;
|
||||
const float rms = 10.f * std::log10(mean_square_norm);
|
||||
RTC_DCHECK_LE(rms, 0.f);
|
||||
RTC_DCHECK_GT(rms, -RmsLevel::kMinLevelDb);
|
||||
// Return the negated value.
|
||||
return static_cast<int>(-rms + 0.5f);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
rms = -rms;
|
||||
RmsLevel::RmsLevel() {
|
||||
Reset();
|
||||
return static_cast<int>(rms + 0.5);
|
||||
}
|
||||
|
||||
RmsLevel::~RmsLevel() = default;
|
||||
|
||||
void RmsLevel::Reset() {
|
||||
sum_square_ = 0.f;
|
||||
sample_count_ = 0;
|
||||
max_sum_square_ = 0.f;
|
||||
block_size_ = absl::nullopt;
|
||||
}
|
||||
|
||||
void RmsLevel::Analyze(rtc::ArrayView<const int16_t> data) {
|
||||
if (data.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
CheckBlockSize(data.size());
|
||||
|
||||
const float sum_square =
|
||||
std::accumulate(data.begin(), data.end(), 0.f,
|
||||
[](float a, int16_t b) { return a + b * b; });
|
||||
RTC_DCHECK_GE(sum_square, 0.f);
|
||||
sum_square_ += sum_square;
|
||||
sample_count_ += data.size();
|
||||
|
||||
max_sum_square_ = std::max(max_sum_square_, sum_square);
|
||||
}
|
||||
|
||||
void RmsLevel::Analyze(rtc::ArrayView<const float> data) {
|
||||
if (data.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
CheckBlockSize(data.size());
|
||||
|
||||
float sum_square = 0.f;
|
||||
|
||||
for (float data_k : data) {
|
||||
int16_t tmp =
|
||||
static_cast<int16_t>(std::min(std::max(data_k, -32768.f), 32767.f));
|
||||
sum_square += tmp * tmp;
|
||||
}
|
||||
RTC_DCHECK_GE(sum_square, 0.f);
|
||||
sum_square_ += sum_square;
|
||||
sample_count_ += data.size();
|
||||
|
||||
max_sum_square_ = std::max(max_sum_square_, sum_square);
|
||||
}
|
||||
|
||||
void RmsLevel::AnalyzeMuted(size_t length) {
|
||||
CheckBlockSize(length);
|
||||
sample_count_ += length;
|
||||
}
|
||||
|
||||
int RmsLevel::Average() {
|
||||
int rms = (sample_count_ == 0) ? RmsLevel::kMinLevelDb
|
||||
: ComputeRms(sum_square_ / sample_count_);
|
||||
Reset();
|
||||
return rms;
|
||||
}
|
||||
|
||||
RmsLevel::Levels RmsLevel::AverageAndPeak() {
|
||||
// Note that block_size_ should by design always be non-empty when
|
||||
// sample_count_ != 0. Also, the * operator of absl::optional enforces this
|
||||
// with a DCHECK.
|
||||
Levels levels = (sample_count_ == 0)
|
||||
? Levels{RmsLevel::kMinLevelDb, RmsLevel::kMinLevelDb}
|
||||
: Levels{ComputeRms(sum_square_ / sample_count_),
|
||||
ComputeRms(max_sum_square_ / *block_size_)};
|
||||
Reset();
|
||||
return levels;
|
||||
}
|
||||
|
||||
void RmsLevel::CheckBlockSize(size_t block_size) {
|
||||
if (block_size_ != block_size) {
|
||||
Reset();
|
||||
block_size_ = block_size;
|
||||
}
|
||||
}
|
||||
} // namespace webrtc
|
||||
|
Reference in New Issue
Block a user