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.
29 #define NONAMELESSUNION
37 #include "wine/debug.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
43 static DWORD
speaker_config_to_channel_mask(DWORD speaker_config
)
45 switch (DSSPEAKER_CONFIG(speaker_config
)) {
47 return SPEAKER_FRONT_LEFT
;
49 case DSSPEAKER_STEREO
:
50 case DSSPEAKER_HEADPHONE
:
51 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
;
54 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
| SPEAKER_BACK_RIGHT
;
56 case DSSPEAKER_5POINT1_BACK
:
57 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_FRONT_CENTER
| SPEAKER_LOW_FREQUENCY
| SPEAKER_BACK_LEFT
| SPEAKER_BACK_RIGHT
;
60 WARN("unknown speaker_config %u\n", speaker_config
);
61 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
;
64 static DWORD
DSOUND_FindSpeakerConfig(IMMDevice
*mmdevice
, int channels
)
66 IPropertyStore
*store
;
71 const DWORD def
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, DSSPEAKER_GEOMETRY_WIDE
);
73 hr
= IMMDevice_OpenPropertyStore(mmdevice
, STGM_READ
, &store
);
75 WARN("IMMDevice_OpenPropertyStore failed: %08x\n", hr
);
79 hr
= IPropertyStore_GetValue(store
, &PKEY_AudioEndpoint_PhysicalSpeakers
, &pv
);
82 WARN("IPropertyStore_GetValue failed: %08x\n", hr
);
83 IPropertyStore_Release(store
);
87 if (pv
.vt
!= VT_UI4
) {
88 WARN("PKEY_AudioEndpoint_PhysicalSpeakers is not a ULONG: 0x%x\n", pv
.vt
);
89 PropVariantClear(&pv
);
90 IPropertyStore_Release(store
);
94 phys_speakers
= pv
.u
.ulVal
;
96 PropVariantClear(&pv
);
97 IPropertyStore_Release(store
);
99 if ((channels
>= 6 || channels
== 0) && (phys_speakers
& KSAUDIO_SPEAKER_5POINT1
) == KSAUDIO_SPEAKER_5POINT1
)
100 return DSSPEAKER_5POINT1_BACK
;
101 else if ((channels
>= 6 || channels
== 0) && (phys_speakers
& KSAUDIO_SPEAKER_5POINT1_SURROUND
) == KSAUDIO_SPEAKER_5POINT1_SURROUND
)
102 return DSSPEAKER_5POINT1_SURROUND
;
103 else if ((channels
>= 4 || channels
== 0) && (phys_speakers
& KSAUDIO_SPEAKER_QUAD
) == KSAUDIO_SPEAKER_QUAD
)
104 return DSSPEAKER_QUAD
;
105 else if ((channels
>= 2 || channels
== 0) && (phys_speakers
& KSAUDIO_SPEAKER_STEREO
) == KSAUDIO_SPEAKER_STEREO
)
106 return DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, DSSPEAKER_GEOMETRY_WIDE
);
107 else if ((phys_speakers
& KSAUDIO_SPEAKER_MONO
) == KSAUDIO_SPEAKER_MONO
)
108 return DSSPEAKER_MONO
;
113 static HRESULT
DSOUND_WaveFormat(DirectSoundDevice
*device
, IAudioClient
*client
,
114 BOOL forcewave
, WAVEFORMATEX
**wfx
)
116 WAVEFORMATEXTENSIBLE
*retwfe
= NULL
;
121 WAVEFORMATEXTENSIBLE
*mixwfe
;
122 hr
= IAudioClient_GetMixFormat(client
, (WAVEFORMATEX
**)&mixwfe
);
127 if (mixwfe
->Format
.nChannels
< device
->num_speakers
) {
128 device
->speaker_config
= DSOUND_FindSpeakerConfig(device
->mmdevice
, mixwfe
->Format
.nChannels
);
129 DSOUND_ParseSpeakerConfig(device
);
130 } else if (mixwfe
->Format
.nChannels
> device
->num_speakers
) {
131 mixwfe
->Format
.nChannels
= device
->num_speakers
;
132 mixwfe
->Format
.nBlockAlign
= mixwfe
->Format
.nChannels
* mixwfe
->Format
.wBitsPerSample
/ 8;
133 mixwfe
->Format
.nAvgBytesPerSec
= mixwfe
->Format
.nSamplesPerSec
* mixwfe
->Format
.nBlockAlign
;
134 mixwfe
->dwChannelMask
= speaker_config_to_channel_mask(device
->speaker_config
);
137 if (!IsEqualGUID(&mixwfe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)) {
138 WAVEFORMATEXTENSIBLE testwfe
= *mixwfe
;
140 testwfe
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
141 testwfe
.Samples
.wValidBitsPerSample
= testwfe
.Format
.wBitsPerSample
= 32;
142 testwfe
.Format
.nBlockAlign
= testwfe
.Format
.nChannels
* testwfe
.Format
.wBitsPerSample
/ 8;
143 testwfe
.Format
.nAvgBytesPerSec
= testwfe
.Format
.nSamplesPerSec
* testwfe
.Format
.nBlockAlign
;
145 if (FAILED(IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
, &testwfe
.Format
, (WAVEFORMATEX
**)&retwfe
)))
146 w
= DSOUND_CopyFormat(&mixwfe
->Format
);
148 w
= DSOUND_CopyFormat(&retwfe
->Format
);
150 w
= DSOUND_CopyFormat(&testwfe
.Format
);
151 CoTaskMemFree(retwfe
);
154 w
= DSOUND_CopyFormat(&mixwfe
->Format
);
155 CoTaskMemFree(mixwfe
);
156 } else if (device
->primary_pwfx
->wFormatTag
== WAVE_FORMAT_PCM
||
157 device
->primary_pwfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
158 WAVEFORMATEX
*wi
= device
->primary_pwfx
;
159 WAVEFORMATEXTENSIBLE
*wfe
;
161 /* Convert to WAVEFORMATEXTENSIBLE */
162 w
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE
));
163 wfe
= (WAVEFORMATEXTENSIBLE
*)w
;
165 return DSERR_OUTOFMEMORY
;
168 w
->wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
169 w
->cbSize
= sizeof(*wfe
) - sizeof(*w
);
170 w
->nBlockAlign
= w
->nChannels
* w
->wBitsPerSample
/ 8;
171 w
->nAvgBytesPerSec
= w
->nSamplesPerSec
* w
->nBlockAlign
;
173 wfe
->dwChannelMask
= 0;
174 if (wi
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
175 w
->wBitsPerSample
= 32;
176 wfe
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
178 wfe
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
179 wfe
->Samples
.wValidBitsPerSample
= w
->wBitsPerSample
;
181 w
= DSOUND_CopyFormat(device
->primary_pwfx
);
184 return DSERR_OUTOFMEMORY
;
186 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
, w
, (WAVEFORMATEX
**)&retwfe
);
188 memcpy(w
, retwfe
, sizeof(WAVEFORMATEX
) + retwfe
->Format
.cbSize
);
189 CoTaskMemFree(retwfe
);
192 WARN("IsFormatSupported failed: %08x\n", hr
);
193 HeapFree(GetProcessHeap(), 0, w
);
200 static void DSOUND_ReleaseDevice(DirectSoundDevice
*device
)
203 IAudioClient_Release(device
->client
);
204 device
->client
= NULL
;
207 IAudioRenderClient_Release(device
->render
);
208 device
->render
= NULL
;
211 IAudioStreamVolume_Release(device
->volume
);
212 device
->volume
= NULL
;
216 device
->playpos
+= device
->pad
;
217 device
->playpos
%= device
->buflen
;
222 static HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
, WAVEFORMATEX
*wfx
, DWORD frames
, BOOL forcewave
)
224 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
227 BOOL mixfloat
= FALSE
;
230 TRACE("(%p)\n", device
);
232 new_buflen
= device
->buflen
;
233 new_buflen
-= new_buflen
% wfx
->nBlockAlign
;
235 if (wfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
236 (wfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
237 IsEqualGUID(&((WAVEFORMATEXTENSIBLE
*)wfx
)->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)))
240 /* reallocate emulated primary buffer */
243 newbuf
= HeapReAlloc(GetProcessHeap(), 0, device
->buffer
, new_buflen
);
245 newbuf
= HeapAlloc(GetProcessHeap(), 0, new_buflen
);
248 ERR("failed to allocate primary buffer\n");
249 return DSERR_OUTOFMEMORY
;
251 FillMemory(newbuf
, new_buflen
, (wfx
->wBitsPerSample
== 8) ? 128 : 0);
252 } else if (!mixfloat
) {
253 DWORD alloc_len
= frames
* sizeof(float);
256 newbuf
= HeapReAlloc(GetProcessHeap(), 0, device
->buffer
, alloc_len
);
258 newbuf
= HeapAlloc(GetProcessHeap(), 0, alloc_len
);
261 ERR("failed to allocate primary buffer\n");
262 return DSERR_OUTOFMEMORY
;
264 FillMemory(newbuf
, alloc_len
, (wfx
->wBitsPerSample
== 8) ? 128 : 0);
266 HeapFree(GetProcessHeap(), 0, device
->buffer
);
270 device
->buffer
= newbuf
;
271 device
->buflen
= new_buflen
;
272 HeapFree(GetProcessHeap(), 0, device
->pwfx
);
275 device
->writelead
= (wfx
->nSamplesPerSec
/ 100) * wfx
->nBlockAlign
;
277 TRACE("buflen: %u, fraglen: %u\n", device
->buflen
, device
->fraglen
);
280 device
->normfunction
= normfunctions
[wfx
->wBitsPerSample
/8 - 1];
282 device
->normfunction
= NULL
;
286 for (i
= 0; i
< device
->nrofbuffers
; i
++) {
287 RtlAcquireResourceExclusive(&dsb
[i
]->lock
, TRUE
);
288 DSOUND_RecalcFormat(dsb
[i
]);
289 RtlReleaseResource(&dsb
[i
]->lock
);
295 HRESULT
DSOUND_ReopenDevice(DirectSoundDevice
*device
, BOOL forcewave
)
298 REFERENCE_TIME period
;
301 IAudioClient
*client
= NULL
;
302 IAudioRenderClient
*render
= NULL
;
303 IAudioStreamVolume
*volume
= NULL
;
305 WAVEFORMATEX
*wfx
= NULL
;
306 DWORD oldspeakerconfig
= device
->speaker_config
;
308 TRACE("(%p, %d)\n", device
, forcewave
);
310 hres
= IMMDevice_Activate(device
->mmdevice
, &IID_IAudioClient
,
311 CLSCTX_INPROC_SERVER
, NULL
, (void **)&client
);
313 WARN("Activate failed: %08x\n", hres
);
317 hres
= DSOUND_WaveFormat(device
, client
, forcewave
, &wfx
);
319 IAudioClient_Release(client
);
323 hres
= IAudioClient_Initialize(client
,
324 AUDCLNT_SHAREMODE_SHARED
, AUDCLNT_STREAMFLAGS_NOPERSIST
|
325 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
, 800000, 0, wfx
, NULL
);
327 IAudioClient_Release(client
);
328 ERR("Initialize failed: %08x\n", hres
);
332 IAudioClient_SetEventHandle(client
, device
->sleepev
);
334 hres
= IAudioClient_GetService(client
, &IID_IAudioRenderClient
, (void**)&render
);
338 hres
= IAudioClient_GetService(client
, &IID_IAudioStreamVolume
, (void**)&volume
);
342 /* Now kick off the timer so the event fires periodically */
343 hres
= IAudioClient_Start(client
);
345 WARN("Start failed with %08x\n", hres
);
348 hres
= IAudioClient_GetStreamLatency(client
, &period
);
350 WARN("GetStreamLatency failed with %08x\n", hres
);
353 hres
= IAudioClient_GetBufferSize(client
, &frames
);
355 WARN("GetBufferSize failed with %08x\n", hres
);
359 period_ms
= (period
+ 9999) / 10000;
360 fraglen
= MulDiv(wfx
->nSamplesPerSec
, period
, 10000000) * wfx
->nBlockAlign
;
361 TRACE("period %u ms fraglen %u buflen %u\n", period_ms
, fraglen
, frames
* wfx
->nBlockAlign
);
363 hres
= DSOUND_PrimaryOpen(device
, wfx
, frames
, forcewave
);
367 DSOUND_ReleaseDevice(device
);
368 device
->client
= client
;
369 device
->render
= render
;
370 device
->volume
= volume
;
371 device
->fraglen
= fraglen
;
372 device
->aclen
= frames
* wfx
->nBlockAlign
;
375 device
->sleeptime
= 5;
377 device
->sleeptime
= period_ms
* 5 / 2;
382 WARN("GetService failed: %08x\n", hres
);
384 device
->speaker_config
= oldspeakerconfig
;
385 DSOUND_ParseSpeakerConfig(device
);
387 IAudioStreamVolume_Release(volume
);
389 IAudioRenderClient_Release(render
);
390 IAudioClient_Release(client
);
391 HeapFree(GetProcessHeap(), 0, wfx
);
395 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
397 TRACE("(%p)\n", device
);
400 EnterCriticalSection(&(device
->mixlock
));
402 if(device
->primary
&& (device
->primary
->ref
|| device
->primary
->numIfaces
))
403 WARN("Destroying primary buffer while references held (%u %u)\n", device
->primary
->ref
, device
->primary
->numIfaces
);
405 HeapFree(GetProcessHeap(), 0, device
->primary
);
406 device
->primary
= NULL
;
408 HeapFree(GetProcessHeap(),0,device
->primary_pwfx
);
409 HeapFree(GetProcessHeap(),0,device
->pwfx
);
412 LeaveCriticalSection(&(device
->mixlock
));
418 WAVEFORMATEX
*DSOUND_CopyFormat(const WAVEFORMATEX
*wfex
)
421 if(wfex
->wFormatTag
== WAVE_FORMAT_PCM
){
422 pwfx
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX
));
425 CopyMemory(pwfx
, wfex
, sizeof(PCMWAVEFORMAT
));
428 pwfx
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX
) + wfex
->cbSize
);
431 CopyMemory(pwfx
, wfex
, sizeof(WAVEFORMATEX
) + wfex
->cbSize
);
434 if(pwfx
->wFormatTag
== WAVE_FORMAT_PCM
||
435 (pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
436 IsEqualGUID(&((const WAVEFORMATEXTENSIBLE
*)pwfx
)->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
)))
437 pwfx
->nBlockAlign
= (pwfx
->nChannels
* pwfx
->wBitsPerSample
) / 8;
442 HRESULT
primarybuffer_SetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX passed_fmt
)
445 WAVEFORMATEX
*old_fmt
;
446 WAVEFORMATEXTENSIBLE
*fmtex
, *passed_fmtex
= (WAVEFORMATEXTENSIBLE
*)passed_fmt
;
448 TRACE("(%p,%p)\n", device
, passed_fmt
);
450 if (device
->priolevel
== DSSCL_NORMAL
) {
451 WARN("failed priority check!\n");
452 return DSERR_PRIOLEVELNEEDED
;
455 /* Let's be pedantic! */
456 if (passed_fmt
== NULL
) {
457 WARN("invalid parameter: passed_fmt==NULL!\n");
458 return DSERR_INVALIDPARAM
;
460 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
461 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
462 passed_fmt
->wFormatTag
, passed_fmt
->nChannels
, passed_fmt
->nSamplesPerSec
,
463 passed_fmt
->nAvgBytesPerSec
, passed_fmt
->nBlockAlign
,
464 passed_fmt
->wBitsPerSample
, passed_fmt
->cbSize
);
466 if(passed_fmt
->wBitsPerSample
< 8 || passed_fmt
->wBitsPerSample
% 8 != 0 ||
467 passed_fmt
->nChannels
== 0 || passed_fmt
->nSamplesPerSec
== 0 ||
468 passed_fmt
->nAvgBytesPerSec
== 0 ||
469 passed_fmt
->nBlockAlign
!= passed_fmt
->nChannels
* passed_fmt
->wBitsPerSample
/ 8)
470 return DSERR_INVALIDPARAM
;
472 if(passed_fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
473 if(passed_fmtex
->Samples
.wValidBitsPerSample
> passed_fmtex
->Format
.wBitsPerSample
)
474 return DSERR_INVALIDPARAM
;
478 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
479 EnterCriticalSection(&(device
->mixlock
));
481 if (device
->priolevel
== DSSCL_WRITEPRIMARY
) {
482 old_fmt
= device
->primary_pwfx
;
483 device
->primary_pwfx
= DSOUND_CopyFormat(passed_fmt
);
484 fmtex
= (WAVEFORMATEXTENSIBLE
*)device
->primary_pwfx
;
485 if (device
->primary_pwfx
== NULL
) {
486 err
= DSERR_OUTOFMEMORY
;
490 if (fmtex
->Format
.wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
491 fmtex
->Samples
.wValidBitsPerSample
== 0) {
492 TRACE("Correcting 0 valid bits per sample\n");
493 fmtex
->Samples
.wValidBitsPerSample
= fmtex
->Format
.wBitsPerSample
;
496 err
= DSOUND_ReopenDevice(device
, TRUE
);
498 ERR("No formats could be opened\n");
499 HeapFree(GetProcessHeap(), 0, device
->primary_pwfx
);
500 device
->primary_pwfx
= old_fmt
;
502 HeapFree(GetProcessHeap(), 0, old_fmt
);
504 WAVEFORMATEX
*wfx
= DSOUND_CopyFormat(passed_fmt
);
506 HeapFree(GetProcessHeap(), 0, device
->primary_pwfx
);
507 device
->primary_pwfx
= wfx
;
509 err
= DSERR_OUTOFMEMORY
;
513 LeaveCriticalSection(&(device
->mixlock
));
514 RtlReleaseResource(&(device
->buffer_list_lock
));
520 /*******************************************************************************
523 static inline IDirectSoundBufferImpl
*impl_from_IDirectSoundBuffer(IDirectSoundBuffer
*iface
)
525 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
526 return CONTAINING_RECORD(iface
, IDirectSoundBufferImpl
, IDirectSoundBuffer8_iface
);
529 /* This sets this format for the primary buffer only */
530 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(IDirectSoundBuffer
*iface
,
531 const WAVEFORMATEX
*wfex
)
533 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
534 TRACE("(%p,%p)\n", iface
, wfex
);
535 return primarybuffer_SetFormat(This
->device
, wfex
);
538 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(IDirectSoundBuffer
*iface
, LONG vol
)
540 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
541 DirectSoundDevice
*device
= This
->device
;
546 TRACE("(%p,%d)\n", iface
, vol
);
548 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
549 WARN("control unavailable\n");
550 return DSERR_CONTROLUNAVAIL
;
553 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
554 WARN("invalid parameter: vol = %d\n", vol
);
555 return DSERR_INVALIDPARAM
;
559 EnterCriticalSection(&device
->mixlock
);
561 for (i
= 0; i
< DS_MAX_CHANNELS
; i
++) {
562 if (device
->pwfx
->nChannels
> i
){
563 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, i
, &fvol
);
565 LeaveCriticalSection(&device
->mixlock
);
566 WARN("GetChannelVolume failed: %08x\n", hr
);
572 device
->volpan
.dwTotalAmpFactor
[i
]=((UINT16
)(fvol
* (DWORD
)0xFFFF));
575 DSOUND_AmpFactorToVolPan(&device
->volpan
);
576 if (vol
!= device
->volpan
.lVolume
) {
577 device
->volpan
.lVolume
=vol
;
578 DSOUND_RecalcVolPan(&device
->volpan
);
580 for (i
= 0; i
< DS_MAX_CHANNELS
; i
++) {
581 if (device
->pwfx
->nChannels
> i
){
582 fvol
= (float)((DWORD
)(device
->volpan
.dwTotalAmpFactor
[i
] & 0xFFFF) / (float)0xFFFF);
583 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, i
, fvol
);
585 LeaveCriticalSection(&device
->mixlock
);
586 WARN("SetChannelVolume failed: %08x\n", hr
);
593 LeaveCriticalSection(&(device
->mixlock
));
599 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(IDirectSoundBuffer
*iface
, LONG
*vol
)
601 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
602 DirectSoundDevice
*device
= This
->device
;
607 TRACE("(%p,%p)\n", iface
, vol
);
609 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
610 WARN("control unavailable\n");
611 return DSERR_CONTROLUNAVAIL
;
615 WARN("invalid parameter: vol = NULL\n");
616 return DSERR_INVALIDPARAM
;
619 EnterCriticalSection(&device
->mixlock
);
621 for (i
= 0; i
< DS_MAX_CHANNELS
; i
++) {
622 if (device
->pwfx
->nChannels
> i
){
623 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, i
, &fvol
);
625 LeaveCriticalSection(&device
->mixlock
);
626 WARN("GetChannelVolume failed: %08x\n", hr
);
632 device
->volpan
.dwTotalAmpFactor
[i
] = ((UINT16
)(fvol
* (DWORD
)0xFFFF));
635 DSOUND_AmpFactorToVolPan(&device
->volpan
);
636 *vol
= device
->volpan
.lVolume
;
638 LeaveCriticalSection(&device
->mixlock
);
643 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer
*iface
, DWORD freq
)
645 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
646 TRACE("(%p,%d)\n",This
,freq
);
648 /* You cannot set the frequency of the primary buffer */
649 WARN("control unavailable\n");
650 return DSERR_CONTROLUNAVAIL
;
653 static HRESULT WINAPI
PrimaryBufferImpl_Play(IDirectSoundBuffer
*iface
, DWORD reserved1
,
654 DWORD reserved2
, DWORD flags
)
656 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
657 DirectSoundDevice
*device
= This
->device
;
658 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
660 if (!(flags
& DSBPLAY_LOOPING
)) {
661 WARN("invalid parameter: flags = %08x\n", flags
);
662 return DSERR_INVALIDPARAM
;
670 static HRESULT WINAPI
PrimaryBufferImpl_Stop(IDirectSoundBuffer
*iface
)
672 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
673 DirectSoundDevice
*device
= This
->device
;
674 TRACE("(%p)\n", iface
);
681 static ULONG WINAPI
PrimaryBufferImpl_AddRef(IDirectSoundBuffer
*iface
)
683 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
684 ULONG ref
= InterlockedIncrement(&(This
->ref
));
685 TRACE("(%p) ref was %d\n", This
, ref
- 1);
687 InterlockedIncrement(&This
->numIfaces
);
691 /* Decreases *out by 1 to no less than 0.
692 * Returns the new value of *out. */
693 LONG
capped_refcount_dec(LONG
*out
)
700 oldref
= InterlockedCompareExchange(out
, ref
- 1, ref
);
701 } while(oldref
!= ref
);
705 static ULONG WINAPI
PrimaryBufferImpl_Release(IDirectSoundBuffer
*iface
)
707 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
710 ref
= capped_refcount_dec(&This
->ref
);
712 capped_refcount_dec(&This
->numIfaces
);
714 TRACE("(%p) primary ref is now %d\n", This
, ref
);
719 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer
*iface
,
720 DWORD
*playpos
, DWORD
*writepos
)
722 HRESULT hres
= DS_OK
;
725 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
726 DirectSoundDevice
*device
= This
->device
;
727 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
730 EnterCriticalSection(&(device
->mixlock
));
733 hres
= IAudioClient_GetCurrentPadding(device
->client
, &pad
);
735 WARN("IAudioClient_GetCurrentPadding failed\n");
736 LeaveCriticalSection(&(device
->mixlock
));
739 mixpos
= (device
->playpos
+ pad
* device
->pwfx
->nBlockAlign
) % device
->buflen
;
744 if (!device
->stopped
) {
745 /* apply the documented 10ms lead to writepos */
746 *writepos
+= device
->writelead
;
747 *writepos
%= device
->buflen
;
751 LeaveCriticalSection(&(device
->mixlock
));
754 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
758 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(IDirectSoundBuffer
*iface
, DWORD
*status
)
760 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
761 DirectSoundDevice
*device
= This
->device
;
762 TRACE("(%p,%p)\n", iface
, status
);
764 if (status
== NULL
) {
765 WARN("invalid parameter: status == NULL\n");
766 return DSERR_INVALIDPARAM
;
770 if (!device
->stopped
)
771 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
773 TRACE("status=%x\n", *status
);
778 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(IDirectSoundBuffer
*iface
, WAVEFORMATEX
*lpwf
,
779 DWORD wfsize
, DWORD
*wfwritten
)
782 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
783 DirectSoundDevice
*device
= This
->device
;
784 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
786 size
= sizeof(WAVEFORMATEX
) + device
->primary_pwfx
->cbSize
;
788 if (lpwf
) { /* NULL is valid */
789 if (wfsize
>= size
) {
790 CopyMemory(lpwf
,device
->primary_pwfx
,size
);
794 WARN("invalid parameter: wfsize too small\n");
797 return DSERR_INVALIDPARAM
;
801 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->primary_pwfx
->cbSize
;
803 WARN("invalid parameter: wfwritten == NULL\n");
804 return DSERR_INVALIDPARAM
;
811 static HRESULT WINAPI
PrimaryBufferImpl_Lock(IDirectSoundBuffer
*iface
, DWORD writecursor
,
812 DWORD writebytes
, void **lplpaudioptr1
, DWORD
*audiobytes1
, void **lplpaudioptr2
,
813 DWORD
*audiobytes2
, DWORD flags
)
816 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
817 DirectSoundDevice
*device
= This
->device
;
818 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
831 return DSERR_INVALIDPARAM
;
833 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
834 WARN("failed priority check!\n");
835 return DSERR_PRIOLEVELNEEDED
;
838 /* when this flag is set, writecursor is meaningless and must be calculated */
839 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
840 /* GetCurrentPosition does too much magic to duplicate here */
841 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
843 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
848 /* when this flag is set, writebytes is meaningless and must be set */
849 if (flags
& DSBLOCK_ENTIREBUFFER
)
850 writebytes
= device
->buflen
;
852 if (writecursor
>= device
->buflen
) {
853 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
854 writecursor
, device
->buflen
);
855 return DSERR_INVALIDPARAM
;
858 if (writebytes
> device
->buflen
) {
859 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
860 writebytes
, device
->buflen
);
861 return DSERR_INVALIDPARAM
;
864 if (writecursor
+writebytes
<= device
->buflen
) {
865 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
866 *audiobytes1
= writebytes
;
868 *(LPBYTE
*)lplpaudioptr2
= NULL
;
871 TRACE("->%d.0\n",writebytes
);
873 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
874 *audiobytes1
= device
->buflen
-writecursor
;
876 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
878 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
879 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
884 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer
*iface
, DWORD newpos
)
886 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
887 TRACE("(%p,%d)\n",This
,newpos
);
889 /* You cannot set the position of the primary buffer */
890 WARN("invalid call\n");
891 return DSERR_INVALIDCALL
;
894 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(IDirectSoundBuffer
*iface
, LONG pan
)
896 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
897 DirectSoundDevice
*device
= This
->device
;
902 TRACE("(%p,%d)\n", iface
, pan
);
904 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
905 WARN("control unavailable\n");
906 return DSERR_CONTROLUNAVAIL
;
909 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
910 WARN("invalid parameter: pan = %d\n", pan
);
911 return DSERR_INVALIDPARAM
;
915 EnterCriticalSection(&device
->mixlock
);
917 for (i
= 0; i
< DS_MAX_CHANNELS
; i
++) {
918 if (device
->pwfx
->nChannels
> i
){
919 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, i
, &fvol
);
921 LeaveCriticalSection(&device
->mixlock
);
922 WARN("GetChannelVolume failed: %08x\n", hr
);
928 device
->volpan
.dwTotalAmpFactor
[i
] = ((UINT16
)(fvol
* (DWORD
)0xFFFF));
931 DSOUND_AmpFactorToVolPan(&device
->volpan
);
932 if (pan
!= device
->volpan
.lPan
) {
933 device
->volpan
.lPan
=pan
;
934 DSOUND_RecalcVolPan(&device
->volpan
);
936 for (i
= 0; i
< DS_MAX_CHANNELS
; i
++) {
937 if (device
->pwfx
->nChannels
> i
) {
938 fvol
= (float)((DWORD
)(device
->volpan
.dwTotalAmpFactor
[i
] & 0xFFFF) / (float)0xFFFF);
939 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, i
, fvol
);
941 LeaveCriticalSection(&device
->mixlock
);
942 WARN("SetChannelVolume failed: %08x\n", hr
);
949 LeaveCriticalSection(&device
->mixlock
);
955 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(IDirectSoundBuffer
*iface
, LONG
*pan
)
957 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
958 DirectSoundDevice
*device
= This
->device
;
963 TRACE("(%p,%p)\n", iface
, pan
);
965 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
966 WARN("control unavailable\n");
967 return DSERR_CONTROLUNAVAIL
;
971 WARN("invalid parameter: pan == NULL\n");
972 return DSERR_INVALIDPARAM
;
975 EnterCriticalSection(&device
->mixlock
);
977 for (i
= 0; i
< DS_MAX_CHANNELS
; i
++) {
978 if (device
->pwfx
->nChannels
> i
) {
979 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, i
, &fvol
);
981 LeaveCriticalSection(&device
->mixlock
);
982 WARN("GetChannelVolume failed: %08x\n", hr
);
988 device
->volpan
.dwTotalAmpFactor
[i
] = ((UINT16
)(fvol
* (DWORD
)0xFFFF));
991 DSOUND_AmpFactorToVolPan(&device
->volpan
);
992 *pan
= device
->volpan
.lPan
;
994 LeaveCriticalSection(&device
->mixlock
);
999 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(IDirectSoundBuffer
*iface
, void *p1
, DWORD x1
,
1002 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1003 DirectSoundDevice
*device
= This
->device
;
1004 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
1006 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
1007 WARN("failed priority check!\n");
1008 return DSERR_PRIOLEVELNEEDED
;
1011 if ((p1
&& ((BYTE
*)p1
< device
->buffer
|| (BYTE
*)p1
>= device
->buffer
+ device
->buflen
)) ||
1012 (p2
&& ((BYTE
*)p2
< device
->buffer
|| (BYTE
*)p2
>= device
->buffer
+ device
->buflen
)))
1013 return DSERR_INVALIDPARAM
;
1018 static HRESULT WINAPI
PrimaryBufferImpl_Restore(IDirectSoundBuffer
*iface
)
1020 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1021 FIXME("(%p):stub\n",This
);
1025 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer
*iface
, DWORD
*freq
)
1027 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1028 DirectSoundDevice
*device
= This
->device
;
1029 TRACE("(%p,%p)\n", iface
, freq
);
1032 WARN("invalid parameter: freq == NULL\n");
1033 return DSERR_INVALIDPARAM
;
1036 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
1037 WARN("control unavailable\n");
1038 return DSERR_CONTROLUNAVAIL
;
1041 *freq
= device
->pwfx
->nSamplesPerSec
;
1042 TRACE("-> %d\n", *freq
);
1047 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(IDirectSoundBuffer
*iface
, IDirectSound
*dsound
,
1048 const DSBUFFERDESC
*dbsd
)
1050 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1051 WARN("(%p) already initialized\n", This
);
1052 return DSERR_ALREADYINITIALIZED
;
1055 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(IDirectSoundBuffer
*iface
, DSBCAPS
*caps
)
1057 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1058 DirectSoundDevice
*device
= This
->device
;
1059 TRACE("(%p,%p)\n", iface
, caps
);
1062 WARN("invalid parameter: caps == NULL\n");
1063 return DSERR_INVALIDPARAM
;
1066 if (caps
->dwSize
< sizeof(*caps
)) {
1067 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
1068 return DSERR_INVALIDPARAM
;
1071 caps
->dwFlags
= This
->dsbd
.dwFlags
;
1072 caps
->dwBufferBytes
= device
->buflen
;
1074 /* Windows reports these as zero */
1075 caps
->dwUnlockTransferRate
= 0;
1076 caps
->dwPlayCpuOverhead
= 0;
1081 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer
*iface
, REFIID riid
,
1084 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1086 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
1088 if (ppobj
== NULL
) {
1089 WARN("invalid parameter\n");
1090 return E_INVALIDARG
;
1093 *ppobj
= NULL
; /* assume failure */
1095 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
1096 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
1097 IDirectSoundBuffer_AddRef(iface
);
1102 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1103 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1104 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1105 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1106 return E_NOINTERFACE
;
1109 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1110 ERR("app requested IDirectSoundNotify on primary buffer\n");
1111 /* FIXME: should we support this? */
1112 return E_NOINTERFACE
;
1115 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1116 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1117 return E_NOINTERFACE
;
1120 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1121 *ppobj
= &This
->IDirectSound3DListener_iface
;
1122 IDirectSound3DListener_AddRef(&This
->IDirectSound3DListener_iface
);
1126 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1127 *ppobj
= &This
->IKsPropertySet_iface
;
1128 IKsPropertySet_AddRef(&This
->IKsPropertySet_iface
);
1132 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1133 return E_NOINTERFACE
;
1136 static const IDirectSoundBufferVtbl dspbvt
=
1138 PrimaryBufferImpl_QueryInterface
,
1139 PrimaryBufferImpl_AddRef
,
1140 PrimaryBufferImpl_Release
,
1141 PrimaryBufferImpl_GetCaps
,
1142 PrimaryBufferImpl_GetCurrentPosition
,
1143 PrimaryBufferImpl_GetFormat
,
1144 PrimaryBufferImpl_GetVolume
,
1145 PrimaryBufferImpl_GetPan
,
1146 PrimaryBufferImpl_GetFrequency
,
1147 PrimaryBufferImpl_GetStatus
,
1148 PrimaryBufferImpl_Initialize
,
1149 PrimaryBufferImpl_Lock
,
1150 PrimaryBufferImpl_Play
,
1151 PrimaryBufferImpl_SetCurrentPosition
,
1152 PrimaryBufferImpl_SetFormat
,
1153 PrimaryBufferImpl_SetVolume
,
1154 PrimaryBufferImpl_SetPan
,
1155 PrimaryBufferImpl_SetFrequency
,
1156 PrimaryBufferImpl_Stop
,
1157 PrimaryBufferImpl_Unlock
,
1158 PrimaryBufferImpl_Restore
1161 HRESULT
primarybuffer_create(DirectSoundDevice
*device
, IDirectSoundBufferImpl
**ppdsb
,
1162 const DSBUFFERDESC
*dsbd
)
1164 IDirectSoundBufferImpl
*dsb
;
1165 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1167 if (dsbd
->lpwfxFormat
) {
1168 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1170 return DSERR_INVALIDPARAM
;
1173 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1176 WARN("out of memory\n");
1178 return DSERR_OUTOFMEMORY
;
1185 dsb
->device
= device
;
1186 dsb
->IDirectSoundBuffer8_iface
.lpVtbl
= (IDirectSoundBuffer8Vtbl
*)&dspbvt
;
1187 dsb
->IDirectSound3DListener_iface
.lpVtbl
= &ds3dlvt
;
1188 dsb
->IKsPropertySet_iface
.lpVtbl
= &iksbvt
;
1191 /* IDirectSound3DListener */
1192 device
->ds3dl
.dwSize
= sizeof(DS3DLISTENER
);
1193 device
->ds3dl
.vPosition
.x
= 0.0;
1194 device
->ds3dl
.vPosition
.y
= 0.0;
1195 device
->ds3dl
.vPosition
.z
= 0.0;
1196 device
->ds3dl
.vVelocity
.x
= 0.0;
1197 device
->ds3dl
.vVelocity
.y
= 0.0;
1198 device
->ds3dl
.vVelocity
.z
= 0.0;
1199 device
->ds3dl
.vOrientFront
.x
= 0.0;
1200 device
->ds3dl
.vOrientFront
.y
= 0.0;
1201 device
->ds3dl
.vOrientFront
.z
= 1.0;
1202 device
->ds3dl
.vOrientTop
.x
= 0.0;
1203 device
->ds3dl
.vOrientTop
.y
= 1.0;
1204 device
->ds3dl
.vOrientTop
.z
= 0.0;
1205 device
->ds3dl
.flDistanceFactor
= DS3D_DEFAULTDISTANCEFACTOR
;
1206 device
->ds3dl
.flRolloffFactor
= DS3D_DEFAULTROLLOFFFACTOR
;
1207 device
->ds3dl
.flDopplerFactor
= DS3D_DEFAULTDOPPLERFACTOR
;
1208 device
->ds3dl_need_recalc
= TRUE
;
1210 TRACE("Created primary buffer at %p\n", dsb
);
1211 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1212 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1213 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1214 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1215 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1216 device
->pwfx
->cbSize
);
1218 IDirectSoundBuffer_AddRef(&dsb
->IDirectSoundBuffer8_iface
);