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