comctl32/tests: Make functions from test macros.
[wine] / dlls / dsound / buffer.c
1 /*                      DirectSound
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998 Rob Riggs
5  * Copyright 2000-2002 TransGaming Technologies, Inc.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23
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"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
39
40 static HRESULT SecondaryBufferImpl_Destroy(SecondaryBufferImpl *pdsb);
41
42 /*******************************************************************************
43  *              IDirectSoundNotify
44  */
45
46 struct IDirectSoundNotifyImpl
47 {
48     /* IUnknown fields */
49     const IDirectSoundNotifyVtbl *lpVtbl;
50     LONG                        ref;
51     IDirectSoundBufferImpl*     dsb;
52 };
53
54 static HRESULT IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl *dsb,
55                                              IDirectSoundNotifyImpl **pdsn);
56 static HRESULT IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl *pdsn);
57
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);
63
64         if (This->dsb == NULL) {
65                 WARN("invalid parameter\n");
66                 return E_INVALIDARG;
67         }
68
69         return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
70 }
71
72 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
73 {
74     IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
75     ULONG ref = InterlockedIncrement(&(This->ref));
76     TRACE("(%p) ref was %d\n", This, ref - 1);
77     return ref;
78 }
79
80 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
81 {
82     IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
83     ULONG ref = InterlockedDecrement(&(This->ref));
84     TRACE("(%p) ref was %d\n", This, ref + 1);
85
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);
91     }
92     return ref;
93 }
94
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);
100
101         if (howmuch > 0 && notify == NULL) {
102             WARN("invalid parameter: notify == NULL\n");
103             return DSERR_INVALIDPARAM;
104         }
105
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);
111         }
112
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));
125
126             if (This->dsb->notifies == NULL) {
127                     WARN("out of memory\n");
128                     return DSERR_OUTOFMEMORY;
129             }
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;
136         }
137
138         return S_OK;
139 }
140
141 static const IDirectSoundNotifyVtbl dsnvt =
142 {
143     IDirectSoundNotifyImpl_QueryInterface,
144     IDirectSoundNotifyImpl_AddRef,
145     IDirectSoundNotifyImpl_Release,
146     IDirectSoundNotifyImpl_SetNotificationPositions,
147 };
148
149 static HRESULT IDirectSoundNotifyImpl_Create(
150     IDirectSoundBufferImpl * dsb,
151     IDirectSoundNotifyImpl **pdsn)
152 {
153     IDirectSoundNotifyImpl * dsn;
154     TRACE("(%p,%p)\n",dsb,pdsn);
155
156     dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dsn));
157
158     if (dsn == NULL) {
159         WARN("out of memory\n");
160         return DSERR_OUTOFMEMORY;
161     }
162
163     dsn->ref = 0;
164     dsn->lpVtbl = &dsnvt;
165     dsn->dsb = dsb;
166     dsb->notify = dsn;
167     IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
168
169     *pdsn = dsn;
170     return DS_OK;
171 }
172
173 static HRESULT IDirectSoundNotifyImpl_Destroy(
174     IDirectSoundNotifyImpl *pdsn)
175 {
176     TRACE("(%p)\n",pdsn);
177
178     while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
179
180     return DS_OK;
181 }
182
183 /*******************************************************************************
184  *              IDirectSoundBuffer
185  */
186
187 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
188         LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
189 ) {
190         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
191
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;
196 }
197
198 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
199         LPDIRECTSOUNDBUFFER8 iface,LONG vol
200 ) {
201         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
202         LONG oldVol;
203         HRESULT hres = DS_OK;
204
205         TRACE("(%p,%d)\n",This,vol);
206
207         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
208                 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
209                 return DSERR_CONTROLUNAVAIL;
210         }
211
212         if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
213                 WARN("invalid parameter: vol = %d\n", vol);
214                 return DSERR_INVALIDPARAM;
215         }
216
217         /* **** */
218         RtlAcquireResourceExclusive(&This->lock, TRUE);
219
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));
231         }
232
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");
238                 }
239         }
240
241         RtlReleaseResource(&This->lock);
242         /* **** */
243
244         return hres;
245 }
246
247 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
248         LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
249 ) {
250         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
251         TRACE("(%p,%p)\n",This,vol);
252
253         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
254                 WARN("control unavailable\n");
255                 return DSERR_CONTROLUNAVAIL;
256         }
257
258         if (vol == NULL) {
259                 WARN("invalid parameter: vol == NULL\n");
260                 return DSERR_INVALIDPARAM;
261         }
262
263         *vol = This->volpan.lVolume;
264
265         return DS_OK;
266 }
267
268 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
269         LPDIRECTSOUNDBUFFER8 iface,DWORD freq
270 ) {
271         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
272         DWORD oldFreq;
273
274         TRACE("(%p,%d)\n",This,freq);
275
276         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
277                 WARN("control unavailable\n");
278                 return DSERR_CONTROLUNAVAIL;
279         }
280
281         if (freq == DSBFREQUENCY_ORIGINAL)
282                 freq = This->pwfx->nSamplesPerSec;
283
284         if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
285                 WARN("invalid parameter: freq = %d\n", freq);
286                 return DSERR_INVALIDPARAM;
287         }
288
289         /* **** */
290         RtlAcquireResourceExclusive(&This->lock, TRUE);
291
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);
299         }
300
301         RtlReleaseResource(&This->lock);
302         /* **** */
303
304         return DS_OK;
305 }
306
307 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
308         LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
309 ) {
310         HRESULT hres = DS_OK;
311         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
312         TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
313
314         /* **** */
315         RtlAcquireResourceExclusive(&This->lock, TRUE);
316
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;
329         }
330
331         RtlReleaseResource(&This->lock);
332         /* **** */
333
334         return hres;
335 }
336
337 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
338 {
339         HRESULT hres = DS_OK;
340         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
341         TRACE("(%p)\n",This);
342
343         /* **** */
344         RtlAcquireResourceExclusive(&This->lock, TRUE);
345
346         if (This->state == STATE_PLAYING)
347                 This->state = STATE_STOPPING;
348         else if (This->state == STATE_STARTING)
349         {
350                 This->state = STATE_STOPPED;
351                 DSOUND_CheckEvent(This, 0, 0);
352         }
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;
359         }
360
361         RtlReleaseResource(&This->lock);
362         /* **** */
363
364         return hres;
365 }
366
367 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
368 {
369     IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
370     ULONG ref = InterlockedIncrement(&(This->ref));
371     TRACE("(%p) ref was %d\n", This, ref - 1);
372     return ref;
373 }
374
375 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
376 {
377     IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
378     ULONG ref = InterlockedDecrement(&(This->ref));
379     TRACE("(%p) ref was %d\n", This, ref + 1);
380
381     if (!ref) {
382         DirectSoundDevice_RemoveBuffer(This->device, This);
383         RtlDeleteResource(&This->lock);
384
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);
393                 }
394         }
395
396         HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
397         HeapFree(GetProcessHeap(), 0, This->notifies);
398         HeapFree(GetProcessHeap(), 0, This->pwfx);
399         HeapFree(GetProcessHeap(), 0, This);
400
401         TRACE("(%p) released\n", This);
402     }
403     return ref;
404 }
405
406 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
407         LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
408 ) {
409         HRESULT hres;
410         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
411         TRACE("(%p,%p,%p)\n",This,playpos,writepos);
412
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;
419                 }
420         } else {
421                 DWORD pos = This->sec_mixpos;
422
423                 /* sanity */
424                 if (pos >= This->buflen){
425                         FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
426                         pos %= This->buflen;
427                 }
428
429                 if (playpos)
430                         *playpos = pos;
431                 if (writepos)
432                         *writepos = pos;
433         }
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;
438         }
439         RtlReleaseResource(&This->lock);
440
441         TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
442                 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
443
444         return DS_OK;
445 }
446
447 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
448         LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
449 ) {
450         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
451         TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
452
453         if (status == NULL) {
454                 WARN("invalid parameter: status = NULL\n");
455                 return DSERR_INVALIDPARAM;
456         }
457
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;
464         }
465         RtlReleaseResource(&This->lock);
466
467         TRACE("status=%x\n", *status);
468         return DS_OK;
469 }
470
471
472 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
473     LPDIRECTSOUNDBUFFER8 iface,
474     LPWAVEFORMATEX lpwf,
475     DWORD wfsize,
476     LPDWORD wfwritten)
477 {
478     DWORD size;
479     IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
480     TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
481
482     size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
483
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;
495         }
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;
502         }
503     }
504
505     return DS_OK;
506 }
507
508 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
509         LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
510 ) {
511         HRESULT hres = DS_OK;
512         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
513
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()
524         );
525
526         if (!audiobytes1)
527             return DSERR_INVALIDPARAM;
528
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;
536                 }
537         }
538
539         /* when this flag is set, writebytes is meaningless and must be set */
540         if (flags & DSBLOCK_ENTIREBUFFER)
541                 writebytes = This->buflen;
542
543         if (writecursor >= This->buflen) {
544                 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
545                      writecursor, This->buflen);
546                 return DSERR_INVALIDPARAM;
547         }
548
549         if (writebytes > This->buflen) {
550                 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
551                      writebytes, This->buflen);
552                 return DSERR_INVALIDPARAM;
553         }
554
555         /* **** */
556         RtlAcquireResourceShared(&This->lock, TRUE);
557
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,
563                                      0);
564                 if (hres != DS_OK) {
565                         WARN("IDsDriverBuffer_Lock failed\n");
566                         RtlReleaseResource(&This->lock);
567                         return hres;
568                 }
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);
595                 }
596         }
597
598         RtlReleaseResource(&This->lock);
599         /* **** */
600
601         return DS_OK;
602 }
603
604 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
605         LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
606 ) {
607         HRESULT hres = DS_OK;
608         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
609         DWORD oldpos;
610         TRACE("(%p,%d)\n",This,newpos);
611
612         /* **** */
613         RtlAcquireResourceExclusive(&This->lock, TRUE);
614
615         oldpos = This->sec_mixpos;
616
617         /* start mixing from this new location instead */
618         newpos %= This->buflen;
619         newpos -= newpos%This->pwfx->nBlockAlign;
620         This->sec_mixpos = newpos;
621
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 */
624
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");
630         }
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);
634
635         RtlReleaseResource(&This->lock);
636         /* **** */
637
638         return hres;
639 }
640
641 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
642         LPDIRECTSOUNDBUFFER8 iface,LONG pan
643 ) {
644         HRESULT hres = DS_OK;
645         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
646
647         TRACE("(%p,%d)\n",This,pan);
648
649         if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
650                 WARN("invalid parameter: pan = %d\n", pan);
651                 return DSERR_INVALIDPARAM;
652         }
653
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;
659         }
660
661         /* **** */
662         RtlAcquireResourceExclusive(&This->lock, TRUE);
663
664         if (This->volpan.lPan != pan) {
665                 This->volpan.lPan = pan;
666                 DSOUND_RecalcVolPan(&(This->volpan));
667
668                 if (This->hwbuf) {
669                         hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
670                         if (hres != DS_OK)
671                                 WARN("IDsDriverBuffer_SetVolumePan failed\n");
672                 }
673         }
674
675         RtlReleaseResource(&This->lock);
676         /* **** */
677
678         return hres;
679 }
680
681 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
682         LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
683 ) {
684         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
685         TRACE("(%p,%p)\n",This,pan);
686
687         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
688                 WARN("control unavailable\n");
689                 return DSERR_CONTROLUNAVAIL;
690         }
691
692         if (pan == NULL) {
693                 WARN("invalid parameter: pan = NULL\n");
694                 return DSERR_INVALIDPARAM;
695         }
696
697         *pan = This->volpan.lPan;
698
699         return DS_OK;
700 }
701
702 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
703         LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
704 ) {
705         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface, *iter;
706         HRESULT hres = DS_OK;
707
708         TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
709
710         /* **** */
711         RtlAcquireResourceShared(&This->lock, TRUE);
712
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");
717         }
718
719         RtlReleaseResource(&This->lock);
720         /* **** */
721
722         if (!p2)
723                 x2 = 0;
724
725         if (!This->hwbuf && (x1 || x2))
726         {
727                 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
728                 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
729                 {
730                         RtlAcquireResourceShared(&iter->lock, TRUE);
731                         if (x1)
732                         {
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);
737                         }
738                         if (x2)
739                                 DSOUND_MixToTemporary(iter, 0, x2, FALSE);
740                         RtlReleaseResource(&iter->lock);
741                 }
742                 RtlReleaseResource(&This->device->buffer_list_lock);
743         }
744
745         return hres;
746 }
747
748 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
749         LPDIRECTSOUNDBUFFER8 iface
750 ) {
751         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
752         FIXME("(%p):stub\n",This);
753         return DS_OK;
754 }
755
756 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
757         LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
758 ) {
759         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
760         TRACE("(%p,%p)\n",This,freq);
761
762         if (freq == NULL) {
763                 WARN("invalid parameter: freq = NULL\n");
764                 return DSERR_INVALIDPARAM;
765         }
766
767         *freq = This->freq;
768         TRACE("-> %d\n", *freq);
769
770         return DS_OK;
771 }
772
773 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
774         LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
775 ) {
776         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
777         DWORD u;
778
779         FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
780
781         if (pdwResultCodes)
782                 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
783
784         WARN("control unavailable\n");
785         return DSERR_CONTROLUNAVAIL;
786 }
787
788 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
789         LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
790 ) {
791         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
792         DWORD u;
793
794         FIXME("(%p,%08u,%u,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
795
796         if (pdwResultCodes)
797                 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
798
799         WARN("control unavailable\n");
800         return DS_OK;
801 }
802
803 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
804         LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
805 ) {
806         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
807
808         FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
809
810         WARN("control unavailable\n");
811         return DSERR_CONTROLUNAVAIL;
812 }
813
814 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
815         LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
816 ) {
817         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
818         WARN("(%p) already initialized\n", This);
819         return DSERR_ALREADYINITIALIZED;
820 }
821
822 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
823         LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
824 ) {
825         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
826         TRACE("(%p)->(%p)\n",This,caps);
827
828         if (caps == NULL) {
829                 WARN("invalid parameter: caps == NULL\n");
830                 return DSERR_INVALIDPARAM;
831         }
832
833         if (caps->dwSize < sizeof(*caps)) {
834                 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
835                 return DSERR_INVALIDPARAM;
836         }
837
838         caps->dwFlags = This->dsbd.dwFlags;
839         if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
840         else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
841
842         caps->dwBufferBytes = This->buflen;
843
844         /* According to windows, this is zero*/
845         caps->dwUnlockTransferRate = 0;
846         caps->dwPlayCpuOverhead = 0;
847
848         return DS_OK;
849 }
850
851 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
852         LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
853 ) {
854         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
855
856         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
857
858         if (ppobj == NULL) {
859                 WARN("invalid parameter\n");
860                 return E_INVALIDARG;
861         }
862
863         *ppobj = NULL;  /* assume failure */
864
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;
874                 }
875                 WARN("IID_IDirectSoundBuffer\n");
876                 return E_NOINTERFACE;
877         }
878
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;
886                 }
887                 WARN("IID_IDirectSoundNotify\n");
888                 return E_NOINTERFACE;
889         }
890
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;
898                 }
899                 WARN("IID_IDirectSound3DBuffer\n");
900                 return E_NOINTERFACE;
901         }
902
903         if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
904                 ERR("app requested IDirectSound3DListener on secondary buffer\n");
905                 return E_NOINTERFACE;
906         }
907
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;
915                 }
916                 WARN("IID_IKsPropertySet\n");
917                 return E_NOINTERFACE;
918         }
919
920         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
921
922         return E_NOINTERFACE;
923 }
924
925 static const IDirectSoundBuffer8Vtbl dsbvt =
926 {
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
951 };
952
953 HRESULT IDirectSoundBufferImpl_Create(
954         DirectSoundDevice * device,
955         IDirectSoundBufferImpl **pdsb,
956         LPCDSBUFFERDESC dsbd)
957 {
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);
964
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? */
969         }
970
971         dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
972
973         if (dsb == 0) {
974                 WARN("out of memory\n");
975                 *pdsb = NULL;
976                 return DSERR_OUTOFMEMORY;
977         }
978
979         TRACE("Created buffer at %p\n", dsb);
980
981         dsb->ref = 0;
982         dsb->secondary = 0;
983         dsb->device = device;
984         dsb->lpVtbl = &dsbvt;
985         dsb->iks = NULL;
986
987         /* size depends on version */
988         CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
989
990         dsb->pwfx = DSOUND_CopyFormat(wfex);
991         if (dsb->pwfx == NULL) {
992                 HeapFree(GetProcessHeap(),0,dsb);
993                 *pdsb = NULL;
994                 return DSERR_OUTOFMEMORY;
995         }
996
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;
1003
1004         dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1005         dsb->notify = NULL;
1006         dsb->notifies = NULL;
1007         dsb->nrofnotifies = 0;
1008         dsb->hwnotify = 0;
1009
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;
1015
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))
1019         {
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;
1028         }
1029
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 */
1034
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;
1043         }
1044
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;
1055                 }
1056         }
1057
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))
1064                 {
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;
1073                 }
1074         }
1075
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);
1080
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;
1085
1086         dsb->freqAdjust = ((DWORD64)dsb->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
1087         dsb->nAvgBytesPerSec = dsb->freq *
1088                 dsbd->lpwfxFormat->nBlockAlign;
1089
1090         /* calculate fragment size and write lead */
1091         DSOUND_RecalcFormat(dsb);
1092
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;
1110
1111                 dsb->ds3db_need_recalc = FALSE;
1112                 DSOUND_Calc3DBuffer(dsb);
1113         } else
1114                 DSOUND_RecalcVolPan(&(dsb->volpan));
1115
1116         RtlInitializeResource(&dsb->lock);
1117
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;
1128                 }
1129         }
1130
1131         *pdsb = dsb;
1132         return err;
1133 }
1134
1135 HRESULT IDirectSoundBufferImpl_Destroy(
1136     IDirectSoundBufferImpl *pdsb)
1137 {
1138     TRACE("(%p)\n",pdsb);
1139
1140     /* This keeps the *_Destroy functions from possibly deleting
1141      * this object until it is ready to be deleted */
1142     IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1143
1144     if (pdsb->iks) {
1145         WARN("iks not NULL\n");
1146         IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1147         pdsb->iks = NULL;
1148     }
1149
1150     if (pdsb->ds3db) {
1151         WARN("ds3db not NULL\n");
1152         IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1153         pdsb->ds3db = NULL;
1154     }
1155
1156     if (pdsb->notify) {
1157         WARN("notify not NULL\n");
1158         IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1159         pdsb->notify = NULL;
1160     }
1161
1162     if (pdsb->secondary) {
1163         WARN("dsb not NULL\n");
1164         SecondaryBufferImpl_Destroy(pdsb->secondary);
1165         pdsb->secondary = NULL;
1166     }
1167
1168     while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1169
1170     return S_OK;
1171 }
1172
1173 HRESULT IDirectSoundBufferImpl_Duplicate(
1174     DirectSoundDevice *device,
1175     IDirectSoundBufferImpl **ppdsb,
1176     IDirectSoundBufferImpl *pdsb)
1177 {
1178     IDirectSoundBufferImpl *dsb;
1179     HRESULT hres = DS_OK;
1180     TRACE("(%p,%p,%p)\n", device, pdsb, pdsb);
1181
1182     dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
1183     if (dsb == NULL) {
1184         WARN("out of memory\n");
1185         *ppdsb = NULL;
1186         return DSERR_OUTOFMEMORY;
1187     }
1188     CopyMemory(dsb, pdsb, sizeof(*dsb));
1189
1190     dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1191     if (dsb->pwfx == NULL) {
1192         HeapFree(GetProcessHeap(),0,dsb);
1193         *ppdsb = NULL;
1194         return DSERR_OUTOFMEMORY;
1195     }
1196
1197     if (pdsb->hwbuf) {
1198         TRACE("duplicating hardware buffer\n");
1199
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;
1208         }
1209     }
1210
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);
1223
1224     RtlInitializeResource(&dsb->lock);
1225
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;
1236     }
1237
1238     *ppdsb = dsb;
1239     return hres;
1240 }
1241
1242 /*******************************************************************************
1243  *              SecondaryBuffer
1244  */
1245
1246 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1247         LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1248 {
1249         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1250         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1251
1252         return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1253 }
1254
1255 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1256 {
1257     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1258     ULONG ref = InterlockedIncrement(&(This->ref));
1259     TRACE("(%p) ref was %d\n", This, ref - 1);
1260     return ref;
1261 }
1262
1263 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1264 {
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);
1270
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);
1276     }
1277     return ref;
1278 }
1279
1280 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1281         LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1282 {
1283         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1284         TRACE("(%p)->(%p)\n",This,caps);
1285
1286         return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1287 }
1288
1289 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1290         LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1291 {
1292         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1293         TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1294
1295         return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1296 }
1297
1298 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1299         LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1300 {
1301         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1302         TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1303
1304         return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1305 }
1306
1307 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1308         LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1309 {
1310         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1311         TRACE("(%p,%p)\n",This,vol);
1312
1313         return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1314 }
1315
1316 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1317         LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1318 {
1319         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1320         TRACE("(%p,%p)\n",This,pan);
1321
1322         return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1323 }
1324
1325 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1326         LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1327 {
1328         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1329         TRACE("(%p,%p)\n",This,freq);
1330
1331         return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1332 }
1333
1334 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1335         LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1336 {
1337         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1338         TRACE("(%p,%p)\n",This,status);
1339
1340         return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1341 }
1342
1343 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1344         LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1345 {
1346         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1347         TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1348
1349         return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1350 }
1351
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)
1361 {
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);
1365
1366     return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1367         writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1368 }
1369
1370 static HRESULT WINAPI SecondaryBufferImpl_Play(
1371         LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1372 {
1373         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1374         TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1375
1376         return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1377 }
1378
1379 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1380         LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1381 {
1382         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1383         TRACE("(%p,%d)\n",This,newpos);
1384
1385         return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1386 }
1387
1388 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1389         LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1390 {
1391         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1392         TRACE("(%p,%p)\n",This,wfex);
1393
1394         return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1395 }
1396
1397 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1398         LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1399 {
1400         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1401         TRACE("(%p,%d)\n",This,vol);
1402
1403         return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1404 }
1405
1406 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1407         LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1408 {
1409         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1410         TRACE("(%p,%d)\n",This,pan);
1411
1412         return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1413 }
1414
1415 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1416         LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1417 {
1418         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1419         TRACE("(%p,%d)\n",This,freq);
1420
1421         return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1422 }
1423
1424 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1425 {
1426         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1427         TRACE("(%p)\n",This);
1428
1429         return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1430 }
1431
1432 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1433     LPDIRECTSOUNDBUFFER8 iface,
1434     LPVOID lpvAudioPtr1,
1435     DWORD dwAudioBytes1,
1436     LPVOID lpvAudioPtr2,
1437     DWORD dwAudioBytes2)
1438 {
1439     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1440     TRACE("(%p,%p,%d,%p,%d)\n",
1441         This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1442
1443     return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1444         lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1445 }
1446
1447 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1448         LPDIRECTSOUNDBUFFER8 iface)
1449 {
1450         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1451         TRACE("(%p)\n",This);
1452
1453         return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1454 }
1455
1456 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1457         LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1458 {
1459         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1460         TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1461
1462         return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1463 }
1464
1465 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1466         LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1467 {
1468         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1469         TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1470
1471         return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1472 }
1473
1474 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1475         LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1476 {
1477         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1478         TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1479
1480         return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1481 }
1482
1483 static const IDirectSoundBuffer8Vtbl sbvt =
1484 {
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
1509 };
1510
1511 HRESULT SecondaryBufferImpl_Create(
1512         IDirectSoundBufferImpl *dsb,
1513         SecondaryBufferImpl **psb)
1514 {
1515         SecondaryBufferImpl *sb;
1516         TRACE("(%p,%p)\n",dsb,psb);
1517
1518         sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1519
1520         if (sb == 0) {
1521                 WARN("out of memory\n");
1522                 *psb = NULL;
1523                 return DSERR_OUTOFMEMORY;
1524         }
1525         sb->ref = 0;
1526         sb->dsb = dsb;
1527         sb->lpVtbl = &sbvt;
1528
1529         IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1530         *psb = sb;
1531         return S_OK;
1532 }
1533
1534 static HRESULT SecondaryBufferImpl_Destroy(
1535     SecondaryBufferImpl *pdsb)
1536 {
1537     TRACE("(%p)\n",pdsb);
1538
1539     while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1540
1541     return S_OK;
1542 }
1543
1544 /*******************************************************************************
1545  *              IKsBufferPropertySet
1546  */
1547
1548 /* IUnknown methods */
1549 static HRESULT WINAPI IKsBufferPropertySetImpl_QueryInterface(
1550     LPKSPROPERTYSET iface,
1551     REFIID riid,
1552     LPVOID *ppobj )
1553 {
1554     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1555     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1556
1557     return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
1558 }
1559
1560 static ULONG WINAPI IKsBufferPropertySetImpl_AddRef(LPKSPROPERTYSET iface)
1561 {
1562     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1563     ULONG ref = InterlockedIncrement(&(This->ref));
1564     TRACE("(%p) ref was %d\n", This, ref - 1);
1565     return ref;
1566 }
1567
1568 static ULONG WINAPI IKsBufferPropertySetImpl_Release(LPKSPROPERTYSET iface)
1569 {
1570     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1571     ULONG ref = InterlockedDecrement(&(This->ref));
1572     TRACE("(%p) ref was %d\n", This, ref + 1);
1573
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);
1579     }
1580     return ref;
1581 }
1582
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 )
1592 {
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);
1597
1598     if (This->dsb->hwbuf) {
1599         IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1600
1601         if (ps) {
1602         DSPROPERTY prop;
1603         HRESULT hres;
1604
1605         prop.s.Set = *guidPropSet;
1606         prop.s.Id = dwPropID;
1607         prop.s.Flags = 0;  /* unused */
1608         prop.s.InstanceId = (ULONG)This->dsb->device;
1609
1610
1611         hres = IDsDriverPropertySet_Get(ps, &prop, pInstanceData, cbInstanceData, pPropData, cbPropData, pcbReturned);
1612
1613         IDsDriverPropertySet_Release(ps);
1614
1615         return hres;
1616         }
1617     }
1618
1619     return E_PROP_ID_UNSUPPORTED;
1620 }
1621
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 )
1630 {
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);
1634
1635     if (This->dsb->hwbuf) {
1636         IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1637
1638         if (ps) {
1639         DSPROPERTY prop;
1640         HRESULT hres;
1641
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);
1647
1648         IDsDriverPropertySet_Release(ps);
1649
1650         return hres;
1651         }
1652     }
1653
1654     return E_PROP_ID_UNSUPPORTED;
1655 }
1656
1657 static HRESULT WINAPI IKsBufferPropertySetImpl_QuerySupport(
1658     LPKSPROPERTYSET iface,
1659     REFGUID guidPropSet,
1660     ULONG dwPropID,
1661     PULONG pTypeSupport )
1662 {
1663     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1664     PIDSDRIVERPROPERTYSET ps;
1665     TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1666
1667     if (This->dsb->hwbuf) {
1668         IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1669
1670         if (ps) {
1671             HRESULT hres;
1672
1673             hres = IDsDriverPropertySet_QuerySupport(ps,guidPropSet, dwPropID,pTypeSupport);
1674
1675             IDsDriverPropertySet_Release(ps);
1676
1677             return hres;
1678         }
1679     }
1680
1681     return E_PROP_ID_UNSUPPORTED;
1682 }
1683
1684 static const IKsPropertySetVtbl iksbvt = {
1685     IKsBufferPropertySetImpl_QueryInterface,
1686     IKsBufferPropertySetImpl_AddRef,
1687     IKsBufferPropertySetImpl_Release,
1688     IKsBufferPropertySetImpl_Get,
1689     IKsBufferPropertySetImpl_Set,
1690     IKsBufferPropertySetImpl_QuerySupport
1691 };
1692
1693 HRESULT IKsBufferPropertySetImpl_Create(
1694     IDirectSoundBufferImpl *dsb,
1695     IKsBufferPropertySetImpl **piks)
1696 {
1697     IKsBufferPropertySetImpl *iks;
1698     TRACE("(%p,%p)\n",dsb,piks);
1699     *piks = NULL;
1700
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;
1706     }
1707
1708     iks->ref = 0;
1709     iks->dsb = dsb;
1710     dsb->iks = iks;
1711     iks->lpVtbl = &iksbvt;
1712
1713     IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
1714
1715     *piks = iks;
1716     return S_OK;
1717 }
1718
1719 HRESULT IKsBufferPropertySetImpl_Destroy(
1720     IKsBufferPropertySetImpl *piks)
1721 {
1722     TRACE("(%p)\n",piks);
1723
1724     while (IKsBufferPropertySetImpl_Release((LPKSPROPERTYSET)piks) > 0);
1725
1726     return S_OK;
1727 }