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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
32 #include "wine/debug.h"
35 #include "dsound_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
39 static void DSOUND_RecalcPrimary(DirectSoundDevice
*device
)
43 TRACE("(%p)\n", device
);
45 nBlockAlign
= device
->pwfx
->nBlockAlign
;
46 /* Alsa doesn't have continuous buffers, instead it has buffers with power of 2,
47 * If DS_TIME_DEL is about 10 ms, 512 * nBlockAlign is roughly correct */
48 fraglen
= 512 * nBlockAlign
;
50 /* Compensate for only being roughly accurate */
51 if (device
->pwfx
->nSamplesPerSec
<= 26000)
54 if (device
->pwfx
->nSamplesPerSec
<= 12000)
57 if (device
->pwfx
->nSamplesPerSec
>= 80000)
60 /* If in emulation mode, reduce fragment size until an integer number of them fits in the buffer */
62 while (device
->buflen
% fraglen
)
63 fraglen
-= nBlockAlign
;
64 device
->fraglen
= fraglen
;
65 device
->helfrags
= device
->buflen
/ fraglen
;
66 TRACE("fraglen=%d helfrags=%d\n", device
->fraglen
, device
->helfrags
);
68 if (device
->hwbuf
&& device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDWRITELEAD
)
69 device
->writelead
= 0;
71 /* calculate the 10ms write lead */
72 device
->writelead
= (device
->pwfx
->nSamplesPerSec
/ 100) * nBlockAlign
;
75 static HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
78 TRACE("(%p)\n", device
);
80 /* are we using waveOut stuff? */
81 if (!device
->driver
) {
83 LPWAVEHDR headers
= NULL
;
86 /* Start in pause mode, to allow buffers to get filled */
87 waveOutPause(device
->hwo
);
88 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
89 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
91 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
92 on windows this size is always fixed (tested on win-xp) */
93 buflen
= ds_hel_buflen
;
94 buflen
-= ds_hel_buflen
% device
->pwfx
->nBlockAlign
;
96 TRACE("desired buflen=%d, old buffer=%p\n", buflen
, device
->buffer
);
98 /* reallocate emulated primary buffer */
100 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
,buflen
);
102 newbuf
= HeapAlloc(GetProcessHeap(),0,buflen
);
105 if (newbuf
== NULL
) {
106 ERR("failed to allocate primary buffer\n");
107 merr
= DSERR_OUTOFMEMORY
;
108 /* but the old buffer might still exist and must be re-prepared */
112 device
->buffer
= newbuf
;
113 device
->buflen
= buflen
;
116 DSOUND_RecalcPrimary(device
);
118 headers
= HeapReAlloc(GetProcessHeap(),0,device
->pwave
, device
->helfrags
* sizeof(WAVEHDR
));
120 headers
= HeapAlloc(GetProcessHeap(),0,device
->helfrags
* sizeof(WAVEHDR
));
123 ERR("failed to allocate wave headers\n");
124 merr
= DSERR_OUTOFMEMORY
;
126 else if (device
->buffer
) {
129 device
->pwave
= headers
;
132 if(device
->buflen
% device
->helfrags
){
133 ERR("Bad helfrags resolution\n");
136 /* prepare fragment headers */
137 for (c
=0; c
<device
->helfrags
; c
++) {
138 device
->pwave
[c
].lpData
= (char*)device
->buffer
+ c
*device
->fraglen
;
139 device
->pwave
[c
].dwBufferLength
= device
->fraglen
;
140 device
->pwave
[c
].dwUser
= (DWORD
)device
;
141 device
->pwave
[c
].dwFlags
= 0;
142 device
->pwave
[c
].dwLoops
= 0;
143 err
= mmErr(waveOutPrepareHeader(device
->hwo
,&device
->pwave
[c
],sizeof(WAVEHDR
)));
146 waveOutUnprepareHeader(device
->hwo
,&device
->pwave
[c
],sizeof(WAVEHDR
));
155 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
156 TRACE("fraglen=%d\n", device
->fraglen
);
158 if ((err
== DS_OK
) && (merr
!= DS_OK
))
160 } else if (!device
->hwbuf
) {
163 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
164 DSBCAPS_PRIMARYBUFFER
,0,
165 &(device
->buflen
),&(device
->buffer
),
166 (LPVOID
*)&(device
->hwbuf
));
168 WARN("IDsDriver_CreateSoundBuffer failed\n");
172 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
173 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
176 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
178 DSOUND_RecalcPrimary(device
);
184 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
186 TRACE("(%p)\n", device
);
188 /* are we using waveOut stuff? */
189 if (!device
->hwbuf
) {
192 /* get out of CS when calling the wave system */
193 LeaveCriticalSection(&(device
->mixlock
));
195 device
->pwqueue
= (DWORD
)-1; /* resetting queues */
196 waveOutReset(device
->hwo
);
197 for (c
=0; c
<device
->helfrags
; c
++)
198 waveOutUnprepareHeader(device
->hwo
, &device
->pwave
[c
], sizeof(WAVEHDR
));
200 EnterCriticalSection(&(device
->mixlock
));
202 /* clear the queue */
205 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
210 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
213 TRACE("(%p)\n", device
);
217 device
->buflen
= device
->pwfx
->nAvgBytesPerSec
;
219 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
221 if (device
->driver
) {
222 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
223 DSBCAPS_PRIMARYBUFFER
,0,
224 &(device
->buflen
),&(device
->buffer
),
225 (LPVOID
*)&(device
->hwbuf
));
227 WARN("IDsDriver_CreateSoundBuffer failed, falling back to waveout\n");
228 /* Wine-only: close wine directsound driver, then reopen without WAVE_DIRECTSOUND */
229 device
->drvdesc
.dwFlags
= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
;
230 waveOutClose(device
->hwo
);
231 IDsDriver_Release(device
->driver
);
232 device
->driver
= NULL
;
233 device
->buffer
= NULL
;
235 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
, device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
, CALLBACK_FUNCTION
));
238 WARN("Falling back to waveout failed too! Giving up\n");
244 err
= DSOUND_PrimaryOpen(device
);
247 WARN("DSOUND_PrimaryOpen failed\n");
251 device
->state
= STATE_STOPPED
;
255 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
257 TRACE("(%p)\n", device
);
260 EnterCriticalSection(&(device
->mixlock
));
262 DSOUND_PrimaryClose(device
);
263 if (device
->driver
) {
265 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
269 HeapFree(GetProcessHeap(),0,device
->pwave
);
270 HeapFree(GetProcessHeap(),0,device
->pwfx
);
273 LeaveCriticalSection(&(device
->mixlock
));
279 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
282 TRACE("(%p)\n", device
);
285 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
287 WARN("IDsDriverBuffer_Play failed\n");
289 err
= mmErr(waveOutRestart(device
->hwo
));
291 WARN("waveOutRestart failed\n");
297 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
300 TRACE("(%p)\n", device
);
303 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
304 if (err
== DSERR_BUFFERLOST
) {
305 DWORD flags
= CALLBACK_FUNCTION
| WAVE_DIRECTSOUND
;
306 /* Wine-only: the driver wants us to reopen the device */
307 /* FIXME: check for errors */
308 IDsDriverBuffer_Release(device
->hwbuf
);
309 waveOutClose(device
->hwo
);
311 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
312 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
317 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
318 DSBCAPS_PRIMARYBUFFER
,0,
319 &(device
->buflen
),&(device
->buffer
),
320 (LPVOID
)&(device
->hwbuf
));
322 WARN("IDsDriver_CreateSoundBuffer failed\n");
323 else if (device
->state
== STATE_STOPPING
)
324 device
->state
= STATE_STOPPED
;
325 else if (device
->state
== STATE_PLAYING
)
326 device
->state
= STATE_STARTING
;
328 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
331 WARN("waveOutOpen failed\n");
333 } else if (err
!= DS_OK
) {
334 WARN("IDsDriverBuffer_Stop failed\n");
338 /* don't call the wave system with the lock set */
339 LeaveCriticalSection(&(device
->mixlock
));
342 err
= mmErr(waveOutPause(device
->hwo
));
345 EnterCriticalSection(&(device
->mixlock
));
348 WARN("waveOutPause failed\n");
354 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
356 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
359 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
361 WARN("IDsDriverBuffer_GetPosition failed\n");
366 /* check if playpos was requested */
368 /* use the cached play position */
369 *playpos
= device
->pwplay
* device
->fraglen
;
372 /* check if writepos was requested */
374 TRACE("pwplay=%i, pwqueue=%i\n", device
->pwplay
, device
->pwqueue
);
376 /* the writepos is the first non-queued position */
377 *writepos
= (device
->pwplay
+ device
->pwqueue
) * device
->fraglen
;
378 *writepos
%= device
->buflen
;
381 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
385 HRESULT
DSOUND_PrimarySetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
388 int i
, alloc_size
, cp_size
;
389 DWORD nSamplesPerSec
, bpp
, chans
;
390 TRACE("(%p,%p)\n", device
, wfex
);
392 if (device
->priolevel
== DSSCL_NORMAL
) {
393 WARN("failed priority check!\n");
394 return DSERR_PRIOLEVELNEEDED
;
397 /* Let's be pedantic! */
399 WARN("invalid parameter: wfex==NULL!\n");
400 return DSERR_INVALIDPARAM
;
402 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
403 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
404 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
405 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
406 wfex
->wBitsPerSample
, wfex
->cbSize
);
409 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
410 EnterCriticalSection(&(device
->mixlock
));
412 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
413 alloc_size
= sizeof(WAVEFORMATEX
);
414 cp_size
= sizeof(PCMWAVEFORMAT
);
416 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
418 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
420 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
421 bpp
= device
->pwfx
->wBitsPerSample
;
422 chans
= device
->pwfx
->nChannels
;
424 CopyMemory(device
->pwfx
, wfex
, cp_size
);
426 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
427 DWORD flags
= CALLBACK_FUNCTION
;
429 flags
|= WAVE_DIRECTSOUND
;
430 /* FIXME: check for errors */
431 DSOUND_PrimaryClose(device
);
432 waveOutClose(device
->hwo
);
434 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
435 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
438 err
= DSOUND_PrimaryOpen(device
);
440 WARN("DSOUND_PrimaryOpen failed\n");
444 WARN("waveOutOpen failed\n");
447 } else if (device
->hwbuf
) {
448 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
449 if (err
== DSERR_BUFFERLOST
) {
450 /* Wine-only: the driver wants us to recreate the HW buffer */
451 IDsDriverBuffer_Release(device
->hwbuf
);
454 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
455 DSBCAPS_PRIMARYBUFFER
,0,
456 &(device
->buflen
),&(device
->buffer
),
457 (LPVOID
)&(device
->hwbuf
));
459 WARN("IDsDriver_CreateSoundBuffer failed\n");
462 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
463 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
464 } else if (FAILED(err
)) {
465 WARN("IDsDriverBuffer_SetFormat failed\n");
468 /* FIXME: should we set err back to DS_OK in all cases ? */
469 DSOUND_RecalcPrimary(device
);
472 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
|| bpp
!= device
->pwfx
->wBitsPerSample
|| chans
!= device
->pwfx
->nChannels
) {
473 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
474 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
476 RtlAcquireResourceExclusive(&(*dsb
)->lock
, TRUE
);
478 (*dsb
)->freqAdjust
= ((DWORD64
)(*dsb
)->freq
<< DSOUND_FREQSHIFT
) / device
->pwfx
->nSamplesPerSec
;
479 DSOUND_RecalcFormat((*dsb
));
480 DSOUND_MixToTemporary((*dsb
), 0, (*dsb
)->buflen
);
482 RtlReleaseResource(&(*dsb
)->lock
);
488 LeaveCriticalSection(&(device
->mixlock
));
489 RtlReleaseResource(&(device
->buffer_list_lock
));
495 /*******************************************************************************
498 /* This sets this format for the <em>Primary Buffer Only</em> */
499 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
500 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
501 LPDIRECTSOUNDBUFFER iface
,
502 LPCWAVEFORMATEX wfex
)
504 TRACE("(%p,%p)\n", iface
, wfex
);
505 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl
*)iface
)->device
, wfex
);
508 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
509 LPDIRECTSOUNDBUFFER iface
,LONG vol
511 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
514 HRESULT hres
= DS_OK
;
515 TRACE("(%p,%d)\n", iface
, vol
);
517 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
518 WARN("control unavailable\n");
519 return DSERR_CONTROLUNAVAIL
;
522 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
523 WARN("invalid parameter: vol = %d\n", vol
);
524 return DSERR_INVALIDPARAM
;
528 EnterCriticalSection(&(device
->mixlock
));
530 waveOutGetVolume(device
->hwo
, &factors
);
531 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
532 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
533 DSOUND_AmpFactorToVolPan(&volpan
);
534 if (vol
!= volpan
.lVolume
) {
536 DSOUND_RecalcVolPan(&volpan
);
538 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
540 WARN("IDsDriverBuffer_SetVolumePan failed\n");
542 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
543 waveOutSetVolume(device
->hwo
, ampfactors
);
547 LeaveCriticalSection(&(device
->mixlock
));
553 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
554 LPDIRECTSOUNDBUFFER iface
,LPLONG vol
556 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
559 TRACE("(%p,%p)\n", iface
, vol
);
561 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
562 WARN("control unavailable\n");
563 return DSERR_CONTROLUNAVAIL
;
567 WARN("invalid parameter: vol = NULL\n");
568 return DSERR_INVALIDPARAM
;
571 waveOutGetVolume(device
->hwo
, &factors
);
572 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
573 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
574 DSOUND_AmpFactorToVolPan(&volpan
);
575 *vol
= volpan
.lVolume
;
579 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
580 LPDIRECTSOUNDBUFFER iface
,DWORD freq
582 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
583 TRACE("(%p,%d)\n",This
,freq
);
585 /* You cannot set the frequency of the primary buffer */
586 WARN("control unavailable\n");
587 return DSERR_CONTROLUNAVAIL
;
590 static HRESULT WINAPI
PrimaryBufferImpl_Play(
591 LPDIRECTSOUNDBUFFER iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
593 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
594 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
596 if (!(flags
& DSBPLAY_LOOPING
)) {
597 WARN("invalid parameter: flags = %08x\n", flags
);
598 return DSERR_INVALIDPARAM
;
602 EnterCriticalSection(&(device
->mixlock
));
604 if (device
->state
== STATE_STOPPED
)
605 device
->state
= STATE_STARTING
;
606 else if (device
->state
== STATE_STOPPING
)
607 device
->state
= STATE_PLAYING
;
609 LeaveCriticalSection(&(device
->mixlock
));
615 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface
)
617 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
618 TRACE("(%p)\n", iface
);
621 EnterCriticalSection(&(device
->mixlock
));
623 if (device
->state
== STATE_PLAYING
)
624 device
->state
= STATE_STOPPING
;
625 else if (device
->state
== STATE_STARTING
)
626 device
->state
= STATE_STOPPED
;
628 LeaveCriticalSection(&(device
->mixlock
));
634 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface
)
636 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
637 ULONG ref
= InterlockedIncrement(&(This
->ref
));
638 TRACE("(%p) ref was %d\n", This
, ref
- 1);
642 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface
)
644 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
645 DWORD ref
= InterlockedDecrement(&(This
->ref
));
646 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
649 This
->device
->primary
= NULL
;
650 HeapFree(GetProcessHeap(), 0, This
);
651 TRACE("(%p) released\n", This
);
656 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
657 LPDIRECTSOUNDBUFFER iface
,LPDWORD playpos
,LPDWORD writepos
660 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
661 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
664 EnterCriticalSection(&(device
->mixlock
));
666 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
668 WARN("DSOUND_PrimaryGetPosition failed\n");
669 LeaveCriticalSection(&(device
->mixlock
));
673 if (device
->state
!= STATE_STOPPED
)
674 /* apply the documented 10ms lead to writepos */
675 *writepos
+= device
->writelead
;
676 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
679 LeaveCriticalSection(&(device
->mixlock
));
682 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
686 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
687 LPDIRECTSOUNDBUFFER iface
,LPDWORD status
689 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
690 TRACE("(%p,%p)\n", iface
, status
);
692 if (status
== NULL
) {
693 WARN("invalid parameter: status == NULL\n");
694 return DSERR_INVALIDPARAM
;
698 if ((device
->state
== STATE_STARTING
) ||
699 (device
->state
== STATE_PLAYING
))
700 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
702 TRACE("status=%x\n", *status
);
707 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
708 LPDIRECTSOUNDBUFFER iface
,
714 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
715 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
717 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
719 if (lpwf
) { /* NULL is valid */
720 if (wfsize
>= size
) {
721 CopyMemory(lpwf
,device
->pwfx
,size
);
725 WARN("invalid parameter: wfsize too small\n");
728 return DSERR_INVALIDPARAM
;
732 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
734 WARN("invalid parameter: wfwritten == NULL\n");
735 return DSERR_INVALIDPARAM
;
742 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
743 LPDIRECTSOUNDBUFFER iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
746 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
747 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
759 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
760 WARN("failed priority check!\n");
761 return DSERR_PRIOLEVELNEEDED
;
764 /* when this flag is set, writecursor is meaningless and must be calculated */
765 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
766 /* GetCurrentPosition does too much magic to duplicate here */
767 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
769 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
774 /* when this flag is set, writebytes is meaningless and must be set */
775 if (flags
& DSBLOCK_ENTIREBUFFER
)
776 writebytes
= device
->buflen
;
778 if (writecursor
>= device
->buflen
) {
779 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
780 writecursor
, device
->buflen
);
781 return DSERR_INVALIDPARAM
;
784 if (writebytes
> device
->buflen
) {
785 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
786 writebytes
, device
->buflen
);
787 return DSERR_INVALIDPARAM
;
790 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
791 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
792 lplpaudioptr1
, audiobytes1
,
793 lplpaudioptr2
, audiobytes2
,
794 writecursor
, writebytes
,
797 WARN("IDsDriverBuffer_Lock failed\n");
801 if (writecursor
+writebytes
<= device
->buflen
) {
802 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
803 *audiobytes1
= writebytes
;
805 *(LPBYTE
*)lplpaudioptr2
= NULL
;
808 TRACE("->%d.0\n",writebytes
);
810 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
811 *audiobytes1
= device
->buflen
-writecursor
;
813 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
815 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
816 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
822 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
823 LPDIRECTSOUNDBUFFER iface
,DWORD newpos
825 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
826 TRACE("(%p,%d)\n",This
,newpos
);
828 /* You cannot set the position of the primary buffer */
829 WARN("invalid call\n");
830 return DSERR_INVALIDCALL
;
833 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
834 LPDIRECTSOUNDBUFFER iface
,LONG pan
836 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
839 HRESULT hres
= DS_OK
;
840 TRACE("(%p,%d)\n", iface
, pan
);
842 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
843 WARN("control unavailable\n");
844 return DSERR_CONTROLUNAVAIL
;
847 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
848 WARN("invalid parameter: pan = %d\n", pan
);
849 return DSERR_INVALIDPARAM
;
853 EnterCriticalSection(&(device
->mixlock
));
855 waveOutGetVolume(device
->hwo
, &factors
);
856 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
857 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
858 DSOUND_AmpFactorToVolPan(&volpan
);
859 if (pan
!= volpan
.lPan
) {
861 DSOUND_RecalcVolPan(&volpan
);
863 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
865 WARN("IDsDriverBuffer_SetVolumePan failed\n");
867 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
868 waveOutSetVolume(device
->hwo
, ampfactors
);
872 LeaveCriticalSection(&(device
->mixlock
));
878 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
879 LPDIRECTSOUNDBUFFER iface
,LPLONG pan
881 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
884 TRACE("(%p,%p)\n", iface
, pan
);
886 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
887 WARN("control unavailable\n");
888 return DSERR_CONTROLUNAVAIL
;
892 WARN("invalid parameter: pan == NULL\n");
893 return DSERR_INVALIDPARAM
;
896 waveOutGetVolume(device
->hwo
, &factors
);
897 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
898 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
899 DSOUND_AmpFactorToVolPan(&volpan
);
904 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
905 LPDIRECTSOUNDBUFFER iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
907 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
908 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
910 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
911 WARN("failed priority check!\n");
912 return DSERR_PRIOLEVELNEEDED
;
915 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
918 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
920 WARN("IDsDriverBuffer_Unlock failed\n");
928 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
929 LPDIRECTSOUNDBUFFER iface
931 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
932 FIXME("(%p):stub\n",This
);
936 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
937 LPDIRECTSOUNDBUFFER iface
,LPDWORD freq
939 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
940 TRACE("(%p,%p)\n", iface
, freq
);
943 WARN("invalid parameter: freq == NULL\n");
944 return DSERR_INVALIDPARAM
;
947 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
948 WARN("control unavailable\n");
949 return DSERR_CONTROLUNAVAIL
;
952 *freq
= device
->pwfx
->nSamplesPerSec
;
953 TRACE("-> %d\n", *freq
);
958 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
959 LPDIRECTSOUNDBUFFER iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
961 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
962 WARN("(%p) already initialized\n", This
);
963 return DSERR_ALREADYINITIALIZED
;
966 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
967 LPDIRECTSOUNDBUFFER iface
,LPDSBCAPS caps
969 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
970 TRACE("(%p,%p)\n", iface
, caps
);
973 WARN("invalid parameter: caps == NULL\n");
974 return DSERR_INVALIDPARAM
;
977 if (caps
->dwSize
< sizeof(*caps
)) {
978 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
979 return DSERR_INVALIDPARAM
;
982 caps
->dwFlags
= device
->dsbd
.dwFlags
;
983 caps
->dwBufferBytes
= device
->buflen
;
985 /* Windows reports these as zero */
986 caps
->dwUnlockTransferRate
= 0;
987 caps
->dwPlayCpuOverhead
= 0;
992 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
993 LPDIRECTSOUNDBUFFER iface
,REFIID riid
,LPVOID
*ppobj
995 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
996 DirectSoundDevice
*device
= This
->device
;
997 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
1000 WARN("invalid parameter\n");
1001 return E_INVALIDARG
;
1004 *ppobj
= NULL
; /* assume failure */
1006 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
1007 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
1008 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
1013 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1014 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1015 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1016 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1017 return E_NOINTERFACE
;
1020 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1021 ERR("app requested IDirectSoundNotify on primary buffer\n");
1022 /* FIXME: should we support this? */
1023 return E_NOINTERFACE
;
1026 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1027 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1028 return E_NOINTERFACE
;
1031 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1032 if (!device
->listener
)
1033 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
1034 if (device
->listener
) {
1035 *ppobj
= device
->listener
;
1036 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1040 WARN("IID_IDirectSound3DListener failed\n");
1041 return E_NOINTERFACE
;
1044 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1045 FIXME("app requested IKsPropertySet on primary buffer\n");
1046 return E_NOINTERFACE
;
1049 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1050 return E_NOINTERFACE
;
1053 static const IDirectSoundBufferVtbl dspbvt
=
1055 PrimaryBufferImpl_QueryInterface
,
1056 PrimaryBufferImpl_AddRef
,
1057 PrimaryBufferImpl_Release
,
1058 PrimaryBufferImpl_GetCaps
,
1059 PrimaryBufferImpl_GetCurrentPosition
,
1060 PrimaryBufferImpl_GetFormat
,
1061 PrimaryBufferImpl_GetVolume
,
1062 PrimaryBufferImpl_GetPan
,
1063 PrimaryBufferImpl_GetFrequency
,
1064 PrimaryBufferImpl_GetStatus
,
1065 PrimaryBufferImpl_Initialize
,
1066 PrimaryBufferImpl_Lock
,
1067 PrimaryBufferImpl_Play
,
1068 PrimaryBufferImpl_SetCurrentPosition
,
1069 PrimaryBufferImpl_SetFormat
,
1070 PrimaryBufferImpl_SetVolume
,
1071 PrimaryBufferImpl_SetPan
,
1072 PrimaryBufferImpl_SetFrequency
,
1073 PrimaryBufferImpl_Stop
,
1074 PrimaryBufferImpl_Unlock
,
1075 PrimaryBufferImpl_Restore
1078 HRESULT
PrimaryBufferImpl_Create(
1079 DirectSoundDevice
* device
,
1080 PrimaryBufferImpl
** ppdsb
,
1081 LPCDSBUFFERDESC dsbd
)
1083 PrimaryBufferImpl
*dsb
;
1084 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1086 if (dsbd
->lpwfxFormat
) {
1087 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1089 return DSERR_INVALIDPARAM
;
1092 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1095 WARN("out of memory\n");
1097 return DSERR_OUTOFMEMORY
;
1101 dsb
->device
= device
;
1102 dsb
->lpVtbl
= &dspbvt
;
1104 CopyMemory(&device
->dsbd
, dsbd
, sizeof(*dsbd
));
1106 TRACE("Created primary buffer at %p\n", dsb
);
1107 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1108 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1109 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1110 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1111 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1112 device
->pwfx
->cbSize
);