Chromium Code Reviews| Index: content/renderer/media/media_stream_audio_level_calculator.cc |
| diff --git a/content/renderer/media/media_stream_audio_level_calculator.cc b/content/renderer/media/media_stream_audio_level_calculator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2b72e02edda3de667762a6603e83ac365b862234 |
| --- /dev/null |
| +++ b/content/renderer/media/media_stream_audio_level_calculator.cc |
| @@ -0,0 +1,72 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/media/media_stream_audio_level_calculator.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/stl_util.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +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.
|
| + {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}; |
| +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.
|
| +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
|
| + |
| +int MaxAmplitude(const int16* audio_data, int length) { |
| + int max = 0, absolute = 0; |
| + for (int i = 0; i < length; ++i) { |
| + absolute = std::abs(audio_data[i]); |
| + if (absolute > max) |
| + max = absolute; |
| + } |
| + |
| + return max; |
| +} |
| + |
| +} // namespace |
| + |
| +MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator() |
| + : counter_(0), |
| + max_amplitude_(0), |
| + level_(0) { |
| +} |
| + |
| +MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() { |
| +} |
| + |
| +int MediaStreamAudioLevelCalculator::Calculate(const int16* audio_data, |
| + int number_of_channels, |
| + int number_of_frames) { |
| + DCHECK(CalledOnValidThread()); |
|
tommi (sloooow) - chröme
2014/02/27 08:39:43
nice
|
| + int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames); |
| + max_amplitude_ = std::max(max_amplitude_, std::min(max, k16BitsMaxValue)); |
| + |
| + if (counter_++ == kUpdateFrequency) { |
| + // Divide k16BitsMaxValue (32767) by 1000 to get in the range of [0,32] |
| + // which is the range of the permutation array. |
| + int index = static_cast<int>(max_amplitude_ / 1000); |
| + |
| + // Make it less likely that the bar stays at position 0. I.e. only if |
| + // its in the range 0-250 (instead of 0-1000) |
| + if (index == 0 && max_amplitude_ > 250) |
| + index = 1; |
| + |
| + // |level_| will be the value in the permutation array that the |index| is |
| + // pointing to. |
| + level_ = kPermutation[index]; |
| + |
| + // Decay the absolute maximum amplitude by 1/4. |
| + max_amplitude_ >>= 2; |
| + |
| + // Reset the counter. |
| + counter_ = 0; |
| + } |
| + |
| + return level_; |
| +} |
| + |
| +} // namespace content |