mshtml: Populate dynamic properties table in get_dynamic_data.
[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 latency. 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, ppdsb, 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->notify = NULL;
1217     dsb->notifies = NULL;
1218     dsb->nrofnotifies = 0;
1219     dsb->device = device;
1220     dsb->ds3db = NULL;
1221     dsb->iks = NULL; /* FIXME? */
1222     dsb->secondary = NULL;
1223     dsb->tmp_buffer = NULL;
1224     DSOUND_RecalcFormat(dsb);
1225     DSOUND_MixToTemporary(dsb, 0, dsb->buflen, FALSE);
1226
1227     RtlInitializeResource(&dsb->lock);
1228
1229     /* register buffer */
1230     hres = DirectSoundDevice_AddBuffer(device, dsb);
1231     if (hres != DS_OK) {
1232         RtlDeleteResource(&dsb->lock);
1233         HeapFree(GetProcessHeap(),0,dsb->tmp_buffer);
1234         list_remove(&dsb->entry);
1235         dsb->buffer->ref--;
1236         HeapFree(GetProcessHeap(),0,dsb->pwfx);
1237         HeapFree(GetProcessHeap(),0,dsb);
1238         dsb = NULL;
1239     }
1240
1241     *ppdsb = dsb;
1242     return hres;
1243 }
1244
1245 /*******************************************************************************
1246  *              SecondaryBuffer
1247  */
1248
1249 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1250         LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1251 {
1252         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1253         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1254
1255         return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1256 }
1257
1258 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1259 {
1260     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1261     ULONG ref = InterlockedIncrement(&(This->ref));
1262     TRACE("(%p) ref was %d\n", This, ref - 1);
1263     return ref;
1264 }
1265
1266 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1267 {
1268     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1269     ULONG ref;
1270     TRACE("(%p)\n", This);
1271     ref = InterlockedDecrement(&(This->ref));
1272     TRACE("ref was %d\n", ref + 1);
1273
1274     if (!ref) {
1275         This->dsb->secondary = NULL;
1276         IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1277         HeapFree(GetProcessHeap(), 0, This);
1278         TRACE("(%p) released\n", This);
1279     }
1280     return ref;
1281 }
1282
1283 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1284         LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1285 {
1286         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1287         TRACE("(%p)->(%p)\n",This,caps);
1288
1289         return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1290 }
1291
1292 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1293         LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1294 {
1295         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1296         TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1297
1298         return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1299 }
1300
1301 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1302         LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1303 {
1304         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1305         TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1306
1307         return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1308 }
1309
1310 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1311         LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1312 {
1313         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1314         TRACE("(%p,%p)\n",This,vol);
1315
1316         return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1317 }
1318
1319 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1320         LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1321 {
1322         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1323         TRACE("(%p,%p)\n",This,pan);
1324
1325         return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1326 }
1327
1328 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1329         LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1330 {
1331         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1332         TRACE("(%p,%p)\n",This,freq);
1333
1334         return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1335 }
1336
1337 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1338         LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1339 {
1340         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1341         TRACE("(%p,%p)\n",This,status);
1342
1343         return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1344 }
1345
1346 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1347         LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1348 {
1349         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1350         TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1351
1352         return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1353 }
1354
1355 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1356     LPDIRECTSOUNDBUFFER8 iface,
1357     DWORD writecursor,
1358     DWORD writebytes,
1359     LPVOID *lplpaudioptr1,
1360     LPDWORD audiobytes1,
1361     LPVOID *lplpaudioptr2,
1362     LPDWORD audiobytes2,
1363     DWORD dwFlags)
1364 {
1365     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1366     TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1367         This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1368
1369     return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1370         writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1371 }
1372
1373 static HRESULT WINAPI SecondaryBufferImpl_Play(
1374         LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1375 {
1376         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1377         TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1378
1379         return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1380 }
1381
1382 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1383         LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1384 {
1385         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1386         TRACE("(%p,%d)\n",This,newpos);
1387
1388         return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1389 }
1390
1391 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1392         LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1393 {
1394         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1395         TRACE("(%p,%p)\n",This,wfex);
1396
1397         return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1398 }
1399
1400 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1401         LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1402 {
1403         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1404         TRACE("(%p,%d)\n",This,vol);
1405
1406         return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1407 }
1408
1409 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1410         LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1411 {
1412         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1413         TRACE("(%p,%d)\n",This,pan);
1414
1415         return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1416 }
1417
1418 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1419         LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1420 {
1421         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1422         TRACE("(%p,%d)\n",This,freq);
1423
1424         return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1425 }
1426
1427 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1428 {
1429         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1430         TRACE("(%p)\n",This);
1431
1432         return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1433 }
1434
1435 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1436     LPDIRECTSOUNDBUFFER8 iface,
1437     LPVOID lpvAudioPtr1,
1438     DWORD dwAudioBytes1,
1439     LPVOID lpvAudioPtr2,
1440     DWORD dwAudioBytes2)
1441 {
1442     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1443     TRACE("(%p,%p,%d,%p,%d)\n",
1444         This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1445
1446     return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1447         lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1448 }
1449
1450 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1451         LPDIRECTSOUNDBUFFER8 iface)
1452 {
1453         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1454         TRACE("(%p)\n",This);
1455
1456         return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1457 }
1458
1459 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1460         LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1461 {
1462         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1463         TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1464
1465         return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1466 }
1467
1468 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1469         LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1470 {
1471         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1472         TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1473
1474         return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1475 }
1476
1477 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1478         LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1479 {
1480         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1481         TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1482
1483         return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1484 }
1485
1486 static const IDirectSoundBuffer8Vtbl sbvt =
1487 {
1488         SecondaryBufferImpl_QueryInterface,
1489         SecondaryBufferImpl_AddRef,
1490         SecondaryBufferImpl_Release,
1491         SecondaryBufferImpl_GetCaps,
1492         SecondaryBufferImpl_GetCurrentPosition,
1493         SecondaryBufferImpl_GetFormat,
1494         SecondaryBufferImpl_GetVolume,
1495         SecondaryBufferImpl_GetPan,
1496         SecondaryBufferImpl_GetFrequency,
1497         SecondaryBufferImpl_GetStatus,
1498         SecondaryBufferImpl_Initialize,
1499         SecondaryBufferImpl_Lock,
1500         SecondaryBufferImpl_Play,
1501         SecondaryBufferImpl_SetCurrentPosition,
1502         SecondaryBufferImpl_SetFormat,
1503         SecondaryBufferImpl_SetVolume,
1504         SecondaryBufferImpl_SetPan,
1505         SecondaryBufferImpl_SetFrequency,
1506         SecondaryBufferImpl_Stop,
1507         SecondaryBufferImpl_Unlock,
1508         SecondaryBufferImpl_Restore,
1509         SecondaryBufferImpl_SetFX,
1510         SecondaryBufferImpl_AcquireResources,
1511         SecondaryBufferImpl_GetObjectInPath
1512 };
1513
1514 HRESULT SecondaryBufferImpl_Create(
1515         IDirectSoundBufferImpl *dsb,
1516         SecondaryBufferImpl **psb)
1517 {
1518         SecondaryBufferImpl *sb;
1519         TRACE("(%p,%p)\n",dsb,psb);
1520
1521         sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1522
1523         if (sb == 0) {
1524                 WARN("out of memory\n");
1525                 *psb = NULL;
1526                 return DSERR_OUTOFMEMORY;
1527         }
1528         sb->ref = 0;
1529         sb->dsb = dsb;
1530         sb->lpVtbl = &sbvt;
1531
1532         IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1533         *psb = sb;
1534         return S_OK;
1535 }
1536
1537 static HRESULT SecondaryBufferImpl_Destroy(
1538     SecondaryBufferImpl *pdsb)
1539 {
1540     TRACE("(%p)\n",pdsb);
1541
1542     while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1543
1544     return S_OK;
1545 }
1546
1547 /*******************************************************************************
1548  *              IKsBufferPropertySet
1549  */
1550
1551 /* IUnknown methods */
1552 static HRESULT WINAPI IKsBufferPropertySetImpl_QueryInterface(
1553     LPKSPROPERTYSET iface,
1554     REFIID riid,
1555     LPVOID *ppobj )
1556 {
1557     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1558     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1559
1560     return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
1561 }
1562
1563 static ULONG WINAPI IKsBufferPropertySetImpl_AddRef(LPKSPROPERTYSET iface)
1564 {
1565     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1566     ULONG ref = InterlockedIncrement(&(This->ref));
1567     TRACE("(%p) ref was %d\n", This, ref - 1);
1568     return ref;
1569 }
1570
1571 static ULONG WINAPI IKsBufferPropertySetImpl_Release(LPKSPROPERTYSET iface)
1572 {
1573     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1574     ULONG ref = InterlockedDecrement(&(This->ref));
1575     TRACE("(%p) ref was %d\n", This, ref + 1);
1576
1577     if (!ref) {
1578     This->dsb->iks = 0;
1579     IDirectSoundBuffer_Release((LPDIRECTSOUND3DBUFFER)This->dsb);
1580     HeapFree(GetProcessHeap(), 0, This);
1581     TRACE("(%p) released\n", This);
1582     }
1583     return ref;
1584 }
1585
1586 static HRESULT WINAPI IKsBufferPropertySetImpl_Get(
1587     LPKSPROPERTYSET iface,
1588     REFGUID guidPropSet,
1589     ULONG dwPropID,
1590     LPVOID pInstanceData,
1591     ULONG cbInstanceData,
1592     LPVOID pPropData,
1593     ULONG cbPropData,
1594     PULONG pcbReturned )
1595 {
1596     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1597     PIDSDRIVERPROPERTYSET ps;
1598     TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1599     This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1600
1601     if (This->dsb->hwbuf) {
1602         IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1603
1604         if (ps) {
1605         DSPROPERTY prop;
1606         HRESULT hres;
1607
1608         prop.s.Set = *guidPropSet;
1609         prop.s.Id = dwPropID;
1610         prop.s.Flags = 0;  /* unused */
1611         prop.s.InstanceId = (ULONG)This->dsb->device;
1612
1613
1614         hres = IDsDriverPropertySet_Get(ps, &prop, pInstanceData, cbInstanceData, pPropData, cbPropData, pcbReturned);
1615
1616         IDsDriverPropertySet_Release(ps);
1617
1618         return hres;
1619         }
1620     }
1621
1622     return E_PROP_ID_UNSUPPORTED;
1623 }
1624
1625 static HRESULT WINAPI IKsBufferPropertySetImpl_Set(
1626     LPKSPROPERTYSET iface,
1627     REFGUID guidPropSet,
1628     ULONG dwPropID,
1629     LPVOID pInstanceData,
1630     ULONG cbInstanceData,
1631     LPVOID pPropData,
1632     ULONG cbPropData )
1633 {
1634     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1635     PIDSDRIVERPROPERTYSET ps;
1636     TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1637
1638     if (This->dsb->hwbuf) {
1639         IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1640
1641         if (ps) {
1642         DSPROPERTY prop;
1643         HRESULT hres;
1644
1645         prop.s.Set = *guidPropSet;
1646         prop.s.Id = dwPropID;
1647         prop.s.Flags = 0;  /* unused */
1648         prop.s.InstanceId = (ULONG)This->dsb->device;
1649         hres = IDsDriverPropertySet_Set(ps,&prop,pInstanceData,cbInstanceData,pPropData,cbPropData);
1650
1651         IDsDriverPropertySet_Release(ps);
1652
1653         return hres;
1654         }
1655     }
1656
1657     return E_PROP_ID_UNSUPPORTED;
1658 }
1659
1660 static HRESULT WINAPI IKsBufferPropertySetImpl_QuerySupport(
1661     LPKSPROPERTYSET iface,
1662     REFGUID guidPropSet,
1663     ULONG dwPropID,
1664     PULONG pTypeSupport )
1665 {
1666     IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1667     PIDSDRIVERPROPERTYSET ps;
1668     TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1669
1670     if (This->dsb->hwbuf) {
1671         IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1672
1673         if (ps) {
1674             HRESULT hres;
1675
1676             hres = IDsDriverPropertySet_QuerySupport(ps,guidPropSet, dwPropID,pTypeSupport);
1677
1678             IDsDriverPropertySet_Release(ps);
1679
1680             return hres;
1681         }
1682     }
1683
1684     return E_PROP_ID_UNSUPPORTED;
1685 }
1686
1687 static const IKsPropertySetVtbl iksbvt = {
1688     IKsBufferPropertySetImpl_QueryInterface,
1689     IKsBufferPropertySetImpl_AddRef,
1690     IKsBufferPropertySetImpl_Release,
1691     IKsBufferPropertySetImpl_Get,
1692     IKsBufferPropertySetImpl_Set,
1693     IKsBufferPropertySetImpl_QuerySupport
1694 };
1695
1696 HRESULT IKsBufferPropertySetImpl_Create(
1697     IDirectSoundBufferImpl *dsb,
1698     IKsBufferPropertySetImpl **piks)
1699 {
1700     IKsBufferPropertySetImpl *iks;
1701     TRACE("(%p,%p)\n",dsb,piks);
1702     *piks = NULL;
1703
1704     iks = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*iks));
1705     if (iks == 0) {
1706         WARN("out of memory\n");
1707         *piks = NULL;
1708         return DSERR_OUTOFMEMORY;
1709     }
1710
1711     iks->ref = 0;
1712     iks->dsb = dsb;
1713     dsb->iks = iks;
1714     iks->lpVtbl = &iksbvt;
1715
1716     IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
1717
1718     *piks = iks;
1719     return S_OK;
1720 }
1721
1722 HRESULT IKsBufferPropertySetImpl_Destroy(
1723     IKsBufferPropertySetImpl *piks)
1724 {
1725     TRACE("(%p)\n",piks);
1726
1727     while (IKsBufferPropertySetImpl_Release((LPKSPROPERTYSET)piks) > 0);
1728
1729     return S_OK;
1730 }