xref: /openbmc/qemu/audio/mixeng.c (revision 00e076795f2d6dfa0c078ff5d5ee5d77190cb4b9)
185571bc7Sbellard /*
285571bc7Sbellard  * QEMU Mixing engine
385571bc7Sbellard  *
41d14ffa9Sbellard  * Copyright (c) 2004-2005 Vassili Karpov (malc)
585571bc7Sbellard  * Copyright (c) 1998 Fabrice Bellard
685571bc7Sbellard  *
785571bc7Sbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
885571bc7Sbellard  * of this software and associated documentation files (the "Software"), to deal
985571bc7Sbellard  * in the Software without restriction, including without limitation the rights
1085571bc7Sbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1185571bc7Sbellard  * copies of the Software, and to permit persons to whom the Software is
1285571bc7Sbellard  * furnished to do so, subject to the following conditions:
1385571bc7Sbellard  *
1485571bc7Sbellard  * The above copyright notice and this permission notice shall be included in
1585571bc7Sbellard  * all copies or substantial portions of the Software.
1685571bc7Sbellard  *
1785571bc7Sbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1885571bc7Sbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1985571bc7Sbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2085571bc7Sbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2185571bc7Sbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2285571bc7Sbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2385571bc7Sbellard  * THE SOFTWARE.
2485571bc7Sbellard  */
2587ecb68bSpbrook #include "qemu-common.h"
2687ecb68bSpbrook #include "audio.h"
2785571bc7Sbellard 
281d14ffa9Sbellard #define AUDIO_CAP "mixeng"
291d14ffa9Sbellard #include "audio_int.h"
301d14ffa9Sbellard 
311d14ffa9Sbellard /* 8 bit */
321d14ffa9Sbellard #define ENDIAN_CONVERSION natural
331d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v)
341d14ffa9Sbellard 
351d14ffa9Sbellard /* Signed 8 bit */
3685571bc7Sbellard #define IN_T int8_t
371d14ffa9Sbellard #define IN_MIN SCHAR_MIN
381d14ffa9Sbellard #define IN_MAX SCHAR_MAX
3985571bc7Sbellard #define SIGNED
401d14ffa9Sbellard #define SHIFT 8
4185571bc7Sbellard #include "mixeng_template.h"
4285571bc7Sbellard #undef SIGNED
4385571bc7Sbellard #undef IN_MAX
4485571bc7Sbellard #undef IN_MIN
4585571bc7Sbellard #undef IN_T
461d14ffa9Sbellard #undef SHIFT
4785571bc7Sbellard 
481d14ffa9Sbellard /* Unsigned 8 bit */
4985571bc7Sbellard #define IN_T uint8_t
5085571bc7Sbellard #define IN_MIN 0
5185571bc7Sbellard #define IN_MAX UCHAR_MAX
521d14ffa9Sbellard #define SHIFT 8
5385571bc7Sbellard #include "mixeng_template.h"
5485571bc7Sbellard #undef IN_MAX
5585571bc7Sbellard #undef IN_MIN
5685571bc7Sbellard #undef IN_T
571d14ffa9Sbellard #undef SHIFT
5885571bc7Sbellard 
591d14ffa9Sbellard #undef ENDIAN_CONVERT
601d14ffa9Sbellard #undef ENDIAN_CONVERSION
611d14ffa9Sbellard 
621d14ffa9Sbellard /* Signed 16 bit */
6385571bc7Sbellard #define IN_T int16_t
6485571bc7Sbellard #define IN_MIN SHRT_MIN
6585571bc7Sbellard #define IN_MAX SHRT_MAX
6685571bc7Sbellard #define SIGNED
671d14ffa9Sbellard #define SHIFT 16
681d14ffa9Sbellard #define ENDIAN_CONVERSION natural
691d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v)
7085571bc7Sbellard #include "mixeng_template.h"
711d14ffa9Sbellard #undef ENDIAN_CONVERT
721d14ffa9Sbellard #undef ENDIAN_CONVERSION
731d14ffa9Sbellard #define ENDIAN_CONVERSION swap
741d14ffa9Sbellard #define ENDIAN_CONVERT(v) bswap16 (v)
751d14ffa9Sbellard #include "mixeng_template.h"
761d14ffa9Sbellard #undef ENDIAN_CONVERT
771d14ffa9Sbellard #undef ENDIAN_CONVERSION
7885571bc7Sbellard #undef SIGNED
7985571bc7Sbellard #undef IN_MAX
8085571bc7Sbellard #undef IN_MIN
8185571bc7Sbellard #undef IN_T
821d14ffa9Sbellard #undef SHIFT
8385571bc7Sbellard 
84f941aa25Sths /* Unsigned 16 bit */
8585571bc7Sbellard #define IN_T uint16_t
8685571bc7Sbellard #define IN_MIN 0
8785571bc7Sbellard #define IN_MAX USHRT_MAX
881d14ffa9Sbellard #define SHIFT 16
891d14ffa9Sbellard #define ENDIAN_CONVERSION natural
901d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v)
9185571bc7Sbellard #include "mixeng_template.h"
921d14ffa9Sbellard #undef ENDIAN_CONVERT
931d14ffa9Sbellard #undef ENDIAN_CONVERSION
941d14ffa9Sbellard #define ENDIAN_CONVERSION swap
951d14ffa9Sbellard #define ENDIAN_CONVERT(v) bswap16 (v)
961d14ffa9Sbellard #include "mixeng_template.h"
971d14ffa9Sbellard #undef ENDIAN_CONVERT
981d14ffa9Sbellard #undef ENDIAN_CONVERSION
9985571bc7Sbellard #undef IN_MAX
10085571bc7Sbellard #undef IN_MIN
10185571bc7Sbellard #undef IN_T
1021d14ffa9Sbellard #undef SHIFT
10385571bc7Sbellard 
104f941aa25Sths /* Signed 32 bit */
105f941aa25Sths #define IN_T int32_t
106f941aa25Sths #define IN_MIN INT32_MIN
107f941aa25Sths #define IN_MAX INT32_MAX
108f941aa25Sths #define SIGNED
109f941aa25Sths #define SHIFT 32
110f941aa25Sths #define ENDIAN_CONVERSION natural
111f941aa25Sths #define ENDIAN_CONVERT(v) (v)
112f941aa25Sths #include "mixeng_template.h"
113f941aa25Sths #undef ENDIAN_CONVERT
114f941aa25Sths #undef ENDIAN_CONVERSION
115f941aa25Sths #define ENDIAN_CONVERSION swap
116f941aa25Sths #define ENDIAN_CONVERT(v) bswap32 (v)
117f941aa25Sths #include "mixeng_template.h"
118f941aa25Sths #undef ENDIAN_CONVERT
119f941aa25Sths #undef ENDIAN_CONVERSION
120f941aa25Sths #undef SIGNED
121f941aa25Sths #undef IN_MAX
122f941aa25Sths #undef IN_MIN
123f941aa25Sths #undef IN_T
124f941aa25Sths #undef SHIFT
125f941aa25Sths 
126ad483a51Smalc /* Unsigned 32 bit */
127f941aa25Sths #define IN_T uint32_t
128f941aa25Sths #define IN_MIN 0
129f941aa25Sths #define IN_MAX UINT32_MAX
130f941aa25Sths #define SHIFT 32
131f941aa25Sths #define ENDIAN_CONVERSION natural
132f941aa25Sths #define ENDIAN_CONVERT(v) (v)
133f941aa25Sths #include "mixeng_template.h"
134f941aa25Sths #undef ENDIAN_CONVERT
135f941aa25Sths #undef ENDIAN_CONVERSION
136f941aa25Sths #define ENDIAN_CONVERSION swap
137f941aa25Sths #define ENDIAN_CONVERT(v) bswap32 (v)
138f941aa25Sths #include "mixeng_template.h"
139f941aa25Sths #undef ENDIAN_CONVERT
140f941aa25Sths #undef ENDIAN_CONVERSION
141f941aa25Sths #undef IN_MAX
142f941aa25Sths #undef IN_MIN
143f941aa25Sths #undef IN_T
144f941aa25Sths #undef SHIFT
145f941aa25Sths 
146f941aa25Sths t_sample *mixeng_conv[2][2][2][3] = {
14785571bc7Sbellard     {
14885571bc7Sbellard         {
1491d14ffa9Sbellard             {
1501d14ffa9Sbellard                 conv_natural_uint8_t_to_mono,
151f941aa25Sths                 conv_natural_uint16_t_to_mono,
152f941aa25Sths                 conv_natural_uint32_t_to_mono
15385571bc7Sbellard             },
15485571bc7Sbellard             {
1551d14ffa9Sbellard                 conv_natural_uint8_t_to_mono,
156f941aa25Sths                 conv_swap_uint16_t_to_mono,
157f941aa25Sths                 conv_swap_uint32_t_to_mono,
15885571bc7Sbellard             }
15985571bc7Sbellard         },
16085571bc7Sbellard         {
16185571bc7Sbellard             {
1621d14ffa9Sbellard                 conv_natural_int8_t_to_mono,
163f941aa25Sths                 conv_natural_int16_t_to_mono,
164f941aa25Sths                 conv_natural_int32_t_to_mono
16585571bc7Sbellard             },
16685571bc7Sbellard             {
1671d14ffa9Sbellard                 conv_natural_int8_t_to_mono,
168f941aa25Sths                 conv_swap_int16_t_to_mono,
169f941aa25Sths                 conv_swap_int32_t_to_mono
1701d14ffa9Sbellard             }
1711d14ffa9Sbellard         }
1721d14ffa9Sbellard     },
1731d14ffa9Sbellard     {
1741d14ffa9Sbellard         {
1751d14ffa9Sbellard             {
1761d14ffa9Sbellard                 conv_natural_uint8_t_to_stereo,
177f941aa25Sths                 conv_natural_uint16_t_to_stereo,
178f941aa25Sths                 conv_natural_uint32_t_to_stereo
1791d14ffa9Sbellard             },
1801d14ffa9Sbellard             {
1811d14ffa9Sbellard                 conv_natural_uint8_t_to_stereo,
182f941aa25Sths                 conv_swap_uint16_t_to_stereo,
183f941aa25Sths                 conv_swap_uint32_t_to_stereo
1841d14ffa9Sbellard             }
1851d14ffa9Sbellard         },
1861d14ffa9Sbellard         {
1871d14ffa9Sbellard             {
1881d14ffa9Sbellard                 conv_natural_int8_t_to_stereo,
189f941aa25Sths                 conv_natural_int16_t_to_stereo,
190f941aa25Sths                 conv_natural_int32_t_to_stereo
1911d14ffa9Sbellard             },
1921d14ffa9Sbellard             {
1931d14ffa9Sbellard                 conv_natural_int8_t_to_stereo,
194f941aa25Sths                 conv_swap_int16_t_to_stereo,
195f941aa25Sths                 conv_swap_int32_t_to_stereo,
1961d14ffa9Sbellard             }
19785571bc7Sbellard         }
19885571bc7Sbellard     }
19985571bc7Sbellard };
20085571bc7Sbellard 
201f941aa25Sths f_sample *mixeng_clip[2][2][2][3] = {
20285571bc7Sbellard     {
20385571bc7Sbellard         {
2041d14ffa9Sbellard             {
2051d14ffa9Sbellard                 clip_natural_uint8_t_from_mono,
206f941aa25Sths                 clip_natural_uint16_t_from_mono,
207f941aa25Sths                 clip_natural_uint32_t_from_mono
20885571bc7Sbellard             },
20985571bc7Sbellard             {
2101d14ffa9Sbellard                 clip_natural_uint8_t_from_mono,
211f941aa25Sths                 clip_swap_uint16_t_from_mono,
212f941aa25Sths                 clip_swap_uint32_t_from_mono
21385571bc7Sbellard             }
21485571bc7Sbellard         },
21585571bc7Sbellard         {
21685571bc7Sbellard             {
2171d14ffa9Sbellard                 clip_natural_int8_t_from_mono,
218f941aa25Sths                 clip_natural_int16_t_from_mono,
219f941aa25Sths                 clip_natural_int32_t_from_mono
22085571bc7Sbellard             },
22185571bc7Sbellard             {
2221d14ffa9Sbellard                 clip_natural_int8_t_from_mono,
223f941aa25Sths                 clip_swap_int16_t_from_mono,
224f941aa25Sths                 clip_swap_int32_t_from_mono
2251d14ffa9Sbellard             }
2261d14ffa9Sbellard         }
2271d14ffa9Sbellard     },
2281d14ffa9Sbellard     {
2291d14ffa9Sbellard         {
2301d14ffa9Sbellard             {
2311d14ffa9Sbellard                 clip_natural_uint8_t_from_stereo,
232f941aa25Sths                 clip_natural_uint16_t_from_stereo,
233f941aa25Sths                 clip_natural_uint32_t_from_stereo
2341d14ffa9Sbellard             },
2351d14ffa9Sbellard             {
2361d14ffa9Sbellard                 clip_natural_uint8_t_from_stereo,
237f941aa25Sths                 clip_swap_uint16_t_from_stereo,
238f941aa25Sths                 clip_swap_uint32_t_from_stereo
2391d14ffa9Sbellard             }
2401d14ffa9Sbellard         },
2411d14ffa9Sbellard         {
2421d14ffa9Sbellard             {
2431d14ffa9Sbellard                 clip_natural_int8_t_from_stereo,
244f941aa25Sths                 clip_natural_int16_t_from_stereo,
245f941aa25Sths                 clip_natural_int32_t_from_stereo
2461d14ffa9Sbellard             },
2471d14ffa9Sbellard             {
2481d14ffa9Sbellard                 clip_natural_int8_t_from_stereo,
249f941aa25Sths                 clip_swap_int16_t_from_stereo,
250f941aa25Sths                 clip_swap_int32_t_from_stereo
2511d14ffa9Sbellard             }
25285571bc7Sbellard         }
25385571bc7Sbellard     }
25485571bc7Sbellard };
25585571bc7Sbellard 
25685571bc7Sbellard /*
25785571bc7Sbellard  * August 21, 1998
25885571bc7Sbellard  * Copyright 1998 Fabrice Bellard.
25985571bc7Sbellard  *
26085571bc7Sbellard  * [Rewrote completly the code of Lance Norskog And Sundry
26185571bc7Sbellard  * Contributors with a more efficient algorithm.]
26285571bc7Sbellard  *
26385571bc7Sbellard  * This source code is freely redistributable and may be used for
26485571bc7Sbellard  * any purpose.  This copyright notice must be maintained.
26585571bc7Sbellard  * Lance Norskog And Sundry Contributors are not responsible for
26685571bc7Sbellard  * the consequences of using this software.
26785571bc7Sbellard  */
26885571bc7Sbellard 
26985571bc7Sbellard /*
27085571bc7Sbellard  * Sound Tools rate change effect file.
27185571bc7Sbellard  */
27285571bc7Sbellard /*
27385571bc7Sbellard  * Linear Interpolation.
27485571bc7Sbellard  *
27585571bc7Sbellard  * The use of fractional increment allows us to use no buffer. It
27685571bc7Sbellard  * avoid the problems at the end of the buffer we had with the old
27785571bc7Sbellard  * method which stored a possibly big buffer of size
27885571bc7Sbellard  * lcm(in_rate,out_rate).
27985571bc7Sbellard  *
28085571bc7Sbellard  * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
28185571bc7Sbellard  * the input & output frequencies are equal, a delay of one sample is
28285571bc7Sbellard  * introduced.  Limited to processing 32-bit count worth of samples.
28385571bc7Sbellard  *
28485571bc7Sbellard  * 1 << FRAC_BITS evaluating to zero in several places.  Changed with
28585571bc7Sbellard  * an (unsigned long) cast to make it safe.  MarkMLl 2/1/99
28685571bc7Sbellard  */
28785571bc7Sbellard 
28885571bc7Sbellard /* Private data */
289c0fe3827Sbellard struct rate {
29085571bc7Sbellard     uint64_t opos;
29185571bc7Sbellard     uint64_t opos_inc;
29285571bc7Sbellard     uint32_t ipos;              /* position in the input stream (integer) */
2931ea879e5Smalc     struct st_sample ilast;          /* last sample in the input stream */
294c0fe3827Sbellard };
29585571bc7Sbellard 
29685571bc7Sbellard /*
29785571bc7Sbellard  * Prepare processing.
29885571bc7Sbellard  */
29985571bc7Sbellard void *st_rate_start (int inrate, int outrate)
30085571bc7Sbellard {
301c0fe3827Sbellard     struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate));
30285571bc7Sbellard 
30385571bc7Sbellard     if (!rate) {
304e7cad338Sbellard         dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate));
3051d14ffa9Sbellard         return NULL;
30685571bc7Sbellard     }
30785571bc7Sbellard 
30885571bc7Sbellard     rate->opos = 0;
30985571bc7Sbellard 
31085571bc7Sbellard     /* increment */
3111d14ffa9Sbellard     rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
31285571bc7Sbellard 
31385571bc7Sbellard     rate->ipos = 0;
31485571bc7Sbellard     rate->ilast.l = 0;
31585571bc7Sbellard     rate->ilast.r = 0;
31685571bc7Sbellard     return rate;
31785571bc7Sbellard }
31885571bc7Sbellard 
3191d14ffa9Sbellard #define NAME st_rate_flow_mix
3201d14ffa9Sbellard #define OP(a, b) a += b
3211d14ffa9Sbellard #include "rate_template.h"
32285571bc7Sbellard 
3231d14ffa9Sbellard #define NAME st_rate_flow
3241d14ffa9Sbellard #define OP(a, b) a = b
3251d14ffa9Sbellard #include "rate_template.h"
32685571bc7Sbellard 
32785571bc7Sbellard void st_rate_stop (void *opaque)
32885571bc7Sbellard {
32985571bc7Sbellard     qemu_free (opaque);
33085571bc7Sbellard }
3311d14ffa9Sbellard 
3321ea879e5Smalc void mixeng_clear (struct st_sample *buf, int len)
3331d14ffa9Sbellard {
3341ea879e5Smalc     memset (buf, 0, len * sizeof (struct st_sample));
3351d14ffa9Sbellard }
336*00e07679SMichael Walle 
337*00e07679SMichael Walle void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
338*00e07679SMichael Walle {
339*00e07679SMichael Walle #ifdef CONFIG_MIXEMU
340*00e07679SMichael Walle     if (vol->mute) {
341*00e07679SMichael Walle         mixeng_clear (buf, len);
342*00e07679SMichael Walle         return;
343*00e07679SMichael Walle     }
344*00e07679SMichael Walle 
345*00e07679SMichael Walle     while (len--) {
346*00e07679SMichael Walle #ifdef FLOAT_MIXENG
347*00e07679SMichael Walle         buf->l = buf->l * vol->l;
348*00e07679SMichael Walle         buf->r = buf->r * vol->r;
349*00e07679SMichael Walle #else
350*00e07679SMichael Walle         buf->l = (buf->l * vol->l) >> 32;
351*00e07679SMichael Walle         buf->r = (buf->r * vol->r) >> 32;
352*00e07679SMichael Walle #endif
353*00e07679SMichael Walle         buf += 1;
354*00e07679SMichael Walle     }
355*00e07679SMichael Walle #else
356*00e07679SMichael Walle     (void) buf;
357*00e07679SMichael Walle     (void) len;
358*00e07679SMichael Walle     (void) vol;
359*00e07679SMichael Walle #endif
360*00e07679SMichael Walle }
361