langpackedit: sorting fixes, 0.015 -- from 8f06f769
[wdl.git] / WDL / pcmfmtcvt.h
blobaa968c44d51f5cdb16cfe81ebaa9cc1f2a7464e7
1 /*
2 WDL - pcmfmtcvt.h
3 Copyright (C) 2005 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 #ifndef _PCMFMTCVT_H_
24 #define _PCMFMTCVT_H_
27 #include "wdltypes.h"
28 #include <math.h>
30 #ifndef PCMFMTCVT_DBL_TYPE
31 #define PCMFMTCVT_DBL_TYPE double
32 #endif
34 #define INT16_TO_float INT16_TO_double
35 #define float_TO_INT16 double_TO_INT16
36 #define float_to_i32 double_to_i32
37 #define float_to_i24 double_to_i24
38 #define i24_to_float i24_to_double
39 #define i32_to_float i32_to_double
41 #define pcmToFloats pcmToDoubles
42 #define floatsToPcm doublesToPcm
45 template<class T> static void INT16_TO_double(T &out, int in)
47 out = (T) (in/32768.0);
49 template<class T> static void double_TO_INT16(T &out, double in)
51 in *= 32768.0;
52 if (in <= -32768.0) out = -32768;
53 else if (in >= 32767.0) out = 32767;
54 else out = (T) floor(in + 0.5);
57 template<class T> static void i32_to_double(int i32, T *p)
59 *p = (T) (i32 * (1.0 / 2147483648.0));
62 template<class T> static void i24_to_double(const unsigned char *i24, T *p)
64 // little endian
65 int val=(i24[0]) | (i24[1]<<8) | (i24[2]<<16);
66 if (val&0x800000) val|=0xFF000000;
67 *p = (T) (((double) val) * (1.0 / 8388608.0));
70 template<class T> static void double_to_i32(const T *vv, int *i32)
72 const double v = *vv * 2147483648.0;
73 if (v <= -2147483648.0) *i32 = 0x80000000;
74 else if (v >= 2147483647.0) *i32 = 0x7FFFFFFF;
75 else *i32 = (int) floor(v + 0.5);
78 static WDL_STATICFUNC_UNUSED int double_to_int_24(const double vv)
80 const double v = vv * 8388608.0;
81 if (v <= -8388608.0) return -0x800000;
82 if (v >= 8388607.0) return 0x7fffff;
83 return (int) floor(v + 0.5);
86 static WDL_STATICFUNC_UNUSED int double_to_int_x(const double vv, int bits)
88 const double sc = (double) (1<<(bits-1));
89 const double v = vv * sc;
90 if (v <= -sc) return -(1<<(bits-1));
91 if (v >= sc-1.0) return (1<<(bits-1))-1;
92 return (int) floor(v + 0.5);
95 template<class T> static void double_to_i24(const T *vv, unsigned char *i24)
97 const int i = double_to_int_24(*vv);
98 i24[0]=i&0xff;
99 i24[1]=(i>>8)&0xff;
100 i24[2]=(i>>16)&0xff;
103 template<class T> static void pcmToDoubles(void *src, int items, int bps, int src_spacing, T *dest, int dest_spacing, int byteadvancefor24=0)
105 if (bps == 32)
107 int *i1=(int *)src;
108 while (items--)
110 i32_to_double(*i1,dest);
111 i1+=src_spacing;
112 dest+=dest_spacing;
115 else if (bps == 24)
117 unsigned char *i1=(unsigned char *)src;
118 int adv=3*src_spacing+byteadvancefor24;
119 while (items--)
121 i24_to_double(i1,dest);
122 dest+=dest_spacing;
123 i1+=adv;
126 else if (bps == 16)
128 short *i1=(short *)src;
129 while (items--)
131 INT16_TO_double(*dest,*i1);
132 i1+=src_spacing;
133 dest+=dest_spacing;
138 template<class T> static void doublesToPcm(const T *src, int src_spacing, int items, void *dest, int bps, int dest_spacing, int byteadvancefor24=0)
140 if (bps==32)
142 int *o1=(int*)dest;
143 while (items--)
145 double_to_i32(src,o1);
146 src+=src_spacing;
147 o1+=dest_spacing;
150 else if (bps == 24)
152 unsigned char *o1=(unsigned char*)dest;
153 int adv=dest_spacing*3+byteadvancefor24;
154 while (items--)
156 double_to_i24(src,o1);
157 src+=src_spacing;
158 o1+=adv;
161 else if (bps==16)
163 short *o1=(short*)dest;
164 while (items--)
166 double_TO_INT16(*o1,*src);
167 src+=src_spacing;
168 o1+=dest_spacing;
173 #endif //_PCMFMTCVT_H_