3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
26 #include <sys/fcntl.h>
30 #include <math.h> /* Insomnia - pow() function */
40 #include "wine/windef16.h"
41 #include "wine/debug.h"
44 #include "dsound_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
48 static HRESULT
mmErr(UINT err
)
51 case MMSYSERR_NOERROR
:
53 case MMSYSERR_ALLOCATED
:
54 return DSERR_ALLOCATED
;
55 case MMSYSERR_INVALHANDLE
:
56 return DSERR_GENERIC
; /* FIXME */
57 case MMSYSERR_NODRIVER
:
58 return DSERR_NODRIVER
;
60 return DSERR_OUTOFMEMORY
;
61 case MMSYSERR_INVALPARAM
:
62 return DSERR_INVALIDPARAM
;
64 FIXME("Unknown MMSYS error %d\n",err
);
69 void DSOUND_RecalcPrimary(IDirectSoundImpl
*This
)
73 sw
= This
->wfx
.nChannels
* (This
->wfx
.wBitsPerSample
/ 8);
76 /* let fragment size approximate the timer delay */
77 fraglen
= (This
->wfx
.nSamplesPerSec
* DS_TIME_DEL
/ 1000) * sw
;
78 /* reduce fragment size until an integer number of them fits in the buffer */
79 /* (FIXME: this may or may not be a good idea) */
80 while (This
->buflen
% fraglen
) fraglen
-= sw
;
81 This
->fraglen
= fraglen
;
82 TRACE("fraglen=%ld\n", This
->fraglen
);
84 /* calculate the 10ms write lead */
85 This
->writelead
= (This
->wfx
.nSamplesPerSec
/ 100) * sw
;
88 static HRESULT
DSOUND_PrimaryOpen(IDirectSoundImpl
*This
)
92 /* are we using waveOut stuff? */
97 /* Start in pause mode, to allow buffers to get filled */
98 waveOutPause(This
->hwo
);
99 if (This
->state
== STATE_PLAYING
) This
->state
= STATE_STARTING
;
100 else if (This
->state
== STATE_STOPPING
) This
->state
= STATE_STOPPED
;
101 /* use fragments of 10ms (1/100s) each (which should get us within
102 * the documented write cursor lead of 10-15ms) */
103 buflen
= ((This
->wfx
.nAvgBytesPerSec
/ 100) & ~3) * DS_HEL_FRAGS
;
104 TRACE("desired buflen=%ld, old buffer=%p\n", buflen
, This
->buffer
);
105 /* reallocate emulated primary buffer */
106 newbuf
= (LPBYTE
)HeapReAlloc(GetProcessHeap(),0,This
->buffer
,buflen
);
107 if (newbuf
== NULL
) {
108 ERR("failed to allocate primary buffer\n");
109 merr
= DSERR_OUTOFMEMORY
;
110 /* but the old buffer might still exists and must be re-prepared */
112 This
->buffer
= newbuf
;
113 This
->buflen
= buflen
;
118 This
->fraglen
= This
->buflen
/ DS_HEL_FRAGS
;
120 /* prepare fragment headers */
121 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
122 This
->pwave
[c
]->lpData
= This
->buffer
+ c
*This
->fraglen
;
123 This
->pwave
[c
]->dwBufferLength
= This
->fraglen
;
124 This
->pwave
[c
]->dwUser
= (DWORD
)This
;
125 This
->pwave
[c
]->dwFlags
= 0;
126 This
->pwave
[c
]->dwLoops
= 0;
127 err
= mmErr(waveOutPrepareHeader(This
->hwo
,This
->pwave
[c
],sizeof(WAVEHDR
)));
130 waveOutUnprepareHeader(This
->hwo
,This
->pwave
[c
],sizeof(WAVEHDR
));
138 memset(This
->buffer
, (This
->wfx
.wBitsPerSample
== 16) ? 0 : 128, This
->buflen
);
139 TRACE("fraglen=%ld\n", This
->fraglen
);
140 DSOUND_WaveQueue(This
, (DWORD
)-1);
142 if ((err
== DS_OK
) && (merr
!= DS_OK
))
149 static void DSOUND_PrimaryClose(IDirectSoundImpl
*This
)
151 /* are we using waveOut stuff? */
155 This
->pwqueue
= (DWORD
)-1; /* resetting queues */
156 waveOutReset(This
->hwo
);
157 for (c
=0; c
<DS_HEL_FRAGS
; c
++)
158 waveOutUnprepareHeader(This
->hwo
, This
->pwave
[c
], sizeof(WAVEHDR
));
163 HRESULT
DSOUND_PrimaryCreate(IDirectSoundImpl
*This
)
167 This
->buflen
= This
->wfx
.nAvgBytesPerSec
;
169 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
172 err
= IDsDriver_CreateSoundBuffer(This
->driver
,&(This
->wfx
),
173 DSBCAPS_PRIMARYBUFFER
,0,
174 &(This
->buflen
),&(This
->buffer
),
175 (LPVOID
*)&(This
->hwbuf
));
178 err
= DSOUND_PrimaryOpen(This
);
181 /* calculate fragment size and write lead */
182 DSOUND_RecalcPrimary(This
);
183 This
->state
= STATE_STOPPED
;
187 HRESULT
DSOUND_PrimaryDestroy(IDirectSoundImpl
*This
)
189 DSOUND_PrimaryClose(This
);
191 IDsDriverBuffer_Release(This
->hwbuf
);
196 HRESULT
DSOUND_PrimaryPlay(IDirectSoundImpl
*This
)
200 err
= IDsDriverBuffer_Play(This
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
202 err
= mmErr(waveOutRestart(This
->hwo
));
206 HRESULT
DSOUND_PrimaryStop(IDirectSoundImpl
*This
)
213 err
= IDsDriverBuffer_Stop(This
->hwbuf
);
214 if (err
== DSERR_BUFFERLOST
) {
215 /* Wine-only: the driver wants us to reopen the device */
216 /* FIXME: check for errors */
217 IDsDriverBuffer_Release(This
->hwbuf
);
218 waveOutClose(This
->hwo
);
220 err
= mmErr(waveOutOpen(&(This
->hwo
), This
->drvdesc
.dnDevNode
,
221 &(This
->wfx
), (DWORD
)DSOUND_callback
, (DWORD
)This
,
222 CALLBACK_FUNCTION
| WAVE_DIRECTSOUND
));
224 err
= IDsDriver_CreateSoundBuffer(This
->driver
,&(This
->wfx
),
225 DSBCAPS_PRIMARYBUFFER
,0,
226 &(This
->buflen
),&(This
->buffer
),
227 (LPVOID
)&(This
->hwbuf
));
231 err
= mmErr(waveOutPause(This
->hwo
));
235 HRESULT
DSOUND_PrimaryGetPosition(IDirectSoundImpl
*This
, LPDWORD playpos
, LPDWORD writepos
)
238 HRESULT err
=IDsDriverBuffer_GetPosition(This
->hwbuf
,playpos
,writepos
);
244 mtime
.wType
= TIME_BYTES
;
245 waveOutGetPosition(This
->hwo
, &mtime
, sizeof(mtime
));
246 mtime
.u
.cb
= mtime
.u
.cb
% This
->buflen
;
247 *playpos
= mtime
.u
.cb
;
250 /* the writepos should only be used by apps with WRITEPRIMARY priority,
251 * in which case our software mixer is disabled anyway */
252 *writepos
= (This
->pwplay
+ ds_hel_margin
) * This
->fraglen
;
253 while (*writepos
>= This
->buflen
)
254 *writepos
-= This
->buflen
;
257 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, This
, GetTickCount());
262 /*******************************************************************************
265 /* This sets this format for the <em>Primary Buffer Only</em> */
266 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
267 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
268 LPDIRECTSOUNDBUFFER8 iface
,LPWAVEFORMATEX wfex
270 ICOM_THIS(PrimaryBufferImpl
,iface
);
271 IDirectSoundImpl
* dsound
= This
->dsound
;
272 IDirectSoundBufferImpl
** dsb
;
276 if (This
->dsound
->priolevel
== DSSCL_NORMAL
) {
277 TRACE("failed priority check!\n");
278 return DSERR_PRIOLEVELNEEDED
;
281 /* Let's be pedantic! */
282 if ((wfex
== NULL
) ||
283 (wfex
->wFormatTag
!= WAVE_FORMAT_PCM
) ||
284 (wfex
->nChannels
< 1) || (wfex
->nChannels
> 2) ||
285 (wfex
->nSamplesPerSec
< 1) ||
286 (wfex
->nBlockAlign
< 1) || (wfex
->nChannels
> 4) ||
287 ((wfex
->wBitsPerSample
!= 8) && (wfex
->wBitsPerSample
!= 16))) {
288 TRACE("failed pedantic check!\n");
289 return DSERR_INVALIDPARAM
;
293 RtlAcquireResourceExclusive(&(dsound
->lock
), TRUE
);
295 if (dsound
->wfx
.nSamplesPerSec
!= wfex
->nSamplesPerSec
) {
296 dsb
= dsound
->buffers
;
297 for (i
= 0; i
< dsound
->nrofbuffers
; i
++, dsb
++) {
299 EnterCriticalSection(&((*dsb
)->lock
));
301 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
302 wfex
->nSamplesPerSec
;
304 LeaveCriticalSection(&((*dsb
)->lock
));
309 memcpy(&(dsound
->wfx
), wfex
, sizeof(dsound
->wfx
));
311 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
312 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
313 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
314 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
315 wfex
->wBitsPerSample
, wfex
->cbSize
);
317 dsound
->wfx
.nAvgBytesPerSec
=
318 dsound
->wfx
.nSamplesPerSec
* dsound
->wfx
.nBlockAlign
;
320 if (dsound
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
321 /* FIXME: check for errors */
322 DSOUND_PrimaryClose(dsound
);
323 waveOutClose(dsound
->hwo
);
325 err
= mmErr(waveOutOpen(&(dsound
->hwo
), dsound
->drvdesc
.dnDevNode
,
326 &(dsound
->wfx
), (DWORD
)DSOUND_callback
, (DWORD
)dsound
,
327 CALLBACK_FUNCTION
| WAVE_DIRECTSOUND
));
329 DSOUND_PrimaryOpen(dsound
);
332 err
= IDsDriverBuffer_SetFormat(dsound
->hwbuf
, &(dsound
->wfx
));
333 if (err
== DSERR_BUFFERLOST
) {
334 /* Wine-only: the driver wants us to recreate the HW buffer */
335 IDsDriverBuffer_Release(dsound
->hwbuf
);
336 err
= IDsDriver_CreateSoundBuffer(dsound
->driver
,&(dsound
->wfx
),
337 DSBCAPS_PRIMARYBUFFER
,0,
338 &(dsound
->buflen
),&(dsound
->buffer
),
339 (LPVOID
)&(dsound
->hwbuf
));
340 if (dsound
->state
== STATE_PLAYING
) dsound
->state
= STATE_STARTING
;
341 else if (dsound
->state
== STATE_STOPPING
) dsound
->state
= STATE_STOPPED
;
343 /* FIXME: should we set err back to DS_OK in all cases ? */
345 DSOUND_RecalcPrimary(dsound
);
347 RtlReleaseResource(&(dsound
->lock
));
353 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
354 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
356 ICOM_THIS(PrimaryBufferImpl
,iface
);
357 IDirectSoundImpl
* dsound
= This
->dsound
;
360 TRACE("(%p,%ld)\n",This
,vol
);
362 /* I'm not sure if we need this for primary buffer */
363 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
))
364 return DSERR_CONTROLUNAVAIL
;
366 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
))
367 return DSERR_INVALIDPARAM
;
370 EnterCriticalSection(&(dsound
->mixlock
));
372 oldVol
= dsound
->volpan
.lVolume
;
373 dsound
->volpan
.lVolume
= vol
;
374 DSOUND_RecalcVolPan(&dsound
->volpan
);
378 IDsDriverBuffer_SetVolumePan(dsound
->hwbuf
, &(dsound
->volpan
));
381 #if 0 /* should we really do this? */
382 /* the DS volume ranges from 0 (max, 0dB attenuation) to -10000 (min, 100dB attenuation) */
383 /* the MM volume ranges from 0 to 0xffff in an unspecified logarithmic scale */
384 WORD cvol
= 0xffff + vol
*6 + vol
/2;
385 DWORD vol
= cvol
| ((DWORD
)cvol
<< 16)
386 waveOutSetVolume(dsound
->hwo
, vol
);
391 LeaveCriticalSection(&(dsound
->mixlock
));
397 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
398 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
400 ICOM_THIS(PrimaryBufferImpl
,iface
);
401 TRACE("(%p,%p)\n",This
,vol
);
404 return DSERR_INVALIDPARAM
;
406 *vol
= This
->dsound
->volpan
.lVolume
;
410 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
411 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
413 ICOM_THIS(PrimaryBufferImpl
,iface
);
415 TRACE("(%p,%ld)\n",This
,freq
);
417 /* You cannot set the frequency of the primary buffer */
418 return DSERR_CONTROLUNAVAIL
;
421 static HRESULT WINAPI
PrimaryBufferImpl_Play(
422 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
424 ICOM_THIS(PrimaryBufferImpl
,iface
);
425 IDirectSoundImpl
* dsound
= This
->dsound
;
427 TRACE("(%p,%08lx,%08lx,%08lx)\n",
428 This
,reserved1
,reserved2
,flags
431 if (!(flags
& DSBPLAY_LOOPING
))
432 return DSERR_INVALIDPARAM
;
435 EnterCriticalSection(&(dsound
->mixlock
));
437 if (dsound
->state
== STATE_STOPPED
)
438 dsound
->state
= STATE_STARTING
;
439 else if (dsound
->state
== STATE_STOPPING
)
440 dsound
->state
= STATE_PLAYING
;
442 LeaveCriticalSection(&(dsound
->mixlock
));
448 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
450 ICOM_THIS(PrimaryBufferImpl
,iface
);
451 IDirectSoundImpl
* dsound
= This
->dsound
;
453 TRACE("(%p)\n",This
);
456 EnterCriticalSection(&(dsound
->mixlock
));
458 if (dsound
->state
== STATE_PLAYING
)
459 dsound
->state
= STATE_STOPPING
;
460 else if (dsound
->state
== STATE_STARTING
)
461 dsound
->state
= STATE_STOPPED
;
463 LeaveCriticalSection(&(dsound
->mixlock
));
469 static DWORD WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
) {
470 ICOM_THIS(PrimaryBufferImpl
,iface
);
473 TRACE("(%p) ref was %ld, thread is %lx\n",This
, This
->ref
, GetCurrentThreadId());
475 ref
= InterlockedIncrement(&(This
->ref
));
477 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
481 static DWORD WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
) {
482 ICOM_THIS(PrimaryBufferImpl
,iface
);
485 TRACE("(%p) ref was %ld, thread is %lx\n",This
, This
->ref
, GetCurrentThreadId());
487 ref
= InterlockedDecrement(&(This
->ref
));
490 IDirectSound_Release((LPDIRECTSOUND
)This
->dsound
);
494 HeapFree(GetProcessHeap(), 0, This
->iks
);
498 HeapFree(GetProcessHeap(),0,This
);
503 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
504 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
506 ICOM_THIS(PrimaryBufferImpl
,iface
);
507 IDirectSoundImpl
* dsound
= This
->dsound
;
509 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
510 DSOUND_PrimaryGetPosition(dsound
, playpos
, writepos
);
512 if (dsound
->state
!= STATE_STOPPED
)
513 /* apply the documented 10ms lead to writepos */
514 *writepos
+= dsound
->writelead
;
515 while (*writepos
>= dsound
->buflen
) *writepos
-= dsound
->buflen
;
517 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, This
, GetTickCount());
521 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
522 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
524 ICOM_THIS(PrimaryBufferImpl
,iface
);
525 TRACE("(%p,%p), thread is %lx\n",This
,status
,GetCurrentThreadId());
528 return DSERR_INVALIDPARAM
;
531 if ((This
->dsound
->state
== STATE_STARTING
) ||
532 (This
->dsound
->state
== STATE_PLAYING
))
533 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
535 TRACE("status=%lx\n", *status
);
540 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
541 LPDIRECTSOUNDBUFFER8 iface
,LPWAVEFORMATEX lpwf
,DWORD wfsize
,LPDWORD wfwritten
543 ICOM_THIS(PrimaryBufferImpl
,iface
);
544 TRACE("(%p,%p,%ld,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
546 if (wfsize
>sizeof(This
->dsound
->wfx
))
547 wfsize
= sizeof(This
->dsound
->wfx
);
548 if (lpwf
) { /* NULL is valid */
549 memcpy(lpwf
,&(This
->dsound
->wfx
),wfsize
);
554 *wfwritten
= sizeof(This
->dsound
->wfx
);
556 return DSERR_INVALIDPARAM
;
561 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
562 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
564 ICOM_THIS(PrimaryBufferImpl
,iface
);
565 IDirectSoundImpl
* dsound
= This
->dsound
;
567 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
579 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
)
580 return DSERR_PRIOLEVELNEEDED
;
582 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
584 /* GetCurrentPosition does too much magic to duplicate here */
585 IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writepos
);
586 writecursor
+= writepos
;
588 while (writecursor
>= dsound
->buflen
)
589 writecursor
-= dsound
->buflen
;
590 if (flags
& DSBLOCK_ENTIREBUFFER
)
591 writebytes
= dsound
->buflen
;
592 if (writebytes
> dsound
->buflen
)
593 writebytes
= dsound
->buflen
;
595 assert(audiobytes1
!=audiobytes2
);
596 assert(lplpaudioptr1
!=lplpaudioptr2
);
598 if (!(dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && dsound
->hwbuf
) {
599 IDsDriverBuffer_Lock(dsound
->hwbuf
,
600 lplpaudioptr1
, audiobytes1
,
601 lplpaudioptr2
, audiobytes2
,
602 writecursor
, writebytes
,
606 if (writecursor
+writebytes
<= dsound
->buflen
) {
607 *(LPBYTE
*)lplpaudioptr1
= dsound
->buffer
+writecursor
;
608 *audiobytes1
= writebytes
;
610 *(LPBYTE
*)lplpaudioptr2
= NULL
;
613 TRACE("->%ld.0\n",writebytes
);
615 *(LPBYTE
*)lplpaudioptr1
= dsound
->buffer
+writecursor
;
616 *audiobytes1
= dsound
->buflen
-writecursor
;
618 *(LPBYTE
*)lplpaudioptr2
= dsound
->buffer
;
620 *audiobytes2
= writebytes
-(dsound
->buflen
-writecursor
);
621 TRACE("->%ld.%ld\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
627 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
628 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
630 ICOM_THIS(PrimaryBufferImpl
,iface
);
631 TRACE("(%p,%ld)\n",This
,newpos
);
633 /* You cannot set the position of the primary buffer */
634 return DSERR_INVALIDCALL
;
637 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
638 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
640 ICOM_THIS(PrimaryBufferImpl
,iface
);
641 TRACE("(%p,%ld)\n",This
,pan
);
643 /* You cannot set the pan of the primary buffer */
644 return DSERR_CONTROLUNAVAIL
;
647 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
648 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
650 ICOM_THIS(PrimaryBufferImpl
,iface
);
651 TRACE("(%p,%p)\n",This
,pan
);
654 return DSERR_INVALIDPARAM
;
656 *pan
= This
->dsound
->volpan
.lPan
;
661 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
662 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
664 ICOM_THIS(PrimaryBufferImpl
,iface
);
665 IDirectSoundImpl
* dsound
= This
->dsound
;
667 TRACE("(%p,%p,%ld,%p,%ld):stub\n", This
,p1
,x1
,p2
,x2
);
669 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
)
670 return DSERR_PRIOLEVELNEEDED
;
672 if (!(dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && dsound
->hwbuf
) {
673 IDsDriverBuffer_Unlock(dsound
->hwbuf
, p1
, x1
, p2
, x2
);
679 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
680 LPDIRECTSOUNDBUFFER8 iface
682 ICOM_THIS(PrimaryBufferImpl
,iface
);
683 FIXME("(%p):stub\n",This
);
687 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
688 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
690 ICOM_THIS(PrimaryBufferImpl
,iface
);
691 TRACE("(%p,%p)\n",This
,freq
);
694 return DSERR_INVALIDPARAM
;
696 *freq
= This
->dsound
->wfx
.nSamplesPerSec
;
697 TRACE("-> %ld\n", *freq
);
702 static HRESULT WINAPI
PrimaryBufferImpl_SetFX(
703 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
705 ICOM_THIS(PrimaryBufferImpl
,iface
);
708 FIXME("(%p,%lu,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
711 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
713 return DSERR_CONTROLUNAVAIL
;
716 static HRESULT WINAPI
PrimaryBufferImpl_AcquireResources(
717 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
719 ICOM_THIS(PrimaryBufferImpl
,iface
);
722 FIXME("(%p,%08lu,%lu,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
725 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
727 return DSERR_CONTROLUNAVAIL
;
730 static HRESULT WINAPI
PrimaryBufferImpl_GetObjectInPath(
731 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
733 ICOM_THIS(PrimaryBufferImpl
,iface
);
735 FIXME("(%p,%s,%lu,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
737 return DSERR_CONTROLUNAVAIL
;
740 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
741 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND8 dsound
,LPDSBUFFERDESC dbsd
743 ICOM_THIS(PrimaryBufferImpl
,iface
);
744 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
745 DPRINTF("Re-Init!!!\n");
746 return DSERR_ALREADYINITIALIZED
;
749 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
750 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
752 ICOM_THIS(PrimaryBufferImpl
,iface
);
753 TRACE("(%p)->(%p)\n",This
,caps
);
756 return DSERR_INVALIDPARAM
;
758 /* I think we should check this value, not set it. See */
759 /* Inside DirectX, p215. That should apply here, too. */
760 caps
->dwSize
= sizeof(*caps
);
762 caps
->dwFlags
= This
->dsbd
.dwFlags
;
763 if (This
->dsound
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
764 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
766 caps
->dwBufferBytes
= This
->dsound
->buflen
;
768 /* This value represents the speed of the "unlock" command.
769 As unlock is quite fast (it does not do anything), I put
770 4096 ko/s = 4 Mo / s */
771 /* FIXME: hwbuf speed */
772 caps
->dwUnlockTransferRate
= 4096;
773 caps
->dwPlayCpuOverhead
= 0;
778 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
779 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
781 ICOM_THIS(PrimaryBufferImpl
,iface
);
783 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
785 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
786 ERR("app requested IDirectSoundNotify on primary buffer\n");
787 /* should we support this? */
792 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
793 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
795 return E_NOINTERFACE
;
798 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
799 if (!This
->dsound
->listener
)
800 IDirectSound3DListenerImpl_Create(This
, &This
->dsound
->listener
);
801 *ppobj
= This
->dsound
->listener
;
802 if (This
->dsound
->listener
) {
803 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
809 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
812 IKsPropertySetImpl_Create(This
, &This
->iks
);
815 IKsPropertySet_AddRef((LPKSPROPERTYSET
)*ppobj
);
820 FIXME("app requested IKsPropertySet on primary buffer\n");
826 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
830 return E_NOINTERFACE
;
833 static ICOM_VTABLE(IDirectSoundBuffer8
) dspbvt
=
835 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
836 PrimaryBufferImpl_QueryInterface
,
837 PrimaryBufferImpl_AddRef
,
838 PrimaryBufferImpl_Release
,
839 PrimaryBufferImpl_GetCaps
,
840 PrimaryBufferImpl_GetCurrentPosition
,
841 PrimaryBufferImpl_GetFormat
,
842 PrimaryBufferImpl_GetVolume
,
843 PrimaryBufferImpl_GetPan
,
844 PrimaryBufferImpl_GetFrequency
,
845 PrimaryBufferImpl_GetStatus
,
846 PrimaryBufferImpl_Initialize
,
847 PrimaryBufferImpl_Lock
,
848 PrimaryBufferImpl_Play
,
849 PrimaryBufferImpl_SetCurrentPosition
,
850 PrimaryBufferImpl_SetFormat
,
851 PrimaryBufferImpl_SetVolume
,
852 PrimaryBufferImpl_SetPan
,
853 PrimaryBufferImpl_SetFrequency
,
854 PrimaryBufferImpl_Stop
,
855 PrimaryBufferImpl_Unlock
,
856 PrimaryBufferImpl_Restore
,
857 PrimaryBufferImpl_SetFX
,
858 PrimaryBufferImpl_AcquireResources
,
859 PrimaryBufferImpl_GetObjectInPath
862 HRESULT WINAPI
PrimaryBuffer_Create(
863 IDirectSoundImpl
*This
,
864 PrimaryBufferImpl
**pdsb
,
867 PrimaryBufferImpl
*dsb
;
869 if (dsbd
->lpwfxFormat
)
870 return DSERR_INVALIDPARAM
;
872 dsb
= (PrimaryBufferImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
875 ICOM_VTBL(dsb
) = &dspbvt
;
877 memcpy(&dsb
->dsbd
, dsbd
, sizeof(*dsbd
));
879 TRACE("Created primary buffer at %p\n", dsb
);
881 if (dsbd
->dwFlags
& DSBCAPS_CTRL3D
) {
882 /* FIXME: IDirectSound3DListener */
885 IDirectSound8_AddRef((LPDIRECTSOUND8
)This
);