blob: b217c683fd05815a7f10f95b000371f03e2db393 [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"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000018#include "webrtc/base/scoped_ptr.h"
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000019#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
andrew@webrtc.org17e40642014-03-04 20:58:13 +000023typedef std::numeric_limits<int16_t> limits_int16;
24
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000025// 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]
29static inline int16_t FloatToS16(float v) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000030 if (v > 0)
Michael Graczyk86c6d332015-07-23 11:41:39 -070031 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.orgd4d5be82014-02-20 20:55:21 +000035}
36
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000037static 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.org17e40642014-03-04 20:58:13 +000040 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000041}
42
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000043static 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 Graczyk86c6d332015-07-23 11:41:39 -070047 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.org4fc4add2014-10-30 03:40:10 +000050}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000051
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000052static inline float FloatToFloatS16(float v) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000053 return v * (v > 0 ? limits_int16::max() : -limits_int16::min());
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000054}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000055
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000056static 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
62void FloatToS16(const float* src, size_t size, int16_t* dest);
63void S16ToFloat(const int16_t* src, size_t size, float* dest);
64void FloatS16ToS16(const float* src, size_t size, int16_t* dest);
65void FloatToFloatS16(const float* src, size_t size, float* dest);
66void FloatS16ToFloat(const float* src, size_t size, float* dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +000067
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000068// Deinterleave audio from |interleaved| to the channel buffers pointed to
69// by |deinterleaved|. There must be sufficient space allocated in the
70// |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
71// per buffer).
andrew@webrtc.org17e40642014-03-04 20:58:13 +000072template <typename T>
Michael Graczyk86c6d332015-07-23 11:41:39 -070073void Deinterleave(const T* interleaved,
74 int samples_per_channel,
75 int num_channels,
76 T* const* deinterleaved) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000077 for (int i = 0; i < num_channels; ++i) {
78 T* channel = deinterleaved[i];
79 int interleaved_idx = i;
80 for (int j = 0; j < samples_per_channel; ++j) {
81 channel[j] = interleaved[interleaved_idx];
82 interleaved_idx += num_channels;
83 }
84 }
85}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000086
87// Interleave audio from the channel buffers pointed to by |deinterleaved| to
88// |interleaved|. There must be sufficient space allocated in |interleaved|
89// (|samples_per_channel| * |num_channels|).
andrew@webrtc.org17e40642014-03-04 20:58:13 +000090template <typename T>
Michael Graczyk86c6d332015-07-23 11:41:39 -070091void Interleave(const T* const* deinterleaved,
92 int samples_per_channel,
93 int num_channels,
94 T* interleaved) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000095 for (int i = 0; i < num_channels; ++i) {
96 const T* channel = deinterleaved[i];
97 int interleaved_idx = i;
98 for (int j = 0; j < samples_per_channel; ++j) {
99 interleaved[interleaved_idx] = channel[j];
100 interleaved_idx += num_channels;
101 }
102 }
103}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000104
Michael Graczyk86c6d332015-07-23 11:41:39 -0700105template <typename T, typename Intermediate>
106void DownmixToMono(const T* const* input_channels,
107 int num_frames,
108 int num_channels,
109 T* out) {
110 for (int i = 0; i < num_frames; ++i) {
111 Intermediate value = input_channels[0][i];
112 for (int j = 1; j < num_channels; ++j) {
113 value += input_channels[j][i];
114 }
115 out[i] = value / num_channels;
116 }
117}
118
119// Downmixes an interleaved multichannel signal to a single channel by averaging
120// all channels.
121template <typename T, typename Intermediate>
122void DownmixInterleavedToMonoImpl(const T* interleaved,
123 int num_frames,
124 int num_channels,
125 T* deinterleaved) {
126 DCHECK_GT(num_channels, 0);
127 DCHECK_GT(num_frames, 0);
128
129 const T* const end = interleaved + num_frames * num_channels;
130
131 while (interleaved < end) {
132 const T* const frame_end = interleaved + num_channels;
133
134 Intermediate value = *interleaved++;
135 while (interleaved < frame_end) {
136 value += *interleaved++;
137 }
138
139 *deinterleaved++ = value / num_channels;
140 }
141}
142
143template <typename T>
144void DownmixInterleavedToMono(const T* interleaved,
145 int num_frames,
146 int num_channels,
147 T* deinterleaved);
148
149template <>
150void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved,
151 int num_frames,
152 int num_channels,
153 int16_t* deinterleaved);
154
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000155} // namespace webrtc
156
157#endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_