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 EnterCriticalSection(&(This->lock));
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 LeaveCriticalSection(&(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 EnterCriticalSection(&(This->lock));
293 oldFreq = This->freq;
295 if (freq != oldFreq) {
296 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
297 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
298 DSOUND_RecalcFormat(This);
301 LeaveCriticalSection(&(This->lock));
307 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
308 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
310 HRESULT hres = DS_OK;
311 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
312 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
315 EnterCriticalSection(&(This->lock));
317 This->playflags = flags;
318 if (This->state == STATE_STOPPED) {
320 This->startpos = This->buf_mixpos;
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 LeaveCriticalSection(&(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 EnterCriticalSection(&(This->lock));
347 if (This->state == STATE_PLAYING)
348 This->state = STATE_STOPPING;
349 else if (This->state == STATE_STARTING)
350 This->state = STATE_STOPPED;
352 hres = IDsDriverBuffer_Stop(This->hwbuf);
354 WARN("IDsDriverBuffer_Stop failed\n");
356 This->state = STATE_STOPPED;
358 DSOUND_CheckEvent(This, 0);
360 LeaveCriticalSection(&(This->lock));
366 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
368 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
369 ULONG ref = InterlockedIncrement(&(This->ref));
370 TRACE("(%p) ref was %d\n", This, ref - 1);
374 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
376 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
377 ULONG ref = InterlockedDecrement(&(This->ref));
378 TRACE("(%p) ref was %d\n", This, ref + 1);
381 DirectSoundDevice_RemoveBuffer(This->device, This);
383 This->lock.DebugInfo->Spare[0] = 0;
384 DeleteCriticalSection(&(This->lock));
387 IDsDriverBuffer_Release(This->hwbuf);
388 if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
390 if (This->buffer->ref==0) {
391 HeapFree(GetProcessHeap(),0,This->buffer->memory);
392 HeapFree(GetProcessHeap(),0,This->buffer);
397 if (This->buffer->ref==0) {
398 HeapFree(GetProcessHeap(),0,This->buffer->memory);
399 HeapFree(GetProcessHeap(),0,This->buffer);
403 HeapFree(GetProcessHeap(), 0, This->notifies);
404 HeapFree(GetProcessHeap(), 0, This->pwfx);
405 HeapFree(GetProcessHeap(), 0, This);
407 TRACE("(%p) released\n", This);
412 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This, DWORD pplay, DWORD pwrite)
414 DWORD bplay = This->buf_mixpos;
415 DWORD pmix = This->primary_mixpos;
416 DirectSoundDevice * device = This->device;
417 TRACE("(%p, pplay=%u, pwrite=%u)\n", This, pplay, pwrite);
419 /* the actual primary play position (pplay) is always behind last mixed (pmix),
420 * unless the computer is too slow or something */
421 /* we need to know how far away we are from there */
422 if (pmix < pplay) pmix += device->buflen; /* wraparound */
425 /* detect buffer underrun (sanity) */
426 if (pwrite < pplay) pwrite += device->buflen; /* wraparound */
428 if (pmix > (ds_snd_queue_max * device->fraglen + pwrite + device->writelead)) {
429 ERR("detected an underrun: primary queue was %d\n",pmix);
433 TRACE("primary back-samples=%d\n",pmix);
435 /* divide the offset by its sample size */
436 pmix /= device->pwfx->nBlockAlign;
438 /* adjust for our frequency */
439 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
441 /* multiply by our own sample size */
442 pmix *= This->pwfx->nBlockAlign;
444 TRACE("this back-offset=%d\n", pmix);
447 if(pmix > This->buflen){
448 ERR("Bad length in CalcPlayPosition!\n");
452 /* subtract from our last mixed position */
453 if (bplay < pmix) bplay += This->buflen; /* wraparound */
456 /* check for lead-in */
457 if (This->leadin && ((bplay < This->startpos) || (bplay > This->buf_mixpos))) {
458 /* seems we haven't started playing yet */
459 TRACE("this still in lead-in phase\n");
460 bplay = This->startpos;
464 if(bplay > This->buflen){
465 ERR("Bad play position in CalcPlayPosition!\n");
469 /* return the result */
473 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
474 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
477 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
478 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
480 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
482 WARN("IDsDriverBuffer_GetPosition failed\n");
486 if (playpos && (This->state != STATE_PLAYING)) {
487 /* we haven't been merged into the primary buffer (yet) */
488 *playpos = This->buf_mixpos;
489 } else if (playpos) {
492 /* get primary lock, before messing with primary/device data */
493 EnterCriticalSection(&(This->device->mixlock));
495 /* let's get this exact; first, recursively call GetPosition on the primary */
496 if (DSOUND_PrimaryGetPosition(This->device, &pplay, &pwrite) != DS_OK)
497 WARN("DSOUND_PrimaryGetPosition failed\n");
498 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->device->hwbuf) {
499 /* calculate play position using this */
500 *playpos = DSOUND_CalcPlayPosition(This, pplay, pwrite);
502 /* (unless the app isn't using GETCURRENTPOSITION2) */
503 /* don't know exactly how this should be handled...
504 * the docs says that play cursor is reported as directly
505 * behind write cursor, hmm... */
506 /* let's just do what might work for Half-Life */
508 wp = (This->device->pwplay + ds_hel_margin) * This->device->fraglen;
509 wp %= This->device->buflen;
510 *playpos = DSOUND_CalcPlayPosition(This, wp, pwrite);
511 TRACE("Using non-GETCURRENTPOSITION2\n");
514 LeaveCriticalSection(&(This->device->mixlock));
517 *writepos = This->buf_mixpos;
520 if (This->state != STATE_STOPPED) {
521 /* apply the documented 10ms lead to writepos */
522 *writepos += This->writelead;
524 *writepos %= This->buflen;
527 This->last_playpos = *playpos;
529 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
530 playpos?*playpos:0, writepos?*writepos:0, This->buflen, This, GetTickCount());
535 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
536 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
538 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
539 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
541 if (status == NULL) {
542 WARN("invalid parameter: status = NULL\n");
543 return DSERR_INVALIDPARAM;
547 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
548 *status |= DSBSTATUS_PLAYING;
549 if (This->playflags & DSBPLAY_LOOPING)
550 *status |= DSBSTATUS_LOOPING;
553 TRACE("status=%x\n", *status);
558 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
559 LPDIRECTSOUNDBUFFER8 iface,
565 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
566 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
568 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
570 if (lpwf) { /* NULL is valid */
571 if (wfsize >= size) {
572 CopyMemory(lpwf,This->pwfx,size);
576 WARN("invalid parameter: wfsize too small\n");
579 return DSERR_INVALIDPARAM;
583 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
585 WARN("invalid parameter: wfwritten == NULL\n");
586 return DSERR_INVALIDPARAM;
593 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
594 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
596 HRESULT hres = DS_OK;
597 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
599 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
611 /* when this flag is set, writecursor is meaningless and must be calculated */
612 if (flags & DSBLOCK_FROMWRITECURSOR) {
613 /* GetCurrentPosition does too much magic to duplicate here */
614 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
616 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
621 /* when this flag is set, writebytes is meaningless and must be set */
622 if (flags & DSBLOCK_ENTIREBUFFER)
623 writebytes = This->buflen;
625 if (writecursor >= This->buflen) {
626 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
627 writecursor, This->buflen);
628 return DSERR_INVALIDPARAM;
631 if (writebytes > This->buflen) {
632 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
633 writebytes, This->buflen);
634 return DSERR_INVALIDPARAM;
638 EnterCriticalSection(&(This->lock));
640 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
641 hres = IDsDriverBuffer_Lock(This->hwbuf,
642 lplpaudioptr1, audiobytes1,
643 lplpaudioptr2, audiobytes2,
644 writecursor, writebytes,
647 WARN("IDsDriverBuffer_Lock failed\n");
648 LeaveCriticalSection(&(This->lock));
652 if (writecursor+writebytes <= This->buflen) {
653 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
654 *audiobytes1 = writebytes;
656 *(LPBYTE*)lplpaudioptr2 = NULL;
659 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
660 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
661 TRACE("->%d.0\n",writebytes);
663 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
664 *audiobytes1 = This->buflen-writecursor;
666 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
668 *audiobytes2 = writebytes-(This->buflen-writecursor);
669 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
673 LeaveCriticalSection(&(This->lock));
679 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
680 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
682 HRESULT hres = DS_OK;
683 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
684 TRACE("(%p,%d)\n",This,newpos);
687 EnterCriticalSection(&(This->lock));
689 /* start mixing from this new location instead */
690 newpos %= This->buflen;
691 newpos -= newpos%This->pwfx->nBlockAlign;
692 This->buf_mixpos = newpos;
694 /* at this point, do not attempt to reset buffers, mess with primary mix position,
695 or anything like that to reduce latancy. The data already prebuffered cannot be changed */
697 /* position HW buffer if applicable */
699 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
701 WARN("IDsDriverBuffer_SetPosition failed\n");
704 LeaveCriticalSection(&(This->lock));
710 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
711 LPDIRECTSOUNDBUFFER8 iface,LONG pan
713 HRESULT hres = DS_OK;
714 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
716 TRACE("(%p,%d)\n",This,pan);
718 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
719 WARN("invalid parameter: pan = %d\n", pan);
720 return DSERR_INVALIDPARAM;
723 /* You cannot use both pan and 3D controls */
724 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
725 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
726 WARN("control unavailable\n");
727 return DSERR_CONTROLUNAVAIL;
731 EnterCriticalSection(&(This->lock));
733 if (This->volpan.lPan != pan) {
734 This->volpan.lPan = pan;
735 DSOUND_RecalcVolPan(&(This->volpan));
738 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
740 WARN("IDsDriverBuffer_SetVolumePan failed\n");
744 LeaveCriticalSection(&(This->lock));
750 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
751 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
753 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
754 TRACE("(%p,%p)\n",This,pan);
756 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
757 WARN("control unavailable\n");
758 return DSERR_CONTROLUNAVAIL;
762 WARN("invalid parameter: pan = NULL\n");
763 return DSERR_INVALIDPARAM;
766 *pan = This->volpan.lPan;
771 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
772 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
774 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
775 HRESULT hres = DS_OK;
777 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
780 EnterCriticalSection(&(This->lock));
782 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
783 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
785 WARN("IDsDriverBuffer_Unlock failed\n");
788 LeaveCriticalSection(&(This->lock));
794 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
795 LPDIRECTSOUNDBUFFER8 iface
797 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
798 FIXME("(%p):stub\n",This);
802 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
803 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
805 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
806 TRACE("(%p,%p)\n",This,freq);
809 WARN("invalid parameter: freq = NULL\n");
810 return DSERR_INVALIDPARAM;
814 TRACE("-> %d\n", *freq);
819 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
820 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
822 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
825 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
828 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
830 WARN("control unavailable\n");
831 return DSERR_CONTROLUNAVAIL;
834 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
835 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
837 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
840 FIXME("(%p,%08u,%u,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
843 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
845 WARN("control unavailable\n");
846 return DSERR_CONTROLUNAVAIL;
849 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
850 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
852 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
854 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
856 WARN("control unavailable\n");
857 return DSERR_CONTROLUNAVAIL;
860 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
861 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
863 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
864 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
865 DPRINTF("Re-Init!!!\n");
866 WARN("already initialized\n");
867 return DSERR_ALREADYINITIALIZED;
870 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
871 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
873 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
874 TRACE("(%p)->(%p)\n",This,caps);
877 WARN("invalid parameter: caps == NULL\n");
878 return DSERR_INVALIDPARAM;
881 if (caps->dwSize < sizeof(*caps)) {
882 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
883 return DSERR_INVALIDPARAM;
886 caps->dwFlags = This->dsbd.dwFlags;
887 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
888 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
890 caps->dwBufferBytes = This->buflen;
892 /* According to windows, this is zero*/
893 caps->dwUnlockTransferRate = 0;
894 caps->dwPlayCpuOverhead = 0;
899 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
900 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
902 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
904 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
907 WARN("invalid parameter\n");
911 *ppobj = NULL; /* assume failure */
913 if ( IsEqualGUID(riid, &IID_IUnknown) ||
914 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
915 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
916 if (!This->secondary)
917 SecondaryBufferImpl_Create(This, &(This->secondary));
918 if (This->secondary) {
919 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->secondary);
920 *ppobj = This->secondary;
923 WARN("IID_IDirectSoundBuffer\n");
924 return E_NOINTERFACE;
927 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
929 IDirectSoundNotifyImpl_Create(This, &(This->notify));
931 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
932 *ppobj = This->notify;
935 WARN("IID_IDirectSoundNotify\n");
936 return E_NOINTERFACE;
939 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
941 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
943 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
944 *ppobj = This->ds3db;
947 WARN("IID_IDirectSound3DBuffer\n");
948 return E_NOINTERFACE;
951 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
952 ERR("app requested IDirectSound3DListener on secondary buffer\n");
953 return E_NOINTERFACE;
956 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
958 IKsBufferPropertySetImpl_Create(This, &(This->iks));
960 IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
964 WARN("IID_IKsPropertySet\n");
965 return E_NOINTERFACE;
968 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
970 return E_NOINTERFACE;
973 static const IDirectSoundBuffer8Vtbl dsbvt =
975 IDirectSoundBufferImpl_QueryInterface,
976 IDirectSoundBufferImpl_AddRef,
977 IDirectSoundBufferImpl_Release,
978 IDirectSoundBufferImpl_GetCaps,
979 IDirectSoundBufferImpl_GetCurrentPosition,
980 IDirectSoundBufferImpl_GetFormat,
981 IDirectSoundBufferImpl_GetVolume,
982 IDirectSoundBufferImpl_GetPan,
983 IDirectSoundBufferImpl_GetFrequency,
984 IDirectSoundBufferImpl_GetStatus,
985 IDirectSoundBufferImpl_Initialize,
986 IDirectSoundBufferImpl_Lock,
987 IDirectSoundBufferImpl_Play,
988 IDirectSoundBufferImpl_SetCurrentPosition,
989 IDirectSoundBufferImpl_SetFormat,
990 IDirectSoundBufferImpl_SetVolume,
991 IDirectSoundBufferImpl_SetPan,
992 IDirectSoundBufferImpl_SetFrequency,
993 IDirectSoundBufferImpl_Stop,
994 IDirectSoundBufferImpl_Unlock,
995 IDirectSoundBufferImpl_Restore,
996 IDirectSoundBufferImpl_SetFX,
997 IDirectSoundBufferImpl_AcquireResources,
998 IDirectSoundBufferImpl_GetObjectInPath
1001 HRESULT IDirectSoundBufferImpl_Create(
1002 DirectSoundDevice * device,
1003 IDirectSoundBufferImpl **pdsb,
1004 LPCDSBUFFERDESC dsbd)
1006 IDirectSoundBufferImpl *dsb;
1007 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1008 HRESULT err = DS_OK;
1010 int use_hw, alloc_size, cp_size;
1011 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
1013 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1014 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
1016 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1019 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1022 WARN("out of memory\n");
1024 return DSERR_OUTOFMEMORY;
1027 TRACE("Created buffer at %p\n", dsb);
1031 dsb->device = device;
1032 dsb->lpVtbl = &dsbvt;
1037 /* size depends on version */
1038 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
1040 /* variable sized struct so calculate size based on format */
1041 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1042 alloc_size = sizeof(WAVEFORMATEX);
1043 cp_size = sizeof(PCMWAVEFORMAT);
1045 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
1047 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
1048 if (dsb->pwfx == NULL) {
1049 WARN("out of memory\n");
1050 HeapFree(GetProcessHeap(),0,dsb);
1052 return DSERR_OUTOFMEMORY;
1055 CopyMemory(dsb->pwfx, wfex, cp_size);
1057 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1058 dsb->buflen = dsbd->dwBufferBytes +
1059 (dsbd->lpwfxFormat->nBlockAlign -
1060 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1062 dsb->buflen = dsbd->dwBufferBytes;
1064 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1066 dsb->notifies = NULL;
1067 dsb->nrofnotifies = 0;
1070 /* Check necessary hardware mixing capabilities */
1071 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1072 else capf |= DSCAPS_SECONDARYMONO;
1073 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1074 else capf |= DSCAPS_SECONDARY8BIT;
1076 use_hw = (device->drvcaps.dwFlags & capf) == capf;
1077 TRACE("use_hw = 0x%08x, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw, capf, device->drvcaps.dwFlags);
1079 /* FIXME: check hardware sample rate mixing capabilities */
1080 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1081 /* FIXME: check whether any hardware buffers are left */
1082 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1084 /* Allocate an empty buffer */
1085 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1086 if (dsb->buffer == NULL) {
1087 WARN("out of memory\n");
1088 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1089 HeapFree(GetProcessHeap(),0,dsb);
1091 return DSERR_OUTOFMEMORY;
1094 /* Allocate system memory for buffer if applicable */
1095 if ((device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1096 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1097 if (dsb->buffer->memory == NULL) {
1098 WARN("out of memory\n");
1099 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1100 HeapFree(GetProcessHeap(),0,dsb->buffer);
1101 HeapFree(GetProcessHeap(),0,dsb);
1103 return DSERR_OUTOFMEMORY;
1105 dsb->buffer->ref = 1;
1106 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1109 /* Allocate the hardware buffer */
1111 err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1112 &(dsb->buflen),&(dsb->buffer->memory),
1113 (LPVOID*)&(dsb->hwbuf));
1114 /* fall back to software buffer on failure */
1116 TRACE("IDsDriver_CreateSoundBuffer failed, falling back to software buffer\n");
1118 if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1119 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1120 if (dsb->buffer->memory == NULL) {
1121 WARN("out of memory\n");
1122 HeapFree(GetProcessHeap(),0,dsb->buffer);
1123 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1124 HeapFree(GetProcessHeap(),0,dsb);
1126 return DSERR_OUTOFMEMORY;
1128 dsb->buffer->ref = 1;
1129 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1135 /* calculate fragment size and write lead */
1136 DSOUND_RecalcFormat(dsb);
1138 /* It's not necessary to initialize values to zero since */
1139 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1141 dsb->buf_mixpos = 0;
1142 dsb->state = STATE_STOPPED;
1144 dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
1145 device->pwfx->nSamplesPerSec;
1146 dsb->nAvgBytesPerSec = dsb->freq *
1147 dsbd->lpwfxFormat->nBlockAlign;
1149 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1150 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1151 dsb->ds3db_ds3db.vPosition.x = 0.0;
1152 dsb->ds3db_ds3db.vPosition.y = 0.0;
1153 dsb->ds3db_ds3db.vPosition.z = 0.0;
1154 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1155 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1156 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1157 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1158 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1159 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1160 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1161 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1162 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1163 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1164 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1165 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1167 dsb->ds3db_need_recalc = FALSE;
1168 DSOUND_Calc3DBuffer(dsb);
1170 DSOUND_RecalcVolPan(&(dsb->volpan));
1172 InitializeCriticalSection(&(dsb->lock));
1173 dsb->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectSoundBufferImpl.lock");
1175 /* register buffer if not primary */
1176 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1177 err = DirectSoundDevice_AddBuffer(device, dsb);
1179 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1180 HeapFree(GetProcessHeap(),0,dsb->buffer);
1181 dsb->lock.DebugInfo->Spare[0] = 0;
1182 DeleteCriticalSection(&(dsb->lock));
1183 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1184 HeapFree(GetProcessHeap(),0,dsb);
1193 HRESULT IDirectSoundBufferImpl_Destroy(
1194 IDirectSoundBufferImpl *pdsb)
1196 TRACE("(%p)\n",pdsb);
1198 /* This keeps the *_Destroy functions from possibly deleting
1199 * this object until it is ready to be deleted */
1200 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1203 WARN("iks not NULL\n");
1204 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1209 WARN("ds3db not NULL\n");
1210 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1215 WARN("notify not NULL\n");
1216 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1217 pdsb->notify = NULL;
1220 if (pdsb->secondary) {
1221 WARN("dsb not NULL\n");
1222 SecondaryBufferImpl_Destroy(pdsb->secondary);
1223 pdsb->secondary = NULL;
1226 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1231 HRESULT IDirectSoundBufferImpl_Duplicate(
1232 DirectSoundDevice *device,
1233 IDirectSoundBufferImpl **ppdsb,
1234 IDirectSoundBufferImpl *pdsb)
1236 IDirectSoundBufferImpl *dsb;
1237 HRESULT hres = DS_OK;
1239 TRACE("(%p,%p,%p)\n", device, pdsb, pdsb);
1241 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1244 WARN("out of memory\n");
1246 return DSERR_OUTOFMEMORY;
1249 CopyMemory(dsb, pdsb, sizeof(IDirectSoundBufferImpl));
1252 TRACE("duplicating hardware buffer\n");
1254 hres = IDsDriver_DuplicateSoundBuffer(device->driver, pdsb->hwbuf,
1255 (LPVOID *)&dsb->hwbuf);
1256 if (hres != DS_OK) {
1257 TRACE("IDsDriver_DuplicateSoundBuffer failed, falling back to "
1258 "software buffer\n");
1260 /* allocate buffer */
1261 if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1262 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1263 if (dsb->buffer == NULL) {
1264 WARN("out of memory\n");
1265 HeapFree(GetProcessHeap(),0,dsb);
1267 return DSERR_OUTOFMEMORY;
1270 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1271 if (dsb->buffer->memory == NULL) {
1272 WARN("out of memory\n");
1273 HeapFree(GetProcessHeap(),0,dsb->buffer);
1274 HeapFree(GetProcessHeap(),0,dsb);
1276 return DSERR_OUTOFMEMORY;
1278 dsb->buffer->ref = 1;
1280 /* FIXME: copy buffer ? */
1289 dsb->state = STATE_STOPPED;
1291 dsb->buf_mixpos = 0;
1292 dsb->device = device;
1294 dsb->iks = NULL; /* FIXME? */
1295 dsb->secondary = NULL;
1297 /* variable sized struct so calculate size based on format */
1298 size = sizeof(WAVEFORMATEX) + pdsb->pwfx->cbSize;
1300 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
1301 if (dsb->pwfx == NULL) {
1302 WARN("out of memory\n");
1303 HeapFree(GetProcessHeap(),0,dsb->buffer);
1304 HeapFree(GetProcessHeap(),0,dsb);
1306 return DSERR_OUTOFMEMORY;
1309 CopyMemory(dsb->pwfx, pdsb->pwfx, size);
1311 InitializeCriticalSection(&(dsb->lock));
1312 dsb->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectSoundBufferImpl.lock");
1314 /* register buffer */
1315 hres = DirectSoundDevice_AddBuffer(device, dsb);
1316 if (hres != DS_OK) {
1317 dsb->lock.DebugInfo->Spare[0] = 0;
1318 DeleteCriticalSection(&(dsb->lock));
1319 HeapFree(GetProcessHeap(),0,dsb->buffer);
1320 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1321 HeapFree(GetProcessHeap(),0,dsb);
1329 /*******************************************************************************
1333 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1334 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1336 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1337 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1339 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1342 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1344 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1345 ULONG ref = InterlockedIncrement(&(This->ref));
1346 TRACE("(%p) ref was %d\n", This, ref - 1);
1350 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1352 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1354 TRACE("(%p)\n", This);
1355 ref = InterlockedDecrement(&(This->ref));
1356 TRACE("ref was %d\n", ref + 1);
1359 This->dsb->secondary = NULL;
1360 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1361 HeapFree(GetProcessHeap(), 0, This);
1362 TRACE("(%p) released\n", This);
1367 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1368 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1370 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1371 TRACE("(%p)->(%p)\n",This,caps);
1373 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1376 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1377 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1379 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1380 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1382 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1385 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1386 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1388 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1389 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1391 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1394 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1395 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1397 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1398 TRACE("(%p,%p)\n",This,vol);
1400 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1403 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1404 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1406 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1407 TRACE("(%p,%p)\n",This,pan);
1409 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1412 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1413 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1415 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1416 TRACE("(%p,%p)\n",This,freq);
1418 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1421 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1422 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1424 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1425 TRACE("(%p,%p)\n",This,status);
1427 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1430 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1431 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1433 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1434 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1436 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1439 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1440 LPDIRECTSOUNDBUFFER8 iface,
1443 LPVOID *lplpaudioptr1,
1444 LPDWORD audiobytes1,
1445 LPVOID *lplpaudioptr2,
1446 LPDWORD audiobytes2,
1449 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1450 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1451 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1453 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1454 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1457 static HRESULT WINAPI SecondaryBufferImpl_Play(
1458 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1460 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1461 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1463 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1466 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1467 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1469 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1470 TRACE("(%p,%d)\n",This,newpos);
1472 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1475 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1476 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1478 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1479 TRACE("(%p,%p)\n",This,wfex);
1481 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1484 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1485 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1487 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1488 TRACE("(%p,%d)\n",This,vol);
1490 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1493 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1494 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1496 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1497 TRACE("(%p,%d)\n",This,pan);
1499 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1502 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1503 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1505 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1506 TRACE("(%p,%d)\n",This,freq);
1508 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1511 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1513 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1514 TRACE("(%p)\n",This);
1516 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1519 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1520 LPDIRECTSOUNDBUFFER8 iface,
1521 LPVOID lpvAudioPtr1,
1522 DWORD dwAudioBytes1,
1523 LPVOID lpvAudioPtr2,
1524 DWORD dwAudioBytes2)
1526 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1527 TRACE("(%p,%p,%d,%p,%d)\n",
1528 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1530 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1531 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1534 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1535 LPDIRECTSOUNDBUFFER8 iface)
1537 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1538 TRACE("(%p)\n",This);
1540 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1543 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1544 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1546 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1547 TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1549 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1552 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1553 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1555 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1556 TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1558 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1561 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1562 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1564 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1565 TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1567 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1570 static const IDirectSoundBuffer8Vtbl sbvt =
1572 SecondaryBufferImpl_QueryInterface,
1573 SecondaryBufferImpl_AddRef,
1574 SecondaryBufferImpl_Release,
1575 SecondaryBufferImpl_GetCaps,
1576 SecondaryBufferImpl_GetCurrentPosition,
1577 SecondaryBufferImpl_GetFormat,
1578 SecondaryBufferImpl_GetVolume,
1579 SecondaryBufferImpl_GetPan,
1580 SecondaryBufferImpl_GetFrequency,
1581 SecondaryBufferImpl_GetStatus,
1582 SecondaryBufferImpl_Initialize,
1583 SecondaryBufferImpl_Lock,
1584 SecondaryBufferImpl_Play,
1585 SecondaryBufferImpl_SetCurrentPosition,
1586 SecondaryBufferImpl_SetFormat,
1587 SecondaryBufferImpl_SetVolume,
1588 SecondaryBufferImpl_SetPan,
1589 SecondaryBufferImpl_SetFrequency,
1590 SecondaryBufferImpl_Stop,
1591 SecondaryBufferImpl_Unlock,
1592 SecondaryBufferImpl_Restore,
1593 SecondaryBufferImpl_SetFX,
1594 SecondaryBufferImpl_AcquireResources,
1595 SecondaryBufferImpl_GetObjectInPath
1598 HRESULT SecondaryBufferImpl_Create(
1599 IDirectSoundBufferImpl *dsb,
1600 SecondaryBufferImpl **psb)
1602 SecondaryBufferImpl *sb;
1603 TRACE("(%p,%p)\n",dsb,psb);
1605 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1608 WARN("out of memory\n");
1610 return DSERR_OUTOFMEMORY;
1616 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1621 static HRESULT SecondaryBufferImpl_Destroy(
1622 SecondaryBufferImpl *pdsb)
1624 TRACE("(%p)\n",pdsb);
1626 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);