VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/mixeng_template.h@ 3778

Last change on this file since 3778 was 355, checked in by vboxsync, 18 years ago

activated soft volume mixing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1/*
2 * QEMU Mixing engine
3 *
4 * Copyright (c) 2004-2005 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#ifndef SIGNED
31#define HALF (IN_MAX >> 1)
32#endif
33
34#ifdef NOVOL
35#define VOL(a, b) a
36#else
37#ifdef VBOX
38#ifdef SIGNED
39#define VOL(a, b) ((ASMMult2xS32RetS64(a, b) >> 31))
40#else
41#define VOL(a, b) ((ASMMult2xU32RetU64(a, b) >> 31))
42#endif
43#else /* !VBOX */
44#ifdef FLOAT_MIXENG
45#define VOL(a, b) ((a) * (b))
46#else
47#define VOL(a, b) ((a) * (b)) >> 32
48#endif
49#endif
50#endif /* !VBOX */
51
52#define ET glue (ENDIAN_CONVERSION, glue (_, IN_T))
53
54#ifdef FLOAT_MIXENG
55static real_t inline glue (conv_, ET) (IN_T v)
56{
57 IN_T nv = ENDIAN_CONVERT (v);
58
59#ifdef RECIPROCAL
60#ifdef SIGNED
61 return nv * (1.f / (real_t) (IN_MAX - IN_MIN));
62#else
63 return (nv - HALF) * (1.f / (real_t) IN_MAX);
64#endif
65#else /* !RECIPROCAL */
66#ifdef SIGNED
67 return nv / (real_t) (IN_MAX - IN_MIN);
68#else
69 return (nv - HALF) / (real_t) IN_MAX;
70#endif
71#endif
72}
73
74static IN_T inline glue (clip_, ET) (real_t v)
75{
76 if (v >= 0.5) {
77 return IN_MAX;
78 }
79 else if (v < -0.5) {
80 return IN_MIN;
81 }
82
83#ifdef SIGNED
84 return ENDIAN_CONVERT ((IN_T) (v * (IN_MAX - IN_MIN)));
85#else
86 return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF));
87#endif
88}
89
90#else /* !FLOAT_MIXENG */
91
92static inline int64_t glue (conv_, ET) (IN_T v)
93{
94 IN_T nv = ENDIAN_CONVERT (v);
95#ifdef SIGNED
96 return ((int64_t) nv) << (32 - SHIFT);
97#else
98 return ((int64_t) nv - HALF) << (32 - SHIFT);
99#endif
100}
101
102static inline IN_T glue (clip_, ET) (int64_t v)
103{
104 if (v >= 0x7f000000) {
105 return IN_MAX;
106 }
107 else if (v < -2147483648LL) {
108 return IN_MIN;
109 }
110
111#ifdef SIGNED
112 return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
113#else
114 return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
115#endif
116}
117#endif
118
119static void glue (glue (conv_, ET), _to_stereo)
120 (st_sample_t *dst, const void *src, int samples, volume_t *vol)
121{
122 st_sample_t *out = dst;
123 IN_T *in = (IN_T *) src;
124#ifndef NOVOL
125 if (vol->mute) {
126 mixeng_clear (dst, samples);
127 return;
128 }
129#else
130 (void) vol;
131#endif
132 while (samples--) {
133 out->l = VOL (glue (conv_, ET) (*in++), vol->l);
134 out->r = VOL (glue (conv_, ET) (*in++), vol->r);
135 out += 1;
136 }
137}
138
139static void glue (glue (conv_, ET), _to_mono)
140 (st_sample_t *dst, const void *src, int samples, volume_t *vol)
141{
142 st_sample_t *out = dst;
143 IN_T *in = (IN_T *) src;
144#ifndef NOVOL
145 if (vol->mute) {
146 mixeng_clear (dst, samples);
147 return;
148 }
149#else
150 (void) vol;
151#endif
152 while (samples--) {
153 out->l = VOL (glue (conv_, ET) (in[0]), vol->l);
154 out->r = out->l;
155 out += 1;
156 in += 1;
157 }
158}
159
160static void glue (glue (clip_, ET), _from_stereo)
161 (void *dst, const st_sample_t *src, int samples)
162{
163 const st_sample_t *in = src;
164 IN_T *out = (IN_T *) dst;
165 while (samples--) {
166 *out++ = glue (clip_, ET) (in->l);
167 *out++ = glue (clip_, ET) (in->r);
168 in += 1;
169 }
170}
171
172static void glue (glue (clip_, ET), _from_mono)
173 (void *dst, const st_sample_t *src, int samples)
174{
175 const st_sample_t *in = src;
176 IN_T *out = (IN_T *) dst;
177 while (samples--) {
178 *out++ = glue (clip_, ET) (in->l + in->r);
179 in += 1;
180 }
181}
182
183#undef ET
184#undef HALF
185#undef VOL
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette