]>
Commit | Line | Data |
---|---|---|
85571bc7 FB |
1 | /* |
2 | * QEMU Mixing engine | |
3 | * | |
1d14ffa9 | 4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) |
85571bc7 FB |
5 | * Copyright (c) 1998 Fabrice Bellard |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
6086a565 | 25 | #include "qemu/osdep.h" |
87ecb68b PB |
26 | #include "qemu-common.h" |
27 | #include "audio.h" | |
85571bc7 | 28 | |
1d14ffa9 FB |
29 | #define AUDIO_CAP "mixeng" |
30 | #include "audio_int.h" | |
31 | ||
1d14ffa9 FB |
32 | /* 8 bit */ |
33 | #define ENDIAN_CONVERSION natural | |
34 | #define ENDIAN_CONVERT(v) (v) | |
35 | ||
36 | /* Signed 8 bit */ | |
a2885387 RPM |
37 | #define BSIZE 8 |
38 | #define ITYPE int | |
1d14ffa9 FB |
39 | #define IN_MIN SCHAR_MIN |
40 | #define IN_MAX SCHAR_MAX | |
85571bc7 | 41 | #define SIGNED |
1d14ffa9 | 42 | #define SHIFT 8 |
85571bc7 FB |
43 | #include "mixeng_template.h" |
44 | #undef SIGNED | |
45 | #undef IN_MAX | |
46 | #undef IN_MIN | |
a2885387 RPM |
47 | #undef BSIZE |
48 | #undef ITYPE | |
1d14ffa9 | 49 | #undef SHIFT |
85571bc7 | 50 | |
1d14ffa9 | 51 | /* Unsigned 8 bit */ |
a2885387 RPM |
52 | #define BSIZE 8 |
53 | #define ITYPE uint | |
85571bc7 FB |
54 | #define IN_MIN 0 |
55 | #define IN_MAX UCHAR_MAX | |
1d14ffa9 | 56 | #define SHIFT 8 |
85571bc7 FB |
57 | #include "mixeng_template.h" |
58 | #undef IN_MAX | |
59 | #undef IN_MIN | |
a2885387 RPM |
60 | #undef BSIZE |
61 | #undef ITYPE | |
1d14ffa9 FB |
62 | #undef SHIFT |
63 | ||
64 | #undef ENDIAN_CONVERT | |
65 | #undef ENDIAN_CONVERSION | |
85571bc7 | 66 | |
1d14ffa9 | 67 | /* Signed 16 bit */ |
a2885387 RPM |
68 | #define BSIZE 16 |
69 | #define ITYPE int | |
85571bc7 FB |
70 | #define IN_MIN SHRT_MIN |
71 | #define IN_MAX SHRT_MAX | |
72 | #define SIGNED | |
1d14ffa9 FB |
73 | #define SHIFT 16 |
74 | #define ENDIAN_CONVERSION natural | |
75 | #define ENDIAN_CONVERT(v) (v) | |
85571bc7 | 76 | #include "mixeng_template.h" |
1d14ffa9 FB |
77 | #undef ENDIAN_CONVERT |
78 | #undef ENDIAN_CONVERSION | |
79 | #define ENDIAN_CONVERSION swap | |
80 | #define ENDIAN_CONVERT(v) bswap16 (v) | |
81 | #include "mixeng_template.h" | |
82 | #undef ENDIAN_CONVERT | |
83 | #undef ENDIAN_CONVERSION | |
85571bc7 FB |
84 | #undef SIGNED |
85 | #undef IN_MAX | |
86 | #undef IN_MIN | |
a2885387 RPM |
87 | #undef BSIZE |
88 | #undef ITYPE | |
1d14ffa9 | 89 | #undef SHIFT |
85571bc7 | 90 | |
f941aa25 | 91 | /* Unsigned 16 bit */ |
a2885387 RPM |
92 | #define BSIZE 16 |
93 | #define ITYPE uint | |
85571bc7 FB |
94 | #define IN_MIN 0 |
95 | #define IN_MAX USHRT_MAX | |
1d14ffa9 FB |
96 | #define SHIFT 16 |
97 | #define ENDIAN_CONVERSION natural | |
98 | #define ENDIAN_CONVERT(v) (v) | |
99 | #include "mixeng_template.h" | |
100 | #undef ENDIAN_CONVERT | |
101 | #undef ENDIAN_CONVERSION | |
102 | #define ENDIAN_CONVERSION swap | |
103 | #define ENDIAN_CONVERT(v) bswap16 (v) | |
85571bc7 | 104 | #include "mixeng_template.h" |
1d14ffa9 FB |
105 | #undef ENDIAN_CONVERT |
106 | #undef ENDIAN_CONVERSION | |
85571bc7 FB |
107 | #undef IN_MAX |
108 | #undef IN_MIN | |
a2885387 RPM |
109 | #undef BSIZE |
110 | #undef ITYPE | |
1d14ffa9 | 111 | #undef SHIFT |
85571bc7 | 112 | |
f941aa25 | 113 | /* Signed 32 bit */ |
a2885387 RPM |
114 | #define BSIZE 32 |
115 | #define ITYPE int | |
f941aa25 TS |
116 | #define IN_MIN INT32_MIN |
117 | #define IN_MAX INT32_MAX | |
118 | #define SIGNED | |
119 | #define SHIFT 32 | |
120 | #define ENDIAN_CONVERSION natural | |
121 | #define ENDIAN_CONVERT(v) (v) | |
122 | #include "mixeng_template.h" | |
123 | #undef ENDIAN_CONVERT | |
124 | #undef ENDIAN_CONVERSION | |
125 | #define ENDIAN_CONVERSION swap | |
126 | #define ENDIAN_CONVERT(v) bswap32 (v) | |
127 | #include "mixeng_template.h" | |
128 | #undef ENDIAN_CONVERT | |
129 | #undef ENDIAN_CONVERSION | |
130 | #undef SIGNED | |
131 | #undef IN_MAX | |
132 | #undef IN_MIN | |
a2885387 RPM |
133 | #undef BSIZE |
134 | #undef ITYPE | |
f941aa25 TS |
135 | #undef SHIFT |
136 | ||
ad483a51 | 137 | /* Unsigned 32 bit */ |
a2885387 RPM |
138 | #define BSIZE 32 |
139 | #define ITYPE uint | |
f941aa25 TS |
140 | #define IN_MIN 0 |
141 | #define IN_MAX UINT32_MAX | |
142 | #define SHIFT 32 | |
143 | #define ENDIAN_CONVERSION natural | |
144 | #define ENDIAN_CONVERT(v) (v) | |
145 | #include "mixeng_template.h" | |
146 | #undef ENDIAN_CONVERT | |
147 | #undef ENDIAN_CONVERSION | |
148 | #define ENDIAN_CONVERSION swap | |
149 | #define ENDIAN_CONVERT(v) bswap32 (v) | |
150 | #include "mixeng_template.h" | |
151 | #undef ENDIAN_CONVERT | |
152 | #undef ENDIAN_CONVERSION | |
153 | #undef IN_MAX | |
154 | #undef IN_MIN | |
a2885387 RPM |
155 | #undef BSIZE |
156 | #undef ITYPE | |
f941aa25 TS |
157 | #undef SHIFT |
158 | ||
159 | t_sample *mixeng_conv[2][2][2][3] = { | |
85571bc7 FB |
160 | { |
161 | { | |
1d14ffa9 FB |
162 | { |
163 | conv_natural_uint8_t_to_mono, | |
f941aa25 TS |
164 | conv_natural_uint16_t_to_mono, |
165 | conv_natural_uint32_t_to_mono | |
1d14ffa9 FB |
166 | }, |
167 | { | |
168 | conv_natural_uint8_t_to_mono, | |
f941aa25 TS |
169 | conv_swap_uint16_t_to_mono, |
170 | conv_swap_uint32_t_to_mono, | |
1d14ffa9 | 171 | } |
85571bc7 FB |
172 | }, |
173 | { | |
1d14ffa9 FB |
174 | { |
175 | conv_natural_int8_t_to_mono, | |
f941aa25 TS |
176 | conv_natural_int16_t_to_mono, |
177 | conv_natural_int32_t_to_mono | |
1d14ffa9 FB |
178 | }, |
179 | { | |
180 | conv_natural_int8_t_to_mono, | |
f941aa25 TS |
181 | conv_swap_int16_t_to_mono, |
182 | conv_swap_int32_t_to_mono | |
1d14ffa9 | 183 | } |
85571bc7 FB |
184 | } |
185 | }, | |
186 | { | |
187 | { | |
1d14ffa9 FB |
188 | { |
189 | conv_natural_uint8_t_to_stereo, | |
f941aa25 TS |
190 | conv_natural_uint16_t_to_stereo, |
191 | conv_natural_uint32_t_to_stereo | |
1d14ffa9 FB |
192 | }, |
193 | { | |
194 | conv_natural_uint8_t_to_stereo, | |
f941aa25 TS |
195 | conv_swap_uint16_t_to_stereo, |
196 | conv_swap_uint32_t_to_stereo | |
1d14ffa9 | 197 | } |
85571bc7 FB |
198 | }, |
199 | { | |
1d14ffa9 FB |
200 | { |
201 | conv_natural_int8_t_to_stereo, | |
f941aa25 TS |
202 | conv_natural_int16_t_to_stereo, |
203 | conv_natural_int32_t_to_stereo | |
1d14ffa9 FB |
204 | }, |
205 | { | |
206 | conv_natural_int8_t_to_stereo, | |
f941aa25 TS |
207 | conv_swap_int16_t_to_stereo, |
208 | conv_swap_int32_t_to_stereo, | |
1d14ffa9 | 209 | } |
85571bc7 FB |
210 | } |
211 | } | |
212 | }; | |
213 | ||
f941aa25 | 214 | f_sample *mixeng_clip[2][2][2][3] = { |
85571bc7 FB |
215 | { |
216 | { | |
1d14ffa9 FB |
217 | { |
218 | clip_natural_uint8_t_from_mono, | |
f941aa25 TS |
219 | clip_natural_uint16_t_from_mono, |
220 | clip_natural_uint32_t_from_mono | |
1d14ffa9 FB |
221 | }, |
222 | { | |
223 | clip_natural_uint8_t_from_mono, | |
f941aa25 TS |
224 | clip_swap_uint16_t_from_mono, |
225 | clip_swap_uint32_t_from_mono | |
1d14ffa9 | 226 | } |
85571bc7 FB |
227 | }, |
228 | { | |
1d14ffa9 FB |
229 | { |
230 | clip_natural_int8_t_from_mono, | |
f941aa25 TS |
231 | clip_natural_int16_t_from_mono, |
232 | clip_natural_int32_t_from_mono | |
1d14ffa9 FB |
233 | }, |
234 | { | |
235 | clip_natural_int8_t_from_mono, | |
f941aa25 TS |
236 | clip_swap_int16_t_from_mono, |
237 | clip_swap_int32_t_from_mono | |
1d14ffa9 | 238 | } |
85571bc7 FB |
239 | } |
240 | }, | |
241 | { | |
242 | { | |
1d14ffa9 FB |
243 | { |
244 | clip_natural_uint8_t_from_stereo, | |
f941aa25 TS |
245 | clip_natural_uint16_t_from_stereo, |
246 | clip_natural_uint32_t_from_stereo | |
1d14ffa9 FB |
247 | }, |
248 | { | |
249 | clip_natural_uint8_t_from_stereo, | |
f941aa25 TS |
250 | clip_swap_uint16_t_from_stereo, |
251 | clip_swap_uint32_t_from_stereo | |
1d14ffa9 | 252 | } |
85571bc7 FB |
253 | }, |
254 | { | |
1d14ffa9 FB |
255 | { |
256 | clip_natural_int8_t_from_stereo, | |
f941aa25 TS |
257 | clip_natural_int16_t_from_stereo, |
258 | clip_natural_int32_t_from_stereo | |
1d14ffa9 FB |
259 | }, |
260 | { | |
261 | clip_natural_int8_t_from_stereo, | |
f941aa25 TS |
262 | clip_swap_int16_t_from_stereo, |
263 | clip_swap_int32_t_from_stereo | |
1d14ffa9 | 264 | } |
85571bc7 FB |
265 | } |
266 | } | |
267 | }; | |
268 | ||
269 | /* | |
270 | * August 21, 1998 | |
271 | * Copyright 1998 Fabrice Bellard. | |
272 | * | |
cb8d4c8f | 273 | * [Rewrote completely the code of Lance Norskog And Sundry |
85571bc7 FB |
274 | * Contributors with a more efficient algorithm.] |
275 | * | |
276 | * This source code is freely redistributable and may be used for | |
1d14ffa9 FB |
277 | * any purpose. This copyright notice must be maintained. |
278 | * Lance Norskog And Sundry Contributors are not responsible for | |
279 | * the consequences of using this software. | |
85571bc7 FB |
280 | */ |
281 | ||
282 | /* | |
283 | * Sound Tools rate change effect file. | |
284 | */ | |
285 | /* | |
286 | * Linear Interpolation. | |
287 | * | |
288 | * The use of fractional increment allows us to use no buffer. It | |
289 | * avoid the problems at the end of the buffer we had with the old | |
290 | * method which stored a possibly big buffer of size | |
291 | * lcm(in_rate,out_rate). | |
292 | * | |
293 | * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If | |
294 | * the input & output frequencies are equal, a delay of one sample is | |
295 | * introduced. Limited to processing 32-bit count worth of samples. | |
296 | * | |
297 | * 1 << FRAC_BITS evaluating to zero in several places. Changed with | |
298 | * an (unsigned long) cast to make it safe. MarkMLl 2/1/99 | |
299 | */ | |
300 | ||
301 | /* Private data */ | |
c0fe3827 | 302 | struct rate { |
85571bc7 FB |
303 | uint64_t opos; |
304 | uint64_t opos_inc; | |
305 | uint32_t ipos; /* position in the input stream (integer) */ | |
1ea879e5 | 306 | struct st_sample ilast; /* last sample in the input stream */ |
c0fe3827 | 307 | }; |
85571bc7 FB |
308 | |
309 | /* | |
310 | * Prepare processing. | |
311 | */ | |
312 | void *st_rate_start (int inrate, int outrate) | |
313 | { | |
c0fe3827 | 314 | struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate)); |
85571bc7 FB |
315 | |
316 | if (!rate) { | |
e7cad338 | 317 | dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate)); |
1d14ffa9 | 318 | return NULL; |
85571bc7 FB |
319 | } |
320 | ||
321 | rate->opos = 0; | |
322 | ||
323 | /* increment */ | |
1d14ffa9 | 324 | rate->opos_inc = ((uint64_t) inrate << 32) / outrate; |
85571bc7 FB |
325 | |
326 | rate->ipos = 0; | |
327 | rate->ilast.l = 0; | |
328 | rate->ilast.r = 0; | |
329 | return rate; | |
330 | } | |
331 | ||
1d14ffa9 FB |
332 | #define NAME st_rate_flow_mix |
333 | #define OP(a, b) a += b | |
334 | #include "rate_template.h" | |
85571bc7 | 335 | |
1d14ffa9 FB |
336 | #define NAME st_rate_flow |
337 | #define OP(a, b) a = b | |
338 | #include "rate_template.h" | |
85571bc7 FB |
339 | |
340 | void st_rate_stop (void *opaque) | |
341 | { | |
7267c094 | 342 | g_free (opaque); |
85571bc7 | 343 | } |
1d14ffa9 | 344 | |
1ea879e5 | 345 | void mixeng_clear (struct st_sample *buf, int len) |
1d14ffa9 | 346 | { |
1ea879e5 | 347 | memset (buf, 0, len * sizeof (struct st_sample)); |
1d14ffa9 | 348 | } |
00e07679 MW |
349 | |
350 | void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol) | |
351 | { | |
00e07679 MW |
352 | if (vol->mute) { |
353 | mixeng_clear (buf, len); | |
354 | return; | |
355 | } | |
356 | ||
357 | while (len--) { | |
358 | #ifdef FLOAT_MIXENG | |
359 | buf->l = buf->l * vol->l; | |
360 | buf->r = buf->r * vol->r; | |
361 | #else | |
362 | buf->l = (buf->l * vol->l) >> 32; | |
363 | buf->r = (buf->r * vol->r) >> 32; | |
364 | #endif | |
365 | buf += 1; | |
366 | } | |
00e07679 | 367 | } |