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; |
| 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; | |
54 float sum_power = 0.0f; | 43 float sum_power = 0.0f; |
55 for (int i = 0; i < num_channels; ++i) { | 44 for (int i = 0; i < num_channels; ++i) { |
56 float average_power_this_channel = average_power_; | 45 float average_power_this_channel = average_power_; |
| 46 bool clipped = false; |
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_; |
| 59 clipped = false; |
| 60 } |
69 sum_power += average_power_this_channel; | 61 sum_power += average_power_this_channel; |
| 62 has_clipped_ |= clipped; |
70 } | 63 } |
71 | 64 |
72 // Update accumulated results. | 65 // Update accumulated results, with clamping for sanity. |
73 average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels)); | 66 average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels)); |
74 clipped_since_last_notification_ |= clipped; | |
75 frames_since_last_notification_ += num_frames; | |
76 | 67 |
77 // Once enough frames have been scanned, report the accumulated results. | 68 // Push results for reading by other threads, non-blocking. |
78 if (frames_since_last_notification_ >= num_frames_per_callback_) { | 69 if (reading_lock_.Try()) { |
79 // Note: Forgo making redundant callbacks when results remain unchanged. | 70 power_reading_ = average_power_; |
80 // Part of this is to pin-down the power to zero if it is insignificantly | 71 if (has_clipped_) { |
81 // small. | 72 clipped_reading_ = true; |
82 const float kInsignificantPower = 1.0e-10f; // -100 dBFS | 73 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 } | 74 } |
103 clipped_since_last_notification_ = false; | 75 reading_lock_.Release(); |
104 frames_since_last_notification_ = 0; | |
105 } | 76 } |
106 } | 77 } |
107 | 78 |
| 79 std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() { |
| 80 base::AutoLock for_reading(reading_lock_); |
| 81 |
| 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_); |
| 87 |
| 88 const bool clipped = clipped_reading_; |
| 89 clipped_reading_ = false; |
| 90 |
| 91 return std::make_pair(power_dbfs, clipped); |
| 92 } |
| 93 |
108 } // namespace media | 94 } // namespace media |
OLD | NEW |