blob: 5e86c1f06ac97c28095b94ee127920c3a9446877 [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
14#include "webrtc/typedefs.h"
15
16namespace webrtc {
17
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000018// Clamp the floating |value| to the range representable by an int16_t.
19static inline float ClampInt16(float value) {
20 const float kMaxInt16 = 32767.f;
21 const float kMinInt16 = -32768.f;
22 return value < kMinInt16 ? kMinInt16 :
23 (value > kMaxInt16 ? kMaxInt16 : value);
24}
25
26// Return a rounded int16_t of the floating |value|. Doesn't handle overflow;
27// use ClampInt16 if necessary.
28static inline int16_t RoundToInt16(float value) {
29 return static_cast<int16_t>(value < 0.f ? value - 0.5f : value + 0.5f);
30}
31
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000032// Deinterleave audio from |interleaved| to the channel buffers pointed to
33// by |deinterleaved|. There must be sufficient space allocated in the
34// |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
35// per buffer).
36void Deinterleave(const int16_t* interleaved, int samples_per_channel,
37 int num_channels, int16_t** deinterleaved);
38
39// Interleave audio from the channel buffers pointed to by |deinterleaved| to
40// |interleaved|. There must be sufficient space allocated in |interleaved|
41// (|samples_per_channel| * |num_channels|).
42void Interleave(const int16_t* const* deinterleaved, int samples_per_channel,
43 int num_channels, int16_t* interleaved);
44
45} // namespace webrtc
46
47#endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_