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
32 #include "wine/debug.h"
34 #include "dsound_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
39 /*******************************************************************************
43 static inline struct IDirectSoundBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
45 return CONTAINING_RECORD(iface, struct IDirectSoundBufferImpl, IDirectSoundNotify_iface);
48 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
51 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
53 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
55 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
58 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
60 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
61 ULONG ref = InterlockedIncrement(&This->refn);
63 TRACE("(%p) ref was %d\n", This, ref - 1);
66 InterlockedIncrement(&This->numIfaces);
71 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
73 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
74 ULONG ref = InterlockedDecrement(&This->refn);
76 TRACE("(%p) ref was %d\n", This, ref + 1);
78 if (!ref && !InterlockedDecrement(&This->numIfaces))
79 secondarybuffer_destroy(This);
84 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
85 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
87 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
89 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
91 if (howmuch > 0 && notify == NULL) {
92 WARN("invalid parameter: notify == NULL\n");
93 return DSERR_INVALIDPARAM;
96 if (TRACE_ON(dsound)) {
98 for (i=0;i<howmuch;i++)
99 TRACE("notify at %d to %p\n",
100 notify[i].dwOffset,notify[i].hEventNotify);
104 /* Make an internal copy of the caller-supplied array.
105 * Replace the existing copy if one is already present. */
106 HeapFree(GetProcessHeap(), 0, This->notifies);
107 This->notifies = HeapAlloc(GetProcessHeap(), 0,
108 howmuch * sizeof(DSBPOSITIONNOTIFY));
110 if (This->notifies == NULL) {
111 WARN("out of memory\n");
112 return DSERR_OUTOFMEMORY;
114 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
115 This->nrofnotifies = howmuch;
117 HeapFree(GetProcessHeap(), 0, This->notifies);
118 This->notifies = NULL;
119 This->nrofnotifies = 0;
125 static const IDirectSoundNotifyVtbl dsnvt =
127 IDirectSoundNotifyImpl_QueryInterface,
128 IDirectSoundNotifyImpl_AddRef,
129 IDirectSoundNotifyImpl_Release,
130 IDirectSoundNotifyImpl_SetNotificationPositions,
133 /*******************************************************************************
137 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
139 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
142 static inline BOOL is_primary_buffer(IDirectSoundBufferImpl *This)
144 return This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER ? TRUE : FALSE;
147 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
148 LPCWAVEFORMATEX wfex)
150 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
152 TRACE("(%p,%p)\n", iface, wfex);
154 if (is_primary_buffer(This))
155 return primarybuffer_SetFormat(This->device, wfex);
157 WARN("not available for secondary buffers.\n");
158 return DSERR_INVALIDCALL;
162 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
164 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
167 HRESULT hres = DS_OK;
169 TRACE("(%p,%d)\n",This,vol);
171 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
172 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
173 return DSERR_CONTROLUNAVAIL;
176 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
177 WARN("invalid parameter: vol = %d\n", vol);
178 return DSERR_INVALIDPARAM;
182 RtlAcquireResourceExclusive(&This->lock, TRUE);
184 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
185 oldVol = This->ds3db_lVolume;
186 This->ds3db_lVolume = vol;
188 /* recalc 3d volume, which in turn recalcs the pans */
189 DSOUND_Calc3DBuffer(This);
191 oldVol = This->volpan.lVolume;
192 This->volpan.lVolume = vol;
194 DSOUND_RecalcVolPan(&(This->volpan));
197 RtlReleaseResource(&This->lock);
203 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
205 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
207 TRACE("(%p,%p)\n",This,vol);
209 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
210 WARN("control unavailable\n");
211 return DSERR_CONTROLUNAVAIL;
215 WARN("invalid parameter: vol == NULL\n");
216 return DSERR_INVALIDPARAM;
219 *vol = This->volpan.lVolume;
224 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
226 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
229 TRACE("(%p,%d)\n",This,freq);
231 if (is_primary_buffer(This)) {
232 WARN("not available for primary buffers.\n");
233 return DSERR_CONTROLUNAVAIL;
236 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
237 WARN("control unavailable\n");
238 return DSERR_CONTROLUNAVAIL;
241 if (freq == DSBFREQUENCY_ORIGINAL)
242 freq = This->pwfx->nSamplesPerSec;
244 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
245 WARN("invalid parameter: freq = %d\n", freq);
246 return DSERR_INVALIDPARAM;
250 RtlAcquireResourceExclusive(&This->lock, TRUE);
252 oldFreq = This->freq;
254 if (freq != oldFreq) {
255 This->freqAdjust = This->freq / (float)This->device->pwfx->nSamplesPerSec;
256 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
257 DSOUND_RecalcFormat(This);
260 RtlReleaseResource(&This->lock);
266 static HRESULT WINAPI IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
267 DWORD reserved2, DWORD flags)
269 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
270 HRESULT hres = DS_OK;
272 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
275 RtlAcquireResourceExclusive(&This->lock, TRUE);
277 This->playflags = flags;
278 if (This->state == STATE_STOPPED) {
280 This->state = STATE_STARTING;
281 } else if (This->state == STATE_STOPPING)
282 This->state = STATE_PLAYING;
284 RtlReleaseResource(&This->lock);
290 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 *iface)
292 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
293 HRESULT hres = DS_OK;
295 TRACE("(%p)\n",This);
298 RtlAcquireResourceExclusive(&This->lock, TRUE);
300 if (This->state == STATE_PLAYING)
301 This->state = STATE_STOPPING;
302 else if (This->state == STATE_STARTING)
304 This->state = STATE_STOPPED;
305 DSOUND_CheckEvent(This, 0, 0);
308 RtlReleaseResource(&This->lock);
314 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
316 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
317 ULONG ref = InterlockedIncrement(&This->ref);
319 TRACE("(%p) ref was %d\n", This, ref - 1);
322 InterlockedIncrement(&This->numIfaces);
327 static ULONG WINAPI IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 *iface)
329 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
332 if (is_primary_buffer(This)){
333 ref = capped_refcount_dec(&This->ref);
335 capped_refcount_dec(&This->numIfaces);
336 TRACE("(%p) ref is now: %d\n", This, ref);
340 ref = InterlockedDecrement(&This->ref);
341 if (!ref && !InterlockedDecrement(&This->numIfaces))
342 secondarybuffer_destroy(This);
344 TRACE("(%p) ref is now %d\n", This, ref);
349 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
350 DWORD *playpos, DWORD *writepos)
352 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
355 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
357 RtlAcquireResourceShared(&This->lock, TRUE);
359 pos = This->sec_mixpos;
362 if (pos >= This->buflen){
363 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
372 if (writepos && This->state != STATE_STOPPED) {
373 /* apply the documented 10ms lead to writepos */
374 *writepos += This->writelead;
375 *writepos %= This->buflen;
378 RtlReleaseResource(&This->lock);
380 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
381 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
386 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
388 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
390 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
392 if (status == NULL) {
393 WARN("invalid parameter: status = NULL\n");
394 return DSERR_INVALIDPARAM;
398 RtlAcquireResourceShared(&This->lock, TRUE);
399 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
400 *status |= DSBSTATUS_PLAYING;
401 if (This->playflags & DSBPLAY_LOOPING)
402 *status |= DSBSTATUS_LOOPING;
404 RtlReleaseResource(&This->lock);
406 TRACE("status=%x\n", *status);
411 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
412 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
414 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
417 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
419 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
421 if (lpwf) { /* NULL is valid */
422 if (wfsize >= size) {
423 CopyMemory(lpwf,This->pwfx,size);
427 WARN("invalid parameter: wfsize too small\n");
428 CopyMemory(lpwf,This->pwfx,wfsize);
431 return DSERR_INVALIDPARAM;
435 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
437 WARN("invalid parameter: wfwritten == NULL\n");
438 return DSERR_INVALIDPARAM;
445 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
446 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
447 DWORD *audiobytes2, DWORD flags)
449 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
450 HRESULT hres = DS_OK;
452 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n", This, writecursor, writebytes, lplpaudioptr1,
453 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
456 return DSERR_INVALIDPARAM;
458 /* when this flag is set, writecursor is meaningless and must be calculated */
459 if (flags & DSBLOCK_FROMWRITECURSOR) {
460 /* GetCurrentPosition does too much magic to duplicate here */
461 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
463 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
468 /* when this flag is set, writebytes is meaningless and must be set */
469 if (flags & DSBLOCK_ENTIREBUFFER)
470 writebytes = This->buflen;
472 if (writecursor >= This->buflen) {
473 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
474 writecursor, This->buflen);
475 return DSERR_INVALIDPARAM;
478 if (writebytes > This->buflen) {
479 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
480 writebytes, This->buflen);
481 return DSERR_INVALIDPARAM;
485 RtlAcquireResourceShared(&This->lock, TRUE);
487 if (writecursor+writebytes <= This->buflen) {
488 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
489 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
490 WARN("Overwriting mixing position, case 1\n");
491 *audiobytes1 = writebytes;
493 *(LPBYTE*)lplpaudioptr2 = NULL;
496 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
497 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
498 TRACE("->%d.0\n",writebytes);
500 DWORD remainder = writebytes + writecursor - This->buflen;
501 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
502 *audiobytes1 = This->buflen-writecursor;
503 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
504 WARN("Overwriting mixing position, case 2\n");
506 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
508 *audiobytes2 = writebytes-(This->buflen-writecursor);
509 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
510 WARN("Overwriting mixing position, case 3\n");
511 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
514 RtlReleaseResource(&This->lock);
520 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
523 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
524 HRESULT hres = DS_OK;
526 TRACE("(%p,%d)\n",This,newpos);
529 RtlAcquireResourceExclusive(&This->lock, TRUE);
531 /* start mixing from this new location instead */
532 newpos %= This->buflen;
533 newpos -= newpos%This->pwfx->nBlockAlign;
534 This->sec_mixpos = newpos;
536 /* at this point, do not attempt to reset buffers, mess with primary mix position,
537 or anything like that to reduce latency. The data already prebuffered cannot be changed */
539 RtlReleaseResource(&This->lock);
545 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
547 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
548 HRESULT hres = DS_OK;
550 TRACE("(%p,%d)\n",This,pan);
552 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
553 WARN("invalid parameter: pan = %d\n", pan);
554 return DSERR_INVALIDPARAM;
557 /* You cannot use both pan and 3D controls */
558 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
559 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
560 WARN("control unavailable\n");
561 return DSERR_CONTROLUNAVAIL;
565 RtlAcquireResourceExclusive(&This->lock, TRUE);
567 if (This->volpan.lPan != pan) {
568 This->volpan.lPan = pan;
569 DSOUND_RecalcVolPan(&(This->volpan));
572 RtlReleaseResource(&This->lock);
578 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
580 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
582 TRACE("(%p,%p)\n",This,pan);
584 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
585 WARN("control unavailable\n");
586 return DSERR_CONTROLUNAVAIL;
590 WARN("invalid parameter: pan = NULL\n");
591 return DSERR_INVALIDPARAM;
594 *pan = This->volpan.lPan;
599 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
602 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
603 HRESULT hres = DS_OK;
605 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
610 if((p1 && ((BYTE*)p1 < This->buffer->memory ||
611 (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
612 (p2 && ((BYTE*)p2 < This->buffer->memory ||
613 (BYTE*)p2 >= This->buffer->memory + This->buflen)))
614 return DSERR_INVALIDPARAM;
618 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
619 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
621 RtlAcquireResourceShared(&iter->lock, TRUE);
624 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
625 hres = DSERR_INVALIDPARAM;
627 RtlReleaseResource(&iter->lock);
629 RtlReleaseResource(&This->device->buffer_list_lock);
635 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
637 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
639 FIXME("(%p):stub\n",This);
643 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
645 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
647 TRACE("(%p,%p)\n",This,freq);
650 WARN("invalid parameter: freq = NULL\n");
651 return DSERR_INVALIDPARAM;
655 TRACE("-> %d\n", *freq);
660 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
661 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
663 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
666 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
669 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
671 WARN("control unavailable\n");
672 return DSERR_CONTROLUNAVAIL;
675 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
676 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
678 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
681 FIXME("(%p,%08u,%u,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
684 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
686 WARN("control unavailable\n");
690 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
691 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
693 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
695 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
697 WARN("control unavailable\n");
698 return DSERR_CONTROLUNAVAIL;
701 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
702 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
704 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
706 WARN("(%p) already initialized\n", This);
707 return DSERR_ALREADYINITIALIZED;
710 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
712 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
714 TRACE("(%p)->(%p)\n",This,caps);
717 WARN("invalid parameter: caps == NULL\n");
718 return DSERR_INVALIDPARAM;
721 if (caps->dwSize < sizeof(*caps)) {
722 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
723 return DSERR_INVALIDPARAM;
726 caps->dwFlags = This->dsbd.dwFlags;
727 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
729 caps->dwBufferBytes = This->buflen;
731 /* According to windows, this is zero*/
732 caps->dwUnlockTransferRate = 0;
733 caps->dwPlayCpuOverhead = 0;
738 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
741 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
743 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
746 WARN("invalid parameter\n");
750 *ppobj = NULL; /* assume failure */
752 if ( IsEqualGUID(riid, &IID_IUnknown) ||
753 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
754 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
755 IDirectSoundBuffer8_AddRef(iface);
760 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
761 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
762 *ppobj = &This->IDirectSoundNotify_iface;
766 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
767 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
768 *ppobj = &This->IDirectSound3DBuffer_iface;
772 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
773 ERR("app requested IDirectSound3DListener on secondary buffer\n");
774 return E_NOINTERFACE;
777 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
778 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
779 *ppobj = &This->IKsPropertySet_iface;
783 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
785 return E_NOINTERFACE;
788 static const IDirectSoundBuffer8Vtbl dsbvt =
790 IDirectSoundBufferImpl_QueryInterface,
791 IDirectSoundBufferImpl_AddRef,
792 IDirectSoundBufferImpl_Release,
793 IDirectSoundBufferImpl_GetCaps,
794 IDirectSoundBufferImpl_GetCurrentPosition,
795 IDirectSoundBufferImpl_GetFormat,
796 IDirectSoundBufferImpl_GetVolume,
797 IDirectSoundBufferImpl_GetPan,
798 IDirectSoundBufferImpl_GetFrequency,
799 IDirectSoundBufferImpl_GetStatus,
800 IDirectSoundBufferImpl_Initialize,
801 IDirectSoundBufferImpl_Lock,
802 IDirectSoundBufferImpl_Play,
803 IDirectSoundBufferImpl_SetCurrentPosition,
804 IDirectSoundBufferImpl_SetFormat,
805 IDirectSoundBufferImpl_SetVolume,
806 IDirectSoundBufferImpl_SetPan,
807 IDirectSoundBufferImpl_SetFrequency,
808 IDirectSoundBufferImpl_Stop,
809 IDirectSoundBufferImpl_Unlock,
810 IDirectSoundBufferImpl_Restore,
811 IDirectSoundBufferImpl_SetFX,
812 IDirectSoundBufferImpl_AcquireResources,
813 IDirectSoundBufferImpl_GetObjectInPath
816 HRESULT IDirectSoundBufferImpl_Create(
817 DirectSoundDevice * device,
818 IDirectSoundBufferImpl **pdsb,
819 LPCDSBUFFERDESC dsbd)
821 IDirectSoundBufferImpl *dsb;
822 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
825 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
827 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
828 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
830 return DSERR_INVALIDPARAM; /* FIXME: which error? */
833 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
836 WARN("out of memory\n");
838 return DSERR_OUTOFMEMORY;
841 TRACE("Created buffer at %p\n", dsb);
848 dsb->device = device;
849 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
850 dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
851 dsb->IDirectSound3DBuffer_iface.lpVtbl = &ds3dbvt;
852 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
854 /* size depends on version */
855 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
857 dsb->pwfx = DSOUND_CopyFormat(wfex);
858 if (dsb->pwfx == NULL) {
859 HeapFree(GetProcessHeap(),0,dsb);
861 return DSERR_OUTOFMEMORY;
864 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
865 dsb->buflen = dsbd->dwBufferBytes +
866 (dsbd->lpwfxFormat->nBlockAlign -
867 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
869 dsb->buflen = dsbd->dwBufferBytes;
871 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
872 dsb->notifies = NULL;
873 dsb->nrofnotifies = 0;
875 /* Check necessary hardware mixing capabilities */
876 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
877 else capf |= DSCAPS_SECONDARYMONO;
878 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
879 else capf |= DSCAPS_SECONDARY8BIT;
881 TRACE("capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", capf, device->drvcaps.dwFlags);
883 /* Allocate an empty buffer */
884 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
885 if (dsb->buffer == NULL) {
886 WARN("out of memory\n");
887 HeapFree(GetProcessHeap(),0,dsb->pwfx);
888 HeapFree(GetProcessHeap(),0,dsb);
890 return DSERR_OUTOFMEMORY;
893 /* Allocate system memory for buffer */
894 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
895 if (dsb->buffer->memory == NULL) {
896 WARN("out of memory\n");
897 HeapFree(GetProcessHeap(),0,dsb->pwfx);
898 HeapFree(GetProcessHeap(),0,dsb->buffer);
899 HeapFree(GetProcessHeap(),0,dsb);
901 return DSERR_OUTOFMEMORY;
904 dsb->buffer->ref = 1;
905 list_init(&dsb->buffer->buffers);
906 list_add_head(&dsb->buffer->buffers, &dsb->entry);
907 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
909 /* It's not necessary to initialize values to zero since */
910 /* we allocated this structure with HEAP_ZERO_MEMORY... */
912 dsb->state = STATE_STOPPED;
914 dsb->freqAdjust = dsb->freq / (float)device->pwfx->nSamplesPerSec;
915 dsb->nAvgBytesPerSec = dsb->freq *
916 dsbd->lpwfxFormat->nBlockAlign;
918 /* calculate fragment size and write lead */
919 DSOUND_RecalcFormat(dsb);
921 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
922 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
923 dsb->ds3db_ds3db.vPosition.x = 0.0;
924 dsb->ds3db_ds3db.vPosition.y = 0.0;
925 dsb->ds3db_ds3db.vPosition.z = 0.0;
926 dsb->ds3db_ds3db.vVelocity.x = 0.0;
927 dsb->ds3db_ds3db.vVelocity.y = 0.0;
928 dsb->ds3db_ds3db.vVelocity.z = 0.0;
929 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
930 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
931 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
932 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
933 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
934 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
935 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
936 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
937 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
939 dsb->ds3db_need_recalc = FALSE;
940 DSOUND_Calc3DBuffer(dsb);
942 DSOUND_RecalcVolPan(&(dsb->volpan));
944 RtlInitializeResource(&dsb->lock);
946 /* register buffer if not primary */
947 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
948 err = DirectSoundDevice_AddBuffer(device, dsb);
950 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
951 HeapFree(GetProcessHeap(),0,dsb->buffer);
952 RtlDeleteResource(&dsb->lock);
953 HeapFree(GetProcessHeap(),0,dsb->pwfx);
954 HeapFree(GetProcessHeap(),0,dsb);
959 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
964 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
966 ULONG ref = InterlockedIncrement(&This->numIfaces);
969 WARN("Destroying buffer with %u in use interfaces\n", ref - 1);
971 DirectSoundDevice_RemoveBuffer(This->device, This);
972 RtlDeleteResource(&This->lock);
975 list_remove(&This->entry);
976 if (This->buffer->ref == 0) {
977 HeapFree(GetProcessHeap(), 0, This->buffer->memory);
978 HeapFree(GetProcessHeap(), 0, This->buffer);
981 HeapFree(GetProcessHeap(), 0, This->notifies);
982 HeapFree(GetProcessHeap(), 0, This->pwfx);
983 HeapFree(GetProcessHeap(), 0, This);
985 TRACE("(%p) released\n", This);
988 HRESULT IDirectSoundBufferImpl_Duplicate(
989 DirectSoundDevice *device,
990 IDirectSoundBufferImpl **ppdsb,
991 IDirectSoundBufferImpl *pdsb)
993 IDirectSoundBufferImpl *dsb;
994 HRESULT hres = DS_OK;
995 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
997 dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
999 WARN("out of memory\n");
1001 return DSERR_OUTOFMEMORY;
1004 RtlAcquireResourceShared(&pdsb->lock, TRUE);
1006 CopyMemory(dsb, pdsb, sizeof(*dsb));
1008 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1010 RtlReleaseResource(&pdsb->lock);
1012 if (dsb->pwfx == NULL) {
1013 HeapFree(GetProcessHeap(),0,dsb);
1015 return DSERR_OUTOFMEMORY;
1019 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1025 dsb->state = STATE_STOPPED;
1026 dsb->sec_mixpos = 0;
1027 dsb->notifies = NULL;
1028 dsb->nrofnotifies = 0;
1029 dsb->device = device;
1030 DSOUND_RecalcFormat(dsb);
1032 RtlInitializeResource(&dsb->lock);
1034 /* register buffer */
1035 hres = DirectSoundDevice_AddBuffer(device, dsb);
1036 if (hres != DS_OK) {
1037 RtlDeleteResource(&dsb->lock);
1038 list_remove(&dsb->entry);
1040 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1041 HeapFree(GetProcessHeap(),0,dsb);
1045 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1050 /*******************************************************************************
1054 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1056 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1059 /* IUnknown methods */
1060 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1063 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1065 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1067 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1070 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1072 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1073 ULONG ref = InterlockedIncrement(&This->refiks);
1075 TRACE("(%p) ref was %d\n", This, ref - 1);
1078 InterlockedIncrement(&This->numIfaces);
1083 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1085 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1088 if (is_primary_buffer(This)){
1089 ref = capped_refcount_dec(&This->refiks);
1091 capped_refcount_dec(&This->numIfaces);
1092 TRACE("(%p) ref is now: %d\n", This, ref);
1096 ref = InterlockedDecrement(&This->refiks);
1097 if (!ref && !InterlockedDecrement(&This->numIfaces))
1098 secondarybuffer_destroy(This);
1100 TRACE("(%p) ref is now %d\n", This, ref);
1105 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1106 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1107 ULONG cbPropData, ULONG *pcbReturned)
1109 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1111 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1112 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1114 return E_PROP_ID_UNSUPPORTED;
1117 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1118 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1121 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1123 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1125 return E_PROP_ID_UNSUPPORTED;
1128 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1129 ULONG dwPropID, ULONG *pTypeSupport)
1131 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1133 TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1135 return E_PROP_ID_UNSUPPORTED;
1138 const IKsPropertySetVtbl iksbvt = {
1139 IKsPropertySetImpl_QueryInterface,
1140 IKsPropertySetImpl_AddRef,
1141 IKsPropertySetImpl_Release,
1142 IKsPropertySetImpl_Get,
1143 IKsPropertySetImpl_Set,
1144 IKsPropertySetImpl_QuerySupport