1 /* DirectSound format conversion and mixing routines
3 * Copyright 2007 Maarten Lankhorst
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* 8 bits is unsigned, the rest is signed.
21 * First I tried to reuse existing stuff from alsa-lib, after that
22 * didn't work, I gave up and just went for individual hacks.
24 * 24 bit is expensive to do, due to unaligned access.
25 * In dlls/winex11.drv/dib_convert.c convert_888_to_0888_asis there is a way
26 * around it, but I'm happy current code works, maybe something for later.
28 * The ^ 0x80 flips the signed bit, this is the conversion from
29 * signed (-128.. 0.. 127) to unsigned (0...255)
30 * This is only temporary: All 8 bit data should be converted to signed.
31 * then when fed to the sound card, it should be converted to unsigned again.
33 * Sound is LITTLE endian
42 #define NONAMELESSSTRUCT
43 #define NONAMELESSUNION
48 #include "wine/debug.h"
51 #include "dsound_private.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
59 #ifdef WORDS_BIGENDIAN
60 #define le16(x) RtlUshortByteSwap((x))
61 #define le32(x) RtlUlongByteSwap((x))
67 static void convert_8_to_8 (const void *src, void *dst)
70 *dest = *(uint8_t*)src;
73 static void convert_8_to_16 (const void *src, void *dst)
75 uint16_t dest = *(uint8_t*)src, *dest16 = dst;
76 *dest16 = le16(dest * 257 - 32768);
79 static void convert_8_to_24 (const void *src, void *dst)
81 uint8_t dest = *(uint8_t*)src;
82 int24_struct *dest24 = dst;
83 dest24->byte[0] = dest;
84 dest24->byte[1] = dest;
85 dest24->byte[2] = dest - 0x80;
88 static void convert_8_to_32 (const void *src, void *dst)
90 uint32_t dest = *(uint8_t*)src, *dest32 = dst;
91 *dest32 = le32(dest * 16843009 - 2147483648U);
94 static void convert_16_to_8 (const void *src, void *dst)
97 *dst8 = (le16(*(uint16_t*)src)) / 256;
101 static void convert_16_to_16 (const void *src, void *dst)
103 uint16_t *dest = dst;
104 *dest = *(uint16_t*)src;
107 static void convert_16_to_24 (const void *src, void *dst)
109 uint16_t dest = le16(*(uint16_t*)src);
110 int24_struct *dest24 = dst;
112 dest24->byte[0] = dest / 256;
113 dest24->byte[1] = dest;
114 dest24->byte[2] = dest / 256;
117 static void convert_16_to_32 (const void *src, void *dst)
119 uint32_t dest = *(uint16_t*)src, *dest32 = dst;
120 *dest32 = dest * 65537;
123 static void convert_24_to_8 (const void *src, void *dst)
126 *dst8 = ((int24_struct*)src)->byte[2];
129 static void convert_24_to_16 (const void *src, void *dst)
131 uint16_t *dest16 = dst;
132 const int24_struct *source = src;
133 *dest16 = le16(source->byte[2] * 256 + source->byte[1]);
136 static void convert_24_to_24 (const void *src, void *dst)
138 int24_struct *dest24 = dst;
139 const int24_struct *src24 = src;
143 static void convert_24_to_32 (const void *src, void *dst)
145 uint32_t *dest32 = dst;
146 const int24_struct *source = src;
147 *dest32 = le32(source->byte[2] * 16777217 + source->byte[1] * 65536 + source->byte[0] * 256);
150 static void convert_32_to_8 (const void *src, void *dst)
153 *dst8 = (le32(*(uint32_t*)src) / 16777216);
157 static void convert_32_to_16 (const void *src, void *dst)
159 uint16_t *dest16 = dst;
160 *dest16 = le16(le32(*(uint32_t*)src) / 65536);
163 static void convert_32_to_24 (const void *src, void *dst)
165 uint32_t dest = le32(*(uint32_t*)dst);
166 int24_struct *dest24 = dst;
168 dest24->byte[0] = dest / 256;
169 dest24->byte[1] = dest / 65536;
170 dest24->byte[2] = dest / 16777216;
173 static void convert_32_to_32 (const void *src, void *dst)
175 uint32_t *dest = dst;
176 *dest = *(uint32_t*)src;
179 const bitsconvertfunc convertbpp[4][4] = {
180 { convert_8_to_8, convert_8_to_16, convert_8_to_24, convert_8_to_32 },
181 { convert_16_to_8, convert_16_to_16, convert_16_to_24, convert_16_to_32 },
182 { convert_24_to_8, convert_24_to_16, convert_24_to_24, convert_24_to_32 },
183 { convert_32_to_8, convert_32_to_16, convert_32_to_24, convert_32_to_32 },
186 static void mix8(int8_t *src, int32_t *dst, unsigned len)
188 TRACE("%p - %p %d\n", src, dst, len);
190 /* 8-bit WAV is unsigned, it's here converted to signed, normalize function will convert it back again */
191 *(dst++) += (int8_t)((uint8_t)*(src++) - (uint8_t)0x80);
194 static void mix16(int16_t *src, int32_t *dst, unsigned len)
196 TRACE("%p - %p %d\n", src, dst, len);
205 static void mix24(int24_struct *src, int32_t *dst, unsigned len)
207 TRACE("%p - %p %d\n", src, dst, len);
212 field = ((unsigned)src->byte[2] << 16) + ((unsigned)src->byte[1] << 8) + (unsigned)src->byte[0];
213 if (src->byte[2] & 0x80)
214 field |= 0xFF000000U;
220 static void mix32(int32_t *src, int64_t *dst, unsigned len)
222 TRACE("%p - %p %d\n", src, dst, len);
225 *(dst++) += le32(*(src++));
228 const mixfunc mixfunctions[4] = {
235 static void norm8(int32_t *src, int8_t *dst, unsigned len)
237 TRACE("%p - %p %d\n", src, dst, len);
240 *dst = (*src) + 0x80;
243 else if (*src > 0x7f)
250 static void norm16(int32_t *src, int16_t *dst, unsigned len)
252 TRACE("%p - %p %d\n", src, dst, len);
259 else if (*src > 0x7fff)
266 static void norm24(int32_t *src, int24_struct *dst, unsigned len)
268 TRACE("%p - %p %d\n", src, dst, len);
272 if (*src <= -0x800000)
278 else if (*src > 0x7fffff)
287 dst->byte[1] = *src >> 8;
288 dst->byte[2] = *src >> 16;
295 static void norm32(int64_t *src, int32_t *dst, unsigned len)
297 TRACE("%p - %p %d\n", src, dst, len);
302 if (*src <= -(int64_t)0x80000000)
303 *dst = le32(0x80000000);
304 else if (*src > 0x7fffffff)
305 *dst = le32(0x7fffffff);
311 const normfunc normfunctions[4] = {