Always create a property set for secondary buffers.
[wine/wine-kai.git] / dlls / dsound / buffer.c
blob74721c8406dec03af29acfa7bb97bfc82de01f8a
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 IDirectSoundNotifyImpl *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)
73 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
74 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
75 return InterlockedIncrement(&(This->ref));
78 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
79 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
80 ULONG ref;
82 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
84 ref = InterlockedDecrement(&(This->ref));
85 if (ref == 0) {
86 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
87 This->dsb->notify = NULL;
88 HeapFree(GetProcessHeap(),0,This);
89 TRACE("(%p) released\n",This);
91 return ref;
94 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
95 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
96 ) {
97 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
98 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
100 if (howmuch > 0 && notify == NULL) {
101 WARN("invalid parameter: notify == NULL\n");
102 return DSERR_INVALIDPARAM;
105 if (TRACE_ON(dsound)) {
106 unsigned int i;
107 for (i=0;i<howmuch;i++)
108 TRACE("notify at %ld to 0x%08lx\n",
109 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
112 if (This->dsb->hwnotify) {
113 HRESULT hres;
114 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
115 if (hres != DS_OK)
116 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
117 return hres;
118 } else if (howmuch > 0) {
119 /* Make an internal copy of the caller-supplied array.
120 * Replace the existing copy if one is already present. */
121 if (This->dsb->notifies)
122 This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
123 This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
124 else
125 This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
126 howmuch * sizeof(DSBPOSITIONNOTIFY));
128 if (This->dsb->notifies == NULL) {
129 WARN("out of memory\n");
130 return DSERR_OUTOFMEMORY;
132 memcpy(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
133 This->dsb->nrofnotifies = howmuch;
134 } else {
135 if (This->dsb->notifies) {
136 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
137 This->dsb->notifies = NULL;
139 This->dsb->nrofnotifies = 0;
142 return S_OK;
145 IDirectSoundNotifyVtbl dsnvt =
147 IDirectSoundNotifyImpl_QueryInterface,
148 IDirectSoundNotifyImpl_AddRef,
149 IDirectSoundNotifyImpl_Release,
150 IDirectSoundNotifyImpl_SetNotificationPositions,
153 HRESULT WINAPI IDirectSoundNotifyImpl_Create(
154 IDirectSoundBufferImpl * dsb,
155 IDirectSoundNotifyImpl **pdsn)
157 IDirectSoundNotifyImpl * dsn;
158 TRACE("(%p,%p)\n",dsb,pdsn);
160 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
162 if (dsn == NULL) {
163 WARN("out of memory\n");
164 return DSERR_OUTOFMEMORY;
167 dsn->ref = 0;
168 dsn->lpVtbl = &dsnvt;
169 dsn->dsb = dsb;
170 dsb->notify = dsn;
171 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
173 *pdsn = dsn;
174 return DS_OK;
177 HRESULT WINAPI IDirectSoundNotifyImpl_Destroy(
178 IDirectSoundNotifyImpl *pdsn)
180 TRACE("(%p)\n",pdsn);
182 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
184 return DS_OK;
187 /*******************************************************************************
188 * IDirectSoundBuffer
191 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
192 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
194 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
196 TRACE("(%p,%p)\n",This,wfex);
197 /* This method is not available on secondary buffers */
198 WARN("invalid call\n");
199 return DSERR_INVALIDCALL;
202 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
203 LPDIRECTSOUNDBUFFER8 iface,LONG vol
205 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
206 LONG oldVol;
207 HRESULT hres = DS_OK;
209 TRACE("(%p,%ld)\n",This,vol);
211 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
212 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This->dsbd.dwFlags);
213 return DSERR_CONTROLUNAVAIL;
216 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
217 WARN("invalid parameter: vol = %ld\n", vol);
218 return DSERR_INVALIDPARAM;
221 /* **** */
222 EnterCriticalSection(&(This->lock));
224 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
225 oldVol = This->ds3db_lVolume;
226 This->ds3db_lVolume = vol;
227 } else {
228 oldVol = This->volpan.lVolume;
229 This->volpan.lVolume = vol;
230 if (vol != oldVol)
231 DSOUND_RecalcVolPan(&(This->volpan));
234 if (vol != oldVol) {
235 if (This->hwbuf) {
236 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
237 if (hres != DS_OK)
238 WARN("IDsDriverBuffer_SetVolumePan failed\n");
239 } else
240 DSOUND_ForceRemix(This);
243 LeaveCriticalSection(&(This->lock));
244 /* **** */
246 return hres;
249 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
250 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
252 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
253 TRACE("(%p,%p)\n",This,vol);
255 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
256 WARN("control unavailable\n");
257 return DSERR_CONTROLUNAVAIL;
260 if (vol == NULL) {
261 WARN("invalid parameter: vol == NULL\n");
262 return DSERR_INVALIDPARAM;
265 *vol = This->volpan.lVolume;
267 return DS_OK;
270 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
271 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
273 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
274 DWORD oldFreq;
276 TRACE("(%p,%ld)\n",This,freq);
278 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
279 WARN("control unavailable\n");
280 return DSERR_CONTROLUNAVAIL;
283 if (freq == DSBFREQUENCY_ORIGINAL)
284 freq = This->pwfx->nSamplesPerSec;
286 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
287 WARN("invalid parameter: freq = %ld\n", freq);
288 return DSERR_INVALIDPARAM;
291 /* **** */
292 EnterCriticalSection(&(This->lock));
294 oldFreq = This->freq;
295 This->freq = freq;
296 if (freq != oldFreq) {
297 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->dsound->pwfx->nSamplesPerSec;
298 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
299 DSOUND_RecalcFormat(This);
300 if (!This->hwbuf)
301 DSOUND_ForceRemix(This);
304 LeaveCriticalSection(&(This->lock));
305 /* **** */
307 return DS_OK;
310 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
311 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
313 HRESULT hres = DS_OK;
314 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
315 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
317 /* **** */
318 EnterCriticalSection(&(This->lock));
320 This->playflags = flags;
321 if (This->state == STATE_STOPPED) {
322 This->leadin = TRUE;
323 This->startpos = This->buf_mixpos;
324 This->state = STATE_STARTING;
325 } else if (This->state == STATE_STOPPING)
326 This->state = STATE_PLAYING;
327 if (This->hwbuf) {
328 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
329 if (hres != DS_OK)
330 WARN("IDsDriverBuffer_Play failed\n");
331 else
332 This->state = STATE_PLAYING;
335 LeaveCriticalSection(&(This->lock));
336 /* **** */
338 return hres;
341 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
343 HRESULT hres = DS_OK;
344 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
345 TRACE("(%p)\n",This);
347 /* **** */
348 EnterCriticalSection(&(This->lock));
350 if (This->state == STATE_PLAYING)
351 This->state = STATE_STOPPING;
352 else if (This->state == STATE_STARTING)
353 This->state = STATE_STOPPED;
354 if (This->hwbuf) {
355 hres = IDsDriverBuffer_Stop(This->hwbuf);
356 if (hres != DS_OK)
357 WARN("IDsDriverBuffer_Stop failed\n");
358 else
359 This->state = STATE_STOPPED;
361 DSOUND_CheckEvent(This, 0);
363 LeaveCriticalSection(&(This->lock));
364 /* **** */
366 return hres;
369 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
371 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
372 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
373 return InterlockedIncrement(&(This->ref));
376 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
378 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
379 ULONG ref;
381 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
383 ref = InterlockedDecrement(&(This->ref));
384 if (ref)
385 return ref;
387 DSOUND_RemoveBuffer(This->dsound, This);
389 This->lock.DebugInfo->Spare[1] = 0;
390 DeleteCriticalSection(&(This->lock));
392 if (This->hwbuf) {
393 IDsDriverBuffer_Release(This->hwbuf);
394 if (This->dsound->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
395 This->buffer->ref--;
396 if (This->buffer->ref==0) {
397 HeapFree(GetProcessHeap(),0,This->buffer->memory);
398 HeapFree(GetProcessHeap(),0,This->buffer);
401 } else {
402 This->buffer->ref--;
403 if (This->buffer->ref==0) {
404 HeapFree(GetProcessHeap(),0,This->buffer->memory);
405 HeapFree(GetProcessHeap(),0,This->buffer);
409 if (This->notifies != NULL)
410 HeapFree(GetProcessHeap(), 0, This->notifies);
412 if (This->pwfx)
413 HeapFree(GetProcessHeap(), 0, This->pwfx);
415 HeapFree(GetProcessHeap(),0,This);
417 TRACE("(%p) released\n",This);
418 return 0;
421 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This,
422 DWORD state, DWORD pplay, DWORD pwrite, DWORD pmix, DWORD bmix)
424 DWORD bplay;
426 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, pmix);
427 TRACE("this mixpos=%ld, time=%ld\n", bmix, GetTickCount());
429 /* the actual primary play position (pplay) is always behind last mixed (pmix),
430 * unless the computer is too slow or something */
431 /* we need to know how far away we are from there */
432 #if 0 /* we'll never fill the primary entirely */
433 if (pmix == pplay) {
434 if ((state == STATE_PLAYING) || (state == STATE_STOPPING)) {
435 /* wow, the software mixer is really doing well,
436 * seems the entire primary buffer is filled! */
437 pmix += This->dsound->buflen;
439 /* else: the primary buffer is not playing, so probably empty */
441 #endif
442 if (pmix < pplay) pmix += This->dsound->buflen; /* wraparound */
443 pmix -= pplay;
444 /* detect buffer underrun */
445 if (pwrite < pplay) pwrite += This->dsound->buflen; /* wraparound */
446 pwrite -= pplay;
447 if (pmix > (ds_snd_queue_max * This->dsound->fraglen + pwrite + This->dsound->writelead)) {
448 WARN("detected an underrun: primary queue was %ld\n",pmix);
449 pmix = 0;
451 /* divide the offset by its sample size */
452 pmix /= This->dsound->pwfx->nBlockAlign;
453 TRACE("primary back-samples=%ld\n",pmix);
454 /* adjust for our frequency */
455 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
456 /* multiply by our own sample size */
457 pmix *= This->pwfx->nBlockAlign;
458 TRACE("this back-offset=%ld\n", pmix);
459 /* subtract from our last mixed position */
460 bplay = bmix;
461 while (bplay < pmix) bplay += This->buflen; /* wraparound */
462 bplay -= pmix;
463 if (This->leadin && ((bplay < This->startpos) || (bplay > bmix))) {
464 /* seems we haven't started playing yet */
465 TRACE("this still in lead-in phase\n");
466 bplay = This->startpos;
468 /* return the result */
469 return bplay;
472 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
473 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
475 HRESULT hres;
476 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
477 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
478 if (This->hwbuf) {
479 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
480 if (hres != DS_OK) {
481 WARN("IDsDriverBuffer_GetPosition failed\n");
482 return hres;
484 } else {
485 if (playpos && (This->state != STATE_PLAYING)) {
486 /* we haven't been merged into the primary buffer (yet) */
487 *playpos = This->buf_mixpos;
488 } else if (playpos) {
489 DWORD pplay, pwrite, lplay, splay, pstate;
490 /* let's get this exact; first, recursively call GetPosition on the primary */
491 EnterCriticalSection(&(This->dsound->mixlock));
492 if (DSOUND_PrimaryGetPosition(This->dsound, &pplay, &pwrite) != DS_OK)
493 WARN("DSOUND_PrimaryGetPosition failed\n");
494 /* detect HEL mode underrun */
495 pstate = This->dsound->state;
496 if (!(This->dsound->hwbuf || This->dsound->pwqueue)) {
497 TRACE("detected an underrun\n");
498 /* pplay = ? */
499 if (pstate == STATE_PLAYING)
500 pstate = STATE_STARTING;
501 else if (pstate == STATE_STOPPING)
502 pstate = STATE_STOPPED;
504 /* get data for ourselves while we still have the lock */
505 pstate &= This->state;
506 lplay = This->primary_mixpos;
507 splay = This->buf_mixpos;
508 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->dsound->hwbuf) {
509 /* calculate play position using this */
510 *playpos = DSOUND_CalcPlayPosition(This, pstate, pplay, pwrite, lplay, splay);
511 } else {
512 /* (unless the app isn't using GETCURRENTPOSITION2) */
513 /* don't know exactly how this should be handled...
514 * the docs says that play cursor is reported as directly
515 * behind write cursor, hmm... */
516 /* let's just do what might work for Half-Life */
517 DWORD wp;
518 wp = (This->dsound->pwplay + ds_hel_margin) * This->dsound->fraglen;
519 wp %= This->dsound->buflen;
520 *playpos = DSOUND_CalcPlayPosition(This, pstate, wp, pwrite, lplay, splay);
522 LeaveCriticalSection(&(This->dsound->mixlock));
524 if (writepos)
525 *writepos = This->buf_mixpos;
527 if (writepos) {
528 if (This->state != STATE_STOPPED) {
529 /* apply the documented 10ms lead to writepos */
530 *writepos += This->writelead;
532 *writepos %= This->buflen;
534 if (playpos)
535 This->last_playpos = *playpos;
536 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
537 return DS_OK;
540 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
541 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
543 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
544 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
546 if (status == NULL) {
547 WARN("invalid parameter: status = NULL\n");
548 return DSERR_INVALIDPARAM;
551 *status = 0;
552 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
553 *status |= DSBSTATUS_PLAYING;
554 if (This->playflags & DSBPLAY_LOOPING)
555 *status |= DSBSTATUS_LOOPING;
558 TRACE("status=%lx\n", *status);
559 return DS_OK;
563 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
564 LPDIRECTSOUNDBUFFER8 iface,
565 LPWAVEFORMATEX lpwf,
566 DWORD wfsize,
567 LPDWORD wfwritten)
569 DWORD size;
570 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
571 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
573 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
575 if (lpwf) { /* NULL is valid */
576 if (wfsize >= size) {
577 memcpy(lpwf,This->pwfx,size);
578 if (wfwritten)
579 *wfwritten = size;
580 } else {
581 WARN("invalid parameter: wfsize to small\n");
582 if (wfwritten)
583 *wfwritten = 0;
584 return DSERR_INVALIDPARAM;
586 } else {
587 if (wfwritten)
588 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
589 else {
590 WARN("invalid parameter: wfwritten == NULL\n");
591 return DSERR_INVALIDPARAM;
595 return DS_OK;
598 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
599 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
601 HRESULT hres = DS_OK;
602 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
604 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
605 This,
606 writecursor,
607 writebytes,
608 lplpaudioptr1,
609 audiobytes1,
610 lplpaudioptr2,
611 audiobytes2,
612 flags,
613 GetTickCount()
616 if (flags & DSBLOCK_FROMWRITECURSOR) {
617 DWORD writepos;
618 /* GetCurrentPosition does too much magic to duplicate here */
619 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
620 if (hres != DS_OK) {
621 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
622 return hres;
624 writecursor += writepos;
626 writecursor %= This->buflen;
627 if (flags & DSBLOCK_ENTIREBUFFER)
628 writebytes = This->buflen;
629 if (writebytes > This->buflen)
630 writebytes = This->buflen;
632 EnterCriticalSection(&(This->lock));
634 if ((writebytes == This->buflen) &&
635 ((This->state == STATE_STARTING) ||
636 (This->state == STATE_PLAYING)))
637 /* some games, like Half-Life, try to be clever (not) and
638 * keep one secondary buffer, and mix sounds into it itself,
639 * locking the entire buffer every time... so we can just forget
640 * about tracking the last-written-to-position... */
641 This->probably_valid_to = (DWORD)-1;
642 else
643 This->probably_valid_to = writecursor;
645 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
646 hres = IDsDriverBuffer_Lock(This->hwbuf,
647 lplpaudioptr1, audiobytes1,
648 lplpaudioptr2, audiobytes2,
649 writecursor, writebytes,
651 if (hres != DS_OK) {
652 WARN("IDsDriverBuffer_Lock failed\n");
653 LeaveCriticalSection(&(This->lock));
654 return hres;
656 } else {
657 BOOL remix = FALSE;
658 if (writecursor+writebytes <= This->buflen) {
659 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
660 *audiobytes1 = writebytes;
661 if (lplpaudioptr2)
662 *(LPBYTE*)lplpaudioptr2 = NULL;
663 if (audiobytes2)
664 *audiobytes2 = 0;
665 TRACE("->%ld.0\n",writebytes);
666 } else {
667 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
668 *audiobytes1 = This->buflen-writecursor;
669 if (lplpaudioptr2)
670 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
671 if (audiobytes2)
672 *audiobytes2 = writebytes-(This->buflen-writecursor);
673 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
675 if (This->state == STATE_PLAYING) {
676 /* if the segment between playpos and buf_mixpos is touched,
677 * we need to cancel some mixing */
678 /* we'll assume that the app always calls GetCurrentPosition before
679 * locking a playing buffer, so that last_playpos is up-to-date */
680 if (This->buf_mixpos >= This->last_playpos) {
681 if (This->buf_mixpos > writecursor &&
682 This->last_playpos < writecursor+writebytes)
683 remix = TRUE;
684 } else {
685 if (This->buf_mixpos > writecursor ||
686 This->last_playpos < writecursor+writebytes)
687 remix = TRUE;
689 if (remix) {
690 TRACE("locking prebuffered region, ouch\n");
691 DSOUND_MixCancelAt(This, writecursor);
696 LeaveCriticalSection(&(This->lock));
697 return DS_OK;
700 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
701 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
703 HRESULT hres = DS_OK;
704 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
705 TRACE("(%p,%ld)\n",This,newpos);
707 /* **** */
708 EnterCriticalSection(&(This->lock));
710 newpos %= This->buflen;
711 This->buf_mixpos = newpos;
712 if (This->hwbuf) {
713 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
714 if (hres != DS_OK)
715 WARN("IDsDriverBuffer_SetPosition failed\n");
718 LeaveCriticalSection(&(This->lock));
719 /* **** */
721 return hres;
724 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
725 LPDIRECTSOUNDBUFFER8 iface,LONG pan
727 HRESULT hres = DS_OK;
728 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
730 TRACE("(%p,%ld)\n",This,pan);
732 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
733 WARN("invalid parameter: pan = %ld\n", pan);
734 return DSERR_INVALIDPARAM;
737 /* You cannot use both pan and 3D controls */
738 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
739 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
740 WARN("control unavailable\n");
741 return DSERR_CONTROLUNAVAIL;
744 /* **** */
745 EnterCriticalSection(&(This->lock));
747 if (This->volpan.lPan != pan) {
748 This->volpan.lPan = pan;
749 DSOUND_RecalcVolPan(&(This->volpan));
751 if (This->hwbuf) {
752 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
753 if (hres != DS_OK)
754 WARN("IDsDriverBuffer_SetVolumePan failed\n");
755 } else
756 DSOUND_ForceRemix(This);
759 LeaveCriticalSection(&(This->lock));
760 /* **** */
762 return hres;
765 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
766 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
768 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
769 TRACE("(%p,%p)\n",This,pan);
771 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
772 WARN("control unavailable\n");
773 return DSERR_CONTROLUNAVAIL;
776 if (pan == NULL) {
777 WARN("invalid parameter: pan = NULL\n");
778 return DSERR_INVALIDPARAM;
781 *pan = This->volpan.lPan;
783 return DS_OK;
786 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
787 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
789 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
790 DWORD probably_valid_to;
791 HRESULT hres = DS_OK;
793 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
795 /* **** */
796 EnterCriticalSection(&(This->lock));
798 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
799 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
800 if (hres != DS_OK)
801 WARN("IDsDriverBuffer_Unlock failed\n");
804 if (hres == DS_OK) {
805 if (p2) probably_valid_to = (((LPBYTE)p2)-This->buffer->memory) + x2;
806 else probably_valid_to = (((LPBYTE)p1)-This->buffer->memory) + x1;
807 probably_valid_to %= This->buflen;
808 if ((probably_valid_to == 0) && ((x1+x2) == This->buflen) &&
809 ((This->state == STATE_STARTING) ||
810 (This->state == STATE_PLAYING)))
811 /* see IDirectSoundBufferImpl_Lock */
812 probably_valid_to = (DWORD)-1;
813 This->probably_valid_to = probably_valid_to;
816 LeaveCriticalSection(&(This->lock));
817 /* **** */
819 TRACE("probably_valid_to=%ld\n", This->probably_valid_to);
820 return hres;
823 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
824 LPDIRECTSOUNDBUFFER8 iface
826 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
827 FIXME("(%p):stub\n",This);
828 return DS_OK;
831 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
832 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
834 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
835 TRACE("(%p,%p)\n",This,freq);
837 if (freq == NULL) {
838 WARN("invalid parameter: freq = NULL\n");
839 return DSERR_INVALIDPARAM;
842 *freq = This->freq;
843 TRACE("-> %ld\n", *freq);
845 return DS_OK;
848 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
849 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
851 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
852 DWORD u;
854 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
856 if (pdwResultCodes)
857 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
859 WARN("control unavailable\n");
860 return DSERR_CONTROLUNAVAIL;
863 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
864 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
866 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
867 DWORD u;
869 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
871 if (pdwResultCodes)
872 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
874 WARN("control unavailable\n");
875 return DSERR_CONTROLUNAVAIL;
878 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
879 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
881 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
883 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
885 WARN("control unavailable\n");
886 return DSERR_CONTROLUNAVAIL;
889 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
890 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
892 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
893 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
894 DPRINTF("Re-Init!!!\n");
895 WARN("already initialized\n");
896 return DSERR_ALREADYINITIALIZED;
899 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
900 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
902 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
903 TRACE("(%p)->(%p)\n",This,caps);
905 if (caps == NULL) {
906 WARN("invalid parameter: caps == NULL\n");
907 return DSERR_INVALIDPARAM;
910 if (caps->dwSize < sizeof(*caps)) {
911 WARN("invalid parameter: caps->dwSize = %ld < %d\n",caps->dwSize, sizeof(*caps));
912 return DSERR_INVALIDPARAM;
915 caps->dwFlags = This->dsbd.dwFlags;
916 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
917 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
919 caps->dwBufferBytes = This->buflen;
921 /* This value represents the speed of the "unlock" command.
922 As unlock is quite fast (it does not do anything), I put
923 4096 ko/s = 4 Mo / s */
924 /* FIXME: hwbuf speed */
925 caps->dwUnlockTransferRate = 4096;
926 caps->dwPlayCpuOverhead = 0;
928 return DS_OK;
931 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
932 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
934 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
936 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
938 if (ppobj == NULL) {
939 WARN("invalid parameter\n");
940 return E_INVALIDARG;
943 *ppobj = NULL; /* assume failure */
945 if ( IsEqualGUID(riid, &IID_IUnknown) ||
946 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
947 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
948 if (!This->dsb)
949 SecondaryBufferImpl_Create(This, &(This->dsb));
950 if (This->dsb) {
951 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->dsb);
952 *ppobj = This->dsb;
953 return S_OK;
955 WARN("IID_IDirectSoundBuffer\n");
956 return E_NOINTERFACE;
959 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
960 if (!This->notify)
961 IDirectSoundNotifyImpl_Create(This, &(This->notify));
962 if (This->notify) {
963 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
964 *ppobj = This->notify;
965 return S_OK;
967 WARN("IID_IDirectSoundNotify\n");
968 return E_NOINTERFACE;
971 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
972 if (!This->ds3db)
973 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
974 if (This->ds3db) {
975 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
976 *ppobj = This->ds3db;
977 return S_OK;
979 WARN("IID_IDirectSound3DBuffer\n");
980 return E_NOINTERFACE;
983 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
984 ERR("app requested IDirectSound3DListener on secondary buffer\n");
985 return E_NOINTERFACE;
988 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
989 if (!This->iks)
990 IKsBufferPropertySetImpl_Create(This, &(This->iks));
991 if (This->iks) {
992 IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
993 *ppobj = This->iks;
994 return S_OK;
996 WARN("IID_IKsPropertySet\n");
997 return E_NOINTERFACE;
1000 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1002 return E_NOINTERFACE;
1005 static IDirectSoundBuffer8Vtbl dsbvt =
1007 IDirectSoundBufferImpl_QueryInterface,
1008 IDirectSoundBufferImpl_AddRef,
1009 IDirectSoundBufferImpl_Release,
1010 IDirectSoundBufferImpl_GetCaps,
1011 IDirectSoundBufferImpl_GetCurrentPosition,
1012 IDirectSoundBufferImpl_GetFormat,
1013 IDirectSoundBufferImpl_GetVolume,
1014 IDirectSoundBufferImpl_GetPan,
1015 IDirectSoundBufferImpl_GetFrequency,
1016 IDirectSoundBufferImpl_GetStatus,
1017 IDirectSoundBufferImpl_Initialize,
1018 IDirectSoundBufferImpl_Lock,
1019 IDirectSoundBufferImpl_Play,
1020 IDirectSoundBufferImpl_SetCurrentPosition,
1021 IDirectSoundBufferImpl_SetFormat,
1022 IDirectSoundBufferImpl_SetVolume,
1023 IDirectSoundBufferImpl_SetPan,
1024 IDirectSoundBufferImpl_SetFrequency,
1025 IDirectSoundBufferImpl_Stop,
1026 IDirectSoundBufferImpl_Unlock,
1027 IDirectSoundBufferImpl_Restore,
1028 IDirectSoundBufferImpl_SetFX,
1029 IDirectSoundBufferImpl_AcquireResources,
1030 IDirectSoundBufferImpl_GetObjectInPath
1033 HRESULT WINAPI IDirectSoundBufferImpl_Create(
1034 IDirectSoundImpl *ds,
1035 IDirectSoundBufferImpl **pdsb,
1036 LPCDSBUFFERDESC dsbd)
1038 IDirectSoundBufferImpl *dsb;
1039 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1040 HRESULT err = DS_OK;
1041 DWORD capf = 0;
1042 int use_hw, alloc_size, cp_size;
1043 TRACE("(%p,%p,%p)\n",ds,pdsb,dsbd);
1045 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1046 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
1047 *pdsb = NULL;
1048 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1051 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1053 if (dsb == 0) {
1054 WARN("out of memory\n");
1055 *pdsb = NULL;
1056 return DSERR_OUTOFMEMORY;
1059 TRACE("Created buffer at %p\n", dsb);
1061 dsb->ref = 0;
1062 dsb->dsb = 0;
1063 dsb->dsound = ds;
1064 dsb->lpVtbl = &dsbvt;
1065 dsb->iks = NULL;
1067 /* size depends on version */
1068 memcpy(&dsb->dsbd, dsbd, dsbd->dwSize);
1070 /* variable sized struct so calculate size based on format */
1071 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1072 alloc_size = sizeof(WAVEFORMATEX);
1073 cp_size = sizeof(PCMWAVEFORMAT);
1074 } else
1075 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
1077 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
1078 if (dsb->pwfx == NULL) {
1079 WARN("out of memory\n");
1080 HeapFree(GetProcessHeap(),0,dsb);
1081 *pdsb = NULL;
1082 return DSERR_OUTOFMEMORY;
1085 memcpy(dsb->pwfx, wfex, cp_size);
1087 dsb->buflen = dsbd->dwBufferBytes;
1088 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1090 dsb->notify = NULL;
1091 dsb->notifies = NULL;
1092 dsb->nrofnotifies = 0;
1093 dsb->hwnotify = 0;
1095 /* Check necessary hardware mixing capabilities */
1096 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1097 else capf |= DSCAPS_SECONDARYMONO;
1098 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1099 else capf |= DSCAPS_SECONDARY8BIT;
1101 use_hw = (ds->drvcaps.dwFlags & capf) == capf;
1102 TRACE("use_hw = 0x%08x, capf = 0x%08lx, ds->drvcaps.dwFlags = 0x%08lx\n", use_hw, capf, ds->drvcaps.dwFlags);
1104 /* FIXME: check hardware sample rate mixing capabilities */
1105 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1106 /* FIXME: check whether any hardware buffers are left */
1107 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1109 /* Allocate system memory if applicable */
1110 if ((ds->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1111 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1112 if (dsb->buffer == NULL) {
1113 WARN("out of memory\n");
1114 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1115 HeapFree(GetProcessHeap(),0,dsb);
1116 *pdsb = NULL;
1117 return DSERR_OUTOFMEMORY;
1120 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1121 if (dsb->buffer->memory == NULL) {
1122 WARN("out of memory\n");
1123 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1124 HeapFree(GetProcessHeap(),0,dsb->buffer);
1125 HeapFree(GetProcessHeap(),0,dsb);
1126 *pdsb = NULL;
1127 return DSERR_OUTOFMEMORY;
1129 dsb->buffer->ref = 1;
1132 /* Allocate the hardware buffer */
1133 if (use_hw) {
1134 err = IDsDriver_CreateSoundBuffer(ds->driver,wfex,dsbd->dwFlags,0,
1135 &(dsb->buflen),&(dsb->buffer->memory),
1136 (LPVOID*)&(dsb->hwbuf));
1137 /* fall back to software buffer on failure */
1138 if (err != DS_OK) {
1139 TRACE("IDsDriver_CreateSoundBuffer failed, falling back to software buffer\n");
1140 use_hw = 0;
1141 if (ds->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1142 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1143 if (dsb->buffer == NULL) {
1144 WARN("out of memory\n");
1145 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1146 HeapFree(GetProcessHeap(),0,dsb);
1147 *pdsb = NULL;
1148 return DSERR_OUTOFMEMORY;
1151 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1152 if (dsb->buffer->memory == NULL) {
1153 WARN("out of memory\n");
1154 HeapFree(GetProcessHeap(),0,dsb->buffer);
1155 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1156 HeapFree(GetProcessHeap(),0,dsb);
1157 *pdsb = NULL;
1158 return DSERR_OUTOFMEMORY;
1160 dsb->buffer->ref = 1;
1162 err = DS_OK;
1166 /* calculate fragment size and write lead */
1167 DSOUND_RecalcFormat(dsb);
1169 /* It's not necessary to initialize values to zero since */
1170 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1171 dsb->playpos = 0;
1172 dsb->buf_mixpos = 0;
1173 dsb->state = STATE_STOPPED;
1175 dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
1176 ds->pwfx->nSamplesPerSec;
1177 dsb->nAvgBytesPerSec = dsb->freq *
1178 dsbd->lpwfxFormat->nBlockAlign;
1180 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1181 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1182 dsb->ds3db_ds3db.vPosition.x = 0.0;
1183 dsb->ds3db_ds3db.vPosition.y = 0.0;
1184 dsb->ds3db_ds3db.vPosition.z = 0.0;
1185 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1186 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1187 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1188 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1189 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1190 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1191 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1192 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1193 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1194 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1195 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1196 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1198 dsb->ds3db_need_recalc = FALSE;
1199 DSOUND_Calc3DBuffer(dsb);
1200 } else
1201 DSOUND_RecalcVolPan(&(dsb->volpan));
1203 InitializeCriticalSection(&(dsb->lock));
1204 dsb->lock.DebugInfo->Spare[1] = (DWORD)"DSOUNDBUFFER_lock";
1206 /* register buffer if not primary */
1207 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1208 err = DSOUND_AddBuffer(ds, dsb);
1209 if (err != DS_OK) {
1210 if (dsb->buffer->memory)
1211 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1212 if (dsb->buffer)
1213 HeapFree(GetProcessHeap(),0,dsb->buffer);
1214 dsb->lock.DebugInfo->Spare[1] = 0;
1215 DeleteCriticalSection(&(dsb->lock));
1216 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1217 HeapFree(GetProcessHeap(),0,dsb);
1218 dsb = NULL;
1222 *pdsb = dsb;
1223 return err;
1226 HRESULT WINAPI IDirectSoundBufferImpl_Destroy(
1227 IDirectSoundBufferImpl *pdsb)
1229 TRACE("(%p)\n",pdsb);
1231 /* This keeps the *_Destroy functions from possibly deleting
1232 * this object until it is ready to be deleted */
1233 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1235 if (pdsb->iks) {
1236 WARN("iks not NULL\n");
1237 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1238 pdsb->iks = NULL;
1241 if (pdsb->ds3db) {
1242 WARN("ds3db not NULL\n");
1243 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1244 pdsb->ds3db = NULL;
1247 if (pdsb->notify) {
1248 WARN("notify not NULL\n");
1249 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1250 pdsb->notify = NULL;
1253 if (pdsb->dsb) {
1254 WARN("dsb not NULL\n");
1255 SecondaryBufferImpl_Destroy(pdsb->dsb);
1256 pdsb->dsb = NULL;
1259 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1261 return S_OK;
1264 /*******************************************************************************
1265 * SecondaryBuffer
1268 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1269 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1271 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1272 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1274 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1277 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1279 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
1280 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1281 return InterlockedIncrement(&(This->ref));
1284 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1286 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
1287 ULONG ref;
1288 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1290 ref = InterlockedDecrement(&(This->ref));
1291 if (!ref) {
1292 This->dsb->dsb = NULL;
1293 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1294 HeapFree(GetProcessHeap(),0,This);
1295 TRACE("(%p) released\n",This);
1297 return ref;
1300 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1301 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1303 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1304 TRACE("(%p)->(%p)\n",This,caps);
1306 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1309 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1310 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1312 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1313 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1315 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1318 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1319 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1321 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1322 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1324 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1327 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1328 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1330 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1331 TRACE("(%p,%p)\n",This,vol);
1333 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1336 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1337 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1339 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1340 TRACE("(%p,%p)\n",This,pan);
1342 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1345 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1346 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1348 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1349 TRACE("(%p,%p)\n",This,freq);
1351 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1354 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1355 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1357 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1358 TRACE("(%p,%p)\n",This,status);
1360 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1363 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1364 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1366 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1367 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1369 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1372 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1373 LPDIRECTSOUNDBUFFER8 iface,
1374 DWORD writecursor,
1375 DWORD writebytes,
1376 LPVOID lplpaudioptr1,
1377 LPDWORD audiobytes1,
1378 LPVOID lplpaudioptr2,
1379 LPDWORD audiobytes2,
1380 DWORD dwFlags)
1382 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1383 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1384 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1386 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1387 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1390 static HRESULT WINAPI SecondaryBufferImpl_Play(
1391 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1393 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1394 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
1396 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1399 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1400 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1402 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1403 TRACE("(%p,%ld)\n",This,newpos);
1405 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1408 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1409 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1411 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1412 TRACE("(%p,%p)\n",This,wfex);
1414 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1417 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1418 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1420 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1421 TRACE("(%p,%ld)\n",This,vol);
1423 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1426 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1427 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1429 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1430 TRACE("(%p,%ld)\n",This,pan);
1432 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1435 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1436 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1438 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1439 TRACE("(%p,%ld)\n",This,freq);
1441 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1444 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1446 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1447 TRACE("(%p)\n",This);
1449 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1452 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1453 LPDIRECTSOUNDBUFFER8 iface,
1454 LPVOID lpvAudioPtr1,
1455 DWORD dwAudioBytes1,
1456 LPVOID lpvAudioPtr2,
1457 DWORD dwAudioBytes2)
1459 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1460 TRACE("(%p,%p,%ld,%p,%ld)\n",
1461 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1463 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1464 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1467 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1468 LPDIRECTSOUNDBUFFER8 iface)
1470 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1471 TRACE("(%p)\n",This);
1473 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1476 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1477 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1479 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1480 TRACE("(%p,%lu,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1482 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1485 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1486 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1488 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1489 TRACE("(%p,%08lu,%lu,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1491 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1494 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1495 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1497 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1498 TRACE("(%p,%s,%lu,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1500 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1503 static IDirectSoundBuffer8Vtbl sbvt =
1505 SecondaryBufferImpl_QueryInterface,
1506 SecondaryBufferImpl_AddRef,
1507 SecondaryBufferImpl_Release,
1508 SecondaryBufferImpl_GetCaps,
1509 SecondaryBufferImpl_GetCurrentPosition,
1510 SecondaryBufferImpl_GetFormat,
1511 SecondaryBufferImpl_GetVolume,
1512 SecondaryBufferImpl_GetPan,
1513 SecondaryBufferImpl_GetFrequency,
1514 SecondaryBufferImpl_GetStatus,
1515 SecondaryBufferImpl_Initialize,
1516 SecondaryBufferImpl_Lock,
1517 SecondaryBufferImpl_Play,
1518 SecondaryBufferImpl_SetCurrentPosition,
1519 SecondaryBufferImpl_SetFormat,
1520 SecondaryBufferImpl_SetVolume,
1521 SecondaryBufferImpl_SetPan,
1522 SecondaryBufferImpl_SetFrequency,
1523 SecondaryBufferImpl_Stop,
1524 SecondaryBufferImpl_Unlock,
1525 SecondaryBufferImpl_Restore,
1526 SecondaryBufferImpl_SetFX,
1527 SecondaryBufferImpl_AcquireResources,
1528 SecondaryBufferImpl_GetObjectInPath
1531 HRESULT WINAPI SecondaryBufferImpl_Create(
1532 IDirectSoundBufferImpl *dsb,
1533 SecondaryBufferImpl **psb)
1535 SecondaryBufferImpl *sb;
1536 TRACE("(%p,%p)\n",dsb,psb);
1538 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1540 if (sb == 0) {
1541 WARN("out of memory\n");
1542 *psb = NULL;
1543 return DSERR_OUTOFMEMORY;
1545 sb->ref = 0;
1546 sb->dsb = dsb;
1547 sb->lpVtbl = &sbvt;
1549 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1550 *psb = sb;
1551 return S_OK;
1554 HRESULT WINAPI SecondaryBufferImpl_Destroy(
1555 SecondaryBufferImpl *pdsb)
1557 TRACE("(%p)\n",pdsb);
1559 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1561 return S_OK;