Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: media/audio/audio_power_monitor.cc

Issue 14600025: Replace AudioSilenceDetector with an AudioPowerMonitor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Replace RMS scheme with 1st-order low-pass filter, per crogers@. Simpler, single-threaded unit tes… Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "media/audio/audio_power_monitor.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
10
11 #include "base/bind.h"
12 #include "base/float_util.h"
13 #include "base/logging.h"
14 #include "base/message_loop.h"
15 #include "base/time/time.h"
16 #include "media/base/audio_bus.h"
17
18 namespace media {
19
20 // dBFS reference level is 1.0f, the maximum possible value of a sample before
21 // clipping.
22 static const float kReferenceLevel = 1.0f;
DaleCurtis 2013/07/02 22:26:34 enum instead?
miu 2013/07/09 00:59:56 Removed it. I originally had this to make the cal
23
24 const float AudioPowerMonitor::kZeroPowerDBFS =
DaleCurtis 2013/07/02 22:26:34 Hmm, I'm think this is a static initializer.
miu 2013/07/09 00:59:56 Replaced these float constants with inline static
25 -std::numeric_limits<float>::infinity();
26
27 const float AudioPowerMonitor::kMaxPowerDBFS = 0.0f;
DaleCurtis 2013/07/02 22:26:34 enum?
miu 2013/07/09 00:59:56 See above comment. However, I'd be a little wary
28
29 AudioPowerMonitor::AudioPowerMonitor(
30 int sample_rate,
31 const base::TimeDelta& time_constant,
32 const base::TimeDelta& measurement_period,
33 base::MessageLoop* message_loop,
34 const PowerMeasurementCallback& callback)
35 : sample_weight_(
36 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))),
37 num_frames_per_callback_(sample_rate * measurement_period.InSecondsF()),
38 message_loop_(message_loop),
39 notify_power_level_(callback),
40 average_power_(0.0f),
41 clipped_since_last_notification_(false),
42 frames_since_last_notification_(0),
43 last_reported_power_(-1.0f),
44 last_reported_clipped_(false) {
45 DCHECK(message_loop_);
46 DCHECK(!notify_power_level_.is_null());
47 }
48
49 AudioPowerMonitor::~AudioPowerMonitor() {
50 }
51
52 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) {
53 DCHECK_LE(num_frames, buffer.frames());
54 const int num_channels = buffer.channels();
55 if (num_frames <= 0 || num_channels <= 0)
56 return;
57
58 // Calculate a new average power by applying a first-order low-pass filter
59 // over the audio samples in |buffer|.
60 //
61 // TODO(miu): Implement optimized SSE/NEON to more efficiently compute the
62 // results (in media/base/vector_math) in soon-upcoming change.
63 bool clipped = false;
64 float sum_power = 0.0f;
65 for (int i = 0; i < num_channels; ++i) {
66 float average_power_this_channel = average_power_;
67 const float* p = buffer.channel(i);
68 const float* const end_of_samples = p + num_frames;
69 for (; p < end_of_samples; ++p) {
70 const float sample_squared = (*p) * (*p);
71 clipped |= (sample_squared > 1.0f);
72 average_power_this_channel +=
73 (sample_squared - average_power_this_channel) * sample_weight_;
74 }
75 // If data in audio buffer is garbage, ignore its effect on the result.
76 if (base::IsNaN(average_power_this_channel))
77 average_power_this_channel = average_power_;
78 sum_power += average_power_this_channel;
79 }
80
81 // Update accumulated results.
82 average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels));
83 clipped_since_last_notification_ |= clipped;
84 frames_since_last_notification_ += num_frames;
85
86 // Once enough frames have been scanned, report the accumulated results.
87 if (frames_since_last_notification_ >= num_frames_per_callback_) {
88 // Note: Forgo making redundant callbacks when results remain unchanged.
89 // Part of this is to pin-down the power to zero if it is insignificantly
90 // small.
91 const float power = (average_power_ < 1.0e-10f) ? 0.0f : average_power_;
DaleCurtis 2013/07/02 22:26:34 Should probably be a top level const.
miu 2013/07/09 00:59:56 Good catch. I meant to break that out. I kept it
92 if (power != last_reported_power_ ||
93 clipped_since_last_notification_ != last_reported_clipped_) {
94 const float power_dBFS =
95 10.0f * log10f(power / (kReferenceLevel * kReferenceLevel));
96 // Try to post a task to run the callback with the dBFS result. The
97 // posting of the task is guaranteed to be non-blocking, and therefore
98 // could fail. However, in the common case, failures should be rare (and
99 // then the task-post will likely succeed the next time it's attempted).
100 if (!message_loop_->TryPostTask(
101 FROM_HERE,
102 base::Bind(notify_power_level_,
103 power_dBFS, clipped_since_last_notification_))) {
104 DVLOG(2) << "TryPostTask() did not succeed.";
105 return;
106 }
107 last_reported_power_ = power;
108 last_reported_clipped_ = clipped_since_last_notification_;
109 }
110 clipped_since_last_notification_ = false;
111 frames_since_last_notification_ = 0;
112 }
113 }
114
115 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698