andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_ |
andrew@webrtc.org | 21299d4 | 2014-05-14 19:00:59 +0000 | [diff] [blame] | 13 | |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 14 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/array_view.h" |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | // Computes the root mean square (RMS) level in dBFs (decibels from digital |
| 20 | // full-scale) of audio data. The computation follows RFC 6465: |
| 21 | // https://tools.ietf.org/html/rfc6465 |
| 22 | // with the intent that it can provide the RTP audio level indication. |
| 23 | // |
| 24 | // The expected approach is to provide constant-sized chunks of audio to |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 25 | // Analyze(). When enough chunks have been accumulated to form a packet, call |
| 26 | // Average() to get the audio level indicator for the RTP header. |
| 27 | class RmsLevel { |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 28 | public: |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 29 | struct Levels { |
| 30 | int average; |
| 31 | int peak; |
| 32 | }; |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 33 | |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 34 | static constexpr int kMinLevelDb = 127; |
| 35 | |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 36 | RmsLevel(); |
| 37 | ~RmsLevel(); |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 38 | |
| 39 | // Can be called to reset internal states, but is not required during normal |
| 40 | // operation. |
| 41 | void Reset(); |
| 42 | |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 43 | // Pass each chunk of audio to Analyze() to accumulate the level. |
| 44 | void Analyze(rtc::ArrayView<const int16_t> data); |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 45 | |
| 46 | // If all samples with the given |length| have a magnitude of zero, this is |
| 47 | // a shortcut to avoid some computation. |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 48 | void AnalyzeMuted(size_t length); |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 49 | |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 50 | // Computes the RMS level over all data passed to Analyze() since the last |
| 51 | // call to Average(). The returned value is positive but should be interpreted |
| 52 | // as negative as per the RFC. It is constrained to [0, 127]. Resets the |
| 53 | // internal state to start a new measurement period. |
| 54 | int Average(); |
| 55 | |
| 56 | // Like Average() above, but also returns the RMS peak value. Resets the |
| 57 | // internal state to start a new measurement period. |
| 58 | Levels AverageAndPeak(); |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 59 | |
| 60 | private: |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 61 | // Compares |block_size| with |block_size_|. If they are different, calls |
| 62 | // Reset() and stores the new size. |
| 63 | void CheckBlockSize(size_t block_size); |
| 64 | |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 65 | float sum_square_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 66 | size_t sample_count_; |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 67 | float max_sum_square_; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 68 | absl::optional<size_t> block_size_; |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace webrtc |
andrew@webrtc.org | 21299d4 | 2014-05-14 19:00:59 +0000 | [diff] [blame] | 72 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 73 | #endif // MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_ |