buffer.c 45 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 86 87 88 89 90

    if (!ref) {
        IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
        This->dsb->notify = NULL;
        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 121
	    if (This->dsb->notifies)
		    This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
122 123
			This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
	    else
124
		    This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
125 126
			howmuch * sizeof(DSBPOSITIONNOTIFY));

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

	return S_OK;
}

142
static const IDirectSoundNotifyVtbl dsnvt =
143
{
144 145 146 147
    IDirectSoundNotifyImpl_QueryInterface,
    IDirectSoundNotifyImpl_AddRef,
    IDirectSoundNotifyImpl_Release,
    IDirectSoundNotifyImpl_SetNotificationPositions,
148 149
};

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

157
    dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
158

159 160 161 162 163 164 165 166 167 168
    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);
169

170 171 172
    *pdsn = dsn;
    return DS_OK;
}
173

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

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

    return DS_OK;
}

184 185 186 187 188
/*******************************************************************************
 *		IDirectSoundBuffer
 */

static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
189
	LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
190
) {
191
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
192 193 194

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

static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
	LPDIRECTSOUNDBUFFER8 iface,LONG vol
) {
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
	RtlAcquireResourceExclusive(&This->lock, TRUE);
220 221

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

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

242
	RtlReleaseResource(&This->lock);
243 244
	/* **** */

245
	return hres;
246 247 248 249 250
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
	LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
) {
251
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
252 253
	TRACE("(%p,%p)\n",This,vol);

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

259 260
	if (vol == NULL) {
		WARN("invalid parameter: vol == NULL\n");
261
		return DSERR_INVALIDPARAM;
262
	}
263

264 265
	*vol = This->volpan.lVolume;

266 267 268 269 270 271
	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
	LPDIRECTSOUNDBUFFER8 iface,DWORD freq
) {
272
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
273 274
	DWORD oldFreq;

275
	TRACE("(%p,%d)\n",This,freq);
276

277 278
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
		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
	RtlAcquireResourceExclusive(&This->lock, TRUE);
292 293 294 295

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

302
	RtlReleaseResource(&This->lock);
303 304 305 306 307 308 309 310
	/* **** */

	return DS_OK;
}

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

	/* **** */
316
	RtlAcquireResourceExclusive(&This->lock, TRUE);
317 318

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

332
	RtlReleaseResource(&This->lock);
333 334
	/* **** */

335
	return hres;
336 337 338 339
}

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

	/* **** */
345
	RtlAcquireResourceExclusive(&This->lock, TRUE);
346 347 348 349

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

362
	RtlReleaseResource(&This->lock);
363 364
	/* **** */

365
	return hres;
366 367
}

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

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

382
    if (!ref) {
383
	DirectSoundDevice_RemoveBuffer(This->device, This);
384
	RtlDeleteResource(&This->lock);
385

386 387
	if (This->hwbuf) {
		IDsDriverBuffer_Release(This->hwbuf);
Robert Reif's avatar
Robert Reif committed
388
		if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
389
			This->buffer->ref--;
390
			list_remove(&This->entry);
391 392 393 394 395 396 397
			if (This->buffer->ref==0) {
				HeapFree(GetProcessHeap(),0,This->buffer->memory);
				HeapFree(GetProcessHeap(),0,This->buffer);
			}
		}
	} else {
		This->buffer->ref--;
398
		list_remove(&This->entry);
399 400
		if (This->buffer->ref==0) {
			HeapFree(GetProcessHeap(),0,This->buffer->memory);
401
			HeapFree(GetProcessHeap(),0,This->buffer);
402
		}
403
	}
404

405
	HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
406 407
	HeapFree(GetProcessHeap(), 0, This->notifies);
	HeapFree(GetProcessHeap(), 0, This->pwfx);
408
	HeapFree(GetProcessHeap(), 0, This);
409

410 411 412
	TRACE("(%p) released\n", This);
    }
    return ref;
413 414 415 416 417 418
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
) {
	HRESULT	hres;
419
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
420
	TRACE("(%p,%p,%p)\n",This,playpos,writepos);
421 422

	RtlAcquireResourceShared(&This->lock, TRUE);
423 424
	if (This->hwbuf) {
		hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
425 426
		if (hres != DS_OK) {
		    WARN("IDsDriverBuffer_GetPosition failed\n");
427
		    return hres;
428
		}
429
	} else {
430 431 432 433 434 435 436 437 438 439
		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;
440
		if (writepos)
441
			*writepos = pos;
442
	}
443 444 445
	if (writepos && This->state != STATE_STOPPED && (!This->hwbuf || !(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD))) {
		/* apply the documented 10ms lead to writepos */
		*writepos += This->writelead;
446
		*writepos %= This->buflen;
447
	}
448
	RtlReleaseResource(&This->lock);
449 450

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

453 454 455 456 457 458
	return DS_OK;
}

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

462 463
	if (status == NULL) {
		WARN("invalid parameter: status = NULL\n");
464
		return DSERR_INVALIDPARAM;
465
	}
466 467 468 469 470 471 472 473

	*status = 0;
	if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
		*status |= DSBSTATUS_PLAYING;
		if (This->playflags & DSBPLAY_LOOPING)
			*status |= DSBSTATUS_LOOPING;
	}

474
	TRACE("status=%x\n", *status);
475 476 477 478 479
	return DS_OK;
}


static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
480 481 482 483 484 485
    LPDIRECTSOUNDBUFFER8 iface,
    LPWAVEFORMATEX lpwf,
    DWORD wfsize,
    LPDWORD wfwritten)
{
    DWORD size;
486
    IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
487
    TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
488

489
    size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
490

491 492
    if (lpwf) { /* NULL is valid */
        if (wfsize >= size) {
493
            CopyMemory(lpwf,This->pwfx,size);
494 495 496
            if (wfwritten)
                *wfwritten = size;
        } else {
497
            WARN("invalid parameter: wfsize too small\n");
498
            CopyMemory(lpwf,This->pwfx,wfsize);
499
            if (wfwritten)
500
                *wfwritten = wfsize;
501 502 503 504 505 506 507 508 509 510 511 512
            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;
513 514 515
}

static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
516
	LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
517
) {
518
	HRESULT hres = DS_OK;
519
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
520

521
	TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
522 523 524 525 526 527 528 529 530 531 532
		This,
		writecursor,
		writebytes,
		lplpaudioptr1,
		audiobytes1,
		lplpaudioptr2,
		audiobytes2,
		flags,
		GetTickCount()
	);

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

        /* when this flag is set, writebytes is meaningless and must be set */
544 545
	if (flags & DSBLOCK_ENTIREBUFFER)
		writebytes = This->buflen;
546 547

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

	if (writebytes > This->buflen) {
554
		WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
555 556 557
		     writebytes, This->buflen);
		return DSERR_INVALIDPARAM;
        }
558

559
	/* **** */
560
	RtlAcquireResourceShared(&This->lock, TRUE);
561

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

602
	RtlReleaseResource(&This->lock);
603
	/* **** */
604

605 606 607 608 609 610
	return DS_OK;
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
	LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
) {
611
	HRESULT hres = DS_OK;
612
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
613
	DWORD oldpos;
614
	TRACE("(%p,%d)\n",This,newpos);
615 616

	/* **** */
617
	RtlAcquireResourceExclusive(&This->lock, TRUE);
618

619 620
	oldpos = This->sec_mixpos;

621
	/* start mixing from this new location instead */
622
	newpos %= This->buflen;
623
	newpos -= newpos%This->pwfx->nBlockAlign;
624
	This->sec_mixpos = newpos;
625 626 627 628

	/* 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 */

629
	/* position HW buffer if applicable, else just start mixing from new location instead */
630 631 632 633 634
	if (This->hwbuf) {
		hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
		if (hres != DS_OK)
			WARN("IDsDriverBuffer_SetPosition failed\n");
	}
635 636 637
	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);
638

639
	RtlReleaseResource(&This->lock);
640 641
	/* **** */

642
	return hres;
643 644 645 646 647
}

static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
	LPDIRECTSOUNDBUFFER8 iface,LONG pan
) {
648
	HRESULT hres = DS_OK;
649
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
650

651
	TRACE("(%p,%d)\n",This,pan);
652

653
	if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
654
		WARN("invalid parameter: pan = %d\n", pan);
655
		return DSERR_INVALIDPARAM;
656
	}
657 658 659

	/* You cannot use both pan and 3D controls */
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
660 661
	    (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
		WARN("control unavailable\n");
662
		return DSERR_CONTROLUNAVAIL;
663
	}
664 665

	/* **** */
666
	RtlAcquireResourceExclusive(&This->lock, TRUE);
667

668 669
	if (This->volpan.lPan != pan) {
		This->volpan.lPan = pan;
670 671 672
		DSOUND_RecalcVolPan(&(This->volpan));

		if (This->hwbuf) {
673 674 675
			hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
			if (hres != DS_OK)
				WARN("IDsDriverBuffer_SetVolumePan failed\n");
676
		}
677 678
	}

679
	RtlReleaseResource(&This->lock);
680 681
	/* **** */

682
	return hres;
683 684 685 686 687
}

static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
	LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
) {
688
	IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
689 690
	TRACE("(%p,%p)\n",This,pan);

691 692 693 694 695
	if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
		WARN("control unavailable\n");
		return DSERR_CONTROLUNAVAIL;
	}

696 697
	if (pan == NULL) {
		WARN("invalid parameter: pan = NULL\n");
698
		return DSERR_INVALIDPARAM;
699
	}
700 701 702 703 704 705 706 707 708

	*pan = This->volpan.lPan;

	return DS_OK;
}

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

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

714
	/* **** */
715
	RtlAcquireResourceShared(&This->lock, TRUE);
716

Robert Reif's avatar
Robert Reif committed
717
	if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
718
		hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
719
		if (hres != DS_OK)
720
			WARN("IDsDriverBuffer_Unlock failed\n");
721 722
	}

723
	RtlReleaseResource(&This->lock);
724 725
	/* **** */

726 727 728
	if (!p2)
		x2 = 0;

729
	if (!This->hwbuf && (x1 || x2))
730 731 732 733 734 735 736 737 738 739 740 741 742 743
	{
		RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
		LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
		{
			RtlAcquireResourceShared(&iter->lock, TRUE);
			if (x1)
				DSOUND_MixToTemporary(iter, (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory, x1);
			if (x2)
				DSOUND_MixToTemporary(iter, 0, x2);
			RtlReleaseResource(&iter->lock);
		}
		RtlReleaseResource(&This->device->buffer_list_lock);
	}

744
	return hres;
745 746 747 748 749
}

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

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

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

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

	return DS_OK;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	caps->dwBufferBytes = This->buflen;

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

	return DS_OK;
}

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

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

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

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

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

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

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

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

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

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

	return E_NOINTERFACE;
}

924
static const IDirectSoundBuffer8Vtbl dsbvt =
925 926 927 928 929 930 931 932 933
{
	IDirectSoundBufferImpl_QueryInterface,
	IDirectSoundBufferImpl_AddRef,
	IDirectSoundBufferImpl_Release,
	IDirectSoundBufferImpl_GetCaps,
	IDirectSoundBufferImpl_GetCurrentPosition,
	IDirectSoundBufferImpl_GetFormat,
	IDirectSoundBufferImpl_GetVolume,
	IDirectSoundBufferImpl_GetPan,
934
	IDirectSoundBufferImpl_GetFrequency,
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
	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
};

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

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

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

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

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

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

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

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	/* 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;
	}

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

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

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

1019 1020 1021 1022 1023
	/* 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;
1024

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

	/* 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 */

1044 1045 1046 1047 1048 1049 1050 1051 1052
	/* 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;
	}
1053

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

	/* Allocate the hardware buffer */
1068
	if (use_hw) {
Robert Reif's avatar
Robert Reif committed
1069
		err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1070
						  &(dsb->buflen),&(dsb->buffer->memory),
1071
						  (LPVOID*)&(dsb->hwbuf));
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
		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;
1082
		}
1083 1084
	}

1085 1086 1087 1088 1089
	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);

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

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

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

1102 1103
	if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
		dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1104 1105 1106 1107 1108 1109
		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;
1110 1111
		dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
		dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1112 1113 1114
		dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
		dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
		dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1115 1116 1117 1118 1119 1120 1121
		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);
1122
	} else
1123 1124
		DSOUND_RecalcVolPan(&(dsb->volpan));

1125
	RtlInitializeResource(&dsb->lock);
1126

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

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

1144
HRESULT IDirectSoundBufferImpl_Destroy(
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 1170
    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;
    }

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

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

    return S_OK;
}

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
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);
1207 1208 1209 1210 1211
        if (FAILED(hres)) {
            WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres);
            HeapFree(GetProcessHeap(),0,dsb);
            *ppdsb = NULL;
            return hres;
1212 1213 1214
        }
    }

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

    /* 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);

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

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

    *ppdsb = dsb;
    return hres;
}

1259 1260 1261 1262 1263
/*******************************************************************************
 *		SecondaryBuffer
 */

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

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

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

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

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

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

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

static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
	LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
{
1309
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1310 1311 1312 1313 1314 1315 1316 1317
	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)
{
1318
	SecondaryBufferImpl *This = (SecondaryBufferImpl *)iface;
1319
	TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
1320 1321 1322 1323 1324 1325 1326

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1500
static const IDirectSoundBuffer8Vtbl sbvt =
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 1527
{
	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
};

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

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

	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;
}
1550

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

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

    return S_OK;
}