OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/rms_level.h" | 11 #include "webrtc/modules/audio_processing/rms_level.h" |
12 | 12 |
13 #include <math.h> | 13 #include <math.h> |
14 #include <algorithm> | 14 #include <algorithm> |
15 #include <numeric> | 15 #include <numeric> |
16 | 16 |
17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
18 | 18 |
19 namespace webrtc { | 19 namespace webrtc { |
20 namespace { | 20 namespace { |
21 static constexpr float kMaxSquaredLevel = 32768 * 32768; | 21 static constexpr float kMaxSquaredLevel = 32768 * 32768; |
22 static constexpr int kMinLevelDb = 127; | |
23 // kMinLevel is the level corresponding to kMinLevelDb, that is 10^(-127/10). | 22 // kMinLevel is the level corresponding to kMinLevelDb, that is 10^(-127/10). |
24 static constexpr float kMinLevel = 1.995262314968883e-13f; | 23 static constexpr float kMinLevel = 1.995262314968883e-13f; |
25 | 24 |
26 // Calculates the normalized RMS value from a mean square value. The input | 25 // 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 | 26 // 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 | 27 // 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 | 28 // returned as a negated dBfs. That is, 0 is full amplitude while 127 is very |
30 // faint. | 29 // faint. |
31 int ComputeRms(float mean_square) { | 30 int ComputeRms(float mean_square) { |
32 if (mean_square <= kMinLevel * kMaxSquaredLevel) { | 31 if (mean_square <= kMinLevel * kMaxSquaredLevel) { |
33 // Very faint; simply return the minimum value. | 32 // Very faint; simply return the minimum value. |
34 return kMinLevelDb; | 33 return RmsLevel::kMinLevelDb; |
35 } | 34 } |
36 // Normalize by the max level. | 35 // Normalize by the max level. |
37 const float mean_square_norm = mean_square / kMaxSquaredLevel; | 36 const float mean_square_norm = mean_square / kMaxSquaredLevel; |
38 RTC_DCHECK_GT(mean_square_norm, kMinLevel); | 37 RTC_DCHECK_GT(mean_square_norm, kMinLevel); |
39 // 20log_10(x^0.5) = 10log_10(x) | 38 // 20log_10(x^0.5) = 10log_10(x) |
40 const float rms = 10.f * log10(mean_square_norm); | 39 const float rms = 10.f * log10(mean_square_norm); |
41 RTC_DCHECK_LE(rms, 0.f); | 40 RTC_DCHECK_LE(rms, 0.f); |
42 RTC_DCHECK_GT(rms, -kMinLevelDb); | 41 RTC_DCHECK_GT(rms, -RmsLevel::kMinLevelDb); |
43 // Return the negated value. | 42 // Return the negated value. |
44 return static_cast<int>(-rms + 0.5f); | 43 return static_cast<int>(-rms + 0.5f); |
45 } | 44 } |
46 } // namespace | 45 } // namespace |
47 | 46 |
48 RmsLevel::RmsLevel() { | 47 RmsLevel::RmsLevel() { |
49 Reset(); | 48 Reset(); |
50 } | 49 } |
51 | 50 |
52 RmsLevel::~RmsLevel() = default; | 51 RmsLevel::~RmsLevel() = default; |
(...skipping 21 matching lines...) Expand all Loading... |
74 | 73 |
75 max_sum_square_ = std::max(max_sum_square_, sum_square); | 74 max_sum_square_ = std::max(max_sum_square_, sum_square); |
76 } | 75 } |
77 | 76 |
78 void RmsLevel::AnalyzeMuted(size_t length) { | 77 void RmsLevel::AnalyzeMuted(size_t length) { |
79 CheckBlockSize(length); | 78 CheckBlockSize(length); |
80 sample_count_ += length; | 79 sample_count_ += length; |
81 } | 80 } |
82 | 81 |
83 int RmsLevel::Average() { | 82 int RmsLevel::Average() { |
84 int rms = (sample_count_ == 0) ? kMinLevelDb | 83 int rms = (sample_count_ == 0) ? RmsLevel::kMinLevelDb |
85 : ComputeRms(sum_square_ / sample_count_); | 84 : ComputeRms(sum_square_ / sample_count_); |
86 Reset(); | 85 Reset(); |
87 return rms; | 86 return rms; |
88 } | 87 } |
89 | 88 |
90 RmsLevel::Levels RmsLevel::AverageAndPeak() { | 89 RmsLevel::Levels RmsLevel::AverageAndPeak() { |
91 // Note that block_size_ should by design always be non-empty when | 90 // Note that block_size_ should by design always be non-empty when |
92 // sample_count_ != 0. Also, the * operator of rtc::Optional enforces this | 91 // sample_count_ != 0. Also, the * operator of rtc::Optional enforces this |
93 // with a DCHECK. | 92 // with a DCHECK. |
94 Levels levels = (sample_count_ == 0) | 93 Levels levels = (sample_count_ == 0) |
95 ? Levels{kMinLevelDb, kMinLevelDb} | 94 ? Levels{RmsLevel::kMinLevelDb, RmsLevel::kMinLevelDb} |
96 : Levels{ComputeRms(sum_square_ / sample_count_), | 95 : Levels{ComputeRms(sum_square_ / sample_count_), |
97 ComputeRms(max_sum_square_ / *block_size_)}; | 96 ComputeRms(max_sum_square_ / *block_size_)}; |
98 Reset(); | 97 Reset(); |
99 return levels; | 98 return levels; |
100 } | 99 } |
101 | 100 |
102 void RmsLevel::CheckBlockSize(size_t block_size) { | 101 void RmsLevel::CheckBlockSize(size_t block_size) { |
103 if (block_size_ != rtc::Optional<size_t>(block_size)) { | 102 if (block_size_ != rtc::Optional<size_t>(block_size)) { |
104 Reset(); | 103 Reset(); |
105 block_size_ = rtc::Optional<size_t>(block_size); | 104 block_size_ = rtc::Optional<size_t>(block_size); |
106 } | 105 } |
107 } | 106 } |
108 } // namespace webrtc | 107 } // namespace webrtc |
OLD | NEW |