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 "wine/port.h"
47 #include "wine/debug.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))
61 static float get8(const IDirectSoundBufferImpl
*dsb
, DWORD pos
, DWORD channel
)
63 const BYTE
* buf
= dsb
->buffer
->memory
;
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
)
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 */
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
;
111 /* XXX: does Windows include LFE into the mix? */
112 for (c
= 0; c
< channels
; c
++)
113 val
+= dsb
->get_aux(dsb
, pos
, c
);
118 static inline unsigned char f_to_8(float value
)
122 if(value
>= 1.f
* 0x7f / 0x80)
124 return lrintf((value
+ 1.f
) * 0x80);
127 static inline SHORT
f_to_16(float value
)
131 if(value
>= 1.f
* 0x7FFF / 0x8000)
133 return le16(lrintf(value
* 0x8000));
136 static LONG
f_to_24(float value
)
140 if(value
>= 1.f
* 0x7FFFFF / 0x800000)
142 return lrintf(value
* 0x80000000U
);
145 static inline LONG
f_to_32(float value
)
149 if(value
>= 1.f
* 0x7FFFFFFF / 0x80000000U
) /* this rounds to 1.f */
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
);
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
);
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 */
222 case 4: /* back left */
223 value
*= 0.056f
; /* (1/9) / (sum of left volumes) */
224 dsb
->put_aux(dsb
, pos
, 0, value
);
227 case 0: /* front left */
228 value
*= 0.503f
; /* 1 / (sum of left volumes) */
229 dsb
->put_aux(dsb
, pos
, 0, value
);
232 case 5: /* back right */
233 value
*= 0.056f
; /* (1/9) / (sum of right volumes) */
234 dsb
->put_aux(dsb
, pos
, 1, value
);
237 case 1: /* front right */
238 value
*= 0.503f
; /* 1 / (sum of right volumes) */
239 dsb
->put_aux(dsb
, pos
, 1, value
);
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
);
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
);
256 void put_quad2stereo(const IDirectSoundBufferImpl
*dsb
, DWORD pos
, DWORD channel
, float value
)
258 /* based on pulseaudio's downmix algorithm */
261 case 2: /* back left */
262 value
*= 0.1f
; /* (1/9) / (sum of left volumes) */
263 dsb
->put_aux(dsb
, pos
, 0, value
);
266 case 0: /* front left */
267 value
*= 0.9f
; /* 1 / (sum of left volumes) */
268 dsb
->put_aux(dsb
, pos
, 0, value
);
271 case 3: /* back right */
272 value
*= 0.1f
; /* (1/9) / (sum of right volumes) */
273 dsb
->put_aux(dsb
, pos
, 1, value
);
276 case 1: /* front right */
277 value
*= 0.9f
; /* 1 / (sum of right volumes) */
278 dsb
->put_aux(dsb
, pos
, 1, value
);
283 void mixieee32(float *src
, float *dst
, unsigned samples
)
285 TRACE("%p - %p %d\n", src
, dst
, samples
);
287 *(dst
++) += *(src
++);
290 static void norm8(float *src
, unsigned char *dst
, unsigned samples
)
292 TRACE("%p - %p %d\n", src
, dst
, samples
);
301 static void norm16(float *src
, SHORT
*dst
, unsigned samples
)
303 TRACE("%p - %p %d\n", src
, dst
, samples
);
306 *dst
= f_to_16(*src
);
312 static void norm24(float *src
, BYTE
*dst
, unsigned samples
)
314 TRACE("%p - %p %d\n", src
, dst
, samples
);
317 LONG t
= f_to_24(*src
);
318 dst
[0] = (t
>> 8) & 0xFF;
319 dst
[1] = (t
>> 16) & 0xFF;
326 static void norm32(float *src
, INT
*dst
, unsigned samples
)
328 TRACE("%p - %p %d\n", src
, dst
, samples
);
331 *dst
= f_to_32(*src
);
337 const normfunc normfunctions
[4] = {