OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | |
6 #define MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | |
7 | |
8 #include <limits> | |
9 | |
10 #include "base/callback.h" | |
11 #include "media/base/media_export.h" | |
12 | |
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 | |
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 | |
17 // low-pass filter over the samples scanned. Whenever reporting the power | |
Chris Rogers
2013/07/18 00:09:36
"over the samples" -> "over the square of the samp
miu
2013/07/19 01:08:07
Done.
| |
18 // level, this running average is converted to dBFS (decibels relative to | |
19 // full-scale) units. | |
20 // | |
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 | |
23 // locks, nor engages in any operation that could result in an | |
24 // undetermined/unbounded amount of run-time. | |
25 | |
26 namespace base { | |
27 class MessageLoop; | |
28 class TimeDelta; | |
29 } | |
30 | |
31 namespace media { | |
32 | |
33 class AudioBus; | |
34 | |
35 class MEDIA_EXPORT AudioPowerMonitor { | |
36 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. | |
Chris Rogers
2013/07/18 00:09:36
I think to really do this well, the idea of "clipp
miu
2013/07/19 01:08:07
Agreed (with the UI details). I wanted to keep th
| |
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| | |
44 // 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 | |
46 // ~63.2% of maximum given a step input signal. |measurement_period| is the | |
47 // time length of signal to analyze before invoking the callback to report the | |
48 // current power level. |message_loop| is where the |notify_power_level| | |
49 // callback will be posted. | |
Chris Rogers
2013/07/18 00:09:36
Not that you have to change this all now, so I'll
miu
2013/07/19 01:08:07
I'll discuss this further with the UI side of the
| |
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& notify_power_level); | |
55 | |
56 ~AudioPowerMonitor(); | |
57 | |
58 // Scan more |frames| of audio data from |buffer|. It is safe to call this | |
59 // from a real-time priority thread. | |
60 void Scan(const AudioBus& buffer, int frames); | |
61 | |
62 // dBFS value corresponding to zero power in the audio signal. | |
63 static float zero_power() { return -std::numeric_limits<float>::infinity(); } | |
64 | |
65 // dBFS value corresponding to maximum power in the audio signal. | |
66 static float max_power() { return 0.0f; } | |
67 | |
Chris Rogers
2013/07/18 00:09:36
Just for completeness, you might consider adding a
miu
2013/07/19 01:08:07
The issue here would be around the threading. I a
| |
68 private: | |
69 // The weight applied when averaging-in each sample. Computed from the | |
70 // |sample_rate| and |time_constant|. | |
71 const float sample_weight_; | |
72 | |
73 // Number of audio frames to be scanned before reporting the current power | |
74 // level via callback, as computed from |sample_rate| and | |
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 notify_power_level_; | |
Chris Rogers
2013/07/18 00:09:36
maybe a better name would be "power_level_callback
miu
2013/07/19 01:08:07
Done.
| |
81 | |
82 // Accumulated results over one or more calls to Scan(). | |
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 | |
88 // notifications. | |
Chris Rogers
2013/07/18 00:09:36
nit: notifications can go on previous comment line
miu
2013/07/19 01:08:07
It's one character too long. :( I s/notification
| |
89 float last_reported_power_; | |
90 bool last_reported_clipped_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitor); | |
93 }; | |
94 | |
95 } // namespace media | |
96 | |
97 #endif // MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | |
OLD | NEW |