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

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

Issue 178223013: Calculate the signal level on the media stream local audio track (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/renderer/media/media_stream_audio_level_calculator.h"
6
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9
10 namespace content {
11
12 namespace {
13
14 int MaxAmplitude(const int16* audio_data, int length) {
15 int max = 0, absolute = 0;
16 for (int i = 0; i < length; ++i) {
17 absolute = std::abs(audio_data[i]);
18 if (absolute > max)
19 max = absolute;
20 }
21 DCHECK(max <= std::numeric_limits<int16>::max());
22 return max;
23 }
24
25 } // namespace
26
27 MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator()
28 : counter_(0),
29 max_amplitude_(0),
30 level_(0) {
31 }
32
33 MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() {
34 }
35
36 int MediaStreamAudioLevelCalculator::Calculate(const int16* audio_data,
37 int number_of_channels,
38 int number_of_frames) {
39 DCHECK(CalledOnValidThread());
40 // Permutation of bars that reprents the amplitude level of the audio signal.
41 // The number of elements is 33 because we are indexing them in the range of
42 // [0, 32].
43 static const int kPermutation[33] =
44 {0,1,2,3,4,4,5,5,5,5,6,6,6,6,6,7,7,7,7,8,8,8,9,9,9,9,9,9,9,9,9,9,9};
45
46 // |level_| is updated every 10 callbacks. For the case where callback comes
47 // every 10ms, |level_| will be updated approximately every 100ms.
48 static const int kUpdateFrequency = 10;
49
50 int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames);
51 max_amplitude_ = std::max(max_amplitude_, max);
52 //std::min(max, std::numeric_limits<int16>::max()));
53
54 if (counter_++ == kUpdateFrequency) {
55 // Divide k16BitsMaxValue (32767) by 1000 to get in the range of [0,32]
56 // which is the range of the permutation array.
57 int index = static_cast<int>(max_amplitude_ / 1000);
58
59 // Make it less likely that the bar stays at position 0. I.e. only if
60 // its in the range 0-250 (instead of 0-1000)
61 if (index == 0 && max_amplitude_ > 250)
62 index = 1;
63
64 // |level_| will be the value in the permutation array that the |index| is
65 // pointing to.
66 level_ = kPermutation[index];
67
68 // Decay the absolute maximum amplitude by 1/4.
69 max_amplitude_ >>= 2;
70
71 // Reset the counter.
72 counter_ = 0;
73 }
74
75 return level_;
76 }
77
78 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698