3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
35 #define NONAMELESSSTRUCT
36 #define NONAMELESSUNION
46 #include "wine/windef16.h"
47 #include "wine/debug.h"
50 #include "dsound_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
54 /*******************************************************************************
57 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
58 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
60 ICOM_THIS(IDirectSoundNotifyImpl,iface);
61 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
63 if (This->dsb == NULL) {
64 WARN("invalid parameter\n");
68 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
71 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
72 ICOM_THIS(IDirectSoundNotifyImpl,iface);
75 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
77 ref = InterlockedIncrement(&(This->ref));
81 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
82 ICOM_THIS(IDirectSoundNotifyImpl,iface);
85 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
87 ref = InterlockedDecrement(&(This->ref));
89 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
90 This->dsb->notify = NULL;
91 HeapFree(GetProcessHeap(),0,This);
92 TRACE("(%p) released\n",This);
97 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
98 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
100 ICOM_THIS(IDirectSoundNotifyImpl,iface);
101 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
103 if (howmuch > 0 && notify == NULL) {
104 WARN("invalid parameter: notify == NULL\n");
105 return DSERR_INVALIDPARAM;
108 if (TRACE_ON(dsound)) {
110 for (i=0;i<howmuch;i++)
111 TRACE("notify at %ld to 0x%08lx\n",
112 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
115 if (This->dsb->hwnotify) {
117 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
119 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
121 } else if (howmuch > 0) {
122 /* Make an internal copy of the caller-supplied array.
123 * Replace the existing copy if one is already present. */
124 if (This->dsb->notifies)
125 This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
126 This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
128 This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
129 howmuch * sizeof(DSBPOSITIONNOTIFY));
131 if (This->dsb->notifies == NULL) {
132 WARN("out of memory\n");
133 return DSERR_OUTOFMEMORY;
135 memcpy(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
136 This->dsb->nrofnotifies = howmuch;
138 if (This->dsb->notifies) {
139 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
140 This->dsb->notifies = NULL;
142 This->dsb->nrofnotifies = 0;
148 IDirectSoundNotifyVtbl dsnvt =
150 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
151 IDirectSoundNotifyImpl_QueryInterface,
152 IDirectSoundNotifyImpl_AddRef,
153 IDirectSoundNotifyImpl_Release,
154 IDirectSoundNotifyImpl_SetNotificationPositions,
157 HRESULT WINAPI IDirectSoundNotifyImpl_Create(
158 IDirectSoundBufferImpl * dsb,
159 IDirectSoundNotifyImpl **pdsn)
161 IDirectSoundNotifyImpl * dsn;
162 TRACE("(%p,%p)\n",dsb,pdsn);
164 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
167 WARN("out of memory\n");
168 return DSERR_OUTOFMEMORY;
172 dsn->lpVtbl = &dsnvt;
175 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
181 HRESULT WINAPI IDirectSoundNotifyImpl_Destroy(
182 IDirectSoundNotifyImpl *pdsn)
184 TRACE("(%p)\n",pdsn);
186 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
191 /*******************************************************************************
195 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
196 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
198 ICOM_THIS(IDirectSoundBufferImpl,iface);
200 TRACE("(%p,%p)\n",This,wfex);
201 /* This method is not available on secondary buffers */
202 WARN("invalid call\n");
203 return DSERR_INVALIDCALL;
206 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
207 LPDIRECTSOUNDBUFFER8 iface,LONG vol
209 ICOM_THIS(IDirectSoundBufferImpl,iface);
212 TRACE("(%p,%ld)\n",This,vol);
214 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
215 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This->dsbd.dwFlags);
216 return DSERR_CONTROLUNAVAIL;
219 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
220 WARN("invalid parameter: vol = %ld\n", vol);
221 return DSERR_INVALIDPARAM;
225 EnterCriticalSection(&(This->lock));
227 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
228 oldVol = This->ds3db_lVolume;
229 This->ds3db_lVolume = vol;
231 oldVol = This->volpan.lVolume;
232 This->volpan.lVolume = vol;
234 DSOUND_RecalcVolPan(&(This->volpan));
240 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
242 WARN("IDsDriverBuffer_SetVolumePan failed\n");
244 DSOUND_ForceRemix(This);
247 LeaveCriticalSection(&(This->lock));
253 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
254 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
256 ICOM_THIS(IDirectSoundBufferImpl,iface);
257 TRACE("(%p,%p)\n",This,vol);
259 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
260 WARN("control unavailable\n");
261 return DSERR_CONTROLUNAVAIL;
265 WARN("invalid parameter: vol == NULL\n");
266 return DSERR_INVALIDPARAM;
269 *vol = This->volpan.lVolume;
274 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
275 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
277 ICOM_THIS(IDirectSoundBufferImpl,iface);
280 TRACE("(%p,%ld)\n",This,freq);
282 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
283 WARN("control unavailable\n");
284 return DSERR_CONTROLUNAVAIL;
287 if (freq == DSBFREQUENCY_ORIGINAL)
288 freq = This->pwfx->nSamplesPerSec;
290 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
291 WARN("invalid parameter: freq = %ld\n", freq);
292 return DSERR_INVALIDPARAM;
296 EnterCriticalSection(&(This->lock));
298 oldFreq = This->freq;
300 if (freq != oldFreq) {
301 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->dsound->pwfx->nSamplesPerSec;
302 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
303 DSOUND_RecalcFormat(This);
305 DSOUND_ForceRemix(This);
308 LeaveCriticalSection(&(This->lock));
314 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
315 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
317 HRESULT hres = DS_OK;
318 ICOM_THIS(IDirectSoundBufferImpl,iface);
319 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
322 EnterCriticalSection(&(This->lock));
324 This->playflags = flags;
325 if (This->state == STATE_STOPPED) {
327 This->startpos = This->buf_mixpos;
328 This->state = STATE_STARTING;
329 } else if (This->state == STATE_STOPPING)
330 This->state = STATE_PLAYING;
332 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
334 WARN("IDsDriverBuffer_Play failed\n");
336 This->state = STATE_PLAYING;
339 LeaveCriticalSection(&(This->lock));
345 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
347 HRESULT hres = DS_OK;
348 ICOM_THIS(IDirectSoundBufferImpl,iface);
349 TRACE("(%p)\n",This);
352 EnterCriticalSection(&(This->lock));
354 if (This->state == STATE_PLAYING)
355 This->state = STATE_STOPPING;
356 else if (This->state == STATE_STARTING)
357 This->state = STATE_STOPPED;
359 hres = IDsDriverBuffer_Stop(This->hwbuf);
361 WARN("IDsDriverBuffer_Stop failed\n");
363 This->state = STATE_STOPPED;
365 DSOUND_CheckEvent(This, 0);
367 LeaveCriticalSection(&(This->lock));
373 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface) {
374 ICOM_THIS(IDirectSoundBufferImpl,iface);
377 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
379 ref = InterlockedIncrement(&(This->ref));
381 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
386 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface) {
387 ICOM_THIS(IDirectSoundBufferImpl,iface);
391 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
393 ref = InterlockedDecrement(&(This->ref));
396 RtlAcquireResourceExclusive(&(This->dsound->lock), TRUE);
397 for (i=0;i<This->dsound->nrofbuffers;i++)
398 if (This->dsound->buffers[i] == This)
400 if (i < This->dsound->nrofbuffers) {
401 /* Put the last buffer of the list in the (now empty) position */
402 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
403 This->dsound->nrofbuffers--;
404 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER8)*This->dsound->nrofbuffers);
405 TRACE("(%p) buffer count is now %d\n", This, This->dsound->nrofbuffers);
407 if (This->dsound->nrofbuffers == 0) {
408 HeapFree(GetProcessHeap(),0,This->dsound->buffers);
409 This->dsound->buffers = NULL;
411 RtlReleaseResource(&(This->dsound->lock));
413 DeleteCriticalSection(&(This->lock));
416 IDsDriverBuffer_Release(This->hwbuf);
417 if (This->dsound->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
419 if (This->buffer->ref==0) {
420 HeapFree(GetProcessHeap(),0,This->buffer->memory);
421 HeapFree(GetProcessHeap(),0,This->buffer);
426 if (This->buffer->ref==0) {
427 HeapFree(GetProcessHeap(),0,This->buffer->memory);
428 HeapFree(GetProcessHeap(),0,This->buffer);
432 if (This->notifies != NULL)
433 HeapFree(GetProcessHeap(), 0, This->notifies);
436 HeapFree(GetProcessHeap(), 0, This->pwfx);
438 HeapFree(GetProcessHeap(),0,This);
440 TRACE("(%p) released\n",This);
444 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This,
445 DWORD state, DWORD pplay, DWORD pwrite, DWORD pmix, DWORD bmix)
449 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, pmix);
450 TRACE("this mixpos=%ld, time=%ld\n", bmix, GetTickCount());
452 /* the actual primary play position (pplay) is always behind last mixed (pmix),
453 * unless the computer is too slow or something */
454 /* we need to know how far away we are from there */
455 #if 0 /* we'll never fill the primary entirely */
457 if ((state == STATE_PLAYING) || (state == STATE_STOPPING)) {
458 /* wow, the software mixer is really doing well,
459 * seems the entire primary buffer is filled! */
460 pmix += This->dsound->buflen;
462 /* else: the primary buffer is not playing, so probably empty */
465 if (pmix < pplay) pmix += This->dsound->buflen; /* wraparound */
467 /* detect buffer underrun */
468 if (pwrite < pplay) pwrite += This->dsound->buflen; /* wraparound */
470 if (pmix > (ds_snd_queue_max * This->dsound->fraglen + pwrite + This->dsound->writelead)) {
471 WARN("detected an underrun: primary queue was %ld\n",pmix);
474 /* divide the offset by its sample size */
475 pmix /= This->dsound->pwfx->nBlockAlign;
476 TRACE("primary back-samples=%ld\n",pmix);
477 /* adjust for our frequency */
478 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
479 /* multiply by our own sample size */
480 pmix *= This->pwfx->nBlockAlign;
481 TRACE("this back-offset=%ld\n", pmix);
482 /* subtract from our last mixed position */
484 while (bplay < pmix) bplay += This->buflen; /* wraparound */
486 if (This->leadin && ((bplay < This->startpos) || (bplay > bmix))) {
487 /* seems we haven't started playing yet */
488 TRACE("this still in lead-in phase\n");
489 bplay = This->startpos;
491 /* return the result */
495 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
496 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
499 ICOM_THIS(IDirectSoundBufferImpl,iface);
500 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
502 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
504 WARN("IDsDriverBuffer_GetPosition failed\n");
508 if (playpos && (This->state != STATE_PLAYING)) {
509 /* we haven't been merged into the primary buffer (yet) */
510 *playpos = This->buf_mixpos;
511 } else if (playpos) {
512 DWORD pplay, pwrite, lplay, splay, pstate;
513 /* let's get this exact; first, recursively call GetPosition on the primary */
514 EnterCriticalSection(&(This->dsound->mixlock));
515 if (DSOUND_PrimaryGetPosition(This->dsound, &pplay, &pwrite) != DS_OK)
516 WARN("DSOUND_PrimaryGetPosition failed\n");
517 /* detect HEL mode underrun */
518 pstate = This->dsound->state;
519 if (!(This->dsound->hwbuf || This->dsound->pwqueue)) {
520 TRACE("detected an underrun\n");
522 if (pstate == STATE_PLAYING)
523 pstate = STATE_STARTING;
524 else if (pstate == STATE_STOPPING)
525 pstate = STATE_STOPPED;
527 /* get data for ourselves while we still have the lock */
528 pstate &= This->state;
529 lplay = This->primary_mixpos;
530 splay = This->buf_mixpos;
531 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->dsound->hwbuf) {
532 /* calculate play position using this */
533 *playpos = DSOUND_CalcPlayPosition(This, pstate, pplay, pwrite, lplay, splay);
535 /* (unless the app isn't using GETCURRENTPOSITION2) */
536 /* don't know exactly how this should be handled...
537 * the docs says that play cursor is reported as directly
538 * behind write cursor, hmm... */
539 /* let's just do what might work for Half-Life */
541 wp = (This->dsound->pwplay + ds_hel_margin) * This->dsound->fraglen;
542 wp %= This->dsound->buflen;
543 *playpos = DSOUND_CalcPlayPosition(This, pstate, wp, pwrite, lplay, splay);
545 LeaveCriticalSection(&(This->dsound->mixlock));
548 *writepos = This->buf_mixpos;
551 if (This->state != STATE_STOPPED) {
552 /* apply the documented 10ms lead to writepos */
553 *writepos += This->writelead;
555 *writepos %= This->buflen;
558 This->last_playpos = *playpos;
559 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
563 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
564 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
566 ICOM_THIS(IDirectSoundBufferImpl,iface);
567 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
569 if (status == NULL) {
570 WARN("invalid parameter: status = NULL\n");
571 return DSERR_INVALIDPARAM;
575 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
576 *status |= DSBSTATUS_PLAYING;
577 if (This->playflags & DSBPLAY_LOOPING)
578 *status |= DSBSTATUS_LOOPING;
581 TRACE("status=%lx\n", *status);
586 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
587 LPDIRECTSOUNDBUFFER8 iface,
593 ICOM_THIS(IDirectSoundBufferImpl,iface);
594 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
596 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
598 if (lpwf) { /* NULL is valid */
599 if (wfsize >= size) {
600 memcpy(lpwf,This->pwfx,size);
604 WARN("invalid parameter: wfsize to small\n");
607 return DSERR_INVALIDPARAM;
611 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
613 WARN("invalid parameter: wfwritten == NULL\n");
614 return DSERR_INVALIDPARAM;
621 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
622 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
624 HRESULT hres = DS_OK;
625 ICOM_THIS(IDirectSoundBufferImpl,iface);
627 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
639 if (flags & DSBLOCK_FROMWRITECURSOR) {
641 /* GetCurrentPosition does too much magic to duplicate here */
642 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
644 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
647 writecursor += writepos;
649 writecursor %= This->buflen;
650 if (flags & DSBLOCK_ENTIREBUFFER)
651 writebytes = This->buflen;
652 if (writebytes > This->buflen)
653 writebytes = This->buflen;
655 EnterCriticalSection(&(This->lock));
657 if ((writebytes == This->buflen) &&
658 ((This->state == STATE_STARTING) ||
659 (This->state == STATE_PLAYING)))
660 /* some games, like Half-Life, try to be clever (not) and
661 * keep one secondary buffer, and mix sounds into it itself,
662 * locking the entire buffer every time... so we can just forget
663 * about tracking the last-written-to-position... */
664 This->probably_valid_to = (DWORD)-1;
666 This->probably_valid_to = writecursor;
668 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
669 hres = IDsDriverBuffer_Lock(This->hwbuf,
670 lplpaudioptr1, audiobytes1,
671 lplpaudioptr2, audiobytes2,
672 writecursor, writebytes,
675 WARN("IDsDriverBuffer_Lock failed\n");
676 LeaveCriticalSection(&(This->lock));
681 if (writecursor+writebytes <= This->buflen) {
682 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
683 *audiobytes1 = writebytes;
685 *(LPBYTE*)lplpaudioptr2 = NULL;
688 TRACE("->%ld.0\n",writebytes);
690 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
691 *audiobytes1 = This->buflen-writecursor;
693 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
695 *audiobytes2 = writebytes-(This->buflen-writecursor);
696 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
698 if (This->state == STATE_PLAYING) {
699 /* if the segment between playpos and buf_mixpos is touched,
700 * we need to cancel some mixing */
701 /* we'll assume that the app always calls GetCurrentPosition before
702 * locking a playing buffer, so that last_playpos is up-to-date */
703 if (This->buf_mixpos >= This->last_playpos) {
704 if (This->buf_mixpos > writecursor &&
705 This->last_playpos < writecursor+writebytes)
708 if (This->buf_mixpos > writecursor ||
709 This->last_playpos < writecursor+writebytes)
713 TRACE("locking prebuffered region, ouch\n");
714 DSOUND_MixCancelAt(This, writecursor);
719 LeaveCriticalSection(&(This->lock));
723 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
724 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
726 HRESULT hres = DS_OK;
727 ICOM_THIS(IDirectSoundBufferImpl,iface);
728 TRACE("(%p,%ld)\n",This,newpos);
731 EnterCriticalSection(&(This->lock));
733 newpos %= This->buflen;
734 This->buf_mixpos = newpos;
736 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
738 WARN("IDsDriverBuffer_SetPosition failed\n");
741 LeaveCriticalSection(&(This->lock));
747 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
748 LPDIRECTSOUNDBUFFER8 iface,LONG pan
750 HRESULT hres = DS_OK;
751 ICOM_THIS(IDirectSoundBufferImpl,iface);
753 TRACE("(%p,%ld)\n",This,pan);
755 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
756 WARN("invalid parameter: pan = %ld\n", pan);
757 return DSERR_INVALIDPARAM;
760 /* You cannot use both pan and 3D controls */
761 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
762 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
763 WARN("control unavailable\n");
764 return DSERR_CONTROLUNAVAIL;
768 EnterCriticalSection(&(This->lock));
770 if (This->volpan.lPan != pan) {
771 This->volpan.lPan = pan;
772 DSOUND_RecalcVolPan(&(This->volpan));
775 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
777 WARN("IDsDriverBuffer_SetVolumePan failed\n");
779 DSOUND_ForceRemix(This);
782 LeaveCriticalSection(&(This->lock));
788 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
789 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
791 ICOM_THIS(IDirectSoundBufferImpl,iface);
792 TRACE("(%p,%p)\n",This,pan);
794 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
795 WARN("control unavailable\n");
796 return DSERR_CONTROLUNAVAIL;
800 WARN("invalid parameter: pan = NULL\n");
801 return DSERR_INVALIDPARAM;
804 *pan = This->volpan.lPan;
809 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
810 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
812 ICOM_THIS(IDirectSoundBufferImpl,iface);
813 DWORD probably_valid_to;
815 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
818 EnterCriticalSection(&(This->lock));
820 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
822 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
824 LeaveCriticalSection(&(This->lock));
825 WARN("IDsDriverBuffer_Unlock failed\n");
830 if (p2) probably_valid_to = (((LPBYTE)p2)-This->buffer->memory) + x2;
831 else probably_valid_to = (((LPBYTE)p1)-This->buffer->memory) + x1;
832 probably_valid_to %= This->buflen;
833 if ((probably_valid_to == 0) && ((x1+x2) == This->buflen) &&
834 ((This->state == STATE_STARTING) ||
835 (This->state == STATE_PLAYING)))
836 /* see IDirectSoundBufferImpl_Lock */
837 probably_valid_to = (DWORD)-1;
838 This->probably_valid_to = probably_valid_to;
840 LeaveCriticalSection(&(This->lock));
843 TRACE("probably_valid_to=%ld\n", This->probably_valid_to);
847 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
848 LPDIRECTSOUNDBUFFER8 iface
850 ICOM_THIS(IDirectSoundBufferImpl,iface);
851 FIXME("(%p):stub\n",This);
855 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
856 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
858 ICOM_THIS(IDirectSoundBufferImpl,iface);
859 TRACE("(%p,%p)\n",This,freq);
862 WARN("invalid parameter: freq = NULL\n");
863 return DSERR_INVALIDPARAM;
867 TRACE("-> %ld\n", *freq);
872 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
873 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
875 ICOM_THIS(IDirectSoundBufferImpl,iface);
878 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
881 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
883 WARN("control unavailable\n");
884 return DSERR_CONTROLUNAVAIL;
887 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
888 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
890 ICOM_THIS(IDirectSoundBufferImpl,iface);
893 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
896 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
898 WARN("control unavailable\n");
899 return DSERR_CONTROLUNAVAIL;
902 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
903 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
905 ICOM_THIS(IDirectSoundBufferImpl,iface);
907 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
909 WARN("control unavailable\n");
910 return DSERR_CONTROLUNAVAIL;
913 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
914 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
916 ICOM_THIS(IDirectSoundBufferImpl,iface);
917 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
918 DPRINTF("Re-Init!!!\n");
919 WARN("already initialized\n");
920 return DSERR_ALREADYINITIALIZED;
923 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
924 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
926 ICOM_THIS(IDirectSoundBufferImpl,iface);
927 TRACE("(%p)->(%p)\n",This,caps);
930 WARN("invalid parameter: caps == NULL\n");
931 return DSERR_INVALIDPARAM;
934 if (caps->dwSize < sizeof(*caps)) {
935 WARN("invalid parameter: caps->dwSize = %ld < %d\n",caps->dwSize, sizeof(*caps));
936 return DSERR_INVALIDPARAM;
939 caps->dwFlags = This->dsbd.dwFlags;
940 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
941 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
943 caps->dwBufferBytes = This->buflen;
945 /* This value represents the speed of the "unlock" command.
946 As unlock is quite fast (it does not do anything), I put
947 4096 ko/s = 4 Mo / s */
948 /* FIXME: hwbuf speed */
949 caps->dwUnlockTransferRate = 4096;
950 caps->dwPlayCpuOverhead = 0;
955 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
956 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
958 ICOM_THIS(IDirectSoundBufferImpl,iface);
960 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
963 WARN("invalid parameter\n");
967 *ppobj = NULL; /* assume failure */
969 if ( IsEqualGUID(riid, &IID_IUnknown) ||
970 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
971 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
973 SecondaryBufferImpl_Create(This, &(This->dsb));
975 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->dsb);
979 WARN("IID_IDirectSoundBuffer\n");
980 return E_NOINTERFACE;
983 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ||
984 IsEqualGUID( &IID_IDirectSoundNotify8, riid ) ) {
986 IDirectSoundNotifyImpl_Create(This, &(This->notify));
988 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
989 *ppobj = This->notify;
992 WARN("IID_IDirectSoundNotify\n");
993 return E_NOINTERFACE;
996 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
998 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
1000 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
1001 *ppobj = This->ds3db;
1004 WARN("IID_IDirectSound3DBuffer\n");
1005 return E_NOINTERFACE;
1008 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1009 ERR("app requested IDirectSound3DListener on secondary buffer\n");
1010 return E_NOINTERFACE;
1013 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1014 /* only supported on hardware 3D secondary buffers */
1015 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) &&
1016 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) &&
1017 (This->hwbuf != NULL) ) {
1019 IKsBufferPropertySetImpl_Create(This, &(This->iks));
1021 IKsPropertySet_AddRef((LPKSPROPERTYSET)*ppobj);
1026 WARN("IID_IKsPropertySet\n");
1027 return E_NOINTERFACE;
1030 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1032 return E_NOINTERFACE;
1035 static IDirectSoundBuffer8Vtbl dsbvt =
1037 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1038 IDirectSoundBufferImpl_QueryInterface,
1039 IDirectSoundBufferImpl_AddRef,
1040 IDirectSoundBufferImpl_Release,
1041 IDirectSoundBufferImpl_GetCaps,
1042 IDirectSoundBufferImpl_GetCurrentPosition,
1043 IDirectSoundBufferImpl_GetFormat,
1044 IDirectSoundBufferImpl_GetVolume,
1045 IDirectSoundBufferImpl_GetPan,
1046 IDirectSoundBufferImpl_GetFrequency,
1047 IDirectSoundBufferImpl_GetStatus,
1048 IDirectSoundBufferImpl_Initialize,
1049 IDirectSoundBufferImpl_Lock,
1050 IDirectSoundBufferImpl_Play,
1051 IDirectSoundBufferImpl_SetCurrentPosition,
1052 IDirectSoundBufferImpl_SetFormat,
1053 IDirectSoundBufferImpl_SetVolume,
1054 IDirectSoundBufferImpl_SetPan,
1055 IDirectSoundBufferImpl_SetFrequency,
1056 IDirectSoundBufferImpl_Stop,
1057 IDirectSoundBufferImpl_Unlock,
1058 IDirectSoundBufferImpl_Restore,
1059 IDirectSoundBufferImpl_SetFX,
1060 IDirectSoundBufferImpl_AcquireResources,
1061 IDirectSoundBufferImpl_GetObjectInPath
1064 HRESULT WINAPI IDirectSoundBufferImpl_Create(
1065 IDirectSoundImpl *ds,
1066 IDirectSoundBufferImpl **pdsb,
1067 LPCDSBUFFERDESC dsbd)
1069 IDirectSoundBufferImpl *dsb;
1070 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1071 HRESULT err = DS_OK;
1073 int use_hw, alloc_size, cp_size;
1074 TRACE("(%p,%p,%p)\n",ds,pdsb,dsbd);
1076 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1077 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
1079 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1082 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1085 WARN("out of memory\n");
1087 return DSERR_OUTOFMEMORY;
1090 TRACE("Created buffer at %p\n", dsb);
1095 dsb->lpVtbl = &dsbvt;
1098 /* size depends on version */
1099 memcpy(&dsb->dsbd, dsbd, dsbd->dwSize);
1101 /* variable sized struct so calculate size based on format */
1102 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1103 alloc_size = sizeof(WAVEFORMATEX);
1104 cp_size = sizeof(PCMWAVEFORMAT);
1106 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
1108 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
1109 if (dsb->pwfx == NULL) {
1110 WARN("out of memory\n");
1111 HeapFree(GetProcessHeap(),0,dsb);
1113 return DSERR_OUTOFMEMORY;
1116 memcpy(dsb->pwfx, wfex, cp_size);
1118 dsb->buflen = dsbd->dwBufferBytes;
1119 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1122 dsb->notifies = NULL;
1123 dsb->nrofnotifies = 0;
1126 /* Check necessary hardware mixing capabilities */
1127 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1128 else capf |= DSCAPS_SECONDARYMONO;
1129 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1130 else capf |= DSCAPS_SECONDARY8BIT;
1132 use_hw = (ds->drvcaps.dwFlags & capf) == capf;
1133 TRACE("use_hw = 0x%08x, capf = 0x%08lx, ds->drvcaps.dwFlags = 0x%08lx\n", use_hw, capf, ds->drvcaps.dwFlags);
1135 /* FIXME: check hardware sample rate mixing capabilities */
1136 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1137 /* FIXME: check whether any hardware buffers are left */
1138 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1140 /* Allocate system memory if applicable */
1141 if ((ds->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1142 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1143 if (dsb->buffer == NULL) {
1144 WARN("out of memory\n");
1145 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1146 HeapFree(GetProcessHeap(),0,dsb);
1148 return DSERR_OUTOFMEMORY;
1151 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1152 if (dsb->buffer->memory == NULL) {
1153 WARN("out of memory\n");
1154 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1155 HeapFree(GetProcessHeap(),0,dsb->buffer);
1156 HeapFree(GetProcessHeap(),0,dsb);
1158 return DSERR_OUTOFMEMORY;
1160 dsb->buffer->ref = 1;
1163 /* Allocate the hardware buffer */
1165 err = IDsDriver_CreateSoundBuffer(ds->driver,wfex,dsbd->dwFlags,0,
1166 &(dsb->buflen),&(dsb->buffer->memory),
1167 (LPVOID*)&(dsb->hwbuf));
1168 /* fall back to software buffer on failure */
1170 TRACE("IDsDriver_CreateSoundBuffer failed, falling back to software buffer\n");
1172 if (ds->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1173 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1174 if (dsb->buffer == NULL) {
1175 WARN("out of memory\n");
1176 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1177 HeapFree(GetProcessHeap(),0,dsb);
1179 return DSERR_OUTOFMEMORY;
1182 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1183 if (dsb->buffer->memory == NULL) {
1184 WARN("out of memory\n");
1185 HeapFree(GetProcessHeap(),0,dsb->buffer);
1186 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1187 HeapFree(GetProcessHeap(),0,dsb);
1189 return DSERR_OUTOFMEMORY;
1191 dsb->buffer->ref = 1;
1196 /* calculate fragment size and write lead */
1197 DSOUND_RecalcFormat(dsb);
1199 /* It's not necessary to initialize values to zero since */
1200 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1202 dsb->buf_mixpos = 0;
1203 dsb->state = STATE_STOPPED;
1205 dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
1206 ds->pwfx->nSamplesPerSec;
1207 dsb->nAvgBytesPerSec = dsb->freq *
1208 dsbd->lpwfxFormat->nBlockAlign;
1210 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1211 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1212 dsb->ds3db_ds3db.vPosition.x = 0.0;
1213 dsb->ds3db_ds3db.vPosition.y = 0.0;
1214 dsb->ds3db_ds3db.vPosition.z = 0.0;
1215 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1216 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1217 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1218 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1219 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1220 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1221 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1222 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1223 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1224 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1225 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1226 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1228 dsb->ds3db_need_recalc = FALSE;
1229 DSOUND_Calc3DBuffer(dsb);
1231 DSOUND_RecalcVolPan(&(dsb->volpan));
1233 InitializeCriticalSection(&(dsb->lock));
1234 dsb->lock.DebugInfo->Spare[1] = (DWORD)"DSOUNDBUFFER_lock";
1236 /* register buffer */
1237 RtlAcquireResourceExclusive(&(ds->lock), TRUE);
1238 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1239 IDirectSoundBufferImpl **newbuffers;
1241 newbuffers = HeapReAlloc(GetProcessHeap(),0,ds->buffers,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1));
1243 newbuffers = HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1));
1246 ds->buffers = newbuffers;
1247 ds->buffers[ds->nrofbuffers] = dsb;
1249 TRACE("buffer count is now %d\n", ds->nrofbuffers);
1251 ERR("out of memory for buffer list! Current buffer count is %d\n", ds->nrofbuffers);
1252 if (dsb->buffer->memory)
1253 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1255 HeapFree(GetProcessHeap(),0,dsb->buffer);
1256 DeleteCriticalSection(&(dsb->lock));
1257 RtlReleaseResource(&(ds->lock));
1258 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1259 HeapFree(GetProcessHeap(),0,dsb);
1261 return DSERR_OUTOFMEMORY;
1264 RtlReleaseResource(&(ds->lock));
1269 HRESULT WINAPI IDirectSoundBufferImpl_Destroy(
1270 IDirectSoundBufferImpl *pdsb)
1272 TRACE("(%p)\n",pdsb);
1274 /* This keeps the *_Destroy functions from possibly deleting
1275 * this object until it is ready to be deleted */
1276 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1279 WARN("iks not NULL\n");
1280 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1285 WARN("ds3db not NULL\n");
1286 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1291 WARN("notify not NULL\n");
1292 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1293 pdsb->notify = NULL;
1297 WARN("dsb not NULL\n");
1298 SecondaryBufferImpl_Destroy(pdsb->dsb);
1302 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1307 /*******************************************************************************
1311 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1312 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1314 ICOM_THIS(SecondaryBufferImpl,iface);
1315 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1317 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1320 static DWORD WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1322 ICOM_THIS(IDirectSoundBufferImpl,iface);
1324 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1326 ref = InterlockedIncrement(&(This->ref));
1328 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
1333 static DWORD WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1335 ICOM_THIS(IDirectSoundBufferImpl,iface);
1337 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1339 ref = InterlockedDecrement(&(This->ref));
1341 This->dsb->dsb = NULL;
1342 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1343 HeapFree(GetProcessHeap(),0,This);
1344 TRACE("(%p) released\n",This);
1349 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1350 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1352 ICOM_THIS(SecondaryBufferImpl,iface);
1353 TRACE("(%p)->(%p)\n",This,caps);
1355 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1358 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1359 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1361 ICOM_THIS(SecondaryBufferImpl,iface);
1362 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1364 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1367 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1368 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1370 ICOM_THIS(SecondaryBufferImpl,iface);
1371 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1373 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1376 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1377 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1379 ICOM_THIS(SecondaryBufferImpl,iface);
1380 TRACE("(%p,%p)\n",This,vol);
1382 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1385 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1386 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1388 ICOM_THIS(SecondaryBufferImpl,iface);
1389 TRACE("(%p,%p)\n",This,pan);
1391 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1394 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1395 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1397 ICOM_THIS(SecondaryBufferImpl,iface);
1398 TRACE("(%p,%p)\n",This,freq);
1400 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1403 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1404 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1406 ICOM_THIS(SecondaryBufferImpl,iface);
1407 TRACE("(%p,%p)\n",This,status);
1409 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1412 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1413 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1415 ICOM_THIS(SecondaryBufferImpl,iface);
1416 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1418 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1421 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1422 LPDIRECTSOUNDBUFFER8 iface,
1425 LPVOID lplpaudioptr1,
1426 LPDWORD audiobytes1,
1427 LPVOID lplpaudioptr2,
1428 LPDWORD audiobytes2,
1431 ICOM_THIS(SecondaryBufferImpl,iface);
1432 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1433 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1435 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1436 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1439 static HRESULT WINAPI SecondaryBufferImpl_Play(
1440 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1442 ICOM_THIS(SecondaryBufferImpl,iface);
1443 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
1445 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1448 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1449 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1451 ICOM_THIS(SecondaryBufferImpl,iface);
1452 TRACE("(%p,%ld)\n",This,newpos);
1454 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1457 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1458 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1460 ICOM_THIS(SecondaryBufferImpl,iface);
1461 TRACE("(%p,%p)\n",This,wfex);
1463 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1466 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1467 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1469 ICOM_THIS(SecondaryBufferImpl,iface);
1470 TRACE("(%p,%ld)\n",This,vol);
1472 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1475 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1476 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1478 ICOM_THIS(SecondaryBufferImpl,iface);
1479 TRACE("(%p,%ld)\n",This,pan);
1481 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1484 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1485 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1487 ICOM_THIS(SecondaryBufferImpl,iface);
1488 TRACE("(%p,%ld)\n",This,freq);
1490 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1493 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1495 ICOM_THIS(SecondaryBufferImpl,iface);
1496 TRACE("(%p)\n",This);
1498 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1501 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1502 LPDIRECTSOUNDBUFFER8 iface,
1503 LPVOID lpvAudioPtr1,
1504 DWORD dwAudioBytes1,
1505 LPVOID lpvAudioPtr2,
1506 DWORD dwAudioBytes2)
1508 ICOM_THIS(SecondaryBufferImpl,iface);
1509 TRACE("(%p,%p,%ld,%p,%ld)\n",
1510 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1512 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1513 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1516 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1517 LPDIRECTSOUNDBUFFER8 iface)
1519 ICOM_THIS(SecondaryBufferImpl,iface);
1520 TRACE("(%p)\n",This);
1522 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1525 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1526 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1528 ICOM_THIS(SecondaryBufferImpl,iface);
1529 TRACE("(%p,%lu,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1531 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1534 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1535 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1537 ICOM_THIS(SecondaryBufferImpl,iface);
1538 TRACE("(%p,%08lu,%lu,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1540 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1543 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1544 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1546 ICOM_THIS(SecondaryBufferImpl,iface);
1547 TRACE("(%p,%s,%lu,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1549 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1552 static IDirectSoundBuffer8Vtbl sbvt =
1554 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1555 SecondaryBufferImpl_QueryInterface,
1556 SecondaryBufferImpl_AddRef,
1557 SecondaryBufferImpl_Release,
1558 SecondaryBufferImpl_GetCaps,
1559 SecondaryBufferImpl_GetCurrentPosition,
1560 SecondaryBufferImpl_GetFormat,
1561 SecondaryBufferImpl_GetVolume,
1562 SecondaryBufferImpl_GetPan,
1563 SecondaryBufferImpl_GetFrequency,
1564 SecondaryBufferImpl_GetStatus,
1565 SecondaryBufferImpl_Initialize,
1566 SecondaryBufferImpl_Lock,
1567 SecondaryBufferImpl_Play,
1568 SecondaryBufferImpl_SetCurrentPosition,
1569 SecondaryBufferImpl_SetFormat,
1570 SecondaryBufferImpl_SetVolume,
1571 SecondaryBufferImpl_SetPan,
1572 SecondaryBufferImpl_SetFrequency,
1573 SecondaryBufferImpl_Stop,
1574 SecondaryBufferImpl_Unlock,
1575 SecondaryBufferImpl_Restore,
1576 SecondaryBufferImpl_SetFX,
1577 SecondaryBufferImpl_AcquireResources,
1578 SecondaryBufferImpl_GetObjectInPath
1581 HRESULT WINAPI SecondaryBufferImpl_Create(
1582 IDirectSoundBufferImpl *dsb,
1583 SecondaryBufferImpl **psb)
1585 SecondaryBufferImpl *sb;
1586 TRACE("(%p,%p)\n",dsb,psb);
1588 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1591 WARN("out of memory\n");
1593 return DSERR_OUTOFMEMORY;
1599 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1604 HRESULT WINAPI SecondaryBufferImpl_Destroy(
1605 SecondaryBufferImpl *pdsb)
1607 TRACE("(%p)\n",pdsb);
1609 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);