blob: 92fc8efe4c37ceafc5e12f045829c467c8d730d9 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000011#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010012#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000013#include "webrtc/voice_engine/level_indicator.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000014
15namespace webrtc {
16
17namespace voe {
18
niklase@google.com470e71d2011-07-07 08:21:25 +000019// Number of bars on the indicator.
20// Note that the number of elements is specified because we are indexing it
21// in the range of 0-32
pbos@webrtc.org6141e132013-04-09 10:09:10 +000022const int8_t permutation[33] =
niklase@google.com470e71d2011-07-07 08:21:25 +000023 {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};
24
25
26AudioLevel::AudioLevel() :
27 _absMax(0),
28 _count(0),
29 _currentLevel(0),
henrika@webrtc.orgd108a462013-04-03 11:25:31 +000030 _currentLevelFullRange(0) {
aleloi616df1e2016-08-24 01:17:12 -070031 WebRtcSpl_Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
henrika@webrtc.orgd108a462013-04-03 11:25:31 +000034AudioLevel::~AudioLevel() {
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
henrika@webrtc.orgd108a462013-04-03 11:25:31 +000037void AudioLevel::Clear()
niklase@google.com470e71d2011-07-07 08:21:25 +000038{
tommi31fc21f2016-01-21 10:37:37 -080039 rtc::CritScope cs(&_critSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000040 _absMax = 0;
41 _count = 0;
42 _currentLevel = 0;
43 _currentLevelFullRange = 0;
44}
45
henrika@webrtc.orgd108a462013-04-03 11:25:31 +000046void AudioLevel::ComputeLevel(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +000047{
pbos@webrtc.org6141e132013-04-09 10:09:10 +000048 int16_t absValue(0);
niklase@google.com470e71d2011-07-07 08:21:25 +000049
50 // Check speech level (works for 2 channels as well)
51 absValue = WebRtcSpl_MaxAbsValueW16(
andrew@webrtc.org63a50982012-05-02 23:56:37 +000052 audioFrame.data_,
53 audioFrame.samples_per_channel_*audioFrame.num_channels_);
henrika@webrtc.orgd108a462013-04-03 11:25:31 +000054
55 // Protect member access using a lock since this method is called on a
56 // dedicated audio thread in the RecordedDataIsAvailable() callback.
tommi31fc21f2016-01-21 10:37:37 -080057 rtc::CritScope cs(&_critSect);
henrika@webrtc.orgd108a462013-04-03 11:25:31 +000058
niklase@google.com470e71d2011-07-07 08:21:25 +000059 if (absValue > _absMax)
60 _absMax = absValue;
61
62 // Update level approximately 10 times per second
63 if (_count++ == kUpdateFrequency)
64 {
65 _currentLevelFullRange = _absMax;
66
67 _count = 0;
68
pbos@webrtc.org6141e132013-04-09 10:09:10 +000069 // Highest value for a int16_t is 0x7fff = 32767
niklase@google.com470e71d2011-07-07 08:21:25 +000070 // Divide with 1000 to get in the range of 0-32 which is the range of
71 // the permutation vector
pbos@webrtc.org6141e132013-04-09 10:09:10 +000072 int32_t position = _absMax/1000;
niklase@google.com470e71d2011-07-07 08:21:25 +000073
74 // Make it less likely that the bar stays at position 0. I.e. only if
75 // its in the range 0-250 (instead of 0-1000)
76 if ((position == 0) && (_absMax > 250))
77 {
78 position = 1;
79 }
80 _currentLevel = permutation[position];
81
82 // Decay the absolute maximum (divide by 4)
83 _absMax >>= 2;
84 }
85}
86
pbos@webrtc.org6141e132013-04-09 10:09:10 +000087int8_t AudioLevel::Level() const
niklase@google.com470e71d2011-07-07 08:21:25 +000088{
tommi31fc21f2016-01-21 10:37:37 -080089 rtc::CritScope cs(&_critSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000090 return _currentLevel;
91}
92
pbos@webrtc.org6141e132013-04-09 10:09:10 +000093int16_t AudioLevel::LevelFullRange() const
niklase@google.com470e71d2011-07-07 08:21:25 +000094{
tommi31fc21f2016-01-21 10:37:37 -080095 rtc::CritScope cs(&_critSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000096 return _currentLevelFullRange;
97}
98
99} // namespace voe
100
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000101} // namespace webrtc