This source file includes following definitions.
- Reset
- Scan
- ReadCurrentPowerAndClip
#include "media/audio/audio_power_monitor.h"
#include <algorithm>
#include <cmath>
#include "base/float_util.h"
#include "base/logging.h"
#include "base/time/time.h"
#include "media/base/audio_bus.h"
#include "media/base/vector_math.h"
namespace media {
AudioPowerMonitor::AudioPowerMonitor(
int sample_rate, const base::TimeDelta& time_constant)
: sample_weight_(
1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) {
Reset();
}
AudioPowerMonitor::~AudioPowerMonitor() {
}
void AudioPowerMonitor::Reset() {
power_reading_ = average_power_ = 0.0f;
clipped_reading_ = has_clipped_ = false;
}
void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) {
DCHECK_LE(num_frames, buffer.frames());
const int num_channels = buffer.channels();
if (num_frames <= 0 || num_channels <= 0)
return;
float sum_power = 0.0f;
for (int i = 0; i < num_channels; ++i) {
const std::pair<float, float> ewma_and_max = vector_math::EWMAAndMaxPower(
average_power_, buffer.channel(i), num_frames, sample_weight_);
if (!base::IsFinite(ewma_and_max.first)) {
sum_power += average_power_;
} else {
sum_power += ewma_and_max.first;
has_clipped_ |= (ewma_and_max.second > 1.0f);
}
}
average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels));
if (reading_lock_.Try()) {
power_reading_ = average_power_;
if (has_clipped_) {
clipped_reading_ = true;
has_clipped_ = false;
}
reading_lock_.Release();
}
}
std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() {
base::AutoLock for_reading(reading_lock_);
const float kInsignificantPower = 1.0e-10f;
const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() :
10.0f * log10f(power_reading_);
const bool clipped = clipped_reading_;
clipped_reading_ = false;
return std::make_pair(power_dbfs, clipped);
}
}