3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
26 #include <sys/fcntl.h>
32 #include <math.h> /* Insomnia - pow() function */
42 #include "wine/windef16.h"
43 #include "wine/debug.h"
46 #include "dsound_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
50 /*******************************************************************************
53 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
54 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
56 ICOM_THIS(IDirectSoundNotifyImpl,iface);
58 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
59 return IDirectSoundBuffer8_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
62 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
63 ICOM_THIS(IDirectSoundNotifyImpl,iface);
66 TRACE("(%p) ref was %ld\n", This, This->ref);
68 ref = InterlockedIncrement(&(This->ref));
72 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
73 ICOM_THIS(IDirectSoundNotifyImpl,iface);
76 TRACE("(%p) ref was %ld\n", This, This->ref);
78 ref = InterlockedDecrement(&(This->ref));
80 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
81 HeapFree(GetProcessHeap(),0,This);
87 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
88 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
90 ICOM_THIS(IDirectSoundNotifyImpl,iface);
93 if (TRACE_ON(dsound)) {
94 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
95 for (i=0;i<howmuch;i++)
96 TRACE("notify at %ld to 0x%08lx\n",
97 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
99 This->dsb->notifies = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->dsb->notifies,(This->dsb->nrofnotifies+howmuch)*sizeof(DSBPOSITIONNOTIFY));
100 memcpy( This->dsb->notifies+This->dsb->nrofnotifies,
102 howmuch*sizeof(DSBPOSITIONNOTIFY)
104 This->dsb->nrofnotifies+=howmuch;
109 static ICOM_VTABLE(IDirectSoundNotify) dsnvt =
111 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
112 IDirectSoundNotifyImpl_QueryInterface,
113 IDirectSoundNotifyImpl_AddRef,
114 IDirectSoundNotifyImpl_Release,
115 IDirectSoundNotifyImpl_SetNotificationPositions,
118 /*******************************************************************************
122 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
123 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX wfex
125 ICOM_THIS(IDirectSoundBufferImpl,iface);
127 TRACE("(%p,%p)\n",This,wfex);
128 /* This method is not available on secondary buffers */
129 return DSERR_INVALIDCALL;
132 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
133 LPDIRECTSOUNDBUFFER8 iface,LONG vol
135 ICOM_THIS(IDirectSoundBufferImpl,iface);
138 TRACE("(%p,%ld)\n",This,vol);
140 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME))
141 return DSERR_CONTROLUNAVAIL;
143 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN))
144 return DSERR_INVALIDPARAM;
147 EnterCriticalSection(&(This->lock));
149 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
150 oldVol = This->ds3db->lVolume;
151 This->ds3db->lVolume = vol;
153 oldVol = This->volpan.lVolume;
154 This->volpan.lVolume = vol;
155 if (vol != oldVol) DSOUND_RecalcVolPan(&(This->volpan));
160 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
162 else DSOUND_ForceRemix(This);
165 LeaveCriticalSection(&(This->lock));
171 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
172 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
174 ICOM_THIS(IDirectSoundBufferImpl,iface);
175 TRACE("(%p,%p)\n",This,vol);
178 return DSERR_INVALIDPARAM;
180 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D)
181 *vol = This->ds3db->lVolume;
183 *vol = This->volpan.lVolume;
187 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
188 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
190 ICOM_THIS(IDirectSoundBufferImpl,iface);
193 TRACE("(%p,%ld)\n",This,freq);
195 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY))
196 return DSERR_CONTROLUNAVAIL;
198 if (freq == DSBFREQUENCY_ORIGINAL)
199 freq = This->wfx.nSamplesPerSec;
201 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX))
202 return DSERR_INVALIDPARAM;
205 EnterCriticalSection(&(This->lock));
207 oldFreq = This->freq;
209 if (freq != oldFreq) {
210 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->dsound->wfx.nSamplesPerSec;
211 This->nAvgBytesPerSec = freq * This->wfx.nBlockAlign;
212 DSOUND_RecalcFormat(This);
213 if (!This->hwbuf) DSOUND_ForceRemix(This);
216 LeaveCriticalSection(&(This->lock));
222 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
223 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
225 ICOM_THIS(IDirectSoundBufferImpl,iface);
226 TRACE("(%p,%08lx,%08lx,%08lx)\n",
227 This,reserved1,reserved2,flags
231 EnterCriticalSection(&(This->lock));
233 This->playflags = flags;
234 if (This->state == STATE_STOPPED) {
236 This->startpos = This->buf_mixpos;
237 This->state = STATE_STARTING;
238 } else if (This->state == STATE_STOPPING)
239 This->state = STATE_PLAYING;
241 IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
242 This->state = STATE_PLAYING;
245 LeaveCriticalSection(&(This->lock));
251 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
253 ICOM_THIS(IDirectSoundBufferImpl,iface);
254 TRACE("(%p)\n",This);
257 EnterCriticalSection(&(This->lock));
259 if (This->state == STATE_PLAYING)
260 This->state = STATE_STOPPING;
261 else if (This->state == STATE_STARTING)
262 This->state = STATE_STOPPED;
264 IDsDriverBuffer_Stop(This->hwbuf);
265 This->state = STATE_STOPPED;
267 DSOUND_CheckEvent(This, 0);
269 LeaveCriticalSection(&(This->lock));
275 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface) {
276 ICOM_THIS(IDirectSoundBufferImpl,iface);
279 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
281 ref = InterlockedIncrement(&(This->ref));
283 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
287 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface) {
288 ICOM_THIS(IDirectSoundBufferImpl,iface);
292 TRACE("(%p) ref was %ld, thread is %lx\n",This, This->ref, GetCurrentThreadId());
294 ref = InterlockedDecrement(&(This->ref));
297 RtlAcquireResourceExclusive(&(This->dsound->lock), TRUE);
298 for (i=0;i<This->dsound->nrofbuffers;i++)
299 if (This->dsound->buffers[i] == This)
302 if (i < This->dsound->nrofbuffers) {
303 /* Put the last buffer of the list in the (now empty) position */
304 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
305 This->dsound->nrofbuffers--;
306 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER8)*This->dsound->nrofbuffers);
307 TRACE("buffer count is now %d\n", This->dsound->nrofbuffers);
308 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
310 RtlReleaseResource(&(This->dsound->lock));
312 DeleteCriticalSection(&(This->lock));
314 IDsDriverBuffer_Release(This->hwbuf);
315 if (This->dsound->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)
316 HeapFree(GetProcessHeap(),0,This->buffer);
318 else if (!This->parent)
319 HeapFree(GetProcessHeap(),0,This->buffer);
321 DeleteCriticalSection(&This->ds3db->lock);
322 HeapFree(GetProcessHeap(), 0, This->ds3db);
325 HeapFree(GetProcessHeap(), 0, This->iks);
328 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->parent);
330 HeapFree(GetProcessHeap(),0,This);
335 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This,
336 DWORD state, DWORD pplay, DWORD pwrite, DWORD pmix, DWORD bmix)
340 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, pmix);
341 TRACE("this mixpos=%ld, time=%ld\n", bmix, GetTickCount());
343 /* the actual primary play position (pplay) is always behind last mixed (pmix),
344 * unless the computer is too slow or something */
345 /* we need to know how far away we are from there */
346 #if 0 /* we'll never fill the primary entirely */
348 if ((state == STATE_PLAYING) || (state == STATE_STOPPING)) {
349 /* wow, the software mixer is really doing well,
350 * seems the entire primary buffer is filled! */
351 pmix += This->dsound->buflen;
353 /* else: the primary buffer is not playing, so probably empty */
356 if (pmix < pplay) pmix += This->dsound->buflen; /* wraparound */
358 /* detect buffer underrun */
359 if (pwrite < pplay) pwrite += This->dsound->buflen; /* wraparound */
361 if (pmix > (ds_snd_queue_max * This->dsound->fraglen + pwrite + This->dsound->writelead)) {
362 WARN("detected an underrun: primary queue was %ld\n",pmix);
365 /* divide the offset by its sample size */
366 pmix /= This->dsound->wfx.nBlockAlign;
367 TRACE("primary back-samples=%ld\n",pmix);
368 /* adjust for our frequency */
369 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
370 /* multiply by our own sample size */
371 pmix *= This->wfx.nBlockAlign;
372 TRACE("this back-offset=%ld\n", pmix);
373 /* subtract from our last mixed position */
375 while (bplay < pmix) bplay += This->buflen; /* wraparound */
377 if (This->leadin && ((bplay < This->startpos) || (bplay > bmix))) {
378 /* seems we haven't started playing yet */
379 TRACE("this still in lead-in phase\n");
380 bplay = This->startpos;
382 /* return the result */
386 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
387 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
390 ICOM_THIS(IDirectSoundBufferImpl,iface);
391 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
393 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
398 if (playpos && (This->state != STATE_PLAYING)) {
399 /* we haven't been merged into the primary buffer (yet) */
400 *playpos = This->buf_mixpos;
403 DWORD pplay, pwrite, lplay, splay, pstate;
404 /* let's get this exact; first, recursively call GetPosition on the primary */
405 EnterCriticalSection(&(This->dsound->mixlock));
406 DSOUND_PrimaryGetPosition(This->dsound, &pplay, &pwrite);
407 /* detect HEL mode underrun */
408 pstate = This->dsound->state;
409 if (!(This->dsound->hwbuf || This->dsound->pwqueue)) {
410 TRACE("detected an underrun\n");
412 if (pstate == STATE_PLAYING)
413 pstate = STATE_STARTING;
414 else if (pstate == STATE_STOPPING)
415 pstate = STATE_STOPPED;
417 /* get data for ourselves while we still have the lock */
418 pstate &= This->state;
419 lplay = This->primary_mixpos;
420 splay = This->buf_mixpos;
421 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->dsound->hwbuf) {
422 /* calculate play position using this */
423 *playpos = DSOUND_CalcPlayPosition(This, pstate, pplay, pwrite, lplay, splay);
425 /* (unless the app isn't using GETCURRENTPOSITION2) */
426 /* don't know exactly how this should be handled...
427 * the docs says that play cursor is reported as directly
428 * behind write cursor, hmm... */
429 /* let's just do what might work for Half-Life */
431 wp = (This->dsound->pwplay + ds_hel_margin) * This->dsound->fraglen;
432 while (wp >= This->dsound->buflen)
433 wp -= This->dsound->buflen;
434 *playpos = DSOUND_CalcPlayPosition(This, pstate, wp, pwrite, lplay, splay);
436 LeaveCriticalSection(&(This->dsound->mixlock));
438 if (writepos) *writepos = This->buf_mixpos;
441 if (This->state != STATE_STOPPED)
442 /* apply the documented 10ms lead to writepos */
443 *writepos += This->writelead;
444 while (*writepos >= This->buflen) *writepos -= This->buflen;
446 if (playpos) This->last_playpos = *playpos;
447 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
451 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
452 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
454 ICOM_THIS(IDirectSoundBufferImpl,iface);
455 TRACE("(%p,%p), thread is %lx\n",This,status,GetCurrentThreadId());
458 return DSERR_INVALIDPARAM;
461 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
462 *status |= DSBSTATUS_PLAYING;
463 if (This->playflags & DSBPLAY_LOOPING)
464 *status |= DSBSTATUS_LOOPING;
467 TRACE("status=%lx\n", *status);
472 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
473 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
475 ICOM_THIS(IDirectSoundBufferImpl,iface);
476 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
478 if (wfsize>sizeof(This->wfx))
479 wfsize = sizeof(This->wfx);
480 if (lpwf) { /* NULL is valid */
481 memcpy(lpwf,&(This->wfx),wfsize);
486 *wfwritten = sizeof(This->wfx);
488 return DSERR_INVALIDPARAM;
493 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
494 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
496 ICOM_THIS(IDirectSoundBufferImpl,iface);
498 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
510 if (flags & DSBLOCK_FROMWRITECURSOR) {
512 /* GetCurrentPosition does too much magic to duplicate here */
513 IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
514 writecursor += writepos;
516 while (writecursor >= This->buflen)
517 writecursor -= This->buflen;
518 if (flags & DSBLOCK_ENTIREBUFFER)
519 writebytes = This->buflen;
520 if (writebytes > This->buflen)
521 writebytes = This->buflen;
523 assert(audiobytes1!=audiobytes2);
524 assert(lplpaudioptr1!=lplpaudioptr2);
526 if ((writebytes == This->buflen) &&
527 ((This->state == STATE_STARTING) ||
528 (This->state == STATE_PLAYING)))
529 /* some games, like Half-Life, try to be clever (not) and
530 * keep one secondary buffer, and mix sounds into it itself,
531 * locking the entire buffer every time... so we can just forget
532 * about tracking the last-written-to-position... */
533 This->probably_valid_to = (DWORD)-1;
535 This->probably_valid_to = writecursor;
537 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
538 IDsDriverBuffer_Lock(This->hwbuf,
539 lplpaudioptr1, audiobytes1,
540 lplpaudioptr2, audiobytes2,
541 writecursor, writebytes,
546 if (writecursor+writebytes <= This->buflen) {
547 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
548 *audiobytes1 = writebytes;
550 *(LPBYTE*)lplpaudioptr2 = NULL;
553 TRACE("->%ld.0\n",writebytes);
555 *(LPBYTE*)lplpaudioptr1 = This->buffer+writecursor;
556 *audiobytes1 = This->buflen-writecursor;
558 *(LPBYTE*)lplpaudioptr2 = This->buffer;
560 *audiobytes2 = writebytes-(This->buflen-writecursor);
561 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
563 if (This->state == STATE_PLAYING) {
564 /* if the segment between playpos and buf_mixpos is touched,
565 * we need to cancel some mixing */
566 /* we'll assume that the app always calls GetCurrentPosition before
567 * locking a playing buffer, so that last_playpos is up-to-date */
568 if (This->buf_mixpos >= This->last_playpos) {
569 if (This->buf_mixpos > writecursor &&
570 This->last_playpos < writecursor+writebytes)
574 if (This->buf_mixpos > writecursor ||
575 This->last_playpos < writecursor+writebytes)
579 TRACE("locking prebuffered region, ouch\n");
580 DSOUND_MixCancelAt(This, writecursor);
587 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
588 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
590 ICOM_THIS(IDirectSoundBufferImpl,iface);
591 TRACE("(%p,%ld)\n",This,newpos);
594 EnterCriticalSection(&(This->lock));
596 while (newpos >= This->buflen)
597 newpos -= This->buflen;
598 This->buf_mixpos = newpos;
600 IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
602 LeaveCriticalSection(&(This->lock));
608 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
609 LPDIRECTSOUNDBUFFER8 iface,LONG pan
611 ICOM_THIS(IDirectSoundBufferImpl,iface);
614 TRACE("(%p,%ld)\n",This,pan);
616 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT))
617 return DSERR_INVALIDPARAM;
619 /* You cannot use both pan and 3D controls */
620 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
621 (This->dsbd.dwFlags & DSBCAPS_CTRL3D))
622 return DSERR_CONTROLUNAVAIL;
625 EnterCriticalSection(&(This->lock));
627 oldPan = This->volpan.lPan;
628 This->volpan.lPan = pan;
631 DSOUND_RecalcVolPan(&(This->volpan));
634 IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
636 else DSOUND_ForceRemix(This);
639 LeaveCriticalSection(&(This->lock));
645 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
646 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
648 ICOM_THIS(IDirectSoundBufferImpl,iface);
649 TRACE("(%p,%p)\n",This,pan);
652 return DSERR_INVALIDPARAM;
654 *pan = This->volpan.lPan;
659 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
660 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
662 ICOM_THIS(IDirectSoundBufferImpl,iface);
663 DWORD probably_valid_to;
665 TRACE("(%p,%p,%ld,%p,%ld):stub\n", This,p1,x1,p2,x2);
668 /* Preprocess 3D buffers... */
670 /* This is highly experimental and liable to break things */
671 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D)
672 DSOUND_Create3DBuffer(This);
675 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
676 IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
679 if (p2) probably_valid_to = (((LPBYTE)p2)-This->buffer) + x2;
680 else probably_valid_to = (((LPBYTE)p1)-This->buffer) + x1;
681 while (probably_valid_to >= This->buflen)
682 probably_valid_to -= This->buflen;
683 if ((probably_valid_to == 0) && ((x1+x2) == This->buflen) &&
684 ((This->state == STATE_STARTING) ||
685 (This->state == STATE_PLAYING)))
686 /* see IDirectSoundBufferImpl_Lock */
687 probably_valid_to = (DWORD)-1;
688 This->probably_valid_to = probably_valid_to;
693 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
694 LPDIRECTSOUNDBUFFER8 iface
696 ICOM_THIS(IDirectSoundBufferImpl,iface);
697 FIXME("(%p):stub\n",This);
701 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
702 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
704 ICOM_THIS(IDirectSoundBufferImpl,iface);
705 TRACE("(%p,%p)\n",This,freq);
708 return DSERR_INVALIDPARAM;
711 TRACE("-> %ld\n", *freq);
716 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
717 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
719 ICOM_THIS(IDirectSoundBufferImpl,iface);
722 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
725 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
727 return DSERR_CONTROLUNAVAIL;
730 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
731 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
733 ICOM_THIS(IDirectSoundBufferImpl,iface);
736 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
739 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
741 return DSERR_CONTROLUNAVAIL;
744 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
745 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
747 ICOM_THIS(IDirectSoundBufferImpl,iface);
749 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
751 return DSERR_CONTROLUNAVAIL;
754 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
755 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND8 dsound,LPDSBUFFERDESC dbsd
757 ICOM_THIS(IDirectSoundBufferImpl,iface);
758 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
759 DPRINTF("Re-Init!!!\n");
760 return DSERR_ALREADYINITIALIZED;
763 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
764 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
766 ICOM_THIS(IDirectSoundBufferImpl,iface);
767 TRACE("(%p)->(%p)\n",This,caps);
769 if (caps == NULL || caps->dwSize!=sizeof(*caps))
770 return DSERR_INVALIDPARAM;
772 caps->dwFlags = This->dsbd.dwFlags;
773 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
774 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
776 caps->dwBufferBytes = This->buflen;
778 /* This value represents the speed of the "unlock" command.
779 As unlock is quite fast (it does not do anything), I put
780 4096 ko/s = 4 Mo / s */
781 /* FIXME: hwbuf speed */
782 caps->dwUnlockTransferRate = 4096;
783 caps->dwPlayCpuOverhead = 0;
788 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
789 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
791 ICOM_THIS(IDirectSoundBufferImpl,iface);
793 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
795 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
796 IDirectSoundNotifyImpl *dsn;
798 dsn = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsn));
801 IDirectSoundBuffer8_AddRef(iface);
802 ICOM_VTBL(dsn) = &dsnvt;
803 *ppobj = (LPVOID)dsn;
807 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
809 IDirectSound3DBufferImpl_Create(This, &This->ds3db);
810 *ppobj = This->ds3db;
812 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)*ppobj);
818 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
819 ERR("app requested IDirectSound3DListener on secondary buffer\n");
824 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
826 IKsPropertySetImpl_Create(This, &This->iks);
829 IKsPropertySet_AddRef((LPKSPROPERTYSET)*ppobj);
835 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
839 return E_NOINTERFACE;
842 static ICOM_VTABLE(IDirectSoundBuffer8) dsbvt =
844 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
845 IDirectSoundBufferImpl_QueryInterface,
846 IDirectSoundBufferImpl_AddRef,
847 IDirectSoundBufferImpl_Release,
848 IDirectSoundBufferImpl_GetCaps,
849 IDirectSoundBufferImpl_GetCurrentPosition,
850 IDirectSoundBufferImpl_GetFormat,
851 IDirectSoundBufferImpl_GetVolume,
852 IDirectSoundBufferImpl_GetPan,
853 IDirectSoundBufferImpl_GetFrequency,
854 IDirectSoundBufferImpl_GetStatus,
855 IDirectSoundBufferImpl_Initialize,
856 IDirectSoundBufferImpl_Lock,
857 IDirectSoundBufferImpl_Play,
858 IDirectSoundBufferImpl_SetCurrentPosition,
859 IDirectSoundBufferImpl_SetFormat,
860 IDirectSoundBufferImpl_SetVolume,
861 IDirectSoundBufferImpl_SetPan,
862 IDirectSoundBufferImpl_SetFrequency,
863 IDirectSoundBufferImpl_Stop,
864 IDirectSoundBufferImpl_Unlock,
865 IDirectSoundBufferImpl_Restore,
866 IDirectSoundBufferImpl_SetFX,
867 IDirectSoundBufferImpl_AcquireResources,
868 IDirectSoundBufferImpl_GetObjectInPath
871 HRESULT WINAPI SecondaryBuffer_Create(
872 IDirectSoundImpl *This,
873 IDirectSoundBufferImpl **pdsb,
876 IDirectSoundBufferImpl *dsb;
877 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
882 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
883 ERR("invalid sound buffer size %ld\n", dsbd->dwBufferBytes);
884 return DSERR_INVALIDPARAM; /* FIXME: which error? */
887 dsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
891 ICOM_VTBL(dsb) = &dsbvt;
893 memcpy(&dsb->dsbd, dsbd, sizeof(*dsbd));
895 memcpy(&dsb->wfx, wfex, sizeof(dsb->wfx));
897 TRACE("Created buffer at %p\n", dsb);
899 dsb->buflen = dsbd->dwBufferBytes;
900 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
902 /* Check necessary hardware mixing capabilities */
903 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
904 else capf |= DSCAPS_SECONDARYMONO;
905 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
906 else capf |= DSCAPS_SECONDARY8BIT;
907 use_hw = (This->drvcaps.dwFlags & capf) == capf;
909 /* FIXME: check hardware sample rate mixing capabilities */
910 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
911 /* FIXME: check whether any hardware buffers are left */
912 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
914 /* Allocate system memory if applicable */
915 if ((This->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
916 dsb->buffer = (LPBYTE)HeapAlloc(GetProcessHeap(),0,dsb->buflen);
917 if (dsb->buffer == NULL)
918 err = DSERR_OUTOFMEMORY;
921 /* Allocate the hardware buffer */
922 if (use_hw && (err == DS_OK)) {
923 err = IDsDriver_CreateSoundBuffer(This->driver,wfex,dsbd->dwFlags,0,
924 &(dsb->buflen),&(dsb->buffer),
925 (LPVOID*)&(dsb->hwbuf));
930 HeapFree(GetProcessHeap(),0,dsb->buffer);
931 HeapFree(GetProcessHeap(),0,dsb);
935 /* calculate fragment size and write lead */
936 DSOUND_RecalcFormat(dsb);
938 /* It's not necessary to initialize values to zero since */
939 /* we allocated this structure with HEAP_ZERO_MEMORY... */
942 dsb->state = STATE_STOPPED;
944 dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
945 This->wfx.nSamplesPerSec;
946 dsb->nAvgBytesPerSec = dsb->freq *
947 dsbd->lpwfxFormat->nBlockAlign;
949 if (dsbd->dwFlags & DSBCAPS_CTRL3D) {
950 IDirectSound3DBufferImpl_Create(dsb, &dsb->ds3db);
953 DSOUND_RecalcVolPan(&(dsb->volpan));
955 InitializeCriticalSection(&(dsb->lock));
957 /* register buffer */
958 RtlAcquireResourceExclusive(&(This->lock), TRUE);
959 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
960 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl*)*(This->nrofbuffers+1));
962 This->buffers = newbuffers;
963 This->buffers[This->nrofbuffers] = dsb;
965 TRACE("buffer count is now %d\n", This->nrofbuffers);
967 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
968 err = DSERR_OUTOFMEMORY;
971 RtlReleaseResource(&(This->lock));
972 IDirectSound8_AddRef((LPDIRECTSOUND8)This);
976 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)dsb);