3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000 Ove Kåven, TransGaming Technologies, Inc.
8 * Most thread locking is complete. There may be a few race
9 * conditions still lurking.
11 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
12 * and a Turtle Beach Tropez+.
15 * Implement DirectSoundCapture API
16 * Implement SetCooperativeLevel properly (need to address focus issues)
17 * Implement DirectSound3DBuffers (stubs in place)
18 * Use hardware 3D support if available
19 * Add critical section locking inside Release and AddRef methods
20 * Handle static buffers - put those in hardware, non-static not in hardware
21 * Hardware DuplicateSoundBuffer
22 * Proper volume calculation, and setting volume in HEL primary buffer
23 * Optimize WINMM and negotiate fragment size, decrease DS_HEL_MARGIN
29 #include <sys/types.h>
30 #include <sys/fcntl.h>
34 #include <math.h> /* Insomnia - pow() function */
41 #include "wine/windef16.h"
42 #include "debugtools.h"
46 DEFAULT_DEBUG_CHANNEL(dsound);
48 /* these are eligible for tuning... they must be high on slow machines... */
49 /* especially since the WINMM overhead is pretty high, and could be improved quite a bit;
50 * the high DS_HEL_MARGIN reflects the currently high wineoss/HEL latency
51 * some settings here should probably get ported to wine.conf */
52 #define DS_EMULDRIVER 1 /* some games (Quake 2, UT) refuse to accept
53 emulated dsound devices. set to 0 ! */
54 #define DS_HEL_FRAGS 48 /* HEL only: number of waveOut fragments in primary buffer */
55 #define DS_HEL_QUEUE 28 /* HEL only: number of waveOut fragments to prebuffer */
56 /* (Starcraft videos won't work with higher than 32 x10ms) */
57 #define DS_HEL_MARGIN 4 /* HEL only: number of waveOut fragments ahead to mix in new buffers */
59 #define DS_HAL_QUEUE 28 /* HAL only: max number of fragments to prebuffer */
61 /* Linux does not support better timing than 10ms */
62 #define DS_TIME_RES 10 /* Resolution of multimedia timer */
63 #define DS_TIME_DEL 10 /* Delay of multimedia timer callback, and duration of HEL fragment */
65 /*****************************************************************************
66 * Predeclare the interface implementation structures
68 typedef struct IDirectSoundImpl IDirectSoundImpl;
69 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
70 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
71 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
72 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
73 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
74 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
76 /*****************************************************************************
77 * IDirectSound implementation structure
79 struct IDirectSoundImpl
82 ICOM_VFIELD(IDirectSound);
84 /* IDirectSoundImpl fields */
89 LPWAVEHDR pwave[DS_HEL_FRAGS];
90 UINT timerID, pwplay, pwwrite, pwqueue;
94 IDirectSoundBufferImpl** buffers;
95 IDirectSoundBufferImpl* primary;
96 IDirectSound3DListenerImpl* listener;
97 WAVEFORMATEX wfx; /* current main waveformat */
98 CRITICAL_SECTION lock;
101 /*****************************************************************************
102 * IDirectSoundBuffer implementation structure
104 struct IDirectSoundBufferImpl
106 /* IUnknown fields */
107 ICOM_VFIELD(IDirectSoundBuffer);
109 /* IDirectSoundBufferImpl fields */
110 PIDSDRIVERBUFFER hwbuf;
113 IDirectSound3DBufferImpl* ds3db;
114 DWORD playflags,state,leadin;
115 DWORD playpos,mixpos,startpos,writelead,buflen;
116 DWORD nAvgBytesPerSec;
120 IDirectSoundBufferImpl* parent; /* for duplicates */
121 IDirectSoundImpl* dsound;
123 LPDSBPOSITIONNOTIFY notifies;
125 CRITICAL_SECTION lock;
128 #define STATE_STOPPED 0
129 #define STATE_STARTING 1
130 #define STATE_PLAYING 2
131 #define STATE_STOPPING 3
133 /*****************************************************************************
134 * IDirectSoundNotify implementation structure
136 struct IDirectSoundNotifyImpl
138 /* IUnknown fields */
139 ICOM_VFIELD(IDirectSoundNotify);
141 /* IDirectSoundNotifyImpl fields */
142 IDirectSoundBufferImpl* dsb;
145 /*****************************************************************************
146 * IDirectSound3DListener implementation structure
148 struct IDirectSound3DListenerImpl
150 /* IUnknown fields */
151 ICOM_VFIELD(IDirectSound3DListener);
153 /* IDirectSound3DListenerImpl fields */
154 IDirectSoundBufferImpl* dsb;
156 CRITICAL_SECTION lock;
159 /*****************************************************************************
160 * IDirectSound3DBuffer implementation structure
162 struct IDirectSound3DBufferImpl
164 /* IUnknown fields */
165 ICOM_VFIELD(IDirectSound3DBuffer);
167 /* IDirectSound3DBufferImpl fields */
168 IDirectSoundBufferImpl* dsb;
172 CRITICAL_SECTION lock;
176 /*****************************************************************************
177 * IDirectSoundCapture implementation structure
179 struct IDirectSoundCaptureImpl
181 /* IUnknown fields */
182 ICOM_VFIELD(IDirectSoundCapture);
185 /* IDirectSoundCaptureImpl fields */
186 CRITICAL_SECTION lock;
189 /*****************************************************************************
190 * IDirectSoundCapture implementation structure
192 struct IDirectSoundCaptureBufferImpl
194 /* IUnknown fields */
195 ICOM_VFIELD(IDirectSoundCaptureBuffer);
198 /* IDirectSoundCaptureBufferImpl fields */
199 CRITICAL_SECTION lock;
203 /* #define USE_DSOUND3D 1 */
205 #define DSOUND_FREQSHIFT (14)
207 static IDirectSoundImpl* dsound = NULL;
209 static IDirectSoundBufferImpl* primarybuf = NULL;
211 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
213 static HRESULT DSOUND_CreateDirectSoundCapture( LPVOID* ppobj );
214 static HRESULT DSOUND_CreateDirectSoundCaptureBuffer( LPCDSCBUFFERDESC lpcDSCBufferDesc, LPVOID* ppobj );
216 static ICOM_VTABLE(IDirectSoundCapture) dscvt;
217 static ICOM_VTABLE(IDirectSoundCaptureBuffer) dscbvt;
219 static HRESULT mmErr(UINT err)
222 case MMSYSERR_NOERROR:
224 case MMSYSERR_ALLOCATED:
225 return DSERR_ALLOCATED;
226 case MMSYSERR_INVALHANDLE:
227 return DSERR_GENERIC; /* FIXME */
228 case MMSYSERR_NODRIVER:
229 return DSERR_NODRIVER;
231 return DSERR_OUTOFMEMORY;
232 case MMSYSERR_INVALPARAM:
233 return DSERR_INVALIDPARAM;
235 FIXME("Unknown MMSYS error %d\n",err);
236 return DSERR_GENERIC;
240 /***************************************************************************
241 * DirectSoundEnumerateA [DSOUND.2]
243 * Enumerate all DirectSound drivers installed in the system
247 * Failure: DSERR_INVALIDPARAM
249 HRESULT WINAPI DirectSoundEnumerateA(
250 LPDSENUMCALLBACKA lpDSEnumCallback,
253 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
254 lpDSEnumCallback, lpContext);
257 if (lpDSEnumCallback != NULL)
258 lpDSEnumCallback(NULL,"WINE DirectSound using Open Sound System",
265 /***************************************************************************
266 * DirectSoundEnumerateW [DSOUND.3]
268 * Enumerate all DirectSound drivers installed in the system
272 * Failure: DSERR_INVALIDPARAM
274 HRESULT WINAPI DirectSoundEnumerateW(
275 LPDSENUMCALLBACKW lpDSEnumCallback,
278 FIXME("lpDSEnumCallback = %p, lpContext = %p: stub\n",
279 lpDSEnumCallback, lpContext);
285 static void _dump_DSBCAPS(DWORD xmask) {
290 #define FE(x) { x, #x },
291 FE(DSBCAPS_PRIMARYBUFFER)
293 FE(DSBCAPS_LOCHARDWARE)
294 FE(DSBCAPS_LOCSOFTWARE)
296 FE(DSBCAPS_CTRLFREQUENCY)
298 FE(DSBCAPS_CTRLVOLUME)
299 FE(DSBCAPS_CTRLPOSITIONNOTIFY)
300 FE(DSBCAPS_CTRLDEFAULT)
302 FE(DSBCAPS_STICKYFOCUS)
303 FE(DSBCAPS_GLOBALFOCUS)
304 FE(DSBCAPS_GETCURRENTPOSITION2)
305 FE(DSBCAPS_MUTE3DATMAXDISTANCE)
310 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
311 if ((flags[i].mask & xmask) == flags[i].mask)
312 DPRINTF("%s ",flags[i].name);
315 /*******************************************************************************
316 * IDirectSound3DBuffer
319 /* IUnknown methods */
321 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
322 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
324 ICOM_THIS(IDirectSound3DBufferImpl,iface);
326 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
332 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
334 ICOM_THIS(IDirectSound3DBufferImpl,iface);
341 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
343 ICOM_THIS(IDirectSound3DBufferImpl,iface);
345 TRACE("(%p) ref was %ld\n", This, This->ref);
351 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
353 DeleteCriticalSection(&This->lock);
355 HeapFree(GetProcessHeap(),0,This->buffer);
356 HeapFree(GetProcessHeap(),0,This);
362 /* IDirectSound3DBuffer methods */
364 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
365 LPDIRECTSOUND3DBUFFER iface,
366 LPDS3DBUFFER lpDs3dBuffer)
374 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
375 LPDIRECTSOUND3DBUFFER iface,
376 LPDWORD lpdwInsideConeAngle,
377 LPDWORD lpdwOutsideConeAngle)
385 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
386 LPDIRECTSOUND3DBUFFER iface,
387 LPD3DVECTOR lpvConeOrientation)
395 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
396 LPDIRECTSOUND3DBUFFER iface,
397 LPLONG lplConeOutsideVolume)
405 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
406 LPDIRECTSOUND3DBUFFER iface,
407 LPD3DVALUE lpfMaxDistance)
415 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
416 LPDIRECTSOUND3DBUFFER iface,
417 LPD3DVALUE lpfMinDistance)
425 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
426 LPDIRECTSOUND3DBUFFER iface,
435 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
436 LPDIRECTSOUND3DBUFFER iface,
437 LPD3DVECTOR lpvPosition)
445 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
446 LPDIRECTSOUND3DBUFFER iface,
447 LPD3DVECTOR lpvVelocity)
455 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
456 LPDIRECTSOUND3DBUFFER iface,
457 LPCDS3DBUFFER lpcDs3dBuffer,
466 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
467 LPDIRECTSOUND3DBUFFER iface,
468 DWORD dwInsideConeAngle,
469 DWORD dwOutsideConeAngle,
478 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
479 LPDIRECTSOUND3DBUFFER iface,
480 D3DVALUE x, D3DVALUE y, D3DVALUE z,
489 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
490 LPDIRECTSOUND3DBUFFER iface,
491 LONG lConeOutsideVolume,
500 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
501 LPDIRECTSOUND3DBUFFER iface,
502 D3DVALUE fMaxDistance,
511 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
512 LPDIRECTSOUND3DBUFFER iface,
513 D3DVALUE fMinDistance,
522 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
523 LPDIRECTSOUND3DBUFFER iface,
527 ICOM_THIS(IDirectSound3DBufferImpl,iface);
528 TRACE("mode = %lx\n", dwMode);
529 This->ds3db.dwMode = dwMode;
535 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
536 LPDIRECTSOUND3DBUFFER iface,
537 D3DVALUE x, D3DVALUE y, D3DVALUE z,
546 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
547 LPDIRECTSOUND3DBUFFER iface,
548 D3DVALUE x, D3DVALUE y, D3DVALUE z,
557 static ICOM_VTABLE(IDirectSound3DBuffer) ds3dbvt =
559 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
560 /* IUnknown methods */
561 IDirectSound3DBufferImpl_QueryInterface,
562 IDirectSound3DBufferImpl_AddRef,
563 IDirectSound3DBufferImpl_Release,
564 /* IDirectSound3DBuffer methods */
565 IDirectSound3DBufferImpl_GetAllParameters,
566 IDirectSound3DBufferImpl_GetConeAngles,
567 IDirectSound3DBufferImpl_GetConeOrientation,
568 IDirectSound3DBufferImpl_GetConeOutsideVolume,
569 IDirectSound3DBufferImpl_GetMaxDistance,
570 IDirectSound3DBufferImpl_GetMinDistance,
571 IDirectSound3DBufferImpl_GetMode,
572 IDirectSound3DBufferImpl_GetPosition,
573 IDirectSound3DBufferImpl_GetVelocity,
574 IDirectSound3DBufferImpl_SetAllParameters,
575 IDirectSound3DBufferImpl_SetConeAngles,
576 IDirectSound3DBufferImpl_SetConeOrientation,
577 IDirectSound3DBufferImpl_SetConeOutsideVolume,
578 IDirectSound3DBufferImpl_SetMaxDistance,
579 IDirectSound3DBufferImpl_SetMinDistance,
580 IDirectSound3DBufferImpl_SetMode,
581 IDirectSound3DBufferImpl_SetPosition,
582 IDirectSound3DBufferImpl_SetVelocity,
587 static int DSOUND_Create3DBuffer(IDirectSoundBufferImpl* dsb)
589 DWORD i, temp, iSize, oSize, offset;
590 LPBYTE bIbuf, bObuf, bTbuf = NULL;
591 LPWORD wIbuf, wObuf, wTbuf = NULL;
593 /* Inside DirectX says it's stupid but allowed */
594 if (dsb->wfx.nChannels == 2) {
595 /* Convert to mono */
596 if (dsb->wfx.wBitsPerSample == 16) {
597 iSize = dsb->buflen / 4;
598 wTbuf = malloc(dsb->buflen / 2);
600 return DSERR_OUTOFMEMORY;
601 for (i = 0; i < iSize; i++)
602 wTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
605 iSize = dsb->buflen / 2;
606 bTbuf = malloc(dsb->buflen / 2);
608 return DSERR_OUTOFMEMORY;
609 for (i = 0; i < iSize; i++)
610 bTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
614 if (dsb->wfx.wBitsPerSample == 16) {
615 iSize = dsb->buflen / 2;
616 wIbuf = (LPWORD) dsb->buffer;
618 bIbuf = (LPBYTE) dsb->buffer;
623 if (primarybuf->wfx.wBitsPerSample == 16) {
624 wObuf = (LPWORD) dsb->ds3db->buffer;
625 oSize = dsb->ds3db->buflen / 2;
627 bObuf = (LPBYTE) dsb->ds3db->buffer;
628 oSize = dsb->ds3db->buflen;
631 offset = primarybuf->wfx.nSamplesPerSec / 100; /* 10ms */
632 if (primarybuf->wfx.wBitsPerSample == 16 && dsb->wfx.wBitsPerSample == 16)
633 for (i = 0; i < iSize; i++) {
636 temp += wIbuf[i - offset] >> 9;
638 temp += wIbuf[i + iSize - offset] >> 9;
640 wObuf[(i * 2) + 1] = temp;
642 else if (primarybuf->wfx.wBitsPerSample == 8 && dsb->wfx.wBitsPerSample == 8)
643 for (i = 0; i < iSize; i++) {
646 temp += bIbuf[i - offset] >> 5;
648 temp += bIbuf[i + iSize - offset] >> 5;
650 bObuf[(i * 2) + 1] = temp;
661 /*******************************************************************************
662 * IDirectSound3DListener
665 /* IUnknown methods */
666 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
667 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
669 ICOM_THIS(IDirectSound3DListenerImpl,iface);
671 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
675 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
677 ICOM_THIS(IDirectSound3DListenerImpl,iface);
682 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
685 ICOM_THIS(IDirectSound3DListenerImpl,iface);
687 TRACE("(%p) ref was %ld\n", This, This->ref);
689 ulReturn = --This->ref;
691 /* Free all resources */
692 if( ulReturn == 0 ) {
694 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
695 DeleteCriticalSection(&This->lock);
696 HeapFree(GetProcessHeap(),0,This);
702 /* IDirectSound3DListener methods */
703 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
704 LPDIRECTSOUND3DLISTENER iface,
705 LPDS3DLISTENER lpDS3DL)
711 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
712 LPDIRECTSOUND3DLISTENER iface,
713 LPD3DVALUE lpfDistanceFactor)
719 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
720 LPDIRECTSOUND3DLISTENER iface,
721 LPD3DVALUE lpfDopplerFactor)
727 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
728 LPDIRECTSOUND3DLISTENER iface,
729 LPD3DVECTOR lpvOrientFront,
730 LPD3DVECTOR lpvOrientTop)
736 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
737 LPDIRECTSOUND3DLISTENER iface,
738 LPD3DVECTOR lpvPosition)
744 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
745 LPDIRECTSOUND3DLISTENER iface,
746 LPD3DVALUE lpfRolloffFactor)
752 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
753 LPDIRECTSOUND3DLISTENER iface,
754 LPD3DVECTOR lpvVelocity)
760 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
761 LPDIRECTSOUND3DLISTENER iface,
762 LPCDS3DLISTENER lpcDS3DL,
769 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
770 LPDIRECTSOUND3DLISTENER iface,
771 D3DVALUE fDistanceFactor,
778 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
779 LPDIRECTSOUND3DLISTENER iface,
780 D3DVALUE fDopplerFactor,
787 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
788 LPDIRECTSOUND3DLISTENER iface,
789 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
790 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
797 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
798 LPDIRECTSOUND3DLISTENER iface,
799 D3DVALUE x, D3DVALUE y, D3DVALUE z,
806 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
807 LPDIRECTSOUND3DLISTENER iface,
808 D3DVALUE fRolloffFactor,
815 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
816 LPDIRECTSOUND3DLISTENER iface,
817 D3DVALUE x, D3DVALUE y, D3DVALUE z,
824 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
825 LPDIRECTSOUND3DLISTENER iface)
832 static ICOM_VTABLE(IDirectSound3DListener) ds3dlvt =
834 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
835 /* IUnknown methods */
836 IDirectSound3DListenerImpl_QueryInterface,
837 IDirectSound3DListenerImpl_AddRef,
838 IDirectSound3DListenerImpl_Release,
839 /* IDirectSound3DListener methods */
840 IDirectSound3DListenerImpl_GetAllParameter,
841 IDirectSound3DListenerImpl_GetDistanceFactor,
842 IDirectSound3DListenerImpl_GetDopplerFactor,
843 IDirectSound3DListenerImpl_GetOrientation,
844 IDirectSound3DListenerImpl_GetPosition,
845 IDirectSound3DListenerImpl_GetRolloffFactor,
846 IDirectSound3DListenerImpl_GetVelocity,
847 IDirectSound3DListenerImpl_SetAllParameters,
848 IDirectSound3DListenerImpl_SetDistanceFactor,
849 IDirectSound3DListenerImpl_SetDopplerFactor,
850 IDirectSound3DListenerImpl_SetOrientation,
851 IDirectSound3DListenerImpl_SetPosition,
852 IDirectSound3DListenerImpl_SetRolloffFactor,
853 IDirectSound3DListenerImpl_SetVelocity,
854 IDirectSound3DListenerImpl_CommitDeferredSettings,
857 /*******************************************************************************
860 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
861 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
863 ICOM_THIS(IDirectSoundNotifyImpl,iface);
865 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
869 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
870 ICOM_THIS(IDirectSoundNotifyImpl,iface);
871 return ++(This->ref);
874 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
875 ICOM_THIS(IDirectSoundNotifyImpl,iface);
877 TRACE("(%p) ref was %ld\n", This, This->ref);
881 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
882 HeapFree(GetProcessHeap(),0,This);
888 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
889 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
891 ICOM_THIS(IDirectSoundNotifyImpl,iface);
894 if (TRACE_ON(dsound)) {
895 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
896 for (i=0;i<howmuch;i++)
897 TRACE("notify at %ld to 0x%08lx\n",
898 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
900 This->dsb->notifies = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->dsb->notifies,(This->dsb->nrofnotifies+howmuch)*sizeof(DSBPOSITIONNOTIFY));
901 memcpy( This->dsb->notifies+This->dsb->nrofnotifies,
903 howmuch*sizeof(DSBPOSITIONNOTIFY)
905 This->dsb->nrofnotifies+=howmuch;
910 static ICOM_VTABLE(IDirectSoundNotify) dsnvt =
912 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
913 IDirectSoundNotifyImpl_QueryInterface,
914 IDirectSoundNotifyImpl_AddRef,
915 IDirectSoundNotifyImpl_Release,
916 IDirectSoundNotifyImpl_SetNotificationPositions,
919 /*******************************************************************************
923 static void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
927 /* the AmpFactors are expressed in 16.16 fixed point */
928 volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 65536);
929 /* FIXME: dwPan{Left|Right}AmpFactor */
931 /* FIXME: use calculated vol and pan ampfactors */
932 temp = (double) (volpan->lVolume - (volpan->lPan > 0 ? volpan->lPan : 0));
933 volpan->dwTotalLeftAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 65536);
934 temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
935 volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 65536);
937 TRACE("left = %lx, right = %lx\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
940 static void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
944 sw = dsb->wfx.nChannels * (dsb->wfx.wBitsPerSample / 8);
945 if ((dsb->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && dsb->hwbuf) {
947 /* let fragment size approximate the timer delay */
948 fraglen = (dsb->freq * DS_TIME_DEL / 1000) * sw;
949 /* reduce fragment size until an integer number of them fits in the buffer */
950 /* (FIXME: this may or may not be a good idea) */
951 while (dsb->buflen % fraglen) fraglen -= sw;
952 dsb->dsound->fraglen = fraglen;
953 TRACE("fraglen=%ld\n", dsb->dsound->fraglen);
955 /* calculate the 10ms write lead */
956 dsb->writelead = (dsb->freq / 100) * sw;
959 static HRESULT DSOUND_PrimaryOpen(IDirectSoundBufferImpl *dsb)
963 /* are we using waveOut stuff? */
967 HRESULT merr = DS_OK;
968 /* Start in pause mode, to allow buffers to get filled */
969 waveOutPause(dsb->dsound->hwo);
970 /* use fragments of 10ms (1/100s) each (which should get us within
971 * the documented write cursor lead of 10-15ms) */
972 buflen = ((dsb->wfx.nAvgBytesPerSec / 100) & ~3) * DS_HEL_FRAGS;
973 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, dsb->buffer);
974 /* reallocate emulated primary buffer */
975 newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,dsb->buffer,buflen);
976 if (newbuf == NULL) {
977 ERR("failed to allocate primary buffer\n");
978 merr = DSERR_OUTOFMEMORY;
979 /* but the old buffer might still exists and must be re-prepared */
981 dsb->buffer = newbuf;
982 dsb->buflen = buflen;
986 IDirectSoundImpl *ds = dsb->dsound;
988 ds->fraglen = dsb->buflen / DS_HEL_FRAGS;
990 /* prepare fragment headers */
991 for (c=0; c<DS_HEL_FRAGS; c++) {
992 ds->pwave[c]->lpData = dsb->buffer + c*ds->fraglen;
993 ds->pwave[c]->dwBufferLength = ds->fraglen;
994 ds->pwave[c]->dwUser = (DWORD)dsb;
995 ds->pwave[c]->dwFlags = 0;
996 ds->pwave[c]->dwLoops = 0;
997 err = mmErr(waveOutPrepareHeader(ds->hwo,ds->pwave[c],sizeof(WAVEHDR)));
1000 waveOutUnprepareHeader(ds->hwo,ds->pwave[c],sizeof(WAVEHDR));
1008 memset(dsb->buffer, (dsb->wfx.wBitsPerSample == 16) ? 0 : 128, dsb->buflen);
1009 TRACE("fraglen=%ld\n", ds->fraglen);
1011 if ((err == DS_OK) && (merr != DS_OK))
1018 static void DSOUND_PrimaryClose(IDirectSoundBufferImpl *dsb)
1020 /* are we using waveOut stuff? */
1023 IDirectSoundImpl *ds = dsb->dsound;
1025 waveOutReset(ds->hwo);
1026 for (c=0; c<DS_HEL_FRAGS; c++)
1027 waveOutUnprepareHeader(ds->hwo, ds->pwave[c], sizeof(WAVEHDR));
1031 /* This sets this format for the <em>Primary Buffer Only</em> */
1032 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
1033 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
1034 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX wfex
1036 ICOM_THIS(IDirectSoundBufferImpl,iface);
1037 IDirectSoundBufferImpl** dsb;
1038 HRESULT err = DS_OK;
1041 /* Let's be pedantic! */
1042 if ((wfex == NULL) ||
1043 (wfex->wFormatTag != WAVE_FORMAT_PCM) ||
1044 (wfex->nChannels < 1) || (wfex->nChannels > 2) ||
1045 (wfex->nSamplesPerSec < 1) ||
1046 (wfex->nBlockAlign < 1) || (wfex->nChannels > 4) ||
1047 ((wfex->wBitsPerSample != 8) && (wfex->wBitsPerSample != 16))) {
1048 TRACE("failed pedantic check!\n");
1049 return DSERR_INVALIDPARAM;
1053 EnterCriticalSection(&(This->dsound->lock));
1055 if (primarybuf->wfx.nSamplesPerSec != wfex->nSamplesPerSec) {
1056 dsb = dsound->buffers;
1057 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
1059 EnterCriticalSection(&((*dsb)->lock));
1061 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
1062 wfex->nSamplesPerSec;
1064 LeaveCriticalSection(&((*dsb)->lock));
1069 memcpy(&(primarybuf->wfx), wfex, sizeof(primarybuf->wfx));
1071 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1072 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1073 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1074 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1075 wfex->wBitsPerSample, wfex->cbSize);
1077 primarybuf->wfx.nAvgBytesPerSec =
1078 This->wfx.nSamplesPerSec * This->wfx.nBlockAlign;
1079 if (primarybuf->dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
1080 /* FIXME: check for errors */
1081 DSOUND_PrimaryClose(primarybuf);
1082 waveOutClose(This->dsound->hwo);
1083 This->dsound->hwo = 0;
1084 waveOutOpen(&(This->dsound->hwo), This->dsound->drvdesc.dnDevNode,
1085 &(primarybuf->wfx), 0, 0, CALLBACK_NULL | WAVE_DIRECTSOUND);
1086 DSOUND_PrimaryOpen(primarybuf);
1088 if (primarybuf->hwbuf) {
1089 err = IDsDriverBuffer_SetFormat(primarybuf->hwbuf, &(primarybuf->wfx));
1090 if (err == DSERR_BUFFERLOST) {
1091 /* Wine-only: the driver wants us to recreate the HW buffer */
1092 IDsDriverBuffer_Release(primarybuf->hwbuf);
1093 err = IDsDriver_CreateSoundBuffer(primarybuf->dsound->driver,&(primarybuf->wfx),primarybuf->dsbd.dwFlags,0,
1094 &(primarybuf->buflen),&(primarybuf->buffer),
1095 (LPVOID)&(primarybuf->hwbuf));
1098 DSOUND_RecalcFormat(primarybuf);
1100 LeaveCriticalSection(&(This->dsound->lock));
1106 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
1107 LPDIRECTSOUNDBUFFER iface,LONG vol
1109 ICOM_THIS(IDirectSoundBufferImpl,iface);
1111 TRACE("(%p,%ld)\n",This,vol);
1113 /* I'm not sure if we need this for primary buffer */
1114 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
1115 return DSERR_CONTROLUNAVAIL;
1117 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN))
1118 return DSERR_INVALIDPARAM;
1121 EnterCriticalSection(&(This->lock));
1123 This->volpan.lVolume = vol;
1125 DSOUND_RecalcVolPan(&(This->volpan));
1128 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
1130 else if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
1131 #if 0 /* should we really do this? */
1132 /* the DS volume ranges from 0 (max, 0dB attenuation) to -10000 (min, 100dB attenuation) */
1133 /* the MM volume ranges from 0 to 0xffff in an unspecified logarithmic scale */
1134 WORD cvol = 0xffff + vol*6 + vol/2;
1135 DWORD vol = cvol | ((DWORD)cvol << 16)
1136 waveOutSetVolume(This->dsound->hwo, vol);
1140 LeaveCriticalSection(&(This->lock));
1146 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
1147 LPDIRECTSOUNDBUFFER iface,LPLONG vol
1149 ICOM_THIS(IDirectSoundBufferImpl,iface);
1150 TRACE("(%p,%p)\n",This,vol);
1153 return DSERR_INVALIDPARAM;
1155 *vol = This->volpan.lVolume;
1159 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
1160 LPDIRECTSOUNDBUFFER iface,DWORD freq
1162 ICOM_THIS(IDirectSoundBufferImpl,iface);
1163 TRACE("(%p,%ld)\n",This,freq);
1165 /* You cannot set the frequency of the primary buffer */
1166 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY) ||
1167 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1168 return DSERR_CONTROLUNAVAIL;
1170 if (!freq) freq = This->wfx.nSamplesPerSec;
1172 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX))
1173 return DSERR_INVALIDPARAM;
1176 EnterCriticalSection(&(This->lock));
1179 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / primarybuf->wfx.nSamplesPerSec;
1180 This->nAvgBytesPerSec = freq * This->wfx.nBlockAlign;
1181 DSOUND_RecalcFormat(This);
1183 LeaveCriticalSection(&(This->lock));
1189 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
1190 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
1192 ICOM_THIS(IDirectSoundBufferImpl,iface);
1193 TRACE("(%p,%08lx,%08lx,%08lx)\n",
1194 This,reserved1,reserved2,flags
1196 This->playflags = flags;
1197 if (This->state == STATE_STOPPED) {
1198 This->leadin = TRUE;
1199 This->startpos = This->mixpos;
1200 This->state = STATE_STARTING;
1201 } else if (This->state == STATE_STOPPING)
1202 This->state = STATE_PLAYING;
1203 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && This->hwbuf) {
1204 IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
1205 This->state = STATE_PLAYING;
1210 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
1212 ICOM_THIS(IDirectSoundBufferImpl,iface);
1213 TRACE("(%p)\n",This);
1216 EnterCriticalSection(&(This->lock));
1218 if (This->state == STATE_PLAYING)
1219 This->state = STATE_STOPPING;
1220 else if (This->state == STATE_STARTING)
1221 This->state = STATE_STOPPED;
1222 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && This->hwbuf) {
1223 IDsDriverBuffer_Stop(This->hwbuf);
1224 This->state = STATE_STOPPED;
1226 DSOUND_CheckEvent(This, 0);
1228 LeaveCriticalSection(&(This->lock));
1234 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface) {
1235 ICOM_THIS(IDirectSoundBufferImpl,iface);
1238 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
1240 ref = InterlockedIncrement(&(This->ref));
1242 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
1246 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER iface) {
1247 ICOM_THIS(IDirectSoundBufferImpl,iface);
1251 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
1253 ref = InterlockedDecrement(&(This->ref));
1254 if (ref) return ref;
1256 EnterCriticalSection(&(This->dsound->lock));
1257 for (i=0;i<This->dsound->nrofbuffers;i++)
1258 if (This->dsound->buffers[i] == This)
1261 if (i < This->dsound->nrofbuffers) {
1262 /* Put the last buffer of the list in the (now empty) position */
1263 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
1264 This->dsound->nrofbuffers--;
1265 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER)*This->dsound->nrofbuffers);
1266 TRACE("buffer count is now %d\n", This->dsound->nrofbuffers);
1267 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
1269 LeaveCriticalSection(&(This->dsound->lock));
1271 DeleteCriticalSection(&(This->lock));
1272 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1273 DSOUND_PrimaryClose(This);
1275 IDsDriverBuffer_Release(This->hwbuf);
1278 IDirectSound3DBuffer_Release((LPDIRECTSOUND3DBUFFER)This->ds3db);
1280 /* this is a duplicate buffer */
1281 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->parent);
1283 /* this is a toplevel buffer */
1284 HeapFree(GetProcessHeap(),0,This->buffer);
1286 HeapFree(GetProcessHeap(),0,This);
1288 if (This == primarybuf)
1294 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
1295 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
1297 ICOM_THIS(IDirectSoundBufferImpl,iface);
1298 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1300 IDsDriverBuffer_GetPosition(This->hwbuf, playpos, writepos);
1302 else if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
1303 if (playpos && (This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2)) {
1305 mtime.wType = TIME_BYTES;
1306 waveOutGetPosition(This->dsound->hwo, &mtime, sizeof(mtime));
1307 mtime.u.cb = mtime.u.cb % This->buflen;
1308 *playpos = mtime.u.cb;
1310 /* don't know how exactly non-GETCURRENTPOSITION2 behaves,
1311 * but I think this works for Starcraft */
1312 else if (playpos) *playpos = This->playpos;
1314 /* the writepos should only be used by apps with WRITEPRIMARY priority,
1315 * in which case our software mixer is disabled anyway */
1316 *writepos = This->playpos + DS_HEL_MARGIN * This->dsound->fraglen;
1317 while (*writepos >= This->buflen)
1318 *writepos -= This->buflen;
1321 if (playpos && (This->state != STATE_PLAYING)) {
1322 /* we haven't been merged into the primary buffer (yet) */
1323 *playpos = This->mixpos;
1326 DWORD pplay, lplay, splay, tplay, pstate;
1327 /* let's get this exact; first, recursively call GetPosition on the primary */
1328 EnterCriticalSection(&(primarybuf->lock));
1329 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || primarybuf->hwbuf) {
1330 IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER)primarybuf, &pplay, NULL);
1332 /* (unless the app isn't using GETCURRENTPOSITION2) */
1333 /* don't know exactly how this should be handled either */
1334 pplay = primarybuf->playpos;
1336 /* get last mixed primary play position */
1337 lplay = primarybuf->mixpos;
1338 pstate = primarybuf->state;
1339 /* detect HEL mode underrun */
1340 if (!(primarybuf->hwbuf || primarybuf->dsound->pwqueue)) {
1341 TRACE("detected an underrun\n");
1343 if (pstate == STATE_PLAYING)
1344 pstate = STATE_STARTING;
1345 else if (pstate == STATE_STOPPING)
1346 pstate = STATE_STOPPED;
1348 /* get our own last mixed position while we still have the lock */
1349 splay = This->mixpos;
1350 LeaveCriticalSection(&(primarybuf->lock));
1351 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, lplay);
1352 TRACE("this mixpos=%ld\n", splay);
1354 /* the actual primary play position (pplay) is always behind last mixed (lplay),
1355 * unless the computer is too slow or something */
1356 /* we need to know how far away we are from there */
1357 if (lplay == pplay) {
1358 if ((pstate == STATE_PLAYING) || (pstate == STATE_STOPPING)) {
1359 /* wow, the software mixer is really doing well,
1360 * seems the entire primary buffer is filled! */
1361 lplay += primarybuf->buflen;
1363 /* else: the primary buffer is not playing, so probably empty */
1365 if (lplay < pplay) lplay += primarybuf->buflen; /* wraparound */
1367 /* detect HAL mode underrun */
1368 if (primarybuf->hwbuf &&
1369 (lplay > ((DS_HAL_QUEUE + 1) * primarybuf->dsound->fraglen + primarybuf->writelead))) {
1370 TRACE("detected an underrun: primary queue was %ld\n",lplay);
1373 /* divide the offset by its sample size */
1374 lplay /= primarybuf->wfx.nChannels * (primarybuf->wfx.wBitsPerSample / 8);
1375 TRACE("primary back-samples=%ld\n",lplay);
1376 /* adjust for our frequency */
1377 lplay = (lplay * This->freqAdjust) >> DSOUND_FREQSHIFT;
1378 /* multiply by our own sample size */
1379 lplay *= This->wfx.nChannels * (This->wfx.wBitsPerSample / 8);
1380 TRACE("this back-offset=%ld\n", lplay);
1381 /* subtract from our last mixed position */
1383 while (tplay < lplay) tplay += This->buflen; /* wraparound */
1385 if (This->leadin && ((tplay < This->startpos) || (tplay > splay))) {
1386 /* seems we haven't started playing yet */
1387 TRACE("this still in lead-in phase\n");
1388 tplay = This->startpos;
1390 /* return the result */
1393 if (writepos) *writepos = This->mixpos;
1396 if (This->state != STATE_STOPPED)
1397 /* apply the documented 10ms lead to writepos */
1398 *writepos += This->writelead;
1399 while (*writepos >= This->buflen) *writepos -= This->buflen;
1401 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
1405 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
1406 LPDIRECTSOUNDBUFFER iface,LPDWORD status
1408 ICOM_THIS(IDirectSoundBufferImpl,iface);
1409 TRACE("(%p,%p), thread is %lx\n",This,status,GetCurrentThreadId());
1412 return DSERR_INVALIDPARAM;
1415 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING))
1416 *status |= DSBSTATUS_PLAYING;
1417 if (This->playflags & DSBPLAY_LOOPING)
1418 *status |= DSBSTATUS_LOOPING;
1420 TRACE("status=%lx\n", *status);
1425 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
1426 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
1428 ICOM_THIS(IDirectSoundBufferImpl,iface);
1429 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1431 if (wfsize>sizeof(This->wfx))
1432 wfsize = sizeof(This->wfx);
1433 if (lpwf) { /* NULL is valid */
1434 memcpy(lpwf,&(This->wfx),wfsize);
1436 *wfwritten = wfsize;
1439 *wfwritten = sizeof(This->wfx);
1441 return DSERR_INVALIDPARAM;
1446 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
1447 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
1449 ICOM_THIS(IDirectSoundBufferImpl,iface);
1452 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1463 if (flags & DSBLOCK_FROMWRITECURSOR) {
1465 /* GetCurrentPosition does too much magic to duplicate here */
1466 IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
1467 writecursor += writepos;
1469 if (flags & DSBLOCK_ENTIREBUFFER)
1470 writebytes = This->buflen;
1471 if (writebytes > This->buflen)
1472 writebytes = This->buflen;
1474 assert(audiobytes1!=audiobytes2);
1475 assert(lplpaudioptr1!=lplpaudioptr2);
1477 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1478 capf = DSDDESC_DONTNEEDPRIMARYLOCK;
1480 capf = DSDDESC_DONTNEEDSECONDARYLOCK;
1481 if (!(This->dsound->drvdesc.dwFlags & capf) && This->hwbuf) {
1482 IDsDriverBuffer_Lock(This->hwbuf,
1483 lplpaudioptr1, audiobytes1,
1484 lplpaudioptr2, audiobytes2,
1485 writecursor, writebytes,
1488 if (writecursor+writebytes <= This->buflen) {
1489 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1490 *audiobytes1 = writebytes;
1492 *(LPBYTE*)lplpaudioptr2 = NULL;
1495 TRACE("->%ld.0\n",writebytes);
1497 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1498 *audiobytes1 = This->buflen-writecursor;
1500 *(LPBYTE*)lplpaudioptr2 = This->buffer;
1502 *audiobytes2 = writebytes-(This->buflen-writecursor);
1503 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
1508 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
1509 LPDIRECTSOUNDBUFFER iface,DWORD newpos
1511 ICOM_THIS(IDirectSoundBufferImpl,iface);
1512 TRACE("(%p,%ld)\n",This,newpos);
1515 EnterCriticalSection(&(This->lock));
1517 This->mixpos = newpos;
1519 IDsDriverBuffer_SetPosition(This->hwbuf, This->mixpos);
1521 LeaveCriticalSection(&(This->lock));
1527 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
1528 LPDIRECTSOUNDBUFFER iface,LONG pan
1530 ICOM_THIS(IDirectSoundBufferImpl,iface);
1532 TRACE("(%p,%ld)\n",This,pan);
1534 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT))
1535 return DSERR_INVALIDPARAM;
1537 /* You cannot set the pan of the primary buffer */
1538 /* and you cannot use both pan and 3D controls */
1539 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
1540 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) ||
1541 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1542 return DSERR_CONTROLUNAVAIL;
1545 EnterCriticalSection(&(This->lock));
1547 This->volpan.lPan = pan;
1549 DSOUND_RecalcVolPan(&(This->volpan));
1552 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
1555 LeaveCriticalSection(&(This->lock));
1561 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
1562 LPDIRECTSOUNDBUFFER iface,LPLONG pan
1564 ICOM_THIS(IDirectSoundBufferImpl,iface);
1565 TRACE("(%p,%p)\n",This,pan);
1568 return DSERR_INVALIDPARAM;
1570 *pan = This->volpan.lPan;
1575 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
1576 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1578 ICOM_THIS(IDirectSoundBufferImpl,iface);
1581 TRACE("(%p,%p,%ld,%p,%ld):stub\n", This,p1,x1,p2,x2);
1584 /* Preprocess 3D buffers... */
1586 /* This is highly experimental and liable to break things */
1587 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D)
1588 DSOUND_Create3DBuffer(This);
1591 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1592 capf = DSDDESC_DONTNEEDPRIMARYLOCK;
1594 capf = DSDDESC_DONTNEEDSECONDARYLOCK;
1595 if (!(This->dsound->drvdesc.dwFlags & capf) && This->hwbuf) {
1596 IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
1602 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
1603 LPDIRECTSOUNDBUFFER iface
1605 ICOM_THIS(IDirectSoundBufferImpl,iface);
1606 FIXME("(%p):stub\n",This);
1610 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
1611 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1613 ICOM_THIS(IDirectSoundBufferImpl,iface);
1614 TRACE("(%p,%p)\n",This,freq);
1617 return DSERR_INVALIDPARAM;
1620 TRACE("-> %ld\n", *freq);
1625 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
1626 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPDSBUFFERDESC dbsd
1628 ICOM_THIS(IDirectSoundBufferImpl,iface);
1629 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
1630 DPRINTF("Re-Init!!!\n");
1631 return DSERR_ALREADYINITIALIZED;
1634 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
1635 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1637 ICOM_THIS(IDirectSoundBufferImpl,iface);
1638 TRACE("(%p)->(%p)\n",This,caps);
1641 return DSERR_INVALIDPARAM;
1643 /* I think we should check this value, not set it. See */
1644 /* Inside DirectX, p215. That should apply here, too. */
1645 caps->dwSize = sizeof(*caps);
1647 caps->dwFlags = This->dsbd.dwFlags;
1648 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
1649 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
1651 caps->dwBufferBytes = This->dsbd.dwBufferBytes;
1653 /* This value represents the speed of the "unlock" command.
1654 As unlock is quite fast (it does not do anything), I put
1655 4096 ko/s = 4 Mo / s */
1656 /* FIXME: hwbuf speed */
1657 caps->dwUnlockTransferRate = 4096;
1658 caps->dwPlayCpuOverhead = 0;
1663 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
1664 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1666 ICOM_THIS(IDirectSoundBufferImpl,iface);
1668 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1670 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1671 IDirectSoundNotifyImpl *dsn;
1673 dsn = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsn));
1676 IDirectSoundBuffer_AddRef(iface);
1677 ICOM_VTBL(dsn) = &dsnvt;
1678 *ppobj = (LPVOID)dsn;
1683 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1684 IDirectSound3DBufferImpl *ds3db;
1686 *ppobj = This->ds3db;
1688 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
1692 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
1695 ds3db->dsb = (*ippdsb);
1696 ICOM_VTBL(ds3db) = &ds3dbvt;
1697 InitializeCriticalSection(&ds3db->lock);
1699 ds3db->ds3db = This;
1700 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1702 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1703 ds3db->ds3db.vPosition.x.x = 0.0;
1704 ds3db->ds3db.vPosition.y.y = 0.0;
1705 ds3db->ds3db.vPosition.z.z = 0.0;
1706 ds3db->ds3db.vVelocity.x.x = 0.0;
1707 ds3db->ds3db.vVelocity.y.y = 0.0;
1708 ds3db->ds3db.vVelocity.z.z = 0.0;
1709 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1710 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1711 ds3db->ds3db.vConeOrientation.x.x = 0.0;
1712 ds3db->ds3db.vConeOrientation.y.y = 0.0;
1713 ds3db->ds3db.vConeOrientation.z.z = 0.0;
1714 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME; ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1715 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1716 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
1717 ds3db->buflen = ((*ippdsb)->buflen * primarybuf->wfx.nBlockAlign) /
1718 (*ippdsb)->wfx.nBlockAlign;
1719 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
1720 if (ds3db->buffer == NULL) {
1722 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
1729 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1730 IDirectSound3DListenerImpl* dsl;
1732 if (This->dsound->listener) {
1733 *ppobj = This->dsound->listener;
1734 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This->dsound->listener);
1738 dsl = (IDirectSound3DListenerImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsl));
1740 ICOM_VTBL(dsl) = &ds3dlvt;
1741 *ppobj = (LPVOID)dsl;
1743 dsl->ds3dl.dwSize = sizeof(DS3DLISTENER);
1744 dsl->ds3dl.vPosition.u1.x = 0.0;
1745 dsl->ds3dl.vPosition.u2.y = 0.0;
1746 dsl->ds3dl.vPosition.u3.z = 0.0;
1747 dsl->ds3dl.vVelocity.u1.x = 0.0;
1748 dsl->ds3dl.vVelocity.u2.y = 0.0;
1749 dsl->ds3dl.vVelocity.u3.z = 0.0;
1750 dsl->ds3dl.vOrientFront.u1.x = 0.0;
1751 dsl->ds3dl.vOrientFront.u2.y = 0.0;
1752 dsl->ds3dl.vOrientFront.u3.z = 1.0;
1753 dsl->ds3dl.vOrientTop.u1.x = 0.0;
1754 dsl->ds3dl.vOrientTop.u2.y = 1.0;
1755 dsl->ds3dl.vOrientTop.u3.z = 0.0;
1756 dsl->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1757 dsl->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1759 InitializeCriticalSection(&dsl->lock);
1762 IDirectSoundBuffer_AddRef(iface);
1764 This->dsound->listener = dsl;
1765 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)dsl);
1770 FIXME( "Unknown GUID %s\n", debugstr_guid( riid ) );
1777 static ICOM_VTABLE(IDirectSoundBuffer) dsbvt =
1779 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1780 IDirectSoundBufferImpl_QueryInterface,
1781 IDirectSoundBufferImpl_AddRef,
1782 IDirectSoundBufferImpl_Release,
1783 IDirectSoundBufferImpl_GetCaps,
1784 IDirectSoundBufferImpl_GetCurrentPosition,
1785 IDirectSoundBufferImpl_GetFormat,
1786 IDirectSoundBufferImpl_GetVolume,
1787 IDirectSoundBufferImpl_GetPan,
1788 IDirectSoundBufferImpl_GetFrequency,
1789 IDirectSoundBufferImpl_GetStatus,
1790 IDirectSoundBufferImpl_Initialize,
1791 IDirectSoundBufferImpl_Lock,
1792 IDirectSoundBufferImpl_Play,
1793 IDirectSoundBufferImpl_SetCurrentPosition,
1794 IDirectSoundBufferImpl_SetFormat,
1795 IDirectSoundBufferImpl_SetVolume,
1796 IDirectSoundBufferImpl_SetPan,
1797 IDirectSoundBufferImpl_SetFrequency,
1798 IDirectSoundBufferImpl_Stop,
1799 IDirectSoundBufferImpl_Unlock,
1800 IDirectSoundBufferImpl_Restore
1803 /*******************************************************************************
1807 static HRESULT WINAPI IDirectSoundImpl_SetCooperativeLevel(
1808 LPDIRECTSOUND iface,HWND hwnd,DWORD level
1810 ICOM_THIS(IDirectSoundImpl,iface);
1812 FIXME("(%p,%08lx,%ld):stub\n",This,(DWORD)hwnd,level);
1814 This->priolevel = level;
1819 static HRESULT WINAPI IDirectSoundImpl_CreateSoundBuffer(
1820 LPDIRECTSOUND iface,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER ppdsb,LPUNKNOWN lpunk
1822 ICOM_THIS(IDirectSoundImpl,iface);
1823 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
1824 LPWAVEFORMATEX wfex;
1825 HRESULT err = DS_OK;
1827 TRACE("(%p,%p,%p,%p)\n",This,dsbd,ippdsb,lpunk);
1829 if ((This == NULL) || (dsbd == NULL) || (ippdsb == NULL))
1830 return DSERR_INVALIDPARAM;
1832 if (TRACE_ON(dsound)) {
1833 TRACE("(structsize=%ld)\n",dsbd->dwSize);
1834 TRACE("(flags=0x%08lx:\n",dsbd->dwFlags);
1835 _dump_DSBCAPS(dsbd->dwFlags);
1837 TRACE("(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
1838 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
1841 wfex = dsbd->lpwfxFormat;
1844 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1845 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1846 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1847 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1848 wfex->wBitsPerSample, wfex->cbSize);
1850 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1852 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
1853 *ippdsb = primarybuf;
1854 primarybuf->dsbd.dwFlags = dsbd->dwFlags;
1856 } /* Else create primary buffer */
1859 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
1860 if (*ippdsb == NULL)
1861 return DSERR_OUTOFMEMORY;
1862 ICOM_VTBL(*ippdsb) = &dsbvt;
1864 (*ippdsb)->dsound = This;
1865 (*ippdsb)->parent = NULL;
1866 (*ippdsb)->buffer = NULL;
1868 memcpy(&((*ippdsb)->dsbd),dsbd,sizeof(*dsbd));
1869 if (dsbd->lpwfxFormat)
1870 memcpy(&((*ippdsb)->wfx), dsbd->lpwfxFormat, sizeof((*ippdsb)->wfx));
1872 TRACE("Created buffer at %p\n", *ippdsb);
1874 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1875 (*ippdsb)->buflen = dsound->wfx.nAvgBytesPerSec;
1876 (*ippdsb)->freq = dsound->wfx.nSamplesPerSec;
1878 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
1881 err = IDsDriver_CreateSoundBuffer(This->driver,wfex,dsbd->dwFlags,0,
1882 &((*ippdsb)->buflen),&((*ippdsb)->buffer),
1883 (LPVOID*)&((*ippdsb)->hwbuf));
1886 err = DSOUND_PrimaryOpen(*ippdsb);
1891 (*ippdsb)->buflen = dsbd->dwBufferBytes;
1892 (*ippdsb)->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1894 /* Check necessary hardware mixing capabilities */
1895 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1896 else capf |= DSCAPS_SECONDARYMONO;
1897 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1898 else capf |= DSCAPS_SECONDARY8BIT;
1899 use_hw = (This->drvcaps.dwFlags & capf) == capf;
1901 /* FIXME: check hardware sample rate mixing capabilities */
1902 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1903 /* FIXME: check whether any hardware buffers are left */
1904 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1906 /* Allocate system memory if applicable */
1907 if ((This->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1908 (*ippdsb)->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,(*ippdsb)->buflen);
1909 if ((*ippdsb)->buffer == NULL)
1910 err = DSERR_OUTOFMEMORY;
1913 /* Allocate the hardware buffer */
1914 if (use_hw && (err == DS_OK)) {
1915 err = IDsDriver_CreateSoundBuffer(This->driver,wfex,dsbd->dwFlags,0,
1916 &((*ippdsb)->buflen),&((*ippdsb)->buffer),
1917 (LPVOID*)&((*ippdsb)->hwbuf));
1922 if ((*ippdsb)->buffer)
1923 HeapFree(GetProcessHeap(),0,(*ippdsb)->buffer);
1924 HeapFree(GetProcessHeap(),0,(*ippdsb));
1928 /* calculate fragment size and write lead */
1929 DSOUND_RecalcFormat(*ippdsb);
1931 /* It's not necessary to initialize values to zero since */
1932 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1933 (*ippdsb)->playpos = 0;
1934 (*ippdsb)->mixpos = 0;
1935 (*ippdsb)->state = STATE_STOPPED;
1936 DSOUND_RecalcVolPan(&((*ippdsb)->volpan));
1938 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1939 (*ippdsb)->freqAdjust = ((*ippdsb)->freq << DSOUND_FREQSHIFT) /
1940 primarybuf->wfx.nSamplesPerSec;
1941 (*ippdsb)->nAvgBytesPerSec = (*ippdsb)->freq *
1942 dsbd->lpwfxFormat->nBlockAlign;
1945 EnterCriticalSection(&(This->lock));
1946 /* register buffer */
1947 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1948 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl*)*(This->nrofbuffers+1));
1950 This->buffers = newbuffers;
1951 This->buffers[This->nrofbuffers] = *ippdsb;
1952 This->nrofbuffers++;
1953 TRACE("buffer count is now %d\n", This->nrofbuffers);
1955 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
1956 err = DSERR_OUTOFMEMORY;
1959 LeaveCriticalSection(&(This->lock));
1961 IDirectSound_AddRef(iface);
1963 InitializeCriticalSection(&((*ippdsb)->lock));
1967 IDirectSoundBuffer_Release(*ppdsb);
1973 if (dsbd->dwFlags & DSBCAPS_CTRL3D) {
1974 IDirectSound3DBufferImpl *ds3db;
1976 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
1978 ICOM_VTBL(ds3db) = &ds3dbvt;
1980 (*ippdsb)->ds3db = ds3db;
1982 ds3db->dsb = (*ippdsb);
1983 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER)ippdsb);
1985 InitializeCriticalSection(&ds3db->lock);
1987 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1988 ds3db->ds3db.vPosition.x.x = 0.0;
1989 ds3db->ds3db.vPosition.y.y = 0.0;
1990 ds3db->ds3db.vPosition.z.z = 0.0;
1991 ds3db->ds3db.vVelocity.x.x = 0.0;
1992 ds3db->ds3db.vVelocity.y.y = 0.0;
1993 ds3db->ds3db.vVelocity.z.z = 0.0;
1994 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1995 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1996 ds3db->ds3db.vConeOrientation.x.x = 0.0;
1997 ds3db->ds3db.vConeOrientation.y.y = 0.0;
1998 ds3db->ds3db.vConeOrientation.z.z = 0.0;
1999 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
2000 ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
2001 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
2002 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
2003 ds3db->buflen = ((*ippdsb)->buflen * primarybuf->wfx.nBlockAlign) /
2004 (*ippdsb)->wfx.nBlockAlign;
2005 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
2006 if (ds3db->buffer == NULL) {
2008 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
2015 static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer(
2016 LPDIRECTSOUND iface,LPDIRECTSOUNDBUFFER pdsb,LPLPDIRECTSOUNDBUFFER ppdsb
2018 ICOM_THIS(IDirectSoundImpl,iface);
2019 IDirectSoundBufferImpl* ipdsb=(IDirectSoundBufferImpl*)pdsb;
2020 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
2021 TRACE("(%p,%p,%p)\n",This,ipdsb,ippdsb);
2024 FIXME("need to duplicate hardware buffer\n");
2027 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
2029 IDirectSoundBuffer_AddRef(pdsb);
2030 memcpy(*ippdsb, ipdsb, sizeof(IDirectSoundBufferImpl));
2032 (*ippdsb)->playpos = 0;
2033 (*ippdsb)->mixpos = 0;
2034 (*ippdsb)->dsound = This;
2035 (*ippdsb)->parent = ipdsb;
2036 memcpy(&((*ippdsb)->wfx), &(ipdsb->wfx), sizeof((*ippdsb)->wfx));
2037 InitializeCriticalSection(&(*ippdsb)->lock);
2038 /* register buffer */
2039 EnterCriticalSection(&(This->lock));
2041 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
2043 This->buffers = newbuffers;
2044 This->buffers[This->nrofbuffers] = *ippdsb;
2045 This->nrofbuffers++;
2046 TRACE("buffer count is now %d\n", This->nrofbuffers);
2048 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
2049 /* FIXME: release buffer */
2052 LeaveCriticalSection(&(This->lock));
2053 IDirectSound_AddRef(iface);
2058 static HRESULT WINAPI IDirectSoundImpl_GetCaps(LPDIRECTSOUND iface,LPDSCAPS caps) {
2059 ICOM_THIS(IDirectSoundImpl,iface);
2060 TRACE("(%p,%p)\n",This,caps);
2061 TRACE("(flags=0x%08lx)\n",caps->dwFlags);
2064 return DSERR_INVALIDPARAM;
2066 /* We should check this value, not set it. See Inside DirectX, p215. */
2067 caps->dwSize = sizeof(*caps);
2069 caps->dwFlags = This->drvcaps.dwFlags;
2071 /* FIXME: copy caps from This->drvcaps */
2072 caps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
2073 caps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
2075 caps->dwPrimaryBuffers = 1;
2077 caps->dwMaxHwMixingAllBuffers = 0;
2078 caps->dwMaxHwMixingStaticBuffers = 0;
2079 caps->dwMaxHwMixingStreamingBuffers = 0;
2081 caps->dwFreeHwMixingAllBuffers = 0;
2082 caps->dwFreeHwMixingStaticBuffers = 0;
2083 caps->dwFreeHwMixingStreamingBuffers = 0;
2085 caps->dwMaxHw3DAllBuffers = 0;
2086 caps->dwMaxHw3DStaticBuffers = 0;
2087 caps->dwMaxHw3DStreamingBuffers = 0;
2089 caps->dwFreeHw3DAllBuffers = 0;
2090 caps->dwFreeHw3DStaticBuffers = 0;
2091 caps->dwFreeHw3DStreamingBuffers = 0;
2093 caps->dwTotalHwMemBytes = 0;
2095 caps->dwFreeHwMemBytes = 0;
2097 caps->dwMaxContigFreeHwMemBytes = 0;
2099 caps->dwUnlockTransferRateHwBuffers = 4096; /* But we have none... */
2101 caps->dwPlayCpuOverheadSwBuffers = 1; /* 1% */
2106 static ULONG WINAPI IDirectSoundImpl_AddRef(LPDIRECTSOUND iface) {
2107 ICOM_THIS(IDirectSoundImpl,iface);
2108 return ++(This->ref);
2111 static ULONG WINAPI IDirectSoundImpl_Release(LPDIRECTSOUND iface) {
2112 ICOM_THIS(IDirectSoundImpl,iface);
2113 TRACE("(%p), ref was %ld\n",This,This->ref);
2114 if (!--(This->ref)) {
2117 timeKillEvent(This->timerID);
2118 timeEndPeriod(DS_TIME_RES);
2121 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)primarybuf);
2123 if (This->buffers) {
2124 for( i=0;i<This->nrofbuffers;i++)
2125 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->buffers[i]);
2129 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->primary);
2131 DeleteCriticalSection(&This->lock);
2133 IDsDriver_Close(This->driver);
2136 for (c=0; c<DS_HEL_FRAGS; c++)
2137 HeapFree(GetProcessHeap(),0,This->pwave[c]);
2139 if (This->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
2140 waveOutClose(This->hwo);
2143 IDsDriver_Release(This->driver);
2145 HeapFree(GetProcessHeap(),0,This);
2152 static HRESULT WINAPI IDirectSoundImpl_SetSpeakerConfig(
2153 LPDIRECTSOUND iface,DWORD config
2155 ICOM_THIS(IDirectSoundImpl,iface);
2156 FIXME("(%p,0x%08lx):stub\n",This,config);
2160 static HRESULT WINAPI IDirectSoundImpl_QueryInterface(
2161 LPDIRECTSOUND iface,REFIID riid,LPVOID *ppobj
2163 ICOM_THIS(IDirectSoundImpl,iface);
2165 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
2167 if (This->listener) {
2168 *ppobj = This->listener;
2169 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This->listener);
2173 This->listener = (IDirectSound3DListenerImpl*)HeapAlloc(
2174 GetProcessHeap(), 0, sizeof(*(This->listener)));
2175 This->listener->ref = 1;
2176 ICOM_VTBL(This->listener) = &ds3dlvt;
2177 *ppobj = (LPVOID)This->listener;
2178 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
2180 This->listener->dsb = NULL;
2182 This->listener->ds3dl.dwSize = sizeof(DS3DLISTENER);
2183 This->listener->ds3dl.vPosition.u1.x = 0.0;
2184 This->listener->ds3dl.vPosition.u2.y = 0.0;
2185 This->listener->ds3dl.vPosition.u3.z = 0.0;
2186 This->listener->ds3dl.vVelocity.u1.x = 0.0;
2187 This->listener->ds3dl.vVelocity.u2.y = 0.0;
2188 This->listener->ds3dl.vVelocity.u3.z = 0.0;
2189 This->listener->ds3dl.vOrientFront.u1.x = 0.0;
2190 This->listener->ds3dl.vOrientFront.u2.y = 0.0;
2191 This->listener->ds3dl.vOrientFront.u3.z = 1.0;
2192 This->listener->ds3dl.vOrientTop.u1.x = 0.0;
2193 This->listener->ds3dl.vOrientTop.u2.y = 1.0;
2194 This->listener->ds3dl.vOrientTop.u3.z = 0.0;
2195 This->listener->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
2196 This->listener->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
2197 This->listener->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
2199 InitializeCriticalSection(&This->listener->lock);
2204 FIXME("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
2208 static HRESULT WINAPI IDirectSoundImpl_Compact(
2209 LPDIRECTSOUND iface)
2211 ICOM_THIS(IDirectSoundImpl,iface);
2212 TRACE("(%p)\n", This);
2216 static HRESULT WINAPI IDirectSoundImpl_GetSpeakerConfig(
2217 LPDIRECTSOUND iface,
2218 LPDWORD lpdwSpeakerConfig)
2220 ICOM_THIS(IDirectSoundImpl,iface);
2221 TRACE("(%p, %p)\n", This, lpdwSpeakerConfig);
2222 *lpdwSpeakerConfig = DSSPEAKER_STEREO | (DSSPEAKER_GEOMETRY_NARROW << 16);
2226 static HRESULT WINAPI IDirectSoundImpl_Initialize(
2227 LPDIRECTSOUND iface,
2230 ICOM_THIS(IDirectSoundImpl,iface);
2231 TRACE("(%p, %p)\n", This, lpcGuid);
2235 static ICOM_VTABLE(IDirectSound) dsvt =
2237 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2238 IDirectSoundImpl_QueryInterface,
2239 IDirectSoundImpl_AddRef,
2240 IDirectSoundImpl_Release,
2241 IDirectSoundImpl_CreateSoundBuffer,
2242 IDirectSoundImpl_GetCaps,
2243 IDirectSoundImpl_DuplicateSoundBuffer,
2244 IDirectSoundImpl_SetCooperativeLevel,
2245 IDirectSoundImpl_Compact,
2246 IDirectSoundImpl_GetSpeakerConfig,
2247 IDirectSoundImpl_SetSpeakerConfig,
2248 IDirectSoundImpl_Initialize
2252 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
2256 LPDSBPOSITIONNOTIFY event;
2258 if (dsb->nrofnotifies == 0)
2261 TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
2262 dsb, dsb->buflen, dsb->playpos, len);
2263 for (i = 0; i < dsb->nrofnotifies ; i++) {
2264 event = dsb->notifies + i;
2265 offset = event->dwOffset;
2266 TRACE("checking %d, position %ld, event = %d\n",
2267 i, offset, event->hEventNotify);
2268 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
2269 /* OK. [Inside DirectX, p274] */
2271 /* This also means we can't sort the entries by offset, */
2272 /* because DSBPN_OFFSETSTOP == -1 */
2273 if (offset == DSBPN_OFFSETSTOP) {
2274 if (dsb->state == STATE_STOPPED) {
2275 SetEvent(event->hEventNotify);
2276 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2281 if ((dsb->playpos + len) >= dsb->buflen) {
2282 if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
2283 (offset >= dsb->playpos)) {
2284 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2285 SetEvent(event->hEventNotify);
2288 if ((offset >= dsb->playpos) && (offset < (dsb->playpos + len))) {
2289 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2290 SetEvent(event->hEventNotify);
2296 /* WAV format info can be found at: */
2298 /* http://www.cwi.nl/ftp/audio/AudioFormats.part2 */
2299 /* ftp://ftp.cwi.nl/pub/audio/RIFF-format */
2301 /* Import points to remember: */
2303 /* 8-bit WAV is unsigned */
2304 /* 16-bit WAV is signed */
2306 static inline INT16 cvtU8toS16(BYTE byte)
2308 INT16 s = (byte - 128) << 8;
2313 static inline BYTE cvtS16toU8(INT16 word)
2315 BYTE b = (word + 32768) >> 8;
2321 /* We should be able to optimize these two inline functions */
2322 /* so that we aren't doing 8->16->8 conversions when it is */
2323 /* not necessary. But this is still a WIP. Optimize later. */
2324 static inline void get_fields(const IDirectSoundBufferImpl *dsb, BYTE *buf, INT *fl, INT *fr)
2326 INT16 *bufs = (INT16 *) buf;
2328 /* TRACE("(%p)", buf); */
2329 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 2) {
2330 *fl = cvtU8toS16(*buf);
2331 *fr = cvtU8toS16(*(buf + 1));
2335 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 2) {
2341 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 1) {
2342 *fl = cvtU8toS16(*buf);
2347 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 1) {
2353 FIXME("get_fields found an unsupported configuration\n");
2357 static inline void set_fields(BYTE *buf, INT fl, INT fr)
2359 INT16 *bufs = (INT16 *) buf;
2361 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 2)) {
2362 *buf = cvtS16toU8(fl);
2363 *(buf + 1) = cvtS16toU8(fr);
2367 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 2)) {
2373 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 1)) {
2374 *buf = cvtS16toU8((fl + fr) >> 1);
2378 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 1)) {
2379 *bufs = (fl + fr) >> 1;
2382 FIXME("set_fields found an unsupported configuration\n");
2386 /* Now with PerfectPitch (tm) technology */
2387 static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2389 INT i, size, ipos, ilen, fieldL, fieldR;
2391 INT iAdvance = dsb->wfx.nBlockAlign;
2392 INT oAdvance = primarybuf->wfx.nBlockAlign;
2394 ibp = dsb->buffer + dsb->mixpos;
2397 TRACE("(%p, %p, %p), mixpos=%ld\n", dsb, ibp, obp, dsb->mixpos);
2398 /* Check for the best case */
2399 if ((dsb->freq == primarybuf->wfx.nSamplesPerSec) &&
2400 (dsb->wfx.wBitsPerSample == primarybuf->wfx.wBitsPerSample) &&
2401 (dsb->wfx.nChannels == primarybuf->wfx.nChannels)) {
2402 DWORD bytesleft = dsb->buflen - dsb->mixpos;
2403 TRACE("(%p) Best case\n", dsb);
2404 if (len <= bytesleft )
2405 memcpy(obp, ibp, len);
2407 memcpy(obp, ibp, bytesleft );
2408 memcpy(obp + bytesleft, dsb->buffer, len - bytesleft);
2413 /* Check for same sample rate */
2414 if (dsb->freq == primarybuf->wfx.nSamplesPerSec) {
2415 TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb,
2416 dsb->freq, primarybuf->wfx.nSamplesPerSec);
2418 for (i = 0; i < len; i += oAdvance) {
2419 get_fields(dsb, ibp, &fieldL, &fieldR);
2422 set_fields(obp, fieldL, fieldR);
2424 if (ibp >= (BYTE *)(dsb->buffer + dsb->buflen))
2425 ibp = dsb->buffer; /* wrap */
2430 /* Mix in different sample rates */
2432 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
2433 /* Patent Pending :-] */
2435 TRACE("(%p) Adjusting frequency: %ld -> %ld\n",
2436 dsb, dsb->freq, primarybuf->wfx.nSamplesPerSec);
2438 size = len / oAdvance;
2439 ilen = ((size * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance;
2440 for (i = 0; i < size; i++) {
2442 ipos = (((i * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance) + dsb->mixpos;
2444 if (ipos >= dsb->buflen)
2445 ipos %= dsb->buflen; /* wrap */
2447 get_fields(dsb, (dsb->buffer + ipos), &fieldL, &fieldR);
2448 set_fields(obp, fieldL, fieldR);
2454 static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2456 INT i, inc = primarybuf->wfx.wBitsPerSample >> 3;
2458 INT16 *bps = (INT16 *) buf;
2460 TRACE("(%p) left = %lx, right = %lx\n", dsb,
2461 dsb->volpan.dwTotalLeftAmpFactor, dsb->volpan.dwTotalRightAmpFactor);
2462 if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
2463 (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
2464 !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
2465 return; /* Nothing to do */
2467 /* If we end up with some bozo coder using panning or 3D sound */
2468 /* with a mono primary buffer, it could sound very weird using */
2469 /* this method. Oh well, tough patooties. */
2471 for (i = 0; i < len; i += inc) {
2477 /* 8-bit WAV is unsigned, but we need to operate */
2478 /* on signed data for this to work properly */
2480 val = ((val * (i & inc ? dsb->volpan.dwTotalRightAmpFactor : dsb->volpan.dwTotalLeftAmpFactor)) >> 16);
2485 /* 16-bit WAV is signed -- much better */
2487 val = ((val * ((i & inc) ? dsb->volpan.dwTotalRightAmpFactor : dsb->volpan.dwTotalLeftAmpFactor)) >> 16);
2493 FIXME("MixerVol had a nasty error\n");
2499 static void DSOUND_Mixer3D(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2502 DWORD buflen, mixpos;
2504 buflen = dsb->ds3db->buflen;
2505 mixpos = (dsb->mixpos * primarybuf->wfx.nBlockAlign) / dsb->wfx.nBlockAlign;
2506 ibp = dsb->ds3db->buffer + mixpos;
2509 if (mixpos > buflen) {
2510 FIXME("Major breakage");
2514 if (len <= (mixpos + buflen))
2515 memcpy(obp, ibp, len);
2517 memcpy(obp, ibp, buflen - mixpos);
2518 memcpy(obp + (buflen - mixpos),
2520 len - (buflen - mixpos));
2526 static void *tmp_buffer;
2527 static size_t tmp_buffer_len = 0;
2529 static void *DSOUND_tmpbuffer(size_t len)
2531 if (len>tmp_buffer_len) {
2532 void *new_buffer = realloc(tmp_buffer, len);
2534 tmp_buffer = new_buffer;
2535 tmp_buffer_len = len;
2542 static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
2544 INT i, len, ilen, temp, field;
2545 INT advance = primarybuf->wfx.wBitsPerSample >> 3;
2546 BYTE *buf, *ibuf, *obuf;
2547 INT16 *ibufs, *obufs;
2550 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2551 temp = MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->buflen,
2552 dsb->nAvgBytesPerSec) -
2553 MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->mixpos,
2554 dsb->nAvgBytesPerSec);
2555 len = (len > temp) ? temp : len;
2557 len &= ~3; /* 4 byte alignment */
2560 /* This should only happen if we aren't looping and temp < 4 */
2562 /* We skip the remainder, so check for possible events */
2563 DSOUND_CheckEvent(dsb, dsb->buflen - dsb->mixpos);
2565 dsb->state = STATE_STOPPED;
2568 dsb->leadin = FALSE;
2569 /* Check for DSBPN_OFFSETSTOP */
2570 DSOUND_CheckEvent(dsb, 0);
2574 /* Been seeing segfaults in malloc() for some reason... */
2575 TRACE("allocating buffer (size = %d)\n", len);
2576 if ((buf = ibuf = (BYTE *) DSOUND_tmpbuffer(len)) == NULL)
2579 TRACE("MixInBuffer (%p) len = %d, dest = %ld\n", dsb, len, writepos);
2581 ilen = DSOUND_MixerNorm(dsb, ibuf, len);
2582 if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
2583 (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
2584 DSOUND_MixerVol(dsb, ibuf, len);
2586 obuf = primarybuf->buffer + writepos;
2587 for (i = 0; i < len; i += advance) {
2588 obufs = (INT16 *) obuf;
2589 ibufs = (INT16 *) ibuf;
2590 if (primarybuf->wfx.wBitsPerSample == 8) {
2591 /* 8-bit WAV is unsigned */
2592 field = (*ibuf - 128);
2593 field += (*obuf - 128);
2594 field = field > 127 ? 127 : field;
2595 field = field < -128 ? -128 : field;
2596 *obuf = field + 128;
2598 /* 16-bit WAV is signed */
2601 field = field > 32767 ? 32767 : field;
2602 field = field < -32768 ? -32768 : field;
2607 if (obuf >= (BYTE *)(primarybuf->buffer + primarybuf->buflen))
2608 obuf = primarybuf->buffer;
2612 if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY)
2613 DSOUND_CheckEvent(dsb, ilen);
2615 if (dsb->leadin && (dsb->startpos > dsb->mixpos) && (dsb->startpos <= dsb->mixpos + ilen)) {
2616 /* HACK... leadin should be reset when the PLAY position reaches the startpos,
2617 * not the MIX position... but if the sound buffer is bigger than our prebuffering
2618 * (which must be the case for the streaming buffers that need this hack anyway)
2619 * plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
2620 dsb->leadin = FALSE;
2623 dsb->mixpos += ilen;
2625 if (dsb->mixpos >= dsb->buflen) {
2626 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2627 dsb->state = STATE_STOPPED;
2630 dsb->leadin = FALSE;
2631 DSOUND_CheckEvent(dsb, 0); /* For DSBPN_OFFSETSTOP */
2634 while (dsb->mixpos >= dsb->buflen)
2635 dsb->mixpos -= dsb->buflen;
2636 if (dsb->leadin && (dsb->startpos <= dsb->mixpos))
2637 dsb->leadin = FALSE; /* HACK: see above */
2644 static DWORD WINAPI DSOUND_MixPrimary(DWORD writepos, DWORD fraglen, BOOL starting)
2646 INT i, len, maxlen = 0;
2647 IDirectSoundBufferImpl *dsb;
2649 TRACE("(%ld,%ld,%d)\n", writepos, fraglen, starting);
2650 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
2651 dsb = dsound->buffers[i];
2653 if (!dsb || !(ICOM_VTBL(dsb)))
2655 if (dsb->buflen && dsb->state && !(starting && (dsb->state != STATE_STARTING))) {
2656 TRACE("Checking %p\n", dsb);
2657 EnterCriticalSection(&(dsb->lock));
2658 if (dsb->state == STATE_STOPPING) {
2659 /* FIXME: perhaps attempt to remove the buffer from the prebuffer */
2660 dsb->state = STATE_STOPPED;
2662 len = DSOUND_MixInBuffer(dsb, writepos, fraglen);
2663 maxlen = len > maxlen ? len : maxlen;
2665 LeaveCriticalSection(&(dsb->lock));
2672 static void WINAPI DSOUND_MarkPlaying(void)
2675 IDirectSoundBufferImpl *dsb;
2677 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
2678 dsb = dsound->buffers[i];
2680 if (!dsb || !(ICOM_VTBL(dsb)))
2682 if (dsb->buflen && (dsb->state == STATE_STARTING))
2683 dsb->state = STATE_PLAYING;
2687 static void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
2693 if (!dsound || !primarybuf) {
2694 ERR("dsound died without killing us?\n");
2695 timeKillEvent(timerID);
2696 timeEndPeriod(DS_TIME_RES);
2700 EnterCriticalSection(&(dsound->lock));
2702 if (!primarybuf || !primarybuf->ref) {
2703 /* seems the primary buffer is currently being released */
2704 LeaveCriticalSection(&(dsound->lock));
2708 /* the sound of silence */
2709 nfiller = primarybuf->wfx.wBitsPerSample == 8 ? 128 : 0;
2711 /* whether the primary is forced to play even without secondary buffers */
2712 forced = ((primarybuf->state == STATE_PLAYING) || (primarybuf->state == STATE_STARTING));
2714 if (primarybuf->hwbuf) {
2715 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2716 BOOL paused = ((primarybuf->state == STATE_STOPPED) || (primarybuf->state == STATE_STARTING));
2717 DWORD playpos, writepos, inq, maxq, mixq, frag;
2718 IDsDriverBuffer_GetPosition(primarybuf->hwbuf, &playpos, &writepos);
2719 /* Well, we *could* do Just-In-Time mixing using the writepos,
2720 * but that's a little bit ambitious and unnecessary... */
2721 /* rather add our safety margin to the writepos, if we're playing */
2723 writepos += primarybuf->writelead;
2724 while (writepos >= primarybuf->buflen)
2725 writepos -= primarybuf->buflen;
2726 } else writepos = playpos;
2727 TRACE("primary playpos=%ld, writepos=%ld, clrpos=%ld, mixpos=%ld\n",
2728 playpos,writepos,primarybuf->playpos,primarybuf->mixpos);
2729 /* wipe out just-played sound data */
2730 if (playpos < primarybuf->playpos) {
2731 memset(primarybuf->buffer + primarybuf->playpos, nfiller, primarybuf->buflen - primarybuf->playpos);
2732 memset(primarybuf->buffer, nfiller, playpos);
2734 memset(primarybuf->buffer + primarybuf->playpos, nfiller, playpos - primarybuf->playpos);
2736 primarybuf->playpos = playpos;
2738 /* check how much prebuffering is left */
2739 inq = primarybuf->mixpos;
2741 inq += primarybuf->buflen;
2744 /* find the maximum we can prebuffer */
2747 if (maxq < writepos)
2748 maxq += primarybuf->buflen;
2750 } else maxq = primarybuf->buflen;
2752 /* clip maxq to DS_HAL_QUEUE */
2753 frag = DS_HAL_QUEUE * dsound->fraglen;
2754 if (maxq > frag) maxq = frag;
2756 EnterCriticalSection(&(primarybuf->lock));
2758 /* check for consistency */
2760 /* the playback position must have passed our last
2761 * mixed position, i.e. it's an underrun, or we have
2762 * nothing more to play */
2764 /* stop the playback now, to allow buffers to refill */
2765 IDsDriverBuffer_Stop(primarybuf->hwbuf);
2766 if (primarybuf->state == STATE_PLAYING) {
2767 primarybuf->state = STATE_STARTING;
2769 else if (primarybuf->state == STATE_STOPPING) {
2770 primarybuf->state = STATE_STOPPED;
2773 /* how can we have an underrun if we aren't playing? */
2774 ERR("unexpected primary state (%ld)\n", primarybuf->state);
2776 /* the Stop is supposed to reset play position to beginning of buffer */
2777 /* unfortunately, OSS is not able to do so, so get current pointer */
2778 IDsDriverBuffer_GetPosition(primarybuf->hwbuf, &playpos, NULL);
2780 primarybuf->playpos = playpos;
2781 primarybuf->mixpos = playpos;
2783 maxq = primarybuf->buflen;
2784 if (maxq > frag) maxq = frag;
2785 memset(primarybuf->buffer, nfiller, primarybuf->buflen);
2789 /* see if some new buffers have been started that we want to merge into our prebuffer;
2790 * this should minimize latency even when we have a large prebuffer */
2792 if (primarybuf->mixpos < writepos) {
2793 /* mix to end of buffer */
2794 len = DSOUND_MixPrimary(writepos, primarybuf->buflen - writepos, TRUE);
2795 if ((len + writepos) < primarybuf->buflen)
2796 goto addmix_complete;
2797 /* mix from beginning of buffer */
2798 if (primarybuf->mixpos)
2799 len = DSOUND_MixPrimary(0, primarybuf->mixpos, TRUE);
2801 /* mix middle of buffer */
2802 len = DSOUND_MixPrimary(writepos, primarybuf->mixpos - writepos, TRUE);
2806 DSOUND_MarkPlaying();
2809 TRACE("queued %ld, max %ld, mixing %ld, paused %d\n", inq, maxq, mixq, paused);
2810 /* it's too inefficient to mix less than a fragment at a time */
2811 if (mixq >= dsound->fraglen) {
2813 #define FRAG_MIXER \
2814 if (frag > mixq) frag = mixq; \
2815 len = DSOUND_MixPrimary(primarybuf->mixpos, frag, FALSE); \
2816 if (forced) len = frag; \
2817 primarybuf->mixpos += len; \
2818 mixq -= len; inq += len
2820 if ((playpos < writepos) || (paused && (playpos == writepos))) {
2821 if (primarybuf->mixpos) {
2822 /* mix to end of buffer */
2823 frag = primarybuf->buflen - primarybuf->mixpos;
2825 if (primarybuf->mixpos < primarybuf->buflen)
2827 primarybuf->mixpos = 0;
2829 if (mixq >= dsound->fraglen) {
2830 /* mix from beginning of buffer */
2832 if ((!frag) && paused) frag = primarybuf->buflen;
2836 else if (playpos > writepos) {
2837 /* mix middle of buffer */
2838 frag = playpos - primarybuf->mixpos;
2842 /* this should preferably not happen... */
2843 ERR("mixer malfunction (ambiguous writepos)!\n");
2849 /* buffers have been filled, restart playback */
2850 if (primarybuf->state == STATE_STARTING) {
2851 IDsDriverBuffer_Play(primarybuf->hwbuf, 0, 0, DSBPLAY_LOOPING);
2852 primarybuf->state = STATE_PLAYING;
2854 else if (primarybuf->state == STATE_STOPPED) {
2855 /* the primarybuf is supposed to play if there's something to play
2856 * even if it is reported as stopped, so don't let this confuse you */
2857 IDsDriverBuffer_Play(primarybuf->hwbuf, 0, 0, DSBPLAY_LOOPING);
2858 primarybuf->state = STATE_STOPPING;
2861 LeaveCriticalSection(&(primarybuf->lock));
2863 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
2864 if (primarybuf->state == STATE_STARTING) {
2865 IDsDriverBuffer_Play(primarybuf->hwbuf, 0, 0, DSBPLAY_LOOPING);
2866 primarybuf->state = STATE_PLAYING;
2868 else if (primarybuf->state == STATE_STOPPING) {
2869 IDsDriverBuffer_Stop(primarybuf->hwbuf);
2870 primarybuf->state = STATE_STOPPED;
2874 /* using waveOut stuff */
2875 /* if no buffers are playing, we should be in pause mode now */
2877 /* clean out completed fragments */
2878 while (dsound->pwqueue && (dsound->pwave[dsound->pwplay]->dwFlags & WHDR_DONE)) {
2879 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2880 DWORD pos = dsound->pwplay * dsound->fraglen;
2881 TRACE("done playing primary pos=%ld\n",pos);
2882 memset(primarybuf->buffer + pos, nfiller, dsound->fraglen);
2885 if (dsound->pwplay >= DS_HEL_FRAGS) dsound->pwplay = 0;
2888 primarybuf->playpos = dsound->pwplay * dsound->fraglen;
2889 TRACE("primary playpos=%ld, mixpos=%ld\n",primarybuf->playpos,primarybuf->mixpos);
2890 EnterCriticalSection(&(primarybuf->lock));
2891 if (!dsound->pwqueue) {
2892 /* this is either an underrun or we have nothing more to play...
2893 * since playback has already stopped now, we can enter pause mode,
2894 * in order to allow buffers to refill */
2895 if (primarybuf->state == STATE_PLAYING) {
2896 waveOutPause(dsound->hwo);
2897 primarybuf->state = STATE_STARTING;
2899 else if (primarybuf->state == STATE_STOPPING) {
2900 waveOutPause(dsound->hwo);
2901 primarybuf->state = STATE_STOPPED;
2905 /* find next write position, plus some extra margin */
2906 writepos = primarybuf->playpos + DS_HEL_MARGIN * dsound->fraglen;
2907 while (writepos >= primarybuf->buflen) writepos -= primarybuf->buflen;
2909 /* see if some new buffers have been started that we want to merge into our prebuffer;
2910 * this should minimize latency even when we have a large prebuffer */
2911 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2912 if ((primarybuf->state == STATE_PLAYING) || (primarybuf->state == STATE_STOPPING)) {
2913 while (writepos != primarybuf->mixpos) {
2914 len = DSOUND_MixPrimary(writepos, dsound->fraglen, TRUE);
2916 writepos += dsound->fraglen;
2917 if (writepos >= primarybuf->buflen)
2918 writepos -= primarybuf->buflen;
2921 DSOUND_MarkPlaying();
2924 /* we want at least DS_HEL_QUEUE fragments in the prebuffer outqueue;
2925 * mix a bunch of fragments now as necessary */
2926 while (dsound->pwqueue < DS_HEL_QUEUE) {
2927 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2928 len = DSOUND_MixPrimary(primarybuf->mixpos, dsound->fraglen, FALSE);
2930 if (forced) len = dsound->fraglen;
2931 /* if we have nothing to play, don't bother to */
2933 if (len < dsound->fraglen) {
2934 TRACE("len=%ld is less than fraglen=%ld\n",len,dsound->fraglen);
2936 /* ok, we have something to play */
2937 /* advance mix positions */
2938 primarybuf->mixpos += dsound->fraglen;
2939 if (primarybuf->mixpos >= primarybuf->buflen)
2940 primarybuf->mixpos -= primarybuf->buflen;
2942 waveOutWrite(dsound->hwo, dsound->pwave[dsound->pwwrite], sizeof(WAVEHDR));
2944 if (dsound->pwwrite >= DS_HEL_FRAGS) dsound->pwwrite = 0;
2947 if (dsound->pwqueue) {
2948 /* buffers have been filled, restart playback */
2949 if (primarybuf->state == STATE_STARTING) {
2950 waveOutRestart(dsound->hwo);
2951 primarybuf->state = STATE_PLAYING;
2953 else if (primarybuf->state == STATE_STOPPED) {
2954 /* the primarybuf is supposed to play if there's something to play
2955 * even if it is reported as stopped, so don't let this confuse you */
2956 waveOutRestart(dsound->hwo);
2957 primarybuf->state = STATE_STOPPING;
2960 LeaveCriticalSection(&(primarybuf->lock));
2962 LeaveCriticalSection(&(dsound->lock));
2965 /*******************************************************************************
2968 HRESULT WINAPI DirectSoundCreate(REFGUID lpGUID,LPDIRECTSOUND *ppDS,IUnknown *pUnkOuter )
2970 IDirectSoundImpl** ippDS=(IDirectSoundImpl**)ppDS;
2971 PIDSDRIVER drv = NULL;
2974 HRESULT err = DS_OK;
2977 TRACE("(%p,%p,%p)\n",lpGUID,ippDS,pUnkOuter);
2979 TRACE("DirectSoundCreate (%p)\n", ippDS);
2982 return DSERR_INVALIDPARAM;
2985 IDirectSound_AddRef((LPDIRECTSOUND)dsound);
2990 /* Enumerate WINMM audio devices and find the one we want */
2991 wodn = waveOutGetNumDevs();
2992 if (!wodn) return DSERR_NODRIVER;
2994 /* FIXME: How do we find the GUID of an audio device? */
2995 /* Well, just use the first available device for now... */
2997 /* Get output device caps */
2998 waveOutGetDevCapsA(wod, &wcaps, sizeof(wcaps));
2999 /* 0x810 is a "Wine extension" to get the DSound interface */
3000 waveOutMessage(wod, 0x810, (DWORD)&drv, 0);
3002 /* Allocate memory */
3003 *ippDS = (IDirectSoundImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundImpl));
3005 return DSERR_OUTOFMEMORY;
3007 ICOM_VTBL(*ippDS) = &dsvt;
3010 (*ippDS)->driver = drv;
3011 (*ippDS)->fraglen = 0;
3012 (*ippDS)->priolevel = DSSCL_NORMAL;
3013 (*ippDS)->nrofbuffers = 0;
3014 (*ippDS)->buffers = NULL;
3015 (*ippDS)->primary = NULL;
3016 (*ippDS)->listener = NULL;
3018 /* Get driver description */
3020 IDsDriver_GetDriverDesc(drv,&((*ippDS)->drvdesc));
3022 /* if no DirectSound interface available, use WINMM API instead */
3023 (*ippDS)->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
3024 (*ippDS)->drvdesc.dnDevNode = wod; /* FIXME? */
3027 /* Set default wave format (may need it for waveOutOpen) */
3028 (*ippDS)->wfx.wFormatTag = WAVE_FORMAT_PCM;
3029 (*ippDS)->wfx.nChannels = 2;
3030 (*ippDS)->wfx.nSamplesPerSec = 22050;
3031 (*ippDS)->wfx.nAvgBytesPerSec = 44100;
3032 (*ippDS)->wfx.nBlockAlign = 2;
3033 (*ippDS)->wfx.wBitsPerSample = 8;
3035 /* If the driver requests being opened through MMSYSTEM
3036 * (which is recommended by the DDK), it is supposed to happen
3037 * before the DirectSound interface is opened */
3038 if ((*ippDS)->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
3039 /* FIXME: is this right? */
3040 err = mmErr(waveOutOpen(&((*ippDS)->hwo), (*ippDS)->drvdesc.dnDevNode, &((*ippDS)->wfx),
3041 0, 0, CALLBACK_NULL | WAVE_DIRECTSOUND));
3044 if (drv && (err == DS_OK))
3045 err = IDsDriver_Open(drv);
3047 /* FIXME: do we want to handle a temporarily busy device? */
3049 HeapFree(GetProcessHeap(),0,*ippDS);
3054 /* the driver is now open, so it's now allowed to call GetCaps */
3056 IDsDriver_GetCaps(drv,&((*ippDS)->drvcaps));
3060 /* FIXME: look at wcaps */
3061 (*ippDS)->drvcaps.dwFlags =
3062 DSCAPS_PRIMARY16BIT | DSCAPS_PRIMARYSTEREO;
3064 (*ippDS)->drvcaps.dwFlags |= DSCAPS_EMULDRIVER;
3066 /* Allocate memory for HEL buffer headers */
3067 for (c=0; c<DS_HEL_FRAGS; c++) {
3068 (*ippDS)->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
3069 if (!(*ippDS)->pwave[c]) {
3070 /* Argh, out of memory */
3072 HeapFree(GetProcessHeap(),0,(*ippDS)->pwave[c]);
3073 waveOutClose((*ippDS)->hwo);
3074 HeapFree(GetProcessHeap(),0,*ippDS);
3076 return DSERR_OUTOFMEMORY;
3082 InitializeCriticalSection(&((*ippDS)->lock));
3086 if (primarybuf == NULL) {
3090 dsbd.dwSize = sizeof(DSBUFFERDESC);
3091 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_GETCURRENTPOSITION2;
3092 dsbd.dwBufferBytes = 0;
3093 dsbd.lpwfxFormat = &(dsound->wfx);
3094 hr = IDirectSound_CreateSoundBuffer(*ppDS, &dsbd, (LPDIRECTSOUNDBUFFER*)&primarybuf, NULL);
3098 /* dsound->primary is NULL - don't need to Release */
3099 dsound->primary = primarybuf;
3100 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
3102 timeBeginPeriod(DS_TIME_RES);
3103 dsound->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, DSOUND_timer,
3104 (DWORD)dsound, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
3109 /***************************************************************************
3110 * DirectSoundCaptureCreate [DSOUND.6]
3112 * Create and initialize a DirectSoundCapture interface
3116 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
3119 HRESULT WINAPI DirectSoundCaptureCreate(
3121 LPDIRECTSOUNDCAPTURE* lplpDSC,
3122 LPUNKNOWN pUnkOuter )
3124 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), lplpDSC, pUnkOuter);
3127 return DSERR_NOAGGREGATION;
3130 /* Default device? */
3132 return DSOUND_CreateDirectSoundCapture( (LPVOID*)lplpDSC );
3135 FIXME( "Unknown GUID %s\n", debugstr_guid(lpcGUID) );
3138 return DSERR_OUTOFMEMORY;
3141 /***************************************************************************
3142 * DirectSoundCaptureEnumerateA [DSOUND.7]
3144 * Enumerate all DirectSound drivers installed in the system
3148 * Failure: DSERR_INVALIDPARAM
3150 HRESULT WINAPI DirectSoundCaptureEnumerateA(
3151 LPDSENUMCALLBACKA lpDSEnumCallback,
3154 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
3156 if ( lpDSEnumCallback )
3157 lpDSEnumCallback(NULL,"WINE Primary Sound Capture Driver",
3158 "SoundCap",lpContext);
3164 /***************************************************************************
3165 * DirectSoundCaptureEnumerateW [DSOUND.8]
3167 * Enumerate all DirectSound drivers installed in the system
3171 * Failure: DSERR_INVALIDPARAM
3173 HRESULT WINAPI DirectSoundCaptureEnumerateW(
3174 LPDSENUMCALLBACKW lpDSEnumCallback,
3177 FIXME("(%p,%p):stub\n", lpDSEnumCallback, lpContext );
3182 DSOUND_CreateDirectSoundCapture( LPVOID* ppobj )
3184 *ppobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( IDirectSoundCaptureImpl ) );
3186 if ( *ppobj == NULL ) {
3187 return DSERR_OUTOFMEMORY;
3191 ICOM_THIS(IDirectSoundCaptureImpl,*ppobj);
3194 ICOM_VTBL(This) = &dscvt;
3196 InitializeCriticalSection( &This->lock );
3202 static HRESULT WINAPI
3203 IDirectSoundCaptureImpl_QueryInterface(
3204 LPDIRECTSOUNDCAPTURE iface,
3208 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3210 FIXME( "(%p)->(%s,%p): stub\n", This, debugstr_guid(riid), ppobj );
3216 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
3219 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3221 EnterCriticalSection( &This->lock );
3223 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3224 uRef = ++(This->ref);
3226 LeaveCriticalSection( &This->lock );
3232 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
3235 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3237 EnterCriticalSection( &This->lock );
3239 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3240 uRef = --(This->ref);
3242 LeaveCriticalSection( &This->lock );
3245 DeleteCriticalSection( &This->lock );
3246 HeapFree( GetProcessHeap(), 0, This );
3252 static HRESULT WINAPI
3253 IDirectSoundCaptureImpl_CreateCaptureBuffer(
3254 LPDIRECTSOUNDCAPTURE iface,
3255 LPCDSCBUFFERDESC lpcDSCBufferDesc,
3256 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
3260 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3262 TRACE( "(%p)->(%p,%p,%p)\n", This, lpcDSCBufferDesc, lplpDSCaptureBuffer, pUnk );
3265 return DSERR_INVALIDPARAM;
3268 hr = DSOUND_CreateDirectSoundCaptureBuffer( lpcDSCBufferDesc, (LPVOID*)lplpDSCaptureBuffer );
3273 static HRESULT WINAPI
3274 IDirectSoundCaptureImpl_GetCaps(
3275 LPDIRECTSOUNDCAPTURE iface,
3276 LPDSCCAPS lpDSCCaps )
3278 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3280 FIXME( "(%p)->(%p): stub\n", This, lpDSCCaps );
3285 static HRESULT WINAPI
3286 IDirectSoundCaptureImpl_Initialize(
3287 LPDIRECTSOUNDCAPTURE iface,
3290 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3292 FIXME( "(%p)->(%p): stub\n", This, lpcGUID );
3298 static ICOM_VTABLE(IDirectSoundCapture) dscvt =
3300 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3301 /* IUnknown methods */
3302 IDirectSoundCaptureImpl_QueryInterface,
3303 IDirectSoundCaptureImpl_AddRef,
3304 IDirectSoundCaptureImpl_Release,
3306 /* IDirectSoundCapture methods */
3307 IDirectSoundCaptureImpl_CreateCaptureBuffer,
3308 IDirectSoundCaptureImpl_GetCaps,
3309 IDirectSoundCaptureImpl_Initialize
3313 DSOUND_CreateDirectSoundCaptureBuffer( LPCDSCBUFFERDESC lpcDSCBufferDesc, LPVOID* ppobj )
3316 FIXME( "(%p,%p): ignoring lpcDSCBufferDesc\n", lpcDSCBufferDesc, ppobj );
3318 *ppobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( IDirectSoundCaptureBufferImpl ) );
3320 if ( *ppobj == NULL ) {
3321 return DSERR_OUTOFMEMORY;
3325 ICOM_THIS(IDirectSoundCaptureBufferImpl,*ppobj);
3328 ICOM_VTBL(This) = &dscbvt;
3330 InitializeCriticalSection( &This->lock );
3337 static HRESULT WINAPI
3338 IDirectSoundCaptureBufferImpl_QueryInterface(
3339 LPDIRECTSOUNDCAPTUREBUFFER iface,
3343 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3345 FIXME( "(%p)->(%s,%p): stub\n", This, debugstr_guid(riid), ppobj );
3351 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER iface )
3354 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3356 EnterCriticalSection( &This->lock );
3358 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3359 uRef = ++(This->ref);
3361 LeaveCriticalSection( &This->lock );
3367 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER iface )
3370 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3372 EnterCriticalSection( &This->lock );
3374 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3375 uRef = --(This->ref);
3377 LeaveCriticalSection( &This->lock );
3380 DeleteCriticalSection( &This->lock );
3381 HeapFree( GetProcessHeap(), 0, This );
3387 static HRESULT WINAPI
3388 IDirectSoundCaptureBufferImpl_GetCaps(
3389 LPDIRECTSOUNDCAPTUREBUFFER iface,
3390 LPDSCBCAPS lpDSCBCaps )
3392 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3394 FIXME( "(%p)->(%p): stub\n", This, lpDSCBCaps );
3399 static HRESULT WINAPI
3400 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
3401 LPDIRECTSOUNDCAPTUREBUFFER iface,
3402 LPDWORD lpdwCapturePosition,
3403 LPDWORD lpdwReadPosition )
3405 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3407 FIXME( "(%p)->(%p,%p): stub\n", This, lpdwCapturePosition, lpdwReadPosition );
3412 static HRESULT WINAPI
3413 IDirectSoundCaptureBufferImpl_GetFormat(
3414 LPDIRECTSOUNDCAPTUREBUFFER iface,
3415 LPWAVEFORMATEX lpwfxFormat,
3416 DWORD dwSizeAllocated,
3417 LPDWORD lpdwSizeWritten )
3419 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3421 FIXME( "(%p)->(%p,0x%08lx,%p): stub\n", This, lpwfxFormat, dwSizeAllocated, lpdwSizeWritten );
3426 static HRESULT WINAPI
3427 IDirectSoundCaptureBufferImpl_GetStatus(
3428 LPDIRECTSOUNDCAPTUREBUFFER iface,
3429 LPDWORD lpdwStatus )
3431 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3433 FIXME( "(%p)->(%p): stub\n", This, lpdwStatus );
3438 static HRESULT WINAPI
3439 IDirectSoundCaptureBufferImpl_Initialize(
3440 LPDIRECTSOUNDCAPTUREBUFFER iface,
3441 LPDIRECTSOUNDCAPTURE lpDSC,
3442 LPCDSCBUFFERDESC lpcDSCBDesc )
3444 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3446 FIXME( "(%p)->(%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
3451 static HRESULT WINAPI
3452 IDirectSoundCaptureBufferImpl_Lock(
3453 LPDIRECTSOUNDCAPTUREBUFFER iface,
3456 LPVOID* lplpvAudioPtr1,
3457 LPDWORD lpdwAudioBytes1,
3458 LPVOID* lplpvAudioPtr2,
3459 LPDWORD lpdwAudioBytes2,
3462 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3464 FIXME( "(%p)->(%08lu,%08lu,%p,%p,%p,%p,0x%08lx): stub\n", This, dwReadCusor, dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2, lpdwAudioBytes2, dwFlags );
3469 static HRESULT WINAPI
3470 IDirectSoundCaptureBufferImpl_Start(
3471 LPDIRECTSOUNDCAPTUREBUFFER iface,
3474 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3476 FIXME( "(%p)->(0x%08lx): stub\n", This, dwFlags );
3481 static HRESULT WINAPI
3482 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER iface )
3484 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3486 FIXME( "(%p): stub\n", This );
3491 static HRESULT WINAPI
3492 IDirectSoundCaptureBufferImpl_Unlock(
3493 LPDIRECTSOUNDCAPTUREBUFFER iface,
3494 LPVOID lpvAudioPtr1,
3495 DWORD dwAudioBytes1,
3496 LPVOID lpvAudioPtr2,
3497 DWORD dwAudioBytes2 )
3499 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3501 FIXME( "(%p)->(%p,%08lu,%p,%08lu): stub\n", This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2 );
3507 static ICOM_VTABLE(IDirectSoundCaptureBuffer) dscbvt =
3509 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3510 /* IUnknown methods */
3511 IDirectSoundCaptureBufferImpl_QueryInterface,
3512 IDirectSoundCaptureBufferImpl_AddRef,
3513 IDirectSoundCaptureBufferImpl_Release,
3515 /* IDirectSoundCaptureBuffer methods */
3516 IDirectSoundCaptureBufferImpl_GetCaps,
3517 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
3518 IDirectSoundCaptureBufferImpl_GetFormat,
3519 IDirectSoundCaptureBufferImpl_GetStatus,
3520 IDirectSoundCaptureBufferImpl_Initialize,
3521 IDirectSoundCaptureBufferImpl_Lock,
3522 IDirectSoundCaptureBufferImpl_Start,
3523 IDirectSoundCaptureBufferImpl_Stop,
3524 IDirectSoundCaptureBufferImpl_Unlock
3527 /*******************************************************************************
3528 * DirectSound ClassFactory
3532 /* IUnknown fields */
3533 ICOM_VFIELD(IClassFactory);
3535 } IClassFactoryImpl;
3537 static HRESULT WINAPI
3538 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
3539 ICOM_THIS(IClassFactoryImpl,iface);
3541 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
3542 return E_NOINTERFACE;
3546 DSCF_AddRef(LPCLASSFACTORY iface) {
3547 ICOM_THIS(IClassFactoryImpl,iface);
3548 return ++(This->ref);
3551 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface) {
3552 ICOM_THIS(IClassFactoryImpl,iface);
3553 /* static class, won't be freed */
3554 return --(This->ref);
3557 static HRESULT WINAPI DSCF_CreateInstance(
3558 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
3560 ICOM_THIS(IClassFactoryImpl,iface);
3562 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
3563 if ( IsEqualGUID( &IID_IDirectSound, riid ) ) {
3564 /* FIXME: reuse already created dsound if present? */
3565 return DirectSoundCreate(riid,(LPDIRECTSOUND*)ppobj,pOuter);
3567 return E_NOINTERFACE;
3570 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
3571 ICOM_THIS(IClassFactoryImpl,iface);
3572 FIXME("(%p)->(%d),stub!\n",This,dolock);
3576 static ICOM_VTABLE(IClassFactory) DSCF_Vtbl = {
3577 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3578 DSCF_QueryInterface,
3581 DSCF_CreateInstance,
3584 static IClassFactoryImpl DSOUND_CF = {&DSCF_Vtbl, 1 };
3586 /*******************************************************************************
3587 * DllGetClassObject [DSOUND.4]
3588 * Retrieves class object from a DLL object
3591 * Docs say returns STDAPI
3594 * rclsid [I] CLSID for the class object
3595 * riid [I] Reference to identifier of interface for class object
3596 * ppv [O] Address of variable to receive interface pointer for riid
3600 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
3603 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
3605 TRACE("(%p,%p,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
3606 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
3607 *ppv = (LPVOID)&DSOUND_CF;
3608 IClassFactory_AddRef((IClassFactory*)*ppv);
3612 FIXME("(%p,%p,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
3613 return CLASS_E_CLASSNOTAVAILABLE;
3617 /*******************************************************************************
3618 * DllCanUnloadNow [DSOUND.3] Determines whether the DLL is in use.
3624 DWORD WINAPI DSOUND_DllCanUnloadNow(void)
3626 FIXME("(void): stub\n");