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 | |
turaj@webrtc.org | d4d5be8 | 2014-02-20 20:55:21 +0000 | [diff] [blame^] | 26 | // Round |value| to the closest int16. |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 27 | static inline int16_t RoundToInt16(float value) { |
turaj@webrtc.org | d4d5be8 | 2014-02-20 20:55:21 +0000 | [diff] [blame^] | 28 | 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|. |
| 34 | static 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.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 37 | } |
| 38 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 39 | // 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). |
| 43 | void 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|). |
| 49 | void 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_ |