mlang/tests: Fix a pointer casting warning on 64-bit.
[wine/testsucceed.git] / dlls / dsound / primary.c
blob288466ad1034de8d56999a1d932c22e5895b20b1
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
22 #include <stdarg.h>
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "mmsystem.h"
30 #include "winternl.h"
31 #include "mmddk.h"
32 #include "wine/debug.h"
33 #include "dsound.h"
34 #include "dsdriver.h"
35 #include "dsound_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
39 /** Calculate how long a fragment length of about 10 ms should be in frames
41 * nSamplesPerSec: Frequency rate in samples per second
42 * nBlockAlign: Size of a single blockalign
44 * Returns:
45 * Size in bytes of a single fragment
47 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
49 /* Given a timer delay of 10ms, the fragment size is approximately:
50 * fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
51 * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
53 * ALSA uses buffers that are powers of 2. Because of this, fraglen
54 * is rounded up to the nearest power of 2:
57 if (nSamplesPerSec <= 12800)
58 return 128 * nBlockAlign;
60 if (nSamplesPerSec <= 25600)
61 return 256 * nBlockAlign;
63 if (nSamplesPerSec <= 51200)
64 return 512 * nBlockAlign;
66 return 1024 * nBlockAlign;
69 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
71 TRACE("(%p)\n", device);
73 device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
74 device->helfrags = device->buflen / device->fraglen;
75 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
77 if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
78 device->writelead = 0;
79 else
80 /* calculate the 10ms write lead */
81 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
84 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
86 HRESULT hres = DS_OK;
87 TRACE("(%p, %d)\n", device, forcewave);
89 if (device->driver)
91 IDsDriver_Close(device->driver);
92 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
93 waveOutClose(device->hwo);
94 IDsDriver_Release(device->driver);
95 device->driver = NULL;
96 device->buffer = NULL;
97 device->hwo = 0;
99 else if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
100 waveOutClose(device->hwo);
102 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
103 if (ds_hw_accel != DS_HW_ACCEL_EMULATION && !forcewave)
104 waveOutMessage((HWAVEOUT)device->drvdesc.dnDevNode, DRV_QUERYDSOUNDIFACE, (DWORD_PTR)&device->driver, 0);
106 /* Get driver description */
107 if (device->driver) {
108 DWORD wod = device->drvdesc.dnDevNode;
109 hres = IDsDriver_GetDriverDesc(device->driver,&(device->drvdesc));
110 device->drvdesc.dnDevNode = wod;
111 if (FAILED(hres)) {
112 WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres);
113 IDsDriver_Release(device->driver);
114 device->driver = NULL;
118 /* if no DirectSound interface available, use WINMM API instead */
119 if (!device->driver)
120 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
122 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
124 DWORD flags = CALLBACK_FUNCTION;
126 if (device->driver)
127 flags |= WAVE_DIRECTSOUND;
129 hres = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode, device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device, flags));
130 if (FAILED(hres)) {
131 WARN("waveOutOpen failed\n");
132 if (device->driver)
134 IDsDriver_Release(device->driver);
135 device->driver = NULL;
137 return hres;
141 if (device->driver)
142 hres = IDsDriver_Open(device->driver);
144 return hres;
147 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
149 DWORD buflen;
150 HRESULT err = DS_OK;
151 TRACE("(%p)\n", device);
153 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
154 on windows this size is always fixed (tested on win-xp) */
155 if (!device->buflen)
156 device->buflen = ds_hel_buflen;
157 buflen = device->buflen;
158 buflen -= buflen % device->pwfx->nBlockAlign;
159 device->buflen = buflen;
161 if (device->driver)
163 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
164 DSBCAPS_PRIMARYBUFFER,0,
165 &(device->buflen),&(device->buffer),
166 (LPVOID*)&(device->hwbuf));
168 if (err != DS_OK) {
169 WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err);
170 err = DSOUND_ReopenDevice(device, TRUE);
171 if (FAILED(err))
173 WARN("Falling back to waveout failed too! Giving up\n");
174 return err;
177 if (device->hwbuf)
178 IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
180 DSOUND_RecalcPrimary(device);
181 device->prebuf = ds_snd_queue_max;
182 if (device->helfrags < ds_snd_queue_min)
184 WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device->buflen, ds_snd_queue_min * device->fraglen);
185 device->buflen = buflen;
186 IDsDriverBuffer_Release(device->hwbuf);
187 device->hwbuf = NULL;
188 err = DSOUND_ReopenDevice(device, TRUE);
189 if (FAILED(err))
191 WARN("Falling back to waveout failed too! Giving up\n");
192 return err;
195 else if (device->helfrags < ds_snd_queue_max)
196 device->prebuf = device->helfrags;
199 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
200 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
201 if (!device->mix_buffer)
203 if (device->hwbuf)
204 IDsDriverBuffer_Release(device->hwbuf);
205 device->hwbuf = NULL;
206 return DSERR_OUTOFMEMORY;
209 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
210 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
212 /* are we using waveOut stuff? */
213 if (!device->driver) {
214 LPBYTE newbuf;
215 LPWAVEHDR headers = NULL;
216 DWORD overshot;
217 unsigned int c;
219 /* Start in pause mode, to allow buffers to get filled */
220 waveOutPause(device->hwo);
222 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
224 /* reallocate emulated primary buffer */
225 if (device->buffer)
226 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
227 else
228 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
230 if (!newbuf) {
231 ERR("failed to allocate primary buffer\n");
232 return DSERR_OUTOFMEMORY;
233 /* but the old buffer might still exist and must be re-prepared */
236 DSOUND_RecalcPrimary(device);
237 if (device->pwave)
238 headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
239 else
240 headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
242 if (!headers) {
243 ERR("failed to allocate wave headers\n");
244 HeapFree(GetProcessHeap(), 0, newbuf);
245 DSOUND_RecalcPrimary(device);
246 return DSERR_OUTOFMEMORY;
249 device->buffer = newbuf;
250 device->pwave = headers;
252 /* prepare fragment headers */
253 for (c=0; c<device->helfrags; c++) {
254 device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
255 device->pwave[c].dwBufferLength = device->fraglen;
256 device->pwave[c].dwUser = (DWORD)device;
257 device->pwave[c].dwFlags = 0;
258 device->pwave[c].dwLoops = 0;
259 err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
260 if (err != DS_OK) {
261 while (c--)
262 waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
263 break;
267 overshot = device->buflen % device->fraglen;
268 /* sanity */
269 if(overshot)
271 overshot -= overshot % device->pwfx->nBlockAlign;
272 device->pwave[device->helfrags - 1].dwBufferLength += overshot;
275 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
277 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
278 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
279 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
280 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
281 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
282 return err;
286 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
288 TRACE("(%p)\n", device);
290 /* are we using waveOut stuff? */
291 if (!device->hwbuf) {
292 unsigned c;
294 /* get out of CS when calling the wave system */
295 LeaveCriticalSection(&(device->mixlock));
296 /* **** */
297 device->pwqueue = (DWORD)-1; /* resetting queues */
298 waveOutReset(device->hwo);
299 for (c=0; c<device->helfrags; c++)
300 waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
301 /* **** */
302 EnterCriticalSection(&(device->mixlock));
304 /* clear the queue */
305 device->pwqueue = 0;
306 } else {
307 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
308 if (!ref)
309 device->hwbuf = 0;
310 else
311 ERR("Still %d references on primary buffer, refcount leak?\n", ref);
315 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
317 HRESULT err = DS_OK;
318 TRACE("(%p)\n", device);
320 device->buflen = ds_hel_buflen;
321 err = DSOUND_PrimaryOpen(device);
323 if (err != DS_OK) {
324 WARN("DSOUND_PrimaryOpen failed\n");
325 return err;
328 device->state = STATE_STOPPED;
329 return DS_OK;
332 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
334 TRACE("(%p)\n", device);
336 /* **** */
337 EnterCriticalSection(&(device->mixlock));
339 DSOUND_PrimaryClose(device);
340 if (device->driver) {
341 if (device->hwbuf) {
342 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
343 device->hwbuf = 0;
345 } else
346 HeapFree(GetProcessHeap(),0,device->pwave);
347 HeapFree(GetProcessHeap(),0,device->pwfx);
348 device->pwfx=NULL;
350 LeaveCriticalSection(&(device->mixlock));
351 /* **** */
353 return DS_OK;
356 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
358 HRESULT err = DS_OK;
359 TRACE("(%p)\n", device);
361 if (device->hwbuf) {
362 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
363 if (err != DS_OK)
364 WARN("IDsDriverBuffer_Play failed\n");
365 } else {
366 err = mmErr(waveOutRestart(device->hwo));
367 if (err != DS_OK)
368 WARN("waveOutRestart failed\n");
371 return err;
374 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
376 HRESULT err = DS_OK;
377 TRACE("(%p)\n", device);
379 if (device->hwbuf) {
380 err = IDsDriverBuffer_Stop(device->hwbuf);
381 if (err == DSERR_BUFFERLOST) {
382 DSOUND_PrimaryClose(device);
383 err = DSOUND_ReopenDevice(device, FALSE);
384 if (FAILED(err))
385 ERR("DSOUND_ReopenDevice failed\n");
386 else
388 err = DSOUND_PrimaryOpen(device);
389 if (FAILED(err))
390 WARN("DSOUND_PrimaryOpen failed\n");
392 } else if (err != DS_OK) {
393 WARN("IDsDriverBuffer_Stop failed\n");
395 } else {
397 /* don't call the wave system with the lock set */
398 LeaveCriticalSection(&(device->mixlock));
399 /* **** */
401 err = mmErr(waveOutPause(device->hwo));
403 /* **** */
404 EnterCriticalSection(&(device->mixlock));
406 if (err != DS_OK)
407 WARN("waveOutPause failed\n");
410 return err;
413 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
415 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
417 if (device->hwbuf) {
418 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
419 if (err != S_OK) {
420 WARN("IDsDriverBuffer_GetPosition failed\n");
421 return err;
423 } else {
424 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
426 /* check if playpos was requested */
427 if (playpos)
428 /* use the cached play position */
429 *playpos = device->pwplay * device->fraglen;
431 /* check if writepos was requested */
432 if (writepos)
433 /* the writepos is the first non-queued position */
434 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
436 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
437 return DS_OK;
440 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
442 HRESULT err = DSERR_BUFFERLOST;
443 int i, alloc_size, cp_size;
444 DWORD nSamplesPerSec, bpp, chans;
445 TRACE("(%p,%p)\n", device, wfex);
447 if (device->priolevel == DSSCL_NORMAL) {
448 WARN("failed priority check!\n");
449 return DSERR_PRIOLEVELNEEDED;
452 /* Let's be pedantic! */
453 if (wfex == NULL) {
454 WARN("invalid parameter: wfex==NULL!\n");
455 return DSERR_INVALIDPARAM;
457 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
458 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
459 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
460 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
461 wfex->wBitsPerSample, wfex->cbSize);
463 /* **** */
464 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
465 EnterCriticalSection(&(device->mixlock));
467 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
468 alloc_size = sizeof(WAVEFORMATEX);
469 cp_size = sizeof(PCMWAVEFORMAT);
470 } else
471 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
473 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
475 nSamplesPerSec = device->pwfx->nSamplesPerSec;
476 bpp = device->pwfx->wBitsPerSample;
477 chans = device->pwfx->nChannels;
479 CopyMemory(device->pwfx, wfex, cp_size);
481 if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
482 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
484 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
485 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
487 err = DSERR_BUFFERLOST;
488 CopyMemory(device->pwfx, wfex, cp_size);
491 if (err != DSERR_BUFFERLOST && FAILED(err)) {
492 WARN("IDsDriverBuffer_SetFormat failed\n");
493 if (!forced)
494 err = DS_OK;
495 goto done;
498 if (err == S_FALSE)
500 /* ALSA specific: S_FALSE tells that recreation was successful,
501 * but size and location may be changed, and buffer has to be restarted
502 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
503 * and the entire re-initialization will occur anyway
505 IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
506 IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
508 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
509 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
510 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
511 err = DS_OK;
513 DSOUND_RecalcPrimary(device);
516 if (err == DSERR_BUFFERLOST)
518 DSOUND_PrimaryClose(device);
520 err = DSOUND_ReopenDevice(device, FALSE);
521 if (FAILED(err))
523 WARN("DSOUND_ReopenDevice failed: %08x\n", err);
524 goto done;
526 err = DSOUND_PrimaryOpen(device);
527 if (err != DS_OK) {
528 WARN("DSOUND_PrimaryOpen failed\n");
529 goto done;
532 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
534 DSOUND_PrimaryClose(device);
535 device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
536 err = DSOUND_ReopenDevice(device, TRUE);
537 if (FAILED(err))
538 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
539 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
540 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
544 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
545 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
546 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
547 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
548 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
550 if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
551 IDirectSoundBufferImpl** dsb = device->buffers;
552 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
553 /* **** */
554 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
556 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
557 DSOUND_RecalcFormat((*dsb));
558 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen, FALSE);
559 (*dsb)->primary_mixpos = 0;
561 RtlReleaseResource(&(*dsb)->lock);
562 /* **** */
566 done:
567 LeaveCriticalSection(&(device->mixlock));
568 RtlReleaseResource(&(device->buffer_list_lock));
569 /* **** */
571 return err;
574 /*******************************************************************************
575 * PrimaryBuffer
577 /* This sets this format for the <em>Primary Buffer Only</em> */
578 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
579 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
580 LPDIRECTSOUNDBUFFER iface,
581 LPCWAVEFORMATEX wfex)
583 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
584 TRACE("(%p,%p)\n", iface, wfex);
585 return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
588 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
589 LPDIRECTSOUNDBUFFER iface,LONG vol
591 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
592 DWORD ampfactors;
593 HRESULT hres = DS_OK;
594 TRACE("(%p,%d)\n", iface, vol);
596 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
597 WARN("control unavailable\n");
598 return DSERR_CONTROLUNAVAIL;
601 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
602 WARN("invalid parameter: vol = %d\n", vol);
603 return DSERR_INVALIDPARAM;
606 /* **** */
607 EnterCriticalSection(&(device->mixlock));
609 waveOutGetVolume(device->hwo, &ampfactors);
610 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
611 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
612 DSOUND_AmpFactorToVolPan(&device->volpan);
613 if (vol != device->volpan.lVolume) {
614 device->volpan.lVolume=vol;
615 DSOUND_RecalcVolPan(&device->volpan);
616 if (device->hwbuf) {
617 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
618 if (hres != DS_OK)
619 WARN("IDsDriverBuffer_SetVolumePan failed\n");
620 } else {
621 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
622 waveOutSetVolume(device->hwo, ampfactors);
626 LeaveCriticalSection(&(device->mixlock));
627 /* **** */
629 return hres;
632 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
633 LPDIRECTSOUNDBUFFER iface,LPLONG vol
635 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
636 DWORD ampfactors;
637 TRACE("(%p,%p)\n", iface, vol);
639 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
640 WARN("control unavailable\n");
641 return DSERR_CONTROLUNAVAIL;
644 if (vol == NULL) {
645 WARN("invalid parameter: vol = NULL\n");
646 return DSERR_INVALIDPARAM;
649 if (!device->hwbuf)
651 waveOutGetVolume(device->hwo, &ampfactors);
652 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
653 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
654 DSOUND_AmpFactorToVolPan(&device->volpan);
656 *vol = device->volpan.lVolume;
657 return DS_OK;
660 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
661 LPDIRECTSOUNDBUFFER iface,DWORD freq
663 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
664 TRACE("(%p,%d)\n",This,freq);
666 /* You cannot set the frequency of the primary buffer */
667 WARN("control unavailable\n");
668 return DSERR_CONTROLUNAVAIL;
671 static HRESULT WINAPI PrimaryBufferImpl_Play(
672 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
674 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
675 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
677 if (!(flags & DSBPLAY_LOOPING)) {
678 WARN("invalid parameter: flags = %08x\n", flags);
679 return DSERR_INVALIDPARAM;
682 /* **** */
683 EnterCriticalSection(&(device->mixlock));
685 if (device->state == STATE_STOPPED)
686 device->state = STATE_STARTING;
687 else if (device->state == STATE_STOPPING)
688 device->state = STATE_PLAYING;
690 LeaveCriticalSection(&(device->mixlock));
691 /* **** */
693 return DS_OK;
696 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
698 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
699 TRACE("(%p)\n", iface);
701 /* **** */
702 EnterCriticalSection(&(device->mixlock));
704 if (device->state == STATE_PLAYING)
705 device->state = STATE_STOPPING;
706 else if (device->state == STATE_STARTING)
707 device->state = STATE_STOPPED;
709 LeaveCriticalSection(&(device->mixlock));
710 /* **** */
712 return DS_OK;
715 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
717 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
718 ULONG ref = InterlockedIncrement(&(This->ref));
719 TRACE("(%p) ref was %d\n", This, ref - 1);
720 return ref;
723 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
725 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
726 DWORD ref = InterlockedDecrement(&(This->ref));
727 TRACE("(%p) ref was %d\n", This, ref + 1);
729 if (!ref) {
730 This->device->primary = NULL;
731 HeapFree(GetProcessHeap(), 0, This);
732 TRACE("(%p) released\n", This);
734 return ref;
737 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
738 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
740 HRESULT hres;
741 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
742 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
744 /* **** */
745 EnterCriticalSection(&(device->mixlock));
747 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
748 if (hres != DS_OK) {
749 WARN("DSOUND_PrimaryGetPosition failed\n");
750 LeaveCriticalSection(&(device->mixlock));
751 return hres;
753 if (writepos) {
754 if (device->state != STATE_STOPPED)
755 /* apply the documented 10ms lead to writepos */
756 *writepos += device->writelead;
757 while (*writepos >= device->buflen) *writepos -= device->buflen;
760 LeaveCriticalSection(&(device->mixlock));
761 /* **** */
763 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
764 return DS_OK;
767 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
768 LPDIRECTSOUNDBUFFER iface,LPDWORD status
770 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
771 TRACE("(%p,%p)\n", iface, status);
773 if (status == NULL) {
774 WARN("invalid parameter: status == NULL\n");
775 return DSERR_INVALIDPARAM;
778 *status = 0;
779 if ((device->state == STATE_STARTING) ||
780 (device->state == STATE_PLAYING))
781 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
783 TRACE("status=%x\n", *status);
784 return DS_OK;
788 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
789 LPDIRECTSOUNDBUFFER iface,
790 LPWAVEFORMATEX lpwf,
791 DWORD wfsize,
792 LPDWORD wfwritten)
794 DWORD size;
795 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
796 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
798 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
800 if (lpwf) { /* NULL is valid */
801 if (wfsize >= size) {
802 CopyMemory(lpwf,device->pwfx,size);
803 if (wfwritten)
804 *wfwritten = size;
805 } else {
806 WARN("invalid parameter: wfsize too small\n");
807 if (wfwritten)
808 *wfwritten = 0;
809 return DSERR_INVALIDPARAM;
811 } else {
812 if (wfwritten)
813 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
814 else {
815 WARN("invalid parameter: wfwritten == NULL\n");
816 return DSERR_INVALIDPARAM;
820 return DS_OK;
823 static HRESULT WINAPI PrimaryBufferImpl_Lock(
824 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
826 HRESULT hres;
827 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
828 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
829 iface,
830 writecursor,
831 writebytes,
832 lplpaudioptr1,
833 audiobytes1,
834 lplpaudioptr2,
835 audiobytes2,
836 flags,
837 GetTickCount()
840 if (!audiobytes1)
841 return DSERR_INVALIDPARAM;
843 if (device->priolevel != DSSCL_WRITEPRIMARY) {
844 WARN("failed priority check!\n");
845 return DSERR_PRIOLEVELNEEDED;
848 /* when this flag is set, writecursor is meaningless and must be calculated */
849 if (flags & DSBLOCK_FROMWRITECURSOR) {
850 /* GetCurrentPosition does too much magic to duplicate here */
851 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
852 if (hres != DS_OK) {
853 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
854 return hres;
858 /* when this flag is set, writebytes is meaningless and must be set */
859 if (flags & DSBLOCK_ENTIREBUFFER)
860 writebytes = device->buflen;
862 if (writecursor >= device->buflen) {
863 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
864 writecursor, device->buflen);
865 return DSERR_INVALIDPARAM;
868 if (writebytes > device->buflen) {
869 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
870 writebytes, device->buflen);
871 return DSERR_INVALIDPARAM;
874 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
875 hres = IDsDriverBuffer_Lock(device->hwbuf,
876 lplpaudioptr1, audiobytes1,
877 lplpaudioptr2, audiobytes2,
878 writecursor, writebytes,
880 if (hres != DS_OK) {
881 WARN("IDsDriverBuffer_Lock failed\n");
882 return hres;
884 } else {
885 if (writecursor+writebytes <= device->buflen) {
886 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
887 *audiobytes1 = writebytes;
888 if (lplpaudioptr2)
889 *(LPBYTE*)lplpaudioptr2 = NULL;
890 if (audiobytes2)
891 *audiobytes2 = 0;
892 TRACE("->%d.0\n",writebytes);
893 } else {
894 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
895 *audiobytes1 = device->buflen-writecursor;
896 if (lplpaudioptr2)
897 *(LPBYTE*)lplpaudioptr2 = device->buffer;
898 if (audiobytes2)
899 *audiobytes2 = writebytes-(device->buflen-writecursor);
900 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
903 return DS_OK;
906 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
907 LPDIRECTSOUNDBUFFER iface,DWORD newpos
909 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
910 TRACE("(%p,%d)\n",This,newpos);
912 /* You cannot set the position of the primary buffer */
913 WARN("invalid call\n");
914 return DSERR_INVALIDCALL;
917 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
918 LPDIRECTSOUNDBUFFER iface,LONG pan
920 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
921 DWORD ampfactors;
922 HRESULT hres = DS_OK;
923 TRACE("(%p,%d)\n", iface, pan);
925 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
926 WARN("control unavailable\n");
927 return DSERR_CONTROLUNAVAIL;
930 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
931 WARN("invalid parameter: pan = %d\n", pan);
932 return DSERR_INVALIDPARAM;
935 /* **** */
936 EnterCriticalSection(&(device->mixlock));
938 if (!device->hwbuf)
940 waveOutGetVolume(device->hwo, &ampfactors);
941 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
942 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
943 DSOUND_AmpFactorToVolPan(&device->volpan);
945 if (pan != device->volpan.lPan) {
946 device->volpan.lPan=pan;
947 DSOUND_RecalcVolPan(&device->volpan);
948 if (device->hwbuf) {
949 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
950 if (hres != DS_OK)
951 WARN("IDsDriverBuffer_SetVolumePan failed\n");
952 } else {
953 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
954 waveOutSetVolume(device->hwo, ampfactors);
958 LeaveCriticalSection(&(device->mixlock));
959 /* **** */
961 return hres;
964 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
965 LPDIRECTSOUNDBUFFER iface,LPLONG pan
967 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
968 DWORD ampfactors;
969 TRACE("(%p,%p)\n", iface, pan);
971 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
972 WARN("control unavailable\n");
973 return DSERR_CONTROLUNAVAIL;
976 if (pan == NULL) {
977 WARN("invalid parameter: pan == NULL\n");
978 return DSERR_INVALIDPARAM;
981 if (!device->hwbuf)
983 waveOutGetVolume(device->hwo, &ampfactors);
984 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
985 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
986 DSOUND_AmpFactorToVolPan(&device->volpan);
988 *pan = device->volpan.lPan;
989 return DS_OK;
992 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
993 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
995 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
996 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
998 if (device->priolevel != DSSCL_WRITEPRIMARY) {
999 WARN("failed priority check!\n");
1000 return DSERR_PRIOLEVELNEEDED;
1003 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
1004 HRESULT hres;
1006 if ((char *)p1 - (char *)device->buffer + x1 > device->buflen)
1007 hres = DSERR_INVALIDPARAM;
1008 else
1009 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
1011 if (hres != DS_OK) {
1012 WARN("IDsDriverBuffer_Unlock failed\n");
1013 return hres;
1017 return DS_OK;
1020 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1021 LPDIRECTSOUNDBUFFER iface
1023 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1024 FIXME("(%p):stub\n",This);
1025 return DS_OK;
1028 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1029 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1031 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1032 TRACE("(%p,%p)\n", iface, freq);
1034 if (freq == NULL) {
1035 WARN("invalid parameter: freq == NULL\n");
1036 return DSERR_INVALIDPARAM;
1039 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1040 WARN("control unavailable\n");
1041 return DSERR_CONTROLUNAVAIL;
1044 *freq = device->pwfx->nSamplesPerSec;
1045 TRACE("-> %d\n", *freq);
1047 return DS_OK;
1050 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1051 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1053 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1054 WARN("(%p) already initialized\n", This);
1055 return DSERR_ALREADYINITIALIZED;
1058 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1059 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1061 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1062 TRACE("(%p,%p)\n", iface, caps);
1064 if (caps == NULL) {
1065 WARN("invalid parameter: caps == NULL\n");
1066 return DSERR_INVALIDPARAM;
1069 if (caps->dwSize < sizeof(*caps)) {
1070 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1071 return DSERR_INVALIDPARAM;
1074 caps->dwFlags = device->dsbd.dwFlags;
1075 caps->dwBufferBytes = device->buflen;
1077 /* Windows reports these as zero */
1078 caps->dwUnlockTransferRate = 0;
1079 caps->dwPlayCpuOverhead = 0;
1081 return DS_OK;
1084 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1085 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1087 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1088 DirectSoundDevice *device = This->device;
1089 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1091 if (ppobj == NULL) {
1092 WARN("invalid parameter\n");
1093 return E_INVALIDARG;
1096 *ppobj = NULL; /* assume failure */
1098 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1099 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1100 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1101 *ppobj = This;
1102 return S_OK;
1105 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1106 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1107 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1108 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1109 return E_NOINTERFACE;
1112 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1113 ERR("app requested IDirectSoundNotify on primary buffer\n");
1114 /* FIXME: should we support this? */
1115 return E_NOINTERFACE;
1118 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1119 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1120 return E_NOINTERFACE;
1123 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1124 if (!device->listener)
1125 IDirectSound3DListenerImpl_Create(device, &device->listener);
1126 if (device->listener) {
1127 *ppobj = device->listener;
1128 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1129 return S_OK;
1132 WARN("IID_IDirectSound3DListener failed\n");
1133 return E_NOINTERFACE;
1136 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1137 FIXME("app requested IKsPropertySet on primary buffer\n");
1138 return E_NOINTERFACE;
1141 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1142 return E_NOINTERFACE;
1145 static const IDirectSoundBufferVtbl dspbvt =
1147 PrimaryBufferImpl_QueryInterface,
1148 PrimaryBufferImpl_AddRef,
1149 PrimaryBufferImpl_Release,
1150 PrimaryBufferImpl_GetCaps,
1151 PrimaryBufferImpl_GetCurrentPosition,
1152 PrimaryBufferImpl_GetFormat,
1153 PrimaryBufferImpl_GetVolume,
1154 PrimaryBufferImpl_GetPan,
1155 PrimaryBufferImpl_GetFrequency,
1156 PrimaryBufferImpl_GetStatus,
1157 PrimaryBufferImpl_Initialize,
1158 PrimaryBufferImpl_Lock,
1159 PrimaryBufferImpl_Play,
1160 PrimaryBufferImpl_SetCurrentPosition,
1161 PrimaryBufferImpl_SetFormat,
1162 PrimaryBufferImpl_SetVolume,
1163 PrimaryBufferImpl_SetPan,
1164 PrimaryBufferImpl_SetFrequency,
1165 PrimaryBufferImpl_Stop,
1166 PrimaryBufferImpl_Unlock,
1167 PrimaryBufferImpl_Restore
1170 HRESULT PrimaryBufferImpl_Create(
1171 DirectSoundDevice * device,
1172 PrimaryBufferImpl ** ppdsb,
1173 LPCDSBUFFERDESC dsbd)
1175 PrimaryBufferImpl *dsb;
1176 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1178 if (dsbd->lpwfxFormat) {
1179 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1180 *ppdsb = NULL;
1181 return DSERR_INVALIDPARAM;
1184 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1186 if (dsb == NULL) {
1187 WARN("out of memory\n");
1188 *ppdsb = NULL;
1189 return DSERR_OUTOFMEMORY;
1192 dsb->ref = 0;
1193 dsb->device = device;
1194 dsb->lpVtbl = &dspbvt;
1196 device->dsbd = *dsbd;
1198 TRACE("Created primary buffer at %p\n", dsb);
1199 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1200 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1201 device->pwfx->wFormatTag, device->pwfx->nChannels,
1202 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1203 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1204 device->pwfx->cbSize);
1206 *ppdsb = dsb;
1207 return S_OK;