Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 <cmath> | |
| 8 #include <limits> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/float_util.h" | |
| 12 #include "base/message_loop.h" | |
| 13 #include "base/time.h" | |
| 14 #include "media/base/audio_bus.h" | |
| 15 | |
| 16 namespace media { | |
| 17 | |
| 18 // dBFS reference level is 1.0f (the maximum possible value of a sample). | |
| 19 static const float kReferenceLevel = 1.0f; | |
|
DaleCurtis
2013/05/16 18:24:45
AudioBus doesn't guarantee 1.0 is maximum. See au
miu
2013/05/16 21:44:11
This is the way dBFS units are defined, not a rest
| |
| 20 | |
| 21 const float AudioPowerMonitor::kZeroPowerDBFS = | |
| 22 -std::numeric_limits<float>::infinity(); | |
| 23 | |
| 24 const float AudioPowerMonitor::kMaxPowerDBFS = 0.0f; | |
| 25 | |
| 26 AudioPowerMonitor::AudioPowerMonitor( | |
| 27 int sample_rate, | |
| 28 const base::TimeDelta& measurement_period, | |
| 29 base::MessageLoop* message_loop, | |
| 30 const PowerMeasurementCallback& callback) | |
| 31 : num_frames_per_callback_(sample_rate * measurement_period.InSecondsF()), | |
| 32 message_loop_(message_loop), | |
| 33 notify_power_level_(callback) { | |
| 34 DCHECK(message_loop_); | |
| 35 DCHECK(!notify_power_level_.is_null()); | |
| 36 ResetScanAccumulations(); | |
| 37 } | |
| 38 | |
| 39 AudioPowerMonitor::~AudioPowerMonitor() { | |
| 40 } | |
| 41 | |
| 42 void AudioPowerMonitor::Scan(const AudioBus* buffer, int num_frames) { | |
| 43 if (!buffer) | |
|
DaleCurtis
2013/05/16 18:24:45
Is this ever true?
miu
2013/05/16 21:44:11
Good point. (Left over from AudioSilenceDetector
| |
| 44 return; | |
| 45 DCHECK_LE(num_frames, buffer->frames()); | |
| 46 const int num_channels = buffer->channels(); | |
| 47 if (num_frames <= 0 || num_channels <= 0) | |
| 48 return; | |
| 49 | |
| 50 // The overall goal here is to compute the RMS of the amplitudes over all | |
|
DaleCurtis
2013/05/16 18:24:45
You say RMS, but I see no sqrt() below :)
miu
2013/05/16 21:44:11
The subtlety: RMS is root-mean-square amplitude.
| |
| 51 // channels. An accumulation of sum-of-squares is maintained over one or more | |
| 52 // calls to this method. Then, once a sufficient number of frames have been | |
| 53 // scanned, the accumulated values are converted into dBFS units and a | |
| 54 // callback is attempted. | |
| 55 | |
| 56 // Compute the sum of squared amplitudes for the audio signal in |buffer|. | |
| 57 // | |
| 58 // TODO(miu): Implement optimized SSE/NEON to efficiently calculate | |
| 59 // sum-of-squares (or dot product, for more general-purpose use?) in | |
| 60 // media/base/vector_math. | |
| 61 float sum_of_squares = 0.0f; | |
| 62 for (int i = 0; i < num_channels; ++i) { | |
| 63 const float* p = buffer->channel(i); | |
| 64 const float* const end_of_samples = p + num_frames; | |
| 65 for (; p < end_of_samples; ++p) | |
| 66 sum_of_squares += (*p) * (*p); | |
| 67 } | |
| 68 DCHECK(base::IsFinite(sum_of_squares)); | |
| 69 | |
| 70 // Accumulate with existing values. | |
| 71 sum_of_squares_so_far_ += sum_of_squares / num_channels; | |
| 72 frames_so_far_ += num_frames; | |
| 73 | |
| 74 // Once enough frames have been scanned, try to post a task to run the | |
| 75 // callback with the dBFS result. The posting of the task is guaranteed to be | |
| 76 // non-blocking, and therefore could fail. If that happens, the accumulated | |
| 77 // values will be retained. | |
| 78 if (frames_so_far_ >= num_frames_per_callback_) { | |
|
DaleCurtis
2013/05/16 18:24:45
The way this is written you end up with a stretche
miu
2013/05/16 21:44:11
I put the answer to this in the form of comments i
| |
| 79 const float average_power = sum_of_squares_so_far_ / frames_so_far_; | |
| 80 const float measurement_in_dbfs = | |
| 81 10.0f * log10(average_power / (kReferenceLevel * kReferenceLevel)); | |
|
DaleCurtis
2013/05/16 18:24:45
10.0? http://en.wikipedia.org/wiki/DBFS says 20.
miu
2013/05/16 21:44:11
20 is for amplitudes. 10 is for power (i.e., ampl
| |
| 82 if (message_loop_->TryPostTask( | |
| 83 FROM_HERE, | |
| 84 base::Bind(notify_power_level_, measurement_in_dbfs))) { | |
| 85 ResetScanAccumulations(); | |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void AudioPowerMonitor::ResetScanAccumulations() { | |
| 91 sum_of_squares_so_far_ = 0.0f; | |
| 92 frames_so_far_ = 0; | |
| 93 } | |
| 94 | |
| 95 } // namespace media | |
| OLD | NEW |