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