blob: 5b49b35fdcc5653824e084ae544425dda0f0722e [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
solenberg949028f2015-12-15 11:39:38 -080035void LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
36 RTC_DCHECK(audio);
37 rtc::CritScope cs(crit_);
38 if (!enabled_) {
39 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000040 }
41
Peter Kasting69558702016-01-12 16:26:35 -080042 for (size_t i = 0; i < audio->num_channels(); i++) {
henrik.lundin50499422016-11-29 04:26:24 -080043 rms_->Analyze(rtc::ArrayView<const int16_t>(audio->channels_const()[i],
44 audio->num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +000045 }
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000048int LevelEstimatorImpl::Enable(bool enable) {
peahdf3efa82015-11-28 12:35:15 -080049 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080050 if (enable && !enabled_) {
51 rms_->Reset();
52 }
53 enabled_ = enable;
54 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +000055}
56
57bool LevelEstimatorImpl::is_enabled() const {
peahdf3efa82015-11-28 12:35:15 -080058 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080059 return enabled_;
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
61
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000062int LevelEstimatorImpl::RMS() {
peahdf3efa82015-11-28 12:35:15 -080063 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080064 if (!enabled_) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000065 return AudioProcessing::kNotEnabledError;
niklase@google.com470e71d2011-07-07 08:21:25 +000066 }
67
henrik.lundin50499422016-11-29 04:26:24 -080068 return rms_->Average();
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
niklase@google.com470e71d2011-07-07 08:21:25 +000070} // namespace webrtc