blob: cfe295a6a0b129e241da0d2306cce23e3ec088b3 [file] [log] [blame]
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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
pbos@webrtc.org382c8b32013-05-28 08:11:59 +000011#include "webrtc/modules/audio_processing/level_estimator_impl.h"
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000012
pbos@webrtc.org382c8b32013-05-28 08:11:59 +000013#include "webrtc/modules/audio_processing/audio_buffer.h"
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000014#include "webrtc/modules/audio_processing/include/audio_processing.h"
andrew@webrtc.org5ec8fee2014-05-14 19:00:59 +000015#include "webrtc/modules/audio_processing/rms_level.h"
pbos@webrtc.org382c8b32013-05-28 08:11:59 +000016#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000017
18namespace webrtc {
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000019
andrew@webrtc.org873f3572014-02-27 22:23:17 +000020LevelEstimatorImpl::LevelEstimatorImpl(const AudioProcessing* apm,
21 CriticalSectionWrapper* crit)
22 : ProcessingComponent(),
andrew@webrtc.org873f3572014-02-27 22:23:17 +000023 crit_(crit) {}
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000024
25LevelEstimatorImpl::~LevelEstimatorImpl() {}
26
27int LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
28 if (!is_component_enabled()) {
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000029 return AudioProcessing::kNoError;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000030 }
31
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000032 RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0));
andrew@webrtc.org5ec8fee2014-05-14 19:00:59 +000033 for (int i = 0; i < audio->num_channels(); ++i) {
34 rms_level->Process(audio->data(i), audio->samples_per_channel());
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000035 }
36
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000037 return AudioProcessing::kNoError;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000038}
39
40int LevelEstimatorImpl::Enable(bool enable) {
andrew@webrtc.org873f3572014-02-27 22:23:17 +000041 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000042 return EnableComponent(enable);
43}
44
45bool LevelEstimatorImpl::is_enabled() const {
46 return is_component_enabled();
47}
48
49int LevelEstimatorImpl::RMS() {
50 if (!is_component_enabled()) {
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000051 return AudioProcessing::kNotEnabledError;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000052 }
53
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000054 RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0));
55 return rms_level->RMS();
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000056}
57
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000058// The ProcessingComponent implementation is pretty weird in this class since
59// we have only a single instance of the trivial underlying component.
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000060void* LevelEstimatorImpl::CreateHandle() const {
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000061 return new RMSLevel;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000062}
63
bjornv@webrtc.org17d096a2014-04-22 06:52:28 +000064void LevelEstimatorImpl::DestroyHandle(void* handle) const {
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000065 delete static_cast<RMSLevel*>(handle);
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000066}
67
68int LevelEstimatorImpl::InitializeHandle(void* handle) const {
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000069 static_cast<RMSLevel*>(handle)->Reset();
70 return AudioProcessing::kNoError;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000071}
72
73int LevelEstimatorImpl::ConfigureHandle(void* /*handle*/) const {
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000074 return AudioProcessing::kNoError;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000075}
76
77int LevelEstimatorImpl::num_handles_required() const {
78 return 1;
79}
80
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000081int LevelEstimatorImpl::GetHandleError(void* /*handle*/) const {
82 return AudioProcessing::kUnspecifiedError;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000083}
andrew@webrtc.org8ec46c62014-05-05 18:22:21 +000084
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000085} // namespace webrtc