qemu-img bench: Add --flush-interval
[qemu.git] / audio / mixeng.c
blob66c0328d4288b931a7dafe6f40728920f9110b75
1 /*
2 * QEMU Mixing engine
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 * Copyright (c) 1998 Fabrice Bellard
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/bswap.h"
28 #include "audio.h"
30 #define AUDIO_CAP "mixeng"
31 #include "audio_int.h"
33 /* 8 bit */
34 #define ENDIAN_CONVERSION natural
35 #define ENDIAN_CONVERT(v) (v)
37 /* Signed 8 bit */
38 #define BSIZE 8
39 #define ITYPE int
40 #define IN_MIN SCHAR_MIN
41 #define IN_MAX SCHAR_MAX
42 #define SIGNED
43 #define SHIFT 8
44 #include "mixeng_template.h"
45 #undef SIGNED
46 #undef IN_MAX
47 #undef IN_MIN
48 #undef BSIZE
49 #undef ITYPE
50 #undef SHIFT
52 /* Unsigned 8 bit */
53 #define BSIZE 8
54 #define ITYPE uint
55 #define IN_MIN 0
56 #define IN_MAX UCHAR_MAX
57 #define SHIFT 8
58 #include "mixeng_template.h"
59 #undef IN_MAX
60 #undef IN_MIN
61 #undef BSIZE
62 #undef ITYPE
63 #undef SHIFT
65 #undef ENDIAN_CONVERT
66 #undef ENDIAN_CONVERSION
68 /* Signed 16 bit */
69 #define BSIZE 16
70 #define ITYPE int
71 #define IN_MIN SHRT_MIN
72 #define IN_MAX SHRT_MAX
73 #define SIGNED
74 #define SHIFT 16
75 #define ENDIAN_CONVERSION natural
76 #define ENDIAN_CONVERT(v) (v)
77 #include "mixeng_template.h"
78 #undef ENDIAN_CONVERT
79 #undef ENDIAN_CONVERSION
80 #define ENDIAN_CONVERSION swap
81 #define ENDIAN_CONVERT(v) bswap16 (v)
82 #include "mixeng_template.h"
83 #undef ENDIAN_CONVERT
84 #undef ENDIAN_CONVERSION
85 #undef SIGNED
86 #undef IN_MAX
87 #undef IN_MIN
88 #undef BSIZE
89 #undef ITYPE
90 #undef SHIFT
92 /* Unsigned 16 bit */
93 #define BSIZE 16
94 #define ITYPE uint
95 #define IN_MIN 0
96 #define IN_MAX USHRT_MAX
97 #define SHIFT 16
98 #define ENDIAN_CONVERSION natural
99 #define ENDIAN_CONVERT(v) (v)
100 #include "mixeng_template.h"
101 #undef ENDIAN_CONVERT
102 #undef ENDIAN_CONVERSION
103 #define ENDIAN_CONVERSION swap
104 #define ENDIAN_CONVERT(v) bswap16 (v)
105 #include "mixeng_template.h"
106 #undef ENDIAN_CONVERT
107 #undef ENDIAN_CONVERSION
108 #undef IN_MAX
109 #undef IN_MIN
110 #undef BSIZE
111 #undef ITYPE
112 #undef SHIFT
114 /* Signed 32 bit */
115 #define BSIZE 32
116 #define ITYPE int
117 #define IN_MIN INT32_MIN
118 #define IN_MAX INT32_MAX
119 #define SIGNED
120 #define SHIFT 32
121 #define ENDIAN_CONVERSION natural
122 #define ENDIAN_CONVERT(v) (v)
123 #include "mixeng_template.h"
124 #undef ENDIAN_CONVERT
125 #undef ENDIAN_CONVERSION
126 #define ENDIAN_CONVERSION swap
127 #define ENDIAN_CONVERT(v) bswap32 (v)
128 #include "mixeng_template.h"
129 #undef ENDIAN_CONVERT
130 #undef ENDIAN_CONVERSION
131 #undef SIGNED
132 #undef IN_MAX
133 #undef IN_MIN
134 #undef BSIZE
135 #undef ITYPE
136 #undef SHIFT
138 /* Unsigned 32 bit */
139 #define BSIZE 32
140 #define ITYPE uint
141 #define IN_MIN 0
142 #define IN_MAX UINT32_MAX
143 #define SHIFT 32
144 #define ENDIAN_CONVERSION natural
145 #define ENDIAN_CONVERT(v) (v)
146 #include "mixeng_template.h"
147 #undef ENDIAN_CONVERT
148 #undef ENDIAN_CONVERSION
149 #define ENDIAN_CONVERSION swap
150 #define ENDIAN_CONVERT(v) bswap32 (v)
151 #include "mixeng_template.h"
152 #undef ENDIAN_CONVERT
153 #undef ENDIAN_CONVERSION
154 #undef IN_MAX
155 #undef IN_MIN
156 #undef BSIZE
157 #undef ITYPE
158 #undef SHIFT
160 t_sample *mixeng_conv[2][2][2][3] = {
164 conv_natural_uint8_t_to_mono,
165 conv_natural_uint16_t_to_mono,
166 conv_natural_uint32_t_to_mono
169 conv_natural_uint8_t_to_mono,
170 conv_swap_uint16_t_to_mono,
171 conv_swap_uint32_t_to_mono,
176 conv_natural_int8_t_to_mono,
177 conv_natural_int16_t_to_mono,
178 conv_natural_int32_t_to_mono
181 conv_natural_int8_t_to_mono,
182 conv_swap_int16_t_to_mono,
183 conv_swap_int32_t_to_mono
190 conv_natural_uint8_t_to_stereo,
191 conv_natural_uint16_t_to_stereo,
192 conv_natural_uint32_t_to_stereo
195 conv_natural_uint8_t_to_stereo,
196 conv_swap_uint16_t_to_stereo,
197 conv_swap_uint32_t_to_stereo
202 conv_natural_int8_t_to_stereo,
203 conv_natural_int16_t_to_stereo,
204 conv_natural_int32_t_to_stereo
207 conv_natural_int8_t_to_stereo,
208 conv_swap_int16_t_to_stereo,
209 conv_swap_int32_t_to_stereo,
215 f_sample *mixeng_clip[2][2][2][3] = {
219 clip_natural_uint8_t_from_mono,
220 clip_natural_uint16_t_from_mono,
221 clip_natural_uint32_t_from_mono
224 clip_natural_uint8_t_from_mono,
225 clip_swap_uint16_t_from_mono,
226 clip_swap_uint32_t_from_mono
231 clip_natural_int8_t_from_mono,
232 clip_natural_int16_t_from_mono,
233 clip_natural_int32_t_from_mono
236 clip_natural_int8_t_from_mono,
237 clip_swap_int16_t_from_mono,
238 clip_swap_int32_t_from_mono
245 clip_natural_uint8_t_from_stereo,
246 clip_natural_uint16_t_from_stereo,
247 clip_natural_uint32_t_from_stereo
250 clip_natural_uint8_t_from_stereo,
251 clip_swap_uint16_t_from_stereo,
252 clip_swap_uint32_t_from_stereo
257 clip_natural_int8_t_from_stereo,
258 clip_natural_int16_t_from_stereo,
259 clip_natural_int32_t_from_stereo
262 clip_natural_int8_t_from_stereo,
263 clip_swap_int16_t_from_stereo,
264 clip_swap_int32_t_from_stereo
271 * August 21, 1998
272 * Copyright 1998 Fabrice Bellard.
274 * [Rewrote completely the code of Lance Norskog And Sundry
275 * Contributors with a more efficient algorithm.]
277 * This source code is freely redistributable and may be used for
278 * any purpose. This copyright notice must be maintained.
279 * Lance Norskog And Sundry Contributors are not responsible for
280 * the consequences of using this software.
284 * Sound Tools rate change effect file.
287 * Linear Interpolation.
289 * The use of fractional increment allows us to use no buffer. It
290 * avoid the problems at the end of the buffer we had with the old
291 * method which stored a possibly big buffer of size
292 * lcm(in_rate,out_rate).
294 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
295 * the input & output frequencies are equal, a delay of one sample is
296 * introduced. Limited to processing 32-bit count worth of samples.
298 * 1 << FRAC_BITS evaluating to zero in several places. Changed with
299 * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
302 /* Private data */
303 struct rate {
304 uint64_t opos;
305 uint64_t opos_inc;
306 uint32_t ipos; /* position in the input stream (integer) */
307 struct st_sample ilast; /* last sample in the input stream */
311 * Prepare processing.
313 void *st_rate_start (int inrate, int outrate)
315 struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate));
317 if (!rate) {
318 dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate));
319 return NULL;
322 rate->opos = 0;
324 /* increment */
325 rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
327 rate->ipos = 0;
328 rate->ilast.l = 0;
329 rate->ilast.r = 0;
330 return rate;
333 #define NAME st_rate_flow_mix
334 #define OP(a, b) a += b
335 #include "rate_template.h"
337 #define NAME st_rate_flow
338 #define OP(a, b) a = b
339 #include "rate_template.h"
341 void st_rate_stop (void *opaque)
343 g_free (opaque);
346 void mixeng_clear (struct st_sample *buf, int len)
348 memset (buf, 0, len * sizeof (struct st_sample));
351 void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
353 if (vol->mute) {
354 mixeng_clear (buf, len);
355 return;
358 while (len--) {
359 #ifdef FLOAT_MIXENG
360 buf->l = buf->l * vol->l;
361 buf->r = buf->r * vol->r;
362 #else
363 buf->l = (buf->l * vol->l) >> 32;
364 buf->r = (buf->r * vol->r) >> 32;
365 #endif
366 buf += 1;