VirtualBox

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

最後變更 在這個檔案從13416是 6521,由 vboxsync 提交於 17 年 前

audio: fixed mixing of 8 bit unsigned; 32-bit updates from qemu

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

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