niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "level_indicator.h" |
| 12 | #include "module_common_types.h" |
| 13 | #include "signal_processing_library.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | namespace voe { |
| 18 | |
| 19 | |
| 20 | // Number of bars on the indicator. |
| 21 | // Note that the number of elements is specified because we are indexing it |
| 22 | // in the range of 0-32 |
| 23 | const WebRtc_Word8 permutation[33] = |
| 24 | {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}; |
| 25 | |
| 26 | |
| 27 | AudioLevel::AudioLevel() : |
| 28 | _absMax(0), |
| 29 | _count(0), |
| 30 | _currentLevel(0), |
| 31 | _currentLevelFullRange(0) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | AudioLevel::~AudioLevel() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | AudioLevel::Clear() |
| 41 | { |
| 42 | _absMax = 0; |
| 43 | _count = 0; |
| 44 | _currentLevel = 0; |
| 45 | _currentLevelFullRange = 0; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | AudioLevel::ComputeLevel(const AudioFrame& audioFrame) |
| 50 | { |
| 51 | WebRtc_Word16 absValue(0); |
| 52 | |
| 53 | // Check speech level (works for 2 channels as well) |
| 54 | absValue = WebRtcSpl_MaxAbsValueW16( |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 55 | audioFrame.data_, |
| 56 | audioFrame.samples_per_channel_*audioFrame.num_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | if (absValue > _absMax) |
| 58 | _absMax = absValue; |
| 59 | |
| 60 | // Update level approximately 10 times per second |
| 61 | if (_count++ == kUpdateFrequency) |
| 62 | { |
| 63 | _currentLevelFullRange = _absMax; |
| 64 | |
| 65 | _count = 0; |
| 66 | |
| 67 | // Highest value for a WebRtc_Word16 is 0x7fff = 32767 |
| 68 | // Divide with 1000 to get in the range of 0-32 which is the range of |
| 69 | // the permutation vector |
| 70 | WebRtc_Word32 position = _absMax/1000; |
| 71 | |
| 72 | // Make it less likely that the bar stays at position 0. I.e. only if |
| 73 | // its in the range 0-250 (instead of 0-1000) |
| 74 | if ((position == 0) && (_absMax > 250)) |
| 75 | { |
| 76 | position = 1; |
| 77 | } |
| 78 | _currentLevel = permutation[position]; |
| 79 | |
| 80 | // Decay the absolute maximum (divide by 4) |
| 81 | _absMax >>= 2; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | WebRtc_Word8 |
| 86 | AudioLevel::Level() const |
| 87 | { |
| 88 | return _currentLevel; |
| 89 | } |
| 90 | |
| 91 | WebRtc_Word16 |
| 92 | AudioLevel::LevelFullRange() const |
| 93 | { |
| 94 | return _currentLevelFullRange; |
| 95 | } |
| 96 | |
| 97 | } // namespace voe |
| 98 | |
| 99 | } // namespace webrtc |