blob: 9972a0e0672427ebcf04fd5294b7200c04e12d8c [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
23static inline int16_t RoundToInt16(float v) {
24 const float kMaxRound = limits_int16::max() - 0.5f;
25 const float kMinRound = limits_int16::min() + 0.5f;
26 if (v > 0)
27 return v >= kMaxRound ? limits_int16::max() :
28 static_cast<int16_t>(v + 0.5f);
29 return v <= kMinRound ? limits_int16::min() :
30 static_cast<int16_t>(v - 0.5f);
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000031}
32
andrew@webrtc.org17e40642014-03-04 20:58:13 +000033// Scale (from [-1, 1]) and round to full-range int16 with clamping.
34static inline int16_t ScaleAndRoundToInt16(float v) {
35 if (v > 0)
36 return v >= 1 ? limits_int16::max() :
37 static_cast<int16_t>(v * limits_int16::max() + 0.5f);
38 return v <= -1 ? limits_int16::min() :
39 static_cast<int16_t>(-v * limits_int16::min() - 0.5f);
turaj@webrtc.orgd4d5be82014-02-20 20:55:21 +000040}
41
andrew@webrtc.org17e40642014-03-04 20:58:13 +000042// Scale to float [-1, 1].
43static inline float ScaleToFloat(int16_t v) {
44 const float kMaxInt16Inverse = 1.f / limits_int16::max();
45 const float kMinInt16Inverse = 1.f / limits_int16::min();
46 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000047}
48
andrew@webrtc.org17e40642014-03-04 20:58:13 +000049// Round |size| elements of |src| to int16 with clamping and write to |dest|.
kwiberg@webrtc.orgefb81d82014-07-16 08:36:52 +000050void RoundToInt16(const float* src, size_t size, int16_t* dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +000051
52// Scale (from [-1, 1]) and round |size| elements of |src| to full-range int16
53// with clamping and write to |dest|.
kwiberg@webrtc.orgefb81d82014-07-16 08:36:52 +000054void ScaleAndRoundToInt16(const float* src, size_t size, int16_t* dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +000055
56// Scale |size| elements of |src| to float [-1, 1] and write to |dest|.
kwiberg@webrtc.orgefb81d82014-07-16 08:36:52 +000057void ScaleToFloat(const int16_t* src, size_t size, float* dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +000058
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000059// Deinterleave audio from |interleaved| to the channel buffers pointed to
60// by |deinterleaved|. There must be sufficient space allocated in the
61// |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
62// per buffer).
andrew@webrtc.org17e40642014-03-04 20:58:13 +000063template <typename T>
64void Deinterleave(const T* interleaved, int samples_per_channel,
65 int num_channels, T** deinterleaved) {
66 for (int i = 0; i < num_channels; ++i) {
67 T* channel = deinterleaved[i];
68 int interleaved_idx = i;
69 for (int j = 0; j < samples_per_channel; ++j) {
70 channel[j] = interleaved[interleaved_idx];
71 interleaved_idx += num_channels;
72 }
73 }
74}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000075
76// Interleave audio from the channel buffers pointed to by |deinterleaved| to
77// |interleaved|. There must be sufficient space allocated in |interleaved|
78// (|samples_per_channel| * |num_channels|).
andrew@webrtc.org17e40642014-03-04 20:58:13 +000079template <typename T>
80void Interleave(const T* const* deinterleaved, int samples_per_channel,
81 int num_channels, T* interleaved) {
82 for (int i = 0; i < num_channels; ++i) {
83 const T* channel = deinterleaved[i];
84 int interleaved_idx = i;
85 for (int j = 0; j < samples_per_channel; ++j) {
86 interleaved[interleaved_idx] = channel[j];
87 interleaved_idx += num_channels;
88 }
89 }
90}
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000091
92} // namespace webrtc
93
94#endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_