andrew@webrtc.org | 8ec46c6 | 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 | |
| 13 | #include <assert.h> |
| 14 | #include <math.h> |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
andrew@webrtc.org | 5ec8fee | 2014-05-14 19:00:59 +0000 | [diff] [blame^] | 18 | static const float kMaxSquaredLevel = 32768 * 32768; |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 19 | |
| 20 | RMSLevel::RMSLevel() |
andrew@webrtc.org | 5ec8fee | 2014-05-14 19:00:59 +0000 | [diff] [blame^] | 21 | : sum_square_(0), |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 22 | sample_count_(0) {} |
| 23 | |
| 24 | RMSLevel::~RMSLevel() {} |
| 25 | |
| 26 | void RMSLevel::Reset() { |
andrew@webrtc.org | 5ec8fee | 2014-05-14 19:00:59 +0000 | [diff] [blame^] | 27 | sum_square_ = 0; |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 28 | sample_count_ = 0; |
| 29 | } |
| 30 | |
| 31 | void RMSLevel::Process(const int16_t* data, int length) { |
| 32 | for (int i = 0; i < length; ++i) { |
| 33 | sum_square_ += data[i] * data[i]; |
| 34 | } |
| 35 | sample_count_ += length; |
| 36 | } |
| 37 | |
| 38 | void RMSLevel::ProcessMuted(int length) { |
| 39 | sample_count_ += length; |
| 40 | } |
| 41 | |
| 42 | int RMSLevel::RMS() { |
andrew@webrtc.org | 5ec8fee | 2014-05-14 19:00:59 +0000 | [diff] [blame^] | 43 | if (sample_count_ == 0 || sum_square_ == 0) { |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 44 | Reset(); |
| 45 | return kMinLevel; |
| 46 | } |
| 47 | |
| 48 | // Normalize by the max level. |
| 49 | float rms = sum_square_ / (sample_count_ * kMaxSquaredLevel); |
| 50 | // 20log_10(x^0.5) = 10log_10(x) |
| 51 | rms = 10 * log10(rms); |
| 52 | assert(rms <= 0); |
| 53 | if (rms < -kMinLevel) |
| 54 | rms = -kMinLevel; |
| 55 | |
| 56 | rms = -rms; |
| 57 | Reset(); |
| 58 | return static_cast<int>(rms + 0.5); |
| 59 | } |
| 60 | |
| 61 | } // namespace webrtc |