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 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 16 | #include <algorithm> |
Alex Loiko | 6df09f6 | 2018-02-16 10:42:48 +0100 | [diff] [blame] | 17 | #include <cmath> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 18 | #include <cstring> |
Alex Loiko | 6df09f6 | 2018-02-16 10:42:48 +0100 | [diff] [blame] | 19 | #include <limits> |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 25 | typedef std::numeric_limits<int16_t> limits_int16; |
| 26 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 27 | // The conversion functions use the following naming convention: |
| 28 | // S16: int16_t [-32768, 32767] |
| 29 | // Float: float [-1.0, 1.0] |
Per Åhgren | 62c174c | 2019-08-16 13:43:00 +0200 | [diff] [blame] | 30 | // FloatS16: float [-32768.0, 32768.0] |
Alex Loiko | 6df09f6 | 2018-02-16 10:42:48 +0100 | [diff] [blame] | 31 | // Dbfs: float [-20.0*log(10, 32768), 0] = [-90.3, 0] |
| 32 | // The ratio conversion functions use this naming convention: |
| 33 | // Ratio: float (0, +inf) |
| 34 | // Db: float (-inf, +inf) |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 35 | static inline float S16ToFloat(int16_t v) { |
Per Åhgren | 62c174c | 2019-08-16 13:43:00 +0200 | [diff] [blame] | 36 | constexpr float kScaling = 1.f / 32768.f; |
| 37 | return v * kScaling; |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 38 | } |
| 39 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 40 | static inline int16_t FloatS16ToS16(float v) { |
Per Åhgren | 62c174c | 2019-08-16 13:43:00 +0200 | [diff] [blame] | 41 | v = std::min(v, 32767.f); |
| 42 | v = std::max(v, -32768.f); |
| 43 | return static_cast<int16_t>(v + std::copysign(0.5f, v)); |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 44 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 45 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 46 | static inline float FloatToFloatS16(float v) { |
Per Åhgren | 62c174c | 2019-08-16 13:43:00 +0200 | [diff] [blame] | 47 | v = std::min(v, 1.f); |
| 48 | v = std::max(v, -1.f); |
| 49 | return v * 32768.f; |
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 FloatS16ToFloat(float v) { |
Per Åhgren | 62c174c | 2019-08-16 13:43:00 +0200 | [diff] [blame] | 53 | v = std::min(v, 32768.f); |
| 54 | v = std::max(v, -32768.f); |
| 55 | constexpr float kScaling = 1.f / 32768.f; |
| 56 | return v * kScaling; |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 57 | } |
| 58 | |
andrew@webrtc.org | 4fc4add | 2014-10-30 03:40:10 +0000 | [diff] [blame] | 59 | void S16ToFloat(const int16_t* src, size_t size, float* dest); |
| 60 | void FloatS16ToS16(const float* src, size_t size, int16_t* dest); |
| 61 | void FloatToFloatS16(const float* src, size_t size, float* dest); |
| 62 | void FloatS16ToFloat(const float* src, size_t size, float* dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 63 | |
Alex Loiko | 6df09f6 | 2018-02-16 10:42:48 +0100 | [diff] [blame] | 64 | inline float DbToRatio(float v) { |
| 65 | return std::pow(10.0f, v / 20.0f); |
| 66 | } |
| 67 | |
| 68 | inline float DbfsToFloatS16(float v) { |
| 69 | static constexpr float kMaximumAbsFloatS16 = -limits_int16::min(); |
| 70 | return DbToRatio(v) * kMaximumAbsFloatS16; |
| 71 | } |
| 72 | |
| 73 | inline float FloatS16ToDbfs(float v) { |
| 74 | RTC_DCHECK_GE(v, 0); |
| 75 | |
| 76 | // kMinDbfs is equal to -20.0 * log10(-limits_int16::min()) |
| 77 | static constexpr float kMinDbfs = -90.30899869919436f; |
| 78 | if (v <= 1.0f) { |
| 79 | return kMinDbfs; |
| 80 | } |
| 81 | // Equal to 20 * log10(v / (-limits_int16::min())) |
| 82 | return 20.0f * std::log10(v) + kMinDbfs; |
| 83 | } |
| 84 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 85 | // Copy audio from |src| channels to |dest| channels unless |src| and |dest| |
| 86 | // point to the same address. |src| and |dest| must have the same number of |
| 87 | // channels, and there must be sufficient space allocated in |dest|. |
| 88 | template <typename T> |
| 89 | void CopyAudioIfNeeded(const T* const* src, |
| 90 | int num_frames, |
| 91 | int num_channels, |
| 92 | T* const* dest) { |
| 93 | for (int i = 0; i < num_channels; ++i) { |
| 94 | if (src[i] != dest[i]) { |
| 95 | std::copy(src[i], src[i] + num_frames, dest[i]); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 100 | // Deinterleave audio from |interleaved| to the channel buffers pointed to |
| 101 | // by |deinterleaved|. There must be sufficient space allocated in the |
| 102 | // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel| |
| 103 | // per buffer). |
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 Deinterleave(const T* interleaved, |
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* const* deinterleaved) { |
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 | 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 | channel[j] = interleaved[interleaved_idx]; |
| 114 | interleaved_idx += num_channels; |
| 115 | } |
| 116 | } |
| 117 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 118 | |
| 119 | // Interleave audio from the channel buffers pointed to by |deinterleaved| to |
| 120 | // |interleaved|. There must be sufficient space allocated in |interleaved| |
| 121 | // (|samples_per_channel| * |num_channels|). |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 122 | template <typename T> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 123 | void Interleave(const T* const* deinterleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 124 | size_t samples_per_channel, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 125 | size_t num_channels, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 126 | T* interleaved) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 127 | for (size_t i = 0; i < num_channels; ++i) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 128 | const T* channel = deinterleaved[i]; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 129 | size_t interleaved_idx = i; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 130 | for (size_t j = 0; j < samples_per_channel; ++j) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 131 | interleaved[interleaved_idx] = channel[j]; |
| 132 | interleaved_idx += num_channels; |
| 133 | } |
| 134 | } |
| 135 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 136 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 137 | // Copies audio from a single channel buffer pointed to by |mono| to each |
| 138 | // channel of |interleaved|. There must be sufficient space allocated in |
| 139 | // |interleaved| (|samples_per_channel| * |num_channels|). |
| 140 | template <typename T> |
| 141 | void UpmixMonoToInterleaved(const T* mono, |
| 142 | int num_frames, |
| 143 | int num_channels, |
| 144 | T* interleaved) { |
| 145 | int interleaved_idx = 0; |
| 146 | for (int i = 0; i < num_frames; ++i) { |
| 147 | for (int j = 0; j < num_channels; ++j) { |
| 148 | interleaved[interleaved_idx++] = mono[i]; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 153 | template <typename T, typename Intermediate> |
| 154 | void DownmixToMono(const T* const* input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 155 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 156 | int num_channels, |
| 157 | T* out) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 158 | for (size_t i = 0; i < num_frames; ++i) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 159 | Intermediate value = input_channels[0][i]; |
| 160 | for (int j = 1; j < num_channels; ++j) { |
| 161 | value += input_channels[j][i]; |
| 162 | } |
| 163 | out[i] = value / num_channels; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Downmixes an interleaved multichannel signal to a single channel by averaging |
| 168 | // all channels. |
| 169 | template <typename T, typename Intermediate> |
| 170 | void DownmixInterleavedToMonoImpl(const T* interleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 171 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 172 | int num_channels, |
| 173 | T* deinterleaved) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 174 | RTC_DCHECK_GT(num_channels, 0); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 175 | RTC_DCHECK_GT(num_frames, 0); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 176 | |
| 177 | const T* const end = interleaved + num_frames * num_channels; |
| 178 | |
| 179 | while (interleaved < end) { |
| 180 | const T* const frame_end = interleaved + num_channels; |
| 181 | |
| 182 | Intermediate value = *interleaved++; |
| 183 | while (interleaved < frame_end) { |
| 184 | value += *interleaved++; |
| 185 | } |
| 186 | |
| 187 | *deinterleaved++ = value / num_channels; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | template <typename T> |
| 192 | void DownmixInterleavedToMono(const T* interleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 193 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 194 | int num_channels, |
| 195 | T* deinterleaved); |
| 196 | |
| 197 | template <> |
| 198 | void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 199 | size_t num_frames, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 200 | int num_channels, |
| 201 | int16_t* deinterleaved); |
| 202 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 203 | } // namespace webrtc |
| 204 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 205 | #endif // COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |