xref: /openbmc/qemu/audio/mixeng.c (revision c0fe3827ea18f7d29550f2ff2495cec2fe7a3d94)
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  */
2585571bc7Sbellard #include "vl.h"
2685571bc7Sbellard 
271d14ffa9Sbellard #define AUDIO_CAP "mixeng"
281d14ffa9Sbellard #include "audio_int.h"
291d14ffa9Sbellard 
301d14ffa9Sbellard #define NOVOL
311d14ffa9Sbellard 
321d14ffa9Sbellard /* 8 bit */
331d14ffa9Sbellard #define ENDIAN_CONVERSION natural
341d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v)
351d14ffa9Sbellard 
361d14ffa9Sbellard /* Signed 8 bit */
3785571bc7Sbellard #define IN_T int8_t
381d14ffa9Sbellard #define IN_MIN SCHAR_MIN
391d14ffa9Sbellard #define IN_MAX SCHAR_MAX
4085571bc7Sbellard #define SIGNED
411d14ffa9Sbellard #define SHIFT 8
4285571bc7Sbellard #include "mixeng_template.h"
4385571bc7Sbellard #undef SIGNED
4485571bc7Sbellard #undef IN_MAX
4585571bc7Sbellard #undef IN_MIN
4685571bc7Sbellard #undef IN_T
471d14ffa9Sbellard #undef SHIFT
4885571bc7Sbellard 
491d14ffa9Sbellard /* Unsigned 8 bit */
5085571bc7Sbellard #define IN_T uint8_t
5185571bc7Sbellard #define IN_MIN 0
5285571bc7Sbellard #define IN_MAX UCHAR_MAX
531d14ffa9Sbellard #define SHIFT 8
5485571bc7Sbellard #include "mixeng_template.h"
5585571bc7Sbellard #undef IN_MAX
5685571bc7Sbellard #undef IN_MIN
5785571bc7Sbellard #undef IN_T
581d14ffa9Sbellard #undef SHIFT
5985571bc7Sbellard 
601d14ffa9Sbellard #undef ENDIAN_CONVERT
611d14ffa9Sbellard #undef ENDIAN_CONVERSION
621d14ffa9Sbellard 
631d14ffa9Sbellard /* Signed 16 bit */
6485571bc7Sbellard #define IN_T int16_t
6585571bc7Sbellard #define IN_MIN SHRT_MIN
6685571bc7Sbellard #define IN_MAX SHRT_MAX
6785571bc7Sbellard #define SIGNED
681d14ffa9Sbellard #define SHIFT 16
691d14ffa9Sbellard #define ENDIAN_CONVERSION natural
701d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v)
7185571bc7Sbellard #include "mixeng_template.h"
721d14ffa9Sbellard #undef ENDIAN_CONVERT
731d14ffa9Sbellard #undef ENDIAN_CONVERSION
741d14ffa9Sbellard #define ENDIAN_CONVERSION swap
751d14ffa9Sbellard #define ENDIAN_CONVERT(v) bswap16 (v)
761d14ffa9Sbellard #include "mixeng_template.h"
771d14ffa9Sbellard #undef ENDIAN_CONVERT
781d14ffa9Sbellard #undef ENDIAN_CONVERSION
7985571bc7Sbellard #undef SIGNED
8085571bc7Sbellard #undef IN_MAX
8185571bc7Sbellard #undef IN_MIN
8285571bc7Sbellard #undef IN_T
831d14ffa9Sbellard #undef SHIFT
8485571bc7Sbellard 
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 
1041d14ffa9Sbellard t_sample *mixeng_conv[2][2][2][2] = {
10585571bc7Sbellard     {
10685571bc7Sbellard         {
1071d14ffa9Sbellard             {
1081d14ffa9Sbellard                 conv_natural_uint8_t_to_mono,
1091d14ffa9Sbellard                 conv_natural_uint16_t_to_mono
11085571bc7Sbellard             },
11185571bc7Sbellard             {
1121d14ffa9Sbellard                 conv_natural_uint8_t_to_mono,
1131d14ffa9Sbellard                 conv_swap_uint16_t_to_mono
11485571bc7Sbellard             }
11585571bc7Sbellard         },
11685571bc7Sbellard         {
11785571bc7Sbellard             {
1181d14ffa9Sbellard                 conv_natural_int8_t_to_mono,
1191d14ffa9Sbellard                 conv_natural_int16_t_to_mono
12085571bc7Sbellard             },
12185571bc7Sbellard             {
1221d14ffa9Sbellard                 conv_natural_int8_t_to_mono,
1231d14ffa9Sbellard                 conv_swap_int16_t_to_mono
1241d14ffa9Sbellard             }
1251d14ffa9Sbellard         }
1261d14ffa9Sbellard     },
1271d14ffa9Sbellard     {
1281d14ffa9Sbellard         {
1291d14ffa9Sbellard             {
1301d14ffa9Sbellard                 conv_natural_uint8_t_to_stereo,
1311d14ffa9Sbellard                 conv_natural_uint16_t_to_stereo
1321d14ffa9Sbellard             },
1331d14ffa9Sbellard             {
1341d14ffa9Sbellard                 conv_natural_uint8_t_to_stereo,
1351d14ffa9Sbellard                 conv_swap_uint16_t_to_stereo
1361d14ffa9Sbellard             }
1371d14ffa9Sbellard         },
1381d14ffa9Sbellard         {
1391d14ffa9Sbellard             {
1401d14ffa9Sbellard                 conv_natural_int8_t_to_stereo,
1411d14ffa9Sbellard                 conv_natural_int16_t_to_stereo
1421d14ffa9Sbellard             },
1431d14ffa9Sbellard             {
1441d14ffa9Sbellard                 conv_natural_int8_t_to_stereo,
1451d14ffa9Sbellard                 conv_swap_int16_t_to_stereo
1461d14ffa9Sbellard             }
14785571bc7Sbellard         }
14885571bc7Sbellard     }
14985571bc7Sbellard };
15085571bc7Sbellard 
1511d14ffa9Sbellard f_sample *mixeng_clip[2][2][2][2] = {
15285571bc7Sbellard     {
15385571bc7Sbellard         {
1541d14ffa9Sbellard             {
1551d14ffa9Sbellard                 clip_natural_uint8_t_from_mono,
1561d14ffa9Sbellard                 clip_natural_uint16_t_from_mono
15785571bc7Sbellard             },
15885571bc7Sbellard             {
1591d14ffa9Sbellard                 clip_natural_uint8_t_from_mono,
1601d14ffa9Sbellard                 clip_swap_uint16_t_from_mono
16185571bc7Sbellard             }
16285571bc7Sbellard         },
16385571bc7Sbellard         {
16485571bc7Sbellard             {
1651d14ffa9Sbellard                 clip_natural_int8_t_from_mono,
1661d14ffa9Sbellard                 clip_natural_int16_t_from_mono
16785571bc7Sbellard             },
16885571bc7Sbellard             {
1691d14ffa9Sbellard                 clip_natural_int8_t_from_mono,
1701d14ffa9Sbellard                 clip_swap_int16_t_from_mono
1711d14ffa9Sbellard             }
1721d14ffa9Sbellard         }
1731d14ffa9Sbellard     },
1741d14ffa9Sbellard     {
1751d14ffa9Sbellard         {
1761d14ffa9Sbellard             {
1771d14ffa9Sbellard                 clip_natural_uint8_t_from_stereo,
1781d14ffa9Sbellard                 clip_natural_uint16_t_from_stereo
1791d14ffa9Sbellard             },
1801d14ffa9Sbellard             {
1811d14ffa9Sbellard                 clip_natural_uint8_t_from_stereo,
1821d14ffa9Sbellard                 clip_swap_uint16_t_from_stereo
1831d14ffa9Sbellard             }
1841d14ffa9Sbellard         },
1851d14ffa9Sbellard         {
1861d14ffa9Sbellard             {
1871d14ffa9Sbellard                 clip_natural_int8_t_from_stereo,
1881d14ffa9Sbellard                 clip_natural_int16_t_from_stereo
1891d14ffa9Sbellard             },
1901d14ffa9Sbellard             {
1911d14ffa9Sbellard                 clip_natural_int8_t_from_stereo,
1921d14ffa9Sbellard                 clip_swap_int16_t_from_stereo
1931d14ffa9Sbellard             }
19485571bc7Sbellard         }
19585571bc7Sbellard     }
19685571bc7Sbellard };
19785571bc7Sbellard 
19885571bc7Sbellard /*
19985571bc7Sbellard  * August 21, 1998
20085571bc7Sbellard  * Copyright 1998 Fabrice Bellard.
20185571bc7Sbellard  *
20285571bc7Sbellard  * [Rewrote completly the code of Lance Norskog And Sundry
20385571bc7Sbellard  * Contributors with a more efficient algorithm.]
20485571bc7Sbellard  *
20585571bc7Sbellard  * This source code is freely redistributable and may be used for
20685571bc7Sbellard  * any purpose.  This copyright notice must be maintained.
20785571bc7Sbellard  * Lance Norskog And Sundry Contributors are not responsible for
20885571bc7Sbellard  * the consequences of using this software.
20985571bc7Sbellard  */
21085571bc7Sbellard 
21185571bc7Sbellard /*
21285571bc7Sbellard  * Sound Tools rate change effect file.
21385571bc7Sbellard  */
21485571bc7Sbellard /*
21585571bc7Sbellard  * Linear Interpolation.
21685571bc7Sbellard  *
21785571bc7Sbellard  * The use of fractional increment allows us to use no buffer. It
21885571bc7Sbellard  * avoid the problems at the end of the buffer we had with the old
21985571bc7Sbellard  * method which stored a possibly big buffer of size
22085571bc7Sbellard  * lcm(in_rate,out_rate).
22185571bc7Sbellard  *
22285571bc7Sbellard  * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
22385571bc7Sbellard  * the input & output frequencies are equal, a delay of one sample is
22485571bc7Sbellard  * introduced.  Limited to processing 32-bit count worth of samples.
22585571bc7Sbellard  *
22685571bc7Sbellard  * 1 << FRAC_BITS evaluating to zero in several places.  Changed with
22785571bc7Sbellard  * an (unsigned long) cast to make it safe.  MarkMLl 2/1/99
22885571bc7Sbellard  */
22985571bc7Sbellard 
23085571bc7Sbellard /* Private data */
231*c0fe3827Sbellard struct rate {
23285571bc7Sbellard     uint64_t opos;
23385571bc7Sbellard     uint64_t opos_inc;
23485571bc7Sbellard     uint32_t ipos;              /* position in the input stream (integer) */
23585571bc7Sbellard     st_sample_t ilast;          /* last sample in the input stream */
236*c0fe3827Sbellard };
23785571bc7Sbellard 
23885571bc7Sbellard /*
23985571bc7Sbellard  * Prepare processing.
24085571bc7Sbellard  */
24185571bc7Sbellard void *st_rate_start (int inrate, int outrate)
24285571bc7Sbellard {
243*c0fe3827Sbellard     struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate));
24485571bc7Sbellard 
24585571bc7Sbellard     if (!rate) {
246*c0fe3827Sbellard         dolog ("Could not allocate resampler (%d bytes)\n", sizeof (*rate));
2471d14ffa9Sbellard         return NULL;
24885571bc7Sbellard     }
24985571bc7Sbellard 
25085571bc7Sbellard     rate->opos = 0;
25185571bc7Sbellard 
25285571bc7Sbellard     /* increment */
2531d14ffa9Sbellard     rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
25485571bc7Sbellard 
25585571bc7Sbellard     rate->ipos = 0;
25685571bc7Sbellard     rate->ilast.l = 0;
25785571bc7Sbellard     rate->ilast.r = 0;
25885571bc7Sbellard     return rate;
25985571bc7Sbellard }
26085571bc7Sbellard 
2611d14ffa9Sbellard #define NAME st_rate_flow_mix
2621d14ffa9Sbellard #define OP(a, b) a += b
2631d14ffa9Sbellard #include "rate_template.h"
26485571bc7Sbellard 
2651d14ffa9Sbellard #define NAME st_rate_flow
2661d14ffa9Sbellard #define OP(a, b) a = b
2671d14ffa9Sbellard #include "rate_template.h"
26885571bc7Sbellard 
26985571bc7Sbellard void st_rate_stop (void *opaque)
27085571bc7Sbellard {
27185571bc7Sbellard     qemu_free (opaque);
27285571bc7Sbellard }
2731d14ffa9Sbellard 
2741d14ffa9Sbellard void mixeng_clear (st_sample_t *buf, int len)
2751d14ffa9Sbellard {
2761d14ffa9Sbellard     memset (buf, 0, len * sizeof (st_sample_t));
2771d14ffa9Sbellard }
278