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 | #include "modules/audio_processing/rms_level.h" |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 12 | |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 13 | #include <math.h> |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <numeric> |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 18 | |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 19 | namespace webrtc { |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 20 | namespace { |
| 21 | static constexpr float kMaxSquaredLevel = 32768 * 32768; |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 22 | // kMinLevel is the level corresponding to kMinLevelDb, that is 10^(-127/10). |
| 23 | static constexpr float kMinLevel = 1.995262314968883e-13f; |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 24 | |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 25 | // Calculates the normalized RMS value from a mean square value. The input |
| 26 | // should be the sum of squared samples divided by the number of samples. The |
| 27 | // value will be normalized to full range before computing the RMS, wich is |
| 28 | // returned as a negated dBfs. That is, 0 is full amplitude while 127 is very |
| 29 | // faint. |
| 30 | int ComputeRms(float mean_square) { |
| 31 | if (mean_square <= kMinLevel * kMaxSquaredLevel) { |
| 32 | // Very faint; simply return the minimum value. |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 33 | return RmsLevel::kMinLevelDb; |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 34 | } |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 35 | // Normalize by the max level. |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 36 | const float mean_square_norm = mean_square / kMaxSquaredLevel; |
| 37 | RTC_DCHECK_GT(mean_square_norm, kMinLevel); |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 38 | // 20log_10(x^0.5) = 10log_10(x) |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 39 | const float rms = 10.f * log10(mean_square_norm); |
| 40 | RTC_DCHECK_LE(rms, 0.f); |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 41 | RTC_DCHECK_GT(rms, -RmsLevel::kMinLevelDb); |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 42 | // Return the negated value. |
| 43 | return static_cast<int>(-rms + 0.5f); |
| 44 | } |
| 45 | } // namespace |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 46 | |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 47 | RmsLevel::RmsLevel() { |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 48 | Reset(); |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 49 | } |
| 50 | |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 51 | RmsLevel::~RmsLevel() = default; |
| 52 | |
| 53 | void RmsLevel::Reset() { |
| 54 | sum_square_ = 0.f; |
| 55 | sample_count_ = 0; |
| 56 | max_sum_square_ = 0.f; |
Oskar Sundbom | aa8b67d | 2017-11-17 14:34:48 +0100 | [diff] [blame] | 57 | block_size_ = rtc::nullopt; |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void RmsLevel::Analyze(rtc::ArrayView<const int16_t> data) { |
| 61 | if (data.empty()) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | CheckBlockSize(data.size()); |
| 66 | |
| 67 | const float sum_square = |
| 68 | std::accumulate(data.begin(), data.end(), 0.f, |
| 69 | [](float a, int16_t b) { return a + b * b; }); |
| 70 | RTC_DCHECK_GE(sum_square, 0.f); |
| 71 | sum_square_ += sum_square; |
| 72 | sample_count_ += data.size(); |
| 73 | |
| 74 | max_sum_square_ = std::max(max_sum_square_, sum_square); |
| 75 | } |
| 76 | |
| 77 | void RmsLevel::AnalyzeMuted(size_t length) { |
| 78 | CheckBlockSize(length); |
| 79 | sample_count_ += length; |
| 80 | } |
| 81 | |
| 82 | int RmsLevel::Average() { |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 83 | int rms = (sample_count_ == 0) ? RmsLevel::kMinLevelDb |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 84 | : ComputeRms(sum_square_ / sample_count_); |
| 85 | Reset(); |
| 86 | return rms; |
| 87 | } |
| 88 | |
| 89 | RmsLevel::Levels RmsLevel::AverageAndPeak() { |
| 90 | // Note that block_size_ should by design always be non-empty when |
| 91 | // sample_count_ != 0. Also, the * operator of rtc::Optional enforces this |
| 92 | // with a DCHECK. |
| 93 | Levels levels = (sample_count_ == 0) |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 94 | ? Levels{RmsLevel::kMinLevelDb, RmsLevel::kMinLevelDb} |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 95 | : Levels{ComputeRms(sum_square_ / sample_count_), |
| 96 | ComputeRms(max_sum_square_ / *block_size_)}; |
| 97 | Reset(); |
| 98 | return levels; |
| 99 | } |
| 100 | |
| 101 | void RmsLevel::CheckBlockSize(size_t block_size) { |
Oskar Sundbom | aa8b67d | 2017-11-17 14:34:48 +0100 | [diff] [blame] | 102 | if (block_size_ != block_size) { |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 103 | Reset(); |
Oskar Sundbom | aa8b67d | 2017-11-17 14:34:48 +0100 | [diff] [blame] | 104 | block_size_ = block_size; |
henrik.lundin | 5049942 | 2016-11-29 04:26:24 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 107 | } // namespace webrtc |