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
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
33 #include <math.h> /* Insomnia - pow() function */
44 #include "wine/windef16.h"
45 #include "wine/debug.h"
48 #include "dsound_private.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
52 void DSOUND_RecalcPrimary(IDirectSoundImpl
*This
)
57 sw
= This
->pwfx
->nChannels
* (This
->pwfx
->wBitsPerSample
/ 8);
60 /* let fragment size approximate the timer delay */
61 fraglen
= (This
->pwfx
->nSamplesPerSec
* DS_TIME_DEL
/ 1000) * sw
;
62 /* reduce fragment size until an integer number of them fits in the buffer */
63 /* (FIXME: this may or may not be a good idea) */
64 while (This
->buflen
% fraglen
) fraglen
-= sw
;
65 This
->fraglen
= fraglen
;
66 TRACE("fraglen=%ld\n", This
->fraglen
);
68 /* calculate the 10ms write lead */
69 This
->writelead
= (This
->pwfx
->nSamplesPerSec
/ 100) * sw
;
72 static HRESULT
DSOUND_PrimaryOpen(IDirectSoundImpl
*This
)
77 /* are we using waveOut stuff? */
82 /* Start in pause mode, to allow buffers to get filled */
83 waveOutPause(This
->hwo
);
84 if (This
->state
== STATE_PLAYING
) This
->state
= STATE_STARTING
;
85 else if (This
->state
== STATE_STOPPING
) This
->state
= STATE_STOPPED
;
86 /* use fragments of 10ms (1/100s) each (which should get us within
87 * the documented write cursor lead of 10-15ms) */
88 buflen
= ((This
->pwfx
->nAvgBytesPerSec
/ 100) & ~3) * DS_HEL_FRAGS
;
89 TRACE("desired buflen=%ld, old buffer=%p\n", buflen
, This
->buffer
);
90 /* reallocate emulated primary buffer */
93 newbuf
= HeapReAlloc(GetProcessHeap(),0,This
->buffer
,buflen
);
95 newbuf
= HeapAlloc(GetProcessHeap(),0,buflen
);
98 ERR("failed to allocate primary buffer\n");
99 merr
= DSERR_OUTOFMEMORY
;
100 /* but the old buffer might still exist and must be re-prepared */
102 This
->buffer
= newbuf
;
103 This
->buflen
= buflen
;
108 This
->fraglen
= This
->buflen
/ DS_HEL_FRAGS
;
110 /* prepare fragment headers */
111 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
112 This
->pwave
[c
]->lpData
= This
->buffer
+ c
*This
->fraglen
;
113 This
->pwave
[c
]->dwBufferLength
= This
->fraglen
;
114 This
->pwave
[c
]->dwUser
= (DWORD
)This
;
115 This
->pwave
[c
]->dwFlags
= 0;
116 This
->pwave
[c
]->dwLoops
= 0;
117 err
= mmErr(waveOutPrepareHeader(This
->hwo
,This
->pwave
[c
],sizeof(WAVEHDR
)));
120 waveOutUnprepareHeader(This
->hwo
,This
->pwave
[c
],sizeof(WAVEHDR
));
130 memset(This
->buffer
, (This
->pwfx
->wBitsPerSample
== 16) ? 0 : 128, This
->buflen
);
131 TRACE("fraglen=%ld\n", This
->fraglen
);
132 DSOUND_WaveQueue(This
, (DWORD
)-1);
134 if ((err
== DS_OK
) && (merr
!= DS_OK
))
136 } else if (!This
->hwbuf
) {
137 err
= IDsDriver_CreateSoundBuffer(This
->driver
,This
->pwfx
,
138 DSBCAPS_PRIMARYBUFFER
,0,
139 &(This
->buflen
),&(This
->buffer
),
140 (LPVOID
*)&(This
->hwbuf
));
142 WARN("IDsDriver_CreateSoundBuffer failed\n");
146 if (dsound
->state
== STATE_PLAYING
) dsound
->state
= STATE_STARTING
;
147 else if (dsound
->state
== STATE_STOPPING
) dsound
->state
= STATE_STOPPED
;
154 static void DSOUND_PrimaryClose(IDirectSoundImpl
*This
)
156 TRACE("(%p)\n",This
);
158 /* are we using waveOut stuff? */
162 This
->pwqueue
= (DWORD
)-1; /* resetting queues */
163 waveOutReset(This
->hwo
);
164 for (c
=0; c
<DS_HEL_FRAGS
; c
++)
165 waveOutUnprepareHeader(This
->hwo
, This
->pwave
[c
], sizeof(WAVEHDR
));
168 if (IDsDriverBuffer_Release(This
->hwbuf
) == 0)
173 HRESULT
DSOUND_PrimaryCreate(IDirectSoundImpl
*This
)
176 TRACE("(%p)\n",This
);
178 This
->buflen
= This
->pwfx
->nAvgBytesPerSec
;
180 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
183 err
= IDsDriver_CreateSoundBuffer(This
->driver
,This
->pwfx
,
184 DSBCAPS_PRIMARYBUFFER
,0,
185 &(This
->buflen
),&(This
->buffer
),
186 (LPVOID
*)&(This
->hwbuf
));
188 WARN("IDsDriver_CreateSoundBuffer failed\n");
193 /* Allocate memory for HEL buffer headers */
195 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
196 This
->pwave
[c
] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(WAVEHDR
));
197 if (!This
->pwave
[c
]) {
198 /* Argh, out of memory */
200 HeapFree(GetProcessHeap(),0,This
->pwave
[c
]);
202 WARN("out of memory\n");
203 return DSERR_OUTOFMEMORY
;
208 err
= DSOUND_PrimaryOpen(This
);
211 WARN("DSOUND_PrimaryOpen failed\n");
215 /* calculate fragment size and write lead */
216 DSOUND_RecalcPrimary(This
);
217 This
->state
= STATE_STOPPED
;
221 HRESULT
DSOUND_PrimaryDestroy(IDirectSoundImpl
*This
)
223 TRACE("(%p)\n",This
);
225 DSOUND_PrimaryClose(This
);
228 if (IDsDriverBuffer_Release(This
->hwbuf
) == 0)
233 for (c
=0; c
<DS_HEL_FRAGS
; c
++) {
234 HeapFree(GetProcessHeap(),0,This
->pwave
[c
]);
238 HeapFree(GetProcessHeap(),0,This
->pwfx
);
244 HRESULT
DSOUND_PrimaryPlay(IDirectSoundImpl
*This
)
247 TRACE("(%p)\n",This
);
250 err
= IDsDriverBuffer_Play(This
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
252 WARN("IDsDriverBuffer_Play failed\n");
254 err
= mmErr(waveOutRestart(This
->hwo
));
256 WARN("waveOutRestart failed\n");
262 HRESULT
DSOUND_PrimaryStop(IDirectSoundImpl
*This
)
265 TRACE("(%p)\n",This
);
268 err
= IDsDriverBuffer_Stop(This
->hwbuf
);
269 if (err
== DSERR_BUFFERLOST
) {
270 DWORD flags
= CALLBACK_FUNCTION
;
271 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
272 flags
|= WAVE_DIRECTSOUND
;
273 /* Wine-only: the driver wants us to reopen the device */
274 /* FIXME: check for errors */
275 IDsDriverBuffer_Release(This
->hwbuf
);
276 waveOutClose(This
->hwo
);
278 err
= mmErr(waveOutOpen(&(This
->hwo
), This
->drvdesc
.dnDevNode
,
279 This
->pwfx
, (DWORD
)DSOUND_callback
, (DWORD
)This
,
282 err
= IDsDriver_CreateSoundBuffer(This
->driver
,This
->pwfx
,
283 DSBCAPS_PRIMARYBUFFER
,0,
284 &(This
->buflen
),&(This
->buffer
),
285 (LPVOID
)&(This
->hwbuf
));
287 WARN("IDsDriver_CreateSoundBuffer failed\n");
289 WARN("waveOutOpen failed\n");
291 } else if (err
!= DS_OK
) {
292 WARN("IDsDriverBuffer_Stop failed\n");
295 err
= mmErr(waveOutPause(This
->hwo
));
297 WARN("waveOutPause failed\n");
302 HRESULT
DSOUND_PrimaryGetPosition(IDirectSoundImpl
*This
, LPDWORD playpos
, LPDWORD writepos
)
304 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
307 HRESULT err
=IDsDriverBuffer_GetPosition(This
->hwbuf
,playpos
,writepos
);
309 WARN("IDsDriverBuffer_GetPosition failed\n");
315 mtime
.wType
= TIME_BYTES
;
316 waveOutGetPosition(This
->hwo
, &mtime
, sizeof(mtime
));
317 mtime
.u
.cb
= mtime
.u
.cb
% This
->buflen
;
318 *playpos
= mtime
.u
.cb
;
321 /* the writepos should only be used by apps with WRITEPRIMARY priority,
322 * in which case our software mixer is disabled anyway */
323 *writepos
= (This
->pwplay
+ ds_hel_margin
) * This
->fraglen
;
324 while (*writepos
>= This
->buflen
)
325 *writepos
-= This
->buflen
;
328 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, This
, GetTickCount());
332 /*******************************************************************************
335 /* This sets this format for the <em>Primary Buffer Only</em> */
336 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
337 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
338 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
340 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
341 IDirectSoundImpl
* dsound
= This
->dsound
;
343 int i
, alloc_size
, cp_size
;
344 DWORD nSamplesPerSec
;
345 TRACE("(%p,%p)\n",This
,wfex
);
347 if (This
->dsound
->priolevel
== DSSCL_NORMAL
) {
348 WARN("failed priority check!\n");
349 return DSERR_PRIOLEVELNEEDED
;
352 /* Let's be pedantic! */
354 WARN("invalid parameter: wfex==NULL!\n");
355 return DSERR_INVALIDPARAM
;
357 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
358 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
359 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
360 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
361 wfex
->wBitsPerSample
, wfex
->cbSize
);
364 RtlAcquireResourceExclusive(&(dsound
->buffer_list_lock
), TRUE
);
366 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
367 alloc_size
= sizeof(WAVEFORMATEX
);
368 cp_size
= sizeof(PCMWAVEFORMAT
);
370 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
372 dsound
->pwfx
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,dsound
->pwfx
,alloc_size
);
374 nSamplesPerSec
= dsound
->pwfx
->nSamplesPerSec
;
376 memcpy(dsound
->pwfx
, wfex
, cp_size
);
378 if (dsound
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) {
379 DWORD flags
= CALLBACK_FUNCTION
;
380 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
)
381 flags
|= WAVE_DIRECTSOUND
;
382 /* FIXME: check for errors */
383 DSOUND_PrimaryClose(dsound
);
384 waveOutClose(dsound
->hwo
);
386 err
= mmErr(waveOutOpen(&(dsound
->hwo
), dsound
->drvdesc
.dnDevNode
,
387 dsound
->pwfx
, (DWORD
)DSOUND_callback
, (DWORD
)dsound
,
390 err
= DSOUND_PrimaryOpen(dsound
);
392 WARN("DSOUND_PrimaryOpen failed\n");
393 RtlReleaseResource(&(dsound
->buffer_list_lock
));
397 WARN("waveOutOpen failed\n");
398 RtlReleaseResource(&(dsound
->buffer_list_lock
));
401 } else if (dsound
->hwbuf
) {
402 err
= IDsDriverBuffer_SetFormat(dsound
->hwbuf
, dsound
->pwfx
);
403 if (err
== DSERR_BUFFERLOST
) {
404 /* Wine-only: the driver wants us to recreate the HW buffer */
405 IDsDriverBuffer_Release(dsound
->hwbuf
);
406 err
= IDsDriver_CreateSoundBuffer(dsound
->driver
,dsound
->pwfx
,
407 DSBCAPS_PRIMARYBUFFER
,0,
408 &(dsound
->buflen
),&(dsound
->buffer
),
409 (LPVOID
)&(dsound
->hwbuf
));
411 WARN("IDsDriver_CreateSoundBuffer failed\n");
412 RtlReleaseResource(&(dsound
->buffer_list_lock
));
415 if (dsound
->state
== STATE_PLAYING
) dsound
->state
= STATE_STARTING
;
416 else if (dsound
->state
== STATE_STOPPING
) dsound
->state
= STATE_STOPPED
;
418 WARN("IDsDriverBuffer_SetFormat failed\n");
419 RtlReleaseResource(&(dsound
->buffer_list_lock
));
422 /* FIXME: should we set err back to DS_OK in all cases ? */
424 DSOUND_RecalcPrimary(dsound
);
426 if (nSamplesPerSec
!= dsound
->pwfx
->nSamplesPerSec
) {
427 IDirectSoundBufferImpl
** dsb
= dsound
->buffers
;
428 for (i
= 0; i
< dsound
->nrofbuffers
; i
++, dsb
++) {
430 EnterCriticalSection(&((*dsb
)->lock
));
432 (*dsb
)->freqAdjust
= ((*dsb
)->freq
<< DSOUND_FREQSHIFT
) /
433 wfex
->nSamplesPerSec
;
435 LeaveCriticalSection(&((*dsb
)->lock
));
440 RtlReleaseResource(&(dsound
->buffer_list_lock
));
446 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
447 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
449 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
450 IDirectSoundImpl
* dsound
= This
->dsound
;
453 HRESULT hres
= DS_OK
;
455 TRACE("(%p,%ld)\n",This
,vol
);
457 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
458 WARN("control unavailable\n");
459 return DSERR_CONTROLUNAVAIL
;
462 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
463 WARN("invalid parameter: vol = %ld\n", vol
);
464 return DSERR_INVALIDPARAM
;
468 EnterCriticalSection(&(dsound
->mixlock
));
470 waveOutGetVolume(dsound
->hwo
, &factors
);
471 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
472 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
473 DSOUND_AmpFactorToVolPan(&volpan
);
474 if (vol
!= volpan
.lVolume
) {
476 DSOUND_RecalcVolPan(&volpan
);
478 hres
= IDsDriverBuffer_SetVolumePan(dsound
->hwbuf
, &volpan
);
480 WARN("IDsDriverBuffer_SetVolumePan failed\n");
482 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
483 waveOutSetVolume(dsound
->hwo
, ampfactors
);
487 LeaveCriticalSection(&(dsound
->mixlock
));
493 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
494 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
496 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
499 TRACE("(%p,%p)\n",This
,vol
);
501 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
502 WARN("control unavailable\n");
503 return DSERR_CONTROLUNAVAIL
;
507 WARN("invalid parameter: vol = NULL\n");
508 return DSERR_INVALIDPARAM
;
511 waveOutGetVolume(dsound
->hwo
, &factors
);
512 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
513 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
514 DSOUND_AmpFactorToVolPan(&volpan
);
515 *vol
= volpan
.lVolume
;
519 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
520 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
522 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
524 TRACE("(%p,%ld)\n",This
,freq
);
526 /* You cannot set the frequency of the primary buffer */
527 WARN("control unavailable\n");
528 return DSERR_CONTROLUNAVAIL
;
531 static HRESULT WINAPI
PrimaryBufferImpl_Play(
532 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
534 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
535 IDirectSoundImpl
* dsound
= This
->dsound
;
537 TRACE("(%p,%08lx,%08lx,%08lx)\n",
538 This
,reserved1
,reserved2
,flags
541 if (!(flags
& DSBPLAY_LOOPING
)) {
542 WARN("invalid parameter: flags = %08lx\n", flags
);
543 return DSERR_INVALIDPARAM
;
547 EnterCriticalSection(&(dsound
->mixlock
));
549 if (dsound
->state
== STATE_STOPPED
)
550 dsound
->state
= STATE_STARTING
;
551 else if (dsound
->state
== STATE_STOPPING
)
552 dsound
->state
= STATE_PLAYING
;
554 LeaveCriticalSection(&(dsound
->mixlock
));
560 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
562 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
563 IDirectSoundImpl
* dsound
= This
->dsound
;
565 TRACE("(%p)\n",This
);
568 EnterCriticalSection(&(dsound
->mixlock
));
570 if (dsound
->state
== STATE_PLAYING
)
571 dsound
->state
= STATE_STOPPING
;
572 else if (dsound
->state
== STATE_STARTING
)
573 dsound
->state
= STATE_STOPPED
;
575 LeaveCriticalSection(&(dsound
->mixlock
));
581 static DWORD WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
) {
582 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
585 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
586 ref
= InterlockedIncrement(&(This
->ref
));
591 static DWORD WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
) {
592 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
595 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
596 ref
= InterlockedDecrement(&(This
->ref
));
599 This
->dsound
->primary
= NULL
;
600 HeapFree(GetProcessHeap(),0,This
);
601 TRACE("(%p) released\n",This
);
607 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
608 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
611 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
612 IDirectSoundImpl
* dsound
= This
->dsound
;
614 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
615 hres
= DSOUND_PrimaryGetPosition(dsound
, playpos
, writepos
);
617 WARN("DSOUND_PrimaryGetPosition failed\n");
621 if (dsound
->state
!= STATE_STOPPED
)
622 /* apply the documented 10ms lead to writepos */
623 *writepos
+= dsound
->writelead
;
624 while (*writepos
>= dsound
->buflen
) *writepos
-= dsound
->buflen
;
626 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, This
, GetTickCount());
630 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
631 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
633 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
634 TRACE("(%p,%p), thread is %04lx\n",This
,status
,GetCurrentThreadId());
636 if (status
== NULL
) {
637 WARN("invalid parameter: status == NULL\n");
638 return DSERR_INVALIDPARAM
;
642 if ((This
->dsound
->state
== STATE_STARTING
) ||
643 (This
->dsound
->state
== STATE_PLAYING
))
644 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
646 TRACE("status=%lx\n", *status
);
651 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
652 LPDIRECTSOUNDBUFFER8 iface
,
658 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
659 TRACE("(%p,%p,%ld,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
661 size
= sizeof(WAVEFORMATEX
) + This
->dsound
->pwfx
->cbSize
;
663 if (lpwf
) { /* NULL is valid */
664 if (wfsize
>= size
) {
665 memcpy(lpwf
,This
->dsound
->pwfx
,size
);
669 WARN("invalid parameter: wfsize to small\n");
672 return DSERR_INVALIDPARAM
;
676 *wfwritten
= sizeof(WAVEFORMATEX
) + This
->dsound
->pwfx
->cbSize
;
678 WARN("invalid parameter: wfwritten == NULL\n");
679 return DSERR_INVALIDPARAM
;
686 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
687 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
689 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
690 IDirectSoundImpl
* dsound
= This
->dsound
;
692 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
704 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
) {
705 WARN("failed priority check!\n");
706 return DSERR_PRIOLEVELNEEDED
;
709 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
712 /* GetCurrentPosition does too much magic to duplicate here */
713 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writepos
);
715 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
718 writecursor
+= writepos
;
720 while (writecursor
>= dsound
->buflen
)
721 writecursor
-= dsound
->buflen
;
722 if (flags
& DSBLOCK_ENTIREBUFFER
)
723 writebytes
= dsound
->buflen
;
724 if (writebytes
> dsound
->buflen
)
725 writebytes
= dsound
->buflen
;
727 if (!(dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && dsound
->hwbuf
) {
729 hres
= IDsDriverBuffer_Lock(dsound
->hwbuf
,
730 lplpaudioptr1
, audiobytes1
,
731 lplpaudioptr2
, audiobytes2
,
732 writecursor
, writebytes
,
735 WARN("IDsDriverBuffer_Lock failed\n");
739 if (writecursor
+writebytes
<= dsound
->buflen
) {
740 *(LPBYTE
*)lplpaudioptr1
= dsound
->buffer
+writecursor
;
741 *audiobytes1
= writebytes
;
743 *(LPBYTE
*)lplpaudioptr2
= NULL
;
746 TRACE("->%ld.0\n",writebytes
);
748 *(LPBYTE
*)lplpaudioptr1
= dsound
->buffer
+writecursor
;
749 *audiobytes1
= dsound
->buflen
-writecursor
;
751 *(LPBYTE
*)lplpaudioptr2
= dsound
->buffer
;
753 *audiobytes2
= writebytes
-(dsound
->buflen
-writecursor
);
754 TRACE("->%ld.%ld\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
760 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
761 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
763 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
764 TRACE("(%p,%ld)\n",This
,newpos
);
766 /* You cannot set the position of the primary buffer */
767 WARN("invalid call\n");
768 return DSERR_INVALIDCALL
;
771 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
772 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
774 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
775 IDirectSoundImpl
* dsound
= This
->dsound
;
778 HRESULT hres
= DS_OK
;
780 TRACE("(%p,%ld)\n",This
,pan
);
782 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
783 WARN("control unavailable\n");
784 return DSERR_CONTROLUNAVAIL
;
787 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
788 WARN("invalid parameter: pan = %ld\n", pan
);
789 return DSERR_INVALIDPARAM
;
793 EnterCriticalSection(&(dsound
->mixlock
));
795 waveOutGetVolume(dsound
->hwo
, &factors
);
796 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
797 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
798 DSOUND_AmpFactorToVolPan(&volpan
);
799 if (pan
!= volpan
.lPan
) {
801 DSOUND_RecalcVolPan(&volpan
);
803 hres
= IDsDriverBuffer_SetVolumePan(dsound
->hwbuf
, &volpan
);
805 WARN("IDsDriverBuffer_SetVolumePan failed\n");
807 ampfactors
= (volpan
.dwTotalLeftAmpFactor
& 0xffff) | (volpan
.dwTotalRightAmpFactor
<< 16);
808 waveOutSetVolume(dsound
->hwo
, ampfactors
);
812 LeaveCriticalSection(&(dsound
->mixlock
));
818 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
819 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
821 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
824 TRACE("(%p,%p)\n",This
,pan
);
826 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
827 WARN("control unavailable\n");
828 return DSERR_CONTROLUNAVAIL
;
832 WARN("invalid parameter: pan == NULL\n");
833 return DSERR_INVALIDPARAM
;
836 waveOutGetVolume(dsound
->hwo
, &factors
);
837 volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
838 volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
839 DSOUND_AmpFactorToVolPan(&volpan
);
844 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
845 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
847 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
848 IDirectSoundImpl
* dsound
= This
->dsound
;
850 TRACE("(%p,%p,%ld,%p,%ld)\n", This
,p1
,x1
,p2
,x2
);
852 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
) {
853 WARN("failed priority check!\n");
854 return DSERR_PRIOLEVELNEEDED
;
857 if (!(dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && dsound
->hwbuf
) {
860 hres
= IDsDriverBuffer_Unlock(dsound
->hwbuf
, p1
, x1
, p2
, x2
);
862 WARN("IDsDriverBuffer_Unlock failed\n");
870 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
871 LPDIRECTSOUNDBUFFER8 iface
873 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
874 FIXME("(%p):stub\n",This
);
878 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
879 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
881 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
882 TRACE("(%p,%p)\n",This
,freq
);
885 WARN("invalid parameter: freq == NULL\n");
886 return DSERR_INVALIDPARAM
;
889 if (!(This
->dsound
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
890 WARN("control unavailable\n");
891 return DSERR_CONTROLUNAVAIL
;
894 *freq
= This
->dsound
->pwfx
->nSamplesPerSec
;
895 TRACE("-> %ld\n", *freq
);
900 static HRESULT WINAPI
PrimaryBufferImpl_SetFX(
901 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
903 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
906 FIXME("(%p,%lu,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
909 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
911 WARN("control unavailable\n");
912 return DSERR_CONTROLUNAVAIL
;
915 static HRESULT WINAPI
PrimaryBufferImpl_AcquireResources(
916 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
918 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
921 FIXME("(%p,%08lu,%lu,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
924 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
926 WARN("control unavailable\n");
927 return DSERR_CONTROLUNAVAIL
;
930 static HRESULT WINAPI
PrimaryBufferImpl_GetObjectInPath(
931 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
933 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
935 FIXME("(%p,%s,%lu,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
937 WARN("control unavailable\n");
938 return DSERR_CONTROLUNAVAIL
;
941 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
942 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
944 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
945 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
946 DPRINTF("Re-Init!!!\n");
947 WARN("already initialized\n");
948 return DSERR_ALREADYINITIALIZED
;
951 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
952 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
954 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
955 TRACE("(%p)->(%p)\n",This
,caps
);
958 WARN("invalid parameter: caps == NULL\n");
959 return DSERR_INVALIDPARAM
;
962 if (caps
->dwSize
< sizeof(*caps
)) {
963 WARN("invalid parameter: caps->dwSize = %ld: < %d\n", caps
->dwSize
, sizeof(*caps
));
964 return DSERR_INVALIDPARAM
;
967 caps
->dwFlags
= This
->dsound
->dsbd
.dwFlags
;
968 if (This
->dsound
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
969 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
971 caps
->dwBufferBytes
= This
->dsound
->buflen
;
973 /* This value represents the speed of the "unlock" command.
974 As unlock is quite fast (it does not do anything), I put
975 4096 ko/s = 4 Mo / s */
976 /* FIXME: hwbuf speed */
977 caps
->dwUnlockTransferRate
= 4096;
978 caps
->dwPlayCpuOverhead
= 0;
983 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
984 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
986 PrimaryBufferImpl
*This
= (PrimaryBufferImpl
*)iface
;
987 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
990 WARN("invalid parameter\n");
994 *ppobj
= NULL
; /* assume failure */
996 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
997 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
998 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
1003 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1004 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1005 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1006 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1007 return E_NOINTERFACE
;
1010 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1011 ERR("app requested IDirectSoundNotify on primary buffer\n");
1012 /* FIXME: should we support this? */
1013 return E_NOINTERFACE
;
1016 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1017 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1018 return E_NOINTERFACE
;
1021 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1022 if (!This
->dsound
->listener
)
1023 IDirectSound3DListenerImpl_Create(This
, &This
->dsound
->listener
);
1024 if (This
->dsound
->listener
) {
1025 *ppobj
= This
->dsound
->listener
;
1026 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1030 WARN("IID_IDirectSound3DListener failed\n");
1031 return E_NOINTERFACE
;
1034 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1035 FIXME("app requested IKsPropertySet on primary buffer\n");
1036 return E_NOINTERFACE
;
1039 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1040 return E_NOINTERFACE
;
1043 static IDirectSoundBuffer8Vtbl dspbvt
=
1045 PrimaryBufferImpl_QueryInterface
,
1046 PrimaryBufferImpl_AddRef
,
1047 PrimaryBufferImpl_Release
,
1048 PrimaryBufferImpl_GetCaps
,
1049 PrimaryBufferImpl_GetCurrentPosition
,
1050 PrimaryBufferImpl_GetFormat
,
1051 PrimaryBufferImpl_GetVolume
,
1052 PrimaryBufferImpl_GetPan
,
1053 PrimaryBufferImpl_GetFrequency
,
1054 PrimaryBufferImpl_GetStatus
,
1055 PrimaryBufferImpl_Initialize
,
1056 PrimaryBufferImpl_Lock
,
1057 PrimaryBufferImpl_Play
,
1058 PrimaryBufferImpl_SetCurrentPosition
,
1059 PrimaryBufferImpl_SetFormat
,
1060 PrimaryBufferImpl_SetVolume
,
1061 PrimaryBufferImpl_SetPan
,
1062 PrimaryBufferImpl_SetFrequency
,
1063 PrimaryBufferImpl_Stop
,
1064 PrimaryBufferImpl_Unlock
,
1065 PrimaryBufferImpl_Restore
,
1066 PrimaryBufferImpl_SetFX
,
1067 PrimaryBufferImpl_AcquireResources
,
1068 PrimaryBufferImpl_GetObjectInPath
1071 HRESULT WINAPI
PrimaryBufferImpl_Create(
1072 IDirectSoundImpl
*ds
,
1073 PrimaryBufferImpl
**pdsb
,
1074 LPCDSBUFFERDESC dsbd
)
1076 PrimaryBufferImpl
*dsb
;
1078 TRACE("%p,%p,%p)\n",ds
,pdsb
,dsbd
);
1080 if (dsbd
->lpwfxFormat
) {
1081 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1083 return DSERR_INVALIDPARAM
;
1086 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1089 WARN("out of memory\n");
1091 return DSERR_OUTOFMEMORY
;
1096 dsb
->lpVtbl
= &dspbvt
;
1098 memcpy(&ds
->dsbd
, dsbd
, sizeof(*dsbd
));
1100 TRACE("Created primary buffer at %p\n", dsb
);
1101 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1102 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1103 ds
->pwfx
->wFormatTag
, ds
->pwfx
->nChannels
, ds
->pwfx
->nSamplesPerSec
,
1104 ds
->pwfx
->nAvgBytesPerSec
, ds
->pwfx
->nBlockAlign
,
1105 ds
->pwfx
->wBitsPerSample
, ds
->pwfx
->cbSize
);