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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
33 #include <math.h> /* Insomnia - pow() function */
44 #include "wine/windef16.h"
45 #include "wine/debug.h"
48 #include "dsound_private.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
52 void DSOUND_RecalcPrimary(IDirectSoundImpl *This)
57 sw = This->wfx.nChannels * (This->wfx.wBitsPerSample / 8);
60 /* let fragment size approximate the timer delay */
61 fraglen = (This->wfx.nSamplesPerSec * DS_TIME_DEL / 1000) * sw;
62 /* reduce fragment size until an integer number of them fits in the buffer */
63 /* (FIXME: this may or may not be a good idea) */
64 while (This->buflen % fraglen) fraglen -= sw;
65 This->fraglen = fraglen;
66 TRACE("fraglen=%ld\n", This->fraglen);
68 /* calculate the 10ms write lead */
69 This->writelead = (This->wfx.nSamplesPerSec / 100) * sw;
72 static HRESULT DSOUND_PrimaryOpen(IDirectSoundImpl *This)
77 DSOUND_RecalcVolPan(&(This->volpan));
79 /* are we using waveOut stuff? */
84 /* Start in pause mode, to allow buffers to get filled */
85 waveOutPause(This->hwo);
86 if (This->state == STATE_PLAYING) This->state = STATE_STARTING;
87 else if (This->state == STATE_STOPPING) This->state = STATE_STOPPED;
88 /* use fragments of 10ms (1/100s) each (which should get us within
89 * the documented write cursor lead of 10-15ms) */
90 buflen = ((This->wfx.nAvgBytesPerSec / 100) & ~3) * DS_HEL_FRAGS;
91 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, This->buffer);
92 /* reallocate emulated primary buffer */
93 newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,This->buffer,buflen);
95 ERR("failed to allocate primary buffer\n");
96 merr = DSERR_OUTOFMEMORY;
97 /* but the old buffer might still exist and must be re-prepared */
99 This->buffer = newbuf;
100 This->buflen = buflen;
105 This->fraglen = This->buflen / DS_HEL_FRAGS;
107 /* prepare fragment headers */
108 for (c=0; c<DS_HEL_FRAGS; c++) {
109 This->pwave[c]->lpData = This->buffer + c*This->fraglen;
110 This->pwave[c]->dwBufferLength = This->fraglen;
111 This->pwave[c]->dwUser = (DWORD)This;
112 This->pwave[c]->dwFlags = 0;
113 This->pwave[c]->dwLoops = 0;
114 err = mmErr(waveOutPrepareHeader(This->hwo,This->pwave[c],sizeof(WAVEHDR)));
117 waveOutUnprepareHeader(This->hwo,This->pwave[c],sizeof(WAVEHDR));
127 memset(This->buffer, (This->wfx.wBitsPerSample == 16) ? 0 : 128, This->buflen);
128 TRACE("fraglen=%ld\n", This->fraglen);
129 DSOUND_WaveQueue(This, (DWORD)-1);
131 if ((err == DS_OK) && (merr != DS_OK))
135 DWORD vol = (This->volpan.dwTotalLeftAmpFactor & 0xffff) | (This->volpan.dwTotalRightAmpFactor << 16);
136 err = mmErr(waveOutSetVolume(This->hwo, vol));
139 err = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
146 static void DSOUND_PrimaryClose(IDirectSoundImpl *This)
148 TRACE("(%p)\n",This);
150 /* are we using waveOut stuff? */
154 This->pwqueue = (DWORD)-1; /* resetting queues */
155 waveOutReset(This->hwo);
156 for (c=0; c<DS_HEL_FRAGS; c++)
157 waveOutUnprepareHeader(This->hwo, This->pwave[c], sizeof(WAVEHDR));
162 HRESULT DSOUND_PrimaryCreate(IDirectSoundImpl *This)
165 TRACE("(%p)\n",This);
167 This->buflen = This->wfx.nAvgBytesPerSec;
169 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
172 err = IDsDriver_CreateSoundBuffer(This->driver,&(This->wfx),
173 DSBCAPS_PRIMARYBUFFER,0,
174 &(This->buflen),&(This->buffer),
175 (LPVOID*)&(This->hwbuf));
177 WARN("IDsDriver_CreateSoundBuffer failed\n");
182 /* Allocate memory for HEL buffer headers */
184 for (c=0; c<DS_HEL_FRAGS; c++) {
185 This->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
186 if (!This->pwave[c]) {
187 /* Argh, out of memory */
189 HeapFree(GetProcessHeap(),0,This->pwave[c]);
191 WARN("out of memory\n");
192 return DSERR_OUTOFMEMORY;
197 err = DSOUND_PrimaryOpen(This);
200 WARN("DSOUND_PrimaryOpen failed\n");
204 /* calculate fragment size and write lead */
205 DSOUND_RecalcPrimary(This);
206 This->state = STATE_STOPPED;
210 HRESULT DSOUND_PrimaryDestroy(IDirectSoundImpl *This)
212 TRACE("(%p)\n",This);
214 DSOUND_PrimaryClose(This);
216 if (IDsDriverBuffer_Release(This->hwbuf) == 0)
220 for (c=0; c<DS_HEL_FRAGS; c++) {
221 HeapFree(GetProcessHeap(),0,This->pwave[c]);
227 HRESULT DSOUND_PrimaryPlay(IDirectSoundImpl *This)
230 TRACE("(%p)\n",This);
233 err = IDsDriverBuffer_Play(This->hwbuf, 0, 0, DSBPLAY_LOOPING);
235 WARN("IDsDriverBuffer_Play failed\n");
237 err = mmErr(waveOutRestart(This->hwo));
239 WARN("waveOutRestart failed\n");
245 HRESULT DSOUND_PrimaryStop(IDirectSoundImpl *This)
248 TRACE("(%p)\n",This);
251 err = IDsDriverBuffer_Stop(This->hwbuf);
252 if (err == DSERR_BUFFERLOST) {
253 DWORD flags = CALLBACK_FUNCTION;
254 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
255 flags |= WAVE_DIRECTSOUND;
256 /* Wine-only: the driver wants us to reopen the device */
257 /* FIXME: check for errors */
258 IDsDriverBuffer_Release(This->hwbuf);
259 waveOutClose(This->hwo);
261 err = mmErr(waveOutOpen(&(This->hwo), This->drvdesc.dnDevNode,
262 &(This->wfx), (DWORD)DSOUND_callback, (DWORD)This,
265 err = IDsDriver_CreateSoundBuffer(This->driver,&(This->wfx),
266 DSBCAPS_PRIMARYBUFFER,0,
267 &(This->buflen),&(This->buffer),
268 (LPVOID)&(This->hwbuf));
270 WARN("IDsDriver_CreateSoundBuffer failed\n");
272 WARN("waveOutOpen failed\n");
274 } else if (err != DS_OK) {
275 WARN("IDsDriverBuffer_Stop failed\n");
278 err = mmErr(waveOutPause(This->hwo));
280 WARN("waveOutPause failed\n");
285 HRESULT DSOUND_PrimaryGetPosition(IDirectSoundImpl *This, LPDWORD playpos, LPDWORD writepos)
287 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
290 HRESULT err=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
292 WARN("IDsDriverBuffer_GetPosition failed\n");
299 mtime.wType = TIME_BYTES;
300 waveOutGetPosition(This->hwo, &mtime, sizeof(mtime));
301 mtime.u.cb = mtime.u.cb % This->buflen;
302 *playpos = mtime.u.cb;
305 /* the writepos should only be used by apps with WRITEPRIMARY priority,
306 * in which case our software mixer is disabled anyway */
307 *writepos = (This->pwplay + ds_hel_margin) * This->fraglen;
308 while (*writepos >= This->buflen)
309 *writepos -= This->buflen;
312 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
317 /*******************************************************************************
320 /* This sets this format for the <em>Primary Buffer Only</em> */
321 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
322 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
323 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX wfex
325 ICOM_THIS(PrimaryBufferImpl,iface);
326 IDirectSoundImpl* dsound = This->dsound;
327 IDirectSoundBufferImpl** dsb;
330 TRACE("(%p,%p)\n",This,wfex);
332 if (This->dsound->priolevel == DSSCL_NORMAL) {
333 WARN("failed priority check!\n");
334 return DSERR_PRIOLEVELNEEDED;
337 /* Let's be pedantic! */
339 WARN("invalid parameter: wfex==NULL!\n");
340 return DSERR_INVALIDPARAM;
342 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
343 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
344 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
345 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
346 wfex->wBitsPerSample, wfex->cbSize);
348 if ((wfex->wFormatTag != WAVE_FORMAT_PCM) ||
349 (wfex->nChannels < 1) || (wfex->nChannels > 2) ||
350 (wfex->nSamplesPerSec < 1) ||
351 ((wfex->wBitsPerSample != 8) && (wfex->wBitsPerSample != 16))) {
352 WARN("invalid paramemer: unsupported format!\n");
353 return DSERR_INVALIDPARAM;
357 RtlAcquireResourceExclusive(&(dsound->lock), TRUE);
359 if (dsound->wfx.nSamplesPerSec != wfex->nSamplesPerSec) {
360 dsb = dsound->buffers;
361 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
363 EnterCriticalSection(&((*dsb)->lock));
365 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
366 wfex->nSamplesPerSec;
368 LeaveCriticalSection(&((*dsb)->lock));
373 dsound->wfx.nSamplesPerSec = wfex->nSamplesPerSec;
374 dsound->wfx.nChannels = wfex->nChannels;
375 dsound->wfx.wBitsPerSample = wfex->wBitsPerSample;
376 dsound->wfx.nBlockAlign = dsound->wfx.wBitsPerSample / 8 * dsound->wfx.nChannels;
377 dsound->wfx.nAvgBytesPerSec =
378 dsound->wfx.nSamplesPerSec * dsound->wfx.nBlockAlign;
380 if (dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
381 DWORD flags = CALLBACK_FUNCTION;
382 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
383 flags |= WAVE_DIRECTSOUND;
384 /* FIXME: check for errors */
385 DSOUND_PrimaryClose(dsound);
386 waveOutClose(dsound->hwo);
388 err = mmErr(waveOutOpen(&(dsound->hwo), dsound->drvdesc.dnDevNode,
389 &(dsound->wfx), (DWORD)DSOUND_callback, (DWORD)dsound,
392 err = DSOUND_PrimaryOpen(dsound);
394 WARN("DSOUND_PrimaryOpen failed\n");
395 RtlReleaseResource(&(dsound->lock));
399 WARN("waveOutOpen failed\n");
400 RtlReleaseResource(&(dsound->lock));
405 err = IDsDriverBuffer_SetFormat(dsound->hwbuf, &(dsound->wfx));
406 if (err == DSERR_BUFFERLOST) {
407 /* Wine-only: the driver wants us to recreate the HW buffer */
408 IDsDriverBuffer_Release(dsound->hwbuf);
409 err = IDsDriver_CreateSoundBuffer(dsound->driver,&(dsound->wfx),
410 DSBCAPS_PRIMARYBUFFER,0,
411 &(dsound->buflen),&(dsound->buffer),
412 (LPVOID)&(dsound->hwbuf));
414 WARN("IDsDriver_CreateSoundBuffer failed\n");
415 RtlReleaseResource(&(dsound->lock));
418 if (dsound->state == STATE_PLAYING) dsound->state = STATE_STARTING;
419 else if (dsound->state == STATE_STOPPING) dsound->state = STATE_STOPPED;
421 WARN("IDsDriverBuffer_SetFormat failed\n");
422 RtlReleaseResource(&(dsound->lock));
425 /* FIXME: should we set err back to DS_OK in all cases ? */
427 DSOUND_RecalcPrimary(dsound);
429 RtlReleaseResource(&(dsound->lock));
435 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
436 LPDIRECTSOUNDBUFFER8 iface,LONG vol
438 ICOM_THIS(PrimaryBufferImpl,iface);
439 IDirectSoundImpl* dsound = This->dsound;
442 TRACE("(%p,%ld)\n",This,vol);
444 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
445 WARN("control unavailable\n");
446 return DSERR_CONTROLUNAVAIL;
449 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
450 WARN("invalid parameter: vol = %ld\n", vol);
451 return DSERR_INVALIDPARAM;
455 EnterCriticalSection(&(dsound->mixlock));
457 oldVol = dsound->volpan.lVolume;
458 dsound->volpan.lVolume = vol;
459 DSOUND_RecalcVolPan(&dsound->volpan);
464 hres = IDsDriverBuffer_SetVolumePan(dsound->hwbuf, &(dsound->volpan));
466 LeaveCriticalSection(&(dsound->mixlock));
467 WARN("IDsDriverBuffer_SetVolumePan failed\n");
471 DWORD vol = (dsound->volpan.dwTotalLeftAmpFactor & 0xffff) | (dsound->volpan.dwTotalRightAmpFactor << 16);
472 waveOutSetVolume(dsound->hwo, vol);
476 LeaveCriticalSection(&(dsound->mixlock));
482 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
483 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
485 ICOM_THIS(PrimaryBufferImpl,iface);
486 TRACE("(%p,%p)\n",This,vol);
488 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
489 WARN("control unavailable\n");
490 return DSERR_CONTROLUNAVAIL;
494 WARN("invalid parameter: vol = NULL\n");
495 return DSERR_INVALIDPARAM;
498 *vol = This->dsound->volpan.lVolume;
502 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
503 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
505 ICOM_THIS(PrimaryBufferImpl,iface);
507 TRACE("(%p,%ld)\n",This,freq);
509 /* You cannot set the frequency of the primary buffer */
510 WARN("control unavailable\n");
511 return DSERR_CONTROLUNAVAIL;
514 static HRESULT WINAPI PrimaryBufferImpl_Play(
515 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
517 ICOM_THIS(PrimaryBufferImpl,iface);
518 IDirectSoundImpl* dsound = This->dsound;
520 TRACE("(%p,%08lx,%08lx,%08lx)\n",
521 This,reserved1,reserved2,flags
524 if (!(flags & DSBPLAY_LOOPING)) {
525 WARN("invalid parameter: flags = %08lx\n", flags);
526 return DSERR_INVALIDPARAM;
530 EnterCriticalSection(&(dsound->mixlock));
532 if (dsound->state == STATE_STOPPED)
533 dsound->state = STATE_STARTING;
534 else if (dsound->state == STATE_STOPPING)
535 dsound->state = STATE_PLAYING;
537 LeaveCriticalSection(&(dsound->mixlock));
543 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
545 ICOM_THIS(PrimaryBufferImpl,iface);
546 IDirectSoundImpl* dsound = This->dsound;
548 TRACE("(%p)\n",This);
551 EnterCriticalSection(&(dsound->mixlock));
553 if (dsound->state == STATE_PLAYING)
554 dsound->state = STATE_STOPPING;
555 else if (dsound->state == STATE_STARTING)
556 dsound->state = STATE_STOPPED;
558 LeaveCriticalSection(&(dsound->mixlock));
564 static DWORD WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface) {
565 ICOM_THIS(PrimaryBufferImpl,iface);
568 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
569 ref = InterlockedIncrement(&(This->ref));
574 static DWORD WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface) {
575 ICOM_THIS(PrimaryBufferImpl,iface);
578 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
579 ref = InterlockedDecrement(&(This->ref));
582 This->dsound->primary = NULL;
583 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
584 HeapFree(GetProcessHeap(),0,This);
585 TRACE("(%p) released\n",This);
591 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
592 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
595 ICOM_THIS(PrimaryBufferImpl,iface);
596 IDirectSoundImpl* dsound = This->dsound;
598 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
599 hres = DSOUND_PrimaryGetPosition(dsound, playpos, writepos);
601 WARN("DSOUND_PrimaryGetPosition failed\n");
605 if (dsound->state != STATE_STOPPED)
606 /* apply the documented 10ms lead to writepos */
607 *writepos += dsound->writelead;
608 while (*writepos >= dsound->buflen) *writepos -= dsound->buflen;
610 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
614 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
615 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
617 ICOM_THIS(PrimaryBufferImpl,iface);
618 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
620 if (status == NULL) {
621 WARN("invalid parameter: status == NULL\n");
622 return DSERR_INVALIDPARAM;
626 if ((This->dsound->state == STATE_STARTING) ||
627 (This->dsound->state == STATE_PLAYING))
628 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
630 TRACE("status=%lx\n", *status);
635 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
636 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
638 ICOM_THIS(PrimaryBufferImpl,iface);
639 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
641 if (wfsize>sizeof(This->dsound->wfx))
642 wfsize = sizeof(This->dsound->wfx);
643 if (lpwf) { /* NULL is valid */
644 memcpy(lpwf,&(This->dsound->wfx),wfsize);
649 *wfwritten = sizeof(This->dsound->wfx);
651 WARN("invalid parameter: wfwritten == NULL\n");
652 return DSERR_INVALIDPARAM;
659 static HRESULT WINAPI PrimaryBufferImpl_Lock(
660 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
662 ICOM_THIS(PrimaryBufferImpl,iface);
663 IDirectSoundImpl* dsound = This->dsound;
665 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
677 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
678 WARN("failed priority check!\n");
679 return DSERR_PRIOLEVELNEEDED;
682 if (flags & DSBLOCK_FROMWRITECURSOR) {
685 /* GetCurrentPosition does too much magic to duplicate here */
686 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writepos);
688 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
691 writecursor += writepos;
693 while (writecursor >= dsound->buflen)
694 writecursor -= dsound->buflen;
695 if (flags & DSBLOCK_ENTIREBUFFER)
696 writebytes = dsound->buflen;
697 if (writebytes > dsound->buflen)
698 writebytes = dsound->buflen;
700 assert(audiobytes1!=audiobytes2);
701 assert(lplpaudioptr1!=lplpaudioptr2);
703 if (!(dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && dsound->hwbuf) {
705 hres = IDsDriverBuffer_Lock(dsound->hwbuf,
706 lplpaudioptr1, audiobytes1,
707 lplpaudioptr2, audiobytes2,
708 writecursor, writebytes,
711 WARN("IDsDriverBuffer_Lock failed\n");
715 if (writecursor+writebytes <= dsound->buflen) {
716 *(LPBYTE*)lplpaudioptr1 = dsound->buffer+writecursor;
717 *audiobytes1 = writebytes;
719 *(LPBYTE*)lplpaudioptr2 = NULL;
722 TRACE("->%ld.0\n",writebytes);
724 *(LPBYTE*)lplpaudioptr1 = dsound->buffer+writecursor;
725 *audiobytes1 = dsound->buflen-writecursor;
727 *(LPBYTE*)lplpaudioptr2 = dsound->buffer;
729 *audiobytes2 = writebytes-(dsound->buflen-writecursor);
730 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
736 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
737 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
739 ICOM_THIS(PrimaryBufferImpl,iface);
740 TRACE("(%p,%ld)\n",This,newpos);
742 /* You cannot set the position of the primary buffer */
743 WARN("invalid call\n");
744 return DSERR_INVALIDCALL;
747 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
748 LPDIRECTSOUNDBUFFER8 iface,LONG pan
750 ICOM_THIS(PrimaryBufferImpl,iface);
751 IDirectSoundImpl* dsound = This->dsound;
754 TRACE("(%p,%ld)\n",This,pan);
756 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
757 WARN("control unavailable\n");
758 return DSERR_CONTROLUNAVAIL;
761 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
762 WARN("invalid parameter: pan = %ld\n", pan);
763 return DSERR_INVALIDPARAM;
767 EnterCriticalSection(&(dsound->mixlock));
769 oldPan = dsound->volpan.lPan;
770 dsound->volpan.lPan = pan;
771 DSOUND_RecalcVolPan(&dsound->volpan);
776 hres = IDsDriverBuffer_SetVolumePan(dsound->hwbuf, &(dsound->volpan));
778 LeaveCriticalSection(&(dsound->mixlock));
779 WARN("IDsDriverBuffer_SetVolumePan failed\n");
784 DWORD vol = (dsound->volpan.dwTotalLeftAmpFactor & 0xffff) | (dsound->volpan.dwTotalRightAmpFactor << 16);
785 waveOutSetVolume(dsound->hwo, vol);
789 LeaveCriticalSection(&(dsound->mixlock));
795 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
796 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
798 ICOM_THIS(PrimaryBufferImpl,iface);
799 TRACE("(%p,%p)\n",This,pan);
801 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
802 WARN("control unavailable\n");
803 return DSERR_CONTROLUNAVAIL;
807 WARN("invalid parameter: pan == NULL\n");
808 return DSERR_INVALIDPARAM;
811 *pan = This->dsound->volpan.lPan;
816 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
817 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
819 ICOM_THIS(PrimaryBufferImpl,iface);
820 IDirectSoundImpl* dsound = This->dsound;
822 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
824 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
825 WARN("failed priority check!\n");
826 return DSERR_PRIOLEVELNEEDED;
829 if (!(dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && dsound->hwbuf) {
832 hres = IDsDriverBuffer_Unlock(dsound->hwbuf, p1, x1, p2, x2);
834 WARN("IDsDriverBuffer_Unlock failed\n");
842 static HRESULT WINAPI PrimaryBufferImpl_Restore(
843 LPDIRECTSOUNDBUFFER8 iface
845 ICOM_THIS(PrimaryBufferImpl,iface);
846 FIXME("(%p):stub\n",This);
850 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
851 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
853 ICOM_THIS(PrimaryBufferImpl,iface);
854 TRACE("(%p,%p)\n",This,freq);
857 WARN("invalid parameter: freq == NULL\n");
858 return DSERR_INVALIDPARAM;
861 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
862 WARN("control unavailable\n");
863 return DSERR_CONTROLUNAVAIL;
866 *freq = This->dsound->wfx.nSamplesPerSec;
867 TRACE("-> %ld\n", *freq);
872 static HRESULT WINAPI PrimaryBufferImpl_SetFX(
873 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
875 ICOM_THIS(PrimaryBufferImpl,iface);
878 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
881 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
883 WARN("control unavailable\n");
884 return DSERR_CONTROLUNAVAIL;
887 static HRESULT WINAPI PrimaryBufferImpl_AcquireResources(
888 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
890 ICOM_THIS(PrimaryBufferImpl,iface);
893 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
896 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
898 WARN("control unavailable\n");
899 return DSERR_CONTROLUNAVAIL;
902 static HRESULT WINAPI PrimaryBufferImpl_GetObjectInPath(
903 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
905 ICOM_THIS(PrimaryBufferImpl,iface);
907 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
909 WARN("control unavailable\n");
910 return DSERR_CONTROLUNAVAIL;
913 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
914 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND8 dsound,LPDSBUFFERDESC dbsd
916 ICOM_THIS(PrimaryBufferImpl,iface);
917 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
918 DPRINTF("Re-Init!!!\n");
919 WARN("already initialized\n");
920 return DSERR_ALREADYINITIALIZED;
923 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
924 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
926 ICOM_THIS(PrimaryBufferImpl,iface);
927 TRACE("(%p)->(%p)\n",This,caps);
930 WARN("invalid parameter: caps == NULL\n");
931 return DSERR_INVALIDPARAM;
934 if (caps->dwSize < sizeof(*caps)) {
935 WARN("invalid parameter: caps->dwSize = %ld: < %d\n", caps->dwSize, sizeof(*caps));
936 return DSERR_INVALIDPARAM;
939 caps->dwFlags = This->dsound->dsbd.dwFlags;
940 if (This->dsound->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
941 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
943 caps->dwBufferBytes = This->dsound->buflen;
945 /* This value represents the speed of the "unlock" command.
946 As unlock is quite fast (it does not do anything), I put
947 4096 ko/s = 4 Mo / s */
948 /* FIXME: hwbuf speed */
949 caps->dwUnlockTransferRate = 4096;
950 caps->dwPlayCpuOverhead = 0;
955 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
956 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
958 ICOM_THIS(PrimaryBufferImpl,iface);
959 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
962 WARN("invalid parameter\n");
966 *ppobj = NULL; /* assume failure */
968 if ( IsEqualGUID(riid, &IID_IUnknown) ||
969 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
970 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
975 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
976 /* a primary buffer can't have a DirectSoundBuffer8 interface */
977 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
978 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
979 return E_NOINTERFACE;
982 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
983 ERR("app requested IDirectSoundNotify on primary buffer\n");
984 /* FIXME: should we support this? */
985 return E_NOINTERFACE;
988 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
989 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
990 return E_NOINTERFACE;
993 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
994 if (!This->dsound->listener)
995 IDirectSound3DListenerImpl_Create(This, &This->dsound->listener);
996 if (This->dsound->listener) {
997 *ppobj = This->dsound->listener;
998 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1002 WARN("IID_IDirectSound3DListener failed\n");
1003 return E_NOINTERFACE;
1006 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1007 FIXME("app requested IKsPropertySet on primary buffer\n");
1008 return E_NOINTERFACE;
1011 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1012 return E_NOINTERFACE;
1015 static ICOM_VTABLE(IDirectSoundBuffer8) dspbvt =
1017 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1018 PrimaryBufferImpl_QueryInterface,
1019 PrimaryBufferImpl_AddRef,
1020 PrimaryBufferImpl_Release,
1021 PrimaryBufferImpl_GetCaps,
1022 PrimaryBufferImpl_GetCurrentPosition,
1023 PrimaryBufferImpl_GetFormat,
1024 PrimaryBufferImpl_GetVolume,
1025 PrimaryBufferImpl_GetPan,
1026 PrimaryBufferImpl_GetFrequency,
1027 PrimaryBufferImpl_GetStatus,
1028 PrimaryBufferImpl_Initialize,
1029 PrimaryBufferImpl_Lock,
1030 PrimaryBufferImpl_Play,
1031 PrimaryBufferImpl_SetCurrentPosition,
1032 PrimaryBufferImpl_SetFormat,
1033 PrimaryBufferImpl_SetVolume,
1034 PrimaryBufferImpl_SetPan,
1035 PrimaryBufferImpl_SetFrequency,
1036 PrimaryBufferImpl_Stop,
1037 PrimaryBufferImpl_Unlock,
1038 PrimaryBufferImpl_Restore,
1039 PrimaryBufferImpl_SetFX,
1040 PrimaryBufferImpl_AcquireResources,
1041 PrimaryBufferImpl_GetObjectInPath
1044 HRESULT WINAPI PrimaryBufferImpl_Create(
1045 IDirectSoundImpl *ds,
1046 PrimaryBufferImpl **pdsb,
1047 LPDSBUFFERDESC dsbd)
1049 PrimaryBufferImpl *dsb;
1051 TRACE("%p,%p,%p)\n",ds,pdsb,dsbd);
1053 if (dsbd->lpwfxFormat) {
1054 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1056 return DSERR_INVALIDPARAM;
1059 dsb = (PrimaryBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1062 WARN("out of memory\n");
1064 return DSERR_OUTOFMEMORY;
1069 dsb->lpVtbl = &dspbvt;
1071 memcpy(&ds->dsbd, dsbd, sizeof(*dsbd));
1073 TRACE("Created primary buffer at %p\n", dsb);
1074 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1075 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1076 ds->wfx.wFormatTag, ds->wfx.nChannels, ds->wfx.nSamplesPerSec,
1077 ds->wfx.nAvgBytesPerSec, ds->wfx.nBlockAlign,
1078 ds->wfx.wBitsPerSample, ds->wfx.cbSize);
1080 IDirectSound_AddRef((LPDIRECTSOUND)ds);