msxml3: Add IObjectSafety support to IXMLHTTPRequest.
[wine.git] / dlls / dsound / buffer.c
blob0e9096acc65ba78829e223719a68688457023fc8
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 "vfwmsgs.h"
32 #include "wine/debug.h"
33 #include "dsound.h"
34 #include "dsdriver.h"
35 #include "dsound_private.h"
36 #include "dsconf.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
40 static HRESULT SecondaryBufferImpl_Destroy(SecondaryBufferImpl *pdsb);
42 /*******************************************************************************
43 * IDirectSoundNotify
46 struct IDirectSoundNotifyImpl
48 /* IUnknown fields */
49 const IDirectSoundNotifyVtbl *lpVtbl;
50 LONG ref;
51 IDirectSoundBufferImpl* dsb;
54 static HRESULT IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl *dsb,
55 IDirectSoundNotifyImpl **pdsn);
56 static HRESULT IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl *pdsn);
58 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
59 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
60 ) {
61 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
62 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
64 if (This->dsb == NULL) {
65 WARN("invalid parameter\n");
66 return E_INVALIDARG;
69 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
72 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
74 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
75 ULONG ref = InterlockedIncrement(&(This->ref));
76 TRACE("(%p) ref was %d\n", This, ref - 1);
77 return ref;
80 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
82 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
83 ULONG ref = InterlockedDecrement(&(This->ref));
84 TRACE("(%p) ref was %d\n", This, ref + 1);
86 if (!ref) {
87 This->dsb->notify = NULL;
88 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
89 HeapFree(GetProcessHeap(), 0, This);
90 TRACE("(%p) released\n", This);
92 return ref;
95 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
96 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
97 ) {
98 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
99 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
101 if (howmuch > 0 && notify == NULL) {
102 WARN("invalid parameter: notify == NULL\n");
103 return DSERR_INVALIDPARAM;
106 if (TRACE_ON(dsound)) {
107 unsigned int i;
108 for (i=0;i<howmuch;i++)
109 TRACE("notify at %d to %p\n",
110 notify[i].dwOffset,notify[i].hEventNotify);
113 if (This->dsb->hwnotify) {
114 HRESULT hres;
115 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
116 if (hres != DS_OK)
117 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
118 return hres;
119 } else if (howmuch > 0) {
120 /* Make an internal copy of the caller-supplied array.
121 * Replace the existing copy if one is already present. */
122 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
123 This->dsb->notifies = HeapAlloc(GetProcessHeap(), 0,
124 howmuch * sizeof(DSBPOSITIONNOTIFY));
126 if (This->dsb->notifies == NULL) {
127 WARN("out of memory\n");
128 return DSERR_OUTOFMEMORY;
130 CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
131 This->dsb->nrofnotifies = howmuch;
132 } else {
133 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
134 This->dsb->notifies = NULL;
135 This->dsb->nrofnotifies = 0;
138 return S_OK;
141 static const IDirectSoundNotifyVtbl dsnvt =
143 IDirectSoundNotifyImpl_QueryInterface,
144 IDirectSoundNotifyImpl_AddRef,
145 IDirectSoundNotifyImpl_Release,
146 IDirectSoundNotifyImpl_SetNotificationPositions,
149 static HRESULT IDirectSoundNotifyImpl_Create(
150 IDirectSoundBufferImpl * dsb,
151 IDirectSoundNotifyImpl **pdsn)
153 IDirectSoundNotifyImpl * dsn;
154 TRACE("(%p,%p)\n",dsb,pdsn);
156 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dsn));
158 if (dsn == NULL) {
159 WARN("out of memory\n");
160 return DSERR_OUTOFMEMORY;
163 dsn->ref = 0;
164 dsn->lpVtbl = &dsnvt;
165 dsn->dsb = dsb;
166 dsb->notify = dsn;
167 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
169 *pdsn = dsn;
170 return DS_OK;
173 static HRESULT IDirectSoundNotifyImpl_Destroy(
174 IDirectSoundNotifyImpl *pdsn)
176 TRACE("(%p)\n",pdsn);
178 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
180 return DS_OK;
183 /*******************************************************************************
184 * IDirectSoundBuffer
187 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
188 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
190 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
192 TRACE("(%p,%p)\n",This,wfex);
193 /* This method is not available on secondary buffers */
194 WARN("invalid call\n");
195 return DSERR_INVALIDCALL;
198 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
199 LPDIRECTSOUNDBUFFER8 iface,LONG vol
201 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
202 LONG oldVol;
203 HRESULT hres = DS_OK;
205 TRACE("(%p,%d)\n",This,vol);
207 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
208 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
209 return DSERR_CONTROLUNAVAIL;
212 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
213 WARN("invalid parameter: vol = %d\n", vol);
214 return DSERR_INVALIDPARAM;
217 /* **** */
218 RtlAcquireResourceExclusive(&This->lock, TRUE);
220 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
221 oldVol = This->ds3db_lVolume;
222 This->ds3db_lVolume = vol;
223 if (vol != oldVol)
224 /* recalc 3d volume, which in turn recalcs the pans */
225 DSOUND_Calc3DBuffer(This);
226 } else {
227 oldVol = This->volpan.lVolume;
228 This->volpan.lVolume = vol;
229 if (vol != oldVol)
230 DSOUND_RecalcVolPan(&(This->volpan));
233 if (vol != oldVol) {
234 if (This->hwbuf) {
235 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
236 if (hres != DS_OK)
237 WARN("IDsDriverBuffer_SetVolumePan failed\n");
241 RtlReleaseResource(&This->lock);
242 /* **** */
244 return hres;
247 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
248 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
250 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
251 TRACE("(%p,%p)\n",This,vol);
253 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
254 WARN("control unavailable\n");
255 return DSERR_CONTROLUNAVAIL;
258 if (vol == NULL) {
259 WARN("invalid parameter: vol == NULL\n");
260 return DSERR_INVALIDPARAM;
263 *vol = This->volpan.lVolume;
265 return DS_OK;
268 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
269 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
271 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
272 DWORD oldFreq;
274 TRACE("(%p,%d)\n",This,freq);
276 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
277 WARN("control unavailable\n");
278 return DSERR_CONTROLUNAVAIL;
281 if (freq == DSBFREQUENCY_ORIGINAL)
282 freq = This->pwfx->nSamplesPerSec;
284 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
285 WARN("invalid parameter: freq = %d\n", freq);
286 return DSERR_INVALIDPARAM;
289 /* **** */
290 RtlAcquireResourceExclusive(&This->lock, TRUE);
292 oldFreq = This->freq;
293 This->freq = freq;
294 if (freq != oldFreq) {
295 This->freqAdjust = ((DWORD64)This->freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
296 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
297 DSOUND_RecalcFormat(This);
298 DSOUND_MixToTemporary(This, 0, This->buflen, FALSE);
301 RtlReleaseResource(&This->lock);
302 /* **** */
304 return DS_OK;
307 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
308 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
310 HRESULT hres = DS_OK;
311 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
312 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
314 /* **** */
315 RtlAcquireResourceExclusive(&This->lock, TRUE);
317 This->playflags = flags;
318 if (This->state == STATE_STOPPED && !This->hwbuf) {
319 This->leadin = TRUE;
320 This->state = STATE_STARTING;
321 } else if (This->state == STATE_STOPPING)
322 This->state = STATE_PLAYING;
323 if (This->hwbuf) {
324 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
325 if (hres != DS_OK)
326 WARN("IDsDriverBuffer_Play failed\n");
327 else
328 This->state = STATE_PLAYING;
331 RtlReleaseResource(&This->lock);
332 /* **** */
334 return hres;
337 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
339 HRESULT hres = DS_OK;
340 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
341 TRACE("(%p)\n",This);
343 /* **** */
344 RtlAcquireResourceExclusive(&This->lock, TRUE);
346 if (This->state == STATE_PLAYING)
347 This->state = STATE_STOPPING;
348 else if (This->state == STATE_STARTING)
350 This->state = STATE_STOPPED;
351 DSOUND_CheckEvent(This, 0, 0);
353 if (This->hwbuf) {
354 hres = IDsDriverBuffer_Stop(This->hwbuf);
355 if (hres != DS_OK)
356 WARN("IDsDriverBuffer_Stop failed\n");
357 else
358 This->state = STATE_STOPPED;
361 RtlReleaseResource(&This->lock);
362 /* **** */
364 return hres;
367 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
369 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
370 ULONG ref = InterlockedIncrement(&(This->ref));
371 TRACE("(%p) ref was %d\n", This, ref - 1);
372 return ref;
375 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
377 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
378 ULONG ref = InterlockedDecrement(&(This->ref));
379 TRACE("(%p) ref was %d\n", This, ref + 1);
381 if (!ref) {
382 DirectSoundDevice_RemoveBuffer(This->device, This);
383 RtlDeleteResource(&This->lock);
385 if (This->hwbuf)
386 IDsDriverBuffer_Release(This->hwbuf);
387 if (!This->hwbuf || (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)) {
388 This->buffer->ref--;
389 list_remove(&This->entry);
390 if (This->buffer->ref==0) {
391 HeapFree(GetProcessHeap(),0,This->buffer->memory);
392 HeapFree(GetProcessHeap(),0,This->buffer);
396 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
397 HeapFree(GetProcessHeap(), 0, This->notifies);
398 HeapFree(GetProcessHeap(), 0, This->pwfx);
399 HeapFree(GetProcessHeap(), 0, This);
401 TRACE("(%p) released\n", This);
403 return ref;
406 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
407 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
409 HRESULT hres;
410 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
411 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
413 RtlAcquireResourceShared(&This->lock, TRUE);
414 if (This->hwbuf) {
415 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
416 if (hres != DS_OK) {
417 WARN("IDsDriverBuffer_GetPosition failed\n");
418 return hres;
420 } else {
421 DWORD pos = This->sec_mixpos;
423 /* sanity */
424 if (pos >= This->buflen){
425 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
426 pos %= This->buflen;
429 if (playpos)
430 *playpos = pos;
431 if (writepos)
432 *writepos = pos;
434 if (writepos && This->state != STATE_STOPPED && (!This->hwbuf || !(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD))) {
435 /* apply the documented 10ms lead to writepos */
436 *writepos += This->writelead;
437 *writepos %= This->buflen;
439 RtlReleaseResource(&This->lock);
441 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
442 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
444 return DS_OK;
447 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
448 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
450 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
451 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
453 if (status == NULL) {
454 WARN("invalid parameter: status = NULL\n");
455 return DSERR_INVALIDPARAM;
458 *status = 0;
459 RtlAcquireResourceShared(&This->lock, TRUE);
460 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
461 *status |= DSBSTATUS_PLAYING;
462 if (This->playflags & DSBPLAY_LOOPING)
463 *status |= DSBSTATUS_LOOPING;
465 RtlReleaseResource(&This->lock);
467 TRACE("status=%x\n", *status);
468 return DS_OK;
472 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
473 LPDIRECTSOUNDBUFFER8 iface,
474 LPWAVEFORMATEX lpwf,
475 DWORD wfsize,
476 LPDWORD wfwritten)
478 DWORD size;
479 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
480 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
482 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
484 if (lpwf) { /* NULL is valid */
485 if (wfsize >= size) {
486 CopyMemory(lpwf,This->pwfx,size);
487 if (wfwritten)
488 *wfwritten = size;
489 } else {
490 WARN("invalid parameter: wfsize too small\n");
491 CopyMemory(lpwf,This->pwfx,wfsize);
492 if (wfwritten)
493 *wfwritten = wfsize;
494 return DSERR_INVALIDPARAM;
496 } else {
497 if (wfwritten)
498 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
499 else {
500 WARN("invalid parameter: wfwritten == NULL\n");
501 return DSERR_INVALIDPARAM;
505 return DS_OK;
508 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
509 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
511 HRESULT hres = DS_OK;
512 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
514 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
515 This,
516 writecursor,
517 writebytes,
518 lplpaudioptr1,
519 audiobytes1,
520 lplpaudioptr2,
521 audiobytes2,
522 flags,
523 GetTickCount()
526 if (!audiobytes1)
527 return DSERR_INVALIDPARAM;
529 /* when this flag is set, writecursor is meaningless and must be calculated */
530 if (flags & DSBLOCK_FROMWRITECURSOR) {
531 /* GetCurrentPosition does too much magic to duplicate here */
532 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
533 if (hres != DS_OK) {
534 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
535 return hres;
539 /* when this flag is set, writebytes is meaningless and must be set */
540 if (flags & DSBLOCK_ENTIREBUFFER)
541 writebytes = This->buflen;
543 if (writecursor >= This->buflen) {
544 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
545 writecursor, This->buflen);
546 return DSERR_INVALIDPARAM;
549 if (writebytes > This->buflen) {
550 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
551 writebytes, This->buflen);
552 return DSERR_INVALIDPARAM;
555 /* **** */
556 RtlAcquireResourceShared(&This->lock, TRUE);
558 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
559 hres = IDsDriverBuffer_Lock(This->hwbuf,
560 lplpaudioptr1, audiobytes1,
561 lplpaudioptr2, audiobytes2,
562 writecursor, writebytes,
564 if (hres != DS_OK) {
565 WARN("IDsDriverBuffer_Lock failed\n");
566 RtlReleaseResource(&This->lock);
567 return hres;
569 } else {
570 if (writecursor+writebytes <= This->buflen) {
571 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
572 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
573 WARN("Overwriting mixing position, case 1\n");
574 *audiobytes1 = writebytes;
575 if (lplpaudioptr2)
576 *(LPBYTE*)lplpaudioptr2 = NULL;
577 if (audiobytes2)
578 *audiobytes2 = 0;
579 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
580 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
581 TRACE("->%d.0\n",writebytes);
582 } else {
583 DWORD remainder = writebytes + writecursor - This->buflen;
584 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
585 *audiobytes1 = This->buflen-writecursor;
586 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
587 WARN("Overwriting mixing position, case 2\n");
588 if (lplpaudioptr2)
589 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
590 if (audiobytes2)
591 *audiobytes2 = writebytes-(This->buflen-writecursor);
592 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
593 WARN("Overwriting mixing position, case 3\n");
594 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
598 RtlReleaseResource(&This->lock);
599 /* **** */
601 return DS_OK;
604 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
605 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
607 HRESULT hres = DS_OK;
608 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
609 DWORD oldpos;
610 TRACE("(%p,%d)\n",This,newpos);
612 /* **** */
613 RtlAcquireResourceExclusive(&This->lock, TRUE);
615 oldpos = This->sec_mixpos;
617 /* start mixing from this new location instead */
618 newpos %= This->buflen;
619 newpos -= newpos%This->pwfx->nBlockAlign;
620 This->sec_mixpos = newpos;
622 /* at this point, do not attempt to reset buffers, mess with primary mix position,
623 or anything like that to reduce latancy. The data already prebuffered cannot be changed */
625 /* position HW buffer if applicable, else just start mixing from new location instead */
626 if (This->hwbuf) {
627 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
628 if (hres != DS_OK)
629 WARN("IDsDriverBuffer_SetPosition failed\n");
631 else if (oldpos != newpos)
632 /* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
633 This->buf_mixpos = DSOUND_secpos_to_bufpos(This, newpos, 0, NULL);
635 RtlReleaseResource(&This->lock);
636 /* **** */
638 return hres;
641 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
642 LPDIRECTSOUNDBUFFER8 iface,LONG pan
644 HRESULT hres = DS_OK;
645 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
647 TRACE("(%p,%d)\n",This,pan);
649 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
650 WARN("invalid parameter: pan = %d\n", pan);
651 return DSERR_INVALIDPARAM;
654 /* You cannot use both pan and 3D controls */
655 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
656 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
657 WARN("control unavailable\n");
658 return DSERR_CONTROLUNAVAIL;
661 /* **** */
662 RtlAcquireResourceExclusive(&This->lock, TRUE);
664 if (This->volpan.lPan != pan) {
665 This->volpan.lPan = pan;
666 DSOUND_RecalcVolPan(&(This->volpan));
668 if (This->hwbuf) {
669 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
670 if (hres != DS_OK)
671 WARN("IDsDriverBuffer_SetVolumePan failed\n");
675 RtlReleaseResource(&This->lock);
676 /* **** */
678 return hres;
681 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
682 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
684 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
685 TRACE("(%p,%p)\n",This,pan);
687 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
688 WARN("control unavailable\n");
689 return DSERR_CONTROLUNAVAIL;
692 if (pan == NULL) {
693 WARN("invalid parameter: pan = NULL\n");
694 return DSERR_INVALIDPARAM;
697 *pan = This->volpan.lPan;
699 return DS_OK;
702 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
703 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
705 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface, *iter;
706 HRESULT hres = DS_OK;
708 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
710 /* **** */
711 RtlAcquireResourceShared(&This->lock, TRUE);
713 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
714 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
715 if (hres != DS_OK)
716 WARN("IDsDriverBuffer_Unlock failed\n");
719 RtlReleaseResource(&This->lock);
720 /* **** */
722 if (!p2)
723 x2 = 0;
725 if (!This->hwbuf && (x1 || x2))
727 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
728 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
730 RtlAcquireResourceShared(&iter->lock, TRUE);
731 if (x1)
733 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
734 hres = DSERR_INVALIDPARAM;
735 else
736 DSOUND_MixToTemporary(iter, (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory, x1, FALSE);
738 if (x2)
739 DSOUND_MixToTemporary(iter, 0, x2, FALSE);
740 RtlReleaseResource(&iter->lock);
742 RtlReleaseResource(&This->device->buffer_list_lock);
745 return hres;
748 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
749 LPDIRECTSOUNDBUFFER8 iface
751 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
752 FIXME("(%p):stub\n",This);
753 return DS_OK;
756 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
757 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
759 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
760 TRACE("(%p,%p)\n",This,freq);
762 if (freq == NULL) {
763 WARN("invalid parameter: freq = NULL\n");
764 return DSERR_INVALIDPARAM;
767 *freq = This->freq;
768 TRACE("-> %d\n", *freq);
770 return DS_OK;
773 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
774 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
776 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
777 DWORD u;
779 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
781 if (pdwResultCodes)
782 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
784 WARN("control unavailable\n");
785 return DSERR_CONTROLUNAVAIL;
788 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
789 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
791 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
792 DWORD u;
794 FIXME("(%p,%08u,%u,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
796 if (pdwResultCodes)
797 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
799 WARN("control unavailable\n");
800 return DSERR_CONTROLUNAVAIL;
803 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
804 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
806 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
808 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
810 WARN("control unavailable\n");
811 return DSERR_CONTROLUNAVAIL;
814 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
815 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
817 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
818 WARN("(%p) already initialized\n", This);
819 return DSERR_ALREADYINITIALIZED;
822 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
823 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
825 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
826 TRACE("(%p)->(%p)\n",This,caps);
828 if (caps == NULL) {
829 WARN("invalid parameter: caps == NULL\n");
830 return DSERR_INVALIDPARAM;
833 if (caps->dwSize < sizeof(*caps)) {
834 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
835 return DSERR_INVALIDPARAM;
838 caps->dwFlags = This->dsbd.dwFlags;
839 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
840 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
842 caps->dwBufferBytes = This->buflen;
844 /* According to windows, this is zero*/
845 caps->dwUnlockTransferRate = 0;
846 caps->dwPlayCpuOverhead = 0;
848 return DS_OK;
851 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
852 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
854 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
856 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
858 if (ppobj == NULL) {
859 WARN("invalid parameter\n");
860 return E_INVALIDARG;
863 *ppobj = NULL; /* assume failure */
865 if ( IsEqualGUID(riid, &IID_IUnknown) ||
866 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
867 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
868 if (!This->secondary)
869 SecondaryBufferImpl_Create(This, &(This->secondary));
870 if (This->secondary) {
871 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->secondary);
872 *ppobj = This->secondary;
873 return S_OK;
875 WARN("IID_IDirectSoundBuffer\n");
876 return E_NOINTERFACE;
879 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
880 if (!This->notify)
881 IDirectSoundNotifyImpl_Create(This, &(This->notify));
882 if (This->notify) {
883 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
884 *ppobj = This->notify;
885 return S_OK;
887 WARN("IID_IDirectSoundNotify\n");
888 return E_NOINTERFACE;
891 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
892 if (!This->ds3db)
893 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
894 if (This->ds3db) {
895 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
896 *ppobj = This->ds3db;
897 return S_OK;
899 WARN("IID_IDirectSound3DBuffer\n");
900 return E_NOINTERFACE;
903 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
904 ERR("app requested IDirectSound3DListener on secondary buffer\n");
905 return E_NOINTERFACE;
908 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
909 if (!This->iks)
910 IKsBufferPropertySetImpl_Create(This, &(This->iks));
911 if (This->iks) {
912 IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
913 *ppobj = This->iks;
914 return S_OK;
916 WARN("IID_IKsPropertySet\n");
917 return E_NOINTERFACE;
920 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
922 return E_NOINTERFACE;
925 static const IDirectSoundBuffer8Vtbl dsbvt =
927 IDirectSoundBufferImpl_QueryInterface,
928 IDirectSoundBufferImpl_AddRef,
929 IDirectSoundBufferImpl_Release,
930 IDirectSoundBufferImpl_GetCaps,
931 IDirectSoundBufferImpl_GetCurrentPosition,
932 IDirectSoundBufferImpl_GetFormat,
933 IDirectSoundBufferImpl_GetVolume,
934 IDirectSoundBufferImpl_GetPan,
935 IDirectSoundBufferImpl_GetFrequency,
936 IDirectSoundBufferImpl_GetStatus,
937 IDirectSoundBufferImpl_Initialize,
938 IDirectSoundBufferImpl_Lock,
939 IDirectSoundBufferImpl_Play,
940 IDirectSoundBufferImpl_SetCurrentPosition,
941 IDirectSoundBufferImpl_SetFormat,
942 IDirectSoundBufferImpl_SetVolume,
943 IDirectSoundBufferImpl_SetPan,
944 IDirectSoundBufferImpl_SetFrequency,
945 IDirectSoundBufferImpl_Stop,
946 IDirectSoundBufferImpl_Unlock,
947 IDirectSoundBufferImpl_Restore,
948 IDirectSoundBufferImpl_SetFX,
949 IDirectSoundBufferImpl_AcquireResources,
950 IDirectSoundBufferImpl_GetObjectInPath
953 HRESULT IDirectSoundBufferImpl_Create(
954 DirectSoundDevice * device,
955 IDirectSoundBufferImpl **pdsb,
956 LPCDSBUFFERDESC dsbd)
958 IDirectSoundBufferImpl *dsb;
959 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
960 HRESULT err = DS_OK;
961 DWORD capf = 0;
962 int use_hw;
963 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
965 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
966 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
967 *pdsb = NULL;
968 return DSERR_INVALIDPARAM; /* FIXME: which error? */
971 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
973 if (dsb == 0) {
974 WARN("out of memory\n");
975 *pdsb = NULL;
976 return DSERR_OUTOFMEMORY;
979 TRACE("Created buffer at %p\n", dsb);
981 dsb->ref = 0;
982 dsb->secondary = 0;
983 dsb->device = device;
984 dsb->lpVtbl = &dsbvt;
985 dsb->iks = NULL;
987 /* size depends on version */
988 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
990 dsb->pwfx = DSOUND_CopyFormat(wfex);
991 if (dsb->pwfx == NULL) {
992 HeapFree(GetProcessHeap(),0,dsb);
993 *pdsb = NULL;
994 return DSERR_OUTOFMEMORY;
997 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
998 dsb->buflen = dsbd->dwBufferBytes +
999 (dsbd->lpwfxFormat->nBlockAlign -
1000 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1001 else
1002 dsb->buflen = dsbd->dwBufferBytes;
1004 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1005 dsb->notify = NULL;
1006 dsb->notifies = NULL;
1007 dsb->nrofnotifies = 0;
1008 dsb->hwnotify = 0;
1010 /* Check necessary hardware mixing capabilities */
1011 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1012 else capf |= DSCAPS_SECONDARYMONO;
1013 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1014 else capf |= DSCAPS_SECONDARY8BIT;
1016 use_hw = !!(dsbd->dwFlags & DSBCAPS_LOCHARDWARE);
1017 TRACE("use_hw = %d, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw, capf, device->drvcaps.dwFlags);
1018 if (use_hw && ((device->drvcaps.dwFlags & capf) != capf || !device->driver))
1020 if (device->driver)
1021 WARN("Format not supported for hardware buffer\n");
1022 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1023 HeapFree(GetProcessHeap(),0,dsb);
1024 *pdsb = NULL;
1025 if ((device->drvcaps.dwFlags & capf) != capf)
1026 return DSERR_BADFORMAT;
1027 return DSERR_GENERIC;
1030 /* FIXME: check hardware sample rate mixing capabilities */
1031 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1032 /* FIXME: check whether any hardware buffers are left */
1033 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1035 /* Allocate an empty buffer */
1036 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1037 if (dsb->buffer == NULL) {
1038 WARN("out of memory\n");
1039 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1040 HeapFree(GetProcessHeap(),0,dsb);
1041 *pdsb = NULL;
1042 return DSERR_OUTOFMEMORY;
1045 /* Allocate system memory for buffer if applicable */
1046 if ((device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1047 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1048 if (dsb->buffer->memory == NULL) {
1049 WARN("out of memory\n");
1050 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1051 HeapFree(GetProcessHeap(),0,dsb->buffer);
1052 HeapFree(GetProcessHeap(),0,dsb);
1053 *pdsb = NULL;
1054 return DSERR_OUTOFMEMORY;
1058 /* Allocate the hardware buffer */
1059 if (use_hw) {
1060 err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1061 &(dsb->buflen),&(dsb->buffer->memory),
1062 (LPVOID*)&(dsb->hwbuf));
1063 if (FAILED(err))
1065 WARN("Failed to create hardware secondary buffer: %08x\n", err);
1066 if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)
1067 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1068 HeapFree(GetProcessHeap(),0,dsb->buffer);
1069 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1070 HeapFree(GetProcessHeap(),0,dsb);
1071 *pdsb = NULL;
1072 return DSERR_GENERIC;
1076 dsb->buffer->ref = 1;
1077 list_init(&dsb->buffer->buffers);
1078 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1079 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1081 /* It's not necessary to initialize values to zero since */
1082 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1083 dsb->buf_mixpos = dsb->sec_mixpos = 0;
1084 dsb->state = STATE_STOPPED;
1086 dsb->freqAdjust = ((DWORD64)dsb->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
1087 dsb->nAvgBytesPerSec = dsb->freq *
1088 dsbd->lpwfxFormat->nBlockAlign;
1090 /* calculate fragment size and write lead */
1091 DSOUND_RecalcFormat(dsb);
1093 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1094 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1095 dsb->ds3db_ds3db.vPosition.x = 0.0;
1096 dsb->ds3db_ds3db.vPosition.y = 0.0;
1097 dsb->ds3db_ds3db.vPosition.z = 0.0;
1098 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1099 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1100 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1101 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1102 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1103 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1104 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1105 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1106 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1107 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1108 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1109 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1111 dsb->ds3db_need_recalc = FALSE;
1112 DSOUND_Calc3DBuffer(dsb);
1113 } else
1114 DSOUND_RecalcVolPan(&(dsb->volpan));
1116 RtlInitializeResource(&dsb->lock);
1118 /* register buffer if not primary */
1119 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1120 err = DirectSoundDevice_AddBuffer(device, dsb);
1121 if (err != DS_OK) {
1122 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1123 HeapFree(GetProcessHeap(),0,dsb->buffer);
1124 RtlDeleteResource(&dsb->lock);
1125 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1126 HeapFree(GetProcessHeap(),0,dsb);
1127 dsb = NULL;
1131 *pdsb = dsb;
1132 return err;
1135 HRESULT IDirectSoundBufferImpl_Destroy(
1136 IDirectSoundBufferImpl *pdsb)
1138 TRACE("(%p)\n",pdsb);
1140 /* This keeps the *_Destroy functions from possibly deleting
1141 * this object until it is ready to be deleted */
1142 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1144 if (pdsb->iks) {
1145 WARN("iks not NULL\n");
1146 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1147 pdsb->iks = NULL;
1150 if (pdsb->ds3db) {
1151 WARN("ds3db not NULL\n");
1152 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1153 pdsb->ds3db = NULL;
1156 if (pdsb->notify) {
1157 WARN("notify not NULL\n");
1158 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1159 pdsb->notify = NULL;
1162 if (pdsb->secondary) {
1163 WARN("dsb not NULL\n");
1164 SecondaryBufferImpl_Destroy(pdsb->secondary);
1165 pdsb->secondary = NULL;
1168 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1170 return S_OK;
1173 HRESULT IDirectSoundBufferImpl_Duplicate(
1174 DirectSoundDevice *device,
1175 IDirectSoundBufferImpl **ppdsb,
1176 IDirectSoundBufferImpl *pdsb)
1178 IDirectSoundBufferImpl *dsb;
1179 HRESULT hres = DS_OK;
1180 TRACE("(%p,%p,%p)\n", device, pdsb, pdsb);
1182 dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
1183 if (dsb == NULL) {
1184 WARN("out of memory\n");
1185 *ppdsb = NULL;
1186 return DSERR_OUTOFMEMORY;
1188 CopyMemory(dsb, pdsb, sizeof(*dsb));
1190 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1191 if (dsb->pwfx == NULL) {
1192 HeapFree(GetProcessHeap(),0,dsb);
1193 *ppdsb = NULL;
1194 return DSERR_OUTOFMEMORY;
1197 if (pdsb->hwbuf) {
1198 TRACE("duplicating hardware buffer\n");
1200 hres = IDsDriver_DuplicateSoundBuffer(device->driver, pdsb->hwbuf,
1201 (LPVOID *)&dsb->hwbuf);
1202 if (FAILED(hres)) {
1203 WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres);
1204 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1205 HeapFree(GetProcessHeap(),0,dsb);
1206 *ppdsb = NULL;
1207 return hres;
1211 dsb->buffer->ref++;
1212 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1213 dsb->ref = 0;
1214 dsb->state = STATE_STOPPED;
1215 dsb->buf_mixpos = dsb->sec_mixpos = 0;
1216 dsb->device = device;
1217 dsb->ds3db = NULL;
1218 dsb->iks = NULL; /* FIXME? */
1219 dsb->secondary = NULL;
1220 dsb->tmp_buffer = NULL;
1221 DSOUND_RecalcFormat(dsb);
1222 DSOUND_MixToTemporary(dsb, 0, dsb->buflen, FALSE);
1224 RtlInitializeResource(&dsb->lock);
1226 /* register buffer */
1227 hres = DirectSoundDevice_AddBuffer(device, dsb);
1228 if (hres != DS_OK) {
1229 RtlDeleteResource(&dsb->lock);
1230 HeapFree(GetProcessHeap(),0,dsb->tmp_buffer);
1231 list_remove(&dsb->entry);
1232 dsb->buffer->ref--;
1233 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1234 HeapFree(GetProcessHeap(),0,dsb);
1235 dsb = NULL;
1238 *ppdsb = dsb;
1239 return hres;
1242 /*******************************************************************************
1243 * SecondaryBuffer
1246 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1247 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1249 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1250 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1252 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1255 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1257 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1258 ULONG ref = InterlockedIncrement(&(This->ref));
1259 TRACE("(%p) ref was %d\n", This, ref - 1);
1260 return ref;
1263 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1265 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1266 ULONG ref;
1267 TRACE("(%p)\n", This);
1268 ref = InterlockedDecrement(&(This->ref));
1269 TRACE("ref was %d\n", ref + 1);
1271 if (!ref) {
1272 This->dsb->secondary = NULL;
1273 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1274 HeapFree(GetProcessHeap(), 0, This);
1275 TRACE("(%p) released\n", This);
1277 return ref;
1280 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1281 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1283 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1284 TRACE("(%p)->(%p)\n",This,caps);
1286 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1289 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1290 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1292 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1293 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1295 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1298 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1299 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1301 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1302 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1304 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1307 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1308 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1310 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1311 TRACE("(%p,%p)\n",This,vol);
1313 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1316 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1317 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1319 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1320 TRACE("(%p,%p)\n",This,pan);
1322 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1325 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1326 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1328 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1329 TRACE("(%p,%p)\n",This,freq);
1331 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1334 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1335 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1337 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1338 TRACE("(%p,%p)\n",This,status);
1340 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1343 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1344 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1346 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1347 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1349 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1352 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1353 LPDIRECTSOUNDBUFFER8 iface,
1354 DWORD writecursor,
1355 DWORD writebytes,
1356 LPVOID *lplpaudioptr1,
1357 LPDWORD audiobytes1,
1358 LPVOID *lplpaudioptr2,
1359 LPDWORD audiobytes2,
1360 DWORD dwFlags)
1362 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1363 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1364 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1366 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1367 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1370 static HRESULT WINAPI SecondaryBufferImpl_Play(
1371 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1373 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1374 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1376 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1379 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1380 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1382 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1383 TRACE("(%p,%d)\n",This,newpos);
1385 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1388 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1389 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1391 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1392 TRACE("(%p,%p)\n",This,wfex);
1394 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1397 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1398 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1400 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1401 TRACE("(%p,%d)\n",This,vol);
1403 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1406 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1407 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1409 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1410 TRACE("(%p,%d)\n",This,pan);
1412 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1415 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1416 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1418 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1419 TRACE("(%p,%d)\n",This,freq);
1421 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1424 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1426 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1427 TRACE("(%p)\n",This);
1429 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1432 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1433 LPDIRECTSOUNDBUFFER8 iface,
1434 LPVOID lpvAudioPtr1,
1435 DWORD dwAudioBytes1,
1436 LPVOID lpvAudioPtr2,
1437 DWORD dwAudioBytes2)
1439 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1440 TRACE("(%p,%p,%d,%p,%d)\n",
1441 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1443 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1444 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1447 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1448 LPDIRECTSOUNDBUFFER8 iface)
1450 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1451 TRACE("(%p)\n",This);
1453 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1456 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1457 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1459 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1460 TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1462 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1465 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1466 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1468 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1469 TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1471 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1474 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1475 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1477 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1478 TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1480 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1483 static const IDirectSoundBuffer8Vtbl sbvt =
1485 SecondaryBufferImpl_QueryInterface,
1486 SecondaryBufferImpl_AddRef,
1487 SecondaryBufferImpl_Release,
1488 SecondaryBufferImpl_GetCaps,
1489 SecondaryBufferImpl_GetCurrentPosition,
1490 SecondaryBufferImpl_GetFormat,
1491 SecondaryBufferImpl_GetVolume,
1492 SecondaryBufferImpl_GetPan,
1493 SecondaryBufferImpl_GetFrequency,
1494 SecondaryBufferImpl_GetStatus,
1495 SecondaryBufferImpl_Initialize,
1496 SecondaryBufferImpl_Lock,
1497 SecondaryBufferImpl_Play,
1498 SecondaryBufferImpl_SetCurrentPosition,
1499 SecondaryBufferImpl_SetFormat,
1500 SecondaryBufferImpl_SetVolume,
1501 SecondaryBufferImpl_SetPan,
1502 SecondaryBufferImpl_SetFrequency,
1503 SecondaryBufferImpl_Stop,
1504 SecondaryBufferImpl_Unlock,
1505 SecondaryBufferImpl_Restore,
1506 SecondaryBufferImpl_SetFX,
1507 SecondaryBufferImpl_AcquireResources,
1508 SecondaryBufferImpl_GetObjectInPath
1511 HRESULT SecondaryBufferImpl_Create(
1512 IDirectSoundBufferImpl *dsb,
1513 SecondaryBufferImpl **psb)
1515 SecondaryBufferImpl *sb;
1516 TRACE("(%p,%p)\n",dsb,psb);
1518 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1520 if (sb == 0) {
1521 WARN("out of memory\n");
1522 *psb = NULL;
1523 return DSERR_OUTOFMEMORY;
1525 sb->ref = 0;
1526 sb->dsb = dsb;
1527 sb->lpVtbl = &sbvt;
1529 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1530 *psb = sb;
1531 return S_OK;
1534 static HRESULT SecondaryBufferImpl_Destroy(
1535 SecondaryBufferImpl *pdsb)
1537 TRACE("(%p)\n",pdsb);
1539 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1541 return S_OK;
1544 /*******************************************************************************
1545 * IKsBufferPropertySet
1548 /* IUnknown methods */
1549 static HRESULT WINAPI IKsBufferPropertySetImpl_QueryInterface(
1550 LPKSPROPERTYSET iface,
1551 REFIID riid,
1552 LPVOID *ppobj )
1554 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1555 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1557 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
1560 static ULONG WINAPI IKsBufferPropertySetImpl_AddRef(LPKSPROPERTYSET iface)
1562 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1563 ULONG ref = InterlockedIncrement(&(This->ref));
1564 TRACE("(%p) ref was %d\n", This, ref - 1);
1565 return ref;
1568 static ULONG WINAPI IKsBufferPropertySetImpl_Release(LPKSPROPERTYSET iface)
1570 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1571 ULONG ref = InterlockedDecrement(&(This->ref));
1572 TRACE("(%p) ref was %d\n", This, ref + 1);
1574 if (!ref) {
1575 This->dsb->iks = 0;
1576 IDirectSoundBuffer_Release((LPDIRECTSOUND3DBUFFER)This->dsb);
1577 HeapFree(GetProcessHeap(), 0, This);
1578 TRACE("(%p) released\n", This);
1580 return ref;
1583 static HRESULT WINAPI IKsBufferPropertySetImpl_Get(
1584 LPKSPROPERTYSET iface,
1585 REFGUID guidPropSet,
1586 ULONG dwPropID,
1587 LPVOID pInstanceData,
1588 ULONG cbInstanceData,
1589 LPVOID pPropData,
1590 ULONG cbPropData,
1591 PULONG pcbReturned )
1593 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1594 PIDSDRIVERPROPERTYSET ps;
1595 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1596 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1598 if (This->dsb->hwbuf) {
1599 IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1601 if (ps) {
1602 DSPROPERTY prop;
1603 HRESULT hres;
1605 prop.s.Set = *guidPropSet;
1606 prop.s.Id = dwPropID;
1607 prop.s.Flags = 0; /* unused */
1608 prop.s.InstanceId = (ULONG)This->dsb->device;
1611 hres = IDsDriverPropertySet_Get(ps, &prop, pInstanceData, cbInstanceData, pPropData, cbPropData, pcbReturned);
1613 IDsDriverPropertySet_Release(ps);
1615 return hres;
1619 return E_PROP_ID_UNSUPPORTED;
1622 static HRESULT WINAPI IKsBufferPropertySetImpl_Set(
1623 LPKSPROPERTYSET iface,
1624 REFGUID guidPropSet,
1625 ULONG dwPropID,
1626 LPVOID pInstanceData,
1627 ULONG cbInstanceData,
1628 LPVOID pPropData,
1629 ULONG cbPropData )
1631 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1632 PIDSDRIVERPROPERTYSET ps;
1633 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1635 if (This->dsb->hwbuf) {
1636 IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1638 if (ps) {
1639 DSPROPERTY prop;
1640 HRESULT hres;
1642 prop.s.Set = *guidPropSet;
1643 prop.s.Id = dwPropID;
1644 prop.s.Flags = 0; /* unused */
1645 prop.s.InstanceId = (ULONG)This->dsb->device;
1646 hres = IDsDriverPropertySet_Set(ps,&prop,pInstanceData,cbInstanceData,pPropData,cbPropData);
1648 IDsDriverPropertySet_Release(ps);
1650 return hres;
1654 return E_PROP_ID_UNSUPPORTED;
1657 static HRESULT WINAPI IKsBufferPropertySetImpl_QuerySupport(
1658 LPKSPROPERTYSET iface,
1659 REFGUID guidPropSet,
1660 ULONG dwPropID,
1661 PULONG pTypeSupport )
1663 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1664 PIDSDRIVERPROPERTYSET ps;
1665 TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1667 if (This->dsb->hwbuf) {
1668 IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1670 if (ps) {
1671 HRESULT hres;
1673 hres = IDsDriverPropertySet_QuerySupport(ps,guidPropSet, dwPropID,pTypeSupport);
1675 IDsDriverPropertySet_Release(ps);
1677 return hres;
1681 return E_PROP_ID_UNSUPPORTED;
1684 static const IKsPropertySetVtbl iksbvt = {
1685 IKsBufferPropertySetImpl_QueryInterface,
1686 IKsBufferPropertySetImpl_AddRef,
1687 IKsBufferPropertySetImpl_Release,
1688 IKsBufferPropertySetImpl_Get,
1689 IKsBufferPropertySetImpl_Set,
1690 IKsBufferPropertySetImpl_QuerySupport
1693 HRESULT IKsBufferPropertySetImpl_Create(
1694 IDirectSoundBufferImpl *dsb,
1695 IKsBufferPropertySetImpl **piks)
1697 IKsBufferPropertySetImpl *iks;
1698 TRACE("(%p,%p)\n",dsb,piks);
1699 *piks = NULL;
1701 iks = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*iks));
1702 if (iks == 0) {
1703 WARN("out of memory\n");
1704 *piks = NULL;
1705 return DSERR_OUTOFMEMORY;
1708 iks->ref = 0;
1709 iks->dsb = dsb;
1710 dsb->iks = iks;
1711 iks->lpVtbl = &iksbvt;
1713 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
1715 *piks = iks;
1716 return S_OK;
1719 HRESULT IKsBufferPropertySetImpl_Destroy(
1720 IKsBufferPropertySetImpl *piks)
1722 TRACE("(%p)\n",piks);
1724 while (IKsBufferPropertySetImpl_Release((LPKSPROPERTYSET)piks) > 0);
1726 return S_OK;