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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
33 #include "wine/debug.h"
36 #include "dsound_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
40 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=%ld\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=%ld, 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=%ld\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
));
276 WARN("IDsDriver_CreateSoundBuffer failed\n");
278 WARN("waveOutOpen failed\n");
280 } else if (err
!= DS_OK
) {
281 WARN("IDsDriverBuffer_Stop failed\n");
284 err
= mmErr(waveOutPause(device
->hwo
));
286 WARN("waveOutPause failed\n");
291 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
293 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
296 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
298 WARN("IDsDriverBuffer_GetPosition failed\n");
304 mtime
.wType
= TIME_BYTES
;
305 waveOutGetPosition(device
->hwo
, &mtime
, sizeof(mtime
));
306 mtime
.u
.cb
= mtime
.u
.cb
% device
->buflen
;
307 *playpos
= mtime
.u
.cb
;
310 /* the writepos should only be used by apps with WRITEPRIMARY priority,
311 * in which case our software mixer is disabled anyway */
312 *writepos
= (device
->pwplay
+ ds_hel_margin
) * device
->fraglen
;
313 while (*writepos
>= device
->buflen
)
314 *writepos
-= device
->buflen
;
317 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
321 /*******************************************************************************
324 /* This sets this format for the <em>Primary Buffer Only</em> */
325 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
326 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
327 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
329 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
331 int i
, alloc_size
, cp_size
;
332 DWORD nSamplesPerSec
;
333 TRACE("(%p,%p)\n", iface
, wfex
);
335 if (device
->priolevel
== DSSCL_NORMAL
) {
336 WARN("failed priority check!\n");
337 return DSERR_PRIOLEVELNEEDED
;
340 /* Let's be pedantic! */
342 WARN("invalid parameter: wfex==NULL!\n");
343 return DSERR_INVALIDPARAM
;
345 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
346 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
347 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
348 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
349 wfex
->wBitsPerSample
, wfex
->cbSize
);
352 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
353 EnterCriticalSection(&(device
->mixlock
));
355 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
356 alloc_size
= sizeof(WAVEFORMATEX
);
357 cp_size
= sizeof(PCMWAVEFORMAT
);
359 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
361 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
363 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
365 CopyMemory(device
->pwfx
, wfex
, cp_size
);
367 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
368 DWORD flags
= CALLBACK_FUNCTION
;
369 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
370 flags
|= WAVE_DIRECTSOUND
;
371 /* FIXME: check for errors */
372 DSOUND_PrimaryClose(device
);
373 waveOutClose(device
->hwo
);
375 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
376 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
379 err
= DSOUND_PrimaryOpen(device
);
381 WARN("DSOUND_PrimaryOpen failed\n");
385 WARN("waveOutOpen failed\n");
388 } else if (device
->hwbuf
) {
389 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
390 if (err
== DSERR_BUFFERLOST
) {
391 /* Wine-only: the driver wants us to recreate the HW buffer */
392 IDsDriverBuffer_Release(device
->hwbuf
);
393 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
394 DSBCAPS_PRIMARYBUFFER
,0,
395 &(device
->buflen
),&(device
->buffer
),
396 (LPVOID
)&(device
->hwbuf
));
398 WARN("IDsDriver_CreateSoundBuffer failed\n");
401 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
402 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
404 WARN("IDsDriverBuffer_SetFormat failed\n");
407 /* FIXME: should we set err back to DS_OK in all cases ? */
409 DSOUND_RecalcPrimary(device
);
411 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
) {
412 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
413 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
415 EnterCriticalSection(&((*dsb
)->lock
));
417 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
418 wfex
->nSamplesPerSec
;
420 LeaveCriticalSection(&((*dsb
)->lock
));
426 LeaveCriticalSection(&(device
->mixlock
));
427 RtlReleaseResource(&(device
->buffer_list_lock
));
433 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
434 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
436 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
439 HRESULT hres
= DS_OK
;
440 TRACE("(%p,%ld)\n", iface
, vol
);
442 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
443 WARN("control unavailable\n");
444 return DSERR_CONTROLUNAVAIL
;
447 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
448 WARN("invalid parameter: vol = %ld\n", vol
);
449 return DSERR_INVALIDPARAM
;
453 EnterCriticalSection(&(device
->mixlock
));
455 waveOutGetVolume(device
->hwo
, &factors
);
456 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
457 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
458 DSOUND_AmpFactorToVolPan(&volpan
);
459 if (vol
!= volpan
.lVolume
) {
461 DSOUND_RecalcVolPan(&volpan
);
463 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
465 WARN("IDsDriverBuffer_SetVolumePan failed\n");
467 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
468 waveOutSetVolume(device
->hwo
, ampfactors
);
472 LeaveCriticalSection(&(device
->mixlock
));
478 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
479 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
481 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
484 TRACE("(%p,%p)\n", iface
, vol
);
486 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
487 WARN("control unavailable\n");
488 return DSERR_CONTROLUNAVAIL
;
492 WARN("invalid parameter: vol = NULL\n");
493 return DSERR_INVALIDPARAM
;
496 waveOutGetVolume(device
->hwo
, &factors
);
497 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
498 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
499 DSOUND_AmpFactorToVolPan(&volpan
);
500 *vol
= volpan
.lVolume
;
504 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
505 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
507 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
508 TRACE("(%p,%ld)\n",This
,freq
);
510 /* You cannot set the frequency of the primary buffer */
511 WARN("control unavailable\n");
512 return DSERR_CONTROLUNAVAIL
;
515 static HRESULT WINAPI
PrimaryBufferImpl_Play(
516 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
518 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
519 TRACE("(%p,%08lx,%08lx,%08lx)\n", iface
, reserved1
, reserved2
, flags
);
521 if (!(flags
& DSBPLAY_LOOPING
)) {
522 WARN("invalid parameter: flags = %08lx\n", flags
);
523 return DSERR_INVALIDPARAM
;
527 EnterCriticalSection(&(device
->mixlock
));
529 if (device
->state
== STATE_STOPPED
)
530 device
->state
= STATE_STARTING
;
531 else if (device
->state
== STATE_STOPPING
)
532 device
->state
= STATE_PLAYING
;
534 LeaveCriticalSection(&(device
->mixlock
));
540 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
542 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
543 TRACE("(%p)\n", iface
);
546 EnterCriticalSection(&(device
->mixlock
));
548 if (device
->state
== STATE_PLAYING
)
549 device
->state
= STATE_STOPPING
;
550 else if (device
->state
== STATE_STARTING
)
551 device
->state
= STATE_STOPPED
;
553 LeaveCriticalSection(&(device
->mixlock
));
559 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
561 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
562 ULONG ref
= InterlockedIncrement(&(This
->ref
));
563 TRACE("(%p) ref was %ld\n", This
, ref
- 1);
567 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
569 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
570 DWORD ref
= InterlockedDecrement(&(This
->ref
));
571 TRACE("(%p) ref was %ld\n", This
, ref
+ 1);
574 This
->dsound
->device
->primary
= NULL
;
575 HeapFree(GetProcessHeap(), 0, This
);
576 TRACE("(%p) released\n", This
);
581 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
582 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
585 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
586 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
588 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
590 WARN("DSOUND_PrimaryGetPosition failed\n");
594 if (device
->state
!= STATE_STOPPED
)
595 /* apply the documented 10ms lead to writepos */
596 *writepos
+= device
->writelead
;
597 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
599 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
603 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
604 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
606 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
607 TRACE("(%p,%p)\n", iface
, status
);
609 if (status
== NULL
) {
610 WARN("invalid parameter: status == NULL\n");
611 return DSERR_INVALIDPARAM
;
615 if ((device
->state
== STATE_STARTING
) ||
616 (device
->state
== STATE_PLAYING
))
617 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
619 TRACE("status=%lx\n", *status
);
624 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
625 LPDIRECTSOUNDBUFFER8 iface
,
631 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
632 TRACE("(%p,%p,%ld,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
634 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
636 if (lpwf
) { /* NULL is valid */
637 if (wfsize
>= size
) {
638 CopyMemory(lpwf
,device
->pwfx
,size
);
642 WARN("invalid parameter: wfsize too small\n");
645 return DSERR_INVALIDPARAM
;
649 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
651 WARN("invalid parameter: wfwritten == NULL\n");
652 return DSERR_INVALIDPARAM
;
659 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
660 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
662 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
663 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
675 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
676 WARN("failed priority check!\n");
677 return DSERR_PRIOLEVELNEEDED
;
680 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
683 /* GetCurrentPosition does too much magic to duplicate here */
684 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writepos
);
686 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
689 writecursor
+= writepos
;
691 while (writecursor
>= device
->buflen
)
692 writecursor
-= device
->buflen
;
693 if (flags
& DSBLOCK_ENTIREBUFFER
)
694 writebytes
= device
->buflen
;
695 if (writebytes
> device
->buflen
)
696 writebytes
= device
->buflen
;
698 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
700 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
701 lplpaudioptr1
, audiobytes1
,
702 lplpaudioptr2
, audiobytes2
,
703 writecursor
, writebytes
,
706 WARN("IDsDriverBuffer_Lock failed\n");
710 if (writecursor
+writebytes
<= device
->buflen
) {
711 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
712 *audiobytes1
= writebytes
;
714 *(LPBYTE
*)lplpaudioptr2
= NULL
;
717 TRACE("->%ld.0\n",writebytes
);
719 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
720 *audiobytes1
= device
->buflen
-writecursor
;
722 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
724 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
725 TRACE("->%ld.%ld\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
731 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
732 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
734 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
735 TRACE("(%p,%ld)\n",This
,newpos
);
737 /* You cannot set the position of the primary buffer */
738 WARN("invalid call\n");
739 return DSERR_INVALIDCALL
;
742 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
743 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
745 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
748 HRESULT hres
= DS_OK
;
749 TRACE("(%p,%ld)\n", iface
, pan
);
751 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
752 WARN("control unavailable\n");
753 return DSERR_CONTROLUNAVAIL
;
756 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
757 WARN("invalid parameter: pan = %ld\n", pan
);
758 return DSERR_INVALIDPARAM
;
762 EnterCriticalSection(&(device
->mixlock
));
764 waveOutGetVolume(device
->hwo
, &factors
);
765 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
766 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
767 DSOUND_AmpFactorToVolPan(&volpan
);
768 if (pan
!= volpan
.lPan
) {
770 DSOUND_RecalcVolPan(&volpan
);
772 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
774 WARN("IDsDriverBuffer_SetVolumePan failed\n");
776 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
777 waveOutSetVolume(device
->hwo
, ampfactors
);
781 LeaveCriticalSection(&(device
->mixlock
));
787 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
788 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
790 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
793 TRACE("(%p,%p)\n", iface
, pan
);
795 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
796 WARN("control unavailable\n");
797 return DSERR_CONTROLUNAVAIL
;
801 WARN("invalid parameter: pan == NULL\n");
802 return DSERR_INVALIDPARAM
;
805 waveOutGetVolume(device
->hwo
, &factors
);
806 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
807 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
808 DSOUND_AmpFactorToVolPan(&volpan
);
813 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
814 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
816 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
817 TRACE("(%p,%p,%ld,%p,%ld)\n", iface
, p1
, x1
, p2
, x2
);
819 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
820 WARN("failed priority check!\n");
821 return DSERR_PRIOLEVELNEEDED
;
824 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
827 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
829 WARN("IDsDriverBuffer_Unlock failed\n");
837 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
838 LPDIRECTSOUNDBUFFER8 iface
840 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
841 FIXME("(%p):stub\n",This
);
845 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
846 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
848 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
849 TRACE("(%p,%p)\n", iface
, freq
);
852 WARN("invalid parameter: freq == NULL\n");
853 return DSERR_INVALIDPARAM
;
856 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
857 WARN("control unavailable\n");
858 return DSERR_CONTROLUNAVAIL
;
861 *freq
= device
->pwfx
->nSamplesPerSec
;
862 TRACE("-> %ld\n", *freq
);
867 static HRESULT WINAPI
PrimaryBufferImpl_SetFX(
868 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
870 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
872 FIXME("(%p,%lu,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
875 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
877 WARN("control unavailable\n");
878 return DSERR_CONTROLUNAVAIL
;
881 static HRESULT WINAPI
PrimaryBufferImpl_AcquireResources(
882 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
884 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
886 FIXME("(%p,%08lu,%lu,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
889 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
891 WARN("control unavailable\n");
892 return DSERR_CONTROLUNAVAIL
;
895 static HRESULT WINAPI
PrimaryBufferImpl_GetObjectInPath(
896 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
898 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
899 FIXME("(%p,%s,%lu,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
901 WARN("control unavailable\n");
902 return DSERR_CONTROLUNAVAIL
;
905 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
906 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
908 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
909 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
910 DPRINTF("Re-Init!!!\n");
911 WARN("already initialized\n");
912 return DSERR_ALREADYINITIALIZED
;
915 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
916 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
918 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
919 TRACE("(%p,%p)\n", iface
, caps
);
922 WARN("invalid parameter: caps == NULL\n");
923 return DSERR_INVALIDPARAM
;
926 if (caps
->dwSize
< sizeof(*caps
)) {
927 WARN("invalid parameter: caps->dwSize = %ld: < %d\n", caps
->dwSize
, sizeof(*caps
));
928 return DSERR_INVALIDPARAM
;
931 caps
->dwFlags
= device
->dsbd
.dwFlags
;
932 if (device
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
933 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
935 caps
->dwBufferBytes
= device
->buflen
;
937 /* This value represents the speed of the "unlock" command.
938 As unlock is quite fast (it does not do anything), I put
939 4096 ko/s = 4 Mo / s */
940 /* FIXME: hwbuf speed */
941 caps
->dwUnlockTransferRate
= 4096;
942 caps
->dwPlayCpuOverhead
= 0;
947 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
948 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
950 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
951 DirectSoundDevice
*device
= This
->dsound
->device
;
952 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
955 WARN("invalid parameter\n");
959 *ppobj
= NULL
; /* assume failure */
961 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
962 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
963 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
968 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
969 /* a primary buffer can't have a DirectSoundBuffer8 interface */
970 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
971 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
972 return E_NOINTERFACE
;
975 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
976 ERR("app requested IDirectSoundNotify on primary buffer\n");
977 /* FIXME: should we support this? */
978 return E_NOINTERFACE
;
981 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
982 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
983 return E_NOINTERFACE
;
986 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
987 if (!device
->listener
)
988 IDirectSound3DListenerImpl_Create(This
, &device
->listener
);
989 if (device
->listener
) {
990 *ppobj
= device
->listener
;
991 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
995 WARN("IID_IDirectSound3DListener failed\n");
996 return E_NOINTERFACE
;
999 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1000 FIXME("app requested IKsPropertySet on primary buffer\n");
1001 return E_NOINTERFACE
;
1004 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1005 return E_NOINTERFACE
;
1008 static const IDirectSoundBuffer8Vtbl dspbvt
=
1010 PrimaryBufferImpl_QueryInterface
,
1011 PrimaryBufferImpl_AddRef
,
1012 PrimaryBufferImpl_Release
,
1013 PrimaryBufferImpl_GetCaps
,
1014 PrimaryBufferImpl_GetCurrentPosition
,
1015 PrimaryBufferImpl_GetFormat
,
1016 PrimaryBufferImpl_GetVolume
,
1017 PrimaryBufferImpl_GetPan
,
1018 PrimaryBufferImpl_GetFrequency
,
1019 PrimaryBufferImpl_GetStatus
,
1020 PrimaryBufferImpl_Initialize
,
1021 PrimaryBufferImpl_Lock
,
1022 PrimaryBufferImpl_Play
,
1023 PrimaryBufferImpl_SetCurrentPosition
,
1024 PrimaryBufferImpl_SetFormat
,
1025 PrimaryBufferImpl_SetVolume
,
1026 PrimaryBufferImpl_SetPan
,
1027 PrimaryBufferImpl_SetFrequency
,
1028 PrimaryBufferImpl_Stop
,
1029 PrimaryBufferImpl_Unlock
,
1030 PrimaryBufferImpl_Restore
,
1031 PrimaryBufferImpl_SetFX
,
1032 PrimaryBufferImpl_AcquireResources
,
1033 PrimaryBufferImpl_GetObjectInPath
1036 HRESULT WINAPI
PrimaryBufferImpl_Create(
1037 IDirectSoundImpl
*ds
,
1038 PrimaryBufferImpl
**pdsb
,
1039 LPCDSBUFFERDESC dsbd
)
1041 PrimaryBufferImpl
*dsb
;
1042 TRACE("%p,%p,%p)\n",ds
,pdsb
,dsbd
);
1044 if (dsbd
->lpwfxFormat
) {
1045 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1047 return DSERR_INVALIDPARAM
;
1050 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1053 WARN("out of memory\n");
1055 return DSERR_OUTOFMEMORY
;
1060 dsb
->lpVtbl
= &dspbvt
;
1062 CopyMemory(&ds
->device
->dsbd
, dsbd
, sizeof(*dsbd
));
1064 TRACE("Created primary buffer at %p\n", dsb
);
1065 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1066 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1067 ds
->device
->pwfx
->wFormatTag
, ds
->device
->pwfx
->nChannels
, ds
->device
->pwfx
->nSamplesPerSec
,
1068 ds
->device
->pwfx
->nAvgBytesPerSec
, ds
->device
->pwfx
->nBlockAlign
,
1069 ds
->device
->pwfx
->wBitsPerSample
, ds
->device
->pwfx
->cbSize
);