1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#ifndef SIGNED
31#define HALF (IN_MAX >> 1)
32#endif
33
34#define ET glue (ENDIAN_CONVERSION, glue (glue (glue (_, ITYPE), BSIZE), _t))
35#define IN_T glue (glue (ITYPE, BSIZE), _t)
36
37#ifdef FLOAT_MIXENG
38static inline mixeng_real glue (conv_, ET) (IN_T v)
39{
40 IN_T nv = ENDIAN_CONVERT (v);
41
42#ifdef RECIPROCAL
43#ifdef SIGNED
44 return nv * (1.f / (mixeng_real) (IN_MAX - IN_MIN));
45#else
46 return (nv - HALF) * (1.f / (mixeng_real) IN_MAX);
47#endif
48#else
49#ifdef SIGNED
50 return nv / (mixeng_real) ((mixeng_real) IN_MAX - IN_MIN);
51#else
52 return (nv - HALF) / (mixeng_real) IN_MAX;
53#endif
54#endif
55}
56
57static inline IN_T glue (clip_, ET) (mixeng_real v)
58{
59 if (v >= 0.5) {
60 return IN_MAX;
61 }
62 else if (v < -0.5) {
63 return IN_MIN;
64 }
65
66#ifdef SIGNED
67 return ENDIAN_CONVERT ((IN_T) (v * ((mixeng_real) IN_MAX - IN_MIN)));
68#else
69 return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF));
70#endif
71}
72
73#else
74
75static inline int64_t glue (conv_, ET) (IN_T v)
76{
77 IN_T nv = ENDIAN_CONVERT (v);
78#ifdef SIGNED
79 return ((int64_t) nv) << (32 - SHIFT);
80#else
81 return ((int64_t) nv - HALF) << (32 - SHIFT);
82#endif
83}
84
85static inline IN_T glue (clip_, ET) (int64_t v)
86{
87 if (v >= 0x7f000000) {
88 return IN_MAX;
89 }
90 else if (v < -2147483648LL) {
91 return IN_MIN;
92 }
93
94#ifdef SIGNED
95 return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
96#else
97 return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
98#endif
99}
100#endif
101
102static void glue (glue (conv_, ET), _to_stereo)
103 (struct st_sample *dst, const void *src, int samples)
104{
105 struct st_sample *out = dst;
106 IN_T *in = (IN_T *) src;
107
108 while (samples--) {
109 out->l = glue (conv_, ET) (*in++);
110 out->r = glue (conv_, ET) (*in++);
111 out += 1;
112 }
113}
114
115static void glue (glue (conv_, ET), _to_mono)
116 (struct st_sample *dst, const void *src, int samples)
117{
118 struct st_sample *out = dst;
119 IN_T *in = (IN_T *) src;
120
121 while (samples--) {
122 out->l = glue (conv_, ET) (in[0]);
123 out->r = out->l;
124 out += 1;
125 in += 1;
126 }
127}
128
129static void glue (glue (clip_, ET), _from_stereo)
130 (void *dst, const struct st_sample *src, int samples)
131{
132 const struct st_sample *in = src;
133 IN_T *out = (IN_T *) dst;
134 while (samples--) {
135 *out++ = glue (clip_, ET) (in->l);
136 *out++ = glue (clip_, ET) (in->r);
137 in += 1;
138 }
139}
140
141static void glue (glue (clip_, ET), _from_mono)
142 (void *dst, const struct st_sample *src, int samples)
143{
144 const struct st_sample *in = src;
145 IN_T *out = (IN_T *) dst;
146 while (samples--) {
147 *out++ = glue (clip_, ET) (in->l + in->r);
148 in += 1;
149 }
150}
151
152#undef ET
153#undef HALF
154#undef IN_T
155