dsound: fix format handling on invalid format to never fail
[wine/multimedia.git] / dlls / dsound / primary.c
blob4bfae7e992fba03fb7192feecebf8ee9029d59c8
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 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 speaker_config_to_channel_mask(DWORD speaker_config)
45 switch (DSSPEAKER_CONFIG(speaker_config)) {
46 case DSSPEAKER_MONO:
47 return SPEAKER_FRONT_LEFT;
49 case DSSPEAKER_STEREO:
50 case DSSPEAKER_HEADPHONE:
51 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
53 case DSSPEAKER_QUAD:
54 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
56 case DSSPEAKER_5POINT1_BACK:
57 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
60 WARN("unknown speaker_config %u\n", speaker_config);
61 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
64 static DWORD DSOUND_FindSpeakerConfig(IMMDevice *mmdevice, int channels)
66 IPropertyStore *store;
67 HRESULT hr;
68 PROPVARIANT pv;
69 ULONG phys_speakers;
71 const DWORD def = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, DSSPEAKER_GEOMETRY_WIDE);
73 hr = IMMDevice_OpenPropertyStore(mmdevice, STGM_READ, &store);
74 if (FAILED(hr)) {
75 WARN("IMMDevice_OpenPropertyStore failed: %08x\n", hr);
76 return def;
79 hr = IPropertyStore_GetValue(store, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv);
81 if (FAILED(hr)) {
82 WARN("IPropertyStore_GetValue failed: %08x\n", hr);
83 IPropertyStore_Release(store);
84 return def;
87 if (pv.vt != VT_UI4) {
88 WARN("PKEY_AudioEndpoint_PhysicalSpeakers is not a ULONG: 0x%x\n", pv.vt);
89 PropVariantClear(&pv);
90 IPropertyStore_Release(store);
91 return def;
94 phys_speakers = pv.u.ulVal;
96 PropVariantClear(&pv);
97 IPropertyStore_Release(store);
99 if ((channels >= 6 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_5POINT1) == KSAUDIO_SPEAKER_5POINT1)
100 return DSSPEAKER_5POINT1_BACK;
101 else if ((channels >= 6 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_5POINT1_SURROUND) == KSAUDIO_SPEAKER_5POINT1_SURROUND)
102 return DSSPEAKER_5POINT1_SURROUND;
103 else if ((channels >= 4 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_QUAD) == KSAUDIO_SPEAKER_QUAD)
104 return DSSPEAKER_QUAD;
105 else if ((channels >= 2 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_STEREO) == KSAUDIO_SPEAKER_STEREO)
106 return DSSPEAKER_COMBINED(DSSPEAKER_STEREO, DSSPEAKER_GEOMETRY_WIDE);
107 else if ((phys_speakers & KSAUDIO_SPEAKER_MONO) == KSAUDIO_SPEAKER_MONO)
108 return DSSPEAKER_MONO;
110 return def;
113 static HRESULT DSOUND_WaveFormat(DirectSoundDevice *device, IAudioClient *client,
114 BOOL forcewave, WAVEFORMATEX **wfx)
116 WAVEFORMATEXTENSIBLE *retwfe = NULL;
117 WAVEFORMATEX *w;
118 HRESULT hr;
120 if (!forcewave) {
121 WAVEFORMATEXTENSIBLE *mixwfe;
122 hr = IAudioClient_GetMixFormat(client, (WAVEFORMATEX**)&mixwfe);
124 if (FAILED(hr))
125 return hr;
127 if (mixwfe->Format.nChannels < device->num_speakers) {
128 device->speaker_config = DSOUND_FindSpeakerConfig(device->mmdevice, mixwfe->Format.nChannels);
129 DSOUND_ParseSpeakerConfig(device);
130 } else if (mixwfe->Format.nChannels > device->num_speakers) {
131 mixwfe->Format.nChannels = device->num_speakers;
132 mixwfe->Format.nBlockAlign = mixwfe->Format.nChannels * mixwfe->Format.wBitsPerSample / 8;
133 mixwfe->Format.nAvgBytesPerSec = mixwfe->Format.nSamplesPerSec * mixwfe->Format.nBlockAlign;
134 mixwfe->dwChannelMask = speaker_config_to_channel_mask(device->speaker_config);
137 if (!IsEqualGUID(&mixwfe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
138 WAVEFORMATEXTENSIBLE testwfe = *mixwfe;
140 testwfe.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
141 testwfe.Samples.wValidBitsPerSample = testwfe.Format.wBitsPerSample = 32;
142 testwfe.Format.nBlockAlign = testwfe.Format.nChannels * testwfe.Format.wBitsPerSample / 8;
143 testwfe.Format.nAvgBytesPerSec = testwfe.Format.nSamplesPerSec * testwfe.Format.nBlockAlign;
145 if (FAILED(IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, &testwfe.Format, (WAVEFORMATEX**)&retwfe)))
146 w = DSOUND_CopyFormat(&mixwfe->Format);
147 else if (retwfe)
148 w = DSOUND_CopyFormat(&retwfe->Format);
149 else
150 w = DSOUND_CopyFormat(&testwfe.Format);
151 CoTaskMemFree(retwfe);
152 retwfe = NULL;
153 } else
154 w = DSOUND_CopyFormat(&mixwfe->Format);
155 CoTaskMemFree(mixwfe);
156 } else if (device->primary_pwfx->wFormatTag == WAVE_FORMAT_PCM ||
157 device->primary_pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
158 WAVEFORMATEX *wi = device->primary_pwfx;
159 WAVEFORMATEXTENSIBLE *wfe;
161 /* Convert to WAVEFORMATEXTENSIBLE */
162 w = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE));
163 wfe = (WAVEFORMATEXTENSIBLE*)w;
164 if (!wfe)
165 return DSERR_OUTOFMEMORY;
167 wfe->Format = *wi;
168 w->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
169 w->cbSize = sizeof(*wfe) - sizeof(*w);
170 w->nBlockAlign = w->nChannels * w->wBitsPerSample / 8;
171 w->nAvgBytesPerSec = w->nSamplesPerSec * w->nBlockAlign;
173 wfe->dwChannelMask = 0;
174 if (wi->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
175 w->wBitsPerSample = 32;
176 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
177 } else
178 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
179 wfe->Samples.wValidBitsPerSample = w->wBitsPerSample;
180 } else
181 w = DSOUND_CopyFormat(device->primary_pwfx);
183 if (!w)
184 return DSERR_OUTOFMEMORY;
186 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, w, (WAVEFORMATEX**)&retwfe);
187 if (retwfe) {
188 memcpy(w, retwfe, sizeof(WAVEFORMATEX) + retwfe->Format.cbSize);
189 CoTaskMemFree(retwfe);
191 if (FAILED(hr)) {
192 WARN("IsFormatSupported failed: %08x\n", hr);
193 HeapFree(GetProcessHeap(), 0, w);
194 return hr;
196 *wfx = w;
197 return S_OK;
200 static void DSOUND_ReleaseDevice(DirectSoundDevice *device)
202 if(device->client){
203 IAudioClient_Release(device->client);
204 device->client = NULL;
206 if(device->render){
207 IAudioRenderClient_Release(device->render);
208 device->render = NULL;
210 if(device->volume){
211 IAudioStreamVolume_Release(device->volume);
212 device->volume = NULL;
215 if (device->pad) {
216 device->playpos += device->pad;
217 device->playpos %= device->buflen;
218 device->pad = 0;
222 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device, WAVEFORMATEX *wfx, DWORD aclen, BOOL forcewave)
224 IDirectSoundBufferImpl** dsb = device->buffers;
225 LPBYTE newbuf;
226 DWORD new_buflen;
227 BOOL mixfloat = FALSE;
228 int i;
230 TRACE("(%p)\n", device);
232 new_buflen = device->buflen;
233 new_buflen -= new_buflen % wfx->nBlockAlign;
235 if (wfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
236 (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
237 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)wfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)))
238 mixfloat = TRUE;
240 /* reallocate emulated primary buffer */
241 if (forcewave) {
242 if (device->buffer)
243 newbuf = HeapReAlloc(GetProcessHeap(), 0, device->buffer, new_buflen);
244 else
245 newbuf = HeapAlloc(GetProcessHeap(), 0, new_buflen);
247 if (!newbuf) {
248 ERR("failed to allocate primary buffer\n");
249 return DSERR_OUTOFMEMORY;
251 device->mix_buffer_len = 0;
252 } else if (!mixfloat) {
253 DWORD alloc_len = aclen / (wfx->nBlockAlign / 8) * sizeof(float);
255 if (device->buffer)
256 newbuf = HeapReAlloc(GetProcessHeap(), 0, device->buffer, alloc_len);
257 else
258 newbuf = HeapAlloc(GetProcessHeap(), 0, alloc_len);
260 if (!newbuf) {
261 ERR("failed to allocate primary buffer\n");
262 return DSERR_OUTOFMEMORY;
264 device->mix_buffer_len = alloc_len;
265 } else {
266 HeapFree(GetProcessHeap(), 0, device->buffer);
267 newbuf = NULL;
268 device->mix_buffer_len = 0;
271 device->buffer = newbuf;
272 device->buflen = new_buflen;
273 HeapFree(GetProcessHeap(), 0, device->pwfx);
274 device->pwfx = wfx;
276 if (device->state == STATE_PLAYING)
277 device->state = STATE_STARTING;
278 else if (device->state == STATE_STOPPING)
279 device->state = STATE_STOPPED;
281 device->writelead = (wfx->nSamplesPerSec / 100) * wfx->nBlockAlign;
283 TRACE("buflen: %u, fraglen: %u, mix_buffer_len: %u\n",
284 device->buflen, device->fraglen, device->mix_buffer_len);
286 if (!forcewave && !mixfloat)
287 device->normfunction = normfunctions[wfx->nBlockAlign/8 - 1];
288 else
289 device->normfunction = NULL;
291 if (device->mix_buffer_len)
292 FillMemory(device->buffer, device->mix_buffer_len, 0);
293 else if (device->buffer)
294 FillMemory(device->buffer, device->buflen, (wfx->wBitsPerSample == 8) ? 128 : 0);
295 device->playpos = 0;
297 for (i = 0; i < device->nrofbuffers; i++) {
298 RtlAcquireResourceExclusive(&dsb[i]->lock, TRUE);
299 DSOUND_RecalcFormat(dsb[i]);
300 RtlReleaseResource(&dsb[i]->lock);
303 return DS_OK;
306 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
308 HRESULT hres;
309 REFERENCE_TIME period;
310 UINT32 frames;
311 DWORD period_ms;
312 IAudioClient *client = NULL;
313 IAudioRenderClient *render = NULL;
314 IAudioStreamVolume *volume = NULL;
315 DWORD fraglen, aclen;
316 WAVEFORMATEX *wfx = NULL;
317 DWORD oldspeakerconfig = device->speaker_config;
319 TRACE("(%p, %d)\n", device, forcewave);
321 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
322 CLSCTX_INPROC_SERVER, NULL, (void **)&client);
323 if(FAILED(hres)){
324 WARN("Activate failed: %08x\n", hres);
325 return hres;
328 hres = DSOUND_WaveFormat(device, client, forcewave, &wfx);
329 if (FAILED(hres)) {
330 IAudioClient_Release(client);
331 return hres;
334 hres = IAudioClient_Initialize(client,
335 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST |
336 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 800000, 0, wfx, NULL);
337 if(FAILED(hres)){
338 IAudioClient_Release(client);
339 ERR("Initialize failed: %08x\n", hres);
340 return hres;
343 IAudioClient_SetEventHandle(client, device->sleepev);
345 hres = IAudioClient_GetService(client, &IID_IAudioRenderClient, (void**)&render);
346 if(FAILED(hres))
347 goto err_service;
349 hres = IAudioClient_GetService(client, &IID_IAudioStreamVolume, (void**)&volume);
350 if(FAILED(hres))
351 goto err_service;
353 /* Now kick off the timer so the event fires periodically */
354 hres = IAudioClient_Start(client);
355 if (FAILED(hres)) {
356 WARN("Start failed with %08x\n", hres);
357 goto err;
359 hres = IAudioClient_GetStreamLatency(client, &period);
360 if (FAILED(hres)) {
361 WARN("GetStreamLatency failed with %08x\n", hres);
362 goto err;
364 hres = IAudioClient_GetBufferSize(client, &frames);
365 if (FAILED(hres)) {
366 WARN("GetBufferSize failed with %08x\n", hres);
367 goto err;
370 period_ms = (period + 9999) / 10000;
371 fraglen = MulDiv(wfx->nSamplesPerSec, period, 10000000) * wfx->nBlockAlign;
372 aclen = frames * wfx->nBlockAlign;
373 TRACE("period %u ms fraglen %u buflen %u\n", period_ms, fraglen, aclen);
375 hres = DSOUND_PrimaryOpen(device, wfx, aclen, forcewave);
376 if(FAILED(hres))
377 goto err;
379 DSOUND_ReleaseDevice(device);
380 device->client = client;
381 device->render = render;
382 device->volume = volume;
383 device->fraglen = fraglen;
384 device->aclen = aclen;
386 if (period_ms < 3)
387 device->sleeptime = 5;
388 else
389 device->sleeptime = period_ms * 5 / 2;
391 return S_OK;
393 err_service:
394 ERR("GetService failed: %08x\n", hres);
395 err:
396 device->speaker_config = oldspeakerconfig;
397 DSOUND_ParseSpeakerConfig(device);
398 if (volume)
399 IAudioStreamVolume_Release(volume);
400 if (render)
401 IAudioRenderClient_Release(render);
402 if (client)
403 IAudioClient_Release(client);
404 HeapFree(GetProcessHeap(), 0, wfx);
405 return hres;
408 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
410 TRACE("(%p)\n", device);
412 /* **** */
413 EnterCriticalSection(&(device->mixlock));
415 if(device->primary && (device->primary->ref || device->primary->numIfaces))
416 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
418 HeapFree(GetProcessHeap(), 0, device->primary);
419 device->primary = NULL;
421 HeapFree(GetProcessHeap(),0,device->primary_pwfx);
422 HeapFree(GetProcessHeap(),0,device->pwfx);
423 device->pwfx=NULL;
425 LeaveCriticalSection(&(device->mixlock));
426 /* **** */
428 return DS_OK;
431 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
433 HRESULT hr;
435 TRACE("(%p)\n", device);
437 hr = IAudioClient_Start(device->client);
438 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
439 WARN("Start failed: %08x\n", hr);
440 return hr;
443 return DS_OK;
446 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
448 HRESULT hr;
450 TRACE("(%p)\n", device);
452 hr = IAudioClient_Stop(device->client);
453 if(FAILED(hr)){
454 WARN("Stop failed: %08x\n", hr);
455 return hr;
458 return DS_OK;
461 WAVEFORMATEX *DSOUND_CopyFormat(const WAVEFORMATEX *wfex)
463 WAVEFORMATEX *pwfx;
464 if(wfex->wFormatTag == WAVE_FORMAT_PCM){
465 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
466 if (!pwfx)
467 return NULL;
468 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
469 pwfx->cbSize = 0;
470 }else{
471 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX) + wfex->cbSize);
472 if (!pwfx)
473 return NULL;
474 CopyMemory(pwfx, wfex, sizeof(WAVEFORMATEX) + wfex->cbSize);
477 if(pwfx->wFormatTag == WAVE_FORMAT_PCM ||
478 (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
479 IsEqualGUID(&((const WAVEFORMATEXTENSIBLE*)pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))
480 pwfx->nBlockAlign = (pwfx->nChannels * pwfx->wBitsPerSample) / 8;
482 return pwfx;
485 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
487 HRESULT err = S_OK;
488 WAVEFORMATEX *old_fmt;
489 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
491 TRACE("(%p,%p)\n", device, passed_fmt);
493 if (device->priolevel == DSSCL_NORMAL) {
494 WARN("failed priority check!\n");
495 return DSERR_PRIOLEVELNEEDED;
498 /* Let's be pedantic! */
499 if (passed_fmt == NULL) {
500 WARN("invalid parameter: passed_fmt==NULL!\n");
501 return DSERR_INVALIDPARAM;
503 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
504 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
505 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
506 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
507 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
509 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
510 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
511 passed_fmt->nAvgBytesPerSec == 0 ||
512 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
513 return DSERR_INVALIDPARAM;
515 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
516 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
517 return DSERR_INVALIDPARAM;
520 /* **** */
521 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
522 EnterCriticalSection(&(device->mixlock));
524 if (device->priolevel == DSSCL_WRITEPRIMARY) {
525 old_fmt = device->primary_pwfx;
526 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
527 fmtex = (WAVEFORMATEXTENSIBLE *)device->primary_pwfx;
528 if (device->primary_pwfx == NULL) {
529 err = DSERR_OUTOFMEMORY;
530 goto out;
533 if (fmtex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
534 fmtex->Samples.wValidBitsPerSample == 0) {
535 TRACE("Correcting 0 valid bits per sample\n");
536 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
539 err = DSOUND_ReopenDevice(device, TRUE);
540 if (FAILED(err)) {
541 ERR("No formats could be opened\n");
542 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
543 device->primary_pwfx = old_fmt;
544 } else
545 HeapFree(GetProcessHeap(), 0, old_fmt);
546 } else {
547 WAVEFORMATEX *wfx = DSOUND_CopyFormat(passed_fmt);
548 if (wfx) {
549 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
550 device->primary_pwfx = wfx;
551 } else
552 err = DSERR_OUTOFMEMORY;
555 out:
556 LeaveCriticalSection(&(device->mixlock));
557 RtlReleaseResource(&(device->buffer_list_lock));
558 /* **** */
560 return err;
563 /*******************************************************************************
564 * PrimaryBuffer
566 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
568 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
569 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
572 /* This sets this format for the primary buffer only */
573 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
574 const WAVEFORMATEX *wfex)
576 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
577 TRACE("(%p,%p)\n", iface, wfex);
578 return primarybuffer_SetFormat(This->device, wfex);
581 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
583 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
584 DirectSoundDevice *device = This->device;
585 HRESULT hr;
586 float fvol;
587 int i;
589 TRACE("(%p,%d)\n", iface, vol);
591 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
592 WARN("control unavailable\n");
593 return DSERR_CONTROLUNAVAIL;
596 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
597 WARN("invalid parameter: vol = %d\n", vol);
598 return DSERR_INVALIDPARAM;
601 /* **** */
602 EnterCriticalSection(&device->mixlock);
604 for (i = 0; i < DS_MAX_CHANNELS; i++) {
605 if (device->pwfx->nChannels > i){
606 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
607 if (FAILED(hr)){
608 LeaveCriticalSection(&device->mixlock);
609 WARN("GetChannelVolume failed: %08x\n", hr);
610 return hr;
612 } else
613 fvol=1.0f;
615 device->volpan.dwTotalAmpFactor[i]=((UINT16)(fvol * (DWORD)0xFFFF));
618 DSOUND_AmpFactorToVolPan(&device->volpan);
619 if (vol != device->volpan.lVolume) {
620 device->volpan.lVolume=vol;
621 DSOUND_RecalcVolPan(&device->volpan);
623 for (i = 0; i < DS_MAX_CHANNELS; i++) {
624 if (device->pwfx->nChannels > i){
625 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
626 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
627 if (FAILED(hr)){
628 LeaveCriticalSection(&device->mixlock);
629 WARN("SetChannelVolume failed: %08x\n", hr);
630 return hr;
636 LeaveCriticalSection(&(device->mixlock));
637 /* **** */
639 return DS_OK;
642 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
644 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
645 DirectSoundDevice *device = This->device;
646 float fvol;
647 HRESULT hr;
648 int i;
650 TRACE("(%p,%p)\n", iface, vol);
652 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
653 WARN("control unavailable\n");
654 return DSERR_CONTROLUNAVAIL;
657 if (vol == NULL) {
658 WARN("invalid parameter: vol = NULL\n");
659 return DSERR_INVALIDPARAM;
662 EnterCriticalSection(&device->mixlock);
664 for (i = 0; i < DS_MAX_CHANNELS; i++) {
665 if (device->pwfx->nChannels > i){
666 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
667 if (FAILED(hr)){
668 LeaveCriticalSection(&device->mixlock);
669 WARN("GetChannelVolume failed: %08x\n", hr);
670 return hr;
672 } else
673 fvol = 1;
675 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
678 DSOUND_AmpFactorToVolPan(&device->volpan);
679 *vol = device->volpan.lVolume;
681 LeaveCriticalSection(&device->mixlock);
683 return DS_OK;
686 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
688 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
689 TRACE("(%p,%d)\n",This,freq);
691 /* You cannot set the frequency of the primary buffer */
692 WARN("control unavailable\n");
693 return DSERR_CONTROLUNAVAIL;
696 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
697 DWORD reserved2, DWORD flags)
699 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
700 DirectSoundDevice *device = This->device;
701 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
703 if (!(flags & DSBPLAY_LOOPING)) {
704 WARN("invalid parameter: flags = %08x\n", flags);
705 return DSERR_INVALIDPARAM;
708 /* **** */
709 EnterCriticalSection(&(device->mixlock));
711 if (device->state == STATE_STOPPED)
712 device->state = STATE_STARTING;
713 else if (device->state == STATE_STOPPING)
714 device->state = STATE_PLAYING;
716 LeaveCriticalSection(&(device->mixlock));
717 /* **** */
719 return DS_OK;
722 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
724 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
725 DirectSoundDevice *device = This->device;
726 TRACE("(%p)\n", iface);
728 /* **** */
729 EnterCriticalSection(&(device->mixlock));
731 if (device->state == STATE_PLAYING)
732 device->state = STATE_STOPPING;
733 else if (device->state == STATE_STARTING)
734 device->state = STATE_STOPPED;
736 LeaveCriticalSection(&(device->mixlock));
737 /* **** */
739 return DS_OK;
742 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
744 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
745 ULONG ref = InterlockedIncrement(&(This->ref));
746 TRACE("(%p) ref was %d\n", This, ref - 1);
747 if(ref == 1)
748 InterlockedIncrement(&This->numIfaces);
749 return ref;
752 /* Decreases *out by 1 to no less than 0.
753 * Returns the new value of *out. */
754 LONG capped_refcount_dec(LONG *out)
756 LONG ref, oldref;
757 do {
758 ref = *out;
759 if(!ref)
760 return 0;
761 oldref = InterlockedCompareExchange(out, ref - 1, ref);
762 } while(oldref != ref);
763 return ref - 1;
766 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
768 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
769 ULONG ref;
771 ref = capped_refcount_dec(&This->ref);
772 if(!ref)
773 capped_refcount_dec(&This->numIfaces);
775 TRACE("(%p) primary ref is now %d\n", This, ref);
777 return ref;
780 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
781 DWORD *playpos, DWORD *writepos)
783 HRESULT hres = DS_OK;
784 UINT32 pad = 0;
785 UINT32 mixpos;
786 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
787 DirectSoundDevice *device = This->device;
788 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
790 /* **** */
791 EnterCriticalSection(&(device->mixlock));
793 if (device->client)
794 hres = IAudioClient_GetCurrentPadding(device->client, &pad);
795 if (hres != DS_OK) {
796 WARN("IAudioClient_GetCurrentPadding failed\n");
797 LeaveCriticalSection(&(device->mixlock));
798 return hres;
800 mixpos = (device->playpos + pad * device->pwfx->nBlockAlign) % device->buflen;
801 if (playpos)
802 *playpos = mixpos;
803 if (writepos) {
804 *writepos = mixpos;
805 if (device->state != STATE_STOPPED) {
806 /* apply the documented 10ms lead to writepos */
807 *writepos += device->writelead;
808 *writepos %= device->buflen;
812 LeaveCriticalSection(&(device->mixlock));
813 /* **** */
815 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
816 return DS_OK;
819 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
821 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
822 DirectSoundDevice *device = This->device;
823 TRACE("(%p,%p)\n", iface, status);
825 if (status == NULL) {
826 WARN("invalid parameter: status == NULL\n");
827 return DSERR_INVALIDPARAM;
830 *status = 0;
831 if ((device->state == STATE_STARTING) ||
832 (device->state == STATE_PLAYING))
833 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
835 TRACE("status=%x\n", *status);
836 return DS_OK;
840 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
841 DWORD wfsize, DWORD *wfwritten)
843 DWORD size;
844 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
845 DirectSoundDevice *device = This->device;
846 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
848 size = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
850 if (lpwf) { /* NULL is valid */
851 if (wfsize >= size) {
852 CopyMemory(lpwf,device->primary_pwfx,size);
853 if (wfwritten)
854 *wfwritten = size;
855 } else {
856 WARN("invalid parameter: wfsize too small\n");
857 if (wfwritten)
858 *wfwritten = 0;
859 return DSERR_INVALIDPARAM;
861 } else {
862 if (wfwritten)
863 *wfwritten = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
864 else {
865 WARN("invalid parameter: wfwritten == NULL\n");
866 return DSERR_INVALIDPARAM;
870 return DS_OK;
873 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
874 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
875 DWORD *audiobytes2, DWORD flags)
877 HRESULT hres;
878 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
879 DirectSoundDevice *device = This->device;
880 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
881 iface,
882 writecursor,
883 writebytes,
884 lplpaudioptr1,
885 audiobytes1,
886 lplpaudioptr2,
887 audiobytes2,
888 flags,
889 GetTickCount()
892 if (!audiobytes1)
893 return DSERR_INVALIDPARAM;
895 if (device->priolevel != DSSCL_WRITEPRIMARY) {
896 WARN("failed priority check!\n");
897 return DSERR_PRIOLEVELNEEDED;
900 /* when this flag is set, writecursor is meaningless and must be calculated */
901 if (flags & DSBLOCK_FROMWRITECURSOR) {
902 /* GetCurrentPosition does too much magic to duplicate here */
903 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
904 if (hres != DS_OK) {
905 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
906 return hres;
910 /* when this flag is set, writebytes is meaningless and must be set */
911 if (flags & DSBLOCK_ENTIREBUFFER)
912 writebytes = device->buflen;
914 if (writecursor >= device->buflen) {
915 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
916 writecursor, device->buflen);
917 return DSERR_INVALIDPARAM;
920 if (writebytes > device->buflen) {
921 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
922 writebytes, device->buflen);
923 return DSERR_INVALIDPARAM;
926 if (writecursor+writebytes <= device->buflen) {
927 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
928 *audiobytes1 = writebytes;
929 if (lplpaudioptr2)
930 *(LPBYTE*)lplpaudioptr2 = NULL;
931 if (audiobytes2)
932 *audiobytes2 = 0;
933 TRACE("->%d.0\n",writebytes);
934 } else {
935 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
936 *audiobytes1 = device->buflen-writecursor;
937 if (lplpaudioptr2)
938 *(LPBYTE*)lplpaudioptr2 = device->buffer;
939 if (audiobytes2)
940 *audiobytes2 = writebytes-(device->buflen-writecursor);
941 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
943 return DS_OK;
946 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
948 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
949 TRACE("(%p,%d)\n",This,newpos);
951 /* You cannot set the position of the primary buffer */
952 WARN("invalid call\n");
953 return DSERR_INVALIDCALL;
956 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
958 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
959 DirectSoundDevice *device = This->device;
960 float fvol;
961 HRESULT hr;
962 int i;
964 TRACE("(%p,%d)\n", iface, pan);
966 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
967 WARN("control unavailable\n");
968 return DSERR_CONTROLUNAVAIL;
971 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
972 WARN("invalid parameter: pan = %d\n", pan);
973 return DSERR_INVALIDPARAM;
976 /* **** */
977 EnterCriticalSection(&device->mixlock);
979 for (i = 0; i < DS_MAX_CHANNELS; i++) {
980 if (device->pwfx->nChannels > i){
981 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
982 if (FAILED(hr)){
983 LeaveCriticalSection(&device->mixlock);
984 WARN("GetChannelVolume failed: %08x\n", hr);
985 return hr;
987 } else
988 fvol = 1;
990 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
993 DSOUND_AmpFactorToVolPan(&device->volpan);
994 if (pan != device->volpan.lPan) {
995 device->volpan.lPan=pan;
996 DSOUND_RecalcVolPan(&device->volpan);
998 for (i = 0; i < DS_MAX_CHANNELS; i++) {
999 if (device->pwfx->nChannels > i) {
1000 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
1001 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
1002 if (FAILED(hr)){
1003 LeaveCriticalSection(&device->mixlock);
1004 WARN("SetChannelVolume failed: %08x\n", hr);
1005 return hr;
1011 LeaveCriticalSection(&device->mixlock);
1012 /* **** */
1014 return DS_OK;
1017 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1019 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1020 DirectSoundDevice *device = This->device;
1021 float fvol;
1022 HRESULT hr;
1023 int i;
1025 TRACE("(%p,%p)\n", iface, pan);
1027 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1028 WARN("control unavailable\n");
1029 return DSERR_CONTROLUNAVAIL;
1032 if (pan == NULL) {
1033 WARN("invalid parameter: pan == NULL\n");
1034 return DSERR_INVALIDPARAM;
1037 EnterCriticalSection(&device->mixlock);
1039 for (i = 0; i < DS_MAX_CHANNELS; i++) {
1040 if (device->pwfx->nChannels > i) {
1041 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
1042 if (FAILED(hr)){
1043 LeaveCriticalSection(&device->mixlock);
1044 WARN("GetChannelVolume failed: %08x\n", hr);
1045 return hr;
1047 } else
1048 fvol = 1;
1050 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
1053 DSOUND_AmpFactorToVolPan(&device->volpan);
1054 *pan = device->volpan.lPan;
1056 LeaveCriticalSection(&device->mixlock);
1058 return DS_OK;
1061 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1062 void *p2, DWORD x2)
1064 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1065 DirectSoundDevice *device = This->device;
1066 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1068 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1069 WARN("failed priority check!\n");
1070 return DSERR_PRIOLEVELNEEDED;
1073 if ((p1 && ((BYTE*)p1 < device->buffer || (BYTE*)p1 >= device->buffer + device->buflen)) ||
1074 (p2 && ((BYTE*)p2 < device->buffer || (BYTE*)p2 >= device->buffer + device->buflen)))
1075 return DSERR_INVALIDPARAM;
1077 return DS_OK;
1080 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1082 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1083 FIXME("(%p):stub\n",This);
1084 return DS_OK;
1087 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1089 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1090 DirectSoundDevice *device = This->device;
1091 TRACE("(%p,%p)\n", iface, freq);
1093 if (freq == NULL) {
1094 WARN("invalid parameter: freq == NULL\n");
1095 return DSERR_INVALIDPARAM;
1098 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1099 WARN("control unavailable\n");
1100 return DSERR_CONTROLUNAVAIL;
1103 *freq = device->pwfx->nSamplesPerSec;
1104 TRACE("-> %d\n", *freq);
1106 return DS_OK;
1109 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1110 const DSBUFFERDESC *dbsd)
1112 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1113 WARN("(%p) already initialized\n", This);
1114 return DSERR_ALREADYINITIALIZED;
1117 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1119 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1120 DirectSoundDevice *device = This->device;
1121 TRACE("(%p,%p)\n", iface, caps);
1123 if (caps == NULL) {
1124 WARN("invalid parameter: caps == NULL\n");
1125 return DSERR_INVALIDPARAM;
1128 if (caps->dwSize < sizeof(*caps)) {
1129 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1130 return DSERR_INVALIDPARAM;
1133 caps->dwFlags = This->dsbd.dwFlags;
1134 caps->dwBufferBytes = device->buflen;
1136 /* Windows reports these as zero */
1137 caps->dwUnlockTransferRate = 0;
1138 caps->dwPlayCpuOverhead = 0;
1140 return DS_OK;
1143 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1144 void **ppobj)
1146 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1148 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1150 if (ppobj == NULL) {
1151 WARN("invalid parameter\n");
1152 return E_INVALIDARG;
1155 *ppobj = NULL; /* assume failure */
1157 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1158 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1159 IDirectSoundBuffer_AddRef(iface);
1160 *ppobj = iface;
1161 return S_OK;
1164 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1165 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1166 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1167 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1168 return E_NOINTERFACE;
1171 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1172 ERR("app requested IDirectSoundNotify on primary buffer\n");
1173 /* FIXME: should we support this? */
1174 return E_NOINTERFACE;
1177 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1178 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1179 return E_NOINTERFACE;
1182 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1183 *ppobj = &This->IDirectSound3DListener_iface;
1184 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1185 return S_OK;
1188 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1189 *ppobj = &This->IKsPropertySet_iface;
1190 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1191 return S_OK;
1194 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1195 return E_NOINTERFACE;
1198 static const IDirectSoundBufferVtbl dspbvt =
1200 PrimaryBufferImpl_QueryInterface,
1201 PrimaryBufferImpl_AddRef,
1202 PrimaryBufferImpl_Release,
1203 PrimaryBufferImpl_GetCaps,
1204 PrimaryBufferImpl_GetCurrentPosition,
1205 PrimaryBufferImpl_GetFormat,
1206 PrimaryBufferImpl_GetVolume,
1207 PrimaryBufferImpl_GetPan,
1208 PrimaryBufferImpl_GetFrequency,
1209 PrimaryBufferImpl_GetStatus,
1210 PrimaryBufferImpl_Initialize,
1211 PrimaryBufferImpl_Lock,
1212 PrimaryBufferImpl_Play,
1213 PrimaryBufferImpl_SetCurrentPosition,
1214 PrimaryBufferImpl_SetFormat,
1215 PrimaryBufferImpl_SetVolume,
1216 PrimaryBufferImpl_SetPan,
1217 PrimaryBufferImpl_SetFrequency,
1218 PrimaryBufferImpl_Stop,
1219 PrimaryBufferImpl_Unlock,
1220 PrimaryBufferImpl_Restore
1223 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1224 const DSBUFFERDESC *dsbd)
1226 IDirectSoundBufferImpl *dsb;
1227 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1229 if (dsbd->lpwfxFormat) {
1230 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1231 *ppdsb = NULL;
1232 return DSERR_INVALIDPARAM;
1235 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1237 if (dsb == NULL) {
1238 WARN("out of memory\n");
1239 *ppdsb = NULL;
1240 return DSERR_OUTOFMEMORY;
1243 dsb->ref = 0;
1244 dsb->ref3D = 0;
1245 dsb->refiks = 0;
1246 dsb->numIfaces = 0;
1247 dsb->device = device;
1248 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1249 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1250 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1251 dsb->dsbd = *dsbd;
1253 /* IDirectSound3DListener */
1254 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1255 device->ds3dl.vPosition.x = 0.0;
1256 device->ds3dl.vPosition.y = 0.0;
1257 device->ds3dl.vPosition.z = 0.0;
1258 device->ds3dl.vVelocity.x = 0.0;
1259 device->ds3dl.vVelocity.y = 0.0;
1260 device->ds3dl.vVelocity.z = 0.0;
1261 device->ds3dl.vOrientFront.x = 0.0;
1262 device->ds3dl.vOrientFront.y = 0.0;
1263 device->ds3dl.vOrientFront.z = 1.0;
1264 device->ds3dl.vOrientTop.x = 0.0;
1265 device->ds3dl.vOrientTop.y = 1.0;
1266 device->ds3dl.vOrientTop.z = 0.0;
1267 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1268 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1269 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1270 device->ds3dl_need_recalc = TRUE;
1272 TRACE("Created primary buffer at %p\n", dsb);
1273 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1274 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1275 device->pwfx->wFormatTag, device->pwfx->nChannels,
1276 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1277 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1278 device->pwfx->cbSize);
1280 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1281 *ppdsb = dsb;
1282 return S_OK;