master
c 607 lines 15.3 KB
Raw
1 /*
2 * QEMU Mixing engine
3 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
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 */
25 #include "qemu/osdep.h"
26 #include "qemu/bswap.h"
27 #include "qemu/audio.h"
28
29 #include "audio_int.h"
30 #ifdef FLOAT_MIXENG
31 #include "qemu/error-report.h"
32 #endif
33
34 /* 8 bit */
35 #define ENDIAN_CONVERSION natural
36 #define ENDIAN_CONVERT(v) (v)
37
38 /* Signed 8 bit */
39 #define BSIZE 8
40 #define ITYPE int
41 #define IN_MIN SCHAR_MIN
42 #define IN_MAX SCHAR_MAX
43 #define SIGNED
44 #define SHIFT 8
45 #include "mixeng_template.h"
46 #undef SIGNED
47 #undef IN_MAX
48 #undef IN_MIN
49 #undef BSIZE
50 #undef ITYPE
51 #undef SHIFT
52
53 /* Unsigned 8 bit */
54 #define BSIZE 8
55 #define ITYPE uint
56 #define IN_MIN 0
57 #define IN_MAX UCHAR_MAX
58 #define SHIFT 8
59 #include "mixeng_template.h"
60 #undef IN_MAX
61 #undef IN_MIN
62 #undef BSIZE
63 #undef ITYPE
64 #undef SHIFT
65
66 #undef ENDIAN_CONVERT
67 #undef ENDIAN_CONVERSION
68
69 /* Signed 16 bit */
70 #define BSIZE 16
71 #define ITYPE int
72 #define IN_MIN SHRT_MIN
73 #define IN_MAX SHRT_MAX
74 #define SIGNED
75 #define SHIFT 16
76 #define ENDIAN_CONVERSION natural
77 #define ENDIAN_CONVERT(v) (v)
78 #include "mixeng_template.h"
79 #undef ENDIAN_CONVERT
80 #undef ENDIAN_CONVERSION
81 #define ENDIAN_CONVERSION swap
82 #define ENDIAN_CONVERT(v) bswap16 (v)
83 #include "mixeng_template.h"
84 #undef ENDIAN_CONVERT
85 #undef ENDIAN_CONVERSION
86 #undef SIGNED
87 #undef IN_MAX
88 #undef IN_MIN
89 #undef BSIZE
90 #undef ITYPE
91 #undef SHIFT
92
93 /* Unsigned 16 bit */
94 #define BSIZE 16
95 #define ITYPE uint
96 #define IN_MIN 0
97 #define IN_MAX USHRT_MAX
98 #define SHIFT 16
99 #define ENDIAN_CONVERSION natural
100 #define ENDIAN_CONVERT(v) (v)
101 #include "mixeng_template.h"
102 #undef ENDIAN_CONVERT
103 #undef ENDIAN_CONVERSION
104 #define ENDIAN_CONVERSION swap
105 #define ENDIAN_CONVERT(v) bswap16 (v)
106 #include "mixeng_template.h"
107 #undef ENDIAN_CONVERT
108 #undef ENDIAN_CONVERSION
109 #undef IN_MAX
110 #undef IN_MIN
111 #undef BSIZE
112 #undef ITYPE
113 #undef SHIFT
114
115 /* Signed 32 bit */
116 #define BSIZE 32
117 #define ITYPE int
118 #define IN_MIN INT32_MIN
119 #define IN_MAX INT32_MAX
120 #define SIGNED
121 #define SHIFT 32
122 #define ENDIAN_CONVERSION natural
123 #define ENDIAN_CONVERT(v) (v)
124 #include "mixeng_template.h"
125 #undef ENDIAN_CONVERT
126 #undef ENDIAN_CONVERSION
127 #define ENDIAN_CONVERSION swap
128 #define ENDIAN_CONVERT(v) bswap32 (v)
129 #include "mixeng_template.h"
130 #undef ENDIAN_CONVERT
131 #undef ENDIAN_CONVERSION
132 #undef SIGNED
133 #undef IN_MAX
134 #undef IN_MIN
135 #undef BSIZE
136 #undef ITYPE
137 #undef SHIFT
138
139 /* Unsigned 32 bit */
140 #define BSIZE 32
141 #define ITYPE uint
142 #define IN_MIN 0
143 #define IN_MAX UINT32_MAX
144 #define SHIFT 32
145 #define ENDIAN_CONVERSION natural
146 #define ENDIAN_CONVERT(v) (v)
147 #include "mixeng_template.h"
148 #undef ENDIAN_CONVERT
149 #undef ENDIAN_CONVERSION
150 #define ENDIAN_CONVERSION swap
151 #define ENDIAN_CONVERT(v) bswap32 (v)
152 #include "mixeng_template.h"
153 #undef ENDIAN_CONVERT
154 #undef ENDIAN_CONVERSION
155 #undef IN_MAX
156 #undef IN_MIN
157 #undef BSIZE
158 #undef ITYPE
159 #undef SHIFT
160
161 t_sample *mixeng_conv[2][2][2][3] = {
162 {
163 {
164 {
165 conv_natural_uint8_t_to_mono,
166 conv_natural_uint16_t_to_mono,
167 conv_natural_uint32_t_to_mono
168 },
169 {
170 conv_natural_uint8_t_to_mono,
171 conv_swap_uint16_t_to_mono,
172 conv_swap_uint32_t_to_mono,
173 }
174 },
175 {
176 {
177 conv_natural_int8_t_to_mono,
178 conv_natural_int16_t_to_mono,
179 conv_natural_int32_t_to_mono
180 },
181 {
182 conv_natural_int8_t_to_mono,
183 conv_swap_int16_t_to_mono,
184 conv_swap_int32_t_to_mono
185 }
186 }
187 },
188 {
189 {
190 {
191 conv_natural_uint8_t_to_stereo,
192 conv_natural_uint16_t_to_stereo,
193 conv_natural_uint32_t_to_stereo
194 },
195 {
196 conv_natural_uint8_t_to_stereo,
197 conv_swap_uint16_t_to_stereo,
198 conv_swap_uint32_t_to_stereo
199 }
200 },
201 {
202 {
203 conv_natural_int8_t_to_stereo,
204 conv_natural_int16_t_to_stereo,
205 conv_natural_int32_t_to_stereo
206 },
207 {
208 conv_natural_int8_t_to_stereo,
209 conv_swap_int16_t_to_stereo,
210 conv_swap_int32_t_to_stereo,
211 }
212 }
213 }
214 };
215
216 f_sample *mixeng_clip[2][2][2][3] = {
217 {
218 {
219 {
220 clip_natural_uint8_t_from_mono,
221 clip_natural_uint16_t_from_mono,
222 clip_natural_uint32_t_from_mono
223 },
224 {
225 clip_natural_uint8_t_from_mono,
226 clip_swap_uint16_t_from_mono,
227 clip_swap_uint32_t_from_mono
228 }
229 },
230 {
231 {
232 clip_natural_int8_t_from_mono,
233 clip_natural_int16_t_from_mono,
234 clip_natural_int32_t_from_mono
235 },
236 {
237 clip_natural_int8_t_from_mono,
238 clip_swap_int16_t_from_mono,
239 clip_swap_int32_t_from_mono
240 }
241 }
242 },
243 {
244 {
245 {
246 clip_natural_uint8_t_from_stereo,
247 clip_natural_uint16_t_from_stereo,
248 clip_natural_uint32_t_from_stereo
249 },
250 {
251 clip_natural_uint8_t_from_stereo,
252 clip_swap_uint16_t_from_stereo,
253 clip_swap_uint32_t_from_stereo
254 }
255 },
256 {
257 {
258 clip_natural_int8_t_from_stereo,
259 clip_natural_int16_t_from_stereo,
260 clip_natural_int32_t_from_stereo
261 },
262 {
263 clip_natural_int8_t_from_stereo,
264 clip_swap_int16_t_from_stereo,
265 clip_swap_int32_t_from_stereo
266 }
267 }
268 }
269 };
270
271 #ifdef FLOAT_MIXENG
272 #define CONV_NATURAL_FLOAT(x) (x)
273 #define CLIP_NATURAL_FLOAT(x) (x)
274 #else
275 /* macros to map [-1.f, 1.f] <-> [INT32_MIN, INT32_MAX + 1] */
276 static const float float_scale = (int64_t)INT32_MAX + 1;
277 #define CONV_NATURAL_FLOAT(x) ((x) * float_scale)
278
279 #ifdef RECIPROCAL
280 static const float float_scale_reciprocal = 1.f / ((int64_t)INT32_MAX + 1);
281 #define CLIP_NATURAL_FLOAT(x) ((x) * float_scale_reciprocal)
282 #else
283 #define CLIP_NATURAL_FLOAT(x) ((x) / float_scale)
284 #endif
285 #endif
286
287 #define F32_TO_F32S(v) \
288 bswap32((union { uint32_t i; float f; }){ .f = (v) }.i)
289 #define F32S_TO_F32(v) \
290 ((union { uint32_t i; float f; }){ .i = bswap32(v) }.f)
291
292 static void conv_natural_float_to_mono(struct st_sample *dst, const void *src,
293 int samples)
294 {
295 const float *in = src;
296
297 while (samples--) {
298 dst->r = dst->l = CONV_NATURAL_FLOAT(*in++);
299 dst++;
300 }
301 }
302
303 static void conv_swap_float_to_mono(struct st_sample *dst, const void *src,
304 int samples)
305 {
306 const uint32_t *in_f32s = src;
307
308 while (samples--) {
309 dst->r = dst->l = CONV_NATURAL_FLOAT(F32S_TO_F32(*in_f32s++));
310 dst++;
311 }
312 }
313
314 static void conv_natural_float_to_stereo(struct st_sample *dst, const void *src,
315 int samples)
316 {
317 const float *in = src;
318
319 while (samples--) {
320 dst->l = CONV_NATURAL_FLOAT(*in++);
321 dst->r = CONV_NATURAL_FLOAT(*in++);
322 dst++;
323 }
324 }
325
326 static void conv_swap_float_to_stereo(struct st_sample *dst, const void *src,
327 int samples)
328 {
329 const uint32_t *in_f32s = src;
330
331 while (samples--) {
332 dst->l = CONV_NATURAL_FLOAT(F32S_TO_F32(*in_f32s++));
333 dst->r = CONV_NATURAL_FLOAT(F32S_TO_F32(*in_f32s++));
334 dst++;
335 }
336 }
337
338 t_sample *mixeng_conv_float[2][2] = {
339 {
340 conv_natural_float_to_mono,
341 conv_swap_float_to_mono,
342 },
343 {
344 conv_natural_float_to_stereo,
345 conv_swap_float_to_stereo,
346 }
347 };
348
349 static void clip_natural_float_from_mono(void *dst, const struct st_sample *src,
350 int samples)
351 {
352 float *out = dst;
353
354 while (samples--) {
355 *out++ = CLIP_NATURAL_FLOAT(src->l + src->r);
356 src++;
357 }
358 }
359
360 static void clip_swap_float_from_mono(void *dst, const struct st_sample *src,
361 int samples)
362 {
363 uint32_t *out_f32s = dst;
364
365 while (samples--) {
366 *out_f32s++ = F32_TO_F32S(CLIP_NATURAL_FLOAT(src->l + src->r));
367 src++;
368 }
369 }
370
371 static void clip_natural_float_from_stereo(
372 void *dst, const struct st_sample *src, int samples)
373 {
374 float *out = dst;
375
376 while (samples--) {
377 *out++ = CLIP_NATURAL_FLOAT(src->l);
378 *out++ = CLIP_NATURAL_FLOAT(src->r);
379 src++;
380 }
381 }
382
383 static void clip_swap_float_from_stereo(
384 void *dst, const struct st_sample *src, int samples)
385 {
386 uint32_t *out_f32s = dst;
387
388 while (samples--) {
389 *out_f32s++ = F32_TO_F32S(CLIP_NATURAL_FLOAT(src->l));
390 *out_f32s++ = F32_TO_F32S(CLIP_NATURAL_FLOAT(src->r));
391 src++;
392 }
393 }
394
395 f_sample *mixeng_clip_float[2][2] = {
396 {
397 clip_natural_float_from_mono,
398 clip_swap_float_from_mono,
399 },
400 {
401 clip_natural_float_from_stereo,
402 clip_swap_float_from_stereo,
403 }
404 };
405
406 void audio_sample_to_uint64(const st_sample *sample, int pos,
407 uint64_t *left, uint64_t *right)
408 {
409 #ifdef FLOAT_MIXENG
410 error_report(
411 "Coreaudio and floating point samples are not supported by replay yet");
412 abort();
413 #else
414 sample += pos;
415 *left = sample->l;
416 *right = sample->r;
417 #endif
418 }
419
420 void audio_sample_from_uint64(st_sample *sample, int pos,
421 uint64_t left, uint64_t right)
422 {
423 #ifdef FLOAT_MIXENG
424 error_report(
425 "Coreaudio and floating point samples are not supported by replay yet");
426 abort();
427 #else
428 sample += pos;
429 sample->l = left;
430 sample->r = right;
431 #endif
432 }
433
434 /*
435 * August 21, 1998
436 * Copyright 1998 Fabrice Bellard.
437 *
438 * [Rewrote completely the code of Lance Norskog And Sundry
439 * Contributors with a more efficient algorithm.]
440 *
441 * This source code is freely redistributable and may be used for
442 * any purpose. This copyright notice must be maintained.
443 * Lance Norskog And Sundry Contributors are not responsible for
444 * the consequences of using this software.
445 */
446
447 /*
448 * Sound Tools rate change effect file.
449 */
450 /*
451 * Linear Interpolation.
452 *
453 * The use of fractional increment allows us to use no buffer. It
454 * avoid the problems at the end of the buffer we had with the old
455 * method which stored a possibly big buffer of size
456 * lcm(in_rate,out_rate).
457 *
458 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
459 * the input & output frequencies are equal, a delay of one sample is
460 * introduced. Limited to processing 32-bit count worth of samples.
461 *
462 * 1 << FRAC_BITS evaluating to zero in several places. Changed with
463 * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
464 */
465
466 /* Private data */
467 struct rate {
468 uint64_t opos;
469 uint64_t opos_inc;
470 uint32_t ipos; /* position in the input stream (integer) */
471 struct st_sample ilast; /* last sample in the input stream */
472 };
473
474 /*
475 * Prepare processing.
476 */
477 void *st_rate_start (int inrate, int outrate)
478 {
479 struct rate *rate = g_new0(struct rate, 1);
480
481 rate->opos = 0;
482
483 /* increment */
484 rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
485
486 rate->ipos = 0;
487 rate->ilast.l = 0;
488 rate->ilast.r = 0;
489 return rate;
490 }
491
492 #define NAME st_rate_flow_mix
493 #define OP(a, b) a += b
494 #include "rate_template.h"
495
496 #define NAME st_rate_flow
497 #define OP(a, b) a = b
498 #include "rate_template.h"
499
500 void st_rate_stop (void *opaque)
501 {
502 g_free (opaque);
503 }
504
505 /**
506 * st_rate_frames_out() - returns the number of frames the resampling code
507 * generates from frames_in frames
508 *
509 * @opaque: pointer to struct rate
510 * @frames_in: number of frames
511 *
512 * When upsampling, there may be more than one correct result. In this case,
513 * the function returns the maximum number of output frames the resampling
514 * code can generate.
515 */
516 uint32_t st_rate_frames_out(void *opaque, uint32_t frames_in)
517 {
518 struct rate *rate = opaque;
519 uint64_t opos_end, opos_delta;
520 uint32_t ipos_end;
521 uint32_t frames_out;
522
523 if (rate->opos_inc == 1ULL << 32) {
524 return frames_in;
525 }
526
527 /* no output frame without at least one input frame */
528 if (!frames_in) {
529 return 0;
530 }
531
532 /* last frame read was at rate->ipos - 1 */
533 ipos_end = rate->ipos - 1 + frames_in;
534 opos_end = (uint64_t)ipos_end << 32;
535
536 /* last frame written was at rate->opos - rate->opos_inc */
537 if (opos_end + rate->opos_inc <= rate->opos) {
538 return 0;
539 }
540 opos_delta = opos_end - rate->opos + rate->opos_inc;
541 frames_out = opos_delta / rate->opos_inc;
542
543 return opos_delta % rate->opos_inc ? frames_out : frames_out - 1;
544 }
545
546 /**
547 * st_rate_frames_in() - returns the number of frames needed to
548 * get frames_out frames after resampling
549 *
550 * @opaque: pointer to struct rate
551 * @frames_out: number of frames
552 *
553 * When downsampling, there may be more than one correct result. In this
554 * case, the function returns the maximum number of input frames needed.
555 */
556 uint32_t st_rate_frames_in(void *opaque, uint32_t frames_out)
557 {
558 struct rate *rate = opaque;
559 uint64_t opos_start, opos_end;
560 uint32_t ipos_start, ipos_end;
561
562 if (rate->opos_inc == 1ULL << 32) {
563 return frames_out;
564 }
565
566 if (frames_out) {
567 opos_start = rate->opos;
568 ipos_start = rate->ipos;
569 } else {
570 uint64_t offset;
571
572 /* add offset = ceil(opos_inc) to opos and ipos to avoid an underflow */
573 offset = (rate->opos_inc + (1ULL << 32) - 1) & ~((1ULL << 32) - 1);
574 opos_start = rate->opos + offset;
575 ipos_start = rate->ipos + (offset >> 32);
576 }
577 /* last frame written was at opos_start - rate->opos_inc */
578 opos_end = opos_start - rate->opos_inc + rate->opos_inc * frames_out;
579 ipos_end = (opos_end >> 32) + 1;
580
581 /* last frame read was at ipos_start - 1 */
582 return ipos_end + 1 > ipos_start ? ipos_end + 1 - ipos_start : 0;
583 }
584
585 void mixeng_clear (struct st_sample *buf, int len)
586 {
587 memset (buf, 0, len * sizeof (struct st_sample));
588 }
589
590 void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
591 {
592 if (vol->mute) {
593 mixeng_clear (buf, len);
594 return;
595 }
596
597 while (len--) {
598 #ifdef FLOAT_MIXENG
599 buf->l = buf->l * vol->l;
600 buf->r = buf->r * vol->r;
601 #else
602 buf->l = (buf->l * vol->l) >> 32;
603 buf->r = (buf->r * vol->r) >> 32;
604 #endif
605 buf += 1;
606 }
607 }