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
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
32 #include "wine/debug.h"
35 #include "dsound_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
39 void DSOUND_RecalcPrimary(IDirectSoundImpl *This)
44 nBlockAlign = This->pwfx->nBlockAlign;
47 /* let fragment size approximate the timer delay */
48 fraglen = (This->pwfx->nSamplesPerSec * DS_TIME_DEL / 1000) * nBlockAlign;
49 /* reduce fragment size until an integer number of them fits in the buffer */
50 /* (FIXME: this may or may not be a good idea) */
51 while (This->buflen % fraglen) fraglen -= nBlockAlign;
52 This->fraglen = fraglen;
53 TRACE("fraglen=%ld\n", This->fraglen);
55 /* calculate the 10ms write lead */
56 This->writelead = (This->pwfx->nSamplesPerSec / 100) * nBlockAlign;
59 static HRESULT DSOUND_PrimaryOpen(IDirectSoundImpl *This)
64 /* are we using waveOut stuff? */
69 /* Start in pause mode, to allow buffers to get filled */
70 waveOutPause(This->hwo);
71 if (This->state == STATE_PLAYING) This->state = STATE_STARTING;
72 else if (This->state == STATE_STOPPING) This->state = STATE_STOPPED;
73 /* use fragments of 10ms (1/100s) each (which should get us within
74 * the documented write cursor lead of 10-15ms) */
75 buflen = ((This->pwfx->nSamplesPerSec / 100) * This->pwfx->nBlockAlign) * DS_HEL_FRAGS;
76 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, This->buffer);
77 /* reallocate emulated primary buffer */
80 newbuf = HeapReAlloc(GetProcessHeap(),0,This->buffer,buflen);
82 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
85 ERR("failed to allocate primary buffer\n");
86 merr = DSERR_OUTOFMEMORY;
87 /* but the old buffer might still exist and must be re-prepared */
89 This->buffer = newbuf;
90 This->buflen = buflen;
95 This->fraglen = This->buflen / DS_HEL_FRAGS;
97 /* prepare fragment headers */
98 for (c=0; c<DS_HEL_FRAGS; c++) {
99 This->pwave[c]->lpData = This->buffer + c*This->fraglen;
100 This->pwave[c]->dwBufferLength = This->fraglen;
101 This->pwave[c]->dwUser = (DWORD)This;
102 This->pwave[c]->dwFlags = 0;
103 This->pwave[c]->dwLoops = 0;
104 err = mmErr(waveOutPrepareHeader(This->hwo,This->pwave[c],sizeof(WAVEHDR)));
107 waveOutUnprepareHeader(This->hwo,This->pwave[c],sizeof(WAVEHDR));
117 FillMemory(This->buffer, This->buflen, (This->pwfx->wBitsPerSample == 8) ? 128 : 0);
118 TRACE("fraglen=%ld\n", This->fraglen);
119 DSOUND_WaveQueue(This, (DWORD)-1);
121 if ((err == DS_OK) && (merr != DS_OK))
123 } else if (!This->hwbuf) {
124 err = IDsDriver_CreateSoundBuffer(This->driver,This->pwfx,
125 DSBCAPS_PRIMARYBUFFER,0,
126 &(This->buflen),&(This->buffer),
127 (LPVOID*)&(This->hwbuf));
129 WARN("IDsDriver_CreateSoundBuffer failed\n");
133 if (This->state == STATE_PLAYING) This->state = STATE_STARTING;
134 else if (This->state == STATE_STOPPING) This->state = STATE_STOPPED;
137 FillMemory(This->buffer, This->buflen, (This->pwfx->wBitsPerSample == 8) ? 128 : 0);
144 static void DSOUND_PrimaryClose(IDirectSoundImpl *This)
146 TRACE("(%p)\n",This);
148 /* are we using waveOut stuff? */
152 This->pwqueue = (DWORD)-1; /* resetting queues */
153 waveOutReset(This->hwo);
154 for (c=0; c<DS_HEL_FRAGS; c++)
155 waveOutUnprepareHeader(This->hwo, This->pwave[c], sizeof(WAVEHDR));
158 if (IDsDriverBuffer_Release(This->hwbuf) == 0)
163 HRESULT DSOUND_PrimaryCreate(IDirectSoundImpl *This)
166 TRACE("(%p)\n",This);
168 This->buflen = This->pwfx->nAvgBytesPerSec;
170 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
173 err = IDsDriver_CreateSoundBuffer(This->driver,This->pwfx,
174 DSBCAPS_PRIMARYBUFFER,0,
175 &(This->buflen),&(This->buffer),
176 (LPVOID*)&(This->hwbuf));
178 WARN("IDsDriver_CreateSoundBuffer failed\n");
183 /* Allocate memory for HEL buffer headers */
185 for (c=0; c<DS_HEL_FRAGS; c++) {
186 This->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
187 if (!This->pwave[c]) {
188 /* Argh, out of memory */
190 HeapFree(GetProcessHeap(),0,This->pwave[c]);
192 WARN("out of memory\n");
193 return DSERR_OUTOFMEMORY;
198 err = DSOUND_PrimaryOpen(This);
201 WARN("DSOUND_PrimaryOpen failed\n");
205 /* calculate fragment size and write lead */
206 DSOUND_RecalcPrimary(This);
207 This->state = STATE_STOPPED;
211 HRESULT DSOUND_PrimaryDestroy(IDirectSoundImpl *This)
213 TRACE("(%p)\n",This);
215 DSOUND_PrimaryClose(This);
218 if (IDsDriverBuffer_Release(This->hwbuf) == 0)
223 for (c=0; c<DS_HEL_FRAGS; c++) {
224 HeapFree(GetProcessHeap(),0,This->pwave[c]);
227 HeapFree(GetProcessHeap(),0,This->pwfx);
232 HRESULT DSOUND_PrimaryPlay(IDirectSoundImpl *This)
235 TRACE("(%p)\n",This);
238 err = IDsDriverBuffer_Play(This->hwbuf, 0, 0, DSBPLAY_LOOPING);
240 WARN("IDsDriverBuffer_Play failed\n");
242 err = mmErr(waveOutRestart(This->hwo));
244 WARN("waveOutRestart failed\n");
250 HRESULT DSOUND_PrimaryStop(IDirectSoundImpl *This)
253 TRACE("(%p)\n",This);
256 err = IDsDriverBuffer_Stop(This->hwbuf);
257 if (err == DSERR_BUFFERLOST) {
258 DWORD flags = CALLBACK_FUNCTION;
259 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
260 flags |= WAVE_DIRECTSOUND;
261 /* Wine-only: the driver wants us to reopen the device */
262 /* FIXME: check for errors */
263 IDsDriverBuffer_Release(This->hwbuf);
264 waveOutClose(This->hwo);
266 err = mmErr(waveOutOpen(&(This->hwo), This->drvdesc.dnDevNode,
267 This->pwfx, (DWORD)DSOUND_callback, (DWORD)This,
270 err = IDsDriver_CreateSoundBuffer(This->driver,This->pwfx,
271 DSBCAPS_PRIMARYBUFFER,0,
272 &(This->buflen),&(This->buffer),
273 (LPVOID)&(This->hwbuf));
275 WARN("IDsDriver_CreateSoundBuffer failed\n");
277 WARN("waveOutOpen failed\n");
279 } else if (err != DS_OK) {
280 WARN("IDsDriverBuffer_Stop failed\n");
283 err = mmErr(waveOutPause(This->hwo));
285 WARN("waveOutPause failed\n");
290 HRESULT DSOUND_PrimaryGetPosition(IDirectSoundImpl *This, LPDWORD playpos, LPDWORD writepos)
292 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
295 HRESULT err=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
297 WARN("IDsDriverBuffer_GetPosition failed\n");
303 mtime.wType = TIME_BYTES;
304 waveOutGetPosition(This->hwo, &mtime, sizeof(mtime));
305 mtime.u.cb = mtime.u.cb % This->buflen;
306 *playpos = mtime.u.cb;
309 /* the writepos should only be used by apps with WRITEPRIMARY priority,
310 * in which case our software mixer is disabled anyway */
311 *writepos = (This->pwplay + ds_hel_margin) * This->fraglen;
312 while (*writepos >= This->buflen)
313 *writepos -= This->buflen;
316 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
320 /*******************************************************************************
323 /* This sets this format for the <em>Primary Buffer Only</em> */
324 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
325 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
326 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
328 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
329 IDirectSoundImpl* dsound = This->dsound;
331 int i, alloc_size, cp_size;
332 DWORD nSamplesPerSec;
333 TRACE("(%p,%p)\n",This,wfex);
335 if (This->dsound->priolevel == DSSCL_NORMAL) {
336 WARN("failed priority check!\n");
337 return DSERR_PRIOLEVELNEEDED;
340 /* Let's be pedantic! */
342 WARN("invalid parameter: wfex==NULL!\n");
343 return DSERR_INVALIDPARAM;
345 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
346 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
347 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
348 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
349 wfex->wBitsPerSample, wfex->cbSize);
352 RtlAcquireResourceExclusive(&(dsound->buffer_list_lock), TRUE);
353 EnterCriticalSection(&(dsound->mixlock));
355 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
356 alloc_size = sizeof(WAVEFORMATEX);
357 cp_size = sizeof(PCMWAVEFORMAT);
359 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
361 dsound->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dsound->pwfx,alloc_size);
363 nSamplesPerSec = dsound->pwfx->nSamplesPerSec;
365 CopyMemory(dsound->pwfx, wfex, cp_size);
367 if (dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
368 DWORD flags = CALLBACK_FUNCTION;
369 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
370 flags |= WAVE_DIRECTSOUND;
371 /* FIXME: check for errors */
372 DSOUND_PrimaryClose(dsound);
373 waveOutClose(dsound->hwo);
375 err = mmErr(waveOutOpen(&(dsound->hwo), dsound->drvdesc.dnDevNode,
376 dsound->pwfx, (DWORD)DSOUND_callback, (DWORD)dsound,
379 err = DSOUND_PrimaryOpen(dsound);
381 WARN("DSOUND_PrimaryOpen failed\n");
385 WARN("waveOutOpen failed\n");
388 } else if (dsound->hwbuf) {
389 err = IDsDriverBuffer_SetFormat(dsound->hwbuf, dsound->pwfx);
390 if (err == DSERR_BUFFERLOST) {
391 /* Wine-only: the driver wants us to recreate the HW buffer */
392 IDsDriverBuffer_Release(dsound->hwbuf);
393 err = IDsDriver_CreateSoundBuffer(dsound->driver,dsound->pwfx,
394 DSBCAPS_PRIMARYBUFFER,0,
395 &(dsound->buflen),&(dsound->buffer),
396 (LPVOID)&(dsound->hwbuf));
398 WARN("IDsDriver_CreateSoundBuffer failed\n");
401 if (dsound->state == STATE_PLAYING) dsound->state = STATE_STARTING;
402 else if (dsound->state == STATE_STOPPING) dsound->state = STATE_STOPPED;
404 WARN("IDsDriverBuffer_SetFormat failed\n");
407 /* FIXME: should we set err back to DS_OK in all cases ? */
409 DSOUND_RecalcPrimary(dsound);
411 if (nSamplesPerSec != dsound->pwfx->nSamplesPerSec) {
412 IDirectSoundBufferImpl** dsb = dsound->buffers;
413 for (i = 0; i < dsound->nrofbuffers; i++, dsb++) {
415 EnterCriticalSection(&((*dsb)->lock));
417 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
418 wfex->nSamplesPerSec;
420 LeaveCriticalSection(&((*dsb)->lock));
426 LeaveCriticalSection(&(dsound->mixlock));
427 RtlReleaseResource(&(dsound->buffer_list_lock));
433 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
434 LPDIRECTSOUNDBUFFER8 iface,LONG vol
436 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
437 IDirectSoundImpl* dsound = This->dsound;
440 HRESULT hres = DS_OK;
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 waveOutGetVolume(dsound->hwo, &factors);
458 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
459 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
460 DSOUND_AmpFactorToVolPan(&volpan);
461 if (vol != volpan.lVolume) {
463 DSOUND_RecalcVolPan(&volpan);
465 hres = IDsDriverBuffer_SetVolumePan(dsound->hwbuf, &volpan);
467 WARN("IDsDriverBuffer_SetVolumePan failed\n");
469 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
470 waveOutSetVolume(dsound->hwo, ampfactors);
474 LeaveCriticalSection(&(dsound->mixlock));
480 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
481 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
483 PrimaryBufferImpl *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 waveOutGetVolume(This->dsound->hwo, &factors);
499 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
500 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
501 DSOUND_AmpFactorToVolPan(&volpan);
502 *vol = volpan.lVolume;
506 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
507 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
509 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
511 TRACE("(%p,%ld)\n",This,freq);
513 /* You cannot set the frequency of the primary buffer */
514 WARN("control unavailable\n");
515 return DSERR_CONTROLUNAVAIL;
518 static HRESULT WINAPI PrimaryBufferImpl_Play(
519 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
521 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
522 IDirectSoundImpl* dsound = This->dsound;
524 TRACE("(%p,%08lx,%08lx,%08lx)\n",
525 This,reserved1,reserved2,flags
528 if (!(flags & DSBPLAY_LOOPING)) {
529 WARN("invalid parameter: flags = %08lx\n", flags);
530 return DSERR_INVALIDPARAM;
534 EnterCriticalSection(&(dsound->mixlock));
536 if (dsound->state == STATE_STOPPED)
537 dsound->state = STATE_STARTING;
538 else if (dsound->state == STATE_STOPPING)
539 dsound->state = STATE_PLAYING;
541 LeaveCriticalSection(&(dsound->mixlock));
547 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
549 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
550 IDirectSoundImpl* dsound = This->dsound;
552 TRACE("(%p)\n",This);
555 EnterCriticalSection(&(dsound->mixlock));
557 if (dsound->state == STATE_PLAYING)
558 dsound->state = STATE_STOPPING;
559 else if (dsound->state == STATE_STARTING)
560 dsound->state = STATE_STOPPED;
562 LeaveCriticalSection(&(dsound->mixlock));
568 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
570 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
571 ULONG ref = InterlockedIncrement(&(This->ref));
572 TRACE("(%p) ref was %ld\n", This, ref - 1);
576 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
578 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
579 DWORD ref = InterlockedDecrement(&(This->ref));
580 TRACE("(%p) ref was %ld\n", This, ref + 1);
583 This->dsound->primary = NULL;
584 HeapFree(GetProcessHeap(), 0, This);
585 TRACE("(%p) released\n", This);
590 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
591 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
594 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
595 IDirectSoundImpl* dsound = This->dsound;
597 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
598 hres = DSOUND_PrimaryGetPosition(dsound, playpos, writepos);
600 WARN("DSOUND_PrimaryGetPosition failed\n");
604 if (dsound->state != STATE_STOPPED)
605 /* apply the documented 10ms lead to writepos */
606 *writepos += dsound->writelead;
607 while (*writepos >= dsound->buflen) *writepos -= dsound->buflen;
609 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
613 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
614 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
616 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
617 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
619 if (status == NULL) {
620 WARN("invalid parameter: status == NULL\n");
621 return DSERR_INVALIDPARAM;
625 if ((This->dsound->state == STATE_STARTING) ||
626 (This->dsound->state == STATE_PLAYING))
627 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
629 TRACE("status=%lx\n", *status);
634 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
635 LPDIRECTSOUNDBUFFER8 iface,
641 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
642 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
644 size = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
646 if (lpwf) { /* NULL is valid */
647 if (wfsize >= size) {
648 CopyMemory(lpwf,This->dsound->pwfx,size);
652 WARN("invalid parameter: wfsize too small\n");
655 return DSERR_INVALIDPARAM;
659 *wfwritten = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
661 WARN("invalid parameter: wfwritten == NULL\n");
662 return DSERR_INVALIDPARAM;
669 static HRESULT WINAPI PrimaryBufferImpl_Lock(
670 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
672 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
673 IDirectSoundImpl* dsound = This->dsound;
675 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
687 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
688 WARN("failed priority check!\n");
689 return DSERR_PRIOLEVELNEEDED;
692 if (flags & DSBLOCK_FROMWRITECURSOR) {
695 /* GetCurrentPosition does too much magic to duplicate here */
696 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writepos);
698 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
701 writecursor += writepos;
703 while (writecursor >= dsound->buflen)
704 writecursor -= dsound->buflen;
705 if (flags & DSBLOCK_ENTIREBUFFER)
706 writebytes = dsound->buflen;
707 if (writebytes > dsound->buflen)
708 writebytes = dsound->buflen;
710 if (!(dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && dsound->hwbuf) {
712 hres = IDsDriverBuffer_Lock(dsound->hwbuf,
713 lplpaudioptr1, audiobytes1,
714 lplpaudioptr2, audiobytes2,
715 writecursor, writebytes,
718 WARN("IDsDriverBuffer_Lock failed\n");
722 if (writecursor+writebytes <= dsound->buflen) {
723 *(LPBYTE*)lplpaudioptr1 = dsound->buffer+writecursor;
724 *audiobytes1 = writebytes;
726 *(LPBYTE*)lplpaudioptr2 = NULL;
729 TRACE("->%ld.0\n",writebytes);
731 *(LPBYTE*)lplpaudioptr1 = dsound->buffer+writecursor;
732 *audiobytes1 = dsound->buflen-writecursor;
734 *(LPBYTE*)lplpaudioptr2 = dsound->buffer;
736 *audiobytes2 = writebytes-(dsound->buflen-writecursor);
737 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
743 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
744 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
746 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
747 TRACE("(%p,%ld)\n",This,newpos);
749 /* You cannot set the position of the primary buffer */
750 WARN("invalid call\n");
751 return DSERR_INVALIDCALL;
754 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
755 LPDIRECTSOUNDBUFFER8 iface,LONG pan
757 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
758 IDirectSoundImpl* dsound = This->dsound;
761 HRESULT hres = DS_OK;
763 TRACE("(%p,%ld)\n",This,pan);
765 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
766 WARN("control unavailable\n");
767 return DSERR_CONTROLUNAVAIL;
770 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
771 WARN("invalid parameter: pan = %ld\n", pan);
772 return DSERR_INVALIDPARAM;
776 EnterCriticalSection(&(dsound->mixlock));
778 waveOutGetVolume(dsound->hwo, &factors);
779 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
780 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
781 DSOUND_AmpFactorToVolPan(&volpan);
782 if (pan != volpan.lPan) {
784 DSOUND_RecalcVolPan(&volpan);
786 hres = IDsDriverBuffer_SetVolumePan(dsound->hwbuf, &volpan);
788 WARN("IDsDriverBuffer_SetVolumePan failed\n");
790 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
791 waveOutSetVolume(dsound->hwo, ampfactors);
795 LeaveCriticalSection(&(dsound->mixlock));
801 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
802 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
804 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
807 TRACE("(%p,%p)\n",This,pan);
809 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
810 WARN("control unavailable\n");
811 return DSERR_CONTROLUNAVAIL;
815 WARN("invalid parameter: pan == NULL\n");
816 return DSERR_INVALIDPARAM;
819 waveOutGetVolume(This->dsound->hwo, &factors);
820 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
821 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
822 DSOUND_AmpFactorToVolPan(&volpan);
827 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
828 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
830 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
831 IDirectSoundImpl* dsound = This->dsound;
833 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
835 if (dsound->priolevel != DSSCL_WRITEPRIMARY) {
836 WARN("failed priority check!\n");
837 return DSERR_PRIOLEVELNEEDED;
840 if (!(dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && dsound->hwbuf) {
843 hres = IDsDriverBuffer_Unlock(dsound->hwbuf, p1, x1, p2, x2);
845 WARN("IDsDriverBuffer_Unlock failed\n");
853 static HRESULT WINAPI PrimaryBufferImpl_Restore(
854 LPDIRECTSOUNDBUFFER8 iface
856 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
857 FIXME("(%p):stub\n",This);
861 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
862 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
864 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
865 TRACE("(%p,%p)\n",This,freq);
868 WARN("invalid parameter: freq == NULL\n");
869 return DSERR_INVALIDPARAM;
872 if (!(This->dsound->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
873 WARN("control unavailable\n");
874 return DSERR_CONTROLUNAVAIL;
877 *freq = This->dsound->pwfx->nSamplesPerSec;
878 TRACE("-> %ld\n", *freq);
883 static HRESULT WINAPI PrimaryBufferImpl_SetFX(
884 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
886 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
889 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
892 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
894 WARN("control unavailable\n");
895 return DSERR_CONTROLUNAVAIL;
898 static HRESULT WINAPI PrimaryBufferImpl_AcquireResources(
899 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
901 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
904 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
907 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
909 WARN("control unavailable\n");
910 return DSERR_CONTROLUNAVAIL;
913 static HRESULT WINAPI PrimaryBufferImpl_GetObjectInPath(
914 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
916 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
918 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
920 WARN("control unavailable\n");
921 return DSERR_CONTROLUNAVAIL;
924 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
925 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
927 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
928 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
929 DPRINTF("Re-Init!!!\n");
930 WARN("already initialized\n");
931 return DSERR_ALREADYINITIALIZED;
934 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
935 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
937 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
938 TRACE("(%p)->(%p)\n",This,caps);
941 WARN("invalid parameter: caps == NULL\n");
942 return DSERR_INVALIDPARAM;
945 if (caps->dwSize < sizeof(*caps)) {
946 WARN("invalid parameter: caps->dwSize = %ld: < %d\n", caps->dwSize, sizeof(*caps));
947 return DSERR_INVALIDPARAM;
950 caps->dwFlags = This->dsound->dsbd.dwFlags;
951 if (This->dsound->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
952 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
954 caps->dwBufferBytes = This->dsound->buflen;
956 /* This value represents the speed of the "unlock" command.
957 As unlock is quite fast (it does not do anything), I put
958 4096 ko/s = 4 Mo / s */
959 /* FIXME: hwbuf speed */
960 caps->dwUnlockTransferRate = 4096;
961 caps->dwPlayCpuOverhead = 0;
966 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
967 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
969 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
970 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
973 WARN("invalid parameter\n");
977 *ppobj = NULL; /* assume failure */
979 if ( IsEqualGUID(riid, &IID_IUnknown) ||
980 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
981 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
986 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
987 /* a primary buffer can't have a DirectSoundBuffer8 interface */
988 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
989 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
990 return E_NOINTERFACE;
993 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
994 ERR("app requested IDirectSoundNotify on primary buffer\n");
995 /* FIXME: should we support this? */
996 return E_NOINTERFACE;
999 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1000 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1001 return E_NOINTERFACE;
1004 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1005 if (!This->dsound->listener)
1006 IDirectSound3DListenerImpl_Create(This, &This->dsound->listener);
1007 if (This->dsound->listener) {
1008 *ppobj = This->dsound->listener;
1009 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1013 WARN("IID_IDirectSound3DListener failed\n");
1014 return E_NOINTERFACE;
1017 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1018 FIXME("app requested IKsPropertySet on primary buffer\n");
1019 return E_NOINTERFACE;
1022 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1023 return E_NOINTERFACE;
1026 static IDirectSoundBuffer8Vtbl dspbvt =
1028 PrimaryBufferImpl_QueryInterface,
1029 PrimaryBufferImpl_AddRef,
1030 PrimaryBufferImpl_Release,
1031 PrimaryBufferImpl_GetCaps,
1032 PrimaryBufferImpl_GetCurrentPosition,
1033 PrimaryBufferImpl_GetFormat,
1034 PrimaryBufferImpl_GetVolume,
1035 PrimaryBufferImpl_GetPan,
1036 PrimaryBufferImpl_GetFrequency,
1037 PrimaryBufferImpl_GetStatus,
1038 PrimaryBufferImpl_Initialize,
1039 PrimaryBufferImpl_Lock,
1040 PrimaryBufferImpl_Play,
1041 PrimaryBufferImpl_SetCurrentPosition,
1042 PrimaryBufferImpl_SetFormat,
1043 PrimaryBufferImpl_SetVolume,
1044 PrimaryBufferImpl_SetPan,
1045 PrimaryBufferImpl_SetFrequency,
1046 PrimaryBufferImpl_Stop,
1047 PrimaryBufferImpl_Unlock,
1048 PrimaryBufferImpl_Restore,
1049 PrimaryBufferImpl_SetFX,
1050 PrimaryBufferImpl_AcquireResources,
1051 PrimaryBufferImpl_GetObjectInPath
1054 HRESULT WINAPI PrimaryBufferImpl_Create(
1055 IDirectSoundImpl *ds,
1056 PrimaryBufferImpl **pdsb,
1057 LPCDSBUFFERDESC dsbd)
1059 PrimaryBufferImpl *dsb;
1061 TRACE("%p,%p,%p)\n",ds,pdsb,dsbd);
1063 if (dsbd->lpwfxFormat) {
1064 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1066 return DSERR_INVALIDPARAM;
1069 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1072 WARN("out of memory\n");
1074 return DSERR_OUTOFMEMORY;
1079 dsb->lpVtbl = &dspbvt;
1081 CopyMemory(&ds->dsbd, dsbd, sizeof(*dsbd));
1083 TRACE("Created primary buffer at %p\n", dsb);
1084 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1085 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1086 ds->pwfx->wFormatTag, ds->pwfx->nChannels, ds->pwfx->nSamplesPerSec,
1087 ds->pwfx->nAvgBytesPerSec, ds->pwfx->nBlockAlign,
1088 ds->pwfx->wBitsPerSample, ds->pwfx->cbSize);