2004-11-07 19:04:02 +01:00
|
|
|
/*
|
|
|
|
* QEMU Mixing engine
|
2005-10-30 19:58:22 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2005 Vassili Karpov (malc)
|
|
|
|
*
|
2004-11-07 19:04:02 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tusen tack till Mike Nordell
|
|
|
|
* dec++'ified by Dscho
|
|
|
|
*/
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#ifndef SIGNED
|
|
|
|
#define HALF (IN_MAX >> 1)
|
|
|
|
#endif
|
|
|
|
|
2012-05-18 13:08:14 +02:00
|
|
|
#define ET glue (ENDIAN_CONVERSION, glue (glue (glue (_, ITYPE), BSIZE), _t))
|
|
|
|
#define IN_T glue (glue (ITYPE, BSIZE), _t)
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
#ifdef FLOAT_MIXENG
|
2013-10-22 11:26:28 +02:00
|
|
|
static inline mixeng_real glue (conv_, ET) (IN_T v)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
|
|
|
IN_T nv = ENDIAN_CONVERT (v);
|
|
|
|
|
|
|
|
#ifdef RECIPROCAL
|
|
|
|
#ifdef SIGNED
|
2020-03-08 20:33:19 +01:00
|
|
|
return nv * (2.f / ((mixeng_real)IN_MAX - IN_MIN));
|
2005-10-30 19:58:22 +01:00
|
|
|
#else
|
2020-03-08 20:33:19 +01:00
|
|
|
return (nv - HALF) * (2.f / (mixeng_real)IN_MAX);
|
2005-10-30 19:58:22 +01:00
|
|
|
#endif
|
|
|
|
#else /* !RECIPROCAL */
|
2004-11-07 19:04:02 +01:00
|
|
|
#ifdef SIGNED
|
2020-03-08 20:33:19 +01:00
|
|
|
return nv / (((mixeng_real)IN_MAX - IN_MIN) / 2.f);
|
2004-11-07 19:04:02 +01:00
|
|
|
#else
|
2020-03-08 20:33:19 +01:00
|
|
|
return (nv - HALF) / ((mixeng_real)IN_MAX / 2.f);
|
2004-11-07 19:04:02 +01:00
|
|
|
#endif
|
2005-10-30 19:58:22 +01:00
|
|
|
#endif
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2013-10-22 11:26:28 +02:00
|
|
|
static inline IN_T glue (clip_, ET) (mixeng_real v)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2020-03-08 20:33:19 +01:00
|
|
|
if (v >= 1.f) {
|
2005-10-30 19:58:22 +01:00
|
|
|
return IN_MAX;
|
2020-03-08 20:33:19 +01:00
|
|
|
} else if (v < -1.f) {
|
2005-10-30 19:58:22 +01:00
|
|
|
return IN_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIGNED
|
2020-03-08 20:33:19 +01:00
|
|
|
return ENDIAN_CONVERT((IN_T)(v * (((mixeng_real)IN_MAX - IN_MIN) / 2.f)));
|
2005-10-30 19:58:22 +01:00
|
|
|
#else
|
2020-03-08 20:33:19 +01:00
|
|
|
return ENDIAN_CONVERT((IN_T)((v * ((mixeng_real)IN_MAX / 2.f)) + HALF));
|
2005-10-30 19:58:22 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !FLOAT_MIXENG */
|
|
|
|
|
|
|
|
static inline int64_t glue (conv_, ET) (IN_T v)
|
|
|
|
{
|
|
|
|
IN_T nv = ENDIAN_CONVERT (v);
|
2004-11-07 19:04:02 +01:00
|
|
|
#ifdef SIGNED
|
2005-10-30 19:58:22 +01:00
|
|
|
return ((int64_t) nv) << (32 - SHIFT);
|
2004-11-07 19:04:02 +01:00
|
|
|
#else
|
2005-10-30 19:58:22 +01:00
|
|
|
return ((int64_t) nv - HALF) << (32 - SHIFT);
|
2004-11-07 19:04:02 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static inline IN_T glue (clip_, ET) (int64_t v)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2020-03-08 20:33:20 +01:00
|
|
|
if (v >= 0x7fffffffLL) {
|
2004-11-07 19:04:02 +01:00
|
|
|
return IN_MAX;
|
2020-03-08 20:33:20 +01:00
|
|
|
} else if (v < -2147483648LL) {
|
2004-11-07 19:04:02 +01:00
|
|
|
return IN_MIN;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
#ifdef SIGNED
|
2005-10-30 19:58:22 +01:00
|
|
|
return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
|
2004-11-07 19:04:02 +01:00
|
|
|
#else
|
2005-10-30 19:58:22 +01:00
|
|
|
return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
|
2004-11-07 19:04:02 +01:00
|
|
|
#endif
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
#endif
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void glue (glue (conv_, ET), _to_stereo)
|
2011-01-05 01:05:47 +01:00
|
|
|
(struct st_sample *dst, const void *src, int samples)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2008-12-03 23:48:44 +01:00
|
|
|
struct st_sample *out = dst;
|
2004-11-07 19:04:02 +01:00
|
|
|
IN_T *in = (IN_T *) src;
|
2011-01-05 01:05:47 +01:00
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
while (samples--) {
|
2011-01-05 01:05:47 +01:00
|
|
|
out->l = glue (conv_, ET) (*in++);
|
|
|
|
out->r = glue (conv_, ET) (*in++);
|
2004-11-07 19:04:02 +01:00
|
|
|
out += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void glue (glue (conv_, ET), _to_mono)
|
2011-01-05 01:05:47 +01:00
|
|
|
(struct st_sample *dst, const void *src, int samples)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2008-12-03 23:48:44 +01:00
|
|
|
struct st_sample *out = dst;
|
2004-11-07 19:04:02 +01:00
|
|
|
IN_T *in = (IN_T *) src;
|
2011-01-05 01:05:47 +01:00
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
while (samples--) {
|
2011-01-05 01:05:47 +01:00
|
|
|
out->l = glue (conv_, ET) (in[0]);
|
2004-11-07 19:04:02 +01:00
|
|
|
out->r = out->l;
|
|
|
|
out += 1;
|
|
|
|
in += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void glue (glue (clip_, ET), _from_stereo)
|
2008-12-03 23:48:44 +01:00
|
|
|
(void *dst, const struct st_sample *src, int samples)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2008-12-03 23:48:44 +01:00
|
|
|
const struct st_sample *in = src;
|
2004-11-07 19:04:02 +01:00
|
|
|
IN_T *out = (IN_T *) dst;
|
|
|
|
while (samples--) {
|
2005-10-30 19:58:22 +01:00
|
|
|
*out++ = glue (clip_, ET) (in->l);
|
|
|
|
*out++ = glue (clip_, ET) (in->r);
|
2004-11-07 19:04:02 +01:00
|
|
|
in += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void glue (glue (clip_, ET), _from_mono)
|
2008-12-03 23:48:44 +01:00
|
|
|
(void *dst, const struct st_sample *src, int samples)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2008-12-03 23:48:44 +01:00
|
|
|
const struct st_sample *in = src;
|
2004-11-07 19:04:02 +01:00
|
|
|
IN_T *out = (IN_T *) dst;
|
|
|
|
while (samples--) {
|
2005-10-30 19:58:22 +01:00
|
|
|
*out++ = glue (clip_, ET) (in->l + in->r);
|
2004-11-07 19:04:02 +01:00
|
|
|
in += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#undef ET
|
2004-11-07 19:04:02 +01:00
|
|
|
#undef HALF
|
2012-05-18 13:08:14 +02:00
|
|
|
#undef IN_T
|