Statistics
| Branch: | Revision:

root / audio / mixeng.c @ 8cfd0495

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