Home | History | Annotate | Download | only in audio_processing
      1 /*
      2  *  Copyright (c) 2014 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 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
     12 #define WEBRTC_MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
     13 
     14 #include <cstddef>
     15 
     16 #include "webrtc/typedefs.h"
     17 
     18 namespace webrtc {
     19 
     20 // Computes the root mean square (RMS) level in dBFs (decibels from digital
     21 // full-scale) of audio data. The computation follows RFC 6465:
     22 // https://tools.ietf.org/html/rfc6465
     23 // with the intent that it can provide the RTP audio level indication.
     24 //
     25 // The expected approach is to provide constant-sized chunks of audio to
     26 // Process(). When enough chunks have been accumulated to form a packet, call
     27 // RMS() to get the audio level indicator for the RTP header.
     28 class RMSLevel {
     29  public:
     30   static const int kMinLevel = 127;
     31 
     32   RMSLevel();
     33   ~RMSLevel();
     34 
     35   // Can be called to reset internal states, but is not required during normal
     36   // operation.
     37   void Reset();
     38 
     39   // Pass each chunk of audio to Process() to accumulate the level.
     40   void Process(const int16_t* data, size_t length);
     41 
     42   // If all samples with the given |length| have a magnitude of zero, this is
     43   // a shortcut to avoid some computation.
     44   void ProcessMuted(size_t length);
     45 
     46   // Computes the RMS level over all data passed to Process() since the last
     47   // call to RMS(). The returned value is positive but should be interpreted as
     48   // negative as per the RFC. It is constrained to [0, 127].
     49   int RMS();
     50 
     51  private:
     52   float sum_square_;
     53   size_t sample_count_;
     54 };
     55 
     56 }  // namespace webrtc
     57 
     58 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_RMS_LEVEL_H_
     59 
     60