0.7.0-alt1
[qemu] / qemu / audio / mixeng_template.h
1 /*
2  * QEMU Mixing engine
3  * 
4  * Copyright (c) 2004 Vassili Karpov (malc)
5  * 
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
30 #ifdef SIGNED
31 #define HALFT IN_MAX
32 #define HALF IN_MAX
33 #else
34 #define HALFT ((IN_MAX)>>1)
35 #define HALF HALFT
36 #endif
37
38 static int64_t inline glue(conv_,IN_T) (IN_T v)
39 {
40 #ifdef SIGNED
41     return (INT_MAX*(int64_t)v)/HALF;
42 #else
43     return (INT_MAX*((int64_t)v-HALFT))/HALF;
44 #endif
45 }
46
47 static IN_T inline glue(clip_,IN_T) (int64_t v)
48 {
49     if (v >= INT_MAX)
50         return IN_MAX;
51     else if (v < -INT_MAX)
52         return IN_MIN;
53
54 #ifdef SIGNED
55     return (IN_T) (v*HALF/INT_MAX);
56 #else
57     return (IN_T) (v+INT_MAX/2)*HALF/INT_MAX;
58 #endif
59 }
60
61 static void glue(glue(conv_,IN_T),_to_stereo) (void *dst, const void *src,
62                                                int samples)
63 {
64     st_sample_t *out = (st_sample_t *) dst;
65     IN_T *in = (IN_T *) src;
66     while (samples--) {
67         out->l = glue(conv_,IN_T) (*in++);
68         out->r = glue(conv_,IN_T) (*in++);
69         out += 1;
70     }
71 }
72
73 static void glue(glue(conv_,IN_T),_to_mono) (void *dst, const void *src,
74                                              int samples)
75 {
76     st_sample_t *out = (st_sample_t *) dst;
77     IN_T *in = (IN_T *) src;
78     while (samples--) {
79         out->l = glue(conv_,IN_T) (in[0]);
80         out->r = out->l;
81         out += 1;
82         in += 1;
83     }
84 }
85
86 static void glue(glue(clip_,IN_T),_from_stereo) (void *dst, const void *src,
87                                                  int samples)
88 {
89     st_sample_t *in = (st_sample_t *) src;
90     IN_T *out = (IN_T *) dst;
91     while (samples--) {
92         *out++ = glue(clip_,IN_T) (in->l);
93         *out++ = glue(clip_,IN_T) (in->r);
94         in += 1;
95     }
96 }
97
98 static void glue(glue(clip_,IN_T),_from_mono) (void *dst, const void *src,
99                                                int samples)
100 {
101     st_sample_t *in = (st_sample_t *) src;
102     IN_T *out = (IN_T *) dst;
103     while (samples--) {
104         *out++ = glue(clip_,IN_T) (in->l + in->r);
105         in += 1;
106     }
107 }
108
109 #undef HALF
110 #undef HALFT
111