root / audio / mixeng_template.h @ c09015dd
History | View | Annotate | Download (3.8 kB)
1 | 85571bc7 | bellard | /*
|
---|---|---|---|
2 | 85571bc7 | bellard | * QEMU Mixing engine
|
3 | 1d14ffa9 | bellard | *
|
4 | 1d14ffa9 | bellard | * Copyright (c) 2004-2005 Vassili Karpov (malc)
|
5 | 1d14ffa9 | bellard | *
|
6 | 85571bc7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 85571bc7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 85571bc7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 85571bc7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 85571bc7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 85571bc7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 85571bc7 | bellard | *
|
13 | 85571bc7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 85571bc7 | bellard | * all copies or substantial portions of the Software.
|
15 | 85571bc7 | bellard | *
|
16 | 85571bc7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 85571bc7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 85571bc7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 85571bc7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 85571bc7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 85571bc7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 85571bc7 | bellard | * THE SOFTWARE.
|
23 | 85571bc7 | bellard | */
|
24 | 85571bc7 | bellard | |
25 | 85571bc7 | bellard | /*
|
26 | 85571bc7 | bellard | * Tusen tack till Mike Nordell
|
27 | 85571bc7 | bellard | * dec++'ified by Dscho
|
28 | 85571bc7 | bellard | */
|
29 | 85571bc7 | bellard | |
30 | 1d14ffa9 | bellard | #ifndef SIGNED
|
31 | 1d14ffa9 | bellard | #define HALF (IN_MAX >> 1) |
32 | 1d14ffa9 | bellard | #endif
|
33 | 1d14ffa9 | bellard | |
34 | 1d14ffa9 | bellard | #define ET glue (ENDIAN_CONVERSION, glue (_, IN_T))
|
35 | 1d14ffa9 | bellard | |
36 | 1d14ffa9 | bellard | #ifdef FLOAT_MIXENG
|
37 | 1ea879e5 | malc | static mixeng_real inline glue (conv_, ET) (IN_T v) |
38 | 1d14ffa9 | bellard | { |
39 | 1d14ffa9 | bellard | IN_T nv = ENDIAN_CONVERT (v); |
40 | 1d14ffa9 | bellard | |
41 | 1d14ffa9 | bellard | #ifdef RECIPROCAL
|
42 | 1d14ffa9 | bellard | #ifdef SIGNED
|
43 | 1ea879e5 | malc | return nv * (1.f / (mixeng_real) (IN_MAX - IN_MIN)); |
44 | 1d14ffa9 | bellard | #else
|
45 | 1ea879e5 | malc | return (nv - HALF) * (1.f / (mixeng_real) IN_MAX); |
46 | 1d14ffa9 | bellard | #endif
|
47 | 1d14ffa9 | bellard | #else /* !RECIPROCAL */ |
48 | 85571bc7 | bellard | #ifdef SIGNED
|
49 | 578c7b2c | Juha Riihim?ki | return nv / (mixeng_real) ((mixeng_real) IN_MAX - IN_MIN);
|
50 | 85571bc7 | bellard | #else
|
51 | 1ea879e5 | malc | return (nv - HALF) / (mixeng_real) IN_MAX;
|
52 | 85571bc7 | bellard | #endif
|
53 | 1d14ffa9 | bellard | #endif
|
54 | 1d14ffa9 | bellard | } |
55 | 85571bc7 | bellard | |
56 | 1ea879e5 | malc | static IN_T inline glue (clip_, ET) (mixeng_real v) |
57 | 85571bc7 | bellard | { |
58 | 1d14ffa9 | bellard | if (v >= 0.5) { |
59 | 1d14ffa9 | bellard | return IN_MAX;
|
60 | 1d14ffa9 | bellard | } |
61 | 1d14ffa9 | bellard | else if (v < -0.5) { |
62 | 1d14ffa9 | bellard | return IN_MIN;
|
63 | 1d14ffa9 | bellard | } |
64 | 1d14ffa9 | bellard | |
65 | 1d14ffa9 | bellard | #ifdef SIGNED
|
66 | 578c7b2c | Juha Riihim?ki | return ENDIAN_CONVERT ((IN_T) (v * ((mixeng_real) IN_MAX - IN_MIN)));
|
67 | 1d14ffa9 | bellard | #else
|
68 | 1d14ffa9 | bellard | return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF));
|
69 | 1d14ffa9 | bellard | #endif
|
70 | 1d14ffa9 | bellard | } |
71 | 1d14ffa9 | bellard | |
72 | 1d14ffa9 | bellard | #else /* !FLOAT_MIXENG */ |
73 | 1d14ffa9 | bellard | |
74 | 1d14ffa9 | bellard | static inline int64_t glue (conv_, ET) (IN_T v) |
75 | 1d14ffa9 | bellard | { |
76 | 1d14ffa9 | bellard | IN_T nv = ENDIAN_CONVERT (v); |
77 | 85571bc7 | bellard | #ifdef SIGNED
|
78 | 1d14ffa9 | bellard | return ((int64_t) nv) << (32 - SHIFT); |
79 | 85571bc7 | bellard | #else
|
80 | 1d14ffa9 | bellard | return ((int64_t) nv - HALF) << (32 - SHIFT); |
81 | 85571bc7 | bellard | #endif
|
82 | 85571bc7 | bellard | } |
83 | 85571bc7 | bellard | |
84 | 1d14ffa9 | bellard | static inline IN_T glue (clip_, ET) (int64_t v) |
85 | 85571bc7 | bellard | { |
86 | 1d14ffa9 | bellard | if (v >= 0x7f000000) { |
87 | 85571bc7 | bellard | return IN_MAX;
|
88 | 1d14ffa9 | bellard | } |
89 | 1d14ffa9 | bellard | else if (v < -2147483648LL) { |
90 | 85571bc7 | bellard | return IN_MIN;
|
91 | 1d14ffa9 | bellard | } |
92 | 85571bc7 | bellard | |
93 | 85571bc7 | bellard | #ifdef SIGNED
|
94 | 1d14ffa9 | bellard | return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT))); |
95 | 85571bc7 | bellard | #else
|
96 | 1d14ffa9 | bellard | return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF)); |
97 | 85571bc7 | bellard | #endif
|
98 | 85571bc7 | bellard | } |
99 | 1d14ffa9 | bellard | #endif
|
100 | 85571bc7 | bellard | |
101 | 1d14ffa9 | bellard | static void glue (glue (conv_, ET), _to_stereo) |
102 | 00e07679 | Michael Walle | (struct st_sample *dst, const void *src, int samples) |
103 | 85571bc7 | bellard | { |
104 | 1ea879e5 | malc | struct st_sample *out = dst;
|
105 | 85571bc7 | bellard | IN_T *in = (IN_T *) src; |
106 | 00e07679 | Michael Walle | |
107 | 85571bc7 | bellard | while (samples--) {
|
108 | 00e07679 | Michael Walle | out->l = glue (conv_, ET) (*in++); |
109 | 00e07679 | Michael Walle | out->r = glue (conv_, ET) (*in++); |
110 | 85571bc7 | bellard | out += 1;
|
111 | 85571bc7 | bellard | } |
112 | 85571bc7 | bellard | } |
113 | 85571bc7 | bellard | |
114 | 1d14ffa9 | bellard | static void glue (glue (conv_, ET), _to_mono) |
115 | 00e07679 | Michael Walle | (struct st_sample *dst, const void *src, int samples) |
116 | 85571bc7 | bellard | { |
117 | 1ea879e5 | malc | struct st_sample *out = dst;
|
118 | 85571bc7 | bellard | IN_T *in = (IN_T *) src; |
119 | 00e07679 | Michael Walle | |
120 | 85571bc7 | bellard | while (samples--) {
|
121 | 00e07679 | Michael Walle | out->l = glue (conv_, ET) (in[0]);
|
122 | 85571bc7 | bellard | out->r = out->l; |
123 | 85571bc7 | bellard | out += 1;
|
124 | 85571bc7 | bellard | in += 1;
|
125 | 85571bc7 | bellard | } |
126 | 85571bc7 | bellard | } |
127 | 85571bc7 | bellard | |
128 | 1d14ffa9 | bellard | static void glue (glue (clip_, ET), _from_stereo) |
129 | 1ea879e5 | malc | (void *dst, const struct st_sample *src, int samples) |
130 | 85571bc7 | bellard | { |
131 | 1ea879e5 | malc | const struct st_sample *in = src; |
132 | 85571bc7 | bellard | IN_T *out = (IN_T *) dst; |
133 | 85571bc7 | bellard | while (samples--) {
|
134 | 1d14ffa9 | bellard | *out++ = glue (clip_, ET) (in->l); |
135 | 1d14ffa9 | bellard | *out++ = glue (clip_, ET) (in->r); |
136 | 85571bc7 | bellard | in += 1;
|
137 | 85571bc7 | bellard | } |
138 | 85571bc7 | bellard | } |
139 | 85571bc7 | bellard | |
140 | 1d14ffa9 | bellard | static void glue (glue (clip_, ET), _from_mono) |
141 | 1ea879e5 | malc | (void *dst, const struct st_sample *src, int samples) |
142 | 85571bc7 | bellard | { |
143 | 1ea879e5 | malc | const struct st_sample *in = src; |
144 | 85571bc7 | bellard | IN_T *out = (IN_T *) dst; |
145 | 85571bc7 | bellard | while (samples--) {
|
146 | 1d14ffa9 | bellard | *out++ = glue (clip_, ET) (in->l + in->r); |
147 | 85571bc7 | bellard | in += 1;
|
148 | 85571bc7 | bellard | } |
149 | 85571bc7 | bellard | } |
150 | 85571bc7 | bellard | |
151 | 1d14ffa9 | bellard | #undef ET
|
152 | 85571bc7 | bellard | #undef HALF |