andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | namespace webrtc { |
| 17 | |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 18 | // Clamp the floating |value| to the range representable by an int16_t. |
| 19 | static 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. |
| 28 | static 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.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 32 | // 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). |
| 36 | void 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|). |
| 42 | void 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_ |