push e7552b73e75828caa36d732caa646c0c70d8de9d
[wine/hacks.git] / dlls / dsound / buffer.c
blob251c436d14aefce5728d948e0085f8a9d8c7b13b
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "mmsystem.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32 #include "dsound.h"
33 #include "dsdriver.h"
34 #include "dsound_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
38 static HRESULT SecondaryBufferImpl_Destroy(SecondaryBufferImpl *pdsb);
40 /*******************************************************************************
41 * IDirectSoundNotify
44 struct IDirectSoundNotifyImpl
46 /* IUnknown fields */
47 const IDirectSoundNotifyVtbl *lpVtbl;
48 LONG ref;
49 IDirectSoundBufferImpl* dsb;
52 static HRESULT IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl *dsb,
53 IDirectSoundNotifyImpl **pdsn);
54 static HRESULT IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl *pdsn);
56 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
57 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
58 ) {
59 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
60 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
62 if (This->dsb == NULL) {
63 WARN("invalid parameter\n");
64 return E_INVALIDARG;
67 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
70 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
72 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
73 ULONG ref = InterlockedIncrement(&(This->ref));
74 TRACE("(%p) ref was %d\n", This, ref - 1);
75 return ref;
78 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
80 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
81 ULONG ref = InterlockedDecrement(&(This->ref));
82 TRACE("(%p) ref was %d\n", This, ref + 1);
84 if (!ref) {
85 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
86 This->dsb->notify = NULL;
87 HeapFree(GetProcessHeap(), 0, This);
88 TRACE("(%p) released\n", This);
90 return ref;
93 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
94 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
95 ) {
96 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
97 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
99 if (howmuch > 0 && notify == NULL) {
100 WARN("invalid parameter: notify == NULL\n");
101 return DSERR_INVALIDPARAM;
104 if (TRACE_ON(dsound)) {
105 unsigned int i;
106 for (i=0;i<howmuch;i++)
107 TRACE("notify at %d to %p\n",
108 notify[i].dwOffset,notify[i].hEventNotify);
111 if (This->dsb->hwnotify) {
112 HRESULT hres;
113 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
114 if (hres != DS_OK)
115 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
116 return hres;
117 } else if (howmuch > 0) {
118 /* Make an internal copy of the caller-supplied array.
119 * Replace the existing copy if one is already present. */
120 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
121 This->dsb->notifies = HeapAlloc(GetProcessHeap(), 0,
122 howmuch * sizeof(DSBPOSITIONNOTIFY));
124 if (This->dsb->notifies == NULL) {
125 WARN("out of memory\n");
126 return DSERR_OUTOFMEMORY;
128 CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
129 This->dsb->nrofnotifies = howmuch;
130 } else {
131 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
132 This->dsb->notifies = NULL;
133 This->dsb->nrofnotifies = 0;
136 return S_OK;
139 static const IDirectSoundNotifyVtbl dsnvt =
141 IDirectSoundNotifyImpl_QueryInterface,
142 IDirectSoundNotifyImpl_AddRef,
143 IDirectSoundNotifyImpl_Release,
144 IDirectSoundNotifyImpl_SetNotificationPositions,
147 static HRESULT IDirectSoundNotifyImpl_Create(
148 IDirectSoundBufferImpl * dsb,
149 IDirectSoundNotifyImpl **pdsn)
151 IDirectSoundNotifyImpl * dsn;
152 TRACE("(%p,%p)\n",dsb,pdsn);
154 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dsn));
156 if (dsn == NULL) {
157 WARN("out of memory\n");
158 return DSERR_OUTOFMEMORY;
161 dsn->ref = 0;
162 dsn->lpVtbl = &dsnvt;
163 dsn->dsb = dsb;
164 dsb->notify = dsn;
165 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
167 *pdsn = dsn;
168 return DS_OK;
171 static HRESULT IDirectSoundNotifyImpl_Destroy(
172 IDirectSoundNotifyImpl *pdsn)
174 TRACE("(%p)\n",pdsn);
176 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
178 return DS_OK;
181 /*******************************************************************************
182 * IDirectSoundBuffer
185 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
186 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
188 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
190 TRACE("(%p,%p)\n",This,wfex);
191 /* This method is not available on secondary buffers */
192 WARN("invalid call\n");
193 return DSERR_INVALIDCALL;
196 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
197 LPDIRECTSOUNDBUFFER8 iface,LONG vol
199 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
200 LONG oldVol;
201 HRESULT hres = DS_OK;
203 TRACE("(%p,%d)\n",This,vol);
205 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
206 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
207 return DSERR_CONTROLUNAVAIL;
210 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
211 WARN("invalid parameter: vol = %d\n", vol);
212 return DSERR_INVALIDPARAM;
215 /* **** */
216 RtlAcquireResourceExclusive(&This->lock, TRUE);
218 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
219 oldVol = This->ds3db_lVolume;
220 This->ds3db_lVolume = vol;
221 if (vol != oldVol)
222 /* recalc 3d volume, which in turn recalcs the pans */
223 DSOUND_Calc3DBuffer(This);
224 } else {
225 oldVol = This->volpan.lVolume;
226 This->volpan.lVolume = vol;
227 if (vol != oldVol)
228 DSOUND_RecalcVolPan(&(This->volpan));
231 if (vol != oldVol) {
232 if (This->hwbuf) {
233 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
234 if (hres != DS_OK)
235 WARN("IDsDriverBuffer_SetVolumePan failed\n");
239 RtlReleaseResource(&This->lock);
240 /* **** */
242 return hres;
245 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
246 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
248 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
249 TRACE("(%p,%p)\n",This,vol);
251 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
252 WARN("control unavailable\n");
253 return DSERR_CONTROLUNAVAIL;
256 if (vol == NULL) {
257 WARN("invalid parameter: vol == NULL\n");
258 return DSERR_INVALIDPARAM;
261 *vol = This->volpan.lVolume;
263 return DS_OK;
266 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
267 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
269 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
270 DWORD oldFreq;
272 TRACE("(%p,%d)\n",This,freq);
274 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
275 WARN("control unavailable\n");
276 return DSERR_CONTROLUNAVAIL;
279 if (freq == DSBFREQUENCY_ORIGINAL)
280 freq = This->pwfx->nSamplesPerSec;
282 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
283 WARN("invalid parameter: freq = %d\n", freq);
284 return DSERR_INVALIDPARAM;
287 /* **** */
288 RtlAcquireResourceExclusive(&This->lock, TRUE);
290 oldFreq = This->freq;
291 This->freq = freq;
292 if (freq != oldFreq) {
293 This->freqAdjust = ((DWORD64)This->freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
294 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
295 DSOUND_RecalcFormat(This);
296 DSOUND_MixToTemporary(This, 0, This->buflen, FALSE);
299 RtlReleaseResource(&This->lock);
300 /* **** */
302 return DS_OK;
305 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
306 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
308 HRESULT hres = DS_OK;
309 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
310 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
312 /* **** */
313 RtlAcquireResourceExclusive(&This->lock, TRUE);
315 This->playflags = flags;
316 if (This->state == STATE_STOPPED && !This->hwbuf) {
317 This->leadin = TRUE;
318 This->state = STATE_STARTING;
319 } else if (This->state == STATE_STOPPING)
320 This->state = STATE_PLAYING;
321 if (This->hwbuf) {
322 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
323 if (hres != DS_OK)
324 WARN("IDsDriverBuffer_Play failed\n");
325 else
326 This->state = STATE_PLAYING;
329 RtlReleaseResource(&This->lock);
330 /* **** */
332 return hres;
335 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
337 HRESULT hres = DS_OK;
338 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
339 TRACE("(%p)\n",This);
341 /* **** */
342 RtlAcquireResourceExclusive(&This->lock, TRUE);
344 if (This->state == STATE_PLAYING)
345 This->state = STATE_STOPPING;
346 else if (This->state == STATE_STARTING)
348 This->state = STATE_STOPPED;
349 DSOUND_CheckEvent(This, 0, 0);
351 if (This->hwbuf) {
352 hres = IDsDriverBuffer_Stop(This->hwbuf);
353 if (hres != DS_OK)
354 WARN("IDsDriverBuffer_Stop failed\n");
355 else
356 This->state = STATE_STOPPED;
359 RtlReleaseResource(&This->lock);
360 /* **** */
362 return hres;
365 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
367 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
368 ULONG ref = InterlockedIncrement(&(This->ref));
369 TRACE("(%p) ref was %d\n", This, ref - 1);
370 return ref;
373 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
375 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
376 ULONG ref = InterlockedDecrement(&(This->ref));
377 TRACE("(%p) ref was %d\n", This, ref + 1);
379 if (!ref) {
380 DirectSoundDevice_RemoveBuffer(This->device, This);
381 RtlDeleteResource(&This->lock);
383 if (This->hwbuf)
384 IDsDriverBuffer_Release(This->hwbuf);
385 if (!This->hwbuf || (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)) {
386 This->buffer->ref--;
387 list_remove(&This->entry);
388 if (This->buffer->ref==0) {
389 HeapFree(GetProcessHeap(),0,This->buffer->memory);
390 HeapFree(GetProcessHeap(),0,This->buffer);
394 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
395 HeapFree(GetProcessHeap(), 0, This->notifies);
396 HeapFree(GetProcessHeap(), 0, This->pwfx);
397 HeapFree(GetProcessHeap(), 0, This);
399 TRACE("(%p) released\n", This);
401 return ref;
404 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
405 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
407 HRESULT hres;
408 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
409 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
411 RtlAcquireResourceShared(&This->lock, TRUE);
412 if (This->hwbuf) {
413 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
414 if (hres != DS_OK) {
415 WARN("IDsDriverBuffer_GetPosition failed\n");
416 return hres;
418 } else {
419 DWORD pos = This->sec_mixpos;
421 /* sanity */
422 if (pos >= This->buflen){
423 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
424 pos %= This->buflen;
427 if (playpos)
428 *playpos = pos;
429 if (writepos)
430 *writepos = pos;
432 if (writepos && This->state != STATE_STOPPED && (!This->hwbuf || !(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD))) {
433 /* apply the documented 10ms lead to writepos */
434 *writepos += This->writelead;
435 *writepos %= This->buflen;
437 RtlReleaseResource(&This->lock);
439 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
440 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
442 return DS_OK;
445 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
446 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
448 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
449 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
451 if (status == NULL) {
452 WARN("invalid parameter: status = NULL\n");
453 return DSERR_INVALIDPARAM;
456 *status = 0;
457 RtlAcquireResourceShared(&This->lock, TRUE);
458 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
459 *status |= DSBSTATUS_PLAYING;
460 if (This->playflags & DSBPLAY_LOOPING)
461 *status |= DSBSTATUS_LOOPING;
463 RtlReleaseResource(&This->lock);
465 TRACE("status=%x\n", *status);
466 return DS_OK;
470 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
471 LPDIRECTSOUNDBUFFER8 iface,
472 LPWAVEFORMATEX lpwf,
473 DWORD wfsize,
474 LPDWORD wfwritten)
476 DWORD size;
477 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
478 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
480 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
482 if (lpwf) { /* NULL is valid */
483 if (wfsize >= size) {
484 CopyMemory(lpwf,This->pwfx,size);
485 if (wfwritten)
486 *wfwritten = size;
487 } else {
488 WARN("invalid parameter: wfsize too small\n");
489 CopyMemory(lpwf,This->pwfx,wfsize);
490 if (wfwritten)
491 *wfwritten = wfsize;
492 return DSERR_INVALIDPARAM;
494 } else {
495 if (wfwritten)
496 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
497 else {
498 WARN("invalid parameter: wfwritten == NULL\n");
499 return DSERR_INVALIDPARAM;
503 return DS_OK;
506 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
507 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
509 HRESULT hres = DS_OK;
510 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
512 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
513 This,
514 writecursor,
515 writebytes,
516 lplpaudioptr1,
517 audiobytes1,
518 lplpaudioptr2,
519 audiobytes2,
520 flags,
521 GetTickCount()
524 if (!audiobytes1)
525 return DSERR_INVALIDPARAM;
527 /* when this flag is set, writecursor is meaningless and must be calculated */
528 if (flags & DSBLOCK_FROMWRITECURSOR) {
529 /* GetCurrentPosition does too much magic to duplicate here */
530 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
531 if (hres != DS_OK) {
532 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
533 return hres;
537 /* when this flag is set, writebytes is meaningless and must be set */
538 if (flags & DSBLOCK_ENTIREBUFFER)
539 writebytes = This->buflen;
541 if (writecursor >= This->buflen) {
542 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
543 writecursor, This->buflen);
544 return DSERR_INVALIDPARAM;
547 if (writebytes > This->buflen) {
548 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
549 writebytes, This->buflen);
550 return DSERR_INVALIDPARAM;
553 /* **** */
554 RtlAcquireResourceShared(&This->lock, TRUE);
556 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
557 hres = IDsDriverBuffer_Lock(This->hwbuf,
558 lplpaudioptr1, audiobytes1,
559 lplpaudioptr2, audiobytes2,
560 writecursor, writebytes,
562 if (hres != DS_OK) {
563 WARN("IDsDriverBuffer_Lock failed\n");
564 RtlReleaseResource(&This->lock);
565 return hres;
567 } else {
568 if (writecursor+writebytes <= This->buflen) {
569 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
570 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
571 WARN("Overwriting mixing position, case 1\n");
572 *audiobytes1 = writebytes;
573 if (lplpaudioptr2)
574 *(LPBYTE*)lplpaudioptr2 = NULL;
575 if (audiobytes2)
576 *audiobytes2 = 0;
577 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
578 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
579 TRACE("->%d.0\n",writebytes);
580 } else {
581 DWORD remainder = writebytes + writecursor - This->buflen;
582 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
583 *audiobytes1 = This->buflen-writecursor;
584 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
585 WARN("Overwriting mixing position, case 2\n");
586 if (lplpaudioptr2)
587 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
588 if (audiobytes2)
589 *audiobytes2 = writebytes-(This->buflen-writecursor);
590 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
591 WARN("Overwriting mixing position, case 3\n");
592 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
596 RtlReleaseResource(&This->lock);
597 /* **** */
599 return DS_OK;
602 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
603 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
605 HRESULT hres = DS_OK;
606 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
607 DWORD oldpos;
608 TRACE("(%p,%d)\n",This,newpos);
610 /* **** */
611 RtlAcquireResourceExclusive(&This->lock, TRUE);
613 oldpos = This->sec_mixpos;
615 /* start mixing from this new location instead */
616 newpos %= This->buflen;
617 newpos -= newpos%This->pwfx->nBlockAlign;
618 This->sec_mixpos = newpos;
620 /* at this point, do not attempt to reset buffers, mess with primary mix position,
621 or anything like that to reduce latancy. The data already prebuffered cannot be changed */
623 /* position HW buffer if applicable, else just start mixing from new location instead */
624 if (This->hwbuf) {
625 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
626 if (hres != DS_OK)
627 WARN("IDsDriverBuffer_SetPosition failed\n");
629 else if (oldpos != newpos)
630 /* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
631 This->buf_mixpos = DSOUND_secpos_to_bufpos(This, newpos, 0, NULL);
633 RtlReleaseResource(&This->lock);
634 /* **** */
636 return hres;
639 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
640 LPDIRECTSOUNDBUFFER8 iface,LONG pan
642 HRESULT hres = DS_OK;
643 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
645 TRACE("(%p,%d)\n",This,pan);
647 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
648 WARN("invalid parameter: pan = %d\n", pan);
649 return DSERR_INVALIDPARAM;
652 /* You cannot use both pan and 3D controls */
653 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
654 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
655 WARN("control unavailable\n");
656 return DSERR_CONTROLUNAVAIL;
659 /* **** */
660 RtlAcquireResourceExclusive(&This->lock, TRUE);
662 if (This->volpan.lPan != pan) {
663 This->volpan.lPan = pan;
664 DSOUND_RecalcVolPan(&(This->volpan));
666 if (This->hwbuf) {
667 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
668 if (hres != DS_OK)
669 WARN("IDsDriverBuffer_SetVolumePan failed\n");
673 RtlReleaseResource(&This->lock);
674 /* **** */
676 return hres;
679 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
680 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
682 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
683 TRACE("(%p,%p)\n",This,pan);
685 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
686 WARN("control unavailable\n");
687 return DSERR_CONTROLUNAVAIL;
690 if (pan == NULL) {
691 WARN("invalid parameter: pan = NULL\n");
692 return DSERR_INVALIDPARAM;
695 *pan = This->volpan.lPan;
697 return DS_OK;
700 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
701 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
703 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface, *iter;
704 HRESULT hres = DS_OK;
706 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
708 /* **** */
709 RtlAcquireResourceShared(&This->lock, TRUE);
711 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
712 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
713 if (hres != DS_OK)
714 WARN("IDsDriverBuffer_Unlock failed\n");
717 RtlReleaseResource(&This->lock);
718 /* **** */
720 if (!p2)
721 x2 = 0;
723 if (!This->hwbuf && (x1 || x2))
725 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
726 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
728 RtlAcquireResourceShared(&iter->lock, TRUE);
729 if (x1)
731 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
732 hres = DSERR_INVALIDPARAM;
733 else
734 DSOUND_MixToTemporary(iter, (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory, x1, FALSE);
736 if (x2)
737 DSOUND_MixToTemporary(iter, 0, x2, FALSE);
738 RtlReleaseResource(&iter->lock);
740 RtlReleaseResource(&This->device->buffer_list_lock);
743 return hres;
746 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
747 LPDIRECTSOUNDBUFFER8 iface
749 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
750 FIXME("(%p):stub\n",This);
751 return DS_OK;
754 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
755 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
757 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
758 TRACE("(%p,%p)\n",This,freq);
760 if (freq == NULL) {
761 WARN("invalid parameter: freq = NULL\n");
762 return DSERR_INVALIDPARAM;
765 *freq = This->freq;
766 TRACE("-> %d\n", *freq);
768 return DS_OK;
771 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
772 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
774 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
775 DWORD u;
777 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
779 if (pdwResultCodes)
780 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
782 WARN("control unavailable\n");
783 return DSERR_CONTROLUNAVAIL;
786 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
787 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
789 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
790 DWORD u;
792 FIXME("(%p,%08u,%u,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
794 if (pdwResultCodes)
795 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
797 WARN("control unavailable\n");
798 return DSERR_CONTROLUNAVAIL;
801 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
802 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
804 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
806 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
808 WARN("control unavailable\n");
809 return DSERR_CONTROLUNAVAIL;
812 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
813 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
815 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
816 WARN("(%p) already initialized\n", This);
817 return DSERR_ALREADYINITIALIZED;
820 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
821 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
823 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
824 TRACE("(%p)->(%p)\n",This,caps);
826 if (caps == NULL) {
827 WARN("invalid parameter: caps == NULL\n");
828 return DSERR_INVALIDPARAM;
831 if (caps->dwSize < sizeof(*caps)) {
832 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
833 return DSERR_INVALIDPARAM;
836 caps->dwFlags = This->dsbd.dwFlags;
837 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
838 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
840 caps->dwBufferBytes = This->buflen;
842 /* According to windows, this is zero*/
843 caps->dwUnlockTransferRate = 0;
844 caps->dwPlayCpuOverhead = 0;
846 return DS_OK;
849 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
850 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
852 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
854 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
856 if (ppobj == NULL) {
857 WARN("invalid parameter\n");
858 return E_INVALIDARG;
861 *ppobj = NULL; /* assume failure */
863 if ( IsEqualGUID(riid, &IID_IUnknown) ||
864 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
865 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
866 if (!This->secondary)
867 SecondaryBufferImpl_Create(This, &(This->secondary));
868 if (This->secondary) {
869 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->secondary);
870 *ppobj = This->secondary;
871 return S_OK;
873 WARN("IID_IDirectSoundBuffer\n");
874 return E_NOINTERFACE;
877 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
878 if (!This->notify)
879 IDirectSoundNotifyImpl_Create(This, &(This->notify));
880 if (This->notify) {
881 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
882 *ppobj = This->notify;
883 return S_OK;
885 WARN("IID_IDirectSoundNotify\n");
886 return E_NOINTERFACE;
889 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
890 if (!This->ds3db)
891 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
892 if (This->ds3db) {
893 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
894 *ppobj = This->ds3db;
895 return S_OK;
897 WARN("IID_IDirectSound3DBuffer\n");
898 return E_NOINTERFACE;
901 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
902 ERR("app requested IDirectSound3DListener on secondary buffer\n");
903 return E_NOINTERFACE;
906 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
907 if (!This->iks)
908 IKsBufferPropertySetImpl_Create(This, &(This->iks));
909 if (This->iks) {
910 IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
911 *ppobj = This->iks;
912 return S_OK;
914 WARN("IID_IKsPropertySet\n");
915 return E_NOINTERFACE;
918 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
920 return E_NOINTERFACE;
923 static const IDirectSoundBuffer8Vtbl dsbvt =
925 IDirectSoundBufferImpl_QueryInterface,
926 IDirectSoundBufferImpl_AddRef,
927 IDirectSoundBufferImpl_Release,
928 IDirectSoundBufferImpl_GetCaps,
929 IDirectSoundBufferImpl_GetCurrentPosition,
930 IDirectSoundBufferImpl_GetFormat,
931 IDirectSoundBufferImpl_GetVolume,
932 IDirectSoundBufferImpl_GetPan,
933 IDirectSoundBufferImpl_GetFrequency,
934 IDirectSoundBufferImpl_GetStatus,
935 IDirectSoundBufferImpl_Initialize,
936 IDirectSoundBufferImpl_Lock,
937 IDirectSoundBufferImpl_Play,
938 IDirectSoundBufferImpl_SetCurrentPosition,
939 IDirectSoundBufferImpl_SetFormat,
940 IDirectSoundBufferImpl_SetVolume,
941 IDirectSoundBufferImpl_SetPan,
942 IDirectSoundBufferImpl_SetFrequency,
943 IDirectSoundBufferImpl_Stop,
944 IDirectSoundBufferImpl_Unlock,
945 IDirectSoundBufferImpl_Restore,
946 IDirectSoundBufferImpl_SetFX,
947 IDirectSoundBufferImpl_AcquireResources,
948 IDirectSoundBufferImpl_GetObjectInPath
951 HRESULT IDirectSoundBufferImpl_Create(
952 DirectSoundDevice * device,
953 IDirectSoundBufferImpl **pdsb,
954 LPCDSBUFFERDESC dsbd)
956 IDirectSoundBufferImpl *dsb;
957 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
958 HRESULT err = DS_OK;
959 DWORD capf = 0;
960 int use_hw, alloc_size, cp_size;
961 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
963 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
964 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
965 *pdsb = NULL;
966 return DSERR_INVALIDPARAM; /* FIXME: which error? */
969 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
971 if (dsb == 0) {
972 WARN("out of memory\n");
973 *pdsb = NULL;
974 return DSERR_OUTOFMEMORY;
977 TRACE("Created buffer at %p\n", dsb);
979 dsb->ref = 0;
980 dsb->secondary = 0;
981 dsb->device = device;
982 dsb->lpVtbl = &dsbvt;
983 dsb->iks = NULL;
985 /* size depends on version */
986 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
988 /* variable sized struct so calculate size based on format */
989 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
990 alloc_size = sizeof(WAVEFORMATEX);
991 cp_size = sizeof(PCMWAVEFORMAT);
992 } else
993 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
995 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
996 if (dsb->pwfx == NULL) {
997 WARN("out of memory\n");
998 HeapFree(GetProcessHeap(),0,dsb);
999 *pdsb = NULL;
1000 return DSERR_OUTOFMEMORY;
1003 CopyMemory(dsb->pwfx, wfex, cp_size);
1005 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1006 dsb->buflen = dsbd->dwBufferBytes +
1007 (dsbd->lpwfxFormat->nBlockAlign -
1008 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1009 else
1010 dsb->buflen = dsbd->dwBufferBytes;
1012 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1013 dsb->notify = NULL;
1014 dsb->notifies = NULL;
1015 dsb->nrofnotifies = 0;
1016 dsb->hwnotify = 0;
1018 /* Check necessary hardware mixing capabilities */
1019 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1020 else capf |= DSCAPS_SECONDARYMONO;
1021 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1022 else capf |= DSCAPS_SECONDARY8BIT;
1024 use_hw = !!(dsbd->dwFlags & DSBCAPS_LOCHARDWARE);
1025 TRACE("use_hw = %d, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw, capf, device->drvcaps.dwFlags);
1026 if (use_hw && ((device->drvcaps.dwFlags & capf) != capf || !device->driver))
1028 if (device->driver)
1029 WARN("Format not supported for hardware buffer\n");
1030 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1031 HeapFree(GetProcessHeap(),0,dsb);
1032 *pdsb = NULL;
1033 if ((device->drvcaps.dwFlags & capf) != capf)
1034 return DSERR_BADFORMAT;
1035 return DSERR_GENERIC;
1038 /* FIXME: check hardware sample rate mixing capabilities */
1039 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1040 /* FIXME: check whether any hardware buffers are left */
1041 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1043 /* Allocate an empty buffer */
1044 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1045 if (dsb->buffer == NULL) {
1046 WARN("out of memory\n");
1047 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1048 HeapFree(GetProcessHeap(),0,dsb);
1049 *pdsb = NULL;
1050 return DSERR_OUTOFMEMORY;
1053 /* Allocate system memory for buffer if applicable */
1054 if ((device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1055 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1056 if (dsb->buffer->memory == NULL) {
1057 WARN("out of memory\n");
1058 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1059 HeapFree(GetProcessHeap(),0,dsb->buffer);
1060 HeapFree(GetProcessHeap(),0,dsb);
1061 *pdsb = NULL;
1062 return DSERR_OUTOFMEMORY;
1066 /* Allocate the hardware buffer */
1067 if (use_hw) {
1068 err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1069 &(dsb->buflen),&(dsb->buffer->memory),
1070 (LPVOID*)&(dsb->hwbuf));
1071 if (FAILED(err))
1073 WARN("Failed to create hardware secondary buffer: %08x\n", err);
1074 if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)
1075 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1076 HeapFree(GetProcessHeap(),0,dsb->buffer);
1077 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1078 HeapFree(GetProcessHeap(),0,dsb);
1079 *pdsb = NULL;
1080 return DSERR_GENERIC;
1084 dsb->buffer->ref = 1;
1085 list_init(&dsb->buffer->buffers);
1086 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1087 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1089 /* It's not necessary to initialize values to zero since */
1090 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1091 dsb->buf_mixpos = dsb->sec_mixpos = 0;
1092 dsb->state = STATE_STOPPED;
1094 dsb->freqAdjust = ((DWORD64)dsb->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
1095 dsb->nAvgBytesPerSec = dsb->freq *
1096 dsbd->lpwfxFormat->nBlockAlign;
1098 /* calculate fragment size and write lead */
1099 DSOUND_RecalcFormat(dsb);
1101 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1102 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1103 dsb->ds3db_ds3db.vPosition.x = 0.0;
1104 dsb->ds3db_ds3db.vPosition.y = 0.0;
1105 dsb->ds3db_ds3db.vPosition.z = 0.0;
1106 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1107 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1108 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1109 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1110 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1111 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1112 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1113 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1114 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1115 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1116 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1117 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1119 dsb->ds3db_need_recalc = FALSE;
1120 DSOUND_Calc3DBuffer(dsb);
1121 } else
1122 DSOUND_RecalcVolPan(&(dsb->volpan));
1124 RtlInitializeResource(&dsb->lock);
1126 /* register buffer if not primary */
1127 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1128 err = DirectSoundDevice_AddBuffer(device, dsb);
1129 if (err != DS_OK) {
1130 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1131 HeapFree(GetProcessHeap(),0,dsb->buffer);
1132 RtlDeleteResource(&dsb->lock);
1133 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1134 HeapFree(GetProcessHeap(),0,dsb);
1135 dsb = NULL;
1139 *pdsb = dsb;
1140 return err;
1143 HRESULT IDirectSoundBufferImpl_Destroy(
1144 IDirectSoundBufferImpl *pdsb)
1146 TRACE("(%p)\n",pdsb);
1148 /* This keeps the *_Destroy functions from possibly deleting
1149 * this object until it is ready to be deleted */
1150 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1152 if (pdsb->iks) {
1153 WARN("iks not NULL\n");
1154 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1155 pdsb->iks = NULL;
1158 if (pdsb->ds3db) {
1159 WARN("ds3db not NULL\n");
1160 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1161 pdsb->ds3db = NULL;
1164 if (pdsb->notify) {
1165 WARN("notify not NULL\n");
1166 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1167 pdsb->notify = NULL;
1170 if (pdsb->secondary) {
1171 WARN("dsb not NULL\n");
1172 SecondaryBufferImpl_Destroy(pdsb->secondary);
1173 pdsb->secondary = NULL;
1176 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1178 return S_OK;
1181 HRESULT IDirectSoundBufferImpl_Duplicate(
1182 DirectSoundDevice *device,
1183 IDirectSoundBufferImpl **ppdsb,
1184 IDirectSoundBufferImpl *pdsb)
1186 IDirectSoundBufferImpl *dsb;
1187 HRESULT hres = DS_OK;
1188 int size;
1189 TRACE("(%p,%p,%p)\n", device, pdsb, pdsb);
1191 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1193 if (dsb == NULL) {
1194 WARN("out of memory\n");
1195 *ppdsb = NULL;
1196 return DSERR_OUTOFMEMORY;
1199 CopyMemory(dsb, pdsb, sizeof(IDirectSoundBufferImpl));
1201 if (pdsb->hwbuf) {
1202 TRACE("duplicating hardware buffer\n");
1204 hres = IDsDriver_DuplicateSoundBuffer(device->driver, pdsb->hwbuf,
1205 (LPVOID *)&dsb->hwbuf);
1206 if (FAILED(hres)) {
1207 WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres);
1208 HeapFree(GetProcessHeap(),0,dsb);
1209 *ppdsb = NULL;
1210 return hres;
1214 dsb->buffer->ref++;
1215 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1216 dsb->ref = 0;
1217 dsb->state = STATE_STOPPED;
1218 dsb->buf_mixpos = dsb->sec_mixpos = 0;
1219 dsb->device = device;
1220 dsb->ds3db = NULL;
1221 dsb->iks = NULL; /* FIXME? */
1222 dsb->secondary = NULL;
1223 dsb->tmp_buffer = NULL;
1224 DSOUND_RecalcFormat(dsb);
1225 DSOUND_MixToTemporary(dsb, 0, dsb->buflen, FALSE);
1227 /* variable sized struct so calculate size based on format */
1228 size = sizeof(WAVEFORMATEX) + pdsb->pwfx->cbSize;
1230 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
1231 if (dsb->pwfx == NULL) {
1232 WARN("out of memory\n");
1233 HeapFree(GetProcessHeap(),0,dsb->buffer);
1234 HeapFree(GetProcessHeap(),0,dsb);
1235 *ppdsb = NULL;
1236 return DSERR_OUTOFMEMORY;
1239 CopyMemory(dsb->pwfx, pdsb->pwfx, size);
1241 RtlInitializeResource(&dsb->lock);
1243 /* register buffer */
1244 hres = DirectSoundDevice_AddBuffer(device, dsb);
1245 if (hres != DS_OK) {
1246 RtlDeleteResource(&dsb->lock);
1247 HeapFree(GetProcessHeap(),0,dsb->tmp_buffer);
1248 HeapFree(GetProcessHeap(),0,dsb->buffer);
1249 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1250 HeapFree(GetProcessHeap(),0,dsb);
1251 *ppdsb = 0;
1254 *ppdsb = dsb;
1255 return hres;
1258 /*******************************************************************************
1259 * SecondaryBuffer
1262 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1263 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1265 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1266 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1268 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1271 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1273 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1274 ULONG ref = InterlockedIncrement(&(This->ref));
1275 TRACE("(%p) ref was %d\n", This, ref - 1);
1276 return ref;
1279 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1281 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1282 ULONG ref;
1283 TRACE("(%p)\n", This);
1284 ref = InterlockedDecrement(&(This->ref));
1285 TRACE("ref was %d\n", ref + 1);
1287 if (!ref) {
1288 This->dsb->secondary = NULL;
1289 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1290 HeapFree(GetProcessHeap(), 0, This);
1291 TRACE("(%p) released\n", This);
1293 return ref;
1296 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1297 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1299 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1300 TRACE("(%p)->(%p)\n",This,caps);
1302 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1305 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1306 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1308 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1309 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1311 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1314 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1315 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1317 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1318 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1320 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1323 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1324 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1326 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1327 TRACE("(%p,%p)\n",This,vol);
1329 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1332 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1333 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1335 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1336 TRACE("(%p,%p)\n",This,pan);
1338 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1341 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1342 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1344 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1345 TRACE("(%p,%p)\n",This,freq);
1347 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1350 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1351 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1353 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1354 TRACE("(%p,%p)\n",This,status);
1356 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1359 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1360 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1362 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1363 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1365 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1368 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1369 LPDIRECTSOUNDBUFFER8 iface,
1370 DWORD writecursor,
1371 DWORD writebytes,
1372 LPVOID *lplpaudioptr1,
1373 LPDWORD audiobytes1,
1374 LPVOID *lplpaudioptr2,
1375 LPDWORD audiobytes2,
1376 DWORD dwFlags)
1378 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1379 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1380 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1382 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1383 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1386 static HRESULT WINAPI SecondaryBufferImpl_Play(
1387 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1389 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1390 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1392 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1395 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1396 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1398 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1399 TRACE("(%p,%d)\n",This,newpos);
1401 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1404 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1405 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1407 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1408 TRACE("(%p,%p)\n",This,wfex);
1410 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1413 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1414 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1416 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1417 TRACE("(%p,%d)\n",This,vol);
1419 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1422 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1423 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1425 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1426 TRACE("(%p,%d)\n",This,pan);
1428 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1431 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1432 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1434 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1435 TRACE("(%p,%d)\n",This,freq);
1437 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1440 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1442 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1443 TRACE("(%p)\n",This);
1445 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1448 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1449 LPDIRECTSOUNDBUFFER8 iface,
1450 LPVOID lpvAudioPtr1,
1451 DWORD dwAudioBytes1,
1452 LPVOID lpvAudioPtr2,
1453 DWORD dwAudioBytes2)
1455 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1456 TRACE("(%p,%p,%d,%p,%d)\n",
1457 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1459 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1460 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1463 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1464 LPDIRECTSOUNDBUFFER8 iface)
1466 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1467 TRACE("(%p)\n",This);
1469 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1472 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1473 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1475 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1476 TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1478 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1481 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1482 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1484 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1485 TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1487 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1490 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1491 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1493 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1494 TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1496 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1499 static const IDirectSoundBuffer8Vtbl sbvt =
1501 SecondaryBufferImpl_QueryInterface,
1502 SecondaryBufferImpl_AddRef,
1503 SecondaryBufferImpl_Release,
1504 SecondaryBufferImpl_GetCaps,
1505 SecondaryBufferImpl_GetCurrentPosition,
1506 SecondaryBufferImpl_GetFormat,
1507 SecondaryBufferImpl_GetVolume,
1508 SecondaryBufferImpl_GetPan,
1509 SecondaryBufferImpl_GetFrequency,
1510 SecondaryBufferImpl_GetStatus,
1511 SecondaryBufferImpl_Initialize,
1512 SecondaryBufferImpl_Lock,
1513 SecondaryBufferImpl_Play,
1514 SecondaryBufferImpl_SetCurrentPosition,
1515 SecondaryBufferImpl_SetFormat,
1516 SecondaryBufferImpl_SetVolume,
1517 SecondaryBufferImpl_SetPan,
1518 SecondaryBufferImpl_SetFrequency,
1519 SecondaryBufferImpl_Stop,
1520 SecondaryBufferImpl_Unlock,
1521 SecondaryBufferImpl_Restore,
1522 SecondaryBufferImpl_SetFX,
1523 SecondaryBufferImpl_AcquireResources,
1524 SecondaryBufferImpl_GetObjectInPath
1527 HRESULT SecondaryBufferImpl_Create(
1528 IDirectSoundBufferImpl *dsb,
1529 SecondaryBufferImpl **psb)
1531 SecondaryBufferImpl *sb;
1532 TRACE("(%p,%p)\n",dsb,psb);
1534 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1536 if (sb == 0) {
1537 WARN("out of memory\n");
1538 *psb = NULL;
1539 return DSERR_OUTOFMEMORY;
1541 sb->ref = 0;
1542 sb->dsb = dsb;
1543 sb->lpVtbl = &sbvt;
1545 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1546 *psb = sb;
1547 return S_OK;
1550 static HRESULT SecondaryBufferImpl_Destroy(
1551 SecondaryBufferImpl *pdsb)
1553 TRACE("(%p)\n",pdsb);
1555 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1557 return S_OK;