]> git.proxmox.com Git - qemu.git/blame - audio/mixeng_template.h
Merge remote-tracking branch 'origin/master' into staging
[qemu.git] / audio / mixeng_template.h
CommitLineData
85571bc7
FB
1/*
2 * QEMU Mixing engine
1d14ffa9
FB
3 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 *
85571bc7
FB
6 * 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
1d14ffa9
FB
30#ifndef SIGNED
31#define HALF (IN_MAX >> 1)
32#endif
33
1d14ffa9
FB
34#define ET glue (ENDIAN_CONVERSION, glue (_, IN_T))
35
36#ifdef FLOAT_MIXENG
1ea879e5 37static mixeng_real inline glue (conv_, ET) (IN_T v)
1d14ffa9
FB
38{
39 IN_T nv = ENDIAN_CONVERT (v);
40
41#ifdef RECIPROCAL
42#ifdef SIGNED
1ea879e5 43 return nv * (1.f / (mixeng_real) (IN_MAX - IN_MIN));
1d14ffa9 44#else
1ea879e5 45 return (nv - HALF) * (1.f / (mixeng_real) IN_MAX);
1d14ffa9
FB
46#endif
47#else /* !RECIPROCAL */
85571bc7 48#ifdef SIGNED
578c7b2c 49 return nv / (mixeng_real) ((mixeng_real) IN_MAX - IN_MIN);
85571bc7 50#else
1ea879e5 51 return (nv - HALF) / (mixeng_real) IN_MAX;
85571bc7 52#endif
1d14ffa9
FB
53#endif
54}
85571bc7 55
1ea879e5 56static IN_T inline glue (clip_, ET) (mixeng_real v)
85571bc7 57{
1d14ffa9
FB
58 if (v >= 0.5) {
59 return IN_MAX;
60 }
61 else if (v < -0.5) {
62 return IN_MIN;
63 }
64
65#ifdef SIGNED
578c7b2c 66 return ENDIAN_CONVERT ((IN_T) (v * ((mixeng_real) IN_MAX - IN_MIN)));
1d14ffa9
FB
67#else
68 return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF));
69#endif
70}
71
72#else /* !FLOAT_MIXENG */
73
74static inline int64_t glue (conv_, ET) (IN_T v)
75{
76 IN_T nv = ENDIAN_CONVERT (v);
85571bc7 77#ifdef SIGNED
1d14ffa9 78 return ((int64_t) nv) << (32 - SHIFT);
85571bc7 79#else
1d14ffa9 80 return ((int64_t) nv - HALF) << (32 - SHIFT);
85571bc7
FB
81#endif
82}
83
1d14ffa9 84static inline IN_T glue (clip_, ET) (int64_t v)
85571bc7 85{
1d14ffa9 86 if (v >= 0x7f000000) {
85571bc7 87 return IN_MAX;
1d14ffa9
FB
88 }
89 else if (v < -2147483648LL) {
85571bc7 90 return IN_MIN;
1d14ffa9 91 }
85571bc7
FB
92
93#ifdef SIGNED
1d14ffa9 94 return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
85571bc7 95#else
1d14ffa9 96 return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
85571bc7
FB
97#endif
98}
1d14ffa9 99#endif
85571bc7 100
1d14ffa9 101static void glue (glue (conv_, ET), _to_stereo)
00e07679 102 (struct st_sample *dst, const void *src, int samples)
85571bc7 103{
1ea879e5 104 struct st_sample *out = dst;
85571bc7 105 IN_T *in = (IN_T *) src;
00e07679 106
85571bc7 107 while (samples--) {
00e07679
MW
108 out->l = glue (conv_, ET) (*in++);
109 out->r = glue (conv_, ET) (*in++);
85571bc7
FB
110 out += 1;
111 }
112}
113
1d14ffa9 114static void glue (glue (conv_, ET), _to_mono)
00e07679 115 (struct st_sample *dst, const void *src, int samples)
85571bc7 116{
1ea879e5 117 struct st_sample *out = dst;
85571bc7 118 IN_T *in = (IN_T *) src;
00e07679 119
85571bc7 120 while (samples--) {
00e07679 121 out->l = glue (conv_, ET) (in[0]);
85571bc7
FB
122 out->r = out->l;
123 out += 1;
124 in += 1;
125 }
126}
127
1d14ffa9 128static void glue (glue (clip_, ET), _from_stereo)
1ea879e5 129 (void *dst, const struct st_sample *src, int samples)
85571bc7 130{
1ea879e5 131 const struct st_sample *in = src;
85571bc7
FB
132 IN_T *out = (IN_T *) dst;
133 while (samples--) {
1d14ffa9
FB
134 *out++ = glue (clip_, ET) (in->l);
135 *out++ = glue (clip_, ET) (in->r);
85571bc7
FB
136 in += 1;
137 }
138}
139
1d14ffa9 140static void glue (glue (clip_, ET), _from_mono)
1ea879e5 141 (void *dst, const struct st_sample *src, int samples)
85571bc7 142{
1ea879e5 143 const struct st_sample *in = src;
85571bc7
FB
144 IN_T *out = (IN_T *) dst;
145 while (samples--) {
1d14ffa9 146 *out++ = glue (clip_, ET) (in->l + in->r);
85571bc7
FB
147 in += 1;
148 }
149}
150
1d14ffa9 151#undef ET
85571bc7 152#undef HALF