wined3d: Keep a reference to the resource in shader resource views.
[wine/multimedia.git] / dlls / dsound / primary.c
bloba8244777f8e66683016b3e6eb247fe0673c8969e
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 static DWORD DSOUND_fraglen(DirectSoundDevice *device)
45 REFERENCE_TIME period;
46 HRESULT hr;
47 DWORD ret;
49 hr = IAudioClient_GetDevicePeriod(device->client, &period, NULL);
50 if(FAILED(hr)){
51 /* just guess at 10ms */
52 WARN("GetDevicePeriod failed: %08x\n", hr);
53 ret = MulDiv(device->pwfx->nBlockAlign, device->pwfx->nSamplesPerSec, 100);
54 }else
55 ret = MulDiv(device->pwfx->nSamplesPerSec * device->pwfx->nBlockAlign, period, 10000000);
57 ret -= ret % device->pwfx->nBlockAlign;
58 return ret;
61 static HRESULT DSOUND_WaveFormat(DirectSoundDevice *device, IAudioClient *client,
62 BOOL forcewave, WAVEFORMATEX **wfx)
64 WAVEFORMATEXTENSIBLE *retwfe = NULL;
65 WAVEFORMATEX *w;
66 HRESULT hr;
68 if (!forcewave) {
69 WAVEFORMATEXTENSIBLE *mixwfe;
70 hr = IAudioClient_GetMixFormat(client, (WAVEFORMATEX**)&mixwfe);
72 if (FAILED(hr))
73 return hr;
75 if (mixwfe->Format.nChannels > 2) {
76 static int once;
77 if (!once++)
78 FIXME("Limiting channels to 2 due to lack of multichannel support\n");
80 mixwfe->Format.nChannels = 2;
81 mixwfe->Format.nBlockAlign = mixwfe->Format.nChannels * mixwfe->Format.wBitsPerSample / 8;
82 mixwfe->Format.nAvgBytesPerSec = mixwfe->Format.nSamplesPerSec * mixwfe->Format.nBlockAlign;
83 mixwfe->dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
86 if (!IsEqualGUID(&mixwfe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
87 WAVEFORMATEXTENSIBLE testwfe = *mixwfe;
89 testwfe.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
90 testwfe.Samples.wValidBitsPerSample = testwfe.Format.wBitsPerSample = 32;
91 testwfe.Format.nBlockAlign = testwfe.Format.nChannels * testwfe.Format.wBitsPerSample / 8;
92 testwfe.Format.nAvgBytesPerSec = testwfe.Format.nSamplesPerSec * testwfe.Format.nBlockAlign;
94 if (FAILED(IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, &testwfe.Format, (WAVEFORMATEX**)&retwfe)))
95 w = DSOUND_CopyFormat(&mixwfe->Format);
96 else if (retwfe)
97 w = DSOUND_CopyFormat(&retwfe->Format);
98 else
99 w = DSOUND_CopyFormat(&testwfe.Format);
100 CoTaskMemFree(retwfe);
101 retwfe = NULL;
102 } else
103 w = DSOUND_CopyFormat(&mixwfe->Format);
104 CoTaskMemFree(mixwfe);
105 } else if (device->primary_pwfx->wFormatTag == WAVE_FORMAT_PCM ||
106 device->primary_pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
107 WAVEFORMATEX *wi = device->primary_pwfx;
108 WAVEFORMATEXTENSIBLE *wfe;
110 /* Convert to WAVEFORMATEXTENSIBLE */
111 w = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE));
112 wfe = (WAVEFORMATEXTENSIBLE*)w;
113 if (!wfe)
114 return DSERR_OUTOFMEMORY;
116 wfe->Format = *wi;
117 w->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
118 w->cbSize = sizeof(*wfe) - sizeof(*w);
119 w->nBlockAlign = w->nChannels * w->wBitsPerSample / 8;
120 w->nAvgBytesPerSec = w->nSamplesPerSec * w->nBlockAlign;
122 wfe->dwChannelMask = 0;
123 if (wi->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
124 w->wBitsPerSample = 32;
125 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
126 } else
127 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
128 wfe->Samples.wValidBitsPerSample = w->wBitsPerSample;
129 } else
130 w = DSOUND_CopyFormat(device->primary_pwfx);
132 if (!w)
133 return DSERR_OUTOFMEMORY;
135 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, w, (WAVEFORMATEX**)&retwfe);
136 if (retwfe) {
137 memcpy(w, retwfe, sizeof(WAVEFORMATEX) + retwfe->Format.cbSize);
138 CoTaskMemFree(retwfe);
140 if (FAILED(hr)) {
141 WARN("IsFormatSupported failed: %08x\n", hr);
142 HeapFree(GetProcessHeap(), 0, w);
143 return hr;
145 *wfx = w;
146 return S_OK;
149 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
151 UINT prebuf_frames;
152 REFERENCE_TIME prebuf_rt;
153 WAVEFORMATEX *wfx = NULL;
154 HRESULT hres;
155 REFERENCE_TIME period;
156 DWORD period_ms;
158 TRACE("(%p, %d)\n", device, forcewave);
160 if(device->client){
161 IAudioClient_Release(device->client);
162 device->client = NULL;
164 if(device->render){
165 IAudioRenderClient_Release(device->render);
166 device->render = NULL;
168 if(device->clock){
169 IAudioClock_Release(device->clock);
170 device->clock = NULL;
172 if(device->volume){
173 IAudioStreamVolume_Release(device->volume);
174 device->volume = NULL;
177 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
178 CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
179 if(FAILED(hres)) {
180 WARN("Activate failed: %08x\n", hres);
181 return hres;
184 hres = DSOUND_WaveFormat(device, device->client, forcewave, &wfx);
185 if (FAILED(hres)) {
186 IAudioClient_Release(device->client);
187 device->client = NULL;
188 return hres;
190 HeapFree(GetProcessHeap(), 0, device->pwfx);
191 device->pwfx = wfx;
193 prebuf_frames = device->prebuf * DSOUND_fraglen(device) / device->pwfx->nBlockAlign;
194 prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
196 hres = IAudioClient_Initialize(device->client,
197 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST |
198 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, prebuf_rt, 0, device->pwfx, NULL);
199 if(FAILED(hres)){
200 IAudioClient_Release(device->client);
201 device->client = NULL;
202 WARN("Initialize failed: %08x\n", hres);
203 return hres;
205 IAudioClient_SetEventHandle(device->client, device->sleepev);
207 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
208 (void**)&device->render);
209 if(FAILED(hres)){
210 IAudioClient_Release(device->client);
211 device->client = NULL;
212 WARN("GetService failed: %08x\n", hres);
213 return hres;
216 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
217 (void**)&device->clock);
218 if(FAILED(hres)){
219 IAudioClient_Release(device->client);
220 IAudioRenderClient_Release(device->render);
221 device->client = NULL;
222 device->render = NULL;
223 WARN("GetService failed: %08x\n", hres);
224 return hres;
227 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
228 (void**)&device->volume);
229 if(FAILED(hres)){
230 IAudioClient_Release(device->client);
231 IAudioRenderClient_Release(device->render);
232 IAudioClock_Release(device->clock);
233 device->client = NULL;
234 device->render = NULL;
235 device->clock = NULL;
236 WARN("GetService failed: %08x\n", hres);
237 return hres;
240 /* Now kick off the timer so the event fires periodically */
241 hres = IAudioClient_Start(device->client);
242 if (FAILED(hres))
243 WARN("starting failed with %08x\n", hres);
245 hres = IAudioClient_GetStreamLatency(device->client, &period);
246 if (FAILED(hres)) {
247 WARN("GetStreamLatency failed with %08x\n", hres);
248 period_ms = 10;
249 } else
250 period_ms = (period + 9999) / 10000;
251 TRACE("period %u ms fraglen %u prebuf %u\n", period_ms, device->fraglen, device->prebuf);
253 if (period_ms < 3)
254 device->sleeptime = 5;
255 else
256 device->sleeptime = period_ms * 5 / 2;
258 return S_OK;
261 HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
263 IDirectSoundBufferImpl** dsb = device->buffers;
264 LPBYTE newbuf;
265 int i;
267 TRACE("(%p)\n", device);
269 device->fraglen = DSOUND_fraglen(device);
271 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
272 on windows this size is always fixed (tested on win-xp) */
273 if (!device->buflen)
274 device->buflen = ds_hel_buflen;
275 device->buflen -= device->buflen % device->pwfx->nBlockAlign;
276 while(device->buflen < device->fraglen * device->prebuf){
277 device->buflen += ds_hel_buflen;
278 device->buflen -= device->buflen % device->pwfx->nBlockAlign;
281 HeapFree(GetProcessHeap(), 0, device->mix_buffer);
282 device->mix_buffer_len = (device->buflen / (device->pwfx->wBitsPerSample / 8)) * sizeof(float);
283 device->mix_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device->mix_buffer_len);
284 if (!device->mix_buffer)
285 return DSERR_OUTOFMEMORY;
287 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
288 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
290 /* reallocate emulated primary buffer */
291 if (device->buffer)
292 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, device->buflen);
293 else
294 newbuf = HeapAlloc(GetProcessHeap(),0, device->buflen);
296 if (!newbuf) {
297 ERR("failed to allocate primary buffer\n");
298 return DSERR_OUTOFMEMORY;
299 /* but the old buffer might still exist and must be re-prepared */
302 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
304 device->buffer = newbuf;
306 TRACE("buflen: %u, fraglen: %u, mix_buffer_len: %u\n",
307 device->buflen, device->fraglen, device->mix_buffer_len);
309 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
310 (device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
311 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)device->pwfx)->SubFormat,
312 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)))
313 device->normfunction = normfunctions[4];
314 else
315 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
317 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
318 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
319 device->playpos = 0;
321 if (device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
322 (device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
323 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)device->pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)))
324 device->normfunction = normfunctions[4];
325 else
326 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
328 for (i = 0; i < device->nrofbuffers; i++) {
329 RtlAcquireResourceExclusive(&dsb[i]->lock, TRUE);
330 DSOUND_RecalcFormat(dsb[i]);
331 RtlReleaseResource(&dsb[i]->lock);
334 return DS_OK;
338 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
340 HRESULT hr;
342 TRACE("(%p)\n", device);
344 if(device->client){
345 hr = IAudioClient_Stop(device->client);
346 if(FAILED(hr))
347 WARN("Stop failed: %08x\n", hr);
350 /* clear the queue */
351 device->in_mmdev_bytes = 0;
354 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
356 HRESULT err = DS_OK;
357 TRACE("(%p)\n", device);
359 device->buflen = ds_hel_buflen;
360 err = DSOUND_PrimaryOpen(device);
362 if (err != DS_OK) {
363 WARN("DSOUND_PrimaryOpen failed\n");
364 return err;
367 device->state = STATE_STOPPED;
368 return DS_OK;
371 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
373 TRACE("(%p)\n", device);
375 /* **** */
376 EnterCriticalSection(&(device->mixlock));
378 DSOUND_PrimaryClose(device);
380 if(device->primary && (device->primary->ref || device->primary->numIfaces))
381 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
383 HeapFree(GetProcessHeap(), 0, device->primary);
384 device->primary = NULL;
386 HeapFree(GetProcessHeap(),0,device->primary_pwfx);
387 HeapFree(GetProcessHeap(),0,device->pwfx);
388 device->pwfx=NULL;
390 LeaveCriticalSection(&(device->mixlock));
391 /* **** */
393 return DS_OK;
396 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
398 HRESULT hr;
400 TRACE("(%p)\n", device);
402 hr = IAudioClient_Start(device->client);
403 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
404 WARN("Start failed: %08x\n", hr);
405 return hr;
408 return DS_OK;
411 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
413 HRESULT hr;
415 TRACE("(%p)\n", device);
417 hr = IAudioClient_Stop(device->client);
418 if(FAILED(hr)){
419 WARN("Stop failed: %08x\n", hr);
420 return hr;
423 return DS_OK;
426 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
428 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
430 /* check if playpos was requested */
431 if (playpos)
432 *playpos = device->playing_offs_bytes;
434 /* check if writepos was requested */
435 if (writepos)
436 /* the writepos is the first non-queued position */
437 *writepos = (device->playing_offs_bytes + device->in_mmdev_bytes) % device->buflen;
439 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
440 return DS_OK;
443 WAVEFORMATEX *DSOUND_CopyFormat(const WAVEFORMATEX *wfex)
445 WAVEFORMATEX *pwfx;
446 if(wfex->wFormatTag == WAVE_FORMAT_PCM){
447 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
448 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
449 pwfx->cbSize = 0;
450 }else{
451 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX) + wfex->cbSize);
452 CopyMemory(pwfx, wfex, sizeof(WAVEFORMATEX) + wfex->cbSize);
455 if(pwfx->wFormatTag == WAVE_FORMAT_PCM ||
456 (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
457 IsEqualGUID(&((const WAVEFORMATEXTENSIBLE*)pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))
458 pwfx->nBlockAlign = (pwfx->nChannels * pwfx->wBitsPerSample) / 8;
460 return pwfx;
463 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
465 HRESULT err = S_OK;
466 WAVEFORMATEX *old_fmt;
467 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
468 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
470 TRACE("(%p,%p)\n", device, passed_fmt);
472 if (device->priolevel == DSSCL_NORMAL) {
473 WARN("failed priority check!\n");
474 return DSERR_PRIOLEVELNEEDED;
477 /* Let's be pedantic! */
478 if (passed_fmt == NULL) {
479 WARN("invalid parameter: passed_fmt==NULL!\n");
480 return DSERR_INVALIDPARAM;
482 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
483 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
484 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
485 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
486 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
488 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
489 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
490 passed_fmt->nAvgBytesPerSec == 0 ||
491 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
492 return DSERR_INVALIDPARAM;
494 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
495 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
496 return DSERR_INVALIDPARAM;
499 /* **** */
500 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
501 EnterCriticalSection(&(device->mixlock));
503 if (device->priolevel == DSSCL_WRITEPRIMARY) {
504 old_fmt = device->primary_pwfx;
505 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
506 fmtex = (WAVEFORMATEXTENSIBLE *)device->primary_pwfx;
507 if (device->primary_pwfx == NULL) {
508 err = DSERR_OUTOFMEMORY;
509 goto out;
512 if (fmtex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
513 fmtex->Samples.wValidBitsPerSample == 0) {
514 TRACE("Correcting 0 valid bits per sample\n");
515 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
518 DSOUND_PrimaryClose(device);
520 err = DSOUND_ReopenDevice(device, forced);
521 if (FAILED(err)) {
522 ERR("No formats could be opened\n");
523 goto done;
526 err = DSOUND_PrimaryOpen(device);
527 if (err != DS_OK) {
528 ERR("DSOUND_PrimaryOpen failed\n");
529 goto done;
532 done:
533 if (err != DS_OK)
534 device->primary_pwfx = old_fmt;
535 else
536 HeapFree(GetProcessHeap(), 0, old_fmt);
537 } else {
538 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
539 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
542 out:
543 LeaveCriticalSection(&(device->mixlock));
544 RtlReleaseResource(&(device->buffer_list_lock));
545 /* **** */
547 return err;
550 /*******************************************************************************
551 * PrimaryBuffer
553 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
555 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
556 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
559 /* This sets this format for the primary buffer only */
560 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
561 const WAVEFORMATEX *wfex)
563 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
564 TRACE("(%p,%p)\n", iface, wfex);
565 return primarybuffer_SetFormat(This->device, wfex);
568 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
570 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
571 DirectSoundDevice *device = This->device;
572 HRESULT hr;
573 float lvol, rvol;
575 TRACE("(%p,%d)\n", iface, vol);
577 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
578 WARN("control unavailable\n");
579 return DSERR_CONTROLUNAVAIL;
582 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
583 WARN("invalid parameter: vol = %d\n", vol);
584 return DSERR_INVALIDPARAM;
587 /* **** */
588 EnterCriticalSection(&device->mixlock);
590 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
591 if(FAILED(hr)){
592 LeaveCriticalSection(&device->mixlock);
593 WARN("GetChannelVolume failed: %08x\n", hr);
594 return hr;
597 if(device->pwfx->nChannels > 1){
598 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
599 if(FAILED(hr)){
600 LeaveCriticalSection(&device->mixlock);
601 WARN("GetChannelVolume failed: %08x\n", hr);
602 return hr;
604 }else
605 rvol = 1;
607 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
608 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
610 DSOUND_AmpFactorToVolPan(&device->volpan);
611 if (vol != device->volpan.lVolume) {
612 device->volpan.lVolume=vol;
613 DSOUND_RecalcVolPan(&device->volpan);
614 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
615 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
616 if(FAILED(hr)){
617 LeaveCriticalSection(&device->mixlock);
618 WARN("SetChannelVolume failed: %08x\n", hr);
619 return hr;
622 if(device->pwfx->nChannels > 1){
623 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
624 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
625 if(FAILED(hr)){
626 LeaveCriticalSection(&device->mixlock);
627 WARN("SetChannelVolume failed: %08x\n", hr);
628 return hr;
633 LeaveCriticalSection(&(device->mixlock));
634 /* **** */
636 return DS_OK;
639 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
641 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
642 DirectSoundDevice *device = This->device;
643 float lvol, rvol;
644 HRESULT hr;
645 TRACE("(%p,%p)\n", iface, vol);
647 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
648 WARN("control unavailable\n");
649 return DSERR_CONTROLUNAVAIL;
652 if (vol == NULL) {
653 WARN("invalid parameter: vol = NULL\n");
654 return DSERR_INVALIDPARAM;
657 EnterCriticalSection(&device->mixlock);
659 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
660 if(FAILED(hr)){
661 LeaveCriticalSection(&device->mixlock);
662 WARN("GetChannelVolume failed: %08x\n", hr);
663 return hr;
666 if(device->pwfx->nChannels > 1){
667 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
668 if(FAILED(hr)){
669 LeaveCriticalSection(&device->mixlock);
670 WARN("GetChannelVolume failed: %08x\n", hr);
671 return hr;
673 }else
674 rvol = 1;
676 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
677 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
679 DSOUND_AmpFactorToVolPan(&device->volpan);
680 *vol = device->volpan.lVolume;
682 LeaveCriticalSection(&device->mixlock);
684 return DS_OK;
687 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
689 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
690 TRACE("(%p,%d)\n",This,freq);
692 /* You cannot set the frequency of the primary buffer */
693 WARN("control unavailable\n");
694 return DSERR_CONTROLUNAVAIL;
697 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
698 DWORD reserved2, DWORD flags)
700 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
701 DirectSoundDevice *device = This->device;
702 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
704 if (!(flags & DSBPLAY_LOOPING)) {
705 WARN("invalid parameter: flags = %08x\n", flags);
706 return DSERR_INVALIDPARAM;
709 /* **** */
710 EnterCriticalSection(&(device->mixlock));
712 if (device->state == STATE_STOPPED)
713 device->state = STATE_STARTING;
714 else if (device->state == STATE_STOPPING)
715 device->state = STATE_PLAYING;
717 LeaveCriticalSection(&(device->mixlock));
718 /* **** */
720 return DS_OK;
723 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
725 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
726 DirectSoundDevice *device = This->device;
727 TRACE("(%p)\n", iface);
729 /* **** */
730 EnterCriticalSection(&(device->mixlock));
732 if (device->state == STATE_PLAYING)
733 device->state = STATE_STOPPING;
734 else if (device->state == STATE_STARTING)
735 device->state = STATE_STOPPED;
737 LeaveCriticalSection(&(device->mixlock));
738 /* **** */
740 return DS_OK;
743 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
745 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
746 ULONG ref = InterlockedIncrement(&(This->ref));
747 TRACE("(%p) ref was %d\n", This, ref - 1);
748 if(ref == 1)
749 InterlockedIncrement(&This->numIfaces);
750 return ref;
753 /* Decreases *out by 1 to no less than 0.
754 * Returns the new value of *out. */
755 LONG capped_refcount_dec(LONG *out)
757 LONG ref, oldref;
758 do {
759 ref = *out;
760 if(!ref)
761 return 0;
762 oldref = InterlockedCompareExchange(out, ref - 1, ref);
763 } while(oldref != ref);
764 return ref - 1;
767 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
769 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
770 ULONG ref;
772 ref = capped_refcount_dec(&This->ref);
773 if(!ref)
774 capped_refcount_dec(&This->numIfaces);
776 TRACE("(%p) primary ref is now %d\n", This, ref);
778 return ref;
781 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
782 DWORD *playpos, DWORD *writepos)
784 HRESULT hres;
785 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
786 DirectSoundDevice *device = This->device;
787 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
789 /* **** */
790 EnterCriticalSection(&(device->mixlock));
792 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
793 if (hres != DS_OK) {
794 WARN("DSOUND_PrimaryGetPosition failed\n");
795 LeaveCriticalSection(&(device->mixlock));
796 return hres;
798 if (writepos) {
799 if (device->state != STATE_STOPPED)
800 /* apply the documented 10ms lead to writepos */
801 *writepos += device->writelead;
802 while (*writepos >= device->buflen) *writepos -= device->buflen;
805 LeaveCriticalSection(&(device->mixlock));
806 /* **** */
808 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
809 return DS_OK;
812 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
814 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
815 DirectSoundDevice *device = This->device;
816 TRACE("(%p,%p)\n", iface, status);
818 if (status == NULL) {
819 WARN("invalid parameter: status == NULL\n");
820 return DSERR_INVALIDPARAM;
823 *status = 0;
824 if ((device->state == STATE_STARTING) ||
825 (device->state == STATE_PLAYING))
826 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
828 TRACE("status=%x\n", *status);
829 return DS_OK;
833 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
834 DWORD wfsize, DWORD *wfwritten)
836 DWORD size;
837 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
838 DirectSoundDevice *device = This->device;
839 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
841 size = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
843 if (lpwf) { /* NULL is valid */
844 if (wfsize >= size) {
845 CopyMemory(lpwf,device->primary_pwfx,size);
846 if (wfwritten)
847 *wfwritten = size;
848 } else {
849 WARN("invalid parameter: wfsize too small\n");
850 if (wfwritten)
851 *wfwritten = 0;
852 return DSERR_INVALIDPARAM;
854 } else {
855 if (wfwritten)
856 *wfwritten = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
857 else {
858 WARN("invalid parameter: wfwritten == NULL\n");
859 return DSERR_INVALIDPARAM;
863 return DS_OK;
866 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
867 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
868 DWORD *audiobytes2, DWORD flags)
870 HRESULT hres;
871 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
872 DirectSoundDevice *device = This->device;
873 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
874 iface,
875 writecursor,
876 writebytes,
877 lplpaudioptr1,
878 audiobytes1,
879 lplpaudioptr2,
880 audiobytes2,
881 flags,
882 GetTickCount()
885 if (!audiobytes1)
886 return DSERR_INVALIDPARAM;
888 if (device->priolevel != DSSCL_WRITEPRIMARY) {
889 WARN("failed priority check!\n");
890 return DSERR_PRIOLEVELNEEDED;
893 /* when this flag is set, writecursor is meaningless and must be calculated */
894 if (flags & DSBLOCK_FROMWRITECURSOR) {
895 /* GetCurrentPosition does too much magic to duplicate here */
896 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
897 if (hres != DS_OK) {
898 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
899 return hres;
903 /* when this flag is set, writebytes is meaningless and must be set */
904 if (flags & DSBLOCK_ENTIREBUFFER)
905 writebytes = device->buflen;
907 if (writecursor >= device->buflen) {
908 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
909 writecursor, device->buflen);
910 return DSERR_INVALIDPARAM;
913 if (writebytes > device->buflen) {
914 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
915 writebytes, device->buflen);
916 return DSERR_INVALIDPARAM;
919 if (writecursor+writebytes <= device->buflen) {
920 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
921 *audiobytes1 = writebytes;
922 if (lplpaudioptr2)
923 *(LPBYTE*)lplpaudioptr2 = NULL;
924 if (audiobytes2)
925 *audiobytes2 = 0;
926 TRACE("->%d.0\n",writebytes);
927 } else {
928 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
929 *audiobytes1 = device->buflen-writecursor;
930 if (lplpaudioptr2)
931 *(LPBYTE*)lplpaudioptr2 = device->buffer;
932 if (audiobytes2)
933 *audiobytes2 = writebytes-(device->buflen-writecursor);
934 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
936 return DS_OK;
939 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
941 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
942 TRACE("(%p,%d)\n",This,newpos);
944 /* You cannot set the position of the primary buffer */
945 WARN("invalid call\n");
946 return DSERR_INVALIDCALL;
949 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
951 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
952 DirectSoundDevice *device = This->device;
953 float lvol, rvol;
954 HRESULT hr;
955 TRACE("(%p,%d)\n", iface, pan);
957 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
958 WARN("control unavailable\n");
959 return DSERR_CONTROLUNAVAIL;
962 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
963 WARN("invalid parameter: pan = %d\n", pan);
964 return DSERR_INVALIDPARAM;
967 /* **** */
968 EnterCriticalSection(&device->mixlock);
970 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
971 if(FAILED(hr)){
972 LeaveCriticalSection(&device->mixlock);
973 WARN("GetChannelVolume failed: %08x\n", hr);
974 return hr;
977 if(device->pwfx->nChannels > 1){
978 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
979 if(FAILED(hr)){
980 LeaveCriticalSection(&device->mixlock);
981 WARN("GetChannelVolume failed: %08x\n", hr);
982 return hr;
984 }else
985 rvol = 1;
987 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
988 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
990 DSOUND_AmpFactorToVolPan(&device->volpan);
991 if (pan != device->volpan.lPan) {
992 device->volpan.lPan=pan;
993 DSOUND_RecalcVolPan(&device->volpan);
995 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
996 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
997 if(FAILED(hr)){
998 LeaveCriticalSection(&device->mixlock);
999 WARN("SetChannelVolume failed: %08x\n", hr);
1000 return hr;
1003 if(device->pwfx->nChannels > 1){
1004 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
1005 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
1006 if(FAILED(hr)){
1007 LeaveCriticalSection(&device->mixlock);
1008 WARN("SetChannelVolume failed: %08x\n", hr);
1009 return hr;
1014 LeaveCriticalSection(&device->mixlock);
1015 /* **** */
1017 return DS_OK;
1020 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1022 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1023 DirectSoundDevice *device = This->device;
1024 float lvol, rvol;
1025 HRESULT hr;
1026 TRACE("(%p,%p)\n", iface, pan);
1028 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1029 WARN("control unavailable\n");
1030 return DSERR_CONTROLUNAVAIL;
1033 if (pan == NULL) {
1034 WARN("invalid parameter: pan == NULL\n");
1035 return DSERR_INVALIDPARAM;
1038 EnterCriticalSection(&device->mixlock);
1040 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1041 if(FAILED(hr)){
1042 LeaveCriticalSection(&device->mixlock);
1043 WARN("GetChannelVolume failed: %08x\n", hr);
1044 return hr;
1047 if(device->pwfx->nChannels > 1){
1048 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1049 if(FAILED(hr)){
1050 LeaveCriticalSection(&device->mixlock);
1051 WARN("GetChannelVolume failed: %08x\n", hr);
1052 return hr;
1054 }else
1055 rvol = 1;
1057 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1058 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1060 DSOUND_AmpFactorToVolPan(&device->volpan);
1061 *pan = device->volpan.lPan;
1063 LeaveCriticalSection(&device->mixlock);
1065 return DS_OK;
1068 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1069 void *p2, DWORD x2)
1071 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1072 DirectSoundDevice *device = This->device;
1073 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1075 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1076 WARN("failed priority check!\n");
1077 return DSERR_PRIOLEVELNEEDED;
1080 if ((p1 && ((BYTE*)p1 < device->buffer || (BYTE*)p1 >= device->buffer + device->buflen)) ||
1081 (p2 && ((BYTE*)p2 < device->buffer || (BYTE*)p2 >= device->buffer + device->buflen)))
1082 return DSERR_INVALIDPARAM;
1084 return DS_OK;
1087 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1089 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1090 FIXME("(%p):stub\n",This);
1091 return DS_OK;
1094 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *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 (!(This->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(IDirectSoundBuffer *iface, IDirectSound *dsound,
1117 const DSBUFFERDESC *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(IDirectSoundBuffer *iface, DSBCAPS *caps)
1126 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1127 DirectSoundDevice *device = This->device;
1128 TRACE("(%p,%p)\n", iface, caps);
1130 if (caps == NULL) {
1131 WARN("invalid parameter: caps == NULL\n");
1132 return DSERR_INVALIDPARAM;
1135 if (caps->dwSize < sizeof(*caps)) {
1136 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1137 return DSERR_INVALIDPARAM;
1140 caps->dwFlags = This->dsbd.dwFlags;
1141 caps->dwBufferBytes = device->buflen;
1143 /* Windows reports these as zero */
1144 caps->dwUnlockTransferRate = 0;
1145 caps->dwPlayCpuOverhead = 0;
1147 return DS_OK;
1150 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1151 void **ppobj)
1153 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1155 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1157 if (ppobj == NULL) {
1158 WARN("invalid parameter\n");
1159 return E_INVALIDARG;
1162 *ppobj = NULL; /* assume failure */
1164 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1165 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1166 IDirectSoundBuffer_AddRef(iface);
1167 *ppobj = iface;
1168 return S_OK;
1171 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1172 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1173 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1174 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1175 return E_NOINTERFACE;
1178 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1179 ERR("app requested IDirectSoundNotify on primary buffer\n");
1180 /* FIXME: should we support this? */
1181 return E_NOINTERFACE;
1184 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1185 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1186 return E_NOINTERFACE;
1189 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1190 *ppobj = &This->IDirectSound3DListener_iface;
1191 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1192 return S_OK;
1195 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1196 *ppobj = &This->IKsPropertySet_iface;
1197 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1198 return S_OK;
1201 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1202 return E_NOINTERFACE;
1205 static const IDirectSoundBufferVtbl dspbvt =
1207 PrimaryBufferImpl_QueryInterface,
1208 PrimaryBufferImpl_AddRef,
1209 PrimaryBufferImpl_Release,
1210 PrimaryBufferImpl_GetCaps,
1211 PrimaryBufferImpl_GetCurrentPosition,
1212 PrimaryBufferImpl_GetFormat,
1213 PrimaryBufferImpl_GetVolume,
1214 PrimaryBufferImpl_GetPan,
1215 PrimaryBufferImpl_GetFrequency,
1216 PrimaryBufferImpl_GetStatus,
1217 PrimaryBufferImpl_Initialize,
1218 PrimaryBufferImpl_Lock,
1219 PrimaryBufferImpl_Play,
1220 PrimaryBufferImpl_SetCurrentPosition,
1221 PrimaryBufferImpl_SetFormat,
1222 PrimaryBufferImpl_SetVolume,
1223 PrimaryBufferImpl_SetPan,
1224 PrimaryBufferImpl_SetFrequency,
1225 PrimaryBufferImpl_Stop,
1226 PrimaryBufferImpl_Unlock,
1227 PrimaryBufferImpl_Restore
1230 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1231 const DSBUFFERDESC *dsbd)
1233 IDirectSoundBufferImpl *dsb;
1234 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1236 if (dsbd->lpwfxFormat) {
1237 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1238 *ppdsb = NULL;
1239 return DSERR_INVALIDPARAM;
1242 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1244 if (dsb == NULL) {
1245 WARN("out of memory\n");
1246 *ppdsb = NULL;
1247 return DSERR_OUTOFMEMORY;
1250 dsb->ref = 0;
1251 dsb->ref3D = 0;
1252 dsb->refiks = 0;
1253 dsb->numIfaces = 0;
1254 dsb->device = device;
1255 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1256 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1257 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1258 dsb->dsbd = *dsbd;
1260 /* IDirectSound3DListener */
1261 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1262 device->ds3dl.vPosition.x = 0.0;
1263 device->ds3dl.vPosition.y = 0.0;
1264 device->ds3dl.vPosition.z = 0.0;
1265 device->ds3dl.vVelocity.x = 0.0;
1266 device->ds3dl.vVelocity.y = 0.0;
1267 device->ds3dl.vVelocity.z = 0.0;
1268 device->ds3dl.vOrientFront.x = 0.0;
1269 device->ds3dl.vOrientFront.y = 0.0;
1270 device->ds3dl.vOrientFront.z = 1.0;
1271 device->ds3dl.vOrientTop.x = 0.0;
1272 device->ds3dl.vOrientTop.y = 1.0;
1273 device->ds3dl.vOrientTop.z = 0.0;
1274 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1275 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1276 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1277 device->ds3dl_need_recalc = TRUE;
1279 TRACE("Created primary buffer at %p\n", dsb);
1280 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1281 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1282 device->pwfx->wFormatTag, device->pwfx->nChannels,
1283 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1284 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1285 device->pwfx->cbSize);
1287 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1288 *ppdsb = dsb;
1289 return S_OK;