webapps: fix theme color on status bar for Galaxy devices
[chromium-blink-merge.git] / media / audio / audio_power_monitor.cc
blobefbad6c5006ec4647e10e31d4db6b3297c66b0f9
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
3 // found in the LICENSE file.
5 #include "media/audio/audio_power_monitor.h"
7 #include <algorithm>
8 #include <cmath>
10 #include "base/logging.h"
11 #include "base/time/time.h"
12 #include "media/base/audio_bus.h"
13 #include "media/base/vector_math.h"
15 namespace media {
17 AudioPowerMonitor::AudioPowerMonitor(
18 int sample_rate, const base::TimeDelta& time_constant)
19 : sample_weight_(
20 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) {
21 Reset();
24 AudioPowerMonitor::~AudioPowerMonitor() {
27 void AudioPowerMonitor::Reset() {
28 // These are only read/written by Scan(), but Scan() should not be running
29 // when Reset() is called.
30 average_power_ = 0.0f;
31 has_clipped_ = false;
33 // These are the copies read by ReadCurrentPowerAndClip(). The lock here is
34 // not necessary, as racey writes/reads are acceptable, but this prevents
35 // quality-enhancement tools like TSAN from complaining.
36 base::AutoLock for_reset(reading_lock_);
37 power_reading_ = 0.0f;
38 clipped_reading_ = false;
41 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) {
42 DCHECK_LE(num_frames, buffer.frames());
43 const int num_channels = buffer.channels();
44 if (num_frames <= 0 || num_channels <= 0)
45 return;
47 // Calculate a new average power by applying a first-order low-pass filter
48 // (a.k.a. an exponentially-weighted moving average) over the audio samples in
49 // each channel in |buffer|.
50 float sum_power = 0.0f;
51 for (int i = 0; i < num_channels; ++i) {
52 const std::pair<float, float> ewma_and_max = vector_math::EWMAAndMaxPower(
53 average_power_, buffer.channel(i), num_frames, sample_weight_);
54 // If data in audio buffer is garbage, ignore its effect on the result.
55 if (!std::isfinite(ewma_and_max.first)) {
56 sum_power += average_power_;
57 } else {
58 sum_power += ewma_and_max.first;
59 has_clipped_ |= (ewma_and_max.second > 1.0f);
63 // Update accumulated results, with clamping for sanity.
64 average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels));
66 // Push results for reading by other threads, non-blocking.
67 if (reading_lock_.Try()) {
68 power_reading_ = average_power_;
69 if (has_clipped_) {
70 clipped_reading_ = true;
71 has_clipped_ = false;
73 reading_lock_.Release();
77 std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() {
78 base::AutoLock for_reading(reading_lock_);
80 // Convert power level to dBFS units, and pin it down to zero if it is
81 // insignificantly small.
82 const float kInsignificantPower = 1.0e-10f; // -100 dBFS
83 const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() :
84 10.0f * log10f(power_reading_);
86 const bool clipped = clipped_reading_;
87 clipped_reading_ = false;
89 return std::make_pair(power_dbfs, clipped);
92 } // namespace media