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 NONAMELESSSTRUCT
30 #define NONAMELESSUNION
37 #include "wine/debug.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
43 static DWORD
DSOUND_fraglen(DirectSoundDevice
*device
)
45 REFERENCE_TIME period
;
49 hr
= IAudioClient_GetDevicePeriod(device
->client
, &period
, NULL
);
51 /* just guess at 10ms */
52 WARN("GetDevicePeriod failed: %08x\n", hr
);
53 ret
= MulDiv(device
->pwfx
->nBlockAlign
, device
->pwfx
->nSamplesPerSec
, 100);
55 ret
= MulDiv(device
->pwfx
->nSamplesPerSec
* device
->pwfx
->nBlockAlign
, period
, 10000000);
57 ret
-= ret
% device
->pwfx
->nBlockAlign
;
61 static HRESULT
DSOUND_WaveFormat(DirectSoundDevice
*device
, IAudioClient
*client
,
62 BOOL forcewave
, WAVEFORMATEX
**wfx
)
64 WAVEFORMATEXTENSIBLE
*retwfe
= NULL
;
69 WAVEFORMATEXTENSIBLE
*mixwfe
;
70 hr
= IAudioClient_GetMixFormat(client
, (WAVEFORMATEX
**)&mixwfe
);
75 if (mixwfe
->Format
.nChannels
> 2) {
78 FIXME("Limiting channels to 2 due to lack of multichannel support\n");
80 mixwfe
->Format
.nChannels
= 2;
81 mixwfe
->Format
.nBlockAlign
= mixwfe
->Format
.nChannels
* mixwfe
->Format
.wBitsPerSample
/ 8;
82 mixwfe
->Format
.nAvgBytesPerSec
= mixwfe
->Format
.nSamplesPerSec
* mixwfe
->Format
.nBlockAlign
;
83 mixwfe
->dwChannelMask
= SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
;
86 if (!IsEqualGUID(&mixwfe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)) {
87 WAVEFORMATEXTENSIBLE testwfe
= *mixwfe
;
89 testwfe
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
90 testwfe
.Samples
.wValidBitsPerSample
= testwfe
.Format
.wBitsPerSample
= 32;
91 testwfe
.Format
.nBlockAlign
= testwfe
.Format
.nChannels
* testwfe
.Format
.wBitsPerSample
/ 8;
92 testwfe
.Format
.nAvgBytesPerSec
= testwfe
.Format
.nSamplesPerSec
* testwfe
.Format
.nBlockAlign
;
94 if (FAILED(IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
, &testwfe
.Format
, (WAVEFORMATEX
**)&retwfe
)))
95 w
= DSOUND_CopyFormat(&mixwfe
->Format
);
97 w
= DSOUND_CopyFormat(&retwfe
->Format
);
99 w
= DSOUND_CopyFormat(&testwfe
.Format
);
100 CoTaskMemFree(retwfe
);
103 w
= DSOUND_CopyFormat(&mixwfe
->Format
);
104 CoTaskMemFree(mixwfe
);
105 } else if (device
->primary_pwfx
->wFormatTag
== WAVE_FORMAT_PCM
||
106 device
->primary_pwfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
107 WAVEFORMATEX
*wi
= device
->primary_pwfx
;
108 WAVEFORMATEXTENSIBLE
*wfe
;
110 /* Convert to WAVEFORMATEXTENSIBLE */
111 w
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE
));
112 wfe
= (WAVEFORMATEXTENSIBLE
*)w
;
114 return DSERR_OUTOFMEMORY
;
117 w
->wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
118 w
->cbSize
= sizeof(*wfe
) - sizeof(*w
);
119 w
->nBlockAlign
= w
->nChannels
* w
->wBitsPerSample
/ 8;
120 w
->nAvgBytesPerSec
= w
->nSamplesPerSec
* w
->nBlockAlign
;
122 wfe
->dwChannelMask
= 0;
123 if (wi
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
124 w
->wBitsPerSample
= 32;
125 wfe
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
127 wfe
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
128 wfe
->Samples
.wValidBitsPerSample
= w
->wBitsPerSample
;
130 w
= DSOUND_CopyFormat(device
->primary_pwfx
);
133 return DSERR_OUTOFMEMORY
;
135 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
, w
, (WAVEFORMATEX
**)&retwfe
);
137 memcpy(w
, retwfe
, sizeof(WAVEFORMATEX
) + retwfe
->Format
.cbSize
);
138 CoTaskMemFree(retwfe
);
141 WARN("IsFormatSupported failed: %08x\n", hr
);
142 HeapFree(GetProcessHeap(), 0, w
);
149 HRESULT
DSOUND_ReopenDevice(DirectSoundDevice
*device
, BOOL forcewave
)
152 REFERENCE_TIME prebuf_rt
;
153 WAVEFORMATEX
*wfx
= NULL
;
155 REFERENCE_TIME period
;
158 TRACE("(%p, %d)\n", device
, forcewave
);
161 IAudioClient_Release(device
->client
);
162 device
->client
= NULL
;
165 IAudioRenderClient_Release(device
->render
);
166 device
->render
= NULL
;
169 IAudioClock_Release(device
->clock
);
170 device
->clock
= NULL
;
173 IAudioStreamVolume_Release(device
->volume
);
174 device
->volume
= NULL
;
177 hres
= IMMDevice_Activate(device
->mmdevice
, &IID_IAudioClient
,
178 CLSCTX_INPROC_SERVER
, NULL
, (void **)&device
->client
);
180 WARN("Activate failed: %08x\n", hres
);
184 hres
= DSOUND_WaveFormat(device
, device
->client
, forcewave
, &wfx
);
186 IAudioClient_Release(device
->client
);
187 device
->client
= NULL
;
190 HeapFree(GetProcessHeap(), 0, device
->pwfx
);
193 prebuf_frames
= device
->prebuf
* DSOUND_fraglen(device
) / device
->pwfx
->nBlockAlign
;
194 prebuf_rt
= (10000000 * (UINT64
)prebuf_frames
) / device
->pwfx
->nSamplesPerSec
;
196 hres
= IAudioClient_Initialize(device
->client
,
197 AUDCLNT_SHAREMODE_SHARED
, AUDCLNT_STREAMFLAGS_NOPERSIST
|
198 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
, prebuf_rt
, 0, device
->pwfx
, NULL
);
200 IAudioClient_Release(device
->client
);
201 device
->client
= NULL
;
202 WARN("Initialize failed: %08x\n", hres
);
205 IAudioClient_SetEventHandle(device
->client
, device
->sleepev
);
207 hres
= IAudioClient_GetService(device
->client
, &IID_IAudioRenderClient
,
208 (void**)&device
->render
);
210 IAudioClient_Release(device
->client
);
211 device
->client
= NULL
;
212 WARN("GetService failed: %08x\n", hres
);
216 hres
= IAudioClient_GetService(device
->client
, &IID_IAudioClock
,
217 (void**)&device
->clock
);
219 IAudioClient_Release(device
->client
);
220 IAudioRenderClient_Release(device
->render
);
221 device
->client
= NULL
;
222 device
->render
= NULL
;
223 WARN("GetService failed: %08x\n", hres
);
227 hres
= IAudioClient_GetService(device
->client
, &IID_IAudioStreamVolume
,
228 (void**)&device
->volume
);
230 IAudioClient_Release(device
->client
);
231 IAudioRenderClient_Release(device
->render
);
232 IAudioClock_Release(device
->clock
);
233 device
->client
= NULL
;
234 device
->render
= NULL
;
235 device
->clock
= NULL
;
236 WARN("GetService failed: %08x\n", hres
);
240 /* Now kick off the timer so the event fires periodically */
241 hres
= IAudioClient_Start(device
->client
);
243 WARN("starting failed with %08x\n", hres
);
245 hres
= IAudioClient_GetStreamLatency(device
->client
, &period
);
247 WARN("GetStreamLatency failed with %08x\n", hres
);
250 period_ms
= (period
+ 9999) / 10000;
251 TRACE("period %u ms fraglen %u prebuf %u\n", period_ms
, device
->fraglen
, device
->prebuf
);
254 device
->sleeptime
= 5;
256 device
->sleeptime
= period_ms
* 5 / 2;
261 HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
263 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
267 TRACE("(%p)\n", device
);
269 device
->fraglen
= DSOUND_fraglen(device
);
271 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
272 on windows this size is always fixed (tested on win-xp) */
274 device
->buflen
= ds_hel_buflen
;
275 device
->buflen
-= device
->buflen
% device
->pwfx
->nBlockAlign
;
276 while(device
->buflen
< device
->fraglen
* device
->prebuf
){
277 device
->buflen
+= ds_hel_buflen
;
278 device
->buflen
-= device
->buflen
% device
->pwfx
->nBlockAlign
;
281 HeapFree(GetProcessHeap(), 0, device
->mix_buffer
);
282 device
->mix_buffer_len
= (device
->buflen
/ (device
->pwfx
->wBitsPerSample
/ 8)) * sizeof(float);
283 device
->mix_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, device
->mix_buffer_len
);
284 if (!device
->mix_buffer
)
285 return DSERR_OUTOFMEMORY
;
287 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
288 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
290 /* reallocate emulated primary buffer */
292 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
, device
->buflen
);
294 newbuf
= HeapAlloc(GetProcessHeap(),0, device
->buflen
);
297 ERR("failed to allocate primary buffer\n");
298 return DSERR_OUTOFMEMORY
;
299 /* but the old buffer might still exist and must be re-prepared */
302 device
->writelead
= (device
->pwfx
->nSamplesPerSec
/ 100) * device
->pwfx
->nBlockAlign
;
304 device
->buffer
= newbuf
;
306 TRACE("buflen: %u, fraglen: %u, mix_buffer_len: %u\n",
307 device
->buflen
, device
->fraglen
, device
->mix_buffer_len
);
309 if(device
->pwfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
310 (device
->pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
311 IsEqualGUID(&((WAVEFORMATEXTENSIBLE
*)device
->pwfx
)->SubFormat
,
312 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)))
313 device
->normfunction
= normfunctions
[4];
315 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
317 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
318 FillMemory(device
->mix_buffer
, device
->mix_buffer_len
, 0);
321 if (device
->pwfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
322 (device
->pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
323 IsEqualGUID(&((WAVEFORMATEXTENSIBLE
*)device
->pwfx
)->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)))
324 device
->normfunction
= normfunctions
[4];
326 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
328 for (i
= 0; i
< device
->nrofbuffers
; i
++) {
329 RtlAcquireResourceExclusive(&dsb
[i
]->lock
, TRUE
);
330 DSOUND_RecalcFormat(dsb
[i
]);
331 RtlReleaseResource(&dsb
[i
]->lock
);
338 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
342 TRACE("(%p)\n", device
);
345 hr
= IAudioClient_Stop(device
->client
);
347 WARN("Stop failed: %08x\n", hr
);
350 /* clear the queue */
351 device
->in_mmdev_bytes
= 0;
354 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
357 TRACE("(%p)\n", device
);
359 device
->buflen
= ds_hel_buflen
;
360 err
= DSOUND_PrimaryOpen(device
);
363 WARN("DSOUND_PrimaryOpen failed\n");
367 device
->state
= STATE_STOPPED
;
371 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
373 TRACE("(%p)\n", device
);
376 EnterCriticalSection(&(device
->mixlock
));
378 DSOUND_PrimaryClose(device
);
380 if(device
->primary
&& (device
->primary
->ref
|| device
->primary
->numIfaces
))
381 WARN("Destroying primary buffer while references held (%u %u)\n", device
->primary
->ref
, device
->primary
->numIfaces
);
383 HeapFree(GetProcessHeap(), 0, device
->primary
);
384 device
->primary
= NULL
;
386 HeapFree(GetProcessHeap(),0,device
->primary_pwfx
);
387 HeapFree(GetProcessHeap(),0,device
->pwfx
);
390 LeaveCriticalSection(&(device
->mixlock
));
396 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
400 TRACE("(%p)\n", device
);
402 hr
= IAudioClient_Start(device
->client
);
403 if(FAILED(hr
) && hr
!= AUDCLNT_E_NOT_STOPPED
){
404 WARN("Start failed: %08x\n", hr
);
411 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
415 TRACE("(%p)\n", device
);
417 hr
= IAudioClient_Stop(device
->client
);
419 WARN("Stop failed: %08x\n", hr
);
426 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
428 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
430 /* check if playpos was requested */
432 *playpos
= device
->playing_offs_bytes
;
434 /* check if writepos was requested */
436 /* the writepos is the first non-queued position */
437 *writepos
= (device
->playing_offs_bytes
+ device
->in_mmdev_bytes
) % device
->buflen
;
439 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:-1, writepos
?*writepos
:-1, device
, GetTickCount());
443 static DWORD
DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex
)
445 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
)
446 return sizeof(WAVEFORMATEX
);
448 return sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
451 LPWAVEFORMATEX
DSOUND_CopyFormat(LPCWAVEFORMATEX wfex
)
453 DWORD size
= DSOUND_GetFormatSize(wfex
);
454 LPWAVEFORMATEX pwfx
= HeapAlloc(GetProcessHeap(),0,size
);
456 WARN("out of memory\n");
457 } else if (wfex
->wFormatTag
!= WAVE_FORMAT_PCM
) {
458 CopyMemory(pwfx
, wfex
, size
);
460 CopyMemory(pwfx
, wfex
, sizeof(PCMWAVEFORMAT
));
462 if (pwfx
->nBlockAlign
!= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8) {
463 WARN("Fixing bad nBlockAlign (%u)\n", pwfx
->nBlockAlign
);
464 pwfx
->nBlockAlign
= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8;
466 if (pwfx
->nAvgBytesPerSec
!= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
) {
467 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx
->nAvgBytesPerSec
);
468 pwfx
->nAvgBytesPerSec
= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
;
474 HRESULT
primarybuffer_SetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX passed_fmt
)
477 WAVEFORMATEX
*old_fmt
;
478 WAVEFORMATEXTENSIBLE
*fmtex
, *passed_fmtex
= (WAVEFORMATEXTENSIBLE
*)passed_fmt
;
479 BOOL forced
= (device
->priolevel
== DSSCL_WRITEPRIMARY
);
481 TRACE("(%p,%p)\n", device
, passed_fmt
);
483 if (device
->priolevel
== DSSCL_NORMAL
) {
484 WARN("failed priority check!\n");
485 return DSERR_PRIOLEVELNEEDED
;
488 /* Let's be pedantic! */
489 if (passed_fmt
== NULL
) {
490 WARN("invalid parameter: passed_fmt==NULL!\n");
491 return DSERR_INVALIDPARAM
;
493 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
494 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
495 passed_fmt
->wFormatTag
, passed_fmt
->nChannels
, passed_fmt
->nSamplesPerSec
,
496 passed_fmt
->nAvgBytesPerSec
, passed_fmt
->nBlockAlign
,
497 passed_fmt
->wBitsPerSample
, passed_fmt
->cbSize
);
499 if(passed_fmt
->wBitsPerSample
< 8 || passed_fmt
->wBitsPerSample
% 8 != 0 ||
500 passed_fmt
->nChannels
== 0 || passed_fmt
->nSamplesPerSec
== 0 ||
501 passed_fmt
->nAvgBytesPerSec
== 0 ||
502 passed_fmt
->nBlockAlign
!= passed_fmt
->nChannels
* passed_fmt
->wBitsPerSample
/ 8)
503 return DSERR_INVALIDPARAM
;
505 if(passed_fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
506 if(passed_fmtex
->Samples
.wValidBitsPerSample
> passed_fmtex
->Format
.wBitsPerSample
)
507 return DSERR_INVALIDPARAM
;
511 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
512 EnterCriticalSection(&(device
->mixlock
));
514 if (device
->priolevel
== DSSCL_WRITEPRIMARY
) {
515 old_fmt
= device
->primary_pwfx
;
516 device
->primary_pwfx
= DSOUND_CopyFormat(passed_fmt
);
517 fmtex
= (WAVEFORMATEXTENSIBLE
*)device
->primary_pwfx
;
518 if (device
->primary_pwfx
== NULL
) {
519 err
= DSERR_OUTOFMEMORY
;
523 if (fmtex
->Format
.wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
524 fmtex
->Samples
.wValidBitsPerSample
== 0) {
525 TRACE("Correcting 0 valid bits per sample\n");
526 fmtex
->Samples
.wValidBitsPerSample
= fmtex
->Format
.wBitsPerSample
;
529 DSOUND_PrimaryClose(device
);
531 err
= DSOUND_ReopenDevice(device
, forced
);
533 ERR("No formats could be opened\n");
537 err
= DSOUND_PrimaryOpen(device
);
539 ERR("DSOUND_PrimaryOpen failed\n");
545 device
->primary_pwfx
= old_fmt
;
547 HeapFree(GetProcessHeap(), 0, old_fmt
);
548 } else if (passed_fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
549 passed_fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
550 /* Fill in "real" values to primary_pwfx */
551 WAVEFORMATEX
*fmt
= device
->primary_pwfx
;
553 *fmt
= *device
->pwfx
;
554 fmtex
= (void*)device
->pwfx
;
556 if (IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
) &&
557 passed_fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
558 fmt
->wFormatTag
= WAVE_FORMAT_IEEE_FLOAT
;
560 fmt
->wFormatTag
= WAVE_FORMAT_PCM
;
561 fmt
->wBitsPerSample
= 16;
563 fmt
->nBlockAlign
= fmt
->nChannels
* fmt
->wBitsPerSample
/ 8;
564 fmt
->nAvgBytesPerSec
= fmt
->nBlockAlign
* fmt
->nSamplesPerSec
;
567 device
->primary_pwfx
= HeapReAlloc(GetProcessHeap(), 0, device
->primary_pwfx
, sizeof(*fmtex
));
568 memcpy(device
->primary_pwfx
, device
->pwfx
, sizeof(*fmtex
));
572 LeaveCriticalSection(&(device
->mixlock
));
573 RtlReleaseResource(&(device
->buffer_list_lock
));
579 /*******************************************************************************
582 static inline IDirectSoundBufferImpl
*impl_from_IDirectSoundBuffer(IDirectSoundBuffer
*iface
)
584 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
585 return CONTAINING_RECORD(iface
, IDirectSoundBufferImpl
, IDirectSoundBuffer8_iface
);
588 /* This sets this format for the primary buffer only */
589 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(IDirectSoundBuffer
*iface
,
590 const WAVEFORMATEX
*wfex
)
592 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
593 TRACE("(%p,%p)\n", iface
, wfex
);
594 return primarybuffer_SetFormat(This
->device
, wfex
);
597 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(IDirectSoundBuffer
*iface
, LONG vol
)
599 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
600 DirectSoundDevice
*device
= This
->device
;
604 TRACE("(%p,%d)\n", iface
, vol
);
606 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
607 WARN("control unavailable\n");
608 return DSERR_CONTROLUNAVAIL
;
611 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
612 WARN("invalid parameter: vol = %d\n", vol
);
613 return DSERR_INVALIDPARAM
;
617 EnterCriticalSection(&device
->mixlock
);
619 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
621 LeaveCriticalSection(&device
->mixlock
);
622 WARN("GetChannelVolume failed: %08x\n", hr
);
626 if(device
->pwfx
->nChannels
> 1){
627 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
629 LeaveCriticalSection(&device
->mixlock
);
630 WARN("GetChannelVolume failed: %08x\n", hr
);
636 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
637 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
639 DSOUND_AmpFactorToVolPan(&device
->volpan
);
640 if (vol
!= device
->volpan
.lVolume
) {
641 device
->volpan
.lVolume
=vol
;
642 DSOUND_RecalcVolPan(&device
->volpan
);
643 lvol
= (float)((DWORD
)(device
->volpan
.dwTotalLeftAmpFactor
& 0xFFFF) / (float)0xFFFF);
644 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 0, lvol
);
646 LeaveCriticalSection(&device
->mixlock
);
647 WARN("SetChannelVolume failed: %08x\n", hr
);
651 if(device
->pwfx
->nChannels
> 1){
652 rvol
= (float)((DWORD
)(device
->volpan
.dwTotalRightAmpFactor
& 0xFFFF) / (float)0xFFFF);
653 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 1, rvol
);
655 LeaveCriticalSection(&device
->mixlock
);
656 WARN("SetChannelVolume failed: %08x\n", hr
);
662 LeaveCriticalSection(&(device
->mixlock
));
668 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(IDirectSoundBuffer
*iface
, LONG
*vol
)
670 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
671 DirectSoundDevice
*device
= This
->device
;
674 TRACE("(%p,%p)\n", iface
, vol
);
676 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
677 WARN("control unavailable\n");
678 return DSERR_CONTROLUNAVAIL
;
682 WARN("invalid parameter: vol = NULL\n");
683 return DSERR_INVALIDPARAM
;
686 EnterCriticalSection(&device
->mixlock
);
688 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
690 LeaveCriticalSection(&device
->mixlock
);
691 WARN("GetChannelVolume failed: %08x\n", hr
);
695 if(device
->pwfx
->nChannels
> 1){
696 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
698 LeaveCriticalSection(&device
->mixlock
);
699 WARN("GetChannelVolume failed: %08x\n", hr
);
705 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
706 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
708 DSOUND_AmpFactorToVolPan(&device
->volpan
);
709 *vol
= device
->volpan
.lVolume
;
711 LeaveCriticalSection(&device
->mixlock
);
716 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer
*iface
, DWORD freq
)
718 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
719 TRACE("(%p,%d)\n",This
,freq
);
721 /* You cannot set the frequency of the primary buffer */
722 WARN("control unavailable\n");
723 return DSERR_CONTROLUNAVAIL
;
726 static HRESULT WINAPI
PrimaryBufferImpl_Play(IDirectSoundBuffer
*iface
, DWORD reserved1
,
727 DWORD reserved2
, DWORD flags
)
729 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
730 DirectSoundDevice
*device
= This
->device
;
731 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
733 if (!(flags
& DSBPLAY_LOOPING
)) {
734 WARN("invalid parameter: flags = %08x\n", flags
);
735 return DSERR_INVALIDPARAM
;
739 EnterCriticalSection(&(device
->mixlock
));
741 if (device
->state
== STATE_STOPPED
)
742 device
->state
= STATE_STARTING
;
743 else if (device
->state
== STATE_STOPPING
)
744 device
->state
= STATE_PLAYING
;
746 LeaveCriticalSection(&(device
->mixlock
));
752 static HRESULT WINAPI
PrimaryBufferImpl_Stop(IDirectSoundBuffer
*iface
)
754 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
755 DirectSoundDevice
*device
= This
->device
;
756 TRACE("(%p)\n", iface
);
759 EnterCriticalSection(&(device
->mixlock
));
761 if (device
->state
== STATE_PLAYING
)
762 device
->state
= STATE_STOPPING
;
763 else if (device
->state
== STATE_STARTING
)
764 device
->state
= STATE_STOPPED
;
766 LeaveCriticalSection(&(device
->mixlock
));
772 static ULONG WINAPI
PrimaryBufferImpl_AddRef(IDirectSoundBuffer
*iface
)
774 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
775 ULONG ref
= InterlockedIncrement(&(This
->ref
));
776 TRACE("(%p) ref was %d\n", This
, ref
- 1);
778 InterlockedIncrement(&This
->numIfaces
);
782 /* Decreases *out by 1 to no less than 0.
783 * Returns the new value of *out. */
784 LONG
capped_refcount_dec(LONG
*out
)
791 oldref
= InterlockedCompareExchange(out
, ref
- 1, ref
);
792 } while(oldref
!= ref
);
796 static ULONG WINAPI
PrimaryBufferImpl_Release(IDirectSoundBuffer
*iface
)
798 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
801 ref
= capped_refcount_dec(&This
->ref
);
803 capped_refcount_dec(&This
->numIfaces
);
805 TRACE("(%p) primary ref is now %d\n", This
, ref
);
810 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer
*iface
,
811 DWORD
*playpos
, DWORD
*writepos
)
814 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
815 DirectSoundDevice
*device
= This
->device
;
816 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
819 EnterCriticalSection(&(device
->mixlock
));
821 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
823 WARN("DSOUND_PrimaryGetPosition failed\n");
824 LeaveCriticalSection(&(device
->mixlock
));
828 if (device
->state
!= STATE_STOPPED
)
829 /* apply the documented 10ms lead to writepos */
830 *writepos
+= device
->writelead
;
831 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
834 LeaveCriticalSection(&(device
->mixlock
));
837 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
841 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(IDirectSoundBuffer
*iface
, DWORD
*status
)
843 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
844 DirectSoundDevice
*device
= This
->device
;
845 TRACE("(%p,%p)\n", iface
, status
);
847 if (status
== NULL
) {
848 WARN("invalid parameter: status == NULL\n");
849 return DSERR_INVALIDPARAM
;
853 if ((device
->state
== STATE_STARTING
) ||
854 (device
->state
== STATE_PLAYING
))
855 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
857 TRACE("status=%x\n", *status
);
862 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(IDirectSoundBuffer
*iface
, WAVEFORMATEX
*lpwf
,
863 DWORD wfsize
, DWORD
*wfwritten
)
866 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
867 DirectSoundDevice
*device
= This
->device
;
868 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
870 size
= sizeof(WAVEFORMATEX
) + device
->primary_pwfx
->cbSize
;
872 if (lpwf
) { /* NULL is valid */
873 if (wfsize
>= size
) {
874 CopyMemory(lpwf
,device
->primary_pwfx
,size
);
878 WARN("invalid parameter: wfsize too small\n");
881 return DSERR_INVALIDPARAM
;
885 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->primary_pwfx
->cbSize
;
887 WARN("invalid parameter: wfwritten == NULL\n");
888 return DSERR_INVALIDPARAM
;
895 static HRESULT WINAPI
PrimaryBufferImpl_Lock(IDirectSoundBuffer
*iface
, DWORD writecursor
,
896 DWORD writebytes
, void **lplpaudioptr1
, DWORD
*audiobytes1
, void **lplpaudioptr2
,
897 DWORD
*audiobytes2
, DWORD flags
)
900 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
901 DirectSoundDevice
*device
= This
->device
;
902 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
915 return DSERR_INVALIDPARAM
;
917 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
918 WARN("failed priority check!\n");
919 return DSERR_PRIOLEVELNEEDED
;
922 /* when this flag is set, writecursor is meaningless and must be calculated */
923 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
924 /* GetCurrentPosition does too much magic to duplicate here */
925 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
927 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
932 /* when this flag is set, writebytes is meaningless and must be set */
933 if (flags
& DSBLOCK_ENTIREBUFFER
)
934 writebytes
= device
->buflen
;
936 if (writecursor
>= device
->buflen
) {
937 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
938 writecursor
, device
->buflen
);
939 return DSERR_INVALIDPARAM
;
942 if (writebytes
> device
->buflen
) {
943 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
944 writebytes
, device
->buflen
);
945 return DSERR_INVALIDPARAM
;
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);
968 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer
*iface
, DWORD newpos
)
970 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
971 TRACE("(%p,%d)\n",This
,newpos
);
973 /* You cannot set the position of the primary buffer */
974 WARN("invalid call\n");
975 return DSERR_INVALIDCALL
;
978 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(IDirectSoundBuffer
*iface
, LONG pan
)
980 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
981 DirectSoundDevice
*device
= This
->device
;
984 TRACE("(%p,%d)\n", iface
, pan
);
986 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
987 WARN("control unavailable\n");
988 return DSERR_CONTROLUNAVAIL
;
991 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
992 WARN("invalid parameter: pan = %d\n", pan
);
993 return DSERR_INVALIDPARAM
;
997 EnterCriticalSection(&device
->mixlock
);
999 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
1001 LeaveCriticalSection(&device
->mixlock
);
1002 WARN("GetChannelVolume failed: %08x\n", hr
);
1006 if(device
->pwfx
->nChannels
> 1){
1007 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
1009 LeaveCriticalSection(&device
->mixlock
);
1010 WARN("GetChannelVolume failed: %08x\n", hr
);
1016 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
1017 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
1019 DSOUND_AmpFactorToVolPan(&device
->volpan
);
1020 if (pan
!= device
->volpan
.lPan
) {
1021 device
->volpan
.lPan
=pan
;
1022 DSOUND_RecalcVolPan(&device
->volpan
);
1024 lvol
= (float)((DWORD
)(device
->volpan
.dwTotalLeftAmpFactor
& 0xFFFF) / (float)0xFFFF);
1025 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 0, lvol
);
1027 LeaveCriticalSection(&device
->mixlock
);
1028 WARN("SetChannelVolume failed: %08x\n", hr
);
1032 if(device
->pwfx
->nChannels
> 1){
1033 rvol
= (float)((DWORD
)(device
->volpan
.dwTotalRightAmpFactor
& 0xFFFF) / (float)0xFFFF);
1034 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 1, rvol
);
1036 LeaveCriticalSection(&device
->mixlock
);
1037 WARN("SetChannelVolume failed: %08x\n", hr
);
1043 LeaveCriticalSection(&device
->mixlock
);
1049 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(IDirectSoundBuffer
*iface
, LONG
*pan
)
1051 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1052 DirectSoundDevice
*device
= This
->device
;
1055 TRACE("(%p,%p)\n", iface
, pan
);
1057 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
1058 WARN("control unavailable\n");
1059 return DSERR_CONTROLUNAVAIL
;
1063 WARN("invalid parameter: pan == NULL\n");
1064 return DSERR_INVALIDPARAM
;
1067 EnterCriticalSection(&device
->mixlock
);
1069 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
1071 LeaveCriticalSection(&device
->mixlock
);
1072 WARN("GetChannelVolume failed: %08x\n", hr
);
1076 if(device
->pwfx
->nChannels
> 1){
1077 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
1079 LeaveCriticalSection(&device
->mixlock
);
1080 WARN("GetChannelVolume failed: %08x\n", hr
);
1086 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
1087 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
1089 DSOUND_AmpFactorToVolPan(&device
->volpan
);
1090 *pan
= device
->volpan
.lPan
;
1092 LeaveCriticalSection(&device
->mixlock
);
1097 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(IDirectSoundBuffer
*iface
, void *p1
, DWORD x1
,
1100 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1101 DirectSoundDevice
*device
= This
->device
;
1102 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
1104 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
1105 WARN("failed priority check!\n");
1106 return DSERR_PRIOLEVELNEEDED
;
1109 if ((p1
&& ((BYTE
*)p1
< device
->buffer
|| (BYTE
*)p1
>= device
->buffer
+ device
->buflen
)) ||
1110 (p2
&& ((BYTE
*)p2
< device
->buffer
|| (BYTE
*)p2
>= device
->buffer
+ device
->buflen
)))
1111 return DSERR_INVALIDPARAM
;
1116 static HRESULT WINAPI
PrimaryBufferImpl_Restore(IDirectSoundBuffer
*iface
)
1118 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1119 FIXME("(%p):stub\n",This
);
1123 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer
*iface
, DWORD
*freq
)
1125 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1126 DirectSoundDevice
*device
= This
->device
;
1127 TRACE("(%p,%p)\n", iface
, freq
);
1130 WARN("invalid parameter: freq == NULL\n");
1131 return DSERR_INVALIDPARAM
;
1134 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
1135 WARN("control unavailable\n");
1136 return DSERR_CONTROLUNAVAIL
;
1139 *freq
= device
->pwfx
->nSamplesPerSec
;
1140 TRACE("-> %d\n", *freq
);
1145 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(IDirectSoundBuffer
*iface
, IDirectSound
*dsound
,
1146 const DSBUFFERDESC
*dbsd
)
1148 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1149 WARN("(%p) already initialized\n", This
);
1150 return DSERR_ALREADYINITIALIZED
;
1153 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(IDirectSoundBuffer
*iface
, DSBCAPS
*caps
)
1155 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1156 DirectSoundDevice
*device
= This
->device
;
1157 TRACE("(%p,%p)\n", iface
, caps
);
1160 WARN("invalid parameter: caps == NULL\n");
1161 return DSERR_INVALIDPARAM
;
1164 if (caps
->dwSize
< sizeof(*caps
)) {
1165 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
1166 return DSERR_INVALIDPARAM
;
1169 caps
->dwFlags
= This
->dsbd
.dwFlags
;
1170 caps
->dwBufferBytes
= device
->buflen
;
1172 /* Windows reports these as zero */
1173 caps
->dwUnlockTransferRate
= 0;
1174 caps
->dwPlayCpuOverhead
= 0;
1179 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer
*iface
, REFIID riid
,
1182 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1184 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
1186 if (ppobj
== NULL
) {
1187 WARN("invalid parameter\n");
1188 return E_INVALIDARG
;
1191 *ppobj
= NULL
; /* assume failure */
1193 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
1194 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
1195 IDirectSoundBuffer_AddRef(iface
);
1200 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1201 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1202 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1203 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1204 return E_NOINTERFACE
;
1207 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1208 ERR("app requested IDirectSoundNotify on primary buffer\n");
1209 /* FIXME: should we support this? */
1210 return E_NOINTERFACE
;
1213 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1214 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1215 return E_NOINTERFACE
;
1218 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1219 *ppobj
= &This
->IDirectSound3DListener_iface
;
1220 IDirectSound3DListener_AddRef(&This
->IDirectSound3DListener_iface
);
1224 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1225 *ppobj
= &This
->IKsPropertySet_iface
;
1226 IKsPropertySet_AddRef(&This
->IKsPropertySet_iface
);
1230 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1231 return E_NOINTERFACE
;
1234 static const IDirectSoundBufferVtbl dspbvt
=
1236 PrimaryBufferImpl_QueryInterface
,
1237 PrimaryBufferImpl_AddRef
,
1238 PrimaryBufferImpl_Release
,
1239 PrimaryBufferImpl_GetCaps
,
1240 PrimaryBufferImpl_GetCurrentPosition
,
1241 PrimaryBufferImpl_GetFormat
,
1242 PrimaryBufferImpl_GetVolume
,
1243 PrimaryBufferImpl_GetPan
,
1244 PrimaryBufferImpl_GetFrequency
,
1245 PrimaryBufferImpl_GetStatus
,
1246 PrimaryBufferImpl_Initialize
,
1247 PrimaryBufferImpl_Lock
,
1248 PrimaryBufferImpl_Play
,
1249 PrimaryBufferImpl_SetCurrentPosition
,
1250 PrimaryBufferImpl_SetFormat
,
1251 PrimaryBufferImpl_SetVolume
,
1252 PrimaryBufferImpl_SetPan
,
1253 PrimaryBufferImpl_SetFrequency
,
1254 PrimaryBufferImpl_Stop
,
1255 PrimaryBufferImpl_Unlock
,
1256 PrimaryBufferImpl_Restore
1259 HRESULT
primarybuffer_create(DirectSoundDevice
*device
, IDirectSoundBufferImpl
**ppdsb
,
1260 const DSBUFFERDESC
*dsbd
)
1262 IDirectSoundBufferImpl
*dsb
;
1263 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1265 if (dsbd
->lpwfxFormat
) {
1266 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1268 return DSERR_INVALIDPARAM
;
1271 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1274 WARN("out of memory\n");
1276 return DSERR_OUTOFMEMORY
;
1283 dsb
->device
= device
;
1284 dsb
->IDirectSoundBuffer8_iface
.lpVtbl
= (IDirectSoundBuffer8Vtbl
*)&dspbvt
;
1285 dsb
->IDirectSound3DListener_iface
.lpVtbl
= &ds3dlvt
;
1286 dsb
->IKsPropertySet_iface
.lpVtbl
= &iksbvt
;
1289 /* IDirectSound3DListener */
1290 device
->ds3dl
.dwSize
= sizeof(DS3DLISTENER
);
1291 device
->ds3dl
.vPosition
.x
= 0.0;
1292 device
->ds3dl
.vPosition
.y
= 0.0;
1293 device
->ds3dl
.vPosition
.z
= 0.0;
1294 device
->ds3dl
.vVelocity
.x
= 0.0;
1295 device
->ds3dl
.vVelocity
.y
= 0.0;
1296 device
->ds3dl
.vVelocity
.z
= 0.0;
1297 device
->ds3dl
.vOrientFront
.x
= 0.0;
1298 device
->ds3dl
.vOrientFront
.y
= 0.0;
1299 device
->ds3dl
.vOrientFront
.z
= 1.0;
1300 device
->ds3dl
.vOrientTop
.x
= 0.0;
1301 device
->ds3dl
.vOrientTop
.y
= 1.0;
1302 device
->ds3dl
.vOrientTop
.z
= 0.0;
1303 device
->ds3dl
.flDistanceFactor
= DS3D_DEFAULTDISTANCEFACTOR
;
1304 device
->ds3dl
.flRolloffFactor
= DS3D_DEFAULTROLLOFFFACTOR
;
1305 device
->ds3dl
.flDopplerFactor
= DS3D_DEFAULTDOPPLERFACTOR
;
1306 device
->ds3dl_need_recalc
= TRUE
;
1308 TRACE("Created primary buffer at %p\n", dsb
);
1309 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1310 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1311 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1312 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1313 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1314 device
->pwfx
->cbSize
);
1316 IDirectSoundBuffer_AddRef(&dsb
->IDirectSoundBuffer8_iface
);