blob: 5a4e8151311a1b905080cf287dd2f55acd3daca8 [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>
15
16#include "webrtc/system_wrappers/interface/scoped_ptr.h"
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
andrew@webrtc.org17e40642014-03-04 20:58:13 +000021typedef std::numeric_limits<int16_t> limits_int16;
22
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000023// The conversion functions use the following naming convention:
24// S16: int16_t [-32768, 32767]
25// Float: float [-1.0, 1.0]
26// FloatS16: float [-32768.0, 32767.0]
27static inline int16_t FloatToS16(float v) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000028 if (v > 0)
29 return v >= 1 ? limits_int16::max() :
30 static_cast<int16_t>(v * limits_int16::max() + 0.5f);
31 return v <= -1 ? limits_int16::min() :
32 static_cast<int16_t>(-v * limits_int16::min() - 0.5f);
turaj@webrtc.orgd4d5be82014-02-20 20:55:21 +000033}
34
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000035static inline float S16ToFloat(int16_t v) {
36 static const float kMaxInt16Inverse = 1.f / limits_int16::max();
37 static const float kMinInt16Inverse = 1.f / limits_int16::min();
andrew@webrtc.org17e40642014-03-04 20:58:13 +000038 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000039}
40
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000041static inline int16_t FloatS16ToS16(float v) {
42 static const float kMaxRound = limits_int16::max() - 0.5f;
43 static const float kMinRound = limits_int16::min() + 0.5f;
44 if (v > 0)
45 return v >= kMaxRound ? limits_int16::max() :
46 static_cast<int16_t>(v + 0.5f);
47 return v <= kMinRound ? limits_int16::min() :
48 static_cast<int16_t>(v - 0.5f);
49}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000050
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000051static inline float FloatToFloatS16(float v) {
52 return v > 0 ? v * limits_int16::max() : -v * limits_int16::min();
53}
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
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000067// Deinterleave audio from |interleaved| to the channel buffers pointed to
68// by |deinterleaved|. There must be sufficient space allocated in the
69// |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
70// per buffer).
andrew@webrtc.org17e40642014-03-04 20:58:13 +000071template <typename T>
72void Deinterleave(const T* interleaved, int samples_per_channel,
andrew@webrtc.org17454f72014-09-08 20:27:04 +000073 int num_channels, T* const* deinterleaved) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000074 for (int i = 0; i < num_channels; ++i) {
75 T* channel = deinterleaved[i];
76 int interleaved_idx = i;
77 for (int j = 0; j < samples_per_channel; ++j) {
78 channel[j] = interleaved[interleaved_idx];
79 interleaved_idx += num_channels;
80 }
81 }
82}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000083
84// Interleave audio from the channel buffers pointed to by |deinterleaved| to
85// |interleaved|. There must be sufficient space allocated in |interleaved|
86// (|samples_per_channel| * |num_channels|).
andrew@webrtc.org17e40642014-03-04 20:58:13 +000087template <typename T>
88void Interleave(const T* const* deinterleaved, int samples_per_channel,
89 int num_channels, T* interleaved) {
90 for (int i = 0; i < num_channels; ++i) {
91 const T* channel = deinterleaved[i];
92 int interleaved_idx = i;
93 for (int j = 0; j < samples_per_channel; ++j) {
94 interleaved[interleaved_idx] = channel[j];
95 interleaved_idx += num_channels;
96 }
97 }
98}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000099
100} // namespace webrtc
101
102#endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_