advapi32: Return a fake handle from EventRegister.
[wine/multimedia.git] / dlls / dsound / primary.c
blobc39da8a7dc31d59becf0445510c1ea63bba11eaa
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, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
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 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
384 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
385 return DSERR_INVALIDPARAM;
388 /* **** */
389 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
390 EnterCriticalSection(&(device->mixlock));
392 old_fmt = device->pwfx;
393 device->pwfx = DSOUND_CopyFormat(passed_fmt);
394 fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
395 if (device->pwfx == NULL) {
396 device->pwfx = old_fmt;
397 old_fmt = NULL;
398 err = DSERR_OUTOFMEMORY;
399 goto done;
402 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
403 if(fmtex->Samples.wValidBitsPerSample == 0){
404 TRACE("Correcting 0 valid bits per sample\n");
405 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
409 DSOUND_PrimaryClose(device);
411 err = DSOUND_ReopenDevice(device, FALSE);
412 if(SUCCEEDED(err))
413 goto opened;
415 /* requested format failed, so try others */
416 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
417 device->pwfx->wFormatTag = WAVE_FORMAT_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 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
428 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
429 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
430 device->pwfx->wBitsPerSample = 32;
431 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
432 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
434 err = DSOUND_ReopenDevice(device, FALSE);
435 if(SUCCEEDED(err))
436 goto opened;
439 device->pwfx->wBitsPerSample = 32;
440 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
441 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
442 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
443 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
444 err = DSOUND_ReopenDevice(device, FALSE);
445 if(SUCCEEDED(err))
446 goto opened;
448 device->pwfx->wBitsPerSample = 16;
449 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
450 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
451 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
452 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
453 err = DSOUND_ReopenDevice(device, FALSE);
454 if(SUCCEEDED(err))
455 goto opened;
457 device->pwfx->wBitsPerSample = 8;
458 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
459 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
460 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
461 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
462 err = DSOUND_ReopenDevice(device, FALSE);
463 if(SUCCEEDED(err))
464 goto opened;
466 device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
467 device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
468 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
469 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
470 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
471 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
472 err = DSOUND_ReopenDevice(device, FALSE);
473 if(SUCCEEDED(err))
474 goto opened;
476 device->pwfx->wBitsPerSample = 32;
477 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
478 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
479 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
480 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
481 err = DSOUND_ReopenDevice(device, FALSE);
482 if(SUCCEEDED(err))
483 goto opened;
485 device->pwfx->wBitsPerSample = 16;
486 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
487 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
488 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
489 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
490 err = DSOUND_ReopenDevice(device, FALSE);
491 if(SUCCEEDED(err))
492 goto opened;
494 device->pwfx->wBitsPerSample = 8;
495 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
496 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
497 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
498 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
499 err = DSOUND_ReopenDevice(device, FALSE);
500 if(SUCCEEDED(err))
501 goto opened;
503 WARN("No formats could be opened\n");
504 goto done;
506 opened:
507 err = DSOUND_PrimaryOpen(device);
508 if (err != DS_OK) {
509 WARN("DSOUND_PrimaryOpen failed\n");
510 goto done;
513 if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
515 DSOUND_PrimaryClose(device);
516 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
517 err = DSOUND_ReopenDevice(device, TRUE);
518 if (FAILED(err))
519 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
520 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
521 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
524 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
525 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
526 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
527 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
528 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
530 if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
531 old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
532 old_fmt->nChannels != device->pwfx->nChannels) {
533 IDirectSoundBufferImpl** dsb = device->buffers;
534 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
535 /* **** */
536 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
538 (*dsb)->freqAdjust = (*dsb)->freq / (float)device->pwfx->nSamplesPerSec;
539 DSOUND_RecalcFormat((*dsb));
540 (*dsb)->primary_mixpos = 0;
542 RtlReleaseResource(&(*dsb)->lock);
543 /* **** */
547 done:
548 LeaveCriticalSection(&(device->mixlock));
549 RtlReleaseResource(&(device->buffer_list_lock));
550 /* **** */
552 HeapFree(GetProcessHeap(), 0, old_fmt);
553 return err;
556 /*******************************************************************************
557 * PrimaryBuffer
559 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
561 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
562 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
565 /* This sets this format for the primary buffer only */
566 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
567 const WAVEFORMATEX *wfex)
569 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
570 TRACE("(%p,%p)\n", iface, wfex);
571 return primarybuffer_SetFormat(This->device, wfex);
574 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
576 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
577 DirectSoundDevice *device = This->device;
578 HRESULT hr;
579 float lvol, rvol;
581 TRACE("(%p,%d)\n", iface, vol);
583 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
584 WARN("control unavailable\n");
585 return DSERR_CONTROLUNAVAIL;
588 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
589 WARN("invalid parameter: vol = %d\n", vol);
590 return DSERR_INVALIDPARAM;
593 /* **** */
594 EnterCriticalSection(&device->mixlock);
596 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
597 if(FAILED(hr)){
598 LeaveCriticalSection(&device->mixlock);
599 WARN("GetChannelVolume failed: %08x\n", hr);
600 return hr;
603 if(device->pwfx->nChannels > 1){
604 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
605 if(FAILED(hr)){
606 LeaveCriticalSection(&device->mixlock);
607 WARN("GetChannelVolume failed: %08x\n", hr);
608 return hr;
610 }else
611 rvol = 1;
613 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
614 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
616 DSOUND_AmpFactorToVolPan(&device->volpan);
617 if (vol != device->volpan.lVolume) {
618 device->volpan.lVolume=vol;
619 DSOUND_RecalcVolPan(&device->volpan);
620 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
621 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
622 if(FAILED(hr)){
623 LeaveCriticalSection(&device->mixlock);
624 WARN("SetChannelVolume failed: %08x\n", hr);
625 return hr;
628 if(device->pwfx->nChannels > 1){
629 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
630 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
631 if(FAILED(hr)){
632 LeaveCriticalSection(&device->mixlock);
633 WARN("SetChannelVolume failed: %08x\n", hr);
634 return hr;
639 LeaveCriticalSection(&(device->mixlock));
640 /* **** */
642 return DS_OK;
645 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
647 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
648 DirectSoundDevice *device = This->device;
649 float lvol, rvol;
650 HRESULT hr;
651 TRACE("(%p,%p)\n", iface, vol);
653 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
654 WARN("control unavailable\n");
655 return DSERR_CONTROLUNAVAIL;
658 if (vol == NULL) {
659 WARN("invalid parameter: vol = NULL\n");
660 return DSERR_INVALIDPARAM;
663 EnterCriticalSection(&device->mixlock);
665 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
666 if(FAILED(hr)){
667 LeaveCriticalSection(&device->mixlock);
668 WARN("GetChannelVolume failed: %08x\n", hr);
669 return hr;
672 if(device->pwfx->nChannels > 1){
673 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
674 if(FAILED(hr)){
675 LeaveCriticalSection(&device->mixlock);
676 WARN("GetChannelVolume failed: %08x\n", hr);
677 return hr;
679 }else
680 rvol = 1;
682 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
683 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
685 DSOUND_AmpFactorToVolPan(&device->volpan);
686 *vol = device->volpan.lVolume;
688 LeaveCriticalSection(&device->mixlock);
690 return DS_OK;
693 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
695 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
696 TRACE("(%p,%d)\n",This,freq);
698 /* You cannot set the frequency of the primary buffer */
699 WARN("control unavailable\n");
700 return DSERR_CONTROLUNAVAIL;
703 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
704 DWORD reserved2, DWORD flags)
706 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
707 DirectSoundDevice *device = This->device;
708 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
710 if (!(flags & DSBPLAY_LOOPING)) {
711 WARN("invalid parameter: flags = %08x\n", flags);
712 return DSERR_INVALIDPARAM;
715 /* **** */
716 EnterCriticalSection(&(device->mixlock));
718 if (device->state == STATE_STOPPED)
719 device->state = STATE_STARTING;
720 else if (device->state == STATE_STOPPING)
721 device->state = STATE_PLAYING;
723 LeaveCriticalSection(&(device->mixlock));
724 /* **** */
726 return DS_OK;
729 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
731 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
732 DirectSoundDevice *device = This->device;
733 TRACE("(%p)\n", iface);
735 /* **** */
736 EnterCriticalSection(&(device->mixlock));
738 if (device->state == STATE_PLAYING)
739 device->state = STATE_STOPPING;
740 else if (device->state == STATE_STARTING)
741 device->state = STATE_STOPPED;
743 LeaveCriticalSection(&(device->mixlock));
744 /* **** */
746 return DS_OK;
749 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
751 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
752 ULONG ref = InterlockedIncrement(&(This->ref));
753 TRACE("(%p) ref was %d\n", This, ref - 1);
754 if(ref == 1)
755 InterlockedIncrement(&This->numIfaces);
756 return ref;
759 void primarybuffer_destroy(IDirectSoundBufferImpl *This)
761 This->device->primary = NULL;
762 HeapFree(GetProcessHeap(), 0, This);
763 TRACE("(%p) released\n", This);
766 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
768 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
769 DWORD ref = InterlockedDecrement(&(This->ref));
770 TRACE("(%p) ref was %d\n", This, ref + 1);
772 if (!ref && !InterlockedDecrement(&This->numIfaces))
773 primarybuffer_destroy(This);
774 return ref;
777 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
778 DWORD *playpos, DWORD *writepos)
780 HRESULT hres;
781 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
782 DirectSoundDevice *device = This->device;
783 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
785 /* **** */
786 EnterCriticalSection(&(device->mixlock));
788 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
789 if (hres != DS_OK) {
790 WARN("DSOUND_PrimaryGetPosition failed\n");
791 LeaveCriticalSection(&(device->mixlock));
792 return hres;
794 if (writepos) {
795 if (device->state != STATE_STOPPED)
796 /* apply the documented 10ms lead to writepos */
797 *writepos += device->writelead;
798 while (*writepos >= device->buflen) *writepos -= device->buflen;
801 LeaveCriticalSection(&(device->mixlock));
802 /* **** */
804 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
805 return DS_OK;
808 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
810 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
811 DirectSoundDevice *device = This->device;
812 TRACE("(%p,%p)\n", iface, status);
814 if (status == NULL) {
815 WARN("invalid parameter: status == NULL\n");
816 return DSERR_INVALIDPARAM;
819 *status = 0;
820 if ((device->state == STATE_STARTING) ||
821 (device->state == STATE_PLAYING))
822 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
824 TRACE("status=%x\n", *status);
825 return DS_OK;
829 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
830 DWORD wfsize, DWORD *wfwritten)
832 DWORD size;
833 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
834 DirectSoundDevice *device = This->device;
835 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
837 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
839 if (lpwf) { /* NULL is valid */
840 if (wfsize >= size) {
841 CopyMemory(lpwf,device->pwfx,size);
842 if (wfwritten)
843 *wfwritten = size;
844 } else {
845 WARN("invalid parameter: wfsize too small\n");
846 if (wfwritten)
847 *wfwritten = 0;
848 return DSERR_INVALIDPARAM;
850 } else {
851 if (wfwritten)
852 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
853 else {
854 WARN("invalid parameter: wfwritten == NULL\n");
855 return DSERR_INVALIDPARAM;
859 return DS_OK;
862 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
863 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
864 DWORD *audiobytes2, DWORD flags)
866 HRESULT hres;
867 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
868 DirectSoundDevice *device = This->device;
869 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
870 iface,
871 writecursor,
872 writebytes,
873 lplpaudioptr1,
874 audiobytes1,
875 lplpaudioptr2,
876 audiobytes2,
877 flags,
878 GetTickCount()
881 if (!audiobytes1)
882 return DSERR_INVALIDPARAM;
884 if (device->priolevel != DSSCL_WRITEPRIMARY) {
885 WARN("failed priority check!\n");
886 return DSERR_PRIOLEVELNEEDED;
889 /* when this flag is set, writecursor is meaningless and must be calculated */
890 if (flags & DSBLOCK_FROMWRITECURSOR) {
891 /* GetCurrentPosition does too much magic to duplicate here */
892 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
893 if (hres != DS_OK) {
894 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
895 return hres;
899 /* when this flag is set, writebytes is meaningless and must be set */
900 if (flags & DSBLOCK_ENTIREBUFFER)
901 writebytes = device->buflen;
903 if (writecursor >= device->buflen) {
904 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
905 writecursor, device->buflen);
906 return DSERR_INVALIDPARAM;
909 if (writebytes > device->buflen) {
910 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
911 writebytes, device->buflen);
912 return DSERR_INVALIDPARAM;
915 if (writecursor+writebytes <= device->buflen) {
916 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
917 *audiobytes1 = writebytes;
918 if (lplpaudioptr2)
919 *(LPBYTE*)lplpaudioptr2 = NULL;
920 if (audiobytes2)
921 *audiobytes2 = 0;
922 TRACE("->%d.0\n",writebytes);
923 } else {
924 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
925 *audiobytes1 = device->buflen-writecursor;
926 if (lplpaudioptr2)
927 *(LPBYTE*)lplpaudioptr2 = device->buffer;
928 if (audiobytes2)
929 *audiobytes2 = writebytes-(device->buflen-writecursor);
930 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
932 return DS_OK;
935 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
937 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
938 TRACE("(%p,%d)\n",This,newpos);
940 /* You cannot set the position of the primary buffer */
941 WARN("invalid call\n");
942 return DSERR_INVALIDCALL;
945 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
947 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
948 DirectSoundDevice *device = This->device;
949 float lvol, rvol;
950 HRESULT hr;
951 TRACE("(%p,%d)\n", iface, pan);
953 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
954 WARN("control unavailable\n");
955 return DSERR_CONTROLUNAVAIL;
958 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
959 WARN("invalid parameter: pan = %d\n", pan);
960 return DSERR_INVALIDPARAM;
963 /* **** */
964 EnterCriticalSection(&device->mixlock);
966 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
967 if(FAILED(hr)){
968 LeaveCriticalSection(&device->mixlock);
969 WARN("GetChannelVolume failed: %08x\n", hr);
970 return hr;
973 if(device->pwfx->nChannels > 1){
974 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
975 if(FAILED(hr)){
976 LeaveCriticalSection(&device->mixlock);
977 WARN("GetChannelVolume failed: %08x\n", hr);
978 return hr;
980 }else
981 rvol = 1;
983 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
984 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
986 DSOUND_AmpFactorToVolPan(&device->volpan);
987 if (pan != device->volpan.lPan) {
988 device->volpan.lPan=pan;
989 DSOUND_RecalcVolPan(&device->volpan);
991 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
992 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
993 if(FAILED(hr)){
994 LeaveCriticalSection(&device->mixlock);
995 WARN("SetChannelVolume failed: %08x\n", hr);
996 return hr;
999 if(device->pwfx->nChannels > 1){
1000 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
1001 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
1002 if(FAILED(hr)){
1003 LeaveCriticalSection(&device->mixlock);
1004 WARN("SetChannelVolume failed: %08x\n", hr);
1005 return hr;
1010 LeaveCriticalSection(&device->mixlock);
1011 /* **** */
1013 return DS_OK;
1016 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1018 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1019 DirectSoundDevice *device = This->device;
1020 float lvol, rvol;
1021 HRESULT hr;
1022 TRACE("(%p,%p)\n", iface, pan);
1024 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1025 WARN("control unavailable\n");
1026 return DSERR_CONTROLUNAVAIL;
1029 if (pan == NULL) {
1030 WARN("invalid parameter: pan == NULL\n");
1031 return DSERR_INVALIDPARAM;
1034 EnterCriticalSection(&device->mixlock);
1036 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1037 if(FAILED(hr)){
1038 LeaveCriticalSection(&device->mixlock);
1039 WARN("GetChannelVolume failed: %08x\n", hr);
1040 return hr;
1043 if(device->pwfx->nChannels > 1){
1044 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1045 if(FAILED(hr)){
1046 LeaveCriticalSection(&device->mixlock);
1047 WARN("GetChannelVolume failed: %08x\n", hr);
1048 return hr;
1050 }else
1051 rvol = 1;
1053 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1054 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1056 DSOUND_AmpFactorToVolPan(&device->volpan);
1057 *pan = device->volpan.lPan;
1059 LeaveCriticalSection(&device->mixlock);
1061 return DS_OK;
1064 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1065 void *p2, DWORD x2)
1067 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1068 DirectSoundDevice *device = This->device;
1069 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1071 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1072 WARN("failed priority check!\n");
1073 return DSERR_PRIOLEVELNEEDED;
1076 if((p1 && ((BYTE*)p1 < device->buffer ||
1077 (BYTE*)p1 >= device->buffer + device->buflen)) ||
1078 (p2 && ((BYTE*)p2 < device->buffer ||
1079 (BYTE*)p2 >= device->buffer + device->buflen)))
1080 return DSERR_INVALIDPARAM;
1082 return DS_OK;
1085 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1087 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1088 FIXME("(%p):stub\n",This);
1089 return DS_OK;
1092 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1094 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1095 DirectSoundDevice *device = This->device;
1096 TRACE("(%p,%p)\n", iface, freq);
1098 if (freq == NULL) {
1099 WARN("invalid parameter: freq == NULL\n");
1100 return DSERR_INVALIDPARAM;
1103 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1104 WARN("control unavailable\n");
1105 return DSERR_CONTROLUNAVAIL;
1108 *freq = device->pwfx->nSamplesPerSec;
1109 TRACE("-> %d\n", *freq);
1111 return DS_OK;
1114 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1115 const DSBUFFERDESC *dbsd)
1117 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1118 WARN("(%p) already initialized\n", This);
1119 return DSERR_ALREADYINITIALIZED;
1122 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1124 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1125 DirectSoundDevice *device = This->device;
1126 TRACE("(%p,%p)\n", iface, caps);
1128 if (caps == NULL) {
1129 WARN("invalid parameter: caps == NULL\n");
1130 return DSERR_INVALIDPARAM;
1133 if (caps->dwSize < sizeof(*caps)) {
1134 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1135 return DSERR_INVALIDPARAM;
1138 caps->dwFlags = This->dsbd.dwFlags;
1139 caps->dwBufferBytes = device->buflen;
1141 /* Windows reports these as zero */
1142 caps->dwUnlockTransferRate = 0;
1143 caps->dwPlayCpuOverhead = 0;
1145 return DS_OK;
1148 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1149 void **ppobj)
1151 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1153 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1155 if (ppobj == NULL) {
1156 WARN("invalid parameter\n");
1157 return E_INVALIDARG;
1160 *ppobj = NULL; /* assume failure */
1162 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1163 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1164 IDirectSoundBuffer_AddRef(iface);
1165 *ppobj = iface;
1166 return S_OK;
1169 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1170 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1171 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1172 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1173 return E_NOINTERFACE;
1176 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1177 ERR("app requested IDirectSoundNotify on primary buffer\n");
1178 /* FIXME: should we support this? */
1179 return E_NOINTERFACE;
1182 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1183 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1184 return E_NOINTERFACE;
1187 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1188 *ppobj = &This->IDirectSound3DListener_iface;
1189 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1190 return S_OK;
1193 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1194 *ppobj = &This->IKsPropertySet_iface;
1195 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1196 return S_OK;
1199 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1200 return E_NOINTERFACE;
1203 static const IDirectSoundBufferVtbl dspbvt =
1205 PrimaryBufferImpl_QueryInterface,
1206 PrimaryBufferImpl_AddRef,
1207 PrimaryBufferImpl_Release,
1208 PrimaryBufferImpl_GetCaps,
1209 PrimaryBufferImpl_GetCurrentPosition,
1210 PrimaryBufferImpl_GetFormat,
1211 PrimaryBufferImpl_GetVolume,
1212 PrimaryBufferImpl_GetPan,
1213 PrimaryBufferImpl_GetFrequency,
1214 PrimaryBufferImpl_GetStatus,
1215 PrimaryBufferImpl_Initialize,
1216 PrimaryBufferImpl_Lock,
1217 PrimaryBufferImpl_Play,
1218 PrimaryBufferImpl_SetCurrentPosition,
1219 PrimaryBufferImpl_SetFormat,
1220 PrimaryBufferImpl_SetVolume,
1221 PrimaryBufferImpl_SetPan,
1222 PrimaryBufferImpl_SetFrequency,
1223 PrimaryBufferImpl_Stop,
1224 PrimaryBufferImpl_Unlock,
1225 PrimaryBufferImpl_Restore
1228 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1229 const DSBUFFERDESC *dsbd)
1231 IDirectSoundBufferImpl *dsb;
1232 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1234 if (dsbd->lpwfxFormat) {
1235 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1236 *ppdsb = NULL;
1237 return DSERR_INVALIDPARAM;
1240 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1242 if (dsb == NULL) {
1243 WARN("out of memory\n");
1244 *ppdsb = NULL;
1245 return DSERR_OUTOFMEMORY;
1248 dsb->ref = 0;
1249 dsb->ref3D = 0;
1250 dsb->refiks = 0;
1251 dsb->numIfaces = 0;
1252 dsb->device = device;
1253 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1254 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1255 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1256 dsb->dsbd = *dsbd;
1258 /* IDirectSound3DListener */
1259 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1260 device->ds3dl.vPosition.x = 0.0;
1261 device->ds3dl.vPosition.y = 0.0;
1262 device->ds3dl.vPosition.z = 0.0;
1263 device->ds3dl.vVelocity.x = 0.0;
1264 device->ds3dl.vVelocity.y = 0.0;
1265 device->ds3dl.vVelocity.z = 0.0;
1266 device->ds3dl.vOrientFront.x = 0.0;
1267 device->ds3dl.vOrientFront.y = 0.0;
1268 device->ds3dl.vOrientFront.z = 1.0;
1269 device->ds3dl.vOrientTop.x = 0.0;
1270 device->ds3dl.vOrientTop.y = 1.0;
1271 device->ds3dl.vOrientTop.z = 0.0;
1272 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1273 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1274 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1275 device->ds3dl_need_recalc = TRUE;
1277 TRACE("Created primary buffer at %p\n", dsb);
1278 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1279 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1280 device->pwfx->wFormatTag, device->pwfx->nChannels,
1281 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1282 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1283 device->pwfx->cbSize);
1285 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1286 *ppdsb = dsb;
1287 return S_OK;