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; | |
| 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 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 // The overall goal here is to compute the RMS of the amplitudes over all | |
| 49 // channels. An accumulation of sum-of-squares is maintained over one or more | |
| 50 // calls to this method. Then, once a sufficient number of frames have been | |
| 51 // scanned, the accumulated values are converted into dBFS units and a | |
| 52 // callback is attempted. | |
| 53 | |
| 54 // Compute the sum of squared amplitudes for the audio signal in |buffer|. | |
| 55 // | |
| 56 // TODO(miu): Implement optimized SSE/NEON to efficiently calculate | |
| 57 // sum-of-squares (or dot product, for more general-purpose use?) in | |
| 58 // media/base/vector_math. | |
| 59 float sum_of_squares = 0.0f; | |
| 60 for (int i = 0; i < num_channels; ++i) { | |
| 61 const float* p = buffer.channel(i); | |
| 62 const float* const end_of_samples = p + num_frames; | |
| 63 for (; p < end_of_samples; ++p) | |
| 64 sum_of_squares += (*p) * (*p); | |
| 65 } | |
|
Chris Rogers
2013/05/16 22:39:02
Instead of computing RMS power here in this inner
miu
2013/07/02 06:10:16
Done.
| |
| 66 if (base::IsNaN(sum_of_squares)) { | |
| 67 sum_of_squares = 0.0f; | |
| 68 } else { | |
| 69 const float max_before_clipping = (1.0f * 1.0f) * num_frames * num_channels; | |
| 70 if (sum_of_squares > max_before_clipping) | |
| 71 sum_of_squares = max_before_clipping; | |
| 72 } | |
|
Chris Rogers
2013/05/16 22:39:02
For clipping, you need to check every single sampl
miu
2013/07/02 06:10:16
Good catch. Done.
| |
| 73 | |
| 74 // Accumulate with existing values. | |
| 75 sum_of_squares_so_far_ += sum_of_squares / num_channels; | |
| 76 frames_so_far_ += num_frames; | |
| 77 | |
| 78 // Once enough frames have been scanned, try to post a task to run the | |
| 79 // callback with the dBFS result. The posting of the task is guaranteed to be | |
| 80 // non-blocking, and therefore could fail. If that happens, the accumulated | |
| 81 // values will be retained. We expect this to be a good policy since | |
| 82 // TryPostTask() should rarely fail. Therefore, only rarely will the | |
| 83 // measurement period be stretched, and only rarely will it be stretched by | |
| 84 // more than one extra AudioBus' worth of frames. | |
| 85 if (frames_so_far_ >= num_frames_per_callback_) { | |
| 86 const float average_power = sum_of_squares_so_far_ / frames_so_far_; | |
| 87 const float measurement_in_dbfs = | |
| 88 10.0f * log10(average_power / (kReferenceLevel * kReferenceLevel)); | |
| 89 if (message_loop_->TryPostTask( | |
| 90 FROM_HERE, | |
| 91 base::Bind(notify_power_level_, measurement_in_dbfs))) { | |
| 92 ResetScanAccumulations(); | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void AudioPowerMonitor::ResetScanAccumulations() { | |
| 98 sum_of_squares_so_far_ = 0.0f; | |
| 99 frames_so_far_ = 0; | |
| 100 } | |
| 101 | |
| 102 } // namespace media | |
| OLD | NEW |