Fix signed/unsigned comparison warnings.
[wine.git] / dlls / dsound / buffer.c
blob67ab7a8af87fae46aa5a2717659b680aaa8d5400
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
35 #define NONAMELESSSTRUCT
36 #define NONAMELESSUNION
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winerror.h"
42 #include "mmsystem.h"
43 #include "winreg.h"
44 #include "winternl.h"
45 #include "mmddk.h"
46 #include "wine/windef16.h"
47 #include "wine/debug.h"
48 #include "dsound.h"
49 #include "dsdriver.h"
50 #include "dsound_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
54 /*******************************************************************************
55 * IDirectSoundNotify
57 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
58 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
59 ) {
60 ICOM_THIS(IDirectSoundNotifyImpl,iface);
61 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
63 if (This->dsb == NULL) {
64 WARN("invalid parameter\n");
65 return E_INVALIDARG;
68 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
71 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
72 ICOM_THIS(IDirectSoundNotifyImpl,iface);
73 DWORD ref;
75 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
77 ref = InterlockedIncrement(&(This->ref));
78 return ref;
81 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
82 ICOM_THIS(IDirectSoundNotifyImpl,iface);
83 DWORD ref;
85 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
87 ref = InterlockedDecrement(&(This->ref));
88 if (ref == 0) {
89 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
90 This->dsb->notify = NULL;
91 HeapFree(GetProcessHeap(),0,This);
92 TRACE("(%p) released\n",This);
94 return ref;
97 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
98 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
99 ) {
100 ICOM_THIS(IDirectSoundNotifyImpl,iface);
101 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
103 if (howmuch > 0 && notify == NULL) {
104 WARN("invalid parameter: notify == NULL\n");
105 return DSERR_INVALIDPARAM;
108 if (TRACE_ON(dsound)) {
109 unsigned int i;
110 for (i=0;i<howmuch;i++)
111 TRACE("notify at %ld to 0x%08lx\n",
112 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
115 if (This->dsb->hwnotify) {
116 HRESULT hres;
117 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
118 if (hres != DS_OK)
119 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
120 return hres;
121 } else if (howmuch > 0) {
122 /* Make an internal copy of the caller-supplied array.
123 * Replace the existing copy if one is already present. */
124 if (This->dsb->notifies)
125 This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
126 This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
127 else
128 This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
129 howmuch * sizeof(DSBPOSITIONNOTIFY));
131 if (This->dsb->notifies == NULL) {
132 WARN("out of memory\n");
133 return DSERR_OUTOFMEMORY;
135 memcpy(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
136 This->dsb->nrofnotifies = howmuch;
137 } else {
138 if (This->dsb->notifies) {
139 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
140 This->dsb->notifies = NULL;
142 This->dsb->nrofnotifies = 0;
145 return S_OK;
148 IDirectSoundNotifyVtbl dsnvt =
150 IDirectSoundNotifyImpl_QueryInterface,
151 IDirectSoundNotifyImpl_AddRef,
152 IDirectSoundNotifyImpl_Release,
153 IDirectSoundNotifyImpl_SetNotificationPositions,
156 HRESULT WINAPI IDirectSoundNotifyImpl_Create(
157 IDirectSoundBufferImpl * dsb,
158 IDirectSoundNotifyImpl **pdsn)
160 IDirectSoundNotifyImpl * dsn;
161 TRACE("(%p,%p)\n",dsb,pdsn);
163 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
165 if (dsn == NULL) {
166 WARN("out of memory\n");
167 return DSERR_OUTOFMEMORY;
170 dsn->ref = 0;
171 dsn->lpVtbl = &dsnvt;
172 dsn->dsb = dsb;
173 dsb->notify = dsn;
174 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
176 *pdsn = dsn;
177 return DS_OK;
180 HRESULT WINAPI IDirectSoundNotifyImpl_Destroy(
181 IDirectSoundNotifyImpl *pdsn)
183 TRACE("(%p)\n",pdsn);
185 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
187 return DS_OK;
190 /*******************************************************************************
191 * IDirectSoundBuffer
194 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
195 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
197 ICOM_THIS(IDirectSoundBufferImpl,iface);
199 TRACE("(%p,%p)\n",This,wfex);
200 /* This method is not available on secondary buffers */
201 WARN("invalid call\n");
202 return DSERR_INVALIDCALL;
205 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
206 LPDIRECTSOUNDBUFFER8 iface,LONG vol
208 ICOM_THIS(IDirectSoundBufferImpl,iface);
209 LONG oldVol;
211 TRACE("(%p,%ld)\n",This,vol);
213 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
214 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This->dsbd.dwFlags);
215 return DSERR_CONTROLUNAVAIL;
218 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
219 WARN("invalid parameter: vol = %ld\n", vol);
220 return DSERR_INVALIDPARAM;
223 /* **** */
224 EnterCriticalSection(&(This->lock));
226 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
227 oldVol = This->ds3db_lVolume;
228 This->ds3db_lVolume = vol;
229 } else {
230 oldVol = This->volpan.lVolume;
231 This->volpan.lVolume = vol;
232 if (vol != oldVol)
233 DSOUND_RecalcVolPan(&(This->volpan));
236 if (vol != oldVol) {
237 if (This->hwbuf) {
238 HRESULT hres;
239 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
240 if (hres != DS_OK)
241 WARN("IDsDriverBuffer_SetVolumePan failed\n");
242 } else
243 DSOUND_ForceRemix(This);
246 LeaveCriticalSection(&(This->lock));
247 /* **** */
249 return DS_OK;
252 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
253 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
255 ICOM_THIS(IDirectSoundBufferImpl,iface);
256 TRACE("(%p,%p)\n",This,vol);
258 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
259 WARN("control unavailable\n");
260 return DSERR_CONTROLUNAVAIL;
263 if (vol == NULL) {
264 WARN("invalid parameter: vol == NULL\n");
265 return DSERR_INVALIDPARAM;
268 *vol = This->volpan.lVolume;
270 return DS_OK;
273 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
274 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
276 ICOM_THIS(IDirectSoundBufferImpl,iface);
277 DWORD oldFreq;
279 TRACE("(%p,%ld)\n",This,freq);
281 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
282 WARN("control unavailable\n");
283 return DSERR_CONTROLUNAVAIL;
286 if (freq == DSBFREQUENCY_ORIGINAL)
287 freq = This->pwfx->nSamplesPerSec;
289 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
290 WARN("invalid parameter: freq = %ld\n", freq);
291 return DSERR_INVALIDPARAM;
294 /* **** */
295 EnterCriticalSection(&(This->lock));
297 oldFreq = This->freq;
298 This->freq = freq;
299 if (freq != oldFreq) {
300 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->dsound->pwfx->nSamplesPerSec;
301 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
302 DSOUND_RecalcFormat(This);
303 if (!This->hwbuf)
304 DSOUND_ForceRemix(This);
307 LeaveCriticalSection(&(This->lock));
308 /* **** */
310 return DS_OK;
313 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
314 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
316 HRESULT hres = DS_OK;
317 ICOM_THIS(IDirectSoundBufferImpl,iface);
318 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
320 /* **** */
321 EnterCriticalSection(&(This->lock));
323 This->playflags = flags;
324 if (This->state == STATE_STOPPED) {
325 This->leadin = TRUE;
326 This->startpos = This->buf_mixpos;
327 This->state = STATE_STARTING;
328 } else if (This->state == STATE_STOPPING)
329 This->state = STATE_PLAYING;
330 if (This->hwbuf) {
331 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
332 if (hres != DS_OK)
333 WARN("IDsDriverBuffer_Play failed\n");
334 else
335 This->state = STATE_PLAYING;
338 LeaveCriticalSection(&(This->lock));
339 /* **** */
341 return hres;
344 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
346 HRESULT hres = DS_OK;
347 ICOM_THIS(IDirectSoundBufferImpl,iface);
348 TRACE("(%p)\n",This);
350 /* **** */
351 EnterCriticalSection(&(This->lock));
353 if (This->state == STATE_PLAYING)
354 This->state = STATE_STOPPING;
355 else if (This->state == STATE_STARTING)
356 This->state = STATE_STOPPED;
357 if (This->hwbuf) {
358 hres = IDsDriverBuffer_Stop(This->hwbuf);
359 if (hres != DS_OK)
360 WARN("IDsDriverBuffer_Stop failed\n");
361 else
362 This->state = STATE_STOPPED;
364 DSOUND_CheckEvent(This, 0);
366 LeaveCriticalSection(&(This->lock));
367 /* **** */
369 return hres;
372 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface) {
373 ICOM_THIS(IDirectSoundBufferImpl,iface);
374 DWORD ref;
376 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
378 ref = InterlockedIncrement(&(This->ref));
379 if (!ref) {
380 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
382 return ref;
385 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
387 ICOM_THIS(IDirectSoundBufferImpl,iface);
388 DWORD ref;
390 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
392 ref = InterlockedDecrement(&(This->ref));
393 if (ref) return ref;
395 DSOUND_RemoveBuffer(This->dsound, This);
397 DeleteCriticalSection(&(This->lock));
399 if (This->hwbuf) {
400 IDsDriverBuffer_Release(This->hwbuf);
401 if (This->dsound->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
402 This->buffer->ref--;
403 if (This->buffer->ref==0) {
404 HeapFree(GetProcessHeap(),0,This->buffer->memory);
405 HeapFree(GetProcessHeap(),0,This->buffer);
408 } else {
409 This->buffer->ref--;
410 if (This->buffer->ref==0) {
411 HeapFree(GetProcessHeap(),0,This->buffer->memory);
412 HeapFree(GetProcessHeap(),0,This->buffer);
416 if (This->notifies != NULL)
417 HeapFree(GetProcessHeap(), 0, This->notifies);
419 if (This->pwfx)
420 HeapFree(GetProcessHeap(), 0, This->pwfx);
422 HeapFree(GetProcessHeap(),0,This);
424 TRACE("(%p) released\n",This);
425 return 0;
428 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This,
429 DWORD state, DWORD pplay, DWORD pwrite, DWORD pmix, DWORD bmix)
431 DWORD bplay;
433 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, pmix);
434 TRACE("this mixpos=%ld, time=%ld\n", bmix, GetTickCount());
436 /* the actual primary play position (pplay) is always behind last mixed (pmix),
437 * unless the computer is too slow or something */
438 /* we need to know how far away we are from there */
439 #if 0 /* we'll never fill the primary entirely */
440 if (pmix == pplay) {
441 if ((state == STATE_PLAYING) || (state == STATE_STOPPING)) {
442 /* wow, the software mixer is really doing well,
443 * seems the entire primary buffer is filled! */
444 pmix += This->dsound->buflen;
446 /* else: the primary buffer is not playing, so probably empty */
448 #endif
449 if (pmix < pplay) pmix += This->dsound->buflen; /* wraparound */
450 pmix -= pplay;
451 /* detect buffer underrun */
452 if (pwrite < pplay) pwrite += This->dsound->buflen; /* wraparound */
453 pwrite -= pplay;
454 if (pmix > (ds_snd_queue_max * This->dsound->fraglen + pwrite + This->dsound->writelead)) {
455 WARN("detected an underrun: primary queue was %ld\n",pmix);
456 pmix = 0;
458 /* divide the offset by its sample size */
459 pmix /= This->dsound->pwfx->nBlockAlign;
460 TRACE("primary back-samples=%ld\n",pmix);
461 /* adjust for our frequency */
462 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
463 /* multiply by our own sample size */
464 pmix *= This->pwfx->nBlockAlign;
465 TRACE("this back-offset=%ld\n", pmix);
466 /* subtract from our last mixed position */
467 bplay = bmix;
468 while (bplay < pmix) bplay += This->buflen; /* wraparound */
469 bplay -= pmix;
470 if (This->leadin && ((bplay < This->startpos) || (bplay > bmix))) {
471 /* seems we haven't started playing yet */
472 TRACE("this still in lead-in phase\n");
473 bplay = This->startpos;
475 /* return the result */
476 return bplay;
479 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
480 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
482 HRESULT hres;
483 ICOM_THIS(IDirectSoundBufferImpl,iface);
484 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
485 if (This->hwbuf) {
486 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
487 if (hres != DS_OK) {
488 WARN("IDsDriverBuffer_GetPosition failed\n");
489 return hres;
491 } else {
492 if (playpos && (This->state != STATE_PLAYING)) {
493 /* we haven't been merged into the primary buffer (yet) */
494 *playpos = This->buf_mixpos;
495 } else if (playpos) {
496 DWORD pplay, pwrite, lplay, splay, pstate;
497 /* let's get this exact; first, recursively call GetPosition on the primary */
498 EnterCriticalSection(&(This->dsound->mixlock));
499 if (DSOUND_PrimaryGetPosition(This->dsound, &pplay, &pwrite) != DS_OK)
500 WARN("DSOUND_PrimaryGetPosition failed\n");
501 /* detect HEL mode underrun */
502 pstate = This->dsound->state;
503 if (!(This->dsound->hwbuf || This->dsound->pwqueue)) {
504 TRACE("detected an underrun\n");
505 /* pplay = ? */
506 if (pstate == STATE_PLAYING)
507 pstate = STATE_STARTING;
508 else if (pstate == STATE_STOPPING)
509 pstate = STATE_STOPPED;
511 /* get data for ourselves while we still have the lock */
512 pstate &= This->state;
513 lplay = This->primary_mixpos;
514 splay = This->buf_mixpos;
515 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->dsound->hwbuf) {
516 /* calculate play position using this */
517 *playpos = DSOUND_CalcPlayPosition(This, pstate, pplay, pwrite, lplay, splay);
518 } else {
519 /* (unless the app isn't using GETCURRENTPOSITION2) */
520 /* don't know exactly how this should be handled...
521 * the docs says that play cursor is reported as directly
522 * behind write cursor, hmm... */
523 /* let's just do what might work for Half-Life */
524 DWORD wp;
525 wp = (This->dsound->pwplay + ds_hel_margin) * This->dsound->fraglen;
526 wp %= This->dsound->buflen;
527 *playpos = DSOUND_CalcPlayPosition(This, pstate, wp, pwrite, lplay, splay);
529 LeaveCriticalSection(&(This->dsound->mixlock));
531 if (writepos)
532 *writepos = This->buf_mixpos;
534 if (writepos) {
535 if (This->state != STATE_STOPPED) {
536 /* apply the documented 10ms lead to writepos */
537 *writepos += This->writelead;
539 *writepos %= This->buflen;
541 if (playpos)
542 This->last_playpos = *playpos;
543 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
544 return DS_OK;
547 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
548 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
550 ICOM_THIS(IDirectSoundBufferImpl,iface);
551 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
553 if (status == NULL) {
554 WARN("invalid parameter: status = NULL\n");
555 return DSERR_INVALIDPARAM;
558 *status = 0;
559 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
560 *status |= DSBSTATUS_PLAYING;
561 if (This->playflags & DSBPLAY_LOOPING)
562 *status |= DSBSTATUS_LOOPING;
565 TRACE("status=%lx\n", *status);
566 return DS_OK;
570 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
571 LPDIRECTSOUNDBUFFER8 iface,
572 LPWAVEFORMATEX lpwf,
573 DWORD wfsize,
574 LPDWORD wfwritten)
576 DWORD size;
577 ICOM_THIS(IDirectSoundBufferImpl,iface);
578 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
580 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
582 if (lpwf) { /* NULL is valid */
583 if (wfsize >= size) {
584 memcpy(lpwf,This->pwfx,size);
585 if (wfwritten)
586 *wfwritten = size;
587 } else {
588 WARN("invalid parameter: wfsize to small\n");
589 if (wfwritten)
590 *wfwritten = 0;
591 return DSERR_INVALIDPARAM;
593 } else {
594 if (wfwritten)
595 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
596 else {
597 WARN("invalid parameter: wfwritten == NULL\n");
598 return DSERR_INVALIDPARAM;
602 return DS_OK;
605 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
606 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
608 HRESULT hres = DS_OK;
609 ICOM_THIS(IDirectSoundBufferImpl,iface);
611 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
612 This,
613 writecursor,
614 writebytes,
615 lplpaudioptr1,
616 audiobytes1,
617 lplpaudioptr2,
618 audiobytes2,
619 flags,
620 GetTickCount()
623 if (flags & DSBLOCK_FROMWRITECURSOR) {
624 DWORD writepos;
625 /* GetCurrentPosition does too much magic to duplicate here */
626 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
627 if (hres != DS_OK) {
628 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
629 return hres;
631 writecursor += writepos;
633 writecursor %= This->buflen;
634 if (flags & DSBLOCK_ENTIREBUFFER)
635 writebytes = This->buflen;
636 if (writebytes > This->buflen)
637 writebytes = This->buflen;
639 EnterCriticalSection(&(This->lock));
641 if ((writebytes == This->buflen) &&
642 ((This->state == STATE_STARTING) ||
643 (This->state == STATE_PLAYING)))
644 /* some games, like Half-Life, try to be clever (not) and
645 * keep one secondary buffer, and mix sounds into it itself,
646 * locking the entire buffer every time... so we can just forget
647 * about tracking the last-written-to-position... */
648 This->probably_valid_to = (DWORD)-1;
649 else
650 This->probably_valid_to = writecursor;
652 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
653 hres = IDsDriverBuffer_Lock(This->hwbuf,
654 lplpaudioptr1, audiobytes1,
655 lplpaudioptr2, audiobytes2,
656 writecursor, writebytes,
658 if (hres != DS_OK) {
659 WARN("IDsDriverBuffer_Lock failed\n");
660 LeaveCriticalSection(&(This->lock));
661 return hres;
663 } else {
664 BOOL remix = FALSE;
665 if (writecursor+writebytes <= This->buflen) {
666 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
667 *audiobytes1 = writebytes;
668 if (lplpaudioptr2)
669 *(LPBYTE*)lplpaudioptr2 = NULL;
670 if (audiobytes2)
671 *audiobytes2 = 0;
672 TRACE("->%ld.0\n",writebytes);
673 } else {
674 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
675 *audiobytes1 = This->buflen-writecursor;
676 if (lplpaudioptr2)
677 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
678 if (audiobytes2)
679 *audiobytes2 = writebytes-(This->buflen-writecursor);
680 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
682 if (This->state == STATE_PLAYING) {
683 /* if the segment between playpos and buf_mixpos is touched,
684 * we need to cancel some mixing */
685 /* we'll assume that the app always calls GetCurrentPosition before
686 * locking a playing buffer, so that last_playpos is up-to-date */
687 if (This->buf_mixpos >= This->last_playpos) {
688 if (This->buf_mixpos > writecursor &&
689 This->last_playpos < writecursor+writebytes)
690 remix = TRUE;
691 } else {
692 if (This->buf_mixpos > writecursor ||
693 This->last_playpos < writecursor+writebytes)
694 remix = TRUE;
696 if (remix) {
697 TRACE("locking prebuffered region, ouch\n");
698 DSOUND_MixCancelAt(This, writecursor);
703 LeaveCriticalSection(&(This->lock));
704 return DS_OK;
707 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
708 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
710 HRESULT hres = DS_OK;
711 ICOM_THIS(IDirectSoundBufferImpl,iface);
712 TRACE("(%p,%ld)\n",This,newpos);
714 /* **** */
715 EnterCriticalSection(&(This->lock));
717 newpos %= This->buflen;
718 This->buf_mixpos = newpos;
719 if (This->hwbuf) {
720 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
721 if (hres != DS_OK)
722 WARN("IDsDriverBuffer_SetPosition failed\n");
725 LeaveCriticalSection(&(This->lock));
726 /* **** */
728 return hres;
731 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
732 LPDIRECTSOUNDBUFFER8 iface,LONG pan
734 HRESULT hres = DS_OK;
735 ICOM_THIS(IDirectSoundBufferImpl,iface);
737 TRACE("(%p,%ld)\n",This,pan);
739 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
740 WARN("invalid parameter: pan = %ld\n", pan);
741 return DSERR_INVALIDPARAM;
744 /* You cannot use both pan and 3D controls */
745 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
746 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
747 WARN("control unavailable\n");
748 return DSERR_CONTROLUNAVAIL;
751 /* **** */
752 EnterCriticalSection(&(This->lock));
754 if (This->volpan.lPan != pan) {
755 This->volpan.lPan = pan;
756 DSOUND_RecalcVolPan(&(This->volpan));
758 if (This->hwbuf) {
759 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
760 if (hres != DS_OK)
761 WARN("IDsDriverBuffer_SetVolumePan failed\n");
762 } else
763 DSOUND_ForceRemix(This);
766 LeaveCriticalSection(&(This->lock));
767 /* **** */
769 return hres;
772 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
773 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
775 ICOM_THIS(IDirectSoundBufferImpl,iface);
776 TRACE("(%p,%p)\n",This,pan);
778 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
779 WARN("control unavailable\n");
780 return DSERR_CONTROLUNAVAIL;
783 if (pan == NULL) {
784 WARN("invalid parameter: pan = NULL\n");
785 return DSERR_INVALIDPARAM;
788 *pan = This->volpan.lPan;
790 return DS_OK;
793 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
794 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
796 ICOM_THIS(IDirectSoundBufferImpl,iface);
797 DWORD probably_valid_to;
799 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
801 /* **** */
802 EnterCriticalSection(&(This->lock));
804 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
805 HRESULT hres;
806 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
807 if (hres != DS_OK) {
808 LeaveCriticalSection(&(This->lock));
809 WARN("IDsDriverBuffer_Unlock failed\n");
810 return hres;
814 if (p2) probably_valid_to = (((LPBYTE)p2)-This->buffer->memory) + x2;
815 else probably_valid_to = (((LPBYTE)p1)-This->buffer->memory) + x1;
816 probably_valid_to %= This->buflen;
817 if ((probably_valid_to == 0) && ((x1+x2) == This->buflen) &&
818 ((This->state == STATE_STARTING) ||
819 (This->state == STATE_PLAYING)))
820 /* see IDirectSoundBufferImpl_Lock */
821 probably_valid_to = (DWORD)-1;
822 This->probably_valid_to = probably_valid_to;
824 LeaveCriticalSection(&(This->lock));
825 /* **** */
827 TRACE("probably_valid_to=%ld\n", This->probably_valid_to);
828 return DS_OK;
831 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
832 LPDIRECTSOUNDBUFFER8 iface
834 ICOM_THIS(IDirectSoundBufferImpl,iface);
835 FIXME("(%p):stub\n",This);
836 return DS_OK;
839 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
840 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
842 ICOM_THIS(IDirectSoundBufferImpl,iface);
843 TRACE("(%p,%p)\n",This,freq);
845 if (freq == NULL) {
846 WARN("invalid parameter: freq = NULL\n");
847 return DSERR_INVALIDPARAM;
850 *freq = This->freq;
851 TRACE("-> %ld\n", *freq);
853 return DS_OK;
856 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
857 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
859 ICOM_THIS(IDirectSoundBufferImpl,iface);
860 DWORD u;
862 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
864 if (pdwResultCodes)
865 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
867 WARN("control unavailable\n");
868 return DSERR_CONTROLUNAVAIL;
871 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
872 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
874 ICOM_THIS(IDirectSoundBufferImpl,iface);
875 DWORD u;
877 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
879 if (pdwResultCodes)
880 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
882 WARN("control unavailable\n");
883 return DSERR_CONTROLUNAVAIL;
886 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
887 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
889 ICOM_THIS(IDirectSoundBufferImpl,iface);
891 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
893 WARN("control unavailable\n");
894 return DSERR_CONTROLUNAVAIL;
897 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
898 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
900 ICOM_THIS(IDirectSoundBufferImpl,iface);
901 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
902 DPRINTF("Re-Init!!!\n");
903 WARN("already initialized\n");
904 return DSERR_ALREADYINITIALIZED;
907 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
908 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
910 ICOM_THIS(IDirectSoundBufferImpl,iface);
911 TRACE("(%p)->(%p)\n",This,caps);
913 if (caps == NULL) {
914 WARN("invalid parameter: caps == NULL\n");
915 return DSERR_INVALIDPARAM;
918 if (caps->dwSize < sizeof(*caps)) {
919 WARN("invalid parameter: caps->dwSize = %ld < %d\n",caps->dwSize, sizeof(*caps));
920 return DSERR_INVALIDPARAM;
923 caps->dwFlags = This->dsbd.dwFlags;
924 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
925 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
927 caps->dwBufferBytes = This->buflen;
929 /* This value represents the speed of the "unlock" command.
930 As unlock is quite fast (it does not do anything), I put
931 4096 ko/s = 4 Mo / s */
932 /* FIXME: hwbuf speed */
933 caps->dwUnlockTransferRate = 4096;
934 caps->dwPlayCpuOverhead = 0;
936 return DS_OK;
939 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
940 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
942 ICOM_THIS(IDirectSoundBufferImpl,iface);
944 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
946 if (ppobj == NULL) {
947 WARN("invalid parameter\n");
948 return E_INVALIDARG;
951 *ppobj = NULL; /* assume failure */
953 if ( IsEqualGUID(riid, &IID_IUnknown) ||
954 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
955 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
956 if (!This->dsb)
957 SecondaryBufferImpl_Create(This, &(This->dsb));
958 if (This->dsb) {
959 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->dsb);
960 *ppobj = This->dsb;
961 return S_OK;
963 WARN("IID_IDirectSoundBuffer\n");
964 return E_NOINTERFACE;
967 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
968 if (!This->notify)
969 IDirectSoundNotifyImpl_Create(This, &(This->notify));
970 if (This->notify) {
971 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
972 *ppobj = This->notify;
973 return S_OK;
975 WARN("IID_IDirectSoundNotify\n");
976 return E_NOINTERFACE;
979 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
980 if (!This->ds3db)
981 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
982 if (This->ds3db) {
983 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
984 *ppobj = This->ds3db;
985 return S_OK;
987 WARN("IID_IDirectSound3DBuffer\n");
988 return E_NOINTERFACE;
991 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
992 ERR("app requested IDirectSound3DListener on secondary buffer\n");
993 return E_NOINTERFACE;
996 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
997 /* only supported on hardware 3D secondary buffers */
998 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) &&
999 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) &&
1000 (This->hwbuf != NULL) ) {
1001 if (!This->iks)
1002 IKsBufferPropertySetImpl_Create(This, &(This->iks));
1003 if (This->iks) {
1004 IKsPropertySet_AddRef((LPKSPROPERTYSET)*ppobj);
1005 *ppobj = This->iks;
1006 return S_OK;
1009 WARN("IID_IKsPropertySet\n");
1010 return E_NOINTERFACE;
1013 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1015 return E_NOINTERFACE;
1018 static IDirectSoundBuffer8Vtbl dsbvt =
1020 IDirectSoundBufferImpl_QueryInterface,
1021 IDirectSoundBufferImpl_AddRef,
1022 IDirectSoundBufferImpl_Release,
1023 IDirectSoundBufferImpl_GetCaps,
1024 IDirectSoundBufferImpl_GetCurrentPosition,
1025 IDirectSoundBufferImpl_GetFormat,
1026 IDirectSoundBufferImpl_GetVolume,
1027 IDirectSoundBufferImpl_GetPan,
1028 IDirectSoundBufferImpl_GetFrequency,
1029 IDirectSoundBufferImpl_GetStatus,
1030 IDirectSoundBufferImpl_Initialize,
1031 IDirectSoundBufferImpl_Lock,
1032 IDirectSoundBufferImpl_Play,
1033 IDirectSoundBufferImpl_SetCurrentPosition,
1034 IDirectSoundBufferImpl_SetFormat,
1035 IDirectSoundBufferImpl_SetVolume,
1036 IDirectSoundBufferImpl_SetPan,
1037 IDirectSoundBufferImpl_SetFrequency,
1038 IDirectSoundBufferImpl_Stop,
1039 IDirectSoundBufferImpl_Unlock,
1040 IDirectSoundBufferImpl_Restore,
1041 IDirectSoundBufferImpl_SetFX,
1042 IDirectSoundBufferImpl_AcquireResources,
1043 IDirectSoundBufferImpl_GetObjectInPath
1046 HRESULT WINAPI IDirectSoundBufferImpl_Create(
1047 IDirectSoundImpl *ds,
1048 IDirectSoundBufferImpl **pdsb,
1049 LPCDSBUFFERDESC dsbd)
1051 IDirectSoundBufferImpl *dsb;
1052 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1053 HRESULT err = DS_OK;
1054 DWORD capf = 0;
1055 int use_hw, alloc_size, cp_size;
1056 TRACE("(%p,%p,%p)\n",ds,pdsb,dsbd);
1058 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1059 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
1060 *pdsb = NULL;
1061 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1064 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1066 if (dsb == 0) {
1067 WARN("out of memory\n");
1068 *pdsb = NULL;
1069 return DSERR_OUTOFMEMORY;
1072 TRACE("Created buffer at %p\n", dsb);
1074 dsb->ref = 0;
1075 dsb->dsb = 0;
1076 dsb->dsound = ds;
1077 dsb->lpVtbl = &dsbvt;
1078 dsb->iks = NULL;
1080 /* size depends on version */
1081 memcpy(&dsb->dsbd, dsbd, dsbd->dwSize);
1083 /* variable sized struct so calculate size based on format */
1084 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1085 alloc_size = sizeof(WAVEFORMATEX);
1086 cp_size = sizeof(PCMWAVEFORMAT);
1087 } else
1088 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
1090 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
1091 if (dsb->pwfx == NULL) {
1092 WARN("out of memory\n");
1093 HeapFree(GetProcessHeap(),0,dsb);
1094 *pdsb = NULL;
1095 return DSERR_OUTOFMEMORY;
1098 memcpy(dsb->pwfx, wfex, cp_size);
1100 dsb->buflen = dsbd->dwBufferBytes;
1101 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1103 dsb->notify = NULL;
1104 dsb->notifies = NULL;
1105 dsb->nrofnotifies = 0;
1106 dsb->hwnotify = 0;
1108 /* Check necessary hardware mixing capabilities */
1109 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1110 else capf |= DSCAPS_SECONDARYMONO;
1111 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1112 else capf |= DSCAPS_SECONDARY8BIT;
1114 use_hw = (ds->drvcaps.dwFlags & capf) == capf;
1115 TRACE("use_hw = 0x%08x, capf = 0x%08lx, ds->drvcaps.dwFlags = 0x%08lx\n", use_hw, capf, ds->drvcaps.dwFlags);
1117 /* FIXME: check hardware sample rate mixing capabilities */
1118 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1119 /* FIXME: check whether any hardware buffers are left */
1120 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1122 /* Allocate system memory if applicable */
1123 if ((ds->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1124 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1125 if (dsb->buffer == NULL) {
1126 WARN("out of memory\n");
1127 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1128 HeapFree(GetProcessHeap(),0,dsb);
1129 *pdsb = NULL;
1130 return DSERR_OUTOFMEMORY;
1133 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1134 if (dsb->buffer->memory == NULL) {
1135 WARN("out of memory\n");
1136 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1137 HeapFree(GetProcessHeap(),0,dsb->buffer);
1138 HeapFree(GetProcessHeap(),0,dsb);
1139 *pdsb = NULL;
1140 return DSERR_OUTOFMEMORY;
1142 dsb->buffer->ref = 1;
1145 /* Allocate the hardware buffer */
1146 if (use_hw) {
1147 err = IDsDriver_CreateSoundBuffer(ds->driver,wfex,dsbd->dwFlags,0,
1148 &(dsb->buflen),&(dsb->buffer->memory),
1149 (LPVOID*)&(dsb->hwbuf));
1150 /* fall back to software buffer on failure */
1151 if (err != DS_OK) {
1152 TRACE("IDsDriver_CreateSoundBuffer failed, falling back to software buffer\n");
1153 use_hw = 0;
1154 if (ds->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1155 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1156 if (dsb->buffer == NULL) {
1157 WARN("out of memory\n");
1158 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1159 HeapFree(GetProcessHeap(),0,dsb);
1160 *pdsb = NULL;
1161 return DSERR_OUTOFMEMORY;
1164 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1165 if (dsb->buffer->memory == NULL) {
1166 WARN("out of memory\n");
1167 HeapFree(GetProcessHeap(),0,dsb->buffer);
1168 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1169 HeapFree(GetProcessHeap(),0,dsb);
1170 *pdsb = NULL;
1171 return DSERR_OUTOFMEMORY;
1173 dsb->buffer->ref = 1;
1175 err = DS_OK;
1179 /* calculate fragment size and write lead */
1180 DSOUND_RecalcFormat(dsb);
1182 /* It's not necessary to initialize values to zero since */
1183 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1184 dsb->playpos = 0;
1185 dsb->buf_mixpos = 0;
1186 dsb->state = STATE_STOPPED;
1188 dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
1189 ds->pwfx->nSamplesPerSec;
1190 dsb->nAvgBytesPerSec = dsb->freq *
1191 dsbd->lpwfxFormat->nBlockAlign;
1193 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1194 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1195 dsb->ds3db_ds3db.vPosition.x = 0.0;
1196 dsb->ds3db_ds3db.vPosition.y = 0.0;
1197 dsb->ds3db_ds3db.vPosition.z = 0.0;
1198 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1199 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1200 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1201 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1202 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1203 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1204 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1205 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1206 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1207 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1208 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1209 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1211 dsb->ds3db_need_recalc = FALSE;
1212 DSOUND_Calc3DBuffer(dsb);
1213 } else
1214 DSOUND_RecalcVolPan(&(dsb->volpan));
1216 InitializeCriticalSection(&(dsb->lock));
1217 dsb->lock.DebugInfo->Spare[1] = (DWORD)"DSOUNDBUFFER_lock";
1219 /* register buffer if not primary */
1220 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1221 err = DSOUND_AddBuffer(ds, dsb);
1222 if (err != DS_OK) {
1223 if (dsb->buffer->memory)
1224 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1225 if (dsb->buffer)
1226 HeapFree(GetProcessHeap(),0,dsb->buffer);
1227 DeleteCriticalSection(&(dsb->lock));
1228 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1229 HeapFree(GetProcessHeap(),0,dsb);
1230 dsb = NULL;
1234 *pdsb = dsb;
1235 return err;
1238 HRESULT WINAPI IDirectSoundBufferImpl_Destroy(
1239 IDirectSoundBufferImpl *pdsb)
1241 TRACE("(%p)\n",pdsb);
1243 /* This keeps the *_Destroy functions from possibly deleting
1244 * this object until it is ready to be deleted */
1245 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1247 if (pdsb->iks) {
1248 WARN("iks not NULL\n");
1249 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1250 pdsb->iks = NULL;
1253 if (pdsb->ds3db) {
1254 WARN("ds3db not NULL\n");
1255 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1256 pdsb->ds3db = NULL;
1259 if (pdsb->notify) {
1260 WARN("notify not NULL\n");
1261 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1262 pdsb->notify = NULL;
1265 if (pdsb->dsb) {
1266 WARN("dsb not NULL\n");
1267 SecondaryBufferImpl_Destroy(pdsb->dsb);
1268 pdsb->dsb = NULL;
1271 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1273 return S_OK;
1276 /*******************************************************************************
1277 * SecondaryBuffer
1280 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1281 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1283 ICOM_THIS(SecondaryBufferImpl,iface);
1284 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1286 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1289 static DWORD WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1291 ICOM_THIS(IDirectSoundBufferImpl,iface);
1292 DWORD ref;
1293 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1295 ref = InterlockedIncrement(&(This->ref));
1296 if (!ref) {
1297 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
1299 return ref;
1302 static DWORD WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1304 ICOM_THIS(IDirectSoundBufferImpl,iface);
1305 DWORD ref;
1306 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1308 ref = InterlockedDecrement(&(This->ref));
1309 if (!ref) {
1310 This->dsb->dsb = NULL;
1311 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1312 HeapFree(GetProcessHeap(),0,This);
1313 TRACE("(%p) released\n",This);
1315 return ref;
1318 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1319 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1321 ICOM_THIS(SecondaryBufferImpl,iface);
1322 TRACE("(%p)->(%p)\n",This,caps);
1324 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1327 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1328 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1330 ICOM_THIS(SecondaryBufferImpl,iface);
1331 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1333 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1336 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1337 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1339 ICOM_THIS(SecondaryBufferImpl,iface);
1340 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1342 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1345 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1346 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1348 ICOM_THIS(SecondaryBufferImpl,iface);
1349 TRACE("(%p,%p)\n",This,vol);
1351 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1354 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1355 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1357 ICOM_THIS(SecondaryBufferImpl,iface);
1358 TRACE("(%p,%p)\n",This,pan);
1360 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1363 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1364 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1366 ICOM_THIS(SecondaryBufferImpl,iface);
1367 TRACE("(%p,%p)\n",This,freq);
1369 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1372 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1373 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1375 ICOM_THIS(SecondaryBufferImpl,iface);
1376 TRACE("(%p,%p)\n",This,status);
1378 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1381 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1382 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1384 ICOM_THIS(SecondaryBufferImpl,iface);
1385 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1387 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1390 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1391 LPDIRECTSOUNDBUFFER8 iface,
1392 DWORD writecursor,
1393 DWORD writebytes,
1394 LPVOID lplpaudioptr1,
1395 LPDWORD audiobytes1,
1396 LPVOID lplpaudioptr2,
1397 LPDWORD audiobytes2,
1398 DWORD dwFlags)
1400 ICOM_THIS(SecondaryBufferImpl,iface);
1401 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1402 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1404 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1405 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1408 static HRESULT WINAPI SecondaryBufferImpl_Play(
1409 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1411 ICOM_THIS(SecondaryBufferImpl,iface);
1412 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
1414 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1417 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1418 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1420 ICOM_THIS(SecondaryBufferImpl,iface);
1421 TRACE("(%p,%ld)\n",This,newpos);
1423 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1426 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1427 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1429 ICOM_THIS(SecondaryBufferImpl,iface);
1430 TRACE("(%p,%p)\n",This,wfex);
1432 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1435 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1436 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1438 ICOM_THIS(SecondaryBufferImpl,iface);
1439 TRACE("(%p,%ld)\n",This,vol);
1441 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1444 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1445 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1447 ICOM_THIS(SecondaryBufferImpl,iface);
1448 TRACE("(%p,%ld)\n",This,pan);
1450 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1453 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1454 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1456 ICOM_THIS(SecondaryBufferImpl,iface);
1457 TRACE("(%p,%ld)\n",This,freq);
1459 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1462 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1464 ICOM_THIS(SecondaryBufferImpl,iface);
1465 TRACE("(%p)\n",This);
1467 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1470 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1471 LPDIRECTSOUNDBUFFER8 iface,
1472 LPVOID lpvAudioPtr1,
1473 DWORD dwAudioBytes1,
1474 LPVOID lpvAudioPtr2,
1475 DWORD dwAudioBytes2)
1477 ICOM_THIS(SecondaryBufferImpl,iface);
1478 TRACE("(%p,%p,%ld,%p,%ld)\n",
1479 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1481 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1482 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1485 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1486 LPDIRECTSOUNDBUFFER8 iface)
1488 ICOM_THIS(SecondaryBufferImpl,iface);
1489 TRACE("(%p)\n",This);
1491 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1494 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1495 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1497 ICOM_THIS(SecondaryBufferImpl,iface);
1498 TRACE("(%p,%lu,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1500 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1503 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1504 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1506 ICOM_THIS(SecondaryBufferImpl,iface);
1507 TRACE("(%p,%08lu,%lu,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1509 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1512 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1513 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1515 ICOM_THIS(SecondaryBufferImpl,iface);
1516 TRACE("(%p,%s,%lu,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1518 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1521 static IDirectSoundBuffer8Vtbl sbvt =
1523 SecondaryBufferImpl_QueryInterface,
1524 SecondaryBufferImpl_AddRef,
1525 SecondaryBufferImpl_Release,
1526 SecondaryBufferImpl_GetCaps,
1527 SecondaryBufferImpl_GetCurrentPosition,
1528 SecondaryBufferImpl_GetFormat,
1529 SecondaryBufferImpl_GetVolume,
1530 SecondaryBufferImpl_GetPan,
1531 SecondaryBufferImpl_GetFrequency,
1532 SecondaryBufferImpl_GetStatus,
1533 SecondaryBufferImpl_Initialize,
1534 SecondaryBufferImpl_Lock,
1535 SecondaryBufferImpl_Play,
1536 SecondaryBufferImpl_SetCurrentPosition,
1537 SecondaryBufferImpl_SetFormat,
1538 SecondaryBufferImpl_SetVolume,
1539 SecondaryBufferImpl_SetPan,
1540 SecondaryBufferImpl_SetFrequency,
1541 SecondaryBufferImpl_Stop,
1542 SecondaryBufferImpl_Unlock,
1543 SecondaryBufferImpl_Restore,
1544 SecondaryBufferImpl_SetFX,
1545 SecondaryBufferImpl_AcquireResources,
1546 SecondaryBufferImpl_GetObjectInPath
1549 HRESULT WINAPI SecondaryBufferImpl_Create(
1550 IDirectSoundBufferImpl *dsb,
1551 SecondaryBufferImpl **psb)
1553 SecondaryBufferImpl *sb;
1554 TRACE("(%p,%p)\n",dsb,psb);
1556 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1558 if (sb == 0) {
1559 WARN("out of memory\n");
1560 *psb = NULL;
1561 return DSERR_OUTOFMEMORY;
1563 sb->ref = 0;
1564 sb->dsb = dsb;
1565 sb->lpVtbl = &sbvt;
1567 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1568 *psb = sb;
1569 return S_OK;
1572 HRESULT WINAPI SecondaryBufferImpl_Destroy(
1573 SecondaryBufferImpl *pdsb)
1575 TRACE("(%p)\n",pdsb);
1577 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1579 return S_OK;