blob: 77cc89b9e8088bc12e9f66da66de2dd90c73bbd6 [file] [log] [blame]
bellard85571bc2004-11-07 18:04:02 +00001/*
2 * QEMU Mixing engine
bellard1d14ffa2005-10-30 18:58:22 +00003 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 *
bellard85571bc2004-11-07 18:04:02 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/*
26 * Tusen tack till Mike Nordell
27 * dec++'ified by Dscho
28 */
29
bellard1d14ffa2005-10-30 18:58:22 +000030#ifndef SIGNED
31#define HALF (IN_MAX >> 1)
bellard85571bc2004-11-07 18:04:02 +000032#endif
33
Roger Pau Monnea2885382012-05-18 12:08:14 +010034#define ET glue (ENDIAN_CONVERSION, glue (glue (glue (_, ITYPE), BSIZE), _t))
35#define IN_T glue (glue (ITYPE, BSIZE), _t)
bellard1d14ffa2005-10-30 18:58:22 +000036
37#ifdef FLOAT_MIXENG
Alex Bligh203cea22013-10-22 10:26:28 +010038static inline mixeng_real glue (conv_, ET) (IN_T v)
bellard1d14ffa2005-10-30 18:58:22 +000039{
40 IN_T nv = ENDIAN_CONVERT (v);
41
42#ifdef RECIPROCAL
43#ifdef SIGNED
malc1ea879e2008-12-03 22:48:44 +000044 return nv * (1.f / (mixeng_real) (IN_MAX - IN_MIN));
bellard1d14ffa2005-10-30 18:58:22 +000045#else
malc1ea879e2008-12-03 22:48:44 +000046 return (nv - HALF) * (1.f / (mixeng_real) IN_MAX);
bellard1d14ffa2005-10-30 18:58:22 +000047#endif
48#else /* !RECIPROCAL */
49#ifdef SIGNED
Juha Riihim?ki578c7b22011-05-31 19:40:21 +010050 return nv / (mixeng_real) ((mixeng_real) IN_MAX - IN_MIN);
bellard1d14ffa2005-10-30 18:58:22 +000051#else
malc1ea879e2008-12-03 22:48:44 +000052 return (nv - HALF) / (mixeng_real) IN_MAX;
bellard1d14ffa2005-10-30 18:58:22 +000053#endif
bellard85571bc2004-11-07 18:04:02 +000054#endif
55}
56
Alex Bligh203cea22013-10-22 10:26:28 +010057static inline IN_T glue (clip_, ET) (mixeng_real v)
bellard85571bc2004-11-07 18:04:02 +000058{
bellard1d14ffa2005-10-30 18:58:22 +000059 if (v >= 0.5) {
bellard85571bc2004-11-07 18:04:02 +000060 return IN_MAX;
bellard1d14ffa2005-10-30 18:58:22 +000061 }
62 else if (v < -0.5) {
bellard85571bc2004-11-07 18:04:02 +000063 return IN_MIN;
bellard1d14ffa2005-10-30 18:58:22 +000064 }
bellard85571bc2004-11-07 18:04:02 +000065
66#ifdef SIGNED
Juha Riihim?ki578c7b22011-05-31 19:40:21 +010067 return ENDIAN_CONVERT ((IN_T) (v * ((mixeng_real) IN_MAX - IN_MIN)));
bellard85571bc2004-11-07 18:04:02 +000068#else
bellard1d14ffa2005-10-30 18:58:22 +000069 return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF));
bellard85571bc2004-11-07 18:04:02 +000070#endif
71}
72
bellard1d14ffa2005-10-30 18:58:22 +000073#else /* !FLOAT_MIXENG */
74
75static inline int64_t glue (conv_, ET) (IN_T v)
bellard85571bc2004-11-07 18:04:02 +000076{
bellard1d14ffa2005-10-30 18:58:22 +000077 IN_T nv = ENDIAN_CONVERT (v);
78#ifdef SIGNED
79 return ((int64_t) nv) << (32 - SHIFT);
80#else
81 return ((int64_t) nv - HALF) << (32 - SHIFT);
82#endif
83}
84
85static inline IN_T glue (clip_, ET) (int64_t v)
86{
87 if (v >= 0x7f000000) {
88 return IN_MAX;
89 }
90 else if (v < -2147483648LL) {
91 return IN_MIN;
92 }
93
94#ifdef SIGNED
95 return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
96#else
97 return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
98#endif
99}
100#endif
101
102static void glue (glue (conv_, ET), _to_stereo)
Michael Walle00e07672011-01-05 01:05:47 +0100103 (struct st_sample *dst, const void *src, int samples)
bellard1d14ffa2005-10-30 18:58:22 +0000104{
malc1ea879e2008-12-03 22:48:44 +0000105 struct st_sample *out = dst;
bellard85571bc2004-11-07 18:04:02 +0000106 IN_T *in = (IN_T *) src;
Michael Walle00e07672011-01-05 01:05:47 +0100107
bellard85571bc2004-11-07 18:04:02 +0000108 while (samples--) {
Michael Walle00e07672011-01-05 01:05:47 +0100109 out->l = glue (conv_, ET) (*in++);
110 out->r = glue (conv_, ET) (*in++);
bellard85571bc2004-11-07 18:04:02 +0000111 out += 1;
112 }
113}
114
bellard1d14ffa2005-10-30 18:58:22 +0000115static void glue (glue (conv_, ET), _to_mono)
Michael Walle00e07672011-01-05 01:05:47 +0100116 (struct st_sample *dst, const void *src, int samples)
bellard85571bc2004-11-07 18:04:02 +0000117{
malc1ea879e2008-12-03 22:48:44 +0000118 struct st_sample *out = dst;
bellard85571bc2004-11-07 18:04:02 +0000119 IN_T *in = (IN_T *) src;
Michael Walle00e07672011-01-05 01:05:47 +0100120
bellard85571bc2004-11-07 18:04:02 +0000121 while (samples--) {
Michael Walle00e07672011-01-05 01:05:47 +0100122 out->l = glue (conv_, ET) (in[0]);
bellard85571bc2004-11-07 18:04:02 +0000123 out->r = out->l;
124 out += 1;
125 in += 1;
126 }
127}
128
bellard1d14ffa2005-10-30 18:58:22 +0000129static void glue (glue (clip_, ET), _from_stereo)
malc1ea879e2008-12-03 22:48:44 +0000130 (void *dst, const struct st_sample *src, int samples)
bellard85571bc2004-11-07 18:04:02 +0000131{
malc1ea879e2008-12-03 22:48:44 +0000132 const struct st_sample *in = src;
bellard85571bc2004-11-07 18:04:02 +0000133 IN_T *out = (IN_T *) dst;
134 while (samples--) {
bellard1d14ffa2005-10-30 18:58:22 +0000135 *out++ = glue (clip_, ET) (in->l);
136 *out++ = glue (clip_, ET) (in->r);
bellard85571bc2004-11-07 18:04:02 +0000137 in += 1;
138 }
139}
140
bellard1d14ffa2005-10-30 18:58:22 +0000141static void glue (glue (clip_, ET), _from_mono)
malc1ea879e2008-12-03 22:48:44 +0000142 (void *dst, const struct st_sample *src, int samples)
bellard85571bc2004-11-07 18:04:02 +0000143{
malc1ea879e2008-12-03 22:48:44 +0000144 const struct st_sample *in = src;
bellard85571bc2004-11-07 18:04:02 +0000145 IN_T *out = (IN_T *) dst;
146 while (samples--) {
bellard1d14ffa2005-10-30 18:58:22 +0000147 *out++ = glue (clip_, ET) (in->l + in->r);
bellard85571bc2004-11-07 18:04:02 +0000148 in += 1;
149 }
150}
151
bellard1d14ffa2005-10-30 18:58:22 +0000152#undef ET
bellard85571bc2004-11-07 18:04:02 +0000153#undef HALF
Roger Pau Monnea2885382012-05-18 12:08:14 +0100154#undef IN_T