andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 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 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 14 | #include <limits> |
| 15 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 16 | #include "webrtc/base/scoped_ptr.h" |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 17 | #include "webrtc/typedefs.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 21 | typedef std::numeric_limits<int16_t> limits_int16; |
| 22 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 23 | // The conversion functions use the following naming convention: |
| 24 | // S16: int16_t [-32768, 32767] |
| 25 | // Float: float [-1.0, 1.0] |
| 26 | // FloatS16: float [-32768.0, 32767.0] |
| 27 | static inline int16_t FloatToS16(float v) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 28 | if (v > 0) |
magjed | 64e753c | 2015-07-23 04:30:06 -0700 | [diff] [blame] | 29 | return v >= 1 ? limits_int16::max() : |
| 30 | static_cast<int16_t>(v * limits_int16::max() + 0.5f); |
| 31 | return v <= -1 ? limits_int16::min() : |
| 32 | static_cast<int16_t>(-v * limits_int16::min() - 0.5f); |
turaj@webrtc.org | d4d5be8 | 2014-02-20 20:55:21 +0000 | [diff] [blame] | 33 | } |
| 34 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 35 | static inline float S16ToFloat(int16_t v) { |
| 36 | static const float kMaxInt16Inverse = 1.f / limits_int16::max(); |
| 37 | static const float kMinInt16Inverse = 1.f / limits_int16::min(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 38 | return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 39 | } |
| 40 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 41 | static inline int16_t FloatS16ToS16(float v) { |
| 42 | static const float kMaxRound = limits_int16::max() - 0.5f; |
| 43 | static const float kMinRound = limits_int16::min() + 0.5f; |
| 44 | if (v > 0) |
magjed | 64e753c | 2015-07-23 04:30:06 -0700 | [diff] [blame] | 45 | return v >= kMaxRound ? limits_int16::max() : |
| 46 | static_cast<int16_t>(v + 0.5f); |
| 47 | return v <= kMinRound ? limits_int16::min() : |
| 48 | static_cast<int16_t>(v - 0.5f); |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 49 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 50 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 51 | static inline float FloatToFloatS16(float v) { |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 52 | return v * (v > 0 ? limits_int16::max() : -limits_int16::min()); |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 53 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 54 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 55 | static inline float FloatS16ToFloat(float v) { |
| 56 | static const float kMaxInt16Inverse = 1.f / limits_int16::max(); |
| 57 | static const float kMinInt16Inverse = 1.f / limits_int16::min(); |
| 58 | return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); |
| 59 | } |
| 60 | |
| 61 | void FloatToS16(const float* src, size_t size, int16_t* dest); |
| 62 | void S16ToFloat(const int16_t* src, size_t size, float* dest); |
| 63 | void FloatS16ToS16(const float* src, size_t size, int16_t* dest); |
| 64 | void FloatToFloatS16(const float* src, size_t size, float* dest); |
| 65 | void FloatS16ToFloat(const float* src, size_t size, float* dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 66 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 67 | // Deinterleave audio from |interleaved| to the channel buffers pointed to |
| 68 | // by |deinterleaved|. There must be sufficient space allocated in the |
| 69 | // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel| |
| 70 | // per buffer). |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 71 | template <typename T> |
magjed | 64e753c | 2015-07-23 04:30:06 -0700 | [diff] [blame] | 72 | void Deinterleave(const T* interleaved, int samples_per_channel, |
| 73 | int num_channels, T* const* deinterleaved) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 74 | for (int i = 0; i < num_channels; ++i) { |
| 75 | T* channel = deinterleaved[i]; |
| 76 | int interleaved_idx = i; |
| 77 | for (int j = 0; j < samples_per_channel; ++j) { |
| 78 | channel[j] = interleaved[interleaved_idx]; |
| 79 | interleaved_idx += num_channels; |
| 80 | } |
| 81 | } |
| 82 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 83 | |
| 84 | // Interleave audio from the channel buffers pointed to by |deinterleaved| to |
| 85 | // |interleaved|. There must be sufficient space allocated in |interleaved| |
| 86 | // (|samples_per_channel| * |num_channels|). |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 87 | template <typename T> |
magjed | 64e753c | 2015-07-23 04:30:06 -0700 | [diff] [blame] | 88 | void Interleave(const T* const* deinterleaved, int samples_per_channel, |
| 89 | int num_channels, T* interleaved) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 90 | for (int i = 0; i < num_channels; ++i) { |
| 91 | const T* channel = deinterleaved[i]; |
| 92 | int interleaved_idx = i; |
| 93 | for (int j = 0; j < samples_per_channel; ++j) { |
| 94 | interleaved[interleaved_idx] = channel[j]; |
| 95 | interleaved_idx += num_channels; |
| 96 | } |
| 97 | } |
| 98 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 99 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 100 | } // namespace webrtc |
| 101 | |
| 102 | #endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |