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>
35 #define NONAMELESSSTRUCT
36 #define NONAMELESSUNION
46 #include "wine/windef16.h"
47 #include "wine/debug.h"
50 #include "dsound_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
54 /*******************************************************************************
57 static HRESULT WINAPI
IDirectSoundNotifyImpl_QueryInterface(
58 LPDIRECTSOUNDNOTIFY iface
,REFIID riid
,LPVOID
*ppobj
60 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
61 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
63 if (This
->dsb
== NULL
) {
64 WARN("invalid parameter\n");
68 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER
)This
->dsb
, riid
, ppobj
);
71 static ULONG WINAPI
IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface
)
73 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
74 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
75 return InterlockedIncrement(&(This
->ref
));
78 static ULONG WINAPI
IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface
) {
79 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
82 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
84 ref
= InterlockedDecrement(&(This
->ref
));
86 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER
)This
->dsb
);
87 This
->dsb
->notify
= NULL
;
88 HeapFree(GetProcessHeap(),0,This
);
89 TRACE("(%p) released\n",This
);
94 static HRESULT WINAPI
IDirectSoundNotifyImpl_SetNotificationPositions(
95 LPDIRECTSOUNDNOTIFY iface
,DWORD howmuch
,LPCDSBPOSITIONNOTIFY notify
97 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
98 TRACE("(%p,0x%08lx,%p)\n",This
,howmuch
,notify
);
100 if (howmuch
> 0 && notify
== NULL
) {
101 WARN("invalid parameter: notify == NULL\n");
102 return DSERR_INVALIDPARAM
;
105 if (TRACE_ON(dsound
)) {
107 for (i
=0;i
<howmuch
;i
++)
108 TRACE("notify at %ld to 0x%08lx\n",
109 notify
[i
].dwOffset
,(DWORD
)notify
[i
].hEventNotify
);
112 if (This
->dsb
->hwnotify
) {
114 hres
= IDsDriverNotify_SetNotificationPositions(This
->dsb
->hwnotify
, howmuch
, notify
);
116 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
118 } else if (howmuch
> 0) {
119 /* Make an internal copy of the caller-supplied array.
120 * Replace the existing copy if one is already present. */
121 if (This
->dsb
->notifies
)
122 This
->dsb
->notifies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
123 This
->dsb
->notifies
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
125 This
->dsb
->notifies
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
126 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
128 if (This
->dsb
->notifies
== NULL
) {
129 WARN("out of memory\n");
130 return DSERR_OUTOFMEMORY
;
132 memcpy(This
->dsb
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
133 This
->dsb
->nrofnotifies
= howmuch
;
135 if (This
->dsb
->notifies
) {
136 HeapFree(GetProcessHeap(), 0, This
->dsb
->notifies
);
137 This
->dsb
->notifies
= NULL
;
139 This
->dsb
->nrofnotifies
= 0;
145 IDirectSoundNotifyVtbl dsnvt
=
147 IDirectSoundNotifyImpl_QueryInterface
,
148 IDirectSoundNotifyImpl_AddRef
,
149 IDirectSoundNotifyImpl_Release
,
150 IDirectSoundNotifyImpl_SetNotificationPositions
,
153 HRESULT WINAPI
IDirectSoundNotifyImpl_Create(
154 IDirectSoundBufferImpl
* dsb
,
155 IDirectSoundNotifyImpl
**pdsn
)
157 IDirectSoundNotifyImpl
* dsn
;
158 TRACE("(%p,%p)\n",dsb
,pdsn
);
160 dsn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(dsn
));
163 WARN("out of memory\n");
164 return DSERR_OUTOFMEMORY
;
168 dsn
->lpVtbl
= &dsnvt
;
171 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)dsb
);
177 HRESULT WINAPI
IDirectSoundNotifyImpl_Destroy(
178 IDirectSoundNotifyImpl
*pdsn
)
180 TRACE("(%p)\n",pdsn
);
182 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY
)pdsn
) > 0);
187 /*******************************************************************************
191 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFormat(
192 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
194 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
196 TRACE("(%p,%p)\n",This
,wfex
);
197 /* This method is not available on secondary buffers */
198 WARN("invalid call\n");
199 return DSERR_INVALIDCALL
;
202 static HRESULT WINAPI
IDirectSoundBufferImpl_SetVolume(
203 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
205 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
207 HRESULT hres
= DS_OK
;
209 TRACE("(%p,%ld)\n",This
,vol
);
211 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
212 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This
->dsbd
.dwFlags
);
213 return DSERR_CONTROLUNAVAIL
;
216 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
217 WARN("invalid parameter: vol = %ld\n", vol
);
218 return DSERR_INVALIDPARAM
;
222 EnterCriticalSection(&(This
->lock
));
224 if (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
225 oldVol
= This
->ds3db_lVolume
;
226 This
->ds3db_lVolume
= vol
;
228 oldVol
= This
->volpan
.lVolume
;
229 This
->volpan
.lVolume
= vol
;
231 DSOUND_RecalcVolPan(&(This
->volpan
));
236 hres
= IDsDriverBuffer_SetVolumePan(This
->hwbuf
, &(This
->volpan
));
238 WARN("IDsDriverBuffer_SetVolumePan failed\n");
240 DSOUND_ForceRemix(This
);
243 LeaveCriticalSection(&(This
->lock
));
249 static HRESULT WINAPI
IDirectSoundBufferImpl_GetVolume(
250 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
252 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
253 TRACE("(%p,%p)\n",This
,vol
);
255 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
256 WARN("control unavailable\n");
257 return DSERR_CONTROLUNAVAIL
;
261 WARN("invalid parameter: vol == NULL\n");
262 return DSERR_INVALIDPARAM
;
265 *vol
= This
->volpan
.lVolume
;
270 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFrequency(
271 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
273 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
276 TRACE("(%p,%ld)\n",This
,freq
);
278 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
279 WARN("control unavailable\n");
280 return DSERR_CONTROLUNAVAIL
;
283 if (freq
== DSBFREQUENCY_ORIGINAL
)
284 freq
= This
->pwfx
->nSamplesPerSec
;
286 if ((freq
< DSBFREQUENCY_MIN
) || (freq
> DSBFREQUENCY_MAX
)) {
287 WARN("invalid parameter: freq = %ld\n", freq
);
288 return DSERR_INVALIDPARAM
;
292 EnterCriticalSection(&(This
->lock
));
294 oldFreq
= This
->freq
;
296 if (freq
!= oldFreq
) {
297 This
->freqAdjust
= (freq
<< DSOUND_FREQSHIFT
) / This
->dsound
->pwfx
->nSamplesPerSec
;
298 This
->nAvgBytesPerSec
= freq
* This
->pwfx
->nBlockAlign
;
299 DSOUND_RecalcFormat(This
);
301 DSOUND_ForceRemix(This
);
304 LeaveCriticalSection(&(This
->lock
));
310 static HRESULT WINAPI
IDirectSoundBufferImpl_Play(
311 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
313 HRESULT hres
= DS_OK
;
314 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
315 TRACE("(%p,%08lx,%08lx,%08lx)\n",This
,reserved1
,reserved2
,flags
);
318 EnterCriticalSection(&(This
->lock
));
320 This
->playflags
= flags
;
321 if (This
->state
== STATE_STOPPED
) {
323 This
->startpos
= This
->buf_mixpos
;
324 This
->state
= STATE_STARTING
;
325 } else if (This
->state
== STATE_STOPPING
)
326 This
->state
= STATE_PLAYING
;
328 hres
= IDsDriverBuffer_Play(This
->hwbuf
, 0, 0, This
->playflags
);
330 WARN("IDsDriverBuffer_Play failed\n");
332 This
->state
= STATE_PLAYING
;
335 LeaveCriticalSection(&(This
->lock
));
341 static HRESULT WINAPI
IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
343 HRESULT hres
= DS_OK
;
344 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
345 TRACE("(%p)\n",This
);
348 EnterCriticalSection(&(This
->lock
));
350 if (This
->state
== STATE_PLAYING
)
351 This
->state
= STATE_STOPPING
;
352 else if (This
->state
== STATE_STARTING
)
353 This
->state
= STATE_STOPPED
;
355 hres
= IDsDriverBuffer_Stop(This
->hwbuf
);
357 WARN("IDsDriverBuffer_Stop failed\n");
359 This
->state
= STATE_STOPPED
;
361 DSOUND_CheckEvent(This
, 0);
363 LeaveCriticalSection(&(This
->lock
));
369 static ULONG WINAPI
IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
371 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
372 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
373 return InterlockedIncrement(&(This
->ref
));
376 static ULONG WINAPI
IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
378 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
381 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
383 ref
= InterlockedDecrement(&(This
->ref
));
387 DSOUND_RemoveBuffer(This
->dsound
, This
);
389 This
->lock
.DebugInfo
->Spare
[1] = 0;
390 DeleteCriticalSection(&(This
->lock
));
393 IDsDriverBuffer_Release(This
->hwbuf
);
394 if (This
->dsound
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
) {
396 if (This
->buffer
->ref
==0) {
397 HeapFree(GetProcessHeap(),0,This
->buffer
->memory
);
398 HeapFree(GetProcessHeap(),0,This
->buffer
);
403 if (This
->buffer
->ref
==0) {
404 HeapFree(GetProcessHeap(),0,This
->buffer
->memory
);
405 HeapFree(GetProcessHeap(),0,This
->buffer
);
409 if (This
->notifies
!= NULL
)
410 HeapFree(GetProcessHeap(), 0, This
->notifies
);
413 HeapFree(GetProcessHeap(), 0, This
->pwfx
);
415 HeapFree(GetProcessHeap(),0,This
);
417 TRACE("(%p) released\n",This
);
421 DWORD
DSOUND_CalcPlayPosition(IDirectSoundBufferImpl
*This
,
422 DWORD state
, DWORD pplay
, DWORD pwrite
, DWORD pmix
, DWORD bmix
)
426 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay
, pmix
);
427 TRACE("this mixpos=%ld, time=%ld\n", bmix
, GetTickCount());
429 /* the actual primary play position (pplay) is always behind last mixed (pmix),
430 * unless the computer is too slow or something */
431 /* we need to know how far away we are from there */
432 #if 0 /* we'll never fill the primary entirely */
434 if ((state
== STATE_PLAYING
) || (state
== STATE_STOPPING
)) {
435 /* wow, the software mixer is really doing well,
436 * seems the entire primary buffer is filled! */
437 pmix
+= This
->dsound
->buflen
;
439 /* else: the primary buffer is not playing, so probably empty */
442 if (pmix
< pplay
) pmix
+= This
->dsound
->buflen
; /* wraparound */
444 /* detect buffer underrun */
445 if (pwrite
< pplay
) pwrite
+= This
->dsound
->buflen
; /* wraparound */
447 if (pmix
> (ds_snd_queue_max
* This
->dsound
->fraglen
+ pwrite
+ This
->dsound
->writelead
)) {
448 WARN("detected an underrun: primary queue was %ld\n",pmix
);
451 /* divide the offset by its sample size */
452 pmix
/= This
->dsound
->pwfx
->nBlockAlign
;
453 TRACE("primary back-samples=%ld\n",pmix
);
454 /* adjust for our frequency */
455 pmix
= (pmix
* This
->freqAdjust
) >> DSOUND_FREQSHIFT
;
456 /* multiply by our own sample size */
457 pmix
*= This
->pwfx
->nBlockAlign
;
458 TRACE("this back-offset=%ld\n", pmix
);
459 /* subtract from our last mixed position */
461 while (bplay
< pmix
) bplay
+= This
->buflen
; /* wraparound */
463 if (This
->leadin
&& ((bplay
< This
->startpos
) || (bplay
> bmix
))) {
464 /* seems we haven't started playing yet */
465 TRACE("this still in lead-in phase\n");
466 bplay
= This
->startpos
;
468 /* return the result */
472 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCurrentPosition(
473 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
476 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
477 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
479 hres
=IDsDriverBuffer_GetPosition(This
->hwbuf
,playpos
,writepos
);
481 WARN("IDsDriverBuffer_GetPosition failed\n");
485 if (playpos
&& (This
->state
!= STATE_PLAYING
)) {
486 /* we haven't been merged into the primary buffer (yet) */
487 *playpos
= This
->buf_mixpos
;
488 } else if (playpos
) {
489 DWORD pplay
, pwrite
, lplay
, splay
, pstate
;
490 /* let's get this exact; first, recursively call GetPosition on the primary */
491 EnterCriticalSection(&(This
->dsound
->mixlock
));
492 if (DSOUND_PrimaryGetPosition(This
->dsound
, &pplay
, &pwrite
) != DS_OK
)
493 WARN("DSOUND_PrimaryGetPosition failed\n");
494 /* detect HEL mode underrun */
495 pstate
= This
->dsound
->state
;
496 if (!(This
->dsound
->hwbuf
|| This
->dsound
->pwqueue
)) {
497 TRACE("detected an underrun\n");
499 if (pstate
== STATE_PLAYING
)
500 pstate
= STATE_STARTING
;
501 else if (pstate
== STATE_STOPPING
)
502 pstate
= STATE_STOPPED
;
504 /* get data for ourselves while we still have the lock */
505 pstate
&= This
->state
;
506 lplay
= This
->primary_mixpos
;
507 splay
= This
->buf_mixpos
;
508 if ((This
->dsbd
.dwFlags
& DSBCAPS_GETCURRENTPOSITION2
) || This
->dsound
->hwbuf
) {
509 /* calculate play position using this */
510 *playpos
= DSOUND_CalcPlayPosition(This
, pstate
, pplay
, pwrite
, lplay
, splay
);
512 /* (unless the app isn't using GETCURRENTPOSITION2) */
513 /* don't know exactly how this should be handled...
514 * the docs says that play cursor is reported as directly
515 * behind write cursor, hmm... */
516 /* let's just do what might work for Half-Life */
518 wp
= (This
->dsound
->pwplay
+ ds_hel_margin
) * This
->dsound
->fraglen
;
519 wp
%= This
->dsound
->buflen
;
520 *playpos
= DSOUND_CalcPlayPosition(This
, pstate
, wp
, pwrite
, lplay
, splay
);
522 LeaveCriticalSection(&(This
->dsound
->mixlock
));
525 *writepos
= This
->buf_mixpos
;
528 if (This
->state
!= STATE_STOPPED
) {
529 /* apply the documented 10ms lead to writepos */
530 *writepos
+= This
->writelead
;
532 *writepos
%= This
->buflen
;
535 This
->last_playpos
= *playpos
;
536 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, This
, GetTickCount());
540 static HRESULT WINAPI
IDirectSoundBufferImpl_GetStatus(
541 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
543 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
544 TRACE("(%p,%p), thread is %04lx\n",This
,status
,GetCurrentThreadId());
546 if (status
== NULL
) {
547 WARN("invalid parameter: status = NULL\n");
548 return DSERR_INVALIDPARAM
;
552 if ((This
->state
== STATE_STARTING
) || (This
->state
== STATE_PLAYING
)) {
553 *status
|= DSBSTATUS_PLAYING
;
554 if (This
->playflags
& DSBPLAY_LOOPING
)
555 *status
|= DSBSTATUS_LOOPING
;
558 TRACE("status=%lx\n", *status
);
563 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFormat(
564 LPDIRECTSOUNDBUFFER8 iface
,
570 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
571 TRACE("(%p,%p,%ld,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
573 size
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
575 if (lpwf
) { /* NULL is valid */
576 if (wfsize
>= size
) {
577 memcpy(lpwf
,This
->pwfx
,size
);
581 WARN("invalid parameter: wfsize to small\n");
584 return DSERR_INVALIDPARAM
;
588 *wfwritten
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
590 WARN("invalid parameter: wfwritten == NULL\n");
591 return DSERR_INVALIDPARAM
;
598 static HRESULT WINAPI
IDirectSoundBufferImpl_Lock(
599 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
601 HRESULT hres
= DS_OK
;
602 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
604 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
616 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
618 /* GetCurrentPosition does too much magic to duplicate here */
619 hres
= IDirectSoundBufferImpl_GetCurrentPosition(iface
, NULL
, &writepos
);
621 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
624 writecursor
+= writepos
;
626 writecursor
%= This
->buflen
;
627 if (flags
& DSBLOCK_ENTIREBUFFER
)
628 writebytes
= This
->buflen
;
629 if (writebytes
> This
->buflen
)
630 writebytes
= This
->buflen
;
632 EnterCriticalSection(&(This
->lock
));
634 if ((writebytes
== This
->buflen
) &&
635 ((This
->state
== STATE_STARTING
) ||
636 (This
->state
== STATE_PLAYING
)))
637 /* some games, like Half-Life, try to be clever (not) and
638 * keep one secondary buffer, and mix sounds into it itself,
639 * locking the entire buffer every time... so we can just forget
640 * about tracking the last-written-to-position... */
641 This
->probably_valid_to
= (DWORD
)-1;
643 This
->probably_valid_to
= writecursor
;
645 if (!(This
->dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDSECONDARYLOCK
) && This
->hwbuf
) {
646 hres
= IDsDriverBuffer_Lock(This
->hwbuf
,
647 lplpaudioptr1
, audiobytes1
,
648 lplpaudioptr2
, audiobytes2
,
649 writecursor
, writebytes
,
652 WARN("IDsDriverBuffer_Lock failed\n");
653 LeaveCriticalSection(&(This
->lock
));
658 if (writecursor
+writebytes
<= This
->buflen
) {
659 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
660 *audiobytes1
= writebytes
;
662 *(LPBYTE
*)lplpaudioptr2
= NULL
;
665 TRACE("->%ld.0\n",writebytes
);
667 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
668 *audiobytes1
= This
->buflen
-writecursor
;
670 *(LPBYTE
*)lplpaudioptr2
= This
->buffer
->memory
;
672 *audiobytes2
= writebytes
-(This
->buflen
-writecursor
);
673 TRACE("->%ld.%ld\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
675 if (This
->state
== STATE_PLAYING
) {
676 /* if the segment between playpos and buf_mixpos is touched,
677 * we need to cancel some mixing */
678 /* we'll assume that the app always calls GetCurrentPosition before
679 * locking a playing buffer, so that last_playpos is up-to-date */
680 if (This
->buf_mixpos
>= This
->last_playpos
) {
681 if (This
->buf_mixpos
> writecursor
&&
682 This
->last_playpos
< writecursor
+writebytes
)
685 if (This
->buf_mixpos
> writecursor
||
686 This
->last_playpos
< writecursor
+writebytes
)
690 TRACE("locking prebuffered region, ouch\n");
691 DSOUND_MixCancelAt(This
, writecursor
);
696 LeaveCriticalSection(&(This
->lock
));
700 static HRESULT WINAPI
IDirectSoundBufferImpl_SetCurrentPosition(
701 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
703 HRESULT hres
= DS_OK
;
704 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
705 TRACE("(%p,%ld)\n",This
,newpos
);
708 EnterCriticalSection(&(This
->lock
));
710 newpos
%= This
->buflen
;
711 This
->buf_mixpos
= newpos
;
713 hres
= IDsDriverBuffer_SetPosition(This
->hwbuf
, This
->buf_mixpos
);
715 WARN("IDsDriverBuffer_SetPosition failed\n");
718 LeaveCriticalSection(&(This
->lock
));
724 static HRESULT WINAPI
IDirectSoundBufferImpl_SetPan(
725 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
727 HRESULT hres
= DS_OK
;
728 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
730 TRACE("(%p,%ld)\n",This
,pan
);
732 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
733 WARN("invalid parameter: pan = %ld\n", pan
);
734 return DSERR_INVALIDPARAM
;
737 /* You cannot use both pan and 3D controls */
738 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
739 (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
)) {
740 WARN("control unavailable\n");
741 return DSERR_CONTROLUNAVAIL
;
745 EnterCriticalSection(&(This
->lock
));
747 if (This
->volpan
.lPan
!= pan
) {
748 This
->volpan
.lPan
= pan
;
749 DSOUND_RecalcVolPan(&(This
->volpan
));
752 hres
= IDsDriverBuffer_SetVolumePan(This
->hwbuf
, &(This
->volpan
));
754 WARN("IDsDriverBuffer_SetVolumePan failed\n");
756 DSOUND_ForceRemix(This
);
759 LeaveCriticalSection(&(This
->lock
));
765 static HRESULT WINAPI
IDirectSoundBufferImpl_GetPan(
766 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
768 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
769 TRACE("(%p,%p)\n",This
,pan
);
771 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
772 WARN("control unavailable\n");
773 return DSERR_CONTROLUNAVAIL
;
777 WARN("invalid parameter: pan = NULL\n");
778 return DSERR_INVALIDPARAM
;
781 *pan
= This
->volpan
.lPan
;
786 static HRESULT WINAPI
IDirectSoundBufferImpl_Unlock(
787 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
789 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
790 DWORD probably_valid_to
;
791 HRESULT hres
= DS_OK
;
793 TRACE("(%p,%p,%ld,%p,%ld)\n", This
,p1
,x1
,p2
,x2
);
796 EnterCriticalSection(&(This
->lock
));
798 if (!(This
->dsound
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDSECONDARYLOCK
) && This
->hwbuf
) {
799 hres
= IDsDriverBuffer_Unlock(This
->hwbuf
, p1
, x1
, p2
, x2
);
801 WARN("IDsDriverBuffer_Unlock failed\n");
805 if (p2
) probably_valid_to
= (((LPBYTE
)p2
)-This
->buffer
->memory
) + x2
;
806 else probably_valid_to
= (((LPBYTE
)p1
)-This
->buffer
->memory
) + x1
;
807 probably_valid_to
%= This
->buflen
;
808 if ((probably_valid_to
== 0) && ((x1
+x2
) == This
->buflen
) &&
809 ((This
->state
== STATE_STARTING
) ||
810 (This
->state
== STATE_PLAYING
)))
811 /* see IDirectSoundBufferImpl_Lock */
812 probably_valid_to
= (DWORD
)-1;
813 This
->probably_valid_to
= probably_valid_to
;
816 LeaveCriticalSection(&(This
->lock
));
819 TRACE("probably_valid_to=%ld\n", This
->probably_valid_to
);
823 static HRESULT WINAPI
IDirectSoundBufferImpl_Restore(
824 LPDIRECTSOUNDBUFFER8 iface
826 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
827 FIXME("(%p):stub\n",This
);
831 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFrequency(
832 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
834 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
835 TRACE("(%p,%p)\n",This
,freq
);
838 WARN("invalid parameter: freq = NULL\n");
839 return DSERR_INVALIDPARAM
;
843 TRACE("-> %ld\n", *freq
);
848 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFX(
849 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
851 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
854 FIXME("(%p,%lu,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
857 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
859 WARN("control unavailable\n");
860 return DSERR_CONTROLUNAVAIL
;
863 static HRESULT WINAPI
IDirectSoundBufferImpl_AcquireResources(
864 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
866 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
869 FIXME("(%p,%08lu,%lu,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
872 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
874 WARN("control unavailable\n");
875 return DSERR_CONTROLUNAVAIL
;
878 static HRESULT WINAPI
IDirectSoundBufferImpl_GetObjectInPath(
879 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
881 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
883 FIXME("(%p,%s,%lu,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
885 WARN("control unavailable\n");
886 return DSERR_CONTROLUNAVAIL
;
889 static HRESULT WINAPI
IDirectSoundBufferImpl_Initialize(
890 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
892 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
893 FIXME("(%p,%p,%p):stub\n",This
,dsound
,dbsd
);
894 DPRINTF("Re-Init!!!\n");
895 WARN("already initialized\n");
896 return DSERR_ALREADYINITIALIZED
;
899 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCaps(
900 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
902 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
903 TRACE("(%p)->(%p)\n",This
,caps
);
906 WARN("invalid parameter: caps == NULL\n");
907 return DSERR_INVALIDPARAM
;
910 if (caps
->dwSize
< sizeof(*caps
)) {
911 WARN("invalid parameter: caps->dwSize = %ld < %d\n",caps
->dwSize
, sizeof(*caps
));
912 return DSERR_INVALIDPARAM
;
915 caps
->dwFlags
= This
->dsbd
.dwFlags
;
916 if (This
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
917 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
919 caps
->dwBufferBytes
= This
->buflen
;
921 /* This value represents the speed of the "unlock" command.
922 As unlock is quite fast (it does not do anything), I put
923 4096 ko/s = 4 Mo / s */
924 /* FIXME: hwbuf speed */
925 caps
->dwUnlockTransferRate
= 4096;
926 caps
->dwPlayCpuOverhead
= 0;
931 static HRESULT WINAPI
IDirectSoundBufferImpl_QueryInterface(
932 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
934 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
936 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
939 WARN("invalid parameter\n");
943 *ppobj
= NULL
; /* assume failure */
945 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
946 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ||
947 IsEqualGUID(riid
, &IID_IDirectSoundBuffer8
) ) {
949 SecondaryBufferImpl_Create(This
, &(This
->dsb
));
951 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
955 WARN("IID_IDirectSoundBuffer\n");
956 return E_NOINTERFACE
;
959 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
961 IDirectSoundNotifyImpl_Create(This
, &(This
->notify
));
963 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY
)This
->notify
);
964 *ppobj
= This
->notify
;
967 WARN("IID_IDirectSoundNotify\n");
968 return E_NOINTERFACE
;
971 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
973 IDirectSound3DBufferImpl_Create(This
, &(This
->ds3db
));
975 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER
)This
->ds3db
);
976 *ppobj
= This
->ds3db
;
979 WARN("IID_IDirectSound3DBuffer\n");
980 return E_NOINTERFACE
;
983 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
984 ERR("app requested IDirectSound3DListener on secondary buffer\n");
985 return E_NOINTERFACE
;
988 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
989 /* only supported on hardware 3D secondary buffers */
990 if (!(This
->dsbd
.dwFlags
& DSBCAPS_PRIMARYBUFFER
) &&
991 (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) &&
992 (This
->hwbuf
!= NULL
) ) {
994 IKsBufferPropertySetImpl_Create(This
, &(This
->iks
));
996 IKsPropertySet_AddRef((LPKSPROPERTYSET
)*ppobj
);
1001 WARN("IID_IKsPropertySet\n");
1002 return E_NOINTERFACE
;
1005 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1007 return E_NOINTERFACE
;
1010 static IDirectSoundBuffer8Vtbl dsbvt
=
1012 IDirectSoundBufferImpl_QueryInterface
,
1013 IDirectSoundBufferImpl_AddRef
,
1014 IDirectSoundBufferImpl_Release
,
1015 IDirectSoundBufferImpl_GetCaps
,
1016 IDirectSoundBufferImpl_GetCurrentPosition
,
1017 IDirectSoundBufferImpl_GetFormat
,
1018 IDirectSoundBufferImpl_GetVolume
,
1019 IDirectSoundBufferImpl_GetPan
,
1020 IDirectSoundBufferImpl_GetFrequency
,
1021 IDirectSoundBufferImpl_GetStatus
,
1022 IDirectSoundBufferImpl_Initialize
,
1023 IDirectSoundBufferImpl_Lock
,
1024 IDirectSoundBufferImpl_Play
,
1025 IDirectSoundBufferImpl_SetCurrentPosition
,
1026 IDirectSoundBufferImpl_SetFormat
,
1027 IDirectSoundBufferImpl_SetVolume
,
1028 IDirectSoundBufferImpl_SetPan
,
1029 IDirectSoundBufferImpl_SetFrequency
,
1030 IDirectSoundBufferImpl_Stop
,
1031 IDirectSoundBufferImpl_Unlock
,
1032 IDirectSoundBufferImpl_Restore
,
1033 IDirectSoundBufferImpl_SetFX
,
1034 IDirectSoundBufferImpl_AcquireResources
,
1035 IDirectSoundBufferImpl_GetObjectInPath
1038 HRESULT WINAPI
IDirectSoundBufferImpl_Create(
1039 IDirectSoundImpl
*ds
,
1040 IDirectSoundBufferImpl
**pdsb
,
1041 LPCDSBUFFERDESC dsbd
)
1043 IDirectSoundBufferImpl
*dsb
;
1044 LPWAVEFORMATEX wfex
= dsbd
->lpwfxFormat
;
1045 HRESULT err
= DS_OK
;
1047 int use_hw
, alloc_size
, cp_size
;
1048 TRACE("(%p,%p,%p)\n",ds
,pdsb
,dsbd
);
1050 if (dsbd
->dwBufferBytes
< DSBSIZE_MIN
|| dsbd
->dwBufferBytes
> DSBSIZE_MAX
) {
1051 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd
->dwBufferBytes
);
1053 return DSERR_INVALIDPARAM
; /* FIXME: which error? */
1056 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1059 WARN("out of memory\n");
1061 return DSERR_OUTOFMEMORY
;
1064 TRACE("Created buffer at %p\n", dsb
);
1069 dsb
->lpVtbl
= &dsbvt
;
1072 /* size depends on version */
1073 memcpy(&dsb
->dsbd
, dsbd
, dsbd
->dwSize
);
1075 /* variable sized struct so calculate size based on format */
1076 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
1077 alloc_size
= sizeof(WAVEFORMATEX
);
1078 cp_size
= sizeof(PCMWAVEFORMAT
);
1080 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
1082 dsb
->pwfx
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,alloc_size
);
1083 if (dsb
->pwfx
== NULL
) {
1084 WARN("out of memory\n");
1085 HeapFree(GetProcessHeap(),0,dsb
);
1087 return DSERR_OUTOFMEMORY
;
1090 memcpy(dsb
->pwfx
, wfex
, cp_size
);
1092 dsb
->buflen
= dsbd
->dwBufferBytes
;
1093 dsb
->freq
= dsbd
->lpwfxFormat
->nSamplesPerSec
;
1096 dsb
->notifies
= NULL
;
1097 dsb
->nrofnotifies
= 0;
1100 /* Check necessary hardware mixing capabilities */
1101 if (wfex
->nChannels
==2) capf
|= DSCAPS_SECONDARYSTEREO
;
1102 else capf
|= DSCAPS_SECONDARYMONO
;
1103 if (wfex
->wBitsPerSample
==16) capf
|= DSCAPS_SECONDARY16BIT
;
1104 else capf
|= DSCAPS_SECONDARY8BIT
;
1106 use_hw
= (ds
->drvcaps
.dwFlags
& capf
) == capf
;
1107 TRACE("use_hw = 0x%08x, capf = 0x%08lx, ds->drvcaps.dwFlags = 0x%08lx\n", use_hw
, capf
, ds
->drvcaps
.dwFlags
);
1109 /* FIXME: check hardware sample rate mixing capabilities */
1110 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1111 /* FIXME: check whether any hardware buffers are left */
1112 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1114 /* Allocate system memory if applicable */
1115 if ((ds
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
) || !use_hw
) {
1116 dsb
->buffer
= HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb
->buffer
)));
1117 if (dsb
->buffer
== NULL
) {
1118 WARN("out of memory\n");
1119 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1120 HeapFree(GetProcessHeap(),0,dsb
);
1122 return DSERR_OUTOFMEMORY
;
1125 dsb
->buffer
->memory
= HeapAlloc(GetProcessHeap(),0,dsb
->buflen
);
1126 if (dsb
->buffer
->memory
== NULL
) {
1127 WARN("out of memory\n");
1128 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1129 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1130 HeapFree(GetProcessHeap(),0,dsb
);
1132 return DSERR_OUTOFMEMORY
;
1134 dsb
->buffer
->ref
= 1;
1137 /* Allocate the hardware buffer */
1139 err
= IDsDriver_CreateSoundBuffer(ds
->driver
,wfex
,dsbd
->dwFlags
,0,
1140 &(dsb
->buflen
),&(dsb
->buffer
->memory
),
1141 (LPVOID
*)&(dsb
->hwbuf
));
1142 /* fall back to software buffer on failure */
1144 TRACE("IDsDriver_CreateSoundBuffer failed, falling back to software buffer\n");
1146 if (ds
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
) {
1147 dsb
->buffer
= HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb
->buffer
)));
1148 if (dsb
->buffer
== NULL
) {
1149 WARN("out of memory\n");
1150 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1151 HeapFree(GetProcessHeap(),0,dsb
);
1153 return DSERR_OUTOFMEMORY
;
1156 dsb
->buffer
->memory
= HeapAlloc(GetProcessHeap(),0,dsb
->buflen
);
1157 if (dsb
->buffer
->memory
== NULL
) {
1158 WARN("out of memory\n");
1159 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1160 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1161 HeapFree(GetProcessHeap(),0,dsb
);
1163 return DSERR_OUTOFMEMORY
;
1165 dsb
->buffer
->ref
= 1;
1171 /* calculate fragment size and write lead */
1172 DSOUND_RecalcFormat(dsb
);
1174 /* It's not necessary to initialize values to zero since */
1175 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1177 dsb
->buf_mixpos
= 0;
1178 dsb
->state
= STATE_STOPPED
;
1180 dsb
->freqAdjust
= (dsb
->freq
<< DSOUND_FREQSHIFT
) /
1181 ds
->pwfx
->nSamplesPerSec
;
1182 dsb
->nAvgBytesPerSec
= dsb
->freq
*
1183 dsbd
->lpwfxFormat
->nBlockAlign
;
1185 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
1186 dsb
->ds3db_ds3db
.dwSize
= sizeof(DS3DBUFFER
);
1187 dsb
->ds3db_ds3db
.vPosition
.x
= 0.0;
1188 dsb
->ds3db_ds3db
.vPosition
.y
= 0.0;
1189 dsb
->ds3db_ds3db
.vPosition
.z
= 0.0;
1190 dsb
->ds3db_ds3db
.vVelocity
.x
= 0.0;
1191 dsb
->ds3db_ds3db
.vVelocity
.y
= 0.0;
1192 dsb
->ds3db_ds3db
.vVelocity
.z
= 0.0;
1193 dsb
->ds3db_ds3db
.dwInsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
1194 dsb
->ds3db_ds3db
.dwOutsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
1195 dsb
->ds3db_ds3db
.vConeOrientation
.x
= 0.0;
1196 dsb
->ds3db_ds3db
.vConeOrientation
.y
= 0.0;
1197 dsb
->ds3db_ds3db
.vConeOrientation
.z
= 0.0;
1198 dsb
->ds3db_ds3db
.lConeOutsideVolume
= DS3D_DEFAULTCONEOUTSIDEVOLUME
;
1199 dsb
->ds3db_ds3db
.flMinDistance
= DS3D_DEFAULTMINDISTANCE
;
1200 dsb
->ds3db_ds3db
.flMaxDistance
= DS3D_DEFAULTMAXDISTANCE
;
1201 dsb
->ds3db_ds3db
.dwMode
= DS3DMODE_NORMAL
;
1203 dsb
->ds3db_need_recalc
= FALSE
;
1204 DSOUND_Calc3DBuffer(dsb
);
1206 DSOUND_RecalcVolPan(&(dsb
->volpan
));
1208 InitializeCriticalSection(&(dsb
->lock
));
1209 dsb
->lock
.DebugInfo
->Spare
[1] = (DWORD
)"DSOUNDBUFFER_lock";
1211 /* register buffer if not primary */
1212 if (!(dsbd
->dwFlags
& DSBCAPS_PRIMARYBUFFER
)) {
1213 err
= DSOUND_AddBuffer(ds
, dsb
);
1215 if (dsb
->buffer
->memory
)
1216 HeapFree(GetProcessHeap(),0,dsb
->buffer
->memory
);
1218 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1219 dsb
->lock
.DebugInfo
->Spare
[1] = 0;
1220 DeleteCriticalSection(&(dsb
->lock
));
1221 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1222 HeapFree(GetProcessHeap(),0,dsb
);
1231 HRESULT WINAPI
IDirectSoundBufferImpl_Destroy(
1232 IDirectSoundBufferImpl
*pdsb
)
1234 TRACE("(%p)\n",pdsb
);
1236 /* This keeps the *_Destroy functions from possibly deleting
1237 * this object until it is ready to be deleted */
1238 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8
)pdsb
);
1241 WARN("iks not NULL\n");
1242 IKsBufferPropertySetImpl_Destroy(pdsb
->iks
);
1247 WARN("ds3db not NULL\n");
1248 IDirectSound3DBufferImpl_Destroy(pdsb
->ds3db
);
1253 WARN("notify not NULL\n");
1254 IDirectSoundNotifyImpl_Destroy(pdsb
->notify
);
1255 pdsb
->notify
= NULL
;
1259 WARN("dsb not NULL\n");
1260 SecondaryBufferImpl_Destroy(pdsb
->dsb
);
1264 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8
)pdsb
) > 0);
1269 /*******************************************************************************
1273 static HRESULT WINAPI
SecondaryBufferImpl_QueryInterface(
1274 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
)
1276 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1277 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
1279 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8
)This
->dsb
,riid
,ppobj
);
1282 static ULONG WINAPI
SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
1284 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
1285 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
1286 return InterlockedIncrement(&(This
->ref
));
1289 static ULONG WINAPI
SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
1291 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
1293 TRACE("(%p) ref was %ld, thread is %04lx\n",This
, This
->ref
, GetCurrentThreadId());
1295 ref
= InterlockedDecrement(&(This
->ref
));
1297 This
->dsb
->dsb
= NULL
;
1298 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1299 HeapFree(GetProcessHeap(),0,This
);
1300 TRACE("(%p) released\n",This
);
1305 static HRESULT WINAPI
SecondaryBufferImpl_GetCaps(
1306 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
)
1308 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1309 TRACE("(%p)->(%p)\n",This
,caps
);
1311 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8
)This
->dsb
,caps
);
1314 static HRESULT WINAPI
SecondaryBufferImpl_GetCurrentPosition(
1315 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
)
1317 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1318 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
1320 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8
)This
->dsb
,playpos
,writepos
);
1323 static HRESULT WINAPI
SecondaryBufferImpl_GetFormat(
1324 LPDIRECTSOUNDBUFFER8 iface
,LPWAVEFORMATEX lpwf
,DWORD wfsize
,LPDWORD wfwritten
)
1326 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1327 TRACE("(%p,%p,%ld,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
1329 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8
)This
->dsb
,lpwf
,wfsize
,wfwritten
);
1332 static HRESULT WINAPI
SecondaryBufferImpl_GetVolume(
1333 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
)
1335 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1336 TRACE("(%p,%p)\n",This
,vol
);
1338 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8
)This
->dsb
,vol
);
1341 static HRESULT WINAPI
SecondaryBufferImpl_GetPan(
1342 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
)
1344 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1345 TRACE("(%p,%p)\n",This
,pan
);
1347 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8
)This
->dsb
,pan
);
1350 static HRESULT WINAPI
SecondaryBufferImpl_GetFrequency(
1351 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
)
1353 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1354 TRACE("(%p,%p)\n",This
,freq
);
1356 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8
)This
->dsb
,freq
);
1359 static HRESULT WINAPI
SecondaryBufferImpl_GetStatus(
1360 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
)
1362 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1363 TRACE("(%p,%p)\n",This
,status
);
1365 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8
)This
->dsb
,status
);
1368 static HRESULT WINAPI
SecondaryBufferImpl_Initialize(
1369 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
)
1371 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1372 TRACE("(%p,%p,%p)\n",This
,dsound
,dbsd
);
1374 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dsound
,dbsd
);
1377 static HRESULT WINAPI
SecondaryBufferImpl_Lock(
1378 LPDIRECTSOUNDBUFFER8 iface
,
1381 LPVOID lplpaudioptr1
,
1382 LPDWORD audiobytes1
,
1383 LPVOID lplpaudioptr2
,
1384 LPDWORD audiobytes2
,
1387 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1388 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1389 This
,writecursor
,writebytes
,lplpaudioptr1
,audiobytes1
,lplpaudioptr2
,audiobytes2
,dwFlags
);
1391 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8
)This
->dsb
,
1392 writecursor
,writebytes
,lplpaudioptr1
,audiobytes1
,lplpaudioptr2
,audiobytes2
,dwFlags
);
1395 static HRESULT WINAPI
SecondaryBufferImpl_Play(
1396 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
)
1398 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1399 TRACE("(%p,%08lx,%08lx,%08lx)\n",This
,reserved1
,reserved2
,flags
);
1401 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8
)This
->dsb
,reserved1
,reserved2
,flags
);
1404 static HRESULT WINAPI
SecondaryBufferImpl_SetCurrentPosition(
1405 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
)
1407 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1408 TRACE("(%p,%ld)\n",This
,newpos
);
1410 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8
)This
->dsb
,newpos
);
1413 static HRESULT WINAPI
SecondaryBufferImpl_SetFormat(
1414 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
)
1416 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1417 TRACE("(%p,%p)\n",This
,wfex
);
1419 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8
)This
->dsb
,wfex
);
1422 static HRESULT WINAPI
SecondaryBufferImpl_SetVolume(
1423 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
)
1425 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1426 TRACE("(%p,%ld)\n",This
,vol
);
1428 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8
)This
->dsb
,vol
);
1431 static HRESULT WINAPI
SecondaryBufferImpl_SetPan(
1432 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
)
1434 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1435 TRACE("(%p,%ld)\n",This
,pan
);
1437 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8
)This
->dsb
,pan
);
1440 static HRESULT WINAPI
SecondaryBufferImpl_SetFrequency(
1441 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
)
1443 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1444 TRACE("(%p,%ld)\n",This
,freq
);
1446 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8
)This
->dsb
,freq
);
1449 static HRESULT WINAPI
SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
1451 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1452 TRACE("(%p)\n",This
);
1454 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1457 static HRESULT WINAPI
SecondaryBufferImpl_Unlock(
1458 LPDIRECTSOUNDBUFFER8 iface
,
1459 LPVOID lpvAudioPtr1
,
1460 DWORD dwAudioBytes1
,
1461 LPVOID lpvAudioPtr2
,
1462 DWORD dwAudioBytes2
)
1464 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1465 TRACE("(%p,%p,%ld,%p,%ld)\n",
1466 This
, lpvAudioPtr1
, dwAudioBytes1
, lpvAudioPtr2
, dwAudioBytes2
);
1468 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8
)This
->dsb
,
1469 lpvAudioPtr1
,dwAudioBytes1
,lpvAudioPtr2
,dwAudioBytes2
);
1472 static HRESULT WINAPI
SecondaryBufferImpl_Restore(
1473 LPDIRECTSOUNDBUFFER8 iface
)
1475 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1476 TRACE("(%p)\n",This
);
1478 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1481 static HRESULT WINAPI
SecondaryBufferImpl_SetFX(
1482 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
)
1484 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1485 TRACE("(%p,%lu,%p,%p)\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
1487 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
1490 static HRESULT WINAPI
SecondaryBufferImpl_AcquireResources(
1491 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
)
1493 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1494 TRACE("(%p,%08lu,%lu,%p)\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
1496 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
1499 static HRESULT WINAPI
SecondaryBufferImpl_GetObjectInPath(
1500 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
)
1502 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1503 TRACE("(%p,%s,%lu,%s,%p)\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
1505 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8
)This
->dsb
,rguidObject
,dwIndex
,rguidInterface
,ppObject
);
1508 static IDirectSoundBuffer8Vtbl sbvt
=
1510 SecondaryBufferImpl_QueryInterface
,
1511 SecondaryBufferImpl_AddRef
,
1512 SecondaryBufferImpl_Release
,
1513 SecondaryBufferImpl_GetCaps
,
1514 SecondaryBufferImpl_GetCurrentPosition
,
1515 SecondaryBufferImpl_GetFormat
,
1516 SecondaryBufferImpl_GetVolume
,
1517 SecondaryBufferImpl_GetPan
,
1518 SecondaryBufferImpl_GetFrequency
,
1519 SecondaryBufferImpl_GetStatus
,
1520 SecondaryBufferImpl_Initialize
,
1521 SecondaryBufferImpl_Lock
,
1522 SecondaryBufferImpl_Play
,
1523 SecondaryBufferImpl_SetCurrentPosition
,
1524 SecondaryBufferImpl_SetFormat
,
1525 SecondaryBufferImpl_SetVolume
,
1526 SecondaryBufferImpl_SetPan
,
1527 SecondaryBufferImpl_SetFrequency
,
1528 SecondaryBufferImpl_Stop
,
1529 SecondaryBufferImpl_Unlock
,
1530 SecondaryBufferImpl_Restore
,
1531 SecondaryBufferImpl_SetFX
,
1532 SecondaryBufferImpl_AcquireResources
,
1533 SecondaryBufferImpl_GetObjectInPath
1536 HRESULT WINAPI
SecondaryBufferImpl_Create(
1537 IDirectSoundBufferImpl
*dsb
,
1538 SecondaryBufferImpl
**psb
)
1540 SecondaryBufferImpl
*sb
;
1541 TRACE("(%p,%p)\n",dsb
,psb
);
1543 sb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*sb
));
1546 WARN("out of memory\n");
1548 return DSERR_OUTOFMEMORY
;
1554 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8
)dsb
);
1559 HRESULT WINAPI
SecondaryBufferImpl_Destroy(
1560 SecondaryBufferImpl
*pdsb
)
1562 TRACE("(%p)\n",pdsb
);
1564 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8
)pdsb
) > 0);