Home | History | Annotate | Download | only in audio_processing
      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 
     11 #include "webrtc/modules/audio_processing/level_estimator_impl.h"
     12 
     13 #include "webrtc/modules/audio_processing/audio_buffer.h"
     14 #include "webrtc/modules/audio_processing/include/audio_processing.h"
     15 #include "webrtc/modules/audio_processing/rms_level.h"
     16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     17 
     18 namespace webrtc {
     19 
     20 LevelEstimatorImpl::LevelEstimatorImpl(const AudioProcessing* apm,
     21                                        CriticalSectionWrapper* crit)
     22     : ProcessingComponent(),
     23       crit_(crit) {}
     24 
     25 LevelEstimatorImpl::~LevelEstimatorImpl() {}
     26 
     27 int LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
     28   if (!is_component_enabled()) {
     29     return AudioProcessing::kNoError;
     30   }
     31 
     32   RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0));
     33   for (int i = 0; i < audio->num_channels(); ++i) {
     34     rms_level->Process(audio->data(i), audio->samples_per_channel());
     35   }
     36 
     37   return AudioProcessing::kNoError;
     38 }
     39 
     40 int LevelEstimatorImpl::Enable(bool enable) {
     41   CriticalSectionScoped crit_scoped(crit_);
     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()) {
     51     return AudioProcessing::kNotEnabledError;
     52   }
     53 
     54   RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0));
     55   return rms_level->RMS();
     56 }
     57 
     58 // The ProcessingComponent implementation is pretty weird in this class since
     59 // we have only a single instance of the trivial underlying component.
     60 void* LevelEstimatorImpl::CreateHandle() const {
     61   return new RMSLevel;
     62 }
     63 
     64 void LevelEstimatorImpl::DestroyHandle(void* handle) const {
     65   delete static_cast<RMSLevel*>(handle);
     66 }
     67 
     68 int LevelEstimatorImpl::InitializeHandle(void* handle) const {
     69   static_cast<RMSLevel*>(handle)->Reset();
     70   return AudioProcessing::kNoError;
     71 }
     72 
     73 int LevelEstimatorImpl::ConfigureHandle(void* /*handle*/) const {
     74   return AudioProcessing::kNoError;
     75 }
     76 
     77 int LevelEstimatorImpl::num_handles_required() const {
     78   return 1;
     79 }
     80 
     81 int LevelEstimatorImpl::GetHandleError(void* /*handle*/) const {
     82   return AudioProcessing::kUnspecifiedError;
     83 }
     84 
     85 }  // namespace webrtc
     86