blob: e6b5849ead8bde88dcc2f717c306b6b20b266fdc [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
12#define MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
andrew@webrtc.org21299d42014-05-14 19:00:59 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
16
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020017#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000019
20namespace webrtc {
21
22// Computes the root mean square (RMS) level in dBFs (decibels from digital
23// full-scale) of audio data. The computation follows RFC 6465:
24// https://tools.ietf.org/html/rfc6465
25// with the intent that it can provide the RTP audio level indication.
26//
27// The expected approach is to provide constant-sized chunks of audio to
henrik.lundin50499422016-11-29 04:26:24 -080028// Analyze(). When enough chunks have been accumulated to form a packet, call
29// Average() to get the audio level indicator for the RTP header.
30class RmsLevel {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000031 public:
henrik.lundin50499422016-11-29 04:26:24 -080032 struct Levels {
33 int average;
34 int peak;
35 };
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000036
henrik.lundin290d43a2016-11-29 08:09:09 -080037 static constexpr int kMinLevelDb = 127;
38
henrik.lundin50499422016-11-29 04:26:24 -080039 RmsLevel();
40 ~RmsLevel();
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000041
42 // Can be called to reset internal states, but is not required during normal
43 // operation.
44 void Reset();
45
henrik.lundin50499422016-11-29 04:26:24 -080046 // Pass each chunk of audio to Analyze() to accumulate the level.
47 void Analyze(rtc::ArrayView<const int16_t> data);
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000048
49 // If all samples with the given |length| have a magnitude of zero, this is
50 // a shortcut to avoid some computation.
henrik.lundin50499422016-11-29 04:26:24 -080051 void AnalyzeMuted(size_t length);
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000052
henrik.lundin50499422016-11-29 04:26:24 -080053 // Computes the RMS level over all data passed to Analyze() since the last
54 // call to Average(). The returned value is positive but should be interpreted
55 // as negative as per the RFC. It is constrained to [0, 127]. Resets the
56 // internal state to start a new measurement period.
57 int Average();
58
59 // Like Average() above, but also returns the RMS peak value. Resets the
60 // internal state to start a new measurement period.
61 Levels AverageAndPeak();
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000062
63 private:
henrik.lundin50499422016-11-29 04:26:24 -080064 // Compares |block_size| with |block_size_|. If they are different, calls
65 // Reset() and stores the new size.
66 void CheckBlockSize(size_t block_size);
67
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000068 float sum_square_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070069 size_t sample_count_;
henrik.lundin50499422016-11-29 04:26:24 -080070 float max_sum_square_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020071 absl::optional<size_t> block_size_;
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000072};
73
74} // namespace webrtc
andrew@webrtc.org21299d42014-05-14 19:00:59 +000075
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020076#endif // MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_