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\n");
232 err
= DSOUND_PrimaryOpen(device
);
235 WARN("DSOUND_PrimaryOpen failed\n");
239 device
->state
= STATE_STOPPED
;
243 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
245 TRACE("(%p)\n", device
);
248 EnterCriticalSection(&(device
->mixlock
));
250 DSOUND_PrimaryClose(device
);
251 if (device
->driver
) {
253 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
257 HeapFree(GetProcessHeap(),0,device
->pwave
);
258 HeapFree(GetProcessHeap(),0,device
->pwfx
);
261 LeaveCriticalSection(&(device
->mixlock
));
267 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
270 TRACE("(%p)\n", device
);
273 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
275 WARN("IDsDriverBuffer_Play failed\n");
277 err
= mmErr(waveOutRestart(device
->hwo
));
279 WARN("waveOutRestart failed\n");
285 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
288 TRACE("(%p)\n", device
);
291 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
292 if (err
== DSERR_BUFFERLOST
) {
293 DWORD flags
= CALLBACK_FUNCTION
| WAVE_DIRECTSOUND
;
294 /* Wine-only: the driver wants us to reopen the device */
295 /* FIXME: check for errors */
296 IDsDriverBuffer_Release(device
->hwbuf
);
297 waveOutClose(device
->hwo
);
299 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
300 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
305 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
306 DSBCAPS_PRIMARYBUFFER
,0,
307 &(device
->buflen
),&(device
->buffer
),
308 (LPVOID
)&(device
->hwbuf
));
310 WARN("IDsDriver_CreateSoundBuffer failed\n");
311 else if (device
->state
== STATE_STOPPING
)
312 device
->state
= STATE_STOPPED
;
313 else if (device
->state
== STATE_PLAYING
)
314 device
->state
= STATE_STARTING
;
316 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
319 WARN("waveOutOpen failed\n");
321 } else if (err
!= DS_OK
) {
322 WARN("IDsDriverBuffer_Stop failed\n");
326 /* don't call the wave system with the lock set */
327 LeaveCriticalSection(&(device
->mixlock
));
330 err
= mmErr(waveOutPause(device
->hwo
));
333 EnterCriticalSection(&(device
->mixlock
));
336 WARN("waveOutPause failed\n");
342 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
344 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
347 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
349 WARN("IDsDriverBuffer_GetPosition failed\n");
354 /* check if playpos was requested */
356 /* use the cached play position */
357 *playpos
= device
->pwplay
* device
->fraglen
;
360 /* check if writepos was requested */
362 TRACE("pwplay=%i, pwqueue=%i\n", device
->pwplay
, device
->pwqueue
);
364 /* the writepos is the first non-queued position */
365 *writepos
= (device
->pwplay
+ device
->pwqueue
) * device
->fraglen
;
366 *writepos
%= device
->buflen
;
369 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
373 HRESULT
DSOUND_PrimarySetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
376 int i
, alloc_size
, cp_size
;
377 DWORD nSamplesPerSec
, bpp
, chans
;
378 TRACE("(%p,%p)\n", device
, wfex
);
380 if (device
->priolevel
== DSSCL_NORMAL
) {
381 WARN("failed priority check!\n");
382 return DSERR_PRIOLEVELNEEDED
;
385 /* Let's be pedantic! */
387 WARN("invalid parameter: wfex==NULL!\n");
388 return DSERR_INVALIDPARAM
;
390 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
391 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
392 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
393 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
394 wfex
->wBitsPerSample
, wfex
->cbSize
);
397 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
398 EnterCriticalSection(&(device
->mixlock
));
400 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
401 alloc_size
= sizeof(WAVEFORMATEX
);
402 cp_size
= sizeof(PCMWAVEFORMAT
);
404 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
406 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
408 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
409 bpp
= device
->pwfx
->wBitsPerSample
;
410 chans
= device
->pwfx
->nChannels
;
412 CopyMemory(device
->pwfx
, wfex
, cp_size
);
414 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
415 DWORD flags
= CALLBACK_FUNCTION
;
417 flags
|= WAVE_DIRECTSOUND
;
418 /* FIXME: check for errors */
419 DSOUND_PrimaryClose(device
);
420 waveOutClose(device
->hwo
);
422 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
423 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
426 err
= DSOUND_PrimaryOpen(device
);
428 WARN("DSOUND_PrimaryOpen failed\n");
432 WARN("waveOutOpen failed\n");
435 } else if (device
->hwbuf
) {
436 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
437 if (err
== DSERR_BUFFERLOST
) {
438 /* Wine-only: the driver wants us to recreate the HW buffer */
439 IDsDriverBuffer_Release(device
->hwbuf
);
442 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
443 DSBCAPS_PRIMARYBUFFER
,0,
444 &(device
->buflen
),&(device
->buffer
),
445 (LPVOID
)&(device
->hwbuf
));
447 WARN("IDsDriver_CreateSoundBuffer failed\n");
450 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
451 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
452 } else if (FAILED(err
)) {
453 WARN("IDsDriverBuffer_SetFormat failed\n");
456 /* FIXME: should we set err back to DS_OK in all cases ? */
457 DSOUND_RecalcPrimary(device
);
460 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
|| bpp
!= device
->pwfx
->wBitsPerSample
|| chans
!= device
->pwfx
->nChannels
) {
461 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
462 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
464 RtlAcquireResourceExclusive(&(*dsb
)->lock
, TRUE
);
466 (*dsb
)->freqAdjust
= ((DWORD64
)(*dsb
)->freq
<< DSOUND_FREQSHIFT
) / device
->pwfx
->nSamplesPerSec
;
467 DSOUND_RecalcFormat((*dsb
));
468 DSOUND_MixToTemporary((*dsb
), 0, (*dsb
)->buflen
);
470 RtlReleaseResource(&(*dsb
)->lock
);
476 LeaveCriticalSection(&(device
->mixlock
));
477 RtlReleaseResource(&(device
->buffer_list_lock
));
483 /*******************************************************************************
486 /* This sets this format for the <em>Primary Buffer Only</em> */
487 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
488 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
489 LPDIRECTSOUNDBUFFER iface
,
490 LPCWAVEFORMATEX wfex
)
492 TRACE("(%p,%p)\n", iface
, wfex
);
493 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl
*)iface
)->device
, wfex
);
496 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
497 LPDIRECTSOUNDBUFFER iface
,LONG vol
499 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
502 HRESULT hres
= DS_OK
;
503 TRACE("(%p,%d)\n", iface
, vol
);
505 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
506 WARN("control unavailable\n");
507 return DSERR_CONTROLUNAVAIL
;
510 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
511 WARN("invalid parameter: vol = %d\n", vol
);
512 return DSERR_INVALIDPARAM
;
516 EnterCriticalSection(&(device
->mixlock
));
518 waveOutGetVolume(device
->hwo
, &factors
);
519 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
520 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
521 DSOUND_AmpFactorToVolPan(&volpan
);
522 if (vol
!= volpan
.lVolume
) {
524 DSOUND_RecalcVolPan(&volpan
);
526 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
528 WARN("IDsDriverBuffer_SetVolumePan failed\n");
530 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
531 waveOutSetVolume(device
->hwo
, ampfactors
);
535 LeaveCriticalSection(&(device
->mixlock
));
541 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
542 LPDIRECTSOUNDBUFFER iface
,LPLONG vol
544 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
547 TRACE("(%p,%p)\n", iface
, vol
);
549 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
550 WARN("control unavailable\n");
551 return DSERR_CONTROLUNAVAIL
;
555 WARN("invalid parameter: vol = NULL\n");
556 return DSERR_INVALIDPARAM
;
559 waveOutGetVolume(device
->hwo
, &factors
);
560 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
561 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
562 DSOUND_AmpFactorToVolPan(&volpan
);
563 *vol
= volpan
.lVolume
;
567 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
568 LPDIRECTSOUNDBUFFER iface
,DWORD freq
570 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
571 TRACE("(%p,%d)\n",This
,freq
);
573 /* You cannot set the frequency of the primary buffer */
574 WARN("control unavailable\n");
575 return DSERR_CONTROLUNAVAIL
;
578 static HRESULT WINAPI
PrimaryBufferImpl_Play(
579 LPDIRECTSOUNDBUFFER iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
581 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
582 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
584 if (!(flags
& DSBPLAY_LOOPING
)) {
585 WARN("invalid parameter: flags = %08x\n", flags
);
586 return DSERR_INVALIDPARAM
;
590 EnterCriticalSection(&(device
->mixlock
));
592 if (device
->state
== STATE_STOPPED
)
593 device
->state
= STATE_STARTING
;
594 else if (device
->state
== STATE_STOPPING
)
595 device
->state
= STATE_PLAYING
;
597 LeaveCriticalSection(&(device
->mixlock
));
603 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface
)
605 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
606 TRACE("(%p)\n", iface
);
609 EnterCriticalSection(&(device
->mixlock
));
611 if (device
->state
== STATE_PLAYING
)
612 device
->state
= STATE_STOPPING
;
613 else if (device
->state
== STATE_STARTING
)
614 device
->state
= STATE_STOPPED
;
616 LeaveCriticalSection(&(device
->mixlock
));
622 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface
)
624 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
625 ULONG ref
= InterlockedIncrement(&(This
->ref
));
626 TRACE("(%p) ref was %d\n", This
, ref
- 1);
630 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface
)
632 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
633 DWORD ref
= InterlockedDecrement(&(This
->ref
));
634 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
637 This
->device
->primary
= NULL
;
638 HeapFree(GetProcessHeap(), 0, This
);
639 TRACE("(%p) released\n", This
);
644 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
645 LPDIRECTSOUNDBUFFER iface
,LPDWORD playpos
,LPDWORD writepos
648 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
649 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
652 EnterCriticalSection(&(device
->mixlock
));
654 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
656 WARN("DSOUND_PrimaryGetPosition failed\n");
657 LeaveCriticalSection(&(device
->mixlock
));
661 if (device
->state
!= STATE_STOPPED
)
662 /* apply the documented 10ms lead to writepos */
663 *writepos
+= device
->writelead
;
664 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
667 LeaveCriticalSection(&(device
->mixlock
));
670 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
674 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
675 LPDIRECTSOUNDBUFFER iface
,LPDWORD status
677 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
678 TRACE("(%p,%p)\n", iface
, status
);
680 if (status
== NULL
) {
681 WARN("invalid parameter: status == NULL\n");
682 return DSERR_INVALIDPARAM
;
686 if ((device
->state
== STATE_STARTING
) ||
687 (device
->state
== STATE_PLAYING
))
688 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
690 TRACE("status=%x\n", *status
);
695 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
696 LPDIRECTSOUNDBUFFER iface
,
702 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
703 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
705 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
707 if (lpwf
) { /* NULL is valid */
708 if (wfsize
>= size
) {
709 CopyMemory(lpwf
,device
->pwfx
,size
);
713 WARN("invalid parameter: wfsize too small\n");
716 return DSERR_INVALIDPARAM
;
720 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
722 WARN("invalid parameter: wfwritten == NULL\n");
723 return DSERR_INVALIDPARAM
;
730 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
731 LPDIRECTSOUNDBUFFER iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
734 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
735 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
747 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
748 WARN("failed priority check!\n");
749 return DSERR_PRIOLEVELNEEDED
;
752 /* when this flag is set, writecursor is meaningless and must be calculated */
753 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
754 /* GetCurrentPosition does too much magic to duplicate here */
755 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
757 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
762 /* when this flag is set, writebytes is meaningless and must be set */
763 if (flags
& DSBLOCK_ENTIREBUFFER
)
764 writebytes
= device
->buflen
;
766 if (writecursor
>= device
->buflen
) {
767 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
768 writecursor
, device
->buflen
);
769 return DSERR_INVALIDPARAM
;
772 if (writebytes
> device
->buflen
) {
773 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
774 writebytes
, device
->buflen
);
775 return DSERR_INVALIDPARAM
;
778 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
779 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
780 lplpaudioptr1
, audiobytes1
,
781 lplpaudioptr2
, audiobytes2
,
782 writecursor
, writebytes
,
785 WARN("IDsDriverBuffer_Lock failed\n");
789 if (writecursor
+writebytes
<= device
->buflen
) {
790 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
791 *audiobytes1
= writebytes
;
793 *(LPBYTE
*)lplpaudioptr2
= NULL
;
796 TRACE("->%d.0\n",writebytes
);
798 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
799 *audiobytes1
= device
->buflen
-writecursor
;
801 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
803 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
804 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
810 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
811 LPDIRECTSOUNDBUFFER iface
,DWORD newpos
813 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
814 TRACE("(%p,%d)\n",This
,newpos
);
816 /* You cannot set the position of the primary buffer */
817 WARN("invalid call\n");
818 return DSERR_INVALIDCALL
;
821 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
822 LPDIRECTSOUNDBUFFER iface
,LONG pan
824 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
827 HRESULT hres
= DS_OK
;
828 TRACE("(%p,%d)\n", iface
, pan
);
830 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
831 WARN("control unavailable\n");
832 return DSERR_CONTROLUNAVAIL
;
835 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
836 WARN("invalid parameter: pan = %d\n", pan
);
837 return DSERR_INVALIDPARAM
;
841 EnterCriticalSection(&(device
->mixlock
));
843 waveOutGetVolume(device
->hwo
, &factors
);
844 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
845 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
846 DSOUND_AmpFactorToVolPan(&volpan
);
847 if (pan
!= volpan
.lPan
) {
849 DSOUND_RecalcVolPan(&volpan
);
851 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
853 WARN("IDsDriverBuffer_SetVolumePan failed\n");
855 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
856 waveOutSetVolume(device
->hwo
, ampfactors
);
860 LeaveCriticalSection(&(device
->mixlock
));
866 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
867 LPDIRECTSOUNDBUFFER iface
,LPLONG pan
869 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
872 TRACE("(%p,%p)\n", iface
, pan
);
874 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
875 WARN("control unavailable\n");
876 return DSERR_CONTROLUNAVAIL
;
880 WARN("invalid parameter: pan == NULL\n");
881 return DSERR_INVALIDPARAM
;
884 waveOutGetVolume(device
->hwo
, &factors
);
885 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
886 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
887 DSOUND_AmpFactorToVolPan(&volpan
);
892 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
893 LPDIRECTSOUNDBUFFER iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
895 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
896 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
898 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
899 WARN("failed priority check!\n");
900 return DSERR_PRIOLEVELNEEDED
;
903 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
906 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
908 WARN("IDsDriverBuffer_Unlock failed\n");
916 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
917 LPDIRECTSOUNDBUFFER iface
919 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
920 FIXME("(%p):stub\n",This
);
924 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
925 LPDIRECTSOUNDBUFFER iface
,LPDWORD freq
927 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
928 TRACE("(%p,%p)\n", iface
, freq
);
931 WARN("invalid parameter: freq == NULL\n");
932 return DSERR_INVALIDPARAM
;
935 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
936 WARN("control unavailable\n");
937 return DSERR_CONTROLUNAVAIL
;
940 *freq
= device
->pwfx
->nSamplesPerSec
;
941 TRACE("-> %d\n", *freq
);
946 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
947 LPDIRECTSOUNDBUFFER iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
949 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
950 WARN("(%p) already initialized\n", This
);
951 return DSERR_ALREADYINITIALIZED
;
954 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
955 LPDIRECTSOUNDBUFFER iface
,LPDSBCAPS caps
957 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
958 TRACE("(%p,%p)\n", iface
, caps
);
961 WARN("invalid parameter: caps == NULL\n");
962 return DSERR_INVALIDPARAM
;
965 if (caps
->dwSize
< sizeof(*caps
)) {
966 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
967 return DSERR_INVALIDPARAM
;
970 caps
->dwFlags
= device
->dsbd
.dwFlags
;
971 if (device
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
972 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
974 caps
->dwBufferBytes
= device
->buflen
;
976 /* Windows reports these as zero */
977 caps
->dwUnlockTransferRate
= 0;
978 caps
->dwPlayCpuOverhead
= 0;
983 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
984 LPDIRECTSOUNDBUFFER iface
,REFIID riid
,LPVOID
*ppobj
986 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
987 DirectSoundDevice
*device
= This
->device
;
988 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
991 WARN("invalid parameter\n");
995 *ppobj
= NULL
; /* assume failure */
997 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
998 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
999 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
1004 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1005 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1006 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1007 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1008 return E_NOINTERFACE
;
1011 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1012 ERR("app requested IDirectSoundNotify on primary buffer\n");
1013 /* FIXME: should we support this? */
1014 return E_NOINTERFACE
;
1017 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1018 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1019 return E_NOINTERFACE
;
1022 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1023 if (!device
->listener
)
1024 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
1025 if (device
->listener
) {
1026 *ppobj
= device
->listener
;
1027 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1031 WARN("IID_IDirectSound3DListener failed\n");
1032 return E_NOINTERFACE
;
1035 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1036 FIXME("app requested IKsPropertySet on primary buffer\n");
1037 return E_NOINTERFACE
;
1040 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1041 return E_NOINTERFACE
;
1044 static const IDirectSoundBufferVtbl dspbvt
=
1046 PrimaryBufferImpl_QueryInterface
,
1047 PrimaryBufferImpl_AddRef
,
1048 PrimaryBufferImpl_Release
,
1049 PrimaryBufferImpl_GetCaps
,
1050 PrimaryBufferImpl_GetCurrentPosition
,
1051 PrimaryBufferImpl_GetFormat
,
1052 PrimaryBufferImpl_GetVolume
,
1053 PrimaryBufferImpl_GetPan
,
1054 PrimaryBufferImpl_GetFrequency
,
1055 PrimaryBufferImpl_GetStatus
,
1056 PrimaryBufferImpl_Initialize
,
1057 PrimaryBufferImpl_Lock
,
1058 PrimaryBufferImpl_Play
,
1059 PrimaryBufferImpl_SetCurrentPosition
,
1060 PrimaryBufferImpl_SetFormat
,
1061 PrimaryBufferImpl_SetVolume
,
1062 PrimaryBufferImpl_SetPan
,
1063 PrimaryBufferImpl_SetFrequency
,
1064 PrimaryBufferImpl_Stop
,
1065 PrimaryBufferImpl_Unlock
,
1066 PrimaryBufferImpl_Restore
1069 HRESULT
PrimaryBufferImpl_Create(
1070 DirectSoundDevice
* device
,
1071 PrimaryBufferImpl
** ppdsb
,
1072 LPCDSBUFFERDESC dsbd
)
1074 PrimaryBufferImpl
*dsb
;
1075 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1077 if (dsbd
->lpwfxFormat
) {
1078 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1080 return DSERR_INVALIDPARAM
;
1083 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1086 WARN("out of memory\n");
1088 return DSERR_OUTOFMEMORY
;
1092 dsb
->device
= device
;
1093 dsb
->lpVtbl
= &dspbvt
;
1095 CopyMemory(&device
->dsbd
, dsbd
, sizeof(*dsbd
));
1097 TRACE("Created primary buffer at %p\n", dsb
);
1098 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1099 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1100 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1101 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1102 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1103 device
->pwfx
->cbSize
);