blob: c937f8452534cea733b0c1f622858e7ea5ebe372 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "api/array_view.h"
14#include "modules/audio_processing/audio_buffer.h"
15#include "modules/audio_processing/rms_level.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
17namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000018
solenberg949028f2015-12-15 11:39:38 -080019LevelEstimatorImpl::LevelEstimatorImpl(rtc::CriticalSection* crit)
henrik.lundin50499422016-11-29 04:26:24 -080020 : crit_(crit), rms_(new RmsLevel()) {
peahdf3efa82015-11-28 12:35:15 -080021 RTC_DCHECK(crit);
22}
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24LevelEstimatorImpl::~LevelEstimatorImpl() {}
25
solenberg949028f2015-12-15 11:39:38 -080026void LevelEstimatorImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -080027 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080028 rms_->Reset();
29}
peahdf3efa82015-11-28 12:35:15 -080030
solenberg949028f2015-12-15 11:39:38 -080031void LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
32 RTC_DCHECK(audio);
33 rtc::CritScope cs(crit_);
34 if (!enabled_) {
35 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000036 }
37
Peter Kasting69558702016-01-12 16:26:35 -080038 for (size_t i = 0; i < audio->num_channels(); i++) {
henrik.lundin50499422016-11-29 04:26:24 -080039 rms_->Analyze(rtc::ArrayView<const int16_t>(audio->channels_const()[i],
40 audio->num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +000041 }
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000044int LevelEstimatorImpl::Enable(bool enable) {
peahdf3efa82015-11-28 12:35:15 -080045 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080046 if (enable && !enabled_) {
47 rms_->Reset();
48 }
49 enabled_ = enable;
50 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +000051}
52
53bool LevelEstimatorImpl::is_enabled() const {
peahdf3efa82015-11-28 12:35:15 -080054 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080055 return enabled_;
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000058int LevelEstimatorImpl::RMS() {
peahdf3efa82015-11-28 12:35:15 -080059 rtc::CritScope cs(crit_);
solenberg949028f2015-12-15 11:39:38 -080060 if (!enabled_) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000061 return AudioProcessing::kNotEnabledError;
niklase@google.com470e71d2011-07-07 08:21:25 +000062 }
63
henrik.lundin50499422016-11-29 04:26:24 -080064 return rms_->Average();
niklase@google.com470e71d2011-07-07 08:21:25 +000065}
niklase@google.com470e71d2011-07-07 08:21:25 +000066} // namespace webrtc