| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/audio_power_monitor.h" | 5 #include "media/audio/audio_power_monitor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 14 #include "media/base/vector_math.h" | 14 #include "media/base/vector_math.h" |
| 15 | 15 |
| 16 namespace media { | 16 namespace media { |
| 17 | 17 |
| 18 AudioPowerMonitor::AudioPowerMonitor( | 18 AudioPowerMonitor::AudioPowerMonitor( |
| 19 int sample_rate, const base::TimeDelta& time_constant) | 19 int sample_rate, const base::TimeDelta& time_constant) |
| 20 : sample_weight_( | 20 : sample_weight_( |
| 21 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) { | 21 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) { |
| 22 Reset(); | 22 Reset(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 AudioPowerMonitor::~AudioPowerMonitor() { | 25 AudioPowerMonitor::~AudioPowerMonitor() { |
| 26 } | 26 } |
| 27 | 27 |
| 28 void AudioPowerMonitor::Reset() { | 28 void AudioPowerMonitor::Reset() { |
| 29 power_reading_ = average_power_ = 0.0f; | 29 // These are only read/written by Scan(), but Scan() should not be running |
| 30 clipped_reading_ = has_clipped_ = false; | 30 // when Reset() is called. |
| 31 average_power_ = 0.0f; |
| 32 has_clipped_ = false; |
| 33 |
| 34 // These are the copies read by ReadCurrentPowerAndClip(). The lock here is |
| 35 // not necessary, as racey writes/reads are acceptable, but this prevents |
| 36 // quality-enhancement tools like TSAN from complaining. |
| 37 base::AutoLock for_reset(reading_lock_); |
| 38 power_reading_ = 0.0f; |
| 39 clipped_reading_ = false; |
| 31 } | 40 } |
| 32 | 41 |
| 33 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) { | 42 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) { |
| 34 DCHECK_LE(num_frames, buffer.frames()); | 43 DCHECK_LE(num_frames, buffer.frames()); |
| 35 const int num_channels = buffer.channels(); | 44 const int num_channels = buffer.channels(); |
| 36 if (num_frames <= 0 || num_channels <= 0) | 45 if (num_frames <= 0 || num_channels <= 0) |
| 37 return; | 46 return; |
| 38 | 47 |
| 39 // Calculate a new average power by applying a first-order low-pass filter | 48 // Calculate a new average power by applying a first-order low-pass filter |
| 40 // (a.k.a. an exponentially-weighted moving average) over the audio samples in | 49 // (a.k.a. an exponentially-weighted moving average) over the audio samples in |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() : | 84 const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() : |
| 76 10.0f * log10f(power_reading_); | 85 10.0f * log10f(power_reading_); |
| 77 | 86 |
| 78 const bool clipped = clipped_reading_; | 87 const bool clipped = clipped_reading_; |
| 79 clipped_reading_ = false; | 88 clipped_reading_ = false; |
| 80 | 89 |
| 81 return std::make_pair(power_dbfs, clipped); | 90 return std::make_pair(power_dbfs, clipped); |
| 82 } | 91 } |
| 83 | 92 |
| 84 } // namespace media | 93 } // namespace media |
| OLD | NEW |