2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-01-30 09:39:08 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "level_estimator_impl.h"
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <string.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
#include "audio_processing_impl.h"
|
|
|
|
|
#include "audio_buffer.h"
|
2011-11-15 16:57:56 +00:00
|
|
|
#include "critical_section_wrapper.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2011-11-15 16:57:56 +00:00
|
|
|
namespace {
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
const double kMaxSquaredLevel = 32768.0 * 32768.0;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
class Level {
|
|
|
|
|
public:
|
|
|
|
|
static const int kMinLevel = 127;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
Level()
|
|
|
|
|
: sum_square_(0.0),
|
|
|
|
|
sample_count_(0) {}
|
|
|
|
|
~Level() {}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
void Init() {
|
|
|
|
|
sum_square_ = 0.0;
|
|
|
|
|
sample_count_ = 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
void Process(int16_t* data, int length) {
|
|
|
|
|
assert(data != NULL);
|
|
|
|
|
assert(length > 0);
|
|
|
|
|
sum_square_ += SumSquare(data, length);
|
|
|
|
|
sample_count_ += length;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
void ProcessMuted(int length) {
|
|
|
|
|
assert(length > 0);
|
|
|
|
|
sample_count_ += length;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-15 16:57:56 +00:00
|
|
|
|
|
|
|
|
int RMS() {
|
|
|
|
|
if (sample_count_ == 0 || sum_square_ == 0.0) {
|
|
|
|
|
Init();
|
|
|
|
|
return kMinLevel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normalize by the max level.
|
|
|
|
|
double rms = sum_square_ / (sample_count_ * kMaxSquaredLevel);
|
|
|
|
|
// 20log_10(x^0.5) = 10log_10(x)
|
|
|
|
|
rms = 10 * log10(rms);
|
|
|
|
|
if (rms > 0)
|
|
|
|
|
rms = 0;
|
|
|
|
|
else if (rms < -kMinLevel)
|
|
|
|
|
rms = -kMinLevel;
|
|
|
|
|
|
|
|
|
|
rms = -rms;
|
|
|
|
|
Init();
|
|
|
|
|
return static_cast<int>(rms + 0.5);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-15 16:57:56 +00:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static double SumSquare(int16_t* data, int length) {
|
|
|
|
|
double sum_square = 0.0;
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
|
double data_d = static_cast<double>(data[i]);
|
|
|
|
|
sum_square += data_d * data_d;
|
|
|
|
|
}
|
|
|
|
|
return sum_square;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
double sum_square_;
|
|
|
|
|
int sample_count_;
|
|
|
|
|
};
|
2011-07-07 08:21:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
LevelEstimatorImpl::LevelEstimatorImpl(const AudioProcessingImpl* apm)
|
|
|
|
|
: ProcessingComponent(apm),
|
|
|
|
|
apm_(apm) {}
|
|
|
|
|
|
|
|
|
|
LevelEstimatorImpl::~LevelEstimatorImpl() {}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
|
|
|
|
|
if (!is_component_enabled()) {
|
2011-07-07 08:21:25 +00:00
|
|
|
return apm_->kNoError;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
Level* level = static_cast<Level*>(handle(0));
|
|
|
|
|
if (audio->is_muted()) {
|
|
|
|
|
level->ProcessMuted(audio->samples_per_channel());
|
2011-07-07 08:21:25 +00:00
|
|
|
return apm_->kNoError;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int16_t* mixed_data = audio->data(0);
|
|
|
|
|
if (audio->num_channels() > 1) {
|
|
|
|
|
audio->CopyAndMix(1);
|
|
|
|
|
mixed_data = audio->mixed_data(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
level->Process(mixed_data, audio->samples_per_channel());
|
|
|
|
|
|
|
|
|
|
return apm_->kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int LevelEstimatorImpl::Enable(bool enable) {
|
2012-01-30 20:51:15 +00:00
|
|
|
CriticalSectionScoped crit_scoped(apm_->crit());
|
2011-11-15 16:57:56 +00:00
|
|
|
return EnableComponent(enable);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LevelEstimatorImpl::is_enabled() const {
|
|
|
|
|
return is_component_enabled();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int LevelEstimatorImpl::RMS() {
|
|
|
|
|
if (!is_component_enabled()) {
|
2011-07-07 08:21:25 +00:00
|
|
|
return apm_->kNotEnabledError;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
Level* level = static_cast<Level*>(handle(0));
|
|
|
|
|
return level->RMS();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* LevelEstimatorImpl::CreateHandle() const {
|
2011-11-15 16:57:56 +00:00
|
|
|
return new Level;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int LevelEstimatorImpl::DestroyHandle(void* handle) const {
|
|
|
|
|
assert(handle != NULL);
|
|
|
|
|
Level* level = static_cast<Level*>(handle);
|
|
|
|
|
delete level;
|
|
|
|
|
return apm_->kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int LevelEstimatorImpl::InitializeHandle(void* handle) const {
|
|
|
|
|
assert(handle != NULL);
|
|
|
|
|
Level* level = static_cast<Level*>(handle);
|
|
|
|
|
level->Init();
|
|
|
|
|
|
|
|
|
|
return apm_->kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LevelEstimatorImpl::ConfigureHandle(void* /*handle*/) const {
|
2011-11-15 16:57:56 +00:00
|
|
|
return apm_->kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LevelEstimatorImpl::num_handles_required() const {
|
2011-11-15 16:57:56 +00:00
|
|
|
return 1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LevelEstimatorImpl::GetHandleError(void* handle) const {
|
|
|
|
|
// The component has no detailed errors.
|
|
|
|
|
assert(handle != NULL);
|
|
|
|
|
return apm_->kUnspecifiedError;
|
|
|
|
|
}
|
|
|
|
|
} // namespace webrtc
|