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

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

Issue 287873004: Adds volume level measurements to the AudioInputController for low-latency clients (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: refactored Created 6 years, 7 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/audio/audio_power_monitor.h" 5 #include "media/audio/audio_power_monitor.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/float_util.h" 10 #include "base/float_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "media/base/audio_bus.h" 13 #include "media/base/audio_bus.h"
14 #include "media/base/vector_math.h" 14 #include "media/base/vector_math.h"
15 15
16 namespace media { 16 namespace media {
17 17
18 static const float kTimeConstantInSec = 0.01f;
19
18 AudioPowerMonitor::AudioPowerMonitor( 20 AudioPowerMonitor::AudioPowerMonitor(
19 int sample_rate, const base::TimeDelta& time_constant) 21 int sample_rate, const base::TimeDelta& time_constant)
20 : sample_weight_( 22 : sample_weight_(
21 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) { 23 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) {
22 Reset(); 24 Reset();
23 } 25 }
24 26
27 AudioPowerMonitor::AudioPowerMonitor(const AudioParameters& params)
28 : sample_weight_(
29 1.0f - expf(-1.0f / (params.sample_rate() * kTimeConstantInSec))),
no longer working on chromium 2014/05/19 15:15:48 indentation.
30 audio_bus_(AudioBus::Create(params)),
31 params_(params) {
32 }
33
25 AudioPowerMonitor::~AudioPowerMonitor() { 34 AudioPowerMonitor::~AudioPowerMonitor() {
26 } 35 }
27 36
28 void AudioPowerMonitor::Reset() { 37 void AudioPowerMonitor::Reset() {
29 power_reading_ = average_power_ = 0.0f; 38 power_reading_ = average_power_ = 0.0f;
30 clipped_reading_ = has_clipped_ = false; 39 clipped_reading_ = has_clipped_ = false;
31 } 40 }
32 41
33 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) { 42 void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) {
34 DCHECK_LE(num_frames, buffer.frames()); 43 DCHECK_LE(num_frames, buffer.frames());
(...skipping 24 matching lines...) Expand all
59 if (reading_lock_.Try()) { 68 if (reading_lock_.Try()) {
60 power_reading_ = average_power_; 69 power_reading_ = average_power_;
61 if (has_clipped_) { 70 if (has_clipped_) {
62 clipped_reading_ = true; 71 clipped_reading_ = true;
63 has_clipped_ = false; 72 has_clipped_ = false;
64 } 73 }
65 reading_lock_.Release(); 74 reading_lock_.Release();
66 } 75 }
67 } 76 }
68 77
78 void AudioPowerMonitor::Scan(const void* data, uint32 size) {
no longer working on chromium 2014/05/19 15:15:48 Ideally we should not change this audio_power_moni
henrika (OOO until Aug 14) 2014/05/19 15:25:46 It is a rather big change to do that for all platf
79 DCHECK_EQ(static_cast<int>(size), params_.GetBytesPerBuffer());
80 audio_bus_->FromInterleaved(
81 data, audio_bus_->frames(), params_.bits_per_sample() / 8);
82 Scan(*audio_bus_, audio_bus_->frames());
83 }
84
69 std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() { 85 std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() {
70 base::AutoLock for_reading(reading_lock_); 86 base::AutoLock for_reading(reading_lock_);
71 87
72 // Convert power level to dBFS units, and pin it down to zero if it is 88 // Convert power level to dBFS units, and pin it down to zero if it is
73 // insignificantly small. 89 // insignificantly small.
74 const float kInsignificantPower = 1.0e-10f; // -100 dBFS 90 const float kInsignificantPower = 1.0e-10f; // -100 dBFS
75 const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() : 91 const float power_dbfs = power_reading_ < kInsignificantPower ? zero_power() :
76 10.0f * log10f(power_reading_); 92 10.0f * log10f(power_reading_);
77 93
78 const bool clipped = clipped_reading_; 94 const bool clipped = clipped_reading_;
79 clipped_reading_ = false; 95 clipped_reading_ = false;
80 96
81 return std::make_pair(power_dbfs, clipped); 97 return std::make_pair(power_dbfs, clipped);
82 } 98 }
83 99
84 } // namespace media 100 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698