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 /** Calculate how long a fragment length of about 10 ms should be in frames
41 * nSamplesPerSec: Frequency rate in samples per second
42 * nBlockAlign: Size of a single blockalign
45 * Size in bytes of a single fragment
47 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
49 DWORD fraglen = 256 * nBlockAlign;
51 /* Compensate for only being roughly accurate */
52 if (nSamplesPerSec <= 26000)
55 if (nSamplesPerSec <= 10000)
58 if (nSamplesPerSec >= 80000)
64 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
66 TRACE("(%p)\n", device);
68 device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
69 device->helfrags = device->buflen / device->fraglen;
70 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
72 if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
73 device->writelead = 0;
75 /* calculate the 10ms write lead */
76 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
79 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
82 TRACE("(%p, %d)\n", device, forcewave);
86 IDsDriver_Close(device->driver);
87 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
88 waveOutClose(device->hwo);
89 IDsDriver_Release(device->driver);
90 device->driver = NULL;
91 device->buffer = NULL;
94 else if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
95 waveOutClose(device->hwo);
97 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
98 if (ds_hw_accel != DS_HW_ACCEL_EMULATION && !forcewave)
99 waveOutMessage((HWAVEOUT)device->drvdesc.dnDevNode, DRV_QUERYDSOUNDIFACE, (DWORD_PTR)&device->driver, 0);
101 /* Get driver description */
102 if (device->driver) {
103 DWORD wod = device->drvdesc.dnDevNode;
104 hres = IDsDriver_GetDriverDesc(device->driver,&(device->drvdesc));
105 device->drvdesc.dnDevNode = wod;
107 WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres);
108 IDsDriver_Release(device->driver);
109 device->driver = NULL;
113 /* if no DirectSound interface available, use WINMM API instead */
115 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
117 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
119 DWORD flags = CALLBACK_FUNCTION;
122 flags |= WAVE_DIRECTSOUND;
124 hres = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode, device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device, flags));
126 WARN("waveOutOpen failed\n");
129 IDsDriver_Release(device->driver);
130 device->driver = NULL;
137 hres = IDsDriver_Open(device->driver);
142 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
146 TRACE("(%p)\n", device);
148 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
149 on windows this size is always fixed (tested on win-xp) */
151 device->buflen = ds_hel_buflen;
152 buflen = device->buflen;
153 buflen -= buflen % device->pwfx->nBlockAlign;
154 device->buflen = buflen;
158 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
159 DSBCAPS_PRIMARYBUFFER,0,
160 &(device->buflen),&(device->buffer),
161 (LPVOID*)&(device->hwbuf));
164 WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err);
165 err = DSOUND_ReopenDevice(device, TRUE);
168 WARN("Falling back to waveout failed too! Giving up\n");
172 DSOUND_RecalcPrimary(device);
173 device->prebuf = ds_snd_queue_max;
174 if (device->helfrags < ds_snd_queue_min)
176 WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device->buflen, ds_snd_queue_min * device->fraglen);
177 device->buflen = buflen;
178 IDsDriverBuffer_Release(device->hwbuf);
179 device->hwbuf = NULL;
180 err = DSOUND_ReopenDevice(device, TRUE);
183 WARN("Falling back to waveout failed too! Giving up\n");
187 else if (device->helfrags < ds_snd_queue_max)
188 device->prebuf = device->helfrags;
191 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
192 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
193 if (!device->mix_buffer)
196 IDsDriverBuffer_Release(device->hwbuf);
197 device->hwbuf = NULL;
198 return DSERR_OUTOFMEMORY;
201 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
202 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
204 /* are we using waveOut stuff? */
205 if (!device->driver) {
207 LPWAVEHDR headers = NULL;
211 /* Start in pause mode, to allow buffers to get filled */
212 waveOutPause(device->hwo);
214 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
216 /* reallocate emulated primary buffer */
218 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
220 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
223 ERR("failed to allocate primary buffer\n");
224 return DSERR_OUTOFMEMORY;
225 /* but the old buffer might still exist and must be re-prepared */
228 DSOUND_RecalcPrimary(device);
230 headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
232 headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
235 ERR("failed to allocate wave headers\n");
236 HeapFree(GetProcessHeap(), 0, newbuf);
237 DSOUND_RecalcPrimary(device);
238 return DSERR_OUTOFMEMORY;
241 device->buffer = newbuf;
242 device->pwave = headers;
244 /* prepare fragment headers */
245 for (c=0; c<device->helfrags; c++) {
246 device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
247 device->pwave[c].dwBufferLength = device->fraglen;
248 device->pwave[c].dwUser = (DWORD)device;
249 device->pwave[c].dwFlags = 0;
250 device->pwave[c].dwLoops = 0;
251 err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
254 waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
259 overshot = device->buflen % device->fraglen;
263 overshot -= overshot % device->pwfx->nBlockAlign;
264 device->pwave[device->helfrags - 1].dwBufferLength += overshot;
267 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
269 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
270 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
271 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
272 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
273 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
278 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
280 TRACE("(%p)\n", device);
282 /* are we using waveOut stuff? */
283 if (!device->hwbuf) {
286 /* get out of CS when calling the wave system */
287 LeaveCriticalSection(&(device->mixlock));
289 device->pwqueue = (DWORD)-1; /* resetting queues */
290 waveOutReset(device->hwo);
291 for (c=0; c<device->helfrags; c++)
292 waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
294 EnterCriticalSection(&(device->mixlock));
296 /* clear the queue */
299 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
303 ERR("Still %d references on primary buffer, refcount leak?\n", ref);
307 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
310 TRACE("(%p)\n", device);
312 device->buflen = ds_hel_buflen;
313 err = DSOUND_PrimaryOpen(device);
316 WARN("DSOUND_PrimaryOpen failed\n");
320 device->state = STATE_STOPPED;
324 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
326 TRACE("(%p)\n", device);
329 EnterCriticalSection(&(device->mixlock));
331 DSOUND_PrimaryClose(device);
332 if (device->driver) {
334 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
338 HeapFree(GetProcessHeap(),0,device->pwave);
339 HeapFree(GetProcessHeap(),0,device->pwfx);
342 LeaveCriticalSection(&(device->mixlock));
348 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
351 TRACE("(%p)\n", device);
354 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
356 WARN("IDsDriverBuffer_Play failed\n");
358 err = mmErr(waveOutRestart(device->hwo));
360 WARN("waveOutRestart failed\n");
366 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
369 TRACE("(%p)\n", device);
372 err = IDsDriverBuffer_Stop(device->hwbuf);
373 if (err == DSERR_BUFFERLOST) {
374 DSOUND_PrimaryClose(device);
375 err = DSOUND_ReopenDevice(device, FALSE);
377 ERR("DSOUND_ReopenDevice failed\n");
380 err = DSOUND_PrimaryOpen(device);
382 WARN("DSOUND_PrimaryOpen failed\n");
384 } else if (err != DS_OK) {
385 WARN("IDsDriverBuffer_Stop failed\n");
389 /* don't call the wave system with the lock set */
390 LeaveCriticalSection(&(device->mixlock));
393 err = mmErr(waveOutPause(device->hwo));
396 EnterCriticalSection(&(device->mixlock));
399 WARN("waveOutPause failed\n");
405 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
407 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
410 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
412 WARN("IDsDriverBuffer_GetPosition failed\n");
416 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
418 /* check if playpos was requested */
420 /* use the cached play position */
421 *playpos = device->pwplay * device->fraglen;
423 /* check if writepos was requested */
425 /* the writepos is the first non-queued position */
426 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
428 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
432 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
434 HRESULT err = DSERR_BUFFERLOST;
435 int i, alloc_size, cp_size;
436 DWORD nSamplesPerSec, bpp, chans;
437 TRACE("(%p,%p)\n", device, wfex);
439 if (device->priolevel == DSSCL_NORMAL) {
440 WARN("failed priority check!\n");
441 return DSERR_PRIOLEVELNEEDED;
444 /* Let's be pedantic! */
446 WARN("invalid parameter: wfex==NULL!\n");
447 return DSERR_INVALIDPARAM;
449 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
450 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
451 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
452 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
453 wfex->wBitsPerSample, wfex->cbSize);
456 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
457 EnterCriticalSection(&(device->mixlock));
459 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
460 alloc_size = sizeof(WAVEFORMATEX);
461 cp_size = sizeof(PCMWAVEFORMAT);
463 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
465 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
467 nSamplesPerSec = device->pwfx->nSamplesPerSec;
468 bpp = device->pwfx->wBitsPerSample;
469 chans = device->pwfx->nChannels;
471 CopyMemory(device->pwfx, wfex, cp_size);
473 if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
474 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
476 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
477 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
479 err = DSERR_BUFFERLOST;
480 CopyMemory(device->pwfx, wfex, cp_size);
483 if (err != DSERR_BUFFERLOST && FAILED(err)) {
484 WARN("IDsDriverBuffer_SetFormat failed\n");
492 /* ALSA specific: S_FALSE tells that recreation was successful,
493 * but size and location may be changed, and buffer has to be restarted
494 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
495 * and the entire re-initialization will occur anyway
497 IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
498 IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
500 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
501 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
502 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
505 DSOUND_RecalcPrimary(device);
508 if (err == DSERR_BUFFERLOST)
510 DSOUND_PrimaryClose(device);
512 err = DSOUND_ReopenDevice(device, FALSE);
515 WARN("DSOUND_ReopenDevice failed: %08x\n", err);
518 err = DSOUND_PrimaryOpen(device);
520 WARN("DSOUND_PrimaryOpen failed\n");
524 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
526 DSOUND_PrimaryClose(device);
527 device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
528 err = DSOUND_ReopenDevice(device, TRUE);
530 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
531 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
532 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
536 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
537 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
538 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
539 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
540 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
542 if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
543 IDirectSoundBufferImpl** dsb = device->buffers;
544 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
546 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
548 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
549 DSOUND_RecalcFormat((*dsb));
550 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen, FALSE);
551 (*dsb)->primary_mixpos = 0;
553 RtlReleaseResource(&(*dsb)->lock);
559 LeaveCriticalSection(&(device->mixlock));
560 RtlReleaseResource(&(device->buffer_list_lock));
566 /*******************************************************************************
569 /* This sets this format for the <em>Primary Buffer Only</em> */
570 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
571 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
572 LPDIRECTSOUNDBUFFER iface,
573 LPCWAVEFORMATEX wfex)
575 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
576 TRACE("(%p,%p)\n", iface, wfex);
577 return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
580 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
581 LPDIRECTSOUNDBUFFER iface,LONG vol
583 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
586 HRESULT hres = DS_OK;
587 TRACE("(%p,%d)\n", iface, vol);
589 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
590 WARN("control unavailable\n");
591 return DSERR_CONTROLUNAVAIL;
594 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
595 WARN("invalid parameter: vol = %d\n", vol);
596 return DSERR_INVALIDPARAM;
600 EnterCriticalSection(&(device->mixlock));
602 waveOutGetVolume(device->hwo, &factors);
603 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
604 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
605 DSOUND_AmpFactorToVolPan(&volpan);
606 if (vol != volpan.lVolume) {
608 DSOUND_RecalcVolPan(&volpan);
610 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
612 WARN("IDsDriverBuffer_SetVolumePan failed\n");
614 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
615 waveOutSetVolume(device->hwo, ampfactors);
619 LeaveCriticalSection(&(device->mixlock));
625 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
626 LPDIRECTSOUNDBUFFER iface,LPLONG vol
628 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
631 TRACE("(%p,%p)\n", iface, vol);
633 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
634 WARN("control unavailable\n");
635 return DSERR_CONTROLUNAVAIL;
639 WARN("invalid parameter: vol = NULL\n");
640 return DSERR_INVALIDPARAM;
643 waveOutGetVolume(device->hwo, &factors);
644 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
645 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
646 DSOUND_AmpFactorToVolPan(&volpan);
647 *vol = volpan.lVolume;
651 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
652 LPDIRECTSOUNDBUFFER iface,DWORD freq
654 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
655 TRACE("(%p,%d)\n",This,freq);
657 /* You cannot set the frequency of the primary buffer */
658 WARN("control unavailable\n");
659 return DSERR_CONTROLUNAVAIL;
662 static HRESULT WINAPI PrimaryBufferImpl_Play(
663 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
665 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
666 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
668 if (!(flags & DSBPLAY_LOOPING)) {
669 WARN("invalid parameter: flags = %08x\n", flags);
670 return DSERR_INVALIDPARAM;
674 EnterCriticalSection(&(device->mixlock));
676 if (device->state == STATE_STOPPED)
677 device->state = STATE_STARTING;
678 else if (device->state == STATE_STOPPING)
679 device->state = STATE_PLAYING;
681 LeaveCriticalSection(&(device->mixlock));
687 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
689 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
690 TRACE("(%p)\n", iface);
693 EnterCriticalSection(&(device->mixlock));
695 if (device->state == STATE_PLAYING)
696 device->state = STATE_STOPPING;
697 else if (device->state == STATE_STARTING)
698 device->state = STATE_STOPPED;
700 LeaveCriticalSection(&(device->mixlock));
706 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
708 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
709 ULONG ref = InterlockedIncrement(&(This->ref));
710 TRACE("(%p) ref was %d\n", This, ref - 1);
714 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
716 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
717 DWORD ref = InterlockedDecrement(&(This->ref));
718 TRACE("(%p) ref was %d\n", This, ref + 1);
721 This->device->primary = NULL;
722 HeapFree(GetProcessHeap(), 0, This);
723 TRACE("(%p) released\n", This);
728 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
729 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
732 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
733 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
736 EnterCriticalSection(&(device->mixlock));
738 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
740 WARN("DSOUND_PrimaryGetPosition failed\n");
741 LeaveCriticalSection(&(device->mixlock));
745 if (device->state != STATE_STOPPED)
746 /* apply the documented 10ms lead to writepos */
747 *writepos += device->writelead;
748 while (*writepos >= device->buflen) *writepos -= device->buflen;
751 LeaveCriticalSection(&(device->mixlock));
754 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
758 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
759 LPDIRECTSOUNDBUFFER iface,LPDWORD status
761 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
762 TRACE("(%p,%p)\n", iface, status);
764 if (status == NULL) {
765 WARN("invalid parameter: status == NULL\n");
766 return DSERR_INVALIDPARAM;
770 if ((device->state == STATE_STARTING) ||
771 (device->state == STATE_PLAYING))
772 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
774 TRACE("status=%x\n", *status);
779 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
780 LPDIRECTSOUNDBUFFER iface,
786 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
787 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
789 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
791 if (lpwf) { /* NULL is valid */
792 if (wfsize >= size) {
793 CopyMemory(lpwf,device->pwfx,size);
797 WARN("invalid parameter: wfsize too small\n");
800 return DSERR_INVALIDPARAM;
804 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
806 WARN("invalid parameter: wfwritten == NULL\n");
807 return DSERR_INVALIDPARAM;
814 static HRESULT WINAPI PrimaryBufferImpl_Lock(
815 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
818 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
819 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
831 if (device->priolevel != DSSCL_WRITEPRIMARY) {
832 WARN("failed priority check!\n");
833 return DSERR_PRIOLEVELNEEDED;
836 /* when this flag is set, writecursor is meaningless and must be calculated */
837 if (flags & DSBLOCK_FROMWRITECURSOR) {
838 /* GetCurrentPosition does too much magic to duplicate here */
839 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
841 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
846 /* when this flag is set, writebytes is meaningless and must be set */
847 if (flags & DSBLOCK_ENTIREBUFFER)
848 writebytes = device->buflen;
850 if (writecursor >= device->buflen) {
851 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
852 writecursor, device->buflen);
853 return DSERR_INVALIDPARAM;
856 if (writebytes > device->buflen) {
857 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
858 writebytes, device->buflen);
859 return DSERR_INVALIDPARAM;
862 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
863 hres = IDsDriverBuffer_Lock(device->hwbuf,
864 lplpaudioptr1, audiobytes1,
865 lplpaudioptr2, audiobytes2,
866 writecursor, writebytes,
869 WARN("IDsDriverBuffer_Lock failed\n");
873 if (writecursor+writebytes <= device->buflen) {
874 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
875 *audiobytes1 = writebytes;
877 *(LPBYTE*)lplpaudioptr2 = NULL;
880 TRACE("->%d.0\n",writebytes);
882 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
883 *audiobytes1 = device->buflen-writecursor;
885 *(LPBYTE*)lplpaudioptr2 = device->buffer;
887 *audiobytes2 = writebytes-(device->buflen-writecursor);
888 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
894 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
895 LPDIRECTSOUNDBUFFER iface,DWORD newpos
897 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
898 TRACE("(%p,%d)\n",This,newpos);
900 /* You cannot set the position of the primary buffer */
901 WARN("invalid call\n");
902 return DSERR_INVALIDCALL;
905 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
906 LPDIRECTSOUNDBUFFER iface,LONG pan
908 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
911 HRESULT hres = DS_OK;
912 TRACE("(%p,%d)\n", iface, pan);
914 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
915 WARN("control unavailable\n");
916 return DSERR_CONTROLUNAVAIL;
919 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
920 WARN("invalid parameter: pan = %d\n", pan);
921 return DSERR_INVALIDPARAM;
925 EnterCriticalSection(&(device->mixlock));
927 waveOutGetVolume(device->hwo, &factors);
928 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
929 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
930 DSOUND_AmpFactorToVolPan(&volpan);
931 if (pan != volpan.lPan) {
933 DSOUND_RecalcVolPan(&volpan);
935 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &volpan);
937 WARN("IDsDriverBuffer_SetVolumePan failed\n");
939 ampfactors = (volpan.dwTotalLeftAmpFactor & 0xffff) | (volpan.dwTotalRightAmpFactor << 16);
940 waveOutSetVolume(device->hwo, ampfactors);
944 LeaveCriticalSection(&(device->mixlock));
950 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
951 LPDIRECTSOUNDBUFFER iface,LPLONG pan
953 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
956 TRACE("(%p,%p)\n", iface, pan);
958 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
959 WARN("control unavailable\n");
960 return DSERR_CONTROLUNAVAIL;
964 WARN("invalid parameter: pan == NULL\n");
965 return DSERR_INVALIDPARAM;
968 waveOutGetVolume(device->hwo, &factors);
969 volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
970 volpan.dwTotalRightAmpFactor=ampfactors >> 16;
971 DSOUND_AmpFactorToVolPan(&volpan);
976 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
977 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
979 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
980 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
982 if (device->priolevel != DSSCL_WRITEPRIMARY) {
983 WARN("failed priority check!\n");
984 return DSERR_PRIOLEVELNEEDED;
987 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
990 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
992 WARN("IDsDriverBuffer_Unlock failed\n");
1000 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1001 LPDIRECTSOUNDBUFFER iface
1003 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1004 FIXME("(%p):stub\n",This);
1008 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1009 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1011 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1012 TRACE("(%p,%p)\n", iface, freq);
1015 WARN("invalid parameter: freq == NULL\n");
1016 return DSERR_INVALIDPARAM;
1019 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1020 WARN("control unavailable\n");
1021 return DSERR_CONTROLUNAVAIL;
1024 *freq = device->pwfx->nSamplesPerSec;
1025 TRACE("-> %d\n", *freq);
1030 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1031 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1033 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1034 WARN("(%p) already initialized\n", This);
1035 return DSERR_ALREADYINITIALIZED;
1038 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1039 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1041 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1042 TRACE("(%p,%p)\n", iface, caps);
1045 WARN("invalid parameter: caps == NULL\n");
1046 return DSERR_INVALIDPARAM;
1049 if (caps->dwSize < sizeof(*caps)) {
1050 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1051 return DSERR_INVALIDPARAM;
1054 caps->dwFlags = device->dsbd.dwFlags;
1055 caps->dwBufferBytes = device->buflen;
1057 /* Windows reports these as zero */
1058 caps->dwUnlockTransferRate = 0;
1059 caps->dwPlayCpuOverhead = 0;
1064 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1065 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1067 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1068 DirectSoundDevice *device = This->device;
1069 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1071 if (ppobj == NULL) {
1072 WARN("invalid parameter\n");
1073 return E_INVALIDARG;
1076 *ppobj = NULL; /* assume failure */
1078 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1079 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1080 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1085 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1086 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1087 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1088 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1089 return E_NOINTERFACE;
1092 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1093 ERR("app requested IDirectSoundNotify on primary buffer\n");
1094 /* FIXME: should we support this? */
1095 return E_NOINTERFACE;
1098 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1099 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1100 return E_NOINTERFACE;
1103 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1104 if (!device->listener)
1105 IDirectSound3DListenerImpl_Create(device, &device->listener);
1106 if (device->listener) {
1107 *ppobj = device->listener;
1108 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1112 WARN("IID_IDirectSound3DListener failed\n");
1113 return E_NOINTERFACE;
1116 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1117 FIXME("app requested IKsPropertySet on primary buffer\n");
1118 return E_NOINTERFACE;
1121 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1122 return E_NOINTERFACE;
1125 static const IDirectSoundBufferVtbl dspbvt =
1127 PrimaryBufferImpl_QueryInterface,
1128 PrimaryBufferImpl_AddRef,
1129 PrimaryBufferImpl_Release,
1130 PrimaryBufferImpl_GetCaps,
1131 PrimaryBufferImpl_GetCurrentPosition,
1132 PrimaryBufferImpl_GetFormat,
1133 PrimaryBufferImpl_GetVolume,
1134 PrimaryBufferImpl_GetPan,
1135 PrimaryBufferImpl_GetFrequency,
1136 PrimaryBufferImpl_GetStatus,
1137 PrimaryBufferImpl_Initialize,
1138 PrimaryBufferImpl_Lock,
1139 PrimaryBufferImpl_Play,
1140 PrimaryBufferImpl_SetCurrentPosition,
1141 PrimaryBufferImpl_SetFormat,
1142 PrimaryBufferImpl_SetVolume,
1143 PrimaryBufferImpl_SetPan,
1144 PrimaryBufferImpl_SetFrequency,
1145 PrimaryBufferImpl_Stop,
1146 PrimaryBufferImpl_Unlock,
1147 PrimaryBufferImpl_Restore
1150 HRESULT PrimaryBufferImpl_Create(
1151 DirectSoundDevice * device,
1152 PrimaryBufferImpl ** ppdsb,
1153 LPCDSBUFFERDESC dsbd)
1155 PrimaryBufferImpl *dsb;
1156 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1158 if (dsbd->lpwfxFormat) {
1159 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1161 return DSERR_INVALIDPARAM;
1164 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1167 WARN("out of memory\n");
1169 return DSERR_OUTOFMEMORY;
1173 dsb->device = device;
1174 dsb->lpVtbl = &dspbvt;
1176 device->dsbd = *dsbd;
1178 TRACE("Created primary buffer at %p\n", dsb);
1179 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1180 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1181 device->pwfx->wFormatTag, device->pwfx->nChannels,
1182 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1183 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1184 device->pwfx->cbSize);