3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_NO_STATUS
26 #define COM_NO_WINDOWS_H
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
32 //#include "winuser.h"
35 #include <wine/debug.h>
38 #include "dsound_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
42 static HRESULT
SecondaryBufferImpl_Destroy(SecondaryBufferImpl
*pdsb
);
44 /*******************************************************************************
48 struct IDirectSoundNotifyImpl
51 const IDirectSoundNotifyVtbl
*lpVtbl
;
53 IDirectSoundBufferImpl
* dsb
;
56 static HRESULT
IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl
*dsb
,
57 IDirectSoundNotifyImpl
**pdsn
);
58 static HRESULT
IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl
*pdsn
);
60 static HRESULT WINAPI
IDirectSoundNotifyImpl_QueryInterface(
61 LPDIRECTSOUNDNOTIFY iface
,REFIID riid
,LPVOID
*ppobj
63 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
64 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
66 if (This
->dsb
== NULL
) {
67 WARN("invalid parameter\n");
71 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER
)This
->dsb
, riid
, ppobj
);
74 static ULONG WINAPI
IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface
)
76 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
77 ULONG ref
= InterlockedIncrement(&(This
->ref
));
78 TRACE("(%p) ref was %d\n", This
, ref
- 1);
82 static ULONG WINAPI
IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface
)
84 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
85 ULONG ref
= InterlockedDecrement(&(This
->ref
));
86 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
89 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER
)This
->dsb
);
90 This
->dsb
->notify
= NULL
;
91 HeapFree(GetProcessHeap(), 0, This
);
92 TRACE("(%p) released\n", This
);
97 static HRESULT WINAPI
IDirectSoundNotifyImpl_SetNotificationPositions(
98 LPDIRECTSOUNDNOTIFY iface
,DWORD howmuch
,LPCDSBPOSITIONNOTIFY notify
100 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
101 TRACE("(%p,0x%08x,%p)\n",This
,howmuch
,notify
);
103 if (howmuch
> 0 && notify
== NULL
) {
104 WARN("invalid parameter: notify == NULL\n");
105 return DSERR_INVALIDPARAM
;
108 if (TRACE_ON(dsound
)) {
110 for (i
=0;i
<howmuch
;i
++)
111 TRACE("notify at %d to %p\n",
112 notify
[i
].dwOffset
,notify
[i
].hEventNotify
);
115 if (This
->dsb
->hwnotify
) {
117 hres
= IDsDriverNotify_SetNotificationPositions(This
->dsb
->hwnotify
, howmuch
, notify
);
119 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
121 } else if (howmuch
> 0) {
122 /* Make an internal copy of the caller-supplied array.
123 * Replace the existing copy if one is already present. */
124 HeapFree(GetProcessHeap(), 0, This
->dsb
->notifies
);
125 This
->dsb
->notifies
= HeapAlloc(GetProcessHeap(), 0,
126 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
128 if (This
->dsb
->notifies
== NULL
) {
129 WARN("out of memory\n");
130 return DSERR_OUTOFMEMORY
;
132 CopyMemory(This
->dsb
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
133 This
->dsb
->nrofnotifies
= howmuch
;
135 HeapFree(GetProcessHeap(), 0, This
->dsb
->notifies
);
136 This
->dsb
->notifies
= NULL
;
137 This
->dsb
->nrofnotifies
= 0;
143 static const IDirectSoundNotifyVtbl dsnvt
=
145 IDirectSoundNotifyImpl_QueryInterface
,
146 IDirectSoundNotifyImpl_AddRef
,
147 IDirectSoundNotifyImpl_Release
,
148 IDirectSoundNotifyImpl_SetNotificationPositions
,
151 static HRESULT
IDirectSoundNotifyImpl_Create(
152 IDirectSoundBufferImpl
* dsb
,
153 IDirectSoundNotifyImpl
**pdsn
)
155 IDirectSoundNotifyImpl
* dsn
;
156 TRACE("(%p,%p)\n",dsb
,pdsn
);
158 dsn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*dsn
));
161 WARN("out of memory\n");
162 return DSERR_OUTOFMEMORY
;
166 dsn
->lpVtbl
= &dsnvt
;
169 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)dsb
);
175 static HRESULT
IDirectSoundNotifyImpl_Destroy(
176 IDirectSoundNotifyImpl
*pdsn
)
178 TRACE("(%p)\n",pdsn
);
180 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY
)pdsn
) > 0);
185 /*******************************************************************************
189 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFormat(
190 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
192 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
194 TRACE("(%p,%p)\n",This
,wfex
);
195 /* This method is not available on secondary buffers */
196 WARN("invalid call\n");
197 return DSERR_INVALIDCALL
;
200 static HRESULT WINAPI
IDirectSoundBufferImpl_SetVolume(
201 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
203 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
205 HRESULT hres
= DS_OK
;
207 TRACE("(%p,%d)\n",This
,vol
);
209 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
210 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This
->dsbd
.dwFlags
);
211 return DSERR_CONTROLUNAVAIL
;
214 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
215 WARN("invalid parameter: vol = %d\n", vol
);
216 return DSERR_INVALIDPARAM
;
220 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
222 if (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
223 oldVol
= This
->ds3db_lVolume
;
224 This
->ds3db_lVolume
= vol
;
226 /* recalc 3d volume, which in turn recalcs the pans */
227 DSOUND_Calc3DBuffer(This
);
229 oldVol
= This
->volpan
.lVolume
;
230 This
->volpan
.lVolume
= vol
;
232 DSOUND_RecalcVolPan(&(This
->volpan
));
237 hres
= IDsDriverBuffer_SetVolumePan(This
->hwbuf
, &(This
->volpan
));
239 WARN("IDsDriverBuffer_SetVolumePan failed\n");
243 RtlReleaseResource(&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,%d)\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 = %d\n", freq
);
288 return DSERR_INVALIDPARAM
;
292 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
294 oldFreq
= This
->freq
;
296 if (freq
!= oldFreq
) {
297 This
->freqAdjust
= ((DWORD64
)This
->freq
<< DSOUND_FREQSHIFT
) / This
->device
->pwfx
->nSamplesPerSec
;
298 This
->nAvgBytesPerSec
= freq
* This
->pwfx
->nBlockAlign
;
299 DSOUND_RecalcFormat(This
);
300 DSOUND_MixToTemporary(This
, 0, This
->buflen
, FALSE
);
303 RtlReleaseResource(&This
->lock
);
309 static HRESULT WINAPI
IDirectSoundBufferImpl_Play(
310 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
312 HRESULT hres
= DS_OK
;
313 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
314 TRACE("(%p,%08x,%08x,%08x)\n",This
,reserved1
,reserved2
,flags
);
317 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
319 This
->playflags
= flags
;
320 if (This
->state
== STATE_STOPPED
&& !This
->hwbuf
) {
322 This
->state
= STATE_STARTING
;
323 } else if (This
->state
== STATE_STOPPING
)
324 This
->state
= STATE_PLAYING
;
326 hres
= IDsDriverBuffer_Play(This
->hwbuf
, 0, 0, This
->playflags
);
328 WARN("IDsDriverBuffer_Play failed\n");
330 This
->state
= STATE_PLAYING
;
333 RtlReleaseResource(&This
->lock
);
339 static HRESULT WINAPI
IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
341 HRESULT hres
= DS_OK
;
342 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
343 TRACE("(%p)\n",This
);
346 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
348 if (This
->state
== STATE_PLAYING
)
349 This
->state
= STATE_STOPPING
;
350 else if (This
->state
== STATE_STARTING
)
352 This
->state
= STATE_STOPPED
;
353 DSOUND_CheckEvent(This
, 0, 0);
356 hres
= IDsDriverBuffer_Stop(This
->hwbuf
);
358 WARN("IDsDriverBuffer_Stop failed\n");
360 This
->state
= STATE_STOPPED
;
363 RtlReleaseResource(&This
->lock
);
369 static ULONG WINAPI
IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
371 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
372 ULONG ref
= InterlockedIncrement(&(This
->ref
));
373 TRACE("(%p) ref was %d\n", This
, ref
- 1);
377 static ULONG WINAPI
IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
379 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
380 ULONG ref
= InterlockedDecrement(&(This
->ref
));
381 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
384 DirectSoundDevice_RemoveBuffer(This
->device
, This
);
385 RtlDeleteResource(&This
->lock
);
388 IDsDriverBuffer_Release(This
->hwbuf
);
389 if (!This
->hwbuf
|| (This
->device
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
)) {
391 list_remove(&This
->entry
);
392 if (This
->buffer
->ref
==0) {
393 HeapFree(GetProcessHeap(),0,This
->buffer
->memory
);
394 HeapFree(GetProcessHeap(),0,This
->buffer
);
398 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
399 HeapFree(GetProcessHeap(), 0, This
->notifies
);
400 HeapFree(GetProcessHeap(), 0, This
->pwfx
);
401 HeapFree(GetProcessHeap(), 0, This
);
403 TRACE("(%p) released\n", This
);
408 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCurrentPosition(
409 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
412 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
413 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
415 RtlAcquireResourceShared(&This
->lock
, TRUE
);
417 hres
=IDsDriverBuffer_GetPosition(This
->hwbuf
,playpos
,writepos
);
419 WARN("IDsDriverBuffer_GetPosition failed\n");
423 DWORD pos
= This
->sec_mixpos
;
426 if (pos
>= This
->buflen
){
427 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos
, This
->buflen
);
436 if (writepos
&& This
->state
!= STATE_STOPPED
&& (!This
->hwbuf
|| !(This
->device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDWRITELEAD
))) {
437 /* apply the documented 10ms lead to writepos */
438 *writepos
+= This
->writelead
;
439 *writepos
%= This
->buflen
;
441 RtlReleaseResource(&This
->lock
);
443 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
444 playpos
?*playpos
:-1, writepos
?*writepos
:-1, This
->buflen
, This
, GetTickCount());
449 static HRESULT WINAPI
IDirectSoundBufferImpl_GetStatus(
450 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
452 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
453 TRACE("(%p,%p), thread is %04x\n",This
,status
,GetCurrentThreadId());
455 if (status
== NULL
) {
456 WARN("invalid parameter: status = NULL\n");
457 return DSERR_INVALIDPARAM
;
461 RtlAcquireResourceShared(&This
->lock
, TRUE
);
462 if ((This
->state
== STATE_STARTING
) || (This
->state
== STATE_PLAYING
)) {
463 *status
|= DSBSTATUS_PLAYING
;
464 if (This
->playflags
& DSBPLAY_LOOPING
)
465 *status
|= DSBSTATUS_LOOPING
;
467 RtlReleaseResource(&This
->lock
);
469 TRACE("status=%x\n", *status
);
474 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFormat(
475 LPDIRECTSOUNDBUFFER8 iface
,
481 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
482 TRACE("(%p,%p,%d,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
484 size
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
486 if (lpwf
) { /* NULL is valid */
487 if (wfsize
>= size
) {
488 CopyMemory(lpwf
,This
->pwfx
,size
);
492 WARN("invalid parameter: wfsize too small\n");
493 CopyMemory(lpwf
,This
->pwfx
,wfsize
);
496 return DSERR_INVALIDPARAM
;
500 *wfwritten
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
502 WARN("invalid parameter: wfwritten == NULL\n");
503 return DSERR_INVALIDPARAM
;
510 static HRESULT WINAPI
IDirectSoundBufferImpl_Lock(
511 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
513 HRESULT hres
= DS_OK
;
514 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
516 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
529 return DSERR_INVALIDPARAM
;
531 /* when this flag is set, writecursor is meaningless and must be calculated */
532 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
533 /* GetCurrentPosition does too much magic to duplicate here */
534 hres
= IDirectSoundBufferImpl_GetCurrentPosition(iface
, NULL
, &writecursor
);
536 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
541 /* when this flag is set, writebytes is meaningless and must be set */
542 if (flags
& DSBLOCK_ENTIREBUFFER
)
543 writebytes
= This
->buflen
;
545 if (writecursor
>= This
->buflen
) {
546 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
547 writecursor
, This
->buflen
);
548 return DSERR_INVALIDPARAM
;
551 if (writebytes
> This
->buflen
) {
552 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
553 writebytes
, This
->buflen
);
554 return DSERR_INVALIDPARAM
;
558 RtlAcquireResourceShared(&This
->lock
, TRUE
);
560 if (!(This
->device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDSECONDARYLOCK
) && This
->hwbuf
) {
561 hres
= IDsDriverBuffer_Lock(This
->hwbuf
,
562 lplpaudioptr1
, audiobytes1
,
563 lplpaudioptr2
, audiobytes2
,
564 writecursor
, writebytes
,
567 WARN("IDsDriverBuffer_Lock failed\n");
568 RtlReleaseResource(&This
->lock
);
572 if (writecursor
+writebytes
<= This
->buflen
) {
573 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
574 if (This
->sec_mixpos
>= writecursor
&& This
->sec_mixpos
< writecursor
+ writebytes
&& This
->state
== STATE_PLAYING
)
575 WARN("Overwriting mixing position, case 1\n");
576 *audiobytes1
= writebytes
;
578 *(LPBYTE
*)lplpaudioptr2
= NULL
;
581 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
582 *(LPBYTE
*)lplpaudioptr1
, *audiobytes1
, lplpaudioptr2
? *(LPBYTE
*)lplpaudioptr2
: NULL
, audiobytes2
? *audiobytes2
: 0, writecursor
);
583 TRACE("->%d.0\n",writebytes
);
585 DWORD remainder
= writebytes
+ writecursor
- This
->buflen
;
586 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
587 *audiobytes1
= This
->buflen
-writecursor
;
588 if (This
->sec_mixpos
>= writecursor
&& This
->sec_mixpos
< writecursor
+ writebytes
&& This
->state
== STATE_PLAYING
)
589 WARN("Overwriting mixing position, case 2\n");
591 *(LPBYTE
*)lplpaudioptr2
= This
->buffer
->memory
;
593 *audiobytes2
= writebytes
-(This
->buflen
-writecursor
);
594 if (audiobytes2
&& This
->sec_mixpos
< remainder
&& This
->state
== STATE_PLAYING
)
595 WARN("Overwriting mixing position, case 3\n");
596 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE
*)lplpaudioptr1
, *audiobytes1
, lplpaudioptr2
? *(LPBYTE
*)lplpaudioptr2
: NULL
, audiobytes2
? *audiobytes2
: 0, writecursor
);
600 RtlReleaseResource(&This
->lock
);
606 static HRESULT WINAPI
IDirectSoundBufferImpl_SetCurrentPosition(
607 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
609 HRESULT hres
= DS_OK
;
610 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
612 TRACE("(%p,%d)\n",This
,newpos
);
615 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
617 oldpos
= This
->sec_mixpos
;
619 /* start mixing from this new location instead */
620 newpos
%= This
->buflen
;
621 newpos
-= newpos
%This
->pwfx
->nBlockAlign
;
622 This
->sec_mixpos
= newpos
;
624 /* at this point, do not attempt to reset buffers, mess with primary mix position,
625 or anything like that to reduce latancy. The data already prebuffered cannot be changed */
627 /* position HW buffer if applicable, else just start mixing from new location instead */
629 hres
= IDsDriverBuffer_SetPosition(This
->hwbuf
, This
->buf_mixpos
);
631 WARN("IDsDriverBuffer_SetPosition failed\n");
633 else if (oldpos
!= newpos
)
634 /* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
635 This
->buf_mixpos
= DSOUND_secpos_to_bufpos(This
, newpos
, 0, NULL
);
637 RtlReleaseResource(&This
->lock
);
643 static HRESULT WINAPI
IDirectSoundBufferImpl_SetPan(
644 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
646 HRESULT hres
= DS_OK
;
647 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
649 TRACE("(%p,%d)\n",This
,pan
);
651 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
652 WARN("invalid parameter: pan = %d\n", pan
);
653 return DSERR_INVALIDPARAM
;
656 /* You cannot use both pan and 3D controls */
657 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
658 (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
)) {
659 WARN("control unavailable\n");
660 return DSERR_CONTROLUNAVAIL
;
664 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
666 if (This
->volpan
.lPan
!= pan
) {
667 This
->volpan
.lPan
= pan
;
668 DSOUND_RecalcVolPan(&(This
->volpan
));
671 hres
= IDsDriverBuffer_SetVolumePan(This
->hwbuf
, &(This
->volpan
));
673 WARN("IDsDriverBuffer_SetVolumePan failed\n");
677 RtlReleaseResource(&This
->lock
);
683 static HRESULT WINAPI
IDirectSoundBufferImpl_GetPan(
684 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
686 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
687 TRACE("(%p,%p)\n",This
,pan
);
689 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
690 WARN("control unavailable\n");
691 return DSERR_CONTROLUNAVAIL
;
695 WARN("invalid parameter: pan = NULL\n");
696 return DSERR_INVALIDPARAM
;
699 *pan
= This
->volpan
.lPan
;
704 static HRESULT WINAPI
IDirectSoundBufferImpl_Unlock(
705 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
707 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
, *iter
;
708 HRESULT hres
= DS_OK
;
710 TRACE("(%p,%p,%d,%p,%d)\n", This
,p1
,x1
,p2
,x2
);
713 RtlAcquireResourceShared(&This
->lock
, TRUE
);
715 if (!(This
->device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDSECONDARYLOCK
) && This
->hwbuf
) {
716 hres
= IDsDriverBuffer_Unlock(This
->hwbuf
, p1
, x1
, p2
, x2
);
718 WARN("IDsDriverBuffer_Unlock failed\n");
721 RtlReleaseResource(&This
->lock
);
727 if (!This
->hwbuf
&& (x1
|| x2
))
729 RtlAcquireResourceShared(&This
->device
->buffer_list_lock
, TRUE
);
730 LIST_FOR_EACH_ENTRY(iter
, &This
->buffer
->buffers
, IDirectSoundBufferImpl
, entry
)
732 RtlAcquireResourceShared(&iter
->lock
, TRUE
);
735 if(x1
+ (DWORD_PTR
)p1
- (DWORD_PTR
)iter
->buffer
->memory
> iter
->buflen
)
736 hres
= DSERR_INVALIDPARAM
;
738 DSOUND_MixToTemporary(iter
, (DWORD_PTR
)p1
- (DWORD_PTR
)iter
->buffer
->memory
, x1
, FALSE
);
741 DSOUND_MixToTemporary(iter
, 0, x2
, FALSE
);
742 RtlReleaseResource(&iter
->lock
);
744 RtlReleaseResource(&This
->device
->buffer_list_lock
);
750 static HRESULT WINAPI
IDirectSoundBufferImpl_Restore(
751 LPDIRECTSOUNDBUFFER8 iface
753 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
754 FIXME("(%p):stub\n",This
);
758 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFrequency(
759 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
761 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
762 TRACE("(%p,%p)\n",This
,freq
);
765 WARN("invalid parameter: freq = NULL\n");
766 return DSERR_INVALIDPARAM
;
770 TRACE("-> %d\n", *freq
);
775 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFX(
776 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
778 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
781 FIXME("(%p,%u,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
784 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
786 WARN("control unavailable\n");
787 return DSERR_CONTROLUNAVAIL
;
790 static HRESULT WINAPI
IDirectSoundBufferImpl_AcquireResources(
791 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
793 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
796 FIXME("(%p,%08u,%u,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
799 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
801 WARN("control unavailable\n");
802 return DSERR_CONTROLUNAVAIL
;
805 static HRESULT WINAPI
IDirectSoundBufferImpl_GetObjectInPath(
806 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
808 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
810 FIXME("(%p,%s,%u,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
812 WARN("control unavailable\n");
813 return DSERR_CONTROLUNAVAIL
;
816 static HRESULT WINAPI
IDirectSoundBufferImpl_Initialize(
817 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
819 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
820 WARN("(%p) already initialized\n", This
);
821 return DSERR_ALREADYINITIALIZED
;
824 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCaps(
825 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
827 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
828 TRACE("(%p)->(%p)\n",This
,caps
);
831 WARN("invalid parameter: caps == NULL\n");
832 return DSERR_INVALIDPARAM
;
835 if (caps
->dwSize
< sizeof(*caps
)) {
836 WARN("invalid parameter: caps->dwSize = %d\n",caps
->dwSize
);
837 return DSERR_INVALIDPARAM
;
840 caps
->dwFlags
= This
->dsbd
.dwFlags
;
841 if (This
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
842 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
844 caps
->dwBufferBytes
= This
->buflen
;
846 /* According to windows, this is zero*/
847 caps
->dwUnlockTransferRate
= 0;
848 caps
->dwPlayCpuOverhead
= 0;
853 static HRESULT WINAPI
IDirectSoundBufferImpl_QueryInterface(
854 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
856 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
858 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
861 WARN("invalid parameter\n");
865 *ppobj
= NULL
; /* assume failure */
867 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
868 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ||
869 IsEqualGUID(riid
, &IID_IDirectSoundBuffer8
) ) {
870 if (!This
->secondary
)
871 SecondaryBufferImpl_Create(This
, &(This
->secondary
));
872 if (This
->secondary
) {
873 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8
)This
->secondary
);
874 *ppobj
= This
->secondary
;
877 WARN("IID_IDirectSoundBuffer\n");
878 return E_NOINTERFACE
;
881 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
883 IDirectSoundNotifyImpl_Create(This
, &(This
->notify
));
885 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY
)This
->notify
);
886 *ppobj
= This
->notify
;
889 WARN("IID_IDirectSoundNotify\n");
890 return E_NOINTERFACE
;
893 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
895 IDirectSound3DBufferImpl_Create(This
, &(This
->ds3db
));
897 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER
)This
->ds3db
);
898 *ppobj
= This
->ds3db
;
901 WARN("IID_IDirectSound3DBuffer\n");
902 return E_NOINTERFACE
;
905 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
906 ERR("app requested IDirectSound3DListener on secondary buffer\n");
907 return E_NOINTERFACE
;
910 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
912 IKsBufferPropertySetImpl_Create(This
, &(This
->iks
));
914 IKsPropertySet_AddRef((LPKSPROPERTYSET
)This
->iks
);
918 WARN("IID_IKsPropertySet\n");
919 return E_NOINTERFACE
;
922 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
924 return E_NOINTERFACE
;
927 static const IDirectSoundBuffer8Vtbl dsbvt
=
929 IDirectSoundBufferImpl_QueryInterface
,
930 IDirectSoundBufferImpl_AddRef
,
931 IDirectSoundBufferImpl_Release
,
932 IDirectSoundBufferImpl_GetCaps
,
933 IDirectSoundBufferImpl_GetCurrentPosition
,
934 IDirectSoundBufferImpl_GetFormat
,
935 IDirectSoundBufferImpl_GetVolume
,
936 IDirectSoundBufferImpl_GetPan
,
937 IDirectSoundBufferImpl_GetFrequency
,
938 IDirectSoundBufferImpl_GetStatus
,
939 IDirectSoundBufferImpl_Initialize
,
940 IDirectSoundBufferImpl_Lock
,
941 IDirectSoundBufferImpl_Play
,
942 IDirectSoundBufferImpl_SetCurrentPosition
,
943 IDirectSoundBufferImpl_SetFormat
,
944 IDirectSoundBufferImpl_SetVolume
,
945 IDirectSoundBufferImpl_SetPan
,
946 IDirectSoundBufferImpl_SetFrequency
,
947 IDirectSoundBufferImpl_Stop
,
948 IDirectSoundBufferImpl_Unlock
,
949 IDirectSoundBufferImpl_Restore
,
950 IDirectSoundBufferImpl_SetFX
,
951 IDirectSoundBufferImpl_AcquireResources
,
952 IDirectSoundBufferImpl_GetObjectInPath
955 HRESULT
IDirectSoundBufferImpl_Create(
956 DirectSoundDevice
* device
,
957 IDirectSoundBufferImpl
**pdsb
,
958 LPCDSBUFFERDESC dsbd
)
960 IDirectSoundBufferImpl
*dsb
;
961 LPWAVEFORMATEX wfex
= dsbd
->lpwfxFormat
;
964 int use_hw
, alloc_size
, cp_size
;
965 TRACE("(%p,%p,%p)\n",device
,pdsb
,dsbd
);
967 if (dsbd
->dwBufferBytes
< DSBSIZE_MIN
|| dsbd
->dwBufferBytes
> DSBSIZE_MAX
) {
968 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd
->dwBufferBytes
);
970 return DSERR_INVALIDPARAM
; /* FIXME: which error? */
973 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
976 WARN("out of memory\n");
978 return DSERR_OUTOFMEMORY
;
981 TRACE("Created buffer at %p\n", dsb
);
985 dsb
->device
= device
;
986 dsb
->lpVtbl
= &dsbvt
;
989 /* size depends on version */
990 CopyMemory(&dsb
->dsbd
, dsbd
, dsbd
->dwSize
);
992 /* variable sized struct so calculate size based on format */
993 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
994 alloc_size
= sizeof(WAVEFORMATEX
);
995 cp_size
= sizeof(PCMWAVEFORMAT
);
997 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
999 dsb
->pwfx
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,alloc_size
);
1000 if (dsb
->pwfx
== NULL
) {
1001 WARN("out of memory\n");
1002 HeapFree(GetProcessHeap(),0,dsb
);
1004 return DSERR_OUTOFMEMORY
;
1007 CopyMemory(dsb
->pwfx
, wfex
, cp_size
);
1009 if (dsbd
->dwBufferBytes
% dsbd
->lpwfxFormat
->nBlockAlign
)
1010 dsb
->buflen
= dsbd
->dwBufferBytes
+
1011 (dsbd
->lpwfxFormat
->nBlockAlign
-
1012 (dsbd
->dwBufferBytes
% dsbd
->lpwfxFormat
->nBlockAlign
));
1014 dsb
->buflen
= dsbd
->dwBufferBytes
;
1016 dsb
->freq
= dsbd
->lpwfxFormat
->nSamplesPerSec
;
1018 dsb
->notifies
= NULL
;
1019 dsb
->nrofnotifies
= 0;
1022 /* Check necessary hardware mixing capabilities */
1023 if (wfex
->nChannels
==2) capf
|= DSCAPS_SECONDARYSTEREO
;
1024 else capf
|= DSCAPS_SECONDARYMONO
;
1025 if (wfex
->wBitsPerSample
==16) capf
|= DSCAPS_SECONDARY16BIT
;
1026 else capf
|= DSCAPS_SECONDARY8BIT
;
1028 use_hw
= !!(dsbd
->dwFlags
& DSBCAPS_LOCHARDWARE
);
1029 TRACE("use_hw = %d, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw
, capf
, device
->drvcaps
.dwFlags
);
1030 if (use_hw
&& ((device
->drvcaps
.dwFlags
& capf
) != capf
|| !device
->driver
))
1033 WARN("Format not supported for hardware buffer\n");
1034 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1035 HeapFree(GetProcessHeap(),0,dsb
);
1037 if ((device
->drvcaps
.dwFlags
& capf
) != capf
)
1038 return DSERR_BADFORMAT
;
1039 return DSERR_GENERIC
;
1042 /* FIXME: check hardware sample rate mixing capabilities */
1043 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1044 /* FIXME: check whether any hardware buffers are left */
1045 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1047 /* Allocate an empty buffer */
1048 dsb
->buffer
= HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb
->buffer
)));
1049 if (dsb
->buffer
== NULL
) {
1050 WARN("out of memory\n");
1051 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1052 HeapFree(GetProcessHeap(),0,dsb
);
1054 return DSERR_OUTOFMEMORY
;
1057 /* Allocate system memory for buffer if applicable */
1058 if ((device
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
) || !use_hw
) {
1059 dsb
->buffer
->memory
= HeapAlloc(GetProcessHeap(),0,dsb
->buflen
);
1060 if (dsb
->buffer
->memory
== NULL
) {
1061 WARN("out of memory\n");
1062 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1063 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1064 HeapFree(GetProcessHeap(),0,dsb
);
1066 return DSERR_OUTOFMEMORY
;
1070 /* Allocate the hardware buffer */
1072 err
= IDsDriver_CreateSoundBuffer(device
->driver
,wfex
,dsbd
->dwFlags
,0,
1073 &(dsb
->buflen
),&(dsb
->buffer
->memory
),
1074 (LPVOID
*)&(dsb
->hwbuf
));
1077 WARN("Failed to create hardware secondary buffer: %08x\n", err
);
1078 if (device
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
)
1079 HeapFree(GetProcessHeap(),0,dsb
->buffer
->memory
);
1080 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1081 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1082 HeapFree(GetProcessHeap(),0,dsb
);
1084 return DSERR_GENERIC
;
1088 dsb
->buffer
->ref
= 1;
1089 list_init(&dsb
->buffer
->buffers
);
1090 list_add_head(&dsb
->buffer
->buffers
, &dsb
->entry
);
1091 FillMemory(dsb
->buffer
->memory
, dsb
->buflen
, dsbd
->lpwfxFormat
->wBitsPerSample
== 8 ? 128 : 0);
1093 /* It's not necessary to initialize values to zero since */
1094 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1095 dsb
->buf_mixpos
= dsb
->sec_mixpos
= 0;
1096 dsb
->state
= STATE_STOPPED
;
1098 dsb
->freqAdjust
= ((DWORD64
)dsb
->freq
<< DSOUND_FREQSHIFT
) / device
->pwfx
->nSamplesPerSec
;
1099 dsb
->nAvgBytesPerSec
= dsb
->freq
*
1100 dsbd
->lpwfxFormat
->nBlockAlign
;
1102 /* calculate fragment size and write lead */
1103 DSOUND_RecalcFormat(dsb
);
1105 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
1106 dsb
->ds3db_ds3db
.dwSize
= sizeof(DS3DBUFFER
);
1107 dsb
->ds3db_ds3db
.vPosition
.x
= 0.0;
1108 dsb
->ds3db_ds3db
.vPosition
.y
= 0.0;
1109 dsb
->ds3db_ds3db
.vPosition
.z
= 0.0;
1110 dsb
->ds3db_ds3db
.vVelocity
.x
= 0.0;
1111 dsb
->ds3db_ds3db
.vVelocity
.y
= 0.0;
1112 dsb
->ds3db_ds3db
.vVelocity
.z
= 0.0;
1113 dsb
->ds3db_ds3db
.dwInsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
1114 dsb
->ds3db_ds3db
.dwOutsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
1115 dsb
->ds3db_ds3db
.vConeOrientation
.x
= 0.0;
1116 dsb
->ds3db_ds3db
.vConeOrientation
.y
= 0.0;
1117 dsb
->ds3db_ds3db
.vConeOrientation
.z
= 0.0;
1118 dsb
->ds3db_ds3db
.lConeOutsideVolume
= DS3D_DEFAULTCONEOUTSIDEVOLUME
;
1119 dsb
->ds3db_ds3db
.flMinDistance
= DS3D_DEFAULTMINDISTANCE
;
1120 dsb
->ds3db_ds3db
.flMaxDistance
= DS3D_DEFAULTMAXDISTANCE
;
1121 dsb
->ds3db_ds3db
.dwMode
= DS3DMODE_NORMAL
;
1123 dsb
->ds3db_need_recalc
= FALSE
;
1124 DSOUND_Calc3DBuffer(dsb
);
1126 DSOUND_RecalcVolPan(&(dsb
->volpan
));
1128 RtlInitializeResource(&dsb
->lock
);
1130 /* register buffer if not primary */
1131 if (!(dsbd
->dwFlags
& DSBCAPS_PRIMARYBUFFER
)) {
1132 err
= DirectSoundDevice_AddBuffer(device
, dsb
);
1134 HeapFree(GetProcessHeap(),0,dsb
->buffer
->memory
);
1135 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1136 RtlDeleteResource(&dsb
->lock
);
1137 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1138 HeapFree(GetProcessHeap(),0,dsb
);
1147 HRESULT
IDirectSoundBufferImpl_Destroy(
1148 IDirectSoundBufferImpl
*pdsb
)
1150 TRACE("(%p)\n",pdsb
);
1152 /* This keeps the *_Destroy functions from possibly deleting
1153 * this object until it is ready to be deleted */
1154 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8
)pdsb
);
1157 WARN("iks not NULL\n");
1158 IKsBufferPropertySetImpl_Destroy(pdsb
->iks
);
1163 WARN("ds3db not NULL\n");
1164 IDirectSound3DBufferImpl_Destroy(pdsb
->ds3db
);
1169 WARN("notify not NULL\n");
1170 IDirectSoundNotifyImpl_Destroy(pdsb
->notify
);
1171 pdsb
->notify
= NULL
;
1174 if (pdsb
->secondary
) {
1175 WARN("dsb not NULL\n");
1176 SecondaryBufferImpl_Destroy(pdsb
->secondary
);
1177 pdsb
->secondary
= NULL
;
1180 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8
)pdsb
) > 0);
1185 HRESULT
IDirectSoundBufferImpl_Duplicate(
1186 DirectSoundDevice
*device
,
1187 IDirectSoundBufferImpl
**ppdsb
,
1188 IDirectSoundBufferImpl
*pdsb
)
1190 IDirectSoundBufferImpl
*dsb
;
1191 HRESULT hres
= DS_OK
;
1193 TRACE("(%p,%p,%p)\n", device
, pdsb
, pdsb
);
1195 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1198 WARN("out of memory\n");
1200 return DSERR_OUTOFMEMORY
;
1203 CopyMemory(dsb
, pdsb
, sizeof(IDirectSoundBufferImpl
));
1206 TRACE("duplicating hardware buffer\n");
1208 hres
= IDsDriver_DuplicateSoundBuffer(device
->driver
, pdsb
->hwbuf
,
1209 (LPVOID
*)&dsb
->hwbuf
);
1211 WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres
);
1212 HeapFree(GetProcessHeap(),0,dsb
);
1219 list_add_head(&dsb
->buffer
->buffers
, &dsb
->entry
);
1221 dsb
->state
= STATE_STOPPED
;
1222 dsb
->buf_mixpos
= dsb
->sec_mixpos
= 0;
1223 dsb
->device
= device
;
1225 dsb
->iks
= NULL
; /* FIXME? */
1226 dsb
->secondary
= NULL
;
1227 dsb
->tmp_buffer
= NULL
;
1228 DSOUND_RecalcFormat(dsb
);
1229 DSOUND_MixToTemporary(dsb
, 0, dsb
->buflen
, FALSE
);
1231 /* variable sized struct so calculate size based on format */
1232 size
= sizeof(WAVEFORMATEX
) + pdsb
->pwfx
->cbSize
;
1234 dsb
->pwfx
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,size
);
1235 if (dsb
->pwfx
== NULL
) {
1236 WARN("out of memory\n");
1237 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1238 HeapFree(GetProcessHeap(),0,dsb
);
1240 return DSERR_OUTOFMEMORY
;
1243 CopyMemory(dsb
->pwfx
, pdsb
->pwfx
, size
);
1245 RtlInitializeResource(&dsb
->lock
);
1247 /* register buffer */
1248 hres
= DirectSoundDevice_AddBuffer(device
, dsb
);
1249 if (hres
!= DS_OK
) {
1250 RtlDeleteResource(&dsb
->lock
);
1251 HeapFree(GetProcessHeap(),0,dsb
->tmp_buffer
);
1252 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1253 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1254 HeapFree(GetProcessHeap(),0,dsb
);
1262 /*******************************************************************************
1266 static HRESULT WINAPI
SecondaryBufferImpl_QueryInterface(
1267 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
)
1269 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1270 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
1272 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8
)This
->dsb
,riid
,ppobj
);
1275 static ULONG WINAPI
SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
1277 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1278 ULONG ref
= InterlockedIncrement(&(This
->ref
));
1279 TRACE("(%p) ref was %d\n", This
, ref
- 1);
1283 static ULONG WINAPI
SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
1285 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1287 TRACE("(%p)\n", This
);
1288 ref
= InterlockedDecrement(&(This
->ref
));
1289 TRACE("ref was %d\n", ref
+ 1);
1292 This
->dsb
->secondary
= NULL
;
1293 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1294 HeapFree(GetProcessHeap(), 0, This
);
1295 TRACE("(%p) released\n", This
);
1300 static HRESULT WINAPI
SecondaryBufferImpl_GetCaps(
1301 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
)
1303 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1304 TRACE("(%p)->(%p)\n",This
,caps
);
1306 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8
)This
->dsb
,caps
);
1309 static HRESULT WINAPI
SecondaryBufferImpl_GetCurrentPosition(
1310 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
)
1312 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1313 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
1315 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8
)This
->dsb
,playpos
,writepos
);
1318 static HRESULT WINAPI
SecondaryBufferImpl_GetFormat(
1319 LPDIRECTSOUNDBUFFER8 iface
,LPWAVEFORMATEX lpwf
,DWORD wfsize
,LPDWORD wfwritten
)
1321 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1322 TRACE("(%p,%p,%d,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
1324 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8
)This
->dsb
,lpwf
,wfsize
,wfwritten
);
1327 static HRESULT WINAPI
SecondaryBufferImpl_GetVolume(
1328 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
)
1330 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1331 TRACE("(%p,%p)\n",This
,vol
);
1333 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8
)This
->dsb
,vol
);
1336 static HRESULT WINAPI
SecondaryBufferImpl_GetPan(
1337 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
)
1339 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1340 TRACE("(%p,%p)\n",This
,pan
);
1342 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8
)This
->dsb
,pan
);
1345 static HRESULT WINAPI
SecondaryBufferImpl_GetFrequency(
1346 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
)
1348 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1349 TRACE("(%p,%p)\n",This
,freq
);
1351 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8
)This
->dsb
,freq
);
1354 static HRESULT WINAPI
SecondaryBufferImpl_GetStatus(
1355 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
)
1357 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1358 TRACE("(%p,%p)\n",This
,status
);
1360 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8
)This
->dsb
,status
);
1363 static HRESULT WINAPI
SecondaryBufferImpl_Initialize(
1364 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
)
1366 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1367 TRACE("(%p,%p,%p)\n",This
,dsound
,dbsd
);
1369 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dsound
,dbsd
);
1372 static HRESULT WINAPI
SecondaryBufferImpl_Lock(
1373 LPDIRECTSOUNDBUFFER8 iface
,
1376 LPVOID
*lplpaudioptr1
,
1377 LPDWORD audiobytes1
,
1378 LPVOID
*lplpaudioptr2
,
1379 LPDWORD audiobytes2
,
1382 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1383 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1384 This
,writecursor
,writebytes
,lplpaudioptr1
,audiobytes1
,lplpaudioptr2
,audiobytes2
,dwFlags
);
1386 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8
)This
->dsb
,
1387 writecursor
,writebytes
,lplpaudioptr1
,audiobytes1
,lplpaudioptr2
,audiobytes2
,dwFlags
);
1390 static HRESULT WINAPI
SecondaryBufferImpl_Play(
1391 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
)
1393 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1394 TRACE("(%p,%08x,%08x,%08x)\n",This
,reserved1
,reserved2
,flags
);
1396 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8
)This
->dsb
,reserved1
,reserved2
,flags
);
1399 static HRESULT WINAPI
SecondaryBufferImpl_SetCurrentPosition(
1400 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
)
1402 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1403 TRACE("(%p,%d)\n",This
,newpos
);
1405 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8
)This
->dsb
,newpos
);
1408 static HRESULT WINAPI
SecondaryBufferImpl_SetFormat(
1409 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
)
1411 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1412 TRACE("(%p,%p)\n",This
,wfex
);
1414 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8
)This
->dsb
,wfex
);
1417 static HRESULT WINAPI
SecondaryBufferImpl_SetVolume(
1418 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
)
1420 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1421 TRACE("(%p,%d)\n",This
,vol
);
1423 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8
)This
->dsb
,vol
);
1426 static HRESULT WINAPI
SecondaryBufferImpl_SetPan(
1427 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
)
1429 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1430 TRACE("(%p,%d)\n",This
,pan
);
1432 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8
)This
->dsb
,pan
);
1435 static HRESULT WINAPI
SecondaryBufferImpl_SetFrequency(
1436 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
)
1438 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1439 TRACE("(%p,%d)\n",This
,freq
);
1441 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8
)This
->dsb
,freq
);
1444 static HRESULT WINAPI
SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
1446 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1447 TRACE("(%p)\n",This
);
1449 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1452 static HRESULT WINAPI
SecondaryBufferImpl_Unlock(
1453 LPDIRECTSOUNDBUFFER8 iface
,
1454 LPVOID lpvAudioPtr1
,
1455 DWORD dwAudioBytes1
,
1456 LPVOID lpvAudioPtr2
,
1457 DWORD dwAudioBytes2
)
1459 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1460 TRACE("(%p,%p,%d,%p,%d)\n",
1461 This
, lpvAudioPtr1
, dwAudioBytes1
, lpvAudioPtr2
, dwAudioBytes2
);
1463 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8
)This
->dsb
,
1464 lpvAudioPtr1
,dwAudioBytes1
,lpvAudioPtr2
,dwAudioBytes2
);
1467 static HRESULT WINAPI
SecondaryBufferImpl_Restore(
1468 LPDIRECTSOUNDBUFFER8 iface
)
1470 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1471 TRACE("(%p)\n",This
);
1473 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1476 static HRESULT WINAPI
SecondaryBufferImpl_SetFX(
1477 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
)
1479 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1480 TRACE("(%p,%u,%p,%p)\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
1482 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
1485 static HRESULT WINAPI
SecondaryBufferImpl_AcquireResources(
1486 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
)
1488 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1489 TRACE("(%p,%08u,%u,%p)\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
1491 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
1494 static HRESULT WINAPI
SecondaryBufferImpl_GetObjectInPath(
1495 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
)
1497 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1498 TRACE("(%p,%s,%u,%s,%p)\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
1500 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8
)This
->dsb
,rguidObject
,dwIndex
,rguidInterface
,ppObject
);
1503 static const IDirectSoundBuffer8Vtbl sbvt
=
1505 SecondaryBufferImpl_QueryInterface
,
1506 SecondaryBufferImpl_AddRef
,
1507 SecondaryBufferImpl_Release
,
1508 SecondaryBufferImpl_GetCaps
,
1509 SecondaryBufferImpl_GetCurrentPosition
,
1510 SecondaryBufferImpl_GetFormat
,
1511 SecondaryBufferImpl_GetVolume
,
1512 SecondaryBufferImpl_GetPan
,
1513 SecondaryBufferImpl_GetFrequency
,
1514 SecondaryBufferImpl_GetStatus
,
1515 SecondaryBufferImpl_Initialize
,
1516 SecondaryBufferImpl_Lock
,
1517 SecondaryBufferImpl_Play
,
1518 SecondaryBufferImpl_SetCurrentPosition
,
1519 SecondaryBufferImpl_SetFormat
,
1520 SecondaryBufferImpl_SetVolume
,
1521 SecondaryBufferImpl_SetPan
,
1522 SecondaryBufferImpl_SetFrequency
,
1523 SecondaryBufferImpl_Stop
,
1524 SecondaryBufferImpl_Unlock
,
1525 SecondaryBufferImpl_Restore
,
1526 SecondaryBufferImpl_SetFX
,
1527 SecondaryBufferImpl_AcquireResources
,
1528 SecondaryBufferImpl_GetObjectInPath
1531 HRESULT
SecondaryBufferImpl_Create(
1532 IDirectSoundBufferImpl
*dsb
,
1533 SecondaryBufferImpl
**psb
)
1535 SecondaryBufferImpl
*sb
;
1536 TRACE("(%p,%p)\n",dsb
,psb
);
1538 sb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*sb
));
1541 WARN("out of memory\n");
1543 return DSERR_OUTOFMEMORY
;
1549 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8
)dsb
);
1554 static HRESULT
SecondaryBufferImpl_Destroy(
1555 SecondaryBufferImpl
*pdsb
)
1557 TRACE("(%p)\n",pdsb
);
1559 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8
)pdsb
) > 0);