blob: e796095170cb5b8ca66322d25f49c2746574fdc8 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/level_estimator_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stddef.h>
14#include <stdint.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/array_view.h"
17#include "modules/audio_processing/audio_buffer.h"
18#include "modules/audio_processing/rms_level.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "rtc_base/checks.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
solenberg949028f2015-12-15 11:39:38 -080023LevelEstimatorImpl::LevelEstimatorImpl(rtc::CriticalSection* crit)
henrik.lundin50499422016-11-29 04:26:24 -080024 : crit_(crit), rms_(new RmsLevel()) {
peahdf3efa82015-11-28 12:35:15 -080025 RTC_DCHECK(crit);
26}
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28LevelEstimatorImpl::~LevelEstimatorImpl() {}
29
solenberg949028f2015-12-15 11:39:38 -080030void LevelEstimatorImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -080031 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080032 rms_->Reset();
33}
peahdf3efa82015-11-28 12:35:15 -080034
Per Åhgrend47941e2019-08-22 11:51:13 +020035void LevelEstimatorImpl::ProcessStream(const AudioBuffer& audio) {
solenberg949028f2015-12-15 11:39:38 -080036 rtc::CritScope cs(crit_);
37 if (!enabled_) {
38 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000039 }
40
Per Åhgrend47941e2019-08-22 11:51:13 +020041 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.com470e71d2011-07-07 08:21:25 +000044 }
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000047int LevelEstimatorImpl::Enable(bool enable) {
peahdf3efa82015-11-28 12:35:15 -080048 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080049 if (enable && !enabled_) {
50 rms_->Reset();
51 }
52 enabled_ = enable;
53 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
56bool LevelEstimatorImpl::is_enabled() const {
peahdf3efa82015-11-28 12:35:15 -080057 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080058 return enabled_;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000061int LevelEstimatorImpl::RMS() {
peahdf3efa82015-11-28 12:35:15 -080062 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080063 if (!enabled_) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000064 return AudioProcessing::kNotEnabledError;
niklase@google.com470e71d2011-07-07 08:21:25 +000065 }
66
henrik.lundin50499422016-11-29 04:26:24 -080067 return rms_->Average();
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
niklase@google.com470e71d2011-07-07 08:21:25 +000069} // namespace webrtc