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> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 15 | #include <cstring> |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 16 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 18 | #include "webrtc/typedefs.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 22 | typedef std::numeric_limits<int16_t> limits_int16; |
| 23 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 24 | // The conversion functions use the following naming convention: |
| 25 | // S16: int16_t [-32768, 32767] |
| 26 | // Float: float [-1.0, 1.0] |
| 27 | // FloatS16: float [-32768.0, 32767.0] |
| 28 | static inline int16_t FloatToS16(float v) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 29 | if (v > 0) |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 30 | return v >= 1 ? limits_int16::max() |
| 31 | : static_cast<int16_t>(v * limits_int16::max() + 0.5f); |
| 32 | return v <= -1 ? limits_int16::min() |
| 33 | : static_cast<int16_t>(-v * limits_int16::min() - 0.5f); |
turaj@webrtc.org | d4d5be8 | 2014-02-20 20:55:21 +0000 | [diff] [blame] | 34 | } |
| 35 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 36 | static inline float S16ToFloat(int16_t v) { |
| 37 | static const float kMaxInt16Inverse = 1.f / limits_int16::max(); |
| 38 | static const float kMinInt16Inverse = 1.f / limits_int16::min(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 39 | return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 40 | } |
| 41 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 42 | static inline int16_t FloatS16ToS16(float v) { |
| 43 | static const float kMaxRound = limits_int16::max() - 0.5f; |
| 44 | static const float kMinRound = limits_int16::min() + 0.5f; |
| 45 | if (v > 0) |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 46 | return v >= kMaxRound ? limits_int16::max() |
| 47 | : static_cast<int16_t>(v + 0.5f); |
| 48 | return v <= kMinRound ? limits_int16::min() : 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 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 67 | // Copy audio from |src| channels to |dest| channels unless |src| and |dest| |
| 68 | // point to the same address. |src| and |dest| must have the same number of |
| 69 | // channels, and there must be sufficient space allocated in |dest|. |
| 70 | template <typename T> |
| 71 | void CopyAudioIfNeeded(const T* const* src, |
| 72 | int num_frames, |
| 73 | int num_channels, |
| 74 | T* const* dest) { |
| 75 | for (int i = 0; i < num_channels; ++i) { |
| 76 | if (src[i] != dest[i]) { |
| 77 | std::copy(src[i], src[i] + num_frames, dest[i]); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 82 | // Deinterleave audio from |interleaved| to the channel buffers pointed to |
| 83 | // by |deinterleaved|. There must be sufficient space allocated in the |
| 84 | // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel| |
| 85 | // per buffer). |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 86 | template <typename T> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 87 | void Deinterleave(const T* interleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 88 | size_t samples_per_channel, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 89 | size_t num_channels, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 90 | T* const* deinterleaved) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 91 | for (size_t i = 0; i < num_channels; ++i) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 92 | T* channel = deinterleaved[i]; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 93 | size_t interleaved_idx = i; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 94 | for (size_t j = 0; j < samples_per_channel; ++j) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 95 | channel[j] = interleaved[interleaved_idx]; |
| 96 | interleaved_idx += num_channels; |
| 97 | } |
| 98 | } |
| 99 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 100 | |
| 101 | // Interleave audio from the channel buffers pointed to by |deinterleaved| to |
| 102 | // |interleaved|. There must be sufficient space allocated in |interleaved| |
| 103 | // (|samples_per_channel| * |num_channels|). |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 104 | template <typename T> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 105 | void Interleave(const T* const* deinterleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 106 | size_t samples_per_channel, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 107 | size_t num_channels, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 108 | T* interleaved) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 109 | for (size_t i = 0; i < num_channels; ++i) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 110 | const T* channel = deinterleaved[i]; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 111 | size_t interleaved_idx = i; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 112 | for (size_t j = 0; j < samples_per_channel; ++j) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 113 | interleaved[interleaved_idx] = channel[j]; |
| 114 | interleaved_idx += num_channels; |
| 115 | } |
| 116 | } |
| 117 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 118 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 119 | // Copies audio from a single channel buffer pointed to by |mono| to each |
| 120 | // channel of |interleaved|. There must be sufficient space allocated in |
| 121 | // |interleaved| (|samples_per_channel| * |num_channels|). |
| 122 | template <typename T> |
| 123 | void UpmixMonoToInterleaved(const T* mono, |
| 124 | int num_frames, |
| 125 | int num_channels, |
| 126 | T* interleaved) { |
| 127 | int interleaved_idx = 0; |
| 128 | for (int i = 0; i < num_frames; ++i) { |
| 129 | for (int j = 0; j < num_channels; ++j) { |
| 130 | interleaved[interleaved_idx++] = mono[i]; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 135 | template <typename T, typename Intermediate> |
| 136 | void DownmixToMono(const T* const* input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 137 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 138 | int num_channels, |
| 139 | T* out) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 140 | for (size_t i = 0; i < num_frames; ++i) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 141 | Intermediate value = input_channels[0][i]; |
| 142 | for (int j = 1; j < num_channels; ++j) { |
| 143 | value += input_channels[j][i]; |
| 144 | } |
| 145 | out[i] = value / num_channels; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Downmixes an interleaved multichannel signal to a single channel by averaging |
| 150 | // all channels. |
| 151 | template <typename T, typename Intermediate> |
| 152 | void DownmixInterleavedToMonoImpl(const T* interleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 153 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 154 | int num_channels, |
| 155 | T* deinterleaved) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 156 | RTC_DCHECK_GT(num_channels, 0); |
| 157 | RTC_DCHECK_GT(num_frames, 0u); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 158 | |
| 159 | const T* const end = interleaved + num_frames * num_channels; |
| 160 | |
| 161 | while (interleaved < end) { |
| 162 | const T* const frame_end = interleaved + num_channels; |
| 163 | |
| 164 | Intermediate value = *interleaved++; |
| 165 | while (interleaved < frame_end) { |
| 166 | value += *interleaved++; |
| 167 | } |
| 168 | |
| 169 | *deinterleaved++ = value / num_channels; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | template <typename T> |
| 174 | void DownmixInterleavedToMono(const T* interleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 175 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 176 | int num_channels, |
| 177 | T* deinterleaved); |
| 178 | |
| 179 | template <> |
| 180 | void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 181 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 182 | int num_channels, |
| 183 | int16_t* deinterleaved); |
| 184 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 185 | } // namespace webrtc |
| 186 | |
| 187 | #endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |