po: Fix a formatting directive in the Danish translation.
[wine/multimedia.git] / dlls / dsound / primary.c
blob5c74353f2125535b4a2c1ac0cbfcfc6c2bfe9782
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23 * it leaves dsound in unusable (not really open) state.
26 #include <stdarg.h>
28 #define COBJMACROS
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "mmsystem.h"
35 #include "winternl.h"
36 #include "mmddk.h"
37 #include "wine/debug.h"
38 #include "dsound.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
43 /** Calculate how long a fragment length of about 10 ms should be in frames
45 * nSamplesPerSec: Frequency rate in samples per second
46 * nBlockAlign: Size of a single blockalign
48 * Returns:
49 * Size in bytes of a single fragment
51 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
53 /* Given a timer delay of 10ms, the fragment size is approximately:
54 * fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
55 * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
57 * ALSA uses buffers that are powers of 2. Because of this, fraglen
58 * is rounded up to the nearest power of 2:
61 if (nSamplesPerSec <= 12800)
62 return 128 * nBlockAlign;
64 if (nSamplesPerSec <= 25600)
65 return 256 * nBlockAlign;
67 if (nSamplesPerSec <= 51200)
68 return 512 * nBlockAlign;
70 return 1024 * nBlockAlign;
73 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
75 TRACE("(%p)\n", device);
77 device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
78 device->helfrags = device->buflen / device->fraglen;
79 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
81 /* calculate the 10ms write lead */
82 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
85 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
87 UINT prebuf_frames;
88 REFERENCE_TIME prebuf_rt;
89 HRESULT hres;
91 TRACE("(%p, %d)\n", device, forcewave);
93 if(device->client){
94 IAudioClient_Release(device->client);
95 device->client = NULL;
97 if(device->render){
98 IAudioRenderClient_Release(device->render);
99 device->render = NULL;
101 if(device->clock){
102 IAudioClock_Release(device->clock);
103 device->clock = NULL;
105 if(device->volume){
106 IAudioStreamVolume_Release(device->volume);
107 device->volume = NULL;
110 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
111 CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
112 if(FAILED(hres)){
113 WARN("Activate failed: %08x\n", hres);
114 return hres;
117 prebuf_frames = device->prebuf * DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign) / device->pwfx->nBlockAlign;
118 prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
120 hres = IAudioClient_Initialize(device->client,
121 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST,
122 prebuf_rt, 0, device->pwfx, NULL);
123 if(FAILED(hres)){
124 IAudioClient_Release(device->client);
125 device->client = NULL;
126 WARN("Initialize failed: %08x\n", hres);
127 return hres;
130 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
131 (void**)&device->render);
132 if(FAILED(hres)){
133 IAudioClient_Release(device->client);
134 device->client = NULL;
135 WARN("GetService failed: %08x\n", hres);
136 return hres;
139 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
140 (void**)&device->clock);
141 if(FAILED(hres)){
142 IAudioClient_Release(device->client);
143 IAudioRenderClient_Release(device->render);
144 device->client = NULL;
145 device->render = NULL;
146 WARN("GetService failed: %08x\n", hres);
147 return hres;
150 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
151 (void**)&device->volume);
152 if(FAILED(hres)){
153 IAudioClient_Release(device->client);
154 IAudioRenderClient_Release(device->render);
155 IAudioClock_Release(device->clock);
156 device->client = NULL;
157 device->render = NULL;
158 device->clock = NULL;
159 WARN("GetService failed: %08x\n", hres);
160 return hres;
163 return S_OK;
166 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
168 DWORD buflen;
169 LPBYTE newbuf;
171 TRACE("(%p)\n", device);
173 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
174 on windows this size is always fixed (tested on win-xp) */
175 if (!device->buflen)
176 device->buflen = ds_hel_buflen;
177 buflen = device->buflen;
178 buflen -= buflen % device->pwfx->nBlockAlign;
179 device->buflen = buflen;
181 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
182 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
183 if (!device->mix_buffer)
184 return DSERR_OUTOFMEMORY;
186 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
187 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
189 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
191 /* reallocate emulated primary buffer */
192 if (device->buffer)
193 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
194 else
195 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
197 if (!newbuf) {
198 ERR("failed to allocate primary buffer\n");
199 return DSERR_OUTOFMEMORY;
200 /* but the old buffer might still exist and must be re-prepared */
203 DSOUND_RecalcPrimary(device);
205 device->buffer = newbuf;
207 TRACE("fraglen=%d\n", device->fraglen);
209 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
210 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
211 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
212 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
213 device->last_pos_bytes = device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
214 return DS_OK;
218 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
220 HRESULT hr;
222 TRACE("(%p)\n", device);
224 device->pwqueue = (DWORD)-1; /* resetting queues */
226 if(device->client){
227 hr = IAudioClient_Stop(device->client);
228 if(FAILED(hr))
229 WARN("Stop failed: %08x\n", hr);
232 /* clear the queue */
233 device->pwqueue = 0;
236 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
238 HRESULT err = DS_OK;
239 TRACE("(%p)\n", device);
241 device->buflen = ds_hel_buflen;
242 err = DSOUND_PrimaryOpen(device);
244 if (err != DS_OK) {
245 WARN("DSOUND_PrimaryOpen failed\n");
246 return err;
249 device->state = STATE_STOPPED;
250 return DS_OK;
253 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
255 TRACE("(%p)\n", device);
257 /* **** */
258 EnterCriticalSection(&(device->mixlock));
260 DSOUND_PrimaryClose(device);
261 HeapFree(GetProcessHeap(),0,device->pwfx);
262 device->pwfx=NULL;
264 LeaveCriticalSection(&(device->mixlock));
265 /* **** */
267 return DS_OK;
270 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
272 HRESULT hr;
274 TRACE("(%p)\n", device);
276 hr = IAudioClient_Start(device->client);
277 if(FAILED(hr)){
278 WARN("Start failed: %08x\n", hr);
279 return hr;
282 return DS_OK;
285 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
287 HRESULT hr;
289 TRACE("(%p)\n", device);
291 hr = IAudioClient_Stop(device->client);
292 if(FAILED(hr)){
293 WARN("Stop failed: %08x\n", hr);
294 return hr;
297 return DS_OK;
300 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
302 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
304 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
306 /* check if playpos was requested */
307 if (playpos)
308 /* use the cached play position */
309 *playpos = device->pwplay * device->fraglen;
311 /* check if writepos was requested */
312 if (writepos)
313 /* the writepos is the first non-queued position */
314 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
316 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
317 return DS_OK;
320 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
322 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
323 return sizeof(WAVEFORMATEX);
324 else
325 return sizeof(WAVEFORMATEX) + wfex->cbSize;
328 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
330 DWORD size = DSOUND_GetFormatSize(wfex);
331 LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
332 if (pwfx == NULL) {
333 WARN("out of memory\n");
334 } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
335 CopyMemory(pwfx, wfex, size);
336 } else {
337 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
338 pwfx->cbSize=0;
339 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
340 WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
341 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample/8;
343 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
344 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
345 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
348 return pwfx;
351 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
353 HRESULT err = DSERR_BUFFERLOST;
354 int i;
355 WAVEFORMATEX *old_fmt;
356 WAVEFORMATEXTENSIBLE *fmtex;
357 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
359 TRACE("(%p,%p)\n", device, passed_fmt);
361 if (device->priolevel == DSSCL_NORMAL) {
362 WARN("failed priority check!\n");
363 return DSERR_PRIOLEVELNEEDED;
366 /* Let's be pedantic! */
367 if (passed_fmt == NULL) {
368 WARN("invalid parameter: passed_fmt==NULL!\n");
369 return DSERR_INVALIDPARAM;
371 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
372 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
373 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
374 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
375 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
377 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
378 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
379 passed_fmt->nAvgBytesPerSec == 0 ||
380 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
381 return DSERR_INVALIDPARAM;
383 /* **** */
384 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
385 EnterCriticalSection(&(device->mixlock));
387 old_fmt = device->pwfx;
388 device->pwfx = DSOUND_CopyFormat(passed_fmt);
389 fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
390 if (device->pwfx == NULL) {
391 device->pwfx = old_fmt;
392 old_fmt = NULL;
393 err = DSERR_OUTOFMEMORY;
394 goto done;
397 DSOUND_PrimaryClose(device);
399 err = DSOUND_ReopenDevice(device, FALSE);
400 if(SUCCEEDED(err))
401 goto opened;
403 /* requested format failed, so try others */
404 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
405 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
406 device->pwfx->wBitsPerSample = 32;
407 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
408 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
410 err = DSOUND_ReopenDevice(device, FALSE);
411 if(SUCCEEDED(err))
412 goto opened;
415 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
416 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
417 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
418 device->pwfx->wBitsPerSample = 32;
419 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
420 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
422 err = DSOUND_ReopenDevice(device, FALSE);
423 if(SUCCEEDED(err))
424 goto opened;
427 device->pwfx->wBitsPerSample = 32;
428 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
429 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
430 err = DSOUND_ReopenDevice(device, FALSE);
431 if(SUCCEEDED(err))
432 goto opened;
434 device->pwfx->wBitsPerSample = 16;
435 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
436 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
437 err = DSOUND_ReopenDevice(device, FALSE);
438 if(SUCCEEDED(err))
439 goto opened;
441 device->pwfx->wBitsPerSample = 8;
442 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
443 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
444 err = DSOUND_ReopenDevice(device, FALSE);
445 if(SUCCEEDED(err))
446 goto opened;
448 device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
449 device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
450 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
451 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
452 err = DSOUND_ReopenDevice(device, FALSE);
453 if(SUCCEEDED(err))
454 goto opened;
456 device->pwfx->wBitsPerSample = 32;
457 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
458 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
459 err = DSOUND_ReopenDevice(device, FALSE);
460 if(SUCCEEDED(err))
461 goto opened;
463 device->pwfx->wBitsPerSample = 16;
464 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
465 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
466 err = DSOUND_ReopenDevice(device, FALSE);
467 if(SUCCEEDED(err))
468 goto opened;
470 device->pwfx->wBitsPerSample = 8;
471 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
472 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
473 err = DSOUND_ReopenDevice(device, FALSE);
474 if(SUCCEEDED(err))
475 goto opened;
477 WARN("No formats could be opened\n");
478 goto done;
480 opened:
481 err = DSOUND_PrimaryOpen(device);
482 if (err != DS_OK) {
483 WARN("DSOUND_PrimaryOpen failed\n");
484 goto done;
487 if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
489 DSOUND_PrimaryClose(device);
490 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
491 err = DSOUND_ReopenDevice(device, TRUE);
492 if (FAILED(err))
493 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
494 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
495 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
498 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
499 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
500 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
501 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
502 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
504 if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
505 old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
506 old_fmt->nChannels != device->pwfx->nChannels) {
507 IDirectSoundBufferImpl** dsb = device->buffers;
508 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
509 /* **** */
510 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
512 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
513 DSOUND_RecalcFormat((*dsb));
514 (*dsb)->primary_mixpos = 0;
516 RtlReleaseResource(&(*dsb)->lock);
517 /* **** */
521 done:
522 LeaveCriticalSection(&(device->mixlock));
523 RtlReleaseResource(&(device->buffer_list_lock));
524 /* **** */
526 HeapFree(GetProcessHeap(), 0, old_fmt);
527 return err;
530 /*******************************************************************************
531 * PrimaryBuffer
533 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
535 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
536 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
539 /* This sets this format for the primary buffer only */
540 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
541 const WAVEFORMATEX *wfex)
543 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
544 TRACE("(%p,%p)\n", iface, wfex);
545 return primarybuffer_SetFormat(This->device, wfex);
548 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
550 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
551 DirectSoundDevice *device = This->device;
552 HRESULT hr;
553 float lvol, rvol;
555 TRACE("(%p,%d)\n", iface, vol);
557 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
558 WARN("control unavailable\n");
559 return DSERR_CONTROLUNAVAIL;
562 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
563 WARN("invalid parameter: vol = %d\n", vol);
564 return DSERR_INVALIDPARAM;
567 /* **** */
568 EnterCriticalSection(&device->mixlock);
570 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
571 if(FAILED(hr)){
572 LeaveCriticalSection(&device->mixlock);
573 WARN("GetChannelVolume failed: %08x\n", hr);
574 return hr;
577 if(device->pwfx->nChannels > 1){
578 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
579 if(FAILED(hr)){
580 LeaveCriticalSection(&device->mixlock);
581 WARN("GetChannelVolume failed: %08x\n", hr);
582 return hr;
584 }else
585 rvol = 1;
587 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
588 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
590 DSOUND_AmpFactorToVolPan(&device->volpan);
591 if (vol != device->volpan.lVolume) {
592 device->volpan.lVolume=vol;
593 DSOUND_RecalcVolPan(&device->volpan);
594 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
595 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
596 if(FAILED(hr)){
597 LeaveCriticalSection(&device->mixlock);
598 WARN("SetChannelVolume failed: %08x\n", hr);
599 return hr;
602 if(device->pwfx->nChannels > 1){
603 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
604 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
605 if(FAILED(hr)){
606 LeaveCriticalSection(&device->mixlock);
607 WARN("SetChannelVolume failed: %08x\n", hr);
608 return hr;
613 LeaveCriticalSection(&(device->mixlock));
614 /* **** */
616 return DS_OK;
619 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
621 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
622 DirectSoundDevice *device = This->device;
623 float lvol, rvol;
624 HRESULT hr;
625 TRACE("(%p,%p)\n", iface, vol);
627 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
628 WARN("control unavailable\n");
629 return DSERR_CONTROLUNAVAIL;
632 if (vol == NULL) {
633 WARN("invalid parameter: vol = NULL\n");
634 return DSERR_INVALIDPARAM;
637 EnterCriticalSection(&device->mixlock);
639 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
640 if(FAILED(hr)){
641 LeaveCriticalSection(&device->mixlock);
642 WARN("GetChannelVolume failed: %08x\n", hr);
643 return hr;
646 if(device->pwfx->nChannels > 1){
647 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
648 if(FAILED(hr)){
649 LeaveCriticalSection(&device->mixlock);
650 WARN("GetChannelVolume failed: %08x\n", hr);
651 return hr;
653 }else
654 rvol = 1;
656 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
657 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
659 DSOUND_AmpFactorToVolPan(&device->volpan);
660 *vol = device->volpan.lVolume;
662 LeaveCriticalSection(&device->mixlock);
664 return DS_OK;
667 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
669 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
670 TRACE("(%p,%d)\n",This,freq);
672 /* You cannot set the frequency of the primary buffer */
673 WARN("control unavailable\n");
674 return DSERR_CONTROLUNAVAIL;
677 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
678 DWORD reserved2, DWORD flags)
680 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
681 DirectSoundDevice *device = This->device;
682 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
684 if (!(flags & DSBPLAY_LOOPING)) {
685 WARN("invalid parameter: flags = %08x\n", flags);
686 return DSERR_INVALIDPARAM;
689 /* **** */
690 EnterCriticalSection(&(device->mixlock));
692 if (device->state == STATE_STOPPED)
693 device->state = STATE_STARTING;
694 else if (device->state == STATE_STOPPING)
695 device->state = STATE_PLAYING;
697 LeaveCriticalSection(&(device->mixlock));
698 /* **** */
700 return DS_OK;
703 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
705 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
706 DirectSoundDevice *device = This->device;
707 TRACE("(%p)\n", iface);
709 /* **** */
710 EnterCriticalSection(&(device->mixlock));
712 if (device->state == STATE_PLAYING)
713 device->state = STATE_STOPPING;
714 else if (device->state == STATE_STARTING)
715 device->state = STATE_STOPPED;
717 LeaveCriticalSection(&(device->mixlock));
718 /* **** */
720 return DS_OK;
723 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
725 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
726 ULONG ref = InterlockedIncrement(&(This->ref));
727 TRACE("(%p) ref was %d\n", This, ref - 1);
728 if(ref == 1)
729 InterlockedIncrement(&This->numIfaces);
730 return ref;
733 void primarybuffer_destroy(IDirectSoundBufferImpl *This)
735 This->device->primary = NULL;
736 HeapFree(GetProcessHeap(), 0, This);
737 TRACE("(%p) released\n", This);
740 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
742 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
743 DWORD ref = InterlockedDecrement(&(This->ref));
744 TRACE("(%p) ref was %d\n", This, ref + 1);
746 if (!ref && !InterlockedDecrement(&This->numIfaces))
747 primarybuffer_destroy(This);
748 return ref;
751 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
752 DWORD *playpos, DWORD *writepos)
754 HRESULT hres;
755 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
756 DirectSoundDevice *device = This->device;
757 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
759 /* **** */
760 EnterCriticalSection(&(device->mixlock));
762 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
763 if (hres != DS_OK) {
764 WARN("DSOUND_PrimaryGetPosition failed\n");
765 LeaveCriticalSection(&(device->mixlock));
766 return hres;
768 if (writepos) {
769 if (device->state != STATE_STOPPED)
770 /* apply the documented 10ms lead to writepos */
771 *writepos += device->writelead;
772 while (*writepos >= device->buflen) *writepos -= device->buflen;
775 LeaveCriticalSection(&(device->mixlock));
776 /* **** */
778 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
779 return DS_OK;
782 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
784 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
785 DirectSoundDevice *device = This->device;
786 TRACE("(%p,%p)\n", iface, status);
788 if (status == NULL) {
789 WARN("invalid parameter: status == NULL\n");
790 return DSERR_INVALIDPARAM;
793 *status = 0;
794 if ((device->state == STATE_STARTING) ||
795 (device->state == STATE_PLAYING))
796 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
798 TRACE("status=%x\n", *status);
799 return DS_OK;
803 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
804 DWORD wfsize, DWORD *wfwritten)
806 DWORD size;
807 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
808 DirectSoundDevice *device = This->device;
809 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
811 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
813 if (lpwf) { /* NULL is valid */
814 if (wfsize >= size) {
815 CopyMemory(lpwf,device->pwfx,size);
816 if (wfwritten)
817 *wfwritten = size;
818 } else {
819 WARN("invalid parameter: wfsize too small\n");
820 if (wfwritten)
821 *wfwritten = 0;
822 return DSERR_INVALIDPARAM;
824 } else {
825 if (wfwritten)
826 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
827 else {
828 WARN("invalid parameter: wfwritten == NULL\n");
829 return DSERR_INVALIDPARAM;
833 return DS_OK;
836 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
837 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
838 DWORD *audiobytes2, DWORD flags)
840 HRESULT hres;
841 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
842 DirectSoundDevice *device = This->device;
843 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
844 iface,
845 writecursor,
846 writebytes,
847 lplpaudioptr1,
848 audiobytes1,
849 lplpaudioptr2,
850 audiobytes2,
851 flags,
852 GetTickCount()
855 if (!audiobytes1)
856 return DSERR_INVALIDPARAM;
858 if (device->priolevel != DSSCL_WRITEPRIMARY) {
859 WARN("failed priority check!\n");
860 return DSERR_PRIOLEVELNEEDED;
863 /* when this flag is set, writecursor is meaningless and must be calculated */
864 if (flags & DSBLOCK_FROMWRITECURSOR) {
865 /* GetCurrentPosition does too much magic to duplicate here */
866 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
867 if (hres != DS_OK) {
868 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
869 return hres;
873 /* when this flag is set, writebytes is meaningless and must be set */
874 if (flags & DSBLOCK_ENTIREBUFFER)
875 writebytes = device->buflen;
877 if (writecursor >= device->buflen) {
878 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
879 writecursor, device->buflen);
880 return DSERR_INVALIDPARAM;
883 if (writebytes > device->buflen) {
884 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
885 writebytes, device->buflen);
886 return DSERR_INVALIDPARAM;
889 if (writecursor+writebytes <= device->buflen) {
890 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
891 *audiobytes1 = writebytes;
892 if (lplpaudioptr2)
893 *(LPBYTE*)lplpaudioptr2 = NULL;
894 if (audiobytes2)
895 *audiobytes2 = 0;
896 TRACE("->%d.0\n",writebytes);
897 } else {
898 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
899 *audiobytes1 = device->buflen-writecursor;
900 if (lplpaudioptr2)
901 *(LPBYTE*)lplpaudioptr2 = device->buffer;
902 if (audiobytes2)
903 *audiobytes2 = writebytes-(device->buflen-writecursor);
904 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
906 return DS_OK;
909 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
911 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
912 TRACE("(%p,%d)\n",This,newpos);
914 /* You cannot set the position of the primary buffer */
915 WARN("invalid call\n");
916 return DSERR_INVALIDCALL;
919 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
921 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
922 DirectSoundDevice *device = This->device;
923 float lvol, rvol;
924 HRESULT hr;
925 TRACE("(%p,%d)\n", iface, pan);
927 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
928 WARN("control unavailable\n");
929 return DSERR_CONTROLUNAVAIL;
932 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
933 WARN("invalid parameter: pan = %d\n", pan);
934 return DSERR_INVALIDPARAM;
937 /* **** */
938 EnterCriticalSection(&device->mixlock);
940 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
941 if(FAILED(hr)){
942 LeaveCriticalSection(&device->mixlock);
943 WARN("GetChannelVolume failed: %08x\n", hr);
944 return hr;
947 if(device->pwfx->nChannels > 1){
948 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
949 if(FAILED(hr)){
950 LeaveCriticalSection(&device->mixlock);
951 WARN("GetChannelVolume failed: %08x\n", hr);
952 return hr;
954 }else
955 rvol = 1;
957 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
958 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
960 DSOUND_AmpFactorToVolPan(&device->volpan);
961 if (pan != device->volpan.lPan) {
962 device->volpan.lPan=pan;
963 DSOUND_RecalcVolPan(&device->volpan);
965 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
966 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
967 if(FAILED(hr)){
968 LeaveCriticalSection(&device->mixlock);
969 WARN("SetChannelVolume failed: %08x\n", hr);
970 return hr;
973 if(device->pwfx->nChannels > 1){
974 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
975 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
976 if(FAILED(hr)){
977 LeaveCriticalSection(&device->mixlock);
978 WARN("SetChannelVolume failed: %08x\n", hr);
979 return hr;
984 LeaveCriticalSection(&device->mixlock);
985 /* **** */
987 return DS_OK;
990 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
992 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
993 DirectSoundDevice *device = This->device;
994 float lvol, rvol;
995 HRESULT hr;
996 TRACE("(%p,%p)\n", iface, pan);
998 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
999 WARN("control unavailable\n");
1000 return DSERR_CONTROLUNAVAIL;
1003 if (pan == NULL) {
1004 WARN("invalid parameter: pan == NULL\n");
1005 return DSERR_INVALIDPARAM;
1008 EnterCriticalSection(&device->mixlock);
1010 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1011 if(FAILED(hr)){
1012 LeaveCriticalSection(&device->mixlock);
1013 WARN("GetChannelVolume failed: %08x\n", hr);
1014 return hr;
1017 if(device->pwfx->nChannels > 1){
1018 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1019 if(FAILED(hr)){
1020 LeaveCriticalSection(&device->mixlock);
1021 WARN("GetChannelVolume failed: %08x\n", hr);
1022 return hr;
1024 }else
1025 rvol = 1;
1027 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1028 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1030 DSOUND_AmpFactorToVolPan(&device->volpan);
1031 *pan = device->volpan.lPan;
1033 LeaveCriticalSection(&device->mixlock);
1035 return DS_OK;
1038 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1039 void *p2, DWORD x2)
1041 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1042 DirectSoundDevice *device = This->device;
1043 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1045 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1046 WARN("failed priority check!\n");
1047 return DSERR_PRIOLEVELNEEDED;
1050 if((p1 && ((BYTE*)p1 < device->buffer ||
1051 (BYTE*)p1 >= device->buffer + device->buflen)) ||
1052 (p2 && ((BYTE*)p2 < device->buffer ||
1053 (BYTE*)p2 >= device->buffer + device->buflen)))
1054 return DSERR_INVALIDPARAM;
1056 return DS_OK;
1059 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1061 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1062 FIXME("(%p):stub\n",This);
1063 return DS_OK;
1066 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1068 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1069 DirectSoundDevice *device = This->device;
1070 TRACE("(%p,%p)\n", iface, freq);
1072 if (freq == NULL) {
1073 WARN("invalid parameter: freq == NULL\n");
1074 return DSERR_INVALIDPARAM;
1077 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1078 WARN("control unavailable\n");
1079 return DSERR_CONTROLUNAVAIL;
1082 *freq = device->pwfx->nSamplesPerSec;
1083 TRACE("-> %d\n", *freq);
1085 return DS_OK;
1088 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1089 const DSBUFFERDESC *dbsd)
1091 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1092 WARN("(%p) already initialized\n", This);
1093 return DSERR_ALREADYINITIALIZED;
1096 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1098 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1099 DirectSoundDevice *device = This->device;
1100 TRACE("(%p,%p)\n", iface, caps);
1102 if (caps == NULL) {
1103 WARN("invalid parameter: caps == NULL\n");
1104 return DSERR_INVALIDPARAM;
1107 if (caps->dwSize < sizeof(*caps)) {
1108 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1109 return DSERR_INVALIDPARAM;
1112 caps->dwFlags = This->dsbd.dwFlags;
1113 caps->dwBufferBytes = device->buflen;
1115 /* Windows reports these as zero */
1116 caps->dwUnlockTransferRate = 0;
1117 caps->dwPlayCpuOverhead = 0;
1119 return DS_OK;
1122 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1123 void **ppobj)
1125 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1127 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1129 if (ppobj == NULL) {
1130 WARN("invalid parameter\n");
1131 return E_INVALIDARG;
1134 *ppobj = NULL; /* assume failure */
1136 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1137 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1138 IDirectSoundBuffer_AddRef(iface);
1139 *ppobj = iface;
1140 return S_OK;
1143 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1144 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1145 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1146 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1147 return E_NOINTERFACE;
1150 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1151 ERR("app requested IDirectSoundNotify on primary buffer\n");
1152 /* FIXME: should we support this? */
1153 return E_NOINTERFACE;
1156 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1157 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1158 return E_NOINTERFACE;
1161 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1162 *ppobj = &This->IDirectSound3DListener_iface;
1163 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1164 return S_OK;
1167 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1168 *ppobj = &This->IKsPropertySet_iface;
1169 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1170 return S_OK;
1173 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1174 return E_NOINTERFACE;
1177 static const IDirectSoundBufferVtbl dspbvt =
1179 PrimaryBufferImpl_QueryInterface,
1180 PrimaryBufferImpl_AddRef,
1181 PrimaryBufferImpl_Release,
1182 PrimaryBufferImpl_GetCaps,
1183 PrimaryBufferImpl_GetCurrentPosition,
1184 PrimaryBufferImpl_GetFormat,
1185 PrimaryBufferImpl_GetVolume,
1186 PrimaryBufferImpl_GetPan,
1187 PrimaryBufferImpl_GetFrequency,
1188 PrimaryBufferImpl_GetStatus,
1189 PrimaryBufferImpl_Initialize,
1190 PrimaryBufferImpl_Lock,
1191 PrimaryBufferImpl_Play,
1192 PrimaryBufferImpl_SetCurrentPosition,
1193 PrimaryBufferImpl_SetFormat,
1194 PrimaryBufferImpl_SetVolume,
1195 PrimaryBufferImpl_SetPan,
1196 PrimaryBufferImpl_SetFrequency,
1197 PrimaryBufferImpl_Stop,
1198 PrimaryBufferImpl_Unlock,
1199 PrimaryBufferImpl_Restore
1202 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1203 const DSBUFFERDESC *dsbd)
1205 IDirectSoundBufferImpl *dsb;
1206 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1208 if (dsbd->lpwfxFormat) {
1209 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1210 *ppdsb = NULL;
1211 return DSERR_INVALIDPARAM;
1214 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1216 if (dsb == NULL) {
1217 WARN("out of memory\n");
1218 *ppdsb = NULL;
1219 return DSERR_OUTOFMEMORY;
1222 dsb->ref = 0;
1223 dsb->ref3D = 0;
1224 dsb->refiks = 0;
1225 dsb->numIfaces = 0;
1226 dsb->device = device;
1227 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1228 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1229 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1230 dsb->dsbd = *dsbd;
1232 /* IDirectSound3DListener */
1233 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1234 device->ds3dl.vPosition.x = 0.0;
1235 device->ds3dl.vPosition.y = 0.0;
1236 device->ds3dl.vPosition.z = 0.0;
1237 device->ds3dl.vVelocity.x = 0.0;
1238 device->ds3dl.vVelocity.y = 0.0;
1239 device->ds3dl.vVelocity.z = 0.0;
1240 device->ds3dl.vOrientFront.x = 0.0;
1241 device->ds3dl.vOrientFront.y = 0.0;
1242 device->ds3dl.vOrientFront.z = 1.0;
1243 device->ds3dl.vOrientTop.x = 0.0;
1244 device->ds3dl.vOrientTop.y = 1.0;
1245 device->ds3dl.vOrientTop.z = 0.0;
1246 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1247 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1248 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1249 device->ds3dl_need_recalc = TRUE;
1251 TRACE("Created primary buffer at %p\n", dsb);
1252 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1253 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1254 device->pwfx->wFormatTag, device->pwfx->nChannels,
1255 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1256 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1257 device->pwfx->cbSize);
1259 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1260 *ppdsb = dsb;
1261 return S_OK;