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"
38 #include "dsound_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
42 /** Calculate how long a fragment length of about 10 ms should be in frames
44 * nSamplesPerSec: Frequency rate in samples per second
45 * nBlockAlign: Size of a single blockalign
48 * Size in bytes of a single fragment
50 DWORD
DSOUND_fraglen(DWORD nSamplesPerSec
, DWORD nBlockAlign
)
52 /* Given a timer delay of 10ms, the fragment size is approximately:
53 * fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
54 * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
56 * ALSA uses buffers that are powers of 2. Because of this, fraglen
57 * is rounded up to the nearest power of 2:
60 if (nSamplesPerSec
<= 12800)
61 return 128 * nBlockAlign
;
63 if (nSamplesPerSec
<= 25600)
64 return 256 * nBlockAlign
;
66 if (nSamplesPerSec
<= 51200)
67 return 512 * nBlockAlign
;
69 return 1024 * nBlockAlign
;
72 static void DSOUND_RecalcPrimary(DirectSoundDevice
*device
)
74 TRACE("(%p)\n", device
);
76 device
->fraglen
= DSOUND_fraglen(device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nBlockAlign
);
77 device
->helfrags
= device
->buflen
/ device
->fraglen
;
78 TRACE("fraglen=%d helfrags=%d\n", device
->fraglen
, device
->helfrags
);
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
);
89 waveOutClose(device
->hwo
);
91 device
->drvdesc
.dwFlags
= 0;
93 hres
= mmErr(waveOutOpen(&(device
->hwo
), device
->drvdesc
.dnDevNode
,
94 device
->pwfx
, (DWORD_PTR
)DSOUND_callback
, (DWORD_PTR
)device
,
95 CALLBACK_FUNCTION
| WAVE_MAPPED
));
97 WARN("waveOutOpen failed: %08x\n", hres
);
104 static HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
109 LPWAVEHDR headers
= NULL
;
113 TRACE("(%p)\n", device
);
115 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
116 on windows this size is always fixed (tested on win-xp) */
118 device
->buflen
= ds_hel_buflen
;
119 buflen
= device
->buflen
;
120 buflen
-= buflen
% device
->pwfx
->nBlockAlign
;
121 device
->buflen
= buflen
;
123 device
->mix_buffer_len
= DSOUND_bufpos_to_mixpos(device
, device
->buflen
);
124 device
->mix_buffer
= HeapAlloc(GetProcessHeap(), 0, device
->mix_buffer_len
);
125 if (!device
->mix_buffer
)
126 return DSERR_OUTOFMEMORY
;
128 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
129 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
131 /* Start in pause mode, to allow buffers to get filled */
132 waveOutPause(device
->hwo
);
134 TRACE("desired buflen=%d, old buffer=%p\n", buflen
, device
->buffer
);
136 /* reallocate emulated primary buffer */
138 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
, buflen
);
140 newbuf
= HeapAlloc(GetProcessHeap(),0, buflen
);
143 ERR("failed to allocate primary buffer\n");
144 return DSERR_OUTOFMEMORY
;
145 /* but the old buffer might still exist and must be re-prepared */
148 DSOUND_RecalcPrimary(device
);
150 headers
= HeapReAlloc(GetProcessHeap(),0,device
->pwave
, device
->helfrags
* sizeof(WAVEHDR
));
152 headers
= HeapAlloc(GetProcessHeap(),0,device
->helfrags
* sizeof(WAVEHDR
));
155 ERR("failed to allocate wave headers\n");
156 HeapFree(GetProcessHeap(), 0, newbuf
);
157 DSOUND_RecalcPrimary(device
);
158 return DSERR_OUTOFMEMORY
;
161 device
->buffer
= newbuf
;
162 device
->pwave
= headers
;
164 /* prepare fragment headers */
165 for (c
=0; c
<device
->helfrags
; c
++) {
166 device
->pwave
[c
].lpData
= (char*)device
->buffer
+ c
*device
->fraglen
;
167 device
->pwave
[c
].dwBufferLength
= device
->fraglen
;
168 device
->pwave
[c
].dwUser
= (DWORD_PTR
)device
;
169 device
->pwave
[c
].dwFlags
= 0;
170 device
->pwave
[c
].dwLoops
= 0;
171 err
= mmErr(waveOutPrepareHeader(device
->hwo
,&device
->pwave
[c
],sizeof(WAVEHDR
)));
174 waveOutUnprepareHeader(device
->hwo
,&device
->pwave
[c
],sizeof(WAVEHDR
));
179 overshot
= device
->buflen
% device
->fraglen
;
183 overshot
-= overshot
% device
->pwfx
->nBlockAlign
;
184 device
->pwave
[device
->helfrags
- 1].dwBufferLength
+= overshot
;
187 TRACE("fraglen=%d, overshot=%d\n", device
->fraglen
, overshot
);
189 device
->mixfunction
= mixfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
190 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
191 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
192 FillMemory(device
->mix_buffer
, device
->mix_buffer_len
, 0);
193 device
->pwplay
= device
->pwqueue
= device
->playpos
= device
->mixpos
= 0;
198 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
202 TRACE("(%p)\n", device
);
204 /* get out of CS when calling the wave system */
205 LeaveCriticalSection(&(device
->mixlock
));
207 device
->pwqueue
= (DWORD
)-1; /* resetting queues */
208 waveOutReset(device
->hwo
);
209 for (c
=0; c
<device
->helfrags
; c
++)
210 waveOutUnprepareHeader(device
->hwo
, &device
->pwave
[c
], sizeof(WAVEHDR
));
212 EnterCriticalSection(&(device
->mixlock
));
214 /* clear the queue */
218 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
221 TRACE("(%p)\n", device
);
223 device
->buflen
= ds_hel_buflen
;
224 err
= DSOUND_PrimaryOpen(device
);
227 WARN("DSOUND_PrimaryOpen failed\n");
231 device
->state
= STATE_STOPPED
;
235 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
237 TRACE("(%p)\n", device
);
240 EnterCriticalSection(&(device
->mixlock
));
242 DSOUND_PrimaryClose(device
);
243 HeapFree(GetProcessHeap(),0,device
->pwave
);
244 HeapFree(GetProcessHeap(),0,device
->pwfx
);
247 LeaveCriticalSection(&(device
->mixlock
));
253 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
256 TRACE("(%p)\n", device
);
258 err
= mmErr(waveOutRestart(device
->hwo
));
260 WARN("waveOutRestart failed\n");
265 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
268 TRACE("(%p)\n", device
);
270 /* don't call the wave system with the lock set */
271 LeaveCriticalSection(&(device
->mixlock
));
273 err
= mmErr(waveOutPause(device
->hwo
));
275 EnterCriticalSection(&(device
->mixlock
));
278 WARN("waveOutPause failed\n");
283 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
285 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
287 TRACE("pwplay=%i, pwqueue=%i\n", device
->pwplay
, device
->pwqueue
);
289 /* check if playpos was requested */
291 /* use the cached play position */
292 *playpos
= device
->pwplay
* device
->fraglen
;
294 /* check if writepos was requested */
296 /* the writepos is the first non-queued position */
297 *writepos
= ((device
->pwplay
+ device
->pwqueue
) % device
->helfrags
) * device
->fraglen
;
299 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:-1, writepos
?*writepos
:-1, device
, GetTickCount());
303 static DWORD
DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex
)
305 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
)
306 return sizeof(WAVEFORMATEX
);
308 return sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
311 LPWAVEFORMATEX
DSOUND_CopyFormat(LPCWAVEFORMATEX wfex
)
313 DWORD size
= DSOUND_GetFormatSize(wfex
);
314 LPWAVEFORMATEX pwfx
= HeapAlloc(GetProcessHeap(),0,size
);
316 WARN("out of memory\n");
317 } else if (wfex
->wFormatTag
!= WAVE_FORMAT_PCM
) {
318 CopyMemory(pwfx
, wfex
, size
);
320 CopyMemory(pwfx
, wfex
, sizeof(PCMWAVEFORMAT
));
322 if (pwfx
->nBlockAlign
!= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8) {
323 WARN("Fixing bad nBlockAlign (%u)\n", pwfx
->nBlockAlign
);
324 pwfx
->nBlockAlign
= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8;
326 if (pwfx
->nAvgBytesPerSec
!= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
) {
327 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx
->nAvgBytesPerSec
);
328 pwfx
->nAvgBytesPerSec
= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
;
334 HRESULT
primarybuffer_SetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX wfex
)
336 HRESULT err
= DSERR_BUFFERLOST
;
338 DWORD nSamplesPerSec
, bpp
, chans
;
339 LPWAVEFORMATEX oldpwfx
;
340 BOOL forced
= device
->priolevel
== DSSCL_WRITEPRIMARY
;
342 TRACE("(%p,%p)\n", device
, wfex
);
344 if (device
->priolevel
== DSSCL_NORMAL
) {
345 WARN("failed priority check!\n");
346 return DSERR_PRIOLEVELNEEDED
;
349 /* Let's be pedantic! */
351 WARN("invalid parameter: wfex==NULL!\n");
352 return DSERR_INVALIDPARAM
;
354 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
355 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
356 wfex
->wFormatTag
, wfex
->nChannels
, wfex
->nSamplesPerSec
,
357 wfex
->nAvgBytesPerSec
, wfex
->nBlockAlign
,
358 wfex
->wBitsPerSample
, wfex
->cbSize
);
361 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
362 EnterCriticalSection(&(device
->mixlock
));
364 nSamplesPerSec
= device
->pwfx
->nSamplesPerSec
;
365 bpp
= device
->pwfx
->wBitsPerSample
;
366 chans
= device
->pwfx
->nChannels
;
368 oldpwfx
= device
->pwfx
;
369 device
->pwfx
= DSOUND_CopyFormat(wfex
);
370 if (device
->pwfx
== NULL
) {
371 device
->pwfx
= oldpwfx
;
373 err
= DSERR_OUTOFMEMORY
;
377 DSOUND_PrimaryClose(device
);
379 err
= DSOUND_ReopenDevice(device
, FALSE
);
382 WARN("DSOUND_ReopenDevice failed: %08x\n", err
);
385 err
= DSOUND_PrimaryOpen(device
);
387 WARN("DSOUND_PrimaryOpen failed\n");
391 if (wfex
->nSamplesPerSec
/100 != device
->pwfx
->nSamplesPerSec
/100 && forced
&& device
->buffer
)
393 DSOUND_PrimaryClose(device
);
394 device
->pwfx
->nSamplesPerSec
= wfex
->nSamplesPerSec
;
395 err
= DSOUND_ReopenDevice(device
, TRUE
);
397 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err
);
398 else if (FAILED((err
= DSOUND_PrimaryOpen(device
))))
399 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err
);
402 device
->mix_buffer_len
= DSOUND_bufpos_to_mixpos(device
, device
->buflen
);
403 device
->mix_buffer
= HeapReAlloc(GetProcessHeap(), 0, device
->mix_buffer
, device
->mix_buffer_len
);
404 FillMemory(device
->mix_buffer
, device
->mix_buffer_len
, 0);
405 device
->mixfunction
= mixfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
406 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
408 if (nSamplesPerSec
!= device
->pwfx
->nSamplesPerSec
|| bpp
!= device
->pwfx
->wBitsPerSample
|| chans
!= device
->pwfx
->nChannels
) {
409 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
410 for (i
= 0; i
< device
->nrofbuffers
; i
++, dsb
++) {
412 RtlAcquireResourceExclusive(&(*dsb
)->lock
, TRUE
);
414 (*dsb
)->freqAdjust
= ((DWORD64
)(*dsb
)->freq
<< DSOUND_FREQSHIFT
) / device
->pwfx
->nSamplesPerSec
;
415 DSOUND_RecalcFormat((*dsb
));
416 DSOUND_MixToTemporary((*dsb
), 0, (*dsb
)->buflen
, FALSE
);
417 (*dsb
)->primary_mixpos
= 0;
419 RtlReleaseResource(&(*dsb
)->lock
);
425 LeaveCriticalSection(&(device
->mixlock
));
426 RtlReleaseResource(&(device
->buffer_list_lock
));
429 HeapFree(GetProcessHeap(), 0, oldpwfx
);
433 /*******************************************************************************
436 static inline IDirectSoundBufferImpl
*impl_from_IDirectSoundBuffer(IDirectSoundBuffer
*iface
)
438 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
439 return CONTAINING_RECORD(iface
, IDirectSoundBufferImpl
, IDirectSoundBuffer8_iface
);
442 /* This sets this format for the <em>Primary Buffer Only</em> */
443 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
444 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(
445 LPDIRECTSOUNDBUFFER iface
,
446 LPCWAVEFORMATEX wfex
)
448 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
449 TRACE("(%p,%p)\n", iface
, wfex
);
450 return primarybuffer_SetFormat(This
->device
, wfex
);
453 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(
454 LPDIRECTSOUNDBUFFER iface
,LONG vol
456 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
457 DirectSoundDevice
*device
= This
->device
;
459 HRESULT hres
= DS_OK
;
460 TRACE("(%p,%d)\n", iface
, vol
);
462 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
463 WARN("control unavailable\n");
464 return DSERR_CONTROLUNAVAIL
;
467 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
468 WARN("invalid parameter: vol = %d\n", vol
);
469 return DSERR_INVALIDPARAM
;
473 EnterCriticalSection(&(device
->mixlock
));
475 waveOutGetVolume(device
->hwo
, &factors
);
476 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
477 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
478 DSOUND_AmpFactorToVolPan(&device
->volpan
);
479 if (vol
!= device
->volpan
.lVolume
) {
480 device
->volpan
.lVolume
=vol
;
481 DSOUND_RecalcVolPan(&device
->volpan
);
482 ampfactors
= (device
->volpan
.dwTotalLeftAmpFactor
& 0xffff) | (device
->volpan
.dwTotalRightAmpFactor
<< 16);
483 waveOutSetVolume(device
->hwo
, ampfactors
);
486 LeaveCriticalSection(&(device
->mixlock
));
492 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(
493 LPDIRECTSOUNDBUFFER iface
,LPLONG vol
495 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
496 DirectSoundDevice
*device
= This
->device
;
498 TRACE("(%p,%p)\n", iface
, vol
);
500 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
501 WARN("control unavailable\n");
502 return DSERR_CONTROLUNAVAIL
;
506 WARN("invalid parameter: vol = NULL\n");
507 return DSERR_INVALIDPARAM
;
510 waveOutGetVolume(device
->hwo
, &factors
);
511 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
512 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
513 DSOUND_AmpFactorToVolPan(&device
->volpan
);
514 *vol
= device
->volpan
.lVolume
;
519 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(
520 LPDIRECTSOUNDBUFFER iface
,DWORD freq
522 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
523 TRACE("(%p,%d)\n",This
,freq
);
525 /* You cannot set the frequency of the primary buffer */
526 WARN("control unavailable\n");
527 return DSERR_CONTROLUNAVAIL
;
530 static HRESULT WINAPI
PrimaryBufferImpl_Play(
531 LPDIRECTSOUNDBUFFER iface
,DWORD reserved1
,DWORD reserved2
,DWORD flags
533 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
534 DirectSoundDevice
*device
= This
->device
;
535 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
537 if (!(flags
& DSBPLAY_LOOPING
)) {
538 WARN("invalid parameter: flags = %08x\n", flags
);
539 return DSERR_INVALIDPARAM
;
543 EnterCriticalSection(&(device
->mixlock
));
545 if (device
->state
== STATE_STOPPED
)
546 device
->state
= STATE_STARTING
;
547 else if (device
->state
== STATE_STOPPING
)
548 device
->state
= STATE_PLAYING
;
550 LeaveCriticalSection(&(device
->mixlock
));
556 static HRESULT WINAPI
PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface
)
558 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
559 DirectSoundDevice
*device
= This
->device
;
560 TRACE("(%p)\n", iface
);
563 EnterCriticalSection(&(device
->mixlock
));
565 if (device
->state
== STATE_PLAYING
)
566 device
->state
= STATE_STOPPING
;
567 else if (device
->state
== STATE_STARTING
)
568 device
->state
= STATE_STOPPED
;
570 LeaveCriticalSection(&(device
->mixlock
));
576 static ULONG WINAPI
PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface
)
578 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
579 ULONG ref
= InterlockedIncrement(&(This
->ref
));
580 TRACE("(%p) ref was %d\n", This
, ref
- 1);
582 InterlockedIncrement(&This
->numIfaces
);
586 void primarybuffer_destroy(IDirectSoundBufferImpl
*This
)
588 This
->device
->primary
= NULL
;
589 HeapFree(GetProcessHeap(), 0, This
);
590 TRACE("(%p) released\n", This
);
593 static ULONG WINAPI
PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface
)
595 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
596 DWORD ref
= InterlockedDecrement(&(This
->ref
));
597 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
599 if (!ref
&& !InterlockedDecrement(&This
->numIfaces
))
600 primarybuffer_destroy(This
);
604 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(
605 LPDIRECTSOUNDBUFFER iface
,LPDWORD playpos
,LPDWORD writepos
608 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
609 DirectSoundDevice
*device
= This
->device
;
610 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
613 EnterCriticalSection(&(device
->mixlock
));
615 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
617 WARN("DSOUND_PrimaryGetPosition failed\n");
618 LeaveCriticalSection(&(device
->mixlock
));
622 if (device
->state
!= STATE_STOPPED
)
623 /* apply the documented 10ms lead to writepos */
624 *writepos
+= device
->writelead
;
625 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
628 LeaveCriticalSection(&(device
->mixlock
));
631 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
635 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(
636 LPDIRECTSOUNDBUFFER iface
,LPDWORD status
638 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
639 DirectSoundDevice
*device
= This
->device
;
640 TRACE("(%p,%p)\n", iface
, status
);
642 if (status
== NULL
) {
643 WARN("invalid parameter: status == NULL\n");
644 return DSERR_INVALIDPARAM
;
648 if ((device
->state
== STATE_STARTING
) ||
649 (device
->state
== STATE_PLAYING
))
650 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
652 TRACE("status=%x\n", *status
);
657 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(
658 LPDIRECTSOUNDBUFFER iface
,
664 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
665 DirectSoundDevice
*device
= This
->device
;
666 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
668 size
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
670 if (lpwf
) { /* NULL is valid */
671 if (wfsize
>= size
) {
672 CopyMemory(lpwf
,device
->pwfx
,size
);
676 WARN("invalid parameter: wfsize too small\n");
679 return DSERR_INVALIDPARAM
;
683 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->pwfx
->cbSize
;
685 WARN("invalid parameter: wfwritten == NULL\n");
686 return DSERR_INVALIDPARAM
;
693 static HRESULT WINAPI
PrimaryBufferImpl_Lock(
694 LPDIRECTSOUNDBUFFER iface
,DWORD writecursor
,DWORD writebytes
,LPVOID
*lplpaudioptr1
,LPDWORD audiobytes1
,LPVOID
*lplpaudioptr2
,LPDWORD audiobytes2
,DWORD flags
697 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
698 DirectSoundDevice
*device
= This
->device
;
699 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
712 return DSERR_INVALIDPARAM
;
714 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
715 WARN("failed priority check!\n");
716 return DSERR_PRIOLEVELNEEDED
;
719 /* when this flag is set, writecursor is meaningless and must be calculated */
720 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
721 /* GetCurrentPosition does too much magic to duplicate here */
722 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
724 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
729 /* when this flag is set, writebytes is meaningless and must be set */
730 if (flags
& DSBLOCK_ENTIREBUFFER
)
731 writebytes
= device
->buflen
;
733 if (writecursor
>= device
->buflen
) {
734 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
735 writecursor
, device
->buflen
);
736 return DSERR_INVALIDPARAM
;
739 if (writebytes
> device
->buflen
) {
740 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
741 writebytes
, device
->buflen
);
742 return DSERR_INVALIDPARAM
;
745 if (writecursor
+writebytes
<= device
->buflen
) {
746 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
747 *audiobytes1
= writebytes
;
749 *(LPBYTE
*)lplpaudioptr2
= NULL
;
752 TRACE("->%d.0\n",writebytes
);
754 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
755 *audiobytes1
= device
->buflen
-writecursor
;
757 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
759 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
760 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
765 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(
766 LPDIRECTSOUNDBUFFER iface
,DWORD newpos
768 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
769 TRACE("(%p,%d)\n",This
,newpos
);
771 /* You cannot set the position of the primary buffer */
772 WARN("invalid call\n");
773 return DSERR_INVALIDCALL
;
776 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(
777 LPDIRECTSOUNDBUFFER iface
,LONG pan
779 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
780 DirectSoundDevice
*device
= This
->device
;
782 HRESULT hres
= DS_OK
;
783 TRACE("(%p,%d)\n", iface
, pan
);
785 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
786 WARN("control unavailable\n");
787 return DSERR_CONTROLUNAVAIL
;
790 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
791 WARN("invalid parameter: pan = %d\n", pan
);
792 return DSERR_INVALIDPARAM
;
796 EnterCriticalSection(&(device
->mixlock
));
798 waveOutGetVolume(device
->hwo
, &factors
);
799 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
800 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
801 DSOUND_AmpFactorToVolPan(&device
->volpan
);
802 if (pan
!= device
->volpan
.lPan
) {
803 device
->volpan
.lPan
=pan
;
804 DSOUND_RecalcVolPan(&device
->volpan
);
805 ampfactors
= (device
->volpan
.dwTotalLeftAmpFactor
& 0xffff) | (device
->volpan
.dwTotalRightAmpFactor
<< 16);
806 waveOutSetVolume(device
->hwo
, ampfactors
);
809 LeaveCriticalSection(&(device
->mixlock
));
815 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(
816 LPDIRECTSOUNDBUFFER iface
,LPLONG pan
818 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
819 DirectSoundDevice
*device
= This
->device
;
821 TRACE("(%p,%p)\n", iface
, pan
);
823 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
824 WARN("control unavailable\n");
825 return DSERR_CONTROLUNAVAIL
;
829 WARN("invalid parameter: pan == NULL\n");
830 return DSERR_INVALIDPARAM
;
833 waveOutGetVolume(device
->hwo
, &factors
);
834 device
->volpan
.dwTotalLeftAmpFactor
=ampfactors
& 0xffff;
835 device
->volpan
.dwTotalRightAmpFactor
=ampfactors
>> 16;
836 DSOUND_AmpFactorToVolPan(&device
->volpan
);
837 *pan
= device
->volpan
.lPan
;
841 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(
842 LPDIRECTSOUNDBUFFER iface
,LPVOID p1
,DWORD x1
,LPVOID p2
,DWORD x2
844 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
845 DirectSoundDevice
*device
= This
->device
;
846 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
848 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
849 WARN("failed priority check!\n");
850 return DSERR_PRIOLEVELNEEDED
;
856 static HRESULT WINAPI
PrimaryBufferImpl_Restore(
857 LPDIRECTSOUNDBUFFER iface
859 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
860 FIXME("(%p):stub\n",This
);
864 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(
865 LPDIRECTSOUNDBUFFER iface
,LPDWORD freq
867 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
868 DirectSoundDevice
*device
= This
->device
;
869 TRACE("(%p,%p)\n", iface
, freq
);
872 WARN("invalid parameter: freq == NULL\n");
873 return DSERR_INVALIDPARAM
;
876 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
877 WARN("control unavailable\n");
878 return DSERR_CONTROLUNAVAIL
;
881 *freq
= device
->pwfx
->nSamplesPerSec
;
882 TRACE("-> %d\n", *freq
);
887 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(
888 LPDIRECTSOUNDBUFFER iface
,LPDIRECTSOUND dsound
,LPCDSBUFFERDESC dbsd
890 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
891 WARN("(%p) already initialized\n", This
);
892 return DSERR_ALREADYINITIALIZED
;
895 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(
896 LPDIRECTSOUNDBUFFER iface
,LPDSBCAPS caps
898 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
899 DirectSoundDevice
*device
= This
->device
;
900 TRACE("(%p,%p)\n", iface
, caps
);
903 WARN("invalid parameter: caps == NULL\n");
904 return DSERR_INVALIDPARAM
;
907 if (caps
->dwSize
< sizeof(*caps
)) {
908 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
909 return DSERR_INVALIDPARAM
;
912 caps
->dwFlags
= This
->dsbd
.dwFlags
;
913 caps
->dwBufferBytes
= device
->buflen
;
915 /* Windows reports these as zero */
916 caps
->dwUnlockTransferRate
= 0;
917 caps
->dwPlayCpuOverhead
= 0;
922 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(
923 LPDIRECTSOUNDBUFFER iface
,REFIID riid
,LPVOID
*ppobj
925 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
926 DirectSoundDevice
*device
= This
->device
;
927 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
930 WARN("invalid parameter\n");
934 *ppobj
= NULL
; /* assume failure */
936 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
937 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
938 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER
)This
);
943 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
944 /* a primary buffer can't have a DirectSoundBuffer8 interface */
945 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
946 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
947 return E_NOINTERFACE
;
950 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
951 ERR("app requested IDirectSoundNotify on primary buffer\n");
952 /* FIXME: should we support this? */
953 return E_NOINTERFACE
;
956 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
957 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
958 return E_NOINTERFACE
;
961 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
962 if (!device
->listener
)
963 IDirectSound3DListenerImpl_Create(device
, &device
->listener
);
964 if (device
->listener
) {
965 *ppobj
= device
->listener
;
966 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER
)*ppobj
);
970 WARN("IID_IDirectSound3DListener failed\n");
971 return E_NOINTERFACE
;
974 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
975 FIXME("app requested IKsPropertySet on primary buffer\n");
976 return E_NOINTERFACE
;
979 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
980 return E_NOINTERFACE
;
983 static const IDirectSoundBufferVtbl dspbvt
=
985 PrimaryBufferImpl_QueryInterface
,
986 PrimaryBufferImpl_AddRef
,
987 PrimaryBufferImpl_Release
,
988 PrimaryBufferImpl_GetCaps
,
989 PrimaryBufferImpl_GetCurrentPosition
,
990 PrimaryBufferImpl_GetFormat
,
991 PrimaryBufferImpl_GetVolume
,
992 PrimaryBufferImpl_GetPan
,
993 PrimaryBufferImpl_GetFrequency
,
994 PrimaryBufferImpl_GetStatus
,
995 PrimaryBufferImpl_Initialize
,
996 PrimaryBufferImpl_Lock
,
997 PrimaryBufferImpl_Play
,
998 PrimaryBufferImpl_SetCurrentPosition
,
999 PrimaryBufferImpl_SetFormat
,
1000 PrimaryBufferImpl_SetVolume
,
1001 PrimaryBufferImpl_SetPan
,
1002 PrimaryBufferImpl_SetFrequency
,
1003 PrimaryBufferImpl_Stop
,
1004 PrimaryBufferImpl_Unlock
,
1005 PrimaryBufferImpl_Restore
1008 HRESULT
primarybuffer_create(DirectSoundDevice
*device
, IDirectSoundBufferImpl
**ppdsb
,
1009 const DSBUFFERDESC
*dsbd
)
1011 IDirectSoundBufferImpl
*dsb
;
1012 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1014 if (dsbd
->lpwfxFormat
) {
1015 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1017 return DSERR_INVALIDPARAM
;
1020 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1023 WARN("out of memory\n");
1025 return DSERR_OUTOFMEMORY
;
1030 dsb
->device
= device
;
1031 dsb
->IDirectSoundBuffer8_iface
.lpVtbl
= (IDirectSoundBuffer8Vtbl
*)&dspbvt
;
1034 TRACE("Created primary buffer at %p\n", dsb
);
1035 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1036 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1037 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1038 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1039 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1040 device
->pwfx
->cbSize
);