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 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "media/base/media_export.h" | 11 #include "media/base/media_export.h" |
12 | 12 |
13 // An audio signal power monitor. It is periodically provided an AudioBus by | 13 // 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 | 14 // 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 | 15 // 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 | 16 // "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 | 17 // 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 | 18 // the power level, this running average is converted to dBFS (decibels relative |
19 // to full-scale) units. | 19 // to full-scale) units. |
20 // | 20 // |
21 // Note that extreme care has been taken to make the AudioPowerMonitor::Scan() | 21 // 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 | 22 // 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 | 23 // locks, nor engages in any operation that could result in an |
24 // undetermined/unbounded amount of run-time. | 24 // undetermined/unbounded amount of run-time. |
25 | 25 |
26 namespace base { | 26 namespace base { |
27 class MessageLoop; | |
28 class TimeDelta; | 27 class TimeDelta; |
29 } | 28 } |
30 | 29 |
31 namespace media { | 30 namespace media { |
32 | 31 |
33 class AudioBus; | 32 class AudioBus; |
34 | 33 |
35 class MEDIA_EXPORT AudioPowerMonitor { | 34 class MEDIA_EXPORT AudioPowerMonitor { |
36 public: | 35 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| | 36 // |sample_rate| is the audio signal sample rate (Hz). |time_constant| |
44 // characterizes how samples are averaged over time to determine the power | 37 // 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 | 38 // 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 | 39 // ~63.2% of maximum given a step input signal. |
47 // time length of signal to analyze before invoking the callback to report the | 40 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 | 41 |
56 ~AudioPowerMonitor(); | 42 ~AudioPowerMonitor(); |
57 | 43 |
44 // Reset power monitor to initial state (zero power level). | |
45 void Reset(); | |
46 | |
58 // Scan more |frames| of audio data from |buffer|. It is safe to call this | 47 // Scan more |frames| of audio data from |buffer|. It is safe to call this |
59 // from a real-time priority thread. | 48 // from a real-time priority thread. |
60 void Scan(const AudioBus& buffer, int frames); | 49 void Scan(const AudioBus& buffer, int frames); |
61 | 50 |
51 // Returns the current power level in terms of dBFS (see zero_power() and | |
DaleCurtis
2013/08/12 18:23:29
I recommend you also read clipped and power togeth
miu
2013/08/12 19:41:46
Done.
| |
52 // max_power() below). It is safe to call this method from any thread. | |
53 float ReadCurrentPower() const; | |
54 | |
55 // Returns true if 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 bool TestForClippingAndClear(); | |
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(). Note: These are |
74 // level via callback, as computed from |sample_rate| and | 72 // read/written by multiple threads without locking, by design. |
75 // |measurement_period|. | |
76 const int num_frames_per_callback_; | |
77 | |
78 // MessageLoop and callback used to notify of the current power level. | |
79 base::MessageLoop* const message_loop_; | |
80 const PowerMeasurementCallback power_level_callback_; | |
81 | |
82 // Accumulated results over one or more calls to Scan(). | |
83 float average_power_; | 73 float average_power_; |
84 bool clipped_since_last_notification_; | 74 bool has_clipped_; |
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 | 75 |
91 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitor); | 76 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitor); |
92 }; | 77 }; |
93 | 78 |
94 } // namespace media | 79 } // namespace media |
95 | 80 |
96 #endif // MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | 81 #endif // MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ |
OLD | NEW |