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

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: rebased and all the try jobs passed 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 // Calculates the maximum absolute amplitude of the audio data.
15 // Note, the return value can be bigger than std::numeric_limits<int16>::max().
16 int MaxAmplitude(const int16* audio_data, int length) {
17 int max = 0, absolute = 0;
18 for (int i = 0; i < length; ++i) {
19 absolute = std::abs(audio_data[i]);
20 if (absolute > max)
21 max = absolute;
22 }
23 // The range of int16 is [-32768, 32767], verify the |max| should not be
24 // bigger than 32768.
25 DCHECK(max <= std::abs(std::numeric_limits<int16>::min()));
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(thread_checker_.CalledOnValidThread());
45 // Permutation of bars that reprents the amplitude level of the audio signal.
ajm 2014/03/04 22:20:05 nit: represents
46 // The number of elements is 33 because we are indexing them in the range of
47 // [0, 32].
48 static const int kPermutation[33] =
49 {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};
50
51 // |level_| is updated every 10 callbacks. For the case where callback comes
52 // every 10ms, |level_| will be updated approximately every 100ms.
53 static const int kUpdateFrequency = 10;
54
55 int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames);
56 max_amplitude_ = std::max(max_amplitude_, max);
57
58 if (counter_++ == kUpdateFrequency) {
59 // Divide the max amplitude (32768) by 1000 to get in the range of [0,32]
60 // which is the range of the permutation array.
61 int index = static_cast<int>(max_amplitude_ / 1000);
62
63 // Make it less likely that the bar stays at position 0. I.e. only if
64 // its in the range 0-250 (instead of 0-1000)
ajm 2014/03/04 22:20:05 nit: "i.e." "it's"
65 if (index == 0 && max_amplitude_ > 250)
66 index = 1;
67
68 // |level_| will be the value in the permutation array that the |index| is
69 // pointing to.
70 level_ = kPermutation[index];
71
72 // Decay the absolute maximum amplitude by 1/4.
73 max_amplitude_ >>= 2;
74
75 // Reset the counter.
76 counter_ = 0;
77 }
78
79 return level_;
80 }
81
82 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698