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 /* let fragment size approximate the timer delay */
48 fraglen
= (device
->pwfx
->nSamplesPerSec
* DS_TIME_DEL
/ 1000) * nBlockAlign
;
49 /* reduce fragment size until an integer number of them fits in the buffer */
50 /* (FIXME: this may or may not be a good idea) */
51 while (device
->buflen
% fraglen
) fraglen
-= nBlockAlign
;
52 device
->fraglen
= fraglen
;
53 TRACE("fraglen=%d\n", device
->fraglen
);
55 /* calculate the 10ms write lead */
56 device
->writelead
= (device
->pwfx
->nSamplesPerSec
/ 100) * nBlockAlign
;
59 static HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
62 TRACE("(%p)\n", device
);
64 /* are we using waveOut stuff? */
65 if (!device
->driver
) {
69 /* Start in pause mode, to allow buffers to get filled */
70 waveOutPause(device
->hwo
);
71 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
72 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
73 /* use fragments of 10ms (1/100s) each (which should get us within
74 * the documented write cursor lead of 10-15ms) */
75 buflen
= ((device
->pwfx
->nSamplesPerSec
/ 100) * device
->pwfx
->nBlockAlign
) * DS_HEL_FRAGS
;
76 TRACE("desired buflen=%d, old buffer=%p\n", buflen
, device
->buffer
);
77 /* reallocate emulated primary buffer */
80 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
,buflen
);
82 newbuf
= HeapAlloc(GetProcessHeap(),0,buflen
);
85 ERR("failed to allocate primary buffer\n");
86 merr
= DSERR_OUTOFMEMORY
;
87 /* but the old buffer might still exist and must be re-prepared */
89 device
->buffer
= newbuf
;
90 device
->buflen
= buflen
;
95 device
->fraglen
= device
->buflen
/ DS_HEL_FRAGS
;
97 /* prepare fragment headers */
98 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
99 device
->pwave
[c
]->lpData
= (char*)device
->buffer
+ c
*device
->fraglen
;
100 device
->pwave
[c
]->dwBufferLength
= device
->fraglen
;
101 device
->pwave
[c
]->dwUser
= (DWORD
)device
;
102 device
->pwave
[c
]->dwFlags
= 0;
103 device
->pwave
[c
]->dwLoops
= 0;
104 err
= mmErr(waveOutPrepareHeader(device
->hwo
,device
->pwave
[c
],sizeof(WAVEHDR
)));
107 waveOutUnprepareHeader(device
->hwo
,device
->pwave
[c
],sizeof(WAVEHDR
));
117 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
118 TRACE("fraglen=%d\n", device
->fraglen
);
119 DSOUND_WaveQueue(device
, (DWORD
)-1);
121 if ((err
== DS_OK
) && (merr
!= DS_OK
))
123 } else if (!device
->hwbuf
) {
124 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
125 DSBCAPS_PRIMARYBUFFER
,0,
126 &(device
->buflen
),&(device
->buffer
),
127 (LPVOID
*)&(device
->hwbuf
));
129 WARN("IDsDriver_CreateSoundBuffer failed\n");
133 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
134 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
137 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
144 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
146 TRACE("(%p)\n", device
);
148 /* are we using waveOut stuff? */
149 if (!device
->hwbuf
) {
152 device
->pwqueue
= (DWORD
)-1; /* resetting queues */
153 waveOutReset(device
->hwo
);
154 for (c
=0; c
<DS_HEL_FRAGS
; c
++)
155 waveOutUnprepareHeader(device
->hwo
, device
->pwave
[c
], sizeof(WAVEHDR
));
158 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
163 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
166 TRACE("(%p)\n", device
);
168 device
->buflen
= device
->pwfx
->nAvgBytesPerSec
;
170 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
172 if (device
->driver
) {
173 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
174 DSBCAPS_PRIMARYBUFFER
,0,
175 &(device
->buflen
),&(device
->buffer
),
176 (LPVOID
*)&(device
->hwbuf
));
178 WARN("IDsDriver_CreateSoundBuffer failed\n");
182 if (!device
->hwbuf
) {
183 /* Allocate memory for HEL buffer headers */
185 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
186 device
->pwave
[c
] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(WAVEHDR
));
187 if (!device
->pwave
[c
]) {
188 /* Argh, out of memory */
190 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
192 WARN("out of memory\n");
193 return DSERR_OUTOFMEMORY
;
198 err
= DSOUND_PrimaryOpen(device
);
201 WARN("DSOUND_PrimaryOpen failed\n");
205 /* calculate fragment size and write lead */
206 DSOUND_RecalcPrimary(device
);
207 device
->state
= STATE_STOPPED
;
211 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
213 TRACE("(%p)\n", device
);
215 EnterCriticalSection(&(device
->mixlock
));
217 DSOUND_PrimaryClose(device
);
218 if (device
->driver
) {
220 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
225 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
226 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
229 HeapFree(GetProcessHeap(),0,device
->pwfx
);
231 LeaveCriticalSection(&(device
->mixlock
));
235 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
238 TRACE("(%p)\n", device
);
241 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
243 WARN("IDsDriverBuffer_Play failed\n");
245 err
= mmErr(waveOutRestart(device
->hwo
));
247 WARN("waveOutRestart failed\n");
253 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
256 TRACE("(%p)\n", device
);
258 EnterCriticalSection(&(device
->mixlock
));
260 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
261 if (err
== DSERR_BUFFERLOST
) {
262 DWORD flags
= CALLBACK_FUNCTION
;
263 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
264 flags
|= WAVE_DIRECTSOUND
;
265 /* Wine-only: the driver wants us to reopen the device */
266 /* FIXME: check for errors */
267 IDsDriverBuffer_Release(device
->hwbuf
);
268 waveOutClose(device
->hwo
);
270 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
271 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
274 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
275 DSBCAPS_PRIMARYBUFFER
,0,
276 &(device
->buflen
),&(device
->buffer
),
277 (LPVOID
)&(device
->hwbuf
));
279 WARN("IDsDriver_CreateSoundBuffer failed\n");
281 WARN("waveOutOpen failed\n");
283 } else if (err
!= DS_OK
) {
284 WARN("IDsDriverBuffer_Stop failed\n");
287 err
= mmErr(waveOutPause(device
->hwo
));
289 WARN("waveOutPause failed\n");
291 LeaveCriticalSection(&(device
->mixlock
));
295 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
297 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
300 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
302 WARN("IDsDriverBuffer_GetPosition failed\n");
308 mtime
.wType
= TIME_BYTES
;
309 waveOutGetPosition(device
->hwo
, &mtime
, sizeof(mtime
));
310 mtime
.u
.cb
= mtime
.u
.cb
% device
->buflen
;
311 *playpos
= mtime
.u
.cb
;
314 /* the writepos should only be used by apps with WRITEPRIMARY priority,
315 * in which case our software mixer is disabled anyway */
316 *writepos
= (device
->pwplay
+ ds_hel_margin
) * device
->fraglen
;
317 while (*writepos
>= device
->buflen
)
318 *writepos
-= device
->buflen
;
321 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
325 HRESULT
DSOUND_PrimarySetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
328 int i
, alloc_size
, cp_size
;
329 DWORD nSamplesPerSec
;
330 TRACE("(%p,%p)\n", device
, wfex
);
332 if (device
->priolevel
== DSSCL_NORMAL
) {
333 WARN("failed priority check!\n");
334 return DSERR_PRIOLEVELNEEDED
;
337 /* Let's be pedantic! */
339 WARN("invalid parameter: wfex==NULL!\n");
340 return DSERR_INVALIDPARAM
;
342 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
343 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
344 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
345 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
346 wfex
->wBitsPerSample
, wfex
->cbSize
);
349 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
350 EnterCriticalSection(&(device
->mixlock
));
352 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
353 alloc_size
= sizeof(WAVEFORMATEX
);
354 cp_size
= sizeof(PCMWAVEFORMAT
);
356 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
358 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
360 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
362 CopyMemory(device
->pwfx
, wfex
, cp_size
);
364 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
365 DWORD flags
= CALLBACK_FUNCTION
;
366 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
367 flags
|= WAVE_DIRECTSOUND
;
368 /* FIXME: check for errors */
369 DSOUND_PrimaryClose(device
);
370 waveOutClose(device
->hwo
);
372 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
373 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
376 err
= DSOUND_PrimaryOpen(device
);
378 WARN("DSOUND_PrimaryOpen failed\n");
382 WARN("waveOutOpen failed\n");
385 } else if (device
->hwbuf
) {
386 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
387 if (err
== DSERR_BUFFERLOST
) {
388 /* Wine-only: the driver wants us to recreate the HW buffer */
389 IDsDriverBuffer_Release(device
->hwbuf
);
390 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
391 DSBCAPS_PRIMARYBUFFER
,0,
392 &(device
->buflen
),&(device
->buffer
),
393 (LPVOID
)&(device
->hwbuf
));
395 WARN("IDsDriver_CreateSoundBuffer failed\n");
398 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
399 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
400 } else if (FAILED(err
)) {
401 WARN("IDsDriverBuffer_SetFormat failed\n");
404 /* FIXME: should we set err back to DS_OK in all cases ? */
406 DSOUND_RecalcPrimary(device
);
408 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
) {
409 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
410 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
412 EnterCriticalSection(&((*dsb
)->lock
));
414 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
415 wfex
->nSamplesPerSec
;
417 LeaveCriticalSection(&((*dsb
)->lock
));
423 LeaveCriticalSection(&(device
->mixlock
));
424 RtlReleaseResource(&(device
->buffer_list_lock
));
430 /*******************************************************************************
433 /* This sets this format for the <em>Primary Buffer Only</em> */
434 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
435 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
436 LPDIRECTSOUNDBUFFER iface
,
437 LPCWAVEFORMATEX wfex
)
439 TRACE("(%p,%p)\n", iface
, wfex
);
440 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl
*)iface
)->device
, wfex
);
443 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
444 LPDIRECTSOUNDBUFFER iface
,LONG vol
446 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
449 HRESULT hres
= DS_OK
;
450 TRACE("(%p,%d)\n", iface
, vol
);
452 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
453 WARN("control unavailable\n");
454 return DSERR_CONTROLUNAVAIL
;
457 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
458 WARN("invalid parameter: vol = %d\n", vol
);
459 return DSERR_INVALIDPARAM
;
463 EnterCriticalSection(&(device
->mixlock
));
465 waveOutGetVolume(device
->hwo
, &factors
);
466 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
467 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
468 DSOUND_AmpFactorToVolPan(&volpan
);
469 if (vol
!= volpan
.lVolume
) {
471 DSOUND_RecalcVolPan(&volpan
);
473 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
475 WARN("IDsDriverBuffer_SetVolumePan failed\n");
477 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
478 waveOutSetVolume(device
->hwo
, ampfactors
);
482 LeaveCriticalSection(&(device
->mixlock
));
488 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
489 LPDIRECTSOUNDBUFFER iface
,LPLONG vol
491 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
494 TRACE("(%p,%p)\n", iface
, vol
);
496 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
497 WARN("control unavailable\n");
498 return DSERR_CONTROLUNAVAIL
;
502 WARN("invalid parameter: vol = NULL\n");
503 return DSERR_INVALIDPARAM
;
506 waveOutGetVolume(device
->hwo
, &factors
);
507 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
508 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
509 DSOUND_AmpFactorToVolPan(&volpan
);
510 *vol
= volpan
.lVolume
;
514 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
515 LPDIRECTSOUNDBUFFER iface
,DWORD freq
517 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
518 TRACE("(%p,%d)\n",This
,freq
);
520 /* You cannot set the frequency of the primary buffer */
521 WARN("control unavailable\n");
522 return DSERR_CONTROLUNAVAIL
;
525 static HRESULT WINAPI
PrimaryBufferImpl_Play(
526 LPDIRECTSOUNDBUFFER iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
528 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
529 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
531 if (!(flags
& DSBPLAY_LOOPING
)) {
532 WARN("invalid parameter: flags = %08x\n", flags
);
533 return DSERR_INVALIDPARAM
;
537 EnterCriticalSection(&(device
->mixlock
));
539 if (device
->state
== STATE_STOPPED
)
540 device
->state
= STATE_STARTING
;
541 else if (device
->state
== STATE_STOPPING
)
542 device
->state
= STATE_PLAYING
;
544 LeaveCriticalSection(&(device
->mixlock
));
550 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface
)
552 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
553 TRACE("(%p)\n", iface
);
556 EnterCriticalSection(&(device
->mixlock
));
558 if (device
->state
== STATE_PLAYING
)
559 device
->state
= STATE_STOPPING
;
560 else if (device
->state
== STATE_STARTING
)
561 device
->state
= STATE_STOPPED
;
563 LeaveCriticalSection(&(device
->mixlock
));
569 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface
)
571 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
572 ULONG ref
= InterlockedIncrement(&(This
->ref
));
573 TRACE("(%p) ref was %d\n", This
, ref
- 1);
577 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface
)
579 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
580 DWORD ref
= InterlockedDecrement(&(This
->ref
));
581 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
584 This
->device
->primary
= NULL
;
585 HeapFree(GetProcessHeap(), 0, This
);
586 TRACE("(%p) released\n", This
);
591 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
592 LPDIRECTSOUNDBUFFER iface
,LPDWORD playpos
,LPDWORD writepos
595 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
596 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
598 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
600 WARN("DSOUND_PrimaryGetPosition failed\n");
604 if (device
->state
!= STATE_STOPPED
)
605 /* apply the documented 10ms lead to writepos */
606 *writepos
+= device
->writelead
;
607 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
609 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
613 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
614 LPDIRECTSOUNDBUFFER iface
,LPDWORD status
616 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
617 TRACE("(%p,%p)\n", iface
, status
);
619 if (status
== NULL
) {
620 WARN("invalid parameter: status == NULL\n");
621 return DSERR_INVALIDPARAM
;
625 if ((device
->state
== STATE_STARTING
) ||
626 (device
->state
== STATE_PLAYING
))
627 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
629 TRACE("status=%x\n", *status
);
634 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
635 LPDIRECTSOUNDBUFFER iface
,
641 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
642 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
644 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
646 if (lpwf
) { /* NULL is valid */
647 if (wfsize
>= size
) {
648 CopyMemory(lpwf
,device
->pwfx
,size
);
652 WARN("invalid parameter: wfsize too small\n");
655 return DSERR_INVALIDPARAM
;
659 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
661 WARN("invalid parameter: wfwritten == NULL\n");
662 return DSERR_INVALIDPARAM
;
669 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
670 LPDIRECTSOUNDBUFFER iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
673 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
674 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
686 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
687 WARN("failed priority check!\n");
688 return DSERR_PRIOLEVELNEEDED
;
691 /* when this flag is set, writecursor is meaningless and must be calculated */
692 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
693 /* GetCurrentPosition does too much magic to duplicate here */
694 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
696 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
701 /* when this flag is set, writebytes is meaningless and must be set */
702 if (flags
& DSBLOCK_ENTIREBUFFER
)
703 writebytes
= device
->buflen
;
705 if (writecursor
>= device
->buflen
) {
706 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
707 writecursor
, device
->buflen
);
708 return DSERR_INVALIDPARAM
;
711 if (writebytes
> device
->buflen
) {
712 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
713 writebytes
, device
->buflen
);
714 return DSERR_INVALIDPARAM
;
717 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
718 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
719 lplpaudioptr1
, audiobytes1
,
720 lplpaudioptr2
, audiobytes2
,
721 writecursor
, writebytes
,
724 WARN("IDsDriverBuffer_Lock failed\n");
728 if (writecursor
+writebytes
<= device
->buflen
) {
729 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
730 *audiobytes1
= writebytes
;
732 *(LPBYTE
*)lplpaudioptr2
= NULL
;
735 TRACE("->%d.0\n",writebytes
);
737 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
738 *audiobytes1
= device
->buflen
-writecursor
;
740 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
742 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
743 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
749 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
750 LPDIRECTSOUNDBUFFER iface
,DWORD newpos
752 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
753 TRACE("(%p,%d)\n",This
,newpos
);
755 /* You cannot set the position of the primary buffer */
756 WARN("invalid call\n");
757 return DSERR_INVALIDCALL
;
760 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
761 LPDIRECTSOUNDBUFFER iface
,LONG pan
763 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
766 HRESULT hres
= DS_OK
;
767 TRACE("(%p,%d)\n", iface
, pan
);
769 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
770 WARN("control unavailable\n");
771 return DSERR_CONTROLUNAVAIL
;
774 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
775 WARN("invalid parameter: pan = %d\n", pan
);
776 return DSERR_INVALIDPARAM
;
780 EnterCriticalSection(&(device
->mixlock
));
782 waveOutGetVolume(device
->hwo
, &factors
);
783 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
784 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
785 DSOUND_AmpFactorToVolPan(&volpan
);
786 if (pan
!= volpan
.lPan
) {
788 DSOUND_RecalcVolPan(&volpan
);
790 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
792 WARN("IDsDriverBuffer_SetVolumePan failed\n");
794 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
795 waveOutSetVolume(device
->hwo
, ampfactors
);
799 LeaveCriticalSection(&(device
->mixlock
));
805 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
806 LPDIRECTSOUNDBUFFER iface
,LPLONG pan
808 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
811 TRACE("(%p,%p)\n", iface
, pan
);
813 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
814 WARN("control unavailable\n");
815 return DSERR_CONTROLUNAVAIL
;
819 WARN("invalid parameter: pan == NULL\n");
820 return DSERR_INVALIDPARAM
;
823 waveOutGetVolume(device
->hwo
, &factors
);
824 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
825 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
826 DSOUND_AmpFactorToVolPan(&volpan
);
831 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
832 LPDIRECTSOUNDBUFFER iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
834 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
835 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
837 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
838 WARN("failed priority check!\n");
839 return DSERR_PRIOLEVELNEEDED
;
842 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
845 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
847 WARN("IDsDriverBuffer_Unlock failed\n");
855 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
856 LPDIRECTSOUNDBUFFER iface
858 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
859 FIXME("(%p):stub\n",This
);
863 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
864 LPDIRECTSOUNDBUFFER iface
,LPDWORD freq
866 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
867 TRACE("(%p,%p)\n", iface
, freq
);
870 WARN("invalid parameter: freq == NULL\n");
871 return DSERR_INVALIDPARAM
;
874 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
875 WARN("control unavailable\n");
876 return DSERR_CONTROLUNAVAIL
;
879 *freq
= device
->pwfx
->nSamplesPerSec
;
880 TRACE("-> %d\n", *freq
);
885 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
886 LPDIRECTSOUNDBUFFER iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
888 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
889 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
890 DPRINTF("Re-Init!!!\n");
891 WARN("already initialized\n");
892 return DSERR_ALREADYINITIALIZED
;
895 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
896 LPDIRECTSOUNDBUFFER iface
,LPDSBCAPS caps
898 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->device
;
899 TRACE("(%p,%p)\n", iface
, caps
);
902 WARN("invalid parameter: caps == NULL\n");
903 return DSERR_INVALIDPARAM
;
906 if (caps
->dwSize
< sizeof(*caps
)) {
907 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
908 return DSERR_INVALIDPARAM
;
911 caps
->dwFlags
= device
->dsbd
.dwFlags
;
912 if (device
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
913 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
915 caps
->dwBufferBytes
= device
->buflen
;
917 /* Windows reports these as zero */
918 caps
->dwUnlockTransferRate
= 0;
919 caps
->dwPlayCpuOverhead
= 0;
924 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
925 LPDIRECTSOUNDBUFFER iface
,REFIID riid
,LPVOID
*ppobj
927 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
928 DirectSoundDevice
*device
= This
->device
;
929 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
932 WARN("invalid parameter\n");
936 *ppobj
= NULL
; /* assume failure */
938 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
939 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
940 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
945 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
946 /* a primary buffer can't have a DirectSoundBuffer8 interface */
947 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
948 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
949 return E_NOINTERFACE
;
952 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
953 ERR("app requested IDirectSoundNotify on primary buffer\n");
954 /* FIXME: should we support this? */
955 return E_NOINTERFACE
;
958 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
959 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
960 return E_NOINTERFACE
;
963 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
964 if (!device
->listener
)
965 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
966 if (device
->listener
) {
967 *ppobj
= device
->listener
;
968 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
972 WARN("IID_IDirectSound3DListener failed\n");
973 return E_NOINTERFACE
;
976 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
977 FIXME("app requested IKsPropertySet on primary buffer\n");
978 return E_NOINTERFACE
;
981 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
982 return E_NOINTERFACE
;
985 static const IDirectSoundBufferVtbl dspbvt
=
987 PrimaryBufferImpl_QueryInterface
,
988 PrimaryBufferImpl_AddRef
,
989 PrimaryBufferImpl_Release
,
990 PrimaryBufferImpl_GetCaps
,
991 PrimaryBufferImpl_GetCurrentPosition
,
992 PrimaryBufferImpl_GetFormat
,
993 PrimaryBufferImpl_GetVolume
,
994 PrimaryBufferImpl_GetPan
,
995 PrimaryBufferImpl_GetFrequency
,
996 PrimaryBufferImpl_GetStatus
,
997 PrimaryBufferImpl_Initialize
,
998 PrimaryBufferImpl_Lock
,
999 PrimaryBufferImpl_Play
,
1000 PrimaryBufferImpl_SetCurrentPosition
,
1001 PrimaryBufferImpl_SetFormat
,
1002 PrimaryBufferImpl_SetVolume
,
1003 PrimaryBufferImpl_SetPan
,
1004 PrimaryBufferImpl_SetFrequency
,
1005 PrimaryBufferImpl_Stop
,
1006 PrimaryBufferImpl_Unlock
,
1007 PrimaryBufferImpl_Restore
1010 HRESULT
PrimaryBufferImpl_Create(
1011 DirectSoundDevice
* device
,
1012 PrimaryBufferImpl
** ppdsb
,
1013 LPCDSBUFFERDESC dsbd
)
1015 PrimaryBufferImpl
*dsb
;
1016 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1018 if (dsbd
->lpwfxFormat
) {
1019 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1021 return DSERR_INVALIDPARAM
;
1024 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1027 WARN("out of memory\n");
1029 return DSERR_OUTOFMEMORY
;
1033 dsb
->device
= device
;
1034 dsb
->lpVtbl
= &dspbvt
;
1036 CopyMemory(&device
->dsbd
, dsbd
, sizeof(*dsbd
));
1038 TRACE("Created primary buffer at %p\n", dsb
);
1039 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1040 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1041 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1042 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1043 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1044 device
->pwfx
->cbSize
);