Fixed 8-bit WAV format handling (it is unsigned data).
[wine/multimedia.git] / multimedia / dsound.c
blobbd9fbb70cefc84c3519dd9a9797e57bbb55987e6
1 /* DirectSound
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 */
6 /*
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 * FIXME: This file is full of race conditions and unlocked variable access
12 * from two threads. But we usually don't need to bother.
14 * Tested with a Soundblaster clone and a Gravis UltraSound Classic.
16 * Status:
17 * - Wing Commander 4/W95:
18 * The intromovie plays without problems. Nearly lipsynchron.
19 * - DiscWorld 2
20 * The sound works, but noticeable chunks are left out (from the sound and
21 * the animation). Don't know why yet.
22 * - Diablo:
23 * Sound works, but slows down the movieplayer.
24 * - XvT:
25 * Doesn't sound yet.
26 * - Monkey Island 3:
27 * The background sound of the startscreen works ;)
28 * - WingCommander Prophecy Demo:
29 * Sound works for the intromovie.
32 #include "config.h"
33 #include <assert.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/fcntl.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <math.h> /* Insomnia - pow() function */
42 #include "windows.h"
43 #include "winerror.h"
44 #include "interfaces.h"
45 #include "mmsystem.h"
46 #include "dsound.h"
47 #include "thread.h"
48 #include "debug.h"
49 #include "xmalloc.h"
51 #ifdef HAVE_OSS
52 # include <sys/ioctl.h>
53 # ifdef HAVE_MACHINE_SOUNDCARD_H
54 # include <machine/soundcard.h>
55 # endif
56 # ifdef HAVE_SYS_SOUNDCARD_H
57 # include <sys/soundcard.h>
58 # endif
60 /* #define USE_DSOUND3D 1 */
62 #define DSOUND_BUFLEN (primarybuf->wfx.nAvgBytesPerSec >> 4)
63 #define DSOUND_FREQSHIFT (14)
65 static int audiofd = -1;
66 static int audioOK = 0;
68 static LPDIRECTSOUND dsound = NULL;
70 static LPDIRECTSOUNDBUFFER primarybuf = NULL;
72 static int DSOUND_setformat(LPWAVEFORMATEX wfex);
73 static void DSOUND_CheckEvent(IDirectSoundBuffer *dsb, int len);
74 static void DSOUND_CloseAudio(void);
76 #endif
78 HRESULT WINAPI DirectSoundEnumerate32A(LPDSENUMCALLBACK32A enumcb,LPVOID context) {
79 #ifdef HAVE_OSS
80 enumcb(NULL,"WINE DirectSound using Open Sound System","sound",context);
81 #endif
82 return 0;
85 #ifdef HAVE_OSS
86 static void _dump_DSBCAPS(DWORD xmask) {
87 struct {
88 DWORD mask;
89 char *name;
90 } flags[] = {
91 #define FE(x) { x, #x },
92 FE(DSBCAPS_PRIMARYBUFFER)
93 FE(DSBCAPS_STATIC)
94 FE(DSBCAPS_LOCHARDWARE)
95 FE(DSBCAPS_LOCSOFTWARE)
96 FE(DSBCAPS_CTRLFREQUENCY)
97 FE(DSBCAPS_CTRLPAN)
98 FE(DSBCAPS_CTRLVOLUME)
99 FE(DSBCAPS_CTRLDEFAULT)
100 FE(DSBCAPS_CTRLALL)
101 FE(DSBCAPS_STICKYFOCUS)
102 FE(DSBCAPS_GETCURRENTPOSITION2)
104 int i;
106 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
107 if (flags[i].mask & xmask)
108 fprintf(stderr,"%s ",flags[i].name);
111 /*******************************************************************************
112 * IDirectSound3DBuffer
115 // IUnknown methods
116 static HRESULT WINAPI IDirectSound3DBuffer_QueryInterface(
117 LPDIRECTSOUND3DBUFFER this, REFIID riid, LPVOID *ppobj)
119 char xbuf[50];
121 WINE_StringFromCLSID(riid,xbuf);
122 TRACE(dsound,"(%p,%s,%p)\n",this,xbuf,ppobj);
123 return E_FAIL;
126 static ULONG WINAPI IDirectSound3DBuffer_AddRef(LPDIRECTSOUND3DBUFFER this)
128 this->ref++;
129 return this->ref;
132 static ULONG WINAPI IDirectSound3DBuffer_Release(LPDIRECTSOUND3DBUFFER this)
134 if(--this->ref)
135 return this->ref;
137 HeapFree(GetProcessHeap(),0,this->buffer);
138 HeapFree(GetProcessHeap(),0,this);
140 return 0;
143 // IDirectSound3DBuffer methods
144 static HRESULT WINAPI IDirectSound3DBuffer_GetAllParameters(
145 LPDIRECTSOUND3DBUFFER this,
146 LPDS3DBUFFER lpDs3dBuffer)
148 FIXME(dsound,"stub\n");
149 return DS_OK;
152 static HRESULT WINAPI IDirectSound3DBuffer_GetConeAngles(
153 LPDIRECTSOUND3DBUFFER this,
154 LPDWORD lpdwInsideConeAngle,
155 LPDWORD lpdwOutsideConeAngle)
157 FIXME(dsound,"stub\n");
158 return DS_OK;
161 static HRESULT WINAPI IDirectSound3DBuffer_GetConeOrientation(
162 LPDIRECTSOUND3DBUFFER this,
163 LPD3DVECTOR lpvConeOrientation)
165 FIXME(dsound,"stub\n");
166 return DS_OK;
169 static HRESULT WINAPI IDirectSound3DBuffer_GetConeOutsideVolume(
170 LPDIRECTSOUND3DBUFFER this,
171 LPLONG lplConeOutsideVolume)
173 FIXME(dsound,"stub\n");
174 return DS_OK;
177 static HRESULT WINAPI IDirectSound3DBuffer_GetMaxDistance(
178 LPDIRECTSOUND3DBUFFER this,
179 LPD3DVALUE lpfMaxDistance)
181 FIXME(dsound,"stub\n");
182 return DS_OK;
185 static HRESULT WINAPI IDirectSound3DBuffer_GetMinDistance(
186 LPDIRECTSOUND3DBUFFER this,
187 LPD3DVALUE lpfMinDistance)
189 FIXME(dsound,"stub\n");
190 return DS_OK;
193 static HRESULT WINAPI IDirectSound3DBuffer_GetMode(
194 LPDIRECTSOUND3DBUFFER this,
195 LPDWORD lpdwMode)
197 FIXME(dsound,"stub\n");
198 return DS_OK;
201 static HRESULT WINAPI IDirectSound3DBuffer_GetPosition(
202 LPDIRECTSOUND3DBUFFER this,
203 LPD3DVECTOR lpvPosition)
205 FIXME(dsound,"stub\n");
206 return DS_OK;
209 static HRESULT WINAPI IDirectSound3DBuffer_GetVelocity(
210 LPDIRECTSOUND3DBUFFER this,
211 LPD3DVECTOR lpvVelocity)
213 FIXME(dsound,"stub\n");
214 return DS_OK;
217 static HRESULT WINAPI IDirectSound3DBuffer_SetAllParameters(
218 LPDIRECTSOUND3DBUFFER this,
219 LPCDS3DBUFFER lpcDs3dBuffer,
220 DWORD dwApply)
222 FIXME(dsound,"stub\n");
223 return DS_OK;
226 static HRESULT WINAPI IDirectSound3DBuffer_SetConeAngles(
227 LPDIRECTSOUND3DBUFFER this,
228 DWORD dwInsideConeAngle,
229 DWORD dwOutsideConeAngle,
230 DWORD dwApply)
232 FIXME(dsound,"stub\n");
233 return DS_OK;
236 static HRESULT WINAPI IDirectSound3DBuffer_SetConeOrientation(
237 LPDIRECTSOUND3DBUFFER this,
238 D3DVALUE x, D3DVALUE y, D3DVALUE z,
239 DWORD dwApply)
241 FIXME(dsound,"stub\n");
242 return DS_OK;
245 static HRESULT WINAPI IDirectSound3DBuffer_SetConeOutsideVolume(
246 LPDIRECTSOUND3DBUFFER this,
247 LONG lConeOutsideVolume,
248 DWORD dwApply)
250 FIXME(dsound,"stub\n");
251 return DS_OK;
254 static HRESULT WINAPI IDirectSound3DBuffer_SetMaxDistance(
255 LPDIRECTSOUND3DBUFFER this,
256 D3DVALUE fMaxDistance,
257 DWORD dwApply)
259 FIXME(dsound,"stub\n");
260 return DS_OK;
263 static HRESULT WINAPI IDirectSound3DBuffer_SetMinDistance(
264 LPDIRECTSOUND3DBUFFER this,
265 D3DVALUE fMinDistance,
266 DWORD dwApply)
268 FIXME(dsound,"stub\n");
269 return DS_OK;
272 static HRESULT WINAPI IDirectSound3DBuffer_SetMode(
273 LPDIRECTSOUND3DBUFFER this,
274 DWORD dwMode,
275 DWORD dwApply)
277 TRACE(dsound, "mode = %lx\n", dwMode);
278 this->ds3db.dwMode = dwMode;
279 return DS_OK;
282 static HRESULT WINAPI IDirectSound3DBuffer_SetPosition(
283 LPDIRECTSOUND3DBUFFER this,
284 D3DVALUE x, D3DVALUE y, D3DVALUE z,
285 DWORD dwApply)
287 FIXME(dsound,"stub\n");
288 return DS_OK;
291 static HRESULT WINAPI IDirectSound3DBuffer_SetVelocity(
292 LPDIRECTSOUND3DBUFFER this,
293 D3DVALUE x, D3DVALUE y, D3DVALUE z,
294 DWORD dwApply)
296 FIXME(dsound,"stub\n");
297 return DS_OK;
300 IDirectSound3DBuffer_VTable ds3dbvt = {
301 // IUnknown methods
302 IDirectSound3DBuffer_QueryInterface,
303 IDirectSound3DBuffer_AddRef,
304 IDirectSound3DBuffer_Release,
305 // IDirectSound3DBuffer methods
306 IDirectSound3DBuffer_GetAllParameters,
307 IDirectSound3DBuffer_GetConeAngles,
308 IDirectSound3DBuffer_GetConeOrientation,
309 IDirectSound3DBuffer_GetConeOutsideVolume,
310 IDirectSound3DBuffer_GetMaxDistance,
311 IDirectSound3DBuffer_GetMinDistance,
312 IDirectSound3DBuffer_GetMode,
313 IDirectSound3DBuffer_GetPosition,
314 IDirectSound3DBuffer_GetVelocity,
315 IDirectSound3DBuffer_SetAllParameters,
316 IDirectSound3DBuffer_SetConeAngles,
317 IDirectSound3DBuffer_SetConeOrientation,
318 IDirectSound3DBuffer_SetConeOutsideVolume,
319 IDirectSound3DBuffer_SetMaxDistance,
320 IDirectSound3DBuffer_SetMinDistance,
321 IDirectSound3DBuffer_SetMode,
322 IDirectSound3DBuffer_SetPosition,
323 IDirectSound3DBuffer_SetVelocity,
326 #ifdef USE_DSOUND3D
327 static int DSOUND_Create3DBuffer(LPDIRECTSOUNDBUFFER dsb)
329 DWORD i, temp, iSize, oSize, offset;
330 LPBYTE bIbuf, bObuf, bTbuf = NULL;
331 LPWORD wIbuf, wObuf, wTbuf = NULL;
333 // Inside DirectX says it's stupid but allowed
334 if (dsb->wfx.nChannels == 2) {
335 // Convert to mono
336 if (dsb->wfx.wBitsPerSample == 16) {
337 iSize = dsb->buflen / 4;
338 wTbuf = malloc(dsb->buflen / 2);
339 if (wTbuf == NULL)
340 return DSERR_OUTOFMEMORY;
341 for (i = 0; i < iSize; i++)
342 wTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
343 wIbuf = wTbuf;
344 } else {
345 iSize = dsb->buflen / 2;
346 bTbuf = malloc(dsb->buflen / 2);
347 if (bTbuf == NULL)
348 return DSERR_OUTOFMEMORY;
349 for (i = 0; i < iSize; i++)
350 bTbuf[i] = (dsb->buffer[i] + dsb->buffer[(i * 2) + 1]) / 2;
351 bIbuf = bTbuf;
353 } else {
354 if (dsb->wfx.wBitsPerSample == 16) {
355 iSize = dsb->buflen / 2;
356 wIbuf = (LPWORD) dsb->buffer;
357 } else {
358 bIbuf = (LPBYTE) dsb->buffer;
359 iSize = dsb->buflen;
363 if (primarybuf->wfx.wBitsPerSample == 16) {
364 wObuf = (LPWORD) dsb->ds3db->buffer;
365 oSize = dsb->ds3db->buflen / 2;
366 } else {
367 bObuf = (LPBYTE) dsb->ds3db->buffer;
368 oSize = dsb->ds3db->buflen;
371 offset = primarybuf->wfx.nSamplesPerSec / 100; // 10ms
372 if (primarybuf->wfx.wBitsPerSample == 16 && dsb->wfx.wBitsPerSample == 16)
373 for (i = 0; i < iSize; i++) {
374 temp = wIbuf[i];
375 if (i >= offset)
376 temp += wIbuf[i - offset] >> 9;
377 else
378 temp += wIbuf[i + iSize - offset] >> 9;
379 wObuf[i * 2] = temp;
380 wObuf[(i * 2) + 1] = temp;
382 else if (primarybuf->wfx.wBitsPerSample == 8 && dsb->wfx.wBitsPerSample == 8)
383 for (i = 0; i < iSize; i++) {
384 temp = bIbuf[i];
385 if (i >= offset)
386 temp += bIbuf[i - offset] >> 5;
387 else
388 temp += bIbuf[i + iSize - offset] >> 5;
389 bObuf[i * 2] = temp;
390 bObuf[(i * 2) + 1] = temp;
393 if (wTbuf)
394 free(wTbuf);
395 if (bTbuf)
396 free(bTbuf);
398 return DS_OK;
400 #endif
401 /*******************************************************************************
402 * IDirectSound3DListener
405 // IUnknown methods
406 static HRESULT WINAPI IDirectSound3DListener_QueryInterface(
407 LPDIRECTSOUND3DLISTENER this, REFIID riid, LPVOID *ppobj)
409 char xbuf[50];
411 WINE_StringFromCLSID(riid,xbuf);
412 TRACE(dsound,"(%p,%s,%p)\n",this,xbuf,ppobj);
413 return E_FAIL;
416 static ULONG WINAPI IDirectSound3DListener_AddRef(LPDIRECTSOUND3DLISTENER this)
418 this->ref++;
419 return this->ref;
422 static ULONG WINAPI IDirectSound3DListener_Release(LPDIRECTSOUND3DLISTENER this)
424 this->ref--;
425 return this->ref;
428 // IDirectSound3DListener methods
429 static HRESULT WINAPI IDirectSound3DListener_GetAllParameter(
430 LPDIRECTSOUND3DLISTENER this,
431 LPDS3DLISTENER lpDS3DL)
433 FIXME(dsound,"stub\n");
434 return DS_OK;
437 static HRESULT WINAPI IDirectSound3DListener_GetDistanceFactor(
438 LPDIRECTSOUND3DLISTENER this,
439 LPD3DVALUE lpfDistanceFactor)
441 FIXME(dsound,"stub\n");
442 return DS_OK;
445 static HRESULT WINAPI IDirectSound3DListener_GetDopplerFactor(
446 LPDIRECTSOUND3DLISTENER this,
447 LPD3DVALUE lpfDopplerFactor)
449 FIXME(dsound,"stub\n");
450 return DS_OK;
453 static HRESULT WINAPI IDirectSound3DListener_GetOrientation(
454 LPDIRECTSOUND3DLISTENER this,
455 LPD3DVECTOR lpvOrientFront,
456 LPD3DVECTOR lpvOrientTop)
458 FIXME(dsound,"stub\n");
459 return DS_OK;
462 static HRESULT WINAPI IDirectSound3DListener_GetPosition(
463 LPDIRECTSOUND3DLISTENER this,
464 LPD3DVECTOR lpvPosition)
466 FIXME(dsound,"stub\n");
467 return DS_OK;
470 static HRESULT WINAPI IDirectSound3DListener_GetRolloffFactor(
471 LPDIRECTSOUND3DLISTENER this,
472 LPD3DVALUE lpfRolloffFactor)
474 FIXME(dsound,"stub\n");
475 return DS_OK;
478 static HRESULT WINAPI IDirectSound3DListener_GetVelocity(
479 LPDIRECTSOUND3DLISTENER this,
480 LPD3DVECTOR lpvVelocity)
482 FIXME(dsound,"stub\n");
483 return DS_OK;
486 static HRESULT WINAPI IDirectSound3DListener_SetAllParameters(
487 LPDIRECTSOUND3DLISTENER this,
488 LPCDS3DLISTENER lpcDS3DL,
489 DWORD dwApply)
491 FIXME(dsound,"stub\n");
492 return DS_OK;
495 static HRESULT WINAPI IDirectSound3DListener_SetDistanceFactor(
496 LPDIRECTSOUND3DLISTENER this,
497 D3DVALUE fDistanceFactor,
498 DWORD dwApply)
500 FIXME(dsound,"stub\n");
501 return DS_OK;
504 static HRESULT WINAPI IDirectSound3DListener_SetDopplerFactor(
505 LPDIRECTSOUND3DLISTENER this,
506 D3DVALUE fDopplerFactor,
507 DWORD dwApply)
509 FIXME(dsound,"stub\n");
510 return DS_OK;
513 static HRESULT WINAPI IDirectSound3DListener_SetOrientation(
514 LPDIRECTSOUND3DLISTENER this,
515 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
516 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
517 DWORD dwApply)
519 FIXME(dsound,"stub\n");
520 return DS_OK;
523 static HRESULT WINAPI IDirectSound3DListener_SetPosition(
524 LPDIRECTSOUND3DLISTENER this,
525 D3DVALUE x, D3DVALUE y, D3DVALUE z,
526 DWORD dwApply)
528 FIXME(dsound,"stub\n");
529 return DS_OK;
532 static HRESULT WINAPI IDirectSound3DListener_SetRolloffFactor(
533 LPDIRECTSOUND3DLISTENER this,
534 D3DVALUE fRolloffFactor,
535 DWORD dwApply)
537 FIXME(dsound,"stub\n");
538 return DS_OK;
541 static HRESULT WINAPI IDirectSound3DListener_SetVelocity(
542 LPDIRECTSOUND3DLISTENER this,
543 D3DVALUE x, D3DVALUE y, D3DVALUE z,
544 DWORD dwApply)
546 FIXME(dsound,"stub\n");
547 return DS_OK;
550 static HRESULT WINAPI IDirectSound3DListener_CommitDeferredSettings(
551 LPDIRECTSOUND3DLISTENER this)
554 FIXME(dsound,"stub\n");
555 return DS_OK;
558 IDirectSound3DListener_VTable ds3dlvt = {
559 // IUnknown methods
560 IDirectSound3DListener_QueryInterface,
561 IDirectSound3DListener_AddRef,
562 IDirectSound3DListener_Release,
563 // IDirectSound3DListener methods
564 IDirectSound3DListener_GetAllParameter,
565 IDirectSound3DListener_GetDistanceFactor,
566 IDirectSound3DListener_GetDopplerFactor,
567 IDirectSound3DListener_GetOrientation,
568 IDirectSound3DListener_GetPosition,
569 IDirectSound3DListener_GetRolloffFactor,
570 IDirectSound3DListener_GetVelocity,
571 IDirectSound3DListener_SetAllParameters,
572 IDirectSound3DListener_SetDistanceFactor,
573 IDirectSound3DListener_SetDopplerFactor,
574 IDirectSound3DListener_SetOrientation,
575 IDirectSound3DListener_SetPosition,
576 IDirectSound3DListener_SetRolloffFactor,
577 IDirectSound3DListener_SetVelocity,
578 IDirectSound3DListener_CommitDeferredSettings,
581 /*******************************************************************************
582 * IDirectSoundNotify
584 static HRESULT WINAPI IDirectSoundNotify_QueryInterface(
585 LPDIRECTSOUNDNOTIFY this,REFIID riid,LPVOID *ppobj
587 char xbuf[50];
589 WINE_StringFromCLSID(riid,xbuf);
590 TRACE(dsound,"(%p,%s,%p)\n",this,xbuf,ppobj);
591 return E_FAIL;
594 static ULONG WINAPI IDirectSoundNotify_AddRef(LPDIRECTSOUNDNOTIFY this) {
595 return ++(this->ref);
598 static ULONG WINAPI IDirectSoundNotify_Release(LPDIRECTSOUNDNOTIFY this) {
599 this->ref--;
600 if (!this->ref) {
601 this->dsb->lpvtbl->fnRelease(this->dsb);
602 HeapFree(GetProcessHeap(),0,this);
603 return 0;
605 return this->ref;
608 static HRESULT WINAPI IDirectSoundNotify_SetNotificationPositions(
609 LPDIRECTSOUNDNOTIFY this,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
611 int i;
613 if (TRACE_ON(dsound)) {
614 TRACE(dsound,"(%p,0x%08lx,%p)\n",this,howmuch,notify);
615 for (i=0;i<howmuch;i++)
616 TRACE(dsound,"notify at %ld to 0x%08lx\n",
617 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
619 this->dsb->notifies = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,this->dsb->notifies,(this->dsb->nrofnotifies+howmuch)*sizeof(DSBPOSITIONNOTIFY));
620 memcpy( this->dsb->notifies+this->dsb->nrofnotifies,
621 notify,
622 howmuch*sizeof(DSBPOSITIONNOTIFY)
624 this->dsb->nrofnotifies+=howmuch;
626 return 0;
629 IDirectSoundNotify_VTable dsnvt = {
630 IDirectSoundNotify_QueryInterface,
631 IDirectSoundNotify_AddRef,
632 IDirectSoundNotify_Release,
633 IDirectSoundNotify_SetNotificationPositions,
636 /*******************************************************************************
637 * IDirectSoundBuffer
640 // This sets this format for the <em>Primary Buffer Only</em>
641 // See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120
642 static HRESULT WINAPI IDirectSoundBuffer_SetFormat(
643 LPDIRECTSOUNDBUFFER this,LPWAVEFORMATEX wfex
645 LPDIRECTSOUNDBUFFER *dsb;
646 int i;
648 // ****
649 EnterCriticalSection(&(primarybuf->lock));
651 if (primarybuf->wfx.nSamplesPerSec != wfex->nSamplesPerSec) {
652 dsb = dsound->buffers;
653 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
654 // ****
655 EnterCriticalSection(&((*dsb)->lock));
657 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
658 wfex->nSamplesPerSec;
660 LeaveCriticalSection(&((*dsb)->lock));
661 // ****
665 memcpy(&(primarybuf->wfx), wfex, sizeof(primarybuf->wfx));
667 TRACE(dsound,"(formattag=0x%04x,chans=%d,samplerate=%ld"
668 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
669 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
670 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
671 wfex->wBitsPerSample, wfex->cbSize);
673 primarybuf->wfx.nAvgBytesPerSec =
674 this->wfx.nSamplesPerSec * this->wfx.nBlockAlign;
676 DSOUND_CloseAudio();
678 LeaveCriticalSection(&(primarybuf->lock));
679 // ****
681 return DS_OK;
684 static HRESULT WINAPI IDirectSoundBuffer_SetVolume(
685 LPDIRECTSOUNDBUFFER this,LONG vol
687 double temp;
689 TRACE(dsound,"(%p,%ld)\n",this,vol);
691 // I'm not sure if we need this for primary buffer
692 if (!(this->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
693 return DSERR_CONTROLUNAVAIL;
695 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN))
696 return DSERR_INVALIDPARAM;
698 // This needs to adjust the soundcard volume when
699 // called for the primary buffer
700 if (this->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
701 FIXME(dsound, "Volume control of primary unimplemented.\n");
702 this->volume = vol;
703 return DS_OK;
706 // ****
707 EnterCriticalSection(&(this->lock));
709 this->volume = vol;
711 temp = (double) (this->volume - (this->pan > 0 ? this->pan : 0));
712 this->lVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
713 temp = (double) (this->volume + (this->pan < 0 ? this->pan : 0));
714 this->rVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
716 LeaveCriticalSection(&(this->lock));
717 // ****
719 TRACE(dsound, "left = %lx, right = %lx\n", this->lVolAdjust, this->rVolAdjust);
721 return DS_OK;
724 static HRESULT WINAPI IDirectSoundBuffer_GetVolume(
725 LPDIRECTSOUNDBUFFER this,LPLONG vol
727 TRACE(dsound,"(%p,%p)\n",this,vol);
728 *vol = this->volume;
729 return 0;
732 static HRESULT WINAPI IDirectSoundBuffer_SetFrequency(
733 LPDIRECTSOUNDBUFFER this,DWORD freq
735 TRACE(dsound,"(%p,%ld)\n",this,freq);
737 // You cannot set the frequency of the primary buffer
738 if (!(this->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY) ||
739 (this->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
740 return DSERR_CONTROLUNAVAIL;
742 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX))
743 return DSERR_INVALIDPARAM;
745 // ****
746 EnterCriticalSection(&(this->lock));
748 this->freq = freq;
749 this->freqAdjust = (freq << DSOUND_FREQSHIFT) / primarybuf->wfx.nSamplesPerSec;
750 this->nAvgBytesPerSec = freq * this->wfx.nBlockAlign;
752 LeaveCriticalSection(&(this->lock));
753 // ****
755 return DS_OK;
758 static HRESULT WINAPI IDirectSoundBuffer_Play(
759 LPDIRECTSOUNDBUFFER this,DWORD reserved1,DWORD reserved2,DWORD flags
761 TRACE(dsound,"(%p,%08lx,%08lx,%08lx)\n",
762 this,reserved1,reserved2,flags
764 this->playflags = flags;
765 this->playing = 1;
766 return 0;
769 static HRESULT WINAPI IDirectSoundBuffer_Stop(LPDIRECTSOUNDBUFFER this)
771 TRACE(dsound,"(%p)\n",this);
773 // ****
774 EnterCriticalSection(&(this->lock));
776 this->playing = 0;
777 DSOUND_CheckEvent(this, 0);
779 LeaveCriticalSection(&(this->lock));
780 // ****
782 return 0;
785 static DWORD WINAPI IDirectSoundBuffer_AddRef(LPDIRECTSOUNDBUFFER this) {
786 // TRACE(dsound,"(%p) ref was %ld\n",this, this->ref);
788 return ++(this->ref);
790 static DWORD WINAPI IDirectSoundBuffer_Release(LPDIRECTSOUNDBUFFER this) {
791 int i;
793 // TRACE(dsound,"(%p) ref was %ld\n",this, this->ref);
795 if (--this->ref)
796 return this->ref;
798 for (i=0;i<this->dsound->nrofbuffers;i++)
799 if (this->dsound->buffers[i] == this)
800 break;
801 if (i < this->dsound->nrofbuffers) {
802 /* Put the last buffer of the list in the (now empty) position */
803 this->dsound->buffers[i] = this->dsound->buffers[this->dsound->nrofbuffers - 1];
804 this->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,this->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER)*this->dsound->nrofbuffers);
805 this->dsound->nrofbuffers--;
806 this->dsound->lpvtbl->fnRelease(this->dsound);
809 DeleteCriticalSection(&(this->lock));
811 if (this->ds3db && this->ds3db->lpvtbl)
812 this->ds3db->lpvtbl->fnRelease(this->ds3db);
814 HeapFree(GetProcessHeap(),0,this->buffer);
815 HeapFree(GetProcessHeap(),0,this);
817 if (this == primarybuf)
818 primarybuf = NULL;
820 return 0;
823 static HRESULT WINAPI IDirectSoundBuffer_GetCurrentPosition(
824 LPDIRECTSOUNDBUFFER this,LPDWORD playpos,LPDWORD writepos
826 TRACE(dsound,"(%p,%p,%p)\n",this,playpos,writepos);
827 if (playpos) *playpos = this->playpos;
828 if (writepos) *writepos = this->writepos;
829 return 0;
832 static HRESULT WINAPI IDirectSoundBuffer_GetStatus(
833 LPDIRECTSOUNDBUFFER this,LPDWORD status
835 TRACE(dsound,"(%p,%p)\n",this,status);
836 *status = 0;
837 if (this->playing)
838 *status |= DSBSTATUS_PLAYING;
839 if (this->playflags & DSBPLAY_LOOPING)
840 *status |= DSBSTATUS_LOOPING;
841 return 0;
845 static HRESULT WINAPI IDirectSoundBuffer_GetFormat(
846 LPDIRECTSOUNDBUFFER this,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
848 TRACE(dsound,"(%p,%p,%ld,%p)\n",this,lpwf,wfsize,wfwritten);
849 if (wfsize>sizeof(this->wfx))
850 wfsize = sizeof(this->wfx);
851 if (lpwf) { // NULL is valid
852 memcpy(lpwf,&(this->wfx),wfsize);
853 if (wfwritten)
854 *wfwritten = wfsize;
855 } else
856 if (wfwritten)
857 *wfwritten = sizeof(this->wfx);
858 else
859 return DSERR_INVALIDPARAM;
861 return 0;
864 static HRESULT WINAPI IDirectSoundBuffer_Lock(
865 LPDIRECTSOUNDBUFFER this,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
868 TRACE(dsound,"(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
869 this,
870 writecursor,
871 writebytes,
872 lplpaudioptr1,
873 audiobytes1,
874 lplpaudioptr2,
875 audiobytes2,
876 flags
878 if (flags & DSBLOCK_FROMWRITECURSOR)
879 writecursor += this->writepos;
880 if (flags & DSBLOCK_ENTIREBUFFER)
881 writebytes = this->buflen;
882 if (writebytes > this->buflen)
883 writebytes = this->buflen;
885 assert(audiobytes1!=audiobytes2);
886 assert(lplpaudioptr1!=lplpaudioptr2);
887 if (writecursor+writebytes <= this->buflen) {
888 *(LPBYTE*)lplpaudioptr1 = this->buffer+writecursor;
889 *audiobytes1 = writebytes;
890 if (lplpaudioptr2)
891 *(LPBYTE*)lplpaudioptr2 = NULL;
892 if (audiobytes2)
893 *audiobytes2 = 0;
894 TRACE(dsound,"->%ld.0\n",writebytes);
895 } else {
896 *(LPBYTE*)lplpaudioptr1 = this->buffer+writecursor;
897 *audiobytes1 = this->buflen-writecursor;
898 if (lplpaudioptr2)
899 *(LPBYTE*)lplpaudioptr2 = this->buffer;
900 if (audiobytes2)
901 *audiobytes2 = writebytes-(this->buflen-writecursor);
902 TRACE(dsound,"->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
904 // No. See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 21
905 // this->writepos=(writecursor+writebytes)%this->buflen;
906 return 0;
909 static HRESULT WINAPI IDirectSoundBuffer_SetCurrentPosition(
910 LPDIRECTSOUNDBUFFER this,DWORD newpos
912 TRACE(dsound,"(%p,%ld)\n",this,newpos);
914 // ****
915 EnterCriticalSection(&(this->lock));
917 this->playpos = newpos;
919 LeaveCriticalSection(&(this->lock));
920 // ****
922 return 0;
925 static HRESULT WINAPI IDirectSoundBuffer_SetPan(
926 LPDIRECTSOUNDBUFFER this,LONG pan
928 double temp;
930 TRACE(dsound,"(%p,%ld)\n",this,pan);
932 if (!(this) || (pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT))
933 return DSERR_INVALIDPARAM;
935 // You cannot set the pan of the primary buffer
936 // and you cannot use both pan and 3D controls
937 if (!(this->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
938 (this->dsbd.dwFlags & DSBCAPS_CTRL3D) ||
939 (this->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER))
940 return DSERR_CONTROLUNAVAIL;
942 // ****
943 EnterCriticalSection(&(this->lock));
945 this->pan = pan;
947 temp = (double) (this->volume - (this->pan > 0 ? this->pan : 0));
948 this->lVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
949 temp = (double) (this->volume + (this->pan < 0 ? this->pan : 0));
950 this->rVolAdjust = (ULONG) (pow(2.0, temp / 600.0) * 32768.0);
952 LeaveCriticalSection(&(this->lock));
953 // ****
955 return DS_OK;
958 static HRESULT WINAPI IDirectSoundBuffer_GetPan(
959 LPDIRECTSOUNDBUFFER this,LPLONG pan
961 TRACE(dsound,"(%p,%p)\n",this,pan);
962 *pan = this->pan;
963 return 0;
966 static HRESULT WINAPI IDirectSoundBuffer_Unlock(
967 LPDIRECTSOUNDBUFFER this,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
969 TRACE(dsound,"(%p,%p,%ld,%p,%ld):stub\n", this,p1,x1,p2,x2);
970 #if 0
971 // This is highly experimental and liable to break things
972 if (this->dsbd.dwFlags & DSBCAPS_CTRL3D)
973 DSOUND_Create3DBuffer(this);
974 #endif
975 return 0;
978 static HRESULT WINAPI IDirectSoundBuffer_GetFrequency(
979 LPDIRECTSOUNDBUFFER this,LPDWORD freq
981 TRACE(dsound,"(%p,%p)\n",this,freq);
982 *freq = this->freq;
983 return 0;
986 static HRESULT WINAPI IDirectSoundBuffer_Initialize(
987 LPDIRECTSOUNDBUFFER this,LPDIRECTSOUND dsound,LPDSBUFFERDESC dbsd
989 FIXME(dsound,"(%p,%p,%p):stub\n",this,dsound,dbsd);
990 printf("Re-Init!!!\n");
991 return DSERR_ALREADYINITIALIZED;
994 static HRESULT WINAPI IDirectSoundBuffer_GetCaps(
995 LPDIRECTSOUNDBUFFER this,LPDSBCAPS caps
997 TRACE(dsound,"(%p)->(%p)\n",this,caps);
999 caps->dwSize = sizeof(*caps);
1000 caps->dwFlags = this->dsbd.dwFlags | DSBCAPS_LOCSOFTWARE;
1001 caps->dwBufferBytes = this->dsbd.dwBufferBytes;
1002 /* This value represents the speed of the "unlock" command.
1003 As unlock is quite fast (it does not do anything), I put
1004 4096 ko/s = 4 Mo / s */
1005 caps->dwUnlockTransferRate = 4096;
1006 caps->dwPlayCpuOverhead = 0;
1008 return DS_OK;
1011 static HRESULT WINAPI IDirectSoundBuffer_QueryInterface(
1012 LPDIRECTSOUNDBUFFER this,REFIID riid,LPVOID *ppobj
1014 char xbuf[50];
1016 WINE_StringFromCLSID(riid,xbuf);
1017 TRACE(dsound,"(%p,%s,%p)\n",this,xbuf,ppobj);
1019 if (!memcmp(&IID_IDirectSoundNotify,riid,sizeof(*riid))) {
1020 IDirectSoundNotify *dsn;
1022 dsn = (LPDIRECTSOUNDNOTIFY)HeapAlloc(GetProcessHeap(),0,sizeof(*dsn));
1023 dsn->ref = 1;
1024 dsn->dsb = this;
1025 this->lpvtbl->fnAddRef(this);
1026 dsn->lpvtbl = &dsnvt;
1027 *ppobj = (LPVOID)dsn;
1028 return 0;
1031 if (!memcmp(&IID_IDirectSound3DBuffer,riid,sizeof(*riid))) {
1032 *ppobj = this->ds3db;
1033 if (*ppobj)
1034 return DS_OK;
1037 return E_FAIL;
1040 static struct tagLPDIRECTSOUNDBUFFER_VTABLE dsbvt = {
1041 IDirectSoundBuffer_QueryInterface,
1042 IDirectSoundBuffer_AddRef,
1043 IDirectSoundBuffer_Release,
1044 IDirectSoundBuffer_GetCaps,
1045 IDirectSoundBuffer_GetCurrentPosition,
1046 IDirectSoundBuffer_GetFormat,
1047 IDirectSoundBuffer_GetVolume,
1048 IDirectSoundBuffer_GetPan,
1049 IDirectSoundBuffer_GetFrequency,
1050 IDirectSoundBuffer_GetStatus,
1051 IDirectSoundBuffer_Initialize,
1052 IDirectSoundBuffer_Lock,
1053 IDirectSoundBuffer_Play,
1054 IDirectSoundBuffer_SetCurrentPosition,
1055 IDirectSoundBuffer_SetFormat,
1056 IDirectSoundBuffer_SetVolume,
1057 IDirectSoundBuffer_SetPan,
1058 IDirectSoundBuffer_SetFrequency,
1059 IDirectSoundBuffer_Stop,
1060 IDirectSoundBuffer_Unlock
1063 /*******************************************************************************
1064 * IDirectSound
1067 static HRESULT WINAPI IDirectSound_SetCooperativeLevel(
1068 LPDIRECTSOUND this,HWND32 hwnd,DWORD level
1070 FIXME(dsound,"(%p,%08lx,%ld):stub\n",this,(DWORD)hwnd,level);
1071 return 0;
1075 static HRESULT WINAPI IDirectSound_CreateSoundBuffer(
1076 LPDIRECTSOUND this,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER ppdsb,LPUNKNOWN lpunk
1078 LPWAVEFORMATEX wfex;
1080 TRACE(dsound,"(%p,%p,%p,%p)\n",this,dsbd,ppdsb,lpunk);
1082 if ((this == NULL) || (dsbd == NULL) || (ppdsb == NULL))
1083 return DSERR_INVALIDPARAM;
1085 if (TRACE_ON(dsound)) {
1086 TRACE(dsound,"(size=%ld)\n",dsbd->dwSize);
1087 TRACE(dsound,"(flags=0x%08lx\n",dsbd->dwFlags);
1088 _dump_DSBCAPS(dsbd->dwFlags);
1089 TRACE(dsound,"(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
1090 TRACE(dsound,"(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
1093 wfex = dsbd->lpwfxFormat;
1095 if (wfex)
1096 TRACE(dsound,"(formattag=0x%04x,chans=%d,samplerate=%ld"
1097 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1098 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1099 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1100 wfex->wBitsPerSample, wfex->cbSize);
1102 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1103 if (primarybuf) {
1104 primarybuf->lpvtbl->fnAddRef(primarybuf);
1105 *ppdsb = primarybuf;
1106 return DS_OK;
1107 } // Else create primarybuf
1110 *ppdsb = (LPDIRECTSOUNDBUFFER)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBuffer));
1111 if (*ppdsb == NULL)
1112 return DSERR_OUTOFMEMORY;
1113 (*ppdsb)->ref = 1;
1115 TRACE(dsound, "Created buffer at %p\n", *ppdsb);
1117 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER) {
1118 (*ppdsb)->buflen = dsound->wfx.nAvgBytesPerSec;
1119 (*ppdsb)->freq = dsound->wfx.nSamplesPerSec;
1120 } else {
1121 (*ppdsb)->buflen = dsbd->dwBufferBytes;
1122 (*ppdsb)->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1124 (*ppdsb)->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,(*ppdsb)->buflen);
1125 if ((*ppdsb)->buffer == NULL) {
1126 HeapFree(GetProcessHeap(),0,(*ppdsb));
1127 *ppdsb = NULL;
1128 return DSERR_OUTOFMEMORY;
1130 (*ppdsb)->playpos = 0;
1131 (*ppdsb)->writepos = 0;
1132 (*ppdsb)->lpvtbl = &dsbvt;
1133 (*ppdsb)->dsound = this;
1134 (*ppdsb)->playing = 0;
1135 (*ppdsb)->lVolAdjust = (1 << 15);
1136 (*ppdsb)->rVolAdjust = (1 << 15);
1138 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1139 (*ppdsb)->freqAdjust = ((*ppdsb)->freq << DSOUND_FREQSHIFT) /
1140 primarybuf->wfx.nSamplesPerSec;
1141 (*ppdsb)->nAvgBytesPerSec = (*ppdsb)->freq *
1142 dsbd->lpwfxFormat->nBlockAlign;
1145 memcpy(&((*ppdsb)->dsbd),dsbd,sizeof(*dsbd));
1147 /* register buffer */
1148 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1149 this->buffers = (LPDIRECTSOUNDBUFFER*)HeapReAlloc(GetProcessHeap(),0,this->buffers,sizeof(LPDIRECTSOUNDBUFFER)*(this->nrofbuffers+1));
1150 this->buffers[this->nrofbuffers] = *ppdsb;
1151 this->nrofbuffers++;
1152 this->lpvtbl->fnAddRef(this);
1155 if (dsbd->lpwfxFormat)
1156 memcpy(&((*ppdsb)->wfx), dsbd->lpwfxFormat, sizeof((*ppdsb)->wfx));
1158 InitializeCriticalSection(&((*ppdsb)->lock));
1160 #if 0
1161 if (dsbd->dwFlags & DSBCAPS_CTRL3D) {
1162 IDirectSound3DBuffer *ds3db;
1164 ds3db = (LPDIRECTSOUND3DBUFFER)HeapAlloc(GetProcessHeap(),
1165 0,sizeof(*ds3db));
1166 ds3db->ref = 1;
1167 ds3db->dsb = (*ppdsb);
1168 ds3db->lpvtbl = &ds3dbvt;
1169 (*ppdsb)->ds3db = ds3db;
1170 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
1171 ds3db->ds3db.vPosition.x = 0.0;
1172 ds3db->ds3db.vPosition.y = 0.0;
1173 ds3db->ds3db.vPosition.z = 0.0;
1174 ds3db->ds3db.vVelocity.x = 0.0;
1175 ds3db->ds3db.vVelocity.y = 0.0;
1176 ds3db->ds3db.vVelocity.z = 0.0;
1177 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1178 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1179 ds3db->ds3db.vConeOrientation.x = 0.0;
1180 ds3db->ds3db.vConeOrientation.y = 0.0;
1181 ds3db->ds3db.vConeOrientation.z = 0.0;
1182 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1183 ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1184 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1185 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
1186 ds3db->buflen = ((*ppdsb)->buflen * primarybuf->wfx.nBlockAlign) /
1187 (*ppdsb)->wfx.nBlockAlign;
1188 ds3db->buffer = HeapAlloc(GetProcessHeap(), 0, ds3db->buflen);
1189 if (ds3db->buffer == NULL) {
1190 ds3db->buflen = 0;
1191 ds3db->ds3db.dwMode = DS3DMODE_DISABLE;
1194 #endif
1195 return DS_OK;
1198 static HRESULT WINAPI IDirectSound_DuplicateSoundBuffer(
1199 LPDIRECTSOUND this,LPDIRECTSOUNDBUFFER pdsb,LPLPDIRECTSOUNDBUFFER ppdsb
1201 TRACE(dsound,"(%p,%p,%p)\n",this,pdsb,ppdsb);
1203 *ppdsb = (LPDIRECTSOUNDBUFFER)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBuffer));
1205 (*ppdsb)->ref =1;
1206 (*ppdsb)->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,pdsb->buflen);
1207 memcpy((*ppdsb)->buffer,pdsb->buffer,pdsb->buflen);
1208 (*ppdsb)->buflen = pdsb->buflen;
1209 (*ppdsb)->playpos = 0;
1210 (*ppdsb)->writepos = 0;
1211 (*ppdsb)->lpvtbl = &dsbvt;
1212 (*ppdsb)->dsound = this;
1213 memcpy(&((*ppdsb)->wfx), &(pdsb->wfx), sizeof((*ppdsb)->wfx));
1214 /* register buffer */
1215 this->buffers = (LPDIRECTSOUNDBUFFER*)HeapReAlloc(GetProcessHeap(),0,this->buffers,sizeof(LPDIRECTSOUNDBUFFER)*(this->nrofbuffers+1));
1216 this->buffers[this->nrofbuffers] = *ppdsb;
1217 this->nrofbuffers++;
1218 this->lpvtbl->fnAddRef(this);
1219 return 0;
1223 static HRESULT WINAPI IDirectSound_GetCaps(LPDIRECTSOUND this,LPDSCAPS caps) {
1224 TRACE(dsound,"(%p,%p)\n",this,caps);
1225 TRACE(dsound,"(flags=0x%08lx)\n",caps->dwFlags);
1227 caps->dwSize = sizeof(*caps);
1228 caps->dwFlags =
1229 DSCAPS_PRIMARYSTEREO |
1230 DSCAPS_PRIMARY16BIT |
1231 DSCAPS_SECONDARYSTEREO |
1232 DSCAPS_SECONDARY16BIT |
1233 DSCAPS_CONTINUOUSRATE;
1234 /* FIXME: query OSS */
1235 caps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
1236 caps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
1238 caps->dwPrimaryBuffers = 1;
1240 caps->dwMaxHwMixingAllBuffers = 0;
1241 caps->dwMaxHwMixingStaticBuffers = 0;
1242 caps->dwMaxHwMixingStreamingBuffers = 0;
1244 caps->dwFreeHwMixingAllBuffers = 0;
1245 caps->dwFreeHwMixingStaticBuffers = 0;
1246 caps->dwFreeHwMixingStreamingBuffers = 0;
1248 caps->dwMaxHw3DAllBuffers = 0;
1249 caps->dwMaxHw3DStaticBuffers = 0;
1250 caps->dwMaxHw3DStreamingBuffers = 0;
1252 caps->dwFreeHw3DAllBuffers = 0;
1253 caps->dwFreeHw3DStaticBuffers = 0;
1254 caps->dwFreeHw3DStreamingBuffers = 0;
1256 caps->dwTotalHwMemBytes = 0;
1258 caps->dwFreeHwMemBytes = 0;
1260 caps->dwMaxContigFreeHwMemBytes = 0;
1262 caps->dwUnlockTransferRateHwBuffers = 4096; // But we have none...
1264 caps->dwPlayCpuOverheadSwBuffers = 1; // 1%
1266 return 0;
1269 static ULONG WINAPI IDirectSound_AddRef(LPDIRECTSOUND this) {
1270 return ++(this->ref);
1273 static ULONG WINAPI IDirectSound_Release(LPDIRECTSOUND this) {
1274 TRACE(dsound,"(%p), ref was %ld\n",this,this->ref);
1275 if (!--(this->ref)) {
1276 DSOUND_CloseAudio();
1277 while(IDirectSoundBuffer_Release(primarybuf)); // Deallocate
1278 FIXME(dsound, "need to release all buffers!\n");
1279 HeapFree(GetProcessHeap(),0,this);
1280 dsound = NULL;
1281 return 0;
1283 return this->ref;
1286 static HRESULT WINAPI IDirectSound_SetSpeakerConfig(
1287 LPDIRECTSOUND this,DWORD config
1289 FIXME(dsound,"(%p,0x%08lx):stub\n",this,config);
1290 return 0;
1293 static HRESULT WINAPI IDirectSound_QueryInterface(
1294 LPDIRECTSOUND this,REFIID riid,LPVOID *ppobj
1296 char xbuf[50];
1298 if (!memcmp(&IID_IDirectSound3DListener,riid,sizeof(*riid))) {
1300 if (this->listener) {
1301 *ppobj = this->listener;
1302 return DS_OK;
1304 this->listener = (LPDIRECTSOUND3DLISTENER)HeapAlloc(
1305 GetProcessHeap(), 0, sizeof(*(this->listener)));
1306 this->listener->ref = 1;
1307 this->listener->lpvtbl = &ds3dlvt;
1308 this->lpvtbl->fnAddRef(this);
1309 this->listener->ds3dl.dwSize = sizeof(DS3DLISTENER);
1310 this->listener->ds3dl.vPosition.x = 0.0;
1311 this->listener->ds3dl.vPosition.y = 0.0;
1312 this->listener->ds3dl.vPosition.z = 0.0;
1313 this->listener->ds3dl.vVelocity.x = 0.0;
1314 this->listener->ds3dl.vVelocity.y = 0.0;
1315 this->listener->ds3dl.vVelocity.z = 0.0;
1316 this->listener->ds3dl.vOrientFront.x = 0.0;
1317 this->listener->ds3dl.vOrientFront.y = 0.0;
1318 this->listener->ds3dl.vOrientFront.z = 1.0;
1319 this->listener->ds3dl.vOrientTop.x = 0.0;
1320 this->listener->ds3dl.vOrientTop.y = 1.0;
1321 this->listener->ds3dl.vOrientTop.z = 0.0;
1322 this->listener->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1323 this->listener->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1324 this->listener->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1325 *ppobj = (LPVOID)this->listener;
1326 return DS_OK;
1329 WINE_StringFromCLSID(riid,xbuf);
1330 TRACE(dsound,"(%p,%s,%p)\n",this,xbuf,ppobj);
1331 return E_FAIL;
1334 static HRESULT WINAPI IDirectSound_Compact(
1335 LPDIRECTSOUND this)
1337 TRACE(dsound, "(%p)\n", this);
1338 return DS_OK;
1341 static HRESULT WINAPI IDirectSound_GetSpeakerConfig(
1342 LPDIRECTSOUND this,
1343 LPDWORD lpdwSpeakerConfig)
1345 TRACE(dsound, "(%p, %p)\n", this, lpdwSpeakerConfig);
1346 *lpdwSpeakerConfig = DSSPEAKER_STEREO | DSSPEAKER_GEOMETRY_NARROW;
1347 return DS_OK;
1350 static HRESULT WINAPI IDirectSound_Initialize(
1351 LPDIRECTSOUND this,
1352 LPGUID lpGuid)
1354 TRACE(dsound, "(%p, %p)\n", this, lpGuid);
1355 return DS_OK;
1358 static struct tagLPDIRECTSOUND_VTABLE dsvt = {
1359 IDirectSound_QueryInterface,
1360 IDirectSound_AddRef,
1361 IDirectSound_Release,
1362 IDirectSound_CreateSoundBuffer,
1363 IDirectSound_GetCaps,
1364 IDirectSound_DuplicateSoundBuffer,
1365 IDirectSound_SetCooperativeLevel,
1366 IDirectSound_Compact,
1367 IDirectSound_GetSpeakerConfig,
1368 IDirectSound_SetSpeakerConfig,
1369 IDirectSound_Initialize
1372 static int
1373 DSOUND_setformat(LPWAVEFORMATEX wfex) {
1374 int xx,channels,speed,format,nformat;
1376 // Race condition here... called by DSOUND_thread() and SetFormat()
1377 if (!audioOK) {
1378 TRACE(dsound, "(%p) deferred\n", wfex);
1379 return 0;
1381 switch (wfex->wFormatTag) {
1382 default:
1383 WARN(dsound,"unknown WAVE_FORMAT tag %d\n",wfex->wFormatTag);
1384 return DSERR_BADFORMAT;
1385 case WAVE_FORMAT_PCM:
1386 break;
1388 if (wfex->wBitsPerSample==8)
1389 format = AFMT_U8;
1390 else
1391 format = AFMT_S16_LE;
1393 if (-1==ioctl(audiofd,SNDCTL_DSP_GETFMTS,&xx)) {
1394 perror("ioctl SNDCTL_DSP_GETFMTS");
1395 return -1;
1397 if ((xx&format)!=format) {/* format unsupported */
1398 FIXME(dsound,"SNDCTL_DSP_GETFMTS: format not supported\n");
1399 return -1;
1401 nformat = format;
1402 if (-1==ioctl(audiofd,SNDCTL_DSP_SETFMT,&nformat)) {
1403 perror("ioctl SNDCTL_DSP_SETFMT");
1404 return -1;
1406 if (nformat!=format) {/* didn't work */
1407 FIXME(dsound,"SNDCTL_DSP_GETFMTS: format not set\n");
1408 return -1;
1411 channels = wfex->nChannels-1;
1412 if (-1==ioctl(audiofd,SNDCTL_DSP_STEREO,&channels)) {
1413 perror("ioctl SNDCTL_DSP_STEREO");
1414 return -1;
1416 speed = wfex->nSamplesPerSec;
1417 if (-1==ioctl(audiofd,SNDCTL_DSP_SPEED,&speed)) {
1418 perror("ioctl SNDCTL_DSP_SPEED");
1419 return -1;
1421 TRACE(dsound,"(freq=%ld,channels=%d,bits=%d)\n",
1422 wfex->nSamplesPerSec,wfex->nChannels,wfex->wBitsPerSample
1424 return 0;
1427 static void DSOUND_CheckEvent(IDirectSoundBuffer *dsb, int len)
1429 int i;
1430 DWORD offset;
1431 LPDSBPOSITIONNOTIFY event;
1433 if (dsb->nrofnotifies == 0)
1434 return;
1436 TRACE(dsound,"(%p) buflen = %ld, playpos = %ld, len = %d\n",
1437 dsb, dsb->buflen, dsb->playpos, len);
1438 for (i = 0; i < dsb->nrofnotifies ; i++) {
1439 event = dsb->notifies + i;
1440 offset = event->dwOffset;
1441 TRACE(dsound, "checking %d, position %ld, event = %d\n",
1442 i, offset, event->hEventNotify);
1443 // DSBPN_OFFSETSTOP has to be the last element. So this is
1444 // OK. [Inside DirectX, p274]
1446 // This also means we can't sort the entries by offset,
1447 // because DSBPN_OFFSETSTOP == -1
1448 if (offset == DSBPN_OFFSETSTOP) {
1449 if (dsb->playing == 0) {
1450 SetEvent(event->hEventNotify);
1451 TRACE(dsound,"signalled event %d (%d)\n", event->hEventNotify, i);
1452 return;
1453 } else
1454 return;
1456 if ((dsb->playpos + len) >= dsb->buflen) {
1457 if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
1458 (offset >= dsb->playpos)) {
1459 TRACE(dsound,"signalled event %d (%d)\n", event->hEventNotify, i);
1460 SetEvent(event->hEventNotify);
1462 } else {
1463 if ((offset >= dsb->playpos) && (offset < (dsb->playpos + len))) {
1464 TRACE(dsound,"signalled event %d (%d)\n", event->hEventNotify, i);
1465 SetEvent(event->hEventNotify);
1471 // WAV format info can be found at:
1473 // http://www.cwi.nl/ftp/audio/AudioFormats.part2
1474 // ftp://ftp.cwi.nl/pub/audio/RIFF-format
1476 // Import points to remember:
1478 // 8-bit WAV is unsigned
1479 // 16-bit WAV is signed
1481 static inline INT16 cvtU8toS16(BYTE byte)
1483 INT16 s = (byte - 128) << 8;
1485 return s;
1488 static inline BYTE cvtS16toU8(INT16 word)
1490 BYTE b = (word + 32768) >> 8;
1492 return b;
1496 // We should be able to optimize these two inline functions
1497 // so that we aren't doing 8->16->8 conversions when it is
1498 // not necessary. But this is still a WIP. Optimize later.
1499 static inline void get_fields(const IDirectSoundBuffer *dsb, BYTE *buf, INT32 *fl, INT32 *fr)
1501 INT16 *bufs = (INT16 *) buf;
1503 // TRACE(dsound, "(%p)", buf);
1504 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 2) {
1505 *fl = cvtU8toS16(*buf);
1506 *fr = cvtU8toS16(*(buf + 1));
1507 return;
1510 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 2) {
1511 *fl = *bufs;
1512 *fr = *(bufs + 1);
1513 return;
1516 if ((dsb->wfx.wBitsPerSample == 8) && dsb->wfx.nChannels == 1) {
1517 *fl = cvtU8toS16(*buf);
1518 *fr = *fl;
1519 return;
1522 if ((dsb->wfx.wBitsPerSample == 16) && dsb->wfx.nChannels == 1) {
1523 *fl = *bufs;
1524 *fr = *bufs;
1525 return;
1528 FIXME(dsound, "get_fields found an unsupported configuration\n");
1529 return;
1532 static inline void set_fields(BYTE *buf, INT32 fl, INT32 fr)
1534 INT16 *bufs = (INT16 *) buf;
1536 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 2)) {
1537 *buf = cvtS16toU8(fl);
1538 *(buf + 1) = cvtS16toU8(fr);
1539 return;
1542 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 2)) {
1543 *bufs = fl;
1544 *(bufs + 1) = fr;
1545 return;
1548 if ((primarybuf->wfx.wBitsPerSample == 8) && (primarybuf->wfx.nChannels == 1)) {
1549 *buf = cvtS16toU8((fl + fr) >> 1);
1550 return;
1553 if ((primarybuf->wfx.wBitsPerSample == 16) && (primarybuf->wfx.nChannels == 1)) {
1554 *bufs = (fl + fr) >> 1;
1555 return;
1557 FIXME(dsound, "set_fields found an unsupported configuration\n");
1558 return;
1561 // Now with PerfectPitch (tm) technology
1562 static INT32 DSOUND_MixerNorm(IDirectSoundBuffer *dsb, BYTE *buf, INT32 len)
1564 INT32 i, size, ipos, ilen, fieldL, fieldR;
1565 BYTE *ibp, *obp;
1566 INT32 iAdvance = dsb->wfx.nBlockAlign;
1567 INT32 oAdvance = primarybuf->wfx.nBlockAlign;
1569 ibp = dsb->buffer + dsb->playpos;
1570 obp = buf;
1572 TRACE(dsound, "(%p, %p, %p), playpos=%8.8lx\n", dsb, ibp, obp, dsb->playpos);
1573 // Check for the best case
1574 if ((dsb->freq == primarybuf->wfx.nSamplesPerSec) &&
1575 (dsb->wfx.wBitsPerSample == primarybuf->wfx.wBitsPerSample) &&
1576 (dsb->wfx.nChannels == primarybuf->wfx.nChannels)) {
1577 TRACE(dsound, "(%p) Best case\n", dsb);
1578 if ((ibp + len) < (BYTE *)(dsb->buffer + dsb->buflen))
1579 memcpy(obp, ibp, len);
1580 else { // wrap
1581 memcpy(obp, ibp, dsb->buflen - dsb->playpos);
1582 memcpy(obp + (dsb->buflen - dsb->playpos),
1583 dsb->buffer,
1584 len - (dsb->buflen - dsb->playpos));
1586 return len;
1589 // Check for same sample rate
1590 if (dsb->freq == primarybuf->wfx.nSamplesPerSec) {
1591 TRACE(dsound, "(%p) Same sample rate %ld = primary %ld\n", dsb,
1592 dsb->freq, primarybuf->wfx.nSamplesPerSec);
1593 ilen = 0;
1594 for (i = 0; i < len; i += oAdvance) {
1595 get_fields(dsb, ibp, &fieldL, &fieldR);
1596 ibp += iAdvance;
1597 ilen += iAdvance;
1598 set_fields(obp, fieldL, fieldR);
1599 obp += oAdvance;
1600 if (ibp >= (BYTE *)(dsb->buffer + dsb->buflen))
1601 ibp = dsb->buffer; // wrap
1603 return (ilen);
1606 // Mix in different sample rates
1608 // New PerfectPitch(tm) Technology (c) 1998 Rob Riggs
1609 // Patent Pending :-]
1611 TRACE(dsound, "(%p) Adjusting frequency: %ld -> %ld\n",
1612 dsb, dsb->freq, primarybuf->wfx.nSamplesPerSec);
1614 size = len / oAdvance;
1615 ilen = ((size * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance;
1616 for (i = 0; i < size; i++) {
1618 ipos = (((i * dsb->freqAdjust) >> DSOUND_FREQSHIFT) * iAdvance) + dsb->playpos;
1620 if (ipos >= dsb->buflen)
1621 ipos %= dsb->buflen; // wrap
1623 get_fields(dsb, (dsb->buffer + ipos), &fieldL, &fieldR);
1624 set_fields(obp, fieldL, fieldR);
1625 obp += oAdvance;
1627 return ilen;
1630 static void DSOUND_MixerVol(IDirectSoundBuffer *dsb, BYTE *buf, INT32 len)
1632 INT32 i, inc = primarybuf->wfx.wBitsPerSample >> 3;
1633 BYTE *bpc = buf;
1634 INT16 *bps = (INT16 *) buf;
1636 TRACE(dsound, "(%p) left = %lx, right = %lx\n", dsb,
1637 dsb->lVolAdjust, dsb->rVolAdjust);
1638 if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->pan == 0)) &&
1639 (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volume == 0)) &&
1640 !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
1641 return; // Nothing to do
1643 // If we end up with some bozo coder using panning or 3D sound
1644 // with a mono primary buffer, it could sound very weird using
1645 // this method. Oh well, tough patooties.
1647 for (i = 0; i < len; i += inc) {
1648 INT32 val;
1650 switch (inc) {
1652 case 1:
1653 // 8-bit WAV is unsigned, but we need to operate
1654 // on signed data for this to work properly
1655 val = *bpc - 128;
1656 val = ((val * (i & inc ? dsb->rVolAdjust : dsb->lVolAdjust)) >> 15);
1657 *bpc = val + 128;
1658 bpc++;
1659 break;
1660 case 2:
1661 // 16-bit WAV is signed -- much better
1662 val = *bps;
1663 val = ((val * ((i & inc) ? dsb->rVolAdjust : dsb->lVolAdjust)) >> 15);
1664 *bps = val;
1665 bps++;
1666 break;
1667 default:
1668 // Very ugly!
1669 FIXME(dsound, "MixerVol had a nasty error\n");
1674 #ifdef USE_DSOUND3D
1675 static void DSOUND_Mixer3D(IDirectSoundBuffer *dsb, BYTE *buf, INT32 len)
1677 BYTE *ibp, *obp;
1678 DWORD buflen, playpos;
1680 buflen = dsb->ds3db->buflen;
1681 playpos = (dsb->playpos * primarybuf->wfx.nBlockAlign) / dsb->wfx.nBlockAlign;
1682 ibp = dsb->ds3db->buffer + playpos;
1683 obp = buf;
1685 if (playpos > buflen) {
1686 FIXME(dsound, "Major breakage");
1687 return;
1690 if (len <= (playpos + buflen))
1691 memcpy(obp, ibp, len);
1692 else { // wrap
1693 memcpy(obp, ibp, buflen - playpos);
1694 memcpy(obp + (buflen - playpos),
1695 dsb->buffer,
1696 len - (buflen - playpos));
1698 return;
1700 #endif
1702 static DWORD DSOUND_MixInBuffer(IDirectSoundBuffer *dsb)
1704 INT32 i, len, ilen, temp, field;
1705 INT32 advance = primarybuf->wfx.wBitsPerSample >> 3;
1706 BYTE *buf, *ibuf, *obuf;
1707 INT16 *ibufs, *obufs;
1709 len = DSOUND_BUFLEN; // The most we will use
1710 len &= ~3; // 4 byte alignment
1711 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
1712 temp = ((primarybuf->wfx.nAvgBytesPerSec * dsb->buflen) /
1713 dsb->nAvgBytesPerSec) -
1714 ((primarybuf->wfx.nAvgBytesPerSec * dsb->playpos) /
1715 dsb->nAvgBytesPerSec);
1716 len = (len > temp) ? temp : len;
1719 if ((buf = ibuf = (BYTE *) malloc(len)) == NULL)
1720 return 0;
1722 TRACE(dsound, "MixInBuffer (%p) len = %d\n", dsb, len);
1724 ilen = DSOUND_MixerNorm(dsb, ibuf, len);
1725 if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
1726 (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
1727 DSOUND_MixerVol(dsb, ibuf, len);
1729 TRACE(dsound, "Mixing buffer - advance = %d\n", advance);
1730 obuf = primarybuf->buffer + primarybuf->playpos;
1731 for (i = 0; i < len; i += advance) {
1732 obufs = (INT16 *) obuf;
1733 ibufs = (INT16 *) ibuf;
1734 if (primarybuf->wfx.wBitsPerSample == 8) {
1735 field = *ibuf;
1736 field += *obuf;
1737 // 8-bit WAV is unsigned
1738 field = field > 255 ? 255 : field;
1739 *obuf = field;
1740 } else {
1741 field = *ibufs;
1742 field += *obufs;
1743 // 16-bit WAV is signed
1744 field = field > 32767 ? 32767 : field;
1745 field = field < -32768 ? -32768 : field;
1746 *obufs = field;
1748 ibuf += advance;
1749 obuf += advance;
1750 if (obuf >= (BYTE *)(primarybuf->buffer + primarybuf->buflen))
1751 obuf = primarybuf->buffer;
1753 free(buf);
1755 if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY)
1756 DSOUND_CheckEvent(dsb, ilen);
1758 dsb->playpos += ilen;
1759 dsb->writepos = dsb->playpos + ilen;
1761 if (dsb->playpos >= dsb->buflen) {
1762 if (!(dsb->playflags & DSBPLAY_LOOPING)) {
1763 dsb->playing = 0;
1764 dsb->writepos = 0;
1765 dsb->playpos = 0;
1766 DSOUND_CheckEvent(dsb, 0); // For DSBPN_OFFSETSTOP
1767 } else
1768 dsb->playpos %= dsb->buflen; // wrap
1771 if (dsb->writepos >= dsb->buflen)
1772 dsb->writepos %= dsb->buflen;
1774 return len;
1777 static DWORD WINAPI DSOUND_MixPrimary(void)
1779 INT32 i, len, maxlen = 0;
1780 IDirectSoundBuffer *dsb;
1782 for (i = dsound->nrofbuffers - 1; i >= 0; i--) {
1783 dsb = dsound->buffers[i];
1785 if (!dsb || !(dsb->lpvtbl))
1786 continue;
1787 dsb->lpvtbl->fnAddRef(dsb);
1788 if (dsb->buflen && dsb->playing) {
1789 EnterCriticalSection(&(dsb->lock));
1790 len = DSOUND_MixInBuffer(dsb);
1791 maxlen = len > maxlen ? len : maxlen;
1792 LeaveCriticalSection(&(dsb->lock));
1794 dsb->lpvtbl->fnRelease(dsb);
1797 return maxlen;
1800 static int DSOUND_OpenAudio(void)
1802 int audioFragment;
1804 if (primarybuf == NULL)
1805 return DSERR_OUTOFMEMORY;
1807 while (audiofd != -1)
1808 sleep(5);
1809 audiofd = open("/dev/audio",O_WRONLY);
1810 if (audiofd==-1) {
1811 perror("open /dev/audio");
1812 audiofd = -1;
1813 return DSERR_NODRIVER;
1816 audioFragment=0x0002000c;
1817 if (-1==ioctl(audiofd,SNDCTL_DSP_SETFRAGMENT,&audioFragment))
1818 perror("ioctl SETFRAGMENT");
1820 audioOK = 1;
1821 DSOUND_setformat(&(primarybuf->wfx));
1823 return 0;
1826 static void DSOUND_CloseAudio(void)
1828 audioOK = 0; // race condition
1829 Sleep(5);
1830 close(audiofd);
1831 primarybuf->playpos = 0;
1832 primarybuf->writepos = DSOUND_BUFLEN;
1833 memset(primarybuf->buffer, 0, primarybuf->buflen);
1834 audiofd = -1;
1835 TRACE(dsound, "Audio stopped\n");
1838 static int DSOUND_WriteAudio(char *buf, int len)
1840 int result, left = 0;
1842 while (left < len) {
1843 result = write(audiofd, buf + left, len - left);
1844 if (result == -1)
1845 if (errno == EINTR)
1846 continue;
1847 else
1848 return result;
1849 left += result;
1851 return 0;
1854 static DWORD WINAPI DSOUND_thread(LPVOID arg)
1856 int maxlen = DSOUND_BUFLEN;
1857 int len;
1859 TRACE(dsound,"dsound is at pid %d\n",getpid());
1860 while (1) {
1861 if (!dsound) {
1862 WARN(dsound,"DSOUND thread giving up.\n");
1863 ExitThread(0);
1865 if (getppid()==1) {
1866 WARN(dsound,"DSOUND father died? Giving up.\n");
1867 ExitThread(0);
1869 /* RACE: dsound could be deleted */
1870 dsound->lpvtbl->fnAddRef(dsound);
1871 if (primarybuf == NULL) { // Should never happen
1872 /* no soundbuffer yet... wait. */
1873 Sleep(100);
1874 dsound->lpvtbl->fnRelease(dsound);
1875 continue;
1878 // ****
1879 EnterCriticalSection(&(primarybuf->lock));
1880 len = DSOUND_MixPrimary();
1881 LeaveCriticalSection(&(primarybuf->lock));
1882 // ****
1884 if (primarybuf->playing)
1885 len = maxlen > len ? maxlen : len;
1886 if (len) {
1887 // ****
1888 EnterCriticalSection(&(primarybuf->lock));
1890 if (audioOK == 0)
1891 DSOUND_OpenAudio();
1892 if (primarybuf->playpos + len >= primarybuf->buflen) {
1893 if (DSOUND_WriteAudio(
1894 primarybuf->buffer + primarybuf->playpos,
1895 primarybuf->buflen - primarybuf->playpos)
1896 != 0) {
1897 perror("DSOUND_WriteAudio");
1898 ExitThread(0);
1900 memset(primarybuf->buffer + primarybuf->playpos, 0,
1901 primarybuf->buflen - primarybuf->playpos);
1902 if (DSOUND_WriteAudio(primarybuf->buffer,
1903 len - (primarybuf->buflen - primarybuf->playpos)) != 0) {
1904 perror("DSOUND_WriteAudio");
1905 ExitThread(0);
1907 memset(primarybuf->buffer, 0,
1908 len - (primarybuf->buflen - primarybuf->playpos));
1909 } else {
1910 if (DSOUND_WriteAudio(
1911 primarybuf->buffer + primarybuf->playpos,
1912 len) != 0) {
1913 perror("DSOUND_WriteAudio");
1914 ExitThread(0);
1916 memset(primarybuf->buffer + primarybuf->playpos, 0, len);
1918 primarybuf->playpos += len;
1919 if (primarybuf->playpos >= primarybuf->buflen)
1920 primarybuf->playpos %= primarybuf->buflen;
1921 primarybuf->writepos = primarybuf->playpos + maxlen;
1922 if (primarybuf->writepos >= primarybuf->buflen)
1923 primarybuf->writepos %= primarybuf->buflen;
1925 LeaveCriticalSection(&(primarybuf->lock));
1926 // ****
1927 } else {
1928 /* no soundbuffer. close and wait. */
1929 if (audioOK)
1930 DSOUND_CloseAudio();
1931 Sleep(100);
1933 dsound->lpvtbl->fnRelease(dsound);
1935 ExitThread(0);
1938 #endif /* HAVE_OSS */
1940 HRESULT WINAPI DirectSoundCreate(LPGUID lpGUID,LPDIRECTSOUND *ppDS,IUnknown *pUnkOuter )
1942 if (lpGUID)
1943 TRACE(dsound,"(%p,%p,%p)\n",lpGUID,ppDS,pUnkOuter);
1944 else
1945 TRACE(dsound,"DirectSoundCreate (%p)\n", ppDS);
1947 #ifdef HAVE_OSS
1949 if (ppDS == NULL)
1950 return DSERR_INVALIDPARAM;
1952 if (primarybuf) {
1953 dsound->lpvtbl->fnAddRef(dsound);
1954 *ppDS = dsound;
1955 return DS_OK;
1958 *ppDS = (LPDIRECTSOUND)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSound));
1959 if (*ppDS == NULL)
1960 return DSERR_OUTOFMEMORY;
1962 (*ppDS)->ref = 1;
1963 (*ppDS)->lpvtbl = &dsvt;
1964 (*ppDS)->buffers = NULL;
1965 (*ppDS)->nrofbuffers = 0;
1967 (*ppDS)->wfx.wFormatTag = 1;
1968 (*ppDS)->wfx.nChannels = 2;
1969 (*ppDS)->wfx.nSamplesPerSec = 22050;
1970 (*ppDS)->wfx.nAvgBytesPerSec = 44100;
1971 (*ppDS)->wfx.nBlockAlign = 2;
1972 (*ppDS)->wfx.wBitsPerSample = 8;
1974 if (!dsound) {
1975 HANDLE32 hnd;
1976 DWORD xid;
1978 dsound = (*ppDS);
1979 if (primarybuf == NULL) {
1980 DSBUFFERDESC dsbd;
1981 HRESULT hr;
1983 dsbd.dwSize = sizeof(DSBUFFERDESC);
1984 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
1985 dsbd.dwBufferBytes = 0;
1986 dsbd.lpwfxFormat = &(dsound->wfx);
1987 hr = IDirectSound_CreateSoundBuffer(*ppDS, &dsbd, &primarybuf, NULL);
1988 if (hr != DS_OK) return hr;
1989 dsound->primary = primarybuf;
1992 hnd = CreateThread(NULL,0,DSOUND_thread,0,0,&xid);
1994 return DS_OK;
1995 #else
1996 MessageBox32A(0,"DirectSound needs the Open Sound System Driver, which has not been found by ./configure.","WINE DirectSound",MB_OK|MB_ICONSTOP);
1997 return DSERR_NODRIVER;
1998 #endif
2001 /*******************************************************************************
2002 * DirectSound ClassFactory
2004 static HRESULT WINAPI
2005 DSCF_QueryInterface(LPCLASSFACTORY this,REFIID riid,LPVOID *ppobj) {
2006 char buf[80];
2008 if (HIWORD(riid))
2009 WINE_StringFromCLSID(riid,buf);
2010 else
2011 sprintf(buf,"<guid-0x%04x>",LOWORD(riid));
2012 FIXME(dsound,"(%p)->(%s,%p),stub!\n",this,buf,ppobj);
2013 return E_NOINTERFACE;
2016 static ULONG WINAPI
2017 DSCF_AddRef(LPCLASSFACTORY this) {
2018 return ++(this->ref);
2021 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY this) {
2022 /* static class, won't be freed */
2023 return --(this->ref);
2026 static HRESULT WINAPI DSCF_CreateInstance(
2027 LPCLASSFACTORY this,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
2029 char buf[80];
2031 WINE_StringFromCLSID(riid,buf);
2032 TRACE(dsound,"(%p)->(%p,%s,%p)\n",this,pOuter,buf,ppobj);
2033 if (!memcmp(riid,&IID_IDirectSound,sizeof(IID_IDirectSound))) {
2034 /* FIXME: reuse already created dsound if present? */
2035 return DirectSoundCreate(riid,(LPDIRECTSOUND*)ppobj,pOuter);
2037 return E_NOINTERFACE;
2040 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY this,BOOL32 dolock) {
2041 FIXME(dsound,"(%p)->(%d),stub!\n",this,dolock);
2042 return S_OK;
2045 static IClassFactory_VTable DSCF_VTable = {
2046 DSCF_QueryInterface,
2047 DSCF_AddRef,
2048 DSCF_Release,
2049 DSCF_CreateInstance,
2050 DSCF_LockServer
2052 static IClassFactory DSOUND_CF = {&DSCF_VTable, 1 };
2054 /*******************************************************************************
2055 * DllGetClassObject [DSOUND.4]
2056 * Retrieves class object from a DLL object
2058 * NOTES
2059 * Docs say returns STDAPI
2061 * PARAMS
2062 * rclsid [I] CLSID for the class object
2063 * riid [I] Reference to identifier of interface for class object
2064 * ppv [O] Address of variable to receive interface pointer for riid
2066 * RETURNS
2067 * Success: S_OK
2068 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
2069 * E_UNEXPECTED
2071 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
2073 char buf[80],xbuf[80];
2075 if (HIWORD(rclsid))
2076 WINE_StringFromCLSID(rclsid,xbuf);
2077 else
2078 sprintf(xbuf,"<guid-0x%04x>",LOWORD(rclsid));
2079 if (HIWORD(riid))
2080 WINE_StringFromCLSID(riid,buf);
2081 else
2082 sprintf(buf,"<guid-0x%04x>",LOWORD(riid));
2083 WINE_StringFromCLSID(riid,xbuf);
2084 TRACE(dsound, "(%p,%p,%p)\n", xbuf, buf, ppv);
2085 if (!memcmp(riid,&IID_IClassFactory,sizeof(IID_IClassFactory))) {
2086 *ppv = (LPVOID)&DSOUND_CF;
2087 DSOUND_CF.lpvtbl->fnAddRef(&DSOUND_CF);
2088 return S_OK;
2090 FIXME(dsound, "(%p,%p,%p): no interface found.\n", xbuf, buf, ppv);
2091 return E_NOINTERFACE;
2095 /*******************************************************************************
2096 * DllCanUnloadNow [DSOUND.3] Determines whether the DLL is in use.
2098 * RETURNS
2099 * Success: S_OK
2100 * Failure: S_FALSE
2102 DWORD WINAPI DllCanUnloadNow(void)
2104 FIXME(dsound, "(void): stub\n");
2105 return S_FALSE;