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