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