imm32: Ignore some messages in ImmTranslateMessage.
[wine.git] / dlls / dsound / buffer.c
blob7b830604a602d892fb8bc0cc1dd22aa5a7bcb2e6
1 /* DirectSound
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
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "mmsystem.h"
30 #include "vfwmsgs.h"
31 #include "wine/debug.h"
32 #include "dsound.h"
33 #include "dsound_private.h"
34 #include "dsconf.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
38 /*******************************************************************************
39 * IDirectSoundNotify
42 static inline struct IDirectSoundBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
44 return CONTAINING_RECORD(iface, struct IDirectSoundBufferImpl, IDirectSoundNotify_iface);
47 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
48 void **ppobj)
50 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
52 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
54 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
57 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
59 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
60 ULONG ref = InterlockedIncrement(&This->refn);
62 TRACE("(%p) ref %ld\n", This, ref);
64 if(ref == 1)
65 InterlockedIncrement(&This->numIfaces);
67 return ref;
70 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
72 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
73 ULONG ref = InterlockedDecrement(&This->refn);
75 TRACE("(%p) ref %ld\n", This, ref);
77 if (!ref && !InterlockedDecrement(&This->numIfaces))
78 secondarybuffer_destroy(This);
80 return ref;
83 static int __cdecl notify_compar(const void *l, const void *r)
85 const DSBPOSITIONNOTIFY *left = l;
86 const DSBPOSITIONNOTIFY *right = r;
88 /* place DSBPN_OFFSETSTOP at the start of the sorted array */
89 if(left->dwOffset == DSBPN_OFFSETSTOP){
90 if(right->dwOffset != DSBPN_OFFSETSTOP)
91 return -1;
92 }else if(right->dwOffset == DSBPN_OFFSETSTOP)
93 return 1;
95 if(left->dwOffset == right->dwOffset)
96 return 0;
98 if(left->dwOffset < right->dwOffset)
99 return -1;
101 return 1;
104 static void commit_next_chunk(IDirectSoundBufferImpl *dsb)
106 void *dstbuff = dsb->committedbuff, *srcbuff = dsb->buffer->memory;
107 DWORD srcoff = dsb->sec_mixpos, srcsize = dsb->buflen, cpysize = dsb->writelead;
109 if(dsb->state != STATE_PLAYING)
110 return;
112 if(cpysize > srcsize - srcoff) {
113 DWORD overflow = cpysize - (srcsize - srcoff);
114 memcpy(dstbuff, (BYTE*)srcbuff + srcoff, srcsize - srcoff);
115 memcpy((BYTE*)dstbuff + (srcsize - srcoff), srcbuff, overflow);
116 }else{
117 memcpy(dstbuff, (BYTE*)srcbuff + srcoff, cpysize);
120 dsb->use_committed = TRUE;
121 dsb->committed_mixpos = 0;
122 TRACE("committing %lu bytes from offset %lu\n", dsb->writelead, dsb->sec_mixpos);
125 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
126 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
128 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
130 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
132 if (howmuch > 0 && notify == NULL) {
133 WARN("invalid parameter: notify == NULL\n");
134 return DSERR_INVALIDPARAM;
137 if (TRACE_ON(dsound)) {
138 unsigned int i;
139 for (i=0;i<howmuch;i++)
140 TRACE("notify at %ld to %p\n",
141 notify[i].dwOffset,notify[i].hEventNotify);
144 if (howmuch > 0) {
145 /* Make an internal copy of the caller-supplied array.
146 * Replace the existing copy if one is already present. */
147 free(This->notifies);
148 This->notifies = malloc(howmuch * sizeof(DSBPOSITIONNOTIFY));
150 if (This->notifies == NULL) {
151 WARN("out of memory\n");
152 return DSERR_OUTOFMEMORY;
154 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
155 This->nrofnotifies = howmuch;
156 qsort(This->notifies, howmuch, sizeof(DSBPOSITIONNOTIFY), notify_compar);
157 } else {
158 free(This->notifies);
159 This->notifies = NULL;
160 This->nrofnotifies = 0;
163 return S_OK;
166 static const IDirectSoundNotifyVtbl dsnvt =
168 IDirectSoundNotifyImpl_QueryInterface,
169 IDirectSoundNotifyImpl_AddRef,
170 IDirectSoundNotifyImpl_Release,
171 IDirectSoundNotifyImpl_SetNotificationPositions,
174 /*******************************************************************************
175 * IDirectSoundBuffer
178 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
180 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
183 static inline BOOL is_primary_buffer(IDirectSoundBufferImpl *This)
185 return (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) != 0;
188 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
189 LPCWAVEFORMATEX wfex)
191 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
193 TRACE("(%p,%p)\n", iface, wfex);
195 if (is_primary_buffer(This))
196 return primarybuffer_SetFormat(This->device, wfex);
197 else {
198 WARN("not available for secondary buffers.\n");
199 return DSERR_INVALIDCALL;
203 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
205 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
206 LONG oldVol;
208 HRESULT hres = DS_OK;
210 TRACE("(%p,%ld)\n",This,vol);
212 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
213 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This->dsbd.dwFlags);
214 return DSERR_CONTROLUNAVAIL;
217 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
218 WARN("invalid parameter: vol = %ld\n", vol);
219 return DSERR_INVALIDPARAM;
222 AcquireSRWLockExclusive(&This->lock);
224 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
225 oldVol = This->ds3db_lVolume;
226 This->ds3db_lVolume = vol;
227 if (vol != oldVol)
228 /* recalc 3d volume, which in turn recalcs the pans */
229 DSOUND_Calc3DBuffer(This);
230 } else {
231 oldVol = This->volpan.lVolume;
232 This->volpan.lVolume = vol;
233 if (vol != oldVol)
234 DSOUND_RecalcVolPan(&(This->volpan));
237 ReleaseSRWLockExclusive(&This->lock);
239 return hres;
242 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
244 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
246 TRACE("(%p,%p)\n",This,vol);
248 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
249 WARN("control unavailable\n");
250 return DSERR_CONTROLUNAVAIL;
253 if (vol == NULL) {
254 WARN("invalid parameter: vol == NULL\n");
255 return DSERR_INVALIDPARAM;
258 *vol = This->volpan.lVolume;
260 return DS_OK;
263 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
265 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
266 DWORD oldFreq;
268 TRACE("(%p,%ld)\n",This,freq);
270 if (is_primary_buffer(This)) {
271 WARN("not available for primary buffers.\n");
272 return DSERR_CONTROLUNAVAIL;
275 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
276 WARN("control unavailable\n");
277 return DSERR_CONTROLUNAVAIL;
280 if (freq == DSBFREQUENCY_ORIGINAL)
281 freq = This->pwfx->nSamplesPerSec;
283 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
284 WARN("invalid parameter: freq = %ld\n", freq);
285 return DSERR_INVALIDPARAM;
288 AcquireSRWLockExclusive(&This->lock);
290 oldFreq = This->freq;
291 This->freq = freq;
292 if (freq != oldFreq)
293 DSOUND_RecalcFormat(This);
295 ReleaseSRWLockExclusive(&This->lock);
297 return DS_OK;
300 static HRESULT WINAPI IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
301 DWORD reserved2, DWORD flags)
303 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
304 HRESULT hres = DS_OK;
305 int i;
307 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
309 AcquireSRWLockExclusive(&This->lock);
311 This->playflags = flags;
312 if (This->state == STATE_STOPPED) {
313 This->leadin = TRUE;
314 This->state = STATE_STARTING;
317 for (i = 0; i < This->num_filters; i++) {
318 IMediaObject_Discontinuity(This->filters[i].obj, 0);
321 ReleaseSRWLockExclusive(&This->lock);
323 return hres;
326 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 *iface)
328 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
329 HRESULT hres = DS_OK;
331 TRACE("(%p)\n",This);
333 AcquireSRWLockExclusive(&This->lock);
335 if (This->state == STATE_PLAYING || This->state == STATE_STARTING)
337 This->state = STATE_STOPPED;
338 This->use_committed = FALSE;
339 This->committed_mixpos = 0;
340 DSOUND_CheckEvent(This, 0, 0);
343 ReleaseSRWLockExclusive(&This->lock);
345 return hres;
348 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
350 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
351 ULONG ref = InterlockedIncrement(&This->ref);
353 TRACE("(%p) ref %ld\n", This, ref);
355 if(ref == 1)
356 InterlockedIncrement(&This->numIfaces);
358 return ref;
361 static ULONG WINAPI IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 *iface)
363 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
364 ULONG ref;
366 if (is_primary_buffer(This)){
367 ref = capped_refcount_dec(&This->ref);
368 if(!ref)
369 capped_refcount_dec(&This->numIfaces);
370 TRACE("(%p) ref %ld\n", This, ref);
371 return ref;
374 ref = InterlockedDecrement(&This->ref);
375 if (!ref && !InterlockedDecrement(&This->numIfaces))
376 secondarybuffer_destroy(This);
378 TRACE("(%p) ref %ld\n", This, ref);
380 return ref;
383 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
384 DWORD *playpos, DWORD *writepos)
386 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
387 DWORD pos;
389 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
391 AcquireSRWLockShared(&This->lock);
393 pos = This->sec_mixpos;
395 /* sanity */
396 if (pos >= This->buflen){
397 FIXME("Bad play position. playpos: %ld, buflen: %ld\n", pos, This->buflen);
398 pos %= This->buflen;
401 if (playpos)
402 *playpos = pos;
403 if (writepos)
404 *writepos = pos;
406 if (writepos && This->state != STATE_STOPPED) {
407 /* apply the documented 10ms lead to writepos */
408 *writepos += This->writelead;
409 *writepos %= This->buflen;
412 ReleaseSRWLockShared(&This->lock);
414 TRACE("playpos = %ld, writepos = %ld, buflen=%ld (%p, time=%ld)\n",
415 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
417 return DS_OK;
420 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
422 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
424 TRACE("(%p,%p)\n",This,status);
426 if (status == NULL) {
427 WARN("invalid parameter: status = NULL\n");
428 return DSERR_INVALIDPARAM;
431 *status = 0;
432 AcquireSRWLockShared(&This->lock);
433 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
434 *status |= DSBSTATUS_PLAYING;
435 if (This->playflags & DSBPLAY_LOOPING)
436 *status |= DSBSTATUS_LOOPING;
438 if (This->dsbd.dwFlags & DSBCAPS_LOCDEFER)
439 *status |= DSBSTATUS_LOCSOFTWARE;
440 ReleaseSRWLockShared(&This->lock);
442 TRACE("status=%lx\n", *status);
443 return DS_OK;
447 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
448 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
450 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
451 DWORD size;
453 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
455 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
457 if (lpwf) { /* NULL is valid */
458 if (wfsize >= size) {
459 CopyMemory(lpwf,This->pwfx,size);
460 if (wfwritten)
461 *wfwritten = size;
462 } else {
463 WARN("invalid parameter: wfsize too small\n");
464 CopyMemory(lpwf,This->pwfx,wfsize);
465 if (wfwritten)
466 *wfwritten = wfsize;
467 return DSERR_INVALIDPARAM;
469 } else {
470 if (wfwritten)
471 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
472 else {
473 WARN("invalid parameter: wfwritten == NULL\n");
474 return DSERR_INVALIDPARAM;
478 return DS_OK;
481 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
482 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
483 DWORD *audiobytes2, DWORD flags)
485 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
486 HRESULT hres = DS_OK;
488 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n", This, writecursor, writebytes, lplpaudioptr1,
489 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
491 if (!audiobytes1)
492 return DSERR_INVALIDPARAM;
494 /* when this flag is set, writecursor is meaningless and must be calculated */
495 if (flags & DSBLOCK_FROMWRITECURSOR) {
496 /* GetCurrentPosition does too much magic to duplicate here */
497 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
498 if (hres != DS_OK) {
499 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
500 return hres;
504 /* when this flag is set, writebytes is meaningless and must be set */
505 if (flags & DSBLOCK_ENTIREBUFFER)
506 writebytes = This->buflen;
508 if (writecursor >= This->buflen) {
509 WARN("Invalid parameter, writecursor: %lu >= buflen: %lu\n",
510 writecursor, This->buflen);
511 return DSERR_INVALIDPARAM;
514 if (writebytes > This->buflen) {
515 WARN("Invalid parameter, writebytes: %lu > buflen: %lu\n",
516 writebytes, This->buflen);
517 return DSERR_INVALIDPARAM;
520 AcquireSRWLockShared(&This->lock);
522 if (writecursor+writebytes <= This->buflen) {
523 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
524 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING) {
525 WARN("Overwriting mixing position, case 1\n");
526 commit_next_chunk(This);
528 *audiobytes1 = writebytes;
529 if (lplpaudioptr2)
530 *(LPBYTE*)lplpaudioptr2 = NULL;
531 if (audiobytes2)
532 *audiobytes2 = 0;
533 TRACE("Locked %p(%li bytes) and %p(%li bytes) writecursor=%ld\n",
534 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
535 TRACE("->%ld.0\n",writebytes);
536 This->buffer->lockedbytes += writebytes;
537 } else {
538 DWORD remainder = writebytes + writecursor - This->buflen;
539 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
540 *audiobytes1 = This->buflen-writecursor;
541 This->buffer->lockedbytes += *audiobytes1;
542 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING) {
543 WARN("Overwriting mixing position, case 2\n");
544 commit_next_chunk(This);
546 if (lplpaudioptr2)
547 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
548 if (audiobytes2) {
549 *audiobytes2 = writebytes-(This->buflen-writecursor);
550 This->buffer->lockedbytes += *audiobytes2;
552 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING) {
553 WARN("Overwriting mixing position, case 3\n");
554 commit_next_chunk(This);
556 TRACE("Locked %p(%li bytes) and %p(%li bytes) writecursor=%ld\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
559 ReleaseSRWLockShared(&This->lock);
561 return DS_OK;
564 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
565 DWORD newpos)
567 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
568 HRESULT hres = DS_OK;
570 TRACE("(%p,%ld)\n",This,newpos);
572 AcquireSRWLockExclusive(&This->lock);
574 /* start mixing from this new location instead */
575 newpos %= This->buflen;
576 newpos -= newpos%This->pwfx->nBlockAlign;
577 This->sec_mixpos = newpos;
579 This->use_committed = FALSE;
580 This->committed_mixpos = 0;
582 /* at this point, do not attempt to reset buffers, mess with primary mix position,
583 or anything like that to reduce latency. The data already prebuffered cannot be changed */
585 ReleaseSRWLockExclusive(&This->lock);
587 return hres;
590 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
592 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
593 HRESULT hres = DS_OK;
595 TRACE("(%p,%ld)\n",This,pan);
597 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
598 WARN("invalid parameter: pan = %ld\n", pan);
599 return DSERR_INVALIDPARAM;
602 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
603 WARN("control unavailable\n");
604 return DSERR_CONTROLUNAVAIL;
607 AcquireSRWLockExclusive(&This->lock);
609 if (This->volpan.lPan != pan) {
610 This->volpan.lPan = pan;
611 DSOUND_RecalcVolPan(&(This->volpan));
614 ReleaseSRWLockExclusive(&This->lock);
616 return hres;
619 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
621 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
623 TRACE("(%p,%p)\n",This,pan);
625 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
626 WARN("control unavailable\n");
627 return DSERR_CONTROLUNAVAIL;
630 if (pan == NULL) {
631 WARN("invalid parameter: pan = NULL\n");
632 return DSERR_INVALIDPARAM;
635 *pan = This->volpan.lPan;
637 return DS_OK;
640 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
641 void *p2, DWORD x2)
643 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
644 HRESULT hres = DS_OK;
646 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
648 if (!p2)
649 x2 = 0;
651 if((p1 && ((BYTE*)p1 < This->buffer->memory || (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
652 (p2 && ((BYTE*)p2 < This->buffer->memory || (BYTE*)p2 >= This->buffer->memory + This->buflen)))
653 return DSERR_INVALIDPARAM;
655 if (x1 || x2)
657 AcquireSRWLockShared(&This->device->buffer_list_lock);
658 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
660 AcquireSRWLockShared(&iter->lock);
661 if (x1)
663 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
664 hres = DSERR_INVALIDPARAM;
665 else
666 iter->buffer->lockedbytes -= x1;
669 if (x2)
671 if(x2 + (DWORD_PTR)p2 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
672 hres = DSERR_INVALIDPARAM;
673 else
674 iter->buffer->lockedbytes -= x2;
676 ReleaseSRWLockShared(&iter->lock);
678 ReleaseSRWLockShared(&This->device->buffer_list_lock);
681 return hres;
684 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
686 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
688 FIXME("(%p):stub\n",This);
689 return DS_OK;
692 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
694 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
696 TRACE("(%p,%p)\n",This,freq);
698 if (freq == NULL) {
699 WARN("invalid parameter: freq = NULL\n");
700 return DSERR_INVALIDPARAM;
703 *freq = This->freq;
704 TRACE("-> %ld\n", *freq);
706 return DS_OK;
709 static const char* dump_DSFX_guid(const DSEFFECTDESC *desc)
711 #define FE(guid) if (IsEqualGUID(&guid, &desc->guidDSFXClass)) return #guid
712 FE(GUID_DSFX_STANDARD_GARGLE);
713 FE(GUID_DSFX_STANDARD_CHORUS);
714 FE(GUID_DSFX_STANDARD_FLANGER);
715 FE(GUID_DSFX_STANDARD_ECHO);
716 FE(GUID_DSFX_STANDARD_DISTORTION);
717 FE(GUID_DSFX_STANDARD_COMPRESSOR);
718 FE(GUID_DSFX_STANDARD_PARAMEQ);
719 FE(GUID_DSFX_STANDARD_I3DL2REVERB);
720 FE(GUID_DSFX_WAVES_REVERB);
721 #undef FE
723 return debugstr_guid(&desc->guidDSFXClass);
726 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
727 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
729 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
730 DWORD u;
731 DSFilter *filters;
732 HRESULT hr, hr2;
733 DMO_MEDIA_TYPE dmt;
734 WAVEFORMATEX wfx;
736 TRACE("(%p,%lu,%p,%p)\n", This, dwEffectsCount, pDSFXDesc, pdwResultCodes);
738 if (pdwResultCodes)
739 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
741 if ((dwEffectsCount > 0 && !pDSFXDesc) ||
742 (dwEffectsCount == 0 && (pDSFXDesc || pdwResultCodes))
744 return E_INVALIDARG;
746 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFX)) {
747 WARN("attempted to call SetFX on buffer without DSBCAPS_CTRLFX\n");
748 return DSERR_CONTROLUNAVAIL;
751 if (This->state != STATE_STOPPED)
752 return DSERR_INVALIDCALL;
754 if (This->buffer->lockedbytes > 0)
755 return DSERR_INVALIDCALL;
757 if (dwEffectsCount == 0) {
758 if (This->num_filters > 0) {
759 for (u = 0; u < This->num_filters; u++) {
760 IMediaObject_Release(This->filters[u].obj);
762 free(This->filters);
764 This->filters = NULL;
765 This->num_filters = 0;
768 return DS_OK;
771 filters = malloc(dwEffectsCount * sizeof(DSFilter));
772 if (!filters) {
773 WARN("out of memory\n");
774 return DSERR_OUTOFMEMORY;
777 hr = DS_OK;
779 wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
780 wfx.nChannels = This->pwfx->nChannels;
781 wfx.nSamplesPerSec = This->pwfx->nSamplesPerSec;
782 wfx.wBitsPerSample = sizeof(float) * 8;
783 wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample)/8;
784 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
785 wfx.cbSize = sizeof(wfx);
787 dmt.majortype = KSDATAFORMAT_TYPE_AUDIO;
788 dmt.subtype = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
789 dmt.bFixedSizeSamples = TRUE;
790 dmt.bTemporalCompression = FALSE;
791 dmt.lSampleSize = sizeof(float) * This->pwfx->nChannels / 8;
792 dmt.formattype = FORMAT_WaveFormatEx;
793 dmt.pUnk = NULL;
794 dmt.cbFormat = sizeof(WAVEFORMATEX);
795 dmt.pbFormat = (BYTE*)&wfx;
797 for (u = 0; u < dwEffectsCount; u++) {
798 TRACE("%ld: 0x%08lx, %s\n", u, pDSFXDesc[u].dwFlags, dump_DSFX_guid(&pDSFXDesc[u]));
800 hr2 = CoCreateInstance(&pDSFXDesc[u].guidDSFXClass, NULL, CLSCTX_INPROC_SERVER, &IID_IMediaObject, (LPVOID*)&filters[u].obj);
802 if (SUCCEEDED(hr2)) {
803 hr2 = IMediaObject_SetInputType(filters[u].obj, 0, &dmt, 0);
804 if (FAILED(hr2))
805 WARN("Could not set DMO input type\n");
808 if (SUCCEEDED(hr2)) {
809 hr2 = IMediaObject_SetOutputType(filters[u].obj, 0, &dmt, 0);
810 if (FAILED(hr2))
811 WARN("Could not set DMO output type\n");
814 if (FAILED(hr2)) {
815 if (hr == DS_OK)
816 hr = hr2;
818 if (pdwResultCodes)
819 pdwResultCodes[u] = (hr2 == REGDB_E_CLASSNOTREG) ? DSFXR_UNKNOWN : DSFXR_FAILED;
820 } else {
821 if (pdwResultCodes)
822 pdwResultCodes[u] = DSFXR_LOCSOFTWARE;
826 if (FAILED(hr)) {
827 for (u = 0; u < dwEffectsCount; u++) {
828 if (pdwResultCodes)
829 pdwResultCodes[u] = (pdwResultCodes[u] != DSFXR_UNKNOWN) ? DSFXR_PRESENT : DSFXR_UNKNOWN;
831 if (filters[u].obj)
832 IMediaObject_Release(filters[u].obj);
835 free(filters);
836 } else {
837 if (This->num_filters > 0) {
838 for (u = 0; u < This->num_filters; u++) {
839 IMediaObject_Release(This->filters[u].obj);
840 if (This->filters[u].inplace) IMediaObjectInPlace_Release(This->filters[u].inplace);
842 free(This->filters);
845 for (u = 0; u < dwEffectsCount; u++) {
846 memcpy(&filters[u].guid, &pDSFXDesc[u].guidDSFXClass, sizeof(GUID));
847 if (FAILED(IMediaObject_QueryInterface(filters[u].obj, &IID_IMediaObjectInPlace, (void*)&filters[u].inplace)))
848 filters[u].inplace = NULL;
851 This->filters = filters;
852 This->num_filters = dwEffectsCount;
855 return hr;
858 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
859 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
861 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
862 DWORD u;
864 FIXME("(%p,%08lu,%lu,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
866 if (pdwResultCodes)
867 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
869 WARN("control unavailable\n");
870 return DS_OK;
873 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
874 REFGUID clsid, DWORD index, REFGUID iid, void **out)
876 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
877 DWORD i, count = 0;
879 TRACE("(%p,%s,%lu,%s,%p)\n", This, debugstr_guid(clsid), index, debugstr_guid(iid), out);
881 if (!out)
882 return E_INVALIDARG;
884 for (i = 0; i < This->num_filters; i++)
886 if (IsEqualGUID(clsid, &This->filters[i].guid) || IsEqualGUID(clsid, &GUID_All_Objects))
888 if (count++ == index)
889 return IMediaObject_QueryInterface(This->filters[i].obj, iid, out);
892 return DSERR_OBJECTNOTFOUND;
895 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
896 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
898 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
900 WARN("(%p) already initialized\n", This);
901 return DSERR_ALREADYINITIALIZED;
904 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
906 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
908 TRACE("(%p)->(%p)\n",This,caps);
910 if (caps == NULL) {
911 WARN("invalid parameter: caps == NULL\n");
912 return DSERR_INVALIDPARAM;
915 if (caps->dwSize < sizeof(*caps)) {
916 WARN("invalid parameter: caps->dwSize = %ld\n",caps->dwSize);
917 return DSERR_INVALIDPARAM;
920 caps->dwFlags = This->dsbd.dwFlags;
921 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
923 caps->dwBufferBytes = This->buflen;
925 /* According to windows, this is zero*/
926 caps->dwUnlockTransferRate = 0;
927 caps->dwPlayCpuOverhead = 0;
929 return DS_OK;
932 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
933 void **ppobj)
935 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
937 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
939 if (ppobj == NULL) {
940 WARN("invalid parameter\n");
941 return E_INVALIDARG;
944 *ppobj = NULL; /* assume failure */
946 if ( IsEqualGUID(riid, &IID_IUnknown) ||
947 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
948 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
949 IDirectSoundBuffer8_AddRef(iface);
950 *ppobj = iface;
951 return S_OK;
954 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
955 if(This->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY) {
956 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
957 *ppobj = &This->IDirectSoundNotify_iface;
958 return S_OK;
961 TRACE( "App requested IDirectSoundNotify without DSBCAPS_CTRLPOSITIONNOTIFY flag.\n");
962 return E_NOINTERFACE;
965 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
966 if(This->dsbd.dwFlags & DSBCAPS_CTRL3D){
967 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
968 *ppobj = &This->IDirectSound3DBuffer_iface;
969 return S_OK;
971 TRACE("app requested IDirectSound3DBuffer on non-3D secondary buffer\n");
972 return E_NOINTERFACE;
975 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
976 ERR("app requested IDirectSound3DListener on secondary buffer\n");
977 return E_NOINTERFACE;
980 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
981 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
982 *ppobj = &This->IKsPropertySet_iface;
983 return S_OK;
986 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
988 return E_NOINTERFACE;
991 static const IDirectSoundBuffer8Vtbl dsbvt =
993 IDirectSoundBufferImpl_QueryInterface,
994 IDirectSoundBufferImpl_AddRef,
995 IDirectSoundBufferImpl_Release,
996 IDirectSoundBufferImpl_GetCaps,
997 IDirectSoundBufferImpl_GetCurrentPosition,
998 IDirectSoundBufferImpl_GetFormat,
999 IDirectSoundBufferImpl_GetVolume,
1000 IDirectSoundBufferImpl_GetPan,
1001 IDirectSoundBufferImpl_GetFrequency,
1002 IDirectSoundBufferImpl_GetStatus,
1003 IDirectSoundBufferImpl_Initialize,
1004 IDirectSoundBufferImpl_Lock,
1005 IDirectSoundBufferImpl_Play,
1006 IDirectSoundBufferImpl_SetCurrentPosition,
1007 IDirectSoundBufferImpl_SetFormat,
1008 IDirectSoundBufferImpl_SetVolume,
1009 IDirectSoundBufferImpl_SetPan,
1010 IDirectSoundBufferImpl_SetFrequency,
1011 IDirectSoundBufferImpl_Stop,
1012 IDirectSoundBufferImpl_Unlock,
1013 IDirectSoundBufferImpl_Restore,
1014 IDirectSoundBufferImpl_SetFX,
1015 IDirectSoundBufferImpl_AcquireResources,
1016 IDirectSoundBufferImpl_GetObjectInPath
1019 HRESULT secondarybuffer_create(DirectSoundDevice *device, const DSBUFFERDESC *dsbd,
1020 IDirectSoundBuffer **buffer)
1022 IDirectSoundBufferImpl *dsb;
1023 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1024 HRESULT err = DS_OK;
1025 DWORD capf = 0;
1026 size_t bufsize;
1028 TRACE("(%p,%p,%p)\n", device, dsbd, buffer);
1030 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1031 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
1032 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1035 dsb = calloc(1, sizeof(*dsb));
1037 if (!dsb)
1038 return DSERR_OUTOFMEMORY;
1040 TRACE("Created buffer at %p\n", dsb);
1042 dsb->ref = 1;
1043 dsb->refn = 0;
1044 dsb->ref3D = 0;
1045 dsb->refiks = 0;
1046 dsb->numIfaces = 1;
1047 dsb->device = device;
1048 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
1049 dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
1050 dsb->IDirectSound3DBuffer_iface.lpVtbl = &ds3dbvt;
1051 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1053 /* size depends on version */
1054 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
1056 dsb->pwfx = DSOUND_CopyFormat(wfex);
1057 if (!dsb->pwfx) {
1058 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1059 return DSERR_OUTOFMEMORY;
1062 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1063 dsb->buflen = dsbd->dwBufferBytes +
1064 (dsbd->lpwfxFormat->nBlockAlign -
1065 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1066 else
1067 dsb->buflen = dsbd->dwBufferBytes;
1069 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1070 dsb->notifies = NULL;
1071 dsb->nrofnotifies = 0;
1073 /* Check necessary hardware mixing capabilities */
1074 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1075 else capf |= DSCAPS_SECONDARYMONO;
1076 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1077 else capf |= DSCAPS_SECONDARY8BIT;
1079 TRACE("capf = 0x%08lx, device->drvcaps.dwFlags = 0x%08lx\n", capf, device->drvcaps.dwFlags);
1081 /* Allocate an empty buffer */
1082 bufsize = (sizeof(*(dsb->buffer)) + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
1083 dsb->buffer = malloc(bufsize + dsb->buflen);
1084 if (!dsb->buffer) {
1085 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1086 return DSERR_OUTOFMEMORY;
1089 /* Allocate system memory for buffer */
1090 dsb->buffer->memory = (BYTE *)dsb->buffer + bufsize;
1092 dsb->buffer->ref = 1;
1093 dsb->buffer->lockedbytes = 0;
1094 list_init(&dsb->buffer->buffers);
1095 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1096 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1098 /* It's not necessary to initialize values to zero since */
1099 /* we allocated this structure with calloc... */
1100 dsb->sec_mixpos = 0;
1101 dsb->state = STATE_STOPPED;
1103 /* calculate fragment size and write lead */
1104 DSOUND_RecalcFormat(dsb);
1106 dsb->committedbuff = malloc(dsb->maxwritelead);
1107 if(!dsb->committedbuff) {
1108 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1109 return DSERR_OUTOFMEMORY;
1112 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1113 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1114 dsb->ds3db_ds3db.vPosition.x = 0.0;
1115 dsb->ds3db_ds3db.vPosition.y = 0.0;
1116 dsb->ds3db_ds3db.vPosition.z = 0.0;
1117 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1118 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1119 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1120 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1121 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1122 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1123 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1124 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1125 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1126 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1127 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1128 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1130 dsb->ds3db_need_recalc = FALSE;
1131 DSOUND_Calc3DBuffer(dsb);
1132 } else
1133 DSOUND_RecalcVolPan(&(dsb->volpan));
1135 InitializeSRWLock(&dsb->lock);
1137 /* register buffer */
1138 err = DirectSoundDevice_AddBuffer(device, dsb);
1139 if (err == DS_OK)
1140 *buffer = (IDirectSoundBuffer*)&dsb->IDirectSoundBuffer8_iface;
1141 else
1142 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1144 return err;
1147 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
1149 ULONG ref = InterlockedIncrement(&This->numIfaces);
1151 if (ref > 1)
1152 WARN("Destroying buffer with %lu in use interfaces\n", ref - 1);
1154 if (This->dsbd.dwFlags & DSBCAPS_LOCHARDWARE)
1155 This->device->drvcaps.dwFreeHwMixingAllBuffers++;
1157 DirectSoundDevice_RemoveBuffer(This->device, This);
1159 This->buffer->ref--;
1160 list_remove(&This->entry);
1161 if (This->buffer->ref == 0)
1162 free(This->buffer);
1164 free(This->notifies);
1165 free(This->pwfx);
1166 free(This->committedbuff);
1168 if (This->filters) {
1169 int i;
1170 for (i = 0; i < This->num_filters; i++) {
1171 IMediaObject_Release(This->filters[i].obj);
1172 if (This->filters[i].inplace) IMediaObjectInPlace_Release(This->filters[i].inplace);
1174 free(This->filters);
1177 TRACE("(%p) released\n", This);
1179 free(This);
1182 BOOL secondarybuffer_is_audible(IDirectSoundBufferImpl *This)
1184 UINT i;
1185 for (i = 0; i < This->device->pwfx->nChannels; i++) {
1186 if (This->volpan.dwTotalAmpFactor[i] != 0)
1187 return TRUE;
1190 return FALSE;
1193 HRESULT IDirectSoundBufferImpl_Duplicate(
1194 DirectSoundDevice *device,
1195 IDirectSoundBufferImpl **ppdsb,
1196 IDirectSoundBufferImpl *pdsb)
1198 IDirectSoundBufferImpl *dsb;
1199 HRESULT hres = DS_OK;
1200 VOID *committedbuff;
1201 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
1203 dsb = malloc(sizeof(*dsb));
1204 if (dsb == NULL) {
1205 WARN("out of memory\n");
1206 *ppdsb = NULL;
1207 return DSERR_OUTOFMEMORY;
1210 committedbuff = malloc(pdsb->maxwritelead);
1211 if (committedbuff == NULL) {
1212 free(dsb);
1213 *ppdsb = NULL;
1214 return DSERR_OUTOFMEMORY;
1217 AcquireSRWLockShared(&pdsb->lock);
1219 CopyMemory(dsb, pdsb, sizeof(*dsb));
1221 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1223 ReleaseSRWLockShared(&pdsb->lock);
1225 if (dsb->pwfx == NULL) {
1226 free(committedbuff);
1227 free(dsb);
1228 *ppdsb = NULL;
1229 return DSERR_OUTOFMEMORY;
1232 dsb->buffer->ref++;
1233 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1234 dsb->ref = 0;
1235 dsb->refn = 0;
1236 dsb->ref3D = 0;
1237 dsb->refiks = 0;
1238 dsb->numIfaces = 0;
1239 dsb->state = STATE_STOPPED;
1240 dsb->sec_mixpos = 0;
1241 dsb->notifies = NULL;
1242 dsb->nrofnotifies = 0;
1243 dsb->device = device;
1244 dsb->committedbuff = committedbuff;
1245 dsb->use_committed = FALSE;
1246 dsb->committed_mixpos = 0;
1247 DSOUND_RecalcFormat(dsb);
1249 InitializeSRWLock(&dsb->lock);
1251 /* register buffer */
1252 hres = DirectSoundDevice_AddBuffer(device, dsb);
1253 if (hres != DS_OK) {
1254 list_remove(&dsb->entry);
1255 dsb->buffer->ref--;
1256 free(dsb->pwfx);
1257 free(dsb->committedbuff);
1258 free(dsb);
1259 dsb = NULL;
1260 }else
1261 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1263 *ppdsb = dsb;
1264 return hres;
1267 /*******************************************************************************
1268 * IKsPropertySet
1271 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1273 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1276 /* IUnknown methods */
1277 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1278 void **ppobj)
1280 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1282 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1284 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1287 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1289 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1290 ULONG ref = InterlockedIncrement(&This->refiks);
1292 TRACE("(%p) ref %ld\n", This, ref);
1294 if(ref == 1)
1295 InterlockedIncrement(&This->numIfaces);
1297 return ref;
1300 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1302 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1303 ULONG ref;
1305 if (is_primary_buffer(This)){
1306 ref = capped_refcount_dec(&This->refiks);
1307 if(!ref)
1308 capped_refcount_dec(&This->numIfaces);
1309 TRACE("(%p) ref %ld\n", This, ref);
1310 return ref;
1313 ref = InterlockedDecrement(&This->refiks);
1314 if (!ref && !InterlockedDecrement(&This->numIfaces))
1315 secondarybuffer_destroy(This);
1317 TRACE("(%p) ref %ld\n", This, ref);
1319 return ref;
1322 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1323 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1324 ULONG cbPropData, ULONG *pcbReturned)
1326 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1328 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%ld,pInstanceData=%p,cbInstanceData=%ld,pPropData=%p,cbPropData=%ld,pcbReturned=%p)\n",
1329 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1331 return E_PROP_ID_UNSUPPORTED;
1334 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1335 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1336 ULONG cbPropData)
1338 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1340 TRACE("(%p,%s,%ld,%p,%ld,%p,%ld)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1342 return E_PROP_ID_UNSUPPORTED;
1345 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1346 ULONG dwPropID, ULONG *pTypeSupport)
1348 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1350 TRACE("(%p,%s,%ld,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1352 return E_PROP_ID_UNSUPPORTED;
1355 const IKsPropertySetVtbl iksbvt = {
1356 IKsPropertySetImpl_QueryInterface,
1357 IKsPropertySetImpl_AddRef,
1358 IKsPropertySetImpl_Release,
1359 IKsPropertySetImpl_Get,
1360 IKsPropertySetImpl_Set,
1361 IKsPropertySetImpl_QuerySupport