blob: f77357a0006d9d8fcaa5ba5c277f3220330ba73f [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/modules/audio_processing/rms_level.h"
12
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000013#include <math.h>
henrik.lundin50499422016-11-29 04:26:24 -080014#include <algorithm>
15#include <numeric>
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000016
kwiberg9e2be5f2016-09-14 05:23:22 -070017#include "webrtc/base/checks.h"
18
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000019namespace webrtc {
henrik.lundin50499422016-11-29 04:26:24 -080020namespace {
21static constexpr float kMaxSquaredLevel = 32768 * 32768;
22static constexpr int kMinLevelDb = 127;
23// kMinLevel is the level corresponding to kMinLevelDb, that is 10^(-127/10).
24static constexpr float kMinLevel = 1.995262314968883e-13f;
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000025
henrik.lundin50499422016-11-29 04:26:24 -080026// Calculates the normalized RMS value from a mean square value. The input
27// should be the sum of squared samples divided by the number of samples. The
28// value will be normalized to full range before computing the RMS, wich is
29// returned as a negated dBfs. That is, 0 is full amplitude while 127 is very
30// faint.
31int ComputeRms(float mean_square) {
32 if (mean_square <= kMinLevel * kMaxSquaredLevel) {
33 // Very faint; simply return the minimum value.
34 return kMinLevelDb;
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000035 }
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000036 // Normalize by the max level.
henrik.lundin50499422016-11-29 04:26:24 -080037 const float mean_square_norm = mean_square / kMaxSquaredLevel;
38 RTC_DCHECK_GT(mean_square_norm, kMinLevel);
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000039 // 20log_10(x^0.5) = 10log_10(x)
henrik.lundin50499422016-11-29 04:26:24 -080040 const float rms = 10.f * log10(mean_square_norm);
41 RTC_DCHECK_LE(rms, 0.f);
42 RTC_DCHECK_GT(rms, -kMinLevelDb);
43 // Return the negated value.
44 return static_cast<int>(-rms + 0.5f);
45}
46} // namespace
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000047
henrik.lundin50499422016-11-29 04:26:24 -080048RmsLevel::RmsLevel() {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000049 Reset();
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000050}
51
henrik.lundin50499422016-11-29 04:26:24 -080052RmsLevel::~RmsLevel() = default;
53
54void RmsLevel::Reset() {
55 sum_square_ = 0.f;
56 sample_count_ = 0;
57 max_sum_square_ = 0.f;
58 block_size_ = rtc::Optional<size_t>();
59}
60
61void RmsLevel::Analyze(rtc::ArrayView<const int16_t> data) {
62 if (data.empty()) {
63 return;
64 }
65
66 CheckBlockSize(data.size());
67
68 const float sum_square =
69 std::accumulate(data.begin(), data.end(), 0.f,
70 [](float a, int16_t b) { return a + b * b; });
71 RTC_DCHECK_GE(sum_square, 0.f);
72 sum_square_ += sum_square;
73 sample_count_ += data.size();
74
75 max_sum_square_ = std::max(max_sum_square_, sum_square);
76}
77
78void RmsLevel::AnalyzeMuted(size_t length) {
79 CheckBlockSize(length);
80 sample_count_ += length;
81}
82
83int RmsLevel::Average() {
84 int rms = (sample_count_ == 0) ? kMinLevelDb
85 : ComputeRms(sum_square_ / sample_count_);
86 Reset();
87 return rms;
88}
89
90RmsLevel::Levels RmsLevel::AverageAndPeak() {
91 // Note that block_size_ should by design always be non-empty when
92 // sample_count_ != 0. Also, the * operator of rtc::Optional enforces this
93 // with a DCHECK.
94 Levels levels = (sample_count_ == 0)
95 ? Levels{kMinLevelDb, kMinLevelDb}
96 : Levels{ComputeRms(sum_square_ / sample_count_),
97 ComputeRms(max_sum_square_ / *block_size_)};
98 Reset();
99 return levels;
100}
101
102void RmsLevel::CheckBlockSize(size_t block_size) {
103 if (block_size_ != rtc::Optional<size_t>(block_size)) {
104 Reset();
105 block_size_ = rtc::Optional<size_t>(block_size);
106 }
107}
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000108} // namespace webrtc