Home | History | Annotate | Download | only in include
      1 /*
      2  *  Copyright (c) 2013 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_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
     12 #define WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
     13 
     14 #include <limits>
     15 
     16 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     17 #include "webrtc/typedefs.h"
     18 
     19 namespace webrtc {
     20 
     21 typedef std::numeric_limits<int16_t> limits_int16;
     22 
     23 static inline int16_t RoundToInt16(float v) {
     24   const float kMaxRound = limits_int16::max() - 0.5f;
     25   const float kMinRound = limits_int16::min() + 0.5f;
     26   if (v > 0)
     27     return v >= kMaxRound ? limits_int16::max() :
     28                             static_cast<int16_t>(v + 0.5f);
     29   return v <= kMinRound ? limits_int16::min() :
     30                           static_cast<int16_t>(v - 0.5f);
     31 }
     32 
     33 // Scale (from [-1, 1]) and round to full-range int16 with clamping.
     34 static inline int16_t ScaleAndRoundToInt16(float v) {
     35   if (v > 0)
     36     return v >= 1 ? limits_int16::max() :
     37                     static_cast<int16_t>(v * limits_int16::max() + 0.5f);
     38   return v <= -1 ? limits_int16::min() :
     39                    static_cast<int16_t>(-v * limits_int16::min() - 0.5f);
     40 }
     41 
     42 // Scale to float [-1, 1].
     43 static inline float ScaleToFloat(int16_t v) {
     44   const float kMaxInt16Inverse = 1.f / limits_int16::max();
     45   const float kMinInt16Inverse = 1.f / limits_int16::min();
     46   return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
     47 }
     48 
     49 // Round |size| elements of |src| to int16 with clamping and write to |dest|.
     50 void RoundToInt16(const float* src, size_t size, int16_t* dest);
     51 
     52 // Scale (from [-1, 1]) and round |size| elements of |src| to full-range int16
     53 // with clamping and write to |dest|.
     54 void ScaleAndRoundToInt16(const float* src, size_t size, int16_t* dest);
     55 
     56 // Scale |size| elements of |src| to float [-1, 1] and write to |dest|.
     57 void ScaleToFloat(const int16_t* src, size_t size, float* dest);
     58 
     59 // Deinterleave audio from |interleaved| to the channel buffers pointed to
     60 // by |deinterleaved|. There must be sufficient space allocated in the
     61 // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
     62 // per buffer).
     63 template <typename T>
     64 void Deinterleave(const T* interleaved, int samples_per_channel,
     65                   int num_channels, T* const* deinterleaved) {
     66   for (int i = 0; i < num_channels; ++i) {
     67     T* channel = deinterleaved[i];
     68     int interleaved_idx = i;
     69     for (int j = 0; j < samples_per_channel; ++j) {
     70       channel[j] = interleaved[interleaved_idx];
     71       interleaved_idx += num_channels;
     72     }
     73   }
     74 }
     75 
     76 // Interleave audio from the channel buffers pointed to by |deinterleaved| to
     77 // |interleaved|. There must be sufficient space allocated in |interleaved|
     78 // (|samples_per_channel| * |num_channels|).
     79 template <typename T>
     80 void Interleave(const T* const* deinterleaved, int samples_per_channel,
     81                 int num_channels, T* interleaved) {
     82   for (int i = 0; i < num_channels; ++i) {
     83     const T* channel = deinterleaved[i];
     84     int interleaved_idx = i;
     85     for (int j = 0; j < samples_per_channel; ++j) {
     86       interleaved[interleaved_idx] = channel[j];
     87       interleaved_idx += num_channels;
     88     }
     89   }
     90 }
     91 
     92 }  // namespace webrtc
     93 
     94 #endif  // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
     95