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
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
32 #include "wine/debug.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
45 * Size in bytes of a single fragment
47 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
49 DWORD fraglen = 512 * nBlockAlign;
51 /* Compensate for only being roughly accurate */
52 if (nSamplesPerSec <= 26000)
55 if (nSamplesPerSec <= 12000)
58 if (nSamplesPerSec >= 80000)
64 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
66 TRACE("(%p)\n", device);
68 device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
69 device->helfrags = device->buflen / device->fraglen;
70 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
72 if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
73 device->writelead = 0;
75 /* calculate the 10ms write lead */
76 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
79 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
82 TRACE("(%p, %d)\n", device, forcewave);
86 IDsDriver_Close(device->driver);
87 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
88 waveOutClose(device->hwo);
89 IDsDriver_Release(device->driver);
90 device->driver = NULL;
91 device->buffer = NULL;
94 else if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
95 waveOutClose(device->hwo);
97 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
98 if (ds_hw_accel != DS_HW_ACCEL_EMULATION && !forcewave)
99 waveOutMessage((HWAVEOUT)device->drvdesc.dnDevNode, DRV_QUERYDSOUNDIFACE, (DWORD_PTR)&device->driver, 0);
101 /* Get driver description */
102 if (device->driver) {
103 DWORD wod = device->drvdesc.dnDevNode;
104 hres = IDsDriver_GetDriverDesc(device->driver,&(device->drvdesc));
105 device->drvdesc.dnDevNode = wod;
107 WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres);
108 IDsDriver_Release(device->driver);
109 device->driver = NULL;
113 /* if no DirectSound interface available, use WINMM API instead */
115 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
117 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
119 DWORD flags = CALLBACK_FUNCTION;
122 flags |= WAVE_DIRECTSOUND;
124 hres = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode, device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device, flags));
126 WARN("waveOutOpen failed\n");
129 IDsDriver_Release(device->driver);
130 device->driver = NULL;
137 hres = IDsDriver_Open(device->driver);
142 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
146 TRACE("(%p)\n", device);
148 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
149 on windows this size is always fixed (tested on win-xp) */
151 buflen = ds_hel_buflen;
152 else /* In case we move from hw accelerated to waveout */
153 buflen = device->buflen;
154 buflen -= buflen % device->pwfx->nBlockAlign;
155 device->buflen = buflen;
159 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
160 DSBCAPS_PRIMARYBUFFER,0,
161 &(device->buflen),&(device->buffer),
162 (LPVOID*)&(device->hwbuf));
165 WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err);
166 err = DSOUND_ReopenDevice(device, TRUE);
169 WARN("Falling back to waveout failed too! Giving up\n");
173 DSOUND_RecalcPrimary(device);
174 device->prebuf = ds_snd_queue_max;
175 if (device->helfrags < ds_snd_queue_min)
177 WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device->buflen, ds_snd_queue_min * device->fraglen);
178 device->buflen = buflen;
179 IDsDriverBuffer_Release(device->hwbuf);
180 device->hwbuf = NULL;
181 err = DSOUND_ReopenDevice(device, TRUE);
184 WARN("Falling back to waveout failed too! Giving up\n");
188 else if (device->helfrags < ds_snd_queue_max)
189 device->prebuf = device->helfrags;
192 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
193 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
195 /* are we using waveOut stuff? */
196 if (!device->driver) {
198 LPWAVEHDR headers = NULL;
202 /* Start in pause mode, to allow buffers to get filled */
203 waveOutPause(device->hwo);
205 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
207 /* reallocate emulated primary buffer */
209 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
211 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
214 ERR("failed to allocate primary buffer\n");
215 return DSERR_OUTOFMEMORY;
216 /* but the old buffer might still exist and must be re-prepared */
219 DSOUND_RecalcPrimary(device);
221 headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
223 headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
226 ERR("failed to allocate wave headers\n");
227 HeapFree(GetProcessHeap(), 0, newbuf);
228 DSOUND_RecalcPrimary(device);
229 return DSERR_OUTOFMEMORY;
232 device->buffer = newbuf;
233 device->pwave = headers;
235 /* prepare fragment headers */
236 for (c=0; c<device->helfrags; c++) {
237 device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
238 device->pwave[c].dwBufferLength = device->fraglen;
239 device->pwave[c].dwUser = (DWORD)device;
240 device->pwave[c].dwFlags = 0;
241 device->pwave[c].dwLoops = 0;
242 err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
245 waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
250 overshot = device->buflen % device->fraglen;
254 overshot -= overshot % device->pwfx->nBlockAlign;
255 device->pwave[device->helfrags - 1].dwBufferLength += overshot;
258 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
260 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
261 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
266 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
268 TRACE("(%p)\n", device);
270 /* are we using waveOut stuff? */
271 if (!device->hwbuf) {
274 /* get out of CS when calling the wave system */
275 LeaveCriticalSection(&(device->mixlock));
277 device->pwqueue = (DWORD)-1; /* resetting queues */
278 waveOutReset(device->hwo);
279 for (c=0; c<device->helfrags; c++)
280 waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
282 EnterCriticalSection(&(device->mixlock));
284 /* clear the queue */
287 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
291 ERR("Still %d references on primary buffer, refcount leak?\n", ref);
295 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
298 TRACE("(%p)\n", device);
300 device->buflen = ds_hel_buflen;
301 err = DSOUND_PrimaryOpen(device);
304 WARN("DSOUND_PrimaryOpen failed\n");
308 device->state = STATE_STOPPED;
312 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
314 TRACE("(%p)\n", device);
317 EnterCriticalSection(&(device->mixlock));
319 DSOUND_PrimaryClose(device);
320 if (device->driver) {
322 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
326 HeapFree(GetProcessHeap(),0,device->pwave);
327 HeapFree(GetProcessHeap(),0,device->pwfx);
330 LeaveCriticalSection(&(device->mixlock));
336 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
339 TRACE("(%p)\n", device);
342 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
344 WARN("IDsDriverBuffer_Play failed\n");
346 err = mmErr(waveOutRestart(device->hwo));
348 WARN("waveOutRestart failed\n");
354 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
357 TRACE("(%p)\n", device);
360 err = IDsDriverBuffer_Stop(device->hwbuf);
361 if (err == DSERR_BUFFERLOST) {
362 DSOUND_PrimaryClose(device);
363 err = DSOUND_ReopenDevice(device, FALSE);
365 ERR("DSOUND_ReopenDevice failed\n");
368 err = DSOUND_PrimaryOpen(device);
370 WARN("DSOUND_PrimaryOpen failed\n");
372 } else if (err != DS_OK) {
373 WARN("IDsDriverBuffer_Stop failed\n");
377 /* don't call the wave system with the lock set */
378 LeaveCriticalSection(&(device->mixlock));
381 err = mmErr(waveOutPause(device->hwo));
384 EnterCriticalSection(&(device->mixlock));
387 WARN("waveOutPause failed\n");
393 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
395 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
398 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
400 WARN("IDsDriverBuffer_GetPosition failed\n");
404 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
406 /* check if playpos was requested */
408 /* use the cached play position */
409 *playpos = device->pwplay * device->fraglen;
411 /* check if writepos was requested */
413 /* the writepos is the first non-queued position */
414 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
416 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
420 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
422 HRESULT err = DSERR_BUFFERLOST;
423 int i, alloc_size, cp_size;
424 DWORD nSamplesPerSec, bpp, chans;
425 TRACE("(%p,%p)\n", device, wfex);
427 if (device->priolevel == DSSCL_NORMAL) {
428 WARN("failed priority check!\n");
429 return DSERR_PRIOLEVELNEEDED;
432 /* Let's be pedantic! */
434 WARN("invalid parameter: wfex==NULL!\n");
435 return DSERR_INVALIDPARAM;
437 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
438 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
439 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
440 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
441 wfex->wBitsPerSample, wfex->cbSize);
444 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
445 EnterCriticalSection(&(device->mixlock));
447 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
448 alloc_size = sizeof(WAVEFORMATEX);
449 cp_size = sizeof(PCMWAVEFORMAT);
451 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
453 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
455 nSamplesPerSec = device->pwfx->nSamplesPerSec;
456 bpp = device->pwfx->wBitsPerSample;
457 chans = device->pwfx->nChannels;
459 CopyMemory(device->pwfx, wfex, cp_size);
461 if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
462 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
464 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
465 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
467 err = DSERR_BUFFERLOST;
468 CopyMemory(device->pwfx, wfex, cp_size);
471 if (err != DSERR_BUFFERLOST && FAILED(err)) {
472 WARN("IDsDriverBuffer_SetFormat failed\n");
480 /* ALSA specific: S_FALSE tells that recreation was successful,
481 * but size and location may be changed, and buffer has to be restarted
482 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
483 * and the entire re-initialization will occur anyway
485 IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
486 IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
488 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
489 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
490 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
493 DSOUND_RecalcPrimary(device);
496 if (err == DSERR_BUFFERLOST)
498 DSOUND_PrimaryClose(device);
500 err = DSOUND_ReopenDevice(device, FALSE);
503 WARN("DSOUND_ReopenDevice failed: %08x\n", err);
506 err = DSOUND_PrimaryOpen(device);
508 WARN("DSOUND_PrimaryOpen failed\n");
512 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
514 DSOUND_PrimaryClose(device);
515 device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
516 err = DSOUND_ReopenDevice(device, TRUE);
518 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
519 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
520 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
524 if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
525 IDirectSoundBufferImpl** dsb = device->buffers;
526 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
528 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
530 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
531 DSOUND_RecalcFormat((*dsb));
532 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen);
533 (*dsb)->primary_mixpos = 0;
535 RtlReleaseResource(&(*dsb)->lock);
541 LeaveCriticalSection(&(device->mixlock));
542 RtlReleaseResource(&(device->buffer_list_lock));
548 /*******************************************************************************
551 /* This sets this format for the <em>Primary Buffer Only</em> */
552 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
553 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
554 LPDIRECTSOUNDBUFFER iface,
555 LPCWAVEFORMATEX wfex)
557 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
558 TRACE("(%p,%p)\n", iface, wfex);
559 return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
562 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
563 LPDIRECTSOUNDBUFFER iface,LONG vol
565 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
568 HRESULT hres = DS_OK;
569 TRACE("(%p,%d)\n", iface, vol);
571 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
572 WARN("control unavailable\n");
573 return DSERR_CONTROLUNAVAIL;
576 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
577 WARN("invalid parameter: vol = %d\n", vol);
578 return DSERR_INVALIDPARAM;
582 EnterCriticalSection(&(device->mixlock));
584 waveOutGetVolume(device->hwo, &factors);
585 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
586 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
587 DSOUND_AmpFactorToVolPan(&volpan);
588 if (vol != volpan.lVolume) {
590 DSOUND_RecalcVolPan(&volpan);
592 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
594 WARN("IDsDriverBuffer_SetVolumePan failed\n");
596 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
597 waveOutSetVolume(device->hwo, ampfactors);
601 LeaveCriticalSection(&(device->mixlock));
607 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
608 LPDIRECTSOUNDBUFFER iface,LPLONG vol
610 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
613 TRACE("(%p,%p)\n", iface, vol);
615 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
616 WARN("control unavailable\n");
617 return DSERR_CONTROLUNAVAIL;
621 WARN("invalid parameter: vol = NULL\n");
622 return DSERR_INVALIDPARAM;
625 waveOutGetVolume(device->hwo, &factors);
626 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
627 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
628 DSOUND_AmpFactorToVolPan(&volpan);
629 *vol = volpan.lVolume;
633 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
634 LPDIRECTSOUNDBUFFER iface,DWORD freq
636 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
637 TRACE("(%p,%d)\n",This,freq);
639 /* You cannot set the frequency of the primary buffer */
640 WARN("control unavailable\n");
641 return DSERR_CONTROLUNAVAIL;
644 static HRESULT WINAPI PrimaryBufferImpl_Play(
645 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
647 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
648 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
650 if (!(flags & DSBPLAY_LOOPING)) {
651 WARN("invalid parameter: flags = %08x\n", flags);
652 return DSERR_INVALIDPARAM;
656 EnterCriticalSection(&(device->mixlock));
658 if (device->state == STATE_STOPPED)
659 device->state = STATE_STARTING;
660 else if (device->state == STATE_STOPPING)
661 device->state = STATE_PLAYING;
663 LeaveCriticalSection(&(device->mixlock));
669 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
671 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
672 TRACE("(%p)\n", iface);
675 EnterCriticalSection(&(device->mixlock));
677 if (device->state == STATE_PLAYING)
678 device->state = STATE_STOPPING;
679 else if (device->state == STATE_STARTING)
680 device->state = STATE_STOPPED;
682 LeaveCriticalSection(&(device->mixlock));
688 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
690 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
691 ULONG ref = InterlockedIncrement(&(This->ref));
692 TRACE("(%p) ref was %d\n", This, ref - 1);
696 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
698 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
699 DWORD ref = InterlockedDecrement(&(This->ref));
700 TRACE("(%p) ref was %d\n", This, ref + 1);
703 This->device->primary = NULL;
704 HeapFree(GetProcessHeap(), 0, This);
705 TRACE("(%p) released\n", This);
710 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
711 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
714 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
715 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
718 EnterCriticalSection(&(device->mixlock));
720 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
722 WARN("DSOUND_PrimaryGetPosition failed\n");
723 LeaveCriticalSection(&(device->mixlock));
727 if (device->state != STATE_STOPPED)
728 /* apply the documented 10ms lead to writepos */
729 *writepos += device->writelead;
730 while (*writepos >= device->buflen) *writepos -= device->buflen;
733 LeaveCriticalSection(&(device->mixlock));
736 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
740 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
741 LPDIRECTSOUNDBUFFER iface,LPDWORD status
743 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
744 TRACE("(%p,%p)\n", iface, status);
746 if (status == NULL) {
747 WARN("invalid parameter: status == NULL\n");
748 return DSERR_INVALIDPARAM;
752 if ((device->state == STATE_STARTING) ||
753 (device->state == STATE_PLAYING))
754 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
756 TRACE("status=%x\n", *status);
761 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
762 LPDIRECTSOUNDBUFFER iface,
768 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
769 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
771 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
773 if (lpwf) { /* NULL is valid */
774 if (wfsize >= size) {
775 CopyMemory(lpwf,device->pwfx,size);
779 WARN("invalid parameter: wfsize too small\n");
782 return DSERR_INVALIDPARAM;
786 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
788 WARN("invalid parameter: wfwritten == NULL\n");
789 return DSERR_INVALIDPARAM;
796 static HRESULT WINAPI PrimaryBufferImpl_Lock(
797 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
800 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
801 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
813 if (device->priolevel != DSSCL_WRITEPRIMARY) {
814 WARN("failed priority check!\n");
815 return DSERR_PRIOLEVELNEEDED;
818 /* when this flag is set, writecursor is meaningless and must be calculated */
819 if (flags & DSBLOCK_FROMWRITECURSOR) {
820 /* GetCurrentPosition does too much magic to duplicate here */
821 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
823 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
828 /* when this flag is set, writebytes is meaningless and must be set */
829 if (flags & DSBLOCK_ENTIREBUFFER)
830 writebytes = device->buflen;
832 if (writecursor >= device->buflen) {
833 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
834 writecursor, device->buflen);
835 return DSERR_INVALIDPARAM;
838 if (writebytes > device->buflen) {
839 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
840 writebytes, device->buflen);
841 return DSERR_INVALIDPARAM;
844 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
845 hres = IDsDriverBuffer_Lock(device->hwbuf,
846 lplpaudioptr1, audiobytes1,
847 lplpaudioptr2, audiobytes2,
848 writecursor, writebytes,
851 WARN("IDsDriverBuffer_Lock failed\n");
855 if (writecursor+writebytes <= device->buflen) {
856 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
857 *audiobytes1 = writebytes;
859 *(LPBYTE*)lplpaudioptr2 = NULL;
862 TRACE("->%d.0\n",writebytes);
864 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
865 *audiobytes1 = device->buflen-writecursor;
867 *(LPBYTE*)lplpaudioptr2 = device->buffer;
869 *audiobytes2 = writebytes-(device->buflen-writecursor);
870 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
876 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
877 LPDIRECTSOUNDBUFFER iface,DWORD newpos
879 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
880 TRACE("(%p,%d)\n",This,newpos);
882 /* You cannot set the position of the primary buffer */
883 WARN("invalid call\n");
884 return DSERR_INVALIDCALL;
887 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
888 LPDIRECTSOUNDBUFFER iface,LONG pan
890 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
893 HRESULT hres = DS_OK;
894 TRACE("(%p,%d)\n", iface, pan);
896 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
897 WARN("control unavailable\n");
898 return DSERR_CONTROLUNAVAIL;
901 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
902 WARN("invalid parameter: pan = %d\n", pan);
903 return DSERR_INVALIDPARAM;
907 EnterCriticalSection(&(device->mixlock));
909 waveOutGetVolume(device->hwo, &factors);
910 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
911 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
912 DSOUND_AmpFactorToVolPan(&volpan);
913 if (pan != volpan.lPan) {
915 DSOUND_RecalcVolPan(&volpan);
917 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
919 WARN("IDsDriverBuffer_SetVolumePan failed\n");
921 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
922 waveOutSetVolume(device->hwo, ampfactors);
926 LeaveCriticalSection(&(device->mixlock));
932 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
933 LPDIRECTSOUNDBUFFER iface,LPLONG pan
935 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
938 TRACE("(%p,%p)\n", iface, pan);
940 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
941 WARN("control unavailable\n");
942 return DSERR_CONTROLUNAVAIL;
946 WARN("invalid parameter: pan == NULL\n");
947 return DSERR_INVALIDPARAM;
950 waveOutGetVolume(device->hwo, &factors);
951 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
952 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
953 DSOUND_AmpFactorToVolPan(&volpan);
958 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
959 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
961 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
962 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
964 if (device->priolevel != DSSCL_WRITEPRIMARY) {
965 WARN("failed priority check!\n");
966 return DSERR_PRIOLEVELNEEDED;
969 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
972 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
974 WARN("IDsDriverBuffer_Unlock failed\n");
982 static HRESULT WINAPI PrimaryBufferImpl_Restore(
983 LPDIRECTSOUNDBUFFER iface
985 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
986 FIXME("(%p):stub\n",This);
990 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
991 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
993 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
994 TRACE("(%p,%p)\n", iface, freq);
997 WARN("invalid parameter: freq == NULL\n");
998 return DSERR_INVALIDPARAM;
1001 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1002 WARN("control unavailable\n");
1003 return DSERR_CONTROLUNAVAIL;
1006 *freq = device->pwfx->nSamplesPerSec;
1007 TRACE("-> %d\n", *freq);
1012 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1013 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1015 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1016 WARN("(%p) already initialized\n", This);
1017 return DSERR_ALREADYINITIALIZED;
1020 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1021 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1023 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1024 TRACE("(%p,%p)\n", iface, caps);
1027 WARN("invalid parameter: caps == NULL\n");
1028 return DSERR_INVALIDPARAM;
1031 if (caps->dwSize < sizeof(*caps)) {
1032 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1033 return DSERR_INVALIDPARAM;
1036 caps->dwFlags = device->dsbd.dwFlags;
1037 caps->dwBufferBytes = device->buflen;
1039 /* Windows reports these as zero */
1040 caps->dwUnlockTransferRate = 0;
1041 caps->dwPlayCpuOverhead = 0;
1046 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1047 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1049 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1050 DirectSoundDevice *device = This->device;
1051 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1053 if (ppobj == NULL) {
1054 WARN("invalid parameter\n");
1055 return E_INVALIDARG;
1058 *ppobj = NULL; /* assume failure */
1060 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1061 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1062 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1067 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1068 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1069 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1070 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1071 return E_NOINTERFACE;
1074 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1075 ERR("app requested IDirectSoundNotify on primary buffer\n");
1076 /* FIXME: should we support this? */
1077 return E_NOINTERFACE;
1080 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1081 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1082 return E_NOINTERFACE;
1085 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1086 if (!device->listener)
1087 IDirectSound3DListenerImpl_Create(device, &device->listener);
1088 if (device->listener) {
1089 *ppobj = device->listener;
1090 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1094 WARN("IID_IDirectSound3DListener failed\n");
1095 return E_NOINTERFACE;
1098 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1099 FIXME("app requested IKsPropertySet on primary buffer\n");
1100 return E_NOINTERFACE;
1103 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1104 return E_NOINTERFACE;
1107 static const IDirectSoundBufferVtbl dspbvt =
1109 PrimaryBufferImpl_QueryInterface,
1110 PrimaryBufferImpl_AddRef,
1111 PrimaryBufferImpl_Release,
1112 PrimaryBufferImpl_GetCaps,
1113 PrimaryBufferImpl_GetCurrentPosition,
1114 PrimaryBufferImpl_GetFormat,
1115 PrimaryBufferImpl_GetVolume,
1116 PrimaryBufferImpl_GetPan,
1117 PrimaryBufferImpl_GetFrequency,
1118 PrimaryBufferImpl_GetStatus,
1119 PrimaryBufferImpl_Initialize,
1120 PrimaryBufferImpl_Lock,
1121 PrimaryBufferImpl_Play,
1122 PrimaryBufferImpl_SetCurrentPosition,
1123 PrimaryBufferImpl_SetFormat,
1124 PrimaryBufferImpl_SetVolume,
1125 PrimaryBufferImpl_SetPan,
1126 PrimaryBufferImpl_SetFrequency,
1127 PrimaryBufferImpl_Stop,
1128 PrimaryBufferImpl_Unlock,
1129 PrimaryBufferImpl_Restore
1132 HRESULT PrimaryBufferImpl_Create(
1133 DirectSoundDevice * device,
1134 PrimaryBufferImpl ** ppdsb,
1135 LPCDSBUFFERDESC dsbd)
1137 PrimaryBufferImpl *dsb;
1138 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1140 if (dsbd->lpwfxFormat) {
1141 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1143 return DSERR_INVALIDPARAM;
1146 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1149 WARN("out of memory\n");
1151 return DSERR_OUTOFMEMORY;
1155 dsb->device = device;
1156 dsb->lpVtbl = &dspbvt;
1158 CopyMemory(&device->dsbd, dsbd, sizeof(*dsbd));
1160 TRACE("Created primary buffer at %p\n", dsb);
1161 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1162 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1163 device->pwfx->wFormatTag, device->pwfx->nChannels,
1164 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1165 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1166 device->pwfx->cbSize);