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);
330 ULONG ref = InterlockedDecrement(&This->ref);
332 TRACE("(%p) ref was %d\n", This, ref + 1);
334 if (!ref && !InterlockedDecrement(&This->numIfaces)) {
335 if (is_primary_buffer(This))
336 primarybuffer_destroy(This);
338 secondarybuffer_destroy(This);
343 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
344 DWORD *playpos, DWORD *writepos)
346 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
349 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
351 RtlAcquireResourceShared(&This->lock, TRUE);
353 pos = This->sec_mixpos;
356 if (pos >= This->buflen){
357 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
366 if (writepos && This->state != STATE_STOPPED) {
367 /* apply the documented 10ms lead to writepos */
368 *writepos += This->writelead;
369 *writepos %= This->buflen;
372 RtlReleaseResource(&This->lock);
374 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
375 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
380 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
382 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
384 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
386 if (status == NULL) {
387 WARN("invalid parameter: status = NULL\n");
388 return DSERR_INVALIDPARAM;
392 RtlAcquireResourceShared(&This->lock, TRUE);
393 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
394 *status |= DSBSTATUS_PLAYING;
395 if (This->playflags & DSBPLAY_LOOPING)
396 *status |= DSBSTATUS_LOOPING;
398 RtlReleaseResource(&This->lock);
400 TRACE("status=%x\n", *status);
405 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
406 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
408 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
411 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
413 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
415 if (lpwf) { /* NULL is valid */
416 if (wfsize >= size) {
417 CopyMemory(lpwf,This->pwfx,size);
421 WARN("invalid parameter: wfsize too small\n");
422 CopyMemory(lpwf,This->pwfx,wfsize);
425 return DSERR_INVALIDPARAM;
429 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
431 WARN("invalid parameter: wfwritten == NULL\n");
432 return DSERR_INVALIDPARAM;
439 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
440 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
441 DWORD *audiobytes2, DWORD flags)
443 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
444 HRESULT hres = DS_OK;
446 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n", This, writecursor, writebytes, lplpaudioptr1,
447 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
450 return DSERR_INVALIDPARAM;
452 /* when this flag is set, writecursor is meaningless and must be calculated */
453 if (flags & DSBLOCK_FROMWRITECURSOR) {
454 /* GetCurrentPosition does too much magic to duplicate here */
455 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
457 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
462 /* when this flag is set, writebytes is meaningless and must be set */
463 if (flags & DSBLOCK_ENTIREBUFFER)
464 writebytes = This->buflen;
466 if (writecursor >= This->buflen) {
467 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
468 writecursor, This->buflen);
469 return DSERR_INVALIDPARAM;
472 if (writebytes > This->buflen) {
473 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
474 writebytes, This->buflen);
475 return DSERR_INVALIDPARAM;
479 RtlAcquireResourceShared(&This->lock, TRUE);
481 if (writecursor+writebytes <= This->buflen) {
482 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
483 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
484 WARN("Overwriting mixing position, case 1\n");
485 *audiobytes1 = writebytes;
487 *(LPBYTE*)lplpaudioptr2 = NULL;
490 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
491 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
492 TRACE("->%d.0\n",writebytes);
494 DWORD remainder = writebytes + writecursor - This->buflen;
495 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
496 *audiobytes1 = This->buflen-writecursor;
497 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
498 WARN("Overwriting mixing position, case 2\n");
500 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
502 *audiobytes2 = writebytes-(This->buflen-writecursor);
503 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
504 WARN("Overwriting mixing position, case 3\n");
505 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
508 RtlReleaseResource(&This->lock);
514 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
517 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
518 HRESULT hres = DS_OK;
520 TRACE("(%p,%d)\n",This,newpos);
523 RtlAcquireResourceExclusive(&This->lock, TRUE);
525 /* start mixing from this new location instead */
526 newpos %= This->buflen;
527 newpos -= newpos%This->pwfx->nBlockAlign;
528 This->sec_mixpos = newpos;
530 /* at this point, do not attempt to reset buffers, mess with primary mix position,
531 or anything like that to reduce latency. The data already prebuffered cannot be changed */
533 RtlReleaseResource(&This->lock);
539 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
541 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
542 HRESULT hres = DS_OK;
544 TRACE("(%p,%d)\n",This,pan);
546 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
547 WARN("invalid parameter: pan = %d\n", pan);
548 return DSERR_INVALIDPARAM;
551 /* You cannot use both pan and 3D controls */
552 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
553 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
554 WARN("control unavailable\n");
555 return DSERR_CONTROLUNAVAIL;
559 RtlAcquireResourceExclusive(&This->lock, TRUE);
561 if (This->volpan.lPan != pan) {
562 This->volpan.lPan = pan;
563 DSOUND_RecalcVolPan(&(This->volpan));
566 RtlReleaseResource(&This->lock);
572 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
574 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
576 TRACE("(%p,%p)\n",This,pan);
578 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
579 WARN("control unavailable\n");
580 return DSERR_CONTROLUNAVAIL;
584 WARN("invalid parameter: pan = NULL\n");
585 return DSERR_INVALIDPARAM;
588 *pan = This->volpan.lPan;
593 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
596 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
597 HRESULT hres = DS_OK;
599 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
604 if((p1 && ((BYTE*)p1 < This->buffer->memory ||
605 (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
606 (p2 && ((BYTE*)p2 < This->buffer->memory ||
607 (BYTE*)p2 >= This->buffer->memory + This->buflen)))
608 return DSERR_INVALIDPARAM;
612 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
613 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
615 RtlAcquireResourceShared(&iter->lock, TRUE);
618 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
619 hres = DSERR_INVALIDPARAM;
621 RtlReleaseResource(&iter->lock);
623 RtlReleaseResource(&This->device->buffer_list_lock);
629 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
631 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
633 FIXME("(%p):stub\n",This);
637 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
639 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
641 TRACE("(%p,%p)\n",This,freq);
644 WARN("invalid parameter: freq = NULL\n");
645 return DSERR_INVALIDPARAM;
649 TRACE("-> %d\n", *freq);
654 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
655 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
657 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
660 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
663 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
665 WARN("control unavailable\n");
666 return DSERR_CONTROLUNAVAIL;
669 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
670 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
672 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
675 FIXME("(%p,%08u,%u,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
678 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
680 WARN("control unavailable\n");
684 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
685 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
687 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
689 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
691 WARN("control unavailable\n");
692 return DSERR_CONTROLUNAVAIL;
695 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
696 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
698 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
700 WARN("(%p) already initialized\n", This);
701 return DSERR_ALREADYINITIALIZED;
704 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
706 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
708 TRACE("(%p)->(%p)\n",This,caps);
711 WARN("invalid parameter: caps == NULL\n");
712 return DSERR_INVALIDPARAM;
715 if (caps->dwSize < sizeof(*caps)) {
716 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
717 return DSERR_INVALIDPARAM;
720 caps->dwFlags = This->dsbd.dwFlags;
721 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
723 caps->dwBufferBytes = This->buflen;
725 /* According to windows, this is zero*/
726 caps->dwUnlockTransferRate = 0;
727 caps->dwPlayCpuOverhead = 0;
732 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
735 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
737 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
740 WARN("invalid parameter\n");
744 *ppobj = NULL; /* assume failure */
746 if ( IsEqualGUID(riid, &IID_IUnknown) ||
747 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
748 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
749 IDirectSoundBuffer8_AddRef(iface);
754 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
755 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
756 *ppobj = &This->IDirectSoundNotify_iface;
760 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
761 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
762 *ppobj = &This->IDirectSound3DBuffer_iface;
766 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
767 ERR("app requested IDirectSound3DListener on secondary buffer\n");
768 return E_NOINTERFACE;
771 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
772 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
773 *ppobj = &This->IKsPropertySet_iface;
777 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
779 return E_NOINTERFACE;
782 static const IDirectSoundBuffer8Vtbl dsbvt =
784 IDirectSoundBufferImpl_QueryInterface,
785 IDirectSoundBufferImpl_AddRef,
786 IDirectSoundBufferImpl_Release,
787 IDirectSoundBufferImpl_GetCaps,
788 IDirectSoundBufferImpl_GetCurrentPosition,
789 IDirectSoundBufferImpl_GetFormat,
790 IDirectSoundBufferImpl_GetVolume,
791 IDirectSoundBufferImpl_GetPan,
792 IDirectSoundBufferImpl_GetFrequency,
793 IDirectSoundBufferImpl_GetStatus,
794 IDirectSoundBufferImpl_Initialize,
795 IDirectSoundBufferImpl_Lock,
796 IDirectSoundBufferImpl_Play,
797 IDirectSoundBufferImpl_SetCurrentPosition,
798 IDirectSoundBufferImpl_SetFormat,
799 IDirectSoundBufferImpl_SetVolume,
800 IDirectSoundBufferImpl_SetPan,
801 IDirectSoundBufferImpl_SetFrequency,
802 IDirectSoundBufferImpl_Stop,
803 IDirectSoundBufferImpl_Unlock,
804 IDirectSoundBufferImpl_Restore,
805 IDirectSoundBufferImpl_SetFX,
806 IDirectSoundBufferImpl_AcquireResources,
807 IDirectSoundBufferImpl_GetObjectInPath
810 HRESULT IDirectSoundBufferImpl_Create(
811 DirectSoundDevice * device,
812 IDirectSoundBufferImpl **pdsb,
813 LPCDSBUFFERDESC dsbd)
815 IDirectSoundBufferImpl *dsb;
816 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
819 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
821 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
822 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
824 return DSERR_INVALIDPARAM; /* FIXME: which error? */
827 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
830 WARN("out of memory\n");
832 return DSERR_OUTOFMEMORY;
835 TRACE("Created buffer at %p\n", dsb);
842 dsb->device = device;
843 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
844 dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
845 dsb->IDirectSound3DBuffer_iface.lpVtbl = &ds3dbvt;
846 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
848 /* size depends on version */
849 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
851 dsb->pwfx = DSOUND_CopyFormat(wfex);
852 if (dsb->pwfx == NULL) {
853 HeapFree(GetProcessHeap(),0,dsb);
855 return DSERR_OUTOFMEMORY;
858 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
859 dsb->buflen = dsbd->dwBufferBytes +
860 (dsbd->lpwfxFormat->nBlockAlign -
861 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
863 dsb->buflen = dsbd->dwBufferBytes;
865 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
866 dsb->notifies = NULL;
867 dsb->nrofnotifies = 0;
869 /* Check necessary hardware mixing capabilities */
870 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
871 else capf |= DSCAPS_SECONDARYMONO;
872 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
873 else capf |= DSCAPS_SECONDARY8BIT;
875 TRACE("capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", capf, device->drvcaps.dwFlags);
877 /* Allocate an empty buffer */
878 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
879 if (dsb->buffer == NULL) {
880 WARN("out of memory\n");
881 HeapFree(GetProcessHeap(),0,dsb->pwfx);
882 HeapFree(GetProcessHeap(),0,dsb);
884 return DSERR_OUTOFMEMORY;
887 /* Allocate system memory for buffer */
888 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
889 if (dsb->buffer->memory == NULL) {
890 WARN("out of memory\n");
891 HeapFree(GetProcessHeap(),0,dsb->pwfx);
892 HeapFree(GetProcessHeap(),0,dsb->buffer);
893 HeapFree(GetProcessHeap(),0,dsb);
895 return DSERR_OUTOFMEMORY;
898 dsb->buffer->ref = 1;
899 list_init(&dsb->buffer->buffers);
900 list_add_head(&dsb->buffer->buffers, &dsb->entry);
901 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
903 /* It's not necessary to initialize values to zero since */
904 /* we allocated this structure with HEAP_ZERO_MEMORY... */
906 dsb->state = STATE_STOPPED;
908 dsb->freqAdjust = dsb->freq / (float)device->pwfx->nSamplesPerSec;
909 dsb->nAvgBytesPerSec = dsb->freq *
910 dsbd->lpwfxFormat->nBlockAlign;
912 /* calculate fragment size and write lead */
913 DSOUND_RecalcFormat(dsb);
915 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
916 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
917 dsb->ds3db_ds3db.vPosition.x = 0.0;
918 dsb->ds3db_ds3db.vPosition.y = 0.0;
919 dsb->ds3db_ds3db.vPosition.z = 0.0;
920 dsb->ds3db_ds3db.vVelocity.x = 0.0;
921 dsb->ds3db_ds3db.vVelocity.y = 0.0;
922 dsb->ds3db_ds3db.vVelocity.z = 0.0;
923 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
924 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
925 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
926 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
927 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
928 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
929 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
930 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
931 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
933 dsb->ds3db_need_recalc = FALSE;
934 DSOUND_Calc3DBuffer(dsb);
936 DSOUND_RecalcVolPan(&(dsb->volpan));
938 RtlInitializeResource(&dsb->lock);
940 /* register buffer if not primary */
941 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
942 err = DirectSoundDevice_AddBuffer(device, dsb);
944 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
945 HeapFree(GetProcessHeap(),0,dsb->buffer);
946 RtlDeleteResource(&dsb->lock);
947 HeapFree(GetProcessHeap(),0,dsb->pwfx);
948 HeapFree(GetProcessHeap(),0,dsb);
953 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
958 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
960 ULONG ref = InterlockedIncrement(&This->numIfaces);
963 WARN("Destroying buffer with %u in use interfaces\n", ref - 1);
965 DirectSoundDevice_RemoveBuffer(This->device, This);
966 RtlDeleteResource(&This->lock);
969 list_remove(&This->entry);
970 if (This->buffer->ref == 0) {
971 HeapFree(GetProcessHeap(), 0, This->buffer->memory);
972 HeapFree(GetProcessHeap(), 0, This->buffer);
975 HeapFree(GetProcessHeap(), 0, This->notifies);
976 HeapFree(GetProcessHeap(), 0, This->pwfx);
977 HeapFree(GetProcessHeap(), 0, This);
979 TRACE("(%p) released\n", This);
982 HRESULT IDirectSoundBufferImpl_Duplicate(
983 DirectSoundDevice *device,
984 IDirectSoundBufferImpl **ppdsb,
985 IDirectSoundBufferImpl *pdsb)
987 IDirectSoundBufferImpl *dsb;
988 HRESULT hres = DS_OK;
989 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
991 dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
993 WARN("out of memory\n");
995 return DSERR_OUTOFMEMORY;
998 RtlAcquireResourceShared(&pdsb->lock, TRUE);
1000 CopyMemory(dsb, pdsb, sizeof(*dsb));
1002 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1004 RtlReleaseResource(&pdsb->lock);
1006 if (dsb->pwfx == NULL) {
1007 HeapFree(GetProcessHeap(),0,dsb);
1009 return DSERR_OUTOFMEMORY;
1013 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1019 dsb->state = STATE_STOPPED;
1020 dsb->sec_mixpos = 0;
1021 dsb->notifies = NULL;
1022 dsb->nrofnotifies = 0;
1023 dsb->device = device;
1024 DSOUND_RecalcFormat(dsb);
1026 RtlInitializeResource(&dsb->lock);
1028 /* register buffer */
1029 hres = DirectSoundDevice_AddBuffer(device, dsb);
1030 if (hres != DS_OK) {
1031 RtlDeleteResource(&dsb->lock);
1032 list_remove(&dsb->entry);
1034 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1035 HeapFree(GetProcessHeap(),0,dsb);
1039 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1044 /*******************************************************************************
1048 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1050 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1053 /* IUnknown methods */
1054 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1057 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1059 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1061 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1064 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1066 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1067 ULONG ref = InterlockedIncrement(&This->refiks);
1069 TRACE("(%p) ref was %d\n", This, ref - 1);
1072 InterlockedIncrement(&This->numIfaces);
1077 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1079 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1080 ULONG ref = InterlockedDecrement(&This->refiks);
1082 TRACE("(%p) ref was %d\n", This, ref + 1);
1084 if (!ref && !InterlockedDecrement(&This->numIfaces)) {
1085 if (is_primary_buffer(This))
1086 primarybuffer_destroy(This);
1088 secondarybuffer_destroy(This);
1093 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1094 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1095 ULONG cbPropData, ULONG *pcbReturned)
1097 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1099 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1100 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1102 return E_PROP_ID_UNSUPPORTED;
1105 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1106 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1109 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1111 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1113 return E_PROP_ID_UNSUPPORTED;
1116 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1117 ULONG dwPropID, ULONG *pTypeSupport)
1119 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1121 TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1123 return E_PROP_ID_UNSUPPORTED;
1126 const IKsPropertySetVtbl iksbvt = {
1127 IKsPropertySetImpl_QueryInterface,
1128 IKsPropertySetImpl_AddRef,
1129 IKsPropertySetImpl_Release,
1130 IKsPropertySetImpl_Get,
1131 IKsPropertySetImpl_Set,
1132 IKsPropertySetImpl_QuerySupport