Chromium Code Reviews| Index: media/audio/audio_power_monitor.cc |
| diff --git a/media/audio/audio_power_monitor.cc b/media/audio/audio_power_monitor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a92d25c246a20fa76e42e83fad5db1086522bed7 |
| --- /dev/null |
| +++ b/media/audio/audio_power_monitor.cc |
| @@ -0,0 +1,115 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/audio/audio_power_monitor.h" |
| + |
| +#include <algorithm> |
| +#include <cmath> |
| +#include <limits> |
| + |
| +#include "base/bind.h" |
| +#include "base/float_util.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop.h" |
| +#include "base/time/time.h" |
| +#include "media/base/audio_bus.h" |
| + |
| +namespace media { |
| + |
| +// dBFS reference level is 1.0f, the maximum possible value of a sample before |
| +// clipping. |
| +static const float kReferenceLevel = 1.0f; |
|
DaleCurtis
2013/07/02 22:26:34
enum instead?
miu
2013/07/09 00:59:56
Removed it. I originally had this to make the cal
|
| + |
| +const float AudioPowerMonitor::kZeroPowerDBFS = |
|
DaleCurtis
2013/07/02 22:26:34
Hmm, I'm think this is a static initializer.
miu
2013/07/09 00:59:56
Replaced these float constants with inline static
|
| + -std::numeric_limits<float>::infinity(); |
| + |
| +const float AudioPowerMonitor::kMaxPowerDBFS = 0.0f; |
|
DaleCurtis
2013/07/02 22:26:34
enum?
miu
2013/07/09 00:59:56
See above comment. However, I'd be a little wary
|
| + |
| +AudioPowerMonitor::AudioPowerMonitor( |
| + int sample_rate, |
| + const base::TimeDelta& time_constant, |
| + const base::TimeDelta& measurement_period, |
| + base::MessageLoop* message_loop, |
| + const PowerMeasurementCallback& callback) |
| + : sample_weight_( |
| + 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))), |
| + num_frames_per_callback_(sample_rate * measurement_period.InSecondsF()), |
| + message_loop_(message_loop), |
| + notify_power_level_(callback), |
| + average_power_(0.0f), |
| + clipped_since_last_notification_(false), |
| + frames_since_last_notification_(0), |
| + last_reported_power_(-1.0f), |
| + last_reported_clipped_(false) { |
| + DCHECK(message_loop_); |
| + DCHECK(!notify_power_level_.is_null()); |
| +} |
| + |
| +AudioPowerMonitor::~AudioPowerMonitor() { |
| +} |
| + |
| +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; |
| + |
| + // Calculate a new average power by applying a first-order low-pass filter |
| + // over the audio samples in |buffer|. |
| + // |
| + // TODO(miu): Implement optimized SSE/NEON to more efficiently compute the |
| + // results (in media/base/vector_math) in soon-upcoming change. |
| + bool clipped = false; |
| + float sum_power = 0.0f; |
| + for (int i = 0; i < num_channels; ++i) { |
| + float average_power_this_channel = average_power_; |
| + const float* p = buffer.channel(i); |
| + const float* const end_of_samples = p + num_frames; |
| + for (; p < end_of_samples; ++p) { |
| + const float sample_squared = (*p) * (*p); |
| + clipped |= (sample_squared > 1.0f); |
| + average_power_this_channel += |
| + (sample_squared - average_power_this_channel) * sample_weight_; |
| + } |
| + // If data in audio buffer is garbage, ignore its effect on the result. |
| + if (base::IsNaN(average_power_this_channel)) |
| + average_power_this_channel = average_power_; |
| + sum_power += average_power_this_channel; |
| + } |
| + |
| + // Update accumulated results. |
| + average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels)); |
| + clipped_since_last_notification_ |= clipped; |
| + frames_since_last_notification_ += num_frames; |
| + |
| + // Once enough frames have been scanned, report the accumulated results. |
| + if (frames_since_last_notification_ >= num_frames_per_callback_) { |
| + // Note: Forgo making redundant callbacks when results remain unchanged. |
| + // Part of this is to pin-down the power to zero if it is insignificantly |
| + // small. |
| + const float power = (average_power_ < 1.0e-10f) ? 0.0f : average_power_; |
|
DaleCurtis
2013/07/02 22:26:34
Should probably be a top level const.
miu
2013/07/09 00:59:56
Good catch. I meant to break that out. I kept it
|
| + if (power != last_reported_power_ || |
| + clipped_since_last_notification_ != last_reported_clipped_) { |
| + const float power_dBFS = |
| + 10.0f * log10f(power / (kReferenceLevel * kReferenceLevel)); |
| + // Try to post a task to run the callback with the dBFS result. The |
| + // posting of the task is guaranteed to be non-blocking, and therefore |
| + // could fail. However, in the common case, failures should be rare (and |
| + // then the task-post will likely succeed the next time it's attempted). |
| + if (!message_loop_->TryPostTask( |
| + FROM_HERE, |
| + base::Bind(notify_power_level_, |
| + power_dBFS, clipped_since_last_notification_))) { |
| + DVLOG(2) << "TryPostTask() did not succeed."; |
| + return; |
| + } |
| + last_reported_power_ = power; |
| + last_reported_clipped_ = clipped_since_last_notification_; |
| + } |
| + clipped_since_last_notification_ = false; |
| + frames_since_last_notification_ = 0; |
| + } |
| +} |
| + |
| +} // namespace media |