blob: 1e8f8d617f48037a838b589fa87d721795acd984 [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
turaj@webrtc.orgd4d5be82014-02-20 20:55:21 +000026// Round |value| to the closest int16.
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000027static inline int16_t RoundToInt16(float value) {
turaj@webrtc.orgd4d5be82014-02-20 20:55:21 +000028 return static_cast<int16_t>(
29 value > 0 ? (value >= 32766.5 ? 32767 : value + 0.5f)
30 : (value <= -32767.5 ? -32768 : value - 0.5f));
31}
32
33// Round |size| elements of |src| to the closest int16 and writes to |dest|.
34static inline void RoundToInt16(const float* src, int size, int16_t* dest) {
35 for (int i = 0; i < size; ++i)
36 dest[i] = RoundToInt16(src[i]);
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000037}
38
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000039// Deinterleave audio from |interleaved| to the channel buffers pointed to
40// by |deinterleaved|. There must be sufficient space allocated in the
41// |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
42// per buffer).
43void Deinterleave(const int16_t* interleaved, int samples_per_channel,
44 int num_channels, int16_t** deinterleaved);
45
46// Interleave audio from the channel buffers pointed to by |deinterleaved| to
47// |interleaved|. There must be sufficient space allocated in |interleaved|
48// (|samples_per_channel| * |num_channels|).
49void Interleave(const int16_t* const* deinterleaved, int samples_per_channel,
50 int num_channels, int16_t* interleaved);
51
52} // namespace webrtc
53
54#endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_