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 #ifndef MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ |
6 #define MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | 6 #define MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
| 9 #include <utility> |
9 | 10 |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/synchronization/lock.h" |
11 #include "media/base/media_export.h" | 13 #include "media/base/media_export.h" |
12 | 14 |
13 // An audio signal power monitor. It is periodically provided an AudioBus by | 15 // An audio signal power monitor. It is periodically provided an AudioBus by |
14 // the native audio thread, and the audio samples in each channel are analyzed | 16 // the native audio thread, and the audio samples in each channel are analyzed |
15 // to determine the average power of the signal over a time period. Here | 17 // to determine the average power of the signal over a time period. Here |
16 // "average power" is a running average calculated by using a first-order | 18 // "average power" is a running average calculated by using a first-order |
17 // low-pass filter over the square of the samples scanned. Whenever reporting | 19 // low-pass filter over the square of the samples scanned. Whenever reporting |
18 // the power level, this running average is converted to dBFS (decibels relative | 20 // the power level, this running average is converted to dBFS (decibels relative |
19 // to full-scale) units. | 21 // to full-scale) units. |
20 // | 22 // |
21 // Note that extreme care has been taken to make the AudioPowerMonitor::Scan() | 23 // Note that extreme care has been taken to make the AudioPowerMonitor::Scan() |
22 // method safe to be called on the native audio thread. The code acquires no | 24 // method safe to be called on the native audio thread. The code acquires no |
23 // locks, nor engages in any operation that could result in an | 25 // locks, nor engages in any operation that could result in an |
24 // undetermined/unbounded amount of run-time. | 26 // undetermined/unbounded amount of run-time. |
25 | 27 |
26 namespace base { | 28 namespace base { |
27 class MessageLoop; | |
28 class TimeDelta; | 29 class TimeDelta; |
29 } | 30 } |
30 | 31 |
31 namespace media { | 32 namespace media { |
32 | 33 |
33 class AudioBus; | 34 class AudioBus; |
34 | 35 |
35 class MEDIA_EXPORT AudioPowerMonitor { | 36 class MEDIA_EXPORT AudioPowerMonitor { |
36 public: | 37 public: |
37 // Reports power level in terms of dBFS (see zero_power() and max_power() | |
38 // below). |clipped| is true if any *one* sample exceeded maximum amplitude | |
39 // since the last invocation. | |
40 typedef base::Callback<void(float power_dbfs, bool clipped)> | |
41 PowerMeasurementCallback; | |
42 | |
43 // |sample_rate| is the audio signal sample rate (Hz). |time_constant| | 38 // |sample_rate| is the audio signal sample rate (Hz). |time_constant| |
44 // characterizes how samples are averaged over time to determine the power | 39 // characterizes how samples are averaged over time to determine the power |
45 // level; and is the amount of time it takes a zero power level to increase to | 40 // level; and is the amount of time it takes a zero power level to increase to |
46 // ~63.2% of maximum given a step input signal. |measurement_period| is the | 41 // ~63.2% of maximum given a step input signal. |
47 // time length of signal to analyze before invoking the callback to report the | 42 AudioPowerMonitor(int sample_rate, const base::TimeDelta& time_constant); |
48 // current power level. |message_loop| is where the |callback| task will be | |
49 // posted. | |
50 AudioPowerMonitor(int sample_rate, | |
51 const base::TimeDelta& time_constant, | |
52 const base::TimeDelta& measurement_period, | |
53 base::MessageLoop* message_loop, | |
54 const PowerMeasurementCallback& callback); | |
55 | 43 |
56 ~AudioPowerMonitor(); | 44 ~AudioPowerMonitor(); |
57 | 45 |
| 46 // Reset power monitor to initial state (zero power level). This should not |
| 47 // be called while another thread is scanning. |
| 48 void Reset(); |
| 49 |
58 // Scan more |frames| of audio data from |buffer|. It is safe to call this | 50 // Scan more |frames| of audio data from |buffer|. It is safe to call this |
59 // from a real-time priority thread. | 51 // from a real-time priority thread. |
60 void Scan(const AudioBus& buffer, int frames); | 52 void Scan(const AudioBus& buffer, int frames); |
61 | 53 |
| 54 // Returns the current power level in dBFS and clip status. Clip status is |
| 55 // true whenever any *one* sample scanned exceeded maximum amplitude since |
| 56 // this method's last invocation. It is safe to call this method from any |
| 57 // thread. |
| 58 std::pair<float, bool> ReadCurrentPowerAndClip(); |
| 59 |
62 // dBFS value corresponding to zero power in the audio signal. | 60 // dBFS value corresponding to zero power in the audio signal. |
63 static float zero_power() { return -std::numeric_limits<float>::infinity(); } | 61 static float zero_power() { return -std::numeric_limits<float>::infinity(); } |
64 | 62 |
65 // dBFS value corresponding to maximum power in the audio signal. | 63 // dBFS value corresponding to maximum power in the audio signal. |
66 static float max_power() { return 0.0f; } | 64 static float max_power() { return 0.0f; } |
67 | 65 |
68 private: | 66 private: |
69 // The weight applied when averaging-in each sample. Computed from the | 67 // The weight applied when averaging-in each sample. Computed from the |
70 // |sample_rate| and |time_constant|. | 68 // |sample_rate| and |time_constant|. |
71 const float sample_weight_; | 69 const float sample_weight_; |
72 | 70 |
73 // Number of audio frames to be scanned before reporting the current power | 71 // Accumulated results over one or more calls to Scan(). These should only be |
74 // level via callback, as computed from |sample_rate| and | 72 // touched by the thread invoking Scan(). |
75 // |measurement_period|. | 73 float average_power_; |
76 const int num_frames_per_callback_; | 74 bool has_clipped_; |
77 | 75 |
78 // MessageLoop and callback used to notify of the current power level. | 76 // Copies of power and clip status, used to deliver results synchronously |
79 base::MessageLoop* const message_loop_; | 77 // across threads. |
80 const PowerMeasurementCallback power_level_callback_; | 78 base::Lock reading_lock_; |
81 | 79 float power_reading_; |
82 // Accumulated results over one or more calls to Scan(). | 80 bool clipped_reading_; |
83 float average_power_; | |
84 bool clipped_since_last_notification_; | |
85 int frames_since_last_notification_; | |
86 | |
87 // Keep track of last reported results to forgo making redundant callbacks. | |
88 float last_reported_power_; | |
89 bool last_reported_clipped_; | |
90 | 81 |
91 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitor); | 82 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitor); |
92 }; | 83 }; |
93 | 84 |
94 } // namespace media | 85 } // namespace media |
95 | 86 |
96 #endif // MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | 87 #endif // MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ |
OLD | NEW |