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 NONAMELESSSTRUCT
25 #define NONAMELESSUNION
31 #include "wine/debug.h"
34 #include "dsound_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
38 static HRESULT
SecondaryBufferImpl_Destroy(SecondaryBufferImpl
*pdsb
);
40 /*******************************************************************************
44 struct IDirectSoundNotifyImpl
47 const IDirectSoundNotifyVtbl
*lpVtbl
;
49 IDirectSoundBufferImpl
* dsb
;
52 static HRESULT
IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl
*dsb
,
53 IDirectSoundNotifyImpl
**pdsn
);
54 static HRESULT
IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl
*pdsn
);
56 static HRESULT WINAPI
IDirectSoundNotifyImpl_QueryInterface(
57 LPDIRECTSOUNDNOTIFY iface
,REFIID riid
,LPVOID
*ppobj
59 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
60 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
62 if (This
->dsb
== NULL
) {
63 WARN("invalid parameter\n");
67 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER
)This
->dsb
, riid
, ppobj
);
70 static ULONG WINAPI
IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface
)
72 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
73 ULONG ref
= InterlockedIncrement(&(This
->ref
));
74 TRACE("(%p) ref was %d\n", This
, ref
- 1);
78 static ULONG WINAPI
IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface
)
80 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
81 ULONG ref
= InterlockedDecrement(&(This
->ref
));
82 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
85 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER
)This
->dsb
);
86 This
->dsb
->notify
= NULL
;
87 HeapFree(GetProcessHeap(), 0, This
);
88 TRACE("(%p) released\n", This
);
93 static HRESULT WINAPI
IDirectSoundNotifyImpl_SetNotificationPositions(
94 LPDIRECTSOUNDNOTIFY iface
,DWORD howmuch
,LPCDSBPOSITIONNOTIFY notify
96 IDirectSoundNotifyImpl
*This
= (IDirectSoundNotifyImpl
*)iface
;
97 TRACE("(%p,0x%08x,%p)\n",This
,howmuch
,notify
);
99 if (howmuch
> 0 && notify
== NULL
) {
100 WARN("invalid parameter: notify == NULL\n");
101 return DSERR_INVALIDPARAM
;
104 if (TRACE_ON(dsound
)) {
106 for (i
=0;i
<howmuch
;i
++)
107 TRACE("notify at %d to %p\n",
108 notify
[i
].dwOffset
,notify
[i
].hEventNotify
);
111 if (This
->dsb
->hwnotify
) {
113 hres
= IDsDriverNotify_SetNotificationPositions(This
->dsb
->hwnotify
, howmuch
, notify
);
115 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
117 } else if (howmuch
> 0) {
118 /* Make an internal copy of the caller-supplied array.
119 * Replace the existing copy if one is already present. */
120 if (This
->dsb
->notifies
)
121 This
->dsb
->notifies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
122 This
->dsb
->notifies
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
124 This
->dsb
->notifies
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
125 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
127 if (This
->dsb
->notifies
== NULL
) {
128 WARN("out of memory\n");
129 return DSERR_OUTOFMEMORY
;
131 CopyMemory(This
->dsb
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
132 This
->dsb
->nrofnotifies
= howmuch
;
134 HeapFree(GetProcessHeap(), 0, This
->dsb
->notifies
);
135 This
->dsb
->notifies
= NULL
;
136 This
->dsb
->nrofnotifies
= 0;
142 static const IDirectSoundNotifyVtbl dsnvt
=
144 IDirectSoundNotifyImpl_QueryInterface
,
145 IDirectSoundNotifyImpl_AddRef
,
146 IDirectSoundNotifyImpl_Release
,
147 IDirectSoundNotifyImpl_SetNotificationPositions
,
150 static HRESULT
IDirectSoundNotifyImpl_Create(
151 IDirectSoundBufferImpl
* dsb
,
152 IDirectSoundNotifyImpl
**pdsn
)
154 IDirectSoundNotifyImpl
* dsn
;
155 TRACE("(%p,%p)\n",dsb
,pdsn
);
157 dsn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*dsn
));
160 WARN("out of memory\n");
161 return DSERR_OUTOFMEMORY
;
165 dsn
->lpVtbl
= &dsnvt
;
168 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)dsb
);
174 static HRESULT
IDirectSoundNotifyImpl_Destroy(
175 IDirectSoundNotifyImpl
*pdsn
)
177 TRACE("(%p)\n",pdsn
);
179 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY
)pdsn
) > 0);
184 /*******************************************************************************
188 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFormat(
189 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
191 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
193 TRACE("(%p,%p)\n",This
,wfex
);
194 /* This method is not available on secondary buffers */
195 WARN("invalid call\n");
196 return DSERR_INVALIDCALL
;
199 static HRESULT WINAPI
IDirectSoundBufferImpl_SetVolume(
200 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
202 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
204 HRESULT hres
= DS_OK
;
206 TRACE("(%p,%d)\n",This
,vol
);
208 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
209 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This
->dsbd
.dwFlags
);
210 return DSERR_CONTROLUNAVAIL
;
213 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
214 WARN("invalid parameter: vol = %d\n", vol
);
215 return DSERR_INVALIDPARAM
;
219 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
221 if (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
222 oldVol
= This
->ds3db_lVolume
;
223 This
->ds3db_lVolume
= vol
;
225 /* recalc 3d volume, which in turn recalcs the pans */
226 DSOUND_Calc3DBuffer(This
);
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");
242 RtlReleaseResource(&This
->lock
);
248 static HRESULT WINAPI
IDirectSoundBufferImpl_GetVolume(
249 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
251 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
252 TRACE("(%p,%p)\n",This
,vol
);
254 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
255 WARN("control unavailable\n");
256 return DSERR_CONTROLUNAVAIL
;
260 WARN("invalid parameter: vol == NULL\n");
261 return DSERR_INVALIDPARAM
;
264 *vol
= This
->volpan
.lVolume
;
269 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFrequency(
270 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
272 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
275 TRACE("(%p,%d)\n",This
,freq
);
277 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
278 WARN("control unavailable\n");
279 return DSERR_CONTROLUNAVAIL
;
282 if (freq
== DSBFREQUENCY_ORIGINAL
)
283 freq
= This
->pwfx
->nSamplesPerSec
;
285 if ((freq
< DSBFREQUENCY_MIN
) || (freq
> DSBFREQUENCY_MAX
)) {
286 WARN("invalid parameter: freq = %d\n", freq
);
287 return DSERR_INVALIDPARAM
;
291 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
293 oldFreq
= This
->freq
;
295 if (freq
!= oldFreq
) {
296 This
->freqAdjust
= ((DWORD64
)This
->freq
<< DSOUND_FREQSHIFT
) / This
->device
->pwfx
->nSamplesPerSec
;
297 This
->nAvgBytesPerSec
= freq
* This
->pwfx
->nBlockAlign
;
298 DSOUND_RecalcFormat(This
);
299 DSOUND_MixToTemporary(This
, 0, This
->buflen
, FALSE
);
302 RtlReleaseResource(&This
->lock
);
308 static HRESULT WINAPI
IDirectSoundBufferImpl_Play(
309 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
311 HRESULT hres
= DS_OK
;
312 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
313 TRACE("(%p,%08x,%08x,%08x)\n",This
,reserved1
,reserved2
,flags
);
316 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
318 This
->playflags
= flags
;
319 if (This
->state
== STATE_STOPPED
&& !This
->hwbuf
) {
321 This
->state
= STATE_STARTING
;
322 } else if (This
->state
== STATE_STOPPING
)
323 This
->state
= STATE_PLAYING
;
325 hres
= IDsDriverBuffer_Play(This
->hwbuf
, 0, 0, This
->playflags
);
327 WARN("IDsDriverBuffer_Play failed\n");
329 This
->state
= STATE_PLAYING
;
332 RtlReleaseResource(&This
->lock
);
338 static HRESULT WINAPI
IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
340 HRESULT hres
= DS_OK
;
341 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
342 TRACE("(%p)\n",This
);
345 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
347 if (This
->state
== STATE_PLAYING
)
348 This
->state
= STATE_STOPPING
;
349 else if (This
->state
== STATE_STARTING
)
351 This
->state
= STATE_STOPPED
;
352 DSOUND_CheckEvent(This
, 0, 0);
355 hres
= IDsDriverBuffer_Stop(This
->hwbuf
);
357 WARN("IDsDriverBuffer_Stop failed\n");
359 This
->state
= STATE_STOPPED
;
362 RtlReleaseResource(&This
->lock
);
368 static ULONG WINAPI
IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
370 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
371 ULONG ref
= InterlockedIncrement(&(This
->ref
));
372 TRACE("(%p) ref was %d\n", This
, ref
- 1);
376 static ULONG WINAPI
IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
378 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
379 ULONG ref
= InterlockedDecrement(&(This
->ref
));
380 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
383 DirectSoundDevice_RemoveBuffer(This
->device
, This
);
384 RtlDeleteResource(&This
->lock
);
387 IDsDriverBuffer_Release(This
->hwbuf
);
388 if (This
->device
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
) {
390 list_remove(&This
->entry
);
391 if (This
->buffer
->ref
==0) {
392 HeapFree(GetProcessHeap(),0,This
->buffer
->memory
);
393 HeapFree(GetProcessHeap(),0,This
->buffer
);
398 list_remove(&This
->entry
);
399 if (This
->buffer
->ref
==0) {
400 HeapFree(GetProcessHeap(),0,This
->buffer
->memory
);
401 HeapFree(GetProcessHeap(),0,This
->buffer
);
405 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
406 HeapFree(GetProcessHeap(), 0, This
->notifies
);
407 HeapFree(GetProcessHeap(), 0, This
->pwfx
);
408 HeapFree(GetProcessHeap(), 0, This
);
410 TRACE("(%p) released\n", This
);
415 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCurrentPosition(
416 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
419 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
420 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
422 RtlAcquireResourceShared(&This
->lock
, TRUE
);
424 hres
=IDsDriverBuffer_GetPosition(This
->hwbuf
,playpos
,writepos
);
426 WARN("IDsDriverBuffer_GetPosition failed\n");
430 DWORD pos
= This
->sec_mixpos
;
433 if (pos
>= This
->buflen
){
434 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos
, This
->buflen
);
443 if (writepos
&& This
->state
!= STATE_STOPPED
&& (!This
->hwbuf
|| !(This
->device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDWRITELEAD
))) {
444 /* apply the documented 10ms lead to writepos */
445 *writepos
+= This
->writelead
;
446 *writepos
%= This
->buflen
;
448 RtlReleaseResource(&This
->lock
);
450 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
451 playpos
?*playpos
:-1, writepos
?*writepos
:-1, This
->buflen
, This
, GetTickCount());
456 static HRESULT WINAPI
IDirectSoundBufferImpl_GetStatus(
457 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
459 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
460 TRACE("(%p,%p), thread is %04x\n",This
,status
,GetCurrentThreadId());
462 if (status
== NULL
) {
463 WARN("invalid parameter: status = NULL\n");
464 return DSERR_INVALIDPARAM
;
468 RtlAcquireResourceShared(&This
->lock
, TRUE
);
469 if ((This
->state
== STATE_STARTING
) || (This
->state
== STATE_PLAYING
)) {
470 *status
|= DSBSTATUS_PLAYING
;
471 if (This
->playflags
& DSBPLAY_LOOPING
)
472 *status
|= DSBSTATUS_LOOPING
;
474 RtlReleaseResource(&This
->lock
);
476 TRACE("status=%x\n", *status
);
481 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFormat(
482 LPDIRECTSOUNDBUFFER8 iface
,
488 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
489 TRACE("(%p,%p,%d,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
491 size
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
493 if (lpwf
) { /* NULL is valid */
494 if (wfsize
>= size
) {
495 CopyMemory(lpwf
,This
->pwfx
,size
);
499 WARN("invalid parameter: wfsize too small\n");
500 CopyMemory(lpwf
,This
->pwfx
,wfsize
);
503 return DSERR_INVALIDPARAM
;
507 *wfwritten
= sizeof(WAVEFORMATEX
) + This
->pwfx
->cbSize
;
509 WARN("invalid parameter: wfwritten == NULL\n");
510 return DSERR_INVALIDPARAM
;
517 static HRESULT WINAPI
IDirectSoundBufferImpl_Lock(
518 LPDIRECTSOUNDBUFFER8 iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
520 HRESULT hres
= DS_OK
;
521 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
523 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
535 /* when this flag is set, writecursor is meaningless and must be calculated */
536 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
537 /* GetCurrentPosition does too much magic to duplicate here */
538 hres
= IDirectSoundBufferImpl_GetCurrentPosition(iface
, NULL
, &writecursor
);
540 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
545 /* when this flag is set, writebytes is meaningless and must be set */
546 if (flags
& DSBLOCK_ENTIREBUFFER
)
547 writebytes
= This
->buflen
;
549 if (writecursor
>= This
->buflen
) {
550 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
551 writecursor
, This
->buflen
);
552 return DSERR_INVALIDPARAM
;
555 if (writebytes
> This
->buflen
) {
556 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
557 writebytes
, This
->buflen
);
558 return DSERR_INVALIDPARAM
;
562 RtlAcquireResourceShared(&This
->lock
, TRUE
);
564 if (!(This
->device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDSECONDARYLOCK
) && This
->hwbuf
) {
565 hres
= IDsDriverBuffer_Lock(This
->hwbuf
,
566 lplpaudioptr1
, audiobytes1
,
567 lplpaudioptr2
, audiobytes2
,
568 writecursor
, writebytes
,
571 WARN("IDsDriverBuffer_Lock failed\n");
572 RtlReleaseResource(&This
->lock
);
576 if (writecursor
+writebytes
<= This
->buflen
) {
577 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
578 if (This
->sec_mixpos
>= writecursor
&& This
->sec_mixpos
< writecursor
+ writebytes
&& This
->state
== STATE_PLAYING
)
579 WARN("Overwriting mixing position, case 1\n");
580 *audiobytes1
= writebytes
;
582 *(LPBYTE
*)lplpaudioptr2
= NULL
;
585 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
586 *(LPBYTE
*)lplpaudioptr1
, *audiobytes1
, lplpaudioptr2
? *(LPBYTE
*)lplpaudioptr2
: NULL
, audiobytes2
? *audiobytes2
: 0, writecursor
);
587 TRACE("->%d.0\n",writebytes
);
589 DWORD remainder
= writebytes
+ writecursor
- This
->buflen
;
590 *(LPBYTE
*)lplpaudioptr1
= This
->buffer
->memory
+writecursor
;
591 *audiobytes1
= This
->buflen
-writecursor
;
592 if (This
->sec_mixpos
>= writecursor
&& This
->sec_mixpos
< writecursor
+ writebytes
&& This
->state
== STATE_PLAYING
)
593 WARN("Overwriting mixing position, case 2\n");
595 *(LPBYTE
*)lplpaudioptr2
= This
->buffer
->memory
;
597 *audiobytes2
= writebytes
-(This
->buflen
-writecursor
);
598 if (audiobytes2
&& This
->sec_mixpos
< remainder
&& This
->state
== STATE_PLAYING
)
599 WARN("Overwriting mixing position, case 3\n");
600 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE
*)lplpaudioptr1
, *audiobytes1
, lplpaudioptr2
? *(LPBYTE
*)lplpaudioptr2
: NULL
, audiobytes2
? *audiobytes2
: 0, writecursor
);
604 RtlReleaseResource(&This
->lock
);
610 static HRESULT WINAPI
IDirectSoundBufferImpl_SetCurrentPosition(
611 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
613 HRESULT hres
= DS_OK
;
614 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
616 TRACE("(%p,%d)\n",This
,newpos
);
619 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
621 oldpos
= This
->sec_mixpos
;
623 /* start mixing from this new location instead */
624 newpos
%= This
->buflen
;
625 newpos
-= newpos
%This
->pwfx
->nBlockAlign
;
626 This
->sec_mixpos
= newpos
;
628 /* at this point, do not attempt to reset buffers, mess with primary mix position,
629 or anything like that to reduce latancy. The data already prebuffered cannot be changed */
631 /* position HW buffer if applicable, else just start mixing from new location instead */
633 hres
= IDsDriverBuffer_SetPosition(This
->hwbuf
, This
->buf_mixpos
);
635 WARN("IDsDriverBuffer_SetPosition failed\n");
637 else if (oldpos
!= newpos
)
638 /* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
639 This
->buf_mixpos
= DSOUND_secpos_to_bufpos(This
, newpos
, 0, NULL
);
641 RtlReleaseResource(&This
->lock
);
647 static HRESULT WINAPI
IDirectSoundBufferImpl_SetPan(
648 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
650 HRESULT hres
= DS_OK
;
651 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
653 TRACE("(%p,%d)\n",This
,pan
);
655 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
656 WARN("invalid parameter: pan = %d\n", pan
);
657 return DSERR_INVALIDPARAM
;
660 /* You cannot use both pan and 3D controls */
661 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
662 (This
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
)) {
663 WARN("control unavailable\n");
664 return DSERR_CONTROLUNAVAIL
;
668 RtlAcquireResourceExclusive(&This
->lock
, TRUE
);
670 if (This
->volpan
.lPan
!= pan
) {
671 This
->volpan
.lPan
= pan
;
672 DSOUND_RecalcVolPan(&(This
->volpan
));
675 hres
= IDsDriverBuffer_SetVolumePan(This
->hwbuf
, &(This
->volpan
));
677 WARN("IDsDriverBuffer_SetVolumePan failed\n");
681 RtlReleaseResource(&This
->lock
);
687 static HRESULT WINAPI
IDirectSoundBufferImpl_GetPan(
688 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
690 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
691 TRACE("(%p,%p)\n",This
,pan
);
693 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
694 WARN("control unavailable\n");
695 return DSERR_CONTROLUNAVAIL
;
699 WARN("invalid parameter: pan = NULL\n");
700 return DSERR_INVALIDPARAM
;
703 *pan
= This
->volpan
.lPan
;
708 static HRESULT WINAPI
IDirectSoundBufferImpl_Unlock(
709 LPDIRECTSOUNDBUFFER8 iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
711 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
, *iter
;
712 HRESULT hres
= DS_OK
;
714 TRACE("(%p,%p,%d,%p,%d)\n", This
,p1
,x1
,p2
,x2
);
717 RtlAcquireResourceShared(&This
->lock
, TRUE
);
719 if (!(This
->device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDSECONDARYLOCK
) && This
->hwbuf
) {
720 hres
= IDsDriverBuffer_Unlock(This
->hwbuf
, p1
, x1
, p2
, x2
);
722 WARN("IDsDriverBuffer_Unlock failed\n");
725 RtlReleaseResource(&This
->lock
);
731 if (!This
->hwbuf
&& (x1
|| x2
))
733 RtlAcquireResourceShared(&This
->device
->buffer_list_lock
, TRUE
);
734 LIST_FOR_EACH_ENTRY(iter
, &This
->buffer
->buffers
, IDirectSoundBufferImpl
, entry
)
736 RtlAcquireResourceShared(&iter
->lock
, TRUE
);
739 if(x1
+ (DWORD_PTR
)p1
- (DWORD_PTR
)iter
->buffer
->memory
> iter
->buflen
)
740 hres
= DSERR_INVALIDPARAM
;
742 DSOUND_MixToTemporary(iter
, (DWORD_PTR
)p1
- (DWORD_PTR
)iter
->buffer
->memory
, x1
, FALSE
);
745 DSOUND_MixToTemporary(iter
, 0, x2
, FALSE
);
746 RtlReleaseResource(&iter
->lock
);
748 RtlReleaseResource(&This
->device
->buffer_list_lock
);
754 static HRESULT WINAPI
IDirectSoundBufferImpl_Restore(
755 LPDIRECTSOUNDBUFFER8 iface
757 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
758 FIXME("(%p):stub\n",This
);
762 static HRESULT WINAPI
IDirectSoundBufferImpl_GetFrequency(
763 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
765 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
766 TRACE("(%p,%p)\n",This
,freq
);
769 WARN("invalid parameter: freq = NULL\n");
770 return DSERR_INVALIDPARAM
;
774 TRACE("-> %d\n", *freq
);
779 static HRESULT WINAPI
IDirectSoundBufferImpl_SetFX(
780 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
782 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
785 FIXME("(%p,%u,%p,%p): stub\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
788 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
790 WARN("control unavailable\n");
791 return DSERR_CONTROLUNAVAIL
;
794 static HRESULT WINAPI
IDirectSoundBufferImpl_AcquireResources(
795 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
797 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
800 FIXME("(%p,%08u,%u,%p): stub\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
803 for (u
=0; u
<dwEffectsCount
; u
++) pdwResultCodes
[u
] = DSFXR_UNKNOWN
;
805 WARN("control unavailable\n");
806 return DSERR_CONTROLUNAVAIL
;
809 static HRESULT WINAPI
IDirectSoundBufferImpl_GetObjectInPath(
810 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
812 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
814 FIXME("(%p,%s,%u,%s,%p): stub\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
816 WARN("control unavailable\n");
817 return DSERR_CONTROLUNAVAIL
;
820 static HRESULT WINAPI
IDirectSoundBufferImpl_Initialize(
821 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
823 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
824 WARN("(%p) already initialized\n", This
);
825 return DSERR_ALREADYINITIALIZED
;
828 static HRESULT WINAPI
IDirectSoundBufferImpl_GetCaps(
829 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
831 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
832 TRACE("(%p)->(%p)\n",This
,caps
);
835 WARN("invalid parameter: caps == NULL\n");
836 return DSERR_INVALIDPARAM
;
839 if (caps
->dwSize
< sizeof(*caps
)) {
840 WARN("invalid parameter: caps->dwSize = %d\n",caps
->dwSize
);
841 return DSERR_INVALIDPARAM
;
844 caps
->dwFlags
= This
->dsbd
.dwFlags
;
845 if (This
->hwbuf
) caps
->dwFlags
|= DSBCAPS_LOCHARDWARE
;
846 else caps
->dwFlags
|= DSBCAPS_LOCSOFTWARE
;
848 caps
->dwBufferBytes
= This
->buflen
;
850 /* According to windows, this is zero*/
851 caps
->dwUnlockTransferRate
= 0;
852 caps
->dwPlayCpuOverhead
= 0;
857 static HRESULT WINAPI
IDirectSoundBufferImpl_QueryInterface(
858 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
860 IDirectSoundBufferImpl
*This
= (IDirectSoundBufferImpl
*)iface
;
862 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
865 WARN("invalid parameter\n");
869 *ppobj
= NULL
; /* assume failure */
871 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
872 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ||
873 IsEqualGUID(riid
, &IID_IDirectSoundBuffer8
) ) {
874 if (!This
->secondary
)
875 SecondaryBufferImpl_Create(This
, &(This
->secondary
));
876 if (This
->secondary
) {
877 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8
)This
->secondary
);
878 *ppobj
= This
->secondary
;
881 WARN("IID_IDirectSoundBuffer\n");
882 return E_NOINTERFACE
;
885 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
887 IDirectSoundNotifyImpl_Create(This
, &(This
->notify
));
889 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY
)This
->notify
);
890 *ppobj
= This
->notify
;
893 WARN("IID_IDirectSoundNotify\n");
894 return E_NOINTERFACE
;
897 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
899 IDirectSound3DBufferImpl_Create(This
, &(This
->ds3db
));
901 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER
)This
->ds3db
);
902 *ppobj
= This
->ds3db
;
905 WARN("IID_IDirectSound3DBuffer\n");
906 return E_NOINTERFACE
;
909 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
910 ERR("app requested IDirectSound3DListener on secondary buffer\n");
911 return E_NOINTERFACE
;
914 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
916 IKsBufferPropertySetImpl_Create(This
, &(This
->iks
));
918 IKsPropertySet_AddRef((LPKSPROPERTYSET
)This
->iks
);
922 WARN("IID_IKsPropertySet\n");
923 return E_NOINTERFACE
;
926 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
928 return E_NOINTERFACE
;
931 static const IDirectSoundBuffer8Vtbl dsbvt
=
933 IDirectSoundBufferImpl_QueryInterface
,
934 IDirectSoundBufferImpl_AddRef
,
935 IDirectSoundBufferImpl_Release
,
936 IDirectSoundBufferImpl_GetCaps
,
937 IDirectSoundBufferImpl_GetCurrentPosition
,
938 IDirectSoundBufferImpl_GetFormat
,
939 IDirectSoundBufferImpl_GetVolume
,
940 IDirectSoundBufferImpl_GetPan
,
941 IDirectSoundBufferImpl_GetFrequency
,
942 IDirectSoundBufferImpl_GetStatus
,
943 IDirectSoundBufferImpl_Initialize
,
944 IDirectSoundBufferImpl_Lock
,
945 IDirectSoundBufferImpl_Play
,
946 IDirectSoundBufferImpl_SetCurrentPosition
,
947 IDirectSoundBufferImpl_SetFormat
,
948 IDirectSoundBufferImpl_SetVolume
,
949 IDirectSoundBufferImpl_SetPan
,
950 IDirectSoundBufferImpl_SetFrequency
,
951 IDirectSoundBufferImpl_Stop
,
952 IDirectSoundBufferImpl_Unlock
,
953 IDirectSoundBufferImpl_Restore
,
954 IDirectSoundBufferImpl_SetFX
,
955 IDirectSoundBufferImpl_AcquireResources
,
956 IDirectSoundBufferImpl_GetObjectInPath
959 HRESULT
IDirectSoundBufferImpl_Create(
960 DirectSoundDevice
* device
,
961 IDirectSoundBufferImpl
**pdsb
,
962 LPCDSBUFFERDESC dsbd
)
964 IDirectSoundBufferImpl
*dsb
;
965 LPWAVEFORMATEX wfex
= dsbd
->lpwfxFormat
;
968 int use_hw
, alloc_size
, cp_size
;
969 TRACE("(%p,%p,%p)\n",device
,pdsb
,dsbd
);
971 if (dsbd
->dwBufferBytes
< DSBSIZE_MIN
|| dsbd
->dwBufferBytes
> DSBSIZE_MAX
) {
972 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd
->dwBufferBytes
);
974 return DSERR_INVALIDPARAM
; /* FIXME: which error? */
977 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
980 WARN("out of memory\n");
982 return DSERR_OUTOFMEMORY
;
985 TRACE("Created buffer at %p\n", dsb
);
989 dsb
->device
= device
;
990 dsb
->lpVtbl
= &dsbvt
;
993 /* size depends on version */
994 CopyMemory(&dsb
->dsbd
, dsbd
, dsbd
->dwSize
);
996 /* variable sized struct so calculate size based on format */
997 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
) {
998 alloc_size
= sizeof(WAVEFORMATEX
);
999 cp_size
= sizeof(PCMWAVEFORMAT
);
1001 alloc_size
= cp_size
= sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
1003 dsb
->pwfx
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,alloc_size
);
1004 if (dsb
->pwfx
== NULL
) {
1005 WARN("out of memory\n");
1006 HeapFree(GetProcessHeap(),0,dsb
);
1008 return DSERR_OUTOFMEMORY
;
1011 CopyMemory(dsb
->pwfx
, wfex
, cp_size
);
1013 if (dsbd
->dwBufferBytes
% dsbd
->lpwfxFormat
->nBlockAlign
)
1014 dsb
->buflen
= dsbd
->dwBufferBytes
+
1015 (dsbd
->lpwfxFormat
->nBlockAlign
-
1016 (dsbd
->dwBufferBytes
% dsbd
->lpwfxFormat
->nBlockAlign
));
1018 dsb
->buflen
= dsbd
->dwBufferBytes
;
1020 dsb
->freq
= dsbd
->lpwfxFormat
->nSamplesPerSec
;
1022 dsb
->notifies
= NULL
;
1023 dsb
->nrofnotifies
= 0;
1026 /* Check necessary hardware mixing capabilities */
1027 if (wfex
->nChannels
==2) capf
|= DSCAPS_SECONDARYSTEREO
;
1028 else capf
|= DSCAPS_SECONDARYMONO
;
1029 if (wfex
->wBitsPerSample
==16) capf
|= DSCAPS_SECONDARY16BIT
;
1030 else capf
|= DSCAPS_SECONDARY8BIT
;
1032 use_hw
= !!(dsbd
->dwFlags
& DSBCAPS_LOCHARDWARE
);
1033 TRACE("use_hw = %d, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw
, capf
, device
->drvcaps
.dwFlags
);
1034 if (use_hw
&& ((device
->drvcaps
.dwFlags
& capf
) != capf
|| !device
->driver
))
1037 WARN("Format not supported for hardware buffer\n");
1038 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1039 HeapFree(GetProcessHeap(),0,dsb
);
1041 if ((device
->drvcaps
.dwFlags
& capf
) != capf
)
1042 return DSERR_BADFORMAT
;
1043 return DSERR_GENERIC
;
1046 /* FIXME: check hardware sample rate mixing capabilities */
1047 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1048 /* FIXME: check whether any hardware buffers are left */
1049 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1051 /* Allocate an empty buffer */
1052 dsb
->buffer
= HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb
->buffer
)));
1053 if (dsb
->buffer
== NULL
) {
1054 WARN("out of memory\n");
1055 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1056 HeapFree(GetProcessHeap(),0,dsb
);
1058 return DSERR_OUTOFMEMORY
;
1061 /* Allocate system memory for buffer if applicable */
1062 if ((device
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
) || !use_hw
) {
1063 dsb
->buffer
->memory
= HeapAlloc(GetProcessHeap(),0,dsb
->buflen
);
1064 if (dsb
->buffer
->memory
== NULL
) {
1065 WARN("out of memory\n");
1066 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1067 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1068 HeapFree(GetProcessHeap(),0,dsb
);
1070 return DSERR_OUTOFMEMORY
;
1074 /* Allocate the hardware buffer */
1076 err
= IDsDriver_CreateSoundBuffer(device
->driver
,wfex
,dsbd
->dwFlags
,0,
1077 &(dsb
->buflen
),&(dsb
->buffer
->memory
),
1078 (LPVOID
*)&(dsb
->hwbuf
));
1081 WARN("Failed to create hardware secondary buffer: %08x\n", err
);
1082 if (device
->drvdesc
.dwFlags
& DSDDESC_USESYSTEMMEMORY
)
1083 HeapFree(GetProcessHeap(),0,dsb
->buffer
->memory
);
1084 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1085 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1086 HeapFree(GetProcessHeap(),0,dsb
);
1088 return DSERR_GENERIC
;
1092 dsb
->buffer
->ref
= 1;
1093 list_init(&dsb
->buffer
->buffers
);
1094 list_add_head(&dsb
->buffer
->buffers
, &dsb
->entry
);
1095 FillMemory(dsb
->buffer
->memory
, dsb
->buflen
, dsbd
->lpwfxFormat
->wBitsPerSample
== 8 ? 128 : 0);
1097 /* It's not necessary to initialize values to zero since */
1098 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1099 dsb
->buf_mixpos
= dsb
->sec_mixpos
= 0;
1100 dsb
->state
= STATE_STOPPED
;
1102 dsb
->freqAdjust
= ((DWORD64
)dsb
->freq
<< DSOUND_FREQSHIFT
) / device
->pwfx
->nSamplesPerSec
;
1103 dsb
->nAvgBytesPerSec
= dsb
->freq
*
1104 dsbd
->lpwfxFormat
->nBlockAlign
;
1106 /* calculate fragment size and write lead */
1107 DSOUND_RecalcFormat(dsb
);
1109 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
) {
1110 dsb
->ds3db_ds3db
.dwSize
= sizeof(DS3DBUFFER
);
1111 dsb
->ds3db_ds3db
.vPosition
.x
= 0.0;
1112 dsb
->ds3db_ds3db
.vPosition
.y
= 0.0;
1113 dsb
->ds3db_ds3db
.vPosition
.z
= 0.0;
1114 dsb
->ds3db_ds3db
.vVelocity
.x
= 0.0;
1115 dsb
->ds3db_ds3db
.vVelocity
.y
= 0.0;
1116 dsb
->ds3db_ds3db
.vVelocity
.z
= 0.0;
1117 dsb
->ds3db_ds3db
.dwInsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
1118 dsb
->ds3db_ds3db
.dwOutsideConeAngle
= DS3D_DEFAULTCONEANGLE
;
1119 dsb
->ds3db_ds3db
.vConeOrientation
.x
= 0.0;
1120 dsb
->ds3db_ds3db
.vConeOrientation
.y
= 0.0;
1121 dsb
->ds3db_ds3db
.vConeOrientation
.z
= 0.0;
1122 dsb
->ds3db_ds3db
.lConeOutsideVolume
= DS3D_DEFAULTCONEOUTSIDEVOLUME
;
1123 dsb
->ds3db_ds3db
.flMinDistance
= DS3D_DEFAULTMINDISTANCE
;
1124 dsb
->ds3db_ds3db
.flMaxDistance
= DS3D_DEFAULTMAXDISTANCE
;
1125 dsb
->ds3db_ds3db
.dwMode
= DS3DMODE_NORMAL
;
1127 dsb
->ds3db_need_recalc
= FALSE
;
1128 DSOUND_Calc3DBuffer(dsb
);
1130 DSOUND_RecalcVolPan(&(dsb
->volpan
));
1132 RtlInitializeResource(&dsb
->lock
);
1134 /* register buffer if not primary */
1135 if (!(dsbd
->dwFlags
& DSBCAPS_PRIMARYBUFFER
)) {
1136 err
= DirectSoundDevice_AddBuffer(device
, dsb
);
1138 HeapFree(GetProcessHeap(),0,dsb
->buffer
->memory
);
1139 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1140 RtlDeleteResource(&dsb
->lock
);
1141 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1142 HeapFree(GetProcessHeap(),0,dsb
);
1151 HRESULT
IDirectSoundBufferImpl_Destroy(
1152 IDirectSoundBufferImpl
*pdsb
)
1154 TRACE("(%p)\n",pdsb
);
1156 /* This keeps the *_Destroy functions from possibly deleting
1157 * this object until it is ready to be deleted */
1158 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8
)pdsb
);
1161 WARN("iks not NULL\n");
1162 IKsBufferPropertySetImpl_Destroy(pdsb
->iks
);
1167 WARN("ds3db not NULL\n");
1168 IDirectSound3DBufferImpl_Destroy(pdsb
->ds3db
);
1173 WARN("notify not NULL\n");
1174 IDirectSoundNotifyImpl_Destroy(pdsb
->notify
);
1175 pdsb
->notify
= NULL
;
1178 if (pdsb
->secondary
) {
1179 WARN("dsb not NULL\n");
1180 SecondaryBufferImpl_Destroy(pdsb
->secondary
);
1181 pdsb
->secondary
= NULL
;
1184 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8
)pdsb
) > 0);
1189 HRESULT
IDirectSoundBufferImpl_Duplicate(
1190 DirectSoundDevice
*device
,
1191 IDirectSoundBufferImpl
**ppdsb
,
1192 IDirectSoundBufferImpl
*pdsb
)
1194 IDirectSoundBufferImpl
*dsb
;
1195 HRESULT hres
= DS_OK
;
1197 TRACE("(%p,%p,%p)\n", device
, pdsb
, pdsb
);
1199 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1202 WARN("out of memory\n");
1204 return DSERR_OUTOFMEMORY
;
1207 CopyMemory(dsb
, pdsb
, sizeof(IDirectSoundBufferImpl
));
1210 TRACE("duplicating hardware buffer\n");
1212 hres
= IDsDriver_DuplicateSoundBuffer(device
->driver
, pdsb
->hwbuf
,
1213 (LPVOID
*)&dsb
->hwbuf
);
1215 WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres
);
1216 HeapFree(GetProcessHeap(),0,dsb
);
1223 list_add_head(&dsb
->buffer
->buffers
, &dsb
->entry
);
1225 dsb
->state
= STATE_STOPPED
;
1226 dsb
->buf_mixpos
= dsb
->sec_mixpos
= 0;
1227 dsb
->device
= device
;
1229 dsb
->iks
= NULL
; /* FIXME? */
1230 dsb
->secondary
= NULL
;
1231 dsb
->tmp_buffer
= NULL
;
1232 DSOUND_RecalcFormat(dsb
);
1233 DSOUND_MixToTemporary(dsb
, 0, dsb
->buflen
, FALSE
);
1235 /* variable sized struct so calculate size based on format */
1236 size
= sizeof(WAVEFORMATEX
) + pdsb
->pwfx
->cbSize
;
1238 dsb
->pwfx
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,size
);
1239 if (dsb
->pwfx
== NULL
) {
1240 WARN("out of memory\n");
1241 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1242 HeapFree(GetProcessHeap(),0,dsb
);
1244 return DSERR_OUTOFMEMORY
;
1247 CopyMemory(dsb
->pwfx
, pdsb
->pwfx
, size
);
1249 RtlInitializeResource(&dsb
->lock
);
1251 /* register buffer */
1252 hres
= DirectSoundDevice_AddBuffer(device
, dsb
);
1253 if (hres
!= DS_OK
) {
1254 RtlDeleteResource(&dsb
->lock
);
1255 HeapFree(GetProcessHeap(),0,dsb
->tmp_buffer
);
1256 HeapFree(GetProcessHeap(),0,dsb
->buffer
);
1257 HeapFree(GetProcessHeap(),0,dsb
->pwfx
);
1258 HeapFree(GetProcessHeap(),0,dsb
);
1266 /*******************************************************************************
1270 static HRESULT WINAPI
SecondaryBufferImpl_QueryInterface(
1271 LPDIRECTSOUNDBUFFER8 iface
,REFIID riid
,LPVOID
*ppobj
)
1273 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1274 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
1276 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8
)This
->dsb
,riid
,ppobj
);
1279 static ULONG WINAPI
SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface
)
1281 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1282 ULONG ref
= InterlockedIncrement(&(This
->ref
));
1283 TRACE("(%p) ref was %d\n", This
, ref
- 1);
1287 static ULONG WINAPI
SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface
)
1289 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1291 TRACE("(%p)\n", This
);
1292 ref
= InterlockedDecrement(&(This
->ref
));
1293 TRACE("ref was %d\n", ref
+ 1);
1296 This
->dsb
->secondary
= NULL
;
1297 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1298 HeapFree(GetProcessHeap(), 0, This
);
1299 TRACE("(%p) released\n", This
);
1304 static HRESULT WINAPI
SecondaryBufferImpl_GetCaps(
1305 LPDIRECTSOUNDBUFFER8 iface
,LPDSBCAPS caps
)
1307 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1308 TRACE("(%p)->(%p)\n",This
,caps
);
1310 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8
)This
->dsb
,caps
);
1313 static HRESULT WINAPI
SecondaryBufferImpl_GetCurrentPosition(
1314 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD playpos
,LPDWORD writepos
)
1316 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1317 TRACE("(%p,%p,%p)\n",This
,playpos
,writepos
);
1319 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8
)This
->dsb
,playpos
,writepos
);
1322 static HRESULT WINAPI
SecondaryBufferImpl_GetFormat(
1323 LPDIRECTSOUNDBUFFER8 iface
,LPWAVEFORMATEX lpwf
,DWORD wfsize
,LPDWORD wfwritten
)
1325 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1326 TRACE("(%p,%p,%d,%p)\n",This
,lpwf
,wfsize
,wfwritten
);
1328 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8
)This
->dsb
,lpwf
,wfsize
,wfwritten
);
1331 static HRESULT WINAPI
SecondaryBufferImpl_GetVolume(
1332 LPDIRECTSOUNDBUFFER8 iface
,LPLONG vol
)
1334 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1335 TRACE("(%p,%p)\n",This
,vol
);
1337 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8
)This
->dsb
,vol
);
1340 static HRESULT WINAPI
SecondaryBufferImpl_GetPan(
1341 LPDIRECTSOUNDBUFFER8 iface
,LPLONG pan
)
1343 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1344 TRACE("(%p,%p)\n",This
,pan
);
1346 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8
)This
->dsb
,pan
);
1349 static HRESULT WINAPI
SecondaryBufferImpl_GetFrequency(
1350 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD freq
)
1352 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1353 TRACE("(%p,%p)\n",This
,freq
);
1355 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8
)This
->dsb
,freq
);
1358 static HRESULT WINAPI
SecondaryBufferImpl_GetStatus(
1359 LPDIRECTSOUNDBUFFER8 iface
,LPDWORD status
)
1361 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1362 TRACE("(%p,%p)\n",This
,status
);
1364 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8
)This
->dsb
,status
);
1367 static HRESULT WINAPI
SecondaryBufferImpl_Initialize(
1368 LPDIRECTSOUNDBUFFER8 iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
)
1370 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1371 TRACE("(%p,%p,%p)\n",This
,dsound
,dbsd
);
1373 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dsound
,dbsd
);
1376 static HRESULT WINAPI
SecondaryBufferImpl_Lock(
1377 LPDIRECTSOUNDBUFFER8 iface
,
1380 LPVOID
*lplpaudioptr1
,
1381 LPDWORD audiobytes1
,
1382 LPVOID
*lplpaudioptr2
,
1383 LPDWORD audiobytes2
,
1386 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1387 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1388 This
,writecursor
,writebytes
,lplpaudioptr1
,audiobytes1
,lplpaudioptr2
,audiobytes2
,dwFlags
);
1390 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8
)This
->dsb
,
1391 writecursor
,writebytes
,lplpaudioptr1
,audiobytes1
,lplpaudioptr2
,audiobytes2
,dwFlags
);
1394 static HRESULT WINAPI
SecondaryBufferImpl_Play(
1395 LPDIRECTSOUNDBUFFER8 iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
)
1397 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1398 TRACE("(%p,%08x,%08x,%08x)\n",This
,reserved1
,reserved2
,flags
);
1400 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8
)This
->dsb
,reserved1
,reserved2
,flags
);
1403 static HRESULT WINAPI
SecondaryBufferImpl_SetCurrentPosition(
1404 LPDIRECTSOUNDBUFFER8 iface
,DWORD newpos
)
1406 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1407 TRACE("(%p,%d)\n",This
,newpos
);
1409 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8
)This
->dsb
,newpos
);
1412 static HRESULT WINAPI
SecondaryBufferImpl_SetFormat(
1413 LPDIRECTSOUNDBUFFER8 iface
,LPCWAVEFORMATEX wfex
)
1415 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1416 TRACE("(%p,%p)\n",This
,wfex
);
1418 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8
)This
->dsb
,wfex
);
1421 static HRESULT WINAPI
SecondaryBufferImpl_SetVolume(
1422 LPDIRECTSOUNDBUFFER8 iface
,LONG vol
)
1424 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1425 TRACE("(%p,%d)\n",This
,vol
);
1427 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8
)This
->dsb
,vol
);
1430 static HRESULT WINAPI
SecondaryBufferImpl_SetPan(
1431 LPDIRECTSOUNDBUFFER8 iface
,LONG pan
)
1433 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1434 TRACE("(%p,%d)\n",This
,pan
);
1436 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8
)This
->dsb
,pan
);
1439 static HRESULT WINAPI
SecondaryBufferImpl_SetFrequency(
1440 LPDIRECTSOUNDBUFFER8 iface
,DWORD freq
)
1442 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1443 TRACE("(%p,%d)\n",This
,freq
);
1445 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8
)This
->dsb
,freq
);
1448 static HRESULT WINAPI
SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface
)
1450 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1451 TRACE("(%p)\n",This
);
1453 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1456 static HRESULT WINAPI
SecondaryBufferImpl_Unlock(
1457 LPDIRECTSOUNDBUFFER8 iface
,
1458 LPVOID lpvAudioPtr1
,
1459 DWORD dwAudioBytes1
,
1460 LPVOID lpvAudioPtr2
,
1461 DWORD dwAudioBytes2
)
1463 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1464 TRACE("(%p,%p,%d,%p,%d)\n",
1465 This
, lpvAudioPtr1
, dwAudioBytes1
, lpvAudioPtr2
, dwAudioBytes2
);
1467 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8
)This
->dsb
,
1468 lpvAudioPtr1
,dwAudioBytes1
,lpvAudioPtr2
,dwAudioBytes2
);
1471 static HRESULT WINAPI
SecondaryBufferImpl_Restore(
1472 LPDIRECTSOUNDBUFFER8 iface
)
1474 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1475 TRACE("(%p)\n",This
);
1477 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8
)This
->dsb
);
1480 static HRESULT WINAPI
SecondaryBufferImpl_SetFX(
1481 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwEffectsCount
,LPDSEFFECTDESC pDSFXDesc
,LPDWORD pdwResultCodes
)
1483 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1484 TRACE("(%p,%u,%p,%p)\n",This
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
1486 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dwEffectsCount
,pDSFXDesc
,pdwResultCodes
);
1489 static HRESULT WINAPI
SecondaryBufferImpl_AcquireResources(
1490 LPDIRECTSOUNDBUFFER8 iface
,DWORD dwFlags
,DWORD dwEffectsCount
,LPDWORD pdwResultCodes
)
1492 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1493 TRACE("(%p,%08u,%u,%p)\n",This
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
1495 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8
)This
->dsb
,dwFlags
,dwEffectsCount
,pdwResultCodes
);
1498 static HRESULT WINAPI
SecondaryBufferImpl_GetObjectInPath(
1499 LPDIRECTSOUNDBUFFER8 iface
,REFGUID rguidObject
,DWORD dwIndex
,REFGUID rguidInterface
,LPVOID
* ppObject
)
1501 SecondaryBufferImpl
*This
= (SecondaryBufferImpl
*)iface
;
1502 TRACE("(%p,%s,%u,%s,%p)\n",This
,debugstr_guid(rguidObject
),dwIndex
,debugstr_guid(rguidInterface
),ppObject
);
1504 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8
)This
->dsb
,rguidObject
,dwIndex
,rguidInterface
,ppObject
);
1507 static const IDirectSoundBuffer8Vtbl sbvt
=
1509 SecondaryBufferImpl_QueryInterface
,
1510 SecondaryBufferImpl_AddRef
,
1511 SecondaryBufferImpl_Release
,
1512 SecondaryBufferImpl_GetCaps
,
1513 SecondaryBufferImpl_GetCurrentPosition
,
1514 SecondaryBufferImpl_GetFormat
,
1515 SecondaryBufferImpl_GetVolume
,
1516 SecondaryBufferImpl_GetPan
,
1517 SecondaryBufferImpl_GetFrequency
,
1518 SecondaryBufferImpl_GetStatus
,
1519 SecondaryBufferImpl_Initialize
,
1520 SecondaryBufferImpl_Lock
,
1521 SecondaryBufferImpl_Play
,
1522 SecondaryBufferImpl_SetCurrentPosition
,
1523 SecondaryBufferImpl_SetFormat
,
1524 SecondaryBufferImpl_SetVolume
,
1525 SecondaryBufferImpl_SetPan
,
1526 SecondaryBufferImpl_SetFrequency
,
1527 SecondaryBufferImpl_Stop
,
1528 SecondaryBufferImpl_Unlock
,
1529 SecondaryBufferImpl_Restore
,
1530 SecondaryBufferImpl_SetFX
,
1531 SecondaryBufferImpl_AcquireResources
,
1532 SecondaryBufferImpl_GetObjectInPath
1535 HRESULT
SecondaryBufferImpl_Create(
1536 IDirectSoundBufferImpl
*dsb
,
1537 SecondaryBufferImpl
**psb
)
1539 SecondaryBufferImpl
*sb
;
1540 TRACE("(%p,%p)\n",dsb
,psb
);
1542 sb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*sb
));
1545 WARN("out of memory\n");
1547 return DSERR_OUTOFMEMORY
;
1553 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8
)dsb
);
1558 static HRESULT
SecondaryBufferImpl_Destroy(
1559 SecondaryBufferImpl
*pdsb
)
1561 TRACE("(%p)\n",pdsb
);
1563 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8
)pdsb
) > 0);