andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 382c8b3 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/level_estimator_impl.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 382c8b3 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 13 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 14 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
andrew@webrtc.org | 5ec8fee | 2014-05-14 19:00:59 +0000 | [diff] [blame^] | 15 | #include "webrtc/modules/audio_processing/rms_level.h" |
pbos@webrtc.org | 382c8b3 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 19 | |
andrew@webrtc.org | 873f357 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 20 | LevelEstimatorImpl::LevelEstimatorImpl(const AudioProcessing* apm, |
| 21 | CriticalSectionWrapper* crit) |
| 22 | : ProcessingComponent(), |
andrew@webrtc.org | 873f357 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 23 | crit_(crit) {} |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 24 | |
| 25 | LevelEstimatorImpl::~LevelEstimatorImpl() {} |
| 26 | |
| 27 | int LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) { |
| 28 | if (!is_component_enabled()) { |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 29 | return AudioProcessing::kNoError; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 30 | } |
| 31 | |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 32 | RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0)); |
andrew@webrtc.org | 5ec8fee | 2014-05-14 19:00:59 +0000 | [diff] [blame^] | 33 | for (int i = 0; i < audio->num_channels(); ++i) { |
| 34 | rms_level->Process(audio->data(i), audio->samples_per_channel()); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 35 | } |
| 36 | |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 37 | return AudioProcessing::kNoError; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | int LevelEstimatorImpl::Enable(bool enable) { |
andrew@webrtc.org | 873f357 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 41 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 42 | return EnableComponent(enable); |
| 43 | } |
| 44 | |
| 45 | bool LevelEstimatorImpl::is_enabled() const { |
| 46 | return is_component_enabled(); |
| 47 | } |
| 48 | |
| 49 | int LevelEstimatorImpl::RMS() { |
| 50 | if (!is_component_enabled()) { |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 51 | return AudioProcessing::kNotEnabledError; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 52 | } |
| 53 | |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 54 | RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0)); |
| 55 | return rms_level->RMS(); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 58 | // The ProcessingComponent implementation is pretty weird in this class since |
| 59 | // we have only a single instance of the trivial underlying component. |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 60 | void* LevelEstimatorImpl::CreateHandle() const { |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 61 | return new RMSLevel; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 62 | } |
| 63 | |
bjornv@webrtc.org | 17d096a | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 64 | void LevelEstimatorImpl::DestroyHandle(void* handle) const { |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 65 | delete static_cast<RMSLevel*>(handle); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int LevelEstimatorImpl::InitializeHandle(void* handle) const { |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 69 | static_cast<RMSLevel*>(handle)->Reset(); |
| 70 | return AudioProcessing::kNoError; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int LevelEstimatorImpl::ConfigureHandle(void* /*handle*/) const { |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 74 | return AudioProcessing::kNoError; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int LevelEstimatorImpl::num_handles_required() const { |
| 78 | return 1; |
| 79 | } |
| 80 | |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 81 | int LevelEstimatorImpl::GetHandleError(void* /*handle*/) const { |
| 82 | return AudioProcessing::kUnspecifiedError; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 83 | } |
andrew@webrtc.org | 8ec46c6 | 2014-05-05 18:22:21 +0000 | [diff] [blame] | 84 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 85 | } // namespace webrtc |