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