3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
6 * Copyright 2004 Robert Reif
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define NONAMELESSSTRUCT
28 #define NONAMELESSUNION
38 #include "wine/debug.h"
40 #include "dsound_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
44 typedef struct IDirectSoundImpl
{
45 IUnknown IUnknown_iface
; /* Separate refcount, not for COM aggregation */
46 IDirectSound8 IDirectSound8_iface
;
47 LONG ref
, refds
, numIfaces
;
48 DirectSoundDevice
*device
;
52 const char * dumpCooperativeLevel(DWORD level
)
54 #define LE(x) case x: return #x
59 LE(DSSCL_WRITEPRIMARY
);
62 return wine_dbg_sprintf("Unknown(%08x)", level
);
65 static void _dump_DSCAPS(DWORD xmask
) {
70 #define FE(x) { x, #x },
71 FE(DSCAPS_PRIMARYMONO
)
72 FE(DSCAPS_PRIMARYSTEREO
)
73 FE(DSCAPS_PRIMARY8BIT
)
74 FE(DSCAPS_PRIMARY16BIT
)
75 FE(DSCAPS_CONTINUOUSRATE
)
78 FE(DSCAPS_SECONDARYMONO
)
79 FE(DSCAPS_SECONDARYSTEREO
)
80 FE(DSCAPS_SECONDARY8BIT
)
81 FE(DSCAPS_SECONDARY16BIT
)
86 for (i
=0;i
<sizeof(flags
)/sizeof(flags
[0]);i
++)
87 if ((flags
[i
].mask
& xmask
) == flags
[i
].mask
)
88 TRACE("%s ",flags
[i
].name
);
91 static void _dump_DSBCAPS(DWORD xmask
) {
96 #define FE(x) { x, #x },
97 FE(DSBCAPS_PRIMARYBUFFER
)
99 FE(DSBCAPS_LOCHARDWARE
)
100 FE(DSBCAPS_LOCSOFTWARE
)
102 FE(DSBCAPS_CTRLFREQUENCY
)
104 FE(DSBCAPS_CTRLVOLUME
)
105 FE(DSBCAPS_CTRLPOSITIONNOTIFY
)
106 FE(DSBCAPS_STICKYFOCUS
)
107 FE(DSBCAPS_GLOBALFOCUS
)
108 FE(DSBCAPS_GETCURRENTPOSITION2
)
109 FE(DSBCAPS_MUTE3DATMAXDISTANCE
)
114 for (i
=0;i
<sizeof(flags
)/sizeof(flags
[0]);i
++)
115 if ((flags
[i
].mask
& xmask
) == flags
[i
].mask
)
116 TRACE("%s ",flags
[i
].name
);
119 static void directsound_destroy(IDirectSoundImpl
*This
)
122 DirectSoundDevice_Release(This
->device
);
123 HeapFree(GetProcessHeap(),0,This
);
124 TRACE("(%p) released\n", This
);
127 /*******************************************************************************
128 * IUnknown Implementation for DirectSound
130 static inline IDirectSoundImpl
*impl_from_IUnknown(IUnknown
*iface
)
132 return CONTAINING_RECORD(iface
, IDirectSoundImpl
, IUnknown_iface
);
135 static HRESULT WINAPI
IUnknownImpl_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
137 IDirectSoundImpl
*This
= impl_from_IUnknown(iface
);
139 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppv
);
142 WARN("invalid parameter\n");
147 if (IsEqualIID(riid
, &IID_IUnknown
))
148 *ppv
= &This
->IUnknown_iface
;
149 else if (IsEqualIID(riid
, &IID_IDirectSound
) ||
150 (IsEqualIID(riid
, &IID_IDirectSound8
) && This
->has_ds8
))
151 *ppv
= &This
->IDirectSound8_iface
;
153 WARN("unknown IID %s\n", debugstr_guid(riid
));
154 return E_NOINTERFACE
;
157 IUnknown_AddRef((IUnknown
*)*ppv
);
161 static ULONG WINAPI
IUnknownImpl_AddRef(IUnknown
*iface
)
163 IDirectSoundImpl
*This
= impl_from_IUnknown(iface
);
164 ULONG ref
= InterlockedIncrement(&This
->ref
);
166 TRACE("(%p) ref=%d\n", This
, ref
);
169 InterlockedIncrement(&This
->numIfaces
);
174 static ULONG WINAPI
IUnknownImpl_Release(IUnknown
*iface
)
176 IDirectSoundImpl
*This
= impl_from_IUnknown(iface
);
177 ULONG ref
= InterlockedDecrement(&This
->ref
);
179 TRACE("(%p) ref=%d\n", This
, ref
);
181 if (!ref
&& !InterlockedDecrement(&This
->numIfaces
))
182 directsound_destroy(This
);
187 static const IUnknownVtbl unk_vtbl
=
189 IUnknownImpl_QueryInterface
,
194 /*******************************************************************************
195 * IDirectSound and IDirectSound8 Implementation
197 static inline IDirectSoundImpl
*impl_from_IDirectSound8(IDirectSound8
*iface
)
199 return CONTAINING_RECORD(iface
, IDirectSoundImpl
, IDirectSound8_iface
);
202 static HRESULT WINAPI
IDirectSound8Impl_QueryInterface(IDirectSound8
*iface
, REFIID riid
,
205 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
206 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppv
);
207 return IUnknown_QueryInterface(&This
->IUnknown_iface
, riid
, ppv
);
210 static ULONG WINAPI
IDirectSound8Impl_AddRef(IDirectSound8
*iface
)
212 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
213 ULONG ref
= InterlockedIncrement(&This
->refds
);
215 TRACE("(%p) refds=%d\n", This
, ref
);
218 InterlockedIncrement(&This
->numIfaces
);
223 static ULONG WINAPI
IDirectSound8Impl_Release(IDirectSound8
*iface
)
225 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
226 ULONG ref
= InterlockedDecrement(&(This
->refds
));
228 TRACE("(%p) refds=%d\n", This
, ref
);
230 if (!ref
&& !InterlockedDecrement(&This
->numIfaces
))
231 directsound_destroy(This
);
236 static HRESULT WINAPI
IDirectSound8Impl_CreateSoundBuffer(IDirectSound8
*iface
,
237 const DSBUFFERDESC
*dsbd
, IDirectSoundBuffer
**ppdsb
, IUnknown
*lpunk
)
239 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
240 TRACE("(%p,%p,%p,%p)\n", This
, dsbd
, ppdsb
, lpunk
);
241 return DirectSoundDevice_CreateSoundBuffer(This
->device
, dsbd
, ppdsb
, lpunk
, This
->has_ds8
);
244 static HRESULT WINAPI
IDirectSound8Impl_GetCaps(IDirectSound8
*iface
, DSCAPS
*lpDSCaps
)
246 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
247 TRACE("(%p,%p)\n", This
, lpDSCaps
);
248 return DirectSoundDevice_GetCaps(This
->device
, lpDSCaps
);
251 static HRESULT WINAPI
IDirectSound8Impl_DuplicateSoundBuffer(IDirectSound8
*iface
,
252 IDirectSoundBuffer
*psb
, IDirectSoundBuffer
**ppdsb
)
254 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
255 TRACE("(%p,%p,%p)\n", This
, psb
, ppdsb
);
256 return DirectSoundDevice_DuplicateSoundBuffer(This
->device
, psb
, ppdsb
);
259 static HRESULT WINAPI
IDirectSound8Impl_SetCooperativeLevel(IDirectSound8
*iface
, HWND hwnd
,
262 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
263 TRACE("(%p,%p,%s)\n", This
, hwnd
, dumpCooperativeLevel(level
));
264 return DirectSoundDevice_SetCooperativeLevel(This
->device
, hwnd
, level
);
267 static HRESULT WINAPI
IDirectSound8Impl_Compact(IDirectSound8
*iface
)
269 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
270 TRACE("(%p)\n", This
);
271 return DirectSoundDevice_Compact(This
->device
);
274 static HRESULT WINAPI
IDirectSound8Impl_GetSpeakerConfig(IDirectSound8
*iface
,
275 DWORD
*lpdwSpeakerConfig
)
277 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
278 TRACE("(%p, %p)\n", This
, lpdwSpeakerConfig
);
279 return DirectSoundDevice_GetSpeakerConfig(This
->device
, lpdwSpeakerConfig
);
282 static HRESULT WINAPI
IDirectSound8Impl_SetSpeakerConfig(IDirectSound8
*iface
, DWORD config
)
284 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
285 TRACE("(%p,0x%08x)\n", This
, config
);
286 return DirectSoundDevice_SetSpeakerConfig(This
->device
, config
);
289 static HRESULT WINAPI
IDirectSound8Impl_Initialize(IDirectSound8
*iface
, const GUID
*lpcGuid
)
291 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
292 TRACE("(%p, %s)\n", This
, debugstr_guid(lpcGuid
));
293 return DirectSoundDevice_Initialize(&This
->device
, lpcGuid
);
296 static HRESULT WINAPI
IDirectSound8Impl_VerifyCertification(IDirectSound8
*iface
,
299 IDirectSoundImpl
*This
= impl_from_IDirectSound8(iface
);
300 TRACE("(%p, %p)\n", This
, pdwCertified
);
301 return DirectSoundDevice_VerifyCertification(This
->device
, pdwCertified
);
304 static const IDirectSound8Vtbl ds8_vtbl
=
306 IDirectSound8Impl_QueryInterface
,
307 IDirectSound8Impl_AddRef
,
308 IDirectSound8Impl_Release
,
309 IDirectSound8Impl_CreateSoundBuffer
,
310 IDirectSound8Impl_GetCaps
,
311 IDirectSound8Impl_DuplicateSoundBuffer
,
312 IDirectSound8Impl_SetCooperativeLevel
,
313 IDirectSound8Impl_Compact
,
314 IDirectSound8Impl_GetSpeakerConfig
,
315 IDirectSound8Impl_SetSpeakerConfig
,
316 IDirectSound8Impl_Initialize
,
317 IDirectSound8Impl_VerifyCertification
320 static HRESULT
IDirectSoundImpl_Create(REFIID riid
, void **ppv
, BOOL has_ds8
)
322 IDirectSoundImpl
*obj
;
325 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
328 obj
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*obj
));
330 WARN("out of memory\n");
331 return DSERR_OUTOFMEMORY
;
334 setup_dsound_options();
336 obj
->IUnknown_iface
.lpVtbl
= &unk_vtbl
;
337 obj
->IDirectSound8_iface
.lpVtbl
= &ds8_vtbl
;
342 obj
->has_ds8
= has_ds8
;
344 hr
= IUnknown_QueryInterface(&obj
->IUnknown_iface
, riid
, ppv
);
345 IUnknown_Release(&obj
->IUnknown_iface
);
350 HRESULT
DSOUND_Create(REFIID riid
, void **ppv
)
352 return IDirectSoundImpl_Create(riid
, ppv
, FALSE
);
355 HRESULT
DSOUND_Create8(REFIID riid
, void **ppv
)
357 return IDirectSoundImpl_Create(riid
, ppv
, TRUE
);
360 /*******************************************************************************
361 * DirectSoundCreate (DSOUND.1)
363 * Creates and initializes a DirectSound interface.
366 * lpcGUID [I] Address of the GUID that identifies the sound device.
367 * ppDS [O] Address of a variable to receive the interface pointer.
368 * pUnkOuter [I] Must be NULL.
372 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
373 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
375 HRESULT WINAPI
DirectSoundCreate(
383 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID
),ppDS
,pUnkOuter
);
386 WARN("invalid parameter: ppDS == NULL\n");
387 return DSERR_INVALIDPARAM
;
390 if (pUnkOuter
!= NULL
) {
391 WARN("invalid parameter: pUnkOuter != NULL\n");
393 return DSERR_INVALIDPARAM
;
396 hr
= DSOUND_Create(&IID_IDirectSound
, (void **)&pDS
);
398 hr
= IDirectSound_Initialize(pDS
, lpcGUID
);
400 if (hr
!= DSERR_ALREADYINITIALIZED
) {
401 IDirectSound_Release(pDS
);
413 /*******************************************************************************
414 * DirectSoundCreate8 (DSOUND.11)
416 * Creates and initializes a DirectSound8 interface.
419 * lpcGUID [I] Address of the GUID that identifies the sound device.
420 * ppDS [O] Address of a variable to receive the interface pointer.
421 * pUnkOuter [I] Must be NULL.
425 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
426 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
428 HRESULT WINAPI
DirectSoundCreate8(
430 LPDIRECTSOUND8
*ppDS
,
436 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID
),ppDS
,pUnkOuter
);
439 WARN("invalid parameter: ppDS == NULL\n");
440 return DSERR_INVALIDPARAM
;
443 if (pUnkOuter
!= NULL
) {
444 WARN("invalid parameter: pUnkOuter != NULL\n");
446 return DSERR_INVALIDPARAM
;
449 hr
= DSOUND_Create8(&IID_IDirectSound8
, (void **)&pDS
);
451 hr
= IDirectSound8_Initialize(pDS
, lpcGUID
);
453 if (hr
!= DSERR_ALREADYINITIALIZED
) {
454 IDirectSound8_Release(pDS
);
466 /*******************************************************************************
469 static HRESULT
DirectSoundDevice_Create(DirectSoundDevice
** ppDevice
)
471 DirectSoundDevice
* device
;
472 TRACE("(%p)\n", ppDevice
);
474 /* Allocate memory */
475 device
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DirectSoundDevice
));
476 if (device
== NULL
) {
477 WARN("out of memory\n");
478 return DSERR_OUTOFMEMORY
;
482 device
->priolevel
= DSSCL_NORMAL
;
483 device
->state
= STATE_STOPPED
;
484 device
->speaker_config
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, DSSPEAKER_GEOMETRY_WIDE
);
486 /* 3D listener initial parameters */
487 device
->ds3dl
.dwSize
= sizeof(DS3DLISTENER
);
488 device
->ds3dl
.vPosition
.x
= 0.0;
489 device
->ds3dl
.vPosition
.y
= 0.0;
490 device
->ds3dl
.vPosition
.z
= 0.0;
491 device
->ds3dl
.vVelocity
.x
= 0.0;
492 device
->ds3dl
.vVelocity
.y
= 0.0;
493 device
->ds3dl
.vVelocity
.z
= 0.0;
494 device
->ds3dl
.vOrientFront
.x
= 0.0;
495 device
->ds3dl
.vOrientFront
.y
= 0.0;
496 device
->ds3dl
.vOrientFront
.z
= 1.0;
497 device
->ds3dl
.vOrientTop
.x
= 0.0;
498 device
->ds3dl
.vOrientTop
.y
= 1.0;
499 device
->ds3dl
.vOrientTop
.z
= 0.0;
500 device
->ds3dl
.flDistanceFactor
= DS3D_DEFAULTDISTANCEFACTOR
;
501 device
->ds3dl
.flRolloffFactor
= DS3D_DEFAULTROLLOFFFACTOR
;
502 device
->ds3dl
.flDopplerFactor
= DS3D_DEFAULTDOPPLERFACTOR
;
504 device
->prebuf
= ds_snd_queue_max
;
505 device
->guid
= GUID_NULL
;
507 /* Set default wave format (may need it for waveOutOpen) */
508 device
->pwfx
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(WAVEFORMATEX
));
509 if (device
->pwfx
== NULL
) {
510 WARN("out of memory\n");
511 HeapFree(GetProcessHeap(),0,device
);
512 return DSERR_OUTOFMEMORY
;
515 device
->pwfx
->wFormatTag
= WAVE_FORMAT_PCM
;
516 device
->pwfx
->nSamplesPerSec
= ds_default_sample_rate
;
517 device
->pwfx
->wBitsPerSample
= ds_default_bits_per_sample
;
518 device
->pwfx
->nChannels
= 2;
519 device
->pwfx
->nBlockAlign
= device
->pwfx
->wBitsPerSample
* device
->pwfx
->nChannels
/ 8;
520 device
->pwfx
->nAvgBytesPerSec
= device
->pwfx
->nSamplesPerSec
* device
->pwfx
->nBlockAlign
;
521 device
->pwfx
->cbSize
= 0;
523 InitializeCriticalSection(&(device
->mixlock
));
524 device
->mixlock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": DirectSoundDevice.mixlock");
526 RtlInitializeResource(&(device
->buffer_list_lock
));
533 static ULONG
DirectSoundDevice_AddRef(DirectSoundDevice
* device
)
535 ULONG ref
= InterlockedIncrement(&(device
->ref
));
536 TRACE("(%p) ref was %d\n", device
, ref
- 1);
540 ULONG
DirectSoundDevice_Release(DirectSoundDevice
* device
)
543 ULONG ref
= InterlockedDecrement(&(device
->ref
));
544 TRACE("(%p) ref was %u\n", device
, ref
+ 1);
547 timeKillEvent(device
->timerID
);
548 timeEndPeriod(DS_TIME_RES
);
550 /* The kill event should have allowed the timer process to expire
551 * but try to grab the lock just in case. Can't hold lock because
552 * secondarybuffer_destroy also grabs the lock */
553 RtlAcquireResourceShared(&(device
->buffer_list_lock
), TRUE
);
554 RtlReleaseResource(&(device
->buffer_list_lock
));
556 EnterCriticalSection(&DSOUND_renderers_lock
);
557 list_remove(&device
->entry
);
558 LeaveCriticalSection(&DSOUND_renderers_lock
);
560 /* It is allowed to release this object even when buffers are playing */
561 if (device
->buffers
) {
562 WARN("%d secondary buffers not released\n", device
->nrofbuffers
);
563 for( i
=0;i
<device
->nrofbuffers
;i
++)
564 secondarybuffer_destroy(device
->buffers
[i
]);
567 hr
= DSOUND_PrimaryDestroy(device
);
569 WARN("DSOUND_PrimaryDestroy failed\n");
572 IAudioClient_Release(device
->client
);
574 IAudioRenderClient_Release(device
->render
);
576 IAudioClock_Release(device
->clock
);
578 IAudioStreamVolume_Release(device
->volume
);
580 HeapFree(GetProcessHeap(), 0, device
->tmp_buffer
);
581 HeapFree(GetProcessHeap(), 0, device
->mix_buffer
);
582 HeapFree(GetProcessHeap(), 0, device
->buffer
);
583 RtlDeleteResource(&device
->buffer_list_lock
);
584 device
->mixlock
.DebugInfo
->Spare
[0] = 0;
585 DeleteCriticalSection(&device
->mixlock
);
586 HeapFree(GetProcessHeap(),0,device
);
587 TRACE("(%p) released\n", device
);
592 HRESULT
DirectSoundDevice_GetCaps(
593 DirectSoundDevice
* device
,
596 TRACE("(%p,%p)\n",device
,lpDSCaps
);
598 if (device
== NULL
) {
599 WARN("not initialized\n");
600 return DSERR_UNINITIALIZED
;
603 if (lpDSCaps
== NULL
) {
604 WARN("invalid parameter: lpDSCaps = NULL\n");
605 return DSERR_INVALIDPARAM
;
608 /* check if there is enough room */
609 if (lpDSCaps
->dwSize
< sizeof(*lpDSCaps
)) {
610 WARN("invalid parameter: lpDSCaps->dwSize = %d\n", lpDSCaps
->dwSize
);
611 return DSERR_INVALIDPARAM
;
614 lpDSCaps
->dwFlags
= device
->drvcaps
.dwFlags
;
615 if (TRACE_ON(dsound
)) {
616 TRACE("(flags=0x%08x:\n",lpDSCaps
->dwFlags
);
617 _dump_DSCAPS(lpDSCaps
->dwFlags
);
620 lpDSCaps
->dwMinSecondarySampleRate
= device
->drvcaps
.dwMinSecondarySampleRate
;
621 lpDSCaps
->dwMaxSecondarySampleRate
= device
->drvcaps
.dwMaxSecondarySampleRate
;
622 lpDSCaps
->dwPrimaryBuffers
= device
->drvcaps
.dwPrimaryBuffers
;
623 lpDSCaps
->dwMaxHwMixingAllBuffers
= device
->drvcaps
.dwMaxHwMixingAllBuffers
;
624 lpDSCaps
->dwMaxHwMixingStaticBuffers
= device
->drvcaps
.dwMaxHwMixingStaticBuffers
;
625 lpDSCaps
->dwMaxHwMixingStreamingBuffers
= device
->drvcaps
.dwMaxHwMixingStreamingBuffers
;
626 lpDSCaps
->dwFreeHwMixingAllBuffers
= device
->drvcaps
.dwFreeHwMixingAllBuffers
;
627 lpDSCaps
->dwFreeHwMixingStaticBuffers
= device
->drvcaps
.dwFreeHwMixingStaticBuffers
;
628 lpDSCaps
->dwFreeHwMixingStreamingBuffers
= device
->drvcaps
.dwFreeHwMixingStreamingBuffers
;
629 lpDSCaps
->dwMaxHw3DAllBuffers
= device
->drvcaps
.dwMaxHw3DAllBuffers
;
630 lpDSCaps
->dwMaxHw3DStaticBuffers
= device
->drvcaps
.dwMaxHw3DStaticBuffers
;
631 lpDSCaps
->dwMaxHw3DStreamingBuffers
= device
->drvcaps
.dwMaxHw3DStreamingBuffers
;
632 lpDSCaps
->dwFreeHw3DAllBuffers
= device
->drvcaps
.dwFreeHw3DAllBuffers
;
633 lpDSCaps
->dwFreeHw3DStaticBuffers
= device
->drvcaps
.dwFreeHw3DStaticBuffers
;
634 lpDSCaps
->dwFreeHw3DStreamingBuffers
= device
->drvcaps
.dwFreeHw3DStreamingBuffers
;
635 lpDSCaps
->dwTotalHwMemBytes
= device
->drvcaps
.dwTotalHwMemBytes
;
636 lpDSCaps
->dwFreeHwMemBytes
= device
->drvcaps
.dwFreeHwMemBytes
;
637 lpDSCaps
->dwMaxContigFreeHwMemBytes
= device
->drvcaps
.dwMaxContigFreeHwMemBytes
;
638 lpDSCaps
->dwUnlockTransferRateHwBuffers
= device
->drvcaps
.dwUnlockTransferRateHwBuffers
;
639 lpDSCaps
->dwPlayCpuOverheadSwBuffers
= device
->drvcaps
.dwPlayCpuOverheadSwBuffers
;
644 BOOL
DSOUND_check_supported(IAudioClient
*client
, DWORD rate
,
645 DWORD depth
, WORD channels
)
647 WAVEFORMATEX fmt
, *junk
;
650 fmt
.wFormatTag
= WAVE_FORMAT_PCM
;
651 fmt
.nChannels
= channels
;
652 fmt
.nSamplesPerSec
= rate
;
653 fmt
.wBitsPerSample
= depth
;
654 fmt
.nBlockAlign
= (channels
* depth
) / 8;
655 fmt
.nAvgBytesPerSec
= rate
* fmt
.nBlockAlign
;
658 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
, &fmt
, &junk
);
665 UINT
DSOUND_create_timer(LPTIMECALLBACK cb
, DWORD_PTR user
)
667 UINT triggertime
= DS_TIME_DEL
, res
= DS_TIME_RES
, id
;
670 timeGetDevCaps(&time
, sizeof(TIMECAPS
));
671 TRACE("Minimum timer resolution: %u, max timer: %u\n", time
.wPeriodMin
, time
.wPeriodMax
);
672 if (triggertime
< time
.wPeriodMin
)
673 triggertime
= time
.wPeriodMin
;
674 if (res
< time
.wPeriodMin
)
675 res
= time
.wPeriodMin
;
676 if (timeBeginPeriod(res
) == TIMERR_NOCANDO
)
677 WARN("Could not set minimum resolution, don't expect sound\n");
678 id
= timeSetEvent(triggertime
, res
, cb
, user
, TIME_PERIODIC
| TIME_KILL_SYNCHRONOUS
);
681 WARN("Timer not created! Retrying without TIME_KILL_SYNCHRONOUS\n");
682 id
= timeSetEvent(triggertime
, res
, cb
, user
, TIME_PERIODIC
);
684 ERR("Could not create timer, sound playback will not occur\n");
689 HRESULT
DirectSoundDevice_Initialize(DirectSoundDevice
** ppDevice
, LPCGUID lpcGUID
)
693 DirectSoundDevice
*device
;
696 TRACE("(%p,%s)\n",ppDevice
,debugstr_guid(lpcGUID
));
698 if (*ppDevice
!= NULL
) {
699 WARN("already initialized\n");
700 return DSERR_ALREADYINITIALIZED
;
703 /* Default device? */
704 if (!lpcGUID
|| IsEqualGUID(lpcGUID
, &GUID_NULL
))
705 lpcGUID
= &DSDEVID_DefaultPlayback
;
707 if(IsEqualGUID(lpcGUID
, &DSDEVID_DefaultCapture
) ||
708 IsEqualGUID(lpcGUID
, &DSDEVID_DefaultVoiceCapture
))
709 return DSERR_NODRIVER
;
711 if (GetDeviceID(lpcGUID
, &devGUID
) != DS_OK
) {
712 WARN("invalid parameter: lpcGUID\n");
713 return DSERR_INVALIDPARAM
;
716 hr
= get_mmdevice(eRender
, &devGUID
, &mmdevice
);
720 EnterCriticalSection(&DSOUND_renderers_lock
);
722 LIST_FOR_EACH_ENTRY(device
, &DSOUND_renderers
, DirectSoundDevice
, entry
){
723 if(IsEqualGUID(&device
->guid
, &devGUID
)){
724 IMMDevice_Release(mmdevice
);
725 DirectSoundDevice_AddRef(device
);
727 LeaveCriticalSection(&DSOUND_renderers_lock
);
732 hr
= DirectSoundDevice_Create(&device
);
734 WARN("DirectSoundDevice_Create failed\n");
735 IMMDevice_Release(mmdevice
);
736 LeaveCriticalSection(&DSOUND_renderers_lock
);
740 device
->mmdevice
= mmdevice
;
741 device
->guid
= devGUID
;
743 hr
= DSOUND_ReopenDevice(device
, FALSE
);
746 HeapFree(GetProcessHeap(), 0, device
);
747 LeaveCriticalSection(&DSOUND_renderers_lock
);
748 IMMDevice_Release(mmdevice
);
749 WARN("DSOUND_ReopenDevice failed: %08x\n", hr
);
753 ZeroMemory(&device
->drvcaps
, sizeof(device
->drvcaps
));
755 if(DSOUND_check_supported(device
->client
, 11025, 8, 1) ||
756 DSOUND_check_supported(device
->client
, 22050, 8, 1) ||
757 DSOUND_check_supported(device
->client
, 44100, 8, 1) ||
758 DSOUND_check_supported(device
->client
, 48000, 8, 1) ||
759 DSOUND_check_supported(device
->client
, 96000, 8, 1))
760 device
->drvcaps
.dwFlags
|= DSCAPS_PRIMARY8BIT
| DSCAPS_PRIMARYMONO
;
762 if(DSOUND_check_supported(device
->client
, 11025, 16, 1) ||
763 DSOUND_check_supported(device
->client
, 22050, 16, 1) ||
764 DSOUND_check_supported(device
->client
, 44100, 16, 1) ||
765 DSOUND_check_supported(device
->client
, 48000, 16, 1) ||
766 DSOUND_check_supported(device
->client
, 96000, 16, 1))
767 device
->drvcaps
.dwFlags
|= DSCAPS_PRIMARY16BIT
| DSCAPS_PRIMARYMONO
;
769 if(DSOUND_check_supported(device
->client
, 11025, 8, 2) ||
770 DSOUND_check_supported(device
->client
, 22050, 8, 2) ||
771 DSOUND_check_supported(device
->client
, 44100, 8, 2) ||
772 DSOUND_check_supported(device
->client
, 48000, 8, 2) ||
773 DSOUND_check_supported(device
->client
, 96000, 8, 2))
774 device
->drvcaps
.dwFlags
|= DSCAPS_PRIMARY8BIT
| DSCAPS_PRIMARYSTEREO
;
776 if(DSOUND_check_supported(device
->client
, 11025, 16, 2) ||
777 DSOUND_check_supported(device
->client
, 22050, 16, 2) ||
778 DSOUND_check_supported(device
->client
, 44100, 16, 2) ||
779 DSOUND_check_supported(device
->client
, 48000, 16, 2) ||
780 DSOUND_check_supported(device
->client
, 96000, 16, 2))
781 device
->drvcaps
.dwFlags
|= DSCAPS_PRIMARY16BIT
| DSCAPS_PRIMARYSTEREO
;
783 /* the dsound mixer supports all of the following */
784 device
->drvcaps
.dwFlags
|= DSCAPS_SECONDARY8BIT
| DSCAPS_SECONDARY16BIT
;
785 device
->drvcaps
.dwFlags
|= DSCAPS_SECONDARYMONO
| DSCAPS_SECONDARYSTEREO
;
786 device
->drvcaps
.dwFlags
|= DSCAPS_CONTINUOUSRATE
;
788 device
->drvcaps
.dwPrimaryBuffers
= 1;
789 device
->drvcaps
.dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
790 device
->drvcaps
.dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
791 device
->drvcaps
.dwMaxHwMixingAllBuffers
= 1;
792 device
->drvcaps
.dwMaxHwMixingStaticBuffers
= 1;
793 device
->drvcaps
.dwMaxHwMixingStreamingBuffers
= 1;
795 ZeroMemory(&device
->volpan
, sizeof(device
->volpan
));
797 hr
= DSOUND_PrimaryCreate(device
);
799 device
->timerID
= DSOUND_create_timer(DSOUND_timer
, (DWORD_PTR
)device
);
801 WARN("DSOUND_PrimaryCreate failed: %08x\n", hr
);
804 list_add_tail(&DSOUND_renderers
, &device
->entry
);
806 LeaveCriticalSection(&DSOUND_renderers_lock
);
811 HRESULT
DirectSoundDevice_CreateSoundBuffer(
812 DirectSoundDevice
* device
,
813 LPCDSBUFFERDESC dsbd
,
814 LPLPDIRECTSOUNDBUFFER ppdsb
,
818 HRESULT hres
= DS_OK
;
819 TRACE("(%p,%p,%p,%p)\n",device
,dsbd
,ppdsb
,lpunk
);
821 if (device
== NULL
) {
822 WARN("not initialized\n");
823 return DSERR_UNINITIALIZED
;
827 WARN("invalid parameter: dsbd == NULL\n");
828 return DSERR_INVALIDPARAM
;
831 if (dsbd
->dwSize
!= sizeof(DSBUFFERDESC
) &&
832 dsbd
->dwSize
!= sizeof(DSBUFFERDESC1
)) {
833 WARN("invalid parameter: dsbd\n");
834 return DSERR_INVALIDPARAM
;
838 WARN("invalid parameter: ppdsb == NULL\n");
839 return DSERR_INVALIDPARAM
;
843 if (TRACE_ON(dsound
)) {
844 TRACE("(structsize=%d)\n",dsbd
->dwSize
);
845 TRACE("(flags=0x%08x:\n",dsbd
->dwFlags
);
846 _dump_DSBCAPS(dsbd
->dwFlags
);
848 TRACE("(bufferbytes=%d)\n",dsbd
->dwBufferBytes
);
849 TRACE("(lpwfxFormat=%p)\n",dsbd
->lpwfxFormat
);
852 if (dsbd
->dwFlags
& DSBCAPS_LOCHARDWARE
&&
853 !(dsbd
->dwFlags
& DSBCAPS_PRIMARYBUFFER
)) {
854 TRACE("LOCHARDWARE is not supported, returning E_NOTIMPL\n");
858 if (dsbd
->dwFlags
& DSBCAPS_PRIMARYBUFFER
) {
859 if (dsbd
->lpwfxFormat
!= NULL
) {
860 WARN("invalid parameter: dsbd->lpwfxFormat must be NULL for "
862 return DSERR_INVALIDPARAM
;
865 if (device
->primary
) {
866 WARN("Primary Buffer already created\n");
867 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER8
)(device
->primary
));
868 *ppdsb
= (LPDIRECTSOUNDBUFFER
)(device
->primary
);
870 hres
= primarybuffer_create(device
, &device
->primary
, dsbd
);
871 if (device
->primary
) {
872 *ppdsb
= (IDirectSoundBuffer
*)&device
->primary
->IDirectSoundBuffer8_iface
;
873 device
->primary
->dsbd
.dwFlags
&= ~(DSBCAPS_LOCHARDWARE
| DSBCAPS_LOCSOFTWARE
);
874 device
->primary
->dsbd
.dwFlags
|= DSBCAPS_LOCSOFTWARE
;
876 WARN("primarybuffer_create() failed\n");
879 IDirectSoundBufferImpl
* dsb
;
880 WAVEFORMATEXTENSIBLE
*pwfxe
;
882 if (dsbd
->lpwfxFormat
== NULL
) {
883 WARN("invalid parameter: dsbd->lpwfxFormat can't be NULL for "
884 "secondary buffer\n");
885 return DSERR_INVALIDPARAM
;
887 pwfxe
= (WAVEFORMATEXTENSIBLE
*)dsbd
->lpwfxFormat
;
889 if (pwfxe
->Format
.wBitsPerSample
!= 16 && pwfxe
->Format
.wBitsPerSample
!= 8 && pwfxe
->Format
.wFormatTag
!= WAVE_FORMAT_EXTENSIBLE
)
891 WARN("wBitsPerSample=%d needs a WAVEFORMATEXTENSIBLE\n", dsbd
->lpwfxFormat
->wBitsPerSample
);
892 return DSERR_CONTROLUNAVAIL
;
894 if (pwfxe
->Format
.wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
896 /* check if cbSize is at least 22 bytes */
897 if (pwfxe
->Format
.cbSize
< (sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
)))
899 WARN("Too small a cbSize %u\n", pwfxe
->Format
.cbSize
);
900 return DSERR_INVALIDPARAM
;
903 /* cbSize should be 22 bytes, with one possible exception */
904 if (pwfxe
->Format
.cbSize
> (sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
)) &&
905 !((IsEqualGUID(&pwfxe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
) || IsEqualGUID(&pwfxe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)) &&
906 pwfxe
->Format
.cbSize
== sizeof(WAVEFORMATEXTENSIBLE
)))
908 WARN("Too big a cbSize %u\n", pwfxe
->Format
.cbSize
);
909 return DSERR_CONTROLUNAVAIL
;
912 if ((!IsEqualGUID(&pwfxe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
)) && (!IsEqualGUID(&pwfxe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)))
914 if (!IsEqualGUID(&pwfxe
->SubFormat
, &GUID_NULL
))
915 FIXME("SubFormat %s not supported right now.\n", debugstr_guid(&pwfxe
->SubFormat
));
916 return DSERR_INVALIDPARAM
;
918 if (pwfxe
->Samples
.wValidBitsPerSample
> dsbd
->lpwfxFormat
->wBitsPerSample
)
920 WARN("Samples.wValidBitsPerSample(%d) > Format.wBitsPerSample (%d)\n", pwfxe
->Samples
.wValidBitsPerSample
, pwfxe
->Format
.wBitsPerSample
);
921 return DSERR_INVALIDPARAM
;
923 if (pwfxe
->Samples
.wValidBitsPerSample
&& pwfxe
->Samples
.wValidBitsPerSample
< dsbd
->lpwfxFormat
->wBitsPerSample
)
925 FIXME("Non-packed formats not supported right now: %d/%d\n", pwfxe
->Samples
.wValidBitsPerSample
, dsbd
->lpwfxFormat
->wBitsPerSample
);
926 return DSERR_CONTROLUNAVAIL
;
930 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
931 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
932 dsbd
->lpwfxFormat
->wFormatTag
, dsbd
->lpwfxFormat
->nChannels
,
933 dsbd
->lpwfxFormat
->nSamplesPerSec
,
934 dsbd
->lpwfxFormat
->nAvgBytesPerSec
,
935 dsbd
->lpwfxFormat
->nBlockAlign
,
936 dsbd
->lpwfxFormat
->wBitsPerSample
, dsbd
->lpwfxFormat
->cbSize
);
938 if (from8
&& (dsbd
->dwFlags
& DSBCAPS_CTRL3D
) && (dsbd
->lpwfxFormat
->nChannels
!= 1)) {
939 WARN("invalid parameter: 3D buffer format must be mono\n");
940 return DSERR_INVALIDPARAM
;
943 hres
= IDirectSoundBufferImpl_Create(device
, &dsb
, dsbd
);
945 *ppdsb
= (IDirectSoundBuffer
*)&dsb
->IDirectSoundBuffer8_iface
;
947 WARN("IDirectSoundBufferImpl_Create failed\n");
953 HRESULT
DirectSoundDevice_DuplicateSoundBuffer(
954 DirectSoundDevice
* device
,
955 LPDIRECTSOUNDBUFFER psb
,
956 LPLPDIRECTSOUNDBUFFER ppdsb
)
958 HRESULT hres
= DS_OK
;
959 IDirectSoundBufferImpl
* dsb
;
960 TRACE("(%p,%p,%p)\n",device
,psb
,ppdsb
);
962 if (device
== NULL
) {
963 WARN("not initialized\n");
964 return DSERR_UNINITIALIZED
;
968 WARN("invalid parameter: psb == NULL\n");
969 return DSERR_INVALIDPARAM
;
973 WARN("invalid parameter: ppdsb == NULL\n");
974 return DSERR_INVALIDPARAM
;
977 /* make sure we have a secondary buffer */
978 if (psb
== (IDirectSoundBuffer
*)&device
->primary
->IDirectSoundBuffer8_iface
) {
979 WARN("trying to duplicate primary buffer\n");
981 return DSERR_INVALIDCALL
;
984 /* duplicate the actual buffer implementation */
985 hres
= IDirectSoundBufferImpl_Duplicate(device
, &dsb
, (IDirectSoundBufferImpl
*)psb
);
987 *ppdsb
= (IDirectSoundBuffer
*)&dsb
->IDirectSoundBuffer8_iface
;
989 WARN("IDirectSoundBufferImpl_Duplicate failed\n");
994 HRESULT
DirectSoundDevice_SetCooperativeLevel(
995 DirectSoundDevice
* device
,
999 TRACE("(%p,%p,%s)\n",device
,hwnd
,dumpCooperativeLevel(level
));
1001 if (device
== NULL
) {
1002 WARN("not initialized\n");
1003 return DSERR_UNINITIALIZED
;
1006 if (level
==DSSCL_PRIORITY
|| level
==DSSCL_EXCLUSIVE
) {
1007 WARN("level=%s not fully supported\n",
1008 level
==DSSCL_PRIORITY
? "DSSCL_PRIORITY" : "DSSCL_EXCLUSIVE");
1011 device
->priolevel
= level
;
1015 HRESULT
DirectSoundDevice_Compact(
1016 DirectSoundDevice
* device
)
1018 TRACE("(%p)\n", device
);
1020 if (device
== NULL
) {
1021 WARN("not initialized\n");
1022 return DSERR_UNINITIALIZED
;
1025 if (device
->priolevel
< DSSCL_PRIORITY
) {
1026 WARN("incorrect priority level\n");
1027 return DSERR_PRIOLEVELNEEDED
;
1033 HRESULT
DirectSoundDevice_GetSpeakerConfig(
1034 DirectSoundDevice
* device
,
1035 LPDWORD lpdwSpeakerConfig
)
1037 TRACE("(%p, %p)\n", device
, lpdwSpeakerConfig
);
1039 if (device
== NULL
) {
1040 WARN("not initialized\n");
1041 return DSERR_UNINITIALIZED
;
1044 if (lpdwSpeakerConfig
== NULL
) {
1045 WARN("invalid parameter: lpdwSpeakerConfig == NULL\n");
1046 return DSERR_INVALIDPARAM
;
1049 WARN("not fully functional\n");
1050 *lpdwSpeakerConfig
= device
->speaker_config
;
1054 HRESULT
DirectSoundDevice_SetSpeakerConfig(
1055 DirectSoundDevice
* device
,
1058 TRACE("(%p,0x%08x)\n",device
,config
);
1060 if (device
== NULL
) {
1061 WARN("not initialized\n");
1062 return DSERR_UNINITIALIZED
;
1065 device
->speaker_config
= config
;
1066 WARN("not fully functional\n");
1070 HRESULT
DirectSoundDevice_VerifyCertification(
1071 DirectSoundDevice
* device
,
1072 LPDWORD pdwCertified
)
1074 TRACE("(%p, %p)\n",device
,pdwCertified
);
1076 if (device
== NULL
) {
1077 WARN("not initialized\n");
1078 return DSERR_UNINITIALIZED
;
1081 if (device
->drvcaps
.dwFlags
& DSCAPS_CERTIFIED
)
1082 *pdwCertified
= DS_CERTIFIED
;
1084 *pdwCertified
= DS_UNCERTIFIED
;
1090 * Add secondary buffer to buffer list.
1091 * Gets exclusive access to buffer for writing.
1093 HRESULT
DirectSoundDevice_AddBuffer(
1094 DirectSoundDevice
* device
,
1095 IDirectSoundBufferImpl
* pDSB
)
1097 IDirectSoundBufferImpl
**newbuffers
;
1100 TRACE("(%p, %p)\n", device
, pDSB
);
1102 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
1104 if (device
->buffers
)
1105 newbuffers
= HeapReAlloc(GetProcessHeap(),0,device
->buffers
,sizeof(IDirectSoundBufferImpl
*)*(device
->nrofbuffers
+1));
1107 newbuffers
= HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundBufferImpl
*)*(device
->nrofbuffers
+1));
1110 device
->buffers
= newbuffers
;
1111 device
->buffers
[device
->nrofbuffers
] = pDSB
;
1112 device
->nrofbuffers
++;
1113 TRACE("buffer count is now %d\n", device
->nrofbuffers
);
1115 ERR("out of memory for buffer list! Current buffer count is %d\n", device
->nrofbuffers
);
1116 hr
= DSERR_OUTOFMEMORY
;
1119 RtlReleaseResource(&(device
->buffer_list_lock
));
1125 * Remove secondary buffer from buffer list.
1126 * Gets exclusive access to buffer for writing.
1128 HRESULT
DirectSoundDevice_RemoveBuffer(
1129 DirectSoundDevice
* device
,
1130 IDirectSoundBufferImpl
* pDSB
)
1135 TRACE("(%p, %p)\n", device
, pDSB
);
1137 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
1139 for (i
= 0; i
< device
->nrofbuffers
; i
++)
1140 if (device
->buffers
[i
] == pDSB
)
1143 if (i
< device
->nrofbuffers
) {
1144 /* Put the last buffer of the list in the (now empty) position */
1145 device
->buffers
[i
] = device
->buffers
[device
->nrofbuffers
- 1];
1146 device
->nrofbuffers
--;
1147 device
->buffers
= HeapReAlloc(GetProcessHeap(),0,device
->buffers
,sizeof(LPDIRECTSOUNDBUFFER8
)*device
->nrofbuffers
);
1148 TRACE("buffer count is now %d\n", device
->nrofbuffers
);
1151 if (device
->nrofbuffers
== 0) {
1152 HeapFree(GetProcessHeap(),0,device
->buffers
);
1153 device
->buffers
= NULL
;
1156 RtlReleaseResource(&(device
->buffer_list_lock
));