blob: 2ab22df2aa7c05a2424209995fc1d00263078724 [file] [log] [blame]
bellard85571bc2004-11-07 18:04:02 +00001/*
2 * QEMU Mixing engine
3 *
bellard1d14ffa2005-10-30 18:58:22 +00004 * Copyright (c) 2004-2005 Vassili Karpov (malc)
bellard85571bc2004-11-07 18:04:02 +00005 * 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 */
Peter Maydell6086a562016-01-18 17:33:52 +000025#include "qemu/osdep.h"
pbrook87ecb682007-11-17 17:14:51 +000026#include "qemu-common.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010027#include "qemu/bswap.h"
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +030028#include "qemu/error-report.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "audio.h"
bellard85571bc2004-11-07 18:04:02 +000030
bellard1d14ffa2005-10-30 18:58:22 +000031#define AUDIO_CAP "mixeng"
32#include "audio_int.h"
33
bellard1d14ffa2005-10-30 18:58:22 +000034/* 8 bit */
35#define ENDIAN_CONVERSION natural
36#define ENDIAN_CONVERT(v) (v)
37
38/* Signed 8 bit */
Roger Pau Monnea2885382012-05-18 12:08:14 +010039#define BSIZE 8
40#define ITYPE int
bellard1d14ffa2005-10-30 18:58:22 +000041#define IN_MIN SCHAR_MIN
42#define IN_MAX SCHAR_MAX
bellard85571bc2004-11-07 18:04:02 +000043#define SIGNED
bellard1d14ffa2005-10-30 18:58:22 +000044#define SHIFT 8
bellard85571bc2004-11-07 18:04:02 +000045#include "mixeng_template.h"
46#undef SIGNED
47#undef IN_MAX
48#undef IN_MIN
Roger Pau Monnea2885382012-05-18 12:08:14 +010049#undef BSIZE
50#undef ITYPE
bellard1d14ffa2005-10-30 18:58:22 +000051#undef SHIFT
bellard85571bc2004-11-07 18:04:02 +000052
bellard1d14ffa2005-10-30 18:58:22 +000053/* Unsigned 8 bit */
Roger Pau Monnea2885382012-05-18 12:08:14 +010054#define BSIZE 8
55#define ITYPE uint
bellard85571bc2004-11-07 18:04:02 +000056#define IN_MIN 0
57#define IN_MAX UCHAR_MAX
bellard1d14ffa2005-10-30 18:58:22 +000058#define SHIFT 8
bellard85571bc2004-11-07 18:04:02 +000059#include "mixeng_template.h"
60#undef IN_MAX
61#undef IN_MIN
Roger Pau Monnea2885382012-05-18 12:08:14 +010062#undef BSIZE
63#undef ITYPE
bellard1d14ffa2005-10-30 18:58:22 +000064#undef SHIFT
bellard85571bc2004-11-07 18:04:02 +000065
bellard1d14ffa2005-10-30 18:58:22 +000066#undef ENDIAN_CONVERT
67#undef ENDIAN_CONVERSION
68
69/* Signed 16 bit */
Roger Pau Monnea2885382012-05-18 12:08:14 +010070#define BSIZE 16
71#define ITYPE int
bellard85571bc2004-11-07 18:04:02 +000072#define IN_MIN SHRT_MIN
73#define IN_MAX SHRT_MAX
74#define SIGNED
bellard1d14ffa2005-10-30 18:58:22 +000075#define SHIFT 16
76#define ENDIAN_CONVERSION natural
77#define ENDIAN_CONVERT(v) (v)
bellard85571bc2004-11-07 18:04:02 +000078#include "mixeng_template.h"
bellard1d14ffa2005-10-30 18:58:22 +000079#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
bellard85571bc2004-11-07 18:04:02 +000086#undef SIGNED
87#undef IN_MAX
88#undef IN_MIN
Roger Pau Monnea2885382012-05-18 12:08:14 +010089#undef BSIZE
90#undef ITYPE
bellard1d14ffa2005-10-30 18:58:22 +000091#undef SHIFT
bellard85571bc2004-11-07 18:04:02 +000092
thsf941aa22007-02-17 22:19:29 +000093/* Unsigned 16 bit */
Roger Pau Monnea2885382012-05-18 12:08:14 +010094#define BSIZE 16
95#define ITYPE uint
bellard85571bc2004-11-07 18:04:02 +000096#define IN_MIN 0
97#define IN_MAX USHRT_MAX
bellard1d14ffa2005-10-30 18:58:22 +000098#define SHIFT 16
99#define ENDIAN_CONVERSION natural
100#define ENDIAN_CONVERT(v) (v)
bellard85571bc2004-11-07 18:04:02 +0000101#include "mixeng_template.h"
bellard1d14ffa2005-10-30 18:58:22 +0000102#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
bellard85571bc2004-11-07 18:04:02 +0000109#undef IN_MAX
110#undef IN_MIN
Roger Pau Monnea2885382012-05-18 12:08:14 +0100111#undef BSIZE
112#undef ITYPE
bellard1d14ffa2005-10-30 18:58:22 +0000113#undef SHIFT
bellard85571bc2004-11-07 18:04:02 +0000114
thsf941aa22007-02-17 22:19:29 +0000115/* Signed 32 bit */
Roger Pau Monnea2885382012-05-18 12:08:14 +0100116#define BSIZE 32
117#define ITYPE int
thsf941aa22007-02-17 22:19:29 +0000118#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
Roger Pau Monnea2885382012-05-18 12:08:14 +0100135#undef BSIZE
136#undef ITYPE
thsf941aa22007-02-17 22:19:29 +0000137#undef SHIFT
138
malcad483a52010-03-11 18:28:38 +0300139/* Unsigned 32 bit */
Roger Pau Monnea2885382012-05-18 12:08:14 +0100140#define BSIZE 32
141#define ITYPE uint
thsf941aa22007-02-17 22:19:29 +0000142#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
Roger Pau Monnea2885382012-05-18 12:08:14 +0100157#undef BSIZE
158#undef ITYPE
thsf941aa22007-02-17 22:19:29 +0000159#undef SHIFT
160
161t_sample *mixeng_conv[2][2][2][3] = {
bellard85571bc2004-11-07 18:04:02 +0000162 {
163 {
bellard1d14ffa2005-10-30 18:58:22 +0000164 {
165 conv_natural_uint8_t_to_mono,
thsf941aa22007-02-17 22:19:29 +0000166 conv_natural_uint16_t_to_mono,
167 conv_natural_uint32_t_to_mono
bellard1d14ffa2005-10-30 18:58:22 +0000168 },
169 {
170 conv_natural_uint8_t_to_mono,
thsf941aa22007-02-17 22:19:29 +0000171 conv_swap_uint16_t_to_mono,
172 conv_swap_uint32_t_to_mono,
bellard1d14ffa2005-10-30 18:58:22 +0000173 }
bellard85571bc2004-11-07 18:04:02 +0000174 },
175 {
bellard1d14ffa2005-10-30 18:58:22 +0000176 {
177 conv_natural_int8_t_to_mono,
thsf941aa22007-02-17 22:19:29 +0000178 conv_natural_int16_t_to_mono,
179 conv_natural_int32_t_to_mono
bellard1d14ffa2005-10-30 18:58:22 +0000180 },
181 {
182 conv_natural_int8_t_to_mono,
thsf941aa22007-02-17 22:19:29 +0000183 conv_swap_int16_t_to_mono,
184 conv_swap_int32_t_to_mono
bellard1d14ffa2005-10-30 18:58:22 +0000185 }
bellard85571bc2004-11-07 18:04:02 +0000186 }
187 },
188 {
189 {
bellard1d14ffa2005-10-30 18:58:22 +0000190 {
191 conv_natural_uint8_t_to_stereo,
thsf941aa22007-02-17 22:19:29 +0000192 conv_natural_uint16_t_to_stereo,
193 conv_natural_uint32_t_to_stereo
bellard1d14ffa2005-10-30 18:58:22 +0000194 },
195 {
196 conv_natural_uint8_t_to_stereo,
thsf941aa22007-02-17 22:19:29 +0000197 conv_swap_uint16_t_to_stereo,
198 conv_swap_uint32_t_to_stereo
bellard1d14ffa2005-10-30 18:58:22 +0000199 }
bellard85571bc2004-11-07 18:04:02 +0000200 },
201 {
bellard1d14ffa2005-10-30 18:58:22 +0000202 {
203 conv_natural_int8_t_to_stereo,
thsf941aa22007-02-17 22:19:29 +0000204 conv_natural_int16_t_to_stereo,
205 conv_natural_int32_t_to_stereo
bellard1d14ffa2005-10-30 18:58:22 +0000206 },
207 {
208 conv_natural_int8_t_to_stereo,
thsf941aa22007-02-17 22:19:29 +0000209 conv_swap_int16_t_to_stereo,
210 conv_swap_int32_t_to_stereo,
bellard1d14ffa2005-10-30 18:58:22 +0000211 }
bellard85571bc2004-11-07 18:04:02 +0000212 }
213 }
214};
215
thsf941aa22007-02-17 22:19:29 +0000216f_sample *mixeng_clip[2][2][2][3] = {
bellard85571bc2004-11-07 18:04:02 +0000217 {
218 {
bellard1d14ffa2005-10-30 18:58:22 +0000219 {
220 clip_natural_uint8_t_from_mono,
thsf941aa22007-02-17 22:19:29 +0000221 clip_natural_uint16_t_from_mono,
222 clip_natural_uint32_t_from_mono
bellard1d14ffa2005-10-30 18:58:22 +0000223 },
224 {
225 clip_natural_uint8_t_from_mono,
thsf941aa22007-02-17 22:19:29 +0000226 clip_swap_uint16_t_from_mono,
227 clip_swap_uint32_t_from_mono
bellard1d14ffa2005-10-30 18:58:22 +0000228 }
bellard85571bc2004-11-07 18:04:02 +0000229 },
230 {
bellard1d14ffa2005-10-30 18:58:22 +0000231 {
232 clip_natural_int8_t_from_mono,
thsf941aa22007-02-17 22:19:29 +0000233 clip_natural_int16_t_from_mono,
234 clip_natural_int32_t_from_mono
bellard1d14ffa2005-10-30 18:58:22 +0000235 },
236 {
237 clip_natural_int8_t_from_mono,
thsf941aa22007-02-17 22:19:29 +0000238 clip_swap_int16_t_from_mono,
239 clip_swap_int32_t_from_mono
bellard1d14ffa2005-10-30 18:58:22 +0000240 }
bellard85571bc2004-11-07 18:04:02 +0000241 }
242 },
243 {
244 {
bellard1d14ffa2005-10-30 18:58:22 +0000245 {
246 clip_natural_uint8_t_from_stereo,
thsf941aa22007-02-17 22:19:29 +0000247 clip_natural_uint16_t_from_stereo,
248 clip_natural_uint32_t_from_stereo
bellard1d14ffa2005-10-30 18:58:22 +0000249 },
250 {
251 clip_natural_uint8_t_from_stereo,
thsf941aa22007-02-17 22:19:29 +0000252 clip_swap_uint16_t_from_stereo,
253 clip_swap_uint32_t_from_stereo
bellard1d14ffa2005-10-30 18:58:22 +0000254 }
bellard85571bc2004-11-07 18:04:02 +0000255 },
256 {
bellard1d14ffa2005-10-30 18:58:22 +0000257 {
258 clip_natural_int8_t_from_stereo,
thsf941aa22007-02-17 22:19:29 +0000259 clip_natural_int16_t_from_stereo,
260 clip_natural_int32_t_from_stereo
bellard1d14ffa2005-10-30 18:58:22 +0000261 },
262 {
263 clip_natural_int8_t_from_stereo,
thsf941aa22007-02-17 22:19:29 +0000264 clip_swap_int16_t_from_stereo,
265 clip_swap_int32_t_from_stereo
bellard1d14ffa2005-10-30 18:58:22 +0000266 }
bellard85571bc2004-11-07 18:04:02 +0000267 }
268 }
269};
270
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +0300271
272void audio_sample_to_uint64(void *samples, int pos,
273 uint64_t *left, uint64_t *right)
274{
275 struct st_sample *sample = samples;
276 sample += pos;
277#ifdef FLOAT_MIXENG
278 error_report(
279 "Coreaudio and floating point samples are not supported by replay yet");
280 abort();
281#else
282 *left = sample->l;
283 *right = sample->r;
284#endif
285}
286
287void audio_sample_from_uint64(void *samples, int pos,
288 uint64_t left, uint64_t right)
289{
290 struct st_sample *sample = samples;
291 sample += pos;
292#ifdef FLOAT_MIXENG
293 error_report(
294 "Coreaudio and floating point samples are not supported by replay yet");
295 abort();
296#else
297 sample->l = left;
298 sample->r = right;
299#endif
300}
301
bellard85571bc2004-11-07 18:04:02 +0000302/*
303 * August 21, 1998
304 * Copyright 1998 Fabrice Bellard.
305 *
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100306 * [Rewrote completely the code of Lance Norskog And Sundry
bellard85571bc2004-11-07 18:04:02 +0000307 * Contributors with a more efficient algorithm.]
308 *
309 * This source code is freely redistributable and may be used for
bellard1d14ffa2005-10-30 18:58:22 +0000310 * any purpose. This copyright notice must be maintained.
311 * Lance Norskog And Sundry Contributors are not responsible for
312 * the consequences of using this software.
bellard85571bc2004-11-07 18:04:02 +0000313 */
314
315/*
316 * Sound Tools rate change effect file.
317 */
318/*
319 * Linear Interpolation.
320 *
321 * The use of fractional increment allows us to use no buffer. It
322 * avoid the problems at the end of the buffer we had with the old
323 * method which stored a possibly big buffer of size
324 * lcm(in_rate,out_rate).
325 *
326 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
327 * the input & output frequencies are equal, a delay of one sample is
328 * introduced. Limited to processing 32-bit count worth of samples.
329 *
330 * 1 << FRAC_BITS evaluating to zero in several places. Changed with
331 * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
332 */
333
334/* Private data */
bellardc0fe3822005-11-05 18:55:28 +0000335struct rate {
bellard85571bc2004-11-07 18:04:02 +0000336 uint64_t opos;
337 uint64_t opos_inc;
338 uint32_t ipos; /* position in the input stream (integer) */
malc1ea879e2008-12-03 22:48:44 +0000339 struct st_sample ilast; /* last sample in the input stream */
bellardc0fe3822005-11-05 18:55:28 +0000340};
bellard85571bc2004-11-07 18:04:02 +0000341
342/*
343 * Prepare processing.
344 */
345void *st_rate_start (int inrate, int outrate)
346{
Alistair Francis470bcab2018-02-03 09:43:02 +0100347 struct rate *rate = audio_calloc(__func__, 1, sizeof(*rate));
bellard85571bc2004-11-07 18:04:02 +0000348
349 if (!rate) {
bellarde7cad332005-11-11 00:03:36 +0000350 dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate));
bellard1d14ffa2005-10-30 18:58:22 +0000351 return NULL;
bellard85571bc2004-11-07 18:04:02 +0000352 }
353
354 rate->opos = 0;
355
356 /* increment */
bellard1d14ffa2005-10-30 18:58:22 +0000357 rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
bellard85571bc2004-11-07 18:04:02 +0000358
359 rate->ipos = 0;
360 rate->ilast.l = 0;
361 rate->ilast.r = 0;
362 return rate;
363}
364
bellard1d14ffa2005-10-30 18:58:22 +0000365#define NAME st_rate_flow_mix
366#define OP(a, b) a += b
367#include "rate_template.h"
bellard85571bc2004-11-07 18:04:02 +0000368
bellard1d14ffa2005-10-30 18:58:22 +0000369#define NAME st_rate_flow
370#define OP(a, b) a = b
371#include "rate_template.h"
bellard85571bc2004-11-07 18:04:02 +0000372
373void st_rate_stop (void *opaque)
374{
Anthony Liguori7267c092011-08-20 22:09:37 -0500375 g_free (opaque);
bellard85571bc2004-11-07 18:04:02 +0000376}
bellard1d14ffa2005-10-30 18:58:22 +0000377
malc1ea879e2008-12-03 22:48:44 +0000378void mixeng_clear (struct st_sample *buf, int len)
bellard1d14ffa2005-10-30 18:58:22 +0000379{
malc1ea879e2008-12-03 22:48:44 +0000380 memset (buf, 0, len * sizeof (struct st_sample));
bellard1d14ffa2005-10-30 18:58:22 +0000381}
Michael Walle00e07672011-01-05 01:05:47 +0100382
383void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
384{
Michael Walle00e07672011-01-05 01:05:47 +0100385 if (vol->mute) {
386 mixeng_clear (buf, len);
387 return;
388 }
389
390 while (len--) {
391#ifdef FLOAT_MIXENG
392 buf->l = buf->l * vol->l;
393 buf->r = buf->r * vol->r;
394#else
395 buf->l = (buf->l * vol->l) >> 32;
396 buf->r = (buf->r * vol->r) >> 32;
397#endif
398 buf += 1;
399 }
Michael Walle00e07672011-01-05 01:05:47 +0100400}