3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
7 * Note: This file requires multithread ability. It is not possible to
8 * implement the stuff in a single thread anyway. And most DirectX apps
9 * require threading themselves.
11 * Most thread locking is complete. There may be a few race
12 * conditions still lurking.
14 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
15 * and a Turtle Beach Tropez+.
18 * Implement DirectSoundCapture API
19 * Implement SetCooperativeLevel properly (need to address focus issues)
20 * Use wavetable synth for static buffers if available
21 * Implement DirectSound3DBuffers (stubs in place)
22 * Use hardware 3D support if available (OSS support may be needed first)
23 * Add support for APIs other than OSS: ALSA (http://alsa.jcu.cz/)
24 * and esound (http://www.gnome.org), for instance
26 * FIXME: Status needs updating.
29 * - Wing Commander 4/W95:
30 * The intromovie plays without problems. Nearly lipsynchron.
32 * The sound works, but noticeable chunks are left out (from the sound and
33 * the animation). Don't know why yet.
35 * Sound works, but slows down the movieplayer.
39 * The background sound of the startscreen works ;)
40 * - WingCommander Prophecy Demo:
41 * Sound works for the intromovie.
42 * - Total Annihilation (1998/12/04):
43 * Sound plays perfectly in the game, but the Smacker movies
44 * (http://www.smacker.com/) play silently.
45 * - A-10 Cuba! Demo (1998/12/04):
46 * Sound works properly (for some people).
47 * - dsstream.exe, from DirectX 5.2 SDK (1998/12/04):
48 * Works properly, but requires "-dll -winmm".
49 * - dsshow.exe, from DirectX 5.2 SDK (1998/12/04):
50 * Initializes the DLL properly with CoCreateInstance(), but the
51 * FileOpen dialog box is broken - could not test properly
58 #include <sys/types.h>
60 #include <sys/fcntl.h>
64 #include <math.h> /* Insomnia - pow() function */
70 #include "wine/obj_base.h"
72 #include "debugtools.h"
74 DEFAULT_DEBUG_CHANNEL(dsound);
77 /*****************************************************************************
78 * Predeclare the interface implementation structures
80 typedef struct IDirectSoundImpl IDirectSoundImpl;
81 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
82 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
83 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
84 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
86 /*****************************************************************************
87 * IDirectSound implementation structure
89 struct IDirectSoundImpl
92 ICOM_VFIELD(IDirectSound);
94 /* IDirectSoundImpl fields */
97 IDirectSoundBufferImpl** buffers;
98 IDirectSoundBufferImpl* primary;
99 IDirectSound3DListenerImpl* listener;
100 WAVEFORMATEX wfx; /* current main waveformat */
101 CRITICAL_SECTION lock;
104 /*****************************************************************************
105 * IDirectSoundBuffer implementation structure
107 struct IDirectSoundBufferImpl
109 /* IUnknown fields */
110 ICOM_VFIELD(IDirectSoundBuffer);
112 /* IDirectSoundBufferImpl fields */
115 IDirectSound3DBufferImpl* ds3db;
116 DWORD playflags,playing;
117 DWORD playpos,writepos,buflen;
118 DWORD nAvgBytesPerSec;
122 LONG lVolAdjust,rVolAdjust;
123 IDirectSoundBufferImpl* parent; /* for duplicates */
124 IDirectSoundImpl* dsound;
126 LPDSBPOSITIONNOTIFY notifies;
128 CRITICAL_SECTION lock;
131 /*****************************************************************************
132 * IDirectSoundNotify implementation structure
134 struct IDirectSoundNotifyImpl
136 /* IUnknown fields */
137 ICOM_VFIELD(IDirectSoundNotify);
139 /* IDirectSoundNotifyImpl fields */
140 IDirectSoundBufferImpl* dsb;
143 /*****************************************************************************
144 * IDirectSound3DListener implementation structure
146 struct IDirectSound3DListenerImpl
148 /* IUnknown fields */
149 ICOM_VFIELD(IDirectSound3DListener);
151 /* IDirectSound3DListenerImpl fields */
152 IDirectSoundBufferImpl* dsb;
154 CRITICAL_SECTION lock;
157 /*****************************************************************************
158 * IDirectSound3DBuffer implementation structure
160 struct IDirectSound3DBufferImpl
162 /* IUnknown fields */
163 ICOM_VFIELD(IDirectSound3DBuffer);
165 /* IDirectSound3DBufferImpl fields */
166 IDirectSoundBufferImpl* dsb;
170 CRITICAL_SECTION lock;
175 # include <sys/ioctl.h>
176 # ifdef HAVE_MACHINE_SOUNDCARD_H
177 # include <machine/soundcard.h>
179 # ifdef HAVE_SYS_SOUNDCARD_H
180 # include <sys/soundcard.h>
182 # ifdef HAVE_SOUNDCARD_H
183 # include <soundcard.h>
186 /* #define USE_DSOUND3D 1 */
188 #define DSOUND_FRAGLEN (primarybuf->wfx.nAvgBytesPerSec >> 4)
189 #define DSOUND_FREQSHIFT (14)
191 static int audiofd = -1;
192 static int audioOK = 0;
194 static IDirectSoundImpl* dsound = NULL;
196 static IDirectSoundBufferImpl* primarybuf = NULL;
198 static int DSOUND_setformat(LPWAVEFORMATEX wfex);
199 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
200 static void DSOUND_CloseAudio(void);
205 HRESULT WINAPI DirectSoundEnumerateA(
206 LPDSENUMCALLBACKA enumcb,
209 TRACE("enumcb = %p, context = %p\n", enumcb, context);
213 enumcb(NULL,"WINE DirectSound using Open Sound System",
221 static void _dump_DSBCAPS(DWORD xmask) {
226 #define FE(x) { x, #x },
227 FE(DSBCAPS_PRIMARYBUFFER)
229 FE(DSBCAPS_LOCHARDWARE)
230 FE(DSBCAPS_LOCSOFTWARE)
231 FE(DSBCAPS_CTRLFREQUENCY)
233 FE(DSBCAPS_CTRLVOLUME)
234 FE(DSBCAPS_CTRLDEFAULT)
236 FE(DSBCAPS_STICKYFOCUS)
237 FE(DSBCAPS_GETCURRENTPOSITION2)
241 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
242 if (flags[i].mask & xmask)
243 DPRINTF("%s ",flags[i].name);
246 /*******************************************************************************
247 * IDirectSound3DBuffer
250 /* IUnknown methods */
251 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
252 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
254 ICOM_THIS(IDirectSound3DBufferImpl,iface);
256 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
260 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
262 ICOM_THIS(IDirectSound3DBufferImpl,iface);
267 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
269 ICOM_THIS(IDirectSound3DBufferImpl,iface);
273 HeapFree(GetProcessHeap(),0,This->buffer);
274 HeapFree(GetProcessHeap(),0,This);
279 /* IDirectSound3DBuffer methods */
280 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
281 LPDIRECTSOUND3DBUFFER iface,
282 LPDS3DBUFFER lpDs3dBuffer)
288 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
289 LPDIRECTSOUND3DBUFFER iface,
290 LPDWORD lpdwInsideConeAngle,
291 LPDWORD lpdwOutsideConeAngle)
297 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
298 LPDIRECTSOUND3DBUFFER iface,
299 LPD3DVECTOR lpvConeOrientation)
305 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
306 LPDIRECTSOUND3DBUFFER iface,
307 LPLONG lplConeOutsideVolume)
313 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
314 LPDIRECTSOUND3DBUFFER iface,
315 LPD3DVALUE lpfMaxDistance)
321 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
322 LPDIRECTSOUND3DBUFFER iface,
323 LPD3DVALUE lpfMinDistance)
329 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
330 LPDIRECTSOUND3DBUFFER iface,
337 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
338 LPDIRECTSOUND3DBUFFER iface,
339 LPD3DVECTOR lpvPosition)
345 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
346 LPDIRECTSOUND3DBUFFER iface,
347 LPD3DVECTOR lpvVelocity)
353 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
354 LPDIRECTSOUND3DBUFFER iface,
355 LPCDS3DBUFFER lpcDs3dBuffer,
362 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
363 LPDIRECTSOUND3DBUFFER iface,
364 DWORD dwInsideConeAngle,
365 DWORD dwOutsideConeAngle,
372 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
373 LPDIRECTSOUND3DBUFFER iface,
374 D3DVALUE x, D3DVALUE y, D3DVALUE z,
381 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
382 LPDIRECTSOUND3DBUFFER iface,
383 LONG lConeOutsideVolume,
390 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
391 LPDIRECTSOUND3DBUFFER iface,
392 D3DVALUE fMaxDistance,
399 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
400 LPDIRECTSOUND3DBUFFER iface,
401 D3DVALUE fMinDistance,
408 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
409 LPDIRECTSOUND3DBUFFER iface,
413 ICOM_THIS(IDirectSound3DBufferImpl,iface);
414 TRACE("mode = %lx\n", dwMode);
415 This->ds3db.dwMode = dwMode;
419 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
420 LPDIRECTSOUND3DBUFFER iface,
421 D3DVALUE x, D3DVALUE y, D3DVALUE z,
428 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
429 LPDIRECTSOUND3DBUFFER iface,
430 D3DVALUE x, D3DVALUE y, D3DVALUE z,
437 ICOM_VTABLE(IDirectSound3DBuffer) ds3dbvt =
439 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
440 /* IUnknown methods */
441 IDirectSound3DBufferImpl_QueryInterface,
442 IDirectSound3DBufferImpl_AddRef,
443 IDirectSound3DBufferImpl_Release,
444 /* IDirectSound3DBuffer methods */
445 IDirectSound3DBufferImpl_GetAllParameters,
446 IDirectSound3DBufferImpl_GetConeAngles,
447 IDirectSound3DBufferImpl_GetConeOrientation,
448 IDirectSound3DBufferImpl_GetConeOutsideVolume,
449 IDirectSound3DBufferImpl_GetMaxDistance,
450 IDirectSound3DBufferImpl_GetMinDistance,
451 IDirectSound3DBufferImpl_GetMode,
452 IDirectSound3DBufferImpl_GetPosition,
453 IDirectSound3DBufferImpl_GetVelocity,
454 IDirectSound3DBufferImpl_SetAllParameters,
455 IDirectSound3DBufferImpl_SetConeAngles,
456 IDirectSound3DBufferImpl_SetConeOrientation,
457 IDirectSound3DBufferImpl_SetConeOutsideVolume,
458 IDirectSound3DBufferImpl_SetMaxDistance,
459 IDirectSound3DBufferImpl_SetMinDistance,
460 IDirectSound3DBufferImpl_SetMode,
461 IDirectSound3DBufferImpl_SetPosition,
462 IDirectSound3DBufferImpl_SetVelocity,
466 static int DSOUND_Create3DBuffer(IDirectSoundBufferImpl* dsb)
468 DWORD i, temp, iSize, oSize, offset;
469 LPBYTE bIbuf, bObuf, bTbuf = NULL;
470 LPWORD wIbuf, wObuf, wTbuf = NULL;
472 /* Inside DirectX says it's stupid but allowed */
473 if (dsb->wfx.nChannels == 2) {
474 /* Convert to mono */
475 if (dsb->wfx.wBitsPerSample == 16) {
476 iSize = dsb->buflen / 4;
477 wTbuf = malloc(dsb->buflen / 2);
479 return DSERR_OUTOFMEMORY;
480 for (i = 0; i < iSize; i++)
481 wTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
484 iSize = dsb->buflen / 2;
485 bTbuf = malloc(dsb->buflen / 2);
487 return DSERR_OUTOFMEMORY;
488 for (i = 0; i < iSize; i++)
489 bTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
493 if (dsb->wfx.wBitsPerSample == 16) {
494 iSize = dsb->buflen / 2;
495 wIbuf = (LPWORD) dsb->buffer;
497 bIbuf = (LPBYTE) dsb->buffer;
502 if (primarybuf->wfx.wBitsPerSample == 16) {
503 wObuf = (LPWORD) dsb->ds3db->buffer;
504 oSize = dsb->ds3db->buflen / 2;
506 bObuf = (LPBYTE) dsb->ds3db->buffer;
507 oSize = dsb->ds3db->buflen;
510 offset = primarybuf->wfx.nSamplesPerSec / 100; /* 10ms */
511 if (primarybuf->wfx.wBitsPerSample == 16 && dsb->wfx.wBitsPerSample == 16)
512 for (i = 0; i < iSize; i++) {
515 temp += wIbuf[i - offset] >> 9;
517 temp += wIbuf[i + iSize - offset] >> 9;
519 wObuf[(i * 2) + 1] = temp;
521 else if (primarybuf->wfx.wBitsPerSample == 8 && dsb->wfx.wBitsPerSample == 8)
522 for (i = 0; i < iSize; i++) {
525 temp += bIbuf[i - offset] >> 5;
527 temp += bIbuf[i + iSize - offset] >> 5;
529 bObuf[(i * 2) + 1] = temp;
540 /*******************************************************************************
541 * IDirectSound3DListener
544 /* IUnknown methods */
545 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
546 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
548 ICOM_THIS(IDirectSound3DListenerImpl,iface);
550 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
554 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
556 ICOM_THIS(IDirectSound3DListenerImpl,iface);
561 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
563 ICOM_THIS(IDirectSound3DListenerImpl,iface);
568 /* IDirectSound3DListener methods */
569 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
570 LPDIRECTSOUND3DLISTENER iface,
571 LPDS3DLISTENER lpDS3DL)
577 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
578 LPDIRECTSOUND3DLISTENER iface,
579 LPD3DVALUE lpfDistanceFactor)
585 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
586 LPDIRECTSOUND3DLISTENER iface,
587 LPD3DVALUE lpfDopplerFactor)
593 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
594 LPDIRECTSOUND3DLISTENER iface,
595 LPD3DVECTOR lpvOrientFront,
596 LPD3DVECTOR lpvOrientTop)
602 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
603 LPDIRECTSOUND3DLISTENER iface,
604 LPD3DVECTOR lpvPosition)
610 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
611 LPDIRECTSOUND3DLISTENER iface,
612 LPD3DVALUE lpfRolloffFactor)
618 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
619 LPDIRECTSOUND3DLISTENER iface,
620 LPD3DVECTOR lpvVelocity)
626 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
627 LPDIRECTSOUND3DLISTENER iface,
628 LPCDS3DLISTENER lpcDS3DL,
635 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
636 LPDIRECTSOUND3DLISTENER iface,
637 D3DVALUE fDistanceFactor,
644 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
645 LPDIRECTSOUND3DLISTENER iface,
646 D3DVALUE fDopplerFactor,
653 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
654 LPDIRECTSOUND3DLISTENER iface,
655 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
656 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
663 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
664 LPDIRECTSOUND3DLISTENER iface,
665 D3DVALUE x, D3DVALUE y, D3DVALUE z,
672 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
673 LPDIRECTSOUND3DLISTENER iface,
674 D3DVALUE fRolloffFactor,
681 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
682 LPDIRECTSOUND3DLISTENER iface,
683 D3DVALUE x, D3DVALUE y, D3DVALUE z,
690 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
691 LPDIRECTSOUND3DLISTENER iface)
698 ICOM_VTABLE(IDirectSound3DListener) ds3dlvt =
700 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
701 /* IUnknown methods */
702 IDirectSound3DListenerImpl_QueryInterface,
703 IDirectSound3DListenerImpl_AddRef,
704 IDirectSound3DListenerImpl_Release,
705 /* IDirectSound3DListener methods */
706 IDirectSound3DListenerImpl_GetAllParameter,
707 IDirectSound3DListenerImpl_GetDistanceFactor,
708 IDirectSound3DListenerImpl_GetDopplerFactor,
709 IDirectSound3DListenerImpl_GetOrientation,
710 IDirectSound3DListenerImpl_GetPosition,
711 IDirectSound3DListenerImpl_GetRolloffFactor,
712 IDirectSound3DListenerImpl_GetVelocity,
713 IDirectSound3DListenerImpl_SetAllParameters,
714 IDirectSound3DListenerImpl_SetDistanceFactor,
715 IDirectSound3DListenerImpl_SetDopplerFactor,
716 IDirectSound3DListenerImpl_SetOrientation,
717 IDirectSound3DListenerImpl_SetPosition,
718 IDirectSound3DListenerImpl_SetRolloffFactor,
719 IDirectSound3DListenerImpl_SetVelocity,
720 IDirectSound3DListenerImpl_CommitDeferredSettings,
723 /*******************************************************************************
726 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
727 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
729 ICOM_THIS(IDirectSoundNotifyImpl,iface);
731 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
735 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
736 ICOM_THIS(IDirectSoundNotifyImpl,iface);
737 return ++(This->ref);
740 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
741 ICOM_THIS(IDirectSoundNotifyImpl,iface);
744 IDirectSoundNotify_Release((LPDIRECTSOUNDBUFFER)This->dsb);
745 HeapFree(GetProcessHeap(),0,This);
751 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
752 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
754 ICOM_THIS(IDirectSoundNotifyImpl,iface);
757 if (TRACE_ON(dsound)) {
758 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
759 for (i=0;i<howmuch;i++)
760 TRACE("notify at %ld to 0x%08lx\n",
761 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
763 This->dsb->notifies = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->dsb->notifies,(This->dsb->nrofnotifies+howmuch)*sizeof(DSBPOSITIONNOTIFY));
764 memcpy( This->dsb->notifies+This->dsb->nrofnotifies,
766 howmuch*sizeof(DSBPOSITIONNOTIFY)
768 This->dsb->nrofnotifies+=howmuch;
773 ICOM_VTABLE(IDirectSoundNotify) dsnvt =
775 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
776 IDirectSoundNotifyImpl_QueryInterface,
777 IDirectSoundNotifyImpl_AddRef,
778 IDirectSoundNotifyImpl_Release,
779 IDirectSoundNotifyImpl_SetNotificationPositions,
782 /*******************************************************************************
786 /* This sets this format for the <em>Primary Buffer Only</em> */
787 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
788 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
789 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX wfex
791 ICOM_THIS(IDirectSoundBufferImpl,iface);
792 IDirectSoundBufferImpl** dsb;
795 /* Let's be pedantic! */
796 if ((wfex == NULL) ||
797 (wfex->wFormatTag != WAVE_FORMAT_PCM) ||
798 (wfex->nChannels < 1) || (wfex->nChannels > 2) ||
799 (wfex->nSamplesPerSec < 1) ||
800 (wfex->nBlockAlign < 1) || (wfex->nChannels > 4) ||
801 ((wfex->wBitsPerSample != 8) && (wfex->wBitsPerSample != 16))) {
802 TRACE("failed pedantic check!\n");
803 return DSERR_INVALIDPARAM;
807 EnterCriticalSection(&(This->dsound->lock));
809 if (primarybuf->wfx.nSamplesPerSec != wfex->nSamplesPerSec) {
810 dsb = dsound->buffers;
811 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
813 EnterCriticalSection(&((*dsb)->lock));
815 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
816 wfex->nSamplesPerSec;
818 LeaveCriticalSection(&((*dsb)->lock));
823 memcpy(&(primarybuf->wfx), wfex, sizeof(primarybuf->wfx));
825 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld"
826 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
827 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
828 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
829 wfex->wBitsPerSample, wfex->cbSize);
831 primarybuf->wfx.nAvgBytesPerSec =
832 This->wfx.nSamplesPerSec * This->wfx.nBlockAlign;
835 LeaveCriticalSection(&(This->dsound->lock));
841 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
842 LPDIRECTSOUNDBUFFER iface,LONG vol
844 ICOM_THIS(IDirectSoundBufferImpl,iface);
847 TRACE("(%p,%ld)\n",This,vol);
849 /* I'm not sure if we need this for primary buffer */
850 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
851 return DSERR_CONTROLUNAVAIL;
853 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN))
854 return DSERR_INVALIDPARAM;
856 /* This needs to adjust the soundcard volume when */
857 /* called for the primary buffer */
858 if (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
859 FIXME("Volume control of primary unimplemented.\n");
865 EnterCriticalSection(&(This->lock));
869 temp = (double) (This->volume - (This->pan > 0 ? This->pan : 0));
870 This->lVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
871 temp = (double) (This->volume + (This->pan < 0 ? This->pan : 0));
872 This->rVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
874 LeaveCriticalSection(&(This->lock));
877 TRACE("left = %lx, right = %lx\n", This->lVolAdjust, This->rVolAdjust);
882 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
883 LPDIRECTSOUNDBUFFER iface,LPLONG vol
885 ICOM_THIS(IDirectSoundBufferImpl,iface);
886 TRACE("(%p,%p)\n",This,vol);
889 return DSERR_INVALIDPARAM;
895 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
896 LPDIRECTSOUNDBUFFER iface,DWORD freq
898 ICOM_THIS(IDirectSoundBufferImpl,iface);
899 TRACE("(%p,%ld)\n",This,freq);
901 /* You cannot set the frequency of the primary buffer */
902 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY) ||
903 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
904 return DSERR_CONTROLUNAVAIL;
906 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX))
907 return DSERR_INVALIDPARAM;
910 EnterCriticalSection(&(This->lock));
913 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / primarybuf->wfx.nSamplesPerSec;
914 This->nAvgBytesPerSec = freq * This->wfx.nBlockAlign;
916 LeaveCriticalSection(&(This->lock));
922 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
923 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
925 ICOM_THIS(IDirectSoundBufferImpl,iface);
926 TRACE("(%p,%08lx,%08lx,%08lx)\n",
927 This,reserved1,reserved2,flags
929 This->playflags = flags;
934 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
936 ICOM_THIS(IDirectSoundBufferImpl,iface);
937 TRACE("(%p)\n",This);
940 EnterCriticalSection(&(This->lock));
943 DSOUND_CheckEvent(This, 0);
945 LeaveCriticalSection(&(This->lock));
951 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface) {
952 ICOM_THIS(IDirectSoundBufferImpl,iface);
953 /* TRACE(dsound,"(%p) ref was %ld\n",This, This->ref); */
955 return ++(This->ref);
957 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER iface) {
958 ICOM_THIS(IDirectSoundBufferImpl,iface);
961 /* TRACE(dsound,"(%p) ref was %ld\n",This, This->ref); */
966 EnterCriticalSection(&(This->dsound->lock));
967 for (i=0;i<This->dsound->nrofbuffers;i++)
968 if (This->dsound->buffers[i] == This)
970 if (i < This->dsound->nrofbuffers) {
971 /* Put the last buffer of the list in the (now empty) position */
972 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
973 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER)*This->dsound->nrofbuffers);
974 This->dsound->nrofbuffers--;
975 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
977 LeaveCriticalSection(&(This->dsound->lock));
979 DeleteCriticalSection(&(This->lock));
980 if (This->ds3db && ICOM_VTBL(This->ds3db))
981 IDirectSound3DBuffer_Release((LPDIRECTSOUND3DBUFFER)This->ds3db);
983 /* this is a duplicate buffer */
984 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->parent);
986 /* this is a toplevel buffer */
987 HeapFree(GetProcessHeap(),0,This->buffer);
989 HeapFree(GetProcessHeap(),0,This);
991 if (This == primarybuf)
997 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
998 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
1000 ICOM_THIS(IDirectSoundBufferImpl,iface);
1001 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1002 if (playpos) *playpos = This->playpos;
1003 if (writepos) *writepos = This->writepos;
1004 TRACE("playpos = %ld, writepos = %ld\n", playpos?*playpos:0, writepos?*writepos:0);
1008 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
1009 LPDIRECTSOUNDBUFFER iface,LPDWORD status
1011 ICOM_THIS(IDirectSoundBufferImpl,iface);
1012 TRACE("(%p,%p)\n",This,status);
1015 return DSERR_INVALIDPARAM;
1019 *status |= DSBSTATUS_PLAYING;
1020 if (This->playflags & DSBPLAY_LOOPING)
1021 *status |= DSBSTATUS_LOOPING;
1027 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
1028 LPDIRECTSOUNDBUFFER iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
1030 ICOM_THIS(IDirectSoundBufferImpl,iface);
1031 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1033 if (wfsize>sizeof(This->wfx))
1034 wfsize = sizeof(This->wfx);
1035 if (lpwf) { /* NULL is valid */
1036 memcpy(lpwf,&(This->wfx),wfsize);
1038 *wfwritten = wfsize;
1041 *wfwritten = sizeof(This->wfx);
1043 return DSERR_INVALIDPARAM;
1048 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
1049 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
1051 ICOM_THIS(IDirectSoundBufferImpl,iface);
1053 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1063 if (flags & DSBLOCK_FROMWRITECURSOR)
1064 writecursor += This->writepos;
1065 if (flags & DSBLOCK_ENTIREBUFFER)
1066 writebytes = This->buflen;
1067 if (writebytes > This->buflen)
1068 writebytes = This->buflen;
1070 assert(audiobytes1!=audiobytes2);
1071 assert(lplpaudioptr1!=lplpaudioptr2);
1072 if (writecursor+writebytes <= This->buflen) {
1073 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1074 *audiobytes1 = writebytes;
1076 *(LPBYTE*)lplpaudioptr2 = NULL;
1079 TRACE("->%ld.0\n",writebytes);
1081 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
1082 *audiobytes1 = This->buflen-writecursor;
1084 *(LPBYTE*)lplpaudioptr2 = This->buffer;
1086 *audiobytes2 = writebytes-(This->buflen-writecursor);
1087 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
1089 /* No. See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 21 */
1090 /* This->writepos=(writecursor+writebytes)%This->buflen; */
1094 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
1095 LPDIRECTSOUNDBUFFER iface,DWORD newpos
1097 ICOM_THIS(IDirectSoundBufferImpl,iface);
1098 TRACE("(%p,%ld)\n",This,newpos);
1101 EnterCriticalSection(&(This->lock));
1103 This->playpos = newpos;
1105 LeaveCriticalSection(&(This->lock));
1111 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
1112 LPDIRECTSOUNDBUFFER iface,LONG pan
1114 ICOM_THIS(IDirectSoundBufferImpl,iface);
1117 TRACE("(%p,%ld)\n",This,pan);
1119 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT))
1120 return DSERR_INVALIDPARAM;
1122 /* You cannot set the pan of the primary buffer */
1123 /* and you cannot use both pan and 3D controls */
1124 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
1125 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) ||
1126 (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
1127 return DSERR_CONTROLUNAVAIL;
1130 EnterCriticalSection(&(This->lock));
1134 temp = (double) (This->volume - (This->pan > 0 ? This->pan : 0));
1135 This->lVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
1136 temp = (double) (This->volume + (This->pan < 0 ? This->pan : 0));
1137 This->rVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
1139 LeaveCriticalSection(&(This->lock));
1145 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
1146 LPDIRECTSOUNDBUFFER iface,LPLONG pan
1148 ICOM_THIS(IDirectSoundBufferImpl,iface);
1149 TRACE("(%p,%p)\n",This,pan);
1152 return DSERR_INVALIDPARAM;
1159 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
1160 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1162 ICOM_THIS(IDirectSoundBufferImpl,iface);
1163 TRACE("(%p,%p,%ld,%p,%ld):stub\n", This,p1,x1,p2,x2);
1165 /* There is really nothing to do here. Should someone */
1166 /* choose to implement static buffers in hardware (by */
1167 /* using a wave table synth, for example) this is where */
1168 /* you'd want to do the loading. For software buffers, */
1169 /* which is what we currently use, we need do nothing. */
1172 /* It's also the place to pre-process 3D buffers... */
1174 /* This is highly experimental and liable to break things */
1175 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D)
1176 DSOUND_Create3DBuffer(This);
1182 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
1183 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1185 ICOM_THIS(IDirectSoundBufferImpl,iface);
1186 TRACE("(%p,%p)\n",This,freq);
1189 return DSERR_INVALIDPARAM;
1196 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
1197 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPDSBUFFERDESC dbsd
1199 ICOM_THIS(IDirectSoundBufferImpl,iface);
1200 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
1201 DPRINTF("Re-Init!!!\n");
1202 return DSERR_ALREADYINITIALIZED;
1205 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
1206 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1208 ICOM_THIS(IDirectSoundBufferImpl,iface);
1209 TRACE("(%p)->(%p)\n",This,caps);
1212 return DSERR_INVALIDPARAM;
1214 /* I think we should check this value, not set it. See */
1215 /* Inside DirectX, p215. That should apply here, too. */
1216 caps->dwSize = sizeof(*caps);
1218 caps->dwFlags = This->dsbd.dwFlags | DSBCAPS_LOCSOFTWARE;
1219 caps->dwBufferBytes = This->dsbd.dwBufferBytes;
1220 /* This value represents the speed of the "unlock" command.
1221 As unlock is quite fast (it does not do anything), I put
1222 4096 ko/s = 4 Mo / s */
1223 caps->dwUnlockTransferRate = 4096;
1224 caps->dwPlayCpuOverhead = 0;
1229 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
1230 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1232 ICOM_THIS(IDirectSoundBufferImpl,iface);
1234 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1236 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1237 IDirectSoundNotifyImpl *dsn;
1239 dsn = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsn));
1242 IDirectSoundBuffer_AddRef(iface);
1243 ICOM_VTBL(dsn) = &dsnvt;
1244 *ppobj = (LPVOID)dsn;
1248 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1249 *ppobj = This->ds3db;
1257 static ICOM_VTABLE(IDirectSoundBuffer) dsbvt =
1259 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1260 IDirectSoundBufferImpl_QueryInterface,
1261 IDirectSoundBufferImpl_AddRef,
1262 IDirectSoundBufferImpl_Release,
1263 IDirectSoundBufferImpl_GetCaps,
1264 IDirectSoundBufferImpl_GetCurrentPosition,
1265 IDirectSoundBufferImpl_GetFormat,
1266 IDirectSoundBufferImpl_GetVolume,
1267 IDirectSoundBufferImpl_GetPan,
1268 IDirectSoundBufferImpl_GetFrequency,
1269 IDirectSoundBufferImpl_GetStatus,
1270 IDirectSoundBufferImpl_Initialize,
1271 IDirectSoundBufferImpl_Lock,
1272 IDirectSoundBufferImpl_Play,
1273 IDirectSoundBufferImpl_SetCurrentPosition,
1274 IDirectSoundBufferImpl_SetFormat,
1275 IDirectSoundBufferImpl_SetVolume,
1276 IDirectSoundBufferImpl_SetPan,
1277 IDirectSoundBufferImpl_SetFrequency,
1278 IDirectSoundBufferImpl_Stop,
1279 IDirectSoundBufferImpl_Unlock
1282 /*******************************************************************************
1286 static HRESULT WINAPI IDirectSoundImpl_SetCooperativeLevel(
1287 LPDIRECTSOUND iface,HWND hwnd,DWORD level
1289 ICOM_THIS(IDirectSoundImpl,iface);
1290 FIXME("(%p,%08lx,%ld):stub\n",This,(DWORD)hwnd,level);
1294 static HRESULT WINAPI IDirectSoundImpl_CreateSoundBuffer(
1295 LPDIRECTSOUND iface,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER ppdsb,LPUNKNOWN lpunk
1297 ICOM_THIS(IDirectSoundImpl,iface);
1298 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
1299 LPWAVEFORMATEX wfex;
1301 TRACE("(%p,%p,%p,%p)\n",This,dsbd,ippdsb,lpunk);
1303 if ((This == NULL) || (dsbd == NULL) || (ippdsb == NULL))
1304 return DSERR_INVALIDPARAM;
1306 if (TRACE_ON(dsound)) {
1307 TRACE("(size=%ld)\n",dsbd->dwSize);
1308 TRACE("(flags=0x%08lx\n",dsbd->dwFlags);
1309 _dump_DSBCAPS(dsbd->dwFlags);
1310 TRACE("(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
1311 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
1314 wfex = dsbd->lpwfxFormat;
1317 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld"
1318 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1319 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1320 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1321 wfex->wBitsPerSample, wfex->cbSize);
1323 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1325 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)primarybuf);
1326 *ippdsb = primarybuf;
1327 primarybuf->dsbd.dwFlags = dsbd->dwFlags;
1329 } /* Else create primarybuf */
1332 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
1333 if (*ippdsb == NULL)
1334 return DSERR_OUTOFMEMORY;
1337 TRACE("Created buffer at %p\n", *ippdsb);
1339 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1340 (*ippdsb)->buflen = dsound->wfx.nAvgBytesPerSec;
1341 (*ippdsb)->freq = dsound->wfx.nSamplesPerSec;
1343 (*ippdsb)->buflen = dsbd->dwBufferBytes;
1344 (*ippdsb)->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1346 (*ippdsb)->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,(*ippdsb)->buflen);
1347 if ((*ippdsb)->buffer == NULL) {
1348 HeapFree(GetProcessHeap(),0,(*ippdsb));
1350 return DSERR_OUTOFMEMORY;
1352 /* It's not necessary to initialize values to zero since */
1353 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1354 (*ippdsb)->playpos = 0;
1355 (*ippdsb)->writepos = 0;
1356 (*ippdsb)->parent = NULL;
1357 ICOM_VTBL(*ippdsb) = &dsbvt;
1358 (*ippdsb)->dsound = This;
1359 (*ippdsb)->playing = 0;
1360 (*ippdsb)->lVolAdjust = (1 << 15);
1361 (*ippdsb)->rVolAdjust = (1 << 15);
1363 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1364 (*ippdsb)->freqAdjust = ((*ippdsb)->freq << DSOUND_FREQSHIFT) /
1365 primarybuf->wfx.nSamplesPerSec;
1366 (*ippdsb)->nAvgBytesPerSec = (*ippdsb)->freq *
1367 dsbd->lpwfxFormat->nBlockAlign;
1370 memcpy(&((*ippdsb)->dsbd),dsbd,sizeof(*dsbd));
1372 EnterCriticalSection(&(This->lock));
1373 /* register buffer */
1374 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1375 This->buffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl*)*(This->nrofbuffers+1));
1376 This->buffers[This->nrofbuffers] = *ippdsb;
1377 This->nrofbuffers++;
1379 LeaveCriticalSection(&(This->lock));
1381 IDirectSound_AddRef(iface);
1383 if (dsbd->lpwfxFormat)
1384 memcpy(&((*ippdsb)->wfx), dsbd->lpwfxFormat, sizeof((*ippdsb)->wfx));
1386 InitializeCriticalSection(&((*ippdsb)->lock));
1389 if (dsbd->dwFlags & DSBCAPS_CTRL3D) {
1390 IDirectSound3DBufferImpl *ds3db;
1392 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),
1395 ds3db->dsb = (*ippdsb);
1396 ICOM_VTBL(ds3db) = &ds3dbvt;
1397 (*ippdsb)->ds3db = ds3db;
1398 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1399 ds3db->ds3db.vPosition.x.x = 0.0;
1400 ds3db->ds3db.vPosition.y.y = 0.0;
1401 ds3db->ds3db.vPosition.z.z = 0.0;
1402 ds3db->ds3db.vVelocity.x.x = 0.0;
1403 ds3db->ds3db.vVelocity.y.y = 0.0;
1404 ds3db->ds3db.vVelocity.z.z = 0.0;
1405 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1406 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1407 ds3db->ds3db.vConeOrientation.x.x = 0.0;
1408 ds3db->ds3db.vConeOrientation.y.y = 0.0;
1409 ds3db->ds3db.vConeOrientation.z.z = 0.0;
1410 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1411 ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1412 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1413 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
1414 ds3db->buflen = ((*ippdsb)->buflen * primarybuf->wfx.nBlockAlign) /
1415 (*ippdsb)->wfx.nBlockAlign;
1416 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
1417 if (ds3db->buffer == NULL) {
1419 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
1426 static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer(
1427 LPDIRECTSOUND iface,LPDIRECTSOUNDBUFFER pdsb,LPLPDIRECTSOUNDBUFFER ppdsb
1429 ICOM_THIS(IDirectSoundImpl,iface);
1430 IDirectSoundBufferImpl* ipdsb=(IDirectSoundBufferImpl*)pdsb;
1431 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
1432 TRACE("(%p,%p,%p)\n",This,ipdsb,ippdsb);
1434 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
1436 IDirectSoundBuffer_AddRef(pdsb);
1437 memcpy(*ippdsb, ipdsb, sizeof(IDirectSoundBufferImpl));
1439 (*ippdsb)->playpos = 0;
1440 (*ippdsb)->writepos = 0;
1441 (*ippdsb)->dsound = This;
1442 (*ippdsb)->parent = ipdsb;
1443 memcpy(&((*ippdsb)->wfx), &(ipdsb->wfx), sizeof((*ippdsb)->wfx));
1444 /* register buffer */
1445 EnterCriticalSection(&(This->lock));
1446 This->buffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
1447 This->buffers[This->nrofbuffers] = *ippdsb;
1448 This->nrofbuffers++;
1449 IDirectSound_AddRef(iface);
1450 LeaveCriticalSection(&(This->lock));
1455 static HRESULT WINAPI IDirectSoundImpl_GetCaps(LPDIRECTSOUND iface,LPDSCAPS caps) {
1456 ICOM_THIS(IDirectSoundImpl,iface);
1457 TRACE("(%p,%p)\n",This,caps);
1458 TRACE("(flags=0x%08lx)\n",caps->dwFlags);
1461 return DSERR_INVALIDPARAM;
1463 /* We should check this value, not set it. See Inside DirectX, p215. */
1464 caps->dwSize = sizeof(*caps);
1467 DSCAPS_PRIMARYSTEREO |
1468 DSCAPS_PRIMARY16BIT |
1469 DSCAPS_SECONDARYSTEREO |
1470 DSCAPS_SECONDARY16BIT |
1471 DSCAPS_CONTINUOUSRATE;
1472 /* FIXME: query OSS */
1473 caps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
1474 caps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
1476 caps->dwPrimaryBuffers = 1;
1478 caps->dwMaxHwMixingAllBuffers = 0;
1479 caps->dwMaxHwMixingStaticBuffers = 0;
1480 caps->dwMaxHwMixingStreamingBuffers = 0;
1482 caps->dwFreeHwMixingAllBuffers = 0;
1483 caps->dwFreeHwMixingStaticBuffers = 0;
1484 caps->dwFreeHwMixingStreamingBuffers = 0;
1486 caps->dwMaxHw3DAllBuffers = 0;
1487 caps->dwMaxHw3DStaticBuffers = 0;
1488 caps->dwMaxHw3DStreamingBuffers = 0;
1490 caps->dwFreeHw3DAllBuffers = 0;
1491 caps->dwFreeHw3DStaticBuffers = 0;
1492 caps->dwFreeHw3DStreamingBuffers = 0;
1494 caps->dwTotalHwMemBytes = 0;
1496 caps->dwFreeHwMemBytes = 0;
1498 caps->dwMaxContigFreeHwMemBytes = 0;
1500 caps->dwUnlockTransferRateHwBuffers = 4096; /* But we have none... */
1502 caps->dwPlayCpuOverheadSwBuffers = 1; /* 1% */
1507 static ULONG WINAPI IDirectSoundImpl_AddRef(LPDIRECTSOUND iface) {
1508 ICOM_THIS(IDirectSoundImpl,iface);
1509 return ++(This->ref);
1512 static ULONG WINAPI IDirectSoundImpl_Release(LPDIRECTSOUND iface) {
1513 ICOM_THIS(IDirectSoundImpl,iface);
1514 TRACE("(%p), ref was %ld\n",This,This->ref);
1515 if (!--(This->ref)) {
1516 DSOUND_CloseAudio();
1517 while(IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)primarybuf)); /* Deallocate */
1518 FIXME("need to release all buffers!\n");
1519 HeapFree(GetProcessHeap(),0,This);
1526 static HRESULT WINAPI IDirectSoundImpl_SetSpeakerConfig(
1527 LPDIRECTSOUND iface,DWORD config
1529 ICOM_THIS(IDirectSoundImpl,iface);
1530 FIXME("(%p,0x%08lx):stub\n",This,config);
1534 static HRESULT WINAPI IDirectSoundImpl_QueryInterface(
1535 LPDIRECTSOUND iface,REFIID riid,LPVOID *ppobj
1537 ICOM_THIS(IDirectSoundImpl,iface);
1539 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1541 if (This->listener) {
1542 *ppobj = This->listener;
1545 This->listener = (IDirectSound3DListenerImpl*)HeapAlloc(
1546 GetProcessHeap(), 0, sizeof(*(This->listener)));
1547 This->listener->ref = 1;
1548 ICOM_VTBL(This->listener) = &ds3dlvt;
1549 IDirectSound_AddRef(iface);
1550 This->listener->ds3dl.dwSize = sizeof(DS3DLISTENER);
1551 This->listener->ds3dl.vPosition.x.x = 0.0;
1552 This->listener->ds3dl.vPosition.y.y = 0.0;
1553 This->listener->ds3dl.vPosition.z.z = 0.0;
1554 This->listener->ds3dl.vVelocity.x.x = 0.0;
1555 This->listener->ds3dl.vVelocity.y.y = 0.0;
1556 This->listener->ds3dl.vVelocity.z.z = 0.0;
1557 This->listener->ds3dl.vOrientFront.x.x = 0.0;
1558 This->listener->ds3dl.vOrientFront.y.y = 0.0;
1559 This->listener->ds3dl.vOrientFront.z.z = 1.0;
1560 This->listener->ds3dl.vOrientTop.x.x = 0.0;
1561 This->listener->ds3dl.vOrientTop.y.y = 1.0;
1562 This->listener->ds3dl.vOrientTop.z.z = 0.0;
1563 This->listener->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1564 This->listener->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1565 This->listener->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1566 *ppobj = (LPVOID)This->listener;
1570 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1574 static HRESULT WINAPI IDirectSoundImpl_Compact(
1575 LPDIRECTSOUND iface)
1577 ICOM_THIS(IDirectSoundImpl,iface);
1578 TRACE("(%p)\n", This);
1582 static HRESULT WINAPI IDirectSoundImpl_GetSpeakerConfig(
1583 LPDIRECTSOUND iface,
1584 LPDWORD lpdwSpeakerConfig)
1586 ICOM_THIS(IDirectSoundImpl,iface);
1587 TRACE("(%p, %p)\n", This, lpdwSpeakerConfig);
1588 *lpdwSpeakerConfig = DSSPEAKER_STEREO | (DSSPEAKER_GEOMETRY_NARROW << 16);
1592 static HRESULT WINAPI IDirectSoundImpl_Initialize(
1593 LPDIRECTSOUND iface,
1596 ICOM_THIS(IDirectSoundImpl,iface);
1597 TRACE("(%p, %p)\n", This, lpGuid);
1601 static ICOM_VTABLE(IDirectSound) dsvt =
1603 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1604 IDirectSoundImpl_QueryInterface,
1605 IDirectSoundImpl_AddRef,
1606 IDirectSoundImpl_Release,
1607 IDirectSoundImpl_CreateSoundBuffer,
1608 IDirectSoundImpl_GetCaps,
1609 IDirectSoundImpl_DuplicateSoundBuffer,
1610 IDirectSoundImpl_SetCooperativeLevel,
1611 IDirectSoundImpl_Compact,
1612 IDirectSoundImpl_GetSpeakerConfig,
1613 IDirectSoundImpl_SetSpeakerConfig,
1614 IDirectSoundImpl_Initialize
1618 /* See http://www.opensound.com/pguide/audio.html for more details */
1621 DSOUND_setformat(LPWAVEFORMATEX wfex) {
1622 int xx,channels,speed,format,nformat;
1625 TRACE("(%p) deferred\n", wfex);
1628 switch (wfex->wFormatTag) {
1630 WARN("unknown WAVE_FORMAT tag %d\n",wfex->wFormatTag);
1631 return DSERR_BADFORMAT;
1632 case WAVE_FORMAT_PCM:
1635 if (wfex->wBitsPerSample==8)
1638 format = AFMT_S16_LE;
1640 if (-1==ioctl(audiofd,SNDCTL_DSP_GETFMTS,&xx)) {
1641 perror("ioctl SNDCTL_DSP_GETFMTS");
1644 if ((xx&format)!=format) {/* format unsupported */
1645 FIXME("SNDCTL_DSP_GETFMTS: format not supported\n");
1649 if (-1==ioctl(audiofd,SNDCTL_DSP_SETFMT,&nformat)) {
1650 perror("ioctl SNDCTL_DSP_SETFMT");
1653 if (nformat!=format) {/* didn't work */
1654 FIXME("SNDCTL_DSP_GETFMTS: format not set\n");
1658 channels = wfex->nChannels-1;
1659 if (-1==ioctl(audiofd,SNDCTL_DSP_STEREO,&channels)) {
1660 perror("ioctl SNDCTL_DSP_STEREO");
1663 speed = wfex->nSamplesPerSec;
1664 if (-1==ioctl(audiofd,SNDCTL_DSP_SPEED,&speed)) {
1665 perror("ioctl SNDCTL_DSP_SPEED");
1668 TRACE("(freq=%ld,channels=%d,bits=%d)\n",
1669 wfex->nSamplesPerSec,wfex->nChannels,wfex->wBitsPerSample
1674 static void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
1678 LPDSBPOSITIONNOTIFY event;
1680 if (dsb->nrofnotifies == 0)
1683 TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
1684 dsb, dsb->buflen, dsb->playpos, len);
1685 for (i = 0; i < dsb->nrofnotifies ; i++) {
1686 event = dsb->notifies + i;
1687 offset = event->dwOffset;
1688 TRACE("checking %d, position %ld, event = %d\n",
1689 i, offset, event->hEventNotify);
1690 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
1691 /* OK. [Inside DirectX, p274] */
1693 /* This also means we can't sort the entries by offset, */
1694 /* because DSBPN_OFFSETSTOP == -1 */
1695 if (offset == DSBPN_OFFSETSTOP) {
1696 if (dsb->playing == 0) {
1697 SetEvent(event->hEventNotify);
1698 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
1703 if ((dsb->playpos + len) >= dsb->buflen) {
1704 if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
1705 (offset >= dsb->playpos)) {
1706 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
1707 SetEvent(event->hEventNotify);
1710 if ((offset >= dsb->playpos) && (offset < (dsb->playpos + len))) {
1711 TRACE("signalled event %d (%d)\n", event->hEventNotify, i);
1712 SetEvent(event->hEventNotify);
1718 /* WAV format info can be found at: */
1720 /* http://www.cwi.nl/ftp/audio/AudioFormats.part2 */
1721 /* ftp://ftp.cwi.nl/pub/audio/RIFF-format */
1723 /* Import points to remember: */
1725 /* 8-bit WAV is unsigned */
1726 /* 16-bit WAV is signed */
1728 static inline INT16 cvtU8toS16(BYTE byte)
1730 INT16 s = (byte - 128) << 8;
1735 static inline BYTE cvtS16toU8(INT16 word)
1737 BYTE b = (word + 32768) >> 8;
1743 /* We should be able to optimize these two inline functions */
1744 /* so that we aren't doing 8->16->8 conversions when it is */
1745 /* not necessary. But this is still a WIP. Optimize later. */
1746 static inline void get_fields(const IDirectSoundBufferImpl *dsb, BYTE *buf, INT *fl, INT *fr)
1748 INT16 *bufs = (INT16 *) buf;
1750 /* TRACE(dsound, "(%p)", buf); */
1751 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 2) {
1752 *fl = cvtU8toS16(*buf);
1753 *fr = cvtU8toS16(*(buf + 1));
1757 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 2) {
1763 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 1) {
1764 *fl = cvtU8toS16(*buf);
1769 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 1) {
1775 FIXME("get_fields found an unsupported configuration\n");
1779 static inline void set_fields(BYTE *buf, INT fl, INT fr)
1781 INT16 *bufs = (INT16 *) buf;
1783 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 2)) {
1784 *buf = cvtS16toU8(fl);
1785 *(buf + 1) = cvtS16toU8(fr);
1789 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 2)) {
1795 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 1)) {
1796 *buf = cvtS16toU8((fl + fr) >> 1);
1800 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 1)) {
1801 *bufs = (fl + fr) >> 1;
1804 FIXME("set_fields found an unsupported configuration\n");
1808 /* Now with PerfectPitch (tm) technology */
1809 static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
1811 INT i, size, ipos, ilen, fieldL, fieldR;
1813 INT iAdvance = dsb->wfx.nBlockAlign;
1814 INT oAdvance = primarybuf->wfx.nBlockAlign;
1816 ibp = dsb->buffer + dsb->playpos;
1819 TRACE("(%p, %p, %p), playpos=%8.8lx\n", dsb, ibp, obp, dsb->playpos);
1820 /* Check for the best case */
1821 if ((dsb->freq == primarybuf->wfx.nSamplesPerSec) &&
1822 (dsb->wfx.wBitsPerSample == primarybuf->wfx.wBitsPerSample) &&
1823 (dsb->wfx.nChannels == primarybuf->wfx.nChannels)) {
1824 TRACE("(%p) Best case\n", dsb);
1825 if ((ibp + len) < (BYTE *)(dsb->buffer + dsb->buflen))
1826 memcpy(obp, ibp, len);
1828 memcpy(obp, ibp, dsb->buflen - dsb->playpos);
1829 memcpy(obp + (dsb->buflen - dsb->playpos),
1831 len - (dsb->buflen - dsb->playpos));
1836 /* Check for same sample rate */
1837 if (dsb->freq == primarybuf->wfx.nSamplesPerSec) {
1838 TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb,
1839 dsb->freq, primarybuf->wfx.nSamplesPerSec);
1841 for (i = 0; i < len; i += oAdvance) {
1842 get_fields(dsb, ibp, &fieldL, &fieldR);
1845 set_fields(obp, fieldL, fieldR);
1847 if (ibp >= (BYTE *)(dsb->buffer + dsb->buflen))
1848 ibp = dsb->buffer; /* wrap */
1853 /* Mix in different sample rates */
1855 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
1856 /* Patent Pending :-] */
1858 TRACE("(%p) Adjusting frequency: %ld -> %ld\n",
1859 dsb, dsb->freq, primarybuf->wfx.nSamplesPerSec);
1861 size = len / oAdvance;
1862 ilen = ((size * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance;
1863 for (i = 0; i < size; i++) {
1865 ipos = (((i * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance) + dsb->playpos;
1867 if (ipos >= dsb->buflen)
1868 ipos %= dsb->buflen; /* wrap */
1870 get_fields(dsb, (dsb->buffer + ipos), &fieldL, &fieldR);
1871 set_fields(obp, fieldL, fieldR);
1877 static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
1879 INT i, inc = primarybuf->wfx.wBitsPerSample >> 3;
1881 INT16 *bps = (INT16 *) buf;
1883 TRACE("(%p) left = %lx, right = %lx\n", dsb,
1884 dsb->lVolAdjust, dsb->rVolAdjust);
1885 if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->pan == 0)) &&
1886 (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volume == 0)) &&
1887 !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
1888 return; /* Nothing to do */
1890 /* If we end up with some bozo coder using panning or 3D sound */
1891 /* with a mono primary buffer, it could sound very weird using */
1892 /* this method. Oh well, tough patooties. */
1894 for (i = 0; i < len; i += inc) {
1900 /* 8-bit WAV is unsigned, but we need to operate */
1901 /* on signed data for this to work properly */
1903 val = ((val * (i & inc ? dsb->rVolAdjust : dsb->lVolAdjust)) >> 15);
1908 /* 16-bit WAV is signed -- much better */
1910 val = ((val * ((i & inc) ? dsb->rVolAdjust : dsb->lVolAdjust)) >> 15);
1916 FIXME("MixerVol had a nasty error\n");
1922 static void DSOUND_Mixer3D(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
1925 DWORD buflen, playpos;
1927 buflen = dsb->ds3db->buflen;
1928 playpos = (dsb->playpos * primarybuf->wfx.nBlockAlign) / dsb->wfx.nBlockAlign;
1929 ibp = dsb->ds3db->buffer + playpos;
1932 if (playpos > buflen) {
1933 FIXME("Major breakage");
1937 if (len <= (playpos + buflen))
1938 memcpy(obp, ibp, len);
1940 memcpy(obp, ibp, buflen - playpos);
1941 memcpy(obp + (buflen - playpos),
1943 len - (buflen - playpos));
1949 static void *tmp_buffer;
1950 static size_t tmp_buffer_len = 0;
1952 static void *DSOUND_tmpbuffer(size_t len)
1954 if (len>tmp_buffer_len) {
1955 void *new_buffer = realloc(tmp_buffer, len);
1957 tmp_buffer = new_buffer;
1958 tmp_buffer_len = len;
1965 static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb)
1967 INT i, len, ilen, temp, field;
1968 INT advance = primarybuf->wfx.wBitsPerSample >> 3;
1969 BYTE *buf, *ibuf, *obuf;
1970 INT16 *ibufs, *obufs;
1972 len = DSOUND_FRAGLEN; /* The most we will use */
1973 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
1974 temp = MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->buflen,
1975 dsb->nAvgBytesPerSec) -
1976 MulDiv(primarybuf->wfx.nAvgBytesPerSec, dsb->playpos,
1977 dsb->nAvgBytesPerSec);
1978 len = (len > temp) ? temp : len;
1980 len &= ~3; /* 4 byte alignment */
1983 /* This should only happen if we aren't looping and temp < 4 */
1985 /* We skip the remainder, so check for possible events */
1986 DSOUND_CheckEvent(dsb, dsb->buflen - dsb->playpos);
1991 /* Check for DSBPN_OFFSETSTOP */
1992 DSOUND_CheckEvent(dsb, 0);
1996 /* Been seeing segfaults in malloc() for some reason... */
1997 TRACE("allocating buffer (size = %d)\n", len);
1998 if ((buf = ibuf = (BYTE *) DSOUND_tmpbuffer(len)) == NULL)
2001 TRACE("MixInBuffer (%p) len = %d\n", dsb, len);
2003 ilen = DSOUND_MixerNorm(dsb, ibuf, len);
2004 if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
2005 (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
2006 DSOUND_MixerVol(dsb, ibuf, len);
2008 obuf = primarybuf->buffer + primarybuf->playpos;
2009 for (i = 0; i < len; i += advance) {
2010 obufs = (INT16 *) obuf;
2011 ibufs = (INT16 *) ibuf;
2012 if (primarybuf->wfx.wBitsPerSample == 8) {
2013 /* 8-bit WAV is unsigned */
2014 field = (*ibuf - 128);
2015 field += (*obuf - 128);
2016 field = field > 127 ? 127 : field;
2017 field = field < -128 ? -128 : field;
2018 *obuf = field + 128;
2020 /* 16-bit WAV is signed */
2023 field = field > 32767 ? 32767 : field;
2024 field = field < -32768 ? -32768 : field;
2029 if (obuf >= (BYTE *)(primarybuf->buffer + primarybuf->buflen))
2030 obuf = primarybuf->buffer;
2034 if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY)
2035 DSOUND_CheckEvent(dsb, ilen);
2037 dsb->playpos += ilen;
2038 dsb->writepos = dsb->playpos + ilen;
2040 if (dsb->playpos >= dsb->buflen) {
2041 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
2045 DSOUND_CheckEvent(dsb, 0); /* For DSBPN_OFFSETSTOP */
2047 dsb->playpos %= dsb->buflen; /* wrap */
2050 if (dsb->writepos >= dsb->buflen)
2051 dsb->writepos %= dsb->buflen;
2056 static DWORD WINAPI DSOUND_MixPrimary(void)
2058 INT i, len, maxlen = 0;
2059 IDirectSoundBufferImpl *dsb;
2061 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
2062 dsb = dsound->buffers[i];
2064 if (!dsb || !(ICOM_VTBL(dsb)))
2066 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
2067 if (dsb->buflen && dsb->playing) {
2068 EnterCriticalSection(&(dsb->lock));
2069 len = DSOUND_MixInBuffer(dsb);
2070 maxlen = len > maxlen ? len : maxlen;
2071 LeaveCriticalSection(&(dsb->lock));
2073 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)dsb);
2079 static int DSOUND_OpenAudio(void)
2083 if (primarybuf == NULL)
2084 return DSERR_OUTOFMEMORY;
2086 while (audiofd >= 0)
2089 /* we will most likely not get one, avoid excessive opens ... */
2090 if (audiofd == -ENODEV)
2092 audiofd = open("/dev/audio",O_WRONLY);
2094 /* Don't worry if sound is busy at the moment */
2095 if ((errno != EBUSY) && (errno != ENODEV))
2096 perror("open /dev/audio");
2101 /* We should probably do something here if SETFRAGMENT fails... */
2102 audioFragment=0x0002000c;
2103 if (-1==ioctl(audiofd,SNDCTL_DSP_SETFRAGMENT,&audioFragment))
2104 perror("ioctl SETFRAGMENT");
2107 DSOUND_setformat(&(primarybuf->wfx));
2112 static void DSOUND_CloseAudio(void)
2116 neutral = primarybuf->wfx.wBitsPerSample == 8 ? 128 : 0;
2117 audioOK = 0; /* race condition */
2119 /* It's possible we've been called with audio closed */
2120 /* from SetFormat()... this is just to force a call */
2121 /* to OpenAudio() to reset the hardware properly */
2124 primarybuf->playpos = 0;
2125 primarybuf->writepos = DSOUND_FRAGLEN;
2126 memset(primarybuf->buffer, neutral, primarybuf->buflen);
2128 TRACE("Audio stopped\n");
2131 static int DSOUND_WriteAudio(char *buf, int len)
2133 int result, left = 0;
2135 while (left < len) {
2136 result = write(audiofd, buf + left, len - left);
2148 static void DSOUND_OutputPrimary(int len)
2150 int neutral, flen1, flen2;
2151 char *frag1, *frag2;
2153 /* This is a bad place for this. We need to clear the */
2154 /* buffer with a neutral value, for unsigned 8-bit WAVE */
2155 /* that's 128, for signed 16-bit it's 0 */
2156 neutral = primarybuf->wfx.wBitsPerSample == 8 ? 128 : 0;
2159 EnterCriticalSection(&(primarybuf->lock));
2162 if ((audioOK == 1) || (DSOUND_OpenAudio() == 0)) {
2163 if (primarybuf->playpos + len >= primarybuf->buflen) {
2164 frag1 = primarybuf->buffer + primarybuf->playpos;
2165 flen1 = primarybuf->buflen - primarybuf->playpos;
2166 frag2 = primarybuf->buffer;
2167 flen2 = len - (primarybuf->buflen - primarybuf->playpos);
2168 if (DSOUND_WriteAudio(frag1, flen1) != 0) {
2169 perror("DSOUND_WriteAudio");
2170 LeaveCriticalSection(&(primarybuf->lock));
2173 memset(frag1, neutral, flen1);
2174 if (DSOUND_WriteAudio(frag2, flen2) != 0) {
2175 perror("DSOUND_WriteAudio");
2176 LeaveCriticalSection(&(primarybuf->lock));
2179 memset(frag2, neutral, flen2);
2181 frag1 = primarybuf->buffer + primarybuf->playpos;
2183 if (DSOUND_WriteAudio(frag1, flen1) != 0) {
2184 perror("DSOUND_WriteAudio");
2185 LeaveCriticalSection(&(primarybuf->lock));
2188 memset(frag1, neutral, flen1);
2191 /* Can't play audio at the moment -- we need to sleep */
2192 /* to make up for the time we'd be blocked in write() */
2196 primarybuf->playpos += len;
2197 if (primarybuf->playpos >= primarybuf->buflen)
2198 primarybuf->playpos %= primarybuf->buflen;
2199 primarybuf->writepos = primarybuf->playpos + DSOUND_FRAGLEN;
2200 if (primarybuf->writepos >= primarybuf->buflen)
2201 primarybuf->writepos %= primarybuf->buflen;
2203 LeaveCriticalSection(&(primarybuf->lock));
2207 static DWORD WINAPI DSOUND_thread(LPVOID arg)
2211 TRACE("dsound is at pid %d\n",getpid());
2214 WARN("DSOUND thread giving up.\n");
2218 /* EP: since the thread creating this thread can
2219 * die before the end of the DSOUND one, this
2221 * What shall be tested is whether the DSOUND thread
2222 * is the last one in the process
2225 WARN("DSOUND father died? Giving up.\n");
2229 /* RACE: dsound could be deleted */
2230 EnterCriticalSection(&(dsound->lock));
2231 if (primarybuf == NULL) {
2232 /* Should never happen */
2233 WARN("Lost the primary buffer!\n");
2234 IDirectSound_Release((LPDIRECTSOUND)dsound);
2238 EnterCriticalSection(&(primarybuf->lock));
2240 len = DSOUND_MixPrimary();
2242 LeaveCriticalSection(&(primarybuf->lock));
2243 LeaveCriticalSection(&(dsound->lock));
2245 if (primarybuf->playing)
2246 len = DSOUND_FRAGLEN > len ? DSOUND_FRAGLEN : len;
2248 /* This does all the work */
2249 DSOUND_OutputPrimary(len);
2251 /* no buffers playing -- close and wait */
2253 DSOUND_CloseAudio();
2260 #endif /* HAVE_OSS */
2262 HRESULT WINAPI DirectSoundCreate(REFGUID lpGUID,LPDIRECTSOUND *ppDS,IUnknown *pUnkOuter )
2264 IDirectSoundImpl** ippDS=(IDirectSoundImpl**)ppDS;
2266 TRACE("(%p,%p,%p)\n",lpGUID,ippDS,pUnkOuter);
2268 TRACE("DirectSoundCreate (%p)\n", ippDS);
2273 return DSERR_INVALIDPARAM;
2276 IDirectSound_AddRef((LPDIRECTSOUND)dsound);
2281 /* Check that we actually have audio capabilities */
2282 /* If we do, whether it's busy or not, we continue */
2283 /* otherwise we return with DSERR_NODRIVER */
2285 audiofd = open("/dev/audio",O_WRONLY);
2286 if (audiofd == -1) {
2288 if (errno == ENODEV) {
2289 MESSAGE("No sound hardware found, but continuing anyway.\n");
2290 } else if (errno == EBUSY) {
2291 MESSAGE("Sound device busy, will keep trying.\n");
2293 MESSAGE("Unexpected error (%d) while checking for sound support.\n",errno);
2294 return DSERR_GENERIC;
2301 *ippDS = (IDirectSoundImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundImpl));
2303 return DSERR_OUTOFMEMORY;
2306 ICOM_VTBL(*ippDS) = &dsvt;
2307 (*ippDS)->buffers = NULL;
2308 (*ippDS)->nrofbuffers = 0;
2310 (*ippDS)->wfx.wFormatTag = 1;
2311 (*ippDS)->wfx.nChannels = 2;
2312 (*ippDS)->wfx.nSamplesPerSec = 22050;
2313 (*ippDS)->wfx.nAvgBytesPerSec = 44100;
2314 (*ippDS)->wfx.nBlockAlign = 2;
2315 (*ippDS)->wfx.wBitsPerSample = 8;
2317 InitializeCriticalSection(&((*ippDS)->lock));
2324 if (primarybuf == NULL) {
2328 dsbd.dwSize = sizeof(DSBUFFERDESC);
2329 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
2330 dsbd.dwBufferBytes = 0;
2331 dsbd.lpwfxFormat = &(dsound->wfx);
2332 hr = IDirectSound_CreateSoundBuffer(*ppDS, &dsbd, (LPDIRECTSOUNDBUFFER*)&primarybuf, NULL);
2335 dsound->primary = primarybuf;
2337 memset(primarybuf->buffer, 128, primarybuf->buflen);
2338 hnd = CreateThread(NULL,0,DSOUND_thread,0,0,&xid);
2342 MessageBoxA(0,"DirectSound needs the Open Sound System Driver, which has not been found by ./configure.","WINE DirectSound",MB_OK|MB_ICONSTOP);
2343 return DSERR_NODRIVER;
2347 /*******************************************************************************
2348 * DirectSound ClassFactory
2352 /* IUnknown fields */
2353 ICOM_VFIELD(IClassFactory);
2355 } IClassFactoryImpl;
2357 static HRESULT WINAPI
2358 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
2359 ICOM_THIS(IClassFactoryImpl,iface);
2361 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
2362 return E_NOINTERFACE;
2366 DSCF_AddRef(LPCLASSFACTORY iface) {
2367 ICOM_THIS(IClassFactoryImpl,iface);
2368 return ++(This->ref);
2371 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface) {
2372 ICOM_THIS(IClassFactoryImpl,iface);
2373 /* static class, won't be freed */
2374 return --(This->ref);
2377 static HRESULT WINAPI DSCF_CreateInstance(
2378 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
2380 ICOM_THIS(IClassFactoryImpl,iface);
2382 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
2383 if ( IsEqualGUID( &IID_IDirectSound, riid ) ) {
2384 /* FIXME: reuse already created dsound if present? */
2385 return DirectSoundCreate(riid,(LPDIRECTSOUND*)ppobj,pOuter);
2387 return E_NOINTERFACE;
2390 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2391 ICOM_THIS(IClassFactoryImpl,iface);
2392 FIXME("(%p)->(%d),stub!\n",This,dolock);
2396 static ICOM_VTABLE(IClassFactory) DSCF_Vtbl = {
2397 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2398 DSCF_QueryInterface,
2401 DSCF_CreateInstance,
2404 static IClassFactoryImpl DSOUND_CF = {&DSCF_Vtbl, 1 };
2406 /*******************************************************************************
2407 * DllGetClassObject [DSOUND.4]
2408 * Retrieves class object from a DLL object
2411 * Docs say returns STDAPI
2414 * rclsid [I] CLSID for the class object
2415 * riid [I] Reference to identifier of interface for class object
2416 * ppv [O] Address of variable to receive interface pointer for riid
2420 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
2423 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
2425 TRACE("(%p,%p,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
2426 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
2427 *ppv = (LPVOID)&DSOUND_CF;
2428 IClassFactory_AddRef((IClassFactory*)*ppv);
2432 FIXME("(%p,%p,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
2433 return CLASS_E_CLASSNOTAVAILABLE;
2437 /*******************************************************************************
2438 * DllCanUnloadNow [DSOUND.3] Determines whether the DLL is in use.
2444 DWORD WINAPI DSOUND_DllCanUnloadNow(void)
2446 FIXME("(void): stub\n");