2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-01-30 09:39:08 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-28 08:11:59 +00:00
|
|
|
#include "webrtc/modules/audio_processing/level_estimator_impl.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-05-28 08:11:59 +00:00
|
|
|
#include "webrtc/modules/audio_processing/audio_buffer.h"
|
2014-05-14 19:00:59 +00:00
|
|
|
#include "webrtc/modules/audio_processing/rms_level.h"
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2015-12-15 11:39:38 -08:00
|
|
|
LevelEstimatorImpl::LevelEstimatorImpl(rtc::CriticalSection* crit)
|
|
|
|
|
: crit_(crit), rms_(new RMSLevel()) {
|
2015-11-28 12:35:15 -08:00
|
|
|
RTC_DCHECK(crit);
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
LevelEstimatorImpl::~LevelEstimatorImpl() {}
|
|
|
|
|
|
2015-12-15 11:39:38 -08:00
|
|
|
void LevelEstimatorImpl::Initialize() {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_);
|
2015-12-15 11:39:38 -08:00
|
|
|
rms_->Reset();
|
|
|
|
|
}
|
2015-11-28 12:35:15 -08:00
|
|
|
|
2015-12-15 11:39:38 -08:00
|
|
|
void LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
|
|
|
|
|
RTC_DCHECK(audio);
|
|
|
|
|
rtc::CritScope cs(crit_);
|
|
|
|
|
if (!enabled_) {
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
for (size_t i = 0; i < audio->num_channels(); i++) {
|
2015-12-15 11:39:38 -08:00
|
|
|
rms_->Process(audio->channels_const()[i], audio->num_frames());
|
2011-11-15 16:57:56 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int LevelEstimatorImpl::Enable(bool enable) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_);
|
2015-12-15 11:39:38 -08:00
|
|
|
if (enable && !enabled_) {
|
|
|
|
|
rms_->Reset();
|
|
|
|
|
}
|
|
|
|
|
enabled_ = enable;
|
|
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LevelEstimatorImpl::is_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_);
|
2015-12-15 11:39:38 -08:00
|
|
|
return enabled_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
int LevelEstimatorImpl::RMS() {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_);
|
2015-12-15 11:39:38 -08:00
|
|
|
if (!enabled_) {
|
2014-05-05 18:22:21 +00:00
|
|
|
return AudioProcessing::kNotEnabledError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-15 11:39:38 -08:00
|
|
|
return rms_->RMS();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
} // namespace webrtc
|