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 */ 256086a565SPeter Maydell #include "qemu/osdep.h" 2687ecb68bSpbrook #include "qemu-common.h" 27*58369e22SPaolo Bonzini #include "qemu/bswap.h" 2887ecb68bSpbrook #include "audio.h" 2985571bc7Sbellard 301d14ffa9Sbellard #define AUDIO_CAP "mixeng" 311d14ffa9Sbellard #include "audio_int.h" 321d14ffa9Sbellard 331d14ffa9Sbellard /* 8 bit */ 341d14ffa9Sbellard #define ENDIAN_CONVERSION natural 351d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v) 361d14ffa9Sbellard 371d14ffa9Sbellard /* Signed 8 bit */ 38a2885387SRoger Pau Monne #define BSIZE 8 39a2885387SRoger Pau Monne #define ITYPE int 401d14ffa9Sbellard #define IN_MIN SCHAR_MIN 411d14ffa9Sbellard #define IN_MAX SCHAR_MAX 4285571bc7Sbellard #define SIGNED 431d14ffa9Sbellard #define SHIFT 8 4485571bc7Sbellard #include "mixeng_template.h" 4585571bc7Sbellard #undef SIGNED 4685571bc7Sbellard #undef IN_MAX 4785571bc7Sbellard #undef IN_MIN 48a2885387SRoger Pau Monne #undef BSIZE 49a2885387SRoger Pau Monne #undef ITYPE 501d14ffa9Sbellard #undef SHIFT 5185571bc7Sbellard 521d14ffa9Sbellard /* Unsigned 8 bit */ 53a2885387SRoger Pau Monne #define BSIZE 8 54a2885387SRoger Pau Monne #define ITYPE uint 5585571bc7Sbellard #define IN_MIN 0 5685571bc7Sbellard #define IN_MAX UCHAR_MAX 571d14ffa9Sbellard #define SHIFT 8 5885571bc7Sbellard #include "mixeng_template.h" 5985571bc7Sbellard #undef IN_MAX 6085571bc7Sbellard #undef IN_MIN 61a2885387SRoger Pau Monne #undef BSIZE 62a2885387SRoger Pau Monne #undef ITYPE 631d14ffa9Sbellard #undef SHIFT 6485571bc7Sbellard 651d14ffa9Sbellard #undef ENDIAN_CONVERT 661d14ffa9Sbellard #undef ENDIAN_CONVERSION 671d14ffa9Sbellard 681d14ffa9Sbellard /* Signed 16 bit */ 69a2885387SRoger Pau Monne #define BSIZE 16 70a2885387SRoger Pau Monne #define ITYPE int 7185571bc7Sbellard #define IN_MIN SHRT_MIN 7285571bc7Sbellard #define IN_MAX SHRT_MAX 7385571bc7Sbellard #define SIGNED 741d14ffa9Sbellard #define SHIFT 16 751d14ffa9Sbellard #define ENDIAN_CONVERSION natural 761d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v) 7785571bc7Sbellard #include "mixeng_template.h" 781d14ffa9Sbellard #undef ENDIAN_CONVERT 791d14ffa9Sbellard #undef ENDIAN_CONVERSION 801d14ffa9Sbellard #define ENDIAN_CONVERSION swap 811d14ffa9Sbellard #define ENDIAN_CONVERT(v) bswap16 (v) 821d14ffa9Sbellard #include "mixeng_template.h" 831d14ffa9Sbellard #undef ENDIAN_CONVERT 841d14ffa9Sbellard #undef ENDIAN_CONVERSION 8585571bc7Sbellard #undef SIGNED 8685571bc7Sbellard #undef IN_MAX 8785571bc7Sbellard #undef IN_MIN 88a2885387SRoger Pau Monne #undef BSIZE 89a2885387SRoger Pau Monne #undef ITYPE 901d14ffa9Sbellard #undef SHIFT 9185571bc7Sbellard 92f941aa25Sths /* Unsigned 16 bit */ 93a2885387SRoger Pau Monne #define BSIZE 16 94a2885387SRoger Pau Monne #define ITYPE uint 9585571bc7Sbellard #define IN_MIN 0 9685571bc7Sbellard #define IN_MAX USHRT_MAX 971d14ffa9Sbellard #define SHIFT 16 981d14ffa9Sbellard #define ENDIAN_CONVERSION natural 991d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v) 10085571bc7Sbellard #include "mixeng_template.h" 1011d14ffa9Sbellard #undef ENDIAN_CONVERT 1021d14ffa9Sbellard #undef ENDIAN_CONVERSION 1031d14ffa9Sbellard #define ENDIAN_CONVERSION swap 1041d14ffa9Sbellard #define ENDIAN_CONVERT(v) bswap16 (v) 1051d14ffa9Sbellard #include "mixeng_template.h" 1061d14ffa9Sbellard #undef ENDIAN_CONVERT 1071d14ffa9Sbellard #undef ENDIAN_CONVERSION 10885571bc7Sbellard #undef IN_MAX 10985571bc7Sbellard #undef IN_MIN 110a2885387SRoger Pau Monne #undef BSIZE 111a2885387SRoger Pau Monne #undef ITYPE 1121d14ffa9Sbellard #undef SHIFT 11385571bc7Sbellard 114f941aa25Sths /* Signed 32 bit */ 115a2885387SRoger Pau Monne #define BSIZE 32 116a2885387SRoger Pau Monne #define ITYPE int 117f941aa25Sths #define IN_MIN INT32_MIN 118f941aa25Sths #define IN_MAX INT32_MAX 119f941aa25Sths #define SIGNED 120f941aa25Sths #define SHIFT 32 121f941aa25Sths #define ENDIAN_CONVERSION natural 122f941aa25Sths #define ENDIAN_CONVERT(v) (v) 123f941aa25Sths #include "mixeng_template.h" 124f941aa25Sths #undef ENDIAN_CONVERT 125f941aa25Sths #undef ENDIAN_CONVERSION 126f941aa25Sths #define ENDIAN_CONVERSION swap 127f941aa25Sths #define ENDIAN_CONVERT(v) bswap32 (v) 128f941aa25Sths #include "mixeng_template.h" 129f941aa25Sths #undef ENDIAN_CONVERT 130f941aa25Sths #undef ENDIAN_CONVERSION 131f941aa25Sths #undef SIGNED 132f941aa25Sths #undef IN_MAX 133f941aa25Sths #undef IN_MIN 134a2885387SRoger Pau Monne #undef BSIZE 135a2885387SRoger Pau Monne #undef ITYPE 136f941aa25Sths #undef SHIFT 137f941aa25Sths 138ad483a51Smalc /* Unsigned 32 bit */ 139a2885387SRoger Pau Monne #define BSIZE 32 140a2885387SRoger Pau Monne #define ITYPE uint 141f941aa25Sths #define IN_MIN 0 142f941aa25Sths #define IN_MAX UINT32_MAX 143f941aa25Sths #define SHIFT 32 144f941aa25Sths #define ENDIAN_CONVERSION natural 145f941aa25Sths #define ENDIAN_CONVERT(v) (v) 146f941aa25Sths #include "mixeng_template.h" 147f941aa25Sths #undef ENDIAN_CONVERT 148f941aa25Sths #undef ENDIAN_CONVERSION 149f941aa25Sths #define ENDIAN_CONVERSION swap 150f941aa25Sths #define ENDIAN_CONVERT(v) bswap32 (v) 151f941aa25Sths #include "mixeng_template.h" 152f941aa25Sths #undef ENDIAN_CONVERT 153f941aa25Sths #undef ENDIAN_CONVERSION 154f941aa25Sths #undef IN_MAX 155f941aa25Sths #undef IN_MIN 156a2885387SRoger Pau Monne #undef BSIZE 157a2885387SRoger Pau Monne #undef ITYPE 158f941aa25Sths #undef SHIFT 159f941aa25Sths 160f941aa25Sths t_sample *mixeng_conv[2][2][2][3] = { 16185571bc7Sbellard { 16285571bc7Sbellard { 1631d14ffa9Sbellard { 1641d14ffa9Sbellard conv_natural_uint8_t_to_mono, 165f941aa25Sths conv_natural_uint16_t_to_mono, 166f941aa25Sths conv_natural_uint32_t_to_mono 16785571bc7Sbellard }, 16885571bc7Sbellard { 1691d14ffa9Sbellard conv_natural_uint8_t_to_mono, 170f941aa25Sths conv_swap_uint16_t_to_mono, 171f941aa25Sths conv_swap_uint32_t_to_mono, 17285571bc7Sbellard } 17385571bc7Sbellard }, 17485571bc7Sbellard { 17585571bc7Sbellard { 1761d14ffa9Sbellard conv_natural_int8_t_to_mono, 177f941aa25Sths conv_natural_int16_t_to_mono, 178f941aa25Sths conv_natural_int32_t_to_mono 17985571bc7Sbellard }, 18085571bc7Sbellard { 1811d14ffa9Sbellard conv_natural_int8_t_to_mono, 182f941aa25Sths conv_swap_int16_t_to_mono, 183f941aa25Sths conv_swap_int32_t_to_mono 1841d14ffa9Sbellard } 1851d14ffa9Sbellard } 1861d14ffa9Sbellard }, 1871d14ffa9Sbellard { 1881d14ffa9Sbellard { 1891d14ffa9Sbellard { 1901d14ffa9Sbellard conv_natural_uint8_t_to_stereo, 191f941aa25Sths conv_natural_uint16_t_to_stereo, 192f941aa25Sths conv_natural_uint32_t_to_stereo 1931d14ffa9Sbellard }, 1941d14ffa9Sbellard { 1951d14ffa9Sbellard conv_natural_uint8_t_to_stereo, 196f941aa25Sths conv_swap_uint16_t_to_stereo, 197f941aa25Sths conv_swap_uint32_t_to_stereo 1981d14ffa9Sbellard } 1991d14ffa9Sbellard }, 2001d14ffa9Sbellard { 2011d14ffa9Sbellard { 2021d14ffa9Sbellard conv_natural_int8_t_to_stereo, 203f941aa25Sths conv_natural_int16_t_to_stereo, 204f941aa25Sths conv_natural_int32_t_to_stereo 2051d14ffa9Sbellard }, 2061d14ffa9Sbellard { 2071d14ffa9Sbellard conv_natural_int8_t_to_stereo, 208f941aa25Sths conv_swap_int16_t_to_stereo, 209f941aa25Sths conv_swap_int32_t_to_stereo, 2101d14ffa9Sbellard } 21185571bc7Sbellard } 21285571bc7Sbellard } 21385571bc7Sbellard }; 21485571bc7Sbellard 215f941aa25Sths f_sample *mixeng_clip[2][2][2][3] = { 21685571bc7Sbellard { 21785571bc7Sbellard { 2181d14ffa9Sbellard { 2191d14ffa9Sbellard clip_natural_uint8_t_from_mono, 220f941aa25Sths clip_natural_uint16_t_from_mono, 221f941aa25Sths clip_natural_uint32_t_from_mono 22285571bc7Sbellard }, 22385571bc7Sbellard { 2241d14ffa9Sbellard clip_natural_uint8_t_from_mono, 225f941aa25Sths clip_swap_uint16_t_from_mono, 226f941aa25Sths clip_swap_uint32_t_from_mono 22785571bc7Sbellard } 22885571bc7Sbellard }, 22985571bc7Sbellard { 23085571bc7Sbellard { 2311d14ffa9Sbellard clip_natural_int8_t_from_mono, 232f941aa25Sths clip_natural_int16_t_from_mono, 233f941aa25Sths clip_natural_int32_t_from_mono 23485571bc7Sbellard }, 23585571bc7Sbellard { 2361d14ffa9Sbellard clip_natural_int8_t_from_mono, 237f941aa25Sths clip_swap_int16_t_from_mono, 238f941aa25Sths clip_swap_int32_t_from_mono 2391d14ffa9Sbellard } 2401d14ffa9Sbellard } 2411d14ffa9Sbellard }, 2421d14ffa9Sbellard { 2431d14ffa9Sbellard { 2441d14ffa9Sbellard { 2451d14ffa9Sbellard clip_natural_uint8_t_from_stereo, 246f941aa25Sths clip_natural_uint16_t_from_stereo, 247f941aa25Sths clip_natural_uint32_t_from_stereo 2481d14ffa9Sbellard }, 2491d14ffa9Sbellard { 2501d14ffa9Sbellard clip_natural_uint8_t_from_stereo, 251f941aa25Sths clip_swap_uint16_t_from_stereo, 252f941aa25Sths clip_swap_uint32_t_from_stereo 2531d14ffa9Sbellard } 2541d14ffa9Sbellard }, 2551d14ffa9Sbellard { 2561d14ffa9Sbellard { 2571d14ffa9Sbellard clip_natural_int8_t_from_stereo, 258f941aa25Sths clip_natural_int16_t_from_stereo, 259f941aa25Sths clip_natural_int32_t_from_stereo 2601d14ffa9Sbellard }, 2611d14ffa9Sbellard { 2621d14ffa9Sbellard clip_natural_int8_t_from_stereo, 263f941aa25Sths clip_swap_int16_t_from_stereo, 264f941aa25Sths clip_swap_int32_t_from_stereo 2651d14ffa9Sbellard } 26685571bc7Sbellard } 26785571bc7Sbellard } 26885571bc7Sbellard }; 26985571bc7Sbellard 27085571bc7Sbellard /* 27185571bc7Sbellard * August 21, 1998 27285571bc7Sbellard * Copyright 1998 Fabrice Bellard. 27385571bc7Sbellard * 274cb8d4c8fSStefan Weil * [Rewrote completely the code of Lance Norskog And Sundry 27585571bc7Sbellard * Contributors with a more efficient algorithm.] 27685571bc7Sbellard * 27785571bc7Sbellard * This source code is freely redistributable and may be used for 27885571bc7Sbellard * any purpose. This copyright notice must be maintained. 27985571bc7Sbellard * Lance Norskog And Sundry Contributors are not responsible for 28085571bc7Sbellard * the consequences of using this software. 28185571bc7Sbellard */ 28285571bc7Sbellard 28385571bc7Sbellard /* 28485571bc7Sbellard * Sound Tools rate change effect file. 28585571bc7Sbellard */ 28685571bc7Sbellard /* 28785571bc7Sbellard * Linear Interpolation. 28885571bc7Sbellard * 28985571bc7Sbellard * The use of fractional increment allows us to use no buffer. It 29085571bc7Sbellard * avoid the problems at the end of the buffer we had with the old 29185571bc7Sbellard * method which stored a possibly big buffer of size 29285571bc7Sbellard * lcm(in_rate,out_rate). 29385571bc7Sbellard * 29485571bc7Sbellard * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If 29585571bc7Sbellard * the input & output frequencies are equal, a delay of one sample is 29685571bc7Sbellard * introduced. Limited to processing 32-bit count worth of samples. 29785571bc7Sbellard * 29885571bc7Sbellard * 1 << FRAC_BITS evaluating to zero in several places. Changed with 29985571bc7Sbellard * an (unsigned long) cast to make it safe. MarkMLl 2/1/99 30085571bc7Sbellard */ 30185571bc7Sbellard 30285571bc7Sbellard /* Private data */ 303c0fe3827Sbellard struct rate { 30485571bc7Sbellard uint64_t opos; 30585571bc7Sbellard uint64_t opos_inc; 30685571bc7Sbellard uint32_t ipos; /* position in the input stream (integer) */ 3071ea879e5Smalc struct st_sample ilast; /* last sample in the input stream */ 308c0fe3827Sbellard }; 30985571bc7Sbellard 31085571bc7Sbellard /* 31185571bc7Sbellard * Prepare processing. 31285571bc7Sbellard */ 31385571bc7Sbellard void *st_rate_start (int inrate, int outrate) 31485571bc7Sbellard { 315c0fe3827Sbellard struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate)); 31685571bc7Sbellard 31785571bc7Sbellard if (!rate) { 318e7cad338Sbellard dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate)); 3191d14ffa9Sbellard return NULL; 32085571bc7Sbellard } 32185571bc7Sbellard 32285571bc7Sbellard rate->opos = 0; 32385571bc7Sbellard 32485571bc7Sbellard /* increment */ 3251d14ffa9Sbellard rate->opos_inc = ((uint64_t) inrate << 32) / outrate; 32685571bc7Sbellard 32785571bc7Sbellard rate->ipos = 0; 32885571bc7Sbellard rate->ilast.l = 0; 32985571bc7Sbellard rate->ilast.r = 0; 33085571bc7Sbellard return rate; 33185571bc7Sbellard } 33285571bc7Sbellard 3331d14ffa9Sbellard #define NAME st_rate_flow_mix 3341d14ffa9Sbellard #define OP(a, b) a += b 3351d14ffa9Sbellard #include "rate_template.h" 33685571bc7Sbellard 3371d14ffa9Sbellard #define NAME st_rate_flow 3381d14ffa9Sbellard #define OP(a, b) a = b 3391d14ffa9Sbellard #include "rate_template.h" 34085571bc7Sbellard 34185571bc7Sbellard void st_rate_stop (void *opaque) 34285571bc7Sbellard { 3437267c094SAnthony Liguori g_free (opaque); 34485571bc7Sbellard } 3451d14ffa9Sbellard 3461ea879e5Smalc void mixeng_clear (struct st_sample *buf, int len) 3471d14ffa9Sbellard { 3481ea879e5Smalc memset (buf, 0, len * sizeof (struct st_sample)); 3491d14ffa9Sbellard } 35000e07679SMichael Walle 35100e07679SMichael Walle void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol) 35200e07679SMichael Walle { 35300e07679SMichael Walle if (vol->mute) { 35400e07679SMichael Walle mixeng_clear (buf, len); 35500e07679SMichael Walle return; 35600e07679SMichael Walle } 35700e07679SMichael Walle 35800e07679SMichael Walle while (len--) { 35900e07679SMichael Walle #ifdef FLOAT_MIXENG 36000e07679SMichael Walle buf->l = buf->l * vol->l; 36100e07679SMichael Walle buf->r = buf->r * vol->r; 36200e07679SMichael Walle #else 36300e07679SMichael Walle buf->l = (buf->l * vol->l) >> 32; 36400e07679SMichael Walle buf->r = (buf->r * vol->r) >> 32; 36500e07679SMichael Walle #endif 36600e07679SMichael Walle buf += 1; 36700e07679SMichael Walle } 36800e07679SMichael Walle } 369