dsound: Tune some parameters for alsa waveout.
[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 "wine/debug.h"
32 #include "dsound.h"
33 #include "dsdriver.h"
34 #include "dsound_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
37
38 static HRESULT SecondaryBufferImpl_Destroy(SecondaryBufferImpl *pdsb);
39
40 /*******************************************************************************
41  *              IDirectSoundNotify
42  */
43
44 struct IDirectSoundNotifyImpl
45 {
46     /* IUnknown fields */
47     const IDirectSoundNotifyVtbl *lpVtbl;
48     LONG                        ref;
49     IDirectSoundBufferImpl*     dsb;
50 };
51
52 static HRESULT IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl *dsb,
53                                              IDirectSoundNotifyImpl **pdsn);
54 static HRESULT IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl *pdsn);
55
56 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
57         LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
58 ) {
59         IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
60         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
61
62         if (This->dsb == NULL) {
63                 WARN("invalid parameter\n");
64                 return E_INVALIDARG;
65         }
66
67         return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
68 }
69
70 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
71 {
72     IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
73     ULONG ref = InterlockedIncrement(&(This->ref));
74     TRACE("(%p) ref was %d\n", This, ref - 1);
75     return ref;
76 }
77
78 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
79 {
80     IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
81     ULONG ref = InterlockedDecrement(&(This->ref));
82     TRACE("(%p) ref was %d\n", This, ref + 1);
83
84     if (!ref) {
85         IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
86         This->dsb->notify = NULL;
87         HeapFree(GetProcessHeap(), 0, This);
88         TRACE("(%p) released\n", This);
89     }
90     return ref;
91 }
92
93 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
94         LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
95 ) {
96         IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
97         TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
98
99         if (howmuch > 0 && notify == NULL) {
100             WARN("invalid parameter: notify == NULL\n");
101             return DSERR_INVALIDPARAM;
102         }
103
104         if (TRACE_ON(dsound)) {
105             unsigned int        i;
106             for (i=0;i<howmuch;i++)
107                 TRACE("notify at %d to %p\n",
108                     notify[i].dwOffset,notify[i].hEventNotify);
109         }
110
111         if (This->dsb->hwnotify) {
112             HRESULT hres;
113             hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
114             if (hres != DS_OK)
115                     WARN("IDsDriverNotify_SetNotificationPositions failed\n");
116             return hres;
117         } else if (howmuch > 0) {
118             /* Make an internal copy of the caller-supplied array.
119              * Replace the existing copy if one is already present. */
120             if (This->dsb->notifies)
121                     This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
122                         This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
123             else
124                     This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
125                         howmuch * sizeof(DSBPOSITIONNOTIFY));
126
127             if (This->dsb->notifies == NULL) {
128                     WARN("out of memory\n");
129                     return DSERR_OUTOFMEMORY;
130             }
131             CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
132             This->dsb->nrofnotifies = howmuch;
133         } else {
134            HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
135            This->dsb->notifies = NULL;
136            This->dsb->nrofnotifies = 0;
137         }
138
139         return S_OK;
140 }
141
142 static const IDirectSoundNotifyVtbl dsnvt =
143 {
144     IDirectSoundNotifyImpl_QueryInterface,
145     IDirectSoundNotifyImpl_AddRef,
146     IDirectSoundNotifyImpl_Release,
147     IDirectSoundNotifyImpl_SetNotificationPositions,
148 };
149
150 static HRESULT IDirectSoundNotifyImpl_Create(
151     IDirectSoundBufferImpl * dsb,
152     IDirectSoundNotifyImpl **pdsn)
153 {
154     IDirectSoundNotifyImpl * dsn;
155     TRACE("(%p,%p)\n",dsb,pdsn);
156
157     dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
158
159     if (dsn == NULL) {
160         WARN("out of memory\n");
161         return DSERR_OUTOFMEMORY;
162     }
163
164     dsn->ref = 0;
165     dsn->lpVtbl = &dsnvt;
166     dsn->dsb = dsb;
167     dsb->notify = dsn;
168     IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
169
170     *pdsn = dsn;
171     return DS_OK;
172 }
173
174 static HRESULT IDirectSoundNotifyImpl_Destroy(
175     IDirectSoundNotifyImpl *pdsn)
176 {
177     TRACE("(%p)\n",pdsn);
178
179     while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
180
181     return DS_OK;
182 }
183
184 /*******************************************************************************
185  *              IDirectSoundBuffer
186  */
187
188 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
189         LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
190 ) {
191         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
192
193         TRACE("(%p,%p)\n",This,wfex);
194         /* This method is not available on secondary buffers */
195         WARN("invalid call\n");
196         return DSERR_INVALIDCALL;
197 }
198
199 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
200         LPDIRECTSOUNDBUFFER8 iface,LONG vol
201 ) {
202         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
203         LONG oldVol;
204         HRESULT hres = DS_OK;
205
206         TRACE("(%p,%d)\n",This,vol);
207
208         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
209                 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
210                 return DSERR_CONTROLUNAVAIL;
211         }
212
213         if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
214                 WARN("invalid parameter: vol = %d\n", vol);
215                 return DSERR_INVALIDPARAM;
216         }
217
218         /* **** */
219         EnterCriticalSection(&(This->lock));
220
221         if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
222                 oldVol = This->ds3db_lVolume;
223                 This->ds3db_lVolume = vol;
224                 if (vol != oldVol)
225                         /* recalc 3d volume, which in turn recalcs the pans */
226                         DSOUND_Calc3DBuffer(This);
227         } else {
228                 oldVol = This->volpan.lVolume;
229                 This->volpan.lVolume = vol;
230                 if (vol != oldVol)
231                         DSOUND_RecalcVolPan(&(This->volpan));
232         }
233
234         if (vol != oldVol) {
235                 if (This->hwbuf) {
236                         hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
237                         if (hres != DS_OK)
238                                 WARN("IDsDriverBuffer_SetVolumePan failed\n");
239                 }
240         }
241
242         LeaveCriticalSection(&(This->lock));
243         /* **** */
244
245         return hres;
246 }
247
248 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
249         LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
250 ) {
251         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
252         TRACE("(%p,%p)\n",This,vol);
253
254         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
255                 WARN("control unavailable\n");
256                 return DSERR_CONTROLUNAVAIL;
257         }
258
259         if (vol == NULL) {
260                 WARN("invalid parameter: vol == NULL\n");
261                 return DSERR_INVALIDPARAM;
262         }
263
264         *vol = This->volpan.lVolume;
265
266         return DS_OK;
267 }
268
269 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
270         LPDIRECTSOUNDBUFFER8 iface,DWORD freq
271 ) {
272         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
273         DWORD oldFreq;
274
275         TRACE("(%p,%d)\n",This,freq);
276
277         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
278                 WARN("control unavailable\n");
279                 return DSERR_CONTROLUNAVAIL;
280         }
281
282         if (freq == DSBFREQUENCY_ORIGINAL)
283                 freq = This->pwfx->nSamplesPerSec;
284
285         if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
286                 WARN("invalid parameter: freq = %d\n", freq);
287                 return DSERR_INVALIDPARAM;
288         }
289
290         /* **** */
291         EnterCriticalSection(&(This->lock));
292
293         oldFreq = This->freq;
294         This->freq = freq;
295         if (freq != oldFreq) {
296                 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
297                 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
298                 DSOUND_RecalcFormat(This);
299         }
300
301         LeaveCriticalSection(&(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         EnterCriticalSection(&(This->lock));
316
317         This->playflags = flags;
318         if (This->state == STATE_STOPPED) {
319                 This->leadin = TRUE;
320                 This->startpos = This->buf_mixpos;
321                 This->state = STATE_STARTING;
322         } else if (This->state == STATE_STOPPING)
323                 This->state = STATE_PLAYING;
324         if (This->hwbuf) {
325                 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
326                 if (hres != DS_OK)
327                         WARN("IDsDriverBuffer_Play failed\n");
328                 else
329                         This->state = STATE_PLAYING;
330         }
331
332         LeaveCriticalSection(&(This->lock));
333         /* **** */
334
335         return hres;
336 }
337
338 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
339 {
340         HRESULT hres = DS_OK;
341         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
342         TRACE("(%p)\n",This);
343
344         /* **** */
345         EnterCriticalSection(&(This->lock));
346
347         if (This->state == STATE_PLAYING)
348                 This->state = STATE_STOPPING;
349         else if (This->state == STATE_STARTING)
350                 This->state = STATE_STOPPED;
351         if (This->hwbuf) {
352                 hres = IDsDriverBuffer_Stop(This->hwbuf);
353                 if (hres != DS_OK)
354                         WARN("IDsDriverBuffer_Stop failed\n");
355                 else
356                         This->state = STATE_STOPPED;
357         }
358         DSOUND_CheckEvent(This, 0);
359
360         LeaveCriticalSection(&(This->lock));
361         /* **** */
362
363         return hres;
364 }
365
366 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
367 {
368     IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
369     ULONG ref = InterlockedIncrement(&(This->ref));
370     TRACE("(%p) ref was %d\n", This, ref - 1);
371     return ref;
372 }
373
374 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
375 {
376     IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
377     ULONG ref = InterlockedDecrement(&(This->ref));
378     TRACE("(%p) ref was %d\n", This, ref + 1);
379
380     if (!ref) {
381         DirectSoundDevice_RemoveBuffer(This->device, This);
382
383         This->lock.DebugInfo->Spare[0] = 0;
384         DeleteCriticalSection(&(This->lock));
385
386         if (This->hwbuf) {
387                 IDsDriverBuffer_Release(This->hwbuf);
388                 if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
389                         This->buffer->ref--;
390                         if (This->buffer->ref==0) {
391                                 HeapFree(GetProcessHeap(),0,This->buffer->memory);
392                                 HeapFree(GetProcessHeap(),0,This->buffer);
393                         }
394                 }
395         } else {
396                 This->buffer->ref--;
397                 if (This->buffer->ref==0) {
398                         HeapFree(GetProcessHeap(),0,This->buffer->memory);
399                         HeapFree(GetProcessHeap(),0,This->buffer);
400                 }
401         }
402
403         HeapFree(GetProcessHeap(), 0, This->notifies);
404         HeapFree(GetProcessHeap(), 0, This->pwfx);
405         HeapFree(GetProcessHeap(), 0, This);
406
407         TRACE("(%p) released\n", This);
408     }
409     return ref;
410 }
411
412 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This, DWORD pplay, DWORD pwrite)
413 {
414         DWORD bplay = This->buf_mixpos;
415         DWORD pmix = This->primary_mixpos;
416         DirectSoundDevice * device = This->device;
417         TRACE("(%p, pplay=%u, pwrite=%u)\n", This, pplay, pwrite);
418
419         /* the actual primary play position (pplay) is always behind last mixed (pmix),
420          * unless the computer is too slow or something */
421         /* we need to know how far away we are from there */
422         if (pmix < pplay) pmix += device->buflen; /* wraparound */
423         pmix -= pplay;
424
425         /* detect buffer underrun (sanity) */
426         if (pwrite < pplay) pwrite += device->buflen; /* wraparound */
427         pwrite -= pplay;
428         if (pmix > (ds_snd_queue_max * device->fraglen + pwrite + device->writelead)) {
429                 ERR("detected an underrun: primary queue was %d\n",pmix);
430                 pmix = 0;
431         }
432
433         TRACE("primary back-samples=%d\n",pmix);
434
435         /* divide the offset by its sample size */
436         pmix /= device->pwfx->nBlockAlign;
437
438         /* adjust for our frequency */
439         pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
440
441         /* multiply by our own sample size */
442         pmix *= This->pwfx->nBlockAlign;
443
444         TRACE("this back-offset=%d\n", pmix);
445
446         /* sanity */
447         if(pmix > This->buflen){
448                 ERR("Bad length in CalcPlayPosition!\n");
449                 return 0;
450         }
451
452         /* subtract from our last mixed position */
453         if (bplay < pmix) bplay += This->buflen; /* wraparound */
454         bplay -= pmix;
455
456         /* check for lead-in */
457         if (This->leadin && ((bplay < This->startpos) || (bplay > This->buf_mixpos))) {
458                 /* seems we haven't started playing yet */
459                 TRACE("this still in lead-in phase\n");
460                 bplay = This->startpos;
461         }
462
463         /* sanity */
464         if(bplay > This->buflen){
465                 ERR("Bad play position in CalcPlayPosition!\n");
466                 return 0;
467         }
468
469         /* return the result */
470         return bplay;
471 }
472
473 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
474         LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
475 ) {
476         HRESULT hres;
477         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
478         TRACE("(%p,%p,%p)\n",This,playpos,writepos);
479         if (This->hwbuf) {
480                 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
481                 if (hres != DS_OK) {
482                     WARN("IDsDriverBuffer_GetPosition failed\n");
483                     return hres;
484                 }
485         } else {
486                 if (playpos && (This->state != STATE_PLAYING)) {
487                         /* we haven't been merged into the primary buffer (yet) */
488                         *playpos = This->buf_mixpos;
489                 } else if (playpos) {
490                         DWORD pplay, pwrite;
491
492                         /* get primary lock, before messing with primary/device data */
493                         EnterCriticalSection(&(This->device->mixlock));
494
495                         /* let's get this exact; first, recursively call GetPosition on the primary */
496                         if (DSOUND_PrimaryGetPosition(This->device, &pplay, &pwrite) != DS_OK)
497                                 WARN("DSOUND_PrimaryGetPosition failed\n");
498                         if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->device->hwbuf) {
499                                 /* calculate play position using this */
500                                 *playpos = DSOUND_CalcPlayPosition(This, pplay, pwrite);
501                         } else {
502                                 /* (unless the app isn't using GETCURRENTPOSITION2) */
503                                 /* don't know exactly how this should be handled...
504                                  * the docs says that play cursor is reported as directly
505                                  * behind write cursor, hmm... */
506                                 /* let's just do what might work for Half-Life */
507                                 DWORD wp;
508                                 wp = (This->device->pwplay + ds_hel_margin) * This->device->fraglen;
509                                 wp %= This->device->buflen;
510                                 *playpos = DSOUND_CalcPlayPosition(This, wp, pwrite);
511                                 TRACE("Using non-GETCURRENTPOSITION2\n");
512                         }
513
514                         LeaveCriticalSection(&(This->device->mixlock));
515                 }
516                 if (writepos)
517                     *writepos = This->buf_mixpos;
518         }
519         if (writepos) {
520                 if (This->state != STATE_STOPPED) {
521                         /* apply the documented 10ms lead to writepos */
522                         *writepos += This->writelead;
523                 }
524                 *writepos %= This->buflen;
525         }
526         if (playpos)
527             This->last_playpos = *playpos;
528
529         TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
530                 playpos?*playpos:0, writepos?*writepos:0, This->buflen, This, GetTickCount());
531
532         return DS_OK;
533 }
534
535 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
536         LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
537 ) {
538         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
539         TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
540
541         if (status == NULL) {
542                 WARN("invalid parameter: status = NULL\n");
543                 return DSERR_INVALIDPARAM;
544         }
545
546         *status = 0;
547         if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
548                 *status |= DSBSTATUS_PLAYING;
549                 if (This->playflags & DSBPLAY_LOOPING)
550                         *status |= DSBSTATUS_LOOPING;
551         }
552
553         TRACE("status=%x\n", *status);
554         return DS_OK;
555 }
556
557
558 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
559     LPDIRECTSOUNDBUFFER8 iface,
560     LPWAVEFORMATEX lpwf,
561     DWORD wfsize,
562     LPDWORD wfwritten)
563 {
564     DWORD size;
565     IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
566     TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
567
568     size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
569
570     if (lpwf) { /* NULL is valid */
571         if (wfsize >= size) {
572             CopyMemory(lpwf,This->pwfx,size);
573             if (wfwritten)
574                 *wfwritten = size;
575         } else {
576             WARN("invalid parameter: wfsize too small\n");
577             if (wfwritten)
578                 *wfwritten = 0;
579             return DSERR_INVALIDPARAM;
580         }
581     } else {
582         if (wfwritten)
583             *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
584         else {
585             WARN("invalid parameter: wfwritten == NULL\n");
586             return DSERR_INVALIDPARAM;
587         }
588     }
589
590     return DS_OK;
591 }
592
593 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
594         LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
595 ) {
596         HRESULT hres = DS_OK;
597         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
598
599         TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
600                 This,
601                 writecursor,
602                 writebytes,
603                 lplpaudioptr1,
604                 audiobytes1,
605                 lplpaudioptr2,
606                 audiobytes2,
607                 flags,
608                 GetTickCount()
609         );
610
611         /* when this flag is set, writecursor is meaningless and must be calculated */
612         if (flags & DSBLOCK_FROMWRITECURSOR) {
613                 /* GetCurrentPosition does too much magic to duplicate here */
614                 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
615                 if (hres != DS_OK) {
616                         WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
617                         return hres;
618                 }
619         }
620
621         /* when this flag is set, writebytes is meaningless and must be set */
622         if (flags & DSBLOCK_ENTIREBUFFER)
623                 writebytes = This->buflen;
624
625         if (writecursor >= This->buflen) {
626                 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
627                      writecursor, This->buflen);
628                 return DSERR_INVALIDPARAM;
629         }
630
631         if (writebytes > This->buflen) {
632                 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
633                      writebytes, This->buflen);
634                 return DSERR_INVALIDPARAM;
635         }
636
637         /* **** */
638         EnterCriticalSection(&(This->lock));
639
640         if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
641                 hres = IDsDriverBuffer_Lock(This->hwbuf,
642                                      lplpaudioptr1, audiobytes1,
643                                      lplpaudioptr2, audiobytes2,
644                                      writecursor, writebytes,
645                                      0);
646                 if (hres != DS_OK) {
647                         WARN("IDsDriverBuffer_Lock failed\n");
648                         LeaveCriticalSection(&(This->lock));
649                         return hres;
650                 }
651         } else {
652                 if (writecursor+writebytes <= This->buflen) {
653                         *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
654                         *audiobytes1 = writebytes;
655                         if (lplpaudioptr2)
656                                 *(LPBYTE*)lplpaudioptr2 = NULL;
657                         if (audiobytes2)
658                                 *audiobytes2 = 0;
659                         TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
660                           *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
661                         TRACE("->%d.0\n",writebytes);
662                 } else {
663                         *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
664                         *audiobytes1 = This->buflen-writecursor;
665                         if (lplpaudioptr2)
666                                 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
667                         if (audiobytes2)
668                                 *audiobytes2 = writebytes-(This->buflen-writecursor);
669                         TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
670                 }
671         }
672
673         LeaveCriticalSection(&(This->lock));
674         /* **** */
675
676         return DS_OK;
677 }
678
679 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
680         LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
681 ) {
682         HRESULT hres = DS_OK;
683         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
684         TRACE("(%p,%d)\n",This,newpos);
685
686         /* **** */
687         EnterCriticalSection(&(This->lock));
688
689         /* start mixing from this new location instead */
690         newpos %= This->buflen;
691         newpos -= newpos%This->pwfx->nBlockAlign;
692         This->buf_mixpos = newpos;
693
694         /* at this point, do not attempt to reset buffers, mess with primary mix position,
695            or anything like that to reduce latancy. The data already prebuffered cannot be changed */
696
697         /* position HW buffer if applicable */
698         if (This->hwbuf) {
699                 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
700                 if (hres != DS_OK)
701                         WARN("IDsDriverBuffer_SetPosition failed\n");
702         }
703
704         LeaveCriticalSection(&(This->lock));
705         /* **** */
706
707         return hres;
708 }
709
710 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
711         LPDIRECTSOUNDBUFFER8 iface,LONG pan
712 ) {
713         HRESULT hres = DS_OK;
714         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
715
716         TRACE("(%p,%d)\n",This,pan);
717
718         if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
719                 WARN("invalid parameter: pan = %d\n", pan);
720                 return DSERR_INVALIDPARAM;
721         }
722
723         /* You cannot use both pan and 3D controls */
724         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
725             (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
726                 WARN("control unavailable\n");
727                 return DSERR_CONTROLUNAVAIL;
728         }
729
730         /* **** */
731         EnterCriticalSection(&(This->lock));
732
733         if (This->volpan.lPan != pan) {
734                 This->volpan.lPan = pan;
735                 DSOUND_RecalcVolPan(&(This->volpan));
736
737                 if (This->hwbuf) {
738                         hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
739                         if (hres != DS_OK)
740                                 WARN("IDsDriverBuffer_SetVolumePan failed\n");
741                 }
742         }
743
744         LeaveCriticalSection(&(This->lock));
745         /* **** */
746
747         return hres;
748 }
749
750 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
751         LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
752 ) {
753         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
754         TRACE("(%p,%p)\n",This,pan);
755
756         if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
757                 WARN("control unavailable\n");
758                 return DSERR_CONTROLUNAVAIL;
759         }
760
761         if (pan == NULL) {
762                 WARN("invalid parameter: pan = NULL\n");
763                 return DSERR_INVALIDPARAM;
764         }
765
766         *pan = This->volpan.lPan;
767
768         return DS_OK;
769 }
770
771 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
772         LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
773 ) {
774         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
775         HRESULT hres = DS_OK;
776
777         TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
778
779         /* **** */
780         EnterCriticalSection(&(This->lock));
781
782         if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
783                 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
784                 if (hres != DS_OK)
785                         WARN("IDsDriverBuffer_Unlock failed\n");
786         }
787
788         LeaveCriticalSection(&(This->lock));
789         /* **** */
790
791         return hres;
792 }
793
794 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
795         LPDIRECTSOUNDBUFFER8 iface
796 ) {
797         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
798         FIXME("(%p):stub\n",This);
799         return DS_OK;
800 }
801
802 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
803         LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
804 ) {
805         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
806         TRACE("(%p,%p)\n",This,freq);
807
808         if (freq == NULL) {
809                 WARN("invalid parameter: freq = NULL\n");
810                 return DSERR_INVALIDPARAM;
811         }
812
813         *freq = This->freq;
814         TRACE("-> %d\n", *freq);
815
816         return DS_OK;
817 }
818
819 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
820         LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
821 ) {
822         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
823         DWORD u;
824
825         FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
826
827         if (pdwResultCodes)
828                 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
829
830         WARN("control unavailable\n");
831         return DSERR_CONTROLUNAVAIL;
832 }
833
834 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
835         LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
836 ) {
837         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
838         DWORD u;
839
840         FIXME("(%p,%08u,%u,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
841
842         if (pdwResultCodes)
843                 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
844
845         WARN("control unavailable\n");
846         return DSERR_CONTROLUNAVAIL;
847 }
848
849 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
850         LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
851 ) {
852         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
853
854         FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
855
856         WARN("control unavailable\n");
857         return DSERR_CONTROLUNAVAIL;
858 }
859
860 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
861         LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
862 ) {
863         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
864         FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
865         DPRINTF("Re-Init!!!\n");
866         WARN("already initialized\n");
867         return DSERR_ALREADYINITIALIZED;
868 }
869
870 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
871         LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
872 ) {
873         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
874         TRACE("(%p)->(%p)\n",This,caps);
875
876         if (caps == NULL) {
877                 WARN("invalid parameter: caps == NULL\n");
878                 return DSERR_INVALIDPARAM;
879         }
880
881         if (caps->dwSize < sizeof(*caps)) {
882                 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
883                 return DSERR_INVALIDPARAM;
884         }
885
886         caps->dwFlags = This->dsbd.dwFlags;
887         if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
888         else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
889
890         caps->dwBufferBytes = This->buflen;
891
892         /* According to windows, this is zero*/
893         caps->dwUnlockTransferRate = 0;
894         caps->dwPlayCpuOverhead = 0;
895
896         return DS_OK;
897 }
898
899 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
900         LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
901 ) {
902         IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
903
904         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
905
906         if (ppobj == NULL) {
907                 WARN("invalid parameter\n");
908                 return E_INVALIDARG;
909         }
910
911         *ppobj = NULL;  /* assume failure */
912
913         if ( IsEqualGUID(riid, &IID_IUnknown) ||
914              IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
915              IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
916                 if (!This->secondary)
917                         SecondaryBufferImpl_Create(This, &(This->secondary));
918                 if (This->secondary) {
919                         IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->secondary);
920                         *ppobj = This->secondary;
921                         return S_OK;
922                 }
923                 WARN("IID_IDirectSoundBuffer\n");
924                 return E_NOINTERFACE;
925         }
926
927         if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
928                 if (!This->notify)
929                         IDirectSoundNotifyImpl_Create(This, &(This->notify));
930                 if (This->notify) {
931                         IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
932                         *ppobj = This->notify;
933                         return S_OK;
934                 }
935                 WARN("IID_IDirectSoundNotify\n");
936                 return E_NOINTERFACE;
937         }
938
939         if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
940                 if (!This->ds3db)
941                         IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
942                 if (This->ds3db) {
943                         IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
944                         *ppobj = This->ds3db;
945                         return S_OK;
946                 }
947                 WARN("IID_IDirectSound3DBuffer\n");
948                 return E_NOINTERFACE;
949         }
950
951         if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
952                 ERR("app requested IDirectSound3DListener on secondary buffer\n");
953                 return E_NOINTERFACE;
954         }
955
956         if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
957                 if (!This->iks)
958                         IKsBufferPropertySetImpl_Create(This, &(This->iks));
959                 if (This->iks) {
960                         IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
961                         *ppobj = This->iks;
962                         return S_OK;
963                 }
964                 WARN("IID_IKsPropertySet\n");
965                 return E_NOINTERFACE;
966         }
967
968         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
969
970         return E_NOINTERFACE;
971 }
972
973 static const IDirectSoundBuffer8Vtbl dsbvt =
974 {
975         IDirectSoundBufferImpl_QueryInterface,
976         IDirectSoundBufferImpl_AddRef,
977         IDirectSoundBufferImpl_Release,
978         IDirectSoundBufferImpl_GetCaps,
979         IDirectSoundBufferImpl_GetCurrentPosition,
980         IDirectSoundBufferImpl_GetFormat,
981         IDirectSoundBufferImpl_GetVolume,
982         IDirectSoundBufferImpl_GetPan,
983         IDirectSoundBufferImpl_GetFrequency,
984         IDirectSoundBufferImpl_GetStatus,
985         IDirectSoundBufferImpl_Initialize,
986         IDirectSoundBufferImpl_Lock,
987         IDirectSoundBufferImpl_Play,
988         IDirectSoundBufferImpl_SetCurrentPosition,
989         IDirectSoundBufferImpl_SetFormat,
990         IDirectSoundBufferImpl_SetVolume,
991         IDirectSoundBufferImpl_SetPan,
992         IDirectSoundBufferImpl_SetFrequency,
993         IDirectSoundBufferImpl_Stop,
994         IDirectSoundBufferImpl_Unlock,
995         IDirectSoundBufferImpl_Restore,
996         IDirectSoundBufferImpl_SetFX,
997         IDirectSoundBufferImpl_AcquireResources,
998         IDirectSoundBufferImpl_GetObjectInPath
999 };
1000
1001 HRESULT IDirectSoundBufferImpl_Create(
1002         DirectSoundDevice * device,
1003         IDirectSoundBufferImpl **pdsb,
1004         LPCDSBUFFERDESC dsbd)
1005 {
1006         IDirectSoundBufferImpl *dsb;
1007         LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1008         HRESULT err = DS_OK;
1009         DWORD capf = 0;
1010         int use_hw, alloc_size, cp_size;
1011         TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
1012
1013         if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1014                 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
1015                 *pdsb = NULL;
1016                 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1017         }
1018
1019         dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1020
1021         if (dsb == 0) {
1022                 WARN("out of memory\n");
1023                 *pdsb = NULL;
1024                 return DSERR_OUTOFMEMORY;
1025         }
1026
1027         TRACE("Created buffer at %p\n", dsb);
1028
1029         dsb->ref = 0;
1030         dsb->secondary = 0;
1031         dsb->device = device;
1032         dsb->lpVtbl = &dsbvt;
1033         dsb->iks = NULL;
1034
1035         dsb->remix_pos = 0;
1036
1037         /* size depends on version */
1038         CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
1039
1040         /* variable sized struct so calculate size based on format */
1041         if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1042                 alloc_size = sizeof(WAVEFORMATEX);
1043                 cp_size = sizeof(PCMWAVEFORMAT);
1044         } else 
1045                 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
1046
1047         dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
1048         if (dsb->pwfx == NULL) {
1049                 WARN("out of memory\n");
1050                 HeapFree(GetProcessHeap(),0,dsb);
1051                 *pdsb = NULL;
1052                 return DSERR_OUTOFMEMORY;
1053         }
1054
1055         CopyMemory(dsb->pwfx, wfex, cp_size);
1056
1057         if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1058                 dsb->buflen = dsbd->dwBufferBytes + 
1059                         (dsbd->lpwfxFormat->nBlockAlign - 
1060                         (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1061         else
1062                 dsb->buflen = dsbd->dwBufferBytes;
1063
1064         dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1065         dsb->notify = NULL;
1066         dsb->notifies = NULL;
1067         dsb->nrofnotifies = 0;
1068         dsb->hwnotify = 0;
1069
1070         /* Check necessary hardware mixing capabilities */
1071         if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1072         else capf |= DSCAPS_SECONDARYMONO;
1073         if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1074         else capf |= DSCAPS_SECONDARY8BIT;
1075
1076         use_hw = (device->drvcaps.dwFlags & capf) == capf;
1077         TRACE("use_hw = 0x%08x, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw, capf, device->drvcaps.dwFlags);
1078
1079         /* FIXME: check hardware sample rate mixing capabilities */
1080         /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1081         /* FIXME: check whether any hardware buffers are left */
1082         /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1083
1084         /* Allocate an empty buffer */
1085         dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1086         if (dsb->buffer == NULL) {
1087                 WARN("out of memory\n");
1088                 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1089                 HeapFree(GetProcessHeap(),0,dsb);
1090                 *pdsb = NULL;
1091                 return DSERR_OUTOFMEMORY;
1092         }
1093
1094         /* Allocate system memory for buffer if applicable */
1095         if ((device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1096                 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1097                 if (dsb->buffer->memory == NULL) {
1098                         WARN("out of memory\n");
1099                         HeapFree(GetProcessHeap(),0,dsb->pwfx);
1100                         HeapFree(GetProcessHeap(),0,dsb->buffer);
1101                         HeapFree(GetProcessHeap(),0,dsb);
1102                         *pdsb = NULL;
1103                         return DSERR_OUTOFMEMORY;
1104                 }
1105                 dsb->buffer->ref = 1;
1106                 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1107         }
1108
1109         /* Allocate the hardware buffer */
1110         if (use_hw) {
1111                 err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1112                                                   &(dsb->buflen),&(dsb->buffer->memory),
1113                                                   (LPVOID*)&(dsb->hwbuf));
1114                 /* fall back to software buffer on failure */
1115                 if (err != DS_OK) {
1116                         TRACE("IDsDriver_CreateSoundBuffer failed, falling back to software buffer\n");
1117                         use_hw = 0;
1118                         if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1119                                 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1120                                 if (dsb->buffer->memory == NULL) {
1121                                         WARN("out of memory\n");
1122                                         HeapFree(GetProcessHeap(),0,dsb->buffer);
1123                                         HeapFree(GetProcessHeap(),0,dsb->pwfx);
1124                                         HeapFree(GetProcessHeap(),0,dsb);
1125                                         *pdsb = NULL;
1126                                         return DSERR_OUTOFMEMORY;
1127                                 }
1128                                 dsb->buffer->ref = 1;
1129                                 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1130                         }
1131                         err = DS_OK;
1132                 }
1133         }
1134
1135         /* calculate fragment size and write lead */
1136         DSOUND_RecalcFormat(dsb);
1137
1138         /* It's not necessary to initialize values to zero since */
1139         /* we allocated this structure with HEAP_ZERO_MEMORY... */
1140         dsb->playpos = 0;
1141         dsb->buf_mixpos = 0;
1142         dsb->state = STATE_STOPPED;
1143
1144         dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
1145                 device->pwfx->nSamplesPerSec;
1146         dsb->nAvgBytesPerSec = dsb->freq *
1147                 dsbd->lpwfxFormat->nBlockAlign;
1148
1149         if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1150                 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1151                 dsb->ds3db_ds3db.vPosition.x = 0.0;
1152                 dsb->ds3db_ds3db.vPosition.y = 0.0;
1153                 dsb->ds3db_ds3db.vPosition.z = 0.0;
1154                 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1155                 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1156                 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1157                 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1158                 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1159                 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1160                 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1161                 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1162                 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1163                 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1164                 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1165                 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1166
1167                 dsb->ds3db_need_recalc = FALSE;
1168                 DSOUND_Calc3DBuffer(dsb);
1169         } else
1170                 DSOUND_RecalcVolPan(&(dsb->volpan));
1171
1172         InitializeCriticalSection(&(dsb->lock));
1173         dsb->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectSoundBufferImpl.lock");
1174
1175         /* register buffer if not primary */
1176         if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1177                 err = DirectSoundDevice_AddBuffer(device, dsb);
1178                 if (err != DS_OK) {
1179                         HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1180                         HeapFree(GetProcessHeap(),0,dsb->buffer);
1181                         dsb->lock.DebugInfo->Spare[0] = 0;
1182                         DeleteCriticalSection(&(dsb->lock));
1183                         HeapFree(GetProcessHeap(),0,dsb->pwfx);
1184                         HeapFree(GetProcessHeap(),0,dsb);
1185                         dsb = NULL;
1186                 }
1187         }
1188
1189         *pdsb = dsb;
1190         return err;
1191 }
1192
1193 HRESULT IDirectSoundBufferImpl_Destroy(
1194     IDirectSoundBufferImpl *pdsb)
1195 {
1196     TRACE("(%p)\n",pdsb);
1197
1198     /* This keeps the *_Destroy functions from possibly deleting
1199      * this object until it is ready to be deleted */
1200     IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);
1201
1202     if (pdsb->iks) {
1203         WARN("iks not NULL\n");
1204         IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1205         pdsb->iks = NULL;
1206     }
1207
1208     if (pdsb->ds3db) {
1209         WARN("ds3db not NULL\n");
1210         IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1211         pdsb->ds3db = NULL;
1212     }
1213
1214     if (pdsb->notify) {
1215         WARN("notify not NULL\n");
1216         IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1217         pdsb->notify = NULL;
1218     }
1219
1220     if (pdsb->secondary) {
1221         WARN("dsb not NULL\n");
1222         SecondaryBufferImpl_Destroy(pdsb->secondary);
1223         pdsb->secondary = NULL;
1224     }
1225
1226     while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1227
1228     return S_OK;
1229 }
1230
1231 HRESULT IDirectSoundBufferImpl_Duplicate(
1232     DirectSoundDevice *device,
1233     IDirectSoundBufferImpl **ppdsb,
1234     IDirectSoundBufferImpl *pdsb)
1235 {
1236     IDirectSoundBufferImpl *dsb;
1237     HRESULT hres = DS_OK;
1238     int size;
1239     TRACE("(%p,%p,%p)\n", device, pdsb, pdsb);
1240
1241     dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1242
1243     if (dsb == NULL) {
1244         WARN("out of memory\n");
1245         *ppdsb = NULL;
1246         return DSERR_OUTOFMEMORY;
1247     }
1248
1249     CopyMemory(dsb, pdsb, sizeof(IDirectSoundBufferImpl));
1250
1251     if (pdsb->hwbuf) {
1252         TRACE("duplicating hardware buffer\n");
1253
1254         hres = IDsDriver_DuplicateSoundBuffer(device->driver, pdsb->hwbuf,
1255                                               (LPVOID *)&dsb->hwbuf);
1256         if (hres != DS_OK) {
1257             TRACE("IDsDriver_DuplicateSoundBuffer failed, falling back to "
1258                   "software buffer\n");
1259             dsb->hwbuf = NULL;
1260             /* allocate buffer */
1261             if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1262                 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1263                 if (dsb->buffer == NULL) {
1264                     WARN("out of memory\n");
1265                     HeapFree(GetProcessHeap(),0,dsb);
1266                     *ppdsb = NULL;
1267                     return DSERR_OUTOFMEMORY;
1268                 }
1269
1270                 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1271                 if (dsb->buffer->memory == NULL) {
1272                     WARN("out of memory\n");
1273                     HeapFree(GetProcessHeap(),0,dsb->buffer);
1274                     HeapFree(GetProcessHeap(),0,dsb);
1275                     *ppdsb = NULL;
1276                     return DSERR_OUTOFMEMORY;
1277                 }
1278                 dsb->buffer->ref = 1;
1279
1280                 /* FIXME: copy buffer ? */
1281             }
1282         }
1283     } else {
1284         dsb->hwbuf = NULL;
1285         dsb->buffer->ref++;
1286     }
1287
1288     dsb->ref = 0;
1289     dsb->state = STATE_STOPPED;
1290     dsb->playpos = 0;
1291     dsb->buf_mixpos = 0;
1292     dsb->device = device;
1293     dsb->ds3db = NULL;
1294     dsb->iks = NULL; /* FIXME? */
1295     dsb->secondary = NULL;
1296
1297     /* variable sized struct so calculate size based on format */
1298     size = sizeof(WAVEFORMATEX) + pdsb->pwfx->cbSize;
1299
1300     dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
1301     if (dsb->pwfx == NULL) {
1302             WARN("out of memory\n");
1303             HeapFree(GetProcessHeap(),0,dsb->buffer);
1304             HeapFree(GetProcessHeap(),0,dsb);
1305             *ppdsb = NULL;
1306             return DSERR_OUTOFMEMORY;
1307     }
1308
1309     CopyMemory(dsb->pwfx, pdsb->pwfx, size);
1310
1311     InitializeCriticalSection(&(dsb->lock));
1312     dsb->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectSoundBufferImpl.lock");
1313
1314     /* register buffer */
1315     hres = DirectSoundDevice_AddBuffer(device, dsb);
1316     if (hres != DS_OK) {
1317         dsb->lock.DebugInfo->Spare[0] = 0;
1318         DeleteCriticalSection(&(dsb->lock));
1319         HeapFree(GetProcessHeap(),0,dsb->buffer);
1320         HeapFree(GetProcessHeap(),0,dsb->pwfx);
1321         HeapFree(GetProcessHeap(),0,dsb);
1322         *ppdsb = 0;
1323     }
1324
1325     *ppdsb = dsb;
1326     return hres;
1327 }
1328
1329 /*******************************************************************************
1330  *              SecondaryBuffer
1331  */
1332
1333 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1334         LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1335 {
1336         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1337         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1338
1339         return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1340 }
1341
1342 static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1343 {
1344     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1345     ULONG ref = InterlockedIncrement(&(This->ref));
1346     TRACE("(%p) ref was %d\n", This, ref - 1);
1347     return ref;
1348 }
1349
1350 static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1351 {
1352     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1353     ULONG ref;
1354     TRACE("(%p)\n", This);
1355     ref = InterlockedDecrement(&(This->ref));
1356     TRACE("ref was %d\n", ref + 1);
1357
1358     if (!ref) {
1359         This->dsb->secondary = NULL;
1360         IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1361         HeapFree(GetProcessHeap(), 0, This);
1362         TRACE("(%p) released\n", This);
1363     }
1364     return ref;
1365 }
1366
1367 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1368         LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1369 {
1370         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1371         TRACE("(%p)->(%p)\n",This,caps);
1372
1373         return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1374 }
1375
1376 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1377         LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1378 {
1379         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1380         TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1381
1382         return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1383 }
1384
1385 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1386         LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1387 {
1388         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1389         TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1390
1391         return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1392 }
1393
1394 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1395         LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1396 {
1397         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1398         TRACE("(%p,%p)\n",This,vol);
1399
1400         return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1401 }
1402
1403 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1404         LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1405 {
1406         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1407         TRACE("(%p,%p)\n",This,pan);
1408
1409         return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1410 }
1411
1412 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1413         LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1414 {
1415         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1416         TRACE("(%p,%p)\n",This,freq);
1417
1418         return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1419 }
1420
1421 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1422         LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1423 {
1424         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1425         TRACE("(%p,%p)\n",This,status);
1426
1427         return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1428 }
1429
1430 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1431         LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1432 {
1433         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1434         TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1435
1436         return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1437 }
1438
1439 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1440     LPDIRECTSOUNDBUFFER8 iface,
1441     DWORD writecursor,
1442     DWORD writebytes,
1443     LPVOID *lplpaudioptr1,
1444     LPDWORD audiobytes1,
1445     LPVOID *lplpaudioptr2,
1446     LPDWORD audiobytes2,
1447     DWORD dwFlags)
1448 {
1449     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1450     TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1451         This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1452
1453     return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
1454         writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1455 }
1456
1457 static HRESULT WINAPI SecondaryBufferImpl_Play(
1458         LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1459 {
1460         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1461         TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1462
1463         return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1464 }
1465
1466 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1467         LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1468 {
1469         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1470         TRACE("(%p,%d)\n",This,newpos);
1471
1472         return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1473 }
1474
1475 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1476         LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1477 {
1478         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1479         TRACE("(%p,%p)\n",This,wfex);
1480
1481         return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1482 }
1483
1484 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1485         LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1486 {
1487         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1488         TRACE("(%p,%d)\n",This,vol);
1489
1490         return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1491 }
1492
1493 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1494         LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1495 {
1496         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1497         TRACE("(%p,%d)\n",This,pan);
1498
1499         return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1500 }
1501
1502 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1503         LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1504 {
1505         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1506         TRACE("(%p,%d)\n",This,freq);
1507
1508         return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1509 }
1510
1511 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1512 {
1513         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1514         TRACE("(%p)\n",This);
1515
1516         return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1517 }
1518
1519 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1520     LPDIRECTSOUNDBUFFER8 iface,
1521     LPVOID lpvAudioPtr1,
1522     DWORD dwAudioBytes1,
1523     LPVOID lpvAudioPtr2,
1524     DWORD dwAudioBytes2)
1525 {
1526     SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1527     TRACE("(%p,%p,%d,%p,%d)\n",
1528         This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1529
1530     return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
1531         lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1532 }
1533
1534 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1535         LPDIRECTSOUNDBUFFER8 iface)
1536 {
1537         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1538         TRACE("(%p)\n",This);
1539
1540         return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1541 }
1542
1543 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1544         LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1545 {
1546         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1547         TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1548
1549         return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1550 }
1551
1552 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1553         LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1554 {
1555         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1556         TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1557
1558         return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1559 }
1560
1561 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1562         LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1563 {
1564         SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1565         TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1566
1567         return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1568 }
1569
1570 static const IDirectSoundBuffer8Vtbl sbvt =
1571 {
1572         SecondaryBufferImpl_QueryInterface,
1573         SecondaryBufferImpl_AddRef,
1574         SecondaryBufferImpl_Release,
1575         SecondaryBufferImpl_GetCaps,
1576         SecondaryBufferImpl_GetCurrentPosition,
1577         SecondaryBufferImpl_GetFormat,
1578         SecondaryBufferImpl_GetVolume,
1579         SecondaryBufferImpl_GetPan,
1580         SecondaryBufferImpl_GetFrequency,
1581         SecondaryBufferImpl_GetStatus,
1582         SecondaryBufferImpl_Initialize,
1583         SecondaryBufferImpl_Lock,
1584         SecondaryBufferImpl_Play,
1585         SecondaryBufferImpl_SetCurrentPosition,
1586         SecondaryBufferImpl_SetFormat,
1587         SecondaryBufferImpl_SetVolume,
1588         SecondaryBufferImpl_SetPan,
1589         SecondaryBufferImpl_SetFrequency,
1590         SecondaryBufferImpl_Stop,
1591         SecondaryBufferImpl_Unlock,
1592         SecondaryBufferImpl_Restore,
1593         SecondaryBufferImpl_SetFX,
1594         SecondaryBufferImpl_AcquireResources,
1595         SecondaryBufferImpl_GetObjectInPath
1596 };
1597
1598 HRESULT SecondaryBufferImpl_Create(
1599         IDirectSoundBufferImpl *dsb,
1600         SecondaryBufferImpl **psb)
1601 {
1602         SecondaryBufferImpl *sb;
1603         TRACE("(%p,%p)\n",dsb,psb);
1604
1605         sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1606
1607         if (sb == 0) {
1608                 WARN("out of memory\n");
1609                 *psb = NULL;
1610                 return DSERR_OUTOFMEMORY;
1611         }
1612         sb->ref = 0;
1613         sb->dsb = dsb;
1614         sb->lpVtbl = &sbvt;
1615
1616         IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1617         *psb = sb;
1618         return S_OK;
1619 }
1620
1621 static HRESULT SecondaryBufferImpl_Destroy(
1622     SecondaryBufferImpl *pdsb)
1623 {
1624     TRACE("(%p)\n",pdsb);
1625
1626     while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);
1627
1628     return S_OK;
1629 }