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 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 // The range of int16 is [-32768, 32767], verify the |max| should be smaller | |
22 // than 32768. | |
23 DCHECK(max <= std::abd(std::numeric_limits<int16>::min())); | |
tommi (sloooow) - chröme
2014/03/01 11:49:33
a few questions:
* does this compile? (abd?)
* Th
no longer working on chromium
2014/03/01 12:37:00
No, it should be abs.
tommi (sloooow) - chröme
2014/03/02 10:19:53
OK, now I get it. Can you add documentation to Ma
no longer working on chromium
2014/03/03 13:07:12
Done with adding a comment.
| |
24 | |
25 return max; | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator() | |
31 : counter_(0), | |
32 max_amplitude_(0), | |
33 level_(0) { | |
34 } | |
35 | |
36 MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() { | |
37 } | |
38 | |
39 int MediaStreamAudioLevelCalculator::Calculate(const int16* audio_data, | |
40 int number_of_channels, | |
41 int number_of_frames) { | |
42 DCHECK(thread_checker_.CalledOnValidThread()); | |
43 // Permutation of bars that reprents the amplitude level of the audio signal. | |
44 // The number of elements is 33 because we are indexing them in the range of | |
45 // [0, 32]. | |
46 static const int kPermutation[33] = | |
47 {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}; | |
48 | |
49 // |level_| is updated every 10 callbacks. For the case where callback comes | |
50 // every 10ms, |level_| will be updated approximately every 100ms. | |
51 static const int kUpdateFrequency = 10; | |
52 | |
53 int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames); | |
54 max_amplitude_ = std::max(max_amplitude_, max); | |
55 | |
56 if (counter_++ == kUpdateFrequency) { | |
57 // Divide the max amplitude (32768) by 1000 to get in the range of [0,32] | |
tommi (sloooow) - chröme
2014/03/01 11:49:33
it can't be 32768. that's higher than int16 can c
no longer working on chromium
2014/03/01 12:37:00
The same explaination, absolute value of <int16>::
| |
58 // which is the range of the permutation array. | |
59 int index = static_cast<int>(max_amplitude_ / 1000); | |
60 | |
61 // Make it less likely that the bar stays at position 0. I.e. only if | |
62 // its in the range 0-250 (instead of 0-1000) | |
63 if (index == 0 && max_amplitude_ > 250) | |
64 index = 1; | |
65 | |
66 // |level_| will be the value in the permutation array that the |index| is | |
67 // pointing to. | |
68 level_ = kPermutation[index]; | |
69 | |
70 // Decay the absolute maximum amplitude by 1/4. | |
71 max_amplitude_ >>= 2; | |
72 | |
73 // Reset the counter. | |
74 counter_ = 0; | |
75 } | |
76 | |
77 return level_; | |
78 } | |
79 | |
80 } // namespace content | |
OLD | NEW |