• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 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 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
12 #define WEBRTC_MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
13 
14 #include "webrtc/typedefs.h"
15 
16 namespace webrtc {
17 
18 // Computes the root mean square (RMS) level in dBFs (decibels from digital
19 // full-scale) of audio data. The computation follows RFC 6465:
20 // https://tools.ietf.org/html/rfc6465
21 // with the intent that it can provide the RTP audio level indication.
22 //
23 // The expected approach is to provide constant-sized chunks of audio to
24 // Process(). When enough chunks have been accumulated to form a packet, call
25 // RMS() to get the audio level indicator for the RTP header.
26 class RMSLevel {
27  public:
28   static const int kMinLevel = 127;
29 
30   RMSLevel();
31   ~RMSLevel();
32 
33   // Can be called to reset internal states, but is not required during normal
34   // operation.
35   void Reset();
36 
37   // Pass each chunk of audio to Process() to accumulate the level.
38   void Process(const int16_t* data, int length);
39 
40   // If all samples with the given |length| have a magnitude of zero, this is
41   // a shortcut to avoid some computation.
42   void ProcessMuted(int length);
43 
44   // Computes the RMS level over all data passed to Process() since the last
45   // call to RMS(). The returned value is positive but should be interpreted as
46   // negative as per the RFC. It is constrained to [0, 127].
47   int RMS();
48 
49  private:
50   float sum_square_;
51   int sample_count_;
52 };
53 
54 }  // namespace webrtc
55 
56 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
57 
58