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