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