QUIC - use size_t for kMaxInitialRoundTripTimeUs.
[chromium-blink-merge.git] / media / audio / audio_power_monitor.cc
blobd8b9436060edd836e1fd0485f36c400b4acc6ec1
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/audio_power_monitor.h"
7 #include <algorithm>
8 #include <cmath>
10 #include "base/float_util.h"
11 #include "base/logging.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_bus.h"
15 namespace media {
17 AudioPowerMonitor::AudioPowerMonitor(
18 int sample_rate, const base::TimeDelta& time_constant)
19 : sample_weight_(
20 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) {
21 Reset();
24 AudioPowerMonitor::~AudioPowerMonitor() {
27 void AudioPowerMonitor::Reset() {
28 power_reading_ = average_power_ = 0.0f;
29 clipped_reading_ = has_clipped_ = false;
32 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) {
33 DCHECK_LE(num_frames, buffer.frames());
34 const int num_channels = buffer.channels();
35 if (num_frames <= 0 || num_channels <= 0)
36 return;
38 // Calculate a new average power by applying a first-order low-pass filter
39 // over the audio samples in |buffer|.
41 // TODO(miu): Implement optimized SSE/NEON to more efficiently compute the
42 // results (in media/base/vector_math) in soon-upcoming change.
43 float sum_power = 0.0f;
44 for (int i = 0; i < num_channels; ++i) {
45 float average_power_this_channel = average_power_;
46 bool clipped = false;
47 const float* p = buffer.channel(i);
48 const float* const end_of_samples = p + num_frames;
49 for (; p < end_of_samples; ++p) {
50 const float sample = *p;
51 const float sample_squared = sample * sample;
52 clipped |= (sample_squared > 1.0f);
53 average_power_this_channel +=
54 (sample_squared - average_power_this_channel) * sample_weight_;
56 // If data in audio buffer is garbage, ignore its effect on the result.
57 if (base::IsNaN(average_power_this_channel)) {
58 average_power_this_channel = average_power_;
59 clipped = false;
61 sum_power += average_power_this_channel;
62 has_clipped_ |= clipped;
65 // Update accumulated results, with clamping for sanity.
66 average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels));
68 // Push results for reading by other threads, non-blocking.
69 if (reading_lock_.Try()) {
70 power_reading_ = average_power_;
71 if (has_clipped_) {
72 clipped_reading_ = true;
73 has_clipped_ = false;
75 reading_lock_.Release();
79 std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() {
80 base::AutoLock for_reading(reading_lock_);
82 // Convert power level to dBFS units, and pin it down to zero if it is
83 // insignificantly small.
84 const float kInsignificantPower = 1.0e-10f; // -100 dBFS
85 const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() :
86 10.0f * log10f(power_reading_);
88 const bool clipped = clipped_reading_;
89 clipped_reading_ = false;
91 return std::make_pair(power_dbfs, clipped);
94 } // namespace media