push 92ef5b88da02911741c0a2f56030fd2e20189321
[wine/hacks.git] / dlls / dsound / buffer.c
blob1a999279c2428601a2aa793eb9700d285bfbcb3a
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 if (This->dsb->notifies)
121 This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
122 This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
123 else
124 This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
125 howmuch * sizeof(DSBPOSITIONNOTIFY));
127 if (This->dsb->notifies == NULL) {
128 WARN("out of memory\n");
129 return DSERR_OUTOFMEMORY;
131 CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
132 This->dsb->nrofnotifies = howmuch;
133 } else {
134 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
135 This->dsb->notifies = NULL;
136 This->dsb->nrofnotifies = 0;
139 return S_OK;
142 static const IDirectSoundNotifyVtbl dsnvt =
144 IDirectSoundNotifyImpl_QueryInterface,
145 IDirectSoundNotifyImpl_AddRef,
146 IDirectSoundNotifyImpl_Release,
147 IDirectSoundNotifyImpl_SetNotificationPositions,
150 static HRESULT IDirectSoundNotifyImpl_Create(
151 IDirectSoundBufferImpl * dsb,
152 IDirectSoundNotifyImpl **pdsn)
154 IDirectSoundNotifyImpl * dsn;
155 TRACE("(%p,%p)\n",dsb,pdsn);
157 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
159 if (dsn == NULL) {
160 WARN("out of memory\n");
161 return DSERR_OUTOFMEMORY;
164 dsn->ref = 0;
165 dsn->lpVtbl = &dsnvt;
166 dsn->dsb = dsb;
167 dsb->notify = dsn;
168 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
170 *pdsn = dsn;
171 return DS_OK;
174 static HRESULT IDirectSoundNotifyImpl_Destroy(
175 IDirectSoundNotifyImpl *pdsn)
177 TRACE("(%p)\n",pdsn);
179 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
181 return DS_OK;
184 /*******************************************************************************
185 * IDirectSoundBuffer
188 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
189 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
191 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
193 TRACE("(%p,%p)\n",This,wfex);
194 /* This method is not available on secondary buffers */
195 WARN("invalid call\n");
196 return DSERR_INVALIDCALL;
199 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
200 LPDIRECTSOUNDBUFFER8 iface,LONG vol
202 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
203 LONG oldVol;
204 HRESULT hres = DS_OK;
206 TRACE("(%p,%d)\n",This,vol);
208 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
209 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
210 return DSERR_CONTROLUNAVAIL;
213 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
214 WARN("invalid parameter: vol = %d\n", vol);
215 return DSERR_INVALIDPARAM;
218 /* **** */
219 RtlAcquireResourceExclusive(&This->lock, TRUE);
221 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
222 oldVol = This->ds3db_lVolume;
223 This->ds3db_lVolume = vol;
224 if (vol != oldVol)
225 /* recalc 3d volume, which in turn recalcs the pans */
226 DSOUND_Calc3DBuffer(This);
227 } else {
228 oldVol = This->volpan.lVolume;
229 This->volpan.lVolume = vol;
230 if (vol != oldVol)
231 DSOUND_RecalcVolPan(&(This->volpan));
234 if (vol != oldVol) {
235 if (This->hwbuf) {
236 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
237 if (hres != DS_OK)
238 WARN("IDsDriverBuffer_SetVolumePan failed\n");
242 RtlReleaseResource(&This->lock);
243 /* **** */
245 return hres;
248 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
249 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
251 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
252 TRACE("(%p,%p)\n",This,vol);
254 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
255 WARN("control unavailable\n");
256 return DSERR_CONTROLUNAVAIL;
259 if (vol == NULL) {
260 WARN("invalid parameter: vol == NULL\n");
261 return DSERR_INVALIDPARAM;
264 *vol = This->volpan.lVolume;
266 return DS_OK;
269 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
270 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
272 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
273 DWORD oldFreq;
275 TRACE("(%p,%d)\n",This,freq);
277 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
278 WARN("control unavailable\n");
279 return DSERR_CONTROLUNAVAIL;
282 if (freq == DSBFREQUENCY_ORIGINAL)
283 freq = This->pwfx->nSamplesPerSec;
285 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
286 WARN("invalid parameter: freq = %d\n", freq);
287 return DSERR_INVALIDPARAM;
290 /* **** */
291 RtlAcquireResourceExclusive(&This->lock, TRUE);
293 oldFreq = This->freq;
294 This->freq = freq;
295 if (freq != oldFreq) {
296 This->freqAdjust = ((DWORD64)This->freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
297 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
298 DSOUND_RecalcFormat(This);
299 DSOUND_MixToTemporary(This, 0, This->buflen);
302 RtlReleaseResource(&This->lock);
303 /* **** */
305 return DS_OK;
308 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
309 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
311 HRESULT hres = DS_OK;
312 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
313 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
315 /* **** */
316 RtlAcquireResourceExclusive(&This->lock, TRUE);
318 This->playflags = flags;
319 if (This->state == STATE_STOPPED && !This->hwbuf) {
320 This->leadin = TRUE;
321 This->state = STATE_STARTING;
322 } else if (This->state == STATE_STOPPING)
323 This->state = STATE_PLAYING;
324 if (This->hwbuf) {
325 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
326 if (hres != DS_OK)
327 WARN("IDsDriverBuffer_Play failed\n");
328 else
329 This->state = STATE_PLAYING;
332 RtlReleaseResource(&This->lock);
333 /* **** */
335 return hres;
338 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
340 HRESULT hres = DS_OK;
341 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
342 TRACE("(%p)\n",This);
344 /* **** */
345 RtlAcquireResourceExclusive(&This->lock, TRUE);
347 if (This->state == STATE_PLAYING)
348 This->state = STATE_STOPPING;
349 else if (This->state == STATE_STARTING)
351 This->state = STATE_STOPPED;
352 DSOUND_CheckEvent(This, 0, 0);
354 if (This->hwbuf) {
355 hres = IDsDriverBuffer_Stop(This->hwbuf);
356 if (hres != DS_OK)
357 WARN("IDsDriverBuffer_Stop failed\n");
358 else
359 This->state = STATE_STOPPED;
362 RtlReleaseResource(&This->lock);
363 /* **** */
365 return hres;
368 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
370 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
371 ULONG ref = InterlockedIncrement(&(This->ref));
372 TRACE("(%p) ref was %d\n", This, ref - 1);
373 return ref;
376 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
378 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
379 ULONG ref = InterlockedDecrement(&(This->ref));
380 TRACE("(%p) ref was %d\n", This, ref + 1);
382 if (!ref) {
383 DirectSoundDevice_RemoveBuffer(This->device, This);
384 RtlDeleteResource(&This->lock);
386 if (This->hwbuf) {
387 IDsDriverBuffer_Release(This->hwbuf);
388 if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
389 This->buffer->ref--;
390 list_remove(&This->entry);
391 if (This->buffer->ref==0) {
392 HeapFree(GetProcessHeap(),0,This->buffer->memory);
393 HeapFree(GetProcessHeap(),0,This->buffer);
396 } else {
397 This->buffer->ref--;
398 list_remove(&This->entry);
399 if (This->buffer->ref==0) {
400 HeapFree(GetProcessHeap(),0,This->buffer->memory);
401 HeapFree(GetProcessHeap(),0,This->buffer);
405 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
406 HeapFree(GetProcessHeap(), 0, This->notifies);
407 HeapFree(GetProcessHeap(), 0, This->pwfx);
408 HeapFree(GetProcessHeap(), 0, This);
410 TRACE("(%p) released\n", This);
412 return ref;
415 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
416 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
418 HRESULT hres;
419 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
420 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
422 RtlAcquireResourceShared(&This->lock, TRUE);
423 if (This->hwbuf) {
424 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
425 if (hres != DS_OK) {
426 WARN("IDsDriverBuffer_GetPosition failed\n");
427 return hres;
429 } else {
430 DWORD pos = This->sec_mixpos;
432 /* sanity */
433 if (pos >= This->buflen){
434 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
435 pos %= This->buflen;
438 if (playpos)
439 *playpos = pos;
440 if (writepos)
441 *writepos = pos;
443 if (writepos && This->state != STATE_STOPPED && (!This->hwbuf || !(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD))) {
444 /* apply the documented 10ms lead to writepos */
445 *writepos += This->writelead;
446 *writepos %= This->buflen;
448 RtlReleaseResource(&This->lock);
450 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
451 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
453 return DS_OK;
456 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
457 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
459 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
460 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
462 if (status == NULL) {
463 WARN("invalid parameter: status = NULL\n");
464 return DSERR_INVALIDPARAM;
467 *status = 0;
468 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
469 *status |= DSBSTATUS_PLAYING;
470 if (This->playflags & DSBPLAY_LOOPING)
471 *status |= DSBSTATUS_LOOPING;
474 TRACE("status=%x\n", *status);
475 return DS_OK;
479 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
480 LPDIRECTSOUNDBUFFER8 iface,
481 LPWAVEFORMATEX lpwf,
482 DWORD wfsize,
483 LPDWORD wfwritten)
485 DWORD size;
486 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
487 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
489 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
491 if (lpwf) { /* NULL is valid */
492 if (wfsize >= size) {
493 CopyMemory(lpwf,This->pwfx,size);
494 if (wfwritten)
495 *wfwritten = size;
496 } else {
497 WARN("invalid parameter: wfsize too small\n");
498 if (wfwritten)
499 *wfwritten = 0;
500 return DSERR_INVALIDPARAM;
502 } else {
503 if (wfwritten)
504 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
505 else {
506 WARN("invalid parameter: wfwritten == NULL\n");
507 return DSERR_INVALIDPARAM;
511 return DS_OK;
514 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
515 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
517 HRESULT hres = DS_OK;
518 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
520 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
521 This,
522 writecursor,
523 writebytes,
524 lplpaudioptr1,
525 audiobytes1,
526 lplpaudioptr2,
527 audiobytes2,
528 flags,
529 GetTickCount()
532 /* when this flag is set, writecursor is meaningless and must be calculated */
533 if (flags & DSBLOCK_FROMWRITECURSOR) {
534 /* GetCurrentPosition does too much magic to duplicate here */
535 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
536 if (hres != DS_OK) {
537 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
538 return hres;
542 /* when this flag is set, writebytes is meaningless and must be set */
543 if (flags & DSBLOCK_ENTIREBUFFER)
544 writebytes = This->buflen;
546 if (writecursor >= This->buflen) {
547 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
548 writecursor, This->buflen);
549 return DSERR_INVALIDPARAM;
552 if (writebytes > This->buflen) {
553 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
554 writebytes, This->buflen);
555 return DSERR_INVALIDPARAM;
558 /* **** */
559 RtlAcquireResourceShared(&This->lock, TRUE);
561 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
562 hres = IDsDriverBuffer_Lock(This->hwbuf,
563 lplpaudioptr1, audiobytes1,
564 lplpaudioptr2, audiobytes2,
565 writecursor, writebytes,
567 if (hres != DS_OK) {
568 WARN("IDsDriverBuffer_Lock failed\n");
569 RtlReleaseResource(&This->lock);
570 return hres;
572 } else {
573 if (writecursor+writebytes <= This->buflen) {
574 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
575 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
576 WARN("Overwriting mixing position, case 1\n");
577 *audiobytes1 = writebytes;
578 if (lplpaudioptr2)
579 *(LPBYTE*)lplpaudioptr2 = NULL;
580 if (audiobytes2)
581 *audiobytes2 = 0;
582 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
583 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
584 TRACE("->%d.0\n",writebytes);
585 } else {
586 DWORD remainder = writebytes + writecursor - This->buflen;
587 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
588 *audiobytes1 = This->buflen-writecursor;
589 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
590 WARN("Overwriting mixing position, case 2\n");
591 if (lplpaudioptr2)
592 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
593 if (audiobytes2)
594 *audiobytes2 = writebytes-(This->buflen-writecursor);
595 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
596 WARN("Overwriting mixing position, case 3\n");
597 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
601 RtlReleaseResource(&This->lock);
602 /* **** */
604 return DS_OK;
607 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
608 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
610 HRESULT hres = DS_OK;
611 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
612 DWORD oldpos;
613 TRACE("(%p,%d)\n",This,newpos);
615 /* **** */
616 RtlAcquireResourceExclusive(&This->lock, TRUE);
618 oldpos = This->sec_mixpos;
620 /* start mixing from this new location instead */
621 newpos %= This->buflen;
622 newpos -= newpos%This->pwfx->nBlockAlign;
623 This->sec_mixpos = newpos;
625 /* at this point, do not attempt to reset buffers, mess with primary mix position,
626 or anything like that to reduce latancy. The data already prebuffered cannot be changed */
628 /* position HW buffer if applicable, else just start mixing from new location instead */
629 if (This->hwbuf) {
630 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
631 if (hres != DS_OK)
632 WARN("IDsDriverBuffer_SetPosition failed\n");
634 else if (oldpos != newpos)
635 /* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
636 This->buf_mixpos = DSOUND_secpos_to_bufpos(This, newpos, 0, NULL);
638 RtlReleaseResource(&This->lock);
639 /* **** */
641 return hres;
644 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
645 LPDIRECTSOUNDBUFFER8 iface,LONG pan
647 HRESULT hres = DS_OK;
648 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
650 TRACE("(%p,%d)\n",This,pan);
652 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
653 WARN("invalid parameter: pan = %d\n", pan);
654 return DSERR_INVALIDPARAM;
657 /* You cannot use both pan and 3D controls */
658 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
659 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
660 WARN("control unavailable\n");
661 return DSERR_CONTROLUNAVAIL;
664 /* **** */
665 RtlAcquireResourceExclusive(&This->lock, TRUE);
667 if (This->volpan.lPan != pan) {
668 This->volpan.lPan = pan;
669 DSOUND_RecalcVolPan(&(This->volpan));
671 if (This->hwbuf) {
672 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
673 if (hres != DS_OK)
674 WARN("IDsDriverBuffer_SetVolumePan failed\n");
678 RtlReleaseResource(&This->lock);
679 /* **** */
681 return hres;
684 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
685 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
687 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
688 TRACE("(%p,%p)\n",This,pan);
690 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
691 WARN("control unavailable\n");
692 return DSERR_CONTROLUNAVAIL;
695 if (pan == NULL) {
696 WARN("invalid parameter: pan = NULL\n");
697 return DSERR_INVALIDPARAM;
700 *pan = This->volpan.lPan;
702 return DS_OK;
705 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
706 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
708 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface, *iter;
709 HRESULT hres = DS_OK;
711 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
713 /* **** */
714 RtlAcquireResourceShared(&This->lock, TRUE);
716 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
717 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
718 if (hres != DS_OK)
719 WARN("IDsDriverBuffer_Unlock failed\n");
722 RtlReleaseResource(&This->lock);
723 /* **** */
725 if (!p2)
726 x2 = 0;
728 if (!This->hwbuf && (x1 || x2))
730 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
731 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
733 RtlAcquireResourceShared(&iter->lock, TRUE);
734 if (x1)
735 DSOUND_MixToTemporary(iter, (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory, x1);
736 if (x2)
737 DSOUND_MixToTemporary(iter, 0, x2);
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)
1028 WARN("Format not supported for hardware buffer\n");
1029 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1030 HeapFree(GetProcessHeap(),0,dsb);
1031 *pdsb = NULL;
1032 return DSERR_BADFORMAT;
1035 /* FIXME: check hardware sample rate mixing capabilities */
1036 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1037 /* FIXME: check whether any hardware buffers are left */
1038 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1040 /* Allocate an empty buffer */
1041 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1042 if (dsb->buffer == NULL) {
1043 WARN("out of memory\n");
1044 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1045 HeapFree(GetProcessHeap(),0,dsb);
1046 *pdsb = NULL;
1047 return DSERR_OUTOFMEMORY;
1050 /* Allocate system memory for buffer if applicable */
1051 if ((device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1052 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1053 if (dsb->buffer->memory == NULL) {
1054 WARN("out of memory\n");
1055 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1056 HeapFree(GetProcessHeap(),0,dsb->buffer);
1057 HeapFree(GetProcessHeap(),0,dsb);
1058 *pdsb = NULL;
1059 return DSERR_OUTOFMEMORY;
1063 /* Allocate the hardware buffer */
1064 if (use_hw) {
1065 err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1066 &(dsb->buflen),&(dsb->buffer->memory),
1067 (LPVOID*)&(dsb->hwbuf));
1068 if (FAILED(err))
1070 WARN("Failed to create hardware secondary buffer: %08x\n", err);
1071 if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)
1072 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1073 HeapFree(GetProcessHeap(),0,dsb->buffer);
1074 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1075 HeapFree(GetProcessHeap(),0,dsb);
1076 *pdsb = NULL;
1077 return DSERR_GENERIC;
1081 dsb->buffer->ref = 1;
1082 list_init(&dsb->buffer->buffers);
1083 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1084 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1086 /* It's not necessary to initialize values to zero since */
1087 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1088 dsb->buf_mixpos = dsb->sec_mixpos = 0;
1089 dsb->state = STATE_STOPPED;
1091 dsb->freqAdjust = ((DWORD64)dsb->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
1092 dsb->nAvgBytesPerSec = dsb->freq *
1093 dsbd->lpwfxFormat->nBlockAlign;
1095 /* calculate fragment size and write lead */
1096 DSOUND_RecalcFormat(dsb);
1098 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1099 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1100 dsb->ds3db_ds3db.vPosition.x = 0.0;
1101 dsb->ds3db_ds3db.vPosition.y = 0.0;
1102 dsb->ds3db_ds3db.vPosition.z = 0.0;
1103 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1104 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1105 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1106 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1107 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1108 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1109 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1110 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1111 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1112 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1113 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1114 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1116 dsb->ds3db_need_recalc = FALSE;
1117 DSOUND_Calc3DBuffer(dsb);
1118 } else
1119 DSOUND_RecalcVolPan(&(dsb->volpan));
1121 RtlInitializeResource(&dsb->lock);
1123 /* register buffer if not primary */
1124 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1125 err = DirectSoundDevice_AddBuffer(device, dsb);
1126 if (err != DS_OK) {
1127 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1128 HeapFree(GetProcessHeap(),0,dsb->buffer);
1129 RtlDeleteResource(&dsb->lock);
1130 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1131 HeapFree(GetProcessHeap(),0,dsb);
1132 dsb = NULL;
1136 *pdsb = dsb;
1137 return err;
1140 HRESULT IDirectSoundBufferImpl_Destroy(
1141 IDirectSoundBufferImpl *pdsb)
1143 TRACE("(%p)\n",pdsb);
1145 /* This keeps the *_Destroy functions from possibly deleting
1146 * this object until it is ready to be deleted */
1147 IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1149 if (pdsb->iks) {
1150 WARN("iks not NULL\n");
1151 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1152 pdsb->iks = NULL;
1155 if (pdsb->ds3db) {
1156 WARN("ds3db not NULL\n");
1157 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1158 pdsb->ds3db = NULL;
1161 if (pdsb->notify) {
1162 WARN("notify not NULL\n");
1163 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1164 pdsb->notify = NULL;
1167 if (pdsb->secondary) {
1168 WARN("dsb not NULL\n");
1169 SecondaryBufferImpl_Destroy(pdsb->secondary);
1170 pdsb->secondary = NULL;
1173 while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1175 return S_OK;
1178 HRESULT IDirectSoundBufferImpl_Duplicate(
1179 DirectSoundDevice *device,
1180 IDirectSoundBufferImpl **ppdsb,
1181 IDirectSoundBufferImpl *pdsb)
1183 IDirectSoundBufferImpl *dsb;
1184 HRESULT hres = DS_OK;
1185 int size;
1186 TRACE("(%p,%p,%p)\n", device, pdsb, pdsb);
1188 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1190 if (dsb == NULL) {
1191 WARN("out of memory\n");
1192 *ppdsb = NULL;
1193 return DSERR_OUTOFMEMORY;
1196 CopyMemory(dsb, pdsb, sizeof(IDirectSoundBufferImpl));
1198 if (pdsb->hwbuf) {
1199 TRACE("duplicating hardware buffer\n");
1201 hres = IDsDriver_DuplicateSoundBuffer(device->driver, pdsb->hwbuf,
1202 (LPVOID *)&dsb->hwbuf);
1203 if (FAILED(hres)) {
1204 WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres);
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);
1224 /* variable sized struct so calculate size based on format */
1225 size = sizeof(WAVEFORMATEX) + pdsb->pwfx->cbSize;
1227 dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
1228 if (dsb->pwfx == NULL) {
1229 WARN("out of memory\n");
1230 HeapFree(GetProcessHeap(),0,dsb->buffer);
1231 HeapFree(GetProcessHeap(),0,dsb);
1232 *ppdsb = NULL;
1233 return DSERR_OUTOFMEMORY;
1236 CopyMemory(dsb->pwfx, pdsb->pwfx, size);
1238 RtlInitializeResource(&dsb->lock);
1240 /* register buffer */
1241 hres = DirectSoundDevice_AddBuffer(device, dsb);
1242 if (hres != DS_OK) {
1243 RtlDeleteResource(&dsb->lock);
1244 HeapFree(GetProcessHeap(),0,dsb->tmp_buffer);
1245 HeapFree(GetProcessHeap(),0,dsb->buffer);
1246 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1247 HeapFree(GetProcessHeap(),0,dsb);
1248 *ppdsb = 0;
1251 *ppdsb = dsb;
1252 return hres;
1255 /*******************************************************************************
1256 * SecondaryBuffer
1259 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1260 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1262 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1263 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1265 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1268 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1270 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1271 ULONG ref = InterlockedIncrement(&(This->ref));
1272 TRACE("(%p) ref was %d\n", This, ref - 1);
1273 return ref;
1276 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1278 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1279 ULONG ref;
1280 TRACE("(%p)\n", This);
1281 ref = InterlockedDecrement(&(This->ref));
1282 TRACE("ref was %d\n", ref + 1);
1284 if (!ref) {
1285 This->dsb->secondary = NULL;
1286 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1287 HeapFree(GetProcessHeap(), 0, This);
1288 TRACE("(%p) released\n", This);
1290 return ref;
1293 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1294 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1296 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1297 TRACE("(%p)->(%p)\n",This,caps);
1299 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1302 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1303 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1305 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1306 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1308 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1311 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1312 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1314 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1315 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1317 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1320 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1321 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1323 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1324 TRACE("(%p,%p)\n",This,vol);
1326 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1329 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1330 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1332 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1333 TRACE("(%p,%p)\n",This,pan);
1335 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1338 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1339 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1341 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1342 TRACE("(%p,%p)\n",This,freq);
1344 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1347 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1348 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1350 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1351 TRACE("(%p,%p)\n",This,status);
1353 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1356 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1357 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1359 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1360 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1362 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1365 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1366 LPDIRECTSOUNDBUFFER8 iface,
1367 DWORD writecursor,
1368 DWORD writebytes,
1369 LPVOID *lplpaudioptr1,
1370 LPDWORD audiobytes1,
1371 LPVOID *lplpaudioptr2,
1372 LPDWORD audiobytes2,
1373 DWORD dwFlags)
1375 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1376 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1377 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1379 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1380 writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1383 static HRESULT WINAPI SecondaryBufferImpl_Play(
1384 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1386 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1387 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1389 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1392 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1393 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1395 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1396 TRACE("(%p,%d)\n",This,newpos);
1398 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1401 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1402 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1404 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1405 TRACE("(%p,%p)\n",This,wfex);
1407 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1410 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1411 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1413 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1414 TRACE("(%p,%d)\n",This,vol);
1416 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1419 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1420 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1422 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1423 TRACE("(%p,%d)\n",This,pan);
1425 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1428 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1429 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1431 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1432 TRACE("(%p,%d)\n",This,freq);
1434 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1437 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1439 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1440 TRACE("(%p)\n",This);
1442 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1445 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1446 LPDIRECTSOUNDBUFFER8 iface,
1447 LPVOID lpvAudioPtr1,
1448 DWORD dwAudioBytes1,
1449 LPVOID lpvAudioPtr2,
1450 DWORD dwAudioBytes2)
1452 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1453 TRACE("(%p,%p,%d,%p,%d)\n",
1454 This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1456 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1457 lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1460 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1461 LPDIRECTSOUNDBUFFER8 iface)
1463 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1464 TRACE("(%p)\n",This);
1466 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1469 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1470 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1472 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1473 TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1475 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1478 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1479 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1481 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1482 TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1484 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1487 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1488 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1490 SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1491 TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1493 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1496 static const IDirectSoundBuffer8Vtbl sbvt =
1498 SecondaryBufferImpl_QueryInterface,
1499 SecondaryBufferImpl_AddRef,
1500 SecondaryBufferImpl_Release,
1501 SecondaryBufferImpl_GetCaps,
1502 SecondaryBufferImpl_GetCurrentPosition,
1503 SecondaryBufferImpl_GetFormat,
1504 SecondaryBufferImpl_GetVolume,
1505 SecondaryBufferImpl_GetPan,
1506 SecondaryBufferImpl_GetFrequency,
1507 SecondaryBufferImpl_GetStatus,
1508 SecondaryBufferImpl_Initialize,
1509 SecondaryBufferImpl_Lock,
1510 SecondaryBufferImpl_Play,
1511 SecondaryBufferImpl_SetCurrentPosition,
1512 SecondaryBufferImpl_SetFormat,
1513 SecondaryBufferImpl_SetVolume,
1514 SecondaryBufferImpl_SetPan,
1515 SecondaryBufferImpl_SetFrequency,
1516 SecondaryBufferImpl_Stop,
1517 SecondaryBufferImpl_Unlock,
1518 SecondaryBufferImpl_Restore,
1519 SecondaryBufferImpl_SetFX,
1520 SecondaryBufferImpl_AcquireResources,
1521 SecondaryBufferImpl_GetObjectInPath
1524 HRESULT SecondaryBufferImpl_Create(
1525 IDirectSoundBufferImpl *dsb,
1526 SecondaryBufferImpl **psb)
1528 SecondaryBufferImpl *sb;
1529 TRACE("(%p,%p)\n",dsb,psb);
1531 sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1533 if (sb == 0) {
1534 WARN("out of memory\n");
1535 *psb = NULL;
1536 return DSERR_OUTOFMEMORY;
1538 sb->ref = 0;
1539 sb->dsb = dsb;
1540 sb->lpVtbl = &sbvt;
1542 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1543 *psb = sb;
1544 return S_OK;
1547 static HRESULT SecondaryBufferImpl_Destroy(
1548 SecondaryBufferImpl *pdsb)
1550 TRACE("(%p)\n",pdsb);
1552 while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1554 return S_OK;