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_inner;
46 IDirectSound8 IDirectSound8_iface;
47 IUnknown *outer_unk; /* internal */
48 LONG ref, refds, numIfaces;
49 DirectSoundDevice *device;
53 static const char * dumpCooperativeLevel(DWORD level)
55 #define LE(x) case x: return #x
60 LE(DSSCL_WRITEPRIMARY);
63 return wine_dbg_sprintf("Unknown(%08x)", level);
66 static void _dump_DSCAPS(DWORD xmask) {
71 #define FE(x) { x, #x },
72 FE(DSCAPS_PRIMARYMONO)
73 FE(DSCAPS_PRIMARYSTEREO)
74 FE(DSCAPS_PRIMARY8BIT)
75 FE(DSCAPS_PRIMARY16BIT)
76 FE(DSCAPS_CONTINUOUSRATE)
79 FE(DSCAPS_SECONDARYMONO)
80 FE(DSCAPS_SECONDARYSTEREO)
81 FE(DSCAPS_SECONDARY8BIT)
82 FE(DSCAPS_SECONDARY16BIT)
87 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
88 if ((flags[i].mask & xmask) == flags[i].mask)
89 TRACE("%s ",flags[i].name);
92 static void _dump_DSBCAPS(DWORD xmask) {
97 #define FE(x) { x, #x },
98 FE(DSBCAPS_PRIMARYBUFFER)
100 FE(DSBCAPS_LOCHARDWARE)
101 FE(DSBCAPS_LOCSOFTWARE)
103 FE(DSBCAPS_CTRLFREQUENCY)
105 FE(DSBCAPS_CTRLVOLUME)
106 FE(DSBCAPS_CTRLPOSITIONNOTIFY)
107 FE(DSBCAPS_STICKYFOCUS)
108 FE(DSBCAPS_GLOBALFOCUS)
109 FE(DSBCAPS_GETCURRENTPOSITION2)
110 FE(DSBCAPS_MUTE3DATMAXDISTANCE)
115 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
116 if ((flags[i].mask & xmask) == flags[i].mask)
117 TRACE("%s ",flags[i].name);
120 static void directsound_destroy(IDirectSoundImpl *This)
123 DirectSoundDevice_Release(This->device);
124 HeapFree(GetProcessHeap(),0,This);
125 TRACE("(%p) released\n", This);
128 /*******************************************************************************
129 * IUnknown Implementation for DirectSound
131 static inline IDirectSoundImpl *impl_from_IUnknown(IUnknown *iface)
133 return CONTAINING_RECORD(iface, IDirectSoundImpl, IUnknown_inner);
136 static HRESULT WINAPI IUnknownImpl_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
138 IDirectSoundImpl *This = impl_from_IUnknown(iface);
140 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppv);
143 WARN("invalid parameter\n");
148 if (IsEqualIID(riid, &IID_IUnknown))
149 *ppv = &This->IUnknown_inner;
150 else if (IsEqualIID(riid, &IID_IDirectSound) ||
151 (IsEqualIID(riid, &IID_IDirectSound8) && This->has_ds8))
152 *ppv = &This->IDirectSound8_iface;
154 WARN("unknown IID %s\n", debugstr_guid(riid));
155 return E_NOINTERFACE;
158 IUnknown_AddRef((IUnknown*)*ppv);
162 static ULONG WINAPI IUnknownImpl_AddRef(IUnknown *iface)
164 IDirectSoundImpl *This = impl_from_IUnknown(iface);
165 ULONG ref = InterlockedIncrement(&This->ref);
167 TRACE("(%p) ref=%d\n", This, ref);
170 InterlockedIncrement(&This->numIfaces);
175 static ULONG WINAPI IUnknownImpl_Release(IUnknown *iface)
177 IDirectSoundImpl *This = impl_from_IUnknown(iface);
178 ULONG ref = InterlockedDecrement(&This->ref);
180 TRACE("(%p) ref=%d\n", This, ref);
182 if (!ref && !InterlockedDecrement(&This->numIfaces))
183 directsound_destroy(This);
188 static const IUnknownVtbl unk_vtbl =
190 IUnknownImpl_QueryInterface,
195 /*******************************************************************************
196 * IDirectSound and IDirectSound8 Implementation
198 static inline IDirectSoundImpl *impl_from_IDirectSound8(IDirectSound8 *iface)
200 return CONTAINING_RECORD(iface, IDirectSoundImpl, IDirectSound8_iface);
203 static HRESULT WINAPI IDirectSound8Impl_QueryInterface(IDirectSound8 *iface, REFIID riid,
206 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
207 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppv);
208 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
211 static ULONG WINAPI IDirectSound8Impl_AddRef(IDirectSound8 *iface)
213 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
214 ULONG ref = InterlockedIncrement(&This->refds);
216 TRACE("(%p) refds=%d\n", This, ref);
219 InterlockedIncrement(&This->numIfaces);
224 static ULONG WINAPI IDirectSound8Impl_Release(IDirectSound8 *iface)
226 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
227 ULONG ref = InterlockedDecrement(&(This->refds));
229 TRACE("(%p) refds=%d\n", This, ref);
231 if (!ref && !InterlockedDecrement(&This->numIfaces))
232 directsound_destroy(This);
237 static HRESULT WINAPI IDirectSound8Impl_CreateSoundBuffer(IDirectSound8 *iface,
238 const DSBUFFERDESC *dsbd, IDirectSoundBuffer **ppdsb, IUnknown *lpunk)
240 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
241 TRACE("(%p,%p,%p,%p)\n", This, dsbd, ppdsb, lpunk);
242 return DirectSoundDevice_CreateSoundBuffer(This->device, dsbd, ppdsb, lpunk, This->has_ds8);
245 static HRESULT WINAPI IDirectSound8Impl_GetCaps(IDirectSound8 *iface, DSCAPS *dscaps)
247 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
249 TRACE("(%p, %p)\n", This, dscaps);
252 WARN("not initialized\n");
253 return DSERR_UNINITIALIZED;
256 WARN("invalid parameter: dscaps = NULL\n");
257 return DSERR_INVALIDPARAM;
259 if (dscaps->dwSize < sizeof(*dscaps)) {
260 WARN("invalid parameter: dscaps->dwSize = %d\n", dscaps->dwSize);
261 return DSERR_INVALIDPARAM;
264 dscaps->dwFlags = This->device->drvcaps.dwFlags;
265 dscaps->dwMinSecondarySampleRate = This->device->drvcaps.dwMinSecondarySampleRate;
266 dscaps->dwMaxSecondarySampleRate = This->device->drvcaps.dwMaxSecondarySampleRate;
267 dscaps->dwPrimaryBuffers = This->device->drvcaps.dwPrimaryBuffers;
268 dscaps->dwMaxHwMixingAllBuffers = This->device->drvcaps.dwMaxHwMixingAllBuffers;
269 dscaps->dwMaxHwMixingStaticBuffers = This->device->drvcaps.dwMaxHwMixingStaticBuffers;
270 dscaps->dwMaxHwMixingStreamingBuffers = This->device->drvcaps.dwMaxHwMixingStreamingBuffers;
271 dscaps->dwFreeHwMixingAllBuffers = This->device->drvcaps.dwFreeHwMixingAllBuffers;
272 dscaps->dwFreeHwMixingStaticBuffers = This->device->drvcaps.dwFreeHwMixingStaticBuffers;
273 dscaps->dwFreeHwMixingStreamingBuffers = This->device->drvcaps.dwFreeHwMixingStreamingBuffers;
274 dscaps->dwMaxHw3DAllBuffers = This->device->drvcaps.dwMaxHw3DAllBuffers;
275 dscaps->dwMaxHw3DStaticBuffers = This->device->drvcaps.dwMaxHw3DStaticBuffers;
276 dscaps->dwMaxHw3DStreamingBuffers = This->device->drvcaps.dwMaxHw3DStreamingBuffers;
277 dscaps->dwFreeHw3DAllBuffers = This->device->drvcaps.dwFreeHw3DAllBuffers;
278 dscaps->dwFreeHw3DStaticBuffers = This->device->drvcaps.dwFreeHw3DStaticBuffers;
279 dscaps->dwFreeHw3DStreamingBuffers = This->device->drvcaps.dwFreeHw3DStreamingBuffers;
280 dscaps->dwTotalHwMemBytes = This->device->drvcaps.dwTotalHwMemBytes;
281 dscaps->dwFreeHwMemBytes = This->device->drvcaps.dwFreeHwMemBytes;
282 dscaps->dwMaxContigFreeHwMemBytes = This->device->drvcaps.dwMaxContigFreeHwMemBytes;
283 dscaps->dwUnlockTransferRateHwBuffers = This->device->drvcaps.dwUnlockTransferRateHwBuffers;
284 dscaps->dwPlayCpuOverheadSwBuffers = This->device->drvcaps.dwPlayCpuOverheadSwBuffers;
286 if (TRACE_ON(dsound)) {
287 TRACE("(flags=0x%08x:\n", dscaps->dwFlags);
288 _dump_DSCAPS(dscaps->dwFlags);
295 static HRESULT WINAPI IDirectSound8Impl_DuplicateSoundBuffer(IDirectSound8 *iface,
296 IDirectSoundBuffer *psb, IDirectSoundBuffer **ppdsb)
298 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
299 TRACE("(%p,%p,%p)\n", This, psb, ppdsb);
300 return DirectSoundDevice_DuplicateSoundBuffer(This->device, psb, ppdsb);
303 static HRESULT WINAPI IDirectSound8Impl_SetCooperativeLevel(IDirectSound8 *iface, HWND hwnd,
306 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
308 TRACE("(%p,%p,%s)\n", This, hwnd, dumpCooperativeLevel(level));
311 WARN("not initialized\n");
312 return DSERR_UNINITIALIZED;
315 if (level == DSSCL_PRIORITY || level == DSSCL_EXCLUSIVE) {
316 WARN("level=%s not fully supported\n",
317 level == DSSCL_PRIORITY ? "DSSCL_PRIORITY" : "DSSCL_EXCLUSIVE");
320 This->device->priolevel = level;
324 static HRESULT WINAPI IDirectSound8Impl_Compact(IDirectSound8 *iface)
326 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
328 TRACE("(%p)\n", This);
331 WARN("not initialized\n");
332 return DSERR_UNINITIALIZED;
335 if (This->device->priolevel < DSSCL_PRIORITY) {
336 WARN("incorrect priority level\n");
337 return DSERR_PRIOLEVELNEEDED;
342 static HRESULT WINAPI IDirectSound8Impl_GetSpeakerConfig(IDirectSound8 *iface, DWORD *config)
344 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
346 TRACE("(%p, %p)\n", This, config);
349 WARN("not initialized\n");
350 return DSERR_UNINITIALIZED;
353 WARN("invalid parameter: config == NULL\n");
354 return DSERR_INVALIDPARAM;
357 WARN("not fully functional\n");
358 *config = This->device->speaker_config;
362 static HRESULT WINAPI IDirectSound8Impl_SetSpeakerConfig(IDirectSound8 *iface, DWORD config)
364 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
366 TRACE("(%p,0x%08x)\n", This, config);
369 WARN("not initialized\n");
370 return DSERR_UNINITIALIZED;
373 This->device->speaker_config = config;
374 WARN("not fully functional\n");
378 static HRESULT WINAPI IDirectSound8Impl_Initialize(IDirectSound8 *iface, const GUID *lpcGuid)
380 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
381 TRACE("(%p, %s)\n", This, debugstr_guid(lpcGuid));
382 return DirectSoundDevice_Initialize(&This->device, lpcGuid);
385 static HRESULT WINAPI IDirectSound8Impl_VerifyCertification(IDirectSound8 *iface, DWORD *certified)
387 IDirectSoundImpl *This = impl_from_IDirectSound8(iface);
389 TRACE("(%p, %p)\n", This, certified);
392 WARN("not initialized\n");
393 return DSERR_UNINITIALIZED;
396 if (This->device->drvcaps.dwFlags & DSCAPS_CERTIFIED)
397 *certified = DS_CERTIFIED;
399 *certified = DS_UNCERTIFIED;
404 static const IDirectSound8Vtbl ds8_vtbl =
406 IDirectSound8Impl_QueryInterface,
407 IDirectSound8Impl_AddRef,
408 IDirectSound8Impl_Release,
409 IDirectSound8Impl_CreateSoundBuffer,
410 IDirectSound8Impl_GetCaps,
411 IDirectSound8Impl_DuplicateSoundBuffer,
412 IDirectSound8Impl_SetCooperativeLevel,
413 IDirectSound8Impl_Compact,
414 IDirectSound8Impl_GetSpeakerConfig,
415 IDirectSound8Impl_SetSpeakerConfig,
416 IDirectSound8Impl_Initialize,
417 IDirectSound8Impl_VerifyCertification
420 HRESULT IDirectSoundImpl_Create(IUnknown *outer_unk, REFIID riid, void **ppv, BOOL has_ds8)
422 IDirectSoundImpl *obj;
425 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
428 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*obj));
430 WARN("out of memory\n");
431 return DSERR_OUTOFMEMORY;
434 setup_dsound_options();
436 obj->IUnknown_inner.lpVtbl = &unk_vtbl;
437 obj->IDirectSound8_iface.lpVtbl = &ds8_vtbl;
442 obj->has_ds8 = has_ds8;
444 /* COM aggregation supported only internally */
446 obj->outer_unk = outer_unk;
448 obj->outer_unk = &obj->IUnknown_inner;
450 hr = IUnknown_QueryInterface(&obj->IUnknown_inner, riid, ppv);
451 IUnknown_Release(&obj->IUnknown_inner);
456 HRESULT DSOUND_Create(REFIID riid, void **ppv)
458 return IDirectSoundImpl_Create(NULL, riid, ppv, FALSE);
461 HRESULT DSOUND_Create8(REFIID riid, void **ppv)
463 return IDirectSoundImpl_Create(NULL, riid, ppv, TRUE);
466 /*******************************************************************************
467 * DirectSoundCreate (DSOUND.1)
469 * Creates and initializes a DirectSound interface.
472 * lpcGUID [I] Address of the GUID that identifies the sound device.
473 * ppDS [O] Address of a variable to receive the interface pointer.
474 * pUnkOuter [I] Must be NULL.
478 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
479 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
481 HRESULT WINAPI DirectSoundCreate(
489 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
492 WARN("invalid parameter: ppDS == NULL\n");
493 return DSERR_INVALIDPARAM;
496 if (pUnkOuter != NULL) {
497 WARN("invalid parameter: pUnkOuter != NULL\n");
499 return DSERR_INVALIDPARAM;
502 hr = DSOUND_Create(&IID_IDirectSound, (void **)&pDS);
504 hr = IDirectSound_Initialize(pDS, lpcGUID);
506 if (hr != DSERR_ALREADYINITIALIZED) {
507 IDirectSound_Release(pDS);
519 /*******************************************************************************
520 * DirectSoundCreate8 (DSOUND.11)
522 * Creates and initializes a DirectSound8 interface.
525 * lpcGUID [I] Address of the GUID that identifies the sound device.
526 * ppDS [O] Address of a variable to receive the interface pointer.
527 * pUnkOuter [I] Must be NULL.
531 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
532 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
534 HRESULT WINAPI DirectSoundCreate8(
536 LPDIRECTSOUND8 *ppDS,
542 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
545 WARN("invalid parameter: ppDS == NULL\n");
546 return DSERR_INVALIDPARAM;
549 if (pUnkOuter != NULL) {
550 WARN("invalid parameter: pUnkOuter != NULL\n");
552 return DSERR_INVALIDPARAM;
555 hr = DSOUND_Create8(&IID_IDirectSound8, (void **)&pDS);
557 hr = IDirectSound8_Initialize(pDS, lpcGUID);
559 if (hr != DSERR_ALREADYINITIALIZED) {
560 IDirectSound8_Release(pDS);
572 /*******************************************************************************
575 static HRESULT DirectSoundDevice_Create(DirectSoundDevice ** ppDevice)
577 DirectSoundDevice * device;
578 TRACE("(%p)\n", ppDevice);
580 /* Allocate memory */
581 device = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DirectSoundDevice));
582 if (device == NULL) {
583 WARN("out of memory\n");
584 return DSERR_OUTOFMEMORY;
588 device->priolevel = DSSCL_NORMAL;
589 device->state = STATE_STOPPED;
590 device->speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, DSSPEAKER_GEOMETRY_WIDE);
592 /* 3D listener initial parameters */
593 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
594 device->ds3dl.vPosition.x = 0.0;
595 device->ds3dl.vPosition.y = 0.0;
596 device->ds3dl.vPosition.z = 0.0;
597 device->ds3dl.vVelocity.x = 0.0;
598 device->ds3dl.vVelocity.y = 0.0;
599 device->ds3dl.vVelocity.z = 0.0;
600 device->ds3dl.vOrientFront.x = 0.0;
601 device->ds3dl.vOrientFront.y = 0.0;
602 device->ds3dl.vOrientFront.z = 1.0;
603 device->ds3dl.vOrientTop.x = 0.0;
604 device->ds3dl.vOrientTop.y = 1.0;
605 device->ds3dl.vOrientTop.z = 0.0;
606 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
607 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
608 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
610 device->prebuf = ds_snd_queue_max;
611 device->guid = GUID_NULL;
613 /* Set default wave format (may need it for waveOutOpen) */
614 device->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEFORMATEX));
615 if (device->pwfx == NULL) {
616 WARN("out of memory\n");
617 HeapFree(GetProcessHeap(),0,device);
618 return DSERR_OUTOFMEMORY;
621 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
622 device->pwfx->nSamplesPerSec = ds_default_sample_rate;
623 device->pwfx->wBitsPerSample = ds_default_bits_per_sample;
624 device->pwfx->nChannels = 2;
625 device->pwfx->nBlockAlign = device->pwfx->wBitsPerSample * device->pwfx->nChannels / 8;
626 device->pwfx->nAvgBytesPerSec = device->pwfx->nSamplesPerSec * device->pwfx->nBlockAlign;
627 device->pwfx->cbSize = 0;
629 InitializeCriticalSection(&(device->mixlock));
630 device->mixlock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DirectSoundDevice.mixlock");
632 RtlInitializeResource(&(device->buffer_list_lock));
639 static ULONG DirectSoundDevice_AddRef(DirectSoundDevice * device)
641 ULONG ref = InterlockedIncrement(&(device->ref));
642 TRACE("(%p) ref was %d\n", device, ref - 1);
646 ULONG DirectSoundDevice_Release(DirectSoundDevice * device)
649 ULONG ref = InterlockedDecrement(&(device->ref));
650 TRACE("(%p) ref was %u\n", device, ref + 1);
653 timeKillEvent(device->timerID);
654 timeEndPeriod(DS_TIME_RES);
656 /* The kill event should have allowed the timer process to expire
657 * but try to grab the lock just in case. Can't hold lock because
658 * secondarybuffer_destroy also grabs the lock */
659 RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
660 RtlReleaseResource(&(device->buffer_list_lock));
662 EnterCriticalSection(&DSOUND_renderers_lock);
663 list_remove(&device->entry);
664 LeaveCriticalSection(&DSOUND_renderers_lock);
666 /* It is allowed to release this object even when buffers are playing */
667 if (device->buffers) {
668 WARN("%d secondary buffers not released\n", device->nrofbuffers);
669 for( i=0;i<device->nrofbuffers;i++)
670 secondarybuffer_destroy(device->buffers[i]);
673 hr = DSOUND_PrimaryDestroy(device);
675 WARN("DSOUND_PrimaryDestroy failed\n");
678 IAudioClient_Release(device->client);
680 IAudioRenderClient_Release(device->render);
682 IAudioClock_Release(device->clock);
684 IAudioStreamVolume_Release(device->volume);
686 HeapFree(GetProcessHeap(), 0, device->tmp_buffer);
687 HeapFree(GetProcessHeap(), 0, device->mix_buffer);
688 HeapFree(GetProcessHeap(), 0, device->buffer);
689 RtlDeleteResource(&device->buffer_list_lock);
690 device->mixlock.DebugInfo->Spare[0] = 0;
691 DeleteCriticalSection(&device->mixlock);
692 HeapFree(GetProcessHeap(),0,device);
693 TRACE("(%p) released\n", device);
698 BOOL DSOUND_check_supported(IAudioClient *client, DWORD rate,
699 DWORD depth, WORD channels)
701 WAVEFORMATEX fmt, *junk;
704 fmt.wFormatTag = WAVE_FORMAT_PCM;
705 fmt.nChannels = channels;
706 fmt.nSamplesPerSec = rate;
707 fmt.wBitsPerSample = depth;
708 fmt.nBlockAlign = (channels * depth) / 8;
709 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
712 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, &fmt, &junk);
719 UINT DSOUND_create_timer(LPTIMECALLBACK cb, DWORD_PTR user)
721 UINT triggertime = DS_TIME_DEL, res = DS_TIME_RES, id;
724 timeGetDevCaps(&time, sizeof(TIMECAPS));
725 TRACE("Minimum timer resolution: %u, max timer: %u\n", time.wPeriodMin, time.wPeriodMax);
726 if (triggertime < time.wPeriodMin)
727 triggertime = time.wPeriodMin;
728 if (res < time.wPeriodMin)
729 res = time.wPeriodMin;
730 if (timeBeginPeriod(res) == TIMERR_NOCANDO)
731 WARN("Could not set minimum resolution, don't expect sound\n");
732 id = timeSetEvent(triggertime, res, cb, user, TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
735 WARN("Timer not created! Retrying without TIME_KILL_SYNCHRONOUS\n");
736 id = timeSetEvent(triggertime, res, cb, user, TIME_PERIODIC);
738 ERR("Could not create timer, sound playback will not occur\n");
743 HRESULT DirectSoundDevice_Initialize(DirectSoundDevice ** ppDevice, LPCGUID lpcGUID)
747 DirectSoundDevice *device;
750 TRACE("(%p,%s)\n",ppDevice,debugstr_guid(lpcGUID));
752 if (*ppDevice != NULL) {
753 WARN("already initialized\n");
754 return DSERR_ALREADYINITIALIZED;
757 /* Default device? */
758 if (!lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL))
759 lpcGUID = &DSDEVID_DefaultPlayback;
761 if(IsEqualGUID(lpcGUID, &DSDEVID_DefaultCapture) ||
762 IsEqualGUID(lpcGUID, &DSDEVID_DefaultVoiceCapture))
763 return DSERR_NODRIVER;
765 if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
766 WARN("invalid parameter: lpcGUID\n");
767 return DSERR_INVALIDPARAM;
770 hr = get_mmdevice(eRender, &devGUID, &mmdevice);
774 EnterCriticalSection(&DSOUND_renderers_lock);
776 LIST_FOR_EACH_ENTRY(device, &DSOUND_renderers, DirectSoundDevice, entry){
777 if(IsEqualGUID(&device->guid, &devGUID)){
778 IMMDevice_Release(mmdevice);
779 DirectSoundDevice_AddRef(device);
781 LeaveCriticalSection(&DSOUND_renderers_lock);
786 hr = DirectSoundDevice_Create(&device);
788 WARN("DirectSoundDevice_Create failed\n");
789 IMMDevice_Release(mmdevice);
790 LeaveCriticalSection(&DSOUND_renderers_lock);
794 device->mmdevice = mmdevice;
795 device->guid = devGUID;
797 hr = DSOUND_ReopenDevice(device, FALSE);
800 HeapFree(GetProcessHeap(), 0, device);
801 LeaveCriticalSection(&DSOUND_renderers_lock);
802 IMMDevice_Release(mmdevice);
803 WARN("DSOUND_ReopenDevice failed: %08x\n", hr);
807 ZeroMemory(&device->drvcaps, sizeof(device->drvcaps));
809 if(DSOUND_check_supported(device->client, 11025, 8, 1) ||
810 DSOUND_check_supported(device->client, 22050, 8, 1) ||
811 DSOUND_check_supported(device->client, 44100, 8, 1) ||
812 DSOUND_check_supported(device->client, 48000, 8, 1) ||
813 DSOUND_check_supported(device->client, 96000, 8, 1))
814 device->drvcaps.dwFlags |= DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARYMONO;
816 if(DSOUND_check_supported(device->client, 11025, 16, 1) ||
817 DSOUND_check_supported(device->client, 22050, 16, 1) ||
818 DSOUND_check_supported(device->client, 44100, 16, 1) ||
819 DSOUND_check_supported(device->client, 48000, 16, 1) ||
820 DSOUND_check_supported(device->client, 96000, 16, 1))
821 device->drvcaps.dwFlags |= DSCAPS_PRIMARY16BIT | DSCAPS_PRIMARYMONO;
823 if(DSOUND_check_supported(device->client, 11025, 8, 2) ||
824 DSOUND_check_supported(device->client, 22050, 8, 2) ||
825 DSOUND_check_supported(device->client, 44100, 8, 2) ||
826 DSOUND_check_supported(device->client, 48000, 8, 2) ||
827 DSOUND_check_supported(device->client, 96000, 8, 2))
828 device->drvcaps.dwFlags |= DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARYSTEREO;
830 if(DSOUND_check_supported(device->client, 11025, 16, 2) ||
831 DSOUND_check_supported(device->client, 22050, 16, 2) ||
832 DSOUND_check_supported(device->client, 44100, 16, 2) ||
833 DSOUND_check_supported(device->client, 48000, 16, 2) ||
834 DSOUND_check_supported(device->client, 96000, 16, 2))
835 device->drvcaps.dwFlags |= DSCAPS_PRIMARY16BIT | DSCAPS_PRIMARYSTEREO;
837 /* the dsound mixer supports all of the following */
838 device->drvcaps.dwFlags |= DSCAPS_SECONDARY8BIT | DSCAPS_SECONDARY16BIT;
839 device->drvcaps.dwFlags |= DSCAPS_SECONDARYMONO | DSCAPS_SECONDARYSTEREO;
840 device->drvcaps.dwFlags |= DSCAPS_CONTINUOUSRATE;
842 device->drvcaps.dwPrimaryBuffers = 1;
843 device->drvcaps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
844 device->drvcaps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
845 device->drvcaps.dwMaxHwMixingAllBuffers = 1;
846 device->drvcaps.dwMaxHwMixingStaticBuffers = 1;
847 device->drvcaps.dwMaxHwMixingStreamingBuffers = 1;
849 ZeroMemory(&device->volpan, sizeof(device->volpan));
851 hr = DSOUND_PrimaryCreate(device);
853 device->timerID = DSOUND_create_timer(DSOUND_timer, (DWORD_PTR)device);
855 WARN("DSOUND_PrimaryCreate failed: %08x\n", hr);
858 list_add_tail(&DSOUND_renderers, &device->entry);
860 LeaveCriticalSection(&DSOUND_renderers_lock);
865 HRESULT DirectSoundDevice_CreateSoundBuffer(
866 DirectSoundDevice * device,
867 LPCDSBUFFERDESC dsbd,
868 LPLPDIRECTSOUNDBUFFER ppdsb,
872 HRESULT hres = DS_OK;
873 TRACE("(%p,%p,%p,%p)\n",device,dsbd,ppdsb,lpunk);
875 if (device == NULL) {
876 WARN("not initialized\n");
877 return DSERR_UNINITIALIZED;
881 WARN("invalid parameter: dsbd == NULL\n");
882 return DSERR_INVALIDPARAM;
885 if (dsbd->dwSize != sizeof(DSBUFFERDESC) &&
886 dsbd->dwSize != sizeof(DSBUFFERDESC1)) {
887 WARN("invalid parameter: dsbd\n");
888 return DSERR_INVALIDPARAM;
892 WARN("invalid parameter: ppdsb == NULL\n");
893 return DSERR_INVALIDPARAM;
897 if (TRACE_ON(dsound)) {
898 TRACE("(structsize=%d)\n",dsbd->dwSize);
899 TRACE("(flags=0x%08x:\n",dsbd->dwFlags);
900 _dump_DSBCAPS(dsbd->dwFlags);
902 TRACE("(bufferbytes=%d)\n",dsbd->dwBufferBytes);
903 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
906 if (dsbd->dwFlags & DSBCAPS_LOCHARDWARE &&
907 !(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
908 TRACE("LOCHARDWARE is not supported, returning E_NOTIMPL\n");
912 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
913 if (dsbd->lpwfxFormat != NULL) {
914 WARN("invalid parameter: dsbd->lpwfxFormat must be NULL for "
916 return DSERR_INVALIDPARAM;
919 if (device->primary) {
920 WARN("Primary Buffer already created\n");
921 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER8)(device->primary));
922 *ppdsb = (LPDIRECTSOUNDBUFFER)(device->primary);
924 hres = primarybuffer_create(device, &device->primary, dsbd);
925 if (device->primary) {
926 *ppdsb = (IDirectSoundBuffer*)&device->primary->IDirectSoundBuffer8_iface;
927 device->primary->dsbd.dwFlags &= ~(DSBCAPS_LOCHARDWARE | DSBCAPS_LOCSOFTWARE);
928 device->primary->dsbd.dwFlags |= DSBCAPS_LOCSOFTWARE;
930 WARN("primarybuffer_create() failed\n");
933 IDirectSoundBufferImpl * dsb;
934 WAVEFORMATEXTENSIBLE *pwfxe;
936 if (dsbd->lpwfxFormat == NULL) {
937 WARN("invalid parameter: dsbd->lpwfxFormat can't be NULL for "
938 "secondary buffer\n");
939 return DSERR_INVALIDPARAM;
941 pwfxe = (WAVEFORMATEXTENSIBLE*)dsbd->lpwfxFormat;
943 if (pwfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
945 /* check if cbSize is at least 22 bytes */
946 if (pwfxe->Format.cbSize < (sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX)))
948 WARN("Too small a cbSize %u\n", pwfxe->Format.cbSize);
949 return DSERR_INVALIDPARAM;
952 /* cbSize should be 22 bytes, with one possible exception */
953 if (pwfxe->Format.cbSize > (sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX)) &&
954 !((IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM) || IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) &&
955 pwfxe->Format.cbSize == sizeof(WAVEFORMATEXTENSIBLE)))
957 WARN("Too big a cbSize %u\n", pwfxe->Format.cbSize);
958 return DSERR_CONTROLUNAVAIL;
961 if ((!IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) && (!IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)))
963 if (!IsEqualGUID(&pwfxe->SubFormat, &GUID_NULL))
964 FIXME("SubFormat %s not supported right now.\n", debugstr_guid(&pwfxe->SubFormat));
965 return DSERR_INVALIDPARAM;
967 if (pwfxe->Samples.wValidBitsPerSample > dsbd->lpwfxFormat->wBitsPerSample)
969 WARN("Samples.wValidBitsPerSample(%d) > Format.wBitsPerSample (%d)\n", pwfxe->Samples.wValidBitsPerSample, pwfxe->Format.wBitsPerSample);
970 return DSERR_INVALIDPARAM;
972 if (pwfxe->Samples.wValidBitsPerSample && pwfxe->Samples.wValidBitsPerSample < dsbd->lpwfxFormat->wBitsPerSample)
974 FIXME("Non-packed formats not supported right now: %d/%d\n", pwfxe->Samples.wValidBitsPerSample, dsbd->lpwfxFormat->wBitsPerSample);
975 return DSERR_CONTROLUNAVAIL;
979 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
980 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
981 dsbd->lpwfxFormat->wFormatTag, dsbd->lpwfxFormat->nChannels,
982 dsbd->lpwfxFormat->nSamplesPerSec,
983 dsbd->lpwfxFormat->nAvgBytesPerSec,
984 dsbd->lpwfxFormat->nBlockAlign,
985 dsbd->lpwfxFormat->wBitsPerSample, dsbd->lpwfxFormat->cbSize);
987 if (from8 && (dsbd->dwFlags & DSBCAPS_CTRL3D) && (dsbd->lpwfxFormat->nChannels != 1)) {
988 WARN("invalid parameter: 3D buffer format must be mono\n");
989 return DSERR_INVALIDPARAM;
992 hres = IDirectSoundBufferImpl_Create(device, &dsb, dsbd);
994 *ppdsb = (IDirectSoundBuffer*)&dsb->IDirectSoundBuffer8_iface;
996 WARN("IDirectSoundBufferImpl_Create failed\n");
1002 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
1003 DirectSoundDevice * device,
1004 LPDIRECTSOUNDBUFFER psb,
1005 LPLPDIRECTSOUNDBUFFER ppdsb)
1007 HRESULT hres = DS_OK;
1008 IDirectSoundBufferImpl* dsb;
1009 TRACE("(%p,%p,%p)\n",device,psb,ppdsb);
1011 if (device == NULL) {
1012 WARN("not initialized\n");
1013 return DSERR_UNINITIALIZED;
1017 WARN("invalid parameter: psb == NULL\n");
1018 return DSERR_INVALIDPARAM;
1021 if (ppdsb == NULL) {
1022 WARN("invalid parameter: ppdsb == NULL\n");
1023 return DSERR_INVALIDPARAM;
1026 /* make sure we have a secondary buffer */
1027 if (psb == (IDirectSoundBuffer *)&device->primary->IDirectSoundBuffer8_iface) {
1028 WARN("trying to duplicate primary buffer\n");
1030 return DSERR_INVALIDCALL;
1033 /* duplicate the actual buffer implementation */
1034 hres = IDirectSoundBufferImpl_Duplicate(device, &dsb, (IDirectSoundBufferImpl*)psb);
1036 *ppdsb = (IDirectSoundBuffer*)&dsb->IDirectSoundBuffer8_iface;
1038 WARN("IDirectSoundBufferImpl_Duplicate failed\n");
1044 * Add secondary buffer to buffer list.
1045 * Gets exclusive access to buffer for writing.
1047 HRESULT DirectSoundDevice_AddBuffer(
1048 DirectSoundDevice * device,
1049 IDirectSoundBufferImpl * pDSB)
1051 IDirectSoundBufferImpl **newbuffers;
1054 TRACE("(%p, %p)\n", device, pDSB);
1056 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
1058 if (device->buffers)
1059 newbuffers = HeapReAlloc(GetProcessHeap(),0,device->buffers,sizeof(IDirectSoundBufferImpl*)*(device->nrofbuffers+1));
1061 newbuffers = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundBufferImpl*)*(device->nrofbuffers+1));
1064 device->buffers = newbuffers;
1065 device->buffers[device->nrofbuffers] = pDSB;
1066 device->nrofbuffers++;
1067 TRACE("buffer count is now %d\n", device->nrofbuffers);
1069 ERR("out of memory for buffer list! Current buffer count is %d\n", device->nrofbuffers);
1070 hr = DSERR_OUTOFMEMORY;
1073 RtlReleaseResource(&(device->buffer_list_lock));
1079 * Remove secondary buffer from buffer list.
1080 * Gets exclusive access to buffer for writing.
1082 HRESULT DirectSoundDevice_RemoveBuffer(
1083 DirectSoundDevice * device,
1084 IDirectSoundBufferImpl * pDSB)
1089 TRACE("(%p, %p)\n", device, pDSB);
1091 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
1093 for (i = 0; i < device->nrofbuffers; i++)
1094 if (device->buffers[i] == pDSB)
1097 if (i < device->nrofbuffers) {
1098 /* Put the last buffer of the list in the (now empty) position */
1099 device->buffers[i] = device->buffers[device->nrofbuffers - 1];
1100 device->nrofbuffers--;
1101 device->buffers = HeapReAlloc(GetProcessHeap(),0,device->buffers,sizeof(LPDIRECTSOUNDBUFFER8)*device->nrofbuffers);
1102 TRACE("buffer count is now %d\n", device->nrofbuffers);
1105 if (device->nrofbuffers == 0) {
1106 HeapFree(GetProcessHeap(),0,device->buffers);
1107 device->buffers = NULL;
1110 RtlReleaseResource(&(device->buffer_list_lock));