wininet: Use return value of sprintf() instead of calling strlen() and simplify code.
[wine.git] / dlls / dsound / dsound_convert.c
blob0c9a34baf9c683fd6469fe1a1a30cdc1e04401fb
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"
39 #include <stdarg.h>
40 #include <math.h>
42 #include "windef.h"
43 #include "winbase.h"
44 #include "mmsystem.h"
45 #include "winternl.h"
46 #include "wine/debug.h"
47 #include "dsound.h"
48 #include "dsound_private.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
52 #ifdef WORDS_BIGENDIAN
53 #define le16(x) RtlUshortByteSwap((x))
54 #define le32(x) RtlUlongByteSwap((x))
55 #else
56 #define le16(x) (x)
57 #define le32(x) (x)
58 #endif
60 static float get8(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
62 const BYTE* buf = dsb->buffer->memory;
63 buf += pos + channel;
64 return (buf[0] - 0x80) / (float)0x80;
67 static float get16(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
69 const BYTE* buf = dsb->buffer->memory;
70 const SHORT *sbuf = (const SHORT*)(buf + pos + 2 * channel);
71 SHORT sample = (SHORT)le16(*sbuf);
72 return sample / (float)0x8000;
75 static float get24(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
77 LONG sample;
78 const BYTE* buf = dsb->buffer->memory;
79 buf += pos + 3 * channel;
80 /* The next expression deliberately has an overflow for buf[2] >= 0x80,
81 this is how negative values are made.
83 sample = (buf[0] << 8) | (buf[1] << 16) | (buf[2] << 24);
84 return sample / (float)0x80000000U;
87 static float get32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
89 const BYTE* buf = dsb->buffer->memory;
90 const LONG *sbuf = (const LONG*)(buf + pos + 4 * channel);
91 LONG sample = le32(*sbuf);
92 return sample / (float)0x80000000U;
95 static float getieee32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
97 const BYTE* buf = dsb->buffer->memory;
98 const float *sbuf = (const float*)(buf + pos + 4 * channel);
99 /* The value will be clipped later, when put into some non-float buffer */
100 return *sbuf;
103 const bitsgetfunc getbpp[5] = {get8, get16, get24, get32, getieee32};
105 float get_mono(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel)
107 DWORD channels = dsb->pwfx->nChannels;
108 DWORD c;
109 float val = 0;
110 /* XXX: does Windows include LFE into the mix? */
111 for (c = 0; c < channels; c++)
112 val += dsb->get_aux(dsb, pos, c);
113 val /= channels;
114 return val;
117 static inline unsigned char f_to_8(float value)
119 if(value <= -1.f)
120 return 0;
121 if(value >= 1.f * 0x7f / 0x80)
122 return 0xFF;
123 return lrintf((value + 1.f) * 0x80);
126 static inline SHORT f_to_16(float value)
128 if(value <= -1.f)
129 return 0x8000;
130 if(value >= 1.f * 0x7FFF / 0x8000)
131 return 0x7FFF;
132 return le16(lrintf(value * 0x8000));
135 static LONG f_to_24(float value)
137 if(value <= -1.f)
138 return 0x80000000;
139 if(value >= 1.f * 0x7FFFFF / 0x800000)
140 return 0x7FFFFF00;
141 return lrintf(value * 0x80000000U);
144 static inline LONG f_to_32(float value)
146 if(value <= -1.f)
147 return 0x80000000;
148 if(value >= 1.f * 0x7FFFFFFF / 0x80000000U) /* this rounds to 1.f */
149 return 0x7FFFFFFF;
150 return le32(lrintf(value * 0x80000000U));
153 void putieee32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
155 BYTE *buf = (BYTE *)dsb->device->tmp_buffer;
156 float *fbuf = (float*)(buf + pos + sizeof(float) * channel);
157 *fbuf = value;
160 void putieee32_sum(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
162 BYTE *buf = (BYTE *)dsb->device->tmp_buffer;
163 float *fbuf = (float*)(buf + pos + sizeof(float) * channel);
164 *fbuf += value;
167 void put_mono2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
169 dsb->put_aux(dsb, pos, 0, value);
170 dsb->put_aux(dsb, pos, 1, value);
173 void put_mono2quad(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
175 dsb->put_aux(dsb, pos, 0, value);
176 dsb->put_aux(dsb, pos, 1, value);
177 dsb->put_aux(dsb, pos, 2, value);
178 dsb->put_aux(dsb, pos, 3, value);
181 void put_stereo2quad(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
183 if (channel == 0) { /* Left */
184 dsb->put_aux(dsb, pos, 0, value); /* Front left */
185 dsb->put_aux(dsb, pos, 2, value); /* Back left */
186 } else if (channel == 1) { /* Right */
187 dsb->put_aux(dsb, pos, 1, value); /* Front right */
188 dsb->put_aux(dsb, pos, 3, value); /* Back right */
192 void put_mono2surround51(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
194 dsb->put_aux(dsb, pos, 0, value);
195 dsb->put_aux(dsb, pos, 1, value);
196 dsb->put_aux(dsb, pos, 2, value);
197 dsb->put_aux(dsb, pos, 3, value);
198 dsb->put_aux(dsb, pos, 4, value);
199 dsb->put_aux(dsb, pos, 5, value);
202 void put_stereo2surround51(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
204 if (channel == 0) { /* Left */
205 dsb->put_aux(dsb, pos, 0, value); /* Front left */
206 dsb->put_aux(dsb, pos, 4, value); /* Back left */
208 dsb->put_aux(dsb, pos, 2, 0.0f); /* Mute front centre */
209 dsb->put_aux(dsb, pos, 3, 0.0f); /* Mute LFE */
210 } else if (channel == 1) { /* Right */
211 dsb->put_aux(dsb, pos, 1, value); /* Front right */
212 dsb->put_aux(dsb, pos, 5, value); /* Back right */
216 void put_surround512stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
218 /* based on pulseaudio's downmix algorithm */
219 switch(channel){
221 case 4: /* back left */
222 value *= 0.056f; /* (1/9) / (sum of left volumes) */
223 dsb->put_aux(dsb, pos, 0, value);
224 break;
226 case 0: /* front left */
227 value *= 0.503f; /* 1 / (sum of left volumes) */
228 dsb->put_aux(dsb, pos, 0, value);
229 break;
231 case 5: /* back right */
232 value *= 0.056f; /* (1/9) / (sum of right volumes) */
233 dsb->put_aux(dsb, pos, 1, value);
234 break;
236 case 1: /* front right */
237 value *= 0.503f; /* 1 / (sum of right volumes) */
238 dsb->put_aux(dsb, pos, 1, value);
239 break;
241 case 2: /* front centre */
242 value *= 0.252f; /* 0.5 / (sum of left/right volumes) */
243 dsb->put_aux(dsb, pos, 0, value);
244 dsb->put_aux(dsb, pos, 1, value);
245 break;
247 case 3: /* LFE */
248 value *= 0.189f; /* 0.375 / (sum of left/right volumes) */
249 dsb->put_aux(dsb, pos, 0, value);
250 dsb->put_aux(dsb, pos, 1, value);
251 break;
255 void put_quad2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value)
257 /* based on pulseaudio's downmix algorithm */
258 switch(channel){
260 case 2: /* back left */
261 value *= 0.1f; /* (1/9) / (sum of left volumes) */
262 dsb->put_aux(dsb, pos, 0, value);
263 break;
265 case 0: /* front left */
266 value *= 0.9f; /* 1 / (sum of left volumes) */
267 dsb->put_aux(dsb, pos, 0, value);
268 break;
270 case 3: /* back right */
271 value *= 0.1f; /* (1/9) / (sum of right volumes) */
272 dsb->put_aux(dsb, pos, 1, value);
273 break;
275 case 1: /* front right */
276 value *= 0.9f; /* 1 / (sum of right volumes) */
277 dsb->put_aux(dsb, pos, 1, value);
278 break;
282 void mixieee32(float *src, float *dst, unsigned samples)
284 TRACE("%p - %p %d\n", src, dst, samples);
285 while (samples--)
286 *(dst++) += *(src++);
289 static void norm8(float *src, unsigned char *dst, unsigned len)
291 TRACE("%p - %p %d\n", src, dst, len);
292 while (len--)
294 *dst = f_to_8(*src);
295 ++dst;
296 ++src;
300 static void norm16(float *src, SHORT *dst, unsigned len)
302 TRACE("%p - %p %d\n", src, dst, len);
303 len /= 2;
304 while (len--)
306 *dst = f_to_16(*src);
307 ++dst;
308 ++src;
312 static void norm24(float *src, BYTE *dst, unsigned len)
314 TRACE("%p - %p %d\n", src, dst, len);
315 len /= 3;
316 while (len--)
318 LONG t = f_to_24(*src);
319 dst[0] = (t >> 8) & 0xFF;
320 dst[1] = (t >> 16) & 0xFF;
321 dst[2] = t >> 24;
322 dst += 3;
323 ++src;
327 static void norm32(float *src, INT *dst, unsigned len)
329 TRACE("%p - %p %d\n", src, dst, len);
330 len /= 4;
331 while (len--)
333 *dst = f_to_32(*src);
334 ++dst;
335 ++src;
339 const normfunc normfunctions[4] = {
340 (normfunc)norm8,
341 (normfunc)norm16,
342 (normfunc)norm24,
343 (normfunc)norm32,