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 roughly 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 if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
68 device->writelead = 0;
70 /* calculate the 10ms write lead */
71 device->writelead = (device->pwfx->nSamplesPerSec / 100) * nBlockAlign;
74 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
77 TRACE("(%p)\n", device);
79 /* are we using waveOut stuff? */
80 if (!device->driver) {
84 /* Start in pause mode, to allow buffers to get filled */
85 waveOutPause(device->hwo);
86 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
87 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
89 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
90 on windows this size is always fixed (tested on win-xp) */
91 buflen = DS_HEL_BUFLEN;
93 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
95 /* reallocate emulated primary buffer */
97 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
99 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
101 if (newbuf == NULL) {
102 ERR("failed to allocate primary buffer\n");
103 merr = DSERR_OUTOFMEMORY;
104 /* but the old buffer might still exist and must be re-prepared */
108 device->buffer = newbuf;
109 device->buflen = buflen;
111 if (device->buffer) {
114 device->fraglen = device->buflen / DS_HEL_FRAGS;
117 if(device->buflen % DS_HEL_FRAGS){
118 ERR("Bad DS_HEL_FRAGS resolution\n");
121 /* prepare fragment headers */
122 for (c=0; c<DS_HEL_FRAGS; c++) {
123 device->pwave[c]->lpData = (char*)device->buffer + c*device->fraglen;
124 device->pwave[c]->dwBufferLength = device->fraglen;
125 device->pwave[c]->dwUser = (DWORD)device;
126 device->pwave[c]->dwFlags = 0;
127 device->pwave[c]->dwLoops = 0;
128 err = mmErr(waveOutPrepareHeader(device->hwo,device->pwave[c],sizeof(WAVEHDR)));
131 waveOutUnprepareHeader(device->hwo,device->pwave[c],sizeof(WAVEHDR));
141 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
142 TRACE("fraglen=%d\n", device->fraglen);
144 if ((err == DS_OK) && (merr != DS_OK))
146 } else if (!device->hwbuf) {
149 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
150 DSBCAPS_PRIMARYBUFFER,0,
151 &(device->buflen),&(device->buffer),
152 (LPVOID*)&(device->hwbuf));
154 WARN("IDsDriver_CreateSoundBuffer failed\n");
158 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
159 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
162 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
169 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
171 TRACE("(%p)\n", device);
173 /* are we using waveOut stuff? */
174 if (!device->hwbuf) {
177 /* get out of CS when calling the wave system */
178 LeaveCriticalSection(&(device->mixlock));
180 device->pwqueue = (DWORD)-1; /* resetting queues */
181 waveOutReset(device->hwo);
182 for (c=0; c<DS_HEL_FRAGS; c++)
183 waveOutUnprepareHeader(device->hwo, device->pwave[c], sizeof(WAVEHDR));
185 EnterCriticalSection(&(device->mixlock));
187 /* clear the queue */
190 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
195 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
198 TRACE("(%p)\n", device);
202 device->buflen = device->pwfx->nAvgBytesPerSec;
204 /* FIXME: verify that hardware capabilities (DSCAPS_PRIMARY flags) match */
206 if (device->driver) {
207 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
208 DSBCAPS_PRIMARYBUFFER,0,
209 &(device->buflen),&(device->buffer),
210 (LPVOID*)&(device->hwbuf));
212 WARN("IDsDriver_CreateSoundBuffer failed\n");
216 if (!device->hwbuf) {
217 /* Allocate memory for HEL buffer headers */
219 for (c=0; c<DS_HEL_FRAGS; c++) {
220 device->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
221 if (!device->pwave[c]) {
222 /* Argh, out of memory */
224 HeapFree(GetProcessHeap(),0,device->pwave[c]);
226 WARN("out of memory\n");
227 return DSERR_OUTOFMEMORY;
232 err = DSOUND_PrimaryOpen(device);
235 WARN("DSOUND_PrimaryOpen failed\n");
239 /* calculate fragment size and write lead */
240 DSOUND_RecalcPrimary(device);
241 device->state = STATE_STOPPED;
245 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
247 TRACE("(%p)\n", device);
250 EnterCriticalSection(&(device->mixlock));
252 DSOUND_PrimaryClose(device);
253 if (device->driver) {
255 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
260 for (c=0; c<DS_HEL_FRAGS; c++) {
261 HeapFree(GetProcessHeap(),0,device->pwave[c]);
264 HeapFree(GetProcessHeap(),0,device->pwfx);
267 LeaveCriticalSection(&(device->mixlock));
273 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
276 TRACE("(%p)\n", device);
279 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
281 WARN("IDsDriverBuffer_Play failed\n");
283 err = mmErr(waveOutRestart(device->hwo));
285 WARN("waveOutRestart failed\n");
291 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
294 TRACE("(%p)\n", device);
297 err = IDsDriverBuffer_Stop(device->hwbuf);
298 if (err == DSERR_BUFFERLOST) {
299 DWORD flags = CALLBACK_FUNCTION;
300 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
301 flags |= WAVE_DIRECTSOUND;
302 /* Wine-only: the driver wants us to reopen the device */
303 /* FIXME: check for errors */
304 IDsDriverBuffer_Release(device->hwbuf);
305 waveOutClose(device->hwo);
307 err = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode,
308 device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device,
313 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
314 DSBCAPS_PRIMARYBUFFER,0,
315 &(device->buflen),&(device->buffer),
316 (LPVOID)&(device->hwbuf));
318 WARN("IDsDriver_CreateSoundBuffer failed\n");
320 WARN("waveOutOpen failed\n");
322 } else if (err != DS_OK) {
323 WARN("IDsDriverBuffer_Stop failed\n");
327 /* don't call the wave system with the lock set */
328 LeaveCriticalSection(&(device->mixlock));
331 err = mmErr(waveOutPause(device->hwo));
334 EnterCriticalSection(&(device->mixlock));
337 WARN("waveOutPause failed\n");
343 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
345 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
348 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
350 WARN("IDsDriverBuffer_GetPosition failed\n");
355 /* check if playpos was requested */
357 /* use the cached play position */
358 *playpos = device->pwplay * device->fraglen;
361 /* check if writepos was requested */
363 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
365 /* the writepos is the first non-queued position */
366 *writepos = (device->pwplay + device->pwqueue) * device->fraglen;
367 *writepos %= device->buflen;
370 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
374 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex)
377 int i, alloc_size, cp_size;
378 DWORD nSamplesPerSec;
379 TRACE("(%p,%p)\n", device, wfex);
381 if (device->priolevel == DSSCL_NORMAL) {
382 WARN("failed priority check!\n");
383 return DSERR_PRIOLEVELNEEDED;
386 /* Let's be pedantic! */
388 WARN("invalid parameter: wfex==NULL!\n");
389 return DSERR_INVALIDPARAM;
391 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
392 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
393 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
394 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
395 wfex->wBitsPerSample, wfex->cbSize);
398 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
399 EnterCriticalSection(&(device->mixlock));
401 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
402 alloc_size = sizeof(WAVEFORMATEX);
403 cp_size = sizeof(PCMWAVEFORMAT);
405 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
407 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
409 nSamplesPerSec = device->pwfx->nSamplesPerSec;
411 CopyMemory(device->pwfx, wfex, cp_size);
413 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) {
414 DWORD flags = CALLBACK_FUNCTION;
415 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
416 flags |= WAVE_DIRECTSOUND;
417 /* FIXME: check for errors */
418 DSOUND_PrimaryClose(device);
419 waveOutClose(device->hwo);
421 err = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode,
422 device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device,
425 err = DSOUND_PrimaryOpen(device);
427 WARN("DSOUND_PrimaryOpen failed\n");
431 WARN("waveOutOpen failed\n");
434 } else if (device->hwbuf) {
435 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
436 if (err == DSERR_BUFFERLOST) {
437 /* Wine-only: the driver wants us to recreate the HW buffer */
438 IDsDriverBuffer_Release(device->hwbuf);
441 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
442 DSBCAPS_PRIMARYBUFFER,0,
443 &(device->buflen),&(device->buffer),
444 (LPVOID)&(device->hwbuf));
446 WARN("IDsDriver_CreateSoundBuffer failed\n");
449 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
450 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
451 } else if (FAILED(err)) {
452 WARN("IDsDriverBuffer_SetFormat failed\n");
455 /* FIXME: should we set err back to DS_OK in all cases ? */
457 DSOUND_RecalcPrimary(device);
459 if (nSamplesPerSec != device->pwfx->nSamplesPerSec) {
460 IDirectSoundBufferImpl** dsb = device->buffers;
461 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
463 EnterCriticalSection(&((*dsb)->lock));
465 (*dsb)->freqAdjust = ((*dsb)->freq << DSOUND_FREQSHIFT) /
466 wfex->nSamplesPerSec;
468 LeaveCriticalSection(&((*dsb)->lock));
474 LeaveCriticalSection(&(device->mixlock));
475 RtlReleaseResource(&(device->buffer_list_lock));
481 /*******************************************************************************
484 /* This sets this format for the <em>Primary Buffer Only</em> */
485 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
486 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
487 LPDIRECTSOUNDBUFFER iface,
488 LPCWAVEFORMATEX wfex)
490 TRACE("(%p,%p)\n", iface, wfex);
491 return DSOUND_PrimarySetFormat(((PrimaryBufferImpl *)iface)->device, wfex);
494 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
495 LPDIRECTSOUNDBUFFER iface,LONG vol
497 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
500 HRESULT hres = DS_OK;
501 TRACE("(%p,%d)\n", iface, vol);
503 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
504 WARN("control unavailable\n");
505 return DSERR_CONTROLUNAVAIL;
508 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
509 WARN("invalid parameter: vol = %d\n", vol);
510 return DSERR_INVALIDPARAM;
514 EnterCriticalSection(&(device->mixlock));
516 waveOutGetVolume(device->hwo, &factors);
517 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
518 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
519 DSOUND_AmpFactorToVolPan(&volpan);
520 if (vol != volpan.lVolume) {
522 DSOUND_RecalcVolPan(&volpan);
524 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
526 WARN("IDsDriverBuffer_SetVolumePan failed\n");
528 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
529 waveOutSetVolume(device->hwo, ampfactors);
533 LeaveCriticalSection(&(device->mixlock));
539 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
540 LPDIRECTSOUNDBUFFER iface,LPLONG vol
542 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
545 TRACE("(%p,%p)\n", iface, vol);
547 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
548 WARN("control unavailable\n");
549 return DSERR_CONTROLUNAVAIL;
553 WARN("invalid parameter: vol = NULL\n");
554 return DSERR_INVALIDPARAM;
557 waveOutGetVolume(device->hwo, &factors);
558 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
559 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
560 DSOUND_AmpFactorToVolPan(&volpan);
561 *vol = volpan.lVolume;
565 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
566 LPDIRECTSOUNDBUFFER iface,DWORD freq
568 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
569 TRACE("(%p,%d)\n",This,freq);
571 /* You cannot set the frequency of the primary buffer */
572 WARN("control unavailable\n");
573 return DSERR_CONTROLUNAVAIL;
576 static HRESULT WINAPI PrimaryBufferImpl_Play(
577 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
579 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
580 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
582 if (!(flags & DSBPLAY_LOOPING)) {
583 WARN("invalid parameter: flags = %08x\n", flags);
584 return DSERR_INVALIDPARAM;
588 EnterCriticalSection(&(device->mixlock));
590 if (device->state == STATE_STOPPED)
591 device->state = STATE_STARTING;
592 else if (device->state == STATE_STOPPING)
593 device->state = STATE_PLAYING;
595 LeaveCriticalSection(&(device->mixlock));
601 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
603 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
604 TRACE("(%p)\n", iface);
607 EnterCriticalSection(&(device->mixlock));
609 if (device->state == STATE_PLAYING)
610 device->state = STATE_STOPPING;
611 else if (device->state == STATE_STARTING)
612 device->state = STATE_STOPPED;
614 LeaveCriticalSection(&(device->mixlock));
620 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
622 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
623 ULONG ref = InterlockedIncrement(&(This->ref));
624 TRACE("(%p) ref was %d\n", This, ref - 1);
628 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
630 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
631 DWORD ref = InterlockedDecrement(&(This->ref));
632 TRACE("(%p) ref was %d\n", This, ref + 1);
635 This->device->primary = NULL;
636 HeapFree(GetProcessHeap(), 0, This);
637 TRACE("(%p) released\n", This);
642 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
643 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
646 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
647 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
650 EnterCriticalSection(&(device->mixlock));
652 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
654 WARN("DSOUND_PrimaryGetPosition failed\n");
655 LeaveCriticalSection(&(device->mixlock));
659 if (device->state != STATE_STOPPED)
660 /* apply the documented 10ms lead to writepos */
661 *writepos += device->writelead;
662 while (*writepos >= device->buflen) *writepos -= device->buflen;
665 LeaveCriticalSection(&(device->mixlock));
668 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
672 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
673 LPDIRECTSOUNDBUFFER iface,LPDWORD status
675 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
676 TRACE("(%p,%p)\n", iface, status);
678 if (status == NULL) {
679 WARN("invalid parameter: status == NULL\n");
680 return DSERR_INVALIDPARAM;
684 if ((device->state == STATE_STARTING) ||
685 (device->state == STATE_PLAYING))
686 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
688 TRACE("status=%x\n", *status);
693 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
694 LPDIRECTSOUNDBUFFER iface,
700 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
701 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
703 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
705 if (lpwf) { /* NULL is valid */
706 if (wfsize >= size) {
707 CopyMemory(lpwf,device->pwfx,size);
711 WARN("invalid parameter: wfsize too small\n");
714 return DSERR_INVALIDPARAM;
718 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
720 WARN("invalid parameter: wfwritten == NULL\n");
721 return DSERR_INVALIDPARAM;
728 static HRESULT WINAPI PrimaryBufferImpl_Lock(
729 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
732 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
733 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
745 if (device->priolevel != DSSCL_WRITEPRIMARY) {
746 WARN("failed priority check!\n");
747 return DSERR_PRIOLEVELNEEDED;
750 /* when this flag is set, writecursor is meaningless and must be calculated */
751 if (flags & DSBLOCK_FROMWRITECURSOR) {
752 /* GetCurrentPosition does too much magic to duplicate here */
753 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
755 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
760 /* when this flag is set, writebytes is meaningless and must be set */
761 if (flags & DSBLOCK_ENTIREBUFFER)
762 writebytes = device->buflen;
764 if (writecursor >= device->buflen) {
765 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
766 writecursor, device->buflen);
767 return DSERR_INVALIDPARAM;
770 if (writebytes > device->buflen) {
771 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
772 writebytes, device->buflen);
773 return DSERR_INVALIDPARAM;
776 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
777 hres = IDsDriverBuffer_Lock(device->hwbuf,
778 lplpaudioptr1, audiobytes1,
779 lplpaudioptr2, audiobytes2,
780 writecursor, writebytes,
783 WARN("IDsDriverBuffer_Lock failed\n");
787 if (writecursor+writebytes <= device->buflen) {
788 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
789 *audiobytes1 = writebytes;
791 *(LPBYTE*)lplpaudioptr2 = NULL;
794 TRACE("->%d.0\n",writebytes);
796 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
797 *audiobytes1 = device->buflen-writecursor;
799 *(LPBYTE*)lplpaudioptr2 = device->buffer;
801 *audiobytes2 = writebytes-(device->buflen-writecursor);
802 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
808 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
809 LPDIRECTSOUNDBUFFER iface,DWORD newpos
811 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
812 TRACE("(%p,%d)\n",This,newpos);
814 /* You cannot set the position of the primary buffer */
815 WARN("invalid call\n");
816 return DSERR_INVALIDCALL;
819 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
820 LPDIRECTSOUNDBUFFER iface,LONG pan
822 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
825 HRESULT hres = DS_OK;
826 TRACE("(%p,%d)\n", iface, pan);
828 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
829 WARN("control unavailable\n");
830 return DSERR_CONTROLUNAVAIL;
833 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
834 WARN("invalid parameter: pan = %d\n", pan);
835 return DSERR_INVALIDPARAM;
839 EnterCriticalSection(&(device->mixlock));
841 waveOutGetVolume(device->hwo, &factors);
842 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
843 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
844 DSOUND_AmpFactorToVolPan(&volpan);
845 if (pan != volpan.lPan) {
847 DSOUND_RecalcVolPan(&volpan);
849 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
851 WARN("IDsDriverBuffer_SetVolumePan failed\n");
853 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
854 waveOutSetVolume(device->hwo, ampfactors);
858 LeaveCriticalSection(&(device->mixlock));
864 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
865 LPDIRECTSOUNDBUFFER iface,LPLONG pan
867 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
870 TRACE("(%p,%p)\n", iface, pan);
872 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
873 WARN("control unavailable\n");
874 return DSERR_CONTROLUNAVAIL;
878 WARN("invalid parameter: pan == NULL\n");
879 return DSERR_INVALIDPARAM;
882 waveOutGetVolume(device->hwo, &factors);
883 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
884 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
885 DSOUND_AmpFactorToVolPan(&volpan);
890 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
891 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
893 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
894 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
896 if (device->priolevel != DSSCL_WRITEPRIMARY) {
897 WARN("failed priority check!\n");
898 return DSERR_PRIOLEVELNEEDED;
901 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
904 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
906 WARN("IDsDriverBuffer_Unlock failed\n");
914 static HRESULT WINAPI PrimaryBufferImpl_Restore(
915 LPDIRECTSOUNDBUFFER iface
917 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
918 FIXME("(%p):stub\n",This);
922 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
923 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
925 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
926 TRACE("(%p,%p)\n", iface, freq);
929 WARN("invalid parameter: freq == NULL\n");
930 return DSERR_INVALIDPARAM;
933 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
934 WARN("control unavailable\n");
935 return DSERR_CONTROLUNAVAIL;
938 *freq = device->pwfx->nSamplesPerSec;
939 TRACE("-> %d\n", *freq);
944 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
945 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
947 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
948 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
949 DPRINTF("Re-Init!!!\n");
950 WARN("already initialized\n");
951 return DSERR_ALREADYINITIALIZED;
954 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
955 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
957 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
958 TRACE("(%p,%p)\n", iface, caps);
961 WARN("invalid parameter: caps == NULL\n");
962 return DSERR_INVALIDPARAM;
965 if (caps->dwSize < sizeof(*caps)) {
966 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
967 return DSERR_INVALIDPARAM;
970 caps->dwFlags = device->dsbd.dwFlags;
971 if (device->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
972 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
974 caps->dwBufferBytes = device->buflen;
976 /* Windows reports these as zero */
977 caps->dwUnlockTransferRate = 0;
978 caps->dwPlayCpuOverhead = 0;
983 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
984 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
986 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
987 DirectSoundDevice *device = This->device;
988 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
991 WARN("invalid parameter\n");
995 *ppobj = NULL; /* assume failure */
997 if ( IsEqualGUID(riid, &IID_IUnknown) ||
998 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
999 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1004 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1005 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1006 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1007 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1008 return E_NOINTERFACE;
1011 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1012 ERR("app requested IDirectSoundNotify on primary buffer\n");
1013 /* FIXME: should we support this? */
1014 return E_NOINTERFACE;
1017 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1018 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1019 return E_NOINTERFACE;
1022 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1023 if (!device->listener)
1024 IDirectSound3DListenerImpl_Create(device, &device->listener);
1025 if (device->listener) {
1026 *ppobj = device->listener;
1027 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1031 WARN("IID_IDirectSound3DListener failed\n");
1032 return E_NOINTERFACE;
1035 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1036 FIXME("app requested IKsPropertySet on primary buffer\n");
1037 return E_NOINTERFACE;
1040 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1041 return E_NOINTERFACE;
1044 static const IDirectSoundBufferVtbl dspbvt =
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
1069 HRESULT PrimaryBufferImpl_Create(
1070 DirectSoundDevice * device,
1071 PrimaryBufferImpl ** ppdsb,
1072 LPCDSBUFFERDESC dsbd)
1074 PrimaryBufferImpl *dsb;
1075 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1077 if (dsbd->lpwfxFormat) {
1078 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1080 return DSERR_INVALIDPARAM;
1083 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1086 WARN("out of memory\n");
1088 return DSERR_OUTOFMEMORY;
1092 dsb->device = device;
1093 dsb->lpVtbl = &dspbvt;
1095 CopyMemory(&device->dsbd, dsbd, sizeof(*dsbd));
1097 TRACE("Created primary buffer at %p\n", dsb);
1098 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1099 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1100 device->pwfx->wFormatTag, device->pwfx->nChannels,
1101 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1102 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1103 device->pwfx->cbSize);