makefiles: Generate the winetest resource list from makedep.
[wine.git] / dlls / dsound / dsound_convert.c
blob26dbb3220ae0af58a1e5719b809c42cad9a3d181
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 pulseaudio's downmix algorithm */
220 switch(channel){
222 case 4: /* back left */
223 value *= 0.056f; /* (1/9) / (sum of left volumes) */
224 dsb->put_aux(dsb, pos, 0, value);
225 break;
227 case 0: /* front left */
228 value *= 0.503f; /* 1 / (sum of left volumes) */
229 dsb->put_aux(dsb, pos, 0, value);
230 break;
232 case 5: /* back right */
233 value *= 0.056f; /* (1/9) / (sum of right volumes) */
234 dsb->put_aux(dsb, pos, 1, value);
235 break;
237 case 1: /* front right */
238 value *= 0.503f; /* 1 / (sum of right volumes) */
239 dsb->put_aux(dsb, pos, 1, value);
240 break;
242 case 2: /* front centre */
243 value *= 0.252f; /* 0.5 / (sum of left/right volumes) */
244 dsb->put_aux(dsb, pos, 0, value);
245 dsb->put_aux(dsb, pos, 1, value);
246 break;
248 case 3: /* LFE */
249 value *= 0.189f; /* 0.375 / (sum of left/right volumes) */
250 dsb->put_aux(dsb, pos, 0, value);
251 dsb->put_aux(dsb, pos, 1, value);
252 break;
256 void put_quad2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
258 /* based on pulseaudio's downmix algorithm */
259 switch(channel){
261 case 2: /* back left */
262 value *= 0.1f; /* (1/9) / (sum of left volumes) */
263 dsb->put_aux(dsb, pos, 0, value);
264 break;
266 case 0: /* front left */
267 value *= 0.9f; /* 1 / (sum of left volumes) */
268 dsb->put_aux(dsb, pos, 0, value);
269 break;
271 case 3: /* back right */
272 value *= 0.1f; /* (1/9) / (sum of right volumes) */
273 dsb->put_aux(dsb, pos, 1, value);
274 break;
276 case 1: /* front right */
277 value *= 0.9f; /* 1 / (sum of right volumes) */
278 dsb->put_aux(dsb, pos, 1, value);
279 break;
283 void mixieee32(float *src, float *dst, unsigned samples)
285 TRACE("%p - %p %d\n", src, dst, samples);
286 while (samples--)
287 *(dst++) += *(src++);
290 static void norm8(float *src, unsigned char *dst, unsigned samples)
292 TRACE("%p - %p %d\n", src, dst, samples);
293 while (samples--)
295 *dst = f_to_8(*src);
296 ++dst;
297 ++src;
301 static void norm16(float *src, SHORT *dst, unsigned samples)
303 TRACE("%p - %p %d\n", src, dst, samples);
304 while (samples--)
306 *dst = f_to_16(*src);
307 ++dst;
308 ++src;
312 static void norm24(float *src, BYTE *dst, unsigned samples)
314 TRACE("%p - %p %d\n", src, dst, samples);
315 while (samples--)
317 LONG t = f_to_24(*src);
318 dst[0] = (t >> 8) & 0xFF;
319 dst[1] = (t >> 16) & 0xFF;
320 dst[2] = t >> 24;
321 dst += 3;
322 ++src;
326 static void norm32(float *src, INT *dst, unsigned samples)
328 TRACE("%p - %p %d\n", src, dst, samples);
329 while (samples--)
331 *dst = f_to_32(*src);
332 ++dst;
333 ++src;
337 const normfunc normfunctions[4] = {
338 (normfunc)norm8,
339 (normfunc)norm16,
340 (normfunc)norm24,
341 (normfunc)norm32,