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 /* Given a timer delay of 10ms, the fragment size is approximately:
50 * fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
51 * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
53 * ALSA uses buffers that are powers of 2. Because of this, fraglen
54 * is rounded up to the nearest power of 2:
57 if (nSamplesPerSec <= 12800)
58 return 128 * nBlockAlign;
60 if (nSamplesPerSec <= 25600)
61 return 256 * nBlockAlign;
63 if (nSamplesPerSec <= 51200)
64 return 512 * nBlockAlign;
66 return 1024 * nBlockAlign;
69 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
71 TRACE("(%p)\n", device);
73 device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
74 device->helfrags = device->buflen / device->fraglen;
75 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
77 if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
78 device->writelead = 0;
80 /* calculate the 10ms write lead */
81 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
84 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
87 TRACE("(%p, %d)\n", device, forcewave);
91 IDsDriver_Close(device->driver);
92 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
93 waveOutClose(device->hwo);
94 IDsDriver_Release(device->driver);
95 device->driver = NULL;
96 device->buffer = NULL;
99 else if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
100 waveOutClose(device->hwo);
102 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
103 if (ds_hw_accel != DS_HW_ACCEL_EMULATION && !forcewave)
104 waveOutMessage((HWAVEOUT)device->drvdesc.dnDevNode, DRV_QUERYDSOUNDIFACE, (DWORD_PTR)&device->driver, 0);
106 /* Get driver description */
107 if (device->driver) {
108 DWORD wod = device->drvdesc.dnDevNode;
109 hres = IDsDriver_GetDriverDesc(device->driver,&(device->drvdesc));
110 device->drvdesc.dnDevNode = wod;
112 WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres);
113 IDsDriver_Release(device->driver);
114 device->driver = NULL;
118 /* if no DirectSound interface available, use WINMM API instead */
120 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
122 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
124 DWORD flags = CALLBACK_FUNCTION;
127 flags |= WAVE_DIRECTSOUND;
129 hres = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode, device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device, flags));
131 WARN("waveOutOpen failed\n");
134 IDsDriver_Release(device->driver);
135 device->driver = NULL;
142 hres = IDsDriver_Open(device->driver);
147 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
151 TRACE("(%p)\n", device);
153 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
154 on windows this size is always fixed (tested on win-xp) */
156 device->buflen = ds_hel_buflen;
157 buflen = device->buflen;
158 buflen -= buflen % device->pwfx->nBlockAlign;
159 device->buflen = buflen;
163 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
164 DSBCAPS_PRIMARYBUFFER,0,
165 &(device->buflen),&(device->buffer),
166 (LPVOID*)&(device->hwbuf));
169 WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err);
170 err = DSOUND_ReopenDevice(device, TRUE);
173 WARN("Falling back to waveout failed too! Giving up\n");
178 IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
180 DSOUND_RecalcPrimary(device);
181 device->prebuf = ds_snd_queue_max;
182 if (device->helfrags < ds_snd_queue_min)
184 WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device->buflen, ds_snd_queue_min * device->fraglen);
185 device->buflen = buflen;
186 IDsDriverBuffer_Release(device->hwbuf);
187 device->hwbuf = NULL;
188 err = DSOUND_ReopenDevice(device, TRUE);
191 WARN("Falling back to waveout failed too! Giving up\n");
195 else if (device->helfrags < ds_snd_queue_max)
196 device->prebuf = device->helfrags;
199 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
200 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
201 if (!device->mix_buffer)
204 IDsDriverBuffer_Release(device->hwbuf);
205 device->hwbuf = NULL;
206 return DSERR_OUTOFMEMORY;
209 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
210 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
212 /* are we using waveOut stuff? */
213 if (!device->driver) {
215 LPWAVEHDR headers = NULL;
219 /* Start in pause mode, to allow buffers to get filled */
220 waveOutPause(device->hwo);
222 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
224 /* reallocate emulated primary buffer */
226 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
228 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
231 ERR("failed to allocate primary buffer\n");
232 return DSERR_OUTOFMEMORY;
233 /* but the old buffer might still exist and must be re-prepared */
236 DSOUND_RecalcPrimary(device);
238 headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
240 headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
243 ERR("failed to allocate wave headers\n");
244 HeapFree(GetProcessHeap(), 0, newbuf);
245 DSOUND_RecalcPrimary(device);
246 return DSERR_OUTOFMEMORY;
249 device->buffer = newbuf;
250 device->pwave = headers;
252 /* prepare fragment headers */
253 for (c=0; c<device->helfrags; c++) {
254 device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
255 device->pwave[c].dwBufferLength = device->fraglen;
256 device->pwave[c].dwUser = (DWORD_PTR)device;
257 device->pwave[c].dwFlags = 0;
258 device->pwave[c].dwLoops = 0;
259 err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
262 waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
267 overshot = device->buflen % device->fraglen;
271 overshot -= overshot % device->pwfx->nBlockAlign;
272 device->pwave[device->helfrags - 1].dwBufferLength += overshot;
275 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
277 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
278 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
279 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
280 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
281 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
286 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
288 TRACE("(%p)\n", device);
290 /* are we using waveOut stuff? */
291 if (!device->hwbuf) {
294 /* get out of CS when calling the wave system */
295 LeaveCriticalSection(&(device->mixlock));
297 device->pwqueue = (DWORD)-1; /* resetting queues */
298 waveOutReset(device->hwo);
299 for (c=0; c<device->helfrags; c++)
300 waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
302 EnterCriticalSection(&(device->mixlock));
304 /* clear the queue */
307 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
311 ERR("Still %d references on primary buffer, refcount leak?\n", ref);
315 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
318 TRACE("(%p)\n", device);
320 device->buflen = ds_hel_buflen;
321 err = DSOUND_PrimaryOpen(device);
324 WARN("DSOUND_PrimaryOpen failed\n");
328 device->state = STATE_STOPPED;
332 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
334 TRACE("(%p)\n", device);
337 EnterCriticalSection(&(device->mixlock));
339 DSOUND_PrimaryClose(device);
340 if (device->driver) {
342 if (IDsDriverBuffer_Release(device->hwbuf) == 0)
346 HeapFree(GetProcessHeap(),0,device->pwave);
347 HeapFree(GetProcessHeap(),0,device->pwfx);
350 LeaveCriticalSection(&(device->mixlock));
356 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
359 TRACE("(%p)\n", device);
362 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
364 WARN("IDsDriverBuffer_Play failed\n");
366 err = mmErr(waveOutRestart(device->hwo));
368 WARN("waveOutRestart failed\n");
374 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
377 TRACE("(%p)\n", device);
380 err = IDsDriverBuffer_Stop(device->hwbuf);
381 if (err == DSERR_BUFFERLOST) {
382 DSOUND_PrimaryClose(device);
383 err = DSOUND_ReopenDevice(device, FALSE);
385 ERR("DSOUND_ReopenDevice failed\n");
388 err = DSOUND_PrimaryOpen(device);
390 WARN("DSOUND_PrimaryOpen failed\n");
392 } else if (err != DS_OK) {
393 WARN("IDsDriverBuffer_Stop failed\n");
397 /* don't call the wave system with the lock set */
398 LeaveCriticalSection(&(device->mixlock));
401 err = mmErr(waveOutPause(device->hwo));
404 EnterCriticalSection(&(device->mixlock));
407 WARN("waveOutPause failed\n");
413 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
415 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
418 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
420 WARN("IDsDriverBuffer_GetPosition failed\n");
424 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
426 /* check if playpos was requested */
428 /* use the cached play position */
429 *playpos = device->pwplay * device->fraglen;
431 /* check if writepos was requested */
433 /* the writepos is the first non-queued position */
434 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
436 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
440 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
442 HRESULT err = DSERR_BUFFERLOST;
443 int i, alloc_size, cp_size;
444 DWORD nSamplesPerSec, bpp, chans;
445 TRACE("(%p,%p)\n", device, wfex);
447 if (device->priolevel == DSSCL_NORMAL) {
448 WARN("failed priority check!\n");
449 return DSERR_PRIOLEVELNEEDED;
452 /* Let's be pedantic! */
454 WARN("invalid parameter: wfex==NULL!\n");
455 return DSERR_INVALIDPARAM;
457 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
458 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
459 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
460 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
461 wfex->wBitsPerSample, wfex->cbSize);
464 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
465 EnterCriticalSection(&(device->mixlock));
467 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
468 alloc_size = sizeof(WAVEFORMATEX);
469 cp_size = sizeof(PCMWAVEFORMAT);
471 alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
473 device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
475 nSamplesPerSec = device->pwfx->nSamplesPerSec;
476 bpp = device->pwfx->wBitsPerSample;
477 chans = device->pwfx->nChannels;
479 CopyMemory(device->pwfx, wfex, cp_size);
481 if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
482 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
484 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
485 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
487 err = DSERR_BUFFERLOST;
488 CopyMemory(device->pwfx, wfex, cp_size);
491 if (err != DSERR_BUFFERLOST && FAILED(err)) {
492 WARN("IDsDriverBuffer_SetFormat failed\n");
500 /* ALSA specific: S_FALSE tells that recreation was successful,
501 * but size and location may be changed, and buffer has to be restarted
502 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
503 * and the entire re-initialization will occur anyway
505 IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
506 IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
508 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
509 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
510 device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
513 DSOUND_RecalcPrimary(device);
516 if (err == DSERR_BUFFERLOST)
518 DSOUND_PrimaryClose(device);
520 err = DSOUND_ReopenDevice(device, FALSE);
523 WARN("DSOUND_ReopenDevice failed: %08x\n", err);
526 err = DSOUND_PrimaryOpen(device);
528 WARN("DSOUND_PrimaryOpen failed\n");
532 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
534 DSOUND_PrimaryClose(device);
535 device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
536 err = DSOUND_ReopenDevice(device, TRUE);
538 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
539 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
540 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
544 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
545 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
546 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
547 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
548 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
550 if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
551 IDirectSoundBufferImpl** dsb = device->buffers;
552 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
554 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
556 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
557 DSOUND_RecalcFormat((*dsb));
558 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen, FALSE);
559 (*dsb)->primary_mixpos = 0;
561 RtlReleaseResource(&(*dsb)->lock);
567 LeaveCriticalSection(&(device->mixlock));
568 RtlReleaseResource(&(device->buffer_list_lock));
574 /*******************************************************************************
577 /* This sets this format for the <em>Primary Buffer Only</em> */
578 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
579 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
580 LPDIRECTSOUNDBUFFER iface,
581 LPCWAVEFORMATEX wfex)
583 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
584 TRACE("(%p,%p)\n", iface, wfex);
585 return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
588 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
589 LPDIRECTSOUNDBUFFER iface,LONG vol
591 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
593 HRESULT hres = DS_OK;
594 TRACE("(%p,%d)\n", iface, vol);
596 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
597 WARN("control unavailable\n");
598 return DSERR_CONTROLUNAVAIL;
601 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
602 WARN("invalid parameter: vol = %d\n", vol);
603 return DSERR_INVALIDPARAM;
607 EnterCriticalSection(&(device->mixlock));
609 waveOutGetVolume(device->hwo, &factors);
610 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
611 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
612 DSOUND_AmpFactorToVolPan(&device->volpan);
613 if (vol != device->volpan.lVolume) {
614 device->volpan.lVolume=vol;
615 DSOUND_RecalcVolPan(&device->volpan);
617 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
619 WARN("IDsDriverBuffer_SetVolumePan failed\n");
621 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
622 waveOutSetVolume(device->hwo, ampfactors);
626 LeaveCriticalSection(&(device->mixlock));
632 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
633 LPDIRECTSOUNDBUFFER iface,LPLONG vol
635 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
637 TRACE("(%p,%p)\n", iface, vol);
639 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
640 WARN("control unavailable\n");
641 return DSERR_CONTROLUNAVAIL;
645 WARN("invalid parameter: vol = NULL\n");
646 return DSERR_INVALIDPARAM;
651 waveOutGetVolume(device->hwo, &factors);
652 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
653 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
654 DSOUND_AmpFactorToVolPan(&device->volpan);
656 *vol = device->volpan.lVolume;
660 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
661 LPDIRECTSOUNDBUFFER iface,DWORD freq
663 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
664 TRACE("(%p,%d)\n",This,freq);
666 /* You cannot set the frequency of the primary buffer */
667 WARN("control unavailable\n");
668 return DSERR_CONTROLUNAVAIL;
671 static HRESULT WINAPI PrimaryBufferImpl_Play(
672 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
674 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
675 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
677 if (!(flags & DSBPLAY_LOOPING)) {
678 WARN("invalid parameter: flags = %08x\n", flags);
679 return DSERR_INVALIDPARAM;
683 EnterCriticalSection(&(device->mixlock));
685 if (device->state == STATE_STOPPED)
686 device->state = STATE_STARTING;
687 else if (device->state == STATE_STOPPING)
688 device->state = STATE_PLAYING;
690 LeaveCriticalSection(&(device->mixlock));
696 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
698 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
699 TRACE("(%p)\n", iface);
702 EnterCriticalSection(&(device->mixlock));
704 if (device->state == STATE_PLAYING)
705 device->state = STATE_STOPPING;
706 else if (device->state == STATE_STARTING)
707 device->state = STATE_STOPPED;
709 LeaveCriticalSection(&(device->mixlock));
715 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
717 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
718 ULONG ref = InterlockedIncrement(&(This->ref));
719 TRACE("(%p) ref was %d\n", This, ref - 1);
723 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
725 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
726 DWORD ref = InterlockedDecrement(&(This->ref));
727 TRACE("(%p) ref was %d\n", This, ref + 1);
730 This->device->primary = NULL;
731 HeapFree(GetProcessHeap(), 0, This);
732 TRACE("(%p) released\n", This);
737 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
738 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
741 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
742 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
745 EnterCriticalSection(&(device->mixlock));
747 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
749 WARN("DSOUND_PrimaryGetPosition failed\n");
750 LeaveCriticalSection(&(device->mixlock));
754 if (device->state != STATE_STOPPED)
755 /* apply the documented 10ms lead to writepos */
756 *writepos += device->writelead;
757 while (*writepos >= device->buflen) *writepos -= device->buflen;
760 LeaveCriticalSection(&(device->mixlock));
763 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
767 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
768 LPDIRECTSOUNDBUFFER iface,LPDWORD status
770 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
771 TRACE("(%p,%p)\n", iface, status);
773 if (status == NULL) {
774 WARN("invalid parameter: status == NULL\n");
775 return DSERR_INVALIDPARAM;
779 if ((device->state == STATE_STARTING) ||
780 (device->state == STATE_PLAYING))
781 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
783 TRACE("status=%x\n", *status);
788 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
789 LPDIRECTSOUNDBUFFER iface,
795 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
796 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
798 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
800 if (lpwf) { /* NULL is valid */
801 if (wfsize >= size) {
802 CopyMemory(lpwf,device->pwfx,size);
806 WARN("invalid parameter: wfsize too small\n");
809 return DSERR_INVALIDPARAM;
813 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
815 WARN("invalid parameter: wfwritten == NULL\n");
816 return DSERR_INVALIDPARAM;
823 static HRESULT WINAPI PrimaryBufferImpl_Lock(
824 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
827 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
828 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
841 return DSERR_INVALIDPARAM;
843 if (device->priolevel != DSSCL_WRITEPRIMARY) {
844 WARN("failed priority check!\n");
845 return DSERR_PRIOLEVELNEEDED;
848 /* when this flag is set, writecursor is meaningless and must be calculated */
849 if (flags & DSBLOCK_FROMWRITECURSOR) {
850 /* GetCurrentPosition does too much magic to duplicate here */
851 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
853 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
858 /* when this flag is set, writebytes is meaningless and must be set */
859 if (flags & DSBLOCK_ENTIREBUFFER)
860 writebytes = device->buflen;
862 if (writecursor >= device->buflen) {
863 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
864 writecursor, device->buflen);
865 return DSERR_INVALIDPARAM;
868 if (writebytes > device->buflen) {
869 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
870 writebytes, device->buflen);
871 return DSERR_INVALIDPARAM;
874 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
875 hres = IDsDriverBuffer_Lock(device->hwbuf,
876 lplpaudioptr1, audiobytes1,
877 lplpaudioptr2, audiobytes2,
878 writecursor, writebytes,
881 WARN("IDsDriverBuffer_Lock failed\n");
885 if (writecursor+writebytes <= device->buflen) {
886 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
887 *audiobytes1 = writebytes;
889 *(LPBYTE*)lplpaudioptr2 = NULL;
892 TRACE("->%d.0\n",writebytes);
894 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
895 *audiobytes1 = device->buflen-writecursor;
897 *(LPBYTE*)lplpaudioptr2 = device->buffer;
899 *audiobytes2 = writebytes-(device->buflen-writecursor);
900 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
906 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
907 LPDIRECTSOUNDBUFFER iface,DWORD newpos
909 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
910 TRACE("(%p,%d)\n",This,newpos);
912 /* You cannot set the position of the primary buffer */
913 WARN("invalid call\n");
914 return DSERR_INVALIDCALL;
917 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
918 LPDIRECTSOUNDBUFFER iface,LONG pan
920 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
922 HRESULT hres = DS_OK;
923 TRACE("(%p,%d)\n", iface, pan);
925 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
926 WARN("control unavailable\n");
927 return DSERR_CONTROLUNAVAIL;
930 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
931 WARN("invalid parameter: pan = %d\n", pan);
932 return DSERR_INVALIDPARAM;
936 EnterCriticalSection(&(device->mixlock));
940 waveOutGetVolume(device->hwo, &factors);
941 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
942 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
943 DSOUND_AmpFactorToVolPan(&device->volpan);
945 if (pan != device->volpan.lPan) {
946 device->volpan.lPan=pan;
947 DSOUND_RecalcVolPan(&device->volpan);
949 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
951 WARN("IDsDriverBuffer_SetVolumePan failed\n");
953 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
954 waveOutSetVolume(device->hwo, ampfactors);
958 LeaveCriticalSection(&(device->mixlock));
964 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
965 LPDIRECTSOUNDBUFFER iface,LPLONG pan
967 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
969 TRACE("(%p,%p)\n", iface, pan);
971 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
972 WARN("control unavailable\n");
973 return DSERR_CONTROLUNAVAIL;
977 WARN("invalid parameter: pan == NULL\n");
978 return DSERR_INVALIDPARAM;
983 waveOutGetVolume(device->hwo, &factors);
984 device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
985 device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
986 DSOUND_AmpFactorToVolPan(&device->volpan);
988 *pan = device->volpan.lPan;
992 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
993 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
995 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
996 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
998 if (device->priolevel != DSSCL_WRITEPRIMARY) {
999 WARN("failed priority check!\n");
1000 return DSERR_PRIOLEVELNEEDED;
1003 if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
1006 if ((char *)p1 - (char *)device->buffer + x1 > device->buflen)
1007 hres = DSERR_INVALIDPARAM;
1009 hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
1011 if (hres != DS_OK) {
1012 WARN("IDsDriverBuffer_Unlock failed\n");
1020 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1021 LPDIRECTSOUNDBUFFER iface
1023 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1024 FIXME("(%p):stub\n",This);
1028 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1029 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1031 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1032 TRACE("(%p,%p)\n", iface, freq);
1035 WARN("invalid parameter: freq == NULL\n");
1036 return DSERR_INVALIDPARAM;
1039 if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1040 WARN("control unavailable\n");
1041 return DSERR_CONTROLUNAVAIL;
1044 *freq = device->pwfx->nSamplesPerSec;
1045 TRACE("-> %d\n", *freq);
1050 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1051 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1053 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1054 WARN("(%p) already initialized\n", This);
1055 return DSERR_ALREADYINITIALIZED;
1058 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1059 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1061 DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1062 TRACE("(%p,%p)\n", iface, caps);
1065 WARN("invalid parameter: caps == NULL\n");
1066 return DSERR_INVALIDPARAM;
1069 if (caps->dwSize < sizeof(*caps)) {
1070 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1071 return DSERR_INVALIDPARAM;
1074 caps->dwFlags = device->dsbd.dwFlags;
1075 caps->dwBufferBytes = device->buflen;
1077 /* Windows reports these as zero */
1078 caps->dwUnlockTransferRate = 0;
1079 caps->dwPlayCpuOverhead = 0;
1084 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1085 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1087 PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1088 DirectSoundDevice *device = This->device;
1089 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1091 if (ppobj == NULL) {
1092 WARN("invalid parameter\n");
1093 return E_INVALIDARG;
1096 *ppobj = NULL; /* assume failure */
1098 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1099 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1100 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1105 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1106 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1107 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1108 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1109 return E_NOINTERFACE;
1112 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1113 ERR("app requested IDirectSoundNotify on primary buffer\n");
1114 /* FIXME: should we support this? */
1115 return E_NOINTERFACE;
1118 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1119 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1120 return E_NOINTERFACE;
1123 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1124 if (!device->listener)
1125 IDirectSound3DListenerImpl_Create(device, &device->listener);
1126 if (device->listener) {
1127 *ppobj = device->listener;
1128 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1132 WARN("IID_IDirectSound3DListener failed\n");
1133 return E_NOINTERFACE;
1136 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1137 FIXME("app requested IKsPropertySet on primary buffer\n");
1138 return E_NOINTERFACE;
1141 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1142 return E_NOINTERFACE;
1145 static const IDirectSoundBufferVtbl dspbvt =
1147 PrimaryBufferImpl_QueryInterface,
1148 PrimaryBufferImpl_AddRef,
1149 PrimaryBufferImpl_Release,
1150 PrimaryBufferImpl_GetCaps,
1151 PrimaryBufferImpl_GetCurrentPosition,
1152 PrimaryBufferImpl_GetFormat,
1153 PrimaryBufferImpl_GetVolume,
1154 PrimaryBufferImpl_GetPan,
1155 PrimaryBufferImpl_GetFrequency,
1156 PrimaryBufferImpl_GetStatus,
1157 PrimaryBufferImpl_Initialize,
1158 PrimaryBufferImpl_Lock,
1159 PrimaryBufferImpl_Play,
1160 PrimaryBufferImpl_SetCurrentPosition,
1161 PrimaryBufferImpl_SetFormat,
1162 PrimaryBufferImpl_SetVolume,
1163 PrimaryBufferImpl_SetPan,
1164 PrimaryBufferImpl_SetFrequency,
1165 PrimaryBufferImpl_Stop,
1166 PrimaryBufferImpl_Unlock,
1167 PrimaryBufferImpl_Restore
1170 HRESULT PrimaryBufferImpl_Create(
1171 DirectSoundDevice * device,
1172 PrimaryBufferImpl ** ppdsb,
1173 LPCDSBUFFERDESC dsbd)
1175 PrimaryBufferImpl *dsb;
1176 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1178 if (dsbd->lpwfxFormat) {
1179 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1181 return DSERR_INVALIDPARAM;
1184 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1187 WARN("out of memory\n");
1189 return DSERR_OUTOFMEMORY;
1193 dsb->device = device;
1194 dsb->lpVtbl = &dspbvt;
1196 device->dsbd = *dsbd;
1198 TRACE("Created primary buffer at %p\n", dsb);
1199 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1200 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1201 device->pwfx->wFormatTag, device->pwfx->nChannels,
1202 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1203 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1204 device->pwfx->cbSize);