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
32 #include "wine/debug.h"
35 #include "dsound_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
39 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=%ld\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=%ld, 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=%ld\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 DSOUND_PrimaryClose(device
);
216 if (device
->driver
) {
218 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
223 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
224 HeapFree(GetProcessHeap(),0,device
->pwave
[c
]);
227 HeapFree(GetProcessHeap(),0,device
->pwfx
);
232 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
235 TRACE("(%p)\n", device
);
238 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
240 WARN("IDsDriverBuffer_Play failed\n");
242 err
= mmErr(waveOutRestart(device
->hwo
));
244 WARN("waveOutRestart failed\n");
250 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
253 TRACE("(%p)\n", device
);
256 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
257 if (err
== DSERR_BUFFERLOST
) {
258 DWORD flags
= CALLBACK_FUNCTION
;
259 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
260 flags
|= WAVE_DIRECTSOUND
;
261 /* Wine-only: the driver wants us to reopen the device */
262 /* FIXME: check for errors */
263 IDsDriverBuffer_Release(device
->hwbuf
);
264 waveOutClose(device
->hwo
);
266 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
267 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
270 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
271 DSBCAPS_PRIMARYBUFFER
,0,
272 &(device
->buflen
),&(device
->buffer
),
273 (LPVOID
)&(device
->hwbuf
));
275 WARN("IDsDriver_CreateSoundBuffer failed\n");
277 WARN("waveOutOpen failed\n");
279 } else if (err
!= DS_OK
) {
280 WARN("IDsDriverBuffer_Stop failed\n");
283 err
= mmErr(waveOutPause(device
->hwo
));
285 WARN("waveOutPause failed\n");
290 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
292 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
295 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
297 WARN("IDsDriverBuffer_GetPosition failed\n");
303 mtime
.wType
= TIME_BYTES
;
304 waveOutGetPosition(device
->hwo
, &mtime
, sizeof(mtime
));
305 mtime
.u
.cb
= mtime
.u
.cb
% device
->buflen
;
306 *playpos
= mtime
.u
.cb
;
309 /* the writepos should only be used by apps with WRITEPRIMARY priority,
310 * in which case our software mixer is disabled anyway */
311 *writepos
= (device
->pwplay
+ ds_hel_margin
) * device
->fraglen
;
312 while (*writepos
>= device
->buflen
)
313 *writepos
-= device
->buflen
;
316 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
320 /*******************************************************************************
323 /* This sets this format for the <em>Primary Buffer Only</em> */
324 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
325 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
326 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
328 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
330 int i
, alloc_size
, cp_size
;
331 DWORD nSamplesPerSec
;
332 TRACE("(%p,%p)\n", iface
, wfex
);
334 if (device
->priolevel
== DSSCL_NORMAL
) {
335 WARN("failed priority check!\n");
336 return DSERR_PRIOLEVELNEEDED
;
339 /* Let's be pedantic! */
341 WARN("invalid parameter: wfex==NULL!\n");
342 return DSERR_INVALIDPARAM
;
344 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
345 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
346 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
347 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
348 wfex
->wBitsPerSample
, wfex
->cbSize
);
351 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
352 EnterCriticalSection(&(device
->mixlock
));
354 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
355 alloc_size
= sizeof(WAVEFORMATEX
);
356 cp_size
= sizeof(PCMWAVEFORMAT
);
358 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
360 device
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,device
->pwfx
,alloc_size
);
362 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
364 CopyMemory(device
->pwfx
, wfex
, cp_size
);
366 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
367 DWORD flags
= CALLBACK_FUNCTION
;
368 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
369 flags
|= WAVE_DIRECTSOUND
;
370 /* FIXME: check for errors */
371 DSOUND_PrimaryClose(device
);
372 waveOutClose(device
->hwo
);
374 err
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
375 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD
)device
,
378 err
= DSOUND_PrimaryOpen(device
);
380 WARN("DSOUND_PrimaryOpen failed\n");
384 WARN("waveOutOpen failed\n");
387 } else if (device
->hwbuf
) {
388 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
389 if (err
== DSERR_BUFFERLOST
) {
390 /* Wine-only: the driver wants us to recreate the HW buffer */
391 IDsDriverBuffer_Release(device
->hwbuf
);
392 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
393 DSBCAPS_PRIMARYBUFFER
,0,
394 &(device
->buflen
),&(device
->buffer
),
395 (LPVOID
)&(device
->hwbuf
));
397 WARN("IDsDriver_CreateSoundBuffer failed\n");
400 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
401 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
403 WARN("IDsDriverBuffer_SetFormat failed\n");
406 /* FIXME: should we set err back to DS_OK in all cases ? */
408 DSOUND_RecalcPrimary(device
);
410 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
) {
411 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
412 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
414 EnterCriticalSection(&((*dsb
)->lock
));
416 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
417 wfex
->nSamplesPerSec
;
419 LeaveCriticalSection(&((*dsb
)->lock
));
425 LeaveCriticalSection(&(device
->mixlock
));
426 RtlReleaseResource(&(device
->buffer_list_lock
));
432 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
433 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
435 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
438 HRESULT hres
= DS_OK
;
439 TRACE("(%p,%ld)\n", iface
, vol
);
441 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
442 WARN("control unavailable\n");
443 return DSERR_CONTROLUNAVAIL
;
446 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
447 WARN("invalid parameter: vol = %ld\n", vol
);
448 return DSERR_INVALIDPARAM
;
452 EnterCriticalSection(&(device
->mixlock
));
454 waveOutGetVolume(device
->hwo
, &factors
);
455 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
456 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
457 DSOUND_AmpFactorToVolPan(&volpan
);
458 if (vol
!= volpan
.lVolume
) {
460 DSOUND_RecalcVolPan(&volpan
);
462 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
464 WARN("IDsDriverBuffer_SetVolumePan failed\n");
466 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
467 waveOutSetVolume(device
->hwo
, ampfactors
);
471 LeaveCriticalSection(&(device
->mixlock
));
477 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
478 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
480 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
483 TRACE("(%p,%p)\n", iface
, vol
);
485 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
486 WARN("control unavailable\n");
487 return DSERR_CONTROLUNAVAIL
;
491 WARN("invalid parameter: vol = NULL\n");
492 return DSERR_INVALIDPARAM
;
495 waveOutGetVolume(device
->hwo
, &factors
);
496 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
497 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
498 DSOUND_AmpFactorToVolPan(&volpan
);
499 *vol
= volpan
.lVolume
;
503 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
504 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
506 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
507 TRACE("(%p,%ld)\n",This
,freq
);
509 /* You cannot set the frequency of the primary buffer */
510 WARN("control unavailable\n");
511 return DSERR_CONTROLUNAVAIL
;
514 static HRESULT WINAPI
PrimaryBufferImpl_Play(
515 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
517 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
518 TRACE("(%p,%08lx,%08lx,%08lx)\n", iface
, reserved1
, reserved2
, flags
);
520 if (!(flags
& DSBPLAY_LOOPING
)) {
521 WARN("invalid parameter: flags = %08lx\n", flags
);
522 return DSERR_INVALIDPARAM
;
526 EnterCriticalSection(&(device
->mixlock
));
528 if (device
->state
== STATE_STOPPED
)
529 device
->state
= STATE_STARTING
;
530 else if (device
->state
== STATE_STOPPING
)
531 device
->state
= STATE_PLAYING
;
533 LeaveCriticalSection(&(device
->mixlock
));
539 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
541 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
542 TRACE("(%p)\n", iface
);
545 EnterCriticalSection(&(device
->mixlock
));
547 if (device
->state
== STATE_PLAYING
)
548 device
->state
= STATE_STOPPING
;
549 else if (device
->state
== STATE_STARTING
)
550 device
->state
= STATE_STOPPED
;
552 LeaveCriticalSection(&(device
->mixlock
));
558 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
560 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
561 ULONG ref
= InterlockedIncrement(&(This
->ref
));
562 TRACE("(%p) ref was %ld\n", This
, ref
- 1);
566 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
568 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
569 DWORD ref
= InterlockedDecrement(&(This
->ref
));
570 TRACE("(%p) ref was %ld\n", This
, ref
+ 1);
573 This
->dsound
->device
->primary
= NULL
;
574 HeapFree(GetProcessHeap(), 0, This
);
575 TRACE("(%p) released\n", This
);
580 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
581 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
584 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
585 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
587 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
589 WARN("DSOUND_PrimaryGetPosition failed\n");
593 if (device
->state
!= STATE_STOPPED
)
594 /* apply the documented 10ms lead to writepos */
595 *writepos
+= device
->writelead
;
596 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
598 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
602 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
603 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
605 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
606 TRACE("(%p,%p)\n", iface
, status
);
608 if (status
== NULL
) {
609 WARN("invalid parameter: status == NULL\n");
610 return DSERR_INVALIDPARAM
;
614 if ((device
->state
== STATE_STARTING
) ||
615 (device
->state
== STATE_PLAYING
))
616 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
618 TRACE("status=%lx\n", *status
);
623 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
624 LPDIRECTSOUNDBUFFER8 iface
,
630 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
631 TRACE("(%p,%p,%ld,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
633 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
635 if (lpwf
) { /* NULL is valid */
636 if (wfsize
>= size
) {
637 CopyMemory(lpwf
,device
->pwfx
,size
);
641 WARN("invalid parameter: wfsize too small\n");
644 return DSERR_INVALIDPARAM
;
648 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
650 WARN("invalid parameter: wfwritten == NULL\n");
651 return DSERR_INVALIDPARAM
;
658 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
659 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
661 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
662 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
674 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
675 WARN("failed priority check!\n");
676 return DSERR_PRIOLEVELNEEDED
;
679 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
682 /* GetCurrentPosition does too much magic to duplicate here */
683 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writepos
);
685 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
688 writecursor
+= writepos
;
690 while (writecursor
>= device
->buflen
)
691 writecursor
-= device
->buflen
;
692 if (flags
& DSBLOCK_ENTIREBUFFER
)
693 writebytes
= device
->buflen
;
694 if (writebytes
> device
->buflen
)
695 writebytes
= device
->buflen
;
697 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
699 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
700 lplpaudioptr1
, audiobytes1
,
701 lplpaudioptr2
, audiobytes2
,
702 writecursor
, writebytes
,
705 WARN("IDsDriverBuffer_Lock failed\n");
709 if (writecursor
+writebytes
<= device
->buflen
) {
710 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
711 *audiobytes1
= writebytes
;
713 *(LPBYTE
*)lplpaudioptr2
= NULL
;
716 TRACE("->%ld.0\n",writebytes
);
718 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
719 *audiobytes1
= device
->buflen
-writecursor
;
721 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
723 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
724 TRACE("->%ld.%ld\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
730 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
731 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
733 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
734 TRACE("(%p,%ld)\n",This
,newpos
);
736 /* You cannot set the position of the primary buffer */
737 WARN("invalid call\n");
738 return DSERR_INVALIDCALL
;
741 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
742 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
744 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
747 HRESULT hres
= DS_OK
;
748 TRACE("(%p,%ld)\n", iface
, pan
);
750 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
751 WARN("control unavailable\n");
752 return DSERR_CONTROLUNAVAIL
;
755 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
756 WARN("invalid parameter: pan = %ld\n", pan
);
757 return DSERR_INVALIDPARAM
;
761 EnterCriticalSection(&(device
->mixlock
));
763 waveOutGetVolume(device
->hwo
, &factors
);
764 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
765 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
766 DSOUND_AmpFactorToVolPan(&volpan
);
767 if (pan
!= volpan
.lPan
) {
769 DSOUND_RecalcVolPan(&volpan
);
771 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &volpan
);
773 WARN("IDsDriverBuffer_SetVolumePan failed\n");
775 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
776 waveOutSetVolume(device
->hwo
, ampfactors
);
780 LeaveCriticalSection(&(device
->mixlock
));
786 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
787 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
789 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
792 TRACE("(%p,%p)\n", iface
, pan
);
794 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
795 WARN("control unavailable\n");
796 return DSERR_CONTROLUNAVAIL
;
800 WARN("invalid parameter: pan == NULL\n");
801 return DSERR_INVALIDPARAM
;
804 waveOutGetVolume(device
->hwo
, &factors
);
805 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
806 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
807 DSOUND_AmpFactorToVolPan(&volpan
);
812 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
813 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
815 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
816 TRACE("(%p,%p,%ld,%p,%ld)\n", iface
, p1
, x1
, p2
, x2
);
818 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
819 WARN("failed priority check!\n");
820 return DSERR_PRIOLEVELNEEDED
;
823 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
826 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
828 WARN("IDsDriverBuffer_Unlock failed\n");
836 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
837 LPDIRECTSOUNDBUFFER8 iface
839 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
840 FIXME("(%p):stub\n",This
);
844 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
845 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
847 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
848 TRACE("(%p,%p)\n", iface
, freq
);
851 WARN("invalid parameter: freq == NULL\n");
852 return DSERR_INVALIDPARAM
;
855 if (!(device
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
856 WARN("control unavailable\n");
857 return DSERR_CONTROLUNAVAIL
;
860 *freq
= device
->pwfx
->nSamplesPerSec
;
861 TRACE("-> %ld\n", *freq
);
866 static HRESULT WINAPI
PrimaryBufferImpl_SetFX(
867 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
869 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
871 FIXME("(%p,%lu,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
874 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
876 WARN("control unavailable\n");
877 return DSERR_CONTROLUNAVAIL
;
880 static HRESULT WINAPI
PrimaryBufferImpl_AcquireResources(
881 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
883 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
885 FIXME("(%p,%08lu,%lu,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
888 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
890 WARN("control unavailable\n");
891 return DSERR_CONTROLUNAVAIL
;
894 static HRESULT WINAPI
PrimaryBufferImpl_GetObjectInPath(
895 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
897 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
898 FIXME("(%p,%s,%lu,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
900 WARN("control unavailable\n");
901 return DSERR_CONTROLUNAVAIL
;
904 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
905 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
907 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
908 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
909 DPRINTF("Re-Init!!!\n");
910 WARN("already initialized\n");
911 return DSERR_ALREADYINITIALIZED
;
914 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
915 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
917 DirectSoundDevice
*device
= ((PrimaryBufferImpl
*)iface
)->dsound
->device
;
918 TRACE("(%p,%p)\n", iface
, caps
);
921 WARN("invalid parameter: caps == NULL\n");
922 return DSERR_INVALIDPARAM
;
925 if (caps
->dwSize
< sizeof(*caps
)) {
926 WARN("invalid parameter: caps->dwSize = %ld: < %d\n", caps
->dwSize
, sizeof(*caps
));
927 return DSERR_INVALIDPARAM
;
930 caps
->dwFlags
= device
->dsbd
.dwFlags
;
931 if (device
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
932 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
934 caps
->dwBufferBytes
= device
->buflen
;
936 /* This value represents the speed of the "unlock" command.
937 As unlock is quite fast (it does not do anything), I put
938 4096 ko/s = 4 Mo / s */
939 /* FIXME: hwbuf speed */
940 caps
->dwUnlockTransferRate
= 4096;
941 caps
->dwPlayCpuOverhead
= 0;
946 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
947 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
949 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
950 DirectSoundDevice
*device
= This
->dsound
->device
;
951 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
954 WARN("invalid parameter\n");
958 *ppobj
= NULL
; /* assume failure */
960 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
961 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
962 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
967 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
968 /* a primary buffer can't have a DirectSoundBuffer8 interface */
969 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
970 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
971 return E_NOINTERFACE
;
974 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
975 ERR("app requested IDirectSoundNotify on primary buffer\n");
976 /* FIXME: should we support this? */
977 return E_NOINTERFACE
;
980 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
981 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
982 return E_NOINTERFACE
;
985 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
986 if (!device
->listener
)
987 IDirectSound3DListenerImpl_Create(This
, &device
->listener
);
988 if (device
->listener
) {
989 *ppobj
= device
->listener
;
990 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
994 WARN("IID_IDirectSound3DListener failed\n");
995 return E_NOINTERFACE
;
998 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
999 FIXME("app requested IKsPropertySet on primary buffer\n");
1000 return E_NOINTERFACE
;
1003 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1004 return E_NOINTERFACE
;
1007 static const IDirectSoundBuffer8Vtbl dspbvt
=
1009 PrimaryBufferImpl_QueryInterface
,
1010 PrimaryBufferImpl_AddRef
,
1011 PrimaryBufferImpl_Release
,
1012 PrimaryBufferImpl_GetCaps
,
1013 PrimaryBufferImpl_GetCurrentPosition
,
1014 PrimaryBufferImpl_GetFormat
,
1015 PrimaryBufferImpl_GetVolume
,
1016 PrimaryBufferImpl_GetPan
,
1017 PrimaryBufferImpl_GetFrequency
,
1018 PrimaryBufferImpl_GetStatus
,
1019 PrimaryBufferImpl_Initialize
,
1020 PrimaryBufferImpl_Lock
,
1021 PrimaryBufferImpl_Play
,
1022 PrimaryBufferImpl_SetCurrentPosition
,
1023 PrimaryBufferImpl_SetFormat
,
1024 PrimaryBufferImpl_SetVolume
,
1025 PrimaryBufferImpl_SetPan
,
1026 PrimaryBufferImpl_SetFrequency
,
1027 PrimaryBufferImpl_Stop
,
1028 PrimaryBufferImpl_Unlock
,
1029 PrimaryBufferImpl_Restore
,
1030 PrimaryBufferImpl_SetFX
,
1031 PrimaryBufferImpl_AcquireResources
,
1032 PrimaryBufferImpl_GetObjectInPath
1035 HRESULT WINAPI
PrimaryBufferImpl_Create(
1036 IDirectSoundImpl
*ds
,
1037 PrimaryBufferImpl
**pdsb
,
1038 LPCDSBUFFERDESC dsbd
)
1040 PrimaryBufferImpl
*dsb
;
1041 TRACE("%p,%p,%p)\n",ds
,pdsb
,dsbd
);
1043 if (dsbd
->lpwfxFormat
) {
1044 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1046 return DSERR_INVALIDPARAM
;
1049 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1052 WARN("out of memory\n");
1054 return DSERR_OUTOFMEMORY
;
1059 dsb
->lpVtbl
= &dspbvt
;
1061 CopyMemory(&ds
->device
->dsbd
, dsbd
, sizeof(*dsbd
));
1063 TRACE("Created primary buffer at %p\n", dsb
);
1064 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1065 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1066 ds
->device
->pwfx
->wFormatTag
, ds
->device
->pwfx
->nChannels
, ds
->device
->pwfx
->nSamplesPerSec
,
1067 ds
->device
->pwfx
->nAvgBytesPerSec
, ds
->device
->pwfx
->nBlockAlign
,
1068 ds
->device
->pwfx
->wBitsPerSample
, ds
->device
->pwfx
->cbSize
);