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 DSOUND_PrimaryClose(device
);
217 if (device
->driver
) {
219 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
224 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
225 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
228 HeapFree(GetProcessHeap(),0,device
->pwfx
);
233 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
236 TRACE("(%p)\n", device
);
239 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
241 WARN("IDsDriverBuffer_Play failed\n");
243 err
= mmErr(waveOutRestart(device
->hwo
));
245 WARN("waveOutRestart failed\n");
251 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
254 TRACE("(%p)\n", device
);
257 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
258 if (err
== DSERR_BUFFERLOST
) {
259 DWORD flags
= CALLBACK_FUNCTION
;
260 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
261 flags
|= WAVE_DIRECTSOUND
;
262 /* Wine-only: the driver wants us to reopen the device */
263 /* FIXME: check for errors */
264 IDsDriverBuffer_Release(device
->hwbuf
);
265 waveOutClose(device
->hwo
);
267 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
268 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
271 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
272 DSBCAPS_PRIMARYBUFFER
,0,
273 &(device
->buflen
),&(device
->buffer
),
274 (LPVOID
)&(device
->hwbuf
));
278 WARN("IDsDriver_CreateSoundBuffer failed\n");
279 /* We had a HW buffer and now we do not. This could cause
280 * problems in the future with pwave being uncreated and
283 device
->buffer
= NULL
;
284 device
->buflen
= device
->pwfx
->nAvgBytesPerSec
;
285 /* Allocate memory for HEL buffer headers */
286 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
287 device
->pwave
[c
] = HeapAlloc(GetProcessHeap(),
288 HEAP_ZERO_MEMORY
,sizeof(WAVEHDR
));
289 if (!device
->pwave
[c
]) {
290 /* Argh, out of memory */
292 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
294 WARN("out of memory\n");
295 err
= DSERR_OUTOFMEMORY
;
300 memset(device
->buffer
, (device
->pwfx
->wBitsPerSample
== 16) ? 0 : 128, device
->buflen
);
302 WARN("waveOutOpen failed\n");
304 } else if (err
!= DS_OK
) {
305 WARN("IDsDriverBuffer_Stop failed\n");
308 err
= mmErr(waveOutPause(device
->hwo
));
310 WARN("waveOutPause failed\n");
315 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
317 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
320 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
322 WARN("IDsDriverBuffer_GetPosition failed\n");
328 mtime
.wType
= TIME_BYTES
;
329 waveOutGetPosition(device
->hwo
, &mtime
, sizeof(mtime
));
330 mtime
.u
.cb
= mtime
.u
.cb
% device
->buflen
;
331 *playpos
= mtime
.u
.cb
;
334 /* the writepos should only be used by apps with WRITEPRIMARY priority,
335 * in which case our software mixer is disabled anyway */
336 *writepos
= (device
->pwplay
+ ds_hel_margin
) * device
->fraglen
;
337 while (*writepos
>= device
->buflen
)
338 *writepos
-= device
->buflen
;
341 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
345 HRESULT
DSOUND_PrimarySetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
348 int i
, alloc_size
, cp_size
;
349 DWORD nSamplesPerSec
;
350 TRACE("(%p,%p)\n", device
, wfex
);
352 if (device
->priolevel
== DSSCL_NORMAL
) {
353 WARN("failed priority check!\n");
354 return DSERR_PRIOLEVELNEEDED
;
357 /* Let's be pedantic! */
359 WARN("invalid parameter: wfex==NULL!\n");
360 return DSERR_INVALIDPARAM
;
362 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
363 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
364 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
365 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
366 wfex
->wBitsPerSample
, wfex
->cbSize
);
368 if (device
->no_reformat
)
370 TRACE("no_reformat set, not changing the format\n");
375 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
376 EnterCriticalSection(&(device
->mixlock
));
378 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
379 alloc_size
= sizeof(WAVEFORMATEX
);
380 cp_size
= sizeof(PCMWAVEFORMAT
);
382 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
384 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
386 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
388 CopyMemory(device
->pwfx
, wfex
, cp_size
);
390 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
391 DWORD flags
= CALLBACK_FUNCTION
;
392 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
393 flags
|= WAVE_DIRECTSOUND
;
394 /* FIXME: check for errors */
395 DSOUND_PrimaryClose(device
);
396 waveOutClose(device
->hwo
);
398 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
399 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
402 err
= DSOUND_PrimaryOpen(device
);
404 WARN("DSOUND_PrimaryOpen failed\n");
408 WARN("waveOutOpen failed\n");
411 } else if (device
->hwbuf
) {
412 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
413 if (err
== DSERR_BUFFERLOST
) {
414 /* Wine-only: the driver wants us to recreate the HW buffer */
415 IDsDriverBuffer_Release(device
->hwbuf
);
416 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
417 DSBCAPS_PRIMARYBUFFER
,0,
418 &(device
->buflen
),&(device
->buffer
),
419 (LPVOID
)&(device
->hwbuf
));
421 WARN("IDsDriver_CreateSoundBuffer failed\n");
424 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
425 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
427 WARN("IDsDriverBuffer_SetFormat failed\n");
430 /* FIXME: should we set err back to DS_OK in all cases ? */
432 DSOUND_RecalcPrimary(device
);
434 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
) {
435 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
436 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
438 EnterCriticalSection(&((*dsb
)->lock
));
440 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
441 wfex
->nSamplesPerSec
;
443 LeaveCriticalSection(&((*dsb
)->lock
));
449 LeaveCriticalSection(&(device
->mixlock
));
450 RtlReleaseResource(&(device
->buffer_list_lock
));
456 /*******************************************************************************
459 /* This sets this format for the <em>Primary Buffer Only</em> */
460 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
461 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
462 LPDIRECTSOUNDBUFFER8 iface
,
463 LPCWAVEFORMATEX wfex
)
465 TRACE("(%p,%p)\n", iface
, wfex
);
466 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl
*)iface
)->device
, wfex
);
469 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
470 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
472 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
475 HRESULT hres
= DS_OK
;
476 TRACE("(%p,%d)\n", iface
, vol
);
478 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
479 WARN("control unavailable\n");
480 return DSERR_CONTROLUNAVAIL
;
483 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
484 WARN("invalid parameter: vol = %d\n", vol
);
485 return DSERR_INVALIDPARAM
;
489 EnterCriticalSection(&(device
->mixlock
));
491 waveOutGetVolume(device
->hwo
, &factors
);
492 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
493 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
494 DSOUND_AmpFactorToVolPan(&volpan
);
495 if (vol
!= volpan
.lVolume
) {
497 DSOUND_RecalcVolPan(&volpan
);
499 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
501 WARN("IDsDriverBuffer_SetVolumePan failed\n");
503 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
504 waveOutSetVolume(device
->hwo
, ampfactors
);
508 LeaveCriticalSection(&(device
->mixlock
));
514 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
515 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
517 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
520 TRACE("(%p,%p)\n", iface
, vol
);
522 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
523 WARN("control unavailable\n");
524 return DSERR_CONTROLUNAVAIL
;
528 WARN("invalid parameter: vol = NULL\n");
529 return DSERR_INVALIDPARAM
;
532 waveOutGetVolume(device
->hwo
, &factors
);
533 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
534 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
535 DSOUND_AmpFactorToVolPan(&volpan
);
536 *vol
= volpan
.lVolume
;
540 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
541 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
543 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
544 TRACE("(%p,%d)\n",This
,freq
);
546 /* You cannot set the frequency of the primary buffer */
547 WARN("control unavailable\n");
548 return DSERR_CONTROLUNAVAIL
;
551 static HRESULT WINAPI
PrimaryBufferImpl_Play(
552 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
554 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
555 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
557 if (!(flags
& DSBPLAY_LOOPING
)) {
558 WARN("invalid parameter: flags = %08x\n", flags
);
559 return DSERR_INVALIDPARAM
;
563 EnterCriticalSection(&(device
->mixlock
));
565 if (device
->state
== STATE_STOPPED
)
566 device
->state
= STATE_STARTING
;
567 else if (device
->state
== STATE_STOPPING
)
568 device
->state
= STATE_PLAYING
;
570 LeaveCriticalSection(&(device
->mixlock
));
576 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
578 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
579 TRACE("(%p)\n", iface
);
582 EnterCriticalSection(&(device
->mixlock
));
584 if (device
->state
== STATE_PLAYING
)
585 device
->state
= STATE_STOPPING
;
586 else if (device
->state
== STATE_STARTING
)
587 device
->state
= STATE_STOPPED
;
589 LeaveCriticalSection(&(device
->mixlock
));
595 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
597 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
598 ULONG ref
= InterlockedIncrement(&(This
->ref
));
599 TRACE("(%p) ref was %d\n", This
, ref
- 1);
603 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
605 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
606 DWORD ref
= InterlockedDecrement(&(This
->ref
));
607 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
610 This
->device
->primary
= NULL
;
611 HeapFree(GetProcessHeap(), 0, This
);
612 TRACE("(%p) released\n", This
);
617 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
618 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
621 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
622 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
624 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
626 WARN("DSOUND_PrimaryGetPosition failed\n");
630 if (device
->state
!= STATE_STOPPED
)
631 /* apply the documented 10ms lead to writepos */
632 *writepos
+= device
->writelead
;
633 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
635 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
639 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
640 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
642 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
643 TRACE("(%p,%p)\n", iface
, status
);
645 if (status
== NULL
) {
646 WARN("invalid parameter: status == NULL\n");
647 return DSERR_INVALIDPARAM
;
651 if ((device
->state
== STATE_STARTING
) ||
652 (device
->state
== STATE_PLAYING
))
653 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
655 TRACE("status=%x\n", *status
);
660 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
661 LPDIRECTSOUNDBUFFER8 iface
,
667 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
668 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
670 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
672 if (lpwf
) { /* NULL is valid */
673 if (wfsize
>= size
) {
674 CopyMemory(lpwf
,device
->pwfx
,size
);
678 WARN("invalid parameter: wfsize too small\n");
681 return DSERR_INVALIDPARAM
;
685 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
687 WARN("invalid parameter: wfwritten == NULL\n");
688 return DSERR_INVALIDPARAM
;
695 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
696 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
699 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
700 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
712 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
713 WARN("failed priority check!\n");
714 return DSERR_PRIOLEVELNEEDED
;
717 /* when this flag is set, writecursor is meaningless and must be calculated */
718 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
719 /* GetCurrentPosition does too much magic to duplicate here */
720 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
722 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
727 /* when this flag is set, writebytes is meaningless and must be set */
728 if (flags
& DSBLOCK_ENTIREBUFFER
)
729 writebytes
= device
->buflen
;
731 if (writecursor
>= device
->buflen
) {
732 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
733 writecursor
, device
->buflen
);
734 return DSERR_INVALIDPARAM
;
737 if (writebytes
> device
->buflen
) {
738 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
739 writebytes
, device
->buflen
);
740 return DSERR_INVALIDPARAM
;
743 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
744 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
745 lplpaudioptr1
, audiobytes1
,
746 lplpaudioptr2
, audiobytes2
,
747 writecursor
, writebytes
,
750 WARN("IDsDriverBuffer_Lock failed\n");
754 if (writecursor
+writebytes
<= device
->buflen
) {
755 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
756 *audiobytes1
= writebytes
;
758 *(LPBYTE
*)lplpaudioptr2
= NULL
;
761 TRACE("->%d.0\n",writebytes
);
763 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
764 *audiobytes1
= device
->buflen
-writecursor
;
766 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
768 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
769 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
775 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
776 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
778 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
779 TRACE("(%p,%d)\n",This
,newpos
);
781 /* You cannot set the position of the primary buffer */
782 WARN("invalid call\n");
783 return DSERR_INVALIDCALL
;
786 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
787 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
789 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
792 HRESULT hres
= DS_OK
;
793 TRACE("(%p,%d)\n", iface
, pan
);
795 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
796 WARN("control unavailable\n");
797 return DSERR_CONTROLUNAVAIL
;
800 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
801 WARN("invalid parameter: pan = %d\n", pan
);
802 return DSERR_INVALIDPARAM
;
806 EnterCriticalSection(&(device
->mixlock
));
808 waveOutGetVolume(device
->hwo
, &factors
);
809 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
810 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
811 DSOUND_AmpFactorToVolPan(&volpan
);
812 if (pan
!= volpan
.lPan
) {
814 DSOUND_RecalcVolPan(&volpan
);
816 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
818 WARN("IDsDriverBuffer_SetVolumePan failed\n");
820 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
821 waveOutSetVolume(device
->hwo
, ampfactors
);
825 LeaveCriticalSection(&(device
->mixlock
));
831 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
832 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
834 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
837 TRACE("(%p,%p)\n", iface
, pan
);
839 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
840 WARN("control unavailable\n");
841 return DSERR_CONTROLUNAVAIL
;
845 WARN("invalid parameter: pan == NULL\n");
846 return DSERR_INVALIDPARAM
;
849 waveOutGetVolume(device
->hwo
, &factors
);
850 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
851 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
852 DSOUND_AmpFactorToVolPan(&volpan
);
857 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
858 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
860 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
861 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
863 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
864 WARN("failed priority check!\n");
865 return DSERR_PRIOLEVELNEEDED
;
868 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
871 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
873 WARN("IDsDriverBuffer_Unlock failed\n");
881 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
882 LPDIRECTSOUNDBUFFER8 iface
884 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
885 FIXME("(%p):stub\n",This
);
889 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
890 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
892 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
893 TRACE("(%p,%p)\n", iface
, freq
);
896 WARN("invalid parameter: freq == NULL\n");
897 return DSERR_INVALIDPARAM
;
900 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
901 WARN("control unavailable\n");
902 return DSERR_CONTROLUNAVAIL
;
905 *freq
= device
->pwfx
->nSamplesPerSec
;
906 TRACE("-> %d\n", *freq
);
911 static HRESULT WINAPI
PrimaryBufferImpl_SetFX(
912 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
914 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
916 FIXME("(%p,%u,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
919 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
921 WARN("control unavailable\n");
922 return DSERR_CONTROLUNAVAIL
;
925 static HRESULT WINAPI
PrimaryBufferImpl_AcquireResources(
926 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
928 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
930 FIXME("(%p,%08u,%u,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
933 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
935 WARN("control unavailable\n");
936 return DSERR_CONTROLUNAVAIL
;
939 static HRESULT WINAPI
PrimaryBufferImpl_GetObjectInPath(
940 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
942 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
943 FIXME("(%p,%s,%u,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
945 WARN("control unavailable\n");
946 return DSERR_CONTROLUNAVAIL
;
949 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
950 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
952 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
953 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
954 DPRINTF("Re-Init!!!\n");
955 WARN("already initialized\n");
956 return DSERR_ALREADYINITIALIZED
;
959 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
960 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
962 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
963 TRACE("(%p,%p)\n", iface
, caps
);
966 WARN("invalid parameter: caps == NULL\n");
967 return DSERR_INVALIDPARAM
;
970 if (caps
->dwSize
< sizeof(*caps
)) {
971 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
972 return DSERR_INVALIDPARAM
;
975 caps
->dwFlags
= device
->dsbd
.dwFlags
;
976 if (device
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
977 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
979 caps
->dwBufferBytes
= device
->buflen
;
981 /* This value represents the speed of the "unlock" command.
982 As unlock is quite fast (it does not do anything), I put
983 4096 ko/s = 4 Mo / s */
984 /* FIXME: hwbuf speed */
985 caps
->dwUnlockTransferRate
= 4096;
986 caps
->dwPlayCpuOverhead
= 0;
991 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
992 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
994 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
995 DirectSoundDevice
*device
= This
->device
;
996 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
999 WARN("invalid parameter\n");
1000 return E_INVALIDARG
;
1003 *ppobj
= NULL
; /* assume failure */
1005 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
1006 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
1007 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
1012 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1013 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1014 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1015 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1016 return E_NOINTERFACE
;
1019 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1020 ERR("app requested IDirectSoundNotify on primary buffer\n");
1021 /* FIXME: should we support this? */
1022 return E_NOINTERFACE
;
1025 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1026 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1027 return E_NOINTERFACE
;
1030 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1031 if (!device
->listener
)
1032 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
1033 if (device
->listener
) {
1034 *ppobj
= device
->listener
;
1035 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1039 WARN("IID_IDirectSound3DListener failed\n");
1040 return E_NOINTERFACE
;
1043 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1044 FIXME("app requested IKsPropertySet on primary buffer\n");
1045 return E_NOINTERFACE
;
1048 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1049 return E_NOINTERFACE
;
1052 static const IDirectSoundBuffer8Vtbl dspbvt
=
1054 PrimaryBufferImpl_QueryInterface
,
1055 PrimaryBufferImpl_AddRef
,
1056 PrimaryBufferImpl_Release
,
1057 PrimaryBufferImpl_GetCaps
,
1058 PrimaryBufferImpl_GetCurrentPosition
,
1059 PrimaryBufferImpl_GetFormat
,
1060 PrimaryBufferImpl_GetVolume
,
1061 PrimaryBufferImpl_GetPan
,
1062 PrimaryBufferImpl_GetFrequency
,
1063 PrimaryBufferImpl_GetStatus
,
1064 PrimaryBufferImpl_Initialize
,
1065 PrimaryBufferImpl_Lock
,
1066 PrimaryBufferImpl_Play
,
1067 PrimaryBufferImpl_SetCurrentPosition
,
1068 PrimaryBufferImpl_SetFormat
,
1069 PrimaryBufferImpl_SetVolume
,
1070 PrimaryBufferImpl_SetPan
,
1071 PrimaryBufferImpl_SetFrequency
,
1072 PrimaryBufferImpl_Stop
,
1073 PrimaryBufferImpl_Unlock
,
1074 PrimaryBufferImpl_Restore
,
1075 PrimaryBufferImpl_SetFX
,
1076 PrimaryBufferImpl_AcquireResources
,
1077 PrimaryBufferImpl_GetObjectInPath
1080 HRESULT
PrimaryBufferImpl_Create(
1081 DirectSoundDevice
* device
,
1082 PrimaryBufferImpl
** ppdsb
,
1083 LPCDSBUFFERDESC dsbd
)
1085 PrimaryBufferImpl
*dsb
;
1086 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1088 if (dsbd
->lpwfxFormat
) {
1089 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1091 return DSERR_INVALIDPARAM
;
1094 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1097 WARN("out of memory\n");
1099 return DSERR_OUTOFMEMORY
;
1103 dsb
->device
= device
;
1104 dsb
->lpVtbl
= &dspbvt
;
1106 CopyMemory(&device
->dsbd
, dsbd
, sizeof(*dsbd
));
1108 TRACE("Created primary buffer at %p\n", dsb
);
1109 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1110 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1111 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1112 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1113 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1114 device
->pwfx
->cbSize
);