Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_conference_mixer / source / level_indicator.cc
1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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 "webrtc/modules/audio_conference_mixer/source/level_indicator.h"
12
13 namespace webrtc {
14 // Array for adding smothing to level changes (ad-hoc).
15 const uint32_t perm[] =
16     {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};
17
18 LevelIndicator::LevelIndicator()
19     : _max(0),
20       _count(0),
21       _currentLevel(0)
22 {
23 }
24
25 LevelIndicator::~LevelIndicator()
26 {
27 }
28
29 // Level is based on the highest absolute value for all samples.
30 void LevelIndicator::ComputeLevel(const int16_t* speech,
31                                   const uint16_t nrOfSamples)
32 {
33     int32_t min = 0;
34     for(uint32_t i = 0; i < nrOfSamples; i++)
35     {
36         if(_max < speech[i])
37         {
38             _max = speech[i];
39         }
40         if(min > speech[i])
41         {
42             min = speech[i];
43         }
44     }
45
46     // Absolute max value.
47     if(-min > _max)
48     {
49         _max = -min;
50     }
51
52     if(_count == TICKS_BEFORE_CALCULATION)
53     {
54         // Highest sample value maps directly to a level.
55         int32_t position = _max / 1000;
56         if ((position == 0) &&
57             (_max > 250))
58         {
59             position = 1;
60         }
61         _currentLevel = perm[position];
62         // The max value is decayed and stored so that it can be reused to slow
63         // down decreases in level.
64         _max = _max >> 1;
65         _count = 0;
66     } else {
67         _count++;
68     }
69 }
70
71 int32_t LevelIndicator::GetLevel()
72 {
73     return _currentLevel;
74 }
75
76 }  // namespace webrtc