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 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
42 TRACE("(%p)\n", device);
44 nBlockAlign = device->pwfx->nBlockAlign;
47 /* Alsa doesn't have continuous buffers, instead it has buffers with power of 2,
48 * If DS_TIME_DEL is about 10 ms, 512 * nBlockAlign is roughly correct */
49 fraglen = 512 * nBlockAlign;
51 /* Compensate for only being rougly accurate */
52 if (device->pwfx->nSamplesPerSec <= 26000)
55 if (device->pwfx->nSamplesPerSec <= 12000)
58 if (device->pwfx->nSamplesPerSec >= 80000)
61 /* reduce fragment size until an integer number of them fits in the buffer */
62 /* (FIXME: this may or may not be a good idea) */
63 while (device->buflen % fraglen) fraglen -= nBlockAlign;
64 device->fraglen = fraglen;
65 TRACE("fraglen=%d\n", device->fraglen);
67 /* calculate the 10ms write lead */
68 device->writelead = (device->pwfx->nSamplesPerSec / 100) * nBlockAlign;
71 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
74 TRACE("(%p)\n", device);
76 /* are we using waveOut stuff? */
77 if (!device->driver) {
81 /* Start in pause mode, to allow buffers to get filled */
82 waveOutPause(device->hwo);
83 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
84 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
86 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
87 on windows this size is always fixed (tested on win-xp) */
88 buflen = DS_HEL_BUFLEN;
90 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
92 /* reallocate emulated primary buffer */
94 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
96 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
99 ERR("failed to allocate primary buffer\n");
100 merr = DSERR_OUTOFMEMORY;
101 /* but the old buffer might still exist and must be re-prepared */
103 device->buffer = newbuf;
104 device->buflen = buflen;
106 if (device->buffer) {
109 device->fraglen = device->buflen / DS_HEL_FRAGS;
112 if(device->buflen % DS_HEL_FRAGS){
113 ERR("Bad DS_HEL_FRAGS resolution\n");
116 /* prepare fragment headers */
117 for (c=0; c<DS_HEL_FRAGS; c++) {
118 device->pwave[c]->lpData = (char*)device->buffer + c*device->fraglen;
119 device->pwave[c]->dwBufferLength = device->fraglen;
120 device->pwave[c]->dwUser = (DWORD)device;
121 device->pwave[c]->dwFlags = 0;
122 device->pwave[c]->dwLoops = 0;
123 err = mmErr(waveOutPrepareHeader(device->hwo,device->pwave[c],sizeof(WAVEHDR)));
126 waveOutUnprepareHeader(device->hwo,device->pwave[c],sizeof(WAVEHDR));
136 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
137 TRACE("fraglen=%d\n", device->fraglen);
139 if ((err == DS_OK) && (merr != DS_OK))
141 } else if (!device->hwbuf) {
142 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
143 DSBCAPS_PRIMARYBUFFER,0,
144 &(device->buflen),&(device->buffer),
145 (LPVOID*)&(device->hwbuf));
147 WARN("IDsDriver_CreateSoundBuffer failed\n");
151 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
152 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
155 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
162 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
164 TRACE("(%p)\n", device);
166 /* are we using waveOut stuff? */
167 if (!device->hwbuf) {
170 /* get out of CS when calling the wave system */
171 LeaveCriticalSection(&(device->mixlock));
173 device->pwqueue = (DWORD)-1; /* resetting queues */
174 waveOutReset(device->hwo);
175 for (c=0; c<DS_HEL_FRAGS; c++)
176 waveOutUnprepareHeader(device->hwo, device->pwave[c], sizeof(WAVEHDR));
178 EnterCriticalSection(&(device->mixlock));
180 /* clear the queue */
183 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
188 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
191 TRACE("(%p)\n", device);
193 device->buflen = device->pwfx->nAvgBytesPerSec;
195 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
197 if (device->driver) {
198 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
199 DSBCAPS_PRIMARYBUFFER,0,
200 &(device->buflen),&(device->buffer),
201 (LPVOID*)&(device->hwbuf));
203 WARN("IDsDriver_CreateSoundBuffer failed\n");
207 if (!device->hwbuf) {
208 /* Allocate memory for HEL buffer headers */
210 for (c=0; c<DS_HEL_FRAGS; c++) {
211 device->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
212 if (!device->pwave[c]) {
213 /* Argh, out of memory */
215 HeapFree(GetProcessHeap(),0,device->pwave[c]);
217 WARN("out of memory\n");
218 return DSERR_OUTOFMEMORY;
223 err = DSOUND_PrimaryOpen(device);
226 WARN("DSOUND_PrimaryOpen failed\n");
230 /* calculate fragment size and write lead */
231 DSOUND_RecalcPrimary(device);
232 device->state = STATE_STOPPED;
236 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
238 TRACE("(%p)\n", device);
241 EnterCriticalSection(&(device->mixlock));
243 DSOUND_PrimaryClose(device);
244 if (device->driver) {
246 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
251 for (c=0; c<DS_HEL_FRAGS; c++) {
252 HeapFree(GetProcessHeap(),0,device->pwave[c]);
255 HeapFree(GetProcessHeap(),0,device->pwfx);
258 LeaveCriticalSection(&(device->mixlock));
264 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
267 TRACE("(%p)\n", device);
270 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
272 WARN("IDsDriverBuffer_Play failed\n");
274 err = mmErr(waveOutRestart(device->hwo));
276 WARN("waveOutRestart failed\n");
282 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
285 TRACE("(%p)\n", device);
288 err = IDsDriverBuffer_Stop(device->hwbuf);
289 if (err == DSERR_BUFFERLOST) {
290 DWORD flags = CALLBACK_FUNCTION;
291 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
292 flags |= WAVE_DIRECTSOUND;
293 /* Wine-only: the driver wants us to reopen the device */
294 /* FIXME: check for errors */
295 IDsDriverBuffer_Release(device->hwbuf);
296 waveOutClose(device->hwo);
298 err = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode,
299 device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device,
302 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
303 DSBCAPS_PRIMARYBUFFER,0,
304 &(device->buflen),&(device->buffer),
305 (LPVOID)&(device->hwbuf));
307 WARN("IDsDriver_CreateSoundBuffer failed\n");
309 WARN("waveOutOpen failed\n");
311 } else if (err != DS_OK) {
312 WARN("IDsDriverBuffer_Stop failed\n");
316 /* dont call the wave system with the lock set */
317 LeaveCriticalSection(&(device->mixlock));
320 err = mmErr(waveOutPause(device->hwo));
323 EnterCriticalSection(&(device->mixlock));
326 WARN("waveOutPause failed\n");
332 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
334 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
337 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
339 WARN("IDsDriverBuffer_GetPosition failed\n");
344 /* check if playpos was requested */
346 /* use the cached play position */
347 *playpos = device->pwplay * device->fraglen;
350 /* check if writepos was requested */
352 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
354 /* the writepos is the first non-queued position */
355 *writepos = (device->pwplay + device->pwqueue) * device->fraglen;
356 *writepos %= device->buflen;
359 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
363 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex)
366 int i, alloc_size, cp_size;
367 DWORD nSamplesPerSec;
368 TRACE("(%p,%p)\n", device, wfex);
370 if (device->priolevel == DSSCL_NORMAL) {
371 WARN("failed priority check!\n");
372 return DSERR_PRIOLEVELNEEDED;
375 /* Let's be pedantic! */
377 WARN("invalid parameter: wfex==NULL!\n");
378 return DSERR_INVALIDPARAM;
380 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
381 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
382 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
383 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
384 wfex->wBitsPerSample, wfex->cbSize);
387 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
388 EnterCriticalSection(&(device->mixlock));
390 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
391 alloc_size = sizeof(WAVEFORMATEX);
392 cp_size = sizeof(PCMWAVEFORMAT);
394 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
396 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
398 nSamplesPerSec = device->pwfx->nSamplesPerSec;
400 CopyMemory(device->pwfx, wfex, cp_size);
402 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
403 DWORD flags = CALLBACK_FUNCTION;
404 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
405 flags |= WAVE_DIRECTSOUND;
406 /* FIXME: check for errors */
407 DSOUND_PrimaryClose(device);
408 waveOutClose(device->hwo);
410 err = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode,
411 device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device,
414 err = DSOUND_PrimaryOpen(device);
416 WARN("DSOUND_PrimaryOpen failed\n");
420 WARN("waveOutOpen failed\n");
423 } else if (device->hwbuf) {
424 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
425 if (err == DSERR_BUFFERLOST) {
426 /* Wine-only: the driver wants us to recreate the HW buffer */
427 IDsDriverBuffer_Release(device->hwbuf);
428 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
429 DSBCAPS_PRIMARYBUFFER,0,
430 &(device->buflen),&(device->buffer),
431 (LPVOID)&(device->hwbuf));
433 WARN("IDsDriver_CreateSoundBuffer failed\n");
436 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
437 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
438 } else if (FAILED(err)) {
439 WARN("IDsDriverBuffer_SetFormat failed\n");
442 /* FIXME: should we set err back to DS_OK in all cases ? */
444 DSOUND_RecalcPrimary(device);
446 if (nSamplesPerSec != device->pwfx->nSamplesPerSec) {
447 IDirectSoundBufferImpl** dsb = device->buffers;
448 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
450 EnterCriticalSection(&((*dsb)->lock));
452 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
453 wfex->nSamplesPerSec;
455 LeaveCriticalSection(&((*dsb)->lock));
461 LeaveCriticalSection(&(device->mixlock));
462 RtlReleaseResource(&(device->buffer_list_lock));
468 /*******************************************************************************
471 /* This sets this format for the <em>Primary Buffer Only</em> */
472 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
473 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
474 LPDIRECTSOUNDBUFFER iface,
475 LPCWAVEFORMATEX wfex)
477 TRACE("(%p,%p)\n", iface, wfex);
478 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl *)iface)->device, wfex);
481 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
482 LPDIRECTSOUNDBUFFER iface,LONG vol
484 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
487 HRESULT hres = DS_OK;
488 TRACE("(%p,%d)\n", iface, vol);
490 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
491 WARN("control unavailable\n");
492 return DSERR_CONTROLUNAVAIL;
495 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
496 WARN("invalid parameter: vol = %d\n", vol);
497 return DSERR_INVALIDPARAM;
501 EnterCriticalSection(&(device->mixlock));
503 waveOutGetVolume(device->hwo, &factors);
504 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
505 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
506 DSOUND_AmpFactorToVolPan(&volpan);
507 if (vol != volpan.lVolume) {
509 DSOUND_RecalcVolPan(&volpan);
511 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
513 WARN("IDsDriverBuffer_SetVolumePan failed\n");
515 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
516 waveOutSetVolume(device->hwo, ampfactors);
520 LeaveCriticalSection(&(device->mixlock));
526 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
527 LPDIRECTSOUNDBUFFER iface,LPLONG vol
529 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
532 TRACE("(%p,%p)\n", iface, vol);
534 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
535 WARN("control unavailable\n");
536 return DSERR_CONTROLUNAVAIL;
540 WARN("invalid parameter: vol = NULL\n");
541 return DSERR_INVALIDPARAM;
544 waveOutGetVolume(device->hwo, &factors);
545 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
546 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
547 DSOUND_AmpFactorToVolPan(&volpan);
548 *vol = volpan.lVolume;
552 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
553 LPDIRECTSOUNDBUFFER iface,DWORD freq
555 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
556 TRACE("(%p,%d)\n",This,freq);
558 /* You cannot set the frequency of the primary buffer */
559 WARN("control unavailable\n");
560 return DSERR_CONTROLUNAVAIL;
563 static HRESULT WINAPI PrimaryBufferImpl_Play(
564 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
566 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
567 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
569 if (!(flags & DSBPLAY_LOOPING)) {
570 WARN("invalid parameter: flags = %08x\n", flags);
571 return DSERR_INVALIDPARAM;
575 EnterCriticalSection(&(device->mixlock));
577 if (device->state == STATE_STOPPED)
578 device->state = STATE_STARTING;
579 else if (device->state == STATE_STOPPING)
580 device->state = STATE_PLAYING;
582 LeaveCriticalSection(&(device->mixlock));
588 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
590 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
591 TRACE("(%p)\n", iface);
594 EnterCriticalSection(&(device->mixlock));
596 if (device->state == STATE_PLAYING)
597 device->state = STATE_STOPPING;
598 else if (device->state == STATE_STARTING)
599 device->state = STATE_STOPPED;
601 LeaveCriticalSection(&(device->mixlock));
607 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
609 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
610 ULONG ref = InterlockedIncrement(&(This->ref));
611 TRACE("(%p) ref was %d\n", This, ref - 1);
615 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
617 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
618 DWORD ref = InterlockedDecrement(&(This->ref));
619 TRACE("(%p) ref was %d\n", This, ref + 1);
622 This->device->primary = NULL;
623 HeapFree(GetProcessHeap(), 0, This);
624 TRACE("(%p) released\n", This);
629 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
630 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
633 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
634 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
637 EnterCriticalSection(&(device->mixlock));
639 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
641 WARN("DSOUND_PrimaryGetPosition failed\n");
642 LeaveCriticalSection(&(device->mixlock));
646 if (device->state != STATE_STOPPED)
647 /* apply the documented 10ms lead to writepos */
648 *writepos += device->writelead;
649 while (*writepos >= device->buflen) *writepos -= device->buflen;
652 LeaveCriticalSection(&(device->mixlock));
655 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
659 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
660 LPDIRECTSOUNDBUFFER iface,LPDWORD status
662 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
663 TRACE("(%p,%p)\n", iface, status);
665 if (status == NULL) {
666 WARN("invalid parameter: status == NULL\n");
667 return DSERR_INVALIDPARAM;
671 if ((device->state == STATE_STARTING) ||
672 (device->state == STATE_PLAYING))
673 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
675 TRACE("status=%x\n", *status);
680 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
681 LPDIRECTSOUNDBUFFER iface,
687 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
688 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
690 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
692 if (lpwf) { /* NULL is valid */
693 if (wfsize >= size) {
694 CopyMemory(lpwf,device->pwfx,size);
698 WARN("invalid parameter: wfsize too small\n");
701 return DSERR_INVALIDPARAM;
705 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
707 WARN("invalid parameter: wfwritten == NULL\n");
708 return DSERR_INVALIDPARAM;
715 static HRESULT WINAPI PrimaryBufferImpl_Lock(
716 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
719 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
720 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
732 if (device->priolevel != DSSCL_WRITEPRIMARY) {
733 WARN("failed priority check!\n");
734 return DSERR_PRIOLEVELNEEDED;
737 /* when this flag is set, writecursor is meaningless and must be calculated */
738 if (flags & DSBLOCK_FROMWRITECURSOR) {
739 /* GetCurrentPosition does too much magic to duplicate here */
740 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
742 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
747 /* when this flag is set, writebytes is meaningless and must be set */
748 if (flags & DSBLOCK_ENTIREBUFFER)
749 writebytes = device->buflen;
751 if (writecursor >= device->buflen) {
752 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
753 writecursor, device->buflen);
754 return DSERR_INVALIDPARAM;
757 if (writebytes > device->buflen) {
758 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
759 writebytes, device->buflen);
760 return DSERR_INVALIDPARAM;
763 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
764 hres = IDsDriverBuffer_Lock(device->hwbuf,
765 lplpaudioptr1, audiobytes1,
766 lplpaudioptr2, audiobytes2,
767 writecursor, writebytes,
770 WARN("IDsDriverBuffer_Lock failed\n");
774 if (writecursor+writebytes <= device->buflen) {
775 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
776 *audiobytes1 = writebytes;
778 *(LPBYTE*)lplpaudioptr2 = NULL;
781 TRACE("->%d.0\n",writebytes);
783 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
784 *audiobytes1 = device->buflen-writecursor;
786 *(LPBYTE*)lplpaudioptr2 = device->buffer;
788 *audiobytes2 = writebytes-(device->buflen-writecursor);
789 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
795 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
796 LPDIRECTSOUNDBUFFER iface,DWORD newpos
798 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
799 TRACE("(%p,%d)\n",This,newpos);
801 /* You cannot set the position of the primary buffer */
802 WARN("invalid call\n");
803 return DSERR_INVALIDCALL;
806 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
807 LPDIRECTSOUNDBUFFER iface,LONG pan
809 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
812 HRESULT hres = DS_OK;
813 TRACE("(%p,%d)\n", iface, pan);
815 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
816 WARN("control unavailable\n");
817 return DSERR_CONTROLUNAVAIL;
820 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
821 WARN("invalid parameter: pan = %d\n", pan);
822 return DSERR_INVALIDPARAM;
826 EnterCriticalSection(&(device->mixlock));
828 waveOutGetVolume(device->hwo, &factors);
829 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
830 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
831 DSOUND_AmpFactorToVolPan(&volpan);
832 if (pan != volpan.lPan) {
834 DSOUND_RecalcVolPan(&volpan);
836 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
838 WARN("IDsDriverBuffer_SetVolumePan failed\n");
840 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
841 waveOutSetVolume(device->hwo, ampfactors);
845 LeaveCriticalSection(&(device->mixlock));
851 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
852 LPDIRECTSOUNDBUFFER iface,LPLONG pan
854 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
857 TRACE("(%p,%p)\n", iface, pan);
859 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
860 WARN("control unavailable\n");
861 return DSERR_CONTROLUNAVAIL;
865 WARN("invalid parameter: pan == NULL\n");
866 return DSERR_INVALIDPARAM;
869 waveOutGetVolume(device->hwo, &factors);
870 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
871 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
872 DSOUND_AmpFactorToVolPan(&volpan);
877 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
878 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
880 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
881 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
883 if (device->priolevel != DSSCL_WRITEPRIMARY) {
884 WARN("failed priority check!\n");
885 return DSERR_PRIOLEVELNEEDED;
888 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
891 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
893 WARN("IDsDriverBuffer_Unlock failed\n");
901 static HRESULT WINAPI PrimaryBufferImpl_Restore(
902 LPDIRECTSOUNDBUFFER iface
904 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
905 FIXME("(%p):stub\n",This);
909 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
910 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
912 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
913 TRACE("(%p,%p)\n", iface, freq);
916 WARN("invalid parameter: freq == NULL\n");
917 return DSERR_INVALIDPARAM;
920 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
921 WARN("control unavailable\n");
922 return DSERR_CONTROLUNAVAIL;
925 *freq = device->pwfx->nSamplesPerSec;
926 TRACE("-> %d\n", *freq);
931 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
932 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
934 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
935 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
936 DPRINTF("Re-Init!!!\n");
937 WARN("already initialized\n");
938 return DSERR_ALREADYINITIALIZED;
941 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
942 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
944 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
945 TRACE("(%p,%p)\n", iface, caps);
948 WARN("invalid parameter: caps == NULL\n");
949 return DSERR_INVALIDPARAM;
952 if (caps->dwSize < sizeof(*caps)) {
953 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
954 return DSERR_INVALIDPARAM;
957 caps->dwFlags = device->dsbd.dwFlags;
958 if (device->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
959 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
961 caps->dwBufferBytes = device->buflen;
963 /* Windows reports these as zero */
964 caps->dwUnlockTransferRate = 0;
965 caps->dwPlayCpuOverhead = 0;
970 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
971 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
973 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
974 DirectSoundDevice *device = This->device;
975 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
978 WARN("invalid parameter\n");
982 *ppobj = NULL; /* assume failure */
984 if ( IsEqualGUID(riid, &IID_IUnknown) ||
985 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
986 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
991 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
992 /* a primary buffer can't have a DirectSoundBuffer8 interface */
993 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
994 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
995 return E_NOINTERFACE;
998 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
999 ERR("app requested IDirectSoundNotify on primary buffer\n");
1000 /* FIXME: should we support this? */
1001 return E_NOINTERFACE;
1004 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1005 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1006 return E_NOINTERFACE;
1009 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1010 if (!device->listener)
1011 IDirectSound3DListenerImpl_Create(device, &device->listener);
1012 if (device->listener) {
1013 *ppobj = device->listener;
1014 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1018 WARN("IID_IDirectSound3DListener failed\n");
1019 return E_NOINTERFACE;
1022 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1023 FIXME("app requested IKsPropertySet on primary buffer\n");
1024 return E_NOINTERFACE;
1027 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1028 return E_NOINTERFACE;
1031 static const IDirectSoundBufferVtbl dspbvt =
1033 PrimaryBufferImpl_QueryInterface,
1034 PrimaryBufferImpl_AddRef,
1035 PrimaryBufferImpl_Release,
1036 PrimaryBufferImpl_GetCaps,
1037 PrimaryBufferImpl_GetCurrentPosition,
1038 PrimaryBufferImpl_GetFormat,
1039 PrimaryBufferImpl_GetVolume,
1040 PrimaryBufferImpl_GetPan,
1041 PrimaryBufferImpl_GetFrequency,
1042 PrimaryBufferImpl_GetStatus,
1043 PrimaryBufferImpl_Initialize,
1044 PrimaryBufferImpl_Lock,
1045 PrimaryBufferImpl_Play,
1046 PrimaryBufferImpl_SetCurrentPosition,
1047 PrimaryBufferImpl_SetFormat,
1048 PrimaryBufferImpl_SetVolume,
1049 PrimaryBufferImpl_SetPan,
1050 PrimaryBufferImpl_SetFrequency,
1051 PrimaryBufferImpl_Stop,
1052 PrimaryBufferImpl_Unlock,
1053 PrimaryBufferImpl_Restore
1056 HRESULT PrimaryBufferImpl_Create(
1057 DirectSoundDevice * device,
1058 PrimaryBufferImpl ** ppdsb,
1059 LPCDSBUFFERDESC dsbd)
1061 PrimaryBufferImpl *dsb;
1062 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1064 if (dsbd->lpwfxFormat) {
1065 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1067 return DSERR_INVALIDPARAM;
1070 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1073 WARN("out of memory\n");
1075 return DSERR_OUTOFMEMORY;
1079 dsb->device = device;
1080 dsb->lpVtbl = &dspbvt;
1082 CopyMemory(&device->dsbd, dsbd, sizeof(*dsbd));
1084 TRACE("Created primary buffer at %p\n", dsb);
1085 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1086 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1087 device->pwfx->wFormatTag, device->pwfx->nChannels,
1088 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1089 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1090 device->pwfx->cbSize);