1 | /*
|
---|
2 | * QEMU Mixing engine
|
---|
3 | *
|
---|
4 | * Copyright (c) 2004-2005 Vassili Karpov (malc)
|
---|
5 | *
|
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
7 | * of this software and associated documentation files (the "Software"), to deal
|
---|
8 | * in the Software without restriction, including without limitation the rights
|
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
10 | * copies of the Software, and to permit persons to whom the Software is
|
---|
11 | * furnished to do so, subject to the following conditions:
|
---|
12 | *
|
---|
13 | * The above copyright notice and this permission notice shall be included in
|
---|
14 | * all copies or substantial portions of the Software.
|
---|
15 | *
|
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
22 | * THE SOFTWARE.
|
---|
23 | */
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * Tusen tack till Mike Nordell
|
---|
27 | * dec++'ified by Dscho
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef SIGNED
|
---|
31 | #define HALF (IN_MAX >> 1)
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #ifdef NOVOL
|
---|
35 | #define VOL(a, b) a
|
---|
36 | #else
|
---|
37 | #ifdef VBOX
|
---|
38 | #define VOL(a, b) ((ASMMult2xS32RetS64(a, b) >> 31))
|
---|
39 | #else /* !VBOX */
|
---|
40 | #ifdef FLOAT_MIXENG
|
---|
41 | #define VOL(a, b) ((a) * (b))
|
---|
42 | #else
|
---|
43 | #define VOL(a, b) ((a) * (b)) >> 32
|
---|
44 | #endif
|
---|
45 | #endif
|
---|
46 | #endif /* !VBOX */
|
---|
47 |
|
---|
48 | #define ET glue (ENDIAN_CONVERSION, glue (_, IN_T))
|
---|
49 |
|
---|
50 | #ifdef FLOAT_MIXENG
|
---|
51 | static real_t inline glue (conv_, ET) (IN_T v)
|
---|
52 | {
|
---|
53 | IN_T nv = ENDIAN_CONVERT (v);
|
---|
54 |
|
---|
55 | #ifdef RECIPROCAL
|
---|
56 | #ifdef SIGNED
|
---|
57 | return nv * (1.f / (real_t) (IN_MAX - IN_MIN));
|
---|
58 | #else
|
---|
59 | return (nv - HALF) * (1.f / (real_t) IN_MAX);
|
---|
60 | #endif
|
---|
61 | #else /* !RECIPROCAL */
|
---|
62 | #ifdef SIGNED
|
---|
63 | return nv / (real_t) (IN_MAX - IN_MIN);
|
---|
64 | #else
|
---|
65 | return (nv - HALF) / (real_t) IN_MAX;
|
---|
66 | #endif
|
---|
67 | #endif
|
---|
68 | }
|
---|
69 |
|
---|
70 | static IN_T inline glue (clip_, ET) (real_t v)
|
---|
71 | {
|
---|
72 | if (v >= 0.5) {
|
---|
73 | return IN_MAX;
|
---|
74 | }
|
---|
75 | else if (v < -0.5) {
|
---|
76 | return IN_MIN;
|
---|
77 | }
|
---|
78 |
|
---|
79 | #ifdef SIGNED
|
---|
80 | return ENDIAN_CONVERT ((IN_T) (v * (IN_MAX - IN_MIN)));
|
---|
81 | #else
|
---|
82 | return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF));
|
---|
83 | #endif
|
---|
84 | }
|
---|
85 |
|
---|
86 | #else /* !FLOAT_MIXENG */
|
---|
87 |
|
---|
88 | static inline int64_t glue (conv_, ET) (IN_T v)
|
---|
89 | {
|
---|
90 | IN_T nv = ENDIAN_CONVERT (v);
|
---|
91 | #ifdef SIGNED
|
---|
92 | return ((int64_t) nv) << (32 - SHIFT);
|
---|
93 | #else
|
---|
94 | return ((int64_t) nv - HALF) << (32 - SHIFT);
|
---|
95 | #endif
|
---|
96 | }
|
---|
97 |
|
---|
98 | static inline IN_T glue (clip_, ET) (int64_t v)
|
---|
99 | {
|
---|
100 | if (v >= 0x7f000000) {
|
---|
101 | return IN_MAX;
|
---|
102 | }
|
---|
103 | else if (v < -2147483648LL) {
|
---|
104 | return IN_MIN;
|
---|
105 | }
|
---|
106 |
|
---|
107 | #ifdef SIGNED
|
---|
108 | return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
|
---|
109 | #else
|
---|
110 | return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
|
---|
111 | #endif
|
---|
112 | }
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | static void glue (glue (conv_, ET), _to_stereo)
|
---|
116 | #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
|
---|
117 | (PPDMHOSTSTEREOSAMPLE dst, const void *src, int samples, volume_t *vol)
|
---|
118 | #else
|
---|
119 | (st_sample_t *dst, const void *src, int samples, volume_t *vol)
|
---|
120 | #endif
|
---|
121 | {
|
---|
122 | #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
|
---|
123 | PPDMHOSTSTEREOSAMPLE out = dst;
|
---|
124 | #else
|
---|
125 | st_sample_t *out = dst;
|
---|
126 | #endif
|
---|
127 | IN_T *in = (IN_T *) src;
|
---|
128 | #ifndef NOVOL
|
---|
129 | if (vol->mute) {
|
---|
130 | mixeng_clear (dst, samples);
|
---|
131 | return;
|
---|
132 | }
|
---|
133 | #else
|
---|
134 | (void) vol;
|
---|
135 | #endif
|
---|
136 | while (samples--) {
|
---|
137 | #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
|
---|
138 | out->u64LSample = VOL (glue (conv_, ET) (*in++), vol->l);
|
---|
139 | out->u64RSample = VOL (glue (conv_, ET) (*in++), vol->r);
|
---|
140 | #else
|
---|
141 | out->l = VOL (glue (conv_, ET) (*in++), vol->l);
|
---|
142 | out->r = VOL (glue (conv_, ET) (*in++), vol->r);
|
---|
143 | #endif
|
---|
144 | out += 1;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | static void glue (glue (conv_, ET), _to_mono)
|
---|
149 | #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
|
---|
150 | (PPDMHOSTSTEREOSAMPLE dst, const void *src, int samples, volume_t *vol)
|
---|
151 | #else
|
---|
152 | (st_sample_t *dst, const void *src, int samples, volume_t *vol)
|
---|
153 | #endif
|
---|
154 | {
|
---|
155 | #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
|
---|
156 | PPDMHOSTSTEREOSAMPLE out = dst;
|
---|
157 | #else
|
---|
158 | st_sample_t *out = dst;
|
---|
159 | #endif
|
---|
160 | IN_T *in = (IN_T *) src;
|
---|
161 | #ifndef NOVOL
|
---|
162 | if (vol->mute) {
|
---|
163 | mixeng_clear (dst, samples);
|
---|
164 | return;
|
---|
165 | }
|
---|
166 | #else
|
---|
167 | (void) vol;
|
---|
168 | #endif
|
---|
169 | while (samples--) {
|
---|
170 | #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
|
---|
171 | out->u64LSample = VOL (glue (conv_, ET) (in[0]), vol->l);
|
---|
172 | out->u64RSample = out->u64LSample;
|
---|
173 | #else
|
---|
174 | out->l = VOL (glue (conv_, ET) (in[0]), vol->l);
|
---|
175 | out->r = out->l;
|
---|
176 |
|
---|
177 | #endif
|
---|
178 | out += 1;
|
---|
179 | in += 1;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | static void glue (glue (clip_, ET), _from_stereo)
|
---|
184 | (void *dst, const st_sample_t *src, int samples)
|
---|
185 | {
|
---|
186 | const st_sample_t *in = src;
|
---|
187 | IN_T *out = (IN_T *) dst;
|
---|
188 | while (samples--) {
|
---|
189 | *out++ = glue (clip_, ET) (in->l);
|
---|
190 | *out++ = glue (clip_, ET) (in->r);
|
---|
191 | in += 1;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | static void glue (glue (clip_, ET), _from_mono)
|
---|
196 | (void *dst, const st_sample_t *src, int samples)
|
---|
197 | {
|
---|
198 | const st_sample_t *in = src;
|
---|
199 | IN_T *out = (IN_T *) dst;
|
---|
200 | while (samples--) {
|
---|
201 | *out++ = glue (clip_, ET) (in->l + in->r);
|
---|
202 | in += 1;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | #undef ET
|
---|
207 | #undef HALF
|
---|
208 | #undef VOL
|
---|