| 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 #include <utility> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "media/audio/audio_parameters.h" |
| 13 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
| 14 | 15 |
| 15 // An audio signal power monitor. It is periodically provided an AudioBus by | 16 // An audio signal power monitor. It is periodically provided an AudioBus by |
| 16 // the native audio thread, and the audio samples in each channel are analyzed | 17 // the native audio thread, and the audio samples in each channel are analyzed |
| 17 // to determine the average power of the signal over a time period. Here | 18 // to determine the average power of the signal over a time period. Here |
| 18 // "average power" is a running average calculated by using a first-order | 19 // "average power" is a running average calculated by using a first-order |
| 19 // low-pass filter over the square of the samples scanned. Whenever reporting | 20 // low-pass filter over the square of the samples scanned. Whenever reporting |
| 20 // the power level, this running average is converted to dBFS (decibels relative | 21 // the power level, this running average is converted to dBFS (decibels relative |
| 21 // to full-scale) units. | 22 // to full-scale) units. |
| 22 // | 23 // |
| (...skipping 10 matching lines...) Expand all Loading... |
| 33 | 34 |
| 34 class AudioBus; | 35 class AudioBus; |
| 35 | 36 |
| 36 class MEDIA_EXPORT AudioPowerMonitor { | 37 class MEDIA_EXPORT AudioPowerMonitor { |
| 37 public: | 38 public: |
| 38 // |sample_rate| is the audio signal sample rate (Hz). |time_constant| | 39 // |sample_rate| is the audio signal sample rate (Hz). |time_constant| |
| 39 // characterizes how samples are averaged over time to determine the power | 40 // characterizes how samples are averaged over time to determine the power |
| 40 // level; and is the amount of time it takes a zero power level to increase to | 41 // level; and is the amount of time it takes a zero power level to increase to |
| 41 // ~63.2% of maximum given a step input signal. | 42 // ~63.2% of maximum given a step input signal. |
| 42 AudioPowerMonitor(int sample_rate, const base::TimeDelta& time_constant); | 43 AudioPowerMonitor(int sample_rate, const base::TimeDelta& time_constant); |
| 44 AudioPowerMonitor(const AudioParameters& params); |
| 43 | 45 |
| 44 ~AudioPowerMonitor(); | 46 ~AudioPowerMonitor(); |
| 45 | 47 |
| 46 // Reset power monitor to initial state (zero power level). This should not | 48 // Reset power monitor to initial state (zero power level). This should not |
| 47 // be called while another thread is scanning. | 49 // be called while another thread is scanning. |
| 48 void Reset(); | 50 void Reset(); |
| 49 | 51 |
| 50 // Scan more |frames| of audio data from |buffer|. It is safe to call this | 52 // Scan more |frames| of audio data from |buffer|. It is safe to call this |
| 51 // from a real-time priority thread. | 53 // from a real-time priority thread. |
| 52 void Scan(const AudioBus& buffer, int frames); | 54 void Scan(const AudioBus& buffer, int frames); |
| 55 void Scan(const void* data, uint32 size); |
| 53 | 56 |
| 54 // Returns the current power level in dBFS and clip status. Clip status is | 57 // Returns the current power level in dBFS and clip status. Clip status is |
| 55 // true whenever any *one* sample scanned exceeded maximum amplitude since | 58 // 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 | 59 // this method's last invocation. It is safe to call this method from any |
| 57 // thread. | 60 // thread. |
| 58 std::pair<float, bool> ReadCurrentPowerAndClip(); | 61 std::pair<float, bool> ReadCurrentPowerAndClip(); |
| 59 | 62 |
| 60 // dBFS value corresponding to zero power in the audio signal. | 63 // dBFS value corresponding to zero power in the audio signal. |
| 61 static float zero_power() { return -std::numeric_limits<float>::infinity(); } | 64 static float zero_power() { return -std::numeric_limits<float>::infinity(); } |
| 62 | 65 |
| 63 // dBFS value corresponding to maximum power in the audio signal. | 66 // dBFS value corresponding to maximum power in the audio signal. |
| 64 static float max_power() { return 0.0f; } | 67 static float max_power() { return 0.0f; } |
| 65 | 68 |
| 66 private: | 69 private: |
| 67 // The weight applied when averaging-in each sample. Computed from the | 70 // The weight applied when averaging-in each sample. Computed from the |
| 68 // |sample_rate| and |time_constant|. | 71 // |sample_rate| and |time_constant|. |
| 69 const float sample_weight_; | 72 const float sample_weight_; |
| 70 | 73 |
| 71 // Accumulated results over one or more calls to Scan(). These should only be | 74 // Accumulated results over one or more calls to Scan(). These should only be |
| 72 // touched by the thread invoking Scan(). | 75 // touched by the thread invoking Scan(). |
| 73 float average_power_; | 76 float average_power_; |
| 74 bool has_clipped_; | 77 bool has_clipped_; |
| 75 | 78 |
| 76 // Copies of power and clip status, used to deliver results synchronously | 79 // Copies of power and clip status, used to deliver results synchronously |
| 77 // across threads. | 80 // across threads. |
| 78 base::Lock reading_lock_; | 81 base::Lock reading_lock_; |
| 79 float power_reading_; | 82 float power_reading_; |
| 80 bool clipped_reading_; | 83 bool clipped_reading_; |
| 81 | 84 |
| 85 scoped_ptr<AudioBus> audio_bus_; |
| 86 AudioParameters params_; |
| 87 |
| 82 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitor); | 88 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitor); |
| 83 }; | 89 }; |
| 84 | 90 |
| 85 } // namespace media | 91 } // namespace media |
| 86 | 92 |
| 87 #endif // MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ | 93 #endif // MEDIA_AUDIO_AUDIO_POWER_MONITOR_H_ |
| OLD | NEW |