| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ | 11 #ifndef WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |
| 12 #define WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ | 12 #define WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |
| 13 | 13 |
| 14 #include <limits> | 14 #include <limits> |
| 15 #include <cstring> | |
| 16 | 15 |
| 17 #include "webrtc/base/checks.h" | |
| 18 #include "webrtc/base/scoped_ptr.h" | 16 #include "webrtc/base/scoped_ptr.h" |
| 19 #include "webrtc/typedefs.h" | 17 #include "webrtc/typedefs.h" |
| 20 | 18 |
| 21 namespace webrtc { | 19 namespace webrtc { |
| 22 | 20 |
| 23 typedef std::numeric_limits<int16_t> limits_int16; | 21 typedef std::numeric_limits<int16_t> limits_int16; |
| 24 | 22 |
| 25 // The conversion functions use the following naming convention: | 23 // The conversion functions use the following naming convention: |
| 26 // S16: int16_t [-32768, 32767] | 24 // S16: int16_t [-32768, 32767] |
| 27 // Float: float [-1.0, 1.0] | 25 // Float: float [-1.0, 1.0] |
| 28 // FloatS16: float [-32768.0, 32767.0] | 26 // FloatS16: float [-32768.0, 32767.0] |
| 29 static inline int16_t FloatToS16(float v) { | 27 static inline int16_t FloatToS16(float v) { |
| 30 if (v > 0) | 28 if (v > 0) |
| 31 return v >= 1 ? limits_int16::max() | 29 return v >= 1 ? limits_int16::max() : |
| 32 : static_cast<int16_t>(v * limits_int16::max() + 0.5f); | 30 static_cast<int16_t>(v * limits_int16::max() + 0.5f); |
| 33 return v <= -1 ? limits_int16::min() | 31 return v <= -1 ? limits_int16::min() : |
| 34 : static_cast<int16_t>(-v * limits_int16::min() - 0.5f); | 32 static_cast<int16_t>(-v * limits_int16::min() - 0.5f); |
| 35 } | 33 } |
| 36 | 34 |
| 37 static inline float S16ToFloat(int16_t v) { | 35 static inline float S16ToFloat(int16_t v) { |
| 38 static const float kMaxInt16Inverse = 1.f / limits_int16::max(); | 36 static const float kMaxInt16Inverse = 1.f / limits_int16::max(); |
| 39 static const float kMinInt16Inverse = 1.f / limits_int16::min(); | 37 static const float kMinInt16Inverse = 1.f / limits_int16::min(); |
| 40 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); | 38 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); |
| 41 } | 39 } |
| 42 | 40 |
| 43 static inline int16_t FloatS16ToS16(float v) { | 41 static inline int16_t FloatS16ToS16(float v) { |
| 44 static const float kMaxRound = limits_int16::max() - 0.5f; | 42 static const float kMaxRound = limits_int16::max() - 0.5f; |
| 45 static const float kMinRound = limits_int16::min() + 0.5f; | 43 static const float kMinRound = limits_int16::min() + 0.5f; |
| 46 if (v > 0) | 44 if (v > 0) |
| 47 return v >= kMaxRound ? limits_int16::max() | 45 return v >= kMaxRound ? limits_int16::max() : |
| 48 : static_cast<int16_t>(v + 0.5f); | 46 static_cast<int16_t>(v + 0.5f); |
| 49 return v <= kMinRound ? limits_int16::min() : static_cast<int16_t>(v - 0.5f); | 47 return v <= kMinRound ? limits_int16::min() : |
| 48 static_cast<int16_t>(v - 0.5f); |
| 50 } | 49 } |
| 51 | 50 |
| 52 static inline float FloatToFloatS16(float v) { | 51 static inline float FloatToFloatS16(float v) { |
| 53 return v * (v > 0 ? limits_int16::max() : -limits_int16::min()); | 52 return v * (v > 0 ? limits_int16::max() : -limits_int16::min()); |
| 54 } | 53 } |
| 55 | 54 |
| 56 static inline float FloatS16ToFloat(float v) { | 55 static inline float FloatS16ToFloat(float v) { |
| 57 static const float kMaxInt16Inverse = 1.f / limits_int16::max(); | 56 static const float kMaxInt16Inverse = 1.f / limits_int16::max(); |
| 58 static const float kMinInt16Inverse = 1.f / limits_int16::min(); | 57 static const float kMinInt16Inverse = 1.f / limits_int16::min(); |
| 59 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); | 58 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); |
| 60 } | 59 } |
| 61 | 60 |
| 62 void FloatToS16(const float* src, size_t size, int16_t* dest); | 61 void FloatToS16(const float* src, size_t size, int16_t* dest); |
| 63 void S16ToFloat(const int16_t* src, size_t size, float* dest); | 62 void S16ToFloat(const int16_t* src, size_t size, float* dest); |
| 64 void FloatS16ToS16(const float* src, size_t size, int16_t* dest); | 63 void FloatS16ToS16(const float* src, size_t size, int16_t* dest); |
| 65 void FloatToFloatS16(const float* src, size_t size, float* dest); | 64 void FloatToFloatS16(const float* src, size_t size, float* dest); |
| 66 void FloatS16ToFloat(const float* src, size_t size, float* dest); | 65 void FloatS16ToFloat(const float* src, size_t size, float* dest); |
| 67 | 66 |
| 68 // Deinterleave audio from |interleaved| to the channel buffers pointed to | 67 // Deinterleave audio from |interleaved| to the channel buffers pointed to |
| 69 // by |deinterleaved|. There must be sufficient space allocated in the | 68 // by |deinterleaved|. There must be sufficient space allocated in the |
| 70 // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel| | 69 // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel| |
| 71 // per buffer). | 70 // per buffer). |
| 72 template <typename T> | 71 template <typename T> |
| 73 void Deinterleave(const T* interleaved, | 72 void Deinterleave(const T* interleaved, int samples_per_channel, |
| 74 int samples_per_channel, | 73 int num_channels, T* const* deinterleaved) { |
| 75 int num_channels, | |
| 76 T* const* deinterleaved) { | |
| 77 for (int i = 0; i < num_channels; ++i) { | 74 for (int i = 0; i < num_channels; ++i) { |
| 78 T* channel = deinterleaved[i]; | 75 T* channel = deinterleaved[i]; |
| 79 int interleaved_idx = i; | 76 int interleaved_idx = i; |
| 80 for (int j = 0; j < samples_per_channel; ++j) { | 77 for (int j = 0; j < samples_per_channel; ++j) { |
| 81 channel[j] = interleaved[interleaved_idx]; | 78 channel[j] = interleaved[interleaved_idx]; |
| 82 interleaved_idx += num_channels; | 79 interleaved_idx += num_channels; |
| 83 } | 80 } |
| 84 } | 81 } |
| 85 } | 82 } |
| 86 | 83 |
| 87 // Interleave audio from the channel buffers pointed to by |deinterleaved| to | 84 // Interleave audio from the channel buffers pointed to by |deinterleaved| to |
| 88 // |interleaved|. There must be sufficient space allocated in |interleaved| | 85 // |interleaved|. There must be sufficient space allocated in |interleaved| |
| 89 // (|samples_per_channel| * |num_channels|). | 86 // (|samples_per_channel| * |num_channels|). |
| 90 template <typename T> | 87 template <typename T> |
| 91 void Interleave(const T* const* deinterleaved, | 88 void Interleave(const T* const* deinterleaved, int samples_per_channel, |
| 92 int samples_per_channel, | 89 int num_channels, T* interleaved) { |
| 93 int num_channels, | |
| 94 T* interleaved) { | |
| 95 for (int i = 0; i < num_channels; ++i) { | 90 for (int i = 0; i < num_channels; ++i) { |
| 96 const T* channel = deinterleaved[i]; | 91 const T* channel = deinterleaved[i]; |
| 97 int interleaved_idx = i; | 92 int interleaved_idx = i; |
| 98 for (int j = 0; j < samples_per_channel; ++j) { | 93 for (int j = 0; j < samples_per_channel; ++j) { |
| 99 interleaved[interleaved_idx] = channel[j]; | 94 interleaved[interleaved_idx] = channel[j]; |
| 100 interleaved_idx += num_channels; | 95 interleaved_idx += num_channels; |
| 101 } | 96 } |
| 102 } | 97 } |
| 103 } | 98 } |
| 104 | 99 |
| 105 template <typename T, typename Intermediate> | |
| 106 void DownmixToMono(const T* const* input_channels, | |
| 107 int num_frames, | |
| 108 int num_channels, | |
| 109 T* out) { | |
| 110 for (int i = 0; i < num_frames; ++i) { | |
| 111 Intermediate value = input_channels[0][i]; | |
| 112 for (int j = 1; j < num_channels; ++j) { | |
| 113 value += input_channels[j][i]; | |
| 114 } | |
| 115 out[i] = value / num_channels; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // Downmixes an interleaved multichannel signal to a single channel by averaging | |
| 120 // all channels. | |
| 121 template <typename T, typename Intermediate> | |
| 122 void DownmixInterleavedToMonoImpl(const T* interleaved, | |
| 123 int num_frames, | |
| 124 int num_channels, | |
| 125 T* deinterleaved) { | |
| 126 DCHECK_GT(num_channels, 0); | |
| 127 DCHECK_GT(num_frames, 0); | |
| 128 | |
| 129 const T* const end = interleaved + num_frames * num_channels; | |
| 130 | |
| 131 while (interleaved < end) { | |
| 132 const T* const frame_end = interleaved + num_channels; | |
| 133 | |
| 134 Intermediate value = *interleaved++; | |
| 135 while (interleaved < frame_end) { | |
| 136 value += *interleaved++; | |
| 137 } | |
| 138 | |
| 139 *deinterleaved++ = value / num_channels; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 template <typename T> | |
| 144 void DownmixInterleavedToMono(const T* interleaved, | |
| 145 int num_frames, | |
| 146 int num_channels, | |
| 147 T* deinterleaved); | |
| 148 | |
| 149 template <> | |
| 150 void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved, | |
| 151 int num_frames, | |
| 152 int num_channels, | |
| 153 int16_t* deinterleaved); | |
| 154 | |
| 155 } // namespace webrtc | 100 } // namespace webrtc |
| 156 | 101 |
| 157 #endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ | 102 #endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |
| OLD | NEW |