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
22 * When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23 * it leaves dsound in unusable (not really open) state.
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
36 #include "wine/debug.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
43 /** Calculate how long a fragment length of about 10 ms should be in frames
45 * nSamplesPerSec: Frequency rate in samples per second
46 * nBlockAlign: Size of a single blockalign
49 * Size in bytes of a single fragment
51 DWORD
DSOUND_fraglen(DWORD nSamplesPerSec
, DWORD nBlockAlign
)
53 /* Given a timer delay of 10ms, the fragment size is approximately:
54 * fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
55 * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
57 * ALSA uses buffers that are powers of 2. Because of this, fraglen
58 * is rounded up to the nearest power of 2:
61 if (nSamplesPerSec
<= 12800)
62 return 128 * nBlockAlign
;
64 if (nSamplesPerSec
<= 25600)
65 return 256 * nBlockAlign
;
67 if (nSamplesPerSec
<= 51200)
68 return 512 * nBlockAlign
;
70 return 1024 * nBlockAlign
;
73 static void DSOUND_RecalcPrimary(DirectSoundDevice
*device
)
75 TRACE("(%p)\n", device
);
77 device
->fraglen
= DSOUND_fraglen(device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nBlockAlign
);
78 device
->helfrags
= device
->buflen
/ device
->fraglen
;
79 TRACE("fraglen=%d helfrags=%d\n", device
->fraglen
, device
->helfrags
);
81 if (device
->hwbuf
&& device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDWRITELEAD
)
82 device
->writelead
= 0;
84 /* calculate the 10ms write lead */
85 device
->writelead
= (device
->pwfx
->nSamplesPerSec
/ 100) * device
->pwfx
->nBlockAlign
;
88 HRESULT
DSOUND_ReopenDevice(DirectSoundDevice
*device
, BOOL forcewave
)
91 TRACE("(%p, %d)\n", device
, forcewave
);
95 IDsDriver_Close(device
->driver
);
96 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMOPEN
)
97 waveOutClose(device
->hwo
);
98 IDsDriver_Release(device
->driver
);
99 device
->driver
= NULL
;
100 device
->buffer
= NULL
;
103 else if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMOPEN
)
104 waveOutClose(device
->hwo
);
106 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
107 if (ds_hw_accel
!= DS_HW_ACCEL_EMULATION
&& !forcewave
)
108 waveOutMessage((HWAVEOUT
)device
->drvdesc
.dnDevNode
, DRV_QUERYDSOUNDIFACE
, (DWORD_PTR
)&device
->driver
, 0);
110 /* Get driver description */
111 if (device
->driver
) {
112 DWORD wod
= device
->drvdesc
.dnDevNode
;
113 hres
= IDsDriver_GetDriverDesc(device
->driver
,&(device
->drvdesc
));
114 device
->drvdesc
.dnDevNode
= wod
;
116 WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres
);
117 IDsDriver_Release(device
->driver
);
118 device
->driver
= NULL
;
122 /* if no DirectSound interface available, use WINMM API instead */
124 device
->drvdesc
.dwFlags
= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
;
126 if (device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMOPEN
)
128 DWORD flags
= CALLBACK_FUNCTION
| WAVE_MAPPED
;
131 flags
|= WAVE_DIRECTSOUND
;
133 hres
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
, device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD_PTR
)device
, flags
));
135 WARN("waveOutOpen failed\n");
138 IDsDriver_Release(device
->driver
);
139 device
->driver
= NULL
;
146 hres
= IDsDriver_Open(device
->driver
);
151 static HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
155 TRACE("(%p)\n", device
);
157 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
158 on windows this size is always fixed (tested on win-xp) */
160 device
->buflen
= ds_hel_buflen
;
161 buflen
= device
->buflen
;
162 buflen
-= buflen
% device
->pwfx
->nBlockAlign
;
163 device
->buflen
= buflen
;
167 err
= IDsDriver_CreateSoundBuffer(device
->driver
,device
->pwfx
,
168 DSBCAPS_PRIMARYBUFFER
,0,
169 &(device
->buflen
),&(device
->buffer
),
170 (LPVOID
*)&(device
->hwbuf
));
173 WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err
);
174 err
= DSOUND_ReopenDevice(device
, TRUE
);
177 WARN("Falling back to waveout failed too! Giving up\n");
182 IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &device
->volpan
);
184 DSOUND_RecalcPrimary(device
);
185 device
->prebuf
= ds_snd_queue_max
;
186 if (device
->helfrags
< ds_snd_queue_min
)
188 WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device
->buflen
, ds_snd_queue_min
* device
->fraglen
);
189 device
->buflen
= buflen
;
190 IDsDriverBuffer_Release(device
->hwbuf
);
191 device
->hwbuf
= NULL
;
192 err
= DSOUND_ReopenDevice(device
, TRUE
);
195 WARN("Falling back to waveout failed too! Giving up\n");
199 else if (device
->helfrags
< ds_snd_queue_max
)
200 device
->prebuf
= device
->helfrags
;
203 device
->mix_buffer_len
= DSOUND_bufpos_to_mixpos(device
, device
->buflen
);
204 device
->mix_buffer
= HeapAlloc(GetProcessHeap(), 0, device
->mix_buffer_len
);
205 if (!device
->mix_buffer
)
208 IDsDriverBuffer_Release(device
->hwbuf
);
209 device
->hwbuf
= NULL
;
210 return DSERR_OUTOFMEMORY
;
213 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
214 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
216 /* are we using waveOut stuff? */
217 if (!device
->driver
) {
219 LPWAVEHDR headers
= NULL
;
223 /* Start in pause mode, to allow buffers to get filled */
224 waveOutPause(device
->hwo
);
226 TRACE("desired buflen=%d, old buffer=%p\n", buflen
, device
->buffer
);
228 /* reallocate emulated primary buffer */
230 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
, buflen
);
232 newbuf
= HeapAlloc(GetProcessHeap(),0, buflen
);
235 ERR("failed to allocate primary buffer\n");
236 return DSERR_OUTOFMEMORY
;
237 /* but the old buffer might still exist and must be re-prepared */
240 DSOUND_RecalcPrimary(device
);
242 headers
= HeapReAlloc(GetProcessHeap(),0,device
->pwave
, device
->helfrags
* sizeof(WAVEHDR
));
244 headers
= HeapAlloc(GetProcessHeap(),0,device
->helfrags
* sizeof(WAVEHDR
));
247 ERR("failed to allocate wave headers\n");
248 HeapFree(GetProcessHeap(), 0, newbuf
);
249 DSOUND_RecalcPrimary(device
);
250 return DSERR_OUTOFMEMORY
;
253 device
->buffer
= newbuf
;
254 device
->pwave
= headers
;
256 /* prepare fragment headers */
257 for (c
=0; c
<device
->helfrags
; c
++) {
258 device
->pwave
[c
].lpData
= (char*)device
->buffer
+ c
*device
->fraglen
;
259 device
->pwave
[c
].dwBufferLength
= device
->fraglen
;
260 device
->pwave
[c
].dwUser
= (DWORD_PTR
)device
;
261 device
->pwave
[c
].dwFlags
= 0;
262 device
->pwave
[c
].dwLoops
= 0;
263 err
= mmErr(waveOutPrepareHeader(device
->hwo
,&device
->pwave
[c
],sizeof(WAVEHDR
)));
266 waveOutUnprepareHeader(device
->hwo
,&device
->pwave
[c
],sizeof(WAVEHDR
));
271 overshot
= device
->buflen
% device
->fraglen
;
275 overshot
-= overshot
% device
->pwfx
->nBlockAlign
;
276 device
->pwave
[device
->helfrags
- 1].dwBufferLength
+= overshot
;
279 TRACE("fraglen=%d, overshot=%d\n", device
->fraglen
, overshot
);
281 device
->mixfunction
= mixfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
282 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
283 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
284 FillMemory(device
->mix_buffer
, device
->mix_buffer_len
, 0);
285 device
->pwplay
= device
->pwqueue
= device
->playpos
= device
->mixpos
= 0;
290 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
292 TRACE("(%p)\n", device
);
294 /* are we using waveOut stuff? */
295 if (!device
->hwbuf
) {
298 /* get out of CS when calling the wave system */
299 LeaveCriticalSection(&(device
->mixlock
));
301 device
->pwqueue
= (DWORD
)-1; /* resetting queues */
302 waveOutReset(device
->hwo
);
303 for (c
=0; c
<device
->helfrags
; c
++)
304 waveOutUnprepareHeader(device
->hwo
, &device
->pwave
[c
], sizeof(WAVEHDR
));
306 EnterCriticalSection(&(device
->mixlock
));
308 /* clear the queue */
311 ULONG ref
= IDsDriverBuffer_Release(device
->hwbuf
);
315 ERR("Still %d references on primary buffer, refcount leak?\n", ref
);
319 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
322 TRACE("(%p)\n", device
);
324 device
->buflen
= ds_hel_buflen
;
325 err
= DSOUND_PrimaryOpen(device
);
328 WARN("DSOUND_PrimaryOpen failed\n");
332 device
->state
= STATE_STOPPED
;
336 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
338 TRACE("(%p)\n", device
);
341 EnterCriticalSection(&(device
->mixlock
));
343 DSOUND_PrimaryClose(device
);
344 if (device
->driver
) {
346 if (IDsDriverBuffer_Release(device
->hwbuf
) == 0)
350 HeapFree(GetProcessHeap(),0,device
->pwave
);
351 HeapFree(GetProcessHeap(),0,device
->pwfx
);
354 LeaveCriticalSection(&(device
->mixlock
));
360 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
363 TRACE("(%p)\n", device
);
366 err
= IDsDriverBuffer_Play(device
->hwbuf
, 0, 0, DSBPLAY_LOOPING
);
368 WARN("IDsDriverBuffer_Play failed\n");
370 err
= mmErr(waveOutRestart(device
->hwo
));
372 WARN("waveOutRestart failed\n");
378 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
381 TRACE("(%p)\n", device
);
384 err
= IDsDriverBuffer_Stop(device
->hwbuf
);
385 if (err
== DSERR_BUFFERLOST
) {
386 DSOUND_PrimaryClose(device
);
387 err
= DSOUND_ReopenDevice(device
, FALSE
);
389 ERR("DSOUND_ReopenDevice failed\n");
392 err
= DSOUND_PrimaryOpen(device
);
394 WARN("DSOUND_PrimaryOpen failed\n");
396 } else if (err
!= DS_OK
) {
397 WARN("IDsDriverBuffer_Stop failed\n");
401 /* don't call the wave system with the lock set */
402 LeaveCriticalSection(&(device
->mixlock
));
405 err
= mmErr(waveOutPause(device
->hwo
));
408 EnterCriticalSection(&(device
->mixlock
));
411 WARN("waveOutPause failed\n");
417 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
419 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
422 HRESULT err
=IDsDriverBuffer_GetPosition(device
->hwbuf
,playpos
,writepos
);
424 WARN("IDsDriverBuffer_GetPosition failed\n");
428 TRACE("pwplay=%i, pwqueue=%i\n", device
->pwplay
, device
->pwqueue
);
430 /* check if playpos was requested */
432 /* use the cached play position */
433 *playpos
= device
->pwplay
* device
->fraglen
;
435 /* check if writepos was requested */
437 /* the writepos is the first non-queued position */
438 *writepos
= ((device
->pwplay
+ device
->pwqueue
) % device
->helfrags
) * device
->fraglen
;
440 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:-1, writepos
?*writepos
:-1, device
, GetTickCount());
444 static DWORD
DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex
)
446 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
)
447 return sizeof(WAVEFORMATEX
);
449 return sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
452 LPWAVEFORMATEX
DSOUND_CopyFormat(LPCWAVEFORMATEX wfex
)
454 DWORD size
= DSOUND_GetFormatSize(wfex
);
455 LPWAVEFORMATEX pwfx
= HeapAlloc(GetProcessHeap(),0,size
);
457 WARN("out of memory\n");
458 } else if (wfex
->wFormatTag
!= WAVE_FORMAT_PCM
) {
459 CopyMemory(pwfx
, wfex
, size
);
461 CopyMemory(pwfx
, wfex
, sizeof(PCMWAVEFORMAT
));
463 if (pwfx
->nBlockAlign
!= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8) {
464 WARN("Fixing bad nBlockAlign (%u)\n", pwfx
->nBlockAlign
);
465 pwfx
->nBlockAlign
= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8;
467 if (pwfx
->nAvgBytesPerSec
!= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
) {
468 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx
->nAvgBytesPerSec
);
469 pwfx
->nAvgBytesPerSec
= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
;
475 HRESULT
primarybuffer_SetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
477 HRESULT err
= DSERR_BUFFERLOST
;
479 DWORD nSamplesPerSec
, bpp
, chans
;
480 LPWAVEFORMATEX oldpwfx
;
481 BOOL forced
= device
->priolevel
== DSSCL_WRITEPRIMARY
;
483 TRACE("(%p,%p)\n", device
, wfex
);
485 if (device
->priolevel
== DSSCL_NORMAL
) {
486 WARN("failed priority check!\n");
487 return DSERR_PRIOLEVELNEEDED
;
490 /* Let's be pedantic! */
492 WARN("invalid parameter: wfex==NULL!\n");
493 return DSERR_INVALIDPARAM
;
495 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
496 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
497 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
498 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
499 wfex
->wBitsPerSample
, wfex
->cbSize
);
502 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
503 EnterCriticalSection(&(device
->mixlock
));
505 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
506 bpp
= device
->pwfx
->wBitsPerSample
;
507 chans
= device
->pwfx
->nChannels
;
509 oldpwfx
= device
->pwfx
;
510 device
->pwfx
= DSOUND_CopyFormat(wfex
);
511 if (device
->pwfx
== NULL
) {
512 device
->pwfx
= oldpwfx
;
514 err
= DSERR_OUTOFMEMORY
;
518 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DOMMSYSTEMSETFORMAT
) && device
->hwbuf
) {
519 err
= IDsDriverBuffer_SetFormat(device
->hwbuf
, device
->pwfx
);
521 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
522 if (forced
&& (device
->pwfx
->nSamplesPerSec
/100 != wfex
->nSamplesPerSec
/100 || err
== DSERR_BADFORMAT
))
524 DWORD cp_size
= wfex
->wFormatTag
== WAVE_FORMAT_PCM
?
525 sizeof(PCMWAVEFORMAT
) : sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
526 err
= DSERR_BUFFERLOST
;
527 CopyMemory(device
->pwfx
, wfex
, cp_size
);
530 if (err
!= DSERR_BUFFERLOST
&& FAILED(err
)) {
531 DWORD size
= DSOUND_GetFormatSize(oldpwfx
);
532 WARN("IDsDriverBuffer_SetFormat failed\n");
534 CopyMemory(device
->pwfx
, oldpwfx
, size
);
542 /* ALSA specific: S_FALSE tells that recreation was successful,
543 * but size and location may be changed, and buffer has to be restarted
544 * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
545 * and the entire re-initialization will occur anyway
547 IDsDriverBuffer_Lock(device
->hwbuf
, (LPVOID
*)&device
->buffer
, &device
->buflen
, NULL
, NULL
, 0, 0, DSBLOCK_ENTIREBUFFER
);
548 IDsDriverBuffer_Unlock(device
->hwbuf
, device
->buffer
, 0, NULL
, 0);
550 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
551 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
552 device
->pwplay
= device
->pwqueue
= device
->playpos
= device
->mixpos
= 0;
555 DSOUND_RecalcPrimary(device
);
558 if (err
== DSERR_BUFFERLOST
)
560 DSOUND_PrimaryClose(device
);
562 err
= DSOUND_ReopenDevice(device
, FALSE
);
565 WARN("DSOUND_ReopenDevice failed: %08x\n", err
);
568 err
= DSOUND_PrimaryOpen(device
);
570 WARN("DSOUND_PrimaryOpen failed\n");
574 if (wfex
->nSamplesPerSec
/100 != device
->pwfx
->nSamplesPerSec
/100 && forced
&& device
->buffer
)
576 DSOUND_PrimaryClose(device
);
577 device
->pwfx
->nSamplesPerSec
= wfex
->nSamplesPerSec
;
578 err
= DSOUND_ReopenDevice(device
, TRUE
);
580 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err
);
581 else if (FAILED((err
= DSOUND_PrimaryOpen(device
))))
582 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err
);
586 device
->mix_buffer_len
= DSOUND_bufpos_to_mixpos(device
, device
->buflen
);
587 device
->mix_buffer
= HeapReAlloc(GetProcessHeap(), 0, device
->mix_buffer
, device
->mix_buffer_len
);
588 FillMemory(device
->mix_buffer
, device
->mix_buffer_len
, 0);
589 device
->mixfunction
= mixfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
590 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
592 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
|| bpp
!= device
->pwfx
->wBitsPerSample
|| chans
!= device
->pwfx
->nChannels
) {
593 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
594 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
596 RtlAcquireResourceExclusive(&(*dsb
)->lock
, TRUE
);
598 (*dsb
)->freqAdjust
= ((DWORD64
)(*dsb
)->freq
<< DSOUND_FREQSHIFT
) / device
->pwfx
->nSamplesPerSec
;
599 DSOUND_RecalcFormat((*dsb
));
600 DSOUND_MixToTemporary((*dsb
), 0, (*dsb
)->buflen
, FALSE
);
601 (*dsb
)->primary_mixpos
= 0;
603 RtlReleaseResource(&(*dsb
)->lock
);
609 LeaveCriticalSection(&(device
->mixlock
));
610 RtlReleaseResource(&(device
->buffer_list_lock
));
613 HeapFree(GetProcessHeap(), 0, oldpwfx
);
617 /*******************************************************************************
620 static inline IDirectSoundBufferImpl
*impl_from_IDirectSoundBuffer(IDirectSoundBuffer
*iface
)
622 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
623 return CONTAINING_RECORD(iface
, IDirectSoundBufferImpl
, IDirectSoundBuffer8_iface
);
626 /* This sets this format for the <em>Primary Buffer Only</em> */
627 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
628 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
629 LPDIRECTSOUNDBUFFER iface
,
630 LPCWAVEFORMATEX wfex
)
632 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
633 TRACE("(%p,%p)\n", iface
, wfex
);
634 return primarybuffer_SetFormat(This
->device
, wfex
);
637 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
638 LPDIRECTSOUNDBUFFER iface
,LONG vol
640 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
641 DirectSoundDevice
*device
= This
->device
;
643 HRESULT hres
= DS_OK
;
644 TRACE("(%p,%d)\n", iface
, vol
);
646 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
647 WARN("control unavailable\n");
648 return DSERR_CONTROLUNAVAIL
;
651 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
652 WARN("invalid parameter: vol = %d\n", vol
);
653 return DSERR_INVALIDPARAM
;
657 EnterCriticalSection(&(device
->mixlock
));
659 waveOutGetVolume(device
->hwo
, &factors
);
660 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
661 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
662 DSOUND_AmpFactorToVolPan(&device
->volpan
);
663 if (vol
!= device
->volpan
.lVolume
) {
664 device
->volpan
.lVolume
=vol
;
665 DSOUND_RecalcVolPan(&device
->volpan
);
667 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &device
->volpan
);
669 WARN("IDsDriverBuffer_SetVolumePan failed\n");
671 ampfactors
= (device
->volpan
.dwTotalLeftAmpFactor
& 0xffff) | (device
->volpan
.dwTotalRightAmpFactor
<< 16);
672 waveOutSetVolume(device
->hwo
, ampfactors
);
676 LeaveCriticalSection(&(device
->mixlock
));
682 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
683 LPDIRECTSOUNDBUFFER iface
,LPLONG vol
685 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
686 DirectSoundDevice
*device
= This
->device
;
688 TRACE("(%p,%p)\n", iface
, vol
);
690 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
691 WARN("control unavailable\n");
692 return DSERR_CONTROLUNAVAIL
;
696 WARN("invalid parameter: vol = NULL\n");
697 return DSERR_INVALIDPARAM
;
702 waveOutGetVolume(device
->hwo
, &factors
);
703 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
704 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
705 DSOUND_AmpFactorToVolPan(&device
->volpan
);
707 *vol
= device
->volpan
.lVolume
;
711 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
712 LPDIRECTSOUNDBUFFER iface
,DWORD freq
714 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
715 TRACE("(%p,%d)\n",This
,freq
);
717 /* You cannot set the frequency of the primary buffer */
718 WARN("control unavailable\n");
719 return DSERR_CONTROLUNAVAIL
;
722 static HRESULT WINAPI
PrimaryBufferImpl_Play(
723 LPDIRECTSOUNDBUFFER iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
725 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
726 DirectSoundDevice
*device
= This
->device
;
727 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
729 if (!(flags
& DSBPLAY_LOOPING
)) {
730 WARN("invalid parameter: flags = %08x\n", flags
);
731 return DSERR_INVALIDPARAM
;
735 EnterCriticalSection(&(device
->mixlock
));
737 if (device
->state
== STATE_STOPPED
)
738 device
->state
= STATE_STARTING
;
739 else if (device
->state
== STATE_STOPPING
)
740 device
->state
= STATE_PLAYING
;
742 LeaveCriticalSection(&(device
->mixlock
));
748 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface
)
750 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
751 DirectSoundDevice
*device
= This
->device
;
752 TRACE("(%p)\n", iface
);
755 EnterCriticalSection(&(device
->mixlock
));
757 if (device
->state
== STATE_PLAYING
)
758 device
->state
= STATE_STOPPING
;
759 else if (device
->state
== STATE_STARTING
)
760 device
->state
= STATE_STOPPED
;
762 LeaveCriticalSection(&(device
->mixlock
));
768 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface
)
770 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
771 ULONG ref
= InterlockedIncrement(&(This
->ref
));
772 TRACE("(%p) ref was %d\n", This
, ref
- 1);
774 InterlockedIncrement(&This
->numIfaces
);
778 void primarybuffer_destroy(IDirectSoundBufferImpl
*This
)
780 This
->device
->primary
= NULL
;
781 HeapFree(GetProcessHeap(), 0, This
);
782 TRACE("(%p) released\n", This
);
785 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface
)
787 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
788 DWORD ref
= InterlockedDecrement(&(This
->ref
));
789 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
791 if (!ref
&& !InterlockedDecrement(&This
->numIfaces
))
792 primarybuffer_destroy(This
);
796 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
797 LPDIRECTSOUNDBUFFER iface
,LPDWORD playpos
,LPDWORD writepos
800 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
801 DirectSoundDevice
*device
= This
->device
;
802 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
805 EnterCriticalSection(&(device
->mixlock
));
807 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
809 WARN("DSOUND_PrimaryGetPosition failed\n");
810 LeaveCriticalSection(&(device
->mixlock
));
814 if (device
->state
!= STATE_STOPPED
)
815 /* apply the documented 10ms lead to writepos */
816 *writepos
+= device
->writelead
;
817 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
820 LeaveCriticalSection(&(device
->mixlock
));
823 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
827 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
828 LPDIRECTSOUNDBUFFER iface
,LPDWORD status
830 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
831 DirectSoundDevice
*device
= This
->device
;
832 TRACE("(%p,%p)\n", iface
, status
);
834 if (status
== NULL
) {
835 WARN("invalid parameter: status == NULL\n");
836 return DSERR_INVALIDPARAM
;
840 if ((device
->state
== STATE_STARTING
) ||
841 (device
->state
== STATE_PLAYING
))
842 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
844 TRACE("status=%x\n", *status
);
849 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
850 LPDIRECTSOUNDBUFFER iface
,
856 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
857 DirectSoundDevice
*device
= This
->device
;
858 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
860 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
862 if (lpwf
) { /* NULL is valid */
863 if (wfsize
>= size
) {
864 CopyMemory(lpwf
,device
->pwfx
,size
);
868 WARN("invalid parameter: wfsize too small\n");
871 return DSERR_INVALIDPARAM
;
875 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
877 WARN("invalid parameter: wfwritten == NULL\n");
878 return DSERR_INVALIDPARAM
;
885 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
886 LPDIRECTSOUNDBUFFER iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
889 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
890 DirectSoundDevice
*device
= This
->device
;
891 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
904 return DSERR_INVALIDPARAM
;
906 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
907 WARN("failed priority check!\n");
908 return DSERR_PRIOLEVELNEEDED
;
911 /* when this flag is set, writecursor is meaningless and must be calculated */
912 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
913 /* GetCurrentPosition does too much magic to duplicate here */
914 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
916 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
921 /* when this flag is set, writebytes is meaningless and must be set */
922 if (flags
& DSBLOCK_ENTIREBUFFER
)
923 writebytes
= device
->buflen
;
925 if (writecursor
>= device
->buflen
) {
926 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
927 writecursor
, device
->buflen
);
928 return DSERR_INVALIDPARAM
;
931 if (writebytes
> device
->buflen
) {
932 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
933 writebytes
, device
->buflen
);
934 return DSERR_INVALIDPARAM
;
937 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
938 hres
= IDsDriverBuffer_Lock(device
->hwbuf
,
939 lplpaudioptr1
, audiobytes1
,
940 lplpaudioptr2
, audiobytes2
,
941 writecursor
, writebytes
,
944 WARN("IDsDriverBuffer_Lock failed\n");
948 if (writecursor
+writebytes
<= device
->buflen
) {
949 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
950 *audiobytes1
= writebytes
;
952 *(LPBYTE
*)lplpaudioptr2
= NULL
;
955 TRACE("->%d.0\n",writebytes
);
957 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
958 *audiobytes1
= device
->buflen
-writecursor
;
960 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
962 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
963 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
969 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
970 LPDIRECTSOUNDBUFFER iface
,DWORD newpos
972 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
973 TRACE("(%p,%d)\n",This
,newpos
);
975 /* You cannot set the position of the primary buffer */
976 WARN("invalid call\n");
977 return DSERR_INVALIDCALL
;
980 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
981 LPDIRECTSOUNDBUFFER iface
,LONG pan
983 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
984 DirectSoundDevice
*device
= This
->device
;
986 HRESULT hres
= DS_OK
;
987 TRACE("(%p,%d)\n", iface
, pan
);
989 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
990 WARN("control unavailable\n");
991 return DSERR_CONTROLUNAVAIL
;
994 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
995 WARN("invalid parameter: pan = %d\n", pan
);
996 return DSERR_INVALIDPARAM
;
1000 EnterCriticalSection(&(device
->mixlock
));
1004 waveOutGetVolume(device
->hwo
, &factors
);
1005 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
1006 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
1007 DSOUND_AmpFactorToVolPan(&device
->volpan
);
1009 if (pan
!= device
->volpan
.lPan
) {
1010 device
->volpan
.lPan
=pan
;
1011 DSOUND_RecalcVolPan(&device
->volpan
);
1012 if (device
->hwbuf
) {
1013 hres
= IDsDriverBuffer_SetVolumePan(device
->hwbuf
, &device
->volpan
);
1015 WARN("IDsDriverBuffer_SetVolumePan failed\n");
1017 ampfactors
= (device
->volpan
.dwTotalLeftAmpFactor
& 0xffff) | (device
->volpan
.dwTotalRightAmpFactor
<< 16);
1018 waveOutSetVolume(device
->hwo
, ampfactors
);
1022 LeaveCriticalSection(&(device
->mixlock
));
1028 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
1029 LPDIRECTSOUNDBUFFER iface
,LPLONG pan
1031 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1032 DirectSoundDevice
*device
= This
->device
;
1034 TRACE("(%p,%p)\n", iface
, pan
);
1036 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
1037 WARN("control unavailable\n");
1038 return DSERR_CONTROLUNAVAIL
;
1042 WARN("invalid parameter: pan == NULL\n");
1043 return DSERR_INVALIDPARAM
;
1048 waveOutGetVolume(device
->hwo
, &factors
);
1049 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
1050 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
1051 DSOUND_AmpFactorToVolPan(&device
->volpan
);
1053 *pan
= device
->volpan
.lPan
;
1057 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
1058 LPDIRECTSOUNDBUFFER iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
1060 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1061 DirectSoundDevice
*device
= This
->device
;
1062 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
1064 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
1065 WARN("failed priority check!\n");
1066 return DSERR_PRIOLEVELNEEDED
;
1069 if (!(device
->drvdesc
.dwFlags
& DSDDESC_DONTNEEDPRIMARYLOCK
) && device
->hwbuf
) {
1072 if ((char *)p1
- (char *)device
->buffer
+ x1
> device
->buflen
)
1073 hres
= DSERR_INVALIDPARAM
;
1075 hres
= IDsDriverBuffer_Unlock(device
->hwbuf
, p1
, x1
, p2
, x2
);
1077 if (hres
!= DS_OK
) {
1078 WARN("IDsDriverBuffer_Unlock failed\n");
1086 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
1087 LPDIRECTSOUNDBUFFER iface
1089 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1090 FIXME("(%p):stub\n",This
);
1094 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
1095 LPDIRECTSOUNDBUFFER iface
,LPDWORD freq
1097 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1098 DirectSoundDevice
*device
= This
->device
;
1099 TRACE("(%p,%p)\n", iface
, freq
);
1102 WARN("invalid parameter: freq == NULL\n");
1103 return DSERR_INVALIDPARAM
;
1106 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
1107 WARN("control unavailable\n");
1108 return DSERR_CONTROLUNAVAIL
;
1111 *freq
= device
->pwfx
->nSamplesPerSec
;
1112 TRACE("-> %d\n", *freq
);
1117 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
1118 LPDIRECTSOUNDBUFFER iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
1120 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1121 WARN("(%p) already initialized\n", This
);
1122 return DSERR_ALREADYINITIALIZED
;
1125 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
1126 LPDIRECTSOUNDBUFFER iface
,LPDSBCAPS caps
1128 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1129 DirectSoundDevice
*device
= This
->device
;
1130 TRACE("(%p,%p)\n", iface
, caps
);
1133 WARN("invalid parameter: caps == NULL\n");
1134 return DSERR_INVALIDPARAM
;
1137 if (caps
->dwSize
< sizeof(*caps
)) {
1138 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
1139 return DSERR_INVALIDPARAM
;
1142 caps
->dwFlags
= This
->dsbd
.dwFlags
;
1143 caps
->dwBufferBytes
= device
->buflen
;
1145 /* Windows reports these as zero */
1146 caps
->dwUnlockTransferRate
= 0;
1147 caps
->dwPlayCpuOverhead
= 0;
1152 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
1153 LPDIRECTSOUNDBUFFER iface
,REFIID riid
,LPVOID
*ppobj
1155 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1156 DirectSoundDevice
*device
= This
->device
;
1157 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
1159 if (ppobj
== NULL
) {
1160 WARN("invalid parameter\n");
1161 return E_INVALIDARG
;
1164 *ppobj
= NULL
; /* assume failure */
1166 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
1167 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
1168 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
1173 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1174 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1175 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1176 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1177 return E_NOINTERFACE
;
1180 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1181 ERR("app requested IDirectSoundNotify on primary buffer\n");
1182 /* FIXME: should we support this? */
1183 return E_NOINTERFACE
;
1186 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1187 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1188 return E_NOINTERFACE
;
1191 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1192 if (!device
->listener
)
1193 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
1194 if (device
->listener
) {
1195 *ppobj
= device
->listener
;
1196 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
1200 WARN("IID_IDirectSound3DListener failed\n");
1201 return E_NOINTERFACE
;
1204 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1205 FIXME("app requested IKsPropertySet on primary buffer\n");
1206 return E_NOINTERFACE
;
1209 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1210 return E_NOINTERFACE
;
1213 static const IDirectSoundBufferVtbl dspbvt
=
1215 PrimaryBufferImpl_QueryInterface
,
1216 PrimaryBufferImpl_AddRef
,
1217 PrimaryBufferImpl_Release
,
1218 PrimaryBufferImpl_GetCaps
,
1219 PrimaryBufferImpl_GetCurrentPosition
,
1220 PrimaryBufferImpl_GetFormat
,
1221 PrimaryBufferImpl_GetVolume
,
1222 PrimaryBufferImpl_GetPan
,
1223 PrimaryBufferImpl_GetFrequency
,
1224 PrimaryBufferImpl_GetStatus
,
1225 PrimaryBufferImpl_Initialize
,
1226 PrimaryBufferImpl_Lock
,
1227 PrimaryBufferImpl_Play
,
1228 PrimaryBufferImpl_SetCurrentPosition
,
1229 PrimaryBufferImpl_SetFormat
,
1230 PrimaryBufferImpl_SetVolume
,
1231 PrimaryBufferImpl_SetPan
,
1232 PrimaryBufferImpl_SetFrequency
,
1233 PrimaryBufferImpl_Stop
,
1234 PrimaryBufferImpl_Unlock
,
1235 PrimaryBufferImpl_Restore
1238 HRESULT
primarybuffer_create(DirectSoundDevice
*device
, IDirectSoundBufferImpl
**ppdsb
,
1239 const DSBUFFERDESC
*dsbd
)
1241 IDirectSoundBufferImpl
*dsb
;
1242 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1244 if (dsbd
->lpwfxFormat
) {
1245 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1247 return DSERR_INVALIDPARAM
;
1250 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1253 WARN("out of memory\n");
1255 return DSERR_OUTOFMEMORY
;
1260 dsb
->device
= device
;
1261 dsb
->IDirectSoundBuffer8_iface
.lpVtbl
= (IDirectSoundBuffer8Vtbl
*)&dspbvt
;
1264 TRACE("Created primary buffer at %p\n", dsb
);
1265 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1266 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1267 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1268 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1269 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1270 device
->pwfx
->cbSize
);