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 square of the samples scanned. Whenever reporting |
| 18 // the power level, this running average is converted to dBFS (decibels relative |
| 19 // to 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. |
| 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 |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 |
| 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 |
| 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 power_level_callback_; |
| 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 callbacks. |
| 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 |