include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / dsound / dsound_convert.c
blobbdd62c8e51fb3a36fa4bad85a35cfb4a4f6879f6
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
38 #include <stdarg.h>
39 #include <math.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "mmsystem.h"
44 #include "wine/debug.h"
45 #include "dsound.h"
46 #include "dsound_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
50 #ifdef WORDS_BIGENDIAN
51 #define le16(x) RtlUshortByteSwap((x))
52 #define le32(x) RtlUlongByteSwap((x))
53 #else
54 #define le16(x) (x)
55 #define le32(x) (x)
56 #endif
58 static float get8(const IDirectSoundBufferImpl *dsb, BYTE *base, DWORD channel)
60 const BYTE *buf = base + channel;
61 return (buf[0] - 0x80) / (float)0x80;
64 static float get16(const IDirectSoundBufferImpl *dsb, BYTE *base, DWORD channel)
66 const BYTE *buf = base + 2 * channel;
67 const SHORT *sbuf = (const SHORT*)(buf);
68 SHORT sample = (SHORT)le16(*sbuf);
69 return sample / (float)0x8000;
72 static float get24(const IDirectSoundBufferImpl *dsb, BYTE *base, DWORD channel)
74 LONG sample;
75 const BYTE *buf = base + 3 * channel;
77 /* The next expression deliberately has an overflow for buf[2] >= 0x80,
78 this is how negative values are made.
80 sample = (buf[0] << 8) | (buf[1] << 16) | (buf[2] << 24);
81 return sample / (float)0x80000000U;
84 static float get32(const IDirectSoundBufferImpl *dsb, BYTE *base, DWORD channel)
86 const BYTE *buf = base + 4 * channel;
87 const LONG *sbuf = (const LONG*)(buf);
88 LONG sample = le32(*sbuf);
89 return sample / (float)0x80000000U;
92 static float getieee32(const IDirectSoundBufferImpl *dsb, BYTE *base, DWORD channel)
94 const BYTE *buf = base + 4 * channel;
95 const float *sbuf = (const float*)(buf);
96 /* The value will be clipped later, when put into some non-float buffer */
97 return *sbuf;
100 const bitsgetfunc getbpp[5] = {get8, get16, get24, get32, getieee32};
102 float get_mono(const IDirectSoundBufferImpl *dsb, BYTE *base, DWORD channel)
104 DWORD channels = dsb->pwfx->nChannels;
105 DWORD c;
106 float val = 0;
107 /* XXX: does Windows include LFE into the mix? */
108 for (c = 0; c < channels; c++)
109 val += dsb->get_aux(dsb, base, c);
110 val /= channels;
111 return val;
114 static inline unsigned char f_to_8(float value)
116 if(value <= -1.f)
117 return 0;
118 if(value >= 1.f * 0x7f / 0x80)
119 return 0xFF;
120 return lrintf((value + 1.f) * 0x80);
123 static inline SHORT f_to_16(float value)
125 if(value <= -1.f)
126 return 0x8000;
127 if(value >= 1.f * 0x7FFF / 0x8000)
128 return 0x7FFF;
129 return le16(lrintf(value * 0x8000));
132 static LONG f_to_24(float value)
134 if(value <= -1.f)
135 return 0x80000000;
136 if(value >= 1.f * 0x7FFFFF / 0x800000)
137 return 0x7FFFFF00;
138 return lrintf(value * 0x80000000U);
141 static inline LONG f_to_32(float value)
143 if(value <= -1.f)
144 return 0x80000000;
145 if(value >= 1.f)
146 return 0x7FFFFFFF;
147 return le32(lrintf(value * 0x80000000U));
150 void putieee32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
152 BYTE *buf = (BYTE *)dsb->device->tmp_buffer;
153 float *fbuf = (float*)(buf + pos + sizeof(float) * channel);
154 *fbuf = value;
157 void putieee32_sum(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
159 BYTE *buf = (BYTE *)dsb->device->tmp_buffer;
160 float *fbuf = (float*)(buf + pos + sizeof(float) * channel);
161 *fbuf += value;
164 void put_mono2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
166 dsb->put_aux(dsb, pos, 0, value);
167 dsb->put_aux(dsb, pos, 1, value);
170 void put_mono2quad(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
172 dsb->put_aux(dsb, pos, 0, value);
173 dsb->put_aux(dsb, pos, 1, value);
174 dsb->put_aux(dsb, pos, 2, value);
175 dsb->put_aux(dsb, pos, 3, value);
178 void put_stereo2quad(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
180 if (channel == 0) { /* Left */
181 dsb->put_aux(dsb, pos, 0, value); /* Front left */
182 dsb->put_aux(dsb, pos, 2, value); /* Back left */
183 } else if (channel == 1) { /* Right */
184 dsb->put_aux(dsb, pos, 1, value); /* Front right */
185 dsb->put_aux(dsb, pos, 3, value); /* Back right */
189 void put_mono2surround51(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
191 dsb->put_aux(dsb, pos, 0, value);
192 dsb->put_aux(dsb, pos, 1, value);
193 dsb->put_aux(dsb, pos, 2, value);
194 dsb->put_aux(dsb, pos, 3, value);
195 dsb->put_aux(dsb, pos, 4, value);
196 dsb->put_aux(dsb, pos, 5, value);
199 void put_stereo2surround51(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
201 if (channel == 0) { /* Left */
202 dsb->put_aux(dsb, pos, 0, value); /* Front left */
203 dsb->put_aux(dsb, pos, 4, value); /* Back left */
205 dsb->put_aux(dsb, pos, 2, 0.0f); /* Mute front centre */
206 dsb->put_aux(dsb, pos, 3, 0.0f); /* Mute LFE */
207 } else if (channel == 1) { /* Right */
208 dsb->put_aux(dsb, pos, 1, value); /* Front right */
209 dsb->put_aux(dsb, pos, 5, value); /* Back right */
213 void put_surround512stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
215 /* based on analyzing a recording of a dsound downmix */
216 switch(channel){
218 case 4: /* surround left */
219 value *= 0.24f;
220 dsb->put_aux(dsb, pos, 0, value);
221 break;
223 case 0: /* front left */
224 value *= 1.0f;
225 dsb->put_aux(dsb, pos, 0, value);
226 break;
228 case 5: /* surround right */
229 value *= 0.24f;
230 dsb->put_aux(dsb, pos, 1, value);
231 break;
233 case 1: /* front right */
234 value *= 1.0f;
235 dsb->put_aux(dsb, pos, 1, value);
236 break;
238 case 2: /* centre */
239 value *= 0.7;
240 dsb->put_aux(dsb, pos, 0, value);
241 dsb->put_aux(dsb, pos, 1, value);
242 break;
244 case 3:
245 /* LFE is totally ignored in dsound when downmixing to 2 channels */
246 break;
250 void put_surround712stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
252 /* based on analyzing a recording of a dsound downmix */
253 switch(channel){
255 case 6: /* back left */
256 value *= 0.24f;
257 dsb->put_aux(dsb, pos, 0, value);
258 break;
260 case 4: /* surround left */
261 value *= 0.24f;
262 dsb->put_aux(dsb, pos, 0, value);
263 break;
265 case 0: /* front left */
266 value *= 1.0f;
267 dsb->put_aux(dsb, pos, 0, value);
268 break;
270 case 7: /* back right */
271 value *= 0.24f;
272 dsb->put_aux(dsb, pos, 1, value);
273 break;
275 case 5: /* surround right */
276 value *= 0.24f;
277 dsb->put_aux(dsb, pos, 1, value);
278 break;
280 case 1: /* front right */
281 value *= 1.0f;
282 dsb->put_aux(dsb, pos, 1, value);
283 break;
285 case 2: /* centre */
286 value *= 0.7;
287 dsb->put_aux(dsb, pos, 0, value);
288 dsb->put_aux(dsb, pos, 1, value);
289 break;
291 case 3:
292 /* LFE is totally ignored in dsound when downmixing to 2 channels */
293 break;
297 void put_quad2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
299 /* based on pulseaudio's downmix algorithm */
300 switch(channel){
302 case 2: /* back left */
303 value *= 0.1f; /* (1/9) / (sum of left volumes) */
304 dsb->put_aux(dsb, pos, 0, value);
305 break;
307 case 0: /* front left */
308 value *= 0.9f; /* 1 / (sum of left volumes) */
309 dsb->put_aux(dsb, pos, 0, value);
310 break;
312 case 3: /* back right */
313 value *= 0.1f; /* (1/9) / (sum of right volumes) */
314 dsb->put_aux(dsb, pos, 1, value);
315 break;
317 case 1: /* front right */
318 value *= 0.9f; /* 1 / (sum of right volumes) */
319 dsb->put_aux(dsb, pos, 1, value);
320 break;
324 void mixieee32(float *src, float *dst, unsigned samples)
326 TRACE("%p - %p %d\n", src, dst, samples);
327 while (samples--)
328 *(dst++) += *(src++);
331 static void norm8(float *src, unsigned char *dst, unsigned samples)
333 TRACE("%p - %p %d\n", src, dst, samples);
334 while (samples--)
336 *dst = f_to_8(*src);
337 ++dst;
338 ++src;
342 static void norm16(float *src, SHORT *dst, unsigned samples)
344 TRACE("%p - %p %d\n", src, dst, samples);
345 while (samples--)
347 *dst = f_to_16(*src);
348 ++dst;
349 ++src;
353 static void norm24(float *src, BYTE *dst, unsigned samples)
355 TRACE("%p - %p %d\n", src, dst, samples);
356 while (samples--)
358 LONG t = f_to_24(*src);
359 dst[0] = (t >> 8) & 0xFF;
360 dst[1] = (t >> 16) & 0xFF;
361 dst[2] = t >> 24;
362 dst += 3;
363 ++src;
367 static void norm32(float *src, INT *dst, unsigned samples)
369 TRACE("%p - %p %d\n", src, dst, samples);
370 while (samples--)
372 *dst = f_to_32(*src);
373 ++dst;
374 ++src;
378 const normfunc normfunctions[4] = {
379 (normfunc)norm8,
380 (normfunc)norm16,
381 (normfunc)norm24,
382 (normfunc)norm32,