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 err = DSOUND_ReopenDevice(device, TRUE);
182 WARN("Falling back to waveout failed too! Giving up\n");
186 else if (device->helfrags < ds_snd_queue_max)
187 device->prebuf = device->helfrags;
190 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
191 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
193 /* are we using waveOut stuff? */
194 if (!device->driver) {
196 LPWAVEHDR headers = NULL;
200 /* Start in pause mode, to allow buffers to get filled */
201 waveOutPause(device->hwo);
203 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
205 /* reallocate emulated primary buffer */
207 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
209 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
212 ERR("failed to allocate primary buffer\n");
213 return DSERR_OUTOFMEMORY;
214 /* but the old buffer might still exist and must be re-prepared */
217 DSOUND_RecalcPrimary(device);
219 headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
221 headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
224 ERR("failed to allocate wave headers\n");
225 HeapFree(GetProcessHeap(), 0, newbuf);
226 DSOUND_RecalcPrimary(device);
227 return DSERR_OUTOFMEMORY;
230 device->buffer = newbuf;
231 device->pwave = headers;
233 /* prepare fragment headers */
234 for (c=0; c<device->helfrags; c++) {
235 device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
236 device->pwave[c].dwBufferLength = device->fraglen;
237 device->pwave[c].dwUser = (DWORD)device;
238 device->pwave[c].dwFlags = 0;
239 device->pwave[c].dwLoops = 0;
240 err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
243 waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
248 overshot = device->buflen % device->fraglen;
252 overshot -= overshot % device->pwfx->nBlockAlign;
253 device->pwave[device->helfrags - 1].dwBufferLength += overshot;
256 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
258 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
259 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
264 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
266 TRACE("(%p)\n", device);
268 /* are we using waveOut stuff? */
269 if (!device->hwbuf) {
272 /* get out of CS when calling the wave system */
273 LeaveCriticalSection(&(device->mixlock));
275 device->pwqueue = (DWORD)-1; /* resetting queues */
276 waveOutReset(device->hwo);
277 for (c=0; c<device->helfrags; c++)
278 waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
280 EnterCriticalSection(&(device->mixlock));
282 /* clear the queue */
285 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
289 ERR("Still %d references on primary buffer, refcount leak?\n", ref);
293 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
296 TRACE("(%p)\n", device);
298 device->buflen = ds_hel_buflen;
299 err = DSOUND_PrimaryOpen(device);
302 WARN("DSOUND_PrimaryOpen failed\n");
306 device->state = STATE_STOPPED;
310 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
312 TRACE("(%p)\n", device);
315 EnterCriticalSection(&(device->mixlock));
317 DSOUND_PrimaryClose(device);
318 if (device->driver) {
320 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
324 HeapFree(GetProcessHeap(),0,device->pwave);
325 HeapFree(GetProcessHeap(),0,device->pwfx);
328 LeaveCriticalSection(&(device->mixlock));
334 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
337 TRACE("(%p)\n", device);
340 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
342 WARN("IDsDriverBuffer_Play failed\n");
344 err = mmErr(waveOutRestart(device->hwo));
346 WARN("waveOutRestart failed\n");
352 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
355 TRACE("(%p)\n", device);
358 err = IDsDriverBuffer_Stop(device->hwbuf);
359 if (err == DSERR_BUFFERLOST) {
360 DSOUND_PrimaryClose(device);
361 err = DSOUND_ReopenDevice(device, FALSE);
363 ERR("DSOUND_ReopenDevice failed\n");
366 err = DSOUND_PrimaryOpen(device);
368 WARN("DSOUND_PrimaryOpen failed\n");
370 } else if (err != DS_OK) {
371 WARN("IDsDriverBuffer_Stop failed\n");
375 /* don't call the wave system with the lock set */
376 LeaveCriticalSection(&(device->mixlock));
379 err = mmErr(waveOutPause(device->hwo));
382 EnterCriticalSection(&(device->mixlock));
385 WARN("waveOutPause failed\n");
391 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
393 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
396 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
398 WARN("IDsDriverBuffer_GetPosition failed\n");
402 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
404 /* check if playpos was requested */
406 /* use the cached play position */
407 *playpos = device->pwplay * device->fraglen;
409 /* check if writepos was requested */
411 /* the writepos is the first non-queued position */
412 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
414 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
418 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
420 HRESULT err = DSERR_BUFFERLOST;
421 int i, alloc_size, cp_size;
422 DWORD nSamplesPerSec, bpp, chans;
423 TRACE("(%p,%p)\n", device, wfex);
425 if (device->priolevel == DSSCL_NORMAL) {
426 WARN("failed priority check!\n");
427 return DSERR_PRIOLEVELNEEDED;
430 /* Let's be pedantic! */
432 WARN("invalid parameter: wfex==NULL!\n");
433 return DSERR_INVALIDPARAM;
435 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
436 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
437 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
438 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
439 wfex->wBitsPerSample, wfex->cbSize);
442 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
443 EnterCriticalSection(&(device->mixlock));
445 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
446 alloc_size = sizeof(WAVEFORMATEX);
447 cp_size = sizeof(PCMWAVEFORMAT);
449 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
451 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
453 nSamplesPerSec = device->pwfx->nSamplesPerSec;
454 bpp = device->pwfx->wBitsPerSample;
455 chans = device->pwfx->nChannels;
457 CopyMemory(device->pwfx, wfex, cp_size);
459 if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
460 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
462 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
463 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
465 err = DSERR_BUFFERLOST;
466 CopyMemory(device->pwfx, wfex, cp_size);
469 if (err != DSERR_BUFFERLOST && FAILED(err)) {
470 WARN("IDsDriverBuffer_SetFormat failed\n");
478 /* ALSA specific: S_FALSE tells that recreation was successful,
479 * but size and location may be changed, and buffer has to be restarted
480 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
481 * and the entire re-initialization will occur anyway
483 IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
484 IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
486 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
487 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
488 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
491 DSOUND_RecalcPrimary(device);
494 if (err == DSERR_BUFFERLOST)
496 DSOUND_PrimaryClose(device);
498 err = DSOUND_ReopenDevice(device, FALSE);
501 WARN("DSOUND_ReopenDevice failed: %08x\n", err);
504 err = DSOUND_PrimaryOpen(device);
506 WARN("DSOUND_PrimaryOpen failed\n");
510 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
512 DSOUND_PrimaryClose(device);
513 device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
514 err = DSOUND_ReopenDevice(device, TRUE);
516 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
517 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
518 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
522 if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
523 IDirectSoundBufferImpl** dsb = device->buffers;
524 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
526 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
528 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
529 DSOUND_RecalcFormat((*dsb));
530 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen);
531 (*dsb)->primary_mixpos = 0;
533 RtlReleaseResource(&(*dsb)->lock);
539 LeaveCriticalSection(&(device->mixlock));
540 RtlReleaseResource(&(device->buffer_list_lock));
546 /*******************************************************************************
549 /* This sets this format for the <em>Primary Buffer Only</em> */
550 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
551 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
552 LPDIRECTSOUNDBUFFER iface,
553 LPCWAVEFORMATEX wfex)
555 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
556 TRACE("(%p,%p)\n", iface, wfex);
557 return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
560 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
561 LPDIRECTSOUNDBUFFER iface,LONG vol
563 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
566 HRESULT hres = DS_OK;
567 TRACE("(%p,%d)\n", iface, vol);
569 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
570 WARN("control unavailable\n");
571 return DSERR_CONTROLUNAVAIL;
574 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
575 WARN("invalid parameter: vol = %d\n", vol);
576 return DSERR_INVALIDPARAM;
580 EnterCriticalSection(&(device->mixlock));
582 waveOutGetVolume(device->hwo, &factors);
583 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
584 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
585 DSOUND_AmpFactorToVolPan(&volpan);
586 if (vol != volpan.lVolume) {
588 DSOUND_RecalcVolPan(&volpan);
590 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
592 WARN("IDsDriverBuffer_SetVolumePan failed\n");
594 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
595 waveOutSetVolume(device->hwo, ampfactors);
599 LeaveCriticalSection(&(device->mixlock));
605 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
606 LPDIRECTSOUNDBUFFER iface,LPLONG vol
608 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
611 TRACE("(%p,%p)\n", iface, vol);
613 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
614 WARN("control unavailable\n");
615 return DSERR_CONTROLUNAVAIL;
619 WARN("invalid parameter: vol = NULL\n");
620 return DSERR_INVALIDPARAM;
623 waveOutGetVolume(device->hwo, &factors);
624 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
625 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
626 DSOUND_AmpFactorToVolPan(&volpan);
627 *vol = volpan.lVolume;
631 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
632 LPDIRECTSOUNDBUFFER iface,DWORD freq
634 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
635 TRACE("(%p,%d)\n",This,freq);
637 /* You cannot set the frequency of the primary buffer */
638 WARN("control unavailable\n");
639 return DSERR_CONTROLUNAVAIL;
642 static HRESULT WINAPI PrimaryBufferImpl_Play(
643 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
645 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
646 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
648 if (!(flags & DSBPLAY_LOOPING)) {
649 WARN("invalid parameter: flags = %08x\n", flags);
650 return DSERR_INVALIDPARAM;
654 EnterCriticalSection(&(device->mixlock));
656 if (device->state == STATE_STOPPED)
657 device->state = STATE_STARTING;
658 else if (device->state == STATE_STOPPING)
659 device->state = STATE_PLAYING;
661 LeaveCriticalSection(&(device->mixlock));
667 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
669 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
670 TRACE("(%p)\n", iface);
673 EnterCriticalSection(&(device->mixlock));
675 if (device->state == STATE_PLAYING)
676 device->state = STATE_STOPPING;
677 else if (device->state == STATE_STARTING)
678 device->state = STATE_STOPPED;
680 LeaveCriticalSection(&(device->mixlock));
686 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
688 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
689 ULONG ref = InterlockedIncrement(&(This->ref));
690 TRACE("(%p) ref was %d\n", This, ref - 1);
694 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
696 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
697 DWORD ref = InterlockedDecrement(&(This->ref));
698 TRACE("(%p) ref was %d\n", This, ref + 1);
701 This->device->primary = NULL;
702 HeapFree(GetProcessHeap(), 0, This);
703 TRACE("(%p) released\n", This);
708 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
709 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
712 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
713 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
716 EnterCriticalSection(&(device->mixlock));
718 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
720 WARN("DSOUND_PrimaryGetPosition failed\n");
721 LeaveCriticalSection(&(device->mixlock));
725 if (device->state != STATE_STOPPED)
726 /* apply the documented 10ms lead to writepos */
727 *writepos += device->writelead;
728 while (*writepos >= device->buflen) *writepos -= device->buflen;
731 LeaveCriticalSection(&(device->mixlock));
734 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
738 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
739 LPDIRECTSOUNDBUFFER iface,LPDWORD status
741 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
742 TRACE("(%p,%p)\n", iface, status);
744 if (status == NULL) {
745 WARN("invalid parameter: status == NULL\n");
746 return DSERR_INVALIDPARAM;
750 if ((device->state == STATE_STARTING) ||
751 (device->state == STATE_PLAYING))
752 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
754 TRACE("status=%x\n", *status);
759 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
760 LPDIRECTSOUNDBUFFER iface,
766 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
767 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
769 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
771 if (lpwf) { /* NULL is valid */
772 if (wfsize >= size) {
773 CopyMemory(lpwf,device->pwfx,size);
777 WARN("invalid parameter: wfsize too small\n");
780 return DSERR_INVALIDPARAM;
784 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
786 WARN("invalid parameter: wfwritten == NULL\n");
787 return DSERR_INVALIDPARAM;
794 static HRESULT WINAPI PrimaryBufferImpl_Lock(
795 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
798 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
799 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
811 if (device->priolevel != DSSCL_WRITEPRIMARY) {
812 WARN("failed priority check!\n");
813 return DSERR_PRIOLEVELNEEDED;
816 /* when this flag is set, writecursor is meaningless and must be calculated */
817 if (flags & DSBLOCK_FROMWRITECURSOR) {
818 /* GetCurrentPosition does too much magic to duplicate here */
819 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
821 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
826 /* when this flag is set, writebytes is meaningless and must be set */
827 if (flags & DSBLOCK_ENTIREBUFFER)
828 writebytes = device->buflen;
830 if (writecursor >= device->buflen) {
831 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
832 writecursor, device->buflen);
833 return DSERR_INVALIDPARAM;
836 if (writebytes > device->buflen) {
837 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
838 writebytes, device->buflen);
839 return DSERR_INVALIDPARAM;
842 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
843 hres = IDsDriverBuffer_Lock(device->hwbuf,
844 lplpaudioptr1, audiobytes1,
845 lplpaudioptr2, audiobytes2,
846 writecursor, writebytes,
849 WARN("IDsDriverBuffer_Lock failed\n");
853 if (writecursor+writebytes <= device->buflen) {
854 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
855 *audiobytes1 = writebytes;
857 *(LPBYTE*)lplpaudioptr2 = NULL;
860 TRACE("->%d.0\n",writebytes);
862 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
863 *audiobytes1 = device->buflen-writecursor;
865 *(LPBYTE*)lplpaudioptr2 = device->buffer;
867 *audiobytes2 = writebytes-(device->buflen-writecursor);
868 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
874 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
875 LPDIRECTSOUNDBUFFER iface,DWORD newpos
877 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
878 TRACE("(%p,%d)\n",This,newpos);
880 /* You cannot set the position of the primary buffer */
881 WARN("invalid call\n");
882 return DSERR_INVALIDCALL;
885 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
886 LPDIRECTSOUNDBUFFER iface,LONG pan
888 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
891 HRESULT hres = DS_OK;
892 TRACE("(%p,%d)\n", iface, pan);
894 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
895 WARN("control unavailable\n");
896 return DSERR_CONTROLUNAVAIL;
899 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
900 WARN("invalid parameter: pan = %d\n", pan);
901 return DSERR_INVALIDPARAM;
905 EnterCriticalSection(&(device->mixlock));
907 waveOutGetVolume(device->hwo, &factors);
908 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
909 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
910 DSOUND_AmpFactorToVolPan(&volpan);
911 if (pan != volpan.lPan) {
913 DSOUND_RecalcVolPan(&volpan);
915 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
917 WARN("IDsDriverBuffer_SetVolumePan failed\n");
919 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
920 waveOutSetVolume(device->hwo, ampfactors);
924 LeaveCriticalSection(&(device->mixlock));
930 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
931 LPDIRECTSOUNDBUFFER iface,LPLONG pan
933 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
936 TRACE("(%p,%p)\n", iface, pan);
938 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
939 WARN("control unavailable\n");
940 return DSERR_CONTROLUNAVAIL;
944 WARN("invalid parameter: pan == NULL\n");
945 return DSERR_INVALIDPARAM;
948 waveOutGetVolume(device->hwo, &factors);
949 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
950 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
951 DSOUND_AmpFactorToVolPan(&volpan);
956 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
957 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
959 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
960 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
962 if (device->priolevel != DSSCL_WRITEPRIMARY) {
963 WARN("failed priority check!\n");
964 return DSERR_PRIOLEVELNEEDED;
967 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
970 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
972 WARN("IDsDriverBuffer_Unlock failed\n");
980 static HRESULT WINAPI PrimaryBufferImpl_Restore(
981 LPDIRECTSOUNDBUFFER iface
983 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
984 FIXME("(%p):stub\n",This);
988 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
989 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
991 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
992 TRACE("(%p,%p)\n", iface, freq);
995 WARN("invalid parameter: freq == NULL\n");
996 return DSERR_INVALIDPARAM;
999 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1000 WARN("control unavailable\n");
1001 return DSERR_CONTROLUNAVAIL;
1004 *freq = device->pwfx->nSamplesPerSec;
1005 TRACE("-> %d\n", *freq);
1010 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1011 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1013 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1014 WARN("(%p) already initialized\n", This);
1015 return DSERR_ALREADYINITIALIZED;
1018 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1019 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1021 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1022 TRACE("(%p,%p)\n", iface, caps);
1025 WARN("invalid parameter: caps == NULL\n");
1026 return DSERR_INVALIDPARAM;
1029 if (caps->dwSize < sizeof(*caps)) {
1030 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1031 return DSERR_INVALIDPARAM;
1034 caps->dwFlags = device->dsbd.dwFlags;
1035 caps->dwBufferBytes = device->buflen;
1037 /* Windows reports these as zero */
1038 caps->dwUnlockTransferRate = 0;
1039 caps->dwPlayCpuOverhead = 0;
1044 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1045 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1047 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1048 DirectSoundDevice *device = This->device;
1049 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1051 if (ppobj == NULL) {
1052 WARN("invalid parameter\n");
1053 return E_INVALIDARG;
1056 *ppobj = NULL; /* assume failure */
1058 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1059 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1060 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1065 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1066 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1067 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1068 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1069 return E_NOINTERFACE;
1072 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1073 ERR("app requested IDirectSoundNotify on primary buffer\n");
1074 /* FIXME: should we support this? */
1075 return E_NOINTERFACE;
1078 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1079 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1080 return E_NOINTERFACE;
1083 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1084 if (!device->listener)
1085 IDirectSound3DListenerImpl_Create(device, &device->listener);
1086 if (device->listener) {
1087 *ppobj = device->listener;
1088 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1092 WARN("IID_IDirectSound3DListener failed\n");
1093 return E_NOINTERFACE;
1096 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1097 FIXME("app requested IKsPropertySet on primary buffer\n");
1098 return E_NOINTERFACE;
1101 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1102 return E_NOINTERFACE;
1105 static const IDirectSoundBufferVtbl dspbvt =
1107 PrimaryBufferImpl_QueryInterface,
1108 PrimaryBufferImpl_AddRef,
1109 PrimaryBufferImpl_Release,
1110 PrimaryBufferImpl_GetCaps,
1111 PrimaryBufferImpl_GetCurrentPosition,
1112 PrimaryBufferImpl_GetFormat,
1113 PrimaryBufferImpl_GetVolume,
1114 PrimaryBufferImpl_GetPan,
1115 PrimaryBufferImpl_GetFrequency,
1116 PrimaryBufferImpl_GetStatus,
1117 PrimaryBufferImpl_Initialize,
1118 PrimaryBufferImpl_Lock,
1119 PrimaryBufferImpl_Play,
1120 PrimaryBufferImpl_SetCurrentPosition,
1121 PrimaryBufferImpl_SetFormat,
1122 PrimaryBufferImpl_SetVolume,
1123 PrimaryBufferImpl_SetPan,
1124 PrimaryBufferImpl_SetFrequency,
1125 PrimaryBufferImpl_Stop,
1126 PrimaryBufferImpl_Unlock,
1127 PrimaryBufferImpl_Restore
1130 HRESULT PrimaryBufferImpl_Create(
1131 DirectSoundDevice * device,
1132 PrimaryBufferImpl ** ppdsb,
1133 LPCDSBUFFERDESC dsbd)
1135 PrimaryBufferImpl *dsb;
1136 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1138 if (dsbd->lpwfxFormat) {
1139 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1141 return DSERR_INVALIDPARAM;
1144 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1147 WARN("out of memory\n");
1149 return DSERR_OUTOFMEMORY;
1153 dsb->device = device;
1154 dsb->lpVtbl = &dspbvt;
1156 CopyMemory(&device->dsbd, dsbd, sizeof(*dsbd));
1158 TRACE("Created primary buffer at %p\n", dsb);
1159 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1160 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1161 device->pwfx->wFormatTag, device->pwfx->nChannels,
1162 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1163 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1164 device->pwfx->cbSize);