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