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"
36 #include <wine/debug.h>
38 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
43 /*******************************************************************************
47 static inline struct IDirectSoundBufferImpl
*impl_from_IDirectSoundNotify(IDirectSoundNotify
*iface
)
49 return CONTAINING_RECORD(iface
, struct IDirectSoundBufferImpl
, IDirectSoundNotify_iface
);
52 static HRESULT WINAPI
IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify
*iface
, REFIID riid
,
55 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundNotify(iface
);
57 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppobj
);
59 return IDirectSoundBuffer8_QueryInterface(&This
->IDirectSoundBuffer8_iface
, riid
, ppobj
);
62 static ULONG WINAPI
IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify
*iface
)
64 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundNotify(iface
);
65 ULONG ref
= InterlockedIncrement(&This
->refn
);
67 TRACE("(%p) ref was %d\n", This
, ref
- 1);
70 InterlockedIncrement(&This
->numIfaces
);
75 static ULONG WINAPI
IDirectSoundNotifyImpl_Release(IDirectSoundNotify
*iface
)
77 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundNotify(iface
);
78 ULONG ref
= InterlockedDecrement(&This
->refn
);
80 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
82 if (!ref
&& !InterlockedDecrement(&This
->numIfaces
))
83 secondarybuffer_destroy(This
);
88 static HRESULT WINAPI
IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify
*iface
,
89 DWORD howmuch
, const DSBPOSITIONNOTIFY
*notify
)
91 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundNotify(iface
);
93 TRACE("(%p,0x%08x,%p)\n",This
,howmuch
,notify
);
95 if (howmuch
> 0 && notify
== NULL
) {
96 WARN("invalid parameter: notify == NULL\n");
97 return DSERR_INVALIDPARAM
;
100 if (TRACE_ON(dsound
)) {
102 for (i
=0;i
<howmuch
;i
++)
103 TRACE("notify at %d to %p\n",
104 notify
[i
].dwOffset
,notify
[i
].hEventNotify
);
108 /* Make an internal copy of the caller-supplied array.
109 * Replace the existing copy if one is already present. */
110 HeapFree(GetProcessHeap(), 0, This
->notifies
);
111 This
->notifies
= HeapAlloc(GetProcessHeap(), 0,
112 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
114 if (This
->notifies
== NULL
) {
115 WARN("out of memory\n");
116 return DSERR_OUTOFMEMORY
;
118 CopyMemory(This
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
119 This
->nrofnotifies
= howmuch
;
121 HeapFree(GetProcessHeap(), 0, This
->notifies
);
122 This
->notifies
= NULL
;
123 This
->nrofnotifies
= 0;
129 static const IDirectSoundNotifyVtbl dsnvt
=
131 IDirectSoundNotifyImpl_QueryInterface
,
132 IDirectSoundNotifyImpl_AddRef
,
133 IDirectSoundNotifyImpl_Release
,
134 IDirectSoundNotifyImpl_SetNotificationPositions
,
137 /*******************************************************************************
141 static inline IDirectSoundBufferImpl
*impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8
*iface
)
143 return CONTAINING_RECORD(iface
, IDirectSoundBufferImpl
, IDirectSoundBuffer8_iface
);
146 static inline BOOL
is_primary_buffer(IDirectSoundBufferImpl
*This
)
148 return (This
->dsbd
.dwFlags
& DSBCAPS_PRIMARYBUFFER
) != 0;
151 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8
*iface
,
152 LPCWAVEFORMATEX wfex
)
154 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
156 TRACE("(%p,%p)\n", iface
, wfex
);
158 if (is_primary_buffer(This
))
159 return primarybuffer_SetFormat(This
->device
, wfex
);
161 WARN("not available for secondary buffers.\n");
162 return DSERR_INVALIDCALL
;
166 static HRESULT WINAPI
IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8
*iface
, LONG vol
)
168 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
171 HRESULT hres
= DS_OK
;
173 TRACE("(%p,%d)\n",This
,vol
);
175 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
176 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This
->dsbd
.dwFlags
);
177 return DSERR_CONTROLUNAVAIL
;
180 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
181 WARN("invalid parameter: vol = %d\n", vol
);
182 return DSERR_INVALIDPARAM
;
186 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
188 if (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
189 oldVol
= This
->ds3db_lVolume
;
190 This
->ds3db_lVolume
= vol
;
192 /* recalc 3d volume, which in turn recalcs the pans */
193 DSOUND_Calc3DBuffer(This
);
195 oldVol
= This
->volpan
.lVolume
;
196 This
->volpan
.lVolume
= vol
;
198 DSOUND_RecalcVolPan(&(This
->volpan
));
201 RtlReleaseResource(&This
->lock
);
207 static HRESULT WINAPI
IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8
*iface
, LONG
*vol
)
209 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
211 TRACE("(%p,%p)\n",This
,vol
);
213 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
214 WARN("control unavailable\n");
215 return DSERR_CONTROLUNAVAIL
;
219 WARN("invalid parameter: vol == NULL\n");
220 return DSERR_INVALIDPARAM
;
223 *vol
= This
->volpan
.lVolume
;
228 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8
*iface
, DWORD freq
)
230 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
233 TRACE("(%p,%d)\n",This
,freq
);
235 if (is_primary_buffer(This
)) {
236 WARN("not available for primary buffers.\n");
237 return DSERR_CONTROLUNAVAIL
;
240 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
241 WARN("control unavailable\n");
242 return DSERR_CONTROLUNAVAIL
;
245 if (freq
== DSBFREQUENCY_ORIGINAL
)
246 freq
= This
->pwfx
->nSamplesPerSec
;
248 if ((freq
< DSBFREQUENCY_MIN
) || (freq
> DSBFREQUENCY_MAX
)) {
249 WARN("invalid parameter: freq = %d\n", freq
);
250 return DSERR_INVALIDPARAM
;
254 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
256 oldFreq
= This
->freq
;
258 if (freq
!= oldFreq
) {
259 This
->freqAdjust
= This
->freq
/ (float)This
->device
->pwfx
->nSamplesPerSec
;
260 This
->nAvgBytesPerSec
= freq
* This
->pwfx
->nBlockAlign
;
261 DSOUND_RecalcFormat(This
);
264 RtlReleaseResource(&This
->lock
);
270 static HRESULT WINAPI
IDirectSoundBufferImpl_Play(IDirectSoundBuffer8
*iface
, DWORD reserved1
,
271 DWORD reserved2
, DWORD flags
)
273 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
274 HRESULT hres
= DS_OK
;
276 TRACE("(%p,%08x,%08x,%08x)\n",This
,reserved1
,reserved2
,flags
);
279 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
281 This
->playflags
= flags
;
282 if (This
->state
== STATE_STOPPED
) {
284 This
->state
= STATE_STARTING
;
285 } else if (This
->state
== STATE_STOPPING
)
286 This
->state
= STATE_PLAYING
;
288 RtlReleaseResource(&This
->lock
);
294 static HRESULT WINAPI
IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8
*iface
)
296 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
297 HRESULT hres
= DS_OK
;
299 TRACE("(%p)\n",This
);
302 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
304 if (This
->state
== STATE_PLAYING
)
305 This
->state
= STATE_STOPPING
;
306 else if (This
->state
== STATE_STARTING
)
308 This
->state
= STATE_STOPPED
;
309 DSOUND_CheckEvent(This
, 0, 0);
312 RtlReleaseResource(&This
->lock
);
318 static ULONG WINAPI
IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8
*iface
)
320 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
321 ULONG ref
= InterlockedIncrement(&This
->ref
);
323 TRACE("(%p) ref was %d\n", This
, ref
- 1);
326 InterlockedIncrement(&This
->numIfaces
);
331 static ULONG WINAPI
IDirectSoundBufferImpl_Release(IDirectSoundBuffer8
*iface
)
333 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
336 if (is_primary_buffer(This
)){
337 ref
= capped_refcount_dec(&This
->ref
);
339 capped_refcount_dec(&This
->numIfaces
);
340 TRACE("(%p) ref is now: %d\n", This
, ref
);
344 ref
= InterlockedDecrement(&This
->ref
);
345 if (!ref
&& !InterlockedDecrement(&This
->numIfaces
))
346 secondarybuffer_destroy(This
);
348 TRACE("(%p) ref is now %d\n", This
, ref
);
353 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8
*iface
,
354 DWORD
*playpos
, DWORD
*writepos
)
356 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
359 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
361 RtlAcquireResourceShared(&This
->lock
, TRUE
);
363 pos
= This
->sec_mixpos
;
366 if (pos
>= This
->buflen
){
367 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos
, This
->buflen
);
376 if (writepos
&& This
->state
!= STATE_STOPPED
) {
377 /* apply the documented 10ms lead to writepos */
378 *writepos
+= This
->writelead
;
379 *writepos
%= This
->buflen
;
382 RtlReleaseResource(&This
->lock
);
384 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
385 playpos
?*playpos
:-1, writepos
?*writepos
:-1, This
->buflen
, This
, GetTickCount());
390 static HRESULT WINAPI
IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8
*iface
, DWORD
*status
)
392 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
394 TRACE("(%p,%p), thread is %04x\n",This
,status
,GetCurrentThreadId());
396 if (status
== NULL
) {
397 WARN("invalid parameter: status = NULL\n");
398 return DSERR_INVALIDPARAM
;
402 RtlAcquireResourceShared(&This
->lock
, TRUE
);
403 if ((This
->state
== STATE_STARTING
) || (This
->state
== STATE_PLAYING
)) {
404 *status
|= DSBSTATUS_PLAYING
;
405 if (This
->playflags
& DSBPLAY_LOOPING
)
406 *status
|= DSBSTATUS_LOOPING
;
408 RtlReleaseResource(&This
->lock
);
410 TRACE("status=%x\n", *status
);
415 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8
*iface
,
416 LPWAVEFORMATEX lpwf
, DWORD wfsize
, DWORD
*wfwritten
)
418 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
421 TRACE("(%p,%p,%d,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
423 size
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
425 if (lpwf
) { /* NULL is valid */
426 if (wfsize
>= size
) {
427 CopyMemory(lpwf
,This
->pwfx
,size
);
431 WARN("invalid parameter: wfsize too small\n");
432 CopyMemory(lpwf
,This
->pwfx
,wfsize
);
435 return DSERR_INVALIDPARAM
;
439 *wfwritten
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
441 WARN("invalid parameter: wfwritten == NULL\n");
442 return DSERR_INVALIDPARAM
;
449 static HRESULT WINAPI
IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8
*iface
, DWORD writecursor
,
450 DWORD writebytes
, void **lplpaudioptr1
, DWORD
*audiobytes1
, void **lplpaudioptr2
,
451 DWORD
*audiobytes2
, DWORD flags
)
453 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
454 HRESULT hres
= DS_OK
;
456 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n", This
, writecursor
, writebytes
, lplpaudioptr1
,
457 audiobytes1
, lplpaudioptr2
, audiobytes2
, flags
, GetTickCount());
460 return DSERR_INVALIDPARAM
;
462 /* when this flag is set, writecursor is meaningless and must be calculated */
463 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
464 /* GetCurrentPosition does too much magic to duplicate here */
465 hres
= IDirectSoundBufferImpl_GetCurrentPosition(iface
, NULL
, &writecursor
);
467 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
472 /* when this flag is set, writebytes is meaningless and must be set */
473 if (flags
& DSBLOCK_ENTIREBUFFER
)
474 writebytes
= This
->buflen
;
476 if (writecursor
>= This
->buflen
) {
477 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
478 writecursor
, This
->buflen
);
479 return DSERR_INVALIDPARAM
;
482 if (writebytes
> This
->buflen
) {
483 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
484 writebytes
, This
->buflen
);
485 return DSERR_INVALIDPARAM
;
489 RtlAcquireResourceShared(&This
->lock
, TRUE
);
491 if (writecursor
+writebytes
<= This
->buflen
) {
492 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
493 if (This
->sec_mixpos
>= writecursor
&& This
->sec_mixpos
< writecursor
+ writebytes
&& This
->state
== STATE_PLAYING
)
494 WARN("Overwriting mixing position, case 1\n");
495 *audiobytes1
= writebytes
;
497 *(LPBYTE
*)lplpaudioptr2
= NULL
;
500 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
501 *(LPBYTE
*)lplpaudioptr1
, *audiobytes1
, lplpaudioptr2
? *(LPBYTE
*)lplpaudioptr2
: NULL
, audiobytes2
? *audiobytes2
: 0, writecursor
);
502 TRACE("->%d.0\n",writebytes
);
504 DWORD remainder
= writebytes
+ writecursor
- This
->buflen
;
505 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
506 *audiobytes1
= This
->buflen
-writecursor
;
507 if (This
->sec_mixpos
>= writecursor
&& This
->sec_mixpos
< writecursor
+ writebytes
&& This
->state
== STATE_PLAYING
)
508 WARN("Overwriting mixing position, case 2\n");
510 *(LPBYTE
*)lplpaudioptr2
= This
->buffer
->memory
;
512 *audiobytes2
= writebytes
-(This
->buflen
-writecursor
);
513 if (audiobytes2
&& This
->sec_mixpos
< remainder
&& This
->state
== STATE_PLAYING
)
514 WARN("Overwriting mixing position, case 3\n");
515 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE
*)lplpaudioptr1
, *audiobytes1
, lplpaudioptr2
? *(LPBYTE
*)lplpaudioptr2
: NULL
, audiobytes2
? *audiobytes2
: 0, writecursor
);
518 RtlReleaseResource(&This
->lock
);
524 static HRESULT WINAPI
IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8
*iface
,
527 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
528 HRESULT hres
= DS_OK
;
530 TRACE("(%p,%d)\n",This
,newpos
);
533 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
535 /* start mixing from this new location instead */
536 newpos
%= This
->buflen
;
537 newpos
-= newpos
%This
->pwfx
->nBlockAlign
;
538 This
->sec_mixpos
= newpos
;
540 /* at this point, do not attempt to reset buffers, mess with primary mix position,
541 or anything like that to reduce latency. The data already prebuffered cannot be changed */
543 RtlReleaseResource(&This
->lock
);
549 static HRESULT WINAPI
IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8
*iface
, LONG pan
)
551 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
552 HRESULT hres
= DS_OK
;
554 TRACE("(%p,%d)\n",This
,pan
);
556 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
557 WARN("invalid parameter: pan = %d\n", pan
);
558 return DSERR_INVALIDPARAM
;
561 /* You cannot use both pan and 3D controls */
562 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
563 (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
)) {
564 WARN("control unavailable\n");
565 return DSERR_CONTROLUNAVAIL
;
569 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
571 if (This
->volpan
.lPan
!= pan
) {
572 This
->volpan
.lPan
= pan
;
573 DSOUND_RecalcVolPan(&(This
->volpan
));
576 RtlReleaseResource(&This
->lock
);
582 static HRESULT WINAPI
IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8
*iface
, LONG
*pan
)
584 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
586 TRACE("(%p,%p)\n",This
,pan
);
588 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
589 WARN("control unavailable\n");
590 return DSERR_CONTROLUNAVAIL
;
594 WARN("invalid parameter: pan = NULL\n");
595 return DSERR_INVALIDPARAM
;
598 *pan
= This
->volpan
.lPan
;
603 static HRESULT WINAPI
IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8
*iface
, void *p1
, DWORD x1
,
606 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
), *iter
;
607 HRESULT hres
= DS_OK
;
609 TRACE("(%p,%p,%d,%p,%d)\n", This
,p1
,x1
,p2
,x2
);
614 if((p1
&& ((BYTE
*)p1
< This
->buffer
->memory
|| (BYTE
*)p1
>= This
->buffer
->memory
+ This
->buflen
)) ||
615 (p2
&& ((BYTE
*)p2
< This
->buffer
->memory
|| (BYTE
*)p2
>= This
->buffer
->memory
+ This
->buflen
)))
616 return DSERR_INVALIDPARAM
;
620 RtlAcquireResourceShared(&This
->device
->buffer_list_lock
, TRUE
);
621 LIST_FOR_EACH_ENTRY(iter
, &This
->buffer
->buffers
, IDirectSoundBufferImpl
, entry
)
623 RtlAcquireResourceShared(&iter
->lock
, TRUE
);
626 if(x1
+ (DWORD_PTR
)p1
- (DWORD_PTR
)iter
->buffer
->memory
> iter
->buflen
)
627 hres
= DSERR_INVALIDPARAM
;
629 RtlReleaseResource(&iter
->lock
);
631 RtlReleaseResource(&This
->device
->buffer_list_lock
);
637 static HRESULT WINAPI
IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8
*iface
)
639 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
641 FIXME("(%p):stub\n",This
);
645 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8
*iface
, DWORD
*freq
)
647 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
649 TRACE("(%p,%p)\n",This
,freq
);
652 WARN("invalid parameter: freq = NULL\n");
653 return DSERR_INVALIDPARAM
;
657 TRACE("-> %d\n", *freq
);
662 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8
*iface
, DWORD dwEffectsCount
,
663 LPDSEFFECTDESC pDSFXDesc
, DWORD
*pdwResultCodes
)
665 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
668 FIXME("(%p,%u,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
671 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
673 WARN("control unavailable\n");
674 return DSERR_CONTROLUNAVAIL
;
677 static HRESULT WINAPI
IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8
*iface
,
678 DWORD dwFlags
, DWORD dwEffectsCount
, DWORD
*pdwResultCodes
)
680 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
683 FIXME("(%p,%08u,%u,%p): stub, faking success\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
686 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
688 WARN("control unavailable\n");
692 static HRESULT WINAPI
IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8
*iface
,
693 REFGUID rguidObject
, DWORD dwIndex
, REFGUID rguidInterface
, void **ppObject
)
695 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
697 FIXME("(%p,%s,%u,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
699 WARN("control unavailable\n");
700 return DSERR_CONTROLUNAVAIL
;
703 static HRESULT WINAPI
IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8
*iface
,
704 IDirectSound
*dsound
, LPCDSBUFFERDESC dbsd
)
706 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
708 WARN("(%p) already initialized\n", This
);
709 return DSERR_ALREADYINITIALIZED
;
712 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8
*iface
, LPDSBCAPS caps
)
714 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
716 TRACE("(%p)->(%p)\n",This
,caps
);
719 WARN("invalid parameter: caps == NULL\n");
720 return DSERR_INVALIDPARAM
;
723 if (caps
->dwSize
< sizeof(*caps
)) {
724 WARN("invalid parameter: caps->dwSize = %d\n",caps
->dwSize
);
725 return DSERR_INVALIDPARAM
;
728 caps
->dwFlags
= This
->dsbd
.dwFlags
;
729 caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
731 caps
->dwBufferBytes
= This
->buflen
;
733 /* According to windows, this is zero*/
734 caps
->dwUnlockTransferRate
= 0;
735 caps
->dwPlayCpuOverhead
= 0;
740 static HRESULT WINAPI
IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8
*iface
, REFIID riid
,
743 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer8(iface
);
745 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
748 WARN("invalid parameter\n");
752 *ppobj
= NULL
; /* assume failure */
754 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
755 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ||
756 IsEqualGUID(riid
, &IID_IDirectSoundBuffer8
) ) {
757 IDirectSoundBuffer8_AddRef(iface
);
762 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
763 IDirectSoundNotify_AddRef(&This
->IDirectSoundNotify_iface
);
764 *ppobj
= &This
->IDirectSoundNotify_iface
;
768 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
769 if(This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
){
770 IDirectSound3DBuffer_AddRef(&This
->IDirectSound3DBuffer_iface
);
771 *ppobj
= &This
->IDirectSound3DBuffer_iface
;
774 TRACE("app requested IDirectSound3DBuffer on non-3D secondary buffer\n");
775 return E_NOINTERFACE
;
778 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
779 ERR("app requested IDirectSound3DListener on secondary buffer\n");
780 return E_NOINTERFACE
;
783 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
784 IKsPropertySet_AddRef(&This
->IKsPropertySet_iface
);
785 *ppobj
= &This
->IKsPropertySet_iface
;
789 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
791 return E_NOINTERFACE
;
794 static const IDirectSoundBuffer8Vtbl dsbvt
=
796 IDirectSoundBufferImpl_QueryInterface
,
797 IDirectSoundBufferImpl_AddRef
,
798 IDirectSoundBufferImpl_Release
,
799 IDirectSoundBufferImpl_GetCaps
,
800 IDirectSoundBufferImpl_GetCurrentPosition
,
801 IDirectSoundBufferImpl_GetFormat
,
802 IDirectSoundBufferImpl_GetVolume
,
803 IDirectSoundBufferImpl_GetPan
,
804 IDirectSoundBufferImpl_GetFrequency
,
805 IDirectSoundBufferImpl_GetStatus
,
806 IDirectSoundBufferImpl_Initialize
,
807 IDirectSoundBufferImpl_Lock
,
808 IDirectSoundBufferImpl_Play
,
809 IDirectSoundBufferImpl_SetCurrentPosition
,
810 IDirectSoundBufferImpl_SetFormat
,
811 IDirectSoundBufferImpl_SetVolume
,
812 IDirectSoundBufferImpl_SetPan
,
813 IDirectSoundBufferImpl_SetFrequency
,
814 IDirectSoundBufferImpl_Stop
,
815 IDirectSoundBufferImpl_Unlock
,
816 IDirectSoundBufferImpl_Restore
,
817 IDirectSoundBufferImpl_SetFX
,
818 IDirectSoundBufferImpl_AcquireResources
,
819 IDirectSoundBufferImpl_GetObjectInPath
822 HRESULT
IDirectSoundBufferImpl_Create(
823 DirectSoundDevice
* device
,
824 IDirectSoundBufferImpl
**pdsb
,
825 LPCDSBUFFERDESC dsbd
)
827 IDirectSoundBufferImpl
*dsb
;
828 LPWAVEFORMATEX wfex
= dsbd
->lpwfxFormat
;
831 TRACE("(%p,%p,%p)\n",device
,pdsb
,dsbd
);
833 if (dsbd
->dwBufferBytes
< DSBSIZE_MIN
|| dsbd
->dwBufferBytes
> DSBSIZE_MAX
) {
834 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd
->dwBufferBytes
);
836 return DSERR_INVALIDPARAM
; /* FIXME: which error? */
839 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
842 WARN("out of memory\n");
844 return DSERR_OUTOFMEMORY
;
847 TRACE("Created buffer at %p\n", dsb
);
854 dsb
->device
= device
;
855 dsb
->IDirectSoundBuffer8_iface
.lpVtbl
= &dsbvt
;
856 dsb
->IDirectSoundNotify_iface
.lpVtbl
= &dsnvt
;
857 dsb
->IDirectSound3DBuffer_iface
.lpVtbl
= &ds3dbvt
;
858 dsb
->IKsPropertySet_iface
.lpVtbl
= &iksbvt
;
860 /* size depends on version */
861 CopyMemory(&dsb
->dsbd
, dsbd
, dsbd
->dwSize
);
863 dsb
->pwfx
= DSOUND_CopyFormat(wfex
);
864 if (dsb
->pwfx
== NULL
) {
865 HeapFree(GetProcessHeap(),0,dsb
);
867 return DSERR_OUTOFMEMORY
;
870 if (dsbd
->dwBufferBytes
% dsbd
->lpwfxFormat
->nBlockAlign
)
871 dsb
->buflen
= dsbd
->dwBufferBytes
+
872 (dsbd
->lpwfxFormat
->nBlockAlign
-
873 (dsbd
->dwBufferBytes
% dsbd
->lpwfxFormat
->nBlockAlign
));
875 dsb
->buflen
= dsbd
->dwBufferBytes
;
877 dsb
->freq
= dsbd
->lpwfxFormat
->nSamplesPerSec
;
878 dsb
->notifies
= NULL
;
879 dsb
->nrofnotifies
= 0;
881 /* Check necessary hardware mixing capabilities */
882 if (wfex
->nChannels
==2) capf
|= DSCAPS_SECONDARYSTEREO
;
883 else capf
|= DSCAPS_SECONDARYMONO
;
884 if (wfex
->wBitsPerSample
==16) capf
|= DSCAPS_SECONDARY16BIT
;
885 else capf
|= DSCAPS_SECONDARY8BIT
;
887 TRACE("capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", capf
, device
->drvcaps
.dwFlags
);
889 /* Allocate an empty buffer */
890 dsb
->buffer
= HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb
->buffer
)));
891 if (dsb
->buffer
== NULL
) {
892 WARN("out of memory\n");
893 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
894 HeapFree(GetProcessHeap(),0,dsb
);
896 return DSERR_OUTOFMEMORY
;
899 /* Allocate system memory for buffer */
900 dsb
->buffer
->memory
= HeapAlloc(GetProcessHeap(),0,dsb
->buflen
);
901 if (dsb
->buffer
->memory
== NULL
) {
902 WARN("out of memory\n");
903 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
904 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
905 HeapFree(GetProcessHeap(),0,dsb
);
907 return DSERR_OUTOFMEMORY
;
910 dsb
->buffer
->ref
= 1;
911 list_init(&dsb
->buffer
->buffers
);
912 list_add_head(&dsb
->buffer
->buffers
, &dsb
->entry
);
913 FillMemory(dsb
->buffer
->memory
, dsb
->buflen
, dsbd
->lpwfxFormat
->wBitsPerSample
== 8 ? 128 : 0);
915 /* It's not necessary to initialize values to zero since */
916 /* we allocated this structure with HEAP_ZERO_MEMORY... */
918 dsb
->state
= STATE_STOPPED
;
920 dsb
->freqAdjust
= dsb
->freq
/ (float)device
->pwfx
->nSamplesPerSec
;
921 dsb
->nAvgBytesPerSec
= dsb
->freq
*
922 dsbd
->lpwfxFormat
->nBlockAlign
;
924 /* calculate fragment size and write lead */
925 DSOUND_RecalcFormat(dsb
);
927 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
928 dsb
->ds3db_ds3db
.dwSize
= sizeof(DS3DBUFFER
);
929 dsb
->ds3db_ds3db
.vPosition
.x
= 0.0;
930 dsb
->ds3db_ds3db
.vPosition
.y
= 0.0;
931 dsb
->ds3db_ds3db
.vPosition
.z
= 0.0;
932 dsb
->ds3db_ds3db
.vVelocity
.x
= 0.0;
933 dsb
->ds3db_ds3db
.vVelocity
.y
= 0.0;
934 dsb
->ds3db_ds3db
.vVelocity
.z
= 0.0;
935 dsb
->ds3db_ds3db
.dwInsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
936 dsb
->ds3db_ds3db
.dwOutsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
937 dsb
->ds3db_ds3db
.vConeOrientation
.x
= 0.0;
938 dsb
->ds3db_ds3db
.vConeOrientation
.y
= 0.0;
939 dsb
->ds3db_ds3db
.vConeOrientation
.z
= 0.0;
940 dsb
->ds3db_ds3db
.lConeOutsideVolume
= DS3D_DEFAULTCONEOUTSIDEVOLUME
;
941 dsb
->ds3db_ds3db
.flMinDistance
= DS3D_DEFAULTMINDISTANCE
;
942 dsb
->ds3db_ds3db
.flMaxDistance
= DS3D_DEFAULTMAXDISTANCE
;
943 dsb
->ds3db_ds3db
.dwMode
= DS3DMODE_NORMAL
;
945 dsb
->ds3db_need_recalc
= FALSE
;
946 DSOUND_Calc3DBuffer(dsb
);
948 DSOUND_RecalcVolPan(&(dsb
->volpan
));
950 RtlInitializeResource(&dsb
->lock
);
952 /* register buffer if not primary */
953 if (!(dsbd
->dwFlags
& DSBCAPS_PRIMARYBUFFER
)) {
954 err
= DirectSoundDevice_AddBuffer(device
, dsb
);
956 HeapFree(GetProcessHeap(),0,dsb
->buffer
->memory
);
957 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
958 RtlDeleteResource(&dsb
->lock
);
959 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
960 HeapFree(GetProcessHeap(),0,dsb
);
965 IDirectSoundBuffer8_AddRef(&dsb
->IDirectSoundBuffer8_iface
);
970 void secondarybuffer_destroy(IDirectSoundBufferImpl
*This
)
972 ULONG ref
= InterlockedIncrement(&This
->numIfaces
);
975 WARN("Destroying buffer with %u in use interfaces\n", ref
- 1);
977 DirectSoundDevice_RemoveBuffer(This
->device
, This
);
978 RtlDeleteResource(&This
->lock
);
981 list_remove(&This
->entry
);
982 if (This
->buffer
->ref
== 0) {
983 HeapFree(GetProcessHeap(), 0, This
->buffer
->memory
);
984 HeapFree(GetProcessHeap(), 0, This
->buffer
);
987 HeapFree(GetProcessHeap(), 0, This
->notifies
);
988 HeapFree(GetProcessHeap(), 0, This
->pwfx
);
989 HeapFree(GetProcessHeap(), 0, This
);
991 TRACE("(%p) released\n", This
);
994 HRESULT
IDirectSoundBufferImpl_Duplicate(
995 DirectSoundDevice
*device
,
996 IDirectSoundBufferImpl
**ppdsb
,
997 IDirectSoundBufferImpl
*pdsb
)
999 IDirectSoundBufferImpl
*dsb
;
1000 HRESULT hres
= DS_OK
;
1001 TRACE("(%p,%p,%p)\n", device
, ppdsb
, pdsb
);
1003 dsb
= HeapAlloc(GetProcessHeap(),0,sizeof(*dsb
));
1005 WARN("out of memory\n");
1007 return DSERR_OUTOFMEMORY
;
1010 RtlAcquireResourceShared(&pdsb
->lock
, TRUE
);
1012 CopyMemory(dsb
, pdsb
, sizeof(*dsb
));
1014 dsb
->pwfx
= DSOUND_CopyFormat(pdsb
->pwfx
);
1016 RtlReleaseResource(&pdsb
->lock
);
1018 if (dsb
->pwfx
== NULL
) {
1019 HeapFree(GetProcessHeap(),0,dsb
);
1021 return DSERR_OUTOFMEMORY
;
1025 list_add_head(&dsb
->buffer
->buffers
, &dsb
->entry
);
1031 dsb
->state
= STATE_STOPPED
;
1032 dsb
->sec_mixpos
= 0;
1033 dsb
->notifies
= NULL
;
1034 dsb
->nrofnotifies
= 0;
1035 dsb
->device
= device
;
1036 DSOUND_RecalcFormat(dsb
);
1038 RtlInitializeResource(&dsb
->lock
);
1040 /* register buffer */
1041 hres
= DirectSoundDevice_AddBuffer(device
, dsb
);
1042 if (hres
!= DS_OK
) {
1043 RtlDeleteResource(&dsb
->lock
);
1044 list_remove(&dsb
->entry
);
1046 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1047 HeapFree(GetProcessHeap(),0,dsb
);
1051 IDirectSoundBuffer8_AddRef(&dsb
->IDirectSoundBuffer8_iface
);
1056 /*******************************************************************************
1060 static inline IDirectSoundBufferImpl
*impl_from_IKsPropertySet(IKsPropertySet
*iface
)
1062 return CONTAINING_RECORD(iface
, IDirectSoundBufferImpl
, IKsPropertySet_iface
);
1065 /* IUnknown methods */
1066 static HRESULT WINAPI
IKsPropertySetImpl_QueryInterface(IKsPropertySet
*iface
, REFIID riid
,
1069 IDirectSoundBufferImpl
*This
= impl_from_IKsPropertySet(iface
);
1071 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
1073 return IDirectSoundBuffer_QueryInterface(&This
->IDirectSoundBuffer8_iface
, riid
, ppobj
);
1076 static ULONG WINAPI
IKsPropertySetImpl_AddRef(IKsPropertySet
*iface
)
1078 IDirectSoundBufferImpl
*This
= impl_from_IKsPropertySet(iface
);
1079 ULONG ref
= InterlockedIncrement(&This
->refiks
);
1081 TRACE("(%p) ref was %d\n", This
, ref
- 1);
1084 InterlockedIncrement(&This
->numIfaces
);
1089 static ULONG WINAPI
IKsPropertySetImpl_Release(IKsPropertySet
*iface
)
1091 IDirectSoundBufferImpl
*This
= impl_from_IKsPropertySet(iface
);
1094 if (is_primary_buffer(This
)){
1095 ref
= capped_refcount_dec(&This
->refiks
);
1097 capped_refcount_dec(&This
->numIfaces
);
1098 TRACE("(%p) ref is now: %d\n", This
, ref
);
1102 ref
= InterlockedDecrement(&This
->refiks
);
1103 if (!ref
&& !InterlockedDecrement(&This
->numIfaces
))
1104 secondarybuffer_destroy(This
);
1106 TRACE("(%p) ref is now %d\n", This
, ref
);
1111 static HRESULT WINAPI
IKsPropertySetImpl_Get(IKsPropertySet
*iface
, REFGUID guidPropSet
,
1112 ULONG dwPropID
, void *pInstanceData
, ULONG cbInstanceData
, void *pPropData
,
1113 ULONG cbPropData
, ULONG
*pcbReturned
)
1115 IDirectSoundBufferImpl
*This
= impl_from_IKsPropertySet(iface
);
1117 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1118 This
,debugstr_guid(guidPropSet
),dwPropID
,pInstanceData
,cbInstanceData
,pPropData
,cbPropData
,pcbReturned
);
1120 return E_PROP_ID_UNSUPPORTED
;
1123 static HRESULT WINAPI
IKsPropertySetImpl_Set(IKsPropertySet
*iface
, REFGUID guidPropSet
,
1124 ULONG dwPropID
, void *pInstanceData
, ULONG cbInstanceData
, void *pPropData
,
1127 IDirectSoundBufferImpl
*This
= impl_from_IKsPropertySet(iface
);
1129 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This
,debugstr_guid(guidPropSet
),dwPropID
,pInstanceData
,cbInstanceData
,pPropData
,cbPropData
);
1131 return E_PROP_ID_UNSUPPORTED
;
1134 static HRESULT WINAPI
IKsPropertySetImpl_QuerySupport(IKsPropertySet
*iface
, REFGUID guidPropSet
,
1135 ULONG dwPropID
, ULONG
*pTypeSupport
)
1137 IDirectSoundBufferImpl
*This
= impl_from_IKsPropertySet(iface
);
1139 TRACE("(%p,%s,%d,%p)\n",This
,debugstr_guid(guidPropSet
),dwPropID
,pTypeSupport
);
1141 return E_PROP_ID_UNSUPPORTED
;
1144 const IKsPropertySetVtbl iksbvt
= {
1145 IKsPropertySetImpl_QueryInterface
,
1146 IKsPropertySetImpl_AddRef
,
1147 IKsPropertySetImpl_Release
,
1148 IKsPropertySetImpl_Get
,
1149 IKsPropertySetImpl_Set
,
1150 IKsPropertySetImpl_QuerySupport