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 | |
| 16 | #include "webrtc/system_wrappers/interface/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 | |
| 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); |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 31 | } |
| 32 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 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); |
turaj@webrtc.org | d4d5be8 | 2014-02-20 20:55:21 +0000 | [diff] [blame] | 40 | } |
| 41 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 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); |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 47 | } |
| 48 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 49 | // Round |size| elements of |src| to int16 with clamping and write to |dest|. |
kwiberg@webrtc.org | efb81d8 | 2014-07-16 08:36:52 +0000 | [diff] [blame] | 50 | void RoundToInt16(const float* src, size_t size, int16_t* dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 51 | |
| 52 | // Scale (from [-1, 1]) and round |size| elements of |src| to full-range int16 |
| 53 | // with clamping and write to |dest|. |
kwiberg@webrtc.org | efb81d8 | 2014-07-16 08:36:52 +0000 | [diff] [blame] | 54 | void ScaleAndRoundToInt16(const float* src, size_t size, int16_t* dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 55 | |
| 56 | // Scale |size| elements of |src| to float [-1, 1] and write to |dest|. |
kwiberg@webrtc.org | efb81d8 | 2014-07-16 08:36:52 +0000 | [diff] [blame] | 57 | void ScaleToFloat(const int16_t* src, size_t size, float* dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 58 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 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). |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 63 | template <typename T> |
| 64 | void Deinterleave(const T* interleaved, int samples_per_channel, |
| 65 | int num_channels, T** 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 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 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|). |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 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 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 91 | |
| 92 | } // namespace webrtc |
| 93 | |
| 94 | #endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |