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