3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 /* Linux does not support better timing than 10ms */
23 #define DS_TIME_RES 2 /* Resolution of multimedia timer */
24 #define DS_TIME_DEL 10 /* Delay of multimedia timer callback, and duration of HEL fragment */
25 /* Default refresh count, can be overridden */
26 #define FAKE_REFRESH_COUNT (1000/DS_TIME_DEL/2)
32 #include <OpenAL/al.h>
33 #include <OpenAL/alc.h>
50 #define TRACE(fmt,args...) do { \
52 fprintf(stderr, "%04x:trace:dsound:%s " fmt, (UINT)GetCurrentThreadId(), __FUNCTION__, ##args); \
54 #define WARN(fmt,args...) do { \
56 fprintf(stderr, "%04x:warn:dsound:%s " fmt, (UINT)GetCurrentThreadId(), __FUNCTION__, ##args); \
58 #define FIXME(fmt,args...) do { \
60 fprintf(stderr, "%04x:fixme:dsound:%s " fmt, (UINT)GetCurrentThreadId(), __FUNCTION__, ##args); \
62 #define ERR(fmt,args...) do { \
64 fprintf(stderr, "%04x:err:dsound:%s " fmt, (UINT)GetCurrentThreadId(), __FUNCTION__, ##args); \
67 #define TRACE(args...)
69 #define FIXME(args...)
73 const char *wine_dbg_sprintf( const char *format
, ... );
74 const char *wine_dbgstr_wn( const WCHAR
*str
, int n
);
75 const char *debugstr_guid( const GUID
*id
);
77 static inline const char *debugstr_w( const WCHAR
*s
) { return wine_dbgstr_wn( s
, -1 ); }
86 /* Set to 1 to build a DLL that can be used in an app that uses OpenAL itself.
87 * Thread-local contexts are needed for true concurrency, however. Disallowing
88 * concurrency can avoid a save and restore of the context. */
89 #define ALLOW_CONCURRENT_AL 0
91 /* All openal functions */
92 extern int openal_loaded
;
93 #ifdef SONAME_LIBOPENAL
94 extern LPALCCREATECONTEXT palcCreateContext
;
95 extern LPALCMAKECONTEXTCURRENT palcMakeContextCurrent
;
96 extern LPALCPROCESSCONTEXT palcProcessContext
;
97 extern LPALCSUSPENDCONTEXT palcSuspendContext
;
98 extern LPALCDESTROYCONTEXT palcDestroyContext
;
99 extern LPALCGETCURRENTCONTEXT palcGetCurrentContext
;
100 extern LPALCGETCONTEXTSDEVICE palcGetContextsDevice
;
101 extern LPALCOPENDEVICE palcOpenDevice
;
102 extern LPALCCLOSEDEVICE palcCloseDevice
;
103 extern LPALCGETERROR palcGetError
;
104 extern LPALCISEXTENSIONPRESENT palcIsExtensionPresent
;
105 extern LPALCGETPROCADDRESS palcGetProcAddress
;
106 extern LPALCGETENUMVALUE palcGetEnumValue
;
107 extern LPALCGETSTRING palcGetString
;
108 extern LPALCGETINTEGERV palcGetIntegerv
;
109 extern LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice
;
110 extern LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice
;
111 extern LPALCCAPTURESTART palcCaptureStart
;
112 extern LPALCCAPTURESTOP palcCaptureStop
;
113 extern LPALCCAPTURESAMPLES palcCaptureSamples
;
114 extern LPALENABLE palEnable
;
115 extern LPALDISABLE palDisable
;
116 extern LPALISENABLED palIsEnabled
;
117 extern LPALGETSTRING palGetString
;
118 extern LPALGETBOOLEANV palGetBooleanv
;
119 extern LPALGETINTEGERV palGetIntegerv
;
120 extern LPALGETFLOATV palGetFloatv
;
121 extern LPALGETDOUBLEV palGetDoublev
;
122 extern LPALGETBOOLEAN palGetBoolean
;
123 extern LPALGETINTEGER palGetInteger
;
124 extern LPALGETFLOAT palGetFloat
;
125 extern LPALGETDOUBLE palGetDouble
;
126 extern LPALGETERROR palGetError
;
127 extern LPALISEXTENSIONPRESENT palIsExtensionPresent
;
128 extern LPALGETPROCADDRESS palGetProcAddress
;
129 extern LPALGETENUMVALUE palGetEnumValue
;
130 extern LPALLISTENERF palListenerf
;
131 extern LPALLISTENER3F palListener3f
;
132 extern LPALLISTENERFV palListenerfv
;
133 extern LPALLISTENERI palListeneri
;
134 extern LPALLISTENER3I palListener3i
;
135 extern LPALLISTENERIV palListeneriv
;
136 extern LPALGETLISTENERF palGetListenerf
;
137 extern LPALGETLISTENER3F palGetListener3f
;
138 extern LPALGETLISTENERFV palGetListenerfv
;
139 extern LPALGETLISTENERI palGetListeneri
;
140 extern LPALGETLISTENER3I palGetListener3i
;
141 extern LPALGETLISTENERIV palGetListeneriv
;
142 extern LPALGENSOURCES palGenSources
;
143 extern LPALDELETESOURCES palDeleteSources
;
144 extern LPALISSOURCE palIsSource
;
145 extern LPALSOURCEF palSourcef
;
146 extern LPALSOURCE3F palSource3f
;
147 extern LPALSOURCEFV palSourcefv
;
148 extern LPALSOURCEI palSourcei
;
149 extern LPALSOURCE3I palSource3i
;
150 extern LPALSOURCEIV palSourceiv
;
151 extern LPALGETSOURCEF palGetSourcef
;
152 extern LPALGETSOURCE3F palGetSource3f
;
153 extern LPALGETSOURCEFV palGetSourcefv
;
154 extern LPALGETSOURCEI palGetSourcei
;
155 extern LPALGETSOURCE3I palGetSource3i
;
156 extern LPALGETSOURCEIV palGetSourceiv
;
157 extern LPALSOURCEPLAYV palSourcePlayv
;
158 extern LPALSOURCESTOPV palSourceStopv
;
159 extern LPALSOURCEREWINDV palSourceRewindv
;
160 extern LPALSOURCEPAUSEV palSourcePausev
;
161 extern LPALSOURCEPLAY palSourcePlay
;
162 extern LPALSOURCESTOP palSourceStop
;
163 extern LPALSOURCEREWIND palSourceRewind
;
164 extern LPALSOURCEPAUSE palSourcePause
;
165 extern LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers
;
166 extern LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers
;
167 extern LPALGENBUFFERS palGenBuffers
;
168 extern LPALDELETEBUFFERS palDeleteBuffers
;
169 extern LPALISBUFFER palIsBuffer
;
170 extern LPALBUFFERF palBufferf
;
171 extern LPALBUFFER3F palBuffer3f
;
172 extern LPALBUFFERFV palBufferfv
;
173 extern LPALBUFFERI palBufferi
;
174 extern LPALBUFFER3I palBuffer3i
;
175 extern LPALBUFFERIV palBufferiv
;
176 extern LPALGETBUFFERF palGetBufferf
;
177 extern LPALGETBUFFER3F palGetBuffer3f
;
178 extern LPALGETBUFFERFV palGetBufferfv
;
179 extern LPALGETBUFFERI palGetBufferi
;
180 extern LPALGETBUFFER3I palGetBuffer3i
;
181 extern LPALGETBUFFERIV palGetBufferiv
;
182 extern LPALBUFFERDATA palBufferData
;
183 extern LPALDOPPLERFACTOR palDopplerFactor
;
184 extern LPALDOPPLERVELOCITY palDopplerVelocity
;
185 extern LPALDISTANCEMODEL palDistanceModel
;
186 extern LPALSPEEDOFSOUND palSpeedOfSound
;
188 #define alcCreateContext palcCreateContext
189 #define alcMakeContextCurrent palcMakeContextCurrent
190 #define alcProcessContext palcProcessContext
191 #define alcSuspendContext palcSuspendContext
192 #define alcDestroyContext palcDestroyContext
193 #define alcGetCurrentContext palcGetCurrentContext
194 #define alcGetContextsDevice palcGetContextsDevice
195 #define alcOpenDevice palcOpenDevice
196 #define alcCloseDevice palcCloseDevice
197 #define alcGetError palcGetError
198 #define alcIsExtensionPresent palcIsExtensionPresent
199 #define alcGetProcAddress palcGetProcAddress
200 #define alcGetEnumValue palcGetEnumValue
201 #define alcGetString palcGetString
202 #define alcGetIntegerv palcGetIntegerv
203 #define alcCaptureOpenDevice palcCaptureOpenDevice
204 #define alcCaptureCloseDevice palcCaptureCloseDevice
205 #define alcCaptureStart palcCaptureStart
206 #define alcCaptureStop palcCaptureStop
207 #define alcCaptureSamples palcCaptureSamples
208 #define alEnable palEnable
209 #define alDisable palDisable
210 #define alIsEnabled palIsEnabled
211 #define alGetString palGetString
212 #define alGetBooleanv palGetBooleanv
213 #define alGetIntegerv palGetIntegerv
214 #define alGetFloatv palGetFloatv
215 #define alGetDoublev palGetDoublev
216 #define alGetBoolean palGetBoolean
217 #define alGetInteger palGetInteger
218 #define alGetFloat palGetFloat
219 #define alGetDouble palGetDouble
220 #define alGetError palGetError
221 #define alIsExtensionPresent palIsExtensionPresent
222 #define alGetProcAddress palGetProcAddress
223 #define alGetEnumValue palGetEnumValue
224 #define alListenerf palListenerf
225 #define alListener3f palListener3f
226 #define alListenerfv palListenerfv
227 #define alListeneri palListeneri
228 #define alListener3i palListener3i
229 #define alListeneriv palListeneriv
230 #define alGetListenerf palGetListenerf
231 #define alGetListener3f palGetListener3f
232 #define alGetListenerfv palGetListenerfv
233 #define alGetListeneri palGetListeneri
234 #define alGetListener3i palGetListener3i
235 #define alGetListeneriv palGetListeneriv
236 #define alGenSources palGenSources
237 #define alDeleteSources palDeleteSources
238 #define alIsSource palIsSource
239 #define alSourcef palSourcef
240 #define alSource3f palSource3f
241 #define alSourcefv palSourcefv
242 #define alSourcei palSourcei
243 #define alSource3i palSource3i
244 #define alSourceiv palSourceiv
245 #define alGetSourcef palGetSourcef
246 #define alGetSource3f palGetSource3f
247 #define alGetSourcefv palGetSourcefv
248 #define alGetSourcei palGetSourcei
249 #define alGetSource3i palGetSource3i
250 #define alGetSourceiv palGetSourceiv
251 #define alSourcePlayv palSourcePlayv
252 #define alSourceStopv palSourceStopv
253 #define alSourceRewindv palSourceRewindv
254 #define alSourcePausev palSourcePausev
255 #define alSourcePlay palSourcePlay
256 #define alSourceStop palSourceStop
257 #define alSourceRewind palSourceRewind
258 #define alSourcePause palSourcePause
259 #define alSourceQueueBuffers palSourceQueueBuffers
260 #define alSourceUnqueueBuffers palSourceUnqueueBuffers
261 #define alGenBuffers palGenBuffers
262 #define alDeleteBuffers palDeleteBuffers
263 #define alIsBuffer palIsBuffer
264 #define alBufferf palBufferf
265 #define alBuffer3f palBuffer3f
266 #define alBufferfv palBufferfv
267 #define alBufferi palBufferi
268 #define alBuffer3i palBuffer3i
269 #define alBufferiv palBufferiv
270 #define alGetBufferf palGetBufferf
271 #define alGetBuffer3f palGetBuffer3f
272 #define alGetBufferfv palGetBufferfv
273 #define alGetBufferi palGetBufferi
274 #define alGetBuffer3i palGetBuffer3i
275 #define alGetBufferiv palGetBufferiv
276 #define alBufferData palBufferData
277 #define alDopplerFactor palDopplerFactor
278 #define alDopplerVelocity palDopplerVelocity
279 #define alDistanceModel palDistanceModel
280 #define alSpeedOfSound palSpeedOfSound
287 /* OpenAL only allows for 1 single access to the device at the same time */
288 extern CRITICAL_SECTION openal_crst
;
290 extern const ALCchar
*DSOUND_getdevicestrings(void);
291 extern const ALCchar
*DSOUND_getcapturedevicestrings(void);
293 extern LPALCMAKECONTEXTCURRENT set_context
;
294 extern LPALCGETCURRENTCONTEXT get_context
;
295 extern BOOL local_contexts
;
298 typedef struct DS8Impl DS8Impl
;
299 typedef struct DS8Primary DS8Primary
;
300 typedef struct DS8Buffer DS8Buffer
;
304 #define AL_BYTE 0x1400
305 #define AL_UNSIGNED_BYTE 0x1401
306 #define AL_SHORT 0x1402
307 #define AL_UNSIGNED_SHORT 0x1403
308 #define AL_INT 0x1404
309 #define AL_UNSIGNED_INT 0x1405
310 #define AL_FLOAT 0x1406
311 #define AL_DOUBLE 0x1407
312 #define AL_BYTE3 0x1408
313 #define AL_UNSIGNED_BYTE3 0x1409
314 #define AL_MULAW 0x1410
315 #define AL_IMA4 0x1411
317 /* Channel configurations */
318 #define AL_MONO 0x1500
319 #define AL_STEREO 0x1501
320 #define AL_REAR 0x1502
321 #define AL_QUAD 0x1503
322 #define AL_5POINT1 0x1504 /* (WFX order) */
323 #define AL_6POINT1 0x1505 /* (WFX order) */
324 #define AL_7POINT1 0x1506 /* (WFX order) */
326 /* Storage formats */
327 #define AL_MONO8 0x1100
328 #define AL_MONO16 0x1101
329 #define AL_MONO32F 0x10010
330 #define AL_STEREO8 0x1102
331 #define AL_STEREO16 0x1103
332 #define AL_STEREO32F 0x10011
333 #define AL_QUAD8 0x1204
334 #define AL_QUAD16 0x1205
335 #define AL_QUAD32F 0x1206
336 #define AL_REAR8 0x1207
337 #define AL_REAR16 0x1208
338 #define AL_REAR32F 0x1209
339 #define AL_5POINT1_8 0x120A
340 #define AL_5POINT1_16 0x120B
341 #define AL_5POINT1_32F 0x120C
342 #define AL_6POINT1_8 0x120D
343 #define AL_6POINT1_16 0x120E
344 #define AL_6POINT1_32F 0x120F
345 #define AL_7POINT1_8 0x1210
346 #define AL_7POINT1_16 0x1211
347 #define AL_7POINT1_32F 0x1212
355 SOFT_BUFFER_SUB_DATA
,
356 SOFT_DEFERRED_UPDATES
,
361 typedef struct ExtALFuncs
{
362 PFNALBUFFERSUBDATASOFTPROC BufferSubData
;
363 PFNALBUFFERDATASTATICPROC BufferDataStatic
;
365 LPALGENEFFECTS GenEffects
;
366 LPALDELETEEFFECTS DeleteEffects
;
370 LPALGENAUXILIARYEFFECTSLOTS GenAuxiliaryEffectSlots
;
371 LPALDELETEAUXILIARYEFFECTSLOTS DeleteAuxiliaryEffectSlots
;
372 LPALAUXILIARYEFFECTSLOTI AuxiliaryEffectSloti
;
374 void (AL_APIENTRY
*BufferSamplesSOFT
)(ALuint
,ALuint
,ALenum
,ALsizei
,ALenum
,ALenum
,const ALvoid
*);
375 void (AL_APIENTRY
*BufferSubSamplesSOFT
)(ALuint
,ALsizei
,ALsizei
,ALenum
,ALenum
,const ALvoid
*);
376 void (AL_APIENTRY
*GetBufferSamplesSOFT
)(ALuint
,ALsizei
,ALsizei
,ALenum
,ALenum
,ALvoid
*);
377 ALboolean (AL_APIENTRY
*IsBufferFormatSupportedSOFT
)(ALenum
);
379 void (AL_APIENTRY
*DeferUpdatesSOFT
)(void);
380 void (AL_APIENTRY
*ProcessUpdatesSOFT
)(void);
383 #define MAX_SOURCES 256
384 typedef struct DeviceShare
{
390 ALboolean SupportedExt
[MAX_EXTENSIONS
];
394 CRITICAL_SECTION crst
;
396 ALuint sources
[MAX_SOURCES
];
397 DWORD nsources
, max_sources
;
407 IDirectSoundBuffer IDirectSoundBuffer_iface
;
408 IDirectSound3DListener IDirectSound3DListener_iface
;
409 IKsPropertySet IKsPropertySet_iface
;
411 LONG ref
, ds3d_ref
, prop_ref
;
412 IDirectSoundBuffer8
*write_emu
;
415 /* Taken from the share */
417 const ALboolean
*SupportedExt
;
418 const ExtALFuncs
*ExtAL
;
419 CRITICAL_SECTION
*crst
;
427 WAVEFORMATEXTENSIBLE format
;
435 DWORD nbuffers
, sizebuffers
;
436 DS8Buffer
**notifies
;
437 DWORD nnotifies
, sizenotifies
;
443 BOOL orientation
: 1;
444 BOOL distancefactor
: 1;
445 BOOL rollofffactor
: 1;
446 BOOL dopplerfactor
: 1;
451 ALfloat rollofffactor
;
454 EAXLISTENERPROPERTIES eax_prop
;
456 void (AL_APIENTRY
*DeferUpdates
)(void);
457 void (AL_APIENTRY
*ProcessUpdates
)(void);
461 /* Device implementation */
463 IDirectSound8 IDirectSound8_iface
;
464 IDirectSound IDirectSound_iface
;
471 /* Taken from the share */
476 DWORD speaker_config
;
481 typedef struct DS8Data
{
484 /* Lock was called and unlock isn't? */
487 WAVEFORMATEXTENSIBLE format
;
491 ALenum in_type
, in_chans
;
499 /* Amount of buffers that have to be queued when
500 * bufferdatastatic and buffersubdata are not available */
504 IDirectSoundBuffer8 IDirectSoundBuffer8_iface
;
505 IDirectSoundBuffer IDirectSoundBuffer_iface
;
506 IDirectSound3DBuffer IDirectSound3DBuffer_iface
;
507 IDirectSoundNotify IDirectSoundNotify_iface
;
508 IKsPropertySet IKsPropertySet_iface
;
510 LONG ref
, ds3d_ref
, not_ref
, prop_ref
;
518 BOOL isplaying
, islooping
, bufferlost
;
520 const ExtALFuncs
*ExtAL
;
521 CRITICAL_SECTION
*crst
;
523 DS3DBUFFER ds3dbuffer
;
528 BOOL cone_angles
: 1;
529 BOOL cone_orient
: 1;
530 BOOL cone_outsidevolume
: 1;
531 BOOL min_distance
: 1;
532 BOOL max_distance
: 1;
538 DWORD nnotify
, lastpos
;
539 DSBPOSITIONNOTIFY
*notify
;
542 extern HRESULT
DS8Primary_PreInit(DS8Primary
*prim
, DS8Impl
*parent
);
543 extern void DS8Primary_Clear(DS8Primary
*prim
);
545 extern HRESULT
DS8Buffer_Create(DS8Buffer
**ppv
, DS8Primary
*parent
, IDirectSoundBuffer
*orig
);
546 extern void DS8Buffer_Destroy(DS8Buffer
*buf
);
548 static inline ALdouble
gain_to_mB(ALdouble gain
)
550 return log10(gain
) * 2000.0;
552 static inline ALdouble
mB_to_gain(ALdouble millibels
)
554 return pow(10.0, millibels
/2000.0);
557 static inline LONG
clampI(LONG val
, LONG minval
, LONG maxval
)
559 if(val
>= maxval
) return maxval
;
560 if(val
<= minval
) return minval
;
563 static inline ULONG
clampU(ULONG val
, ULONG minval
, ULONG maxval
)
565 if(val
>= maxval
) return maxval
;
566 if(val
<= minval
) return minval
;
569 static inline FLOAT
clampF(FLOAT val
, FLOAT minval
, FLOAT maxval
)
571 if(val
>= maxval
) return maxval
;
572 if(val
<= minval
) return minval
;
577 #define checkALError() do { \
578 ALenum err = alGetError(); \
579 if(err != AL_NO_ERROR) \
580 ERR(">>>>>>>>>>>> Received AL error %#x on context %p, %s:%u\n", \
581 err, get_context(), __FUNCTION__, __LINE__); \
584 #define checkALCError(dev) do { \
585 ALenum err = alcGetError(dev); \
586 if(err != ALC_NO_ERROR) \
587 ERR(">>>>>>>>>>>> Received ALC error %#x on device %p, %s:%u\n", \
588 err, dev, __FUNCTION__, __LINE__); \
591 #if ALLOW_CONCURRENT_AL
593 #define setALContext(actx) do { \
594 ALCcontext *__old_ctx, *cur_ctx = actx; \
595 if(!local_contexts) EnterCriticalSection(&openal_crst); \
596 __old_ctx = get_context(); \
597 if(__old_ctx != cur_ctx && set_context(cur_ctx) == ALC_FALSE) { \
598 ERR("Couldn't set current context!!\n"); \
599 checkALCError(alcGetContextsDevice(cur_ctx)); \
601 /* Only restore a NULL context if using global contexts, for TLS contexts always restore */
602 #define popALContext() \
603 if(__old_ctx != cur_ctx && (local_contexts || __old_ctx) && \
604 set_context(__old_ctx) == ALC_FALSE) { \
605 ERR("Couldn't restore old context!!\n"); \
606 checkALCError(alcGetContextsDevice(__old_ctx)); \
608 if (!local_contexts) LeaveCriticalSection(&openal_crst); \
613 #define setALContext(actx) do { \
614 ALCcontext *cur_ctx = actx; \
615 if(!local_contexts) EnterCriticalSection(&openal_crst); \
616 if(set_context(cur_ctx) == ALC_FALSE) { \
617 ERR("Couldn't set current context!!\n"); \
618 checkALCError(alcGetContextsDevice(cur_ctx)); \
620 #define popALContext() \
621 if (!local_contexts) LeaveCriticalSection(&openal_crst); \
626 HRESULT
DSOUND_Create(REFIID riid
, void **ppDS
);
627 HRESULT
DSOUND_Create8(REFIID riid
, void **ppDS
);
628 HRESULT
DSOUND_FullDuplexCreate(REFIID riid
, void **ppDSFD
);
629 HRESULT
IKsPrivatePropertySetImpl_Create(REFIID riid
, void **piks
);
630 HRESULT
DSOUND_CaptureCreate(REFIID riid
, void **ppDSC
);
631 HRESULT
DSOUND_CaptureCreate8(REFIID riid
, void **ppDSC
);
633 extern const GUID DSOUND_renderer_guid
;
634 extern const GUID DSOUND_capture_guid
;