]> git.proxmox.com Git - mirror_qemu.git/blame - audio/mixeng.c
coreaudio: fix coreaudio playback
[mirror_qemu.git] / audio / mixeng.c
CommitLineData
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"
58369e22 26#include "qemu/bswap.h"
3d4d16f4 27#include "qemu/error-report.h"
87ecb68b 28#include "audio.h"
85571bc7 29
1d14ffa9
FB
30#define AUDIO_CAP "mixeng"
31#include "audio_int.h"
32
1d14ffa9
FB
33/* 8 bit */
34#define ENDIAN_CONVERSION natural
35#define ENDIAN_CONVERT(v) (v)
36
37/* Signed 8 bit */
a2885387
RPM
38#define BSIZE 8
39#define ITYPE int
1d14ffa9
FB
40#define IN_MIN SCHAR_MIN
41#define IN_MAX SCHAR_MAX
85571bc7 42#define SIGNED
1d14ffa9 43#define SHIFT 8
85571bc7
FB
44#include "mixeng_template.h"
45#undef SIGNED
46#undef IN_MAX
47#undef IN_MIN
a2885387
RPM
48#undef BSIZE
49#undef ITYPE
1d14ffa9 50#undef SHIFT
85571bc7 51
1d14ffa9 52/* Unsigned 8 bit */
a2885387
RPM
53#define BSIZE 8
54#define ITYPE uint
85571bc7
FB
55#define IN_MIN 0
56#define IN_MAX UCHAR_MAX
1d14ffa9 57#define SHIFT 8
85571bc7
FB
58#include "mixeng_template.h"
59#undef IN_MAX
60#undef IN_MIN
a2885387
RPM
61#undef BSIZE
62#undef ITYPE
1d14ffa9
FB
63#undef SHIFT
64
65#undef ENDIAN_CONVERT
66#undef ENDIAN_CONVERSION
85571bc7 67
1d14ffa9 68/* Signed 16 bit */
a2885387
RPM
69#define BSIZE 16
70#define ITYPE int
85571bc7
FB
71#define IN_MIN SHRT_MIN
72#define IN_MAX SHRT_MAX
73#define SIGNED
1d14ffa9
FB
74#define SHIFT 16
75#define ENDIAN_CONVERSION natural
76#define ENDIAN_CONVERT(v) (v)
85571bc7 77#include "mixeng_template.h"
1d14ffa9
FB
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
85571bc7
FB
85#undef SIGNED
86#undef IN_MAX
87#undef IN_MIN
a2885387
RPM
88#undef BSIZE
89#undef ITYPE
1d14ffa9 90#undef SHIFT
85571bc7 91
f941aa25 92/* Unsigned 16 bit */
a2885387
RPM
93#define BSIZE 16
94#define ITYPE uint
85571bc7
FB
95#define IN_MIN 0
96#define IN_MAX USHRT_MAX
1d14ffa9
FB
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)
85571bc7 105#include "mixeng_template.h"
1d14ffa9
FB
106#undef ENDIAN_CONVERT
107#undef ENDIAN_CONVERSION
85571bc7
FB
108#undef IN_MAX
109#undef IN_MIN
a2885387
RPM
110#undef BSIZE
111#undef ITYPE
1d14ffa9 112#undef SHIFT
85571bc7 113
f941aa25 114/* Signed 32 bit */
a2885387
RPM
115#define BSIZE 32
116#define ITYPE int
f941aa25
TS
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
a2885387
RPM
134#undef BSIZE
135#undef ITYPE
f941aa25
TS
136#undef SHIFT
137
ad483a51 138/* Unsigned 32 bit */
a2885387
RPM
139#define BSIZE 32
140#define ITYPE uint
f941aa25
TS
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
a2885387
RPM
156#undef BSIZE
157#undef ITYPE
f941aa25
TS
158#undef SHIFT
159
160t_sample *mixeng_conv[2][2][2][3] = {
85571bc7
FB
161 {
162 {
1d14ffa9
FB
163 {
164 conv_natural_uint8_t_to_mono,
f941aa25
TS
165 conv_natural_uint16_t_to_mono,
166 conv_natural_uint32_t_to_mono
1d14ffa9
FB
167 },
168 {
169 conv_natural_uint8_t_to_mono,
f941aa25
TS
170 conv_swap_uint16_t_to_mono,
171 conv_swap_uint32_t_to_mono,
1d14ffa9 172 }
85571bc7
FB
173 },
174 {
1d14ffa9
FB
175 {
176 conv_natural_int8_t_to_mono,
f941aa25
TS
177 conv_natural_int16_t_to_mono,
178 conv_natural_int32_t_to_mono
1d14ffa9
FB
179 },
180 {
181 conv_natural_int8_t_to_mono,
f941aa25
TS
182 conv_swap_int16_t_to_mono,
183 conv_swap_int32_t_to_mono
1d14ffa9 184 }
85571bc7
FB
185 }
186 },
187 {
188 {
1d14ffa9
FB
189 {
190 conv_natural_uint8_t_to_stereo,
f941aa25
TS
191 conv_natural_uint16_t_to_stereo,
192 conv_natural_uint32_t_to_stereo
1d14ffa9
FB
193 },
194 {
195 conv_natural_uint8_t_to_stereo,
f941aa25
TS
196 conv_swap_uint16_t_to_stereo,
197 conv_swap_uint32_t_to_stereo
1d14ffa9 198 }
85571bc7
FB
199 },
200 {
1d14ffa9
FB
201 {
202 conv_natural_int8_t_to_stereo,
f941aa25
TS
203 conv_natural_int16_t_to_stereo,
204 conv_natural_int32_t_to_stereo
1d14ffa9
FB
205 },
206 {
207 conv_natural_int8_t_to_stereo,
f941aa25
TS
208 conv_swap_int16_t_to_stereo,
209 conv_swap_int32_t_to_stereo,
1d14ffa9 210 }
85571bc7
FB
211 }
212 }
213};
214
f941aa25 215f_sample *mixeng_clip[2][2][2][3] = {
85571bc7
FB
216 {
217 {
1d14ffa9
FB
218 {
219 clip_natural_uint8_t_from_mono,
f941aa25
TS
220 clip_natural_uint16_t_from_mono,
221 clip_natural_uint32_t_from_mono
1d14ffa9
FB
222 },
223 {
224 clip_natural_uint8_t_from_mono,
f941aa25
TS
225 clip_swap_uint16_t_from_mono,
226 clip_swap_uint32_t_from_mono
1d14ffa9 227 }
85571bc7
FB
228 },
229 {
1d14ffa9
FB
230 {
231 clip_natural_int8_t_from_mono,
f941aa25
TS
232 clip_natural_int16_t_from_mono,
233 clip_natural_int32_t_from_mono
1d14ffa9
FB
234 },
235 {
236 clip_natural_int8_t_from_mono,
f941aa25
TS
237 clip_swap_int16_t_from_mono,
238 clip_swap_int32_t_from_mono
1d14ffa9 239 }
85571bc7
FB
240 }
241 },
242 {
243 {
1d14ffa9
FB
244 {
245 clip_natural_uint8_t_from_stereo,
f941aa25
TS
246 clip_natural_uint16_t_from_stereo,
247 clip_natural_uint32_t_from_stereo
1d14ffa9
FB
248 },
249 {
250 clip_natural_uint8_t_from_stereo,
f941aa25
TS
251 clip_swap_uint16_t_from_stereo,
252 clip_swap_uint32_t_from_stereo
1d14ffa9 253 }
85571bc7
FB
254 },
255 {
1d14ffa9
FB
256 {
257 clip_natural_int8_t_from_stereo,
f941aa25
TS
258 clip_natural_int16_t_from_stereo,
259 clip_natural_int32_t_from_stereo
1d14ffa9
FB
260 },
261 {
262 clip_natural_int8_t_from_stereo,
f941aa25
TS
263 clip_swap_int16_t_from_stereo,
264 clip_swap_int32_t_from_stereo
1d14ffa9 265 }
85571bc7
FB
266 }
267 }
268};
269
180b044f
VR
270void conv_natural_float_to_stereo(struct st_sample *dst, const void *src,
271 int samples)
272{
273 float *in = (float *)src;
274#ifndef FLOAT_MIXENG
275 const float scale = UINT_MAX;
276#endif
277
278 while (samples--) {
279#ifdef FLOAT_MIXENG
280 dst->l = *in++;
281 dst->r = *in++;
282#else
283 dst->l = *in++ * scale;
284 dst->r = *in++ * scale;
285#endif
286 dst++;
287 }
288}
289
290void clip_natural_float_from_stereo(void *dst, const struct st_sample *src,
291 int samples)
292{
293 float *out = (float *)dst;
294#ifndef FLOAT_MIXENG
295#ifdef RECIPROCAL
296 const float scale = 1.f / UINT_MAX;
297#else
298 const float scale = UINT_MAX;
299#endif
300#endif
301
302 while (samples--) {
303#ifdef FLOAT_MIXENG
304 *out++ = src->l;
305 *out++ = src->r;
306#else
307#ifdef RECIPROCAL
308 *out++ = src->l * scale;
309 *out++ = src->r * scale;
310#else
311 *out++ = src->l / scale;
312 *out++ = src->r / scale;
313#endif
314#endif
315 src++;
316 }
317}
3d4d16f4
PD
318
319void audio_sample_to_uint64(void *samples, int pos,
320 uint64_t *left, uint64_t *right)
321{
322 struct st_sample *sample = samples;
323 sample += pos;
324#ifdef FLOAT_MIXENG
325 error_report(
326 "Coreaudio and floating point samples are not supported by replay yet");
327 abort();
328#else
329 *left = sample->l;
330 *right = sample->r;
331#endif
332}
333
334void audio_sample_from_uint64(void *samples, int pos,
335 uint64_t left, uint64_t right)
336{
337 struct st_sample *sample = samples;
338 sample += pos;
339#ifdef FLOAT_MIXENG
340 error_report(
341 "Coreaudio and floating point samples are not supported by replay yet");
342 abort();
343#else
344 sample->l = left;
345 sample->r = right;
346#endif
347}
348
85571bc7
FB
349/*
350 * August 21, 1998
351 * Copyright 1998 Fabrice Bellard.
352 *
cb8d4c8f 353 * [Rewrote completely the code of Lance Norskog And Sundry
85571bc7
FB
354 * Contributors with a more efficient algorithm.]
355 *
356 * This source code is freely redistributable and may be used for
1d14ffa9
FB
357 * any purpose. This copyright notice must be maintained.
358 * Lance Norskog And Sundry Contributors are not responsible for
359 * the consequences of using this software.
85571bc7
FB
360 */
361
362/*
363 * Sound Tools rate change effect file.
364 */
365/*
366 * Linear Interpolation.
367 *
368 * The use of fractional increment allows us to use no buffer. It
369 * avoid the problems at the end of the buffer we had with the old
370 * method which stored a possibly big buffer of size
371 * lcm(in_rate,out_rate).
372 *
373 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
374 * the input & output frequencies are equal, a delay of one sample is
375 * introduced. Limited to processing 32-bit count worth of samples.
376 *
377 * 1 << FRAC_BITS evaluating to zero in several places. Changed with
378 * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
379 */
380
381/* Private data */
c0fe3827 382struct rate {
85571bc7
FB
383 uint64_t opos;
384 uint64_t opos_inc;
385 uint32_t ipos; /* position in the input stream (integer) */
1ea879e5 386 struct st_sample ilast; /* last sample in the input stream */
c0fe3827 387};
85571bc7
FB
388
389/*
390 * Prepare processing.
391 */
392void *st_rate_start (int inrate, int outrate)
393{
470bcabd 394 struct rate *rate = audio_calloc(__func__, 1, sizeof(*rate));
85571bc7
FB
395
396 if (!rate) {
e7cad338 397 dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate));
1d14ffa9 398 return NULL;
85571bc7
FB
399 }
400
401 rate->opos = 0;
402
403 /* increment */
1d14ffa9 404 rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
85571bc7
FB
405
406 rate->ipos = 0;
407 rate->ilast.l = 0;
408 rate->ilast.r = 0;
409 return rate;
410}
411
1d14ffa9
FB
412#define NAME st_rate_flow_mix
413#define OP(a, b) a += b
414#include "rate_template.h"
85571bc7 415
1d14ffa9
FB
416#define NAME st_rate_flow
417#define OP(a, b) a = b
418#include "rate_template.h"
85571bc7
FB
419
420void st_rate_stop (void *opaque)
421{
7267c094 422 g_free (opaque);
85571bc7 423}
1d14ffa9 424
1ea879e5 425void mixeng_clear (struct st_sample *buf, int len)
1d14ffa9 426{
1ea879e5 427 memset (buf, 0, len * sizeof (struct st_sample));
1d14ffa9 428}
00e07679
MW
429
430void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
431{
00e07679
MW
432 if (vol->mute) {
433 mixeng_clear (buf, len);
434 return;
435 }
436
437 while (len--) {
438#ifdef FLOAT_MIXENG
439 buf->l = buf->l * vol->l;
440 buf->r = buf->r * vol->r;
441#else
442 buf->l = (buf->l * vol->l) >> 32;
443 buf->r = (buf->r * vol->r) >> 32;
444#endif
445 buf += 1;
446 }
00e07679 447}