dsound: Don't delete the primary buffer if a sub iface is still in use.
[wine/testsucceed.git] / dlls / dsound / primary.c
blob2e0a6a41e484fe022ed3d10fad889727ff14f84d
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23 * it leaves dsound in unusable (not really open) state.
26 #include <stdarg.h>
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "mmsystem.h"
34 #include "winternl.h"
35 #include "mmddk.h"
36 #include "wine/debug.h"
37 #include "dsound.h"
38 #include "dsdriver.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
43 /** Calculate how long a fragment length of about 10 ms should be in frames
45 * nSamplesPerSec: Frequency rate in samples per second
46 * nBlockAlign: Size of a single blockalign
48 * Returns:
49 * Size in bytes of a single fragment
51 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
53 /* Given a timer delay of 10ms, the fragment size is approximately:
54 * fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
55 * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
57 * ALSA uses buffers that are powers of 2. Because of this, fraglen
58 * is rounded up to the nearest power of 2:
61 if (nSamplesPerSec <= 12800)
62 return 128 * nBlockAlign;
64 if (nSamplesPerSec <= 25600)
65 return 256 * nBlockAlign;
67 if (nSamplesPerSec <= 51200)
68 return 512 * nBlockAlign;
70 return 1024 * nBlockAlign;
73 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
75 TRACE("(%p)\n", device);
77 device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
78 device->helfrags = device->buflen / device->fraglen;
79 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
81 if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
82 device->writelead = 0;
83 else
84 /* calculate the 10ms write lead */
85 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
88 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
90 HRESULT hres = DS_OK;
91 TRACE("(%p, %d)\n", device, forcewave);
93 if (device->driver)
95 IDsDriver_Close(device->driver);
96 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
97 waveOutClose(device->hwo);
98 IDsDriver_Release(device->driver);
99 device->driver = NULL;
100 device->buffer = NULL;
101 device->hwo = 0;
103 else if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
104 waveOutClose(device->hwo);
106 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
107 if (ds_hw_accel != DS_HW_ACCEL_EMULATION && !forcewave)
108 waveOutMessage((HWAVEOUT)device->drvdesc.dnDevNode, DRV_QUERYDSOUNDIFACE, (DWORD_PTR)&device->driver, 0);
110 /* Get driver description */
111 if (device->driver) {
112 DWORD wod = device->drvdesc.dnDevNode;
113 hres = IDsDriver_GetDriverDesc(device->driver,&(device->drvdesc));
114 device->drvdesc.dnDevNode = wod;
115 if (FAILED(hres)) {
116 WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres);
117 IDsDriver_Release(device->driver);
118 device->driver = NULL;
122 /* if no DirectSound interface available, use WINMM API instead */
123 if (!device->driver)
124 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
126 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
128 DWORD flags = CALLBACK_FUNCTION | WAVE_MAPPED;
130 if (device->driver)
131 flags |= WAVE_DIRECTSOUND;
133 hres = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode, device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD_PTR)device, flags));
134 if (FAILED(hres)) {
135 WARN("waveOutOpen failed\n");
136 if (device->driver)
138 IDsDriver_Release(device->driver);
139 device->driver = NULL;
141 return hres;
145 if (device->driver)
146 hres = IDsDriver_Open(device->driver);
148 return hres;
151 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
153 DWORD buflen;
154 HRESULT err = DS_OK;
155 TRACE("(%p)\n", device);
157 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
158 on windows this size is always fixed (tested on win-xp) */
159 if (!device->buflen)
160 device->buflen = ds_hel_buflen;
161 buflen = device->buflen;
162 buflen -= buflen % device->pwfx->nBlockAlign;
163 device->buflen = buflen;
165 if (device->driver)
167 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
168 DSBCAPS_PRIMARYBUFFER,0,
169 &(device->buflen),&(device->buffer),
170 (LPVOID*)&(device->hwbuf));
172 if (err != DS_OK) {
173 WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err);
174 err = DSOUND_ReopenDevice(device, TRUE);
175 if (FAILED(err))
177 WARN("Falling back to waveout failed too! Giving up\n");
178 return err;
181 if (device->hwbuf)
182 IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
184 DSOUND_RecalcPrimary(device);
185 device->prebuf = ds_snd_queue_max;
186 if (device->helfrags < ds_snd_queue_min)
188 WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device->buflen, ds_snd_queue_min * device->fraglen);
189 device->buflen = buflen;
190 IDsDriverBuffer_Release(device->hwbuf);
191 device->hwbuf = NULL;
192 err = DSOUND_ReopenDevice(device, TRUE);
193 if (FAILED(err))
195 WARN("Falling back to waveout failed too! Giving up\n");
196 return err;
199 else if (device->helfrags < ds_snd_queue_max)
200 device->prebuf = device->helfrags;
203 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
204 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
205 if (!device->mix_buffer)
207 if (device->hwbuf)
208 IDsDriverBuffer_Release(device->hwbuf);
209 device->hwbuf = NULL;
210 return DSERR_OUTOFMEMORY;
213 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
214 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
216 /* are we using waveOut stuff? */
217 if (!device->driver) {
218 LPBYTE newbuf;
219 LPWAVEHDR headers = NULL;
220 DWORD overshot;
221 unsigned int c;
223 /* Start in pause mode, to allow buffers to get filled */
224 waveOutPause(device->hwo);
226 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
228 /* reallocate emulated primary buffer */
229 if (device->buffer)
230 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
231 else
232 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
234 if (!newbuf) {
235 ERR("failed to allocate primary buffer\n");
236 return DSERR_OUTOFMEMORY;
237 /* but the old buffer might still exist and must be re-prepared */
240 DSOUND_RecalcPrimary(device);
241 if (device->pwave)
242 headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
243 else
244 headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
246 if (!headers) {
247 ERR("failed to allocate wave headers\n");
248 HeapFree(GetProcessHeap(), 0, newbuf);
249 DSOUND_RecalcPrimary(device);
250 return DSERR_OUTOFMEMORY;
253 device->buffer = newbuf;
254 device->pwave = headers;
256 /* prepare fragment headers */
257 for (c=0; c<device->helfrags; c++) {
258 device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
259 device->pwave[c].dwBufferLength = device->fraglen;
260 device->pwave[c].dwUser = (DWORD_PTR)device;
261 device->pwave[c].dwFlags = 0;
262 device->pwave[c].dwLoops = 0;
263 err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
264 if (err != DS_OK) {
265 while (c--)
266 waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
267 break;
271 overshot = device->buflen % device->fraglen;
272 /* sanity */
273 if(overshot)
275 overshot -= overshot % device->pwfx->nBlockAlign;
276 device->pwave[device->helfrags - 1].dwBufferLength += overshot;
279 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
281 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
282 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
283 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
284 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
285 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
286 return err;
290 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
292 TRACE("(%p)\n", device);
294 /* are we using waveOut stuff? */
295 if (!device->hwbuf) {
296 unsigned c;
298 /* get out of CS when calling the wave system */
299 LeaveCriticalSection(&(device->mixlock));
300 /* **** */
301 device->pwqueue = (DWORD)-1; /* resetting queues */
302 waveOutReset(device->hwo);
303 for (c=0; c<device->helfrags; c++)
304 waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
305 /* **** */
306 EnterCriticalSection(&(device->mixlock));
308 /* clear the queue */
309 device->pwqueue = 0;
310 } else {
311 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
312 if (!ref)
313 device->hwbuf = 0;
314 else
315 ERR("Still %d references on primary buffer, refcount leak?\n", ref);
319 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
321 HRESULT err = DS_OK;
322 TRACE("(%p)\n", device);
324 device->buflen = ds_hel_buflen;
325 err = DSOUND_PrimaryOpen(device);
327 if (err != DS_OK) {
328 WARN("DSOUND_PrimaryOpen failed\n");
329 return err;
332 device->state = STATE_STOPPED;
333 return DS_OK;
336 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
338 TRACE("(%p)\n", device);
340 /* **** */
341 EnterCriticalSection(&(device->mixlock));
343 DSOUND_PrimaryClose(device);
344 if (device->driver) {
345 if (device->hwbuf) {
346 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
347 device->hwbuf = 0;
349 } else
350 HeapFree(GetProcessHeap(),0,device->pwave);
351 HeapFree(GetProcessHeap(),0,device->pwfx);
352 device->pwfx=NULL;
354 LeaveCriticalSection(&(device->mixlock));
355 /* **** */
357 return DS_OK;
360 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
362 HRESULT err = DS_OK;
363 TRACE("(%p)\n", device);
365 if (device->hwbuf) {
366 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
367 if (err != DS_OK)
368 WARN("IDsDriverBuffer_Play failed\n");
369 } else {
370 err = mmErr(waveOutRestart(device->hwo));
371 if (err != DS_OK)
372 WARN("waveOutRestart failed\n");
375 return err;
378 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
380 HRESULT err = DS_OK;
381 TRACE("(%p)\n", device);
383 if (device->hwbuf) {
384 err = IDsDriverBuffer_Stop(device->hwbuf);
385 if (err == DSERR_BUFFERLOST) {
386 DSOUND_PrimaryClose(device);
387 err = DSOUND_ReopenDevice(device, FALSE);
388 if (FAILED(err))
389 ERR("DSOUND_ReopenDevice failed\n");
390 else
392 err = DSOUND_PrimaryOpen(device);
393 if (FAILED(err))
394 WARN("DSOUND_PrimaryOpen failed\n");
396 } else if (err != DS_OK) {
397 WARN("IDsDriverBuffer_Stop failed\n");
399 } else {
401 /* don't call the wave system with the lock set */
402 LeaveCriticalSection(&(device->mixlock));
403 /* **** */
405 err = mmErr(waveOutPause(device->hwo));
407 /* **** */
408 EnterCriticalSection(&(device->mixlock));
410 if (err != DS_OK)
411 WARN("waveOutPause failed\n");
414 return err;
417 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
419 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
421 if (device->hwbuf) {
422 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
423 if (err != S_OK) {
424 WARN("IDsDriverBuffer_GetPosition failed\n");
425 return err;
427 } else {
428 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
430 /* check if playpos was requested */
431 if (playpos)
432 /* use the cached play position */
433 *playpos = device->pwplay * device->fraglen;
435 /* check if writepos was requested */
436 if (writepos)
437 /* the writepos is the first non-queued position */
438 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
440 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
441 return DS_OK;
444 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
446 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
447 return sizeof(WAVEFORMATEX);
448 else
449 return sizeof(WAVEFORMATEX) + wfex->cbSize;
452 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
454 DWORD size = DSOUND_GetFormatSize(wfex);
455 LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
456 if (pwfx == NULL) {
457 WARN("out of memory\n");
458 } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
459 CopyMemory(pwfx, wfex, size);
460 } else {
461 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
462 pwfx->cbSize=0;
463 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
464 WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
465 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample/8;
467 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
468 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
469 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
472 return pwfx;
475 static HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
477 HRESULT err = DSERR_BUFFERLOST;
478 int i;
479 DWORD nSamplesPerSec, bpp, chans;
480 LPWAVEFORMATEX oldpwfx;
481 TRACE("(%p,%p)\n", device, wfex);
483 if (device->priolevel == DSSCL_NORMAL) {
484 WARN("failed priority check!\n");
485 return DSERR_PRIOLEVELNEEDED;
488 /* Let's be pedantic! */
489 if (wfex == NULL) {
490 WARN("invalid parameter: wfex==NULL!\n");
491 return DSERR_INVALIDPARAM;
493 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
494 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
495 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
496 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
497 wfex->wBitsPerSample, wfex->cbSize);
499 /* **** */
500 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
501 EnterCriticalSection(&(device->mixlock));
503 nSamplesPerSec = device->pwfx->nSamplesPerSec;
504 bpp = device->pwfx->wBitsPerSample;
505 chans = device->pwfx->nChannels;
507 oldpwfx = device->pwfx;
508 device->pwfx = DSOUND_CopyFormat(wfex);
509 if (device->pwfx == NULL) {
510 device->pwfx = oldpwfx;
511 oldpwfx = NULL;
512 err = DSERR_OUTOFMEMORY;
513 goto done;
516 if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
517 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
519 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
520 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
522 DWORD cp_size = wfex->wFormatTag == WAVE_FORMAT_PCM ?
523 sizeof(PCMWAVEFORMAT) : sizeof(WAVEFORMATEX) + wfex->cbSize;
524 err = DSERR_BUFFERLOST;
525 CopyMemory(device->pwfx, wfex, cp_size);
528 if (err != DSERR_BUFFERLOST && FAILED(err)) {
529 DWORD size = DSOUND_GetFormatSize(oldpwfx);
530 WARN("IDsDriverBuffer_SetFormat failed\n");
531 if (!forced) {
532 CopyMemory(device->pwfx, oldpwfx, size);
533 err = DS_OK;
535 goto done;
538 if (err == S_FALSE)
540 /* ALSA specific: S_FALSE tells that recreation was successful,
541 * but size and location may be changed, and buffer has to be restarted
542 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
543 * and the entire re-initialization will occur anyway
545 IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
546 IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
548 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
549 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
550 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
551 err = DS_OK;
553 DSOUND_RecalcPrimary(device);
556 if (err == DSERR_BUFFERLOST)
558 DSOUND_PrimaryClose(device);
560 err = DSOUND_ReopenDevice(device, FALSE);
561 if (FAILED(err))
563 WARN("DSOUND_ReopenDevice failed: %08x\n", err);
564 goto done;
566 err = DSOUND_PrimaryOpen(device);
567 if (err != DS_OK) {
568 WARN("DSOUND_PrimaryOpen failed\n");
569 goto done;
572 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
574 DSOUND_PrimaryClose(device);
575 device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
576 err = DSOUND_ReopenDevice(device, TRUE);
577 if (FAILED(err))
578 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
579 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
580 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
584 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
585 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
586 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
587 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
588 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
590 if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
591 IDirectSoundBufferImpl** dsb = device->buffers;
592 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
593 /* **** */
594 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
596 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
597 DSOUND_RecalcFormat((*dsb));
598 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen, FALSE);
599 (*dsb)->primary_mixpos = 0;
601 RtlReleaseResource(&(*dsb)->lock);
602 /* **** */
606 done:
607 LeaveCriticalSection(&(device->mixlock));
608 RtlReleaseResource(&(device->buffer_list_lock));
609 /* **** */
611 HeapFree(GetProcessHeap(), 0, oldpwfx);
612 return err;
615 /*******************************************************************************
616 * PrimaryBuffer
618 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
620 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
621 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
624 /* This sets this format for the <em>Primary Buffer Only</em> */
625 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
626 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
627 LPDIRECTSOUNDBUFFER iface,
628 LPCWAVEFORMATEX wfex)
630 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
631 DirectSoundDevice *device = This->device;
632 TRACE("(%p,%p)\n", iface, wfex);
633 return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
636 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
637 LPDIRECTSOUNDBUFFER iface,LONG vol
639 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
640 DirectSoundDevice *device = This->device;
641 DWORD ampfactors;
642 HRESULT hres = DS_OK;
643 TRACE("(%p,%d)\n", iface, vol);
645 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
646 WARN("control unavailable\n");
647 return DSERR_CONTROLUNAVAIL;
650 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
651 WARN("invalid parameter: vol = %d\n", vol);
652 return DSERR_INVALIDPARAM;
655 /* **** */
656 EnterCriticalSection(&(device->mixlock));
658 waveOutGetVolume(device->hwo, &ampfactors);
659 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
660 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
661 DSOUND_AmpFactorToVolPan(&device->volpan);
662 if (vol != device->volpan.lVolume) {
663 device->volpan.lVolume=vol;
664 DSOUND_RecalcVolPan(&device->volpan);
665 if (device->hwbuf) {
666 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
667 if (hres != DS_OK)
668 WARN("IDsDriverBuffer_SetVolumePan failed\n");
669 } else {
670 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
671 waveOutSetVolume(device->hwo, ampfactors);
675 LeaveCriticalSection(&(device->mixlock));
676 /* **** */
678 return hres;
681 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
682 LPDIRECTSOUNDBUFFER iface,LPLONG vol
684 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
685 DirectSoundDevice *device = This->device;
686 DWORD ampfactors;
687 TRACE("(%p,%p)\n", iface, vol);
689 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
690 WARN("control unavailable\n");
691 return DSERR_CONTROLUNAVAIL;
694 if (vol == NULL) {
695 WARN("invalid parameter: vol = NULL\n");
696 return DSERR_INVALIDPARAM;
699 if (!device->hwbuf)
701 waveOutGetVolume(device->hwo, &ampfactors);
702 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
703 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
704 DSOUND_AmpFactorToVolPan(&device->volpan);
706 *vol = device->volpan.lVolume;
707 return DS_OK;
710 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
711 LPDIRECTSOUNDBUFFER iface,DWORD freq
713 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
714 TRACE("(%p,%d)\n",This,freq);
716 /* You cannot set the frequency of the primary buffer */
717 WARN("control unavailable\n");
718 return DSERR_CONTROLUNAVAIL;
721 static HRESULT WINAPI PrimaryBufferImpl_Play(
722 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
724 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
725 DirectSoundDevice *device = This->device;
726 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
728 if (!(flags & DSBPLAY_LOOPING)) {
729 WARN("invalid parameter: flags = %08x\n", flags);
730 return DSERR_INVALIDPARAM;
733 /* **** */
734 EnterCriticalSection(&(device->mixlock));
736 if (device->state == STATE_STOPPED)
737 device->state = STATE_STARTING;
738 else if (device->state == STATE_STOPPING)
739 device->state = STATE_PLAYING;
741 LeaveCriticalSection(&(device->mixlock));
742 /* **** */
744 return DS_OK;
747 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
749 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
750 DirectSoundDevice *device = This->device;
751 TRACE("(%p)\n", iface);
753 /* **** */
754 EnterCriticalSection(&(device->mixlock));
756 if (device->state == STATE_PLAYING)
757 device->state = STATE_STOPPING;
758 else if (device->state == STATE_STARTING)
759 device->state = STATE_STOPPED;
761 LeaveCriticalSection(&(device->mixlock));
762 /* **** */
764 return DS_OK;
767 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
769 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
770 ULONG ref = InterlockedIncrement(&(This->ref));
771 TRACE("(%p) ref was %d\n", This, ref - 1);
772 if(ref == 1)
773 InterlockedIncrement(&This->numIfaces);
774 return ref;
777 void primarybuffer_destroy(IDirectSoundBufferImpl *This)
779 This->device->primary = NULL;
780 HeapFree(GetProcessHeap(), 0, This);
781 TRACE("(%p) released\n", This);
784 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
786 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
787 DWORD ref = InterlockedDecrement(&(This->ref));
788 TRACE("(%p) ref was %d\n", This, ref + 1);
790 if (!ref && !InterlockedDecrement(&This->numIfaces))
791 primarybuffer_destroy(This);
792 return ref;
795 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
796 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
798 HRESULT hres;
799 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
800 DirectSoundDevice *device = This->device;
801 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
803 /* **** */
804 EnterCriticalSection(&(device->mixlock));
806 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
807 if (hres != DS_OK) {
808 WARN("DSOUND_PrimaryGetPosition failed\n");
809 LeaveCriticalSection(&(device->mixlock));
810 return hres;
812 if (writepos) {
813 if (device->state != STATE_STOPPED)
814 /* apply the documented 10ms lead to writepos */
815 *writepos += device->writelead;
816 while (*writepos >= device->buflen) *writepos -= device->buflen;
819 LeaveCriticalSection(&(device->mixlock));
820 /* **** */
822 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
823 return DS_OK;
826 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
827 LPDIRECTSOUNDBUFFER iface,LPDWORD status
829 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
830 DirectSoundDevice *device = This->device;
831 TRACE("(%p,%p)\n", iface, status);
833 if (status == NULL) {
834 WARN("invalid parameter: status == NULL\n");
835 return DSERR_INVALIDPARAM;
838 *status = 0;
839 if ((device->state == STATE_STARTING) ||
840 (device->state == STATE_PLAYING))
841 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
843 TRACE("status=%x\n", *status);
844 return DS_OK;
848 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
849 LPDIRECTSOUNDBUFFER iface,
850 LPWAVEFORMATEX lpwf,
851 DWORD wfsize,
852 LPDWORD wfwritten)
854 DWORD size;
855 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
856 DirectSoundDevice *device = This->device;
857 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
859 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
861 if (lpwf) { /* NULL is valid */
862 if (wfsize >= size) {
863 CopyMemory(lpwf,device->pwfx,size);
864 if (wfwritten)
865 *wfwritten = size;
866 } else {
867 WARN("invalid parameter: wfsize too small\n");
868 if (wfwritten)
869 *wfwritten = 0;
870 return DSERR_INVALIDPARAM;
872 } else {
873 if (wfwritten)
874 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
875 else {
876 WARN("invalid parameter: wfwritten == NULL\n");
877 return DSERR_INVALIDPARAM;
881 return DS_OK;
884 static HRESULT WINAPI PrimaryBufferImpl_Lock(
885 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
887 HRESULT hres;
888 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
889 DirectSoundDevice *device = This->device;
890 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
891 iface,
892 writecursor,
893 writebytes,
894 lplpaudioptr1,
895 audiobytes1,
896 lplpaudioptr2,
897 audiobytes2,
898 flags,
899 GetTickCount()
902 if (!audiobytes1)
903 return DSERR_INVALIDPARAM;
905 if (device->priolevel != DSSCL_WRITEPRIMARY) {
906 WARN("failed priority check!\n");
907 return DSERR_PRIOLEVELNEEDED;
910 /* when this flag is set, writecursor is meaningless and must be calculated */
911 if (flags & DSBLOCK_FROMWRITECURSOR) {
912 /* GetCurrentPosition does too much magic to duplicate here */
913 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
914 if (hres != DS_OK) {
915 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
916 return hres;
920 /* when this flag is set, writebytes is meaningless and must be set */
921 if (flags & DSBLOCK_ENTIREBUFFER)
922 writebytes = device->buflen;
924 if (writecursor >= device->buflen) {
925 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
926 writecursor, device->buflen);
927 return DSERR_INVALIDPARAM;
930 if (writebytes > device->buflen) {
931 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
932 writebytes, device->buflen);
933 return DSERR_INVALIDPARAM;
936 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
937 hres = IDsDriverBuffer_Lock(device->hwbuf,
938 lplpaudioptr1, audiobytes1,
939 lplpaudioptr2, audiobytes2,
940 writecursor, writebytes,
942 if (hres != DS_OK) {
943 WARN("IDsDriverBuffer_Lock failed\n");
944 return hres;
946 } else {
947 if (writecursor+writebytes <= device->buflen) {
948 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
949 *audiobytes1 = writebytes;
950 if (lplpaudioptr2)
951 *(LPBYTE*)lplpaudioptr2 = NULL;
952 if (audiobytes2)
953 *audiobytes2 = 0;
954 TRACE("->%d.0\n",writebytes);
955 } else {
956 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
957 *audiobytes1 = device->buflen-writecursor;
958 if (lplpaudioptr2)
959 *(LPBYTE*)lplpaudioptr2 = device->buffer;
960 if (audiobytes2)
961 *audiobytes2 = writebytes-(device->buflen-writecursor);
962 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
965 return DS_OK;
968 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
969 LPDIRECTSOUNDBUFFER iface,DWORD newpos
971 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
972 TRACE("(%p,%d)\n",This,newpos);
974 /* You cannot set the position of the primary buffer */
975 WARN("invalid call\n");
976 return DSERR_INVALIDCALL;
979 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
980 LPDIRECTSOUNDBUFFER iface,LONG pan
982 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
983 DirectSoundDevice *device = This->device;
984 DWORD ampfactors;
985 HRESULT hres = DS_OK;
986 TRACE("(%p,%d)\n", iface, pan);
988 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
989 WARN("control unavailable\n");
990 return DSERR_CONTROLUNAVAIL;
993 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
994 WARN("invalid parameter: pan = %d\n", pan);
995 return DSERR_INVALIDPARAM;
998 /* **** */
999 EnterCriticalSection(&(device->mixlock));
1001 if (!device->hwbuf)
1003 waveOutGetVolume(device->hwo, &ampfactors);
1004 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
1005 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
1006 DSOUND_AmpFactorToVolPan(&device->volpan);
1008 if (pan != device->volpan.lPan) {
1009 device->volpan.lPan=pan;
1010 DSOUND_RecalcVolPan(&device->volpan);
1011 if (device->hwbuf) {
1012 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
1013 if (hres != DS_OK)
1014 WARN("IDsDriverBuffer_SetVolumePan failed\n");
1015 } else {
1016 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
1017 waveOutSetVolume(device->hwo, ampfactors);
1021 LeaveCriticalSection(&(device->mixlock));
1022 /* **** */
1024 return hres;
1027 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
1028 LPDIRECTSOUNDBUFFER iface,LPLONG pan
1030 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1031 DirectSoundDevice *device = This->device;
1032 DWORD ampfactors;
1033 TRACE("(%p,%p)\n", iface, pan);
1035 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1036 WARN("control unavailable\n");
1037 return DSERR_CONTROLUNAVAIL;
1040 if (pan == NULL) {
1041 WARN("invalid parameter: pan == NULL\n");
1042 return DSERR_INVALIDPARAM;
1045 if (!device->hwbuf)
1047 waveOutGetVolume(device->hwo, &ampfactors);
1048 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
1049 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
1050 DSOUND_AmpFactorToVolPan(&device->volpan);
1052 *pan = device->volpan.lPan;
1053 return DS_OK;
1056 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
1057 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1059 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1060 DirectSoundDevice *device = This->device;
1061 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1063 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1064 WARN("failed priority check!\n");
1065 return DSERR_PRIOLEVELNEEDED;
1068 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
1069 HRESULT hres;
1071 if ((char *)p1 - (char *)device->buffer + x1 > device->buflen)
1072 hres = DSERR_INVALIDPARAM;
1073 else
1074 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
1076 if (hres != DS_OK) {
1077 WARN("IDsDriverBuffer_Unlock failed\n");
1078 return hres;
1082 return DS_OK;
1085 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1086 LPDIRECTSOUNDBUFFER iface
1088 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1089 FIXME("(%p):stub\n",This);
1090 return DS_OK;
1093 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1094 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1096 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1097 DirectSoundDevice *device = This->device;
1098 TRACE("(%p,%p)\n", iface, freq);
1100 if (freq == NULL) {
1101 WARN("invalid parameter: freq == NULL\n");
1102 return DSERR_INVALIDPARAM;
1105 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1106 WARN("control unavailable\n");
1107 return DSERR_CONTROLUNAVAIL;
1110 *freq = device->pwfx->nSamplesPerSec;
1111 TRACE("-> %d\n", *freq);
1113 return DS_OK;
1116 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1117 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1119 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1120 WARN("(%p) already initialized\n", This);
1121 return DSERR_ALREADYINITIALIZED;
1124 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1125 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1127 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1128 DirectSoundDevice *device = This->device;
1129 TRACE("(%p,%p)\n", iface, caps);
1131 if (caps == NULL) {
1132 WARN("invalid parameter: caps == NULL\n");
1133 return DSERR_INVALIDPARAM;
1136 if (caps->dwSize < sizeof(*caps)) {
1137 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1138 return DSERR_INVALIDPARAM;
1141 caps->dwFlags = device->dsbd.dwFlags;
1142 caps->dwBufferBytes = device->buflen;
1144 /* Windows reports these as zero */
1145 caps->dwUnlockTransferRate = 0;
1146 caps->dwPlayCpuOverhead = 0;
1148 return DS_OK;
1151 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1152 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1154 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1155 DirectSoundDevice *device = This->device;
1156 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1158 if (ppobj == NULL) {
1159 WARN("invalid parameter\n");
1160 return E_INVALIDARG;
1163 *ppobj = NULL; /* assume failure */
1165 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1166 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1167 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1168 *ppobj = This;
1169 return S_OK;
1172 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1173 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1174 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1175 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1176 return E_NOINTERFACE;
1179 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1180 ERR("app requested IDirectSoundNotify on primary buffer\n");
1181 /* FIXME: should we support this? */
1182 return E_NOINTERFACE;
1185 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1186 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1187 return E_NOINTERFACE;
1190 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1191 if (!device->listener)
1192 IDirectSound3DListenerImpl_Create(device, &device->listener);
1193 if (device->listener) {
1194 *ppobj = device->listener;
1195 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1196 return S_OK;
1199 WARN("IID_IDirectSound3DListener failed\n");
1200 return E_NOINTERFACE;
1203 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1204 FIXME("app requested IKsPropertySet on primary buffer\n");
1205 return E_NOINTERFACE;
1208 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1209 return E_NOINTERFACE;
1212 static const IDirectSoundBufferVtbl dspbvt =
1214 PrimaryBufferImpl_QueryInterface,
1215 PrimaryBufferImpl_AddRef,
1216 PrimaryBufferImpl_Release,
1217 PrimaryBufferImpl_GetCaps,
1218 PrimaryBufferImpl_GetCurrentPosition,
1219 PrimaryBufferImpl_GetFormat,
1220 PrimaryBufferImpl_GetVolume,
1221 PrimaryBufferImpl_GetPan,
1222 PrimaryBufferImpl_GetFrequency,
1223 PrimaryBufferImpl_GetStatus,
1224 PrimaryBufferImpl_Initialize,
1225 PrimaryBufferImpl_Lock,
1226 PrimaryBufferImpl_Play,
1227 PrimaryBufferImpl_SetCurrentPosition,
1228 PrimaryBufferImpl_SetFormat,
1229 PrimaryBufferImpl_SetVolume,
1230 PrimaryBufferImpl_SetPan,
1231 PrimaryBufferImpl_SetFrequency,
1232 PrimaryBufferImpl_Stop,
1233 PrimaryBufferImpl_Unlock,
1234 PrimaryBufferImpl_Restore
1237 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1238 const DSBUFFERDESC *dsbd)
1240 IDirectSoundBufferImpl *dsb;
1241 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1243 if (dsbd->lpwfxFormat) {
1244 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1245 *ppdsb = NULL;
1246 return DSERR_INVALIDPARAM;
1249 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1251 if (dsb == NULL) {
1252 WARN("out of memory\n");
1253 *ppdsb = NULL;
1254 return DSERR_OUTOFMEMORY;
1257 dsb->ref = 1;
1258 dsb->numIfaces = 1;
1259 dsb->device = device;
1260 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1262 device->dsbd = *dsbd;
1264 TRACE("Created primary buffer at %p\n", dsb);
1265 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1266 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1267 device->pwfx->wFormatTag, device->pwfx->nChannels,
1268 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1269 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1270 device->pwfx->cbSize);
1272 *ppdsb = dsb;
1273 return S_OK;