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" 2787ecb68bSpbrook #include "audio.h" 2885571bc7Sbellard 291d14ffa9Sbellard #define AUDIO_CAP "mixeng" 301d14ffa9Sbellard #include "audio_int.h" 311d14ffa9Sbellard 321d14ffa9Sbellard /* 8 bit */ 331d14ffa9Sbellard #define ENDIAN_CONVERSION natural 341d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v) 351d14ffa9Sbellard 361d14ffa9Sbellard /* Signed 8 bit */ 37a2885387SRoger Pau Monne #define BSIZE 8 38a2885387SRoger Pau Monne #define ITYPE int 391d14ffa9Sbellard #define IN_MIN SCHAR_MIN 401d14ffa9Sbellard #define IN_MAX SCHAR_MAX 4185571bc7Sbellard #define SIGNED 421d14ffa9Sbellard #define SHIFT 8 4385571bc7Sbellard #include "mixeng_template.h" 4485571bc7Sbellard #undef SIGNED 4585571bc7Sbellard #undef IN_MAX 4685571bc7Sbellard #undef IN_MIN 47a2885387SRoger Pau Monne #undef BSIZE 48a2885387SRoger Pau Monne #undef ITYPE 491d14ffa9Sbellard #undef SHIFT 5085571bc7Sbellard 511d14ffa9Sbellard /* Unsigned 8 bit */ 52a2885387SRoger Pau Monne #define BSIZE 8 53a2885387SRoger Pau Monne #define ITYPE uint 5485571bc7Sbellard #define IN_MIN 0 5585571bc7Sbellard #define IN_MAX UCHAR_MAX 561d14ffa9Sbellard #define SHIFT 8 5785571bc7Sbellard #include "mixeng_template.h" 5885571bc7Sbellard #undef IN_MAX 5985571bc7Sbellard #undef IN_MIN 60a2885387SRoger Pau Monne #undef BSIZE 61a2885387SRoger Pau Monne #undef ITYPE 621d14ffa9Sbellard #undef SHIFT 6385571bc7Sbellard 641d14ffa9Sbellard #undef ENDIAN_CONVERT 651d14ffa9Sbellard #undef ENDIAN_CONVERSION 661d14ffa9Sbellard 671d14ffa9Sbellard /* Signed 16 bit */ 68a2885387SRoger Pau Monne #define BSIZE 16 69a2885387SRoger Pau Monne #define ITYPE int 7085571bc7Sbellard #define IN_MIN SHRT_MIN 7185571bc7Sbellard #define IN_MAX SHRT_MAX 7285571bc7Sbellard #define SIGNED 731d14ffa9Sbellard #define SHIFT 16 741d14ffa9Sbellard #define ENDIAN_CONVERSION natural 751d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v) 7685571bc7Sbellard #include "mixeng_template.h" 771d14ffa9Sbellard #undef ENDIAN_CONVERT 781d14ffa9Sbellard #undef ENDIAN_CONVERSION 791d14ffa9Sbellard #define ENDIAN_CONVERSION swap 801d14ffa9Sbellard #define ENDIAN_CONVERT(v) bswap16 (v) 811d14ffa9Sbellard #include "mixeng_template.h" 821d14ffa9Sbellard #undef ENDIAN_CONVERT 831d14ffa9Sbellard #undef ENDIAN_CONVERSION 8485571bc7Sbellard #undef SIGNED 8585571bc7Sbellard #undef IN_MAX 8685571bc7Sbellard #undef IN_MIN 87a2885387SRoger Pau Monne #undef BSIZE 88a2885387SRoger Pau Monne #undef ITYPE 891d14ffa9Sbellard #undef SHIFT 9085571bc7Sbellard 91f941aa25Sths /* Unsigned 16 bit */ 92a2885387SRoger Pau Monne #define BSIZE 16 93a2885387SRoger Pau Monne #define ITYPE uint 9485571bc7Sbellard #define IN_MIN 0 9585571bc7Sbellard #define IN_MAX USHRT_MAX 961d14ffa9Sbellard #define SHIFT 16 971d14ffa9Sbellard #define ENDIAN_CONVERSION natural 981d14ffa9Sbellard #define ENDIAN_CONVERT(v) (v) 9985571bc7Sbellard #include "mixeng_template.h" 1001d14ffa9Sbellard #undef ENDIAN_CONVERT 1011d14ffa9Sbellard #undef ENDIAN_CONVERSION 1021d14ffa9Sbellard #define ENDIAN_CONVERSION swap 1031d14ffa9Sbellard #define ENDIAN_CONVERT(v) bswap16 (v) 1041d14ffa9Sbellard #include "mixeng_template.h" 1051d14ffa9Sbellard #undef ENDIAN_CONVERT 1061d14ffa9Sbellard #undef ENDIAN_CONVERSION 10785571bc7Sbellard #undef IN_MAX 10885571bc7Sbellard #undef IN_MIN 109a2885387SRoger Pau Monne #undef BSIZE 110a2885387SRoger Pau Monne #undef ITYPE 1111d14ffa9Sbellard #undef SHIFT 11285571bc7Sbellard 113f941aa25Sths /* Signed 32 bit */ 114a2885387SRoger Pau Monne #define BSIZE 32 115a2885387SRoger Pau Monne #define ITYPE int 116f941aa25Sths #define IN_MIN INT32_MIN 117f941aa25Sths #define IN_MAX INT32_MAX 118f941aa25Sths #define SIGNED 119f941aa25Sths #define SHIFT 32 120f941aa25Sths #define ENDIAN_CONVERSION natural 121f941aa25Sths #define ENDIAN_CONVERT(v) (v) 122f941aa25Sths #include "mixeng_template.h" 123f941aa25Sths #undef ENDIAN_CONVERT 124f941aa25Sths #undef ENDIAN_CONVERSION 125f941aa25Sths #define ENDIAN_CONVERSION swap 126f941aa25Sths #define ENDIAN_CONVERT(v) bswap32 (v) 127f941aa25Sths #include "mixeng_template.h" 128f941aa25Sths #undef ENDIAN_CONVERT 129f941aa25Sths #undef ENDIAN_CONVERSION 130f941aa25Sths #undef SIGNED 131f941aa25Sths #undef IN_MAX 132f941aa25Sths #undef IN_MIN 133a2885387SRoger Pau Monne #undef BSIZE 134a2885387SRoger Pau Monne #undef ITYPE 135f941aa25Sths #undef SHIFT 136f941aa25Sths 137ad483a51Smalc /* Unsigned 32 bit */ 138a2885387SRoger Pau Monne #define BSIZE 32 139a2885387SRoger Pau Monne #define ITYPE uint 140f941aa25Sths #define IN_MIN 0 141f941aa25Sths #define IN_MAX UINT32_MAX 142f941aa25Sths #define SHIFT 32 143f941aa25Sths #define ENDIAN_CONVERSION natural 144f941aa25Sths #define ENDIAN_CONVERT(v) (v) 145f941aa25Sths #include "mixeng_template.h" 146f941aa25Sths #undef ENDIAN_CONVERT 147f941aa25Sths #undef ENDIAN_CONVERSION 148f941aa25Sths #define ENDIAN_CONVERSION swap 149f941aa25Sths #define ENDIAN_CONVERT(v) bswap32 (v) 150f941aa25Sths #include "mixeng_template.h" 151f941aa25Sths #undef ENDIAN_CONVERT 152f941aa25Sths #undef ENDIAN_CONVERSION 153f941aa25Sths #undef IN_MAX 154f941aa25Sths #undef IN_MIN 155a2885387SRoger Pau Monne #undef BSIZE 156a2885387SRoger Pau Monne #undef ITYPE 157f941aa25Sths #undef SHIFT 158f941aa25Sths 159f941aa25Sths t_sample *mixeng_conv[2][2][2][3] = { 16085571bc7Sbellard { 16185571bc7Sbellard { 1621d14ffa9Sbellard { 1631d14ffa9Sbellard conv_natural_uint8_t_to_mono, 164f941aa25Sths conv_natural_uint16_t_to_mono, 165f941aa25Sths conv_natural_uint32_t_to_mono 16685571bc7Sbellard }, 16785571bc7Sbellard { 1681d14ffa9Sbellard conv_natural_uint8_t_to_mono, 169f941aa25Sths conv_swap_uint16_t_to_mono, 170f941aa25Sths conv_swap_uint32_t_to_mono, 17185571bc7Sbellard } 17285571bc7Sbellard }, 17385571bc7Sbellard { 17485571bc7Sbellard { 1751d14ffa9Sbellard conv_natural_int8_t_to_mono, 176f941aa25Sths conv_natural_int16_t_to_mono, 177f941aa25Sths conv_natural_int32_t_to_mono 17885571bc7Sbellard }, 17985571bc7Sbellard { 1801d14ffa9Sbellard conv_natural_int8_t_to_mono, 181f941aa25Sths conv_swap_int16_t_to_mono, 182f941aa25Sths conv_swap_int32_t_to_mono 1831d14ffa9Sbellard } 1841d14ffa9Sbellard } 1851d14ffa9Sbellard }, 1861d14ffa9Sbellard { 1871d14ffa9Sbellard { 1881d14ffa9Sbellard { 1891d14ffa9Sbellard conv_natural_uint8_t_to_stereo, 190f941aa25Sths conv_natural_uint16_t_to_stereo, 191f941aa25Sths conv_natural_uint32_t_to_stereo 1921d14ffa9Sbellard }, 1931d14ffa9Sbellard { 1941d14ffa9Sbellard conv_natural_uint8_t_to_stereo, 195f941aa25Sths conv_swap_uint16_t_to_stereo, 196f941aa25Sths conv_swap_uint32_t_to_stereo 1971d14ffa9Sbellard } 1981d14ffa9Sbellard }, 1991d14ffa9Sbellard { 2001d14ffa9Sbellard { 2011d14ffa9Sbellard conv_natural_int8_t_to_stereo, 202f941aa25Sths conv_natural_int16_t_to_stereo, 203f941aa25Sths conv_natural_int32_t_to_stereo 2041d14ffa9Sbellard }, 2051d14ffa9Sbellard { 2061d14ffa9Sbellard conv_natural_int8_t_to_stereo, 207f941aa25Sths conv_swap_int16_t_to_stereo, 208f941aa25Sths conv_swap_int32_t_to_stereo, 2091d14ffa9Sbellard } 21085571bc7Sbellard } 21185571bc7Sbellard } 21285571bc7Sbellard }; 21385571bc7Sbellard 214f941aa25Sths f_sample *mixeng_clip[2][2][2][3] = { 21585571bc7Sbellard { 21685571bc7Sbellard { 2171d14ffa9Sbellard { 2181d14ffa9Sbellard clip_natural_uint8_t_from_mono, 219f941aa25Sths clip_natural_uint16_t_from_mono, 220f941aa25Sths clip_natural_uint32_t_from_mono 22185571bc7Sbellard }, 22285571bc7Sbellard { 2231d14ffa9Sbellard clip_natural_uint8_t_from_mono, 224f941aa25Sths clip_swap_uint16_t_from_mono, 225f941aa25Sths clip_swap_uint32_t_from_mono 22685571bc7Sbellard } 22785571bc7Sbellard }, 22885571bc7Sbellard { 22985571bc7Sbellard { 2301d14ffa9Sbellard clip_natural_int8_t_from_mono, 231f941aa25Sths clip_natural_int16_t_from_mono, 232f941aa25Sths clip_natural_int32_t_from_mono 23385571bc7Sbellard }, 23485571bc7Sbellard { 2351d14ffa9Sbellard clip_natural_int8_t_from_mono, 236f941aa25Sths clip_swap_int16_t_from_mono, 237f941aa25Sths clip_swap_int32_t_from_mono 2381d14ffa9Sbellard } 2391d14ffa9Sbellard } 2401d14ffa9Sbellard }, 2411d14ffa9Sbellard { 2421d14ffa9Sbellard { 2431d14ffa9Sbellard { 2441d14ffa9Sbellard clip_natural_uint8_t_from_stereo, 245f941aa25Sths clip_natural_uint16_t_from_stereo, 246f941aa25Sths clip_natural_uint32_t_from_stereo 2471d14ffa9Sbellard }, 2481d14ffa9Sbellard { 2491d14ffa9Sbellard clip_natural_uint8_t_from_stereo, 250f941aa25Sths clip_swap_uint16_t_from_stereo, 251f941aa25Sths clip_swap_uint32_t_from_stereo 2521d14ffa9Sbellard } 2531d14ffa9Sbellard }, 2541d14ffa9Sbellard { 2551d14ffa9Sbellard { 2561d14ffa9Sbellard clip_natural_int8_t_from_stereo, 257f941aa25Sths clip_natural_int16_t_from_stereo, 258f941aa25Sths clip_natural_int32_t_from_stereo 2591d14ffa9Sbellard }, 2601d14ffa9Sbellard { 2611d14ffa9Sbellard clip_natural_int8_t_from_stereo, 262f941aa25Sths clip_swap_int16_t_from_stereo, 263f941aa25Sths clip_swap_int32_t_from_stereo 2641d14ffa9Sbellard } 26585571bc7Sbellard } 26685571bc7Sbellard } 26785571bc7Sbellard }; 26885571bc7Sbellard 26985571bc7Sbellard /* 27085571bc7Sbellard * August 21, 1998 27185571bc7Sbellard * Copyright 1998 Fabrice Bellard. 27285571bc7Sbellard * 273*cb8d4c8fSStefan Weil * [Rewrote completely the code of Lance Norskog And Sundry 27485571bc7Sbellard * Contributors with a more efficient algorithm.] 27585571bc7Sbellard * 27685571bc7Sbellard * This source code is freely redistributable and may be used for 27785571bc7Sbellard * any purpose. This copyright notice must be maintained. 27885571bc7Sbellard * Lance Norskog And Sundry Contributors are not responsible for 27985571bc7Sbellard * the consequences of using this software. 28085571bc7Sbellard */ 28185571bc7Sbellard 28285571bc7Sbellard /* 28385571bc7Sbellard * Sound Tools rate change effect file. 28485571bc7Sbellard */ 28585571bc7Sbellard /* 28685571bc7Sbellard * Linear Interpolation. 28785571bc7Sbellard * 28885571bc7Sbellard * The use of fractional increment allows us to use no buffer. It 28985571bc7Sbellard * avoid the problems at the end of the buffer we had with the old 29085571bc7Sbellard * method which stored a possibly big buffer of size 29185571bc7Sbellard * lcm(in_rate,out_rate). 29285571bc7Sbellard * 29385571bc7Sbellard * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If 29485571bc7Sbellard * the input & output frequencies are equal, a delay of one sample is 29585571bc7Sbellard * introduced. Limited to processing 32-bit count worth of samples. 29685571bc7Sbellard * 29785571bc7Sbellard * 1 << FRAC_BITS evaluating to zero in several places. Changed with 29885571bc7Sbellard * an (unsigned long) cast to make it safe. MarkMLl 2/1/99 29985571bc7Sbellard */ 30085571bc7Sbellard 30185571bc7Sbellard /* Private data */ 302c0fe3827Sbellard struct rate { 30385571bc7Sbellard uint64_t opos; 30485571bc7Sbellard uint64_t opos_inc; 30585571bc7Sbellard uint32_t ipos; /* position in the input stream (integer) */ 3061ea879e5Smalc struct st_sample ilast; /* last sample in the input stream */ 307c0fe3827Sbellard }; 30885571bc7Sbellard 30985571bc7Sbellard /* 31085571bc7Sbellard * Prepare processing. 31185571bc7Sbellard */ 31285571bc7Sbellard void *st_rate_start (int inrate, int outrate) 31385571bc7Sbellard { 314c0fe3827Sbellard struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate)); 31585571bc7Sbellard 31685571bc7Sbellard if (!rate) { 317e7cad338Sbellard dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate)); 3181d14ffa9Sbellard return NULL; 31985571bc7Sbellard } 32085571bc7Sbellard 32185571bc7Sbellard rate->opos = 0; 32285571bc7Sbellard 32385571bc7Sbellard /* increment */ 3241d14ffa9Sbellard rate->opos_inc = ((uint64_t) inrate << 32) / outrate; 32585571bc7Sbellard 32685571bc7Sbellard rate->ipos = 0; 32785571bc7Sbellard rate->ilast.l = 0; 32885571bc7Sbellard rate->ilast.r = 0; 32985571bc7Sbellard return rate; 33085571bc7Sbellard } 33185571bc7Sbellard 3321d14ffa9Sbellard #define NAME st_rate_flow_mix 3331d14ffa9Sbellard #define OP(a, b) a += b 3341d14ffa9Sbellard #include "rate_template.h" 33585571bc7Sbellard 3361d14ffa9Sbellard #define NAME st_rate_flow 3371d14ffa9Sbellard #define OP(a, b) a = b 3381d14ffa9Sbellard #include "rate_template.h" 33985571bc7Sbellard 34085571bc7Sbellard void st_rate_stop (void *opaque) 34185571bc7Sbellard { 3427267c094SAnthony Liguori g_free (opaque); 34385571bc7Sbellard } 3441d14ffa9Sbellard 3451ea879e5Smalc void mixeng_clear (struct st_sample *buf, int len) 3461d14ffa9Sbellard { 3471ea879e5Smalc memset (buf, 0, len * sizeof (struct st_sample)); 3481d14ffa9Sbellard } 34900e07679SMichael Walle 35000e07679SMichael Walle void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol) 35100e07679SMichael Walle { 35200e07679SMichael Walle if (vol->mute) { 35300e07679SMichael Walle mixeng_clear (buf, len); 35400e07679SMichael Walle return; 35500e07679SMichael Walle } 35600e07679SMichael Walle 35700e07679SMichael Walle while (len--) { 35800e07679SMichael Walle #ifdef FLOAT_MIXENG 35900e07679SMichael Walle buf->l = buf->l * vol->l; 36000e07679SMichael Walle buf->r = buf->r * vol->r; 36100e07679SMichael Walle #else 36200e07679SMichael Walle buf->l = (buf->l * vol->l) >> 32; 36300e07679SMichael Walle buf->r = (buf->r * vol->r) >> 32; 36400e07679SMichael Walle #endif 36500e07679SMichael Walle buf += 1; 36600e07679SMichael Walle } 36700e07679SMichael Walle } 368