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

Side by Side Diff: content/renderer/media/media_stream_audio_level_calculator.cc

Issue 1780653002: Revert of MediaStream audio object graph untangling and clean-ups. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/renderer/media/media_stream_audio_level_calculator.h" 5 #include "content/renderer/media/media_stream_audio_level_calculator.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <limits>
9 8
10 #include "base/logging.h" 9 #include "base/logging.h"
11 #include "base/stl_util.h" 10 #include "base/stl_util.h"
12 #include "media/base/audio_bus.h" 11 #include "media/base/audio_bus.h"
13 12
14 namespace content { 13 namespace content {
15 14
16 namespace { 15 namespace {
17 16
18 // Calculates the maximum absolute amplitude of the audio data. 17 // Calculates the maximum absolute amplitude of the audio data.
19 float MaxAmplitude(const float* audio_data, int length) { 18 float MaxAmplitude(const float* audio_data, int length) {
20 float max = 0.0f; 19 float max = 0.0f;
21 for (int i = 0; i < length; ++i) { 20 for (int i = 0; i < length; ++i) {
22 const float absolute = fabsf(audio_data[i]); 21 const float absolute = fabsf(audio_data[i]);
23 if (absolute > max) 22 if (absolute > max)
24 max = absolute; 23 max = absolute;
25 } 24 }
26 DCHECK(std::isfinite(max)); 25 DCHECK(std::isfinite(max));
27 return max; 26 return max;
28 } 27 }
29 28
30 } // namespace 29 } // namespace
31 30
32 MediaStreamAudioLevelCalculator::Level::Level() : level_(0.0f) {} 31 MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator()
33 32 : counter_(0),
34 MediaStreamAudioLevelCalculator::Level::~Level() {} 33 max_amplitude_(0.0f),
35 34 level_(0.0f) {
36 float MediaStreamAudioLevelCalculator::Level::GetCurrent() const {
37 base::AutoLock auto_lock(lock_);
38 return level_;
39 } 35 }
40 36
41 void MediaStreamAudioLevelCalculator::Level::Set(float level) { 37 MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() {
42 base::AutoLock auto_lock(lock_);
43 level_ = level;
44 } 38 }
45 39
46 MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator() 40 float MediaStreamAudioLevelCalculator::Calculate(
47 : counter_(0), max_amplitude_(0.0f), level_(new Level()) {} 41 const media::AudioBus& audio_bus) {
48 42 DCHECK(thread_checker_.CalledOnValidThread());
49 MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() {
50 level_->Set(0.0f);
51 }
52
53 void MediaStreamAudioLevelCalculator::Calculate(
54 const media::AudioBus& audio_bus,
55 bool assume_nonzero_energy) {
56 // |level_| is updated every 10 callbacks. For the case where callback comes 43 // |level_| is updated every 10 callbacks. For the case where callback comes
57 // every 10ms, |level_| will be updated approximately every 100ms. 44 // every 10ms, |level_| will be updated approximately every 100ms.
58 static const int kUpdateFrequency = 10; 45 static const int kUpdateFrequency = 10;
59 46
60 float max = 47 float max = 0.0f;
61 assume_nonzero_energy ? 1.0f / std::numeric_limits<int16_t>::max() : 0.0f;
62 for (int i = 0; i < audio_bus.channels(); ++i) { 48 for (int i = 0; i < audio_bus.channels(); ++i) {
63 const float max_this_channel = 49 const float max_this_channel =
64 MaxAmplitude(audio_bus.channel(i), audio_bus.frames()); 50 MaxAmplitude(audio_bus.channel(i), audio_bus.frames());
65 if (max_this_channel > max) 51 if (max_this_channel > max)
66 max = max_this_channel; 52 max = max_this_channel;
67 } 53 }
68 max_amplitude_ = std::max(max_amplitude_, max); 54 max_amplitude_ = std::max(max_amplitude_, max);
69 55
70 if (counter_++ == kUpdateFrequency) { 56 if (counter_++ == kUpdateFrequency) {
71 // Clip the exposed signal level to make sure it is in the range [0.0,1.0]. 57 level_ = max_amplitude_;
72 level_->Set(std::min(1.0f, max_amplitude_));
73 58
74 // Decay the absolute maximum amplitude by 1/4. 59 // Decay the absolute maximum amplitude by 1/4.
75 max_amplitude_ /= 4.0f; 60 max_amplitude_ /= 4.0f;
76 61
77 // Reset the counter. 62 // Reset the counter.
78 counter_ = 0; 63 counter_ = 0;
79 } 64 }
65
66 return level_;
80 } 67 }
81 68
82 } // namespace content 69 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/media_stream_audio_level_calculator.h ('k') | content/renderer/media/media_stream_audio_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698