blob: e5ad7015955b524fa69592a70789c09b01165d48 [file] [log] [blame]
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +00001/*
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.org17e40642014-03-04 20:58:13 +000014#include <limits>
Michael Graczyk86c6d332015-07-23 11:41:39 -070015#include <cstring>
andrew@webrtc.org17e40642014-03-04 20:58:13 +000016
Michael Graczyk86c6d332015-07-23 11:41:39 -070017#include "webrtc/base/checks.h"
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000018#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
andrew@webrtc.org17e40642014-03-04 20:58:13 +000022typedef std::numeric_limits<int16_t> limits_int16;
23
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000024// 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]
28static inline int16_t FloatToS16(float v) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000029 if (v > 0)
Michael Graczyk86c6d332015-07-23 11:41:39 -070030 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.orgd4d5be82014-02-20 20:55:21 +000034}
35
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000036static 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.org17e40642014-03-04 20:58:13 +000039 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000040}
41
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000042static 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 Graczyk86c6d332015-07-23 11:41:39 -070046 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.org4fc4add2014-10-30 03:40:10 +000049}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000050
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000051static inline float FloatToFloatS16(float v) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000052 return v * (v > 0 ? limits_int16::max() : -limits_int16::min());
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000053}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000054
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000055static 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
61void FloatToS16(const float* src, size_t size, int16_t* dest);
62void S16ToFloat(const int16_t* src, size_t size, float* dest);
63void FloatS16ToS16(const float* src, size_t size, int16_t* dest);
64void FloatToFloatS16(const float* src, size_t size, float* dest);
65void FloatS16ToFloat(const float* src, size_t size, float* dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +000066
ekmeyerson60d9b332015-08-14 10:35:55 -070067// 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|.
70template <typename T>
71void 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.org50b2efe2013-04-29 17:27:29 +000082// 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.org17e40642014-03-04 20:58:13 +000086template <typename T>
Michael Graczyk86c6d332015-07-23 11:41:39 -070087void Deinterleave(const T* interleaved,
Peter Kastingdce40cf2015-08-24 14:52:23 -070088 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -080089 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -070090 T* const* deinterleaved) {
Peter Kasting69558702016-01-12 16:26:35 -080091 for (size_t i = 0; i < num_channels; ++i) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000092 T* channel = deinterleaved[i];
Peter Kasting69558702016-01-12 16:26:35 -080093 size_t interleaved_idx = i;
Peter Kastingdce40cf2015-08-24 14:52:23 -070094 for (size_t j = 0; j < samples_per_channel; ++j) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000095 channel[j] = interleaved[interleaved_idx];
96 interleaved_idx += num_channels;
97 }
98 }
99}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000100
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.org17e40642014-03-04 20:58:13 +0000104template <typename T>
Michael Graczyk86c6d332015-07-23 11:41:39 -0700105void Interleave(const T* const* deinterleaved,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700106 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -0800107 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700108 T* interleaved) {
Peter Kasting69558702016-01-12 16:26:35 -0800109 for (size_t i = 0; i < num_channels; ++i) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000110 const T* channel = deinterleaved[i];
Peter Kasting69558702016-01-12 16:26:35 -0800111 size_t interleaved_idx = i;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700112 for (size_t j = 0; j < samples_per_channel; ++j) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000113 interleaved[interleaved_idx] = channel[j];
114 interleaved_idx += num_channels;
115 }
116 }
117}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000118
ekmeyerson60d9b332015-08-14 10:35:55 -0700119// 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|).
122template <typename T>
123void 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 Graczyk86c6d332015-07-23 11:41:39 -0700135template <typename T, typename Intermediate>
136void DownmixToMono(const T* const* input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 size_t num_frames,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700138 int num_channels,
139 T* out) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700140 for (size_t i = 0; i < num_frames; ++i) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700141 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.
151template <typename T, typename Intermediate>
152void DownmixInterleavedToMonoImpl(const T* interleaved,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700153 size_t num_frames,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700154 int num_channels,
155 T* deinterleaved) {
henrikg91d6ede2015-09-17 00:24:34 -0700156 RTC_DCHECK_GT(num_channels, 0);
157 RTC_DCHECK_GT(num_frames, 0u);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700158
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
173template <typename T>
174void DownmixInterleavedToMono(const T* interleaved,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700175 size_t num_frames,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700176 int num_channels,
177 T* deinterleaved);
178
179template <>
180void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700181 size_t num_frames,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700182 int num_channels,
183 int16_t* deinterleaved);
184
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000185} // namespace webrtc
186
187#endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_