msvcr: Fix the event_wait() spec entries.
[wine.git] / dlls / dsound / dsound_convert.c
blob3519337c3c07c89178711fe80ab2026d8dcd1b3a
1 /* DirectSound format conversion and mixing routines
3 * Copyright 2007 Maarten Lankhorst
4 * Copyright 2011 Owen Rudge for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* 8 bits is unsigned, the rest is signed.
22 * First I tried to reuse existing stuff from alsa-lib, after that
23 * didn't work, I gave up and just went for individual hacks.
25 * 24 bit is expensive to do, due to unaligned access.
26 * In dlls/winex11.drv/dib_convert.c convert_888_to_0888_asis there is a way
27 * around it, but I'm happy current code works, maybe something for later.
29 * The ^ 0x80 flips the signed bit, this is the conversion from
30 * signed (-128.. 0.. 127) to unsigned (0...255)
31 * This is only temporary: All 8 bit data should be converted to signed.
32 * then when fed to the sound card, it should be converted to unsigned again.
34 * Sound is LITTLE endian
37 #include "config.h"
38 #include "wine/port.h"
40 #include <stdarg.h>
41 #include <math.h>
43 #include "windef.h"
44 #include "winbase.h"
45 #include "mmsystem.h"
46 #include "winternl.h"
47 #include "wine/debug.h"
48 #include "dsound.h"
49 #include "dsound_private.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
53 #ifdef WORDS_BIGENDIAN
54 #define le16(x) RtlUshortByteSwap((x))
55 #define le32(x) RtlUlongByteSwap((x))
56 #else
57 #define le16(x) (x)
58 #define le32(x) (x)
59 #endif
61 static float get8(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
63 const BYTE* buf = dsb->buffer->memory;
64 buf += pos + channel;
65 return (buf[0] - 0x80) / (float)0x80;
68 static float get16(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
70 const BYTE* buf = dsb->buffer->memory;
71 const SHORT *sbuf = (const SHORT*)(buf + pos + 2 * channel);
72 SHORT sample = (SHORT)le16(*sbuf);
73 return sample / (float)0x8000;
76 static float get24(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
78 LONG sample;
79 const BYTE* buf = dsb->buffer->memory;
80 buf += pos + 3 * channel;
81 /* The next expression deliberately has an overflow for buf[2] >= 0x80,
82 this is how negative values are made.
84 sample = (buf[0] << 8) | (buf[1] << 16) | (buf[2] << 24);
85 return sample / (float)0x80000000U;
88 static float get32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
90 const BYTE* buf = dsb->buffer->memory;
91 const LONG *sbuf = (const LONG*)(buf + pos + 4 * channel);
92 LONG sample = le32(*sbuf);
93 return sample / (float)0x80000000U;
96 static float getieee32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
98 const BYTE* buf = dsb->buffer->memory;
99 const float *sbuf = (const float*)(buf + pos + 4 * channel);
100 /* The value will be clipped later, when put into some non-float buffer */
101 return *sbuf;
104 const bitsgetfunc getbpp[5] = {get8, get16, get24, get32, getieee32};
106 float get_mono(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
108 DWORD channels = dsb->pwfx->nChannels;
109 DWORD c;
110 float val = 0;
111 /* XXX: does Windows include LFE into the mix? */
112 for (c = 0; c < channels; c++)
113 val += dsb->get_aux(dsb, pos, c);
114 val /= channels;
115 return val;
118 static inline unsigned char f_to_8(float value)
120 if(value <= -1.f)
121 return 0;
122 if(value >= 1.f * 0x7f / 0x80)
123 return 0xFF;
124 return lrintf((value + 1.f) * 0x80);
127 static inline SHORT f_to_16(float value)
129 if(value <= -1.f)
130 return 0x8000;
131 if(value >= 1.f * 0x7FFF / 0x8000)
132 return 0x7FFF;
133 return le16(lrintf(value * 0x8000));
136 static LONG f_to_24(float value)
138 if(value <= -1.f)
139 return 0x80000000;
140 if(value >= 1.f * 0x7FFFFF / 0x800000)
141 return 0x7FFFFF00;
142 return lrintf(value * 0x80000000U);
145 static inline LONG f_to_32(float value)
147 if(value <= -1.f)
148 return 0x80000000;
149 if(value >= 1.f * 0x7FFFFFFF / 0x80000000U) /* this rounds to 1.f */
150 return 0x7FFFFFFF;
151 return le32(lrintf(value * 0x80000000U));
154 void putieee32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
156 BYTE *buf = (BYTE *)dsb->device->tmp_buffer;
157 float *fbuf = (float*)(buf + pos + sizeof(float) * channel);
158 *fbuf = value;
161 void putieee32_sum(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
163 BYTE *buf = (BYTE *)dsb->device->tmp_buffer;
164 float *fbuf = (float*)(buf + pos + sizeof(float) * channel);
165 *fbuf += value;
168 void put_mono2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
170 dsb->put_aux(dsb, pos, 0, value);
171 dsb->put_aux(dsb, pos, 1, value);
174 void put_mono2quad(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
176 dsb->put_aux(dsb, pos, 0, value);
177 dsb->put_aux(dsb, pos, 1, value);
178 dsb->put_aux(dsb, pos, 2, value);
179 dsb->put_aux(dsb, pos, 3, value);
182 void put_stereo2quad(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
184 if (channel == 0) { /* Left */
185 dsb->put_aux(dsb, pos, 0, value); /* Front left */
186 dsb->put_aux(dsb, pos, 2, value); /* Back left */
187 } else if (channel == 1) { /* Right */
188 dsb->put_aux(dsb, pos, 1, value); /* Front right */
189 dsb->put_aux(dsb, pos, 3, value); /* Back right */
193 void put_mono2surround51(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
195 dsb->put_aux(dsb, pos, 0, value);
196 dsb->put_aux(dsb, pos, 1, value);
197 dsb->put_aux(dsb, pos, 2, value);
198 dsb->put_aux(dsb, pos, 3, value);
199 dsb->put_aux(dsb, pos, 4, value);
200 dsb->put_aux(dsb, pos, 5, value);
203 void put_stereo2surround51(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
205 if (channel == 0) { /* Left */
206 dsb->put_aux(dsb, pos, 0, value); /* Front left */
207 dsb->put_aux(dsb, pos, 4, value); /* Back left */
209 dsb->put_aux(dsb, pos, 2, 0.0f); /* Mute front centre */
210 dsb->put_aux(dsb, pos, 3, 0.0f); /* Mute LFE */
211 } else if (channel == 1) { /* Right */
212 dsb->put_aux(dsb, pos, 1, value); /* Front right */
213 dsb->put_aux(dsb, pos, 5, value); /* Back right */
217 void put_surround512stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
219 /* based on analyzing a recording of a dsound downmix */
220 switch(channel){
222 case 4: /* surround left */
223 value *= 0.24f;
224 dsb->put_aux(dsb, pos, 0, value);
225 break;
227 case 0: /* front left */
228 value *= 1.0f;
229 dsb->put_aux(dsb, pos, 0, value);
230 break;
232 case 5: /* surround right */
233 value *= 0.24f;
234 dsb->put_aux(dsb, pos, 1, value);
235 break;
237 case 1: /* front right */
238 value *= 1.0f;
239 dsb->put_aux(dsb, pos, 1, value);
240 break;
242 case 2: /* centre */
243 value *= 0.7;
244 dsb->put_aux(dsb, pos, 0, value);
245 dsb->put_aux(dsb, pos, 1, value);
246 break;
248 case 3:
249 /* LFE is totally ignored in dsound when downmixing to 2 channels */
250 break;
254 void put_surround712stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
256 /* based on analyzing a recording of a dsound downmix */
257 switch(channel){
259 case 6: /* back left */
260 value *= 0.24f;
261 dsb->put_aux(dsb, pos, 0, value);
262 break;
264 case 4: /* surround left */
265 value *= 0.24f;
266 dsb->put_aux(dsb, pos, 0, value);
267 break;
269 case 0: /* front left */
270 value *= 1.0f;
271 dsb->put_aux(dsb, pos, 0, value);
272 break;
274 case 7: /* back right */
275 value *= 0.24f;
276 dsb->put_aux(dsb, pos, 1, value);
277 break;
279 case 5: /* surround right */
280 value *= 0.24f;
281 dsb->put_aux(dsb, pos, 1, value);
282 break;
284 case 1: /* front right */
285 value *= 1.0f;
286 dsb->put_aux(dsb, pos, 1, value);
287 break;
289 case 2: /* centre */
290 value *= 0.7;
291 dsb->put_aux(dsb, pos, 0, value);
292 dsb->put_aux(dsb, pos, 1, value);
293 break;
295 case 3:
296 /* LFE is totally ignored in dsound when downmixing to 2 channels */
297 break;
301 void put_quad2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
303 /* based on pulseaudio's downmix algorithm */
304 switch(channel){
306 case 2: /* back left */
307 value *= 0.1f; /* (1/9) / (sum of left volumes) */
308 dsb->put_aux(dsb, pos, 0, value);
309 break;
311 case 0: /* front left */
312 value *= 0.9f; /* 1 / (sum of left volumes) */
313 dsb->put_aux(dsb, pos, 0, value);
314 break;
316 case 3: /* back right */
317 value *= 0.1f; /* (1/9) / (sum of right volumes) */
318 dsb->put_aux(dsb, pos, 1, value);
319 break;
321 case 1: /* front right */
322 value *= 0.9f; /* 1 / (sum of right volumes) */
323 dsb->put_aux(dsb, pos, 1, value);
324 break;
328 void mixieee32(float *src, float *dst, unsigned samples)
330 TRACE("%p - %p %d\n", src, dst, samples);
331 while (samples--)
332 *(dst++) += *(src++);
335 static void norm8(float *src, unsigned char *dst, unsigned samples)
337 TRACE("%p - %p %d\n", src, dst, samples);
338 while (samples--)
340 *dst = f_to_8(*src);
341 ++dst;
342 ++src;
346 static void norm16(float *src, SHORT *dst, unsigned samples)
348 TRACE("%p - %p %d\n", src, dst, samples);
349 while (samples--)
351 *dst = f_to_16(*src);
352 ++dst;
353 ++src;
357 static void norm24(float *src, BYTE *dst, unsigned samples)
359 TRACE("%p - %p %d\n", src, dst, samples);
360 while (samples--)
362 LONG t = f_to_24(*src);
363 dst[0] = (t >> 8) & 0xFF;
364 dst[1] = (t >> 16) & 0xFF;
365 dst[2] = t >> 24;
366 dst += 3;
367 ++src;
371 static void norm32(float *src, INT *dst, unsigned samples)
373 TRACE("%p - %p %d\n", src, dst, samples);
374 while (samples--)
376 *dst = f_to_32(*src);
377 ++dst;
378 ++src;
382 const normfunc normfunctions[4] = {
383 (normfunc)norm8,
384 (normfunc)norm16,
385 (normfunc)norm24,
386 (normfunc)norm32,