buffer.c 45.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*  			DirectSound
 *
 * Copyright 1998 Marcus Meissner
 * Copyright 1998 Rob Riggs
 * Copyright 2000-2002 TransGaming Technologies, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 25
#define NONAMELESSSTRUCT
#define NONAMELESSUNION
26 27
#include "windef.h"
#include "winbase.h"
28
#include "winuser.h"
29
#include "mmsystem.h"
30
#include "winternl.h"
31 32 33 34 35 36 37
#include "wine/debug.h"
#include "dsound.h"
#include "dsdriver.h"
#include "dsound_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(dsound);

38 39
static HRESULT SecondaryBufferImpl_Destroy(SecondaryBufferImpl *pdsb);

40 41 42
/*******************************************************************************
 *		IDirectSoundNotify
 */
43 44 45 46 47 48 49 50 51 52 53 54 55

struct IDirectSoundNotifyImpl
{
    /* IUnknown fields */
    const IDirectSoundNotifyVtbl *lpVtbl;
    LONG                        ref;
    IDirectSoundBufferImpl*     dsb;
};

static HRESULT IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl *dsb,
                                             IDirectSoundNotifyImpl **pdsn);
static HRESULT IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl *pdsn);

56 57 58
static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
	LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
) {
59
	IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
60
	TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
61

62
	if (This->dsb == NULL) {
63 64 65 66
		WARN("invalid parameter\n");
		return E_INVALIDARG;
	}

67
	return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
68 69
}

70 71
static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
{
72 73
    IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
    ULONG ref = InterlockedIncrement(&(This->ref));
74
    TRACE("(%p) ref was %d\n", This, ref - 1);
75
    return ref;
76 77
}

78 79 80 81
static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
{
    IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
    ULONG ref = InterlockedDecrement(&(This->ref));
82
    TRACE("(%p) ref was %d\n", This, ref + 1);
83 84 85

    if (!ref) {
        This->dsb->notify = NULL;
86
        IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
87 88 89 90
        HeapFree(GetProcessHeap(), 0, This);
        TRACE("(%p) released\n", This);
    }
    return ref;
91 92 93 94 95
}

static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
	LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
) {
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 102
	    return DSERR_INVALIDPARAM;
	}
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 115 116
	    if (hres != DS_OK)
		    WARN("IDsDriverNotify_SetNotificationPositions failed\n");
	    return hres;
117
        } else if (howmuch > 0) {
118 119
	    /* Make an internal copy of the caller-supplied array.
	     * Replace the existing copy if one is already present. */
120
	    HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
121
	    This->dsb->notifies = HeapAlloc(GetProcessHeap(), 0,
122 123
			howmuch * sizeof(DSBPOSITIONNOTIFY));

124
	    if (This->dsb->notifies == NULL) {
125 126 127
		    WARN("out of memory\n");
		    return DSERR_OUTOFMEMORY;
	    }
128
	    CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
129
	    This->dsb->nrofnotifies = howmuch;
130
        } else {
131 132
           HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
           This->dsb->notifies = NULL;
133 134
           This->dsb->nrofnotifies = 0;
        }
135 136 137 138

	return S_OK;
}

139
static const IDirectSoundNotifyVtbl dsnvt =
140
{
141 142 143 144
    IDirectSoundNotifyImpl_QueryInterface,
    IDirectSoundNotifyImpl_AddRef,
    IDirectSoundNotifyImpl_Release,
    IDirectSoundNotifyImpl_SetNotificationPositions,
145 146
};

147
static HRESULT IDirectSoundNotifyImpl_Create(
148 149 150 151 152
    IDirectSoundBufferImpl * dsb,
    IDirectSoundNotifyImpl **pdsn)
{
    IDirectSoundNotifyImpl * dsn;
    TRACE("(%p,%p)\n",dsb,pdsn);
153

154
    dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dsn));
155

156 157 158 159 160 161 162 163 164 165
    if (dsn == NULL) {
        WARN("out of memory\n");
        return DSERR_OUTOFMEMORY;
    }

    dsn->ref = 0;
    dsn->lpVtbl = &dsnvt;
    dsn->dsb = dsb;
    dsb->notify = dsn;
    IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
166

167 168 169
    *pdsn = dsn;
    return DS_OK;
}
170

171
static HRESULT IDirectSoundNotifyImpl_Destroy(
172 173 174 175 176 177 178 179 180
    IDirectSoundNotifyImpl *pdsn)
{
    TRACE("(%p)\n",pdsn);

    while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);

    return DS_OK;
}

181 182 183 184 185
/*******************************************************************************
 *		IDirectSoundBuffer
 */

static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
186
	LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
187
) {
188
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
189 190 191

	TRACE("(%p,%p)\n",This,wfex);
	/* This method is not available on secondary buffers */
192
	WARN("invalid call\n");
193 194 195 196 197 198
	return DSERR_INVALIDCALL;
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
	LPDIRECTSOUNDBUFFER8 iface,LONG vol
) {
199
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
200
	LONG oldVol;
201
	HRESULT hres = DS_OK;
202

203
	TRACE("(%p,%d)\n",This,vol);
204

205
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
206
		WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
207
		return DSERR_CONTROLUNAVAIL;
208
	}
209

210
	if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
211
		WARN("invalid parameter: vol = %d\n", vol);
212
		return DSERR_INVALIDPARAM;
213
	}
214 215

	/* **** */
216
	RtlAcquireResourceExclusive(&This->lock, TRUE);
217 218

	if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
219 220
		oldVol = This->ds3db_lVolume;
		This->ds3db_lVolume = vol;
221 222 223
		if (vol != oldVol)
			/* recalc 3d volume, which in turn recalcs the pans */
			DSOUND_Calc3DBuffer(This);
224 225 226
	} else {
		oldVol = This->volpan.lVolume;
		This->volpan.lVolume = vol;
227
		if (vol != oldVol)
228
			DSOUND_RecalcVolPan(&(This->volpan));
229 230 231 232
	}

	if (vol != oldVol) {
		if (This->hwbuf) {
233 234 235
			hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
	    		if (hres != DS_OK)
		    		WARN("IDsDriverBuffer_SetVolumePan failed\n");
236
		}
237 238
	}

239
	RtlReleaseResource(&This->lock);
240 241
	/* **** */

242
	return hres;
243 244 245 246 247
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
	LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
) {
248
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
249 250
	TRACE("(%p,%p)\n",This,vol);

251 252 253 254 255
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
		WARN("control unavailable\n");
		return DSERR_CONTROLUNAVAIL;
	}

256 257
	if (vol == NULL) {
		WARN("invalid parameter: vol == NULL\n");
258
		return DSERR_INVALIDPARAM;
259
	}
260

261 262
	*vol = This->volpan.lVolume;

263 264 265 266 267 268
	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
	LPDIRECTSOUNDBUFFER8 iface,DWORD freq
) {
269
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
270 271
	DWORD oldFreq;

272
	TRACE("(%p,%d)\n",This,freq);
273

274 275
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
		WARN("control unavailable\n");
276
		return DSERR_CONTROLUNAVAIL;
277
	}
278 279

	if (freq == DSBFREQUENCY_ORIGINAL)
280
		freq = This->pwfx->nSamplesPerSec;
281

282
	if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
283
		WARN("invalid parameter: freq = %d\n", freq);
284
		return DSERR_INVALIDPARAM;
285
	}
286 287

	/* **** */
288
	RtlAcquireResourceExclusive(&This->lock, TRUE);
289 290 291 292

	oldFreq = This->freq;
	This->freq = freq;
	if (freq != oldFreq) {
293
		This->freqAdjust = ((DWORD64)This->freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
294
		This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
295
		DSOUND_RecalcFormat(This);
296
		DSOUND_MixToTemporary(This, 0, This->buflen, FALSE);
297 298
	}

299
	RtlReleaseResource(&This->lock);
300 301 302 303 304 305 306 307
	/* **** */

	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_Play(
	LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
) {
308
	HRESULT hres = DS_OK;
309
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
310
	TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
311 312

	/* **** */
313
	RtlAcquireResourceExclusive(&This->lock, TRUE);
314 315

	This->playflags = flags;
316
	if (This->state == STATE_STOPPED && !This->hwbuf) {
317 318 319 320 321
		This->leadin = TRUE;
		This->state = STATE_STARTING;
	} else if (This->state == STATE_STOPPING)
		This->state = STATE_PLAYING;
	if (This->hwbuf) {
322 323 324 325 326
		hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
		if (hres != DS_OK)
			WARN("IDsDriverBuffer_Play failed\n");
		else
			This->state = STATE_PLAYING;
327 328
	}

329
	RtlReleaseResource(&This->lock);
330 331
	/* **** */

332
	return hres;
333 334 335 336
}

static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
{
337
	HRESULT hres = DS_OK;
338
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
339 340 341
	TRACE("(%p)\n",This);

	/* **** */
342
	RtlAcquireResourceExclusive(&This->lock, TRUE);
343 344 345 346

	if (This->state == STATE_PLAYING)
		This->state = STATE_STOPPING;
	else if (This->state == STATE_STARTING)
347
	{
348
		This->state = STATE_STOPPED;
349 350
		DSOUND_CheckEvent(This, 0, 0);
	}
351
	if (This->hwbuf) {
352 353 354 355 356
		hres = IDsDriverBuffer_Stop(This->hwbuf);
		if (hres != DS_OK)
			WARN("IDsDriverBuffer_Stop failed\n");
		else
			This->state = STATE_STOPPED;
357 358
	}

359
	RtlReleaseResource(&This->lock);
360 361
	/* **** */

362
	return hres;
363 364
}

365 366
static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
{
367 368
    IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
    ULONG ref = InterlockedIncrement(&(This->ref));
369
    TRACE("(%p) ref was %d\n", This, ref - 1);
370
    return ref;
371
}
372

373
static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
374
{
375 376
    IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
    ULONG ref = InterlockedDecrement(&(This->ref));
377
    TRACE("(%p) ref was %d\n", This, ref + 1);
378

379
    if (!ref) {
380
	DirectSoundDevice_RemoveBuffer(This->device, This);
381
	RtlDeleteResource(&This->lock);
382

383
	if (This->hwbuf)
384
		IDsDriverBuffer_Release(This->hwbuf);
385
	if (!This->hwbuf || (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)) {
386
		This->buffer->ref--;
387
		list_remove(&This->entry);
388 389
		if (This->buffer->ref==0) {
			HeapFree(GetProcessHeap(),0,This->buffer->memory);
390
			HeapFree(GetProcessHeap(),0,This->buffer);
391
		}
392
	}
393

394
	HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
395 396
	HeapFree(GetProcessHeap(), 0, This->notifies);
	HeapFree(GetProcessHeap(), 0, This->pwfx);
397
	HeapFree(GetProcessHeap(), 0, This);
398

399 400 401
	TRACE("(%p) released\n", This);
    }
    return ref;
402 403 404 405 406 407
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
) {
	HRESULT	hres;
408
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
409
	TRACE("(%p,%p,%p)\n",This,playpos,writepos);
410 411

	RtlAcquireResourceShared(&This->lock, TRUE);
412 413
	if (This->hwbuf) {
		hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
414 415
		if (hres != DS_OK) {
		    WARN("IDsDriverBuffer_GetPosition failed\n");
416
		    return hres;
417
		}
418
	} else {
419 420 421 422 423 424 425 426 427 428
		DWORD pos = This->sec_mixpos;

		/* sanity */
		if (pos >= This->buflen){
			FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
			pos %= This->buflen;
		}

		if (playpos)
			*playpos = pos;
429
		if (writepos)
430
			*writepos = pos;
431
	}
432 433 434
	if (writepos && This->state != STATE_STOPPED && (!This->hwbuf || !(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD))) {
		/* apply the documented 10ms lead to writepos */
		*writepos += This->writelead;
435
		*writepos %= This->buflen;
436
	}
437
	RtlReleaseResource(&This->lock);
438 439

	TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
440
		playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
441

442 443 444 445 446 447
	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
) {
448
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
449
	TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
450

451 452
	if (status == NULL) {
		WARN("invalid parameter: status = NULL\n");
453
		return DSERR_INVALIDPARAM;
454
	}
455 456

	*status = 0;
457
	RtlAcquireResourceShared(&This->lock, TRUE);
458 459 460 461 462
	if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
		*status |= DSBSTATUS_PLAYING;
		if (This->playflags & DSBPLAY_LOOPING)
			*status |= DSBSTATUS_LOOPING;
	}
463
	RtlReleaseResource(&This->lock);
464

465
	TRACE("status=%x\n", *status);
466 467 468 469 470
	return DS_OK;
}


static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
471 472 473 474 475 476
    LPDIRECTSOUNDBUFFER8 iface,
    LPWAVEFORMATEX lpwf,
    DWORD wfsize,
    LPDWORD wfwritten)
{
    DWORD size;
477
    IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
478
    TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
479

480
    size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
481

482 483
    if (lpwf) { /* NULL is valid */
        if (wfsize >= size) {
484
            CopyMemory(lpwf,This->pwfx,size);
485 486 487
            if (wfwritten)
                *wfwritten = size;
        } else {
488
            WARN("invalid parameter: wfsize too small\n");
489
            CopyMemory(lpwf,This->pwfx,wfsize);
490
            if (wfwritten)
491
                *wfwritten = wfsize;
492 493 494 495 496 497 498 499 500 501 502 503
            return DSERR_INVALIDPARAM;
        }
    } else {
        if (wfwritten)
            *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
        else {
            WARN("invalid parameter: wfwritten == NULL\n");
            return DSERR_INVALIDPARAM;
        }
    }

    return DS_OK;
504 505 506
}

static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
507
	LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
508
) {
509
	HRESULT hres = DS_OK;
510
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
511

512
	TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
513 514 515 516 517 518 519 520 521 522 523
		This,
		writecursor,
		writebytes,
		lplpaudioptr1,
		audiobytes1,
		lplpaudioptr2,
		audiobytes2,
		flags,
		GetTickCount()
	);

524 525 526
        if (!audiobytes1)
            return DSERR_INVALIDPARAM;

527
        /* when this flag is set, writecursor is meaningless and must be calculated */
528 529
	if (flags & DSBLOCK_FROMWRITECURSOR) {
		/* GetCurrentPosition does too much magic to duplicate here */
530
		hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
531 532 533 534
		if (hres != DS_OK) {
			WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
			return hres;
		}
535
	}
536 537

        /* when this flag is set, writebytes is meaningless and must be set */
538 539
	if (flags & DSBLOCK_ENTIREBUFFER)
		writebytes = This->buflen;
540 541

	if (writecursor >= This->buflen) {
542
		WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
543 544 545 546 547
		     writecursor, This->buflen);
		return DSERR_INVALIDPARAM;
        }

	if (writebytes > This->buflen) {
548
		WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
549 550 551
		     writebytes, This->buflen);
		return DSERR_INVALIDPARAM;
        }
552

553
	/* **** */
554
	RtlAcquireResourceShared(&This->lock, TRUE);
555

Robert Reif's avatar
Robert Reif committed
556
	if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
557
		hres = IDsDriverBuffer_Lock(This->hwbuf,
558 559 560 561
				     lplpaudioptr1, audiobytes1,
				     lplpaudioptr2, audiobytes2,
				     writecursor, writebytes,
				     0);
562 563
		if (hres != DS_OK) {
			WARN("IDsDriverBuffer_Lock failed\n");
564
			RtlReleaseResource(&This->lock);
565 566 567
			return hres;
		}
	} else {
568
		if (writecursor+writebytes <= This->buflen) {
569
			*(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
570 571
			if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
				WARN("Overwriting mixing position, case 1\n");
572 573 574 575 576
			*audiobytes1 = writebytes;
			if (lplpaudioptr2)
				*(LPBYTE*)lplpaudioptr2 = NULL;
			if (audiobytes2)
				*audiobytes2 = 0;
577 578
			TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
			  *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
579
			TRACE("->%d.0\n",writebytes);
580
		} else {
581
			DWORD remainder = writebytes + writecursor - This->buflen;
582
			*(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
583
			*audiobytes1 = This->buflen-writecursor;
584 585
			if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
				WARN("Overwriting mixing position, case 2\n");
586
			if (lplpaudioptr2)
587
				*(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
588 589
			if (audiobytes2)
				*audiobytes2 = writebytes-(This->buflen-writecursor);
590 591
			if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
				WARN("Overwriting mixing position, case 3\n");
592
			TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
593 594
		}
	}
595

596
	RtlReleaseResource(&This->lock);
597
	/* **** */
598

599 600 601 602 603 604
	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
	LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
) {
605
	HRESULT hres = DS_OK;
606
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
607
	DWORD oldpos;
608
	TRACE("(%p,%d)\n",This,newpos);
609 610

	/* **** */
611
	RtlAcquireResourceExclusive(&This->lock, TRUE);
612

613 614
	oldpos = This->sec_mixpos;

615
	/* start mixing from this new location instead */
616
	newpos %= This->buflen;
617
	newpos -= newpos%This->pwfx->nBlockAlign;
618
	This->sec_mixpos = newpos;
619 620 621 622

	/* at this point, do not attempt to reset buffers, mess with primary mix position,
           or anything like that to reduce latancy. The data already prebuffered cannot be changed */

623
	/* position HW buffer if applicable, else just start mixing from new location instead */
624 625 626 627 628
	if (This->hwbuf) {
		hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
		if (hres != DS_OK)
			WARN("IDsDriverBuffer_SetPosition failed\n");
	}
629 630 631
	else if (oldpos != newpos)
		/* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
		This->buf_mixpos = DSOUND_secpos_to_bufpos(This, newpos, 0, NULL);
632

633
	RtlReleaseResource(&This->lock);
634 635
	/* **** */

636
	return hres;
637 638 639 640 641
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
	LPDIRECTSOUNDBUFFER8 iface,LONG pan
) {
642
	HRESULT hres = DS_OK;
643
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
644

645
	TRACE("(%p,%d)\n",This,pan);
646

647
	if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
648
		WARN("invalid parameter: pan = %d\n", pan);
649
		return DSERR_INVALIDPARAM;
650
	}
651 652 653

	/* You cannot use both pan and 3D controls */
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
654 655
	    (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
		WARN("control unavailable\n");
656
		return DSERR_CONTROLUNAVAIL;
657
	}
658 659

	/* **** */
660
	RtlAcquireResourceExclusive(&This->lock, TRUE);
661

662 663
	if (This->volpan.lPan != pan) {
		This->volpan.lPan = pan;
664 665 666
		DSOUND_RecalcVolPan(&(This->volpan));

		if (This->hwbuf) {
667 668 669
			hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
			if (hres != DS_OK)
				WARN("IDsDriverBuffer_SetVolumePan failed\n");
670
		}
671 672
	}

673
	RtlReleaseResource(&This->lock);
674 675
	/* **** */

676
	return hres;
677 678 679 680 681
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
	LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
) {
682
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
683 684
	TRACE("(%p,%p)\n",This,pan);

685 686 687 688 689
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
		WARN("control unavailable\n");
		return DSERR_CONTROLUNAVAIL;
	}

690 691
	if (pan == NULL) {
		WARN("invalid parameter: pan = NULL\n");
692
		return DSERR_INVALIDPARAM;
693
	}
694 695 696 697 698 699 700 701 702

	*pan = This->volpan.lPan;

	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
	LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
) {
703
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface, *iter;
704
	HRESULT hres = DS_OK;
705

706
	TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
707

708
	/* **** */
709
	RtlAcquireResourceShared(&This->lock, TRUE);
710

Robert Reif's avatar
Robert Reif committed
711
	if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
712
		hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
713
		if (hres != DS_OK)
714
			WARN("IDsDriverBuffer_Unlock failed\n");
715 716
	}

717
	RtlReleaseResource(&This->lock);
718 719
	/* **** */

720 721 722
	if (!p2)
		x2 = 0;

723
	if (!This->hwbuf && (x1 || x2))
724 725 726 727 728 729
	{
		RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
		LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
		{
			RtlAcquireResourceShared(&iter->lock, TRUE);
			if (x1)
730 731 732 733 734 735
                        {
			    if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
			      hres = DSERR_INVALIDPARAM;
			    else
			      DSOUND_MixToTemporary(iter, (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory, x1, FALSE);
                        }
736
			if (x2)
737
				DSOUND_MixToTemporary(iter, 0, x2, FALSE);
738 739 740 741 742
			RtlReleaseResource(&iter->lock);
		}
		RtlReleaseResource(&This->device->buffer_list_lock);
	}

743
	return hres;
744 745 746 747 748
}

static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
	LPDIRECTSOUNDBUFFER8 iface
) {
749
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
750 751 752 753 754 755 756
	FIXME("(%p):stub\n",This);
	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
) {
757
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
758 759
	TRACE("(%p,%p)\n",This,freq);

760 761
	if (freq == NULL) {
		WARN("invalid parameter: freq = NULL\n");
762
		return DSERR_INVALIDPARAM;
763
	}
764 765

	*freq = This->freq;
766
	TRACE("-> %d\n", *freq);
767 768 769 770 771 772 773

	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
	LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
) {
774
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
775 776
	DWORD u;

777
	FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
778 779 780 781

	if (pdwResultCodes)
		for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;

782
	WARN("control unavailable\n");
783 784 785 786 787 788
	return DSERR_CONTROLUNAVAIL;
}

static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
	LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
) {
789
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
790 791
	DWORD u;

792
	FIXME("(%p,%08u,%u,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
793 794 795 796

	if (pdwResultCodes)
		for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;

797
	WARN("control unavailable\n");
798 799 800 801 802 803
	return DSERR_CONTROLUNAVAIL;
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
	LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
) {
804
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
805

806
	FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
807

808
	WARN("control unavailable\n");
809 810 811 812
	return DSERR_CONTROLUNAVAIL;
}

static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
813
	LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
814
) {
815
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
816
	WARN("(%p) already initialized\n", This);
817 818 819 820 821 822
	return DSERR_ALREADYINITIALIZED;
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
	LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
) {
823
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
824 825
  	TRACE("(%p)->(%p)\n",This,caps);

826 827
	if (caps == NULL) {
		WARN("invalid parameter: caps == NULL\n");
828
		return DSERR_INVALIDPARAM;
829 830 831
	}

	if (caps->dwSize < sizeof(*caps)) {
832
		WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
833 834
		return DSERR_INVALIDPARAM;
	}
835 836 837 838 839 840 841

	caps->dwFlags = This->dsbd.dwFlags;
	if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
	else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;

	caps->dwBufferBytes = This->buflen;

842 843
	/* According to windows, this is zero*/
	caps->dwUnlockTransferRate = 0;
844 845 846 847 848 849 850 851
	caps->dwPlayCpuOverhead = 0;

	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
	LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
) {
852
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
853 854 855

	TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);

856 857 858 859 860
	if (ppobj == NULL) {
		WARN("invalid parameter\n");
		return E_INVALIDARG;
	}

861 862
	*ppobj = NULL;	/* assume failure */

863 864
	if ( IsEqualGUID(riid, &IID_IUnknown) ||
	     IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
865
	     IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
866 867 868 869 870
		if (!This->secondary)
			SecondaryBufferImpl_Create(This, &(This->secondary));
		if (This->secondary) {
			IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->secondary);
			*ppobj = This->secondary;
871 872 873 874
			return S_OK;
		}
		WARN("IID_IDirectSoundBuffer\n");
		return E_NOINTERFACE;
875 876
	}

877
	if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
878 879
		if (!This->notify)
			IDirectSoundNotifyImpl_Create(This, &(This->notify));
880 881
		if (This->notify) {
			IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
882
			*ppobj = This->notify;
883 884
			return S_OK;
		}
885
		WARN("IID_IDirectSoundNotify\n");
886
		return E_NOINTERFACE;
887 888 889 890
	}

	if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
		if (!This->ds3db)
891 892 893 894
			IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
		if (This->ds3db) {
			IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
			*ppobj = This->ds3db;
895 896
			return S_OK;
		}
897
		WARN("IID_IDirectSound3DBuffer\n");
898
		return E_NOINTERFACE;
899 900
	}

901
	if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
902
		ERR("app requested IDirectSound3DListener on secondary buffer\n");
903
		return E_NOINTERFACE;
904 905 906
	}

	if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
907 908 909 910 911 912
		if (!This->iks)
			IKsBufferPropertySetImpl_Create(This, &(This->iks));
		if (This->iks) {
			IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
	    		*ppobj = This->iks;
			return S_OK;
913
		}
914
		WARN("IID_IKsPropertySet\n");
915
		return E_NOINTERFACE;
916 917
	}

918 919 920 921 922
	FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );

	return E_NOINTERFACE;
}

923
static const IDirectSoundBuffer8Vtbl dsbvt =
924 925 926 927 928 929 930 931 932
{
	IDirectSoundBufferImpl_QueryInterface,
	IDirectSoundBufferImpl_AddRef,
	IDirectSoundBufferImpl_Release,
	IDirectSoundBufferImpl_GetCaps,
	IDirectSoundBufferImpl_GetCurrentPosition,
	IDirectSoundBufferImpl_GetFormat,
	IDirectSoundBufferImpl_GetVolume,
	IDirectSoundBufferImpl_GetPan,
933
	IDirectSoundBufferImpl_GetFrequency,
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
	IDirectSoundBufferImpl_GetStatus,
	IDirectSoundBufferImpl_Initialize,
	IDirectSoundBufferImpl_Lock,
	IDirectSoundBufferImpl_Play,
	IDirectSoundBufferImpl_SetCurrentPosition,
	IDirectSoundBufferImpl_SetFormat,
	IDirectSoundBufferImpl_SetVolume,
	IDirectSoundBufferImpl_SetPan,
	IDirectSoundBufferImpl_SetFrequency,
	IDirectSoundBufferImpl_Stop,
	IDirectSoundBufferImpl_Unlock,
	IDirectSoundBufferImpl_Restore,
	IDirectSoundBufferImpl_SetFX,
	IDirectSoundBufferImpl_AcquireResources,
	IDirectSoundBufferImpl_GetObjectInPath
};

951
HRESULT IDirectSoundBufferImpl_Create(
Robert Reif's avatar
Robert Reif committed
952
	DirectSoundDevice * device,
953
	IDirectSoundBufferImpl **pdsb,
954
	LPCDSBUFFERDESC dsbd)
955 956 957 958 959
{
	IDirectSoundBufferImpl *dsb;
	LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
	HRESULT err = DS_OK;
	DWORD capf = 0;
960
	int use_hw, alloc_size, cp_size;
Robert Reif's avatar
Robert Reif committed
961
	TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
962 963

	if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
964
		WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
965
		*pdsb = NULL;
966 967 968
		return DSERR_INVALIDPARAM; /* FIXME: which error? */
	}

969
	dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
970 971 972 973 974 975

	if (dsb == 0) {
		WARN("out of memory\n");
		*pdsb = NULL;
		return DSERR_OUTOFMEMORY;
	}
976 977 978

	TRACE("Created buffer at %p\n", dsb);

979
	dsb->ref = 0;
980
	dsb->secondary = 0;
Robert Reif's avatar
Robert Reif committed
981
	dsb->device = device;
982
	dsb->lpVtbl = &dsbvt;
983
	dsb->iks = NULL;
984

985
	/* size depends on version */
986
	CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
987

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	/* variable sized struct so calculate size based on format */
	if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
		alloc_size = sizeof(WAVEFORMATEX);
		cp_size = sizeof(PCMWAVEFORMAT);
	} else 
		alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;

	dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,alloc_size);
	if (dsb->pwfx == NULL) {
		WARN("out of memory\n");
		HeapFree(GetProcessHeap(),0,dsb);
		*pdsb = NULL;
		return DSERR_OUTOFMEMORY;
	}

1003
	CopyMemory(dsb->pwfx, wfex, cp_size);
1004

1005 1006 1007 1008 1009 1010
	if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
		dsb->buflen = dsbd->dwBufferBytes + 
			(dsbd->lpwfxFormat->nBlockAlign - 
			(dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
	else
		dsb->buflen = dsbd->dwBufferBytes;
1011

1012
	dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1013 1014 1015 1016 1017
	dsb->notify = NULL;
	dsb->notifies = NULL;
	dsb->nrofnotifies = 0;
	dsb->hwnotify = 0;

1018 1019 1020 1021 1022
	/* Check necessary hardware mixing capabilities */
	if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
	else capf |= DSCAPS_SECONDARYMONO;
	if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
	else capf |= DSCAPS_SECONDARY8BIT;
1023

1024 1025
	use_hw = !!(dsbd->dwFlags & DSBCAPS_LOCHARDWARE);
	TRACE("use_hw = %d, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw, capf, device->drvcaps.dwFlags);
1026
	if (use_hw && ((device->drvcaps.dwFlags & capf) != capf || !device->driver))
1027
	{
1028 1029
		if (device->driver)
			WARN("Format not supported for hardware buffer\n");
1030 1031 1032
		HeapFree(GetProcessHeap(),0,dsb->pwfx);
		HeapFree(GetProcessHeap(),0,dsb);
		*pdsb = NULL;
1033 1034 1035
		if ((device->drvcaps.dwFlags & capf) != capf)
			return DSERR_BADFORMAT;
		return DSERR_GENERIC;
1036
	}
1037 1038 1039 1040 1041 1042

	/* FIXME: check hardware sample rate mixing capabilities */
	/* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
	/* FIXME: check whether any hardware buffers are left */
	/* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */

1043 1044 1045 1046 1047 1048 1049 1050 1051
	/* Allocate an empty buffer */
	dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
	if (dsb->buffer == NULL) {
		WARN("out of memory\n");
		HeapFree(GetProcessHeap(),0,dsb->pwfx);
		HeapFree(GetProcessHeap(),0,dsb);
		*pdsb = NULL;
		return DSERR_OUTOFMEMORY;
	}
1052

1053
	/* Allocate system memory for buffer if applicable */
Robert Reif's avatar
Robert Reif committed
1054
	if ((device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1055
		dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1056 1057
		if (dsb->buffer->memory == NULL) {
			WARN("out of memory\n");
1058
			HeapFree(GetProcessHeap(),0,dsb->pwfx);
1059 1060 1061 1062 1063
			HeapFree(GetProcessHeap(),0,dsb->buffer);
			HeapFree(GetProcessHeap(),0,dsb);
			*pdsb = NULL;
			return DSERR_OUTOFMEMORY;
		}
1064 1065 1066
	}

	/* Allocate the hardware buffer */
1067
	if (use_hw) {
Robert Reif's avatar
Robert Reif committed
1068
		err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1069
						  &(dsb->buflen),&(dsb->buffer->memory),
1070
						  (LPVOID*)&(dsb->hwbuf));
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
		if (FAILED(err))
		{
			WARN("Failed to create hardware secondary buffer: %08x\n", err);
			if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)
				HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
			HeapFree(GetProcessHeap(),0,dsb->buffer);
			HeapFree(GetProcessHeap(),0,dsb->pwfx);
			HeapFree(GetProcessHeap(),0,dsb);
			*pdsb = NULL;
			return DSERR_GENERIC;
1081
		}
1082 1083
	}

1084 1085 1086 1087 1088
	dsb->buffer->ref = 1;
	list_init(&dsb->buffer->buffers);
	list_add_head(&dsb->buffer->buffers, &dsb->entry);
	FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);

1089 1090
	/* It's not necessary to initialize values to zero since */
	/* we allocated this structure with HEAP_ZERO_MEMORY... */
1091
	dsb->buf_mixpos = dsb->sec_mixpos = 0;
1092 1093
	dsb->state = STATE_STOPPED;

1094
	dsb->freqAdjust = ((DWORD64)dsb->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
1095 1096 1097
	dsb->nAvgBytesPerSec = dsb->freq *
		dsbd->lpwfxFormat->nBlockAlign;

1098 1099 1100
	/* calculate fragment size and write lead */
	DSOUND_RecalcFormat(dsb);

1101 1102
	if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
		dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1103 1104 1105 1106 1107 1108
		dsb->ds3db_ds3db.vPosition.x = 0.0;
		dsb->ds3db_ds3db.vPosition.y = 0.0;
		dsb->ds3db_ds3db.vPosition.z = 0.0;
		dsb->ds3db_ds3db.vVelocity.x = 0.0;
		dsb->ds3db_ds3db.vVelocity.y = 0.0;
		dsb->ds3db_ds3db.vVelocity.z = 0.0;
1109 1110
		dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
		dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1111 1112 1113
		dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
		dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
		dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1114 1115 1116 1117 1118 1119 1120
		dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
		dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
		dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
		dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;

		dsb->ds3db_need_recalc = FALSE;
		DSOUND_Calc3DBuffer(dsb);
1121
	} else
1122 1123
		DSOUND_RecalcVolPan(&(dsb->volpan));

1124
	RtlInitializeResource(&dsb->lock);
1125

1126
	/* register buffer if not primary */
1127
	if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1128
		err = DirectSoundDevice_AddBuffer(device, dsb);
1129
		if (err != DS_OK) {
1130 1131
			HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
			HeapFree(GetProcessHeap(),0,dsb->buffer);
1132
			RtlDeleteResource(&dsb->lock);
1133
			HeapFree(GetProcessHeap(),0,dsb->pwfx);
1134
			HeapFree(GetProcessHeap(),0,dsb);
1135
			dsb = NULL;
1136 1137
		}
	}
1138

1139
	*pdsb = dsb;
1140
	return err;
1141
}
1142

1143
HRESULT IDirectSoundBufferImpl_Destroy(
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
    IDirectSoundBufferImpl *pdsb)
{
    TRACE("(%p)\n",pdsb);

    /* This keeps the *_Destroy functions from possibly deleting
     * this object until it is ready to be deleted */
    IDirectSoundBufferImpl_AddRef((LPDIRECTSOUNDBUFFER8)pdsb);

    if (pdsb->iks) {
        WARN("iks not NULL\n");
        IKsBufferPropertySetImpl_Destroy(pdsb->iks);
        pdsb->iks = NULL;
    }

    if (pdsb->ds3db) {
        WARN("ds3db not NULL\n");
        IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
        pdsb->ds3db = NULL;
    }

    if (pdsb->notify) {
        WARN("notify not NULL\n");
        IDirectSoundNotifyImpl_Destroy(pdsb->notify);
        pdsb->notify = NULL;
    }

1170
    if (pdsb->secondary) {
1171
        WARN("dsb not NULL\n");
1172 1173
        SecondaryBufferImpl_Destroy(pdsb->secondary);
        pdsb->secondary = NULL;
1174 1175 1176 1177 1178 1179 1180
    }

    while (IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);

    return S_OK;
}

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
HRESULT IDirectSoundBufferImpl_Duplicate(
    DirectSoundDevice *device,
    IDirectSoundBufferImpl **ppdsb,
    IDirectSoundBufferImpl *pdsb)
{
    IDirectSoundBufferImpl *dsb;
    HRESULT hres = DS_OK;
    int size;
    TRACE("(%p,%p,%p)\n", device, pdsb, pdsb);

    dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));

    if (dsb == NULL) {
        WARN("out of memory\n");
        *ppdsb = NULL;
        return DSERR_OUTOFMEMORY;
    }

    CopyMemory(dsb, pdsb, sizeof(IDirectSoundBufferImpl));

    if (pdsb->hwbuf) {
        TRACE("duplicating hardware buffer\n");

        hres = IDsDriver_DuplicateSoundBuffer(device->driver, pdsb->hwbuf,
                                              (LPVOID *)&dsb->hwbuf);
1206 1207 1208 1209 1210
        if (FAILED(hres)) {
            WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres);
            HeapFree(GetProcessHeap(),0,dsb);
            *ppdsb = NULL;
            return hres;
1211 1212 1213
        }
    }

1214 1215
    dsb->buffer->ref++;
    list_add_head(&dsb->buffer->buffers, &dsb->entry);
1216 1217
    dsb->ref = 0;
    dsb->state = STATE_STOPPED;
1218
    dsb->buf_mixpos = dsb->sec_mixpos = 0;
1219 1220 1221 1222
    dsb->device = device;
    dsb->ds3db = NULL;
    dsb->iks = NULL; /* FIXME? */
    dsb->secondary = NULL;
1223 1224
    dsb->tmp_buffer = NULL;
    DSOUND_RecalcFormat(dsb);
1225
    DSOUND_MixToTemporary(dsb, 0, dsb->buflen, FALSE);
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

    /* variable sized struct so calculate size based on format */
    size = sizeof(WAVEFORMATEX) + pdsb->pwfx->cbSize;

    dsb->pwfx = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
    if (dsb->pwfx == NULL) {
            WARN("out of memory\n");
            HeapFree(GetProcessHeap(),0,dsb->buffer);
            HeapFree(GetProcessHeap(),0,dsb);
            *ppdsb = NULL;
            return DSERR_OUTOFMEMORY;
    }

    CopyMemory(dsb->pwfx, pdsb->pwfx, size);

1241
    RtlInitializeResource(&dsb->lock);
1242 1243 1244 1245

    /* register buffer */
    hres = DirectSoundDevice_AddBuffer(device, dsb);
    if (hres != DS_OK) {
1246
        RtlDeleteResource(&dsb->lock);
1247
        HeapFree(GetProcessHeap(),0,dsb->tmp_buffer);
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
        HeapFree(GetProcessHeap(),0,dsb->buffer);
        HeapFree(GetProcessHeap(),0,dsb->pwfx);
        HeapFree(GetProcessHeap(),0,dsb);
        *ppdsb = 0;
    }

    *ppdsb = dsb;
    return hres;
}

1258 1259 1260 1261 1262
/*******************************************************************************
 *		SecondaryBuffer
 */

static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1263
	LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1264
{
1265
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1266 1267 1268 1269 1270
	TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);

	return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
}

1271
static ULONG WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1272
{
1273
    SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1274
    ULONG ref = InterlockedIncrement(&(This->ref));
1275
    TRACE("(%p) ref was %d\n", This, ref - 1);
1276
    return ref;
1277 1278
}

1279
static ULONG WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1280
{
1281
    SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
Robert Reif's avatar
Robert Reif committed
1282 1283 1284
    ULONG ref;
    TRACE("(%p)\n", This);
    ref = InterlockedDecrement(&(This->ref));
1285
    TRACE("ref was %d\n", ref + 1);
1286 1287

    if (!ref) {
1288
        This->dsb->secondary = NULL;
1289 1290 1291 1292 1293
        IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
        HeapFree(GetProcessHeap(), 0, This);
        TRACE("(%p) released\n", This);
    }
    return ref;
1294 1295 1296 1297 1298
}

static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
	LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
{
1299
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1300 1301 1302 1303 1304 1305 1306 1307
  	TRACE("(%p)->(%p)\n",This,caps);

	return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
}

static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
{
1308
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1309 1310 1311 1312 1313 1314 1315 1316
	TRACE("(%p,%p,%p)\n",This,playpos,writepos);

	return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
}

static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
	LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
{
1317
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1318
	TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1319 1320 1321 1322 1323 1324 1325

	return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
}

static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
	LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
{
1326
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1327 1328 1329 1330 1331 1332 1333 1334
	TRACE("(%p,%p)\n",This,vol);

	return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
}

static HRESULT WINAPI SecondaryBufferImpl_GetPan(
	LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
{
1335
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1336 1337 1338 1339 1340 1341 1342 1343
	TRACE("(%p,%p)\n",This,pan);

	return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
}

static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
{
1344
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1345 1346 1347 1348 1349 1350 1351 1352
	TRACE("(%p,%p)\n",This,freq);

	return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
}

static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
{
1353
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1354 1355 1356 1357 1358 1359
	TRACE("(%p,%p)\n",This,status);

	return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
}

static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1360
	LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd)
1361
{
1362
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1363 1364 1365 1366 1367 1368
	TRACE("(%p,%p,%p)\n",This,dsound,dbsd);

	return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
}

static HRESULT WINAPI SecondaryBufferImpl_Lock(
1369 1370 1371
    LPDIRECTSOUNDBUFFER8 iface,
    DWORD writecursor,
    DWORD writebytes,
1372
    LPVOID *lplpaudioptr1,
1373
    LPDWORD audiobytes1,
1374
    LPVOID *lplpaudioptr2,
1375 1376
    LPDWORD audiobytes2,
    DWORD dwFlags)
1377
{
1378
    SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1379
    TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x)\n",
1380
        This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1381

1382 1383
    return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,
        writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,dwFlags);
1384 1385 1386 1387 1388
}

static HRESULT WINAPI SecondaryBufferImpl_Play(
	LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
{
1389
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1390
	TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
1391 1392 1393 1394 1395 1396 1397

	return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
}

static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
	LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
{
1398
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1399
	TRACE("(%p,%d)\n",This,newpos);
1400 1401 1402 1403 1404

	return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
}

static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1405
	LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex)
1406
{
1407
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1408 1409 1410 1411 1412 1413 1414 1415
	TRACE("(%p,%p)\n",This,wfex);

	return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
}

static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
	LPDIRECTSOUNDBUFFER8 iface,LONG vol)
{
1416
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1417
	TRACE("(%p,%d)\n",This,vol);
1418 1419 1420 1421 1422 1423 1424

	return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
}

static HRESULT WINAPI SecondaryBufferImpl_SetPan(
	LPDIRECTSOUNDBUFFER8 iface,LONG pan)
{
1425
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1426
	TRACE("(%p,%d)\n",This,pan);
1427 1428 1429 1430 1431 1432 1433

	return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
}

static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
	LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
{
1434
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1435
	TRACE("(%p,%d)\n",This,freq);
1436 1437 1438 1439 1440 1441

	return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
}

static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
{
1442
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1443 1444 1445 1446 1447 1448
	TRACE("(%p)\n",This);

	return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
}

static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1449 1450 1451 1452 1453
    LPDIRECTSOUNDBUFFER8 iface,
    LPVOID lpvAudioPtr1,
    DWORD dwAudioBytes1,
    LPVOID lpvAudioPtr2,
    DWORD dwAudioBytes2)
1454
{
1455
    SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1456
    TRACE("(%p,%p,%d,%p,%d)\n",
1457
        This, lpvAudioPtr1, dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1458

1459 1460
    return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,
        lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,dwAudioBytes2);
1461 1462 1463 1464 1465
}

static HRESULT WINAPI SecondaryBufferImpl_Restore(
	LPDIRECTSOUNDBUFFER8 iface)
{
1466
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1467 1468 1469 1470 1471 1472 1473 1474
	TRACE("(%p)\n",This);

	return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
}

static HRESULT WINAPI SecondaryBufferImpl_SetFX(
	LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
{
1475
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1476
	TRACE("(%p,%u,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1477 1478 1479 1480 1481 1482 1483

	return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
}

static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
	LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
{
1484
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1485
	TRACE("(%p,%08u,%u,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1486 1487 1488 1489 1490 1491 1492

	return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
}

static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
	LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
{
1493
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1494
	TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1495 1496 1497 1498

	return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
}

1499
static const IDirectSoundBuffer8Vtbl sbvt =
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
{
	SecondaryBufferImpl_QueryInterface,
	SecondaryBufferImpl_AddRef,
	SecondaryBufferImpl_Release,
	SecondaryBufferImpl_GetCaps,
	SecondaryBufferImpl_GetCurrentPosition,
	SecondaryBufferImpl_GetFormat,
	SecondaryBufferImpl_GetVolume,
	SecondaryBufferImpl_GetPan,
	SecondaryBufferImpl_GetFrequency,
	SecondaryBufferImpl_GetStatus,
	SecondaryBufferImpl_Initialize,
	SecondaryBufferImpl_Lock,
	SecondaryBufferImpl_Play,
	SecondaryBufferImpl_SetCurrentPosition,
	SecondaryBufferImpl_SetFormat,
	SecondaryBufferImpl_SetVolume,
	SecondaryBufferImpl_SetPan,
	SecondaryBufferImpl_SetFrequency,
	SecondaryBufferImpl_Stop,
	SecondaryBufferImpl_Unlock,
	SecondaryBufferImpl_Restore,
	SecondaryBufferImpl_SetFX,
	SecondaryBufferImpl_AcquireResources,
	SecondaryBufferImpl_GetObjectInPath
};

1527
HRESULT SecondaryBufferImpl_Create(
1528 1529 1530 1531 1532 1533
	IDirectSoundBufferImpl *dsb,
	SecondaryBufferImpl **psb)
{
	SecondaryBufferImpl *sb;
	TRACE("(%p,%p)\n",dsb,psb);

1534
	sb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

	if (sb == 0) {
		WARN("out of memory\n");
		*psb = NULL;
		return DSERR_OUTOFMEMORY;
	}
	sb->ref = 0;
	sb->dsb = dsb;
	sb->lpVtbl = &sbvt;

	IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
	*psb = sb;
	return S_OK;
}
1549

1550
static HRESULT SecondaryBufferImpl_Destroy(
1551 1552 1553 1554 1555 1556 1557 1558
    SecondaryBufferImpl *pdsb)
{
    TRACE("(%p)\n",pdsb);

    while (SecondaryBufferImpl_Release((LPDIRECTSOUNDBUFFER8)pdsb) > 0);

    return S_OK;
}