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 /* are we using waveOut stuff? */
82 /* Start in pause mode, to allow buffers to get filled */
83 waveOutPause(This->hwo);
84 if (This->state == STATE_PLAYING) This->state = STATE_STARTING;
85 else if (This->state == STATE_STOPPING) This->state = STATE_STOPPED;
86 /* use fragments of 10ms (1/100s) each (which should get us within
87 * the documented write cursor lead of 10-15ms) */
88 buflen = ((This->wfx.nAvgBytesPerSec / 100) & ~3) * DS_HEL_FRAGS;
89 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, This->buffer);
90 /* reallocate emulated primary buffer */
93 newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,This->buffer,buflen);
95 newbuf = (LPBYTE)HeapAlloc(GetProcessHeap(),0,buflen);
98 ERR("failed to allocate primary buffer\n");
99 merr = DSERR_OUTOFMEMORY;
100 /* but the old buffer might still exist and must be re-prepared */
102 This->buffer = newbuf;
103 This->buflen = buflen;
108 This->fraglen = This->buflen / DS_HEL_FRAGS;
110 /* prepare fragment headers */
111 for (c=0; c<DS_HEL_FRAGS; c++) {
112 This->pwave[c]->lpData = This->buffer + c*This->fraglen;
113 This->pwave[c]->dwBufferLength = This->fraglen;
114 This->pwave[c]->dwUser = (DWORD)This;
115 This->pwave[c]->dwFlags = 0;
116 This->pwave[c]->dwLoops = 0;
117 err = mmErr(waveOutPrepareHeader(This->hwo,This->pwave[c],sizeof(WAVEHDR)));
120 waveOutUnprepareHeader(This->hwo,This->pwave[c],sizeof(WAVEHDR));
130 memset(This->buffer, (This->wfx.wBitsPerSample == 16) ? 0 : 128, This->buflen);
131 TRACE("fraglen=%ld\n", This->fraglen);
132 DSOUND_WaveQueue(This, (DWORD)-1);
134 if ((err == DS_OK) && (merr != DS_OK))
138 err = IDsDriver_CreateSoundBuffer(This->driver,&(This->wfx),
139 DSBCAPS_PRIMARYBUFFER,0,
140 &(This->buflen),&(This->buffer),
141 (LPVOID*)&(This->hwbuf));
143 WARN("IDsDriver_CreateSoundBuffer failed\n");
147 if (dsound->state == STATE_PLAYING) dsound->state = STATE_STARTING;
148 else if (dsound->state == STATE_STOPPING) dsound->state = STATE_STOPPED;
156 static void DSOUND_PrimaryClose(IDirectSoundImpl *This)
158 TRACE("(%p)\n",This);
160 /* are we using waveOut stuff? */
164 This->pwqueue = (DWORD)-1; /* resetting queues */
165 waveOutReset(This->hwo);
166 for (c=0; c<DS_HEL_FRAGS; c++)
167 waveOutUnprepareHeader(This->hwo, This->pwave[c], sizeof(WAVEHDR));
170 if (IDsDriverBuffer_Release(This->hwbuf) == 0)
175 HRESULT DSOUND_PrimaryCreate(IDirectSoundImpl *This)
178 TRACE("(%p)\n",This);
180 This->buflen = This->wfx.nAvgBytesPerSec;
182 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
185 err = IDsDriver_CreateSoundBuffer(This->driver,&(This->wfx),
186 DSBCAPS_PRIMARYBUFFER,0,
187 &(This->buflen),&(This->buffer),
188 (LPVOID*)&(This->hwbuf));
190 WARN("IDsDriver_CreateSoundBuffer failed\n");
195 /* Allocate memory for HEL buffer headers */
197 for (c=0; c<DS_HEL_FRAGS; c++) {
198 This->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
199 if (!This->pwave[c]) {
200 /* Argh, out of memory */
202 HeapFree(GetProcessHeap(),0,This->pwave[c]);
204 WARN("out of memory\n");
205 return DSERR_OUTOFMEMORY;
210 err = DSOUND_PrimaryOpen(This);
213 WARN("DSOUND_PrimaryOpen failed\n");
217 /* calculate fragment size and write lead */
218 DSOUND_RecalcPrimary(This);
219 This->state = STATE_STOPPED;
223 HRESULT DSOUND_PrimaryDestroy(IDirectSoundImpl *This)
225 TRACE("(%p)\n",This);
227 DSOUND_PrimaryClose(This);
230 if (IDsDriverBuffer_Release(This->hwbuf) == 0)
235 for (c=0; c<DS_HEL_FRAGS; c++) {
236 HeapFree(GetProcessHeap(),0,This->pwave[c]);
242 HRESULT DSOUND_PrimaryPlay(IDirectSoundImpl *This)
245 TRACE("(%p)\n",This);
248 err = IDsDriverBuffer_Play(This->hwbuf, 0, 0, DSBPLAY_LOOPING);
250 WARN("IDsDriverBuffer_Play failed\n");
252 err = mmErr(waveOutRestart(This->hwo));
254 WARN("waveOutRestart failed\n");
260 HRESULT DSOUND_PrimaryStop(IDirectSoundImpl *This)
263 TRACE("(%p)\n",This);
266 err = IDsDriverBuffer_Stop(This->hwbuf);
267 if (err == DSERR_BUFFERLOST) {
268 DWORD flags = CALLBACK_FUNCTION;
269 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
270 flags |= WAVE_DIRECTSOUND;
271 /* Wine-only: the driver wants us to reopen the device */
272 /* FIXME: check for errors */
273 IDsDriverBuffer_Release(This->hwbuf);
274 waveOutClose(This->hwo);
276 err = mmErr(waveOutOpen(&(This->hwo), This->drvdesc.dnDevNode,
277 &(This->wfx), (DWORD)DSOUND_callback, (DWORD)This,
280 err = IDsDriver_CreateSoundBuffer(This->driver,&(This->wfx),
281 DSBCAPS_PRIMARYBUFFER,0,
282 &(This->buflen),&(This->buffer),
283 (LPVOID)&(This->hwbuf));
285 WARN("IDsDriver_CreateSoundBuffer failed\n");
287 WARN("waveOutOpen failed\n");
289 } else if (err != DS_OK) {
290 WARN("IDsDriverBuffer_Stop failed\n");
293 err = mmErr(waveOutPause(This->hwo));
295 WARN("waveOutPause failed\n");
300 HRESULT DSOUND_PrimaryGetPosition(IDirectSoundImpl *This, LPDWORD playpos, LPDWORD writepos)
302 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
305 HRESULT err=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
307 WARN("IDsDriverBuffer_GetPosition failed\n");
314 mtime.wType = TIME_BYTES;
315 waveOutGetPosition(This->hwo, &mtime, sizeof(mtime));
316 mtime.u.cb = mtime.u.cb % This->buflen;
317 *playpos = mtime.u.cb;
320 /* the writepos should only be used by apps with WRITEPRIMARY priority,
321 * in which case our software mixer is disabled anyway */
322 *writepos = (This->pwplay + ds_hel_margin) * This->fraglen;
323 while (*writepos >= This->buflen)
324 *writepos -= This->buflen;
327 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
332 /*******************************************************************************
335 /* This sets this format for the <em>Primary Buffer Only</em> */
336 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
337 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
338 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
340 ICOM_THIS(PrimaryBufferImpl,iface);
341 IDirectSoundImpl* dsound = This->dsound;
344 DWORD nSamplesPerSec;
345 TRACE("(%p,%p)\n",This,wfex);
347 if (This->dsound->priolevel == DSSCL_NORMAL) {
348 WARN("failed priority check!\n");
349 return DSERR_PRIOLEVELNEEDED;
352 /* Let's be pedantic! */
354 WARN("invalid parameter: wfex==NULL!\n");
355 return DSERR_INVALIDPARAM;
357 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
358 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
359 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
360 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
361 wfex->wBitsPerSample, wfex->cbSize);
363 if ((wfex->wFormatTag != WAVE_FORMAT_PCM) ||
364 (wfex->nChannels < 1) || (wfex->nChannels > 2) ||
365 (wfex->nSamplesPerSec < 1) ||
366 ((wfex->wBitsPerSample != 8) && (wfex->wBitsPerSample != 16))) {
367 WARN("invalid paramemer: unsupported format!\n");
368 return DSERR_INVALIDPARAM;
372 RtlAcquireResourceExclusive(&(dsound->lock), TRUE);
374 nSamplesPerSec = dsound->wfx.nSamplesPerSec;
375 dsound->wfx.nSamplesPerSec = wfex->nSamplesPerSec;
376 dsound->wfx.nChannels = wfex->nChannels;
377 dsound->wfx.wBitsPerSample = wfex->wBitsPerSample;
378 dsound->wfx.nBlockAlign = dsound->wfx.wBitsPerSample / 8 * dsound->wfx.nChannels;
379 dsound->wfx.nAvgBytesPerSec =
380 dsound->wfx.nSamplesPerSec * dsound->wfx.nBlockAlign;
382 if (dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
383 DWORD flags = CALLBACK_FUNCTION;
384 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
385 flags |= WAVE_DIRECTSOUND;
386 /* FIXME: check for errors */
387 DSOUND_PrimaryClose(dsound);
388 waveOutClose(dsound->hwo);
390 err = mmErr(waveOutOpen(&(dsound->hwo), dsound->drvdesc.dnDevNode,
391 &(dsound->wfx), (DWORD)DSOUND_callback, (DWORD)dsound,
394 err = DSOUND_PrimaryOpen(dsound);
396 WARN("DSOUND_PrimaryOpen failed\n");
397 RtlReleaseResource(&(dsound->lock));
401 WARN("waveOutOpen failed\n");
402 RtlReleaseResource(&(dsound->lock));
405 } else if (dsound->hwbuf) {
406 err = IDsDriverBuffer_SetFormat(dsound->hwbuf, &(dsound->wfx));
407 if (err == DSERR_BUFFERLOST) {
408 /* Wine-only: the driver wants us to recreate the HW buffer */
409 IDsDriverBuffer_Release(dsound->hwbuf);
410 err = IDsDriver_CreateSoundBuffer(dsound->driver,&(dsound->wfx),
411 DSBCAPS_PRIMARYBUFFER,0,
412 &(dsound->buflen),&(dsound->buffer),
413 (LPVOID)&(dsound->hwbuf));
415 WARN("IDsDriver_CreateSoundBuffer failed\n");
416 RtlReleaseResource(&(dsound->lock));
419 if (dsound->state == STATE_PLAYING) dsound->state = STATE_STARTING;
420 else if (dsound->state == STATE_STOPPING) dsound->state = STATE_STOPPED;
422 WARN("IDsDriverBuffer_SetFormat failed\n");
423 RtlReleaseResource(&(dsound->lock));
426 /* FIXME: should we set err back to DS_OK in all cases ? */
428 DSOUND_RecalcPrimary(dsound);
430 if (nSamplesPerSec != dsound->wfx.nSamplesPerSec) {
431 IDirectSoundBufferImpl** dsb = dsound->buffers;
432 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
434 EnterCriticalSection(&((*dsb)->lock));
436 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
437 wfex->nSamplesPerSec;
439 LeaveCriticalSection(&((*dsb)->lock));
444 RtlReleaseResource(&(dsound->lock));
450 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
451 LPDIRECTSOUNDBUFFER8 iface,LONG vol
453 ICOM_THIS(PrimaryBufferImpl,iface);
454 IDirectSoundImpl* dsound = This->dsound;
458 TRACE("(%p,%ld)\n",This,vol);
460 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
461 WARN("control unavailable\n");
462 return DSERR_CONTROLUNAVAIL;
465 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
466 WARN("invalid parameter: vol = %ld\n", vol);
467 return DSERR_INVALIDPARAM;
471 EnterCriticalSection(&(dsound->mixlock));
473 waveOutGetVolume(dsound->hwo, &factors);
474 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
475 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
476 DSOUND_AmpFactorToVolPan(&volpan);
477 if (vol != volpan.lVolume) {
479 DSOUND_RecalcVolPan(&volpan);
482 hres = IDsDriverBuffer_SetVolumePan(dsound->hwbuf, &volpan);
484 LeaveCriticalSection(&(dsound->mixlock));
485 WARN("IDsDriverBuffer_SetVolumePan failed\n");
489 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
490 waveOutSetVolume(dsound->hwo, ampfactors);
494 LeaveCriticalSection(&(dsound->mixlock));
500 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
501 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
503 ICOM_THIS(PrimaryBufferImpl,iface);
506 TRACE("(%p,%p)\n",This,vol);
508 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
509 WARN("control unavailable\n");
510 return DSERR_CONTROLUNAVAIL;
514 WARN("invalid parameter: vol = NULL\n");
515 return DSERR_INVALIDPARAM;
518 waveOutGetVolume(dsound->hwo, &factors);
519 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
520 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
521 DSOUND_AmpFactorToVolPan(&volpan);
522 *vol = volpan.lVolume;
526 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
527 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
529 ICOM_THIS(PrimaryBufferImpl,iface);
531 TRACE("(%p,%ld)\n",This,freq);
533 /* You cannot set the frequency of the primary buffer */
534 WARN("control unavailable\n");
535 return DSERR_CONTROLUNAVAIL;
538 static HRESULT WINAPI PrimaryBufferImpl_Play(
539 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
541 ICOM_THIS(PrimaryBufferImpl,iface);
542 IDirectSoundImpl* dsound = This->dsound;
544 TRACE("(%p,%08lx,%08lx,%08lx)\n",
545 This,reserved1,reserved2,flags
548 if (!(flags & DSBPLAY_LOOPING)) {
549 WARN("invalid parameter: flags = %08lx\n", flags);
550 return DSERR_INVALIDPARAM;
554 EnterCriticalSection(&(dsound->mixlock));
556 if (dsound->state == STATE_STOPPED)
557 dsound->state = STATE_STARTING;
558 else if (dsound->state == STATE_STOPPING)
559 dsound->state = STATE_PLAYING;
561 LeaveCriticalSection(&(dsound->mixlock));
567 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
569 ICOM_THIS(PrimaryBufferImpl,iface);
570 IDirectSoundImpl* dsound = This->dsound;
572 TRACE("(%p)\n",This);
575 EnterCriticalSection(&(dsound->mixlock));
577 if (dsound->state == STATE_PLAYING)
578 dsound->state = STATE_STOPPING;
579 else if (dsound->state == STATE_STARTING)
580 dsound->state = STATE_STOPPED;
582 LeaveCriticalSection(&(dsound->mixlock));
588 static DWORD WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface) {
589 ICOM_THIS(PrimaryBufferImpl,iface);
592 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
593 ref = InterlockedIncrement(&(This->ref));
598 static DWORD WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface) {
599 ICOM_THIS(PrimaryBufferImpl,iface);
602 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
603 ref = InterlockedDecrement(&(This->ref));
606 This->dsound->primary = NULL;
607 HeapFree(GetProcessHeap(),0,This);
608 TRACE("(%p) released\n",This);
614 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
615 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
618 ICOM_THIS(PrimaryBufferImpl,iface);
619 IDirectSoundImpl* dsound = This->dsound;
621 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
622 hres = DSOUND_PrimaryGetPosition(dsound, playpos, writepos);
624 WARN("DSOUND_PrimaryGetPosition failed\n");
628 if (dsound->state != STATE_STOPPED)
629 /* apply the documented 10ms lead to writepos */
630 *writepos += dsound->writelead;
631 while (*writepos >= dsound->buflen) *writepos -= dsound->buflen;
633 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
637 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
638 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
640 ICOM_THIS(PrimaryBufferImpl,iface);
641 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
643 if (status == NULL) {
644 WARN("invalid parameter: status == NULL\n");
645 return DSERR_INVALIDPARAM;
649 if ((This->dsound->state == STATE_STARTING) ||
650 (This->dsound->state == STATE_PLAYING))
651 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
653 TRACE("status=%lx\n", *status);
658 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
659 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
661 ICOM_THIS(PrimaryBufferImpl,iface);
662 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
664 if (wfsize>sizeof(This->dsound->wfx))
665 wfsize = sizeof(This->dsound->wfx);
666 if (lpwf) { /* NULL is valid */
667 memcpy(lpwf,&(This->dsound->wfx),wfsize);
672 *wfwritten = sizeof(This->dsound->wfx);
674 WARN("invalid parameter: wfwritten == NULL\n");
675 return DSERR_INVALIDPARAM;
682 static HRESULT WINAPI PrimaryBufferImpl_Lock(
683 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
685 ICOM_THIS(PrimaryBufferImpl,iface);
686 IDirectSoundImpl* dsound = This->dsound;
688 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
700 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
701 WARN("failed priority check!\n");
702 return DSERR_PRIOLEVELNEEDED;
705 if (flags & DSBLOCK_FROMWRITECURSOR) {
708 /* GetCurrentPosition does too much magic to duplicate here */
709 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writepos);
711 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
714 writecursor += writepos;
716 while (writecursor >= dsound->buflen)
717 writecursor -= dsound->buflen;
718 if (flags & DSBLOCK_ENTIREBUFFER)
719 writebytes = dsound->buflen;
720 if (writebytes > dsound->buflen)
721 writebytes = dsound->buflen;
723 if (!(dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && dsound->hwbuf) {
725 hres = IDsDriverBuffer_Lock(dsound->hwbuf,
726 lplpaudioptr1, audiobytes1,
727 lplpaudioptr2, audiobytes2,
728 writecursor, writebytes,
731 WARN("IDsDriverBuffer_Lock failed\n");
735 if (writecursor+writebytes <= dsound->buflen) {
736 *(LPBYTE*)lplpaudioptr1 = dsound->buffer+writecursor;
737 *audiobytes1 = writebytes;
739 *(LPBYTE*)lplpaudioptr2 = NULL;
742 TRACE("->%ld.0\n",writebytes);
744 *(LPBYTE*)lplpaudioptr1 = dsound->buffer+writecursor;
745 *audiobytes1 = dsound->buflen-writecursor;
747 *(LPBYTE*)lplpaudioptr2 = dsound->buffer;
749 *audiobytes2 = writebytes-(dsound->buflen-writecursor);
750 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
756 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
757 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
759 ICOM_THIS(PrimaryBufferImpl,iface);
760 TRACE("(%p,%ld)\n",This,newpos);
762 /* You cannot set the position of the primary buffer */
763 WARN("invalid call\n");
764 return DSERR_INVALIDCALL;
767 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
768 LPDIRECTSOUNDBUFFER8 iface,LONG pan
770 ICOM_THIS(PrimaryBufferImpl,iface);
771 IDirectSoundImpl* dsound = This->dsound;
775 TRACE("(%p,%ld)\n",This,pan);
777 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
778 WARN("control unavailable\n");
779 return DSERR_CONTROLUNAVAIL;
782 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
783 WARN("invalid parameter: pan = %ld\n", pan);
784 return DSERR_INVALIDPARAM;
788 EnterCriticalSection(&(dsound->mixlock));
790 waveOutGetVolume(dsound->hwo, &factors);
791 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
792 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
793 DSOUND_AmpFactorToVolPan(&volpan);
794 if (pan != volpan.lPan) {
796 DSOUND_RecalcVolPan(&volpan);
799 hres = IDsDriverBuffer_SetVolumePan(dsound->hwbuf, &volpan);
801 LeaveCriticalSection(&(dsound->mixlock));
802 WARN("IDsDriverBuffer_SetVolumePan failed\n");
807 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
808 waveOutSetVolume(dsound->hwo, ampfactors);
812 LeaveCriticalSection(&(dsound->mixlock));
818 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
819 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
821 ICOM_THIS(PrimaryBufferImpl,iface);
824 TRACE("(%p,%p)\n",This,pan);
826 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
827 WARN("control unavailable\n");
828 return DSERR_CONTROLUNAVAIL;
832 WARN("invalid parameter: pan == NULL\n");
833 return DSERR_INVALIDPARAM;
836 waveOutGetVolume(dsound->hwo, &factors);
837 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
838 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
839 DSOUND_AmpFactorToVolPan(&volpan);
844 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
845 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
847 ICOM_THIS(PrimaryBufferImpl,iface);
848 IDirectSoundImpl* dsound = This->dsound;
850 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
852 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
853 WARN("failed priority check!\n");
854 return DSERR_PRIOLEVELNEEDED;
857 if (!(dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && dsound->hwbuf) {
860 hres = IDsDriverBuffer_Unlock(dsound->hwbuf, p1, x1, p2, x2);
862 WARN("IDsDriverBuffer_Unlock failed\n");
870 static HRESULT WINAPI PrimaryBufferImpl_Restore(
871 LPDIRECTSOUNDBUFFER8 iface
873 ICOM_THIS(PrimaryBufferImpl,iface);
874 FIXME("(%p):stub\n",This);
878 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
879 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
881 ICOM_THIS(PrimaryBufferImpl,iface);
882 TRACE("(%p,%p)\n",This,freq);
885 WARN("invalid parameter: freq == NULL\n");
886 return DSERR_INVALIDPARAM;
889 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
890 WARN("control unavailable\n");
891 return DSERR_CONTROLUNAVAIL;
894 *freq = This->dsound->wfx.nSamplesPerSec;
895 TRACE("-> %ld\n", *freq);
900 static HRESULT WINAPI PrimaryBufferImpl_SetFX(
901 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
903 ICOM_THIS(PrimaryBufferImpl,iface);
906 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
909 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
911 WARN("control unavailable\n");
912 return DSERR_CONTROLUNAVAIL;
915 static HRESULT WINAPI PrimaryBufferImpl_AcquireResources(
916 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
918 ICOM_THIS(PrimaryBufferImpl,iface);
921 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
924 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
926 WARN("control unavailable\n");
927 return DSERR_CONTROLUNAVAIL;
930 static HRESULT WINAPI PrimaryBufferImpl_GetObjectInPath(
931 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
933 ICOM_THIS(PrimaryBufferImpl,iface);
935 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
937 WARN("control unavailable\n");
938 return DSERR_CONTROLUNAVAIL;
941 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
942 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
944 ICOM_THIS(PrimaryBufferImpl,iface);
945 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
946 DPRINTF("Re-Init!!!\n");
947 WARN("already initialized\n");
948 return DSERR_ALREADYINITIALIZED;
951 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
952 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
954 ICOM_THIS(PrimaryBufferImpl,iface);
955 TRACE("(%p)->(%p)\n",This,caps);
958 WARN("invalid parameter: caps == NULL\n");
959 return DSERR_INVALIDPARAM;
962 if (caps->dwSize < sizeof(*caps)) {
963 WARN("invalid parameter: caps->dwSize = %ld: < %d\n", caps->dwSize, sizeof(*caps));
964 return DSERR_INVALIDPARAM;
967 caps->dwFlags = This->dsound->dsbd.dwFlags;
968 if (This->dsound->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
969 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
971 caps->dwBufferBytes = This->dsound->buflen;
973 /* This value represents the speed of the "unlock" command.
974 As unlock is quite fast (it does not do anything), I put
975 4096 ko/s = 4 Mo / s */
976 /* FIXME: hwbuf speed */
977 caps->dwUnlockTransferRate = 4096;
978 caps->dwPlayCpuOverhead = 0;
983 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
984 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
986 ICOM_THIS(PrimaryBufferImpl,iface);
987 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
990 WARN("invalid parameter\n");
994 *ppobj = NULL; /* assume failure */
996 if ( IsEqualGUID(riid, &IID_IUnknown) ||
997 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
998 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1003 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1004 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1005 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1006 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1007 return E_NOINTERFACE;
1010 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1011 ERR("app requested IDirectSoundNotify on primary buffer\n");
1012 /* FIXME: should we support this? */
1013 return E_NOINTERFACE;
1016 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1017 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1018 return E_NOINTERFACE;
1021 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1022 if (!This->dsound->listener)
1023 IDirectSound3DListenerImpl_Create(This, &This->dsound->listener);
1024 if (This->dsound->listener) {
1025 *ppobj = This->dsound->listener;
1026 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1030 WARN("IID_IDirectSound3DListener failed\n");
1031 return E_NOINTERFACE;
1034 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1035 FIXME("app requested IKsPropertySet on primary buffer\n");
1036 return E_NOINTERFACE;
1039 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1040 return E_NOINTERFACE;
1043 static ICOM_VTABLE(IDirectSoundBuffer8) dspbvt =
1045 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1046 PrimaryBufferImpl_QueryInterface,
1047 PrimaryBufferImpl_AddRef,
1048 PrimaryBufferImpl_Release,
1049 PrimaryBufferImpl_GetCaps,
1050 PrimaryBufferImpl_GetCurrentPosition,
1051 PrimaryBufferImpl_GetFormat,
1052 PrimaryBufferImpl_GetVolume,
1053 PrimaryBufferImpl_GetPan,
1054 PrimaryBufferImpl_GetFrequency,
1055 PrimaryBufferImpl_GetStatus,
1056 PrimaryBufferImpl_Initialize,
1057 PrimaryBufferImpl_Lock,
1058 PrimaryBufferImpl_Play,
1059 PrimaryBufferImpl_SetCurrentPosition,
1060 PrimaryBufferImpl_SetFormat,
1061 PrimaryBufferImpl_SetVolume,
1062 PrimaryBufferImpl_SetPan,
1063 PrimaryBufferImpl_SetFrequency,
1064 PrimaryBufferImpl_Stop,
1065 PrimaryBufferImpl_Unlock,
1066 PrimaryBufferImpl_Restore,
1067 PrimaryBufferImpl_SetFX,
1068 PrimaryBufferImpl_AcquireResources,
1069 PrimaryBufferImpl_GetObjectInPath
1072 HRESULT WINAPI PrimaryBufferImpl_Create(
1073 IDirectSoundImpl *ds,
1074 PrimaryBufferImpl **pdsb,
1075 LPCDSBUFFERDESC dsbd)
1077 PrimaryBufferImpl *dsb;
1079 TRACE("%p,%p,%p)\n",ds,pdsb,dsbd);
1081 if (dsbd->lpwfxFormat) {
1082 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1084 return DSERR_INVALIDPARAM;
1087 dsb = (PrimaryBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1090 WARN("out of memory\n");
1092 return DSERR_OUTOFMEMORY;
1097 dsb->lpVtbl = &dspbvt;
1099 memcpy(&ds->dsbd, dsbd, sizeof(*dsbd));
1101 TRACE("Created primary buffer at %p\n", dsb);
1102 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1103 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1104 ds->wfx.wFormatTag, ds->wfx.nChannels, ds->wfx.nSamplesPerSec,
1105 ds->wfx.nAvgBytesPerSec, ds->wfx.nBlockAlign,
1106 ds->wfx.wBitsPerSample, ds->wfx.cbSize);