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
30 #include <sys/types.h>
32 #include <sys/fcntl.h>
36 #include <math.h> /* Insomnia - pow() function */
44 #include "debugtools.h"
50 DEFAULT_DEBUG_CHANNEL(dsound);
52 /* these are eligible for tuning... they must be high on slow machines... */
53 /* especially since the WINMM overhead is pretty high, and could be improved quite a bit;
54 * the high DS_HEL_MARGIN reflects the currently high wineoss/HEL latency */
55 #define DS_HEL_FRAGS 48 /* HEL only: number of waveOut fragments in primary buffer */
56 #define DS_HEL_QUEUE 28 /* HEL only: number of waveOut fragments to prebuffer */
57 /* (Starcraft videos won't work with higher than 32 x10ms) */
58 #define DS_HEL_MARGIN 4 /* HEL only: number of waveOut fragments ahead to mix in new buffers */
60 #define DS_HAL_QUEUE 28 /* HAL only: max number of fragments to prebuffer */
62 /* Linux does not support better timing than 10ms */
63 #define DS_TIME_RES 10 /* Resolution of multimedia timer */
64 #define DS_TIME_DEL 10 /* Delay of multimedia timer callback, and duration of HEL fragment */
66 /*****************************************************************************
67 * Predeclare the interface implementation structures
69 typedef struct IDirectSoundImpl IDirectSoundImpl;
70 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
71 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
72 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
73 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
74 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
75 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
77 /*****************************************************************************
78 * IDirectSound implementation structure
80 struct IDirectSoundImpl
83 ICOM_VFIELD(IDirectSound);
85 /* IDirectSoundImpl fields */
90 LPWAVEHDR pwave[DS_HEL_FRAGS];
91 UINT timerID, pwplay, pwwrite, pwqueue;
95 IDirectSoundBufferImpl** buffers;
96 IDirectSoundBufferImpl* primary;
97 IDirectSound3DListenerImpl* listener;
98 WAVEFORMATEX wfx; /* current main waveformat */
99 CRITICAL_SECTION lock;
102 /*****************************************************************************
103 * IDirectSoundBuffer implementation structure
105 struct IDirectSoundBufferImpl
107 /* IUnknown fields */
108 ICOM_VFIELD(IDirectSoundBuffer);
110 /* IDirectSoundBufferImpl fields */
111 PIDSDRIVERBUFFER hwbuf;
114 IDirectSound3DBufferImpl* ds3db;
115 DWORD playflags,state,leadin;
116 DWORD playpos,mixpos,startpos,writelead,buflen;
117 DWORD nAvgBytesPerSec;
121 IDirectSoundBufferImpl* parent; /* for duplicates */
122 IDirectSoundImpl* dsound;
124 LPDSBPOSITIONNOTIFY notifies;
126 CRITICAL_SECTION lock;
129 #define STATE_STOPPED 0
130 #define STATE_STARTING 1
131 #define STATE_PLAYING 2
132 #define STATE_STOPPING 3
134 /*****************************************************************************
135 * IDirectSoundNotify implementation structure
137 struct IDirectSoundNotifyImpl
139 /* IUnknown fields */
140 ICOM_VFIELD(IDirectSoundNotify);
142 /* IDirectSoundNotifyImpl fields */
143 IDirectSoundBufferImpl* dsb;
146 /*****************************************************************************
147 * IDirectSound3DListener implementation structure
149 struct IDirectSound3DListenerImpl
151 /* IUnknown fields */
152 ICOM_VFIELD(IDirectSound3DListener);
154 /* IDirectSound3DListenerImpl fields */
155 IDirectSoundBufferImpl* dsb;
157 CRITICAL_SECTION lock;
160 /*****************************************************************************
161 * IDirectSound3DBuffer implementation structure
163 struct IDirectSound3DBufferImpl
165 /* IUnknown fields */
166 ICOM_VFIELD(IDirectSound3DBuffer);
168 /* IDirectSound3DBufferImpl fields */
169 IDirectSoundBufferImpl* dsb;
173 CRITICAL_SECTION lock;
177 /*****************************************************************************
178 * IDirectSoundCapture implementation structure
180 struct IDirectSoundCaptureImpl
182 /* IUnknown fields */
183 ICOM_VFIELD(IDirectSoundCapture);
186 /* IDirectSoundCaptureImpl fields */
187 CRITICAL_SECTION lock;
190 /*****************************************************************************
191 * IDirectSoundCapture implementation structure
193 struct IDirectSoundCaptureBufferImpl
195 /* IUnknown fields */
196 ICOM_VFIELD(IDirectSoundCaptureBuffer);
199 /* IDirectSoundCaptureBufferImpl fields */
200 CRITICAL_SECTION lock;
204 /* #define USE_DSOUND3D 1 */
206 #define DSOUND_FREQSHIFT (14)
208 static IDirectSoundImpl* dsound = NULL;
210 static IDirectSoundBufferImpl* primarybuf = NULL;
212 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
214 static HRESULT DSOUND_CreateDirectSoundCapture( LPVOID* ppobj );
215 static HRESULT DSOUND_CreateDirectSoundCaptureBuffer( LPCDSCBUFFERDESC lpcDSCBufferDesc, LPVOID* ppobj );
217 static ICOM_VTABLE(IDirectSoundCapture) dscvt;
218 static ICOM_VTABLE(IDirectSoundCaptureBuffer) dscbvt;
220 static HRESULT mmErr(UINT err)
223 case MMSYSERR_NOERROR:
225 case MMSYSERR_ALLOCATED:
226 return DSERR_ALLOCATED;
227 case MMSYSERR_INVALHANDLE:
228 return DSERR_GENERIC; /* FIXME */
229 case MMSYSERR_NODRIVER:
230 return DSERR_NODRIVER;
232 return DSERR_OUTOFMEMORY;
233 case MMSYSERR_INVALPARAM:
234 return DSERR_INVALIDPARAM;
236 FIXME("Unknown MMSYS error %d\n",err);
237 return DSERR_GENERIC;
241 /***************************************************************************
242 * DirectSoundEnumerateA [DSOUND.2]
244 * Enumerate all DirectSound drivers installed in the system
248 * Failure: DSERR_INVALIDPARAM
250 HRESULT WINAPI DirectSoundEnumerateA(
251 LPDSENUMCALLBACKA lpDSEnumCallback,
254 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
255 lpDSEnumCallback, lpContext);
258 if (lpDSEnumCallback != NULL)
259 lpDSEnumCallback(NULL,"WINE DirectSound using Open Sound System",
266 /***************************************************************************
267 * DirectSoundEnumerateW [DSOUND.3]
269 * Enumerate all DirectSound drivers installed in the system
273 * Failure: DSERR_INVALIDPARAM
275 HRESULT WINAPI DirectSoundEnumerateW(
276 LPDSENUMCALLBACKW lpDSEnumCallback,
279 FIXME("lpDSEnumCallback = %p, lpContext = %p: stub\n",
280 lpDSEnumCallback, lpContext);
286 static void _dump_DSBCAPS(DWORD xmask) {
291 #define FE(x) { x, #x },
292 FE(DSBCAPS_PRIMARYBUFFER)
294 FE(DSBCAPS_LOCHARDWARE)
295 FE(DSBCAPS_LOCSOFTWARE)
297 FE(DSBCAPS_CTRLFREQUENCY)
299 FE(DSBCAPS_CTRLVOLUME)
300 FE(DSBCAPS_CTRLPOSITIONNOTIFY)
301 FE(DSBCAPS_CTRLDEFAULT)
303 FE(DSBCAPS_STICKYFOCUS)
304 FE(DSBCAPS_GLOBALFOCUS)
305 FE(DSBCAPS_GETCURRENTPOSITION2)
306 FE(DSBCAPS_MUTE3DATMAXDISTANCE)
311 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
312 if ((flags[i].mask & xmask) == flags[i].mask)
313 DPRINTF("%s ",flags[i].name);
316 /*******************************************************************************
317 * IDirectSound3DBuffer
320 /* IUnknown methods */
322 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
323 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
325 ICOM_THIS(IDirectSound3DBufferImpl,iface);
327 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
333 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
335 ICOM_THIS(IDirectSound3DBufferImpl,iface);
342 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
344 ICOM_THIS(IDirectSound3DBufferImpl,iface);
346 TRACE("(%p) ref was %ld\n", This, This->ref);
352 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
354 DeleteCriticalSection(&This->lock);
356 HeapFree(GetProcessHeap(),0,This->buffer);
357 HeapFree(GetProcessHeap(),0,This);
363 /* IDirectSound3DBuffer methods */
365 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
366 LPDIRECTSOUND3DBUFFER iface,
367 LPDS3DBUFFER lpDs3dBuffer)
375 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
376 LPDIRECTSOUND3DBUFFER iface,
377 LPDWORD lpdwInsideConeAngle,
378 LPDWORD lpdwOutsideConeAngle)
386 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
387 LPDIRECTSOUND3DBUFFER iface,
388 LPD3DVECTOR lpvConeOrientation)
396 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
397 LPDIRECTSOUND3DBUFFER iface,
398 LPLONG lplConeOutsideVolume)
406 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
407 LPDIRECTSOUND3DBUFFER iface,
408 LPD3DVALUE lpfMaxDistance)
416 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
417 LPDIRECTSOUND3DBUFFER iface,
418 LPD3DVALUE lpfMinDistance)
426 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
427 LPDIRECTSOUND3DBUFFER iface,
436 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
437 LPDIRECTSOUND3DBUFFER iface,
438 LPD3DVECTOR lpvPosition)
446 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
447 LPDIRECTSOUND3DBUFFER iface,
448 LPD3DVECTOR lpvVelocity)
456 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
457 LPDIRECTSOUND3DBUFFER iface,
458 LPCDS3DBUFFER lpcDs3dBuffer,
467 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
468 LPDIRECTSOUND3DBUFFER iface,
469 DWORD dwInsideConeAngle,
470 DWORD dwOutsideConeAngle,
479 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
480 LPDIRECTSOUND3DBUFFER iface,
481 D3DVALUE x, D3DVALUE y, D3DVALUE z,
490 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
491 LPDIRECTSOUND3DBUFFER iface,
492 LONG lConeOutsideVolume,
501 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
502 LPDIRECTSOUND3DBUFFER iface,
503 D3DVALUE fMaxDistance,
512 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
513 LPDIRECTSOUND3DBUFFER iface,
514 D3DVALUE fMinDistance,
523 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
524 LPDIRECTSOUND3DBUFFER iface,
528 ICOM_THIS(IDirectSound3DBufferImpl,iface);
529 TRACE("mode = %lx\n", dwMode);
530 This->ds3db.dwMode = dwMode;
536 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
537 LPDIRECTSOUND3DBUFFER iface,
538 D3DVALUE x, D3DVALUE y, D3DVALUE z,
547 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
548 LPDIRECTSOUND3DBUFFER iface,
549 D3DVALUE x, D3DVALUE y, D3DVALUE z,
558 static ICOM_VTABLE(IDirectSound3DBuffer) ds3dbvt =
560 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
561 /* IUnknown methods */
562 IDirectSound3DBufferImpl_QueryInterface,
563 IDirectSound3DBufferImpl_AddRef,
564 IDirectSound3DBufferImpl_Release,
565 /* IDirectSound3DBuffer methods */
566 IDirectSound3DBufferImpl_GetAllParameters,
567 IDirectSound3DBufferImpl_GetConeAngles,
568 IDirectSound3DBufferImpl_GetConeOrientation,
569 IDirectSound3DBufferImpl_GetConeOutsideVolume,
570 IDirectSound3DBufferImpl_GetMaxDistance,
571 IDirectSound3DBufferImpl_GetMinDistance,
572 IDirectSound3DBufferImpl_GetMode,
573 IDirectSound3DBufferImpl_GetPosition,
574 IDirectSound3DBufferImpl_GetVelocity,
575 IDirectSound3DBufferImpl_SetAllParameters,
576 IDirectSound3DBufferImpl_SetConeAngles,
577 IDirectSound3DBufferImpl_SetConeOrientation,
578 IDirectSound3DBufferImpl_SetConeOutsideVolume,
579 IDirectSound3DBufferImpl_SetMaxDistance,
580 IDirectSound3DBufferImpl_SetMinDistance,
581 IDirectSound3DBufferImpl_SetMode,
582 IDirectSound3DBufferImpl_SetPosition,
583 IDirectSound3DBufferImpl_SetVelocity,
588 static int DSOUND_Create3DBuffer(IDirectSoundBufferImpl* dsb)
590 DWORD i, temp, iSize, oSize, offset;
591 LPBYTE bIbuf, bObuf, bTbuf = NULL;
592 LPWORD wIbuf, wObuf, wTbuf = NULL;
594 /* Inside DirectX says it's stupid but allowed */
595 if (dsb->wfx.nChannels == 2) {
596 /* Convert to mono */
597 if (dsb->wfx.wBitsPerSample == 16) {
598 iSize = dsb->buflen / 4;
599 wTbuf = malloc(dsb->buflen / 2);
601 return DSERR_OUTOFMEMORY;
602 for (i = 0; i < iSize; i++)
603 wTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
606 iSize = dsb->buflen / 2;
607 bTbuf = malloc(dsb->buflen / 2);
609 return DSERR_OUTOFMEMORY;
610 for (i = 0; i < iSize; i++)
611 bTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
615 if (dsb->wfx.wBitsPerSample == 16) {
616 iSize = dsb->buflen / 2;
617 wIbuf = (LPWORD) dsb->buffer;
619 bIbuf = (LPBYTE) dsb->buffer;
624 if (primarybuf->wfx.wBitsPerSample == 16) {
625 wObuf = (LPWORD) dsb->ds3db->buffer;
626 oSize = dsb->ds3db->buflen / 2;
628 bObuf = (LPBYTE) dsb->ds3db->buffer;
629 oSize = dsb->ds3db->buflen;
632 offset = primarybuf->wfx.nSamplesPerSec / 100; /* 10ms */
633 if (primarybuf->wfx.wBitsPerSample == 16 && dsb->wfx.wBitsPerSample == 16)
634 for (i = 0; i < iSize; i++) {
637 temp += wIbuf[i - offset] >> 9;
639 temp += wIbuf[i + iSize - offset] >> 9;
641 wObuf[(i * 2) + 1] = temp;
643 else if (primarybuf->wfx.wBitsPerSample == 8 && dsb->wfx.wBitsPerSample == 8)
644 for (i = 0; i < iSize; i++) {
647 temp += bIbuf[i - offset] >> 5;
649 temp += bIbuf[i + iSize - offset] >> 5;
651 bObuf[(i * 2) + 1] = temp;
662 /*******************************************************************************
663 * IDirectSound3DListener
666 /* IUnknown methods */
667 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
668 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
670 ICOM_THIS(IDirectSound3DListenerImpl,iface);
672 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
676 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
678 ICOM_THIS(IDirectSound3DListenerImpl,iface);
683 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
686 ICOM_THIS(IDirectSound3DListenerImpl,iface);
688 TRACE("(%p) ref was %ld\n", This, This->ref);
690 ulReturn = --This->ref;
692 /* Free all resources */
693 if( ulReturn == 0 ) {
695 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
696 DeleteCriticalSection(&This->lock);
697 HeapFree(GetProcessHeap(),0,This);
703 /* IDirectSound3DListener methods */
704 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
705 LPDIRECTSOUND3DLISTENER iface,
706 LPDS3DLISTENER lpDS3DL)
712 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
713 LPDIRECTSOUND3DLISTENER iface,
714 LPD3DVALUE lpfDistanceFactor)
720 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
721 LPDIRECTSOUND3DLISTENER iface,
722 LPD3DVALUE lpfDopplerFactor)
728 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
729 LPDIRECTSOUND3DLISTENER iface,
730 LPD3DVECTOR lpvOrientFront,
731 LPD3DVECTOR lpvOrientTop)
737 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
738 LPDIRECTSOUND3DLISTENER iface,
739 LPD3DVECTOR lpvPosition)
745 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
746 LPDIRECTSOUND3DLISTENER iface,
747 LPD3DVALUE lpfRolloffFactor)
753 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
754 LPDIRECTSOUND3DLISTENER iface,
755 LPD3DVECTOR lpvVelocity)
761 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
762 LPDIRECTSOUND3DLISTENER iface,
763 LPCDS3DLISTENER lpcDS3DL,
770 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
771 LPDIRECTSOUND3DLISTENER iface,
772 D3DVALUE fDistanceFactor,
779 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
780 LPDIRECTSOUND3DLISTENER iface,
781 D3DVALUE fDopplerFactor,
788 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
789 LPDIRECTSOUND3DLISTENER iface,
790 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
791 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
798 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
799 LPDIRECTSOUND3DLISTENER iface,
800 D3DVALUE x, D3DVALUE y, D3DVALUE z,
807 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
808 LPDIRECTSOUND3DLISTENER iface,
809 D3DVALUE fRolloffFactor,
816 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
817 LPDIRECTSOUND3DLISTENER iface,
818 D3DVALUE x, D3DVALUE y, D3DVALUE z,
825 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
826 LPDIRECTSOUND3DLISTENER iface)
833 static ICOM_VTABLE(IDirectSound3DListener) ds3dlvt =
835 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
836 /* IUnknown methods */
837 IDirectSound3DListenerImpl_QueryInterface,
838 IDirectSound3DListenerImpl_AddRef,
839 IDirectSound3DListenerImpl_Release,
840 /* IDirectSound3DListener methods */
841 IDirectSound3DListenerImpl_GetAllParameter,
842 IDirectSound3DListenerImpl_GetDistanceFactor,
843 IDirectSound3DListenerImpl_GetDopplerFactor,
844 IDirectSound3DListenerImpl_GetOrientation,
845 IDirectSound3DListenerImpl_GetPosition,
846 IDirectSound3DListenerImpl_GetRolloffFactor,
847 IDirectSound3DListenerImpl_GetVelocity,
848 IDirectSound3DListenerImpl_SetAllParameters,
849 IDirectSound3DListenerImpl_SetDistanceFactor,
850 IDirectSound3DListenerImpl_SetDopplerFactor,
851 IDirectSound3DListenerImpl_SetOrientation,
852 IDirectSound3DListenerImpl_SetPosition,
853 IDirectSound3DListenerImpl_SetRolloffFactor,
854 IDirectSound3DListenerImpl_SetVelocity,
855 IDirectSound3DListenerImpl_CommitDeferredSettings,
858 /*******************************************************************************
861 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
862 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
864 ICOM_THIS(IDirectSoundNotifyImpl,iface);
866 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
870 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
871 ICOM_THIS(IDirectSoundNotifyImpl,iface);
872 return ++(This->ref);
875 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
876 ICOM_THIS(IDirectSoundNotifyImpl,iface);
878 TRACE("(%p) ref was %ld\n", This, This->ref);
882 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
883 HeapFree(GetProcessHeap(),0,This);
889 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
890 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
892 ICOM_THIS(IDirectSoundNotifyImpl,iface);
895 if (TRACE_ON(dsound)) {
896 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
897 for (i=0;i<howmuch;i++)
898 TRACE("notify at %ld to 0x%08lx\n",
899 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
901 This->dsb->notifies = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->dsb->notifies,(This->dsb->nrofnotifies+howmuch)*sizeof(DSBPOSITIONNOTIFY));
902 memcpy( This->dsb->notifies+This->dsb->nrofnotifies,
904 howmuch*sizeof(DSBPOSITIONNOTIFY)
906 This->dsb->nrofnotifies+=howmuch;
911 static ICOM_VTABLE(IDirectSoundNotify) dsnvt =
913 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
914 IDirectSoundNotifyImpl_QueryInterface,
915 IDirectSoundNotifyImpl_AddRef,
916 IDirectSoundNotifyImpl_Release,
917 IDirectSoundNotifyImpl_SetNotificationPositions,
920 /*******************************************************************************
924 static void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
928 /* the AmpFactors are expressed in 16.16 fixed point */
929 volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 65536);
930 /* FIXME: dwPan{Left|Right}AmpFactor */
932 /* FIXME: use calculated vol and pan ampfactors */
933 temp = (double) (volpan->lVolume - (volpan->lPan > 0 ? volpan->lPan : 0));
934 volpan->dwTotalLeftAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 65536);
935 temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
936 volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 65536);
938 TRACE("left = %lx, right = %lx\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
941 static void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
945 sw = dsb->wfx.nChannels * (dsb->wfx.wBitsPerSample / 8);
946 if ((dsb->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && dsb->hwbuf) {
948 /* let fragment size approximate the timer delay */
949 fraglen = (dsb->freq * DS_TIME_DEL / 1000) * sw;
950 /* reduce fragment size until an integer number of them fits in the buffer */
951 /* (FIXME: this may or may not be a good idea) */
952 while (dsb->buflen % fraglen) fraglen -= sw;
953 dsb->dsound->fraglen = fraglen;
954 TRACE("fraglen=%ld\n", dsb->dsound->fraglen);
956 /* calculate the 10ms write lead */
957 dsb->writelead = (dsb->freq / 100) * sw;
960 static HRESULT DSOUND_PrimaryOpen(IDirectSoundBufferImpl *dsb)
964 /* are we using waveOut stuff? */
968 HRESULT merr = DS_OK;
969 /* Start in pause mode, to allow buffers to get filled */
970 waveOutPause(dsb->dsound->hwo);
971 /* use fragments of 10ms (1/100s) each (which should get us within
972 * the documented write cursor lead of 10-15ms) */
973 buflen = ((dsb->wfx.nAvgBytesPerSec / 100) & ~3) * DS_HEL_FRAGS;
974 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, dsb->buffer);
975 /* reallocate emulated primary buffer */
976 newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,dsb->buffer,buflen);
977 if (newbuf == NULL) {
978 ERR("failed to allocate primary buffer\n");
979 merr = DSERR_OUTOFMEMORY;
980 /* but the old buffer might still exists and must be re-prepared */
982 dsb->buffer = newbuf;
983 dsb->buflen = buflen;
987 IDirectSoundImpl *ds = dsb->dsound;
989 ds->fraglen = dsb->buflen / DS_HEL_FRAGS;
991 /* prepare fragment headers */
992 for (c=0; c<DS_HEL_FRAGS; c++) {
993 ds->pwave[c]->lpData = dsb->buffer + c*ds->fraglen;
994 ds->pwave[c]->dwBufferLength = ds->fraglen;
995 ds->pwave[c]->dwUser = (DWORD)dsb;
996 ds->pwave[c]->dwFlags = 0;
997 ds->pwave[c]->dwLoops = 0;
998 err = mmErr(waveOutPrepareHeader(ds->hwo,ds->pwave[c],sizeof(WAVEHDR)));
1001 waveOutUnprepareHeader(ds->hwo,ds->pwave[c],sizeof(WAVEHDR));
1009 memset(dsb->buffer, (dsb->wfx.wBitsPerSample == 16) ? 0 : 128, dsb->buflen);
1010 TRACE("fraglen=%ld\n", ds->fraglen);
1012 if ((err == DS_OK) && (merr != DS_OK))
1019 static void DSOUND_PrimaryClose(IDirectSoundBufferImpl *dsb)
1021 /* are we using waveOut stuff? */
1024 IDirectSoundImpl *ds = dsb->dsound;
1026 waveOutReset(ds->hwo);
1027 for (c=0; c<DS_HEL_FRAGS; c++)
1028 waveOutUnprepareHeader(ds->hwo, ds->pwave[c], sizeof(WAVEHDR));
1032 /* This sets this format for the <em>Primary Buffer Only</em> */
1033 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
1034 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
1035 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX wfex
1037 ICOM_THIS(IDirectSoundBufferImpl,iface);
1038 IDirectSoundBufferImpl** dsb;
1039 HRESULT err = DS_OK;
1042 /* Let's be pedantic! */
1043 if ((wfex == NULL) ||
1044 (wfex->wFormatTag != WAVE_FORMAT_PCM) ||
1045 (wfex->nChannels < 1) || (wfex->nChannels > 2) ||
1046 (wfex->nSamplesPerSec < 1) ||
1047 (wfex->nBlockAlign < 1) || (wfex->nChannels > 4) ||
1048 ((wfex->wBitsPerSample != 8) && (wfex->wBitsPerSample != 16))) {
1049 TRACE("failed pedantic check!\n");
1050 return DSERR_INVALIDPARAM;
1054 EnterCriticalSection(&(This->dsound->lock));
1056 if (primarybuf->wfx.nSamplesPerSec != wfex->nSamplesPerSec) {
1057 dsb = dsound->buffers;
1058 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
1060 EnterCriticalSection(&((*dsb)->lock));
1062 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
1063 wfex->nSamplesPerSec;
1065 LeaveCriticalSection(&((*dsb)->lock));
1070 memcpy(&(primarybuf->wfx), wfex, sizeof(primarybuf->wfx));
1072 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1073 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1074 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1075 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1076 wfex->wBitsPerSample, wfex->cbSize);
1078 primarybuf->wfx.nAvgBytesPerSec =
1079 This->wfx.nSamplesPerSec * This->wfx.nBlockAlign;
1080 if (primarybuf->dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
1081 /* FIXME: check for errors */
1082 DSOUND_PrimaryClose(primarybuf);
1083 waveOutClose(This->dsound->hwo);
1084 This->dsound->hwo = 0;
1085 waveOutOpen(&(This->dsound->hwo), This->dsound->drvdesc.dnDevNode,
1086 &(primarybuf->wfx), 0, 0, CALLBACK_NULL | WAVE_DIRECTSOUND);
1087 DSOUND_PrimaryOpen(primarybuf);
1089 if (primarybuf->hwbuf) {
1090 err = IDsDriverBuffer_SetFormat(primarybuf->hwbuf, &(primarybuf->wfx));
1091 if (err == DSERR_BUFFERLOST) {
1092 /* Wine-only: the driver wants us to recreate the HW buffer */
1093 IDsDriverBuffer_Release(primarybuf->hwbuf);
1094 err = IDsDriver_CreateSoundBuffer(primarybuf->dsound->driver,&(primarybuf->wfx),primarybuf->dsbd.dwFlags,0,
1095 &(primarybuf->buflen),&(primarybuf->buffer),
1096 (LPVOID)&(primarybuf->hwbuf));
1099 DSOUND_RecalcFormat(primarybuf);
1101 LeaveCriticalSection(&(This->dsound->lock));
1107 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
1108 LPDIRECTSOUNDBUFFER iface,LONG vol
1110 ICOM_THIS(IDirectSoundBufferImpl,iface);
1112 TRACE("(%p,%ld)\n",This,vol);
1114 /* I'm not sure if we need this for primary buffer */
1115 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
1116 return DSERR_CONTROLUNAVAIL;
1118 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN))
1119 return DSERR_INVALIDPARAM;
1122 EnterCriticalSection(&(This->lock));
1124 This->volpan.lVolume = vol;
1126 DSOUND_RecalcVolPan(&(This->volpan));
1129 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
1131 else if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
1132 #if 0 /* should we really do this? */
1133 /* the DS volume ranges from 0 (max, 0dB attenuation) to -10000 (min, 100dB attenuation) */
1134 /* the MM volume ranges from 0 to 0xffff in an unspecified logarithmic scale */
1135 WORD cvol = 0xffff + vol*6 + vol/2;
1136 DWORD vol = cvol | ((DWORD)cvol << 16)
1137 waveOutSetVolume(This->dsound->hwo, vol);
1141 LeaveCriticalSection(&(This->lock));
1147 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
1148 LPDIRECTSOUNDBUFFER iface,LPLONG vol
1150 ICOM_THIS(IDirectSoundBufferImpl,iface);
1151 TRACE("(%p,%p)\n",This,vol);
1154 return DSERR_INVALIDPARAM;
1156 *vol = This->volpan.lVolume;
1160 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
1161 LPDIRECTSOUNDBUFFER iface,DWORD freq
1163 ICOM_THIS(IDirectSoundBufferImpl,iface);
1164 TRACE("(%p,%ld)\n",This,freq);
1166 /* You cannot set the frequency of the primary buffer */
1167 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY) ||
1168 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1169 return DSERR_CONTROLUNAVAIL;
1171 if (!freq) freq = This->wfx.nSamplesPerSec;
1173 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX))
1174 return DSERR_INVALIDPARAM;
1177 EnterCriticalSection(&(This->lock));
1180 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / primarybuf->wfx.nSamplesPerSec;
1181 This->nAvgBytesPerSec = freq * This->wfx.nBlockAlign;
1182 DSOUND_RecalcFormat(This);
1184 LeaveCriticalSection(&(This->lock));
1190 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
1191 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
1193 ICOM_THIS(IDirectSoundBufferImpl,iface);
1194 TRACE("(%p,%08lx,%08lx,%08lx)\n",
1195 This,reserved1,reserved2,flags
1197 This->playflags = flags;
1198 if (This->state == STATE_STOPPED) {
1199 This->leadin = TRUE;
1200 This->startpos = This->mixpos;
1201 This->state = STATE_STARTING;
1202 } else if (This->state == STATE_STOPPING)
1203 This->state = STATE_PLAYING;
1204 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && This->hwbuf) {
1205 IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
1206 This->state = STATE_PLAYING;
1211 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
1213 ICOM_THIS(IDirectSoundBufferImpl,iface);
1214 TRACE("(%p)\n",This);
1217 EnterCriticalSection(&(This->lock));
1219 if (This->state == STATE_PLAYING)
1220 This->state = STATE_STOPPING;
1221 else if (This->state == STATE_STARTING)
1222 This->state = STATE_STOPPED;
1223 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && This->hwbuf) {
1224 IDsDriverBuffer_Stop(This->hwbuf);
1225 This->state = STATE_STOPPED;
1227 DSOUND_CheckEvent(This, 0);
1229 LeaveCriticalSection(&(This->lock));
1235 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface) {
1236 ICOM_THIS(IDirectSoundBufferImpl,iface);
1239 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
1241 ref = InterlockedIncrement(&(This->ref));
1243 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
1247 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER iface) {
1248 ICOM_THIS(IDirectSoundBufferImpl,iface);
1252 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
1254 ref = InterlockedDecrement(&(This->ref));
1255 if (ref) return ref;
1257 EnterCriticalSection(&(This->dsound->lock));
1258 for (i=0;i<This->dsound->nrofbuffers;i++)
1259 if (This->dsound->buffers[i] == This)
1262 if (i < This->dsound->nrofbuffers) {
1263 /* Put the last buffer of the list in the (now empty) position */
1264 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
1265 This->dsound->nrofbuffers--;
1266 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER)*This->dsound->nrofbuffers);
1267 TRACE("buffer count is now %d\n", This->dsound->nrofbuffers);
1268 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
1270 LeaveCriticalSection(&(This->dsound->lock));
1272 DeleteCriticalSection(&(This->lock));
1273 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1274 DSOUND_PrimaryClose(This);
1276 IDsDriverBuffer_Release(This->hwbuf);
1279 IDirectSound3DBuffer_Release((LPDIRECTSOUND3DBUFFER)This->ds3db);
1281 /* this is a duplicate buffer */
1282 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->parent);
1284 /* this is a toplevel buffer */
1285 HeapFree(GetProcessHeap(),0,This->buffer);
1287 HeapFree(GetProcessHeap(),0,This);
1289 if (This == primarybuf)
1295 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
1296 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
1298 ICOM_THIS(IDirectSoundBufferImpl,iface);
1299 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1301 IDsDriverBuffer_GetPosition(This->hwbuf, playpos, writepos);
1303 else if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
1304 if (playpos && (This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2)) {
1306 mtime.wType = TIME_BYTES;
1307 waveOutGetPosition(This->dsound->hwo, &mtime, sizeof(mtime));
1308 mtime.u.cb = mtime.u.cb % This->buflen;
1309 *playpos = mtime.u.cb;
1311 /* don't know how exactly non-GETCURRENTPOSITION2 behaves,
1312 * but I think this works for Starcraft */
1313 else if (playpos) *playpos = This->playpos;
1315 /* the writepos should only be used by apps with WRITEPRIMARY priority,
1316 * in which case our software mixer is disabled anyway */
1317 *writepos = This->playpos + DS_HEL_MARGIN * This->dsound->fraglen;
1318 while (*writepos >= This->buflen)
1319 *writepos -= This->buflen;
1322 if (playpos && (This->state != STATE_PLAYING)) {
1323 /* we haven't been merged into the primary buffer (yet) */
1324 *playpos = This->mixpos;
1327 DWORD pplay, lplay, splay, tplay, pstate;
1328 /* let's get this exact; first, recursively call GetPosition on the primary */
1329 EnterCriticalSection(&(primarybuf->lock));
1330 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || primarybuf->hwbuf) {
1331 IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER)primarybuf, &pplay, NULL);
1333 /* (unless the app isn't using GETCURRENTPOSITION2) */
1334 /* don't know exactly how this should be handled either */
1335 pplay = primarybuf->playpos;
1337 /* get last mixed primary play position */
1338 lplay = primarybuf->mixpos;
1339 pstate = primarybuf->state;
1340 /* detect HEL mode underrun */
1341 if (!(primarybuf->hwbuf || primarybuf->dsound->pwqueue)) {
1342 TRACE("detected an underrun\n");
1344 if (pstate == STATE_PLAYING)
1345 pstate = STATE_STARTING;
1346 else if (pstate == STATE_STOPPING)
1347 pstate = STATE_STOPPED;
1349 /* get our own last mixed position while we still have the lock */
1350 splay = This->mixpos;
1351 LeaveCriticalSection(&(primarybuf->lock));
1352 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, lplay);
1353 TRACE("this mixpos=%ld\n", splay);
1355 /* the actual primary play position (pplay) is always behind last mixed (lplay),
1356 * unless the computer is too slow or something */
1357 /* we need to know how far away we are from there */
1358 if (lplay == pplay) {
1359 if ((pstate == STATE_PLAYING) || (pstate == STATE_STOPPING)) {
1360 /* wow, the software mixer is really doing well,
1361 * seems the entire primary buffer is filled! */
1362 lplay += primarybuf->buflen;
1364 /* else: the primary buffer is not playing, so probably empty */
1366 if (lplay < pplay) lplay += primarybuf->buflen; /* wraparound */
1368 /* detect HAL mode underrun */
1369 if (primarybuf->hwbuf &&
1370 (lplay > ((DS_HAL_QUEUE + 1) * primarybuf->dsound->fraglen + primarybuf->writelead))) {
1371 TRACE("detected an underrun: primary queue was %ld\n",lplay);
1374 /* divide the offset by its sample size */
1375 lplay /= primarybuf->wfx.nChannels * (primarybuf->wfx.wBitsPerSample / 8);
1376 TRACE("primary back-samples=%ld\n",lplay);
1377 /* adjust for our frequency */
1378 lplay = (lplay * This->freqAdjust) >> DSOUND_FREQSHIFT;
1379 /* multiply by our own sample size */
1380 lplay *= This->wfx.nChannels * (This->wfx.wBitsPerSample / 8);
1381 TRACE("this back-offset=%ld\n", lplay);
1382 /* subtract from our last mixed position */
1384 while (tplay < lplay) tplay += This->buflen; /* wraparound */
1386 if (This->leadin && ((tplay < This->startpos) || (tplay > splay))) {
1387 /* seems we haven't started playing yet */
1388 TRACE("this still in lead-in phase\n");
1389 tplay = This->startpos;
1391 /* return the result */
1394 if (writepos) *writepos = This->mixpos;
1397 if (This->state != STATE_STOPPED)
1398 /* apply the documented 10ms lead to writepos */
1399 *writepos += This->writelead;
1400 while (*writepos >= This->buflen) *writepos -= This->buflen;
1402 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
1406 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
1407 LPDIRECTSOUNDBUFFER iface,LPDWORD status
1409 ICOM_THIS(IDirectSoundBufferImpl,iface);
1410 TRACE("(%p,%p), thread is %lx\n",This,status,GetCurrentThreadId());
1413 return DSERR_INVALIDPARAM;
1416 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING))
1417 *status |= DSBSTATUS_PLAYING;
1418 if (This->playflags & DSBPLAY_LOOPING)
1419 *status |= DSBSTATUS_LOOPING;
1421 TRACE("status=%lx\n", *status);
1426 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
1427 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
1429 ICOM_THIS(IDirectSoundBufferImpl,iface);
1430 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1432 if (wfsize>sizeof(This->wfx))
1433 wfsize = sizeof(This->wfx);
1434 if (lpwf) { /* NULL is valid */
1435 memcpy(lpwf,&(This->wfx),wfsize);
1437 *wfwritten = wfsize;
1440 *wfwritten = sizeof(This->wfx);
1442 return DSERR_INVALIDPARAM;
1447 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
1448 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
1450 ICOM_THIS(IDirectSoundBufferImpl,iface);
1453 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1464 if (flags & DSBLOCK_FROMWRITECURSOR) {
1466 /* GetCurrentPosition does too much magic to duplicate here */
1467 IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
1468 writecursor += writepos;
1470 if (flags & DSBLOCK_ENTIREBUFFER)
1471 writebytes = This->buflen;
1472 if (writebytes > This->buflen)
1473 writebytes = This->buflen;
1475 assert(audiobytes1!=audiobytes2);
1476 assert(lplpaudioptr1!=lplpaudioptr2);
1478 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1479 capf = DSDDESC_DONTNEEDPRIMARYLOCK;
1481 capf = DSDDESC_DONTNEEDSECONDARYLOCK;
1482 if (!(This->dsound->drvdesc.dwFlags & capf) && This->hwbuf) {
1483 IDsDriverBuffer_Lock(This->hwbuf,
1484 lplpaudioptr1, audiobytes1,
1485 lplpaudioptr2, audiobytes2,
1486 writecursor, writebytes,
1489 if (writecursor+writebytes <= This->buflen) {
1490 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1491 *audiobytes1 = writebytes;
1493 *(LPBYTE*)lplpaudioptr2 = NULL;
1496 TRACE("->%ld.0\n",writebytes);
1498 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1499 *audiobytes1 = This->buflen-writecursor;
1501 *(LPBYTE*)lplpaudioptr2 = This->buffer;
1503 *audiobytes2 = writebytes-(This->buflen-writecursor);
1504 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
1509 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
1510 LPDIRECTSOUNDBUFFER iface,DWORD newpos
1512 ICOM_THIS(IDirectSoundBufferImpl,iface);
1513 TRACE("(%p,%ld)\n",This,newpos);
1516 EnterCriticalSection(&(This->lock));
1518 This->mixpos = newpos;
1520 IDsDriverBuffer_SetPosition(This->hwbuf, This->mixpos);
1522 LeaveCriticalSection(&(This->lock));
1528 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
1529 LPDIRECTSOUNDBUFFER iface,LONG pan
1531 ICOM_THIS(IDirectSoundBufferImpl,iface);
1533 TRACE("(%p,%ld)\n",This,pan);
1535 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT))
1536 return DSERR_INVALIDPARAM;
1538 /* You cannot set the pan of the primary buffer */
1539 /* and you cannot use both pan and 3D controls */
1540 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
1541 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) ||
1542 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1543 return DSERR_CONTROLUNAVAIL;
1546 EnterCriticalSection(&(This->lock));
1548 This->volpan.lPan = pan;
1550 DSOUND_RecalcVolPan(&(This->volpan));
1553 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
1556 LeaveCriticalSection(&(This->lock));
1562 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
1563 LPDIRECTSOUNDBUFFER iface,LPLONG pan
1565 ICOM_THIS(IDirectSoundBufferImpl,iface);
1566 TRACE("(%p,%p)\n",This,pan);
1569 return DSERR_INVALIDPARAM;
1571 *pan = This->volpan.lPan;
1576 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
1577 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1579 ICOM_THIS(IDirectSoundBufferImpl,iface);
1582 TRACE("(%p,%p,%ld,%p,%ld):stub\n", This,p1,x1,p2,x2);
1585 /* Preprocess 3D buffers... */
1587 /* This is highly experimental and liable to break things */
1588 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D)
1589 DSOUND_Create3DBuffer(This);
1592 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1593 capf = DSDDESC_DONTNEEDPRIMARYLOCK;
1595 capf = DSDDESC_DONTNEEDSECONDARYLOCK;
1596 if (!(This->dsound->drvdesc.dwFlags & capf) && This->hwbuf) {
1597 IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
1603 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
1604 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1606 ICOM_THIS(IDirectSoundBufferImpl,iface);
1607 TRACE("(%p,%p)\n",This,freq);
1610 return DSERR_INVALIDPARAM;
1613 TRACE("-> %ld\n", *freq);
1618 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
1619 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPDSBUFFERDESC dbsd
1621 ICOM_THIS(IDirectSoundBufferImpl,iface);
1622 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
1623 DPRINTF("Re-Init!!!\n");
1624 return DSERR_ALREADYINITIALIZED;
1627 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
1628 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1630 ICOM_THIS(IDirectSoundBufferImpl,iface);
1631 TRACE("(%p)->(%p)\n",This,caps);
1634 return DSERR_INVALIDPARAM;
1636 /* I think we should check this value, not set it. See */
1637 /* Inside DirectX, p215. That should apply here, too. */
1638 caps->dwSize = sizeof(*caps);
1640 caps->dwFlags = This->dsbd.dwFlags;
1641 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
1642 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
1644 caps->dwBufferBytes = This->dsbd.dwBufferBytes;
1646 /* This value represents the speed of the "unlock" command.
1647 As unlock is quite fast (it does not do anything), I put
1648 4096 ko/s = 4 Mo / s */
1649 /* FIXME: hwbuf speed */
1650 caps->dwUnlockTransferRate = 4096;
1651 caps->dwPlayCpuOverhead = 0;
1656 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
1657 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1659 ICOM_THIS(IDirectSoundBufferImpl,iface);
1661 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1663 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1664 IDirectSoundNotifyImpl *dsn;
1666 dsn = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsn));
1669 IDirectSoundBuffer_AddRef(iface);
1670 ICOM_VTBL(dsn) = &dsnvt;
1671 *ppobj = (LPVOID)dsn;
1676 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1677 IDirectSound3DBufferImpl *ds3db;
1679 *ppobj = This->ds3db;
1681 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
1685 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
1688 ds3db->dsb = (*ippdsb);
1689 ICOM_VTBL(ds3db) = &ds3dbvt;
1690 InitializeCriticalSection(&ds3db->lock);
1692 ds3db->ds3db = This;
1693 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1695 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1696 ds3db->ds3db.vPosition.x.x = 0.0;
1697 ds3db->ds3db.vPosition.y.y = 0.0;
1698 ds3db->ds3db.vPosition.z.z = 0.0;
1699 ds3db->ds3db.vVelocity.x.x = 0.0;
1700 ds3db->ds3db.vVelocity.y.y = 0.0;
1701 ds3db->ds3db.vVelocity.z.z = 0.0;
1702 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1703 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1704 ds3db->ds3db.vConeOrientation.x.x = 0.0;
1705 ds3db->ds3db.vConeOrientation.y.y = 0.0;
1706 ds3db->ds3db.vConeOrientation.z.z = 0.0;
1707 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME; ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1708 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1709 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
1710 ds3db->buflen = ((*ippdsb)->buflen * primarybuf->wfx.nBlockAlign) /
1711 (*ippdsb)->wfx.nBlockAlign;
1712 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
1713 if (ds3db->buffer == NULL) {
1715 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
1722 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1723 IDirectSound3DListenerImpl* dsl;
1725 if (This->dsound->listener) {
1726 *ppobj = This->dsound->listener;
1727 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This->dsound->listener);
1731 dsl = (IDirectSound3DListenerImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsl));
1733 ICOM_VTBL(dsl) = &ds3dlvt;
1734 *ppobj = (LPVOID)dsl;
1736 dsl->ds3dl.dwSize = sizeof(DS3DLISTENER);
1737 dsl->ds3dl.vPosition.u1.x = 0.0;
1738 dsl->ds3dl.vPosition.u2.y = 0.0;
1739 dsl->ds3dl.vPosition.u3.z = 0.0;
1740 dsl->ds3dl.vVelocity.u1.x = 0.0;
1741 dsl->ds3dl.vVelocity.u2.y = 0.0;
1742 dsl->ds3dl.vVelocity.u3.z = 0.0;
1743 dsl->ds3dl.vOrientFront.u1.x = 0.0;
1744 dsl->ds3dl.vOrientFront.u2.y = 0.0;
1745 dsl->ds3dl.vOrientFront.u3.z = 1.0;
1746 dsl->ds3dl.vOrientTop.u1.x = 0.0;
1747 dsl->ds3dl.vOrientTop.u2.y = 1.0;
1748 dsl->ds3dl.vOrientTop.u3.z = 0.0;
1749 dsl->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1750 dsl->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1752 InitializeCriticalSection(&dsl->lock);
1755 IDirectSoundBuffer_AddRef(iface);
1757 This->dsound->listener = dsl;
1758 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)dsl);
1763 FIXME( "Unknown GUID %s\n", debugstr_guid( riid ) );
1770 static ICOM_VTABLE(IDirectSoundBuffer) dsbvt =
1772 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1773 IDirectSoundBufferImpl_QueryInterface,
1774 IDirectSoundBufferImpl_AddRef,
1775 IDirectSoundBufferImpl_Release,
1776 IDirectSoundBufferImpl_GetCaps,
1777 IDirectSoundBufferImpl_GetCurrentPosition,
1778 IDirectSoundBufferImpl_GetFormat,
1779 IDirectSoundBufferImpl_GetVolume,
1780 IDirectSoundBufferImpl_GetPan,
1781 IDirectSoundBufferImpl_GetFrequency,
1782 IDirectSoundBufferImpl_GetStatus,
1783 IDirectSoundBufferImpl_Initialize,
1784 IDirectSoundBufferImpl_Lock,
1785 IDirectSoundBufferImpl_Play,
1786 IDirectSoundBufferImpl_SetCurrentPosition,
1787 IDirectSoundBufferImpl_SetFormat,
1788 IDirectSoundBufferImpl_SetVolume,
1789 IDirectSoundBufferImpl_SetPan,
1790 IDirectSoundBufferImpl_SetFrequency,
1791 IDirectSoundBufferImpl_Stop,
1792 IDirectSoundBufferImpl_Unlock
1795 /*******************************************************************************
1799 static HRESULT WINAPI IDirectSoundImpl_SetCooperativeLevel(
1800 LPDIRECTSOUND iface,HWND hwnd,DWORD level
1802 ICOM_THIS(IDirectSoundImpl,iface);
1804 FIXME("(%p,%08lx,%ld):stub\n",This,(DWORD)hwnd,level);
1806 This->priolevel = level;
1811 static HRESULT WINAPI IDirectSoundImpl_CreateSoundBuffer(
1812 LPDIRECTSOUND iface,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER ppdsb,LPUNKNOWN lpunk
1814 ICOM_THIS(IDirectSoundImpl,iface);
1815 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
1816 LPWAVEFORMATEX wfex;
1817 HRESULT err = DS_OK;
1819 TRACE("(%p,%p,%p,%p)\n",This,dsbd,ippdsb,lpunk);
1821 if ((This == NULL) || (dsbd == NULL) || (ippdsb == NULL))
1822 return DSERR_INVALIDPARAM;
1824 if (TRACE_ON(dsound)) {
1825 TRACE("(structsize=%ld)\n",dsbd->dwSize);
1826 TRACE("(flags=0x%08lx:\n",dsbd->dwFlags);
1827 _dump_DSBCAPS(dsbd->dwFlags);
1829 TRACE("(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
1830 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
1833 wfex = dsbd->lpwfxFormat;
1836 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1837 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1838 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1839 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1840 wfex->wBitsPerSample, wfex->cbSize);
1842 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1844 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
1845 *ippdsb = primarybuf;
1846 primarybuf->dsbd.dwFlags = dsbd->dwFlags;
1848 } /* Else create primary buffer */
1851 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
1852 if (*ippdsb == NULL)
1853 return DSERR_OUTOFMEMORY;
1854 ICOM_VTBL(*ippdsb) = &dsbvt;
1856 (*ippdsb)->dsound = This;
1857 (*ippdsb)->parent = NULL;
1858 (*ippdsb)->buffer = NULL;
1860 memcpy(&((*ippdsb)->dsbd),dsbd,sizeof(*dsbd));
1861 if (dsbd->lpwfxFormat)
1862 memcpy(&((*ippdsb)->wfx), dsbd->lpwfxFormat, sizeof((*ippdsb)->wfx));
1864 TRACE("Created buffer at %p\n", *ippdsb);
1866 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1867 (*ippdsb)->buflen = dsound->wfx.nAvgBytesPerSec;
1868 (*ippdsb)->freq = dsound->wfx.nSamplesPerSec;
1870 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
1873 err = IDsDriver_CreateSoundBuffer(This->driver,wfex,dsbd->dwFlags,0,
1874 &((*ippdsb)->buflen),&((*ippdsb)->buffer),
1875 (LPVOID*)&((*ippdsb)->hwbuf));
1878 err = DSOUND_PrimaryOpen(*ippdsb);
1883 (*ippdsb)->buflen = dsbd->dwBufferBytes;
1884 (*ippdsb)->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1886 /* Check necessary hardware mixing capabilities */
1887 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1888 else capf |= DSCAPS_SECONDARYMONO;
1889 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1890 else capf |= DSCAPS_SECONDARY8BIT;
1891 use_hw = (This->drvcaps.dwFlags & capf) == capf;
1893 /* FIXME: check hardware sample rate mixing capabilities */
1894 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1895 /* FIXME: check whether any hardware buffers are left */
1896 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1898 /* Allocate system memory if applicable */
1899 if ((This->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1900 (*ippdsb)->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,(*ippdsb)->buflen);
1901 if ((*ippdsb)->buffer == NULL)
1902 err = DSERR_OUTOFMEMORY;
1905 /* Allocate the hardware buffer */
1906 if (use_hw && (err == DS_OK)) {
1907 err = IDsDriver_CreateSoundBuffer(This->driver,wfex,dsbd->dwFlags,0,
1908 &((*ippdsb)->buflen),&((*ippdsb)->buffer),
1909 (LPVOID*)&((*ippdsb)->hwbuf));
1914 if ((*ippdsb)->buffer)
1915 HeapFree(GetProcessHeap(),0,(*ippdsb)->buffer);
1916 HeapFree(GetProcessHeap(),0,(*ippdsb));
1920 /* calculate fragment size and write lead */
1921 DSOUND_RecalcFormat(*ippdsb);
1923 /* It's not necessary to initialize values to zero since */
1924 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1925 (*ippdsb)->playpos = 0;
1926 (*ippdsb)->mixpos = 0;
1927 (*ippdsb)->state = STATE_STOPPED;
1928 DSOUND_RecalcVolPan(&((*ippdsb)->volpan));
1930 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1931 (*ippdsb)->freqAdjust = ((*ippdsb)->freq << DSOUND_FREQSHIFT) /
1932 primarybuf->wfx.nSamplesPerSec;
1933 (*ippdsb)->nAvgBytesPerSec = (*ippdsb)->freq *
1934 dsbd->lpwfxFormat->nBlockAlign;
1937 EnterCriticalSection(&(This->lock));
1938 /* register buffer */
1939 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1940 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl*)*(This->nrofbuffers+1));
1942 This->buffers = newbuffers;
1943 This->buffers[This->nrofbuffers] = *ippdsb;
1944 This->nrofbuffers++;
1945 TRACE("buffer count is now %d\n", This->nrofbuffers);
1947 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
1948 err = DSERR_OUTOFMEMORY;
1951 LeaveCriticalSection(&(This->lock));
1953 IDirectSound_AddRef(iface);
1955 InitializeCriticalSection(&((*ippdsb)->lock));
1959 IDirectSoundBuffer_Release(*ppdsb);
1965 if (dsbd->dwFlags & DSBCAPS_CTRL3D) {
1966 IDirectSound3DBufferImpl *ds3db;
1968 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
1970 ICOM_VTBL(ds3db) = &ds3dbvt;
1972 (*ippdsb)->ds3db = ds3db;
1974 ds3db->dsb = (*ippdsb);
1975 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER)ippdsb);
1977 InitializeCriticalSection(&ds3db->lock);
1979 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1980 ds3db->ds3db.vPosition.x.x = 0.0;
1981 ds3db->ds3db.vPosition.y.y = 0.0;
1982 ds3db->ds3db.vPosition.z.z = 0.0;
1983 ds3db->ds3db.vVelocity.x.x = 0.0;
1984 ds3db->ds3db.vVelocity.y.y = 0.0;
1985 ds3db->ds3db.vVelocity.z.z = 0.0;
1986 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1987 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1988 ds3db->ds3db.vConeOrientation.x.x = 0.0;
1989 ds3db->ds3db.vConeOrientation.y.y = 0.0;
1990 ds3db->ds3db.vConeOrientation.z.z = 0.0;
1991 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1992 ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1993 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1994 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
1995 ds3db->buflen = ((*ippdsb)->buflen * primarybuf->wfx.nBlockAlign) /
1996 (*ippdsb)->wfx.nBlockAlign;
1997 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
1998 if (ds3db->buffer == NULL) {
2000 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
2007 static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer(
2008 LPDIRECTSOUND iface,LPDIRECTSOUNDBUFFER pdsb,LPLPDIRECTSOUNDBUFFER ppdsb
2010 ICOM_THIS(IDirectSoundImpl,iface);
2011 IDirectSoundBufferImpl* ipdsb=(IDirectSoundBufferImpl*)pdsb;
2012 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
2013 TRACE("(%p,%p,%p)\n",This,ipdsb,ippdsb);
2016 FIXME("need to duplicate hardware buffer\n");
2019 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
2021 IDirectSoundBuffer_AddRef(pdsb);
2022 memcpy(*ippdsb, ipdsb, sizeof(IDirectSoundBufferImpl));
2024 (*ippdsb)->playpos = 0;
2025 (*ippdsb)->mixpos = 0;
2026 (*ippdsb)->dsound = This;
2027 (*ippdsb)->parent = ipdsb;
2028 memcpy(&((*ippdsb)->wfx), &(ipdsb->wfx), sizeof((*ippdsb)->wfx));
2029 /* register buffer */
2030 EnterCriticalSection(&(This->lock));
2032 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
2034 This->buffers = newbuffers;
2035 This->buffers[This->nrofbuffers] = *ippdsb;
2036 This->nrofbuffers++;
2037 TRACE("buffer count is now %d\n", This->nrofbuffers);
2039 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
2040 /* FIXME: release buffer */
2043 LeaveCriticalSection(&(This->lock));
2044 IDirectSound_AddRef(iface);
2049 static HRESULT WINAPI IDirectSoundImpl_GetCaps(LPDIRECTSOUND iface,LPDSCAPS caps) {
2050 ICOM_THIS(IDirectSoundImpl,iface);
2051 TRACE("(%p,%p)\n",This,caps);
2052 TRACE("(flags=0x%08lx)\n",caps->dwFlags);
2055 return DSERR_INVALIDPARAM;
2057 /* We should check this value, not set it. See Inside DirectX, p215. */
2058 caps->dwSize = sizeof(*caps);
2060 caps->dwFlags = This->drvcaps.dwFlags;
2062 /* FIXME: copy caps from This->drvcaps */
2063 caps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
2064 caps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
2066 caps->dwPrimaryBuffers = 1;
2068 caps->dwMaxHwMixingAllBuffers = 0;
2069 caps->dwMaxHwMixingStaticBuffers = 0;
2070 caps->dwMaxHwMixingStreamingBuffers = 0;
2072 caps->dwFreeHwMixingAllBuffers = 0;
2073 caps->dwFreeHwMixingStaticBuffers = 0;
2074 caps->dwFreeHwMixingStreamingBuffers = 0;
2076 caps->dwMaxHw3DAllBuffers = 0;
2077 caps->dwMaxHw3DStaticBuffers = 0;
2078 caps->dwMaxHw3DStreamingBuffers = 0;
2080 caps->dwFreeHw3DAllBuffers = 0;
2081 caps->dwFreeHw3DStaticBuffers = 0;
2082 caps->dwFreeHw3DStreamingBuffers = 0;
2084 caps->dwTotalHwMemBytes = 0;
2086 caps->dwFreeHwMemBytes = 0;
2088 caps->dwMaxContigFreeHwMemBytes = 0;
2090 caps->dwUnlockTransferRateHwBuffers = 4096; /* But we have none... */
2092 caps->dwPlayCpuOverheadSwBuffers = 1; /* 1% */
2097 static ULONG WINAPI IDirectSoundImpl_AddRef(LPDIRECTSOUND iface) {
2098 ICOM_THIS(IDirectSoundImpl,iface);
2099 return ++(This->ref);
2102 static ULONG WINAPI IDirectSoundImpl_Release(LPDIRECTSOUND iface) {
2103 ICOM_THIS(IDirectSoundImpl,iface);
2104 TRACE("(%p), ref was %ld\n",This,This->ref);
2105 if (!--(This->ref)) {
2108 timeKillEvent(This->timerID);
2109 timeEndPeriod(DS_TIME_RES);
2112 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)primarybuf);
2114 if (This->buffers) {
2115 for( i=0;i<This->nrofbuffers;i++)
2116 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->buffers[i]);
2120 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->primary);
2122 DeleteCriticalSection(&This->lock);
2124 IDsDriver_Close(This->driver);
2127 for (c=0; c<DS_HEL_FRAGS; c++)
2128 HeapFree(GetProcessHeap(),0,This->pwave[c]);
2130 if (This->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
2131 waveOutClose(This->hwo);
2134 IDsDriver_Release(This->driver);
2136 HeapFree(GetProcessHeap(),0,This);
2143 static HRESULT WINAPI IDirectSoundImpl_SetSpeakerConfig(
2144 LPDIRECTSOUND iface,DWORD config
2146 ICOM_THIS(IDirectSoundImpl,iface);
2147 FIXME("(%p,0x%08lx):stub\n",This,config);
2151 static HRESULT WINAPI IDirectSoundImpl_QueryInterface(
2152 LPDIRECTSOUND iface,REFIID riid,LPVOID *ppobj
2154 ICOM_THIS(IDirectSoundImpl,iface);
2156 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
2158 if (This->listener) {
2159 *ppobj = This->listener;
2160 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This->listener);
2164 This->listener = (IDirectSound3DListenerImpl*)HeapAlloc(
2165 GetProcessHeap(), 0, sizeof(*(This->listener)));
2166 This->listener->ref = 1;
2167 ICOM_VTBL(This->listener) = &ds3dlvt;
2168 *ppobj = (LPVOID)This->listener;
2169 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
2171 This->listener->dsb = NULL;
2173 This->listener->ds3dl.dwSize = sizeof(DS3DLISTENER);
2174 This->listener->ds3dl.vPosition.u1.x = 0.0;
2175 This->listener->ds3dl.vPosition.u2.y = 0.0;
2176 This->listener->ds3dl.vPosition.u3.z = 0.0;
2177 This->listener->ds3dl.vVelocity.u1.x = 0.0;
2178 This->listener->ds3dl.vVelocity.u2.y = 0.0;
2179 This->listener->ds3dl.vVelocity.u3.z = 0.0;
2180 This->listener->ds3dl.vOrientFront.u1.x = 0.0;
2181 This->listener->ds3dl.vOrientFront.u2.y = 0.0;
2182 This->listener->ds3dl.vOrientFront.u3.z = 1.0;
2183 This->listener->ds3dl.vOrientTop.u1.x = 0.0;
2184 This->listener->ds3dl.vOrientTop.u2.y = 1.0;
2185 This->listener->ds3dl.vOrientTop.u3.z = 0.0;
2186 This->listener->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
2187 This->listener->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
2188 This->listener->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
2190 InitializeCriticalSection(&This->listener->lock);
2195 FIXME("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
2199 static HRESULT WINAPI IDirectSoundImpl_Compact(
2200 LPDIRECTSOUND iface)
2202 ICOM_THIS(IDirectSoundImpl,iface);
2203 TRACE("(%p)\n", This);
2207 static HRESULT WINAPI IDirectSoundImpl_GetSpeakerConfig(
2208 LPDIRECTSOUND iface,
2209 LPDWORD lpdwSpeakerConfig)
2211 ICOM_THIS(IDirectSoundImpl,iface);
2212 TRACE("(%p, %p)\n", This, lpdwSpeakerConfig);
2213 *lpdwSpeakerConfig = DSSPEAKER_STEREO | (DSSPEAKER_GEOMETRY_NARROW << 16);
2217 static HRESULT WINAPI IDirectSoundImpl_Initialize(
2218 LPDIRECTSOUND iface,
2221 ICOM_THIS(IDirectSoundImpl,iface);
2222 TRACE("(%p, %p)\n", This, lpcGuid);
2226 static ICOM_VTABLE(IDirectSound) dsvt =
2228 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2229 IDirectSoundImpl_QueryInterface,
2230 IDirectSoundImpl_AddRef,
2231 IDirectSoundImpl_Release,
2232 IDirectSoundImpl_CreateSoundBuffer,
2233 IDirectSoundImpl_GetCaps,
2234 IDirectSoundImpl_DuplicateSoundBuffer,
2235 IDirectSoundImpl_SetCooperativeLevel,
2236 IDirectSoundImpl_Compact,
2237 IDirectSoundImpl_GetSpeakerConfig,
2238 IDirectSoundImpl_SetSpeakerConfig,
2239 IDirectSoundImpl_Initialize
2243 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
2247 LPDSBPOSITIONNOTIFY event;
2249 if (dsb->nrofnotifies == 0)
2252 TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
2253 dsb, dsb->buflen, dsb->playpos, len);
2254 for (i = 0; i < dsb->nrofnotifies ; i++) {
2255 event = dsb->notifies + i;
2256 offset = event->dwOffset;
2257 TRACE("checking %d, position %ld, event = %d\n",
2258 i, offset, event->hEventNotify);
2259 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
2260 /* OK. [Inside DirectX, p274] */
2262 /* This also means we can't sort the entries by offset, */
2263 /* because DSBPN_OFFSETSTOP == -1 */
2264 if (offset == DSBPN_OFFSETSTOP) {
2265 if (dsb->state == STATE_STOPPED) {
2266 SetEvent(event->hEventNotify);
2267 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2272 if ((dsb->playpos + len) >= dsb->buflen) {
2273 if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
2274 (offset >= dsb->playpos)) {
2275 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2276 SetEvent(event->hEventNotify);
2279 if ((offset >= dsb->playpos) && (offset < (dsb->playpos + len))) {
2280 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2281 SetEvent(event->hEventNotify);
2287 /* WAV format info can be found at: */
2289 /* http://www.cwi.nl/ftp/audio/AudioFormats.part2 */
2290 /* ftp://ftp.cwi.nl/pub/audio/RIFF-format */
2292 /* Import points to remember: */
2294 /* 8-bit WAV is unsigned */
2295 /* 16-bit WAV is signed */
2297 static inline INT16 cvtU8toS16(BYTE byte)
2299 INT16 s = (byte - 128) << 8;
2304 static inline BYTE cvtS16toU8(INT16 word)
2306 BYTE b = (word + 32768) >> 8;
2312 /* We should be able to optimize these two inline functions */
2313 /* so that we aren't doing 8->16->8 conversions when it is */
2314 /* not necessary. But this is still a WIP. Optimize later. */
2315 static inline void get_fields(const IDirectSoundBufferImpl *dsb, BYTE *buf, INT *fl, INT *fr)
2317 INT16 *bufs = (INT16 *) buf;
2319 /* TRACE("(%p)", buf); */
2320 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 2) {
2321 *fl = cvtU8toS16(*buf);
2322 *fr = cvtU8toS16(*(buf + 1));
2326 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 2) {
2332 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 1) {
2333 *fl = cvtU8toS16(*buf);
2338 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 1) {
2344 FIXME("get_fields found an unsupported configuration\n");
2348 static inline void set_fields(BYTE *buf, INT fl, INT fr)
2350 INT16 *bufs = (INT16 *) buf;
2352 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 2)) {
2353 *buf = cvtS16toU8(fl);
2354 *(buf + 1) = cvtS16toU8(fr);
2358 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 2)) {
2364 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 1)) {
2365 *buf = cvtS16toU8((fl + fr) >> 1);
2369 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 1)) {
2370 *bufs = (fl + fr) >> 1;
2373 FIXME("set_fields found an unsupported configuration\n");
2377 /* Now with PerfectPitch (tm) technology */
2378 static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2380 INT i, size, ipos, ilen, fieldL, fieldR;
2382 INT iAdvance = dsb->wfx.nBlockAlign;
2383 INT oAdvance = primarybuf->wfx.nBlockAlign;
2385 ibp = dsb->buffer + dsb->mixpos;
2388 TRACE("(%p, %p, %p), mixpos=%ld\n", dsb, ibp, obp, dsb->mixpos);
2389 /* Check for the best case */
2390 if ((dsb->freq == primarybuf->wfx.nSamplesPerSec) &&
2391 (dsb->wfx.wBitsPerSample == primarybuf->wfx.wBitsPerSample) &&
2392 (dsb->wfx.nChannels == primarybuf->wfx.nChannels)) {
2393 DWORD bytesleft = dsb->buflen - dsb->mixpos;
2394 TRACE("(%p) Best case\n", dsb);
2395 if (len <= bytesleft )
2396 memcpy(obp, ibp, len);
2398 memcpy(obp, ibp, bytesleft );
2399 memcpy(obp + bytesleft, dsb->buffer, len - bytesleft);
2404 /* Check for same sample rate */
2405 if (dsb->freq == primarybuf->wfx.nSamplesPerSec) {
2406 TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb,
2407 dsb->freq, primarybuf->wfx.nSamplesPerSec);
2409 for (i = 0; i < len; i += oAdvance) {
2410 get_fields(dsb, ibp, &fieldL, &fieldR);
2413 set_fields(obp, fieldL, fieldR);
2415 if (ibp >= (BYTE *)(dsb->buffer + dsb->buflen))
2416 ibp = dsb->buffer; /* wrap */
2421 /* Mix in different sample rates */
2423 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
2424 /* Patent Pending :-] */
2426 TRACE("(%p) Adjusting frequency: %ld -> %ld\n",
2427 dsb, dsb->freq, primarybuf->wfx.nSamplesPerSec);
2429 size = len / oAdvance;
2430 ilen = ((size * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance;
2431 for (i = 0; i < size; i++) {
2433 ipos = (((i * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance) + dsb->mixpos;
2435 if (ipos >= dsb->buflen)
2436 ipos %= dsb->buflen; /* wrap */
2438 get_fields(dsb, (dsb->buffer + ipos), &fieldL, &fieldR);
2439 set_fields(obp, fieldL, fieldR);
2445 static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2447 INT i, inc = primarybuf->wfx.wBitsPerSample >> 3;
2449 INT16 *bps = (INT16 *) buf;
2451 TRACE("(%p) left = %lx, right = %lx\n", dsb,
2452 dsb->volpan.dwTotalLeftAmpFactor, dsb->volpan.dwTotalRightAmpFactor);
2453 if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
2454 (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
2455 !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
2456 return; /* Nothing to do */
2458 /* If we end up with some bozo coder using panning or 3D sound */
2459 /* with a mono primary buffer, it could sound very weird using */
2460 /* this method. Oh well, tough patooties. */
2462 for (i = 0; i < len; i += inc) {
2468 /* 8-bit WAV is unsigned, but we need to operate */
2469 /* on signed data for this to work properly */
2471 val = ((val * (i & inc ? dsb->volpan.dwTotalRightAmpFactor : dsb->volpan.dwTotalLeftAmpFactor)) >> 16);
2476 /* 16-bit WAV is signed -- much better */
2478 val = ((val * ((i & inc) ? dsb->volpan.dwTotalRightAmpFactor : dsb->volpan.dwTotalLeftAmpFactor)) >> 16);
2484 FIXME("MixerVol had a nasty error\n");
2490 static void DSOUND_Mixer3D(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2493 DWORD buflen, mixpos;
2495 buflen = dsb->ds3db->buflen;
2496 mixpos = (dsb->mixpos * primarybuf->wfx.nBlockAlign) / dsb->wfx.nBlockAlign;
2497 ibp = dsb->ds3db->buffer + mixpos;
2500 if (mixpos > buflen) {
2501 FIXME("Major breakage");
2505 if (len <= (mixpos + buflen))
2506 memcpy(obp, ibp, len);
2508 memcpy(obp, ibp, buflen - mixpos);
2509 memcpy(obp + (buflen - mixpos),
2511 len - (buflen - mixpos));
2517 static void *tmp_buffer;
2518 static size_t tmp_buffer_len = 0;
2520 static void *DSOUND_tmpbuffer(size_t len)
2522 if (len>tmp_buffer_len) {
2523 void *new_buffer = realloc(tmp_buffer, len);
2525 tmp_buffer = new_buffer;
2526 tmp_buffer_len = len;
2533 static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
2535 INT i, len, ilen, temp, field;
2536 INT advance = primarybuf->wfx.wBitsPerSample >> 3;
2537 BYTE *buf, *ibuf, *obuf;
2538 INT16 *ibufs, *obufs;
2541 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2542 temp = MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->buflen,
2543 dsb->nAvgBytesPerSec) -
2544 MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->mixpos,
2545 dsb->nAvgBytesPerSec);
2546 len = (len > temp) ? temp : len;
2548 len &= ~3; /* 4 byte alignment */
2551 /* This should only happen if we aren't looping and temp < 4 */
2553 /* We skip the remainder, so check for possible events */
2554 DSOUND_CheckEvent(dsb, dsb->buflen - dsb->mixpos);
2556 dsb->state = STATE_STOPPED;
2559 dsb->leadin = FALSE;
2560 /* Check for DSBPN_OFFSETSTOP */
2561 DSOUND_CheckEvent(dsb, 0);
2565 /* Been seeing segfaults in malloc() for some reason... */
2566 TRACE("allocating buffer (size = %d)\n", len);
2567 if ((buf = ibuf = (BYTE *) DSOUND_tmpbuffer(len)) == NULL)
2570 TRACE("MixInBuffer (%p) len = %d, dest = %ld\n", dsb, len, writepos);
2572 ilen = DSOUND_MixerNorm(dsb, ibuf, len);
2573 if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
2574 (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
2575 DSOUND_MixerVol(dsb, ibuf, len);
2577 obuf = primarybuf->buffer + writepos;
2578 for (i = 0; i < len; i += advance) {
2579 obufs = (INT16 *) obuf;
2580 ibufs = (INT16 *) ibuf;
2581 if (primarybuf->wfx.wBitsPerSample == 8) {
2582 /* 8-bit WAV is unsigned */
2583 field = (*ibuf - 128);
2584 field += (*obuf - 128);
2585 field = field > 127 ? 127 : field;
2586 field = field < -128 ? -128 : field;
2587 *obuf = field + 128;
2589 /* 16-bit WAV is signed */
2592 field = field > 32767 ? 32767 : field;
2593 field = field < -32768 ? -32768 : field;
2598 if (obuf >= (BYTE *)(primarybuf->buffer + primarybuf->buflen))
2599 obuf = primarybuf->buffer;
2603 if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY)
2604 DSOUND_CheckEvent(dsb, ilen);
2606 if (dsb->leadin && (dsb->startpos > dsb->mixpos) && (dsb->startpos <= dsb->mixpos + ilen)) {
2607 /* HACK... leadin should be reset when the PLAY position reaches the startpos,
2608 * not the MIX position... but if the sound buffer is bigger than our prebuffering
2609 * (which must be the case for the streaming buffers that need this hack anyway)
2610 * plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
2611 dsb->leadin = FALSE;
2614 dsb->mixpos += ilen;
2616 if (dsb->mixpos >= dsb->buflen) {
2617 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2618 dsb->state = STATE_STOPPED;
2621 dsb->leadin = FALSE;
2622 DSOUND_CheckEvent(dsb, 0); /* For DSBPN_OFFSETSTOP */
2625 while (dsb->mixpos >= dsb->buflen)
2626 dsb->mixpos -= dsb->buflen;
2627 if (dsb->leadin && (dsb->startpos <= dsb->mixpos))
2628 dsb->leadin = FALSE; /* HACK: see above */
2635 static DWORD WINAPI DSOUND_MixPrimary(DWORD writepos, DWORD fraglen, BOOL starting)
2637 INT i, len, maxlen = 0;
2638 IDirectSoundBufferImpl *dsb;
2640 TRACE("(%ld,%ld,%d)\n", writepos, fraglen, starting);
2641 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
2642 dsb = dsound->buffers[i];
2644 if (!dsb || !(ICOM_VTBL(dsb)))
2646 if (dsb->buflen && dsb->state && !(starting && (dsb->state != STATE_STARTING))) {
2647 TRACE("Checking %p\n", dsb);
2648 EnterCriticalSection(&(dsb->lock));
2649 if (dsb->state == STATE_STOPPING) {
2650 /* FIXME: perhaps attempt to remove the buffer from the prebuffer */
2651 dsb->state = STATE_STOPPED;
2653 len = DSOUND_MixInBuffer(dsb, writepos, fraglen);
2654 maxlen = len > maxlen ? len : maxlen;
2656 LeaveCriticalSection(&(dsb->lock));
2663 static void WINAPI DSOUND_MarkPlaying(void)
2666 IDirectSoundBufferImpl *dsb;
2668 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
2669 dsb = dsound->buffers[i];
2671 if (!dsb || !(ICOM_VTBL(dsb)))
2673 if (dsb->buflen && (dsb->state == STATE_STARTING))
2674 dsb->state = STATE_PLAYING;
2678 static void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
2684 if (!dsound || !primarybuf) {
2685 ERR("dsound died without killing us?\n");
2686 timeKillEvent(timerID);
2687 timeEndPeriod(DS_TIME_RES);
2691 EnterCriticalSection(&(dsound->lock));
2693 if (!primarybuf || !primarybuf->ref) {
2694 /* seems the primary buffer is currently being released */
2695 LeaveCriticalSection(&(dsound->lock));
2699 /* the sound of silence */
2700 nfiller = primarybuf->wfx.wBitsPerSample == 8 ? 128 : 0;
2702 /* whether the primary is forced to play even without secondary buffers */
2703 forced = ((primarybuf->state == STATE_PLAYING) || (primarybuf->state == STATE_STARTING));
2705 if (primarybuf->hwbuf) {
2706 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2707 BOOL paused = ((primarybuf->state == STATE_STOPPED) || (primarybuf->state == STATE_STARTING));
2708 DWORD playpos, writepos, inq, maxq, mixq, frag;
2709 IDsDriverBuffer_GetPosition(primarybuf->hwbuf, &playpos, &writepos);
2710 /* Well, we *could* do Just-In-Time mixing using the writepos,
2711 * but that's a little bit ambitious and unnecessary... */
2712 /* rather add our safety margin to the writepos, if we're playing */
2714 writepos += primarybuf->writelead;
2715 while (writepos >= primarybuf->buflen)
2716 writepos -= primarybuf->buflen;
2717 } else writepos = playpos;
2718 TRACE("primary playpos=%ld, writepos=%ld, clrpos=%ld, mixpos=%ld\n",
2719 playpos,writepos,primarybuf->playpos,primarybuf->mixpos);
2720 /* wipe out just-played sound data */
2721 if (playpos < primarybuf->playpos) {
2722 memset(primarybuf->buffer + primarybuf->playpos, nfiller, primarybuf->buflen - primarybuf->playpos);
2723 memset(primarybuf->buffer, nfiller, playpos);
2725 memset(primarybuf->buffer + primarybuf->playpos, nfiller, playpos - primarybuf->playpos);
2727 primarybuf->playpos = playpos;
2729 /* check how much prebuffering is left */
2730 inq = primarybuf->mixpos;
2732 inq += primarybuf->buflen;
2735 /* find the maximum we can prebuffer */
2738 if (maxq < writepos)
2739 maxq += primarybuf->buflen;
2741 } else maxq = primarybuf->buflen;
2743 /* clip maxq to DS_HAL_QUEUE */
2744 frag = DS_HAL_QUEUE * dsound->fraglen;
2745 if (maxq > frag) maxq = frag;
2747 EnterCriticalSection(&(primarybuf->lock));
2749 /* check for consistency */
2751 /* the playback position must have passed our last
2752 * mixed position, i.e. it's an underrun, or we have
2753 * nothing more to play */
2755 /* stop the playback now, to allow buffers to refill */
2756 IDsDriverBuffer_Stop(primarybuf->hwbuf);
2757 if (primarybuf->state == STATE_PLAYING) {
2758 primarybuf->state = STATE_STARTING;
2760 else if (primarybuf->state == STATE_STOPPING) {
2761 primarybuf->state = STATE_STOPPED;
2764 /* how can we have an underrun if we aren't playing? */
2765 ERR("unexpected primary state (%ld)\n", primarybuf->state);
2767 /* the Stop is supposed to reset play position to beginning of buffer */
2768 /* unfortunately, OSS is not able to do so, so get current pointer */
2769 IDsDriverBuffer_GetPosition(primarybuf->hwbuf, &playpos, NULL);
2771 primarybuf->playpos = playpos;
2772 primarybuf->mixpos = playpos;
2774 maxq = primarybuf->buflen;
2775 if (maxq > frag) maxq = frag;
2776 memset(primarybuf->buffer, nfiller, primarybuf->buflen);
2780 /* see if some new buffers have been started that we want to merge into our prebuffer;
2781 * this should minimize latency even when we have a large prebuffer */
2783 if (primarybuf->mixpos < writepos) {
2784 /* mix to end of buffer */
2785 len = DSOUND_MixPrimary(writepos, primarybuf->buflen - writepos, TRUE);
2786 if ((len + writepos) < primarybuf->buflen)
2787 goto addmix_complete;
2788 /* mix from beginning of buffer */
2789 if (primarybuf->mixpos)
2790 len = DSOUND_MixPrimary(0, primarybuf->mixpos, TRUE);
2792 /* mix middle of buffer */
2793 len = DSOUND_MixPrimary(writepos, primarybuf->mixpos - writepos, TRUE);
2797 DSOUND_MarkPlaying();
2800 TRACE("queued %ld, max %ld, mixing %ld, paused %d\n", inq, maxq, mixq, paused);
2801 /* it's too inefficient to mix less than a fragment at a time */
2802 if (mixq >= dsound->fraglen) {
2804 #define FRAG_MIXER \
2805 if (frag > mixq) frag = mixq; \
2806 len = DSOUND_MixPrimary(primarybuf->mixpos, frag, FALSE); \
2807 if (forced) len = frag; \
2808 primarybuf->mixpos += len; \
2809 mixq -= len; inq += len
2811 if ((playpos < writepos) || (paused && (playpos == writepos))) {
2812 if (primarybuf->mixpos) {
2813 /* mix to end of buffer */
2814 frag = primarybuf->buflen - primarybuf->mixpos;
2816 if (primarybuf->mixpos < primarybuf->buflen)
2818 primarybuf->mixpos = 0;
2820 if (mixq >= dsound->fraglen) {
2821 /* mix from beginning of buffer */
2823 if ((!frag) && paused) frag = primarybuf->buflen;
2827 else if (playpos > writepos) {
2828 /* mix middle of buffer */
2829 frag = playpos - primarybuf->mixpos;
2833 /* this should preferably not happen... */
2834 ERR("mixer malfunction (ambiguous writepos)!\n");
2840 /* buffers have been filled, restart playback */
2841 if (primarybuf->state == STATE_STARTING) {
2842 IDsDriverBuffer_Play(primarybuf->hwbuf, 0, 0, DSBPLAY_LOOPING);
2843 primarybuf->state = STATE_PLAYING;
2845 else if (primarybuf->state == STATE_STOPPED) {
2846 /* the primarybuf is supposed to play if there's something to play
2847 * even if it is reported as stopped, so don't let this confuse you */
2848 IDsDriverBuffer_Play(primarybuf->hwbuf, 0, 0, DSBPLAY_LOOPING);
2849 primarybuf->state = STATE_STOPPING;
2852 LeaveCriticalSection(&(primarybuf->lock));
2854 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
2855 if (primarybuf->state == STATE_STARTING) {
2856 IDsDriverBuffer_Play(primarybuf->hwbuf, 0, 0, DSBPLAY_LOOPING);
2857 primarybuf->state = STATE_PLAYING;
2859 else if (primarybuf->state == STATE_STOPPING) {
2860 IDsDriverBuffer_Stop(primarybuf->hwbuf);
2861 primarybuf->state = STATE_STOPPED;
2865 /* using waveOut stuff */
2866 /* if no buffers are playing, we should be in pause mode now */
2868 /* clean out completed fragments */
2869 while (dsound->pwqueue && (dsound->pwave[dsound->pwplay]->dwFlags & WHDR_DONE)) {
2870 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2871 DWORD pos = dsound->pwplay * dsound->fraglen;
2872 TRACE("done playing primary pos=%ld\n",pos);
2873 memset(primarybuf->buffer + pos, nfiller, dsound->fraglen);
2876 if (dsound->pwplay >= DS_HEL_FRAGS) dsound->pwplay = 0;
2879 primarybuf->playpos = dsound->pwplay * dsound->fraglen;
2880 TRACE("primary playpos=%ld, mixpos=%ld\n",primarybuf->playpos,primarybuf->mixpos);
2881 EnterCriticalSection(&(primarybuf->lock));
2882 if (!dsound->pwqueue) {
2883 /* this is either an underrun or we have nothing more to play...
2884 * since playback has already stopped now, we can enter pause mode,
2885 * in order to allow buffers to refill */
2886 if (primarybuf->state == STATE_PLAYING) {
2887 waveOutPause(dsound->hwo);
2888 primarybuf->state = STATE_STARTING;
2890 else if (primarybuf->state == STATE_STOPPING) {
2891 waveOutPause(dsound->hwo);
2892 primarybuf->state = STATE_STOPPED;
2896 /* find next write position, plus some extra margin */
2897 writepos = primarybuf->playpos + DS_HEL_MARGIN * dsound->fraglen;
2898 while (writepos >= primarybuf->buflen) writepos -= primarybuf->buflen;
2900 /* see if some new buffers have been started that we want to merge into our prebuffer;
2901 * this should minimize latency even when we have a large prebuffer */
2902 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2903 if ((primarybuf->state == STATE_PLAYING) || (primarybuf->state == STATE_STOPPING)) {
2904 while (writepos != primarybuf->mixpos) {
2905 len = DSOUND_MixPrimary(writepos, dsound->fraglen, TRUE);
2907 writepos += dsound->fraglen;
2908 if (writepos >= primarybuf->buflen)
2909 writepos -= primarybuf->buflen;
2912 DSOUND_MarkPlaying();
2915 /* we want at least DS_HEL_QUEUE fragments in the prebuffer outqueue;
2916 * mix a bunch of fragments now as necessary */
2917 while (dsound->pwqueue < DS_HEL_QUEUE) {
2918 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2919 len = DSOUND_MixPrimary(primarybuf->mixpos, dsound->fraglen, FALSE);
2921 if (forced) len = dsound->fraglen;
2922 /* if we have nothing to play, don't bother to */
2924 if (len < dsound->fraglen) {
2925 TRACE("len=%ld is less than fraglen=%ld\n",len,dsound->fraglen);
2927 /* ok, we have something to play */
2928 /* advance mix positions */
2929 primarybuf->mixpos += dsound->fraglen;
2930 if (primarybuf->mixpos >= primarybuf->buflen)
2931 primarybuf->mixpos -= primarybuf->buflen;
2933 waveOutWrite(dsound->hwo, dsound->pwave[dsound->pwwrite], sizeof(WAVEHDR));
2935 if (dsound->pwwrite >= DS_HEL_FRAGS) dsound->pwwrite = 0;
2938 if (dsound->pwqueue) {
2939 /* buffers have been filled, restart playback */
2940 if (primarybuf->state == STATE_STARTING) {
2941 waveOutRestart(dsound->hwo);
2942 primarybuf->state = STATE_PLAYING;
2944 else if (primarybuf->state == STATE_STOPPED) {
2945 /* the primarybuf is supposed to play if there's something to play
2946 * even if it is reported as stopped, so don't let this confuse you */
2947 waveOutRestart(dsound->hwo);
2948 primarybuf->state = STATE_STOPPING;
2951 LeaveCriticalSection(&(primarybuf->lock));
2953 LeaveCriticalSection(&(dsound->lock));
2956 /*******************************************************************************
2959 HRESULT WINAPI DirectSoundCreate(REFGUID lpGUID,LPDIRECTSOUND *ppDS,IUnknown *pUnkOuter )
2961 IDirectSoundImpl** ippDS=(IDirectSoundImpl**)ppDS;
2962 PIDSDRIVER drv = NULL;
2965 HRESULT err = DS_OK;
2968 TRACE("(%p,%p,%p)\n",lpGUID,ippDS,pUnkOuter);
2970 TRACE("DirectSoundCreate (%p)\n", ippDS);
2973 return DSERR_INVALIDPARAM;
2976 IDirectSound_AddRef((LPDIRECTSOUND)dsound);
2981 /* Enumerate WINMM audio devices and find the one we want */
2982 wodn = waveOutGetNumDevs();
2983 if (!wodn) return DSERR_NODRIVER;
2985 /* FIXME: How do we find the GUID of an audio device? */
2986 /* Well, just use the first available device for now... */
2988 /* Get output device caps */
2989 waveOutGetDevCapsA(wod, &wcaps, sizeof(wcaps));
2990 /* 0x810 is a "Wine extension" to get the DSound interface */
2991 waveOutMessage(wod, 0x810, (DWORD)&drv, 0);
2993 /* Allocate memory */
2994 *ippDS = (IDirectSoundImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundImpl));
2996 return DSERR_OUTOFMEMORY;
2998 ICOM_VTBL(*ippDS) = &dsvt;
3001 (*ippDS)->driver = drv;
3002 (*ippDS)->fraglen = 0;
3003 (*ippDS)->priolevel = DSSCL_NORMAL;
3004 (*ippDS)->nrofbuffers = 0;
3005 (*ippDS)->buffers = NULL;
3006 (*ippDS)->primary = NULL;
3007 (*ippDS)->listener = NULL;
3009 /* Get driver description */
3011 IDsDriver_GetDriverDesc(drv,&((*ippDS)->drvdesc));
3013 /* if no DirectSound interface available, use WINMM API instead */
3014 (*ippDS)->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
3015 (*ippDS)->drvdesc.dnDevNode = wod; /* FIXME? */
3018 /* Set default wave format (may need it for waveOutOpen) */
3019 (*ippDS)->wfx.wFormatTag = WAVE_FORMAT_PCM;
3020 (*ippDS)->wfx.nChannels = 2;
3021 (*ippDS)->wfx.nSamplesPerSec = 22050;
3022 (*ippDS)->wfx.nAvgBytesPerSec = 44100;
3023 (*ippDS)->wfx.nBlockAlign = 2;
3024 (*ippDS)->wfx.wBitsPerSample = 8;
3026 /* If the driver requests being opened through MMSYSTEM
3027 * (which is recommended by the DDK), it is supposed to happen
3028 * before the DirectSound interface is opened */
3029 if ((*ippDS)->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
3030 /* FIXME: is this right? */
3031 err = mmErr(waveOutOpen(&((*ippDS)->hwo), (*ippDS)->drvdesc.dnDevNode, &((*ippDS)->wfx),
3032 0, 0, CALLBACK_NULL | WAVE_DIRECTSOUND));
3035 if (drv && (err == DS_OK))
3036 err = IDsDriver_Open(drv);
3038 /* FIXME: do we want to handle a temporarily busy device? */
3040 HeapFree(GetProcessHeap(),0,*ippDS);
3045 /* the driver is now open, so it's now allowed to call GetCaps */
3047 IDsDriver_GetCaps(drv,&((*ippDS)->drvcaps));
3051 /* FIXME: look at wcaps */
3052 (*ippDS)->drvcaps.dwFlags = DSCAPS_EMULDRIVER |
3053 DSCAPS_PRIMARY16BIT | DSCAPS_PRIMARYSTEREO;
3055 /* Allocate memory for HEL buffer headers */
3056 for (c=0; c<DS_HEL_FRAGS; c++) {
3057 (*ippDS)->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
3058 if (!(*ippDS)->pwave[c]) {
3059 /* Argh, out of memory */
3061 HeapFree(GetProcessHeap(),0,(*ippDS)->pwave[c]);
3062 waveOutClose((*ippDS)->hwo);
3063 HeapFree(GetProcessHeap(),0,*ippDS);
3065 return DSERR_OUTOFMEMORY;
3071 InitializeCriticalSection(&((*ippDS)->lock));
3075 if (primarybuf == NULL) {
3079 dsbd.dwSize = sizeof(DSBUFFERDESC);
3080 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_GETCURRENTPOSITION2;
3081 dsbd.dwBufferBytes = 0;
3082 dsbd.lpwfxFormat = &(dsound->wfx);
3083 hr = IDirectSound_CreateSoundBuffer(*ppDS, &dsbd, (LPDIRECTSOUNDBUFFER*)&primarybuf, NULL);
3087 /* dsound->primary is NULL - don't need to Release */
3088 dsound->primary = primarybuf;
3089 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
3091 timeBeginPeriod(DS_TIME_RES);
3092 dsound->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, DSOUND_timer,
3093 (DWORD)dsound, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
3098 /***************************************************************************
3099 * DirectSoundCaptureCreate [DSOUND.6]
3101 * Create and initialize a DirectSoundCapture interface
3105 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
3108 HRESULT WINAPI DirectSoundCaptureCreate(
3110 LPDIRECTSOUNDCAPTURE* lplpDSC,
3111 LPUNKNOWN pUnkOuter )
3113 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), lplpDSC, pUnkOuter);
3116 return DSERR_NOAGGREGATION;
3119 /* Default device? */
3121 return DSOUND_CreateDirectSoundCapture( (LPVOID*)lplpDSC );
3124 FIXME( "Unknown GUID %s\n", debugstr_guid(lpcGUID) );
3127 return DSERR_OUTOFMEMORY;
3130 /***************************************************************************
3131 * DirectSoundCaptureEnumerateA [DSOUND.7]
3133 * Enumerate all DirectSound drivers installed in the system
3137 * Failure: DSERR_INVALIDPARAM
3139 HRESULT WINAPI DirectSoundCaptureEnumerateA(
3140 LPDSENUMCALLBACKA lpDSEnumCallback,
3143 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
3145 if ( lpDSEnumCallback )
3146 lpDSEnumCallback(NULL,"WINE Primary Sound Capture Driver",
3147 "SoundCap",lpContext);
3153 /***************************************************************************
3154 * DirectSoundCaptureEnumerateW [DSOUND.8]
3156 * Enumerate all DirectSound drivers installed in the system
3160 * Failure: DSERR_INVALIDPARAM
3162 HRESULT WINAPI DirectSoundCaptureEnumerateW(
3163 LPDSENUMCALLBACKW lpDSEnumCallback,
3166 FIXME("(%p,%p):stub\n", lpDSEnumCallback, lpContext );
3171 DSOUND_CreateDirectSoundCapture( LPVOID* ppobj )
3173 *ppobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( IDirectSoundCaptureImpl ) );
3175 if ( *ppobj == NULL ) {
3176 return DSERR_OUTOFMEMORY;
3180 ICOM_THIS(IDirectSoundCaptureImpl,*ppobj);
3183 ICOM_VTBL(This) = &dscvt;
3185 InitializeCriticalSection( &This->lock );
3192 IDirectSoundCaptureImpl_QueryInterface(
3193 LPDIRECTSOUNDCAPTURE iface,
3197 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3199 FIXME( "(%p)->(%s,%p): stub\n", This, debugstr_guid(riid), ppobj );
3205 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
3208 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3210 EnterCriticalSection( &This->lock );
3212 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3213 uRef = ++(This->ref);
3215 LeaveCriticalSection( &This->lock );
3221 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
3224 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3226 EnterCriticalSection( &This->lock );
3228 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3229 uRef = --(This->ref);
3231 LeaveCriticalSection( &This->lock );
3234 DeleteCriticalSection( &This->lock );
3235 HeapFree( GetProcessHeap(), 0, This );
3242 IDirectSoundCaptureImpl_CreateCaptureBuffer(
3243 LPDIRECTSOUNDCAPTURE iface,
3244 LPCDSCBUFFERDESC lpcDSCBufferDesc,
3245 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
3249 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3251 TRACE( "(%p)->(%p,%p,%p)\n", This, lpcDSCBufferDesc, lplpDSCaptureBuffer, pUnk );
3254 return DSERR_INVALIDPARAM;
3257 hr = DSOUND_CreateDirectSoundCaptureBuffer( lpcDSCBufferDesc, (LPVOID*)lplpDSCaptureBuffer );
3263 IDirectSoundCaptureImpl_GetCaps(
3264 LPDIRECTSOUNDCAPTURE iface,
3265 LPDSCCAPS lpDSCCaps )
3267 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3269 FIXME( "(%p)->(%p): stub\n", This, lpDSCCaps );
3275 IDirectSoundCaptureImpl_Initialize(
3276 LPDIRECTSOUNDCAPTURE iface,
3279 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3281 FIXME( "(%p)->(%p): stub\n", This, lpcGUID );
3287 static ICOM_VTABLE(IDirectSoundCapture) dscvt =
3289 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3290 /* IUnknown methods */
3291 IDirectSoundCaptureImpl_QueryInterface,
3292 IDirectSoundCaptureImpl_AddRef,
3293 IDirectSoundCaptureImpl_Release,
3295 /* IDirectSoundCapture methods */
3296 IDirectSoundCaptureImpl_CreateCaptureBuffer,
3297 IDirectSoundCaptureImpl_GetCaps,
3298 IDirectSoundCaptureImpl_Initialize
3302 DSOUND_CreateDirectSoundCaptureBuffer( LPCDSCBUFFERDESC lpcDSCBufferDesc, LPVOID* ppobj )
3305 FIXME( "(%p,%p): ignoring lpcDSCBufferDesc\n", lpcDSCBufferDesc, ppobj );
3307 *ppobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( IDirectSoundCaptureBufferImpl ) );
3309 if ( *ppobj == NULL ) {
3310 return DSERR_OUTOFMEMORY;
3314 ICOM_THIS(IDirectSoundCaptureBufferImpl,*ppobj);
3317 ICOM_VTBL(This) = &dscbvt;
3319 InitializeCriticalSection( &This->lock );
3327 IDirectSoundCaptureBufferImpl_QueryInterface(
3328 LPDIRECTSOUNDCAPTUREBUFFER iface,
3332 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3334 FIXME( "(%p)->(%s,%p): stub\n", This, debugstr_guid(riid), ppobj );
3340 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER iface )
3343 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3345 EnterCriticalSection( &This->lock );
3347 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3348 uRef = ++(This->ref);
3350 LeaveCriticalSection( &This->lock );
3356 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER iface )
3359 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3361 EnterCriticalSection( &This->lock );
3363 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3364 uRef = --(This->ref);
3366 LeaveCriticalSection( &This->lock );
3369 DeleteCriticalSection( &This->lock );
3370 HeapFree( GetProcessHeap(), 0, This );
3377 IDirectSoundCaptureBufferImpl_GetCaps(
3378 LPDIRECTSOUNDCAPTUREBUFFER iface,
3379 LPDSCBCAPS lpDSCBCaps )
3381 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3383 FIXME( "(%p)->(%p): stub\n", This, lpDSCBCaps );
3389 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
3390 LPDIRECTSOUNDCAPTUREBUFFER iface,
3391 LPDWORD lpdwCapturePosition,
3392 LPDWORD lpdwReadPosition )
3394 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3396 FIXME( "(%p)->(%p,%p): stub\n", This, lpdwCapturePosition, lpdwReadPosition );
3402 IDirectSoundCaptureBufferImpl_GetFormat(
3403 LPDIRECTSOUNDCAPTUREBUFFER iface,
3404 LPWAVEFORMATEX lpwfxFormat,
3405 DWORD dwSizeAllocated,
3406 LPDWORD lpdwSizeWritten )
3408 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3410 FIXME( "(%p)->(%p,0x%08lx,%p): stub\n", This, lpwfxFormat, dwSizeAllocated, lpdwSizeWritten );
3416 IDirectSoundCaptureBufferImpl_GetStatus(
3417 LPDIRECTSOUNDCAPTUREBUFFER iface,
3418 LPDWORD lpdwStatus )
3420 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3422 FIXME( "(%p)->(%p): stub\n", This, lpdwStatus );
3428 IDirectSoundCaptureBufferImpl_Initialize(
3429 LPDIRECTSOUNDCAPTUREBUFFER iface,
3430 LPDIRECTSOUNDCAPTURE lpDSC,
3431 LPCDSCBUFFERDESC lpcDSCBDesc )
3433 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3435 FIXME( "(%p)->(%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
3441 IDirectSoundCaptureBufferImpl_Lock(
3442 LPDIRECTSOUNDCAPTUREBUFFER iface,
3445 LPVOID* lplpvAudioPtr1,
3446 LPDWORD lpdwAudioBytes1,
3447 LPVOID* lplpvAudioPtr2,
3448 LPDWORD lpdwAudioBytes2,
3451 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3453 FIXME( "(%p)->(%08lu,%08lu,%p,%p,%p,%p,0x%08lx): stub\n", This, dwReadCusor, dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2, lpdwAudioBytes2, dwFlags );
3459 IDirectSoundCaptureBufferImpl_Start(
3460 LPDIRECTSOUNDCAPTUREBUFFER iface,
3463 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3465 FIXME( "(%p)->(0x%08lx): stub\n", This, dwFlags );
3471 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER iface )
3473 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3475 FIXME( "(%p): stub\n", This );
3481 IDirectSoundCaptureBufferImpl_Unlock(
3482 LPDIRECTSOUNDCAPTUREBUFFER iface,
3483 LPVOID lpvAudioPtr1,
3484 DWORD dwAudioBytes1,
3485 LPVOID lpvAudioPtr2,
3486 DWORD dwAudioBytes2 )
3488 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3490 FIXME( "(%p)->(%p,%08lu,%p,%08lu): stub\n", This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2 );
3496 static ICOM_VTABLE(IDirectSoundCaptureBuffer) dscbvt =
3498 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3499 /* IUnknown methods */
3500 IDirectSoundCaptureBufferImpl_QueryInterface,
3501 IDirectSoundCaptureBufferImpl_AddRef,
3502 IDirectSoundCaptureBufferImpl_Release,
3504 /* IDirectSoundCaptureBuffer methods */
3505 IDirectSoundCaptureBufferImpl_GetCaps,
3506 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
3507 IDirectSoundCaptureBufferImpl_GetFormat,
3508 IDirectSoundCaptureBufferImpl_GetStatus,
3509 IDirectSoundCaptureBufferImpl_Initialize,
3510 IDirectSoundCaptureBufferImpl_Lock,
3511 IDirectSoundCaptureBufferImpl_Start,
3512 IDirectSoundCaptureBufferImpl_Stop,
3513 IDirectSoundCaptureBufferImpl_Unlock
3516 /*******************************************************************************
3517 * DirectSound ClassFactory
3521 /* IUnknown fields */
3522 ICOM_VFIELD(IClassFactory);
3524 } IClassFactoryImpl;
3526 static HRESULT WINAPI
3527 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
3528 ICOM_THIS(IClassFactoryImpl,iface);
3530 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
3531 return E_NOINTERFACE;
3535 DSCF_AddRef(LPCLASSFACTORY iface) {
3536 ICOM_THIS(IClassFactoryImpl,iface);
3537 return ++(This->ref);
3540 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface) {
3541 ICOM_THIS(IClassFactoryImpl,iface);
3542 /* static class, won't be freed */
3543 return --(This->ref);
3546 static HRESULT WINAPI DSCF_CreateInstance(
3547 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
3549 ICOM_THIS(IClassFactoryImpl,iface);
3551 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
3552 if ( IsEqualGUID( &IID_IDirectSound, riid ) ) {
3553 /* FIXME: reuse already created dsound if present? */
3554 return DirectSoundCreate(riid,(LPDIRECTSOUND*)ppobj,pOuter);
3556 return E_NOINTERFACE;
3559 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
3560 ICOM_THIS(IClassFactoryImpl,iface);
3561 FIXME("(%p)->(%d),stub!\n",This,dolock);
3565 static ICOM_VTABLE(IClassFactory) DSCF_Vtbl = {
3566 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3567 DSCF_QueryInterface,
3570 DSCF_CreateInstance,
3573 static IClassFactoryImpl DSOUND_CF = {&DSCF_Vtbl, 1 };
3575 /*******************************************************************************
3576 * DllGetClassObject [DSOUND.4]
3577 * Retrieves class object from a DLL object
3580 * Docs say returns STDAPI
3583 * rclsid [I] CLSID for the class object
3584 * riid [I] Reference to identifier of interface for class object
3585 * ppv [O] Address of variable to receive interface pointer for riid
3589 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
3592 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
3594 TRACE("(%p,%p,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
3595 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
3596 *ppv = (LPVOID)&DSOUND_CF;
3597 IClassFactory_AddRef((IClassFactory*)*ppv);
3601 FIXME("(%p,%p,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
3602 return CLASS_E_CLASSNOTAVAILABLE;
3606 /*******************************************************************************
3607 * DllCanUnloadNow [DSOUND.3] Determines whether the DLL is in use.
3613 DWORD WINAPI DSOUND_DllCanUnloadNow(void)
3615 FIXME("(void): stub\n");