OLD | NEW |
---|---|
(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 static const int kPermutation[33] = | |
tommi (sloooow) - chröme
2014/02/27 08:39:43
Add documentation. Also document why the size is 3
no longer working on chromium
2014/02/28 08:16:25
Done.
| |
15 {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}; | |
16 static const int kUpdateFrequency = 10; | |
tommi (sloooow) - chröme
2014/02/27 08:39:43
documentation. Also, since this is only used in o
no longer working on chromium
2014/02/28 08:16:25
Done.
| |
17 static const int k16BitsMaxValue = 32767; | |
tommi (sloooow) - chröme
2014/02/27 08:39:43
do we need this constant?
Could we instead just u
no longer working on chromium
2014/02/28 08:16:25
I used it because that is how webrtc code was writ
no longer working on chromium
2014/03/01 11:34:28
AudioBus::ToInterleave guarantees the value in the
| |
18 | |
19 int MaxAmplitude(const int16* audio_data, int length) { | |
20 int max = 0, absolute = 0; | |
21 for (int i = 0; i < length; ++i) { | |
22 absolute = std::abs(audio_data[i]); | |
23 if (absolute > max) | |
24 max = absolute; | |
25 } | |
26 | |
27 return max; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator() | |
33 : counter_(0), | |
34 max_amplitude_(0), | |
35 level_(0) { | |
36 } | |
37 | |
38 MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() { | |
39 } | |
40 | |
41 int MediaStreamAudioLevelCalculator::Calculate(const int16* audio_data, | |
42 int number_of_channels, | |
43 int number_of_frames) { | |
44 DCHECK(CalledOnValidThread()); | |
tommi (sloooow) - chröme
2014/02/27 08:39:43
nice
| |
45 int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames); | |
46 max_amplitude_ = std::max(max_amplitude_, std::min(max, k16BitsMaxValue)); | |
47 | |
48 if (counter_++ == kUpdateFrequency) { | |
49 // Divide k16BitsMaxValue (32767) by 1000 to get in the range of [0,32] | |
50 // which is the range of the permutation array. | |
51 int index = static_cast<int>(max_amplitude_ / 1000); | |
52 | |
53 // Make it less likely that the bar stays at position 0. I.e. only if | |
54 // its in the range 0-250 (instead of 0-1000) | |
55 if (index == 0 && max_amplitude_ > 250) | |
56 index = 1; | |
57 | |
58 // |level_| will be the value in the permutation array that the |index| is | |
59 // pointing to. | |
60 level_ = kPermutation[index]; | |
61 | |
62 // Decay the absolute maximum amplitude by 1/4. | |
63 max_amplitude_ >>= 2; | |
64 | |
65 // Reset the counter. | |
66 counter_ = 0; | |
67 } | |
68 | |
69 return level_; | |
70 } | |
71 | |
72 } // namespace content | |
OLD | NEW |