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 | |
| 11 | #include "webrtc/modules/audio_processing/rms_level.h" |
| 12 | |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame^] | 15 | #include "webrtc/base/checks.h" |
| 16 | |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | |
andrew@webrtc.org | 21299d4 | 2014-05-14 19:00:59 +0000 | [diff] [blame] | 19 | static const float kMaxSquaredLevel = 32768 * 32768; |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 20 | |
| 21 | RMSLevel::RMSLevel() |
andrew@webrtc.org | 21299d4 | 2014-05-14 19:00:59 +0000 | [diff] [blame] | 22 | : sum_square_(0), |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 23 | sample_count_(0) {} |
| 24 | |
| 25 | RMSLevel::~RMSLevel() {} |
| 26 | |
| 27 | void RMSLevel::Reset() { |
andrew@webrtc.org | 21299d4 | 2014-05-14 19:00:59 +0000 | [diff] [blame] | 28 | sum_square_ = 0; |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 29 | sample_count_ = 0; |
| 30 | } |
| 31 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 32 | void RMSLevel::Process(const int16_t* data, size_t length) { |
| 33 | for (size_t i = 0; i < length; ++i) { |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 34 | sum_square_ += data[i] * data[i]; |
| 35 | } |
| 36 | sample_count_ += length; |
| 37 | } |
| 38 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 39 | void RMSLevel::ProcessMuted(size_t length) { |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 40 | sample_count_ += length; |
| 41 | } |
| 42 | |
| 43 | int RMSLevel::RMS() { |
andrew@webrtc.org | 21299d4 | 2014-05-14 19:00:59 +0000 | [diff] [blame] | 44 | if (sample_count_ == 0 || sum_square_ == 0) { |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 45 | Reset(); |
| 46 | return kMinLevel; |
| 47 | } |
| 48 | |
| 49 | // Normalize by the max level. |
| 50 | float rms = sum_square_ / (sample_count_ * kMaxSquaredLevel); |
| 51 | // 20log_10(x^0.5) = 10log_10(x) |
| 52 | rms = 10 * log10(rms); |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame^] | 53 | RTC_DCHECK_LE(rms, 0); |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 54 | if (rms < -kMinLevel) |
| 55 | rms = -kMinLevel; |
| 56 | |
| 57 | rms = -rms; |
| 58 | Reset(); |
| 59 | return static_cast<int>(rms + 0.5); |
| 60 | } |
| 61 | |
| 62 | } // namespace webrtc |