3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 TransGaming Technologies, Inc.
8 * Most thread locking is complete. There may be a few race
9 * conditions still lurking.
11 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
12 * and a Turtle Beach Tropez+.
15 * Implement DirectSoundCapture API
16 * Implement SetCooperativeLevel properly (need to address focus issues)
17 * Implement DirectSound3DBuffers (stubs in place)
18 * Use hardware 3D support if available
19 * Add critical section locking inside Release and AddRef methods
20 * Handle static buffers - put those in hardware, non-static not in hardware
21 * Hardware DuplicateSoundBuffer
22 * Proper volume calculation, and setting volume in HEL primary buffer
23 * Optimize WINMM and negotiate fragment size, decrease DS_HEL_MARGIN
29 #include <sys/types.h>
30 #include <sys/fcntl.h>
34 #include <math.h> /* Insomnia - pow() function */
43 #include "wine/windef16.h"
44 #include "debugtools.h"
48 DEFAULT_DEBUG_CHANNEL(dsound);
50 /* these are eligible for tuning... they must be high on slow machines... */
51 /* especially since the WINMM overhead is pretty high, and could be improved quite a bit;
52 * the high DS_HEL_MARGIN reflects the currently high wineoss/HEL latency
53 * some settings here should probably get ported to wine.conf */
54 #define DS_EMULDRIVER 1 /* some games (Quake 2, UT) refuse to accept
55 emulated dsound devices. set to 0 ! */
56 #define DS_HEL_FRAGS 48 /* HEL only: number of waveOut fragments in primary buffer */
57 #define DS_HEL_MARGIN 4 /* HEL only: number of waveOut fragments ahead to mix in new buffers */
59 #define DS_SND_QUEUE 28 /* max number of fragments to prebuffer */
61 /* Linux does not support better timing than 10ms */
62 #define DS_TIME_RES 10 /* Resolution of multimedia timer */
63 #define DS_TIME_DEL 10 /* Delay of multimedia timer callback, and duration of HEL fragment */
65 /*****************************************************************************
66 * Predeclare the interface implementation structures
68 typedef struct IDirectSoundImpl IDirectSoundImpl;
69 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
70 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
71 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
72 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
73 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
74 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
76 /*****************************************************************************
77 * IDirectSound implementation structure
79 struct IDirectSoundImpl
82 ICOM_VFIELD(IDirectSound);
84 /* IDirectSoundImpl fields */
89 LPWAVEHDR pwave[DS_HEL_FRAGS];
90 UINT timerID, pwplay, pwwrite, pwqueue;
94 IDirectSoundBufferImpl** buffers;
95 IDirectSoundBufferImpl* primary;
96 IDirectSound3DListenerImpl* listener;
97 WAVEFORMATEX wfx; /* current main waveformat */
98 CRITICAL_SECTION lock;
101 /*****************************************************************************
102 * IDirectSoundBuffer implementation structure
104 struct IDirectSoundBufferImpl
106 /* IUnknown fields */
107 ICOM_VFIELD(IDirectSoundBuffer);
109 /* IDirectSoundBufferImpl fields */
110 PIDSDRIVERBUFFER hwbuf;
113 IDirectSound3DBufferImpl* ds3db;
114 DWORD playflags,state,leadin;
115 DWORD playpos,startpos,writelead,buflen;
116 DWORD nAvgBytesPerSec;
119 IDirectSoundBufferImpl* parent; /* for duplicates */
120 IDirectSoundImpl* dsound;
122 LPDSBPOSITIONNOTIFY notifies;
124 CRITICAL_SECTION lock;
125 /* used for frequency conversion (PerfectPitch) */
126 ULONG freqAdjust, freqAcc;
127 /* used for intelligent (well, sort of) prebuffering */
128 DWORD probably_valid_to;
129 DWORD primary_mixpos, buf_mixpos;
132 #define STATE_STOPPED 0
133 #define STATE_STARTING 1
134 #define STATE_PLAYING 2
135 #define STATE_STOPPING 3
137 /*****************************************************************************
138 * IDirectSoundNotify implementation structure
140 struct IDirectSoundNotifyImpl
142 /* IUnknown fields */
143 ICOM_VFIELD(IDirectSoundNotify);
145 /* IDirectSoundNotifyImpl fields */
146 IDirectSoundBufferImpl* dsb;
149 /*****************************************************************************
150 * IDirectSound3DListener implementation structure
152 struct IDirectSound3DListenerImpl
154 /* IUnknown fields */
155 ICOM_VFIELD(IDirectSound3DListener);
157 /* IDirectSound3DListenerImpl fields */
158 IDirectSoundBufferImpl* dsb;
160 CRITICAL_SECTION lock;
163 /*****************************************************************************
164 * IDirectSound3DBuffer implementation structure
166 struct IDirectSound3DBufferImpl
168 /* IUnknown fields */
169 ICOM_VFIELD(IDirectSound3DBuffer);
171 /* IDirectSound3DBufferImpl fields */
172 IDirectSoundBufferImpl* dsb;
176 CRITICAL_SECTION lock;
180 /*****************************************************************************
181 * IDirectSoundCapture implementation structure
183 struct IDirectSoundCaptureImpl
185 /* IUnknown fields */
186 ICOM_VFIELD(IDirectSoundCapture);
189 /* IDirectSoundCaptureImpl fields */
190 CRITICAL_SECTION lock;
193 /*****************************************************************************
194 * IDirectSoundCapture implementation structure
196 struct IDirectSoundCaptureBufferImpl
198 /* IUnknown fields */
199 ICOM_VFIELD(IDirectSoundCaptureBuffer);
202 /* IDirectSoundCaptureBufferImpl fields */
203 CRITICAL_SECTION lock;
207 /* #define USE_DSOUND3D 1 */
209 #define DSOUND_FREQSHIFT (14)
211 static IDirectSoundImpl* dsound = NULL;
213 static IDirectSoundBufferImpl* primarybuf = NULL;
215 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
217 static HRESULT DSOUND_CreateDirectSoundCapture( LPVOID* ppobj );
218 static HRESULT DSOUND_CreateDirectSoundCaptureBuffer( LPCDSCBUFFERDESC lpcDSCBufferDesc, LPVOID* ppobj );
220 static ICOM_VTABLE(IDirectSoundCapture) dscvt;
221 static ICOM_VTABLE(IDirectSoundCaptureBuffer) dscbvt;
223 static HRESULT mmErr(UINT err)
226 case MMSYSERR_NOERROR:
228 case MMSYSERR_ALLOCATED:
229 return DSERR_ALLOCATED;
230 case MMSYSERR_INVALHANDLE:
231 return DSERR_GENERIC; /* FIXME */
232 case MMSYSERR_NODRIVER:
233 return DSERR_NODRIVER;
235 return DSERR_OUTOFMEMORY;
236 case MMSYSERR_INVALPARAM:
237 return DSERR_INVALIDPARAM;
239 FIXME("Unknown MMSYS error %d\n",err);
240 return DSERR_GENERIC;
244 /***************************************************************************
245 * DirectSoundEnumerateA [DSOUND.2]
247 * Enumerate all DirectSound drivers installed in the system
251 * Failure: DSERR_INVALIDPARAM
253 HRESULT WINAPI DirectSoundEnumerateA(
254 LPDSENUMCALLBACKA lpDSEnumCallback,
257 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
258 lpDSEnumCallback, lpContext);
261 if (lpDSEnumCallback != NULL)
262 lpDSEnumCallback(NULL,"WINE DirectSound using Open Sound System",
269 /***************************************************************************
270 * DirectSoundEnumerateW [DSOUND.3]
272 * Enumerate all DirectSound drivers installed in the system
276 * Failure: DSERR_INVALIDPARAM
278 HRESULT WINAPI DirectSoundEnumerateW(
279 LPDSENUMCALLBACKW lpDSEnumCallback,
282 FIXME("lpDSEnumCallback = %p, lpContext = %p: stub\n",
283 lpDSEnumCallback, lpContext);
289 static void _dump_DSBCAPS(DWORD xmask) {
294 #define FE(x) { x, #x },
295 FE(DSBCAPS_PRIMARYBUFFER)
297 FE(DSBCAPS_LOCHARDWARE)
298 FE(DSBCAPS_LOCSOFTWARE)
300 FE(DSBCAPS_CTRLFREQUENCY)
302 FE(DSBCAPS_CTRLVOLUME)
303 FE(DSBCAPS_CTRLPOSITIONNOTIFY)
304 FE(DSBCAPS_CTRLDEFAULT)
306 FE(DSBCAPS_STICKYFOCUS)
307 FE(DSBCAPS_GLOBALFOCUS)
308 FE(DSBCAPS_GETCURRENTPOSITION2)
309 FE(DSBCAPS_MUTE3DATMAXDISTANCE)
314 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
315 if ((flags[i].mask & xmask) == flags[i].mask)
316 DPRINTF("%s ",flags[i].name);
319 /*******************************************************************************
320 * IDirectSound3DBuffer
323 /* IUnknown methods */
325 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
326 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
328 ICOM_THIS(IDirectSound3DBufferImpl,iface);
330 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
336 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
338 ICOM_THIS(IDirectSound3DBufferImpl,iface);
345 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
347 ICOM_THIS(IDirectSound3DBufferImpl,iface);
349 TRACE("(%p) ref was %ld\n", This, This->ref);
355 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
357 DeleteCriticalSection(&This->lock);
359 HeapFree(GetProcessHeap(),0,This->buffer);
360 HeapFree(GetProcessHeap(),0,This);
366 /* IDirectSound3DBuffer methods */
368 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
369 LPDIRECTSOUND3DBUFFER iface,
370 LPDS3DBUFFER lpDs3dBuffer)
378 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
379 LPDIRECTSOUND3DBUFFER iface,
380 LPDWORD lpdwInsideConeAngle,
381 LPDWORD lpdwOutsideConeAngle)
389 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
390 LPDIRECTSOUND3DBUFFER iface,
391 LPD3DVECTOR lpvConeOrientation)
399 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
400 LPDIRECTSOUND3DBUFFER iface,
401 LPLONG lplConeOutsideVolume)
409 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
410 LPDIRECTSOUND3DBUFFER iface,
411 LPD3DVALUE lpfMaxDistance)
419 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
420 LPDIRECTSOUND3DBUFFER iface,
421 LPD3DVALUE lpfMinDistance)
429 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
430 LPDIRECTSOUND3DBUFFER iface,
439 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
440 LPDIRECTSOUND3DBUFFER iface,
441 LPD3DVECTOR lpvPosition)
449 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
450 LPDIRECTSOUND3DBUFFER iface,
451 LPD3DVECTOR lpvVelocity)
459 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
460 LPDIRECTSOUND3DBUFFER iface,
461 LPCDS3DBUFFER lpcDs3dBuffer,
470 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
471 LPDIRECTSOUND3DBUFFER iface,
472 DWORD dwInsideConeAngle,
473 DWORD dwOutsideConeAngle,
482 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
483 LPDIRECTSOUND3DBUFFER iface,
484 D3DVALUE x, D3DVALUE y, D3DVALUE z,
493 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
494 LPDIRECTSOUND3DBUFFER iface,
495 LONG lConeOutsideVolume,
504 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
505 LPDIRECTSOUND3DBUFFER iface,
506 D3DVALUE fMaxDistance,
515 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
516 LPDIRECTSOUND3DBUFFER iface,
517 D3DVALUE fMinDistance,
526 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
527 LPDIRECTSOUND3DBUFFER iface,
531 ICOM_THIS(IDirectSound3DBufferImpl,iface);
532 TRACE("mode = %lx\n", dwMode);
533 This->ds3db.dwMode = dwMode;
539 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
540 LPDIRECTSOUND3DBUFFER iface,
541 D3DVALUE x, D3DVALUE y, D3DVALUE z,
550 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
551 LPDIRECTSOUND3DBUFFER iface,
552 D3DVALUE x, D3DVALUE y, D3DVALUE z,
561 static ICOM_VTABLE(IDirectSound3DBuffer) ds3dbvt =
563 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
564 /* IUnknown methods */
565 IDirectSound3DBufferImpl_QueryInterface,
566 IDirectSound3DBufferImpl_AddRef,
567 IDirectSound3DBufferImpl_Release,
568 /* IDirectSound3DBuffer methods */
569 IDirectSound3DBufferImpl_GetAllParameters,
570 IDirectSound3DBufferImpl_GetConeAngles,
571 IDirectSound3DBufferImpl_GetConeOrientation,
572 IDirectSound3DBufferImpl_GetConeOutsideVolume,
573 IDirectSound3DBufferImpl_GetMaxDistance,
574 IDirectSound3DBufferImpl_GetMinDistance,
575 IDirectSound3DBufferImpl_GetMode,
576 IDirectSound3DBufferImpl_GetPosition,
577 IDirectSound3DBufferImpl_GetVelocity,
578 IDirectSound3DBufferImpl_SetAllParameters,
579 IDirectSound3DBufferImpl_SetConeAngles,
580 IDirectSound3DBufferImpl_SetConeOrientation,
581 IDirectSound3DBufferImpl_SetConeOutsideVolume,
582 IDirectSound3DBufferImpl_SetMaxDistance,
583 IDirectSound3DBufferImpl_SetMinDistance,
584 IDirectSound3DBufferImpl_SetMode,
585 IDirectSound3DBufferImpl_SetPosition,
586 IDirectSound3DBufferImpl_SetVelocity,
591 static int DSOUND_Create3DBuffer(IDirectSoundBufferImpl* dsb)
593 DWORD i, temp, iSize, oSize, offset;
594 LPBYTE bIbuf, bObuf, bTbuf = NULL;
595 LPWORD wIbuf, wObuf, wTbuf = NULL;
597 /* Inside DirectX says it's stupid but allowed */
598 if (dsb->wfx.nChannels == 2) {
599 /* Convert to mono */
600 if (dsb->wfx.wBitsPerSample == 16) {
601 iSize = dsb->buflen / 4;
602 wTbuf = malloc(dsb->buflen / 2);
604 return DSERR_OUTOFMEMORY;
605 for (i = 0; i < iSize; i++)
606 wTbuf[i] = (dsb->buffer[i * 2] + dsb->buffer[(i * 2) + 1]) / 2;
609 iSize = dsb->buflen / 2;
610 bTbuf = malloc(dsb->buflen / 2);
612 return DSERR_OUTOFMEMORY;
613 for (i = 0; i < iSize; i++)
614 bTbuf[i] = (dsb->buffer[i * 2] + dsb->buffer[(i * 2) + 1]) / 2;
618 if (dsb->wfx.wBitsPerSample == 16) {
619 iSize = dsb->buflen / 2;
620 wIbuf = (LPWORD) dsb->buffer;
622 bIbuf = (LPBYTE) dsb->buffer;
627 if (primarybuf->wfx.wBitsPerSample == 16) {
628 wObuf = (LPWORD) dsb->ds3db->buffer;
629 oSize = dsb->ds3db->buflen / 2;
631 bObuf = (LPBYTE) dsb->ds3db->buffer;
632 oSize = dsb->ds3db->buflen;
635 offset = primarybuf->wfx.nSamplesPerSec / 100; /* 10ms */
636 if (primarybuf->wfx.wBitsPerSample == 16 && dsb->wfx.wBitsPerSample == 16)
637 for (i = 0; i < iSize; i++) {
640 temp += wIbuf[i - offset] >> 9;
642 temp += wIbuf[i + iSize - offset] >> 9;
644 wObuf[(i * 2) + 1] = temp;
646 else if (primarybuf->wfx.wBitsPerSample == 8 && dsb->wfx.wBitsPerSample == 8)
647 for (i = 0; i < iSize; i++) {
650 temp += bIbuf[i - offset] >> 5;
652 temp += bIbuf[i + iSize - offset] >> 5;
654 bObuf[(i * 2) + 1] = temp;
665 /*******************************************************************************
666 * IDirectSound3DListener
669 /* IUnknown methods */
670 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
671 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
673 ICOM_THIS(IDirectSound3DListenerImpl,iface);
675 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
679 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
681 ICOM_THIS(IDirectSound3DListenerImpl,iface);
686 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
689 ICOM_THIS(IDirectSound3DListenerImpl,iface);
691 TRACE("(%p) ref was %ld\n", This, This->ref);
693 ulReturn = --This->ref;
695 /* Free all resources */
696 if( ulReturn == 0 ) {
698 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
699 DeleteCriticalSection(&This->lock);
700 HeapFree(GetProcessHeap(),0,This);
706 /* IDirectSound3DListener methods */
707 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
708 LPDIRECTSOUND3DLISTENER iface,
709 LPDS3DLISTENER lpDS3DL)
715 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
716 LPDIRECTSOUND3DLISTENER iface,
717 LPD3DVALUE lpfDistanceFactor)
723 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
724 LPDIRECTSOUND3DLISTENER iface,
725 LPD3DVALUE lpfDopplerFactor)
731 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
732 LPDIRECTSOUND3DLISTENER iface,
733 LPD3DVECTOR lpvOrientFront,
734 LPD3DVECTOR lpvOrientTop)
740 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
741 LPDIRECTSOUND3DLISTENER iface,
742 LPD3DVECTOR lpvPosition)
748 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
749 LPDIRECTSOUND3DLISTENER iface,
750 LPD3DVALUE lpfRolloffFactor)
756 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
757 LPDIRECTSOUND3DLISTENER iface,
758 LPD3DVECTOR lpvVelocity)
764 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
765 LPDIRECTSOUND3DLISTENER iface,
766 LPCDS3DLISTENER lpcDS3DL,
773 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
774 LPDIRECTSOUND3DLISTENER iface,
775 D3DVALUE fDistanceFactor,
782 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
783 LPDIRECTSOUND3DLISTENER iface,
784 D3DVALUE fDopplerFactor,
791 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
792 LPDIRECTSOUND3DLISTENER iface,
793 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
794 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
801 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
802 LPDIRECTSOUND3DLISTENER iface,
803 D3DVALUE x, D3DVALUE y, D3DVALUE z,
810 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
811 LPDIRECTSOUND3DLISTENER iface,
812 D3DVALUE fRolloffFactor,
819 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
820 LPDIRECTSOUND3DLISTENER iface,
821 D3DVALUE x, D3DVALUE y, D3DVALUE z,
828 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
829 LPDIRECTSOUND3DLISTENER iface)
836 static ICOM_VTABLE(IDirectSound3DListener) ds3dlvt =
838 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
839 /* IUnknown methods */
840 IDirectSound3DListenerImpl_QueryInterface,
841 IDirectSound3DListenerImpl_AddRef,
842 IDirectSound3DListenerImpl_Release,
843 /* IDirectSound3DListener methods */
844 IDirectSound3DListenerImpl_GetAllParameter,
845 IDirectSound3DListenerImpl_GetDistanceFactor,
846 IDirectSound3DListenerImpl_GetDopplerFactor,
847 IDirectSound3DListenerImpl_GetOrientation,
848 IDirectSound3DListenerImpl_GetPosition,
849 IDirectSound3DListenerImpl_GetRolloffFactor,
850 IDirectSound3DListenerImpl_GetVelocity,
851 IDirectSound3DListenerImpl_SetAllParameters,
852 IDirectSound3DListenerImpl_SetDistanceFactor,
853 IDirectSound3DListenerImpl_SetDopplerFactor,
854 IDirectSound3DListenerImpl_SetOrientation,
855 IDirectSound3DListenerImpl_SetPosition,
856 IDirectSound3DListenerImpl_SetRolloffFactor,
857 IDirectSound3DListenerImpl_SetVelocity,
858 IDirectSound3DListenerImpl_CommitDeferredSettings,
861 /*******************************************************************************
864 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
865 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
867 ICOM_THIS(IDirectSoundNotifyImpl,iface);
869 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
873 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
874 ICOM_THIS(IDirectSoundNotifyImpl,iface);
875 return ++(This->ref);
878 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
879 ICOM_THIS(IDirectSoundNotifyImpl,iface);
881 TRACE("(%p) ref was %ld\n", This, This->ref);
885 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
886 HeapFree(GetProcessHeap(),0,This);
892 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
893 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
895 ICOM_THIS(IDirectSoundNotifyImpl,iface);
898 if (TRACE_ON(dsound)) {
899 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
900 for (i=0;i<howmuch;i++)
901 TRACE("notify at %ld to 0x%08lx\n",
902 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
904 This->dsb->notifies = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->dsb->notifies,(This->dsb->nrofnotifies+howmuch)*sizeof(DSBPOSITIONNOTIFY));
905 memcpy( This->dsb->notifies+This->dsb->nrofnotifies,
907 howmuch*sizeof(DSBPOSITIONNOTIFY)
909 This->dsb->nrofnotifies+=howmuch;
914 static ICOM_VTABLE(IDirectSoundNotify) dsnvt =
916 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
917 IDirectSoundNotifyImpl_QueryInterface,
918 IDirectSoundNotifyImpl_AddRef,
919 IDirectSoundNotifyImpl_Release,
920 IDirectSoundNotifyImpl_SetNotificationPositions,
923 /*******************************************************************************
927 static void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
931 /* the AmpFactors are expressed in 16.16 fixed point */
932 volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 65536);
933 /* FIXME: dwPan{Left|Right}AmpFactor */
935 /* FIXME: use calculated vol and pan ampfactors */
936 temp = (double) (volpan->lVolume - (volpan->lPan > 0 ? volpan->lPan : 0));
937 volpan->dwTotalLeftAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 65536);
938 temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
939 volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 65536);
941 TRACE("left = %lx, right = %lx\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
944 static void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
948 sw = dsb->wfx.nChannels * (dsb->wfx.wBitsPerSample / 8);
949 if ((dsb->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && dsb->hwbuf) {
951 /* let fragment size approximate the timer delay */
952 fraglen = (dsb->freq * DS_TIME_DEL / 1000) * sw;
953 /* reduce fragment size until an integer number of them fits in the buffer */
954 /* (FIXME: this may or may not be a good idea) */
955 while (dsb->buflen % fraglen) fraglen -= sw;
956 dsb->dsound->fraglen = fraglen;
957 TRACE("fraglen=%ld\n", dsb->dsound->fraglen);
959 /* calculate the 10ms write lead */
960 dsb->writelead = (dsb->freq / 100) * sw;
963 static HRESULT DSOUND_PrimaryOpen(IDirectSoundBufferImpl *dsb)
967 /* are we using waveOut stuff? */
971 HRESULT merr = DS_OK;
972 /* Start in pause mode, to allow buffers to get filled */
973 waveOutPause(dsb->dsound->hwo);
974 if (dsb->state == STATE_PLAYING) dsb->state = STATE_STARTING;
975 else if (dsb->state == STATE_STOPPING) dsb->state = STATE_STOPPED;
976 /* use fragments of 10ms (1/100s) each (which should get us within
977 * the documented write cursor lead of 10-15ms) */
978 buflen = ((dsb->wfx.nAvgBytesPerSec / 100) & ~3) * DS_HEL_FRAGS;
979 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, dsb->buffer);
980 /* reallocate emulated primary buffer */
981 newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,dsb->buffer,buflen);
982 if (newbuf == NULL) {
983 ERR("failed to allocate primary buffer\n");
984 merr = DSERR_OUTOFMEMORY;
985 /* but the old buffer might still exists and must be re-prepared */
987 dsb->buffer = newbuf;
988 dsb->buflen = buflen;
992 IDirectSoundImpl *ds = dsb->dsound;
994 ds->fraglen = dsb->buflen / DS_HEL_FRAGS;
996 /* prepare fragment headers */
997 for (c=0; c<DS_HEL_FRAGS; c++) {
998 ds->pwave[c]->lpData = dsb->buffer + c*ds->fraglen;
999 ds->pwave[c]->dwBufferLength = ds->fraglen;
1000 ds->pwave[c]->dwUser = (DWORD)dsb;
1001 ds->pwave[c]->dwFlags = 0;
1002 ds->pwave[c]->dwLoops = 0;
1003 err = mmErr(waveOutPrepareHeader(ds->hwo,ds->pwave[c],sizeof(WAVEHDR)));
1006 waveOutUnprepareHeader(ds->hwo,ds->pwave[c],sizeof(WAVEHDR));
1014 memset(dsb->buffer, (dsb->wfx.wBitsPerSample == 16) ? 0 : 128, dsb->buflen);
1015 TRACE("fraglen=%ld\n", ds->fraglen);
1017 if ((err == DS_OK) && (merr != DS_OK))
1024 static void DSOUND_PrimaryClose(IDirectSoundBufferImpl *dsb)
1026 /* are we using waveOut stuff? */
1029 IDirectSoundImpl *ds = dsb->dsound;
1031 waveOutReset(ds->hwo);
1032 for (c=0; c<DS_HEL_FRAGS; c++)
1033 waveOutUnprepareHeader(ds->hwo, ds->pwave[c], sizeof(WAVEHDR));
1037 static HRESULT DSOUND_PrimaryPlay(IDirectSoundBufferImpl *dsb)
1039 HRESULT err = DS_OK;
1041 err = IDsDriverBuffer_Play(dsb->hwbuf, 0, 0, DSBPLAY_LOOPING);
1045 static HRESULT DSOUND_PrimaryStop(IDirectSoundBufferImpl *dsb)
1047 HRESULT err = DS_OK;
1049 err = IDsDriverBuffer_Stop(dsb->hwbuf);
1050 if (err == DSERR_BUFFERLOST) {
1051 /* Wine-only: the driver wants us to reopen the device */
1052 /* FIXME: check for errors */
1053 IDsDriverBuffer_Release(primarybuf->hwbuf);
1054 waveOutClose(dsb->dsound->hwo);
1055 dsb->dsound->hwo = 0;
1056 waveOutOpen(&(dsb->dsound->hwo), dsb->dsound->drvdesc.dnDevNode,
1057 &(primarybuf->wfx), 0, 0, CALLBACK_NULL | WAVE_DIRECTSOUND);
1058 err = IDsDriver_CreateSoundBuffer(dsb->dsound->driver,&(dsb->wfx),dsb->dsbd.dwFlags,0,
1059 &(dsb->buflen),&(dsb->buffer),
1060 (LPVOID)&(dsb->hwbuf));
1066 /* This sets this format for the <em>Primary Buffer Only</em> */
1067 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
1068 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
1069 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX wfex
1071 ICOM_THIS(IDirectSoundBufferImpl,iface);
1072 IDirectSoundBufferImpl** dsb;
1073 HRESULT err = DS_OK;
1076 /* Let's be pedantic! */
1077 if ((wfex == NULL) ||
1078 (wfex->wFormatTag != WAVE_FORMAT_PCM) ||
1079 (wfex->nChannels < 1) || (wfex->nChannels > 2) ||
1080 (wfex->nSamplesPerSec < 1) ||
1081 (wfex->nBlockAlign < 1) || (wfex->nChannels > 4) ||
1082 ((wfex->wBitsPerSample != 8) && (wfex->wBitsPerSample != 16))) {
1083 TRACE("failed pedantic check!\n");
1084 return DSERR_INVALIDPARAM;
1088 EnterCriticalSection(&(This->dsound->lock));
1090 if (primarybuf->wfx.nSamplesPerSec != wfex->nSamplesPerSec) {
1091 dsb = dsound->buffers;
1092 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
1094 EnterCriticalSection(&((*dsb)->lock));
1096 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
1097 wfex->nSamplesPerSec;
1099 LeaveCriticalSection(&((*dsb)->lock));
1104 memcpy(&(primarybuf->wfx), wfex, sizeof(primarybuf->wfx));
1106 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1107 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1108 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1109 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1110 wfex->wBitsPerSample, wfex->cbSize);
1112 primarybuf->wfx.nAvgBytesPerSec =
1113 This->wfx.nSamplesPerSec * This->wfx.nBlockAlign;
1114 if (primarybuf->dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
1115 /* FIXME: check for errors */
1116 DSOUND_PrimaryClose(primarybuf);
1117 waveOutClose(This->dsound->hwo);
1118 This->dsound->hwo = 0;
1119 waveOutOpen(&(This->dsound->hwo), This->dsound->drvdesc.dnDevNode,
1120 &(primarybuf->wfx), 0, 0, CALLBACK_NULL | WAVE_DIRECTSOUND);
1121 DSOUND_PrimaryOpen(primarybuf);
1123 if (primarybuf->hwbuf) {
1124 err = IDsDriverBuffer_SetFormat(primarybuf->hwbuf, &(primarybuf->wfx));
1125 if (err == DSERR_BUFFERLOST) {
1126 /* Wine-only: the driver wants us to recreate the HW buffer */
1127 IDsDriverBuffer_Release(primarybuf->hwbuf);
1128 err = IDsDriver_CreateSoundBuffer(primarybuf->dsound->driver,&(primarybuf->wfx),primarybuf->dsbd.dwFlags,0,
1129 &(primarybuf->buflen),&(primarybuf->buffer),
1130 (LPVOID)&(primarybuf->hwbuf));
1131 if (primarybuf->state == STATE_PLAYING) primarybuf->state = STATE_STARTING;
1132 else if (primarybuf->state == STATE_STOPPING) primarybuf->state = STATE_STOPPED;
1135 DSOUND_RecalcFormat(primarybuf);
1137 LeaveCriticalSection(&(This->dsound->lock));
1143 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
1144 LPDIRECTSOUNDBUFFER iface,LONG vol
1146 ICOM_THIS(IDirectSoundBufferImpl,iface);
1148 TRACE("(%p,%ld)\n",This,vol);
1150 /* I'm not sure if we need this for primary buffer */
1151 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
1152 return DSERR_CONTROLUNAVAIL;
1154 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN))
1155 return DSERR_INVALIDPARAM;
1158 EnterCriticalSection(&(This->lock));
1160 This->volpan.lVolume = vol;
1162 DSOUND_RecalcVolPan(&(This->volpan));
1165 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
1167 else if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
1168 #if 0 /* should we really do this? */
1169 /* the DS volume ranges from 0 (max, 0dB attenuation) to -10000 (min, 100dB attenuation) */
1170 /* the MM volume ranges from 0 to 0xffff in an unspecified logarithmic scale */
1171 WORD cvol = 0xffff + vol*6 + vol/2;
1172 DWORD vol = cvol | ((DWORD)cvol << 16)
1173 waveOutSetVolume(This->dsound->hwo, vol);
1177 LeaveCriticalSection(&(This->lock));
1183 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
1184 LPDIRECTSOUNDBUFFER iface,LPLONG vol
1186 ICOM_THIS(IDirectSoundBufferImpl,iface);
1187 TRACE("(%p,%p)\n",This,vol);
1190 return DSERR_INVALIDPARAM;
1192 *vol = This->volpan.lVolume;
1196 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
1197 LPDIRECTSOUNDBUFFER iface,DWORD freq
1199 ICOM_THIS(IDirectSoundBufferImpl,iface);
1200 TRACE("(%p,%ld)\n",This,freq);
1202 /* You cannot set the frequency of the primary buffer */
1203 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY) ||
1204 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1205 return DSERR_CONTROLUNAVAIL;
1207 if (!freq) freq = This->wfx.nSamplesPerSec;
1209 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX))
1210 return DSERR_INVALIDPARAM;
1213 EnterCriticalSection(&(This->lock));
1216 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / primarybuf->wfx.nSamplesPerSec;
1217 This->nAvgBytesPerSec = freq * This->wfx.nBlockAlign;
1218 DSOUND_RecalcFormat(This);
1220 LeaveCriticalSection(&(This->lock));
1226 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
1227 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
1229 ICOM_THIS(IDirectSoundBufferImpl,iface);
1230 TRACE("(%p,%08lx,%08lx,%08lx)\n",
1231 This,reserved1,reserved2,flags
1235 EnterCriticalSection(&(This->lock));
1237 This->playflags = flags;
1238 if (This->state == STATE_STOPPED) {
1239 This->leadin = TRUE;
1240 This->startpos = This->buf_mixpos;
1241 This->state = STATE_STARTING;
1242 } else if (This->state == STATE_STOPPING)
1243 This->state = STATE_PLAYING;
1244 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && This->hwbuf) {
1245 IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
1246 This->state = STATE_PLAYING;
1249 LeaveCriticalSection(&(This->lock));
1255 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
1257 ICOM_THIS(IDirectSoundBufferImpl,iface);
1258 TRACE("(%p)\n",This);
1261 EnterCriticalSection(&(This->lock));
1263 if (This->state == STATE_PLAYING)
1264 This->state = STATE_STOPPING;
1265 else if (This->state == STATE_STARTING)
1266 This->state = STATE_STOPPED;
1267 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) && This->hwbuf) {
1268 IDsDriverBuffer_Stop(This->hwbuf);
1269 This->state = STATE_STOPPED;
1271 DSOUND_CheckEvent(This, 0);
1273 LeaveCriticalSection(&(This->lock));
1279 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface) {
1280 ICOM_THIS(IDirectSoundBufferImpl,iface);
1283 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
1285 ref = InterlockedIncrement(&(This->ref));
1287 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
1291 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER iface) {
1292 ICOM_THIS(IDirectSoundBufferImpl,iface);
1296 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
1298 ref = InterlockedDecrement(&(This->ref));
1299 if (ref) return ref;
1301 EnterCriticalSection(&(This->dsound->lock));
1302 for (i=0;i<This->dsound->nrofbuffers;i++)
1303 if (This->dsound->buffers[i] == This)
1306 if (i < This->dsound->nrofbuffers) {
1307 /* Put the last buffer of the list in the (now empty) position */
1308 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
1309 This->dsound->nrofbuffers--;
1310 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER)*This->dsound->nrofbuffers);
1311 TRACE("buffer count is now %d\n", This->dsound->nrofbuffers);
1312 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
1314 LeaveCriticalSection(&(This->dsound->lock));
1316 DeleteCriticalSection(&(This->lock));
1317 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1318 DSOUND_PrimaryClose(This);
1320 IDsDriverBuffer_Release(This->hwbuf);
1323 IDirectSound3DBuffer_Release((LPDIRECTSOUND3DBUFFER)This->ds3db);
1325 /* this is a duplicate buffer */
1326 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->parent);
1328 /* this is a toplevel buffer */
1329 HeapFree(GetProcessHeap(),0,This->buffer);
1331 HeapFree(GetProcessHeap(),0,This);
1333 if (This == primarybuf)
1339 static DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This,
1340 DWORD state, DWORD pplay, DWORD pwrite, DWORD pmix, DWORD bmix)
1344 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, pmix);
1345 TRACE("this mixpos=%ld\n", bmix);
1347 /* the actual primary play position (pplay) is always behind last mixed (pmix),
1348 * unless the computer is too slow or something */
1349 /* we need to know how far away we are from there */
1350 if (pmix == pplay) {
1351 if ((state == STATE_PLAYING) || (state == STATE_STOPPING)) {
1352 /* wow, the software mixer is really doing well,
1353 * seems the entire primary buffer is filled! */
1354 pmix += primarybuf->buflen;
1356 /* else: the primary buffer is not playing, so probably empty */
1358 if (pmix < pplay) pmix += primarybuf->buflen; /* wraparound */
1360 /* detect buffer underrun */
1361 if (pwrite < pplay) pwrite += primarybuf->buflen; /* wraparound */
1363 if (pmix > (DS_SND_QUEUE * primarybuf->dsound->fraglen + pwrite + primarybuf->writelead)) {
1364 TRACE("detected an underrun: primary queue was %ld\n",pmix);
1367 /* divide the offset by its sample size */
1368 pmix /= primarybuf->wfx.nBlockAlign;
1369 TRACE("primary back-samples=%ld\n",pmix);
1370 /* adjust for our frequency */
1371 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
1372 /* multiply by our own sample size */
1373 pmix *= This->wfx.nBlockAlign;
1374 TRACE("this back-offset=%ld\n", pmix);
1375 /* subtract from our last mixed position */
1377 while (bplay < pmix) bplay += This->buflen; /* wraparound */
1379 if (This->leadin && ((bplay < This->startpos) || (bplay > bmix))) {
1380 /* seems we haven't started playing yet */
1381 TRACE("this still in lead-in phase\n");
1382 bplay = This->startpos;
1384 /* return the result */
1388 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
1389 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
1392 ICOM_THIS(IDirectSoundBufferImpl,iface);
1393 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1395 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
1400 else if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
1403 mtime.wType = TIME_BYTES;
1404 waveOutGetPosition(This->dsound->hwo, &mtime, sizeof(mtime));
1405 mtime.u.cb = mtime.u.cb % This->buflen;
1406 *playpos = mtime.u.cb;
1409 /* the writepos should only be used by apps with WRITEPRIMARY priority,
1410 * in which case our software mixer is disabled anyway */
1411 *writepos = This->playpos + DS_HEL_MARGIN * This->dsound->fraglen;
1412 while (*writepos >= This->buflen)
1413 *writepos -= This->buflen;
1416 if (playpos && (This->state != STATE_PLAYING)) {
1417 /* we haven't been merged into the primary buffer (yet) */
1418 *playpos = This->buf_mixpos;
1421 DWORD pplay, pwrite, lplay, splay, pstate;
1422 /* let's get this exact; first, recursively call GetPosition on the primary */
1423 EnterCriticalSection(&(primarybuf->lock));
1424 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || primarybuf->hwbuf || !DS_EMULDRIVER) {
1425 IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER)primarybuf, &pplay, &pwrite);
1426 /* detect HEL mode underrun */
1427 pstate = primarybuf->state;
1428 if (!(primarybuf->hwbuf || primarybuf->dsound->pwqueue)) {
1429 TRACE("detected an underrun\n");
1431 if (pstate == STATE_PLAYING)
1432 pstate = STATE_STARTING;
1433 else if (pstate == STATE_STOPPING)
1434 pstate = STATE_STOPPED;
1436 /* get data for ourselves while we still have the lock */
1437 pstate &= This->state;
1438 lplay = This->primary_mixpos;
1439 splay = This->buf_mixpos;
1440 /* calculate play position using this */
1441 *playpos = DSOUND_CalcPlayPosition(This, pstate, pplay, pwrite, lplay, splay);
1443 /* (unless the app isn't using GETCURRENTPOSITION2) */
1444 /* don't know exactly how this should be handled...
1445 * the docs says that play cursor is reported as directly
1446 * behind write cursor, hmm... */
1447 *playpos = This->playpos;
1449 LeaveCriticalSection(&(primarybuf->lock));
1451 if (writepos) *writepos = This->buf_mixpos;
1454 if (This->state != STATE_STOPPED)
1455 /* apply the documented 10ms lead to writepos */
1456 *writepos += This->writelead;
1457 while (*writepos >= This->buflen) *writepos -= This->buflen;
1459 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
1463 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
1464 LPDIRECTSOUNDBUFFER iface,LPDWORD status
1466 ICOM_THIS(IDirectSoundBufferImpl,iface);
1467 TRACE("(%p,%p), thread is %lx\n",This,status,GetCurrentThreadId());
1470 return DSERR_INVALIDPARAM;
1473 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING))
1474 *status |= DSBSTATUS_PLAYING;
1475 if (This->playflags & DSBPLAY_LOOPING)
1476 *status |= DSBSTATUS_LOOPING;
1478 TRACE("status=%lx\n", *status);
1483 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
1484 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
1486 ICOM_THIS(IDirectSoundBufferImpl,iface);
1487 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1489 if (wfsize>sizeof(This->wfx))
1490 wfsize = sizeof(This->wfx);
1491 if (lpwf) { /* NULL is valid */
1492 memcpy(lpwf,&(This->wfx),wfsize);
1494 *wfwritten = wfsize;
1497 *wfwritten = sizeof(This->wfx);
1499 return DSERR_INVALIDPARAM;
1504 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
1505 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
1507 ICOM_THIS(IDirectSoundBufferImpl,iface);
1510 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1521 if (flags & DSBLOCK_FROMWRITECURSOR) {
1523 /* GetCurrentPosition does too much magic to duplicate here */
1524 IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
1525 writecursor += writepos;
1527 if (flags & DSBLOCK_ENTIREBUFFER)
1528 writebytes = This->buflen;
1529 if (writebytes > This->buflen)
1530 writebytes = This->buflen;
1532 assert(audiobytes1!=audiobytes2);
1533 assert(lplpaudioptr1!=lplpaudioptr2);
1535 if ((writebytes == This->buflen) &&
1536 ((This->state == STATE_STARTING) ||
1537 (This->state == STATE_PLAYING)))
1538 /* some games, like Half-Life, tries to be clever (not) and
1539 * keeps one secondary buffer, and mixes sounds into it itself,
1540 * locking the entire buffer every time... so we can just forget
1541 * about tracking the last-written-to-position... */
1542 This->probably_valid_to = (DWORD)-1;
1544 This->probably_valid_to = writecursor;
1546 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1547 capf = DSDDESC_DONTNEEDPRIMARYLOCK;
1549 capf = DSDDESC_DONTNEEDSECONDARYLOCK;
1550 if (!(This->dsound->drvdesc.dwFlags & capf) && This->hwbuf) {
1551 IDsDriverBuffer_Lock(This->hwbuf,
1552 lplpaudioptr1, audiobytes1,
1553 lplpaudioptr2, audiobytes2,
1554 writecursor, writebytes,
1557 if (writecursor+writebytes <= This->buflen) {
1558 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1559 *audiobytes1 = writebytes;
1561 *(LPBYTE*)lplpaudioptr2 = NULL;
1564 TRACE("->%ld.0\n",writebytes);
1566 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1567 *audiobytes1 = This->buflen-writecursor;
1569 *(LPBYTE*)lplpaudioptr2 = This->buffer;
1571 *audiobytes2 = writebytes-(This->buflen-writecursor);
1572 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
1577 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
1578 LPDIRECTSOUNDBUFFER iface,DWORD newpos
1580 ICOM_THIS(IDirectSoundBufferImpl,iface);
1581 TRACE("(%p,%ld)\n",This,newpos);
1584 EnterCriticalSection(&(This->lock));
1586 This->buf_mixpos = newpos;
1588 IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
1590 LeaveCriticalSection(&(This->lock));
1596 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
1597 LPDIRECTSOUNDBUFFER iface,LONG pan
1599 ICOM_THIS(IDirectSoundBufferImpl,iface);
1601 TRACE("(%p,%ld)\n",This,pan);
1603 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT))
1604 return DSERR_INVALIDPARAM;
1606 /* You cannot set the pan of the primary buffer */
1607 /* and you cannot use both pan and 3D controls */
1608 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
1609 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) ||
1610 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1611 return DSERR_CONTROLUNAVAIL;
1614 EnterCriticalSection(&(This->lock));
1616 This->volpan.lPan = pan;
1618 DSOUND_RecalcVolPan(&(This->volpan));
1621 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
1624 LeaveCriticalSection(&(This->lock));
1630 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
1631 LPDIRECTSOUNDBUFFER iface,LPLONG pan
1633 ICOM_THIS(IDirectSoundBufferImpl,iface);
1634 TRACE("(%p,%p)\n",This,pan);
1637 return DSERR_INVALIDPARAM;
1639 *pan = This->volpan.lPan;
1644 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
1645 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1647 ICOM_THIS(IDirectSoundBufferImpl,iface);
1648 DWORD capf, probably_valid_to;
1650 TRACE("(%p,%p,%ld,%p,%ld):stub\n", This,p1,x1,p2,x2);
1653 /* Preprocess 3D buffers... */
1655 /* This is highly experimental and liable to break things */
1656 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D)
1657 DSOUND_Create3DBuffer(This);
1660 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER)
1661 capf = DSDDESC_DONTNEEDPRIMARYLOCK;
1663 capf = DSDDESC_DONTNEEDSECONDARYLOCK;
1664 if (!(This->dsound->drvdesc.dwFlags & capf) && This->hwbuf) {
1665 IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
1668 if (p2) probably_valid_to = (((LPBYTE)p2)-This->buffer) + x2;
1669 else probably_valid_to = (((LPBYTE)p1)-This->buffer) + x1;
1670 while (probably_valid_to >= This->buflen)
1671 probably_valid_to -= This->buflen;
1672 if ((probably_valid_to == 0) && ((x1+x2) == This->buflen) &&
1673 ((This->state == STATE_STARTING) ||
1674 (This->state == STATE_PLAYING)))
1675 /* see IDirectSoundBufferImpl_Lock */
1676 probably_valid_to = (DWORD)-1;
1677 This->probably_valid_to = probably_valid_to;
1682 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
1683 LPDIRECTSOUNDBUFFER iface
1685 ICOM_THIS(IDirectSoundBufferImpl,iface);
1686 FIXME("(%p):stub\n",This);
1690 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
1691 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1693 ICOM_THIS(IDirectSoundBufferImpl,iface);
1694 TRACE("(%p,%p)\n",This,freq);
1697 return DSERR_INVALIDPARAM;
1700 TRACE("-> %ld\n", *freq);
1705 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
1706 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPDSBUFFERDESC dbsd
1708 ICOM_THIS(IDirectSoundBufferImpl,iface);
1709 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
1710 DPRINTF("Re-Init!!!\n");
1711 return DSERR_ALREADYINITIALIZED;
1714 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
1715 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1717 ICOM_THIS(IDirectSoundBufferImpl,iface);
1718 TRACE("(%p)->(%p)\n",This,caps);
1721 return DSERR_INVALIDPARAM;
1723 /* I think we should check this value, not set it. See */
1724 /* Inside DirectX, p215. That should apply here, too. */
1725 caps->dwSize = sizeof(*caps);
1727 caps->dwFlags = This->dsbd.dwFlags;
1728 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
1729 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
1731 caps->dwBufferBytes = This->dsbd.dwBufferBytes;
1733 /* This value represents the speed of the "unlock" command.
1734 As unlock is quite fast (it does not do anything), I put
1735 4096 ko/s = 4 Mo / s */
1736 /* FIXME: hwbuf speed */
1737 caps->dwUnlockTransferRate = 4096;
1738 caps->dwPlayCpuOverhead = 0;
1743 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
1744 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1746 ICOM_THIS(IDirectSoundBufferImpl,iface);
1748 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1750 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1751 IDirectSoundNotifyImpl *dsn;
1753 dsn = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsn));
1756 IDirectSoundBuffer_AddRef(iface);
1757 ICOM_VTBL(dsn) = &dsnvt;
1758 *ppobj = (LPVOID)dsn;
1763 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1764 IDirectSound3DBufferImpl *ds3db;
1766 *ppobj = This->ds3db;
1768 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
1772 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
1776 ICOM_VTBL(ds3db) = &ds3dbvt;
1777 InitializeCriticalSection(&ds3db->lock);
1779 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1781 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1782 ds3db->ds3db.vPosition.u1.x = 0.0;
1783 ds3db->ds3db.vPosition.u2.y = 0.0;
1784 ds3db->ds3db.vPosition.u3.z = 0.0;
1785 ds3db->ds3db.vVelocity.u1.x = 0.0;
1786 ds3db->ds3db.vVelocity.u2.y = 0.0;
1787 ds3db->ds3db.vVelocity.u3.z = 0.0;
1788 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1789 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1790 ds3db->ds3db.vConeOrientation.u1.x = 0.0;
1791 ds3db->ds3db.vConeOrientation.u2.y = 0.0;
1792 ds3db->ds3db.vConeOrientation.u3.z = 0.0;
1793 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME; ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1794 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1795 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
1796 ds3db->buflen = (This->buflen * primarybuf->wfx.nBlockAlign) /
1797 This->wfx.nBlockAlign;
1798 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
1799 if (ds3db->buffer == NULL) {
1801 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
1807 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1808 FIXME("%s: I know about this GUID, but don't support it yet\n",
1809 debugstr_guid( riid ));
1815 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1816 IDirectSound3DListenerImpl* dsl;
1818 if (This->dsound->listener) {
1819 *ppobj = This->dsound->listener;
1820 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This->dsound->listener);
1824 dsl = (IDirectSound3DListenerImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsl));
1826 ICOM_VTBL(dsl) = &ds3dlvt;
1827 *ppobj = (LPVOID)dsl;
1829 dsl->ds3dl.dwSize = sizeof(DS3DLISTENER);
1830 dsl->ds3dl.vPosition.u1.x = 0.0;
1831 dsl->ds3dl.vPosition.u2.y = 0.0;
1832 dsl->ds3dl.vPosition.u3.z = 0.0;
1833 dsl->ds3dl.vVelocity.u1.x = 0.0;
1834 dsl->ds3dl.vVelocity.u2.y = 0.0;
1835 dsl->ds3dl.vVelocity.u3.z = 0.0;
1836 dsl->ds3dl.vOrientFront.u1.x = 0.0;
1837 dsl->ds3dl.vOrientFront.u2.y = 0.0;
1838 dsl->ds3dl.vOrientFront.u3.z = 1.0;
1839 dsl->ds3dl.vOrientTop.u1.x = 0.0;
1840 dsl->ds3dl.vOrientTop.u2.y = 1.0;
1841 dsl->ds3dl.vOrientTop.u3.z = 0.0;
1842 dsl->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1843 dsl->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1845 InitializeCriticalSection(&dsl->lock);
1848 IDirectSoundBuffer_AddRef(iface);
1850 This->dsound->listener = dsl;
1851 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)dsl);
1856 FIXME( "Unknown GUID %s\n", debugstr_guid( riid ) );
1863 static ICOM_VTABLE(IDirectSoundBuffer) dsbvt =
1865 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1866 IDirectSoundBufferImpl_QueryInterface,
1867 IDirectSoundBufferImpl_AddRef,
1868 IDirectSoundBufferImpl_Release,
1869 IDirectSoundBufferImpl_GetCaps,
1870 IDirectSoundBufferImpl_GetCurrentPosition,
1871 IDirectSoundBufferImpl_GetFormat,
1872 IDirectSoundBufferImpl_GetVolume,
1873 IDirectSoundBufferImpl_GetPan,
1874 IDirectSoundBufferImpl_GetFrequency,
1875 IDirectSoundBufferImpl_GetStatus,
1876 IDirectSoundBufferImpl_Initialize,
1877 IDirectSoundBufferImpl_Lock,
1878 IDirectSoundBufferImpl_Play,
1879 IDirectSoundBufferImpl_SetCurrentPosition,
1880 IDirectSoundBufferImpl_SetFormat,
1881 IDirectSoundBufferImpl_SetVolume,
1882 IDirectSoundBufferImpl_SetPan,
1883 IDirectSoundBufferImpl_SetFrequency,
1884 IDirectSoundBufferImpl_Stop,
1885 IDirectSoundBufferImpl_Unlock,
1886 IDirectSoundBufferImpl_Restore
1889 /*******************************************************************************
1893 static HRESULT WINAPI IDirectSoundImpl_SetCooperativeLevel(
1894 LPDIRECTSOUND iface,HWND hwnd,DWORD level
1896 ICOM_THIS(IDirectSoundImpl,iface);
1898 FIXME("(%p,%08lx,%ld):stub\n",This,(DWORD)hwnd,level);
1900 This->priolevel = level;
1905 static HRESULT WINAPI IDirectSoundImpl_CreateSoundBuffer(
1906 LPDIRECTSOUND iface,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER ppdsb,LPUNKNOWN lpunk
1908 ICOM_THIS(IDirectSoundImpl,iface);
1909 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
1910 LPWAVEFORMATEX wfex;
1911 HRESULT err = DS_OK;
1913 TRACE("(%p,%p,%p,%p)\n",This,dsbd,ippdsb,lpunk);
1915 if ((This == NULL) || (dsbd == NULL) || (ippdsb == NULL))
1916 return DSERR_INVALIDPARAM;
1918 if (TRACE_ON(dsound)) {
1919 TRACE("(structsize=%ld)\n",dsbd->dwSize);
1920 TRACE("(flags=0x%08lx:\n",dsbd->dwFlags);
1921 _dump_DSBCAPS(dsbd->dwFlags);
1923 TRACE("(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
1924 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
1927 wfex = dsbd->lpwfxFormat;
1930 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1931 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1932 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1933 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1934 wfex->wBitsPerSample, wfex->cbSize);
1936 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1938 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
1939 *ippdsb = primarybuf;
1940 primarybuf->dsbd.dwFlags = dsbd->dwFlags;
1942 } /* Else create primary buffer */
1945 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
1946 if (*ippdsb == NULL)
1947 return DSERR_OUTOFMEMORY;
1948 ICOM_VTBL(*ippdsb) = &dsbvt;
1950 (*ippdsb)->dsound = This;
1951 (*ippdsb)->parent = NULL;
1952 (*ippdsb)->buffer = NULL;
1954 memcpy(&((*ippdsb)->dsbd),dsbd,sizeof(*dsbd));
1955 if (dsbd->lpwfxFormat)
1956 memcpy(&((*ippdsb)->wfx), dsbd->lpwfxFormat, sizeof((*ippdsb)->wfx));
1958 TRACE("Created buffer at %p\n", *ippdsb);
1960 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1961 (*ippdsb)->buflen = dsound->wfx.nAvgBytesPerSec;
1962 (*ippdsb)->freq = dsound->wfx.nSamplesPerSec;
1964 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
1967 err = IDsDriver_CreateSoundBuffer(This->driver,wfex,dsbd->dwFlags,0,
1968 &((*ippdsb)->buflen),&((*ippdsb)->buffer),
1969 (LPVOID*)&((*ippdsb)->hwbuf));
1972 err = DSOUND_PrimaryOpen(*ippdsb);
1977 (*ippdsb)->buflen = dsbd->dwBufferBytes;
1978 (*ippdsb)->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1980 /* Check necessary hardware mixing capabilities */
1981 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1982 else capf |= DSCAPS_SECONDARYMONO;
1983 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1984 else capf |= DSCAPS_SECONDARY8BIT;
1985 use_hw = (This->drvcaps.dwFlags & capf) == capf;
1987 /* FIXME: check hardware sample rate mixing capabilities */
1988 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1989 /* FIXME: check whether any hardware buffers are left */
1990 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1992 /* Allocate system memory if applicable */
1993 if ((This->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1994 (*ippdsb)->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,(*ippdsb)->buflen);
1995 if ((*ippdsb)->buffer == NULL)
1996 err = DSERR_OUTOFMEMORY;
1999 /* Allocate the hardware buffer */
2000 if (use_hw && (err == DS_OK)) {
2001 err = IDsDriver_CreateSoundBuffer(This->driver,wfex,dsbd->dwFlags,0,
2002 &((*ippdsb)->buflen),&((*ippdsb)->buffer),
2003 (LPVOID*)&((*ippdsb)->hwbuf));
2008 if ((*ippdsb)->buffer)
2009 HeapFree(GetProcessHeap(),0,(*ippdsb)->buffer);
2010 HeapFree(GetProcessHeap(),0,(*ippdsb));
2014 /* calculate fragment size and write lead */
2015 DSOUND_RecalcFormat(*ippdsb);
2017 /* It's not necessary to initialize values to zero since */
2018 /* we allocated this structure with HEAP_ZERO_MEMORY... */
2019 (*ippdsb)->playpos = 0;
2020 (*ippdsb)->buf_mixpos = 0;
2021 (*ippdsb)->state = STATE_STOPPED;
2022 DSOUND_RecalcVolPan(&((*ippdsb)->volpan));
2024 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
2025 (*ippdsb)->freqAdjust = ((*ippdsb)->freq << DSOUND_FREQSHIFT) /
2026 primarybuf->wfx.nSamplesPerSec;
2027 (*ippdsb)->nAvgBytesPerSec = (*ippdsb)->freq *
2028 dsbd->lpwfxFormat->nBlockAlign;
2031 EnterCriticalSection(&(This->lock));
2032 /* register buffer */
2033 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
2034 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl*)*(This->nrofbuffers+1));
2036 This->buffers = newbuffers;
2037 This->buffers[This->nrofbuffers] = *ippdsb;
2038 This->nrofbuffers++;
2039 TRACE("buffer count is now %d\n", This->nrofbuffers);
2041 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
2042 err = DSERR_OUTOFMEMORY;
2045 LeaveCriticalSection(&(This->lock));
2047 IDirectSound_AddRef(iface);
2049 InitializeCriticalSection(&((*ippdsb)->lock));
2053 IDirectSoundBuffer_Release(*ppdsb);
2059 if (dsbd->dwFlags & DSBCAPS_CTRL3D) {
2060 IDirectSound3DBufferImpl *ds3db;
2062 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
2064 ICOM_VTBL(ds3db) = &ds3dbvt;
2066 (*ippdsb)->ds3db = ds3db;
2068 ds3db->dsb = (*ippdsb);
2069 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER)ippdsb);
2071 InitializeCriticalSection(&ds3db->lock);
2073 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
2074 ds3db->ds3db.vPosition.u1.x = 0.0;
2075 ds3db->ds3db.vPosition.u2.y = 0.0;
2076 ds3db->ds3db.vPosition.u3.z = 0.0;
2077 ds3db->ds3db.vVelocity.u1.x = 0.0;
2078 ds3db->ds3db.vVelocity.u2.y = 0.0;
2079 ds3db->ds3db.vVelocity.u3.z = 0.0;
2080 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
2081 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
2082 ds3db->ds3db.vConeOrientation.u1.x = 0.0;
2083 ds3db->ds3db.vConeOrientation.u2.y = 0.0;
2084 ds3db->ds3db.vConeOrientation.u3.z = 0.0;
2085 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
2086 ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
2087 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
2088 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
2089 ds3db->buflen = ((*ippdsb)->buflen * primarybuf->wfx.nBlockAlign) /
2090 (*ippdsb)->wfx.nBlockAlign;
2091 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
2092 if (ds3db->buffer == NULL) {
2094 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
2101 static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer(
2102 LPDIRECTSOUND iface,LPDIRECTSOUNDBUFFER pdsb,LPLPDIRECTSOUNDBUFFER ppdsb
2104 ICOM_THIS(IDirectSoundImpl,iface);
2105 IDirectSoundBufferImpl* ipdsb=(IDirectSoundBufferImpl*)pdsb;
2106 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
2107 TRACE("(%p,%p,%p)\n",This,ipdsb,ippdsb);
2110 FIXME("need to duplicate hardware buffer\n");
2113 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
2115 IDirectSoundBuffer_AddRef(pdsb);
2116 memcpy(*ippdsb, ipdsb, sizeof(IDirectSoundBufferImpl));
2118 (*ippdsb)->state = STATE_STOPPED;
2119 (*ippdsb)->playpos = 0;
2120 (*ippdsb)->buf_mixpos = 0;
2121 (*ippdsb)->dsound = This;
2122 (*ippdsb)->parent = ipdsb;
2123 memcpy(&((*ippdsb)->wfx), &(ipdsb->wfx), sizeof((*ippdsb)->wfx));
2124 InitializeCriticalSection(&(*ippdsb)->lock);
2125 /* register buffer */
2126 EnterCriticalSection(&(This->lock));
2128 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
2130 This->buffers = newbuffers;
2131 This->buffers[This->nrofbuffers] = *ippdsb;
2132 This->nrofbuffers++;
2133 TRACE("buffer count is now %d\n", This->nrofbuffers);
2135 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
2136 /* FIXME: release buffer */
2139 LeaveCriticalSection(&(This->lock));
2140 IDirectSound_AddRef(iface);
2145 static HRESULT WINAPI IDirectSoundImpl_GetCaps(LPDIRECTSOUND iface,LPDSCAPS caps) {
2146 ICOM_THIS(IDirectSoundImpl,iface);
2147 TRACE("(%p,%p)\n",This,caps);
2148 TRACE("(flags=0x%08lx)\n",caps->dwFlags);
2151 return DSERR_INVALIDPARAM;
2153 /* We should check this value, not set it. See Inside DirectX, p215. */
2154 caps->dwSize = sizeof(*caps);
2156 caps->dwFlags = This->drvcaps.dwFlags;
2158 /* FIXME: copy caps from This->drvcaps */
2159 caps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
2160 caps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
2162 caps->dwPrimaryBuffers = 1;
2164 caps->dwMaxHwMixingAllBuffers = 0;
2165 caps->dwMaxHwMixingStaticBuffers = 0;
2166 caps->dwMaxHwMixingStreamingBuffers = 0;
2168 caps->dwFreeHwMixingAllBuffers = 0;
2169 caps->dwFreeHwMixingStaticBuffers = 0;
2170 caps->dwFreeHwMixingStreamingBuffers = 0;
2172 caps->dwMaxHw3DAllBuffers = 0;
2173 caps->dwMaxHw3DStaticBuffers = 0;
2174 caps->dwMaxHw3DStreamingBuffers = 0;
2176 caps->dwFreeHw3DAllBuffers = 0;
2177 caps->dwFreeHw3DStaticBuffers = 0;
2178 caps->dwFreeHw3DStreamingBuffers = 0;
2180 caps->dwTotalHwMemBytes = 0;
2182 caps->dwFreeHwMemBytes = 0;
2184 caps->dwMaxContigFreeHwMemBytes = 0;
2186 caps->dwUnlockTransferRateHwBuffers = 4096; /* But we have none... */
2188 caps->dwPlayCpuOverheadSwBuffers = 1; /* 1% */
2193 static ULONG WINAPI IDirectSoundImpl_AddRef(LPDIRECTSOUND iface) {
2194 ICOM_THIS(IDirectSoundImpl,iface);
2195 return ++(This->ref);
2198 static ULONG WINAPI IDirectSoundImpl_Release(LPDIRECTSOUND iface) {
2199 ICOM_THIS(IDirectSoundImpl,iface);
2200 TRACE("(%p), ref was %ld\n",This,This->ref);
2201 if (!--(This->ref)) {
2204 timeKillEvent(This->timerID);
2205 timeEndPeriod(DS_TIME_RES);
2208 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)primarybuf);
2210 if (This->buffers) {
2211 for( i=0;i<This->nrofbuffers;i++)
2212 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->buffers[i]);
2216 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->primary);
2218 DeleteCriticalSection(&This->lock);
2220 IDsDriver_Close(This->driver);
2223 for (c=0; c<DS_HEL_FRAGS; c++)
2224 HeapFree(GetProcessHeap(),0,This->pwave[c]);
2226 if (This->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
2227 waveOutClose(This->hwo);
2230 IDsDriver_Release(This->driver);
2232 HeapFree(GetProcessHeap(),0,This);
2239 static HRESULT WINAPI IDirectSoundImpl_SetSpeakerConfig(
2240 LPDIRECTSOUND iface,DWORD config
2242 ICOM_THIS(IDirectSoundImpl,iface);
2243 FIXME("(%p,0x%08lx):stub\n",This,config);
2247 static HRESULT WINAPI IDirectSoundImpl_QueryInterface(
2248 LPDIRECTSOUND iface,REFIID riid,LPVOID *ppobj
2250 ICOM_THIS(IDirectSoundImpl,iface);
2252 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
2254 if (This->listener) {
2255 *ppobj = This->listener;
2256 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This->listener);
2260 This->listener = (IDirectSound3DListenerImpl*)HeapAlloc(
2261 GetProcessHeap(), 0, sizeof(*(This->listener)));
2262 This->listener->ref = 1;
2263 ICOM_VTBL(This->listener) = &ds3dlvt;
2264 *ppobj = (LPVOID)This->listener;
2265 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
2267 This->listener->dsb = NULL;
2269 This->listener->ds3dl.dwSize = sizeof(DS3DLISTENER);
2270 This->listener->ds3dl.vPosition.u1.x = 0.0;
2271 This->listener->ds3dl.vPosition.u2.y = 0.0;
2272 This->listener->ds3dl.vPosition.u3.z = 0.0;
2273 This->listener->ds3dl.vVelocity.u1.x = 0.0;
2274 This->listener->ds3dl.vVelocity.u2.y = 0.0;
2275 This->listener->ds3dl.vVelocity.u3.z = 0.0;
2276 This->listener->ds3dl.vOrientFront.u1.x = 0.0;
2277 This->listener->ds3dl.vOrientFront.u2.y = 0.0;
2278 This->listener->ds3dl.vOrientFront.u3.z = 1.0;
2279 This->listener->ds3dl.vOrientTop.u1.x = 0.0;
2280 This->listener->ds3dl.vOrientTop.u2.y = 1.0;
2281 This->listener->ds3dl.vOrientTop.u3.z = 0.0;
2282 This->listener->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
2283 This->listener->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
2284 This->listener->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
2286 InitializeCriticalSection(&This->listener->lock);
2291 FIXME("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
2295 static HRESULT WINAPI IDirectSoundImpl_Compact(
2296 LPDIRECTSOUND iface)
2298 ICOM_THIS(IDirectSoundImpl,iface);
2299 TRACE("(%p)\n", This);
2303 static HRESULT WINAPI IDirectSoundImpl_GetSpeakerConfig(
2304 LPDIRECTSOUND iface,
2305 LPDWORD lpdwSpeakerConfig)
2307 ICOM_THIS(IDirectSoundImpl,iface);
2308 TRACE("(%p, %p)\n", This, lpdwSpeakerConfig);
2309 *lpdwSpeakerConfig = DSSPEAKER_STEREO | (DSSPEAKER_GEOMETRY_NARROW << 16);
2313 static HRESULT WINAPI IDirectSoundImpl_Initialize(
2314 LPDIRECTSOUND iface,
2317 ICOM_THIS(IDirectSoundImpl,iface);
2318 TRACE("(%p, %p)\n", This, lpcGuid);
2322 static ICOM_VTABLE(IDirectSound) dsvt =
2324 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2325 IDirectSoundImpl_QueryInterface,
2326 IDirectSoundImpl_AddRef,
2327 IDirectSoundImpl_Release,
2328 IDirectSoundImpl_CreateSoundBuffer,
2329 IDirectSoundImpl_GetCaps,
2330 IDirectSoundImpl_DuplicateSoundBuffer,
2331 IDirectSoundImpl_SetCooperativeLevel,
2332 IDirectSoundImpl_Compact,
2333 IDirectSoundImpl_GetSpeakerConfig,
2334 IDirectSoundImpl_SetSpeakerConfig,
2335 IDirectSoundImpl_Initialize
2339 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
2343 LPDSBPOSITIONNOTIFY event;
2345 if (dsb->nrofnotifies == 0)
2348 TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
2349 dsb, dsb->buflen, dsb->playpos, len);
2350 for (i = 0; i < dsb->nrofnotifies ; i++) {
2351 event = dsb->notifies + i;
2352 offset = event->dwOffset;
2353 TRACE("checking %d, position %ld, event = %d\n",
2354 i, offset, event->hEventNotify);
2355 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
2356 /* OK. [Inside DirectX, p274] */
2358 /* This also means we can't sort the entries by offset, */
2359 /* because DSBPN_OFFSETSTOP == -1 */
2360 if (offset == DSBPN_OFFSETSTOP) {
2361 if (dsb->state == STATE_STOPPED) {
2362 SetEvent(event->hEventNotify);
2363 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2368 if ((dsb->playpos + len) >= dsb->buflen) {
2369 if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
2370 (offset >= dsb->playpos)) {
2371 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2372 SetEvent(event->hEventNotify);
2375 if ((offset >= dsb->playpos) && (offset < (dsb->playpos + len))) {
2376 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
2377 SetEvent(event->hEventNotify);
2383 /* WAV format info can be found at: */
2385 /* http://www.cwi.nl/ftp/audio/AudioFormats.part2 */
2386 /* ftp://ftp.cwi.nl/pub/audio/RIFF-format */
2388 /* Import points to remember: */
2390 /* 8-bit WAV is unsigned */
2391 /* 16-bit WAV is signed */
2393 static inline INT16 cvtU8toS16(BYTE byte)
2395 INT16 s = (byte - 128) << 8;
2400 static inline BYTE cvtS16toU8(INT16 word)
2402 BYTE b = (word + 32768) >> 8;
2408 /* We should be able to optimize these two inline functions */
2409 /* so that we aren't doing 8->16->8 conversions when it is */
2410 /* not necessary. But this is still a WIP. Optimize later. */
2411 static inline void get_fields(const IDirectSoundBufferImpl *dsb, BYTE *buf, INT *fl, INT *fr)
2413 INT16 *bufs = (INT16 *) buf;
2415 /* TRACE("(%p)", buf); */
2416 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 2) {
2417 *fl = cvtU8toS16(*buf);
2418 *fr = cvtU8toS16(*(buf + 1));
2422 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 2) {
2428 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 1) {
2429 *fl = cvtU8toS16(*buf);
2434 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 1) {
2440 FIXME("get_fields found an unsupported configuration\n");
2444 static inline void set_fields(BYTE *buf, INT fl, INT fr)
2446 INT16 *bufs = (INT16 *) buf;
2448 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 2)) {
2449 *buf = cvtS16toU8(fl);
2450 *(buf + 1) = cvtS16toU8(fr);
2454 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 2)) {
2460 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 1)) {
2461 *buf = cvtS16toU8((fl + fr) >> 1);
2465 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 1)) {
2466 *bufs = (fl + fr) >> 1;
2469 FIXME("set_fields found an unsupported configuration\n");
2473 /* Now with PerfectPitch (tm) technology */
2474 static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2476 INT i, size, ipos, ilen, fieldL, fieldR;
2478 INT iAdvance = dsb->wfx.nBlockAlign;
2479 INT oAdvance = primarybuf->wfx.nBlockAlign;
2481 ibp = dsb->buffer + dsb->buf_mixpos;
2484 TRACE("(%p, %p, %p), buf_mixpos=%ld\n", dsb, ibp, obp, dsb->buf_mixpos);
2485 /* Check for the best case */
2486 if ((dsb->freq == primarybuf->wfx.nSamplesPerSec) &&
2487 (dsb->wfx.wBitsPerSample == primarybuf->wfx.wBitsPerSample) &&
2488 (dsb->wfx.nChannels == primarybuf->wfx.nChannels)) {
2489 DWORD bytesleft = dsb->buflen - dsb->buf_mixpos;
2490 TRACE("(%p) Best case\n", dsb);
2491 if (len <= bytesleft )
2492 memcpy(obp, ibp, len);
2494 memcpy(obp, ibp, bytesleft );
2495 memcpy(obp + bytesleft, dsb->buffer, len - bytesleft);
2500 /* Check for same sample rate */
2501 if (dsb->freq == primarybuf->wfx.nSamplesPerSec) {
2502 TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb,
2503 dsb->freq, primarybuf->wfx.nSamplesPerSec);
2505 for (i = 0; i < len; i += oAdvance) {
2506 get_fields(dsb, ibp, &fieldL, &fieldR);
2509 set_fields(obp, fieldL, fieldR);
2511 if (ibp >= (BYTE *)(dsb->buffer + dsb->buflen))
2512 ibp = dsb->buffer; /* wrap */
2517 /* Mix in different sample rates */
2519 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
2520 /* Patent Pending :-] */
2522 /* Patent enhancements (c) 2000 Ove KÃ¥ven,
2523 * TransGaming Technologies Inc. */
2525 TRACE("(%p) Adjusting frequency: %ld -> %ld\n",
2526 dsb, dsb->freq, primarybuf->wfx.nSamplesPerSec);
2528 size = len / oAdvance;
2530 ipos = dsb->buf_mixpos;
2531 for (i = 0; i < size; i++) {
2532 get_fields(dsb, (dsb->buffer + ipos), &fieldL, &fieldR);
2533 set_fields(obp, fieldL, fieldR);
2536 dsb->freqAcc += dsb->freqAdjust;
2537 if (dsb->freqAcc >= (1<<DSOUND_FREQSHIFT)) {
2538 ULONG adv = (dsb->freqAcc>>DSOUND_FREQSHIFT) * iAdvance;
2539 dsb->freqAcc &= (1<<DSOUND_FREQSHIFT)-1;
2540 ipos += adv; ilen += adv;
2541 while (ipos >= dsb->buflen)
2542 ipos -= dsb->buflen;
2548 static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2550 INT i, inc = primarybuf->wfx.wBitsPerSample >> 3;
2552 INT16 *bps = (INT16 *) buf;
2554 TRACE("(%p) left = %lx, right = %lx\n", dsb,
2555 dsb->volpan.dwTotalLeftAmpFactor, dsb->volpan.dwTotalRightAmpFactor);
2556 if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
2557 (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
2558 !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
2559 return; /* Nothing to do */
2561 /* If we end up with some bozo coder using panning or 3D sound */
2562 /* with a mono primary buffer, it could sound very weird using */
2563 /* this method. Oh well, tough patooties. */
2565 for (i = 0; i < len; i += inc) {
2571 /* 8-bit WAV is unsigned, but we need to operate */
2572 /* on signed data for this to work properly */
2574 val = ((val * (i & inc ? dsb->volpan.dwTotalRightAmpFactor : dsb->volpan.dwTotalLeftAmpFactor)) >> 16);
2579 /* 16-bit WAV is signed -- much better */
2581 val = ((val * ((i & inc) ? dsb->volpan.dwTotalRightAmpFactor : dsb->volpan.dwTotalLeftAmpFactor)) >> 16);
2587 FIXME("MixerVol had a nasty error\n");
2593 static void DSOUND_Mixer3D(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
2596 DWORD buflen, buf_mixpos;
2598 buflen = dsb->ds3db->buflen;
2599 buf_mixpos = (dsb->buf_mixpos * primarybuf->wfx.nBlockAlign) / dsb->wfx.nBlockAlign;
2600 ibp = dsb->ds3db->buffer + buf_mixpos;
2603 if (buf_mixpos > buflen) {
2604 FIXME("Major breakage");
2608 if (len <= (buf_mixpos + buflen))
2609 memcpy(obp, ibp, len);
2611 memcpy(obp, ibp, buflen - buf_mixpos);
2612 memcpy(obp + (buflen - buf_mixpos),
2614 len - (buflen - buf_mixpos));
2620 static void *tmp_buffer;
2621 static size_t tmp_buffer_len = 0;
2623 static void *DSOUND_tmpbuffer(size_t len)
2625 if (len>tmp_buffer_len) {
2626 void *new_buffer = realloc(tmp_buffer, len);
2628 tmp_buffer = new_buffer;
2629 tmp_buffer_len = len;
2636 static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
2638 INT i, len, ilen, temp, field;
2639 INT advance = primarybuf->wfx.wBitsPerSample >> 3;
2640 BYTE *buf, *ibuf, *obuf;
2641 INT16 *ibufs, *obufs;
2644 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2645 temp = MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->buflen,
2646 dsb->nAvgBytesPerSec) -
2647 MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->buf_mixpos,
2648 dsb->nAvgBytesPerSec);
2649 len = (len > temp) ? temp : len;
2651 len &= ~3; /* 4 byte alignment */
2654 /* This should only happen if we aren't looping and temp < 4 */
2656 /* We skip the remainder, so check for possible events */
2657 DSOUND_CheckEvent(dsb, dsb->buflen - dsb->buf_mixpos);
2659 dsb->state = STATE_STOPPED;
2661 dsb->buf_mixpos = 0;
2662 dsb->leadin = FALSE;
2663 /* Check for DSBPN_OFFSETSTOP */
2664 DSOUND_CheckEvent(dsb, 0);
2668 /* Been seeing segfaults in malloc() for some reason... */
2669 TRACE("allocating buffer (size = %d)\n", len);
2670 if ((buf = ibuf = (BYTE *) DSOUND_tmpbuffer(len)) == NULL)
2673 TRACE("MixInBuffer (%p) len = %d, dest = %ld\n", dsb, len, writepos);
2675 ilen = DSOUND_MixerNorm(dsb, ibuf, len);
2676 if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
2677 (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
2678 DSOUND_MixerVol(dsb, ibuf, len);
2680 obuf = primarybuf->buffer + writepos;
2681 for (i = 0; i < len; i += advance) {
2682 obufs = (INT16 *) obuf;
2683 ibufs = (INT16 *) ibuf;
2684 if (primarybuf->wfx.wBitsPerSample == 8) {
2685 /* 8-bit WAV is unsigned */
2686 field = (*ibuf - 128);
2687 field += (*obuf - 128);
2688 field = field > 127 ? 127 : field;
2689 field = field < -128 ? -128 : field;
2690 *obuf = field + 128;
2692 /* 16-bit WAV is signed */
2695 field = field > 32767 ? 32767 : field;
2696 field = field < -32768 ? -32768 : field;
2701 if (obuf >= (BYTE *)(primarybuf->buffer + primarybuf->buflen))
2702 obuf = primarybuf->buffer;
2706 if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY)
2707 DSOUND_CheckEvent(dsb, ilen);
2709 if (dsb->leadin && (dsb->startpos > dsb->buf_mixpos) && (dsb->startpos <= dsb->buf_mixpos + ilen)) {
2710 /* HACK... leadin should be reset when the PLAY position reaches the startpos,
2711 * not the MIX position... but if the sound buffer is bigger than our prebuffering
2712 * (which must be the case for the streaming buffers that need this hack anyway)
2713 * plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
2714 dsb->leadin = FALSE;
2717 dsb->buf_mixpos += ilen;
2719 if (dsb->buf_mixpos >= dsb->buflen) {
2720 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2721 dsb->state = STATE_STOPPED;
2723 dsb->buf_mixpos = 0;
2724 dsb->leadin = FALSE;
2725 DSOUND_CheckEvent(dsb, 0); /* For DSBPN_OFFSETSTOP */
2728 while (dsb->buf_mixpos >= dsb->buflen)
2729 dsb->buf_mixpos -= dsb->buflen;
2730 if (dsb->leadin && (dsb->startpos <= dsb->buf_mixpos))
2731 dsb->leadin = FALSE; /* HACK: see above */
2738 static void DSOUND_MixCancel(IDirectSoundBufferImpl *dsb, DWORD writepos)
2740 FIXME("prebuffer cancel not implemented yet\n");
2743 static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD playpos, DWORD writepos, DWORD mixlen)
2746 /* determine this buffer's write position */
2747 DWORD buf_writepos = DSOUND_CalcPlayPosition(dsb, dsb->state & primarybuf->state, writepos,
2748 writepos, dsb->primary_mixpos, dsb->buf_mixpos);
2749 /* determine how much already-mixed data exist */
2751 ((dsb->buf_mixpos < buf_writepos) ? dsb->buflen : 0) +
2752 dsb->buf_mixpos - buf_writepos;
2753 DWORD primary_done =
2754 ((dsb->primary_mixpos < writepos) ? primarybuf->buflen : 0) +
2755 dsb->primary_mixpos - writepos;
2757 ((primarybuf->buf_mixpos < writepos) ? primarybuf->buflen : 0) +
2758 primarybuf->buf_mixpos - writepos;
2761 TRACE("buf_writepos=%ld, primary_writepos=%ld\n", buf_writepos, writepos);
2762 TRACE("buf_done=%ld, primary_done=%ld\n", buf_done, primary_done);
2763 TRACE("buf_mixpos=%ld, primary_mixpos=%ld, mixlen=%ld\n", dsb->buf_mixpos, dsb->primary_mixpos,
2765 TRACE("looping=%ld, startpos=%ld, leadin=%ld\n", dsb->playflags, dsb->startpos, dsb->leadin);
2767 /* save write position for non-GETCURRENTPOSITION2... */
2768 dsb->playpos = buf_writepos;
2770 /* check whether CalcPlayPosition detected a mixing underrun */
2771 if ((buf_done == 0) && (dsb->primary_mixpos != writepos)) {
2772 /* it did, but did we have more to play? */
2773 if ((dsb->playflags & DSBPLAY_LOOPING) ||
2774 (dsb->buf_mixpos < dsb->buflen)) {
2775 /* yes, have to recover */
2776 ERR("underrun on sound buffer %p\n", dsb);
2777 TRACE("recovering from underrun: primary_mixpos=%ld\n", writepos);
2779 dsb->primary_mixpos = writepos;
2782 /* determine how far ahead we should mix */
2783 if (((dsb->playflags & DSBPLAY_LOOPING) ||
2784 (dsb->leadin && (dsb->probably_valid_to != 0))) &&
2785 !(dsb->dsbd.dwFlags & DSBCAPS_STATIC)) {
2786 /* if this is a streaming buffer, it typically means that
2787 * we should defer mixing past probably_valid_to as long
2788 * as we can, to avoid unnecessary remixing */
2789 /* the heavy-looking calculations shouldn't be that bad,
2790 * as any game isn't likely to be have more than 1 or 2
2791 * streaming buffers in use at any time anyway... */
2792 DWORD probably_valid_left =
2793 (dsb->probably_valid_to == (DWORD)-1) ? dsb->buflen :
2794 ((dsb->probably_valid_to < buf_writepos) ? dsb->buflen : 0) +
2795 dsb->probably_valid_to - buf_writepos;
2796 /* check for leadin condition */
2797 if ((probably_valid_left == 0) &&
2798 (dsb->probably_valid_to == dsb->startpos) &&
2800 probably_valid_left = dsb->buflen;
2801 TRACE("streaming buffer probably_valid_to=%ld, probably_valid_left=%ld\n",
2802 dsb->probably_valid_to, probably_valid_left);
2803 /* check whether the app's time is already up */
2804 if (probably_valid_left < dsb->writelead) {
2805 WARN("probably_valid_to now within writelead, possible streaming underrun\n");
2806 /* once we pass the point of no return,
2807 * no reason to hold back anymore */
2808 dsb->probably_valid_to = (DWORD)-1;
2809 /* we just have to go ahead and mix what we have,
2810 * there's no telling what the app is thinking anyway */
2812 /* divide valid length by our sample size */
2813 probably_valid_left /= dsb->wfx.nBlockAlign;
2814 /* adjust for our frequency */
2815 probably_valid_left = (probably_valid_left << DSOUND_FREQSHIFT) / dsb->freqAdjust;
2816 /* multiply by primary sample size */
2817 probably_valid_left *= primarybuf->wfx.nBlockAlign;
2818 /* check whether to clip mix_len */
2819 if (probably_valid_left < mixlen) {
2820 TRACE("clipping to probably_valid_left=%ld\n", probably_valid_left);
2821 mixlen = probably_valid_left;
2825 /* cut mixlen with what's already been mixed */
2826 if (mixlen < primary_done) {
2827 /* huh? and still CalcPlayPosition didn't
2828 * detect an underrun? */
2829 FIXME("problem with underrun detection (mixlen=%ld < primary_done=%ld)\n", mixlen, primary_done);
2832 len = mixlen - primary_done;
2833 TRACE("remaining mixlen=%ld\n", len);
2835 if (len < primarybuf->dsound->fraglen) {
2836 /* smaller than a fragment, wait until it gets larger
2837 * before we take the mixing overhead */
2838 TRACE("mixlen not worth it, deferring mixing\n");
2842 /* ok, we know how much to mix, let's go */
2843 still_behind = (adv_done > primary_done);
2845 slen = primarybuf->buflen - dsb->primary_mixpos;
2846 if (slen > len) slen = len;
2847 slen = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, slen);
2849 if ((dsb->primary_mixpos < primarybuf->buf_mixpos) &&
2850 (dsb->primary_mixpos + slen >= primarybuf->buf_mixpos))
2851 still_behind = FALSE;
2853 dsb->primary_mixpos += slen; len -= slen;
2854 while (dsb->primary_mixpos >= primarybuf->buflen)
2855 dsb->primary_mixpos -= primarybuf->buflen;
2857 if ((dsb->state == STATE_STOPPED) || !slen) break;
2859 TRACE("new primary_mixpos=%ld, primary_advbase=%ld\n", dsb->primary_mixpos, primarybuf->buf_mixpos);
2860 TRACE("mixed data len=%ld, still_behind=%d\n", mixlen-len, still_behind);
2861 /* return how far we think the primary buffer can
2862 * advance its underrun detector...*/
2863 if (still_behind) return 0;
2864 if ((mixlen - len) < primary_done) return 0;
2865 slen = ((dsb->primary_mixpos < primarybuf->buf_mixpos) ?
2866 primarybuf->buflen : 0) + dsb->primary_mixpos -
2867 primarybuf->buf_mixpos;
2868 if (slen > mixlen) {
2869 /* the primary_done and still_behind checks above should have worked */
2870 FIXME("problem with advancement calculation (advlen=%ld > mixlen=%ld)\n", slen, mixlen);
2876 static DWORD DSOUND_MixToPrimary(DWORD playpos, DWORD writepos, DWORD mixlen, BOOL recover)
2878 INT i, len, maxlen = 0;
2879 IDirectSoundBufferImpl *dsb;
2881 TRACE("(%ld,%ld,%ld)\n", playpos, writepos, mixlen);
2882 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
2883 dsb = dsound->buffers[i];
2885 if (!dsb || !(ICOM_VTBL(dsb)))
2887 if (dsb->buflen && dsb->state && !dsb->hwbuf) {
2888 TRACE("Checking %p, mixlen=%ld\n", dsb, mixlen);
2889 EnterCriticalSection(&(dsb->lock));
2890 if (dsb->state == STATE_STOPPING) {
2891 DSOUND_MixCancel(dsb, writepos);
2892 dsb->state = STATE_STOPPED;
2894 if ((dsb->state == STATE_STARTING) || recover)
2895 dsb->primary_mixpos = writepos;
2896 len = DSOUND_MixOne(dsb, playpos, writepos, mixlen);
2897 if (dsb->state == STATE_STARTING)
2898 dsb->state = STATE_PLAYING;
2899 maxlen = (len > maxlen) ? len : maxlen;
2901 LeaveCriticalSection(&(dsb->lock));
2908 static void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
2914 if (!dsound || !primarybuf) {
2915 ERR("dsound died without killing us?\n");
2916 timeKillEvent(timerID);
2917 timeEndPeriod(DS_TIME_RES);
2921 EnterCriticalSection(&(dsound->lock));
2923 if (!primarybuf || !primarybuf->ref) {
2924 /* seems the primary buffer is currently being released */
2925 LeaveCriticalSection(&(dsound->lock));
2929 /* the sound of silence */
2930 nfiller = primarybuf->wfx.wBitsPerSample == 8 ? 128 : 0;
2932 /* whether the primary is forced to play even without secondary buffers */
2933 forced = ((primarybuf->state == STATE_PLAYING) || (primarybuf->state == STATE_STARTING));
2935 if (primarybuf->hwbuf) {
2936 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
2937 BOOL paused = ((primarybuf->state == STATE_STOPPED) || (primarybuf->state == STATE_STARTING));
2938 DWORD playpos, writepos, inq, maxq, frag;
2939 hres = IDsDriverBuffer_GetPosition(primarybuf->hwbuf, &playpos, &writepos);
2941 LeaveCriticalSection(&(dsound->lock));
2944 /* Well, we *could* do Just-In-Time mixing using the writepos,
2945 * but that's a little bit ambitious and unnecessary... */
2946 /* rather add our safety margin to the writepos, if we're playing */
2948 writepos += primarybuf->writelead;
2949 while (writepos >= primarybuf->buflen)
2950 writepos -= primarybuf->buflen;
2951 } else writepos = playpos;
2952 TRACE("primary playpos=%ld, writepos=%ld, clrpos=%ld, mixpos=%ld\n",
2953 playpos,writepos,primarybuf->playpos,primarybuf->buf_mixpos);
2954 /* wipe out just-played sound data */
2955 if (playpos < primarybuf->playpos) {
2956 memset(primarybuf->buffer + primarybuf->playpos, nfiller, primarybuf->buflen - primarybuf->playpos);
2957 memset(primarybuf->buffer, nfiller, playpos);
2959 memset(primarybuf->buffer + primarybuf->playpos, nfiller, playpos - primarybuf->playpos);
2961 primarybuf->playpos = playpos;
2963 /* check how much prebuffering is left */
2964 inq = primarybuf->buf_mixpos;
2966 inq += primarybuf->buflen;
2969 /* find the maximum we can prebuffer */
2972 if (maxq < writepos)
2973 maxq += primarybuf->buflen;
2975 } else maxq = primarybuf->buflen;
2977 /* clip maxq to DS_SND_QUEUE */
2978 frag = DS_SND_QUEUE * dsound->fraglen;
2979 if (maxq > frag) maxq = frag;
2981 EnterCriticalSection(&(primarybuf->lock));
2983 /* check for consistency */
2985 /* the playback position must have passed our last
2986 * mixed position, i.e. it's an underrun, or we have
2987 * nothing more to play */
2988 TRACE("reached end of mixed data (inq=%ld, maxq=%ld)\n", inq, maxq);
2990 /* stop the playback now, to allow buffers to refill */
2991 DSOUND_PrimaryStop(primarybuf);
2992 if (primarybuf->state == STATE_PLAYING) {
2993 primarybuf->state = STATE_STARTING;
2995 else if (primarybuf->state == STATE_STOPPING) {
2996 primarybuf->state = STATE_STOPPED;
2999 /* how can we have an underrun if we aren't playing? */
3000 WARN("unexpected primary state (%ld)\n", primarybuf->state);
3002 /* the Stop is supposed to reset play position to beginning of buffer */
3003 /* unfortunately, OSS is not able to do so, so get current pointer */
3004 hres = IDsDriverBuffer_GetPosition(primarybuf->hwbuf, &playpos, NULL);
3006 LeaveCriticalSection(&(dsound->lock));
3007 LeaveCriticalSection(&(primarybuf->lock));
3011 primarybuf->playpos = playpos;
3012 primarybuf->buf_mixpos = writepos;
3014 maxq = primarybuf->buflen;
3015 if (maxq > frag) maxq = frag;
3016 memset(primarybuf->buffer, nfiller, primarybuf->buflen);
3021 frag = DSOUND_MixToPrimary(playpos, writepos, maxq, paused);
3022 if (forced) frag = maxq - inq;
3023 primarybuf->buf_mixpos += frag;
3024 while (primarybuf->buf_mixpos >= primarybuf->buflen)
3025 primarybuf->buf_mixpos -= primarybuf->buflen;
3028 /* buffers have been filled, restart playback */
3029 if (primarybuf->state == STATE_STARTING) {
3030 DSOUND_PrimaryPlay(primarybuf);
3031 primarybuf->state = STATE_PLAYING;
3032 TRACE("starting playback\n");
3034 else if (primarybuf->state == STATE_STOPPED) {
3035 /* the primarybuf is supposed to play if there's something to play
3036 * even if it is reported as stopped, so don't let this confuse you */
3037 DSOUND_PrimaryPlay(primarybuf);
3038 primarybuf->state = STATE_STOPPING;
3039 TRACE("starting playback\n");
3042 LeaveCriticalSection(&(primarybuf->lock));
3044 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
3045 if (primarybuf->state == STATE_STARTING) {
3046 DSOUND_PrimaryPlay(primarybuf);
3047 primarybuf->state = STATE_PLAYING;
3049 else if (primarybuf->state == STATE_STOPPING) {
3050 DSOUND_PrimaryStop(primarybuf);
3051 primarybuf->state = STATE_STOPPED;
3055 /* using waveOut stuff */
3056 /* if no buffers are playing, we should be in pause mode now */
3057 DWORD writepos, mixq;
3058 /* clean out completed fragments */
3059 while (dsound->pwqueue && (dsound->pwave[dsound->pwplay]->dwFlags & WHDR_DONE)) {
3060 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
3061 DWORD pos = dsound->pwplay * dsound->fraglen;
3062 TRACE("done playing primary pos=%ld\n",pos);
3063 memset(primarybuf->buffer + pos, nfiller, dsound->fraglen);
3066 if (dsound->pwplay >= DS_HEL_FRAGS) dsound->pwplay = 0;
3069 primarybuf->playpos = dsound->pwplay * dsound->fraglen;
3070 TRACE("primary playpos=%ld, mixpos=%ld\n",primarybuf->playpos,primarybuf->buf_mixpos);
3071 EnterCriticalSection(&(primarybuf->lock));
3073 if (!dsound->pwqueue) {
3074 /* this is either an underrun or we have nothing more to play...
3075 * since playback has already stopped now, we can enter pause mode,
3076 * in order to allow buffers to refill */
3077 if (primarybuf->state == STATE_PLAYING) {
3078 TRACE("no more fragments ready to play\n");
3079 waveOutPause(dsound->hwo);
3080 primarybuf->state = STATE_STARTING;
3082 else if (primarybuf->state == STATE_STOPPING) {
3083 TRACE("no more fragments ready to play\n");
3084 waveOutPause(dsound->hwo);
3085 primarybuf->state = STATE_STOPPED;
3089 /* find next write position, plus some extra margin, if necessary */
3090 mixq = DS_HEL_MARGIN;
3091 if (mixq > dsound->pwqueue) mixq = dsound->pwqueue;
3092 writepos = primarybuf->playpos + mixq * dsound->fraglen;
3093 while (writepos >= primarybuf->buflen) writepos -= primarybuf->buflen;
3096 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
3097 DWORD frag, maxq = DS_SND_QUEUE * dsound->fraglen;
3098 frag = DSOUND_MixToPrimary(primarybuf->playpos, writepos, maxq, FALSE);
3099 mixq = frag / dsound->fraglen;
3100 if (frag - (mixq * dsound->fraglen))
3103 if (forced) mixq = DS_SND_QUEUE;
3106 for (; mixq; mixq--) {
3107 waveOutWrite(dsound->hwo, dsound->pwave[dsound->pwwrite], sizeof(WAVEHDR));
3109 if (dsound->pwwrite >= DS_HEL_FRAGS) dsound->pwwrite = 0;
3112 primarybuf->buf_mixpos = dsound->pwwrite * dsound->fraglen;
3114 if (dsound->pwqueue) {
3115 /* buffers have been filled, restart playback */
3116 if (primarybuf->state == STATE_STARTING) {
3117 waveOutRestart(dsound->hwo);
3118 primarybuf->state = STATE_PLAYING;
3119 TRACE("starting playback\n");
3121 else if (primarybuf->state == STATE_STOPPED) {
3122 /* the primarybuf is supposed to play if there's something to play
3123 * even if it is reported as stopped, so don't let this confuse you */
3124 waveOutRestart(dsound->hwo);
3125 primarybuf->state = STATE_STOPPING;
3126 TRACE("starting playback\n");
3129 LeaveCriticalSection(&(primarybuf->lock));
3131 TRACE("completed processing\n");
3132 LeaveCriticalSection(&(dsound->lock));
3135 /*******************************************************************************
3138 HRESULT WINAPI DirectSoundCreate(REFGUID lpGUID,LPDIRECTSOUND *ppDS,IUnknown *pUnkOuter )
3140 IDirectSoundImpl** ippDS=(IDirectSoundImpl**)ppDS;
3141 PIDSDRIVER drv = NULL;
3144 HRESULT err = DS_OK;
3147 TRACE("(%p,%p,%p)\n",lpGUID,ippDS,pUnkOuter);
3149 TRACE("DirectSoundCreate (%p)\n", ippDS);
3152 return DSERR_INVALIDPARAM;
3155 IDirectSound_AddRef((LPDIRECTSOUND)dsound);
3160 /* Enumerate WINMM audio devices and find the one we want */
3161 wodn = waveOutGetNumDevs();
3162 if (!wodn) return DSERR_NODRIVER;
3164 /* FIXME: How do we find the GUID of an audio device? */
3165 /* Well, just use the first available device for now... */
3167 /* Get output device caps */
3168 waveOutGetDevCapsA(wod, &wcaps, sizeof(wcaps));
3169 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
3170 waveOutMessage(wod, DRV_QUERYDSOUNDIFACE, (DWORD)&drv, 0);
3172 /* Allocate memory */
3173 *ippDS = (IDirectSoundImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundImpl));
3175 return DSERR_OUTOFMEMORY;
3177 ICOM_VTBL(*ippDS) = &dsvt;
3180 (*ippDS)->driver = drv;
3181 (*ippDS)->fraglen = 0;
3182 (*ippDS)->priolevel = DSSCL_NORMAL;
3183 (*ippDS)->nrofbuffers = 0;
3184 (*ippDS)->buffers = NULL;
3185 (*ippDS)->primary = NULL;
3186 (*ippDS)->listener = NULL;
3188 /* Get driver description */
3190 IDsDriver_GetDriverDesc(drv,&((*ippDS)->drvdesc));
3192 /* if no DirectSound interface available, use WINMM API instead */
3193 (*ippDS)->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
3194 (*ippDS)->drvdesc.dnDevNode = wod; /* FIXME? */
3197 /* Set default wave format (may need it for waveOutOpen) */
3198 (*ippDS)->wfx.wFormatTag = WAVE_FORMAT_PCM;
3199 (*ippDS)->wfx.nChannels = 2;
3200 (*ippDS)->wfx.nSamplesPerSec = 22050;
3201 (*ippDS)->wfx.nAvgBytesPerSec = 44100;
3202 (*ippDS)->wfx.nBlockAlign = 2;
3203 (*ippDS)->wfx.wBitsPerSample = 8;
3205 /* If the driver requests being opened through MMSYSTEM
3206 * (which is recommended by the DDK), it is supposed to happen
3207 * before the DirectSound interface is opened */
3208 if ((*ippDS)->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
3209 /* FIXME: is this right? */
3210 err = mmErr(waveOutOpen(&((*ippDS)->hwo), (*ippDS)->drvdesc.dnDevNode, &((*ippDS)->wfx),
3211 0, 0, CALLBACK_NULL | WAVE_DIRECTSOUND));
3214 if (drv && (err == DS_OK))
3215 err = IDsDriver_Open(drv);
3217 /* FIXME: do we want to handle a temporarily busy device? */
3219 HeapFree(GetProcessHeap(),0,*ippDS);
3224 /* the driver is now open, so it's now allowed to call GetCaps */
3226 IDsDriver_GetCaps(drv,&((*ippDS)->drvcaps));
3230 /* FIXME: look at wcaps */
3231 (*ippDS)->drvcaps.dwFlags =
3232 DSCAPS_PRIMARY16BIT | DSCAPS_PRIMARYSTEREO;
3234 (*ippDS)->drvcaps.dwFlags |= DSCAPS_EMULDRIVER;
3236 /* Allocate memory for HEL buffer headers */
3237 for (c=0; c<DS_HEL_FRAGS; c++) {
3238 (*ippDS)->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
3239 if (!(*ippDS)->pwave[c]) {
3240 /* Argh, out of memory */
3242 HeapFree(GetProcessHeap(),0,(*ippDS)->pwave[c]);
3243 waveOutClose((*ippDS)->hwo);
3244 HeapFree(GetProcessHeap(),0,*ippDS);
3246 return DSERR_OUTOFMEMORY;
3252 InitializeCriticalSection(&((*ippDS)->lock));
3256 if (primarybuf == NULL) {
3260 dsbd.dwSize = sizeof(DSBUFFERDESC);
3261 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
3262 dsbd.dwBufferBytes = 0;
3263 dsbd.lpwfxFormat = &(dsound->wfx);
3264 hr = IDirectSound_CreateSoundBuffer(*ppDS, &dsbd, (LPDIRECTSOUNDBUFFER*)&primarybuf, NULL);
3268 /* dsound->primary is NULL - don't need to Release */
3269 dsound->primary = primarybuf;
3270 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
3272 timeBeginPeriod(DS_TIME_RES);
3273 dsound->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, DSOUND_timer,
3274 (DWORD)dsound, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
3279 /***************************************************************************
3280 * DirectSoundCaptureCreate [DSOUND.6]
3282 * Create and initialize a DirectSoundCapture interface
3286 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
3289 HRESULT WINAPI DirectSoundCaptureCreate(
3291 LPDIRECTSOUNDCAPTURE* lplpDSC,
3292 LPUNKNOWN pUnkOuter )
3294 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), lplpDSC, pUnkOuter);
3297 return DSERR_NOAGGREGATION;
3300 /* Default device? */
3302 return DSOUND_CreateDirectSoundCapture( (LPVOID*)lplpDSC );
3305 FIXME( "Unknown GUID %s\n", debugstr_guid(lpcGUID) );
3308 return DSERR_OUTOFMEMORY;
3311 /***************************************************************************
3312 * DirectSoundCaptureEnumerateA [DSOUND.7]
3314 * Enumerate all DirectSound drivers installed in the system
3318 * Failure: DSERR_INVALIDPARAM
3320 HRESULT WINAPI DirectSoundCaptureEnumerateA(
3321 LPDSENUMCALLBACKA lpDSEnumCallback,
3324 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
3326 if ( lpDSEnumCallback )
3327 lpDSEnumCallback(NULL,"WINE Primary Sound Capture Driver",
3328 "SoundCap",lpContext);
3334 /***************************************************************************
3335 * DirectSoundCaptureEnumerateW [DSOUND.8]
3337 * Enumerate all DirectSound drivers installed in the system
3341 * Failure: DSERR_INVALIDPARAM
3343 HRESULT WINAPI DirectSoundCaptureEnumerateW(
3344 LPDSENUMCALLBACKW lpDSEnumCallback,
3347 FIXME("(%p,%p):stub\n", lpDSEnumCallback, lpContext );
3352 DSOUND_CreateDirectSoundCapture( LPVOID* ppobj )
3354 *ppobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( IDirectSoundCaptureImpl ) );
3356 if ( *ppobj == NULL ) {
3357 return DSERR_OUTOFMEMORY;
3361 ICOM_THIS(IDirectSoundCaptureImpl,*ppobj);
3364 ICOM_VTBL(This) = &dscvt;
3366 InitializeCriticalSection( &This->lock );
3372 static HRESULT WINAPI
3373 IDirectSoundCaptureImpl_QueryInterface(
3374 LPDIRECTSOUNDCAPTURE iface,
3378 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3380 FIXME( "(%p)->(%s,%p): stub\n", This, debugstr_guid(riid), ppobj );
3386 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
3389 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3391 EnterCriticalSection( &This->lock );
3393 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3394 uRef = ++(This->ref);
3396 LeaveCriticalSection( &This->lock );
3402 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
3405 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3407 EnterCriticalSection( &This->lock );
3409 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3410 uRef = --(This->ref);
3412 LeaveCriticalSection( &This->lock );
3415 DeleteCriticalSection( &This->lock );
3416 HeapFree( GetProcessHeap(), 0, This );
3422 static HRESULT WINAPI
3423 IDirectSoundCaptureImpl_CreateCaptureBuffer(
3424 LPDIRECTSOUNDCAPTURE iface,
3425 LPCDSCBUFFERDESC lpcDSCBufferDesc,
3426 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
3430 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3432 TRACE( "(%p)->(%p,%p,%p)\n", This, lpcDSCBufferDesc, lplpDSCaptureBuffer, pUnk );
3435 return DSERR_INVALIDPARAM;
3438 hr = DSOUND_CreateDirectSoundCaptureBuffer( lpcDSCBufferDesc, (LPVOID*)lplpDSCaptureBuffer );
3443 static HRESULT WINAPI
3444 IDirectSoundCaptureImpl_GetCaps(
3445 LPDIRECTSOUNDCAPTURE iface,
3446 LPDSCCAPS lpDSCCaps )
3448 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3450 FIXME( "(%p)->(%p): stub\n", This, lpDSCCaps );
3455 static HRESULT WINAPI
3456 IDirectSoundCaptureImpl_Initialize(
3457 LPDIRECTSOUNDCAPTURE iface,
3460 ICOM_THIS(IDirectSoundCaptureImpl,iface);
3462 FIXME( "(%p)->(%p): stub\n", This, lpcGUID );
3468 static ICOM_VTABLE(IDirectSoundCapture) dscvt =
3470 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3471 /* IUnknown methods */
3472 IDirectSoundCaptureImpl_QueryInterface,
3473 IDirectSoundCaptureImpl_AddRef,
3474 IDirectSoundCaptureImpl_Release,
3476 /* IDirectSoundCapture methods */
3477 IDirectSoundCaptureImpl_CreateCaptureBuffer,
3478 IDirectSoundCaptureImpl_GetCaps,
3479 IDirectSoundCaptureImpl_Initialize
3483 DSOUND_CreateDirectSoundCaptureBuffer( LPCDSCBUFFERDESC lpcDSCBufferDesc, LPVOID* ppobj )
3486 FIXME( "(%p,%p): ignoring lpcDSCBufferDesc\n", lpcDSCBufferDesc, ppobj );
3488 *ppobj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( IDirectSoundCaptureBufferImpl ) );
3490 if ( *ppobj == NULL ) {
3491 return DSERR_OUTOFMEMORY;
3495 ICOM_THIS(IDirectSoundCaptureBufferImpl,*ppobj);
3498 ICOM_VTBL(This) = &dscbvt;
3500 InitializeCriticalSection( &This->lock );
3507 static HRESULT WINAPI
3508 IDirectSoundCaptureBufferImpl_QueryInterface(
3509 LPDIRECTSOUNDCAPTUREBUFFER iface,
3513 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3515 FIXME( "(%p)->(%s,%p): stub\n", This, debugstr_guid(riid), ppobj );
3521 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER iface )
3524 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3526 EnterCriticalSection( &This->lock );
3528 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3529 uRef = ++(This->ref);
3531 LeaveCriticalSection( &This->lock );
3537 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER iface )
3540 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3542 EnterCriticalSection( &This->lock );
3544 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
3545 uRef = --(This->ref);
3547 LeaveCriticalSection( &This->lock );
3550 DeleteCriticalSection( &This->lock );
3551 HeapFree( GetProcessHeap(), 0, This );
3557 static HRESULT WINAPI
3558 IDirectSoundCaptureBufferImpl_GetCaps(
3559 LPDIRECTSOUNDCAPTUREBUFFER iface,
3560 LPDSCBCAPS lpDSCBCaps )
3562 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3564 FIXME( "(%p)->(%p): stub\n", This, lpDSCBCaps );
3569 static HRESULT WINAPI
3570 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
3571 LPDIRECTSOUNDCAPTUREBUFFER iface,
3572 LPDWORD lpdwCapturePosition,
3573 LPDWORD lpdwReadPosition )
3575 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3577 FIXME( "(%p)->(%p,%p): stub\n", This, lpdwCapturePosition, lpdwReadPosition );
3582 static HRESULT WINAPI
3583 IDirectSoundCaptureBufferImpl_GetFormat(
3584 LPDIRECTSOUNDCAPTUREBUFFER iface,
3585 LPWAVEFORMATEX lpwfxFormat,
3586 DWORD dwSizeAllocated,
3587 LPDWORD lpdwSizeWritten )
3589 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3591 FIXME( "(%p)->(%p,0x%08lx,%p): stub\n", This, lpwfxFormat, dwSizeAllocated, lpdwSizeWritten );
3596 static HRESULT WINAPI
3597 IDirectSoundCaptureBufferImpl_GetStatus(
3598 LPDIRECTSOUNDCAPTUREBUFFER iface,
3599 LPDWORD lpdwStatus )
3601 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3603 FIXME( "(%p)->(%p): stub\n", This, lpdwStatus );
3608 static HRESULT WINAPI
3609 IDirectSoundCaptureBufferImpl_Initialize(
3610 LPDIRECTSOUNDCAPTUREBUFFER iface,
3611 LPDIRECTSOUNDCAPTURE lpDSC,
3612 LPCDSCBUFFERDESC lpcDSCBDesc )
3614 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3616 FIXME( "(%p)->(%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
3621 static HRESULT WINAPI
3622 IDirectSoundCaptureBufferImpl_Lock(
3623 LPDIRECTSOUNDCAPTUREBUFFER iface,
3626 LPVOID* lplpvAudioPtr1,
3627 LPDWORD lpdwAudioBytes1,
3628 LPVOID* lplpvAudioPtr2,
3629 LPDWORD lpdwAudioBytes2,
3632 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3634 FIXME( "(%p)->(%08lu,%08lu,%p,%p,%p,%p,0x%08lx): stub\n", This, dwReadCusor, dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2, lpdwAudioBytes2, dwFlags );
3639 static HRESULT WINAPI
3640 IDirectSoundCaptureBufferImpl_Start(
3641 LPDIRECTSOUNDCAPTUREBUFFER iface,
3644 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3646 FIXME( "(%p)->(0x%08lx): stub\n", This, dwFlags );
3651 static HRESULT WINAPI
3652 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER iface )
3654 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3656 FIXME( "(%p): stub\n", This );
3661 static HRESULT WINAPI
3662 IDirectSoundCaptureBufferImpl_Unlock(
3663 LPDIRECTSOUNDCAPTUREBUFFER iface,
3664 LPVOID lpvAudioPtr1,
3665 DWORD dwAudioBytes1,
3666 LPVOID lpvAudioPtr2,
3667 DWORD dwAudioBytes2 )
3669 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
3671 FIXME( "(%p)->(%p,%08lu,%p,%08lu): stub\n", This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2 );
3677 static ICOM_VTABLE(IDirectSoundCaptureBuffer) dscbvt =
3679 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3680 /* IUnknown methods */
3681 IDirectSoundCaptureBufferImpl_QueryInterface,
3682 IDirectSoundCaptureBufferImpl_AddRef,
3683 IDirectSoundCaptureBufferImpl_Release,
3685 /* IDirectSoundCaptureBuffer methods */
3686 IDirectSoundCaptureBufferImpl_GetCaps,
3687 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
3688 IDirectSoundCaptureBufferImpl_GetFormat,
3689 IDirectSoundCaptureBufferImpl_GetStatus,
3690 IDirectSoundCaptureBufferImpl_Initialize,
3691 IDirectSoundCaptureBufferImpl_Lock,
3692 IDirectSoundCaptureBufferImpl_Start,
3693 IDirectSoundCaptureBufferImpl_Stop,
3694 IDirectSoundCaptureBufferImpl_Unlock
3697 /*******************************************************************************
3698 * DirectSound ClassFactory
3702 /* IUnknown fields */
3703 ICOM_VFIELD(IClassFactory);
3705 } IClassFactoryImpl;
3707 static HRESULT WINAPI
3708 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
3709 ICOM_THIS(IClassFactoryImpl,iface);
3711 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
3712 return E_NOINTERFACE;
3716 DSCF_AddRef(LPCLASSFACTORY iface) {
3717 ICOM_THIS(IClassFactoryImpl,iface);
3718 return ++(This->ref);
3721 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface) {
3722 ICOM_THIS(IClassFactoryImpl,iface);
3723 /* static class, won't be freed */
3724 return --(This->ref);
3727 static HRESULT WINAPI DSCF_CreateInstance(
3728 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
3730 ICOM_THIS(IClassFactoryImpl,iface);
3732 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
3733 if ( IsEqualGUID( &IID_IDirectSound, riid ) ) {
3734 /* FIXME: reuse already created dsound if present? */
3735 return DirectSoundCreate(riid,(LPDIRECTSOUND*)ppobj,pOuter);
3737 return E_NOINTERFACE;
3740 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
3741 ICOM_THIS(IClassFactoryImpl,iface);
3742 FIXME("(%p)->(%d),stub!\n",This,dolock);
3746 static ICOM_VTABLE(IClassFactory) DSCF_Vtbl = {
3747 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3748 DSCF_QueryInterface,
3751 DSCF_CreateInstance,
3754 static IClassFactoryImpl DSOUND_CF = {&DSCF_Vtbl, 1 };
3756 /*******************************************************************************
3757 * DllGetClassObject [DSOUND.4]
3758 * Retrieves class object from a DLL object
3761 * Docs say returns STDAPI
3764 * rclsid [I] CLSID for the class object
3765 * riid [I] Reference to identifier of interface for class object
3766 * ppv [O] Address of variable to receive interface pointer for riid
3770 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
3773 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
3775 TRACE("(%p,%p,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
3776 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
3777 *ppv = (LPVOID)&DSOUND_CF;
3778 IClassFactory_AddRef((IClassFactory*)*ppv);
3782 FIXME("(%p,%p,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
3783 return CLASS_E_CLASSNOTAVAILABLE;
3787 /*******************************************************************************
3788 * DllCanUnloadNow [DSOUND.3] Determines whether the DLL is in use.
3794 DWORD WINAPI DSOUND_DllCanUnloadNow(void)
3796 FIXME("(void): stub\n");