| 1 | /* |
| 2 | * Copyright (C) 2013 nu774 |
| 3 | * For conditions of distribution and use, see copyright notice in COPYING |
| 4 | */ |
| 5 | #ifndef LPCM_H |
| 6 | #define LPCM_H |
| 7 | |
| 8 | enum pcm_type { |
| 9 | PCM_TYPE_UNKNOWN = 0, |
| 10 | PCM_TYPE_SINT = 1, |
| 11 | PCM_TYPE_UINT = 2, |
| 12 | PCM_TYPE_FLOAT = 4, |
| 13 | PCM_TYPE_SINT_BE = (8|1), |
| 14 | PCM_TYPE_UINT_BE = (8|2), |
| 15 | PCM_TYPE_FLOAT_BE = (8|4), |
| 16 | }; |
| 17 | |
| 18 | typedef struct pcm_sample_description_t { |
| 19 | enum pcm_type sample_type; |
| 20 | uint32_t sample_rate; |
| 21 | uint8_t bits_per_channel; |
| 22 | uint8_t bytes_per_frame; |
| 23 | uint8_t channels_per_frame; |
| 24 | uint32_t channel_mask; |
| 25 | } pcm_sample_description_t; |
| 26 | |
| 27 | #define PCM_IS_SINT(desc) ((desc)->sample_type & 1) |
| 28 | #define PCM_IS_UINT(desc) ((desc)->sample_type & 2) |
| 29 | #define PCM_IS_FLOAT(desc) ((desc)->sample_type & 4) |
| 30 | #define PCM_IS_BIG_ENDIAN(desc) ((desc)->sample_type & 8) |
| 31 | #define PCM_BYTES_PER_CHANNEL(desc) \ |
| 32 | ((desc)->bytes_per_frame / (desc)->channels_per_frame) |
| 33 | |
| 34 | #if defined(_MSC_VER) && _MSC_VER < 1800 |
| 35 | # ifdef _M_IX86 |
| 36 | static inline int lrint(double x) |
| 37 | { |
| 38 | int n; |
| 39 | _asm { |
| 40 | fld x |
| 41 | fistp n |
| 42 | } |
| 43 | return n; |
| 44 | } |
| 45 | # else |
| 46 | # include <emmintrin.h> |
| 47 | static inline int lrint(double x) |
| 48 | { |
| 49 | return _mm_cvtsd_si32(_mm_load_sd(&x)); |
| 50 | } |
| 51 | # endif |
| 52 | #endif |
| 53 | |
| 54 | static |
| 55 | inline double pcm_clip(double n, double min_value, double max_value) |
| 56 | { |
| 57 | if (n < min_value) |
| 58 | return min_value; |
| 59 | else if (n > max_value) |
| 60 | return max_value; |
| 61 | return n; |
| 62 | } |
| 63 | |
| 64 | int pcm_convert_to_native_sint16(const pcm_sample_description_t *format, |
| 65 | const void *input, uint32_t nframes, |
| 66 | int16_t *result); |
| 67 | #endif |