secur32/tests: Use importlib for functions available since Windows XP.
[wine.git] / dlls / dsound / primary.c
blobf5a1020700ae0cc97a50c0d0a46a4096c4ce1865
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 frames, 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 FillMemory(newbuf, new_buflen, (wfx->wBitsPerSample == 8) ? 128 : 0);
252 } else if (!mixfloat) {
253 DWORD alloc_len = frames * 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 FillMemory(newbuf, alloc_len, (wfx->wBitsPerSample == 8) ? 128 : 0);
265 } else {
266 HeapFree(GetProcessHeap(), 0, device->buffer);
267 newbuf = NULL;
270 device->buffer = newbuf;
271 device->buflen = new_buflen;
272 HeapFree(GetProcessHeap(), 0, device->pwfx);
273 device->pwfx = wfx;
275 device->writelead = (wfx->nSamplesPerSec / 100) * wfx->nBlockAlign;
277 TRACE("buflen: %u, fraglen: %u\n", device->buflen, device->fraglen);
279 if (!mixfloat)
280 device->normfunction = normfunctions[wfx->wBitsPerSample/8 - 1];
281 else
282 device->normfunction = NULL;
284 device->playpos = 0;
286 for (i = 0; i < device->nrofbuffers; i++) {
287 RtlAcquireResourceExclusive(&dsb[i]->lock, TRUE);
288 DSOUND_RecalcFormat(dsb[i]);
289 RtlReleaseResource(&dsb[i]->lock);
292 return DS_OK;
295 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
297 HRESULT hres;
298 REFERENCE_TIME period;
299 UINT32 acbuf_frames, aclen_frames;
300 DWORD period_ms;
301 IAudioClient *client = NULL;
302 IAudioRenderClient *render = NULL;
303 IAudioStreamVolume *volume = NULL;
304 DWORD frag_frames;
305 WAVEFORMATEX *wfx = NULL;
306 DWORD oldspeakerconfig = device->speaker_config;
308 TRACE("(%p, %d)\n", device, forcewave);
310 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
311 CLSCTX_INPROC_SERVER, NULL, (void **)&client);
312 if(FAILED(hres)){
313 WARN("Activate failed: %08x\n", hres);
314 return hres;
317 hres = DSOUND_WaveFormat(device, client, forcewave, &wfx);
318 if (FAILED(hres)) {
319 IAudioClient_Release(client);
320 return hres;
323 hres = IAudioClient_Initialize(client,
324 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST |
325 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 800000, 0, wfx, NULL);
326 if(FAILED(hres)){
327 IAudioClient_Release(client);
328 ERR("Initialize failed: %08x\n", hres);
329 return hres;
332 IAudioClient_SetEventHandle(client, device->sleepev);
334 hres = IAudioClient_GetService(client, &IID_IAudioRenderClient, (void**)&render);
335 if(FAILED(hres))
336 goto err_service;
338 hres = IAudioClient_GetService(client, &IID_IAudioStreamVolume, (void**)&volume);
339 if(FAILED(hres))
340 goto err_service;
342 /* Now kick off the timer so the event fires periodically */
343 hres = IAudioClient_Start(client);
344 if (FAILED(hres)) {
345 WARN("Start failed with %08x\n", hres);
346 goto err;
348 hres = IAudioClient_GetStreamLatency(client, &period);
349 if (FAILED(hres)) {
350 WARN("GetStreamLatency failed with %08x\n", hres);
351 goto err;
353 hres = IAudioClient_GetBufferSize(client, &acbuf_frames);
354 if (FAILED(hres)) {
355 WARN("GetBufferSize failed with %08x\n", hres);
356 goto err;
359 period_ms = (period + 9999) / 10000;
360 frag_frames = MulDiv(wfx->nSamplesPerSec, period, 10000000);
362 aclen_frames = min(acbuf_frames, 3 * frag_frames);
364 TRACE("period %u ms fraglen %u buflen %u\n", period_ms, frag_frames * wfx->nBlockAlign, aclen_frames * wfx->nBlockAlign);
366 hres = DSOUND_PrimaryOpen(device, wfx, aclen_frames, forcewave);
367 if(FAILED(hres))
368 goto err;
370 DSOUND_ReleaseDevice(device);
371 device->client = client;
372 device->render = render;
373 device->volume = volume;
374 device->fraglen = frag_frames * wfx->nBlockAlign;
375 device->aclen = aclen_frames * wfx->nBlockAlign;
377 if (period_ms < 3)
378 device->sleeptime = 5;
379 else
380 device->sleeptime = period_ms * 5 / 2;
382 return S_OK;
384 err_service:
385 WARN("GetService failed: %08x\n", hres);
386 err:
387 device->speaker_config = oldspeakerconfig;
388 DSOUND_ParseSpeakerConfig(device);
389 if (volume)
390 IAudioStreamVolume_Release(volume);
391 if (render)
392 IAudioRenderClient_Release(render);
393 IAudioClient_Release(client);
394 HeapFree(GetProcessHeap(), 0, wfx);
395 return hres;
398 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
400 TRACE("(%p)\n", device);
402 /* **** */
403 EnterCriticalSection(&(device->mixlock));
405 if(device->primary && (device->primary->ref || device->primary->numIfaces))
406 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
408 HeapFree(GetProcessHeap(), 0, device->primary);
409 device->primary = NULL;
411 HeapFree(GetProcessHeap(),0,device->primary_pwfx);
412 HeapFree(GetProcessHeap(),0,device->pwfx);
413 device->pwfx=NULL;
415 LeaveCriticalSection(&(device->mixlock));
416 /* **** */
418 return DS_OK;
421 WAVEFORMATEX *DSOUND_CopyFormat(const WAVEFORMATEX *wfex)
423 WAVEFORMATEX *pwfx;
424 if(wfex->wFormatTag == WAVE_FORMAT_PCM){
425 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
426 if (!pwfx)
427 return NULL;
428 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
429 pwfx->cbSize = 0;
430 }else{
431 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX) + wfex->cbSize);
432 if (!pwfx)
433 return NULL;
434 CopyMemory(pwfx, wfex, sizeof(WAVEFORMATEX) + wfex->cbSize);
437 if(pwfx->wFormatTag == WAVE_FORMAT_PCM ||
438 (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
439 IsEqualGUID(&((const WAVEFORMATEXTENSIBLE*)pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))
440 pwfx->nBlockAlign = (pwfx->nChannels * pwfx->wBitsPerSample) / 8;
442 return pwfx;
445 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
447 HRESULT err = S_OK;
448 WAVEFORMATEX *old_fmt;
449 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
451 TRACE("(%p,%p)\n", device, passed_fmt);
453 if (device->priolevel == DSSCL_NORMAL) {
454 WARN("failed priority check!\n");
455 return DSERR_PRIOLEVELNEEDED;
458 /* Let's be pedantic! */
459 if (passed_fmt == NULL) {
460 WARN("invalid parameter: passed_fmt==NULL!\n");
461 return DSERR_INVALIDPARAM;
463 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
464 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
465 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
466 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
467 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
469 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
470 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
471 passed_fmt->nAvgBytesPerSec == 0 ||
472 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
473 return DSERR_INVALIDPARAM;
475 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
476 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
477 return DSERR_INVALIDPARAM;
480 /* **** */
481 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
482 EnterCriticalSection(&(device->mixlock));
484 if (device->priolevel == DSSCL_WRITEPRIMARY) {
485 old_fmt = device->primary_pwfx;
486 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
487 fmtex = (WAVEFORMATEXTENSIBLE *)device->primary_pwfx;
488 if (device->primary_pwfx == NULL) {
489 err = DSERR_OUTOFMEMORY;
490 goto out;
493 if (fmtex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
494 fmtex->Samples.wValidBitsPerSample == 0) {
495 TRACE("Correcting 0 valid bits per sample\n");
496 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
499 err = DSOUND_ReopenDevice(device, TRUE);
500 if (FAILED(err)) {
501 ERR("No formats could be opened\n");
502 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
503 device->primary_pwfx = old_fmt;
504 } else
505 HeapFree(GetProcessHeap(), 0, old_fmt);
506 } else {
507 WAVEFORMATEX *wfx = DSOUND_CopyFormat(passed_fmt);
508 if (wfx) {
509 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
510 device->primary_pwfx = wfx;
511 } else
512 err = DSERR_OUTOFMEMORY;
515 out:
516 LeaveCriticalSection(&(device->mixlock));
517 RtlReleaseResource(&(device->buffer_list_lock));
518 /* **** */
520 return err;
523 /*******************************************************************************
524 * PrimaryBuffer
526 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
528 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
529 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
532 /* This sets this format for the primary buffer only */
533 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
534 const WAVEFORMATEX *wfex)
536 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
537 TRACE("(%p,%p)\n", iface, wfex);
538 return primarybuffer_SetFormat(This->device, wfex);
541 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
543 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
544 DirectSoundDevice *device = This->device;
545 HRESULT hr;
546 float fvol;
547 int i;
549 TRACE("(%p,%d)\n", iface, vol);
551 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
552 WARN("control unavailable\n");
553 return DSERR_CONTROLUNAVAIL;
556 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
557 WARN("invalid parameter: vol = %d\n", vol);
558 return DSERR_INVALIDPARAM;
561 /* **** */
562 EnterCriticalSection(&device->mixlock);
564 for (i = 0; i < DS_MAX_CHANNELS; i++) {
565 if (device->pwfx->nChannels > i){
566 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
567 if (FAILED(hr)){
568 LeaveCriticalSection(&device->mixlock);
569 WARN("GetChannelVolume failed: %08x\n", hr);
570 return hr;
572 } else
573 fvol=1.0f;
575 device->volpan.dwTotalAmpFactor[i]=((UINT16)(fvol * (DWORD)0xFFFF));
578 DSOUND_AmpFactorToVolPan(&device->volpan);
579 if (vol != device->volpan.lVolume) {
580 device->volpan.lVolume=vol;
581 DSOUND_RecalcVolPan(&device->volpan);
583 for (i = 0; i < DS_MAX_CHANNELS; i++) {
584 if (device->pwfx->nChannels > i){
585 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
586 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
587 if (FAILED(hr)){
588 LeaveCriticalSection(&device->mixlock);
589 WARN("SetChannelVolume failed: %08x\n", hr);
590 return hr;
596 LeaveCriticalSection(&(device->mixlock));
597 /* **** */
599 return DS_OK;
602 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
604 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
605 DirectSoundDevice *device = This->device;
606 float fvol;
607 HRESULT hr;
608 int i;
610 TRACE("(%p,%p)\n", iface, vol);
612 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
613 WARN("control unavailable\n");
614 return DSERR_CONTROLUNAVAIL;
617 if (vol == NULL) {
618 WARN("invalid parameter: vol = NULL\n");
619 return DSERR_INVALIDPARAM;
622 EnterCriticalSection(&device->mixlock);
624 for (i = 0; i < DS_MAX_CHANNELS; i++) {
625 if (device->pwfx->nChannels > i){
626 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
627 if (FAILED(hr)){
628 LeaveCriticalSection(&device->mixlock);
629 WARN("GetChannelVolume failed: %08x\n", hr);
630 return hr;
632 } else
633 fvol = 1;
635 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
638 DSOUND_AmpFactorToVolPan(&device->volpan);
639 *vol = device->volpan.lVolume;
641 LeaveCriticalSection(&device->mixlock);
643 return DS_OK;
646 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
648 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
649 TRACE("(%p,%d)\n",This,freq);
651 /* You cannot set the frequency of the primary buffer */
652 WARN("control unavailable\n");
653 return DSERR_CONTROLUNAVAIL;
656 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
657 DWORD reserved2, DWORD flags)
659 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
660 DirectSoundDevice *device = This->device;
661 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
663 if (!(flags & DSBPLAY_LOOPING)) {
664 WARN("invalid parameter: flags = %08x\n", flags);
665 return DSERR_INVALIDPARAM;
668 device->stopped = 0;
670 return DS_OK;
673 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
675 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
676 DirectSoundDevice *device = This->device;
677 TRACE("(%p)\n", iface);
679 device->stopped = 1;
681 return DS_OK;
684 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
686 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
687 ULONG ref = InterlockedIncrement(&(This->ref));
688 TRACE("(%p) ref was %d\n", This, ref - 1);
689 if(ref == 1)
690 InterlockedIncrement(&This->numIfaces);
691 return ref;
694 /* Decreases *out by 1 to no less than 0.
695 * Returns the new value of *out. */
696 LONG capped_refcount_dec(LONG *out)
698 LONG ref, oldref;
699 do {
700 ref = *out;
701 if(!ref)
702 return 0;
703 oldref = InterlockedCompareExchange(out, ref - 1, ref);
704 } while(oldref != ref);
705 return ref - 1;
708 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
710 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
711 ULONG ref;
713 ref = capped_refcount_dec(&This->ref);
714 if(!ref)
715 capped_refcount_dec(&This->numIfaces);
717 TRACE("(%p) primary ref is now %d\n", This, ref);
719 return ref;
722 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
723 DWORD *playpos, DWORD *writepos)
725 HRESULT hres = DS_OK;
726 UINT32 pad = 0;
727 UINT32 mixpos;
728 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
729 DirectSoundDevice *device = This->device;
730 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
732 /* **** */
733 EnterCriticalSection(&(device->mixlock));
735 if (device->client)
736 hres = IAudioClient_GetCurrentPadding(device->client, &pad);
737 if (hres != DS_OK) {
738 WARN("IAudioClient_GetCurrentPadding failed\n");
739 LeaveCriticalSection(&(device->mixlock));
740 return hres;
742 mixpos = (device->playpos + pad * device->pwfx->nBlockAlign) % device->buflen;
743 if (playpos)
744 *playpos = mixpos;
745 if (writepos) {
746 *writepos = mixpos;
747 if (!device->stopped) {
748 /* apply the documented 10ms lead to writepos */
749 *writepos += device->writelead;
750 *writepos %= device->buflen;
754 LeaveCriticalSection(&(device->mixlock));
755 /* **** */
757 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
758 return DS_OK;
761 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
763 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
764 DirectSoundDevice *device = This->device;
765 TRACE("(%p,%p)\n", iface, status);
767 if (status == NULL) {
768 WARN("invalid parameter: status == NULL\n");
769 return DSERR_INVALIDPARAM;
772 *status = 0;
773 if (!device->stopped)
774 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
776 TRACE("status=%x\n", *status);
777 return DS_OK;
781 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
782 DWORD wfsize, DWORD *wfwritten)
784 DWORD size;
785 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
786 DirectSoundDevice *device = This->device;
787 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
789 size = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
791 if (lpwf) { /* NULL is valid */
792 if (wfsize >= size) {
793 CopyMemory(lpwf,device->primary_pwfx,size);
794 if (wfwritten)
795 *wfwritten = size;
796 } else {
797 WARN("invalid parameter: wfsize too small\n");
798 if (wfwritten)
799 *wfwritten = 0;
800 return DSERR_INVALIDPARAM;
802 } else {
803 if (wfwritten)
804 *wfwritten = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
805 else {
806 WARN("invalid parameter: wfwritten == NULL\n");
807 return DSERR_INVALIDPARAM;
811 return DS_OK;
814 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
815 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
816 DWORD *audiobytes2, DWORD flags)
818 HRESULT hres;
819 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
820 DirectSoundDevice *device = This->device;
821 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
822 iface,
823 writecursor,
824 writebytes,
825 lplpaudioptr1,
826 audiobytes1,
827 lplpaudioptr2,
828 audiobytes2,
829 flags,
830 GetTickCount()
833 if (!audiobytes1)
834 return DSERR_INVALIDPARAM;
836 if (device->priolevel != DSSCL_WRITEPRIMARY) {
837 WARN("failed priority check!\n");
838 return DSERR_PRIOLEVELNEEDED;
841 /* when this flag is set, writecursor is meaningless and must be calculated */
842 if (flags & DSBLOCK_FROMWRITECURSOR) {
843 /* GetCurrentPosition does too much magic to duplicate here */
844 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
845 if (hres != DS_OK) {
846 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
847 return hres;
851 /* when this flag is set, writebytes is meaningless and must be set */
852 if (flags & DSBLOCK_ENTIREBUFFER)
853 writebytes = device->buflen;
855 if (writecursor >= device->buflen) {
856 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
857 writecursor, device->buflen);
858 return DSERR_INVALIDPARAM;
861 if (writebytes > device->buflen) {
862 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
863 writebytes, device->buflen);
864 return DSERR_INVALIDPARAM;
867 if (writecursor+writebytes <= device->buflen) {
868 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
869 *audiobytes1 = writebytes;
870 if (lplpaudioptr2)
871 *(LPBYTE*)lplpaudioptr2 = NULL;
872 if (audiobytes2)
873 *audiobytes2 = 0;
874 TRACE("->%d.0\n",writebytes);
875 } else {
876 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
877 *audiobytes1 = device->buflen-writecursor;
878 if (lplpaudioptr2)
879 *(LPBYTE*)lplpaudioptr2 = device->buffer;
880 if (audiobytes2)
881 *audiobytes2 = writebytes-(device->buflen-writecursor);
882 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
884 return DS_OK;
887 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
889 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
890 TRACE("(%p,%d)\n",This,newpos);
892 /* You cannot set the position of the primary buffer */
893 WARN("invalid call\n");
894 return DSERR_INVALIDCALL;
897 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
899 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
900 DirectSoundDevice *device = This->device;
901 float fvol;
902 HRESULT hr;
903 int i;
905 TRACE("(%p,%d)\n", iface, pan);
907 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
908 WARN("control unavailable\n");
909 return DSERR_CONTROLUNAVAIL;
912 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
913 WARN("invalid parameter: pan = %d\n", pan);
914 return DSERR_INVALIDPARAM;
917 /* **** */
918 EnterCriticalSection(&device->mixlock);
920 for (i = 0; i < DS_MAX_CHANNELS; i++) {
921 if (device->pwfx->nChannels > i){
922 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
923 if (FAILED(hr)){
924 LeaveCriticalSection(&device->mixlock);
925 WARN("GetChannelVolume failed: %08x\n", hr);
926 return hr;
928 } else
929 fvol = 1;
931 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
934 DSOUND_AmpFactorToVolPan(&device->volpan);
935 if (pan != device->volpan.lPan) {
936 device->volpan.lPan=pan;
937 DSOUND_RecalcVolPan(&device->volpan);
939 for (i = 0; i < DS_MAX_CHANNELS; i++) {
940 if (device->pwfx->nChannels > i) {
941 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
942 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
943 if (FAILED(hr)){
944 LeaveCriticalSection(&device->mixlock);
945 WARN("SetChannelVolume failed: %08x\n", hr);
946 return hr;
952 LeaveCriticalSection(&device->mixlock);
953 /* **** */
955 return DS_OK;
958 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
960 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
961 DirectSoundDevice *device = This->device;
962 float fvol;
963 HRESULT hr;
964 int i;
966 TRACE("(%p,%p)\n", iface, pan);
968 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
969 WARN("control unavailable\n");
970 return DSERR_CONTROLUNAVAIL;
973 if (pan == NULL) {
974 WARN("invalid parameter: pan == NULL\n");
975 return DSERR_INVALIDPARAM;
978 EnterCriticalSection(&device->mixlock);
980 for (i = 0; i < DS_MAX_CHANNELS; i++) {
981 if (device->pwfx->nChannels > i) {
982 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
983 if (FAILED(hr)){
984 LeaveCriticalSection(&device->mixlock);
985 WARN("GetChannelVolume failed: %08x\n", hr);
986 return hr;
988 } else
989 fvol = 1;
991 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
994 DSOUND_AmpFactorToVolPan(&device->volpan);
995 *pan = device->volpan.lPan;
997 LeaveCriticalSection(&device->mixlock);
999 return DS_OK;
1002 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1003 void *p2, DWORD x2)
1005 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1006 DirectSoundDevice *device = This->device;
1007 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1009 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1010 WARN("failed priority check!\n");
1011 return DSERR_PRIOLEVELNEEDED;
1014 if ((p1 && ((BYTE*)p1 < device->buffer || (BYTE*)p1 >= device->buffer + device->buflen)) ||
1015 (p2 && ((BYTE*)p2 < device->buffer || (BYTE*)p2 >= device->buffer + device->buflen)))
1016 return DSERR_INVALIDPARAM;
1018 return DS_OK;
1021 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1023 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1024 FIXME("(%p):stub\n",This);
1025 return DS_OK;
1028 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1030 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1031 DirectSoundDevice *device = This->device;
1032 TRACE("(%p,%p)\n", iface, freq);
1034 if (freq == NULL) {
1035 WARN("invalid parameter: freq == NULL\n");
1036 return DSERR_INVALIDPARAM;
1039 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1040 WARN("control unavailable\n");
1041 return DSERR_CONTROLUNAVAIL;
1044 *freq = device->pwfx->nSamplesPerSec;
1045 TRACE("-> %d\n", *freq);
1047 return DS_OK;
1050 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1051 const DSBUFFERDESC *dbsd)
1053 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1054 WARN("(%p) already initialized\n", This);
1055 return DSERR_ALREADYINITIALIZED;
1058 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1060 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1061 DirectSoundDevice *device = This->device;
1062 TRACE("(%p,%p)\n", iface, caps);
1064 if (caps == NULL) {
1065 WARN("invalid parameter: caps == NULL\n");
1066 return DSERR_INVALIDPARAM;
1069 if (caps->dwSize < sizeof(*caps)) {
1070 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1071 return DSERR_INVALIDPARAM;
1074 caps->dwFlags = This->dsbd.dwFlags;
1075 caps->dwBufferBytes = device->buflen;
1077 /* Windows reports these as zero */
1078 caps->dwUnlockTransferRate = 0;
1079 caps->dwPlayCpuOverhead = 0;
1081 return DS_OK;
1084 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1085 void **ppobj)
1087 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1089 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1091 if (ppobj == NULL) {
1092 WARN("invalid parameter\n");
1093 return E_INVALIDARG;
1096 *ppobj = NULL; /* assume failure */
1098 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1099 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1100 IDirectSoundBuffer_AddRef(iface);
1101 *ppobj = iface;
1102 return S_OK;
1105 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1106 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1107 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1108 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1109 return E_NOINTERFACE;
1112 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1113 ERR("app requested IDirectSoundNotify on primary buffer\n");
1114 /* FIXME: should we support this? */
1115 return E_NOINTERFACE;
1118 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1119 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1120 return E_NOINTERFACE;
1123 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1124 *ppobj = &This->IDirectSound3DListener_iface;
1125 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1126 return S_OK;
1129 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1130 *ppobj = &This->IKsPropertySet_iface;
1131 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1132 return S_OK;
1135 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1136 return E_NOINTERFACE;
1139 static const IDirectSoundBufferVtbl dspbvt =
1141 PrimaryBufferImpl_QueryInterface,
1142 PrimaryBufferImpl_AddRef,
1143 PrimaryBufferImpl_Release,
1144 PrimaryBufferImpl_GetCaps,
1145 PrimaryBufferImpl_GetCurrentPosition,
1146 PrimaryBufferImpl_GetFormat,
1147 PrimaryBufferImpl_GetVolume,
1148 PrimaryBufferImpl_GetPan,
1149 PrimaryBufferImpl_GetFrequency,
1150 PrimaryBufferImpl_GetStatus,
1151 PrimaryBufferImpl_Initialize,
1152 PrimaryBufferImpl_Lock,
1153 PrimaryBufferImpl_Play,
1154 PrimaryBufferImpl_SetCurrentPosition,
1155 PrimaryBufferImpl_SetFormat,
1156 PrimaryBufferImpl_SetVolume,
1157 PrimaryBufferImpl_SetPan,
1158 PrimaryBufferImpl_SetFrequency,
1159 PrimaryBufferImpl_Stop,
1160 PrimaryBufferImpl_Unlock,
1161 PrimaryBufferImpl_Restore
1164 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1165 const DSBUFFERDESC *dsbd)
1167 IDirectSoundBufferImpl *dsb;
1168 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1170 if (dsbd->lpwfxFormat) {
1171 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1172 *ppdsb = NULL;
1173 return DSERR_INVALIDPARAM;
1176 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1178 if (dsb == NULL) {
1179 WARN("out of memory\n");
1180 *ppdsb = NULL;
1181 return DSERR_OUTOFMEMORY;
1184 dsb->ref = 0;
1185 dsb->ref3D = 0;
1186 dsb->refiks = 0;
1187 dsb->numIfaces = 0;
1188 dsb->device = device;
1189 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1190 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1191 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1192 dsb->dsbd = *dsbd;
1194 /* IDirectSound3DListener */
1195 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1196 device->ds3dl.vPosition.x = 0.0;
1197 device->ds3dl.vPosition.y = 0.0;
1198 device->ds3dl.vPosition.z = 0.0;
1199 device->ds3dl.vVelocity.x = 0.0;
1200 device->ds3dl.vVelocity.y = 0.0;
1201 device->ds3dl.vVelocity.z = 0.0;
1202 device->ds3dl.vOrientFront.x = 0.0;
1203 device->ds3dl.vOrientFront.y = 0.0;
1204 device->ds3dl.vOrientFront.z = 1.0;
1205 device->ds3dl.vOrientTop.x = 0.0;
1206 device->ds3dl.vOrientTop.y = 1.0;
1207 device->ds3dl.vOrientTop.z = 0.0;
1208 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1209 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1210 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1211 device->ds3dl_need_recalc = TRUE;
1213 TRACE("Created primary buffer at %p\n", dsb);
1214 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1215 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1216 device->pwfx->wFormatTag, device->pwfx->nChannels,
1217 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1218 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1219 device->pwfx->cbSize);
1221 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1222 *ppdsb = dsb;
1223 return S_OK;