TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / dsound / primary.c
blob0f83eb97a0c4d660833b8fd13cc5bd4328546ade
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 device->writelead = (wfx->nSamplesPerSec / 100) * wfx->nBlockAlign;
278 TRACE("buflen: %u, fraglen: %u, mix_buffer_len: %u\n",
279 device->buflen, device->fraglen, device->mix_buffer_len);
281 if (!forcewave && !mixfloat)
282 device->normfunction = normfunctions[wfx->nBlockAlign/8 - 1];
283 else
284 device->normfunction = NULL;
286 if (device->mix_buffer_len)
287 FillMemory(device->buffer, device->mix_buffer_len, 0);
288 else if (device->buffer)
289 FillMemory(device->buffer, device->buflen, (wfx->wBitsPerSample == 8) ? 128 : 0);
290 device->playpos = 0;
292 for (i = 0; i < device->nrofbuffers; i++) {
293 RtlAcquireResourceExclusive(&dsb[i]->lock, TRUE);
294 DSOUND_RecalcFormat(dsb[i]);
295 RtlReleaseResource(&dsb[i]->lock);
298 return DS_OK;
301 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
303 HRESULT hres;
304 REFERENCE_TIME period;
305 UINT32 frames;
306 DWORD period_ms;
307 IAudioClient *client = NULL;
308 IAudioRenderClient *render = NULL;
309 IAudioStreamVolume *volume = NULL;
310 DWORD fraglen, aclen;
311 WAVEFORMATEX *wfx = NULL;
312 DWORD oldspeakerconfig = device->speaker_config;
314 TRACE("(%p, %d)\n", device, forcewave);
316 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
317 CLSCTX_INPROC_SERVER, NULL, (void **)&client);
318 if(FAILED(hres)){
319 WARN("Activate failed: %08x\n", hres);
320 return hres;
323 hres = DSOUND_WaveFormat(device, client, forcewave, &wfx);
324 if (FAILED(hres)) {
325 IAudioClient_Release(client);
326 return hres;
329 hres = IAudioClient_Initialize(client,
330 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST |
331 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 800000, 0, wfx, NULL);
332 if(FAILED(hres)){
333 IAudioClient_Release(client);
334 ERR("Initialize failed: %08x\n", hres);
335 return hres;
338 IAudioClient_SetEventHandle(client, device->sleepev);
340 hres = IAudioClient_GetService(client, &IID_IAudioRenderClient, (void**)&render);
341 if(FAILED(hres))
342 goto err_service;
344 hres = IAudioClient_GetService(client, &IID_IAudioStreamVolume, (void**)&volume);
345 if(FAILED(hres))
346 goto err_service;
348 /* Now kick off the timer so the event fires periodically */
349 hres = IAudioClient_Start(client);
350 if (FAILED(hres)) {
351 WARN("Start failed with %08x\n", hres);
352 goto err;
354 hres = IAudioClient_GetStreamLatency(client, &period);
355 if (FAILED(hres)) {
356 WARN("GetStreamLatency failed with %08x\n", hres);
357 goto err;
359 hres = IAudioClient_GetBufferSize(client, &frames);
360 if (FAILED(hres)) {
361 WARN("GetBufferSize failed with %08x\n", hres);
362 goto err;
365 period_ms = (period + 9999) / 10000;
366 fraglen = MulDiv(wfx->nSamplesPerSec, period, 10000000) * wfx->nBlockAlign;
367 aclen = frames * wfx->nBlockAlign;
368 TRACE("period %u ms fraglen %u buflen %u\n", period_ms, fraglen, aclen);
370 hres = DSOUND_PrimaryOpen(device, wfx, aclen, forcewave);
371 if(FAILED(hres))
372 goto err;
374 DSOUND_ReleaseDevice(device);
375 device->client = client;
376 device->render = render;
377 device->volume = volume;
378 device->fraglen = fraglen;
379 device->aclen = aclen;
381 if (period_ms < 3)
382 device->sleeptime = 5;
383 else
384 device->sleeptime = period_ms * 5 / 2;
386 return S_OK;
388 err_service:
389 WARN("GetService failed: %08x\n", hres);
390 err:
391 device->speaker_config = oldspeakerconfig;
392 DSOUND_ParseSpeakerConfig(device);
393 if (volume)
394 IAudioStreamVolume_Release(volume);
395 if (render)
396 IAudioRenderClient_Release(render);
397 if (client)
398 IAudioClient_Release(client);
399 HeapFree(GetProcessHeap(), 0, wfx);
400 return hres;
403 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
405 TRACE("(%p)\n", device);
407 /* **** */
408 EnterCriticalSection(&(device->mixlock));
410 if(device->primary && (device->primary->ref || device->primary->numIfaces))
411 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
413 HeapFree(GetProcessHeap(), 0, device->primary);
414 device->primary = NULL;
416 HeapFree(GetProcessHeap(),0,device->primary_pwfx);
417 HeapFree(GetProcessHeap(),0,device->pwfx);
418 device->pwfx=NULL;
420 LeaveCriticalSection(&(device->mixlock));
421 /* **** */
423 return DS_OK;
426 WAVEFORMATEX *DSOUND_CopyFormat(const WAVEFORMATEX *wfex)
428 WAVEFORMATEX *pwfx;
429 if(wfex->wFormatTag == WAVE_FORMAT_PCM){
430 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
431 if (!pwfx)
432 return NULL;
433 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
434 pwfx->cbSize = 0;
435 }else{
436 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX) + wfex->cbSize);
437 if (!pwfx)
438 return NULL;
439 CopyMemory(pwfx, wfex, sizeof(WAVEFORMATEX) + wfex->cbSize);
442 if(pwfx->wFormatTag == WAVE_FORMAT_PCM ||
443 (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
444 IsEqualGUID(&((const WAVEFORMATEXTENSIBLE*)pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))
445 pwfx->nBlockAlign = (pwfx->nChannels * pwfx->wBitsPerSample) / 8;
447 return pwfx;
450 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
452 HRESULT err = S_OK;
453 WAVEFORMATEX *old_fmt;
454 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
456 TRACE("(%p,%p)\n", device, passed_fmt);
458 if (device->priolevel == DSSCL_NORMAL) {
459 WARN("failed priority check!\n");
460 return DSERR_PRIOLEVELNEEDED;
463 /* Let's be pedantic! */
464 if (passed_fmt == NULL) {
465 WARN("invalid parameter: passed_fmt==NULL!\n");
466 return DSERR_INVALIDPARAM;
468 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
469 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
470 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
471 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
472 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
474 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
475 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
476 passed_fmt->nAvgBytesPerSec == 0 ||
477 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
478 return DSERR_INVALIDPARAM;
480 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
481 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
482 return DSERR_INVALIDPARAM;
485 /* **** */
486 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
487 EnterCriticalSection(&(device->mixlock));
489 if (device->priolevel == DSSCL_WRITEPRIMARY) {
490 old_fmt = device->primary_pwfx;
491 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
492 fmtex = (WAVEFORMATEXTENSIBLE *)device->primary_pwfx;
493 if (device->primary_pwfx == NULL) {
494 err = DSERR_OUTOFMEMORY;
495 goto out;
498 if (fmtex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
499 fmtex->Samples.wValidBitsPerSample == 0) {
500 TRACE("Correcting 0 valid bits per sample\n");
501 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
504 err = DSOUND_ReopenDevice(device, TRUE);
505 if (FAILED(err)) {
506 ERR("No formats could be opened\n");
507 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
508 device->primary_pwfx = old_fmt;
509 } else
510 HeapFree(GetProcessHeap(), 0, old_fmt);
511 } else {
512 WAVEFORMATEX *wfx = DSOUND_CopyFormat(passed_fmt);
513 if (wfx) {
514 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
515 device->primary_pwfx = wfx;
516 } else
517 err = DSERR_OUTOFMEMORY;
520 out:
521 LeaveCriticalSection(&(device->mixlock));
522 RtlReleaseResource(&(device->buffer_list_lock));
523 /* **** */
525 return err;
528 /*******************************************************************************
529 * PrimaryBuffer
531 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
533 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
534 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
537 /* This sets this format for the primary buffer only */
538 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
539 const WAVEFORMATEX *wfex)
541 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
542 TRACE("(%p,%p)\n", iface, wfex);
543 return primarybuffer_SetFormat(This->device, wfex);
546 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
548 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
549 DirectSoundDevice *device = This->device;
550 HRESULT hr;
551 float fvol;
552 int i;
554 TRACE("(%p,%d)\n", iface, vol);
556 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
557 WARN("control unavailable\n");
558 return DSERR_CONTROLUNAVAIL;
561 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
562 WARN("invalid parameter: vol = %d\n", vol);
563 return DSERR_INVALIDPARAM;
566 /* **** */
567 EnterCriticalSection(&device->mixlock);
569 for (i = 0; i < DS_MAX_CHANNELS; i++) {
570 if (device->pwfx->nChannels > i){
571 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
572 if (FAILED(hr)){
573 LeaveCriticalSection(&device->mixlock);
574 WARN("GetChannelVolume failed: %08x\n", hr);
575 return hr;
577 } else
578 fvol=1.0f;
580 device->volpan.dwTotalAmpFactor[i]=((UINT16)(fvol * (DWORD)0xFFFF));
583 DSOUND_AmpFactorToVolPan(&device->volpan);
584 if (vol != device->volpan.lVolume) {
585 device->volpan.lVolume=vol;
586 DSOUND_RecalcVolPan(&device->volpan);
588 for (i = 0; i < DS_MAX_CHANNELS; i++) {
589 if (device->pwfx->nChannels > i){
590 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
591 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
592 if (FAILED(hr)){
593 LeaveCriticalSection(&device->mixlock);
594 WARN("SetChannelVolume failed: %08x\n", hr);
595 return hr;
601 LeaveCriticalSection(&(device->mixlock));
602 /* **** */
604 return DS_OK;
607 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
609 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
610 DirectSoundDevice *device = This->device;
611 float fvol;
612 HRESULT hr;
613 int i;
615 TRACE("(%p,%p)\n", iface, vol);
617 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
618 WARN("control unavailable\n");
619 return DSERR_CONTROLUNAVAIL;
622 if (vol == NULL) {
623 WARN("invalid parameter: vol = NULL\n");
624 return DSERR_INVALIDPARAM;
627 EnterCriticalSection(&device->mixlock);
629 for (i = 0; i < DS_MAX_CHANNELS; i++) {
630 if (device->pwfx->nChannels > i){
631 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
632 if (FAILED(hr)){
633 LeaveCriticalSection(&device->mixlock);
634 WARN("GetChannelVolume failed: %08x\n", hr);
635 return hr;
637 } else
638 fvol = 1;
640 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
643 DSOUND_AmpFactorToVolPan(&device->volpan);
644 *vol = device->volpan.lVolume;
646 LeaveCriticalSection(&device->mixlock);
648 return DS_OK;
651 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
653 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
654 TRACE("(%p,%d)\n",This,freq);
656 /* You cannot set the frequency of the primary buffer */
657 WARN("control unavailable\n");
658 return DSERR_CONTROLUNAVAIL;
661 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
662 DWORD reserved2, DWORD flags)
664 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
665 DirectSoundDevice *device = This->device;
666 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
668 if (!(flags & DSBPLAY_LOOPING)) {
669 WARN("invalid parameter: flags = %08x\n", flags);
670 return DSERR_INVALIDPARAM;
673 device->stopped = 0;
675 return DS_OK;
678 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
680 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
681 DirectSoundDevice *device = This->device;
682 TRACE("(%p)\n", iface);
684 device->stopped = 1;
686 return DS_OK;
689 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
691 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
692 ULONG ref = InterlockedIncrement(&(This->ref));
693 TRACE("(%p) ref was %d\n", This, ref - 1);
694 if(ref == 1)
695 InterlockedIncrement(&This->numIfaces);
696 return ref;
699 /* Decreases *out by 1 to no less than 0.
700 * Returns the new value of *out. */
701 LONG capped_refcount_dec(LONG *out)
703 LONG ref, oldref;
704 do {
705 ref = *out;
706 if(!ref)
707 return 0;
708 oldref = InterlockedCompareExchange(out, ref - 1, ref);
709 } while(oldref != ref);
710 return ref - 1;
713 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
715 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
716 ULONG ref;
718 ref = capped_refcount_dec(&This->ref);
719 if(!ref)
720 capped_refcount_dec(&This->numIfaces);
722 TRACE("(%p) primary ref is now %d\n", This, ref);
724 return ref;
727 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
728 DWORD *playpos, DWORD *writepos)
730 HRESULT hres = DS_OK;
731 UINT32 pad = 0;
732 UINT32 mixpos;
733 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
734 DirectSoundDevice *device = This->device;
735 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
737 /* **** */
738 EnterCriticalSection(&(device->mixlock));
740 if (device->client)
741 hres = IAudioClient_GetCurrentPadding(device->client, &pad);
742 if (hres != DS_OK) {
743 WARN("IAudioClient_GetCurrentPadding failed\n");
744 LeaveCriticalSection(&(device->mixlock));
745 return hres;
747 mixpos = (device->playpos + pad * device->pwfx->nBlockAlign) % device->buflen;
748 if (playpos)
749 *playpos = mixpos;
750 if (writepos) {
751 *writepos = mixpos;
752 if (!device->stopped) {
753 /* apply the documented 10ms lead to writepos */
754 *writepos += device->writelead;
755 *writepos %= device->buflen;
759 LeaveCriticalSection(&(device->mixlock));
760 /* **** */
762 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
763 return DS_OK;
766 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
768 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
769 DirectSoundDevice *device = This->device;
770 TRACE("(%p,%p)\n", iface, status);
772 if (status == NULL) {
773 WARN("invalid parameter: status == NULL\n");
774 return DSERR_INVALIDPARAM;
777 *status = 0;
778 if (!device->stopped)
779 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
781 TRACE("status=%x\n", *status);
782 return DS_OK;
786 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
787 DWORD wfsize, DWORD *wfwritten)
789 DWORD size;
790 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
791 DirectSoundDevice *device = This->device;
792 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
794 size = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
796 if (lpwf) { /* NULL is valid */
797 if (wfsize >= size) {
798 CopyMemory(lpwf,device->primary_pwfx,size);
799 if (wfwritten)
800 *wfwritten = size;
801 } else {
802 WARN("invalid parameter: wfsize too small\n");
803 if (wfwritten)
804 *wfwritten = 0;
805 return DSERR_INVALIDPARAM;
807 } else {
808 if (wfwritten)
809 *wfwritten = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
810 else {
811 WARN("invalid parameter: wfwritten == NULL\n");
812 return DSERR_INVALIDPARAM;
816 return DS_OK;
819 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
820 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
821 DWORD *audiobytes2, DWORD flags)
823 HRESULT hres;
824 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
825 DirectSoundDevice *device = This->device;
826 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
827 iface,
828 writecursor,
829 writebytes,
830 lplpaudioptr1,
831 audiobytes1,
832 lplpaudioptr2,
833 audiobytes2,
834 flags,
835 GetTickCount()
838 if (!audiobytes1)
839 return DSERR_INVALIDPARAM;
841 if (device->priolevel != DSSCL_WRITEPRIMARY) {
842 WARN("failed priority check!\n");
843 return DSERR_PRIOLEVELNEEDED;
846 /* when this flag is set, writecursor is meaningless and must be calculated */
847 if (flags & DSBLOCK_FROMWRITECURSOR) {
848 /* GetCurrentPosition does too much magic to duplicate here */
849 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
850 if (hres != DS_OK) {
851 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
852 return hres;
856 /* when this flag is set, writebytes is meaningless and must be set */
857 if (flags & DSBLOCK_ENTIREBUFFER)
858 writebytes = device->buflen;
860 if (writecursor >= device->buflen) {
861 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
862 writecursor, device->buflen);
863 return DSERR_INVALIDPARAM;
866 if (writebytes > device->buflen) {
867 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
868 writebytes, device->buflen);
869 return DSERR_INVALIDPARAM;
872 if (writecursor+writebytes <= device->buflen) {
873 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
874 *audiobytes1 = writebytes;
875 if (lplpaudioptr2)
876 *(LPBYTE*)lplpaudioptr2 = NULL;
877 if (audiobytes2)
878 *audiobytes2 = 0;
879 TRACE("->%d.0\n",writebytes);
880 } else {
881 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
882 *audiobytes1 = device->buflen-writecursor;
883 if (lplpaudioptr2)
884 *(LPBYTE*)lplpaudioptr2 = device->buffer;
885 if (audiobytes2)
886 *audiobytes2 = writebytes-(device->buflen-writecursor);
887 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
889 return DS_OK;
892 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
894 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
895 TRACE("(%p,%d)\n",This,newpos);
897 /* You cannot set the position of the primary buffer */
898 WARN("invalid call\n");
899 return DSERR_INVALIDCALL;
902 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
904 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
905 DirectSoundDevice *device = This->device;
906 float fvol;
907 HRESULT hr;
908 int i;
910 TRACE("(%p,%d)\n", iface, pan);
912 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
913 WARN("control unavailable\n");
914 return DSERR_CONTROLUNAVAIL;
917 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
918 WARN("invalid parameter: pan = %d\n", pan);
919 return DSERR_INVALIDPARAM;
922 /* **** */
923 EnterCriticalSection(&device->mixlock);
925 for (i = 0; i < DS_MAX_CHANNELS; i++) {
926 if (device->pwfx->nChannels > i){
927 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
928 if (FAILED(hr)){
929 LeaveCriticalSection(&device->mixlock);
930 WARN("GetChannelVolume failed: %08x\n", hr);
931 return hr;
933 } else
934 fvol = 1;
936 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
939 DSOUND_AmpFactorToVolPan(&device->volpan);
940 if (pan != device->volpan.lPan) {
941 device->volpan.lPan=pan;
942 DSOUND_RecalcVolPan(&device->volpan);
944 for (i = 0; i < DS_MAX_CHANNELS; i++) {
945 if (device->pwfx->nChannels > i) {
946 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
947 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
948 if (FAILED(hr)){
949 LeaveCriticalSection(&device->mixlock);
950 WARN("SetChannelVolume failed: %08x\n", hr);
951 return hr;
957 LeaveCriticalSection(&device->mixlock);
958 /* **** */
960 return DS_OK;
963 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
965 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
966 DirectSoundDevice *device = This->device;
967 float fvol;
968 HRESULT hr;
969 int i;
971 TRACE("(%p,%p)\n", iface, pan);
973 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
974 WARN("control unavailable\n");
975 return DSERR_CONTROLUNAVAIL;
978 if (pan == NULL) {
979 WARN("invalid parameter: pan == NULL\n");
980 return DSERR_INVALIDPARAM;
983 EnterCriticalSection(&device->mixlock);
985 for (i = 0; i < DS_MAX_CHANNELS; i++) {
986 if (device->pwfx->nChannels > i) {
987 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
988 if (FAILED(hr)){
989 LeaveCriticalSection(&device->mixlock);
990 WARN("GetChannelVolume failed: %08x\n", hr);
991 return hr;
993 } else
994 fvol = 1;
996 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
999 DSOUND_AmpFactorToVolPan(&device->volpan);
1000 *pan = device->volpan.lPan;
1002 LeaveCriticalSection(&device->mixlock);
1004 return DS_OK;
1007 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1008 void *p2, DWORD x2)
1010 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1011 DirectSoundDevice *device = This->device;
1012 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1014 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1015 WARN("failed priority check!\n");
1016 return DSERR_PRIOLEVELNEEDED;
1019 if ((p1 && ((BYTE*)p1 < device->buffer || (BYTE*)p1 >= device->buffer + device->buflen)) ||
1020 (p2 && ((BYTE*)p2 < device->buffer || (BYTE*)p2 >= device->buffer + device->buflen)))
1021 return DSERR_INVALIDPARAM;
1023 return DS_OK;
1026 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1028 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1029 FIXME("(%p):stub\n",This);
1030 return DS_OK;
1033 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1035 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1036 DirectSoundDevice *device = This->device;
1037 TRACE("(%p,%p)\n", iface, freq);
1039 if (freq == NULL) {
1040 WARN("invalid parameter: freq == NULL\n");
1041 return DSERR_INVALIDPARAM;
1044 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1045 WARN("control unavailable\n");
1046 return DSERR_CONTROLUNAVAIL;
1049 *freq = device->pwfx->nSamplesPerSec;
1050 TRACE("-> %d\n", *freq);
1052 return DS_OK;
1055 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1056 const DSBUFFERDESC *dbsd)
1058 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1059 WARN("(%p) already initialized\n", This);
1060 return DSERR_ALREADYINITIALIZED;
1063 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1065 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1066 DirectSoundDevice *device = This->device;
1067 TRACE("(%p,%p)\n", iface, caps);
1069 if (caps == NULL) {
1070 WARN("invalid parameter: caps == NULL\n");
1071 return DSERR_INVALIDPARAM;
1074 if (caps->dwSize < sizeof(*caps)) {
1075 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1076 return DSERR_INVALIDPARAM;
1079 caps->dwFlags = This->dsbd.dwFlags;
1080 caps->dwBufferBytes = device->buflen;
1082 /* Windows reports these as zero */
1083 caps->dwUnlockTransferRate = 0;
1084 caps->dwPlayCpuOverhead = 0;
1086 return DS_OK;
1089 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1090 void **ppobj)
1092 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1094 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1096 if (ppobj == NULL) {
1097 WARN("invalid parameter\n");
1098 return E_INVALIDARG;
1101 *ppobj = NULL; /* assume failure */
1103 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1104 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1105 IDirectSoundBuffer_AddRef(iface);
1106 *ppobj = iface;
1107 return S_OK;
1110 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1111 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1112 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1113 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1114 return E_NOINTERFACE;
1117 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1118 ERR("app requested IDirectSoundNotify on primary buffer\n");
1119 /* FIXME: should we support this? */
1120 return E_NOINTERFACE;
1123 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1124 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1125 return E_NOINTERFACE;
1128 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1129 *ppobj = &This->IDirectSound3DListener_iface;
1130 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1131 return S_OK;
1134 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1135 *ppobj = &This->IKsPropertySet_iface;
1136 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1137 return S_OK;
1140 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1141 return E_NOINTERFACE;
1144 static const IDirectSoundBufferVtbl dspbvt =
1146 PrimaryBufferImpl_QueryInterface,
1147 PrimaryBufferImpl_AddRef,
1148 PrimaryBufferImpl_Release,
1149 PrimaryBufferImpl_GetCaps,
1150 PrimaryBufferImpl_GetCurrentPosition,
1151 PrimaryBufferImpl_GetFormat,
1152 PrimaryBufferImpl_GetVolume,
1153 PrimaryBufferImpl_GetPan,
1154 PrimaryBufferImpl_GetFrequency,
1155 PrimaryBufferImpl_GetStatus,
1156 PrimaryBufferImpl_Initialize,
1157 PrimaryBufferImpl_Lock,
1158 PrimaryBufferImpl_Play,
1159 PrimaryBufferImpl_SetCurrentPosition,
1160 PrimaryBufferImpl_SetFormat,
1161 PrimaryBufferImpl_SetVolume,
1162 PrimaryBufferImpl_SetPan,
1163 PrimaryBufferImpl_SetFrequency,
1164 PrimaryBufferImpl_Stop,
1165 PrimaryBufferImpl_Unlock,
1166 PrimaryBufferImpl_Restore
1169 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1170 const DSBUFFERDESC *dsbd)
1172 IDirectSoundBufferImpl *dsb;
1173 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1175 if (dsbd->lpwfxFormat) {
1176 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1177 *ppdsb = NULL;
1178 return DSERR_INVALIDPARAM;
1181 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1183 if (dsb == NULL) {
1184 WARN("out of memory\n");
1185 *ppdsb = NULL;
1186 return DSERR_OUTOFMEMORY;
1189 dsb->ref = 0;
1190 dsb->ref3D = 0;
1191 dsb->refiks = 0;
1192 dsb->numIfaces = 0;
1193 dsb->device = device;
1194 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1195 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1196 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1197 dsb->dsbd = *dsbd;
1199 /* IDirectSound3DListener */
1200 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1201 device->ds3dl.vPosition.x = 0.0;
1202 device->ds3dl.vPosition.y = 0.0;
1203 device->ds3dl.vPosition.z = 0.0;
1204 device->ds3dl.vVelocity.x = 0.0;
1205 device->ds3dl.vVelocity.y = 0.0;
1206 device->ds3dl.vVelocity.z = 0.0;
1207 device->ds3dl.vOrientFront.x = 0.0;
1208 device->ds3dl.vOrientFront.y = 0.0;
1209 device->ds3dl.vOrientFront.z = 1.0;
1210 device->ds3dl.vOrientTop.x = 0.0;
1211 device->ds3dl.vOrientTop.y = 1.0;
1212 device->ds3dl.vOrientTop.z = 0.0;
1213 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1214 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1215 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1216 device->ds3dl_need_recalc = TRUE;
1218 TRACE("Created primary buffer at %p\n", dsb);
1219 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1220 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1221 device->pwfx->wFormatTag, device->pwfx->nChannels,
1222 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1223 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1224 device->pwfx->cbSize);
1226 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1227 *ppdsb = dsb;
1228 return S_OK;