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
)
42 TRACE("(%p)\n", device
);
44 nBlockAlign
= device
->pwfx
->nBlockAlign
;
47 /* Alsa doesn't have continuous buffers, instead it has buffers with power of 2,
48 * If DS_TIME_DEL is about 10 ms, 512 * nBlockAlign is roughly correct */
49 fraglen
= 512 * nBlockAlign
;
51 /* Compensate for only being rougly accurate */
52 if (device
->pwfx
->nSamplesPerSec
<= 26000)
55 if (device
->pwfx
->nSamplesPerSec
<= 12000)
58 if (device
->pwfx
->nSamplesPerSec
>= 80000)
61 /* reduce fragment size until an integer number of them fits in the buffer */
62 /* (FIXME: this may or may not be a good idea) */
63 while (device
->buflen
% fraglen
) fraglen
-= nBlockAlign
;
64 device
->fraglen
= fraglen
;
65 TRACE("fraglen=%d\n", device
->fraglen
);
67 /* calculate the 10ms write lead */
68 device
->writelead
= (device
->pwfx
->nSamplesPerSec
/ 100) * nBlockAlign
;
71 static HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
74 TRACE("(%p)\n", device
);
76 /* are we using waveOut stuff? */
77 if (!device
->driver
) {
81 /* Start in pause mode, to allow buffers to get filled */
82 waveOutPause(device
->hwo
);
83 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
84 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
86 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
87 on windows this size is always fixed (tested on win-xp) */
88 buflen
= DS_HEL_BUFLEN
;
90 TRACE("desired buflen=%d, old buffer=%p\n", buflen
, device
->buffer
);
92 /* reallocate emulated primary buffer */
94 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
,buflen
);
96 newbuf
= HeapAlloc(GetProcessHeap(),0,buflen
);
99 ERR("failed to allocate primary buffer\n");
100 merr
= DSERR_OUTOFMEMORY
;
101 /* but the old buffer might still exist and must be re-prepared */
105 device
->buffer
= newbuf
;
106 device
->buflen
= buflen
;
108 if (device
->buffer
) {
111 device
->fraglen
= device
->buflen
/ DS_HEL_FRAGS
;
114 if(device
->buflen
% DS_HEL_FRAGS
){
115 ERR("Bad DS_HEL_FRAGS resolution\n");
118 /* prepare fragment headers */
119 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
120 device
->pwave
[c
]->lpData
= (char*)device
->buffer
+ c
*device
->fraglen
;
121 device
->pwave
[c
]->dwBufferLength
= device
->fraglen
;
122 device
->pwave
[c
]->dwUser
= (DWORD
)device
;
123 device
->pwave
[c
]->dwFlags
= 0;
124 device
->pwave
[c
]->dwLoops
= 0;
125 err
= mmErr(waveOutPrepareHeader(device
->hwo
,device
->pwave
[c
],sizeof(WAVEHDR
)));
128 waveOutUnprepareHeader(device
->hwo
,device
->pwave
[c
],sizeof(WAVEHDR
));
138 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
139 TRACE("fraglen=%d\n", device
->fraglen
);
141 if ((err
== DS_OK
) && (merr
!= DS_OK
))
143 } else if (!device
->hwbuf
) {
146 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
147 DSBCAPS_PRIMARYBUFFER
,0,
148 &(device
->buflen
),&(device
->buffer
),
149 (LPVOID
*)&(device
->hwbuf
));
151 WARN("IDsDriver_CreateSoundBuffer failed\n");
155 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
156 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
159 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
166 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
168 TRACE("(%p)\n", device
);
170 /* are we using waveOut stuff? */
171 if (!device
->hwbuf
) {
174 /* get out of CS when calling the wave system */
175 LeaveCriticalSection(&(device
->mixlock
));
177 device
->pwqueue
= (DWORD
)-1; /* resetting queues */
178 waveOutReset(device
->hwo
);
179 for (c
=0; c
<DS_HEL_FRAGS
; c
++)
180 waveOutUnprepareHeader(device
->hwo
, device
->pwave
[c
], sizeof(WAVEHDR
));
182 EnterCriticalSection(&(device
->mixlock
));
184 /* clear the queue */
187 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
192 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
195 TRACE("(%p)\n", device
);
199 device
->buflen
= device
->pwfx
->nAvgBytesPerSec
;
201 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
203 if (device
->driver
) {
204 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
205 DSBCAPS_PRIMARYBUFFER
,0,
206 &(device
->buflen
),&(device
->buffer
),
207 (LPVOID
*)&(device
->hwbuf
));
209 WARN("IDsDriver_CreateSoundBuffer failed\n");
213 if (!device
->hwbuf
) {
214 /* Allocate memory for HEL buffer headers */
216 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
217 device
->pwave
[c
] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(WAVEHDR
));
218 if (!device
->pwave
[c
]) {
219 /* Argh, out of memory */
221 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
223 WARN("out of memory\n");
224 return DSERR_OUTOFMEMORY
;
229 err
= DSOUND_PrimaryOpen(device
);
232 WARN("DSOUND_PrimaryOpen failed\n");
236 /* calculate fragment size and write lead */
237 DSOUND_RecalcPrimary(device
);
238 device
->state
= STATE_STOPPED
;
242 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
244 TRACE("(%p)\n", device
);
247 EnterCriticalSection(&(device
->mixlock
));
249 DSOUND_PrimaryClose(device
);
250 if (device
->driver
) {
252 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
257 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
258 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
261 HeapFree(GetProcessHeap(),0,device
->pwfx
);
264 LeaveCriticalSection(&(device
->mixlock
));
270 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
273 TRACE("(%p)\n", device
);
276 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
278 WARN("IDsDriverBuffer_Play failed\n");
280 err
= mmErr(waveOutRestart(device
->hwo
));
282 WARN("waveOutRestart failed\n");
288 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
291 TRACE("(%p)\n", device
);
294 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
295 if (err
== DSERR_BUFFERLOST
) {
296 DWORD flags
= CALLBACK_FUNCTION
;
297 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
298 flags
|= WAVE_DIRECTSOUND
;
299 /* Wine-only: the driver wants us to reopen the device */
300 /* FIXME: check for errors */
301 IDsDriverBuffer_Release(device
->hwbuf
);
302 waveOutClose(device
->hwo
);
304 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
305 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
310 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
311 DSBCAPS_PRIMARYBUFFER
,0,
312 &(device
->buflen
),&(device
->buffer
),
313 (LPVOID
)&(device
->hwbuf
));
315 WARN("IDsDriver_CreateSoundBuffer failed\n");
317 WARN("waveOutOpen failed\n");
319 } else if (err
!= DS_OK
) {
320 WARN("IDsDriverBuffer_Stop failed\n");
324 /* dont call the wave system with the lock set */
325 LeaveCriticalSection(&(device
->mixlock
));
328 err
= mmErr(waveOutPause(device
->hwo
));
331 EnterCriticalSection(&(device
->mixlock
));
334 WARN("waveOutPause failed\n");
340 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
342 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
345 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
347 WARN("IDsDriverBuffer_GetPosition failed\n");
352 /* check if playpos was requested */
354 /* use the cached play position */
355 *playpos
= device
->pwplay
* device
->fraglen
;
358 /* check if writepos was requested */
360 TRACE("pwplay=%i, pwqueue=%i\n", device
->pwplay
, device
->pwqueue
);
362 /* the writepos is the first non-queued position */
363 *writepos
= (device
->pwplay
+ device
->pwqueue
) * device
->fraglen
;
364 *writepos
%= device
->buflen
;
367 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
371 HRESULT
DSOUND_PrimarySetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
374 int i
, alloc_size
, cp_size
;
375 DWORD nSamplesPerSec
;
376 TRACE("(%p,%p)\n", device
, wfex
);
378 if (device
->priolevel
== DSSCL_NORMAL
) {
379 WARN("failed priority check!\n");
380 return DSERR_PRIOLEVELNEEDED
;
383 /* Let's be pedantic! */
385 WARN("invalid parameter: wfex==NULL!\n");
386 return DSERR_INVALIDPARAM
;
388 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
389 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
390 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
391 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
392 wfex
->wBitsPerSample
, wfex
->cbSize
);
395 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
396 EnterCriticalSection(&(device
->mixlock
));
398 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
399 alloc_size
= sizeof(WAVEFORMATEX
);
400 cp_size
= sizeof(PCMWAVEFORMAT
);
402 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
404 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
406 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
408 CopyMemory(device
->pwfx
, wfex
, cp_size
);
410 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
411 DWORD flags
= CALLBACK_FUNCTION
;
412 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
413 flags
|= WAVE_DIRECTSOUND
;
414 /* FIXME: check for errors */
415 DSOUND_PrimaryClose(device
);
416 waveOutClose(device
->hwo
);
418 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
419 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
422 err
= DSOUND_PrimaryOpen(device
);
424 WARN("DSOUND_PrimaryOpen failed\n");
428 WARN("waveOutOpen failed\n");
431 } else if (device
->hwbuf
) {
432 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
433 if (err
== DSERR_BUFFERLOST
) {
434 /* Wine-only: the driver wants us to recreate the HW buffer */
435 IDsDriverBuffer_Release(device
->hwbuf
);
438 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
439 DSBCAPS_PRIMARYBUFFER
,0,
440 &(device
->buflen
),&(device
->buffer
),
441 (LPVOID
)&(device
->hwbuf
));
443 WARN("IDsDriver_CreateSoundBuffer failed\n");
446 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
447 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
448 } else if (FAILED(err
)) {
449 WARN("IDsDriverBuffer_SetFormat failed\n");
452 /* FIXME: should we set err back to DS_OK in all cases ? */
454 DSOUND_RecalcPrimary(device
);
456 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
) {
457 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
458 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
460 EnterCriticalSection(&((*dsb
)->lock
));
462 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
463 wfex
->nSamplesPerSec
;
465 LeaveCriticalSection(&((*dsb
)->lock
));
471 LeaveCriticalSection(&(device
->mixlock
));
472 RtlReleaseResource(&(device
->buffer_list_lock
));
478 /*******************************************************************************
481 /* This sets this format for the <em>Primary Buffer Only</em> */
482 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
483 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
484 LPDIRECTSOUNDBUFFER iface
,
485 LPCWAVEFORMATEX wfex
)
487 TRACE("(%p,%p)\n", iface
, wfex
);
488 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl
*)iface
)->device
, wfex
);
491 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
492 LPDIRECTSOUNDBUFFER iface
,LONG vol
494 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
497 HRESULT hres
= DS_OK
;
498 TRACE("(%p,%d)\n", iface
, vol
);
500 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
501 WARN("control unavailable\n");
502 return DSERR_CONTROLUNAVAIL
;
505 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
506 WARN("invalid parameter: vol = %d\n", vol
);
507 return DSERR_INVALIDPARAM
;
511 EnterCriticalSection(&(device
->mixlock
));
513 waveOutGetVolume(device
->hwo
, &factors
);
514 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
515 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
516 DSOUND_AmpFactorToVolPan(&volpan
);
517 if (vol
!= volpan
.lVolume
) {
519 DSOUND_RecalcVolPan(&volpan
);
521 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
523 WARN("IDsDriverBuffer_SetVolumePan failed\n");
525 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
526 waveOutSetVolume(device
->hwo
, ampfactors
);
530 LeaveCriticalSection(&(device
->mixlock
));
536 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
537 LPDIRECTSOUNDBUFFER iface
,LPLONG vol
539 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
542 TRACE("(%p,%p)\n", iface
, vol
);
544 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
545 WARN("control unavailable\n");
546 return DSERR_CONTROLUNAVAIL
;
550 WARN("invalid parameter: vol = NULL\n");
551 return DSERR_INVALIDPARAM
;
554 waveOutGetVolume(device
->hwo
, &factors
);
555 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
556 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
557 DSOUND_AmpFactorToVolPan(&volpan
);
558 *vol
= volpan
.lVolume
;
562 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
563 LPDIRECTSOUNDBUFFER iface
,DWORD freq
565 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
566 TRACE("(%p,%d)\n",This
,freq
);
568 /* You cannot set the frequency of the primary buffer */
569 WARN("control unavailable\n");
570 return DSERR_CONTROLUNAVAIL
;
573 static HRESULT WINAPI
PrimaryBufferImpl_Play(
574 LPDIRECTSOUNDBUFFER iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
576 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
577 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
579 if (!(flags
& DSBPLAY_LOOPING
)) {
580 WARN("invalid parameter: flags = %08x\n", flags
);
581 return DSERR_INVALIDPARAM
;
585 EnterCriticalSection(&(device
->mixlock
));
587 if (device
->state
== STATE_STOPPED
)
588 device
->state
= STATE_STARTING
;
589 else if (device
->state
== STATE_STOPPING
)
590 device
->state
= STATE_PLAYING
;
592 LeaveCriticalSection(&(device
->mixlock
));
598 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface
)
600 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
601 TRACE("(%p)\n", iface
);
604 EnterCriticalSection(&(device
->mixlock
));
606 if (device
->state
== STATE_PLAYING
)
607 device
->state
= STATE_STOPPING
;
608 else if (device
->state
== STATE_STARTING
)
609 device
->state
= STATE_STOPPED
;
611 LeaveCriticalSection(&(device
->mixlock
));
617 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface
)
619 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
620 ULONG ref
= InterlockedIncrement(&(This
->ref
));
621 TRACE("(%p) ref was %d\n", This
, ref
- 1);
625 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface
)
627 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
628 DWORD ref
= InterlockedDecrement(&(This
->ref
));
629 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
632 This
->device
->primary
= NULL
;
633 HeapFree(GetProcessHeap(), 0, This
);
634 TRACE("(%p) released\n", This
);
639 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
640 LPDIRECTSOUNDBUFFER iface
,LPDWORD playpos
,LPDWORD writepos
643 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
644 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
647 EnterCriticalSection(&(device
->mixlock
));
649 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
651 WARN("DSOUND_PrimaryGetPosition failed\n");
652 LeaveCriticalSection(&(device
->mixlock
));
656 if (device
->state
!= STATE_STOPPED
)
657 /* apply the documented 10ms lead to writepos */
658 *writepos
+= device
->writelead
;
659 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
662 LeaveCriticalSection(&(device
->mixlock
));
665 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
669 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
670 LPDIRECTSOUNDBUFFER iface
,LPDWORD status
672 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
673 TRACE("(%p,%p)\n", iface
, status
);
675 if (status
== NULL
) {
676 WARN("invalid parameter: status == NULL\n");
677 return DSERR_INVALIDPARAM
;
681 if ((device
->state
== STATE_STARTING
) ||
682 (device
->state
== STATE_PLAYING
))
683 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
685 TRACE("status=%x\n", *status
);
690 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
691 LPDIRECTSOUNDBUFFER iface
,
697 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
698 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
700 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
702 if (lpwf
) { /* NULL is valid */
703 if (wfsize
>= size
) {
704 CopyMemory(lpwf
,device
->pwfx
,size
);
708 WARN("invalid parameter: wfsize too small\n");
711 return DSERR_INVALIDPARAM
;
715 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
717 WARN("invalid parameter: wfwritten == NULL\n");
718 return DSERR_INVALIDPARAM
;
725 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
726 LPDIRECTSOUNDBUFFER iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
729 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
730 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
742 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
743 WARN("failed priority check!\n");
744 return DSERR_PRIOLEVELNEEDED
;
747 /* when this flag is set, writecursor is meaningless and must be calculated */
748 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
749 /* GetCurrentPosition does too much magic to duplicate here */
750 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
752 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
757 /* when this flag is set, writebytes is meaningless and must be set */
758 if (flags
& DSBLOCK_ENTIREBUFFER
)
759 writebytes
= device
->buflen
;
761 if (writecursor
>= device
->buflen
) {
762 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
763 writecursor
, device
->buflen
);
764 return DSERR_INVALIDPARAM
;
767 if (writebytes
> device
->buflen
) {
768 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
769 writebytes
, device
->buflen
);
770 return DSERR_INVALIDPARAM
;
773 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
774 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
775 lplpaudioptr1
, audiobytes1
,
776 lplpaudioptr2
, audiobytes2
,
777 writecursor
, writebytes
,
780 WARN("IDsDriverBuffer_Lock failed\n");
784 if (writecursor
+writebytes
<= device
->buflen
) {
785 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
786 *audiobytes1
= writebytes
;
788 *(LPBYTE
*)lplpaudioptr2
= NULL
;
791 TRACE("->%d.0\n",writebytes
);
793 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
794 *audiobytes1
= device
->buflen
-writecursor
;
796 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
798 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
799 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
805 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
806 LPDIRECTSOUNDBUFFER iface
,DWORD newpos
808 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
809 TRACE("(%p,%d)\n",This
,newpos
);
811 /* You cannot set the position of the primary buffer */
812 WARN("invalid call\n");
813 return DSERR_INVALIDCALL
;
816 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
817 LPDIRECTSOUNDBUFFER iface
,LONG pan
819 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
822 HRESULT hres
= DS_OK
;
823 TRACE("(%p,%d)\n", iface
, pan
);
825 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
826 WARN("control unavailable\n");
827 return DSERR_CONTROLUNAVAIL
;
830 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
831 WARN("invalid parameter: pan = %d\n", pan
);
832 return DSERR_INVALIDPARAM
;
836 EnterCriticalSection(&(device
->mixlock
));
838 waveOutGetVolume(device
->hwo
, &factors
);
839 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
840 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
841 DSOUND_AmpFactorToVolPan(&volpan
);
842 if (pan
!= volpan
.lPan
) {
844 DSOUND_RecalcVolPan(&volpan
);
846 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
848 WARN("IDsDriverBuffer_SetVolumePan failed\n");
850 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
851 waveOutSetVolume(device
->hwo
, ampfactors
);
855 LeaveCriticalSection(&(device
->mixlock
));
861 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
862 LPDIRECTSOUNDBUFFER iface
,LPLONG pan
864 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
867 TRACE("(%p,%p)\n", iface
, pan
);
869 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
870 WARN("control unavailable\n");
871 return DSERR_CONTROLUNAVAIL
;
875 WARN("invalid parameter: pan == NULL\n");
876 return DSERR_INVALIDPARAM
;
879 waveOutGetVolume(device
->hwo
, &factors
);
880 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
881 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
882 DSOUND_AmpFactorToVolPan(&volpan
);
887 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
888 LPDIRECTSOUNDBUFFER iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
890 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
891 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
893 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
894 WARN("failed priority check!\n");
895 return DSERR_PRIOLEVELNEEDED
;
898 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
901 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
903 WARN("IDsDriverBuffer_Unlock failed\n");
911 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
912 LPDIRECTSOUNDBUFFER iface
914 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
915 FIXME("(%p):stub\n",This
);
919 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
920 LPDIRECTSOUNDBUFFER iface
,LPDWORD freq
922 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
923 TRACE("(%p,%p)\n", iface
, freq
);
926 WARN("invalid parameter: freq == NULL\n");
927 return DSERR_INVALIDPARAM
;
930 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
931 WARN("control unavailable\n");
932 return DSERR_CONTROLUNAVAIL
;
935 *freq
= device
->pwfx
->nSamplesPerSec
;
936 TRACE("-> %d\n", *freq
);
941 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
942 LPDIRECTSOUNDBUFFER iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
944 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
945 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
946 DPRINTF("Re-Init!!!\n");
947 WARN("already initialized\n");
948 return DSERR_ALREADYINITIALIZED
;
951 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
952 LPDIRECTSOUNDBUFFER iface
,LPDSBCAPS caps
954 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
955 TRACE("(%p,%p)\n", iface
, caps
);
958 WARN("invalid parameter: caps == NULL\n");
959 return DSERR_INVALIDPARAM
;
962 if (caps
->dwSize
< sizeof(*caps
)) {
963 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
964 return DSERR_INVALIDPARAM
;
967 caps
->dwFlags
= device
->dsbd
.dwFlags
;
968 if (device
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
969 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
971 caps
->dwBufferBytes
= device
->buflen
;
973 /* Windows reports these as zero */
974 caps
->dwUnlockTransferRate
= 0;
975 caps
->dwPlayCpuOverhead
= 0;
980 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
981 LPDIRECTSOUNDBUFFER iface
,REFIID riid
,LPVOID
*ppobj
983 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
984 DirectSoundDevice
*device
= This
->device
;
985 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
988 WARN("invalid parameter\n");
992 *ppobj
= NULL
; /* assume failure */
994 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
995 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
996 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
1001 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1002 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1003 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1004 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1005 return E_NOINTERFACE
;
1008 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1009 ERR("app requested IDirectSoundNotify on primary buffer\n");
1010 /* FIXME: should we support this? */
1011 return E_NOINTERFACE
;
1014 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1015 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1016 return E_NOINTERFACE
;
1019 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1020 if (!device
->listener
)
1021 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
1022 if (device
->listener
) {
1023 *ppobj
= device
->listener
;
1024 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1028 WARN("IID_IDirectSound3DListener failed\n");
1029 return E_NOINTERFACE
;
1032 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1033 FIXME("app requested IKsPropertySet on primary buffer\n");
1034 return E_NOINTERFACE
;
1037 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1038 return E_NOINTERFACE
;
1041 static const IDirectSoundBufferVtbl dspbvt
=
1043 PrimaryBufferImpl_QueryInterface
,
1044 PrimaryBufferImpl_AddRef
,
1045 PrimaryBufferImpl_Release
,
1046 PrimaryBufferImpl_GetCaps
,
1047 PrimaryBufferImpl_GetCurrentPosition
,
1048 PrimaryBufferImpl_GetFormat
,
1049 PrimaryBufferImpl_GetVolume
,
1050 PrimaryBufferImpl_GetPan
,
1051 PrimaryBufferImpl_GetFrequency
,
1052 PrimaryBufferImpl_GetStatus
,
1053 PrimaryBufferImpl_Initialize
,
1054 PrimaryBufferImpl_Lock
,
1055 PrimaryBufferImpl_Play
,
1056 PrimaryBufferImpl_SetCurrentPosition
,
1057 PrimaryBufferImpl_SetFormat
,
1058 PrimaryBufferImpl_SetVolume
,
1059 PrimaryBufferImpl_SetPan
,
1060 PrimaryBufferImpl_SetFrequency
,
1061 PrimaryBufferImpl_Stop
,
1062 PrimaryBufferImpl_Unlock
,
1063 PrimaryBufferImpl_Restore
1066 HRESULT
PrimaryBufferImpl_Create(
1067 DirectSoundDevice
* device
,
1068 PrimaryBufferImpl
** ppdsb
,
1069 LPCDSBUFFERDESC dsbd
)
1071 PrimaryBufferImpl
*dsb
;
1072 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1074 if (dsbd
->lpwfxFormat
) {
1075 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1077 return DSERR_INVALIDPARAM
;
1080 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1083 WARN("out of memory\n");
1085 return DSERR_OUTOFMEMORY
;
1089 dsb
->device
= device
;
1090 dsb
->lpVtbl
= &dspbvt
;
1092 CopyMemory(&device
->dsbd
, dsbd
, sizeof(*dsbd
));
1094 TRACE("Created primary buffer at %p\n", dsb
);
1095 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1096 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1097 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1098 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1099 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1100 device
->pwfx
->cbSize
);