niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 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 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_processing/level_estimator_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "api/array_view.h" |
| 14 | #include "modules/audio_processing/audio_buffer.h" |
| 15 | #include "modules/audio_processing/rms_level.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 18 | |
solenberg | 949028f | 2015-12-15 19:39:38 | [diff] [blame] | 19 | LevelEstimatorImpl::LevelEstimatorImpl(rtc::CriticalSection* crit) |
henrik.lundin | 5049942 | 2016-11-29 12:26:24 | [diff] [blame] | 20 | : crit_(crit), rms_(new RmsLevel()) { |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 21 | RTC_DCHECK(crit); |
| 22 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 23 | |
| 24 | LevelEstimatorImpl::~LevelEstimatorImpl() {} |
| 25 | |
solenberg | 949028f | 2015-12-15 19:39:38 | [diff] [blame] | 26 | void LevelEstimatorImpl::Initialize() { |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 27 | rtc::CritScope cs(crit_); |
solenberg | 949028f | 2015-12-15 19:39:38 | [diff] [blame] | 28 | rms_->Reset(); |
| 29 | } |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 30 | |
solenberg | 949028f | 2015-12-15 19:39:38 | [diff] [blame] | 31 | void LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) { |
| 32 | RTC_DCHECK(audio); |
| 33 | rtc::CritScope cs(crit_); |
| 34 | if (!enabled_) { |
| 35 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 36 | } |
| 37 | |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 38 | for (size_t i = 0; i < audio->num_channels(); i++) { |
henrik.lundin | 5049942 | 2016-11-29 12:26:24 | [diff] [blame] | 39 | rms_->Analyze(rtc::ArrayView<const int16_t>(audio->channels_const()[i], |
| 40 | audio->num_frames())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 41 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 42 | } |
| 43 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 | [diff] [blame] | 44 | int LevelEstimatorImpl::Enable(bool enable) { |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 45 | rtc::CritScope cs(crit_); |
solenberg | 949028f | 2015-12-15 19:39:38 | [diff] [blame] | 46 | if (enable && !enabled_) { |
| 47 | rms_->Reset(); |
| 48 | } |
| 49 | enabled_ = enable; |
| 50 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool LevelEstimatorImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 54 | rtc::CritScope cs(crit_); |
solenberg | 949028f | 2015-12-15 19:39:38 | [diff] [blame] | 55 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 56 | } |
| 57 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 | [diff] [blame] | 58 | int LevelEstimatorImpl::RMS() { |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 59 | rtc::CritScope cs(crit_); |
solenberg | 949028f | 2015-12-15 19:39:38 | [diff] [blame] | 60 | if (!enabled_) { |
andrew@webrtc.org | 382c0c2 | 2014-05-05 18:22:21 | [diff] [blame] | 61 | return AudioProcessing::kNotEnabledError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 62 | } |
| 63 | |
henrik.lundin | 5049942 | 2016-11-29 12:26:24 | [diff] [blame] | 64 | return rms_->Average(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 65 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 66 | } // namespace webrtc |