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
33 #include "wine/debug.h"
36 #include "dsound_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
40 static void DSOUND_RecalcPrimary(DirectSoundDevice
*device
)
43 TRACE("(%p)\n", device
);
45 nBlockAlign
= device
->pwfx
->nBlockAlign
;
48 /* let fragment size approximate the timer delay */
49 fraglen
= (device
->pwfx
->nSamplesPerSec
* DS_TIME_DEL
/ 1000) * nBlockAlign
;
50 /* reduce fragment size until an integer number of them fits in the buffer */
51 /* (FIXME: this may or may not be a good idea) */
52 while (device
->buflen
% fraglen
) fraglen
-= nBlockAlign
;
53 device
->fraglen
= fraglen
;
54 TRACE("fraglen=%d\n", device
->fraglen
);
56 /* calculate the 10ms write lead */
57 device
->writelead
= (device
->pwfx
->nSamplesPerSec
/ 100) * nBlockAlign
;
60 static HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
63 TRACE("(%p)\n", device
);
65 /* are we using waveOut stuff? */
66 if (!device
->driver
) {
70 /* Start in pause mode, to allow buffers to get filled */
71 waveOutPause(device
->hwo
);
72 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
73 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
74 /* use fragments of 10ms (1/100s) each (which should get us within
75 * the documented write cursor lead of 10-15ms) */
76 buflen
= ((device
->pwfx
->nSamplesPerSec
/ 100) * device
->pwfx
->nBlockAlign
) * DS_HEL_FRAGS
;
77 TRACE("desired buflen=%d, old buffer=%p\n", buflen
, device
->buffer
);
78 /* reallocate emulated primary buffer */
81 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
,buflen
);
83 newbuf
= HeapAlloc(GetProcessHeap(),0,buflen
);
86 ERR("failed to allocate primary buffer\n");
87 merr
= DSERR_OUTOFMEMORY
;
88 /* but the old buffer might still exist and must be re-prepared */
90 device
->buffer
= newbuf
;
91 device
->buflen
= buflen
;
96 device
->fraglen
= device
->buflen
/ DS_HEL_FRAGS
;
98 /* prepare fragment headers */
99 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
100 device
->pwave
[c
]->lpData
= (char*)device
->buffer
+ c
*device
->fraglen
;
101 device
->pwave
[c
]->dwBufferLength
= device
->fraglen
;
102 device
->pwave
[c
]->dwUser
= (DWORD
)device
;
103 device
->pwave
[c
]->dwFlags
= 0;
104 device
->pwave
[c
]->dwLoops
= 0;
105 err
= mmErr(waveOutPrepareHeader(device
->hwo
,device
->pwave
[c
],sizeof(WAVEHDR
)));
108 waveOutUnprepareHeader(device
->hwo
,device
->pwave
[c
],sizeof(WAVEHDR
));
118 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
119 TRACE("fraglen=%d\n", device
->fraglen
);
120 DSOUND_WaveQueue(device
, (DWORD
)-1);
122 if ((err
== DS_OK
) && (merr
!= DS_OK
))
124 } else if (!device
->hwbuf
) {
125 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
126 DSBCAPS_PRIMARYBUFFER
,0,
127 &(device
->buflen
),&(device
->buffer
),
128 (LPVOID
*)&(device
->hwbuf
));
130 WARN("IDsDriver_CreateSoundBuffer failed\n");
134 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
135 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
138 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
145 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
147 TRACE("(%p)\n", device
);
149 /* are we using waveOut stuff? */
150 if (!device
->hwbuf
) {
153 device
->pwqueue
= (DWORD
)-1; /* resetting queues */
154 waveOutReset(device
->hwo
);
155 for (c
=0; c
<DS_HEL_FRAGS
; c
++)
156 waveOutUnprepareHeader(device
->hwo
, device
->pwave
[c
], sizeof(WAVEHDR
));
159 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
164 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
167 TRACE("(%p)\n", device
);
169 device
->buflen
= device
->pwfx
->nAvgBytesPerSec
;
171 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
173 if (device
->driver
) {
174 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
175 DSBCAPS_PRIMARYBUFFER
,0,
176 &(device
->buflen
),&(device
->buffer
),
177 (LPVOID
*)&(device
->hwbuf
));
179 WARN("IDsDriver_CreateSoundBuffer failed\n");
183 if (!device
->hwbuf
) {
184 /* Allocate memory for HEL buffer headers */
186 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
187 device
->pwave
[c
] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(WAVEHDR
));
188 if (!device
->pwave
[c
]) {
189 /* Argh, out of memory */
191 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
193 WARN("out of memory\n");
194 return DSERR_OUTOFMEMORY
;
199 err
= DSOUND_PrimaryOpen(device
);
202 WARN("DSOUND_PrimaryOpen failed\n");
206 /* calculate fragment size and write lead */
207 DSOUND_RecalcPrimary(device
);
208 device
->state
= STATE_STOPPED
;
212 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
214 TRACE("(%p)\n", device
);
216 EnterCriticalSection(&(device
->mixlock
));
218 DSOUND_PrimaryClose(device
);
219 if (device
->driver
) {
221 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
226 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
227 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
230 HeapFree(GetProcessHeap(),0,device
->pwfx
);
232 LeaveCriticalSection(&(device
->mixlock
));
236 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
239 TRACE("(%p)\n", device
);
242 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
244 WARN("IDsDriverBuffer_Play failed\n");
246 err
= mmErr(waveOutRestart(device
->hwo
));
248 WARN("waveOutRestart failed\n");
254 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
257 TRACE("(%p)\n", device
);
259 EnterCriticalSection(&(device
->mixlock
));
261 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
262 if (err
== DSERR_BUFFERLOST
) {
263 DWORD flags
= CALLBACK_FUNCTION
;
264 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
265 flags
|= WAVE_DIRECTSOUND
;
266 /* Wine-only: the driver wants us to reopen the device */
267 /* FIXME: check for errors */
268 IDsDriverBuffer_Release(device
->hwbuf
);
269 waveOutClose(device
->hwo
);
271 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
272 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
275 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
276 DSBCAPS_PRIMARYBUFFER
,0,
277 &(device
->buflen
),&(device
->buffer
),
278 (LPVOID
)&(device
->hwbuf
));
280 WARN("IDsDriver_CreateSoundBuffer failed\n");
282 WARN("waveOutOpen failed\n");
284 } else if (err
!= DS_OK
) {
285 WARN("IDsDriverBuffer_Stop failed\n");
288 err
= mmErr(waveOutPause(device
->hwo
));
290 WARN("waveOutPause failed\n");
292 LeaveCriticalSection(&(device
->mixlock
));
296 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
298 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
301 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
303 WARN("IDsDriverBuffer_GetPosition failed\n");
309 mtime
.wType
= TIME_BYTES
;
310 waveOutGetPosition(device
->hwo
, &mtime
, sizeof(mtime
));
311 mtime
.u
.cb
= mtime
.u
.cb
% device
->buflen
;
312 *playpos
= mtime
.u
.cb
;
315 /* the writepos should only be used by apps with WRITEPRIMARY priority,
316 * in which case our software mixer is disabled anyway */
317 *writepos
= (device
->pwplay
+ ds_hel_margin
) * device
->fraglen
;
318 while (*writepos
>= device
->buflen
)
319 *writepos
-= device
->buflen
;
322 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
326 HRESULT
DSOUND_PrimarySetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
329 int i
, alloc_size
, cp_size
;
330 DWORD nSamplesPerSec
;
331 TRACE("(%p,%p)\n", device
, wfex
);
333 if (device
->priolevel
== DSSCL_NORMAL
) {
334 WARN("failed priority check!\n");
335 return DSERR_PRIOLEVELNEEDED
;
338 /* Let's be pedantic! */
340 WARN("invalid parameter: wfex==NULL!\n");
341 return DSERR_INVALIDPARAM
;
343 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
344 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
345 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
346 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
347 wfex
->wBitsPerSample
, wfex
->cbSize
);
350 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
351 EnterCriticalSection(&(device
->mixlock
));
353 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
354 alloc_size
= sizeof(WAVEFORMATEX
);
355 cp_size
= sizeof(PCMWAVEFORMAT
);
357 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
359 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
361 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
363 CopyMemory(device
->pwfx
, wfex
, cp_size
);
365 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
366 DWORD flags
= CALLBACK_FUNCTION
;
367 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
368 flags
|= WAVE_DIRECTSOUND
;
369 /* FIXME: check for errors */
370 DSOUND_PrimaryClose(device
);
371 waveOutClose(device
->hwo
);
373 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
374 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
377 err
= DSOUND_PrimaryOpen(device
);
379 WARN("DSOUND_PrimaryOpen failed\n");
383 WARN("waveOutOpen failed\n");
386 } else if (device
->hwbuf
) {
387 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
388 if (err
== DSERR_BUFFERLOST
) {
389 /* Wine-only: the driver wants us to recreate the HW buffer */
390 IDsDriverBuffer_Release(device
->hwbuf
);
391 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
392 DSBCAPS_PRIMARYBUFFER
,0,
393 &(device
->buflen
),&(device
->buffer
),
394 (LPVOID
)&(device
->hwbuf
));
396 WARN("IDsDriver_CreateSoundBuffer failed\n");
399 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
400 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
402 WARN("IDsDriverBuffer_SetFormat failed\n");
405 /* FIXME: should we set err back to DS_OK in all cases ? */
407 DSOUND_RecalcPrimary(device
);
409 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
) {
410 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
411 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
413 EnterCriticalSection(&((*dsb
)->lock
));
415 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
416 wfex
->nSamplesPerSec
;
418 LeaveCriticalSection(&((*dsb
)->lock
));
424 LeaveCriticalSection(&(device
->mixlock
));
425 RtlReleaseResource(&(device
->buffer_list_lock
));
431 /*******************************************************************************
434 /* This sets this format for the <em>Primary Buffer Only</em> */
435 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
436 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
437 LPDIRECTSOUNDBUFFER8 iface
,
438 LPCWAVEFORMATEX wfex
)
440 TRACE("(%p,%p)\n", iface
, wfex
);
441 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl
*)iface
)->device
, wfex
);
444 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
445 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
447 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
450 HRESULT hres
= DS_OK
;
451 TRACE("(%p,%d)\n", iface
, vol
);
453 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
454 WARN("control unavailable\n");
455 return DSERR_CONTROLUNAVAIL
;
458 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
459 WARN("invalid parameter: vol = %d\n", vol
);
460 return DSERR_INVALIDPARAM
;
464 EnterCriticalSection(&(device
->mixlock
));
466 waveOutGetVolume(device
->hwo
, &factors
);
467 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
468 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
469 DSOUND_AmpFactorToVolPan(&volpan
);
470 if (vol
!= volpan
.lVolume
) {
472 DSOUND_RecalcVolPan(&volpan
);
474 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
476 WARN("IDsDriverBuffer_SetVolumePan failed\n");
478 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
479 waveOutSetVolume(device
->hwo
, ampfactors
);
483 LeaveCriticalSection(&(device
->mixlock
));
489 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
490 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
492 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
495 TRACE("(%p,%p)\n", iface
, vol
);
497 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
498 WARN("control unavailable\n");
499 return DSERR_CONTROLUNAVAIL
;
503 WARN("invalid parameter: vol = NULL\n");
504 return DSERR_INVALIDPARAM
;
507 waveOutGetVolume(device
->hwo
, &factors
);
508 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
509 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
510 DSOUND_AmpFactorToVolPan(&volpan
);
511 *vol
= volpan
.lVolume
;
515 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
516 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
518 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
519 TRACE("(%p,%d)\n",This
,freq
);
521 /* You cannot set the frequency of the primary buffer */
522 WARN("control unavailable\n");
523 return DSERR_CONTROLUNAVAIL
;
526 static HRESULT WINAPI
PrimaryBufferImpl_Play(
527 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
529 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
530 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
532 if (!(flags
& DSBPLAY_LOOPING
)) {
533 WARN("invalid parameter: flags = %08x\n", flags
);
534 return DSERR_INVALIDPARAM
;
538 EnterCriticalSection(&(device
->mixlock
));
540 if (device
->state
== STATE_STOPPED
)
541 device
->state
= STATE_STARTING
;
542 else if (device
->state
== STATE_STOPPING
)
543 device
->state
= STATE_PLAYING
;
545 LeaveCriticalSection(&(device
->mixlock
));
551 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
553 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
554 TRACE("(%p)\n", iface
);
557 EnterCriticalSection(&(device
->mixlock
));
559 if (device
->state
== STATE_PLAYING
)
560 device
->state
= STATE_STOPPING
;
561 else if (device
->state
== STATE_STARTING
)
562 device
->state
= STATE_STOPPED
;
564 LeaveCriticalSection(&(device
->mixlock
));
570 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
572 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
573 ULONG ref
= InterlockedIncrement(&(This
->ref
));
574 TRACE("(%p) ref was %d\n", This
, ref
- 1);
578 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
580 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
581 DWORD ref
= InterlockedDecrement(&(This
->ref
));
582 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
585 This
->device
->primary
= NULL
;
586 HeapFree(GetProcessHeap(), 0, This
);
587 TRACE("(%p) released\n", This
);
592 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
593 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
596 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
597 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
599 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
601 WARN("DSOUND_PrimaryGetPosition failed\n");
605 if (device
->state
!= STATE_STOPPED
)
606 /* apply the documented 10ms lead to writepos */
607 *writepos
+= device
->writelead
;
608 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
610 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
614 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
615 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
617 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
618 TRACE("(%p,%p)\n", iface
, status
);
620 if (status
== NULL
) {
621 WARN("invalid parameter: status == NULL\n");
622 return DSERR_INVALIDPARAM
;
626 if ((device
->state
== STATE_STARTING
) ||
627 (device
->state
== STATE_PLAYING
))
628 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
630 TRACE("status=%x\n", *status
);
635 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
636 LPDIRECTSOUNDBUFFER8 iface
,
642 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
643 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
645 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
647 if (lpwf
) { /* NULL is valid */
648 if (wfsize
>= size
) {
649 CopyMemory(lpwf
,device
->pwfx
,size
);
653 WARN("invalid parameter: wfsize too small\n");
656 return DSERR_INVALIDPARAM
;
660 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
662 WARN("invalid parameter: wfwritten == NULL\n");
663 return DSERR_INVALIDPARAM
;
670 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
671 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
674 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
675 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
687 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
688 WARN("failed priority check!\n");
689 return DSERR_PRIOLEVELNEEDED
;
692 /* when this flag is set, writecursor is meaningless and must be calculated */
693 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
694 /* GetCurrentPosition does too much magic to duplicate here */
695 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
697 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
702 /* when this flag is set, writebytes is meaningless and must be set */
703 if (flags
& DSBLOCK_ENTIREBUFFER
)
704 writebytes
= device
->buflen
;
706 if (writecursor
>= device
->buflen
) {
707 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
708 writecursor
, device
->buflen
);
709 return DSERR_INVALIDPARAM
;
712 if (writebytes
> device
->buflen
) {
713 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
714 writebytes
, device
->buflen
);
715 return DSERR_INVALIDPARAM
;
718 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
719 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
720 lplpaudioptr1
, audiobytes1
,
721 lplpaudioptr2
, audiobytes2
,
722 writecursor
, writebytes
,
725 WARN("IDsDriverBuffer_Lock failed\n");
729 if (writecursor
+writebytes
<= device
->buflen
) {
730 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
731 *audiobytes1
= writebytes
;
733 *(LPBYTE
*)lplpaudioptr2
= NULL
;
736 TRACE("->%d.0\n",writebytes
);
738 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
739 *audiobytes1
= device
->buflen
-writecursor
;
741 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
743 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
744 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
750 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
751 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
753 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
754 TRACE("(%p,%d)\n",This
,newpos
);
756 /* You cannot set the position of the primary buffer */
757 WARN("invalid call\n");
758 return DSERR_INVALIDCALL
;
761 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
762 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
764 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
767 HRESULT hres
= DS_OK
;
768 TRACE("(%p,%d)\n", iface
, pan
);
770 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
771 WARN("control unavailable\n");
772 return DSERR_CONTROLUNAVAIL
;
775 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
776 WARN("invalid parameter: pan = %d\n", pan
);
777 return DSERR_INVALIDPARAM
;
781 EnterCriticalSection(&(device
->mixlock
));
783 waveOutGetVolume(device
->hwo
, &factors
);
784 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
785 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
786 DSOUND_AmpFactorToVolPan(&volpan
);
787 if (pan
!= volpan
.lPan
) {
789 DSOUND_RecalcVolPan(&volpan
);
791 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
793 WARN("IDsDriverBuffer_SetVolumePan failed\n");
795 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
796 waveOutSetVolume(device
->hwo
, ampfactors
);
800 LeaveCriticalSection(&(device
->mixlock
));
806 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
807 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
809 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
812 TRACE("(%p,%p)\n", iface
, pan
);
814 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
815 WARN("control unavailable\n");
816 return DSERR_CONTROLUNAVAIL
;
820 WARN("invalid parameter: pan == NULL\n");
821 return DSERR_INVALIDPARAM
;
824 waveOutGetVolume(device
->hwo
, &factors
);
825 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
826 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
827 DSOUND_AmpFactorToVolPan(&volpan
);
832 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
833 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
835 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
836 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
838 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
839 WARN("failed priority check!\n");
840 return DSERR_PRIOLEVELNEEDED
;
843 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
846 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
848 WARN("IDsDriverBuffer_Unlock failed\n");
856 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
857 LPDIRECTSOUNDBUFFER8 iface
859 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
860 FIXME("(%p):stub\n",This
);
864 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
865 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
867 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
868 TRACE("(%p,%p)\n", iface
, freq
);
871 WARN("invalid parameter: freq == NULL\n");
872 return DSERR_INVALIDPARAM
;
875 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
876 WARN("control unavailable\n");
877 return DSERR_CONTROLUNAVAIL
;
880 *freq
= device
->pwfx
->nSamplesPerSec
;
881 TRACE("-> %d\n", *freq
);
886 static HRESULT WINAPI
PrimaryBufferImpl_SetFX(
887 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
889 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
891 FIXME("(%p,%u,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
894 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
896 WARN("control unavailable\n");
897 return DSERR_CONTROLUNAVAIL
;
900 static HRESULT WINAPI
PrimaryBufferImpl_AcquireResources(
901 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
903 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
905 FIXME("(%p,%08u,%u,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
908 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
910 WARN("control unavailable\n");
911 return DSERR_CONTROLUNAVAIL
;
914 static HRESULT WINAPI
PrimaryBufferImpl_GetObjectInPath(
915 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
917 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
918 FIXME("(%p,%s,%u,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
920 WARN("control unavailable\n");
921 return DSERR_CONTROLUNAVAIL
;
924 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
925 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
927 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
928 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
929 DPRINTF("Re-Init!!!\n");
930 WARN("already initialized\n");
931 return DSERR_ALREADYINITIALIZED
;
934 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
935 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
937 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
938 TRACE("(%p,%p)\n", iface
, caps
);
941 WARN("invalid parameter: caps == NULL\n");
942 return DSERR_INVALIDPARAM
;
945 if (caps
->dwSize
< sizeof(*caps
)) {
946 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
947 return DSERR_INVALIDPARAM
;
950 caps
->dwFlags
= device
->dsbd
.dwFlags
;
951 if (device
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
952 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
954 caps
->dwBufferBytes
= device
->buflen
;
956 /* This value represents the speed of the "unlock" command.
957 As unlock is quite fast (it does not do anything), I put
958 4096 ko/s = 4 Mo / s */
959 /* FIXME: hwbuf speed */
960 caps
->dwUnlockTransferRate
= 4096;
961 caps
->dwPlayCpuOverhead
= 0;
966 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
967 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
969 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
970 DirectSoundDevice
*device
= This
->device
;
971 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
974 WARN("invalid parameter\n");
978 *ppobj
= NULL
; /* assume failure */
980 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
981 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
982 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
987 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
988 /* a primary buffer can't have a DirectSoundBuffer8 interface */
989 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
990 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
991 return E_NOINTERFACE
;
994 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
995 ERR("app requested IDirectSoundNotify on primary buffer\n");
996 /* FIXME: should we support this? */
997 return E_NOINTERFACE
;
1000 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1001 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1002 return E_NOINTERFACE
;
1005 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1006 if (!device
->listener
)
1007 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
1008 if (device
->listener
) {
1009 *ppobj
= device
->listener
;
1010 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1014 WARN("IID_IDirectSound3DListener failed\n");
1015 return E_NOINTERFACE
;
1018 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1019 FIXME("app requested IKsPropertySet on primary buffer\n");
1020 return E_NOINTERFACE
;
1023 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1024 return E_NOINTERFACE
;
1027 static const IDirectSoundBuffer8Vtbl dspbvt
=
1029 PrimaryBufferImpl_QueryInterface
,
1030 PrimaryBufferImpl_AddRef
,
1031 PrimaryBufferImpl_Release
,
1032 PrimaryBufferImpl_GetCaps
,
1033 PrimaryBufferImpl_GetCurrentPosition
,
1034 PrimaryBufferImpl_GetFormat
,
1035 PrimaryBufferImpl_GetVolume
,
1036 PrimaryBufferImpl_GetPan
,
1037 PrimaryBufferImpl_GetFrequency
,
1038 PrimaryBufferImpl_GetStatus
,
1039 PrimaryBufferImpl_Initialize
,
1040 PrimaryBufferImpl_Lock
,
1041 PrimaryBufferImpl_Play
,
1042 PrimaryBufferImpl_SetCurrentPosition
,
1043 PrimaryBufferImpl_SetFormat
,
1044 PrimaryBufferImpl_SetVolume
,
1045 PrimaryBufferImpl_SetPan
,
1046 PrimaryBufferImpl_SetFrequency
,
1047 PrimaryBufferImpl_Stop
,
1048 PrimaryBufferImpl_Unlock
,
1049 PrimaryBufferImpl_Restore
,
1050 PrimaryBufferImpl_SetFX
,
1051 PrimaryBufferImpl_AcquireResources
,
1052 PrimaryBufferImpl_GetObjectInPath
1055 HRESULT
PrimaryBufferImpl_Create(
1056 DirectSoundDevice
* device
,
1057 PrimaryBufferImpl
** ppdsb
,
1058 LPCDSBUFFERDESC dsbd
)
1060 PrimaryBufferImpl
*dsb
;
1061 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1063 if (dsbd
->lpwfxFormat
) {
1064 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1066 return DSERR_INVALIDPARAM
;
1069 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1072 WARN("out of memory\n");
1074 return DSERR_OUTOFMEMORY
;
1078 dsb
->device
= device
;
1079 dsb
->lpVtbl
= &dspbvt
;
1081 CopyMemory(&device
->dsbd
, dsbd
, sizeof(*dsbd
));
1083 TRACE("Created primary buffer at %p\n", dsb
);
1084 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1085 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1086 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1087 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1088 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1089 device
->pwfx
->cbSize
);