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