Chromium Code Reviews| 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/bind.h" | |
| 11 #include "base/float_util.h" | 10 #include "base/float_util.h" |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 15 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 16 | 14 |
| 17 namespace media { | 15 namespace media { |
| 18 | 16 |
| 19 AudioPowerMonitor::AudioPowerMonitor( | 17 AudioPowerMonitor::AudioPowerMonitor( |
| 20 int sample_rate, | 18 int sample_rate, const base::TimeDelta& time_constant) |
| 21 const base::TimeDelta& time_constant, | |
| 22 const base::TimeDelta& measurement_period, | |
| 23 base::MessageLoop* message_loop, | |
| 24 const PowerMeasurementCallback& callback) | |
| 25 : sample_weight_( | 19 : sample_weight_( |
| 26 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))), | 20 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) { |
| 27 num_frames_per_callback_(sample_rate * measurement_period.InSecondsF()), | 21 Reset(); |
| 28 message_loop_(message_loop), | |
| 29 power_level_callback_(callback), | |
| 30 average_power_(0.0f), | |
| 31 clipped_since_last_notification_(false), | |
| 32 frames_since_last_notification_(0), | |
| 33 last_reported_power_(-1.0f), | |
| 34 last_reported_clipped_(false) { | |
| 35 DCHECK(message_loop_); | |
| 36 DCHECK(!power_level_callback_.is_null()); | |
| 37 } | 22 } |
| 38 | 23 |
| 39 AudioPowerMonitor::~AudioPowerMonitor() { | 24 AudioPowerMonitor::~AudioPowerMonitor() { |
| 40 } | 25 } |
| 41 | 26 |
| 27 void AudioPowerMonitor::Reset() { | |
| 28 power_reading_ = average_power_ = 0.0f; | |
|
DaleCurtis
2013/08/12 20:43:56
Needs documentation that it can't be called while
miu
2013/08/12 21:10:54
Done.
| |
| 29 clipped_reading_ = has_clipped_ = false; | |
| 30 } | |
| 31 | |
| 42 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) { | 32 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) { |
| 43 DCHECK_LE(num_frames, buffer.frames()); | 33 DCHECK_LE(num_frames, buffer.frames()); |
| 44 const int num_channels = buffer.channels(); | 34 const int num_channels = buffer.channels(); |
| 45 if (num_frames <= 0 || num_channels <= 0) | 35 if (num_frames <= 0 || num_channels <= 0) |
| 46 return; | 36 return; |
| 47 | 37 |
| 48 // Calculate a new average power by applying a first-order low-pass filter | 38 // Calculate a new average power by applying a first-order low-pass filter |
| 49 // over the audio samples in |buffer|. | 39 // over the audio samples in |buffer|. |
| 50 // | 40 // |
| 51 // TODO(miu): Implement optimized SSE/NEON to more efficiently compute the | 41 // TODO(miu): Implement optimized SSE/NEON to more efficiently compute the |
| 52 // results (in media/base/vector_math) in soon-upcoming change. | 42 // results (in media/base/vector_math) in soon-upcoming change. |
| 53 bool clipped = false; | 43 bool clipped = false; |
|
DaleCurtis
2013/08/12 20:43:56
Just use has_clipped_ directly?
miu
2013/08/12 21:10:54
Done.
| |
| 54 float sum_power = 0.0f; | 44 float sum_power = 0.0f; |
| 55 for (int i = 0; i < num_channels; ++i) { | 45 for (int i = 0; i < num_channels; ++i) { |
| 56 float average_power_this_channel = average_power_; | 46 float average_power_this_channel = average_power_; |
| 57 const float* p = buffer.channel(i); | 47 const float* p = buffer.channel(i); |
| 58 const float* const end_of_samples = p + num_frames; | 48 const float* const end_of_samples = p + num_frames; |
| 59 for (; p < end_of_samples; ++p) { | 49 for (; p < end_of_samples; ++p) { |
| 60 const float sample = *p; | 50 const float sample = *p; |
| 61 const float sample_squared = sample * sample; | 51 const float sample_squared = sample * sample; |
| 62 clipped |= (sample_squared > 1.0f); | 52 clipped |= (sample_squared > 1.0f); |
| 63 average_power_this_channel += | 53 average_power_this_channel += |
| 64 (sample_squared - average_power_this_channel) * sample_weight_; | 54 (sample_squared - average_power_this_channel) * sample_weight_; |
| 65 } | 55 } |
| 66 // If data in audio buffer is garbage, ignore its effect on the result. | 56 // If data in audio buffer is garbage, ignore its effect on the result. |
| 67 if (base::IsNaN(average_power_this_channel)) | 57 if (base::IsNaN(average_power_this_channel)) |
| 68 average_power_this_channel = average_power_; | 58 average_power_this_channel = average_power_; |
| 69 sum_power += average_power_this_channel; | 59 sum_power += average_power_this_channel; |
| 70 } | 60 } |
| 71 | 61 |
| 72 // Update accumulated results. | 62 // Update accumulated results. |
| 73 average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels)); | 63 average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels)); |
| 74 clipped_since_last_notification_ |= clipped; | 64 if (clipped) |
| 75 frames_since_last_notification_ += num_frames; | 65 has_clipped_ = true; |
| 76 | 66 |
| 77 // Once enough frames have been scanned, report the accumulated results. | 67 // Push results for reading by other threads, non-blocking. |
| 78 if (frames_since_last_notification_ >= num_frames_per_callback_) { | 68 if (reading_lock_.Try()) { |
| 79 // Note: Forgo making redundant callbacks when results remain unchanged. | 69 power_reading_ = average_power_; |
| 80 // Part of this is to pin-down the power to zero if it is insignificantly | 70 if (has_clipped_) { |
| 81 // small. | 71 clipped_reading_ = true; |
| 82 const float kInsignificantPower = 1.0e-10f; // -100 dBFS | 72 has_clipped_ = false; |
| 83 const float power = | |
| 84 (average_power_ < kInsignificantPower) ? 0.0f : average_power_; | |
| 85 if (power != last_reported_power_ || | |
| 86 clipped_since_last_notification_ != last_reported_clipped_) { | |
| 87 const float power_dbfs = | |
| 88 power > 0.0f ? 10.0f * log10f(power) : zero_power(); | |
| 89 // Try to post a task to run the callback with the dBFS result. The | |
| 90 // posting of the task is guaranteed to be non-blocking, and therefore | |
| 91 // could fail. However, in the common case, failures should be rare (and | |
| 92 // then the task-post will likely succeed the next time it's attempted). | |
| 93 if (!message_loop_->TryPostTask( | |
| 94 FROM_HERE, | |
| 95 base::Bind(power_level_callback_, | |
| 96 power_dbfs, clipped_since_last_notification_))) { | |
| 97 DVLOG(2) << "TryPostTask() did not succeed."; | |
| 98 return; | |
| 99 } | |
| 100 last_reported_power_ = power; | |
| 101 last_reported_clipped_ = clipped_since_last_notification_; | |
| 102 } | 73 } |
| 103 clipped_since_last_notification_ = false; | 74 reading_lock_.Release(); |
| 104 frames_since_last_notification_ = 0; | |
| 105 } | 75 } |
| 106 } | 76 } |
| 107 | 77 |
| 78 std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() { | |
| 79 base::AutoLock for_reading(reading_lock_); | |
| 80 | |
| 81 // Convert power level to dBFS units, and pin it down to zero if it is | |
| 82 // insignificantly small. | |
| 83 const float kInsignificantPower = 1.0e-10f; // -100 dBFS | |
| 84 const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() : | |
| 85 10.0f * log10f(power_reading_); | |
| 86 | |
| 87 const bool clipped = clipped_reading_; | |
| 88 if (clipped) | |
| 89 clipped_reading_ = false; | |
|
DaleCurtis
2013/08/12 20:43:56
No need for if. If you're so inclined:
bool clip
miu
2013/08/12 21:10:54
Done.
| |
| 90 | |
| 91 return std::make_pair(power_dbfs, clipped); | |
| 92 } | |
| 93 | |
| 108 } // namespace media | 94 } // namespace media |
| OLD | NEW |