shell32: Remove redundant loop to count already known value.
[wine/multimedia.git] / dlls / dsound / primary.c
bloba726ec8dc14830a9b2cacbcb4e34980ad2b89464
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 COBJMACROS
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "mmsystem.h"
35 #include "winternl.h"
36 #include "mmddk.h"
37 #include "wine/debug.h"
38 #include "dsound.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 /* calculate the 10ms write lead */
82 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
85 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
87 UINT prebuf_frames;
88 REFERENCE_TIME prebuf_rt;
89 HRESULT hres;
91 TRACE("(%p, %d)\n", device, forcewave);
93 if(device->client){
94 IAudioClient_Release(device->client);
95 device->client = NULL;
97 if(device->render){
98 IAudioRenderClient_Release(device->render);
99 device->render = NULL;
101 if(device->clock){
102 IAudioClock_Release(device->clock);
103 device->clock = NULL;
105 if(device->volume){
106 IAudioStreamVolume_Release(device->volume);
107 device->volume = NULL;
110 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
111 CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
112 if(FAILED(hres)){
113 WARN("Activate failed: %08x\n", hres);
114 return hres;
117 prebuf_frames = device->prebuf * DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign) / device->pwfx->nBlockAlign;
118 prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
120 hres = IAudioClient_Initialize(device->client,
121 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST,
122 prebuf_rt, 0, device->pwfx, NULL);
123 if(FAILED(hres)){
124 IAudioClient_Release(device->client);
125 device->client = NULL;
126 WARN("Initialize failed: %08x\n", hres);
127 return hres;
130 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
131 (void**)&device->render);
132 if(FAILED(hres)){
133 IAudioClient_Release(device->client);
134 device->client = NULL;
135 WARN("GetService failed: %08x\n", hres);
136 return hres;
139 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
140 (void**)&device->clock);
141 if(FAILED(hres)){
142 IAudioClient_Release(device->client);
143 IAudioRenderClient_Release(device->render);
144 device->client = NULL;
145 device->render = NULL;
146 WARN("GetService failed: %08x\n", hres);
147 return hres;
150 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
151 (void**)&device->volume);
152 if(FAILED(hres)){
153 IAudioClient_Release(device->client);
154 IAudioRenderClient_Release(device->render);
155 IAudioClock_Release(device->clock);
156 device->client = NULL;
157 device->render = NULL;
158 device->clock = NULL;
159 WARN("GetService failed: %08x\n", hres);
160 return hres;
163 return S_OK;
166 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
168 DWORD buflen;
169 LPBYTE newbuf;
171 TRACE("(%p)\n", device);
173 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
174 on windows this size is always fixed (tested on win-xp) */
175 if (!device->buflen)
176 device->buflen = ds_hel_buflen;
177 buflen = device->buflen;
178 buflen -= buflen % device->pwfx->nBlockAlign;
179 device->buflen = buflen;
181 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
182 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
183 if (!device->mix_buffer)
184 return DSERR_OUTOFMEMORY;
186 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
187 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
189 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
191 /* reallocate emulated primary buffer */
192 if (device->buffer)
193 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
194 else
195 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
197 if (!newbuf) {
198 ERR("failed to allocate primary buffer\n");
199 return DSERR_OUTOFMEMORY;
200 /* but the old buffer might still exist and must be re-prepared */
203 DSOUND_RecalcPrimary(device);
205 device->buffer = newbuf;
207 TRACE("fraglen=%d\n", device->fraglen);
209 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
210 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
211 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
212 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
213 device->last_pos_bytes = device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
214 return DS_OK;
218 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
220 HRESULT hr;
222 TRACE("(%p)\n", device);
224 device->pwqueue = (DWORD)-1; /* resetting queues */
226 if(device->client){
227 hr = IAudioClient_Stop(device->client);
228 if(FAILED(hr))
229 WARN("Stop failed: %08x\n", hr);
232 /* clear the queue */
233 device->pwqueue = 0;
236 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
238 HRESULT err = DS_OK;
239 TRACE("(%p)\n", device);
241 device->buflen = ds_hel_buflen;
242 err = DSOUND_PrimaryOpen(device);
244 if (err != DS_OK) {
245 WARN("DSOUND_PrimaryOpen failed\n");
246 return err;
249 device->state = STATE_STOPPED;
250 return DS_OK;
253 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
255 TRACE("(%p)\n", device);
257 /* **** */
258 EnterCriticalSection(&(device->mixlock));
260 DSOUND_PrimaryClose(device);
261 HeapFree(GetProcessHeap(),0,device->pwfx);
262 device->pwfx=NULL;
264 LeaveCriticalSection(&(device->mixlock));
265 /* **** */
267 return DS_OK;
270 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
272 HRESULT hr;
274 TRACE("(%p)\n", device);
276 hr = IAudioClient_Start(device->client);
277 if(FAILED(hr)){
278 WARN("Start failed: %08x\n", hr);
279 return hr;
282 return DS_OK;
285 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
287 HRESULT hr;
289 TRACE("(%p)\n", device);
291 hr = IAudioClient_Stop(device->client);
292 if(FAILED(hr)){
293 WARN("Stop failed: %08x\n", hr);
294 return hr;
297 return DS_OK;
300 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
302 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
304 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
306 /* check if playpos was requested */
307 if (playpos)
308 /* use the cached play position */
309 *playpos = device->pwplay * device->fraglen;
311 /* check if writepos was requested */
312 if (writepos)
313 /* the writepos is the first non-queued position */
314 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
316 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
317 return DS_OK;
320 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
322 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
323 return sizeof(WAVEFORMATEX);
324 else
325 return sizeof(WAVEFORMATEX) + wfex->cbSize;
328 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
330 DWORD size = DSOUND_GetFormatSize(wfex);
331 LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
332 if (pwfx == NULL) {
333 WARN("out of memory\n");
334 } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
335 CopyMemory(pwfx, wfex, size);
336 } else {
337 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
338 pwfx->cbSize=0;
339 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
340 WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
341 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample/8;
343 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
344 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
345 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
348 return pwfx;
351 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
353 HRESULT err = DSERR_BUFFERLOST;
354 int i;
355 WAVEFORMATEX *old_fmt;
356 WAVEFORMATEXTENSIBLE *fmtex;
357 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
359 TRACE("(%p,%p)\n", device, passed_fmt);
361 if (device->priolevel == DSSCL_NORMAL) {
362 WARN("failed priority check!\n");
363 return DSERR_PRIOLEVELNEEDED;
366 /* Let's be pedantic! */
367 if (passed_fmt == NULL) {
368 WARN("invalid parameter: passed_fmt==NULL!\n");
369 return DSERR_INVALIDPARAM;
371 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
372 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
373 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
374 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
375 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
377 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
378 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
379 passed_fmt->nAvgBytesPerSec == 0 ||
380 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
381 return DSERR_INVALIDPARAM;
383 /* **** */
384 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
385 EnterCriticalSection(&(device->mixlock));
387 old_fmt = device->pwfx;
388 device->pwfx = DSOUND_CopyFormat(passed_fmt);
389 fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
390 if (device->pwfx == NULL) {
391 device->pwfx = old_fmt;
392 old_fmt = NULL;
393 err = DSERR_OUTOFMEMORY;
394 goto done;
397 DSOUND_PrimaryClose(device);
399 err = DSOUND_ReopenDevice(device, FALSE);
400 if(SUCCEEDED(err))
401 goto opened;
403 /* requested format failed, so try others */
404 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
405 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
406 device->pwfx->wBitsPerSample = 32;
407 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
408 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
410 err = DSOUND_ReopenDevice(device, FALSE);
411 if(SUCCEEDED(err))
412 goto opened;
415 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
416 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
417 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
418 device->pwfx->wBitsPerSample = 32;
419 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
420 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
422 err = DSOUND_ReopenDevice(device, FALSE);
423 if(SUCCEEDED(err))
424 goto opened;
427 device->pwfx->wBitsPerSample = 32;
428 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
429 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
430 err = DSOUND_ReopenDevice(device, FALSE);
431 if(SUCCEEDED(err))
432 goto opened;
434 device->pwfx->wBitsPerSample = 16;
435 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
436 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
437 err = DSOUND_ReopenDevice(device, FALSE);
438 if(SUCCEEDED(err))
439 goto opened;
441 device->pwfx->wBitsPerSample = 8;
442 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
443 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
444 err = DSOUND_ReopenDevice(device, FALSE);
445 if(SUCCEEDED(err))
446 goto opened;
448 device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
449 device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
450 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
451 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
452 err = DSOUND_ReopenDevice(device, FALSE);
453 if(SUCCEEDED(err))
454 goto opened;
456 device->pwfx->wBitsPerSample = 32;
457 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
458 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
459 err = DSOUND_ReopenDevice(device, FALSE);
460 if(SUCCEEDED(err))
461 goto opened;
463 device->pwfx->wBitsPerSample = 16;
464 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
465 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
466 err = DSOUND_ReopenDevice(device, FALSE);
467 if(SUCCEEDED(err))
468 goto opened;
470 device->pwfx->wBitsPerSample = 8;
471 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
472 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
473 err = DSOUND_ReopenDevice(device, FALSE);
474 if(SUCCEEDED(err))
475 goto opened;
477 WARN("No formats could be opened\n");
478 goto done;
480 opened:
481 err = DSOUND_PrimaryOpen(device);
482 if (err != DS_OK) {
483 WARN("DSOUND_PrimaryOpen failed\n");
484 goto done;
487 if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
489 DSOUND_PrimaryClose(device);
490 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
491 err = DSOUND_ReopenDevice(device, TRUE);
492 if (FAILED(err))
493 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
494 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
495 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
498 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
499 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
500 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
501 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
502 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
504 if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
505 old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
506 old_fmt->nChannels != device->pwfx->nChannels) {
507 IDirectSoundBufferImpl** dsb = device->buffers;
508 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
509 /* **** */
510 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
512 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
513 DSOUND_RecalcFormat((*dsb));
514 (*dsb)->primary_mixpos = 0;
516 RtlReleaseResource(&(*dsb)->lock);
517 /* **** */
521 done:
522 LeaveCriticalSection(&(device->mixlock));
523 RtlReleaseResource(&(device->buffer_list_lock));
524 /* **** */
526 HeapFree(GetProcessHeap(), 0, old_fmt);
527 return err;
530 /*******************************************************************************
531 * PrimaryBuffer
533 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
535 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
536 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
539 /* This sets this format for the <em>Primary Buffer Only</em> */
540 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
541 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
542 LPDIRECTSOUNDBUFFER iface,
543 LPCWAVEFORMATEX wfex)
545 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
546 TRACE("(%p,%p)\n", iface, wfex);
547 return primarybuffer_SetFormat(This->device, wfex);
550 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
551 LPDIRECTSOUNDBUFFER iface,LONG vol
553 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
554 DirectSoundDevice *device = This->device;
555 HRESULT hr;
556 float lvol, rvol;
558 TRACE("(%p,%d)\n", iface, vol);
560 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
561 WARN("control unavailable\n");
562 return DSERR_CONTROLUNAVAIL;
565 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
566 WARN("invalid parameter: vol = %d\n", vol);
567 return DSERR_INVALIDPARAM;
570 /* **** */
571 EnterCriticalSection(&device->mixlock);
573 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
574 if(FAILED(hr)){
575 LeaveCriticalSection(&device->mixlock);
576 WARN("GetChannelVolume failed: %08x\n", hr);
577 return hr;
580 if(device->pwfx->nChannels > 1){
581 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
582 if(FAILED(hr)){
583 LeaveCriticalSection(&device->mixlock);
584 WARN("GetChannelVolume failed: %08x\n", hr);
585 return hr;
587 }else
588 rvol = 1;
590 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
591 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
593 DSOUND_AmpFactorToVolPan(&device->volpan);
594 if (vol != device->volpan.lVolume) {
595 device->volpan.lVolume=vol;
596 DSOUND_RecalcVolPan(&device->volpan);
597 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
598 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
599 if(FAILED(hr)){
600 LeaveCriticalSection(&device->mixlock);
601 WARN("SetChannelVolume failed: %08x\n", hr);
602 return hr;
605 if(device->pwfx->nChannels > 1){
606 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
607 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
608 if(FAILED(hr)){
609 LeaveCriticalSection(&device->mixlock);
610 WARN("SetChannelVolume failed: %08x\n", hr);
611 return hr;
616 LeaveCriticalSection(&(device->mixlock));
617 /* **** */
619 return DS_OK;
622 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
623 LPDIRECTSOUNDBUFFER iface,LPLONG vol
625 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
626 DirectSoundDevice *device = This->device;
627 float lvol, rvol;
628 HRESULT hr;
629 TRACE("(%p,%p)\n", iface, vol);
631 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
632 WARN("control unavailable\n");
633 return DSERR_CONTROLUNAVAIL;
636 if (vol == NULL) {
637 WARN("invalid parameter: vol = NULL\n");
638 return DSERR_INVALIDPARAM;
641 EnterCriticalSection(&device->mixlock);
643 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
644 if(FAILED(hr)){
645 LeaveCriticalSection(&device->mixlock);
646 WARN("GetChannelVolume failed: %08x\n", hr);
647 return hr;
650 if(device->pwfx->nChannels > 1){
651 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
652 if(FAILED(hr)){
653 LeaveCriticalSection(&device->mixlock);
654 WARN("GetChannelVolume failed: %08x\n", hr);
655 return hr;
657 }else
658 rvol = 1;
660 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
661 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
663 DSOUND_AmpFactorToVolPan(&device->volpan);
664 *vol = device->volpan.lVolume;
666 LeaveCriticalSection(&device->mixlock);
668 return DS_OK;
671 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
672 LPDIRECTSOUNDBUFFER iface,DWORD freq
674 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
675 TRACE("(%p,%d)\n",This,freq);
677 /* You cannot set the frequency of the primary buffer */
678 WARN("control unavailable\n");
679 return DSERR_CONTROLUNAVAIL;
682 static HRESULT WINAPI PrimaryBufferImpl_Play(
683 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
685 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
686 DirectSoundDevice *device = This->device;
687 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
689 if (!(flags & DSBPLAY_LOOPING)) {
690 WARN("invalid parameter: flags = %08x\n", flags);
691 return DSERR_INVALIDPARAM;
694 /* **** */
695 EnterCriticalSection(&(device->mixlock));
697 if (device->state == STATE_STOPPED)
698 device->state = STATE_STARTING;
699 else if (device->state == STATE_STOPPING)
700 device->state = STATE_PLAYING;
702 LeaveCriticalSection(&(device->mixlock));
703 /* **** */
705 return DS_OK;
708 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
710 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
711 DirectSoundDevice *device = This->device;
712 TRACE("(%p)\n", iface);
714 /* **** */
715 EnterCriticalSection(&(device->mixlock));
717 if (device->state == STATE_PLAYING)
718 device->state = STATE_STOPPING;
719 else if (device->state == STATE_STARTING)
720 device->state = STATE_STOPPED;
722 LeaveCriticalSection(&(device->mixlock));
723 /* **** */
725 return DS_OK;
728 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
730 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
731 ULONG ref = InterlockedIncrement(&(This->ref));
732 TRACE("(%p) ref was %d\n", This, ref - 1);
733 if(ref == 1)
734 InterlockedIncrement(&This->numIfaces);
735 return ref;
738 void primarybuffer_destroy(IDirectSoundBufferImpl *This)
740 This->device->primary = NULL;
741 HeapFree(GetProcessHeap(), 0, This);
742 TRACE("(%p) released\n", This);
745 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
747 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
748 DWORD ref = InterlockedDecrement(&(This->ref));
749 TRACE("(%p) ref was %d\n", This, ref + 1);
751 if (!ref && !InterlockedDecrement(&This->numIfaces))
752 primarybuffer_destroy(This);
753 return ref;
756 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
757 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
759 HRESULT hres;
760 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
761 DirectSoundDevice *device = This->device;
762 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
764 /* **** */
765 EnterCriticalSection(&(device->mixlock));
767 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
768 if (hres != DS_OK) {
769 WARN("DSOUND_PrimaryGetPosition failed\n");
770 LeaveCriticalSection(&(device->mixlock));
771 return hres;
773 if (writepos) {
774 if (device->state != STATE_STOPPED)
775 /* apply the documented 10ms lead to writepos */
776 *writepos += device->writelead;
777 while (*writepos >= device->buflen) *writepos -= device->buflen;
780 LeaveCriticalSection(&(device->mixlock));
781 /* **** */
783 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
784 return DS_OK;
787 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
788 LPDIRECTSOUNDBUFFER iface,LPDWORD status
790 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
791 DirectSoundDevice *device = This->device;
792 TRACE("(%p,%p)\n", iface, status);
794 if (status == NULL) {
795 WARN("invalid parameter: status == NULL\n");
796 return DSERR_INVALIDPARAM;
799 *status = 0;
800 if ((device->state == STATE_STARTING) ||
801 (device->state == STATE_PLAYING))
802 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
804 TRACE("status=%x\n", *status);
805 return DS_OK;
809 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
810 LPDIRECTSOUNDBUFFER iface,
811 LPWAVEFORMATEX lpwf,
812 DWORD wfsize,
813 LPDWORD wfwritten)
815 DWORD size;
816 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
817 DirectSoundDevice *device = This->device;
818 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
820 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
822 if (lpwf) { /* NULL is valid */
823 if (wfsize >= size) {
824 CopyMemory(lpwf,device->pwfx,size);
825 if (wfwritten)
826 *wfwritten = size;
827 } else {
828 WARN("invalid parameter: wfsize too small\n");
829 if (wfwritten)
830 *wfwritten = 0;
831 return DSERR_INVALIDPARAM;
833 } else {
834 if (wfwritten)
835 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
836 else {
837 WARN("invalid parameter: wfwritten == NULL\n");
838 return DSERR_INVALIDPARAM;
842 return DS_OK;
845 static HRESULT WINAPI PrimaryBufferImpl_Lock(
846 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
848 HRESULT hres;
849 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
850 DirectSoundDevice *device = This->device;
851 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
852 iface,
853 writecursor,
854 writebytes,
855 lplpaudioptr1,
856 audiobytes1,
857 lplpaudioptr2,
858 audiobytes2,
859 flags,
860 GetTickCount()
863 if (!audiobytes1)
864 return DSERR_INVALIDPARAM;
866 if (device->priolevel != DSSCL_WRITEPRIMARY) {
867 WARN("failed priority check!\n");
868 return DSERR_PRIOLEVELNEEDED;
871 /* when this flag is set, writecursor is meaningless and must be calculated */
872 if (flags & DSBLOCK_FROMWRITECURSOR) {
873 /* GetCurrentPosition does too much magic to duplicate here */
874 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
875 if (hres != DS_OK) {
876 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
877 return hres;
881 /* when this flag is set, writebytes is meaningless and must be set */
882 if (flags & DSBLOCK_ENTIREBUFFER)
883 writebytes = device->buflen;
885 if (writecursor >= device->buflen) {
886 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
887 writecursor, device->buflen);
888 return DSERR_INVALIDPARAM;
891 if (writebytes > device->buflen) {
892 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
893 writebytes, device->buflen);
894 return DSERR_INVALIDPARAM;
897 if (writecursor+writebytes <= device->buflen) {
898 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
899 *audiobytes1 = writebytes;
900 if (lplpaudioptr2)
901 *(LPBYTE*)lplpaudioptr2 = NULL;
902 if (audiobytes2)
903 *audiobytes2 = 0;
904 TRACE("->%d.0\n",writebytes);
905 } else {
906 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
907 *audiobytes1 = device->buflen-writecursor;
908 if (lplpaudioptr2)
909 *(LPBYTE*)lplpaudioptr2 = device->buffer;
910 if (audiobytes2)
911 *audiobytes2 = writebytes-(device->buflen-writecursor);
912 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
914 return DS_OK;
917 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
918 LPDIRECTSOUNDBUFFER iface,DWORD newpos
920 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
921 TRACE("(%p,%d)\n",This,newpos);
923 /* You cannot set the position of the primary buffer */
924 WARN("invalid call\n");
925 return DSERR_INVALIDCALL;
928 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
929 LPDIRECTSOUNDBUFFER iface,LONG pan
931 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
932 DirectSoundDevice *device = This->device;
933 float lvol, rvol;
934 HRESULT hr;
935 TRACE("(%p,%d)\n", iface, pan);
937 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
938 WARN("control unavailable\n");
939 return DSERR_CONTROLUNAVAIL;
942 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
943 WARN("invalid parameter: pan = %d\n", pan);
944 return DSERR_INVALIDPARAM;
947 /* **** */
948 EnterCriticalSection(&device->mixlock);
950 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
951 if(FAILED(hr)){
952 LeaveCriticalSection(&device->mixlock);
953 WARN("GetChannelVolume failed: %08x\n", hr);
954 return hr;
957 if(device->pwfx->nChannels > 1){
958 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
959 if(FAILED(hr)){
960 LeaveCriticalSection(&device->mixlock);
961 WARN("GetChannelVolume failed: %08x\n", hr);
962 return hr;
964 }else
965 rvol = 1;
967 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
968 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
970 DSOUND_AmpFactorToVolPan(&device->volpan);
971 if (pan != device->volpan.lPan) {
972 device->volpan.lPan=pan;
973 DSOUND_RecalcVolPan(&device->volpan);
975 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
976 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
977 if(FAILED(hr)){
978 LeaveCriticalSection(&device->mixlock);
979 WARN("SetChannelVolume failed: %08x\n", hr);
980 return hr;
983 if(device->pwfx->nChannels > 1){
984 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
985 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
986 if(FAILED(hr)){
987 LeaveCriticalSection(&device->mixlock);
988 WARN("SetChannelVolume failed: %08x\n", hr);
989 return hr;
994 LeaveCriticalSection(&device->mixlock);
995 /* **** */
997 return DS_OK;
1000 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
1001 LPDIRECTSOUNDBUFFER iface,LPLONG pan
1003 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1004 DirectSoundDevice *device = This->device;
1005 float lvol, rvol;
1006 HRESULT hr;
1007 TRACE("(%p,%p)\n", iface, pan);
1009 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1010 WARN("control unavailable\n");
1011 return DSERR_CONTROLUNAVAIL;
1014 if (pan == NULL) {
1015 WARN("invalid parameter: pan == NULL\n");
1016 return DSERR_INVALIDPARAM;
1019 EnterCriticalSection(&device->mixlock);
1021 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1022 if(FAILED(hr)){
1023 LeaveCriticalSection(&device->mixlock);
1024 WARN("GetChannelVolume failed: %08x\n", hr);
1025 return hr;
1028 if(device->pwfx->nChannels > 1){
1029 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1030 if(FAILED(hr)){
1031 LeaveCriticalSection(&device->mixlock);
1032 WARN("GetChannelVolume failed: %08x\n", hr);
1033 return hr;
1035 }else
1036 rvol = 1;
1038 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1039 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1041 DSOUND_AmpFactorToVolPan(&device->volpan);
1042 *pan = device->volpan.lPan;
1044 LeaveCriticalSection(&device->mixlock);
1046 return DS_OK;
1049 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
1050 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1052 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1053 DirectSoundDevice *device = This->device;
1054 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1056 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1057 WARN("failed priority check!\n");
1058 return DSERR_PRIOLEVELNEEDED;
1061 if((p1 && ((BYTE*)p1 < device->buffer ||
1062 (BYTE*)p1 >= device->buffer + device->buflen)) ||
1063 (p2 && ((BYTE*)p2 < device->buffer ||
1064 (BYTE*)p2 >= device->buffer + device->buflen)))
1065 return DSERR_INVALIDPARAM;
1067 return DS_OK;
1070 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1071 LPDIRECTSOUNDBUFFER iface
1073 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1074 FIXME("(%p):stub\n",This);
1075 return DS_OK;
1078 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1079 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1081 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1082 DirectSoundDevice *device = This->device;
1083 TRACE("(%p,%p)\n", iface, freq);
1085 if (freq == NULL) {
1086 WARN("invalid parameter: freq == NULL\n");
1087 return DSERR_INVALIDPARAM;
1090 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1091 WARN("control unavailable\n");
1092 return DSERR_CONTROLUNAVAIL;
1095 *freq = device->pwfx->nSamplesPerSec;
1096 TRACE("-> %d\n", *freq);
1098 return DS_OK;
1101 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1102 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1104 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1105 WARN("(%p) already initialized\n", This);
1106 return DSERR_ALREADYINITIALIZED;
1109 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1110 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1112 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1113 DirectSoundDevice *device = This->device;
1114 TRACE("(%p,%p)\n", iface, caps);
1116 if (caps == NULL) {
1117 WARN("invalid parameter: caps == NULL\n");
1118 return DSERR_INVALIDPARAM;
1121 if (caps->dwSize < sizeof(*caps)) {
1122 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1123 return DSERR_INVALIDPARAM;
1126 caps->dwFlags = This->dsbd.dwFlags;
1127 caps->dwBufferBytes = device->buflen;
1129 /* Windows reports these as zero */
1130 caps->dwUnlockTransferRate = 0;
1131 caps->dwPlayCpuOverhead = 0;
1133 return DS_OK;
1136 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1137 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1139 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1140 DirectSoundDevice *device = This->device;
1141 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1143 if (ppobj == NULL) {
1144 WARN("invalid parameter\n");
1145 return E_INVALIDARG;
1148 *ppobj = NULL; /* assume failure */
1150 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1151 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1152 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1153 *ppobj = This;
1154 return S_OK;
1157 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1158 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1159 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1160 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1161 return E_NOINTERFACE;
1164 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1165 ERR("app requested IDirectSoundNotify on primary buffer\n");
1166 /* FIXME: should we support this? */
1167 return E_NOINTERFACE;
1170 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1171 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1172 return E_NOINTERFACE;
1175 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1176 if (!device->listener)
1177 IDirectSound3DListenerImpl_Create(device, &device->listener);
1178 if (device->listener) {
1179 *ppobj = device->listener;
1180 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1181 return S_OK;
1184 WARN("IID_IDirectSound3DListener failed\n");
1185 return E_NOINTERFACE;
1188 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1189 FIXME("app requested IKsPropertySet on primary buffer\n");
1190 return E_NOINTERFACE;
1193 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1194 return E_NOINTERFACE;
1197 static const IDirectSoundBufferVtbl dspbvt =
1199 PrimaryBufferImpl_QueryInterface,
1200 PrimaryBufferImpl_AddRef,
1201 PrimaryBufferImpl_Release,
1202 PrimaryBufferImpl_GetCaps,
1203 PrimaryBufferImpl_GetCurrentPosition,
1204 PrimaryBufferImpl_GetFormat,
1205 PrimaryBufferImpl_GetVolume,
1206 PrimaryBufferImpl_GetPan,
1207 PrimaryBufferImpl_GetFrequency,
1208 PrimaryBufferImpl_GetStatus,
1209 PrimaryBufferImpl_Initialize,
1210 PrimaryBufferImpl_Lock,
1211 PrimaryBufferImpl_Play,
1212 PrimaryBufferImpl_SetCurrentPosition,
1213 PrimaryBufferImpl_SetFormat,
1214 PrimaryBufferImpl_SetVolume,
1215 PrimaryBufferImpl_SetPan,
1216 PrimaryBufferImpl_SetFrequency,
1217 PrimaryBufferImpl_Stop,
1218 PrimaryBufferImpl_Unlock,
1219 PrimaryBufferImpl_Restore
1222 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1223 const DSBUFFERDESC *dsbd)
1225 IDirectSoundBufferImpl *dsb;
1226 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1228 if (dsbd->lpwfxFormat) {
1229 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1230 *ppdsb = NULL;
1231 return DSERR_INVALIDPARAM;
1234 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1236 if (dsb == NULL) {
1237 WARN("out of memory\n");
1238 *ppdsb = NULL;
1239 return DSERR_OUTOFMEMORY;
1242 dsb->ref = 1;
1243 dsb->numIfaces = 1;
1244 dsb->device = device;
1245 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1246 dsb->dsbd = *dsbd;
1248 TRACE("Created primary buffer at %p\n", dsb);
1249 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1250 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1251 device->pwfx->wFormatTag, device->pwfx->nChannels,
1252 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1253 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1254 device->pwfx->cbSize);
1256 *ppdsb = dsb;
1257 return S_OK;