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
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
32 #include "wine/debug.h"
35 #include "dsound_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
39 void DSOUND_RecalcPrimary(IDirectSoundImpl
*This
)
44 nBlockAlign
= This
->pwfx
->nBlockAlign
;
47 /* let fragment size approximate the timer delay */
48 fraglen
= (This
->pwfx
->nSamplesPerSec
* DS_TIME_DEL
/ 1000) * nBlockAlign
;
49 /* reduce fragment size until an integer number of them fits in the buffer */
50 /* (FIXME: this may or may not be a good idea) */
51 while (This
->buflen
% fraglen
) fraglen
-= nBlockAlign
;
52 This
->fraglen
= fraglen
;
53 TRACE("fraglen=%ld\n", This
->fraglen
);
55 /* calculate the 10ms write lead */
56 This
->writelead
= (This
->pwfx
->nSamplesPerSec
/ 100) * nBlockAlign
;
59 static HRESULT
DSOUND_PrimaryOpen(IDirectSoundImpl
*This
)
64 /* are we using waveOut stuff? */
69 /* Start in pause mode, to allow buffers to get filled */
70 waveOutPause(This
->hwo
);
71 if (This
->state
== STATE_PLAYING
) This
->state
= STATE_STARTING
;
72 else if (This
->state
== STATE_STOPPING
) This
->state
= STATE_STOPPED
;
73 /* use fragments of 10ms (1/100s) each (which should get us within
74 * the documented write cursor lead of 10-15ms) */
75 buflen
= ((This
->pwfx
->nSamplesPerSec
/ 100) * This
->pwfx
->nBlockAlign
) * DS_HEL_FRAGS
;
76 TRACE("desired buflen=%ld, old buffer=%p\n", buflen
, This
->buffer
);
77 /* reallocate emulated primary buffer */
80 newbuf
= HeapReAlloc(GetProcessHeap(),0,This
->buffer
,buflen
);
82 newbuf
= HeapAlloc(GetProcessHeap(),0,buflen
);
85 ERR("failed to allocate primary buffer\n");
86 merr
= DSERR_OUTOFMEMORY
;
87 /* but the old buffer might still exist and must be re-prepared */
89 This
->buffer
= newbuf
;
90 This
->buflen
= buflen
;
95 This
->fraglen
= This
->buflen
/ DS_HEL_FRAGS
;
97 /* prepare fragment headers */
98 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
99 This
->pwave
[c
]->lpData
= This
->buffer
+ c
*This
->fraglen
;
100 This
->pwave
[c
]->dwBufferLength
= This
->fraglen
;
101 This
->pwave
[c
]->dwUser
= (DWORD
)This
;
102 This
->pwave
[c
]->dwFlags
= 0;
103 This
->pwave
[c
]->dwLoops
= 0;
104 err
= mmErr(waveOutPrepareHeader(This
->hwo
,This
->pwave
[c
],sizeof(WAVEHDR
)));
107 waveOutUnprepareHeader(This
->hwo
,This
->pwave
[c
],sizeof(WAVEHDR
));
117 FillMemory(This
->buffer
, This
->buflen
, (This
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
118 TRACE("fraglen=%ld\n", This
->fraglen
);
119 DSOUND_WaveQueue(This
, (DWORD
)-1);
121 if ((err
== DS_OK
) && (merr
!= DS_OK
))
123 } else if (!This
->hwbuf
) {
124 err
= IDsDriver_CreateSoundBuffer(This
->driver
,This
->pwfx
,
125 DSBCAPS_PRIMARYBUFFER
,0,
126 &(This
->buflen
),&(This
->buffer
),
127 (LPVOID
*)&(This
->hwbuf
));
129 WARN("IDsDriver_CreateSoundBuffer failed\n");
133 if (dsound
->state
== STATE_PLAYING
) dsound
->state
= STATE_STARTING
;
134 else if (dsound
->state
== STATE_STOPPING
) dsound
->state
= STATE_STOPPED
;
141 static void DSOUND_PrimaryClose(IDirectSoundImpl
*This
)
143 TRACE("(%p)\n",This
);
145 /* are we using waveOut stuff? */
149 This
->pwqueue
= (DWORD
)-1; /* resetting queues */
150 waveOutReset(This
->hwo
);
151 for (c
=0; c
<DS_HEL_FRAGS
; c
++)
152 waveOutUnprepareHeader(This
->hwo
, This
->pwave
[c
], sizeof(WAVEHDR
));
155 if (IDsDriverBuffer_Release(This
->hwbuf
) == 0)
160 HRESULT
DSOUND_PrimaryCreate(IDirectSoundImpl
*This
)
163 TRACE("(%p)\n",This
);
165 This
->buflen
= This
->pwfx
->nAvgBytesPerSec
;
167 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
170 err
= IDsDriver_CreateSoundBuffer(This
->driver
,This
->pwfx
,
171 DSBCAPS_PRIMARYBUFFER
,0,
172 &(This
->buflen
),&(This
->buffer
),
173 (LPVOID
*)&(This
->hwbuf
));
175 WARN("IDsDriver_CreateSoundBuffer failed\n");
180 /* Allocate memory for HEL buffer headers */
182 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
183 This
->pwave
[c
] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(WAVEHDR
));
184 if (!This
->pwave
[c
]) {
185 /* Argh, out of memory */
187 HeapFree(GetProcessHeap(),0,This
->pwave
[c
]);
189 WARN("out of memory\n");
190 return DSERR_OUTOFMEMORY
;
195 err
= DSOUND_PrimaryOpen(This
);
198 WARN("DSOUND_PrimaryOpen failed\n");
202 /* calculate fragment size and write lead */
203 DSOUND_RecalcPrimary(This
);
204 This
->state
= STATE_STOPPED
;
208 HRESULT
DSOUND_PrimaryDestroy(IDirectSoundImpl
*This
)
210 TRACE("(%p)\n",This
);
212 DSOUND_PrimaryClose(This
);
215 if (IDsDriverBuffer_Release(This
->hwbuf
) == 0)
220 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
221 HeapFree(GetProcessHeap(),0,This
->pwave
[c
]);
224 HeapFree(GetProcessHeap(),0,This
->pwfx
);
229 HRESULT
DSOUND_PrimaryPlay(IDirectSoundImpl
*This
)
232 TRACE("(%p)\n",This
);
235 err
= IDsDriverBuffer_Play(This
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
237 WARN("IDsDriverBuffer_Play failed\n");
239 err
= mmErr(waveOutRestart(This
->hwo
));
241 WARN("waveOutRestart failed\n");
247 HRESULT
DSOUND_PrimaryStop(IDirectSoundImpl
*This
)
250 TRACE("(%p)\n",This
);
253 err
= IDsDriverBuffer_Stop(This
->hwbuf
);
254 if (err
== DSERR_BUFFERLOST
) {
255 DWORD flags
= CALLBACK_FUNCTION
;
256 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
257 flags
|= WAVE_DIRECTSOUND
;
258 /* Wine-only: the driver wants us to reopen the device */
259 /* FIXME: check for errors */
260 IDsDriverBuffer_Release(This
->hwbuf
);
261 waveOutClose(This
->hwo
);
263 err
= mmErr(waveOutOpen(&(This
->hwo
), This
->drvdesc
.dnDevNode
,
264 This
->pwfx
, (DWORD
)DSOUND_callback
, (DWORD
)This
,
267 err
= IDsDriver_CreateSoundBuffer(This
->driver
,This
->pwfx
,
268 DSBCAPS_PRIMARYBUFFER
,0,
269 &(This
->buflen
),&(This
->buffer
),
270 (LPVOID
)&(This
->hwbuf
));
272 WARN("IDsDriver_CreateSoundBuffer failed\n");
274 WARN("waveOutOpen failed\n");
276 } else if (err
!= DS_OK
) {
277 WARN("IDsDriverBuffer_Stop failed\n");
280 err
= mmErr(waveOutPause(This
->hwo
));
282 WARN("waveOutPause failed\n");
287 HRESULT
DSOUND_PrimaryGetPosition(IDirectSoundImpl
*This
, LPDWORD playpos
, LPDWORD writepos
)
289 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
292 HRESULT err
=IDsDriverBuffer_GetPosition(This
->hwbuf
,playpos
,writepos
);
294 WARN("IDsDriverBuffer_GetPosition failed\n");
300 mtime
.wType
= TIME_BYTES
;
301 waveOutGetPosition(This
->hwo
, &mtime
, sizeof(mtime
));
302 mtime
.u
.cb
= mtime
.u
.cb
% This
->buflen
;
303 *playpos
= mtime
.u
.cb
;
306 /* the writepos should only be used by apps with WRITEPRIMARY priority,
307 * in which case our software mixer is disabled anyway */
308 *writepos
= (This
->pwplay
+ ds_hel_margin
) * This
->fraglen
;
309 while (*writepos
>= This
->buflen
)
310 *writepos
-= This
->buflen
;
313 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, This
, GetTickCount());
317 /*******************************************************************************
320 /* This sets this format for the <em>Primary Buffer Only</em> */
321 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
322 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
323 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
325 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
326 IDirectSoundImpl
* dsound
= This
->dsound
;
328 int i
, alloc_size
, cp_size
;
329 DWORD nSamplesPerSec
;
330 TRACE("(%p,%p)\n",This
,wfex
);
332 if (This
->dsound
->priolevel
== DSSCL_NORMAL
) {
333 WARN("failed priority check!\n");
334 return DSERR_PRIOLEVELNEEDED
;
337 /* Let's be pedantic! */
339 WARN("invalid parameter: wfex==NULL!\n");
340 return DSERR_INVALIDPARAM
;
342 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
343 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
344 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
345 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
346 wfex
->wBitsPerSample
, wfex
->cbSize
);
349 RtlAcquireResourceExclusive(&(dsound
->buffer_list_lock
), TRUE
);
350 EnterCriticalSection(&(dsound
->mixlock
));
352 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
353 alloc_size
= sizeof(WAVEFORMATEX
);
354 cp_size
= sizeof(PCMWAVEFORMAT
);
356 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
358 dsound
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,dsound
->pwfx
,alloc_size
);
360 nSamplesPerSec
= dsound
->pwfx
->nSamplesPerSec
;
362 CopyMemory(dsound
->pwfx
, wfex
, cp_size
);
364 if (dsound
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
365 DWORD flags
= CALLBACK_FUNCTION
;
366 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
367 flags
|= WAVE_DIRECTSOUND
;
368 /* FIXME: check for errors */
369 DSOUND_PrimaryClose(dsound
);
370 waveOutClose(dsound
->hwo
);
372 err
= mmErr(waveOutOpen(&(dsound
->hwo
), dsound
->drvdesc
.dnDevNode
,
373 dsound
->pwfx
, (DWORD
)DSOUND_callback
, (DWORD
)dsound
,
376 err
= DSOUND_PrimaryOpen(dsound
);
378 WARN("DSOUND_PrimaryOpen failed\n");
382 WARN("waveOutOpen failed\n");
385 } else if (dsound
->hwbuf
) {
386 err
= IDsDriverBuffer_SetFormat(dsound
->hwbuf
, dsound
->pwfx
);
387 if (err
== DSERR_BUFFERLOST
) {
388 /* Wine-only: the driver wants us to recreate the HW buffer */
389 IDsDriverBuffer_Release(dsound
->hwbuf
);
390 err
= IDsDriver_CreateSoundBuffer(dsound
->driver
,dsound
->pwfx
,
391 DSBCAPS_PRIMARYBUFFER
,0,
392 &(dsound
->buflen
),&(dsound
->buffer
),
393 (LPVOID
)&(dsound
->hwbuf
));
395 WARN("IDsDriver_CreateSoundBuffer failed\n");
398 if (dsound
->state
== STATE_PLAYING
) dsound
->state
= STATE_STARTING
;
399 else if (dsound
->state
== STATE_STOPPING
) dsound
->state
= STATE_STOPPED
;
401 WARN("IDsDriverBuffer_SetFormat failed\n");
404 /* FIXME: should we set err back to DS_OK in all cases ? */
406 DSOUND_RecalcPrimary(dsound
);
408 if (nSamplesPerSec
!= dsound
->pwfx
->nSamplesPerSec
) {
409 IDirectSoundBufferImpl
** dsb
= dsound
->buffers
;
410 for (i
= 0; i
< dsound
->nrofbuffers
; i
++, dsb
++) {
412 EnterCriticalSection(&((*dsb
)->lock
));
414 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
415 wfex
->nSamplesPerSec
;
417 LeaveCriticalSection(&((*dsb
)->lock
));
423 LeaveCriticalSection(&(dsound
->mixlock
));
424 RtlReleaseResource(&(dsound
->buffer_list_lock
));
430 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
431 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
433 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
434 IDirectSoundImpl
* dsound
= This
->dsound
;
437 HRESULT hres
= DS_OK
;
439 TRACE("(%p,%ld)\n",This
,vol
);
441 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
442 WARN("control unavailable\n");
443 return DSERR_CONTROLUNAVAIL
;
446 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
447 WARN("invalid parameter: vol = %ld\n", vol
);
448 return DSERR_INVALIDPARAM
;
452 EnterCriticalSection(&(dsound
->mixlock
));
454 waveOutGetVolume(dsound
->hwo
, &factors
);
455 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
456 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
457 DSOUND_AmpFactorToVolPan(&volpan
);
458 if (vol
!= volpan
.lVolume
) {
460 DSOUND_RecalcVolPan(&volpan
);
462 hres
= IDsDriverBuffer_SetVolumePan(dsound
->hwbuf
, &volpan
);
464 WARN("IDsDriverBuffer_SetVolumePan failed\n");
466 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
467 waveOutSetVolume(dsound
->hwo
, ampfactors
);
471 LeaveCriticalSection(&(dsound
->mixlock
));
477 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
478 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
480 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
483 TRACE("(%p,%p)\n",This
,vol
);
485 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
486 WARN("control unavailable\n");
487 return DSERR_CONTROLUNAVAIL
;
491 WARN("invalid parameter: vol = NULL\n");
492 return DSERR_INVALIDPARAM
;
495 waveOutGetVolume(dsound
->hwo
, &factors
);
496 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
497 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
498 DSOUND_AmpFactorToVolPan(&volpan
);
499 *vol
= volpan
.lVolume
;
503 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
504 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
506 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
508 TRACE("(%p,%ld)\n",This
,freq
);
510 /* You cannot set the frequency of the primary buffer */
511 WARN("control unavailable\n");
512 return DSERR_CONTROLUNAVAIL
;
515 static HRESULT WINAPI
PrimaryBufferImpl_Play(
516 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
518 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
519 IDirectSoundImpl
* dsound
= This
->dsound
;
521 TRACE("(%p,%08lx,%08lx,%08lx)\n",
522 This
,reserved1
,reserved2
,flags
525 if (!(flags
& DSBPLAY_LOOPING
)) {
526 WARN("invalid parameter: flags = %08lx\n", flags
);
527 return DSERR_INVALIDPARAM
;
531 EnterCriticalSection(&(dsound
->mixlock
));
533 if (dsound
->state
== STATE_STOPPED
)
534 dsound
->state
= STATE_STARTING
;
535 else if (dsound
->state
== STATE_STOPPING
)
536 dsound
->state
= STATE_PLAYING
;
538 LeaveCriticalSection(&(dsound
->mixlock
));
544 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
546 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
547 IDirectSoundImpl
* dsound
= This
->dsound
;
549 TRACE("(%p)\n",This
);
552 EnterCriticalSection(&(dsound
->mixlock
));
554 if (dsound
->state
== STATE_PLAYING
)
555 dsound
->state
= STATE_STOPPING
;
556 else if (dsound
->state
== STATE_STARTING
)
557 dsound
->state
= STATE_STOPPED
;
559 LeaveCriticalSection(&(dsound
->mixlock
));
565 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
567 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
568 ULONG ref
= InterlockedIncrement(&(This
->ref
));
569 TRACE("(%p) ref was %ld\n", This
, ref
- 1);
573 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
575 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
576 DWORD ref
= InterlockedDecrement(&(This
->ref
));
577 TRACE("(%p) ref was %ld\n", This
, ref
+ 1);
580 This
->dsound
->primary
= NULL
;
581 HeapFree(GetProcessHeap(), 0, This
);
582 TRACE("(%p) released\n", This
);
587 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
588 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
591 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
592 IDirectSoundImpl
* dsound
= This
->dsound
;
594 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
595 hres
= DSOUND_PrimaryGetPosition(dsound
, playpos
, writepos
);
597 WARN("DSOUND_PrimaryGetPosition failed\n");
601 if (dsound
->state
!= STATE_STOPPED
)
602 /* apply the documented 10ms lead to writepos */
603 *writepos
+= dsound
->writelead
;
604 while (*writepos
>= dsound
->buflen
) *writepos
-= dsound
->buflen
;
606 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, This
, GetTickCount());
610 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
611 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
613 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
614 TRACE("(%p,%p), thread is %04lx\n",This
,status
,GetCurrentThreadId());
616 if (status
== NULL
) {
617 WARN("invalid parameter: status == NULL\n");
618 return DSERR_INVALIDPARAM
;
622 if ((This
->dsound
->state
== STATE_STARTING
) ||
623 (This
->dsound
->state
== STATE_PLAYING
))
624 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
626 TRACE("status=%lx\n", *status
);
631 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
632 LPDIRECTSOUNDBUFFER8 iface
,
638 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
639 TRACE("(%p,%p,%ld,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
641 size
= sizeof(WAVEFORMATEX
) + This
->dsound
->pwfx
->cbSize
;
643 if (lpwf
) { /* NULL is valid */
644 if (wfsize
>= size
) {
645 CopyMemory(lpwf
,This
->dsound
->pwfx
,size
);
649 WARN("invalid parameter: wfsize to small\n");
652 return DSERR_INVALIDPARAM
;
656 *wfwritten
= sizeof(WAVEFORMATEX
) + This
->dsound
->pwfx
->cbSize
;
658 WARN("invalid parameter: wfwritten == NULL\n");
659 return DSERR_INVALIDPARAM
;
666 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
667 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
669 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
670 IDirectSoundImpl
* dsound
= This
->dsound
;
672 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
684 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
) {
685 WARN("failed priority check!\n");
686 return DSERR_PRIOLEVELNEEDED
;
689 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
692 /* GetCurrentPosition does too much magic to duplicate here */
693 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writepos
);
695 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
698 writecursor
+= writepos
;
700 while (writecursor
>= dsound
->buflen
)
701 writecursor
-= dsound
->buflen
;
702 if (flags
& DSBLOCK_ENTIREBUFFER
)
703 writebytes
= dsound
->buflen
;
704 if (writebytes
> dsound
->buflen
)
705 writebytes
= dsound
->buflen
;
707 if (!(dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && dsound
->hwbuf
) {
709 hres
= IDsDriverBuffer_Lock(dsound
->hwbuf
,
710 lplpaudioptr1
, audiobytes1
,
711 lplpaudioptr2
, audiobytes2
,
712 writecursor
, writebytes
,
715 WARN("IDsDriverBuffer_Lock failed\n");
719 if (writecursor
+writebytes
<= dsound
->buflen
) {
720 *(LPBYTE
*)lplpaudioptr1
= dsound
->buffer
+writecursor
;
721 *audiobytes1
= writebytes
;
723 *(LPBYTE
*)lplpaudioptr2
= NULL
;
726 TRACE("->%ld.0\n",writebytes
);
728 *(LPBYTE
*)lplpaudioptr1
= dsound
->buffer
+writecursor
;
729 *audiobytes1
= dsound
->buflen
-writecursor
;
731 *(LPBYTE
*)lplpaudioptr2
= dsound
->buffer
;
733 *audiobytes2
= writebytes
-(dsound
->buflen
-writecursor
);
734 TRACE("->%ld.%ld\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
740 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
741 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
743 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
744 TRACE("(%p,%ld)\n",This
,newpos
);
746 /* You cannot set the position of the primary buffer */
747 WARN("invalid call\n");
748 return DSERR_INVALIDCALL
;
751 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
752 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
754 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
755 IDirectSoundImpl
* dsound
= This
->dsound
;
758 HRESULT hres
= DS_OK
;
760 TRACE("(%p,%ld)\n",This
,pan
);
762 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
763 WARN("control unavailable\n");
764 return DSERR_CONTROLUNAVAIL
;
767 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
768 WARN("invalid parameter: pan = %ld\n", pan
);
769 return DSERR_INVALIDPARAM
;
773 EnterCriticalSection(&(dsound
->mixlock
));
775 waveOutGetVolume(dsound
->hwo
, &factors
);
776 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
777 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
778 DSOUND_AmpFactorToVolPan(&volpan
);
779 if (pan
!= volpan
.lPan
) {
781 DSOUND_RecalcVolPan(&volpan
);
783 hres
= IDsDriverBuffer_SetVolumePan(dsound
->hwbuf
, &volpan
);
785 WARN("IDsDriverBuffer_SetVolumePan failed\n");
787 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
788 waveOutSetVolume(dsound
->hwo
, ampfactors
);
792 LeaveCriticalSection(&(dsound
->mixlock
));
798 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
799 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
801 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
804 TRACE("(%p,%p)\n",This
,pan
);
806 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
807 WARN("control unavailable\n");
808 return DSERR_CONTROLUNAVAIL
;
812 WARN("invalid parameter: pan == NULL\n");
813 return DSERR_INVALIDPARAM
;
816 waveOutGetVolume(dsound
->hwo
, &factors
);
817 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
818 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
819 DSOUND_AmpFactorToVolPan(&volpan
);
824 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
825 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
827 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
828 IDirectSoundImpl
* dsound
= This
->dsound
;
830 TRACE("(%p,%p,%ld,%p,%ld)\n", This
,p1
,x1
,p2
,x2
);
832 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
) {
833 WARN("failed priority check!\n");
834 return DSERR_PRIOLEVELNEEDED
;
837 if (!(dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && dsound
->hwbuf
) {
840 hres
= IDsDriverBuffer_Unlock(dsound
->hwbuf
, p1
, x1
, p2
, x2
);
842 WARN("IDsDriverBuffer_Unlock failed\n");
850 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
851 LPDIRECTSOUNDBUFFER8 iface
853 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
854 FIXME("(%p):stub\n",This
);
858 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
859 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
861 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
862 TRACE("(%p,%p)\n",This
,freq
);
865 WARN("invalid parameter: freq == NULL\n");
866 return DSERR_INVALIDPARAM
;
869 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
870 WARN("control unavailable\n");
871 return DSERR_CONTROLUNAVAIL
;
874 *freq
= This
->dsound
->pwfx
->nSamplesPerSec
;
875 TRACE("-> %ld\n", *freq
);
880 static HRESULT WINAPI
PrimaryBufferImpl_SetFX(
881 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
883 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
886 FIXME("(%p,%lu,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
889 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
891 WARN("control unavailable\n");
892 return DSERR_CONTROLUNAVAIL
;
895 static HRESULT WINAPI
PrimaryBufferImpl_AcquireResources(
896 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
898 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
901 FIXME("(%p,%08lu,%lu,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
904 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
906 WARN("control unavailable\n");
907 return DSERR_CONTROLUNAVAIL
;
910 static HRESULT WINAPI
PrimaryBufferImpl_GetObjectInPath(
911 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
913 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
915 FIXME("(%p,%s,%lu,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
917 WARN("control unavailable\n");
918 return DSERR_CONTROLUNAVAIL
;
921 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
922 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
924 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
925 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
926 DPRINTF("Re-Init!!!\n");
927 WARN("already initialized\n");
928 return DSERR_ALREADYINITIALIZED
;
931 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
932 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
934 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
935 TRACE("(%p)->(%p)\n",This
,caps
);
938 WARN("invalid parameter: caps == NULL\n");
939 return DSERR_INVALIDPARAM
;
942 if (caps
->dwSize
< sizeof(*caps
)) {
943 WARN("invalid parameter: caps->dwSize = %ld: < %d\n", caps
->dwSize
, sizeof(*caps
));
944 return DSERR_INVALIDPARAM
;
947 caps
->dwFlags
= This
->dsound
->dsbd
.dwFlags
;
948 if (This
->dsound
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
949 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
951 caps
->dwBufferBytes
= This
->dsound
->buflen
;
953 /* This value represents the speed of the "unlock" command.
954 As unlock is quite fast (it does not do anything), I put
955 4096 ko/s = 4 Mo / s */
956 /* FIXME: hwbuf speed */
957 caps
->dwUnlockTransferRate
= 4096;
958 caps
->dwPlayCpuOverhead
= 0;
963 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
964 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
966 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
967 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
970 WARN("invalid parameter\n");
974 *ppobj
= NULL
; /* assume failure */
976 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
977 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
978 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
983 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
984 /* a primary buffer can't have a DirectSoundBuffer8 interface */
985 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
986 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
987 return E_NOINTERFACE
;
990 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
991 ERR("app requested IDirectSoundNotify on primary buffer\n");
992 /* FIXME: should we support this? */
993 return E_NOINTERFACE
;
996 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
997 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
998 return E_NOINTERFACE
;
1001 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1002 if (!This
->dsound
->listener
)
1003 IDirectSound3DListenerImpl_Create(This
, &This
->dsound
->listener
);
1004 if (This
->dsound
->listener
) {
1005 *ppobj
= This
->dsound
->listener
;
1006 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1010 WARN("IID_IDirectSound3DListener failed\n");
1011 return E_NOINTERFACE
;
1014 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1015 FIXME("app requested IKsPropertySet on primary buffer\n");
1016 return E_NOINTERFACE
;
1019 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1020 return E_NOINTERFACE
;
1023 static IDirectSoundBuffer8Vtbl dspbvt
=
1025 PrimaryBufferImpl_QueryInterface
,
1026 PrimaryBufferImpl_AddRef
,
1027 PrimaryBufferImpl_Release
,
1028 PrimaryBufferImpl_GetCaps
,
1029 PrimaryBufferImpl_GetCurrentPosition
,
1030 PrimaryBufferImpl_GetFormat
,
1031 PrimaryBufferImpl_GetVolume
,
1032 PrimaryBufferImpl_GetPan
,
1033 PrimaryBufferImpl_GetFrequency
,
1034 PrimaryBufferImpl_GetStatus
,
1035 PrimaryBufferImpl_Initialize
,
1036 PrimaryBufferImpl_Lock
,
1037 PrimaryBufferImpl_Play
,
1038 PrimaryBufferImpl_SetCurrentPosition
,
1039 PrimaryBufferImpl_SetFormat
,
1040 PrimaryBufferImpl_SetVolume
,
1041 PrimaryBufferImpl_SetPan
,
1042 PrimaryBufferImpl_SetFrequency
,
1043 PrimaryBufferImpl_Stop
,
1044 PrimaryBufferImpl_Unlock
,
1045 PrimaryBufferImpl_Restore
,
1046 PrimaryBufferImpl_SetFX
,
1047 PrimaryBufferImpl_AcquireResources
,
1048 PrimaryBufferImpl_GetObjectInPath
1051 HRESULT WINAPI
PrimaryBufferImpl_Create(
1052 IDirectSoundImpl
*ds
,
1053 PrimaryBufferImpl
**pdsb
,
1054 LPCDSBUFFERDESC dsbd
)
1056 PrimaryBufferImpl
*dsb
;
1058 TRACE("%p,%p,%p)\n",ds
,pdsb
,dsbd
);
1060 if (dsbd
->lpwfxFormat
) {
1061 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1063 return DSERR_INVALIDPARAM
;
1066 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1069 WARN("out of memory\n");
1071 return DSERR_OUTOFMEMORY
;
1076 dsb
->lpVtbl
= &dspbvt
;
1078 CopyMemory(&ds
->dsbd
, dsbd
, sizeof(*dsbd
));
1080 TRACE("Created primary buffer at %p\n", dsb
);
1081 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1082 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1083 ds
->pwfx
->wFormatTag
, ds
->pwfx
->nChannels
, ds
->pwfx
->nSamplesPerSec
,
1084 ds
->pwfx
->nAvgBytesPerSec
, ds
->pwfx
->nBlockAlign
,
1085 ds
->pwfx
->wBitsPerSample
, ds
->pwfx
->cbSize
);