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 WIN32_NO_STATUS
30 #define COM_NO_WINDOWS_H
32 #define NONAMELESSSTRUCT
33 #define NONAMELESSUNION
35 //#include "winbase.h"
36 //#include "winuser.h"
37 //#include "mmsystem.h"
40 #include <wine/debug.h>
42 #include "dsound_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
46 static DWORD
DSOUND_fraglen(DirectSoundDevice
*device
)
48 REFERENCE_TIME period
;
52 hr
= IAudioClient_GetDevicePeriod(device
->client
, &period
, NULL
);
54 /* just guess at 10ms */
55 WARN("GetDevicePeriod failed: %08x\n", hr
);
56 ret
= MulDiv(device
->pwfx
->nBlockAlign
, device
->pwfx
->nSamplesPerSec
, 100);
58 ret
= MulDiv(device
->pwfx
->nSamplesPerSec
* device
->pwfx
->nBlockAlign
, period
, 10000000);
60 ret
-= ret
% device
->pwfx
->nBlockAlign
;
64 static HRESULT
DSOUND_WaveFormat(DirectSoundDevice
*device
, IAudioClient
*client
,
65 BOOL forcewave
, WAVEFORMATEX
**wfx
)
67 WAVEFORMATEXTENSIBLE
*retwfe
= NULL
;
72 WAVEFORMATEXTENSIBLE
*mixwfe
;
73 hr
= IAudioClient_GetMixFormat(client
, (WAVEFORMATEX
**)&mixwfe
);
78 if (mixwfe
->Format
.nChannels
> 2) {
81 FIXME("Limiting channels to 2 due to lack of multichannel support\n");
83 mixwfe
->Format
.nChannels
= 2;
84 mixwfe
->Format
.nBlockAlign
= mixwfe
->Format
.nChannels
* mixwfe
->Format
.wBitsPerSample
/ 8;
85 mixwfe
->Format
.nAvgBytesPerSec
= mixwfe
->Format
.nSamplesPerSec
* mixwfe
->Format
.nBlockAlign
;
86 mixwfe
->dwChannelMask
= SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
;
89 if (!IsEqualGUID(&mixwfe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)) {
90 WAVEFORMATEXTENSIBLE testwfe
= *mixwfe
;
92 testwfe
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
93 testwfe
.Samples
.wValidBitsPerSample
= testwfe
.Format
.wBitsPerSample
= 32;
94 testwfe
.Format
.nBlockAlign
= testwfe
.Format
.nChannels
* testwfe
.Format
.wBitsPerSample
/ 8;
95 testwfe
.Format
.nAvgBytesPerSec
= testwfe
.Format
.nSamplesPerSec
* testwfe
.Format
.nBlockAlign
;
97 if (FAILED(IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
, &testwfe
.Format
, (WAVEFORMATEX
**)&retwfe
)))
98 w
= DSOUND_CopyFormat(&mixwfe
->Format
);
100 w
= DSOUND_CopyFormat(&retwfe
->Format
);
102 w
= DSOUND_CopyFormat(&testwfe
.Format
);
103 CoTaskMemFree(retwfe
);
106 w
= DSOUND_CopyFormat(&mixwfe
->Format
);
107 CoTaskMemFree(mixwfe
);
108 } else if (device
->primary_pwfx
->wFormatTag
== WAVE_FORMAT_PCM
||
109 device
->primary_pwfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
110 WAVEFORMATEX
*wi
= device
->primary_pwfx
;
111 WAVEFORMATEXTENSIBLE
*wfe
;
113 /* Convert to WAVEFORMATEXTENSIBLE */
114 w
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE
));
115 wfe
= (WAVEFORMATEXTENSIBLE
*)w
;
117 return DSERR_OUTOFMEMORY
;
120 w
->wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
121 w
->cbSize
= sizeof(*wfe
) - sizeof(*w
);
122 w
->nBlockAlign
= w
->nChannels
* w
->wBitsPerSample
/ 8;
123 w
->nAvgBytesPerSec
= w
->nSamplesPerSec
* w
->nBlockAlign
;
125 wfe
->dwChannelMask
= 0;
126 if (wi
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
127 w
->wBitsPerSample
= 32;
128 wfe
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
130 wfe
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
131 wfe
->Samples
.wValidBitsPerSample
= w
->wBitsPerSample
;
133 w
= DSOUND_CopyFormat(device
->primary_pwfx
);
136 return DSERR_OUTOFMEMORY
;
138 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
, w
, (WAVEFORMATEX
**)&retwfe
);
140 memcpy(w
, retwfe
, sizeof(WAVEFORMATEX
) + retwfe
->Format
.cbSize
);
141 CoTaskMemFree(retwfe
);
144 WARN("IsFormatSupported failed: %08x\n", hr
);
145 HeapFree(GetProcessHeap(), 0, w
);
152 HRESULT
DSOUND_ReopenDevice(DirectSoundDevice
*device
, BOOL forcewave
)
155 REFERENCE_TIME prebuf_rt
;
156 WAVEFORMATEX
*wfx
= NULL
;
158 REFERENCE_TIME period
;
161 TRACE("(%p, %d)\n", device
, forcewave
);
164 IAudioClient_Release(device
->client
);
165 device
->client
= NULL
;
168 IAudioRenderClient_Release(device
->render
);
169 device
->render
= NULL
;
172 IAudioClock_Release(device
->clock
);
173 device
->clock
= NULL
;
176 IAudioStreamVolume_Release(device
->volume
);
177 device
->volume
= NULL
;
180 hres
= IMMDevice_Activate(device
->mmdevice
, &IID_IAudioClient
,
181 CLSCTX_INPROC_SERVER
, NULL
, (void **)&device
->client
);
183 WARN("Activate failed: %08x\n", hres
);
187 hres
= DSOUND_WaveFormat(device
, device
->client
, forcewave
, &wfx
);
189 IAudioClient_Release(device
->client
);
190 device
->client
= NULL
;
193 HeapFree(GetProcessHeap(), 0, device
->pwfx
);
196 prebuf_frames
= device
->prebuf
* DSOUND_fraglen(device
) / device
->pwfx
->nBlockAlign
;
197 prebuf_rt
= (10000000 * (UINT64
)prebuf_frames
) / device
->pwfx
->nSamplesPerSec
;
199 hres
= IAudioClient_Initialize(device
->client
,
200 AUDCLNT_SHAREMODE_SHARED
, AUDCLNT_STREAMFLAGS_NOPERSIST
|
201 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
, prebuf_rt
, 0, device
->pwfx
, NULL
);
203 IAudioClient_Release(device
->client
);
204 device
->client
= NULL
;
205 WARN("Initialize failed: %08x\n", hres
);
208 IAudioClient_SetEventHandle(device
->client
, device
->sleepev
);
210 hres
= IAudioClient_GetService(device
->client
, &IID_IAudioRenderClient
,
211 (void**)&device
->render
);
213 IAudioClient_Release(device
->client
);
214 device
->client
= NULL
;
215 WARN("GetService failed: %08x\n", hres
);
219 hres
= IAudioClient_GetService(device
->client
, &IID_IAudioClock
,
220 (void**)&device
->clock
);
222 IAudioClient_Release(device
->client
);
223 IAudioRenderClient_Release(device
->render
);
224 device
->client
= NULL
;
225 device
->render
= NULL
;
226 WARN("GetService failed: %08x\n", hres
);
230 hres
= IAudioClient_GetService(device
->client
, &IID_IAudioStreamVolume
,
231 (void**)&device
->volume
);
233 IAudioClient_Release(device
->client
);
234 IAudioRenderClient_Release(device
->render
);
235 IAudioClock_Release(device
->clock
);
236 device
->client
= NULL
;
237 device
->render
= NULL
;
238 device
->clock
= NULL
;
239 WARN("GetService failed: %08x\n", hres
);
243 /* Now kick off the timer so the event fires periodically */
244 hres
= IAudioClient_Start(device
->client
);
246 WARN("starting failed with %08x\n", hres
);
248 hres
= IAudioClient_GetStreamLatency(device
->client
, &period
);
250 WARN("GetStreamLatency failed with %08x\n", hres
);
253 period_ms
= (period
+ 9999) / 10000;
254 TRACE("period %u ms fraglen %u prebuf %u\n", period_ms
, device
->fraglen
, device
->prebuf
);
257 device
->sleeptime
= 5;
259 device
->sleeptime
= period_ms
* 5 / 2;
264 HRESULT
DSOUND_PrimaryOpen(DirectSoundDevice
*device
)
266 IDirectSoundBufferImpl
** dsb
= device
->buffers
;
270 TRACE("(%p)\n", device
);
272 device
->fraglen
= DSOUND_fraglen(device
);
274 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
275 on windows this size is always fixed (tested on win-xp) */
277 device
->buflen
= ds_hel_buflen
;
278 device
->buflen
-= device
->buflen
% device
->pwfx
->nBlockAlign
;
279 while(device
->buflen
< device
->fraglen
* device
->prebuf
){
280 device
->buflen
+= ds_hel_buflen
;
281 device
->buflen
-= device
->buflen
% device
->pwfx
->nBlockAlign
;
284 HeapFree(GetProcessHeap(), 0, device
->mix_buffer
);
285 device
->mix_buffer_len
= (device
->buflen
/ (device
->pwfx
->wBitsPerSample
/ 8)) * sizeof(float);
286 device
->mix_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, device
->mix_buffer_len
);
287 if (!device
->mix_buffer
)
288 return DSERR_OUTOFMEMORY
;
290 if (device
->state
== STATE_PLAYING
) device
->state
= STATE_STARTING
;
291 else if (device
->state
== STATE_STOPPING
) device
->state
= STATE_STOPPED
;
293 /* reallocate emulated primary buffer */
295 newbuf
= HeapReAlloc(GetProcessHeap(),0,device
->buffer
, device
->buflen
);
297 newbuf
= HeapAlloc(GetProcessHeap(),0, device
->buflen
);
300 ERR("failed to allocate primary buffer\n");
301 return DSERR_OUTOFMEMORY
;
302 /* but the old buffer might still exist and must be re-prepared */
305 device
->writelead
= (device
->pwfx
->nSamplesPerSec
/ 100) * device
->pwfx
->nBlockAlign
;
307 device
->buffer
= newbuf
;
309 TRACE("buflen: %u, fraglen: %u, mix_buffer_len: %u\n",
310 device
->buflen
, device
->fraglen
, device
->mix_buffer_len
);
312 if(device
->pwfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
313 (device
->pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
314 IsEqualGUID(&((WAVEFORMATEXTENSIBLE
*)device
->pwfx
)->SubFormat
,
315 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)))
316 device
->normfunction
= normfunctions
[4];
318 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
320 FillMemory(device
->buffer
, device
->buflen
, (device
->pwfx
->wBitsPerSample
== 8) ? 128 : 0);
321 FillMemory(device
->mix_buffer
, device
->mix_buffer_len
, 0);
324 if (device
->pwfx
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
325 (device
->pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
326 IsEqualGUID(&((WAVEFORMATEXTENSIBLE
*)device
->pwfx
)->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)))
327 device
->normfunction
= normfunctions
[4];
329 device
->normfunction
= normfunctions
[device
->pwfx
->wBitsPerSample
/8 - 1];
331 for (i
= 0; i
< device
->nrofbuffers
; i
++) {
332 RtlAcquireResourceExclusive(&dsb
[i
]->lock
, TRUE
);
333 DSOUND_RecalcFormat(dsb
[i
]);
334 RtlReleaseResource(&dsb
[i
]->lock
);
341 static void DSOUND_PrimaryClose(DirectSoundDevice
*device
)
345 TRACE("(%p)\n", device
);
348 hr
= IAudioClient_Stop(device
->client
);
350 WARN("Stop failed: %08x\n", hr
);
353 /* clear the queue */
354 device
->in_mmdev_bytes
= 0;
357 HRESULT
DSOUND_PrimaryCreate(DirectSoundDevice
*device
)
360 TRACE("(%p)\n", device
);
362 device
->buflen
= ds_hel_buflen
;
363 err
= DSOUND_PrimaryOpen(device
);
366 WARN("DSOUND_PrimaryOpen failed\n");
370 device
->state
= STATE_STOPPED
;
374 HRESULT
DSOUND_PrimaryDestroy(DirectSoundDevice
*device
)
376 TRACE("(%p)\n", device
);
379 EnterCriticalSection(&(device
->mixlock
));
381 DSOUND_PrimaryClose(device
);
383 if(device
->primary
&& (device
->primary
->ref
|| device
->primary
->numIfaces
))
384 WARN("Destroying primary buffer while references held (%u %u)\n", device
->primary
->ref
, device
->primary
->numIfaces
);
386 HeapFree(GetProcessHeap(), 0, device
->primary
);
387 device
->primary
= NULL
;
389 HeapFree(GetProcessHeap(),0,device
->primary_pwfx
);
390 HeapFree(GetProcessHeap(),0,device
->pwfx
);
393 LeaveCriticalSection(&(device
->mixlock
));
399 HRESULT
DSOUND_PrimaryPlay(DirectSoundDevice
*device
)
403 TRACE("(%p)\n", device
);
405 hr
= IAudioClient_Start(device
->client
);
406 if(FAILED(hr
) && hr
!= AUDCLNT_E_NOT_STOPPED
){
407 WARN("Start failed: %08x\n", hr
);
414 HRESULT
DSOUND_PrimaryStop(DirectSoundDevice
*device
)
418 TRACE("(%p)\n", device
);
420 hr
= IAudioClient_Stop(device
->client
);
422 WARN("Stop failed: %08x\n", hr
);
429 HRESULT
DSOUND_PrimaryGetPosition(DirectSoundDevice
*device
, LPDWORD playpos
, LPDWORD writepos
)
431 TRACE("(%p,%p,%p)\n", device
, playpos
, writepos
);
433 /* check if playpos was requested */
435 *playpos
= device
->playing_offs_bytes
;
437 /* check if writepos was requested */
439 /* the writepos is the first non-queued position */
440 *writepos
= (device
->playing_offs_bytes
+ device
->in_mmdev_bytes
) % device
->buflen
;
442 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:-1, writepos
?*writepos
:-1, device
, GetTickCount());
446 static DWORD
DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex
)
448 if (wfex
->wFormatTag
== WAVE_FORMAT_PCM
)
449 return sizeof(WAVEFORMATEX
);
451 return sizeof(WAVEFORMATEX
) + wfex
->cbSize
;
454 LPWAVEFORMATEX
DSOUND_CopyFormat(LPCWAVEFORMATEX wfex
)
456 DWORD size
= DSOUND_GetFormatSize(wfex
);
457 LPWAVEFORMATEX pwfx
= HeapAlloc(GetProcessHeap(),0,size
);
459 WARN("out of memory\n");
460 } else if (wfex
->wFormatTag
!= WAVE_FORMAT_PCM
) {
461 CopyMemory(pwfx
, wfex
, size
);
463 CopyMemory(pwfx
, wfex
, sizeof(PCMWAVEFORMAT
));
465 if (pwfx
->nBlockAlign
!= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8) {
466 WARN("Fixing bad nBlockAlign (%u)\n", pwfx
->nBlockAlign
);
467 pwfx
->nBlockAlign
= pwfx
->nChannels
* pwfx
->wBitsPerSample
/8;
469 if (pwfx
->nAvgBytesPerSec
!= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
) {
470 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx
->nAvgBytesPerSec
);
471 pwfx
->nAvgBytesPerSec
= pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
;
477 HRESULT
primarybuffer_SetFormat(DirectSoundDevice
*device
, LPCWAVEFORMATEX passed_fmt
)
480 WAVEFORMATEX
*old_fmt
;
481 WAVEFORMATEXTENSIBLE
*fmtex
, *passed_fmtex
= (WAVEFORMATEXTENSIBLE
*)passed_fmt
;
482 BOOL forced
= (device
->priolevel
== DSSCL_WRITEPRIMARY
);
484 TRACE("(%p,%p)\n", device
, passed_fmt
);
486 if (device
->priolevel
== DSSCL_NORMAL
) {
487 WARN("failed priority check!\n");
488 return DSERR_PRIOLEVELNEEDED
;
491 /* Let's be pedantic! */
492 if (passed_fmt
== NULL
) {
493 WARN("invalid parameter: passed_fmt==NULL!\n");
494 return DSERR_INVALIDPARAM
;
496 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
497 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
498 passed_fmt
->wFormatTag
, passed_fmt
->nChannels
, passed_fmt
->nSamplesPerSec
,
499 passed_fmt
->nAvgBytesPerSec
, passed_fmt
->nBlockAlign
,
500 passed_fmt
->wBitsPerSample
, passed_fmt
->cbSize
);
502 if(passed_fmt
->wBitsPerSample
< 8 || passed_fmt
->wBitsPerSample
% 8 != 0 ||
503 passed_fmt
->nChannels
== 0 || passed_fmt
->nSamplesPerSec
== 0 ||
504 passed_fmt
->nAvgBytesPerSec
== 0 ||
505 passed_fmt
->nBlockAlign
!= passed_fmt
->nChannels
* passed_fmt
->wBitsPerSample
/ 8)
506 return DSERR_INVALIDPARAM
;
508 if(passed_fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
509 if(passed_fmtex
->Samples
.wValidBitsPerSample
> passed_fmtex
->Format
.wBitsPerSample
)
510 return DSERR_INVALIDPARAM
;
514 RtlAcquireResourceExclusive(&(device
->buffer_list_lock
), TRUE
);
515 EnterCriticalSection(&(device
->mixlock
));
517 if (device
->priolevel
== DSSCL_WRITEPRIMARY
) {
518 old_fmt
= device
->primary_pwfx
;
519 device
->primary_pwfx
= DSOUND_CopyFormat(passed_fmt
);
520 fmtex
= (WAVEFORMATEXTENSIBLE
*)device
->primary_pwfx
;
521 if (device
->primary_pwfx
== NULL
) {
522 err
= DSERR_OUTOFMEMORY
;
526 if (fmtex
->Format
.wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
527 fmtex
->Samples
.wValidBitsPerSample
== 0) {
528 TRACE("Correcting 0 valid bits per sample\n");
529 fmtex
->Samples
.wValidBitsPerSample
= fmtex
->Format
.wBitsPerSample
;
532 DSOUND_PrimaryClose(device
);
534 err
= DSOUND_ReopenDevice(device
, forced
);
536 ERR("No formats could be opened\n");
540 err
= DSOUND_PrimaryOpen(device
);
542 ERR("DSOUND_PrimaryOpen failed\n");
548 device
->primary_pwfx
= old_fmt
;
550 HeapFree(GetProcessHeap(), 0, old_fmt
);
551 } else if (passed_fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
552 passed_fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
553 /* Fill in "real" values to primary_pwfx */
554 WAVEFORMATEX
*fmt
= device
->primary_pwfx
;
556 *fmt
= *device
->pwfx
;
557 fmtex
= (void*)device
->pwfx
;
559 if (IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
) &&
560 passed_fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) {
561 fmt
->wFormatTag
= WAVE_FORMAT_IEEE_FLOAT
;
563 fmt
->wFormatTag
= WAVE_FORMAT_PCM
;
564 fmt
->wBitsPerSample
= 16;
566 fmt
->nBlockAlign
= fmt
->nChannels
* fmt
->wBitsPerSample
/ 8;
567 fmt
->nAvgBytesPerSec
= fmt
->nBlockAlign
* fmt
->nSamplesPerSec
;
570 device
->primary_pwfx
= HeapReAlloc(GetProcessHeap(), 0, device
->primary_pwfx
, sizeof(*fmtex
));
571 memcpy(device
->primary_pwfx
, device
->pwfx
, sizeof(*fmtex
));
575 LeaveCriticalSection(&(device
->mixlock
));
576 RtlReleaseResource(&(device
->buffer_list_lock
));
582 /*******************************************************************************
585 static inline IDirectSoundBufferImpl
*impl_from_IDirectSoundBuffer(IDirectSoundBuffer
*iface
)
587 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
588 return CONTAINING_RECORD(iface
, IDirectSoundBufferImpl
, IDirectSoundBuffer8_iface
);
591 /* This sets this format for the primary buffer only */
592 static HRESULT WINAPI
PrimaryBufferImpl_SetFormat(IDirectSoundBuffer
*iface
,
593 const WAVEFORMATEX
*wfex
)
595 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
596 TRACE("(%p,%p)\n", iface
, wfex
);
597 return primarybuffer_SetFormat(This
->device
, wfex
);
600 static HRESULT WINAPI
PrimaryBufferImpl_SetVolume(IDirectSoundBuffer
*iface
, LONG vol
)
602 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
603 DirectSoundDevice
*device
= This
->device
;
607 TRACE("(%p,%d)\n", iface
, vol
);
609 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
610 WARN("control unavailable\n");
611 return DSERR_CONTROLUNAVAIL
;
614 if ((vol
> DSBVOLUME_MAX
) || (vol
< DSBVOLUME_MIN
)) {
615 WARN("invalid parameter: vol = %d\n", vol
);
616 return DSERR_INVALIDPARAM
;
620 EnterCriticalSection(&device
->mixlock
);
622 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
624 LeaveCriticalSection(&device
->mixlock
);
625 WARN("GetChannelVolume failed: %08x\n", hr
);
629 if(device
->pwfx
->nChannels
> 1){
630 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
632 LeaveCriticalSection(&device
->mixlock
);
633 WARN("GetChannelVolume failed: %08x\n", hr
);
639 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
640 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
642 DSOUND_AmpFactorToVolPan(&device
->volpan
);
643 if (vol
!= device
->volpan
.lVolume
) {
644 device
->volpan
.lVolume
=vol
;
645 DSOUND_RecalcVolPan(&device
->volpan
);
646 lvol
= (float)((DWORD
)(device
->volpan
.dwTotalLeftAmpFactor
& 0xFFFF) / (float)0xFFFF);
647 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 0, lvol
);
649 LeaveCriticalSection(&device
->mixlock
);
650 WARN("SetChannelVolume failed: %08x\n", hr
);
654 if(device
->pwfx
->nChannels
> 1){
655 rvol
= (float)((DWORD
)(device
->volpan
.dwTotalRightAmpFactor
& 0xFFFF) / (float)0xFFFF);
656 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 1, rvol
);
658 LeaveCriticalSection(&device
->mixlock
);
659 WARN("SetChannelVolume failed: %08x\n", hr
);
665 LeaveCriticalSection(&(device
->mixlock
));
671 static HRESULT WINAPI
PrimaryBufferImpl_GetVolume(IDirectSoundBuffer
*iface
, LONG
*vol
)
673 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
674 DirectSoundDevice
*device
= This
->device
;
677 TRACE("(%p,%p)\n", iface
, vol
);
679 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
)) {
680 WARN("control unavailable\n");
681 return DSERR_CONTROLUNAVAIL
;
685 WARN("invalid parameter: vol = NULL\n");
686 return DSERR_INVALIDPARAM
;
689 EnterCriticalSection(&device
->mixlock
);
691 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
693 LeaveCriticalSection(&device
->mixlock
);
694 WARN("GetChannelVolume failed: %08x\n", hr
);
698 if(device
->pwfx
->nChannels
> 1){
699 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
701 LeaveCriticalSection(&device
->mixlock
);
702 WARN("GetChannelVolume failed: %08x\n", hr
);
708 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
709 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
711 DSOUND_AmpFactorToVolPan(&device
->volpan
);
712 *vol
= device
->volpan
.lVolume
;
714 LeaveCriticalSection(&device
->mixlock
);
719 static HRESULT WINAPI
PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer
*iface
, DWORD freq
)
721 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
722 TRACE("(%p,%d)\n",This
,freq
);
724 /* You cannot set the frequency of the primary buffer */
725 WARN("control unavailable\n");
726 return DSERR_CONTROLUNAVAIL
;
729 static HRESULT WINAPI
PrimaryBufferImpl_Play(IDirectSoundBuffer
*iface
, DWORD reserved1
,
730 DWORD reserved2
, DWORD flags
)
732 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
733 DirectSoundDevice
*device
= This
->device
;
734 TRACE("(%p,%08x,%08x,%08x)\n", iface
, reserved1
, reserved2
, flags
);
736 if (!(flags
& DSBPLAY_LOOPING
)) {
737 WARN("invalid parameter: flags = %08x\n", flags
);
738 return DSERR_INVALIDPARAM
;
742 EnterCriticalSection(&(device
->mixlock
));
744 if (device
->state
== STATE_STOPPED
)
745 device
->state
= STATE_STARTING
;
746 else if (device
->state
== STATE_STOPPING
)
747 device
->state
= STATE_PLAYING
;
749 LeaveCriticalSection(&(device
->mixlock
));
755 static HRESULT WINAPI
PrimaryBufferImpl_Stop(IDirectSoundBuffer
*iface
)
757 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
758 DirectSoundDevice
*device
= This
->device
;
759 TRACE("(%p)\n", iface
);
762 EnterCriticalSection(&(device
->mixlock
));
764 if (device
->state
== STATE_PLAYING
)
765 device
->state
= STATE_STOPPING
;
766 else if (device
->state
== STATE_STARTING
)
767 device
->state
= STATE_STOPPED
;
769 LeaveCriticalSection(&(device
->mixlock
));
775 static ULONG WINAPI
PrimaryBufferImpl_AddRef(IDirectSoundBuffer
*iface
)
777 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
778 ULONG ref
= InterlockedIncrement(&(This
->ref
));
779 TRACE("(%p) ref was %d\n", This
, ref
- 1);
781 InterlockedIncrement(&This
->numIfaces
);
785 /* Decreases *out by 1 to no less than 0.
786 * Returns the new value of *out. */
787 LONG
capped_refcount_dec(LONG
*out
)
794 oldref
= InterlockedCompareExchange(out
, ref
- 1, ref
);
795 } while(oldref
!= ref
);
799 static ULONG WINAPI
PrimaryBufferImpl_Release(IDirectSoundBuffer
*iface
)
801 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
804 ref
= capped_refcount_dec(&This
->ref
);
806 capped_refcount_dec(&This
->numIfaces
);
808 TRACE("(%p) primary ref is now %d\n", This
, ref
);
813 static HRESULT WINAPI
PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer
*iface
,
814 DWORD
*playpos
, DWORD
*writepos
)
817 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
818 DirectSoundDevice
*device
= This
->device
;
819 TRACE("(%p,%p,%p)\n", iface
, playpos
, writepos
);
822 EnterCriticalSection(&(device
->mixlock
));
824 hres
= DSOUND_PrimaryGetPosition(device
, playpos
, writepos
);
826 WARN("DSOUND_PrimaryGetPosition failed\n");
827 LeaveCriticalSection(&(device
->mixlock
));
831 if (device
->state
!= STATE_STOPPED
)
832 /* apply the documented 10ms lead to writepos */
833 *writepos
+= device
->writelead
;
834 while (*writepos
>= device
->buflen
) *writepos
-= device
->buflen
;
837 LeaveCriticalSection(&(device
->mixlock
));
840 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos
?*playpos
:0, writepos
?*writepos
:0, device
, GetTickCount());
844 static HRESULT WINAPI
PrimaryBufferImpl_GetStatus(IDirectSoundBuffer
*iface
, DWORD
*status
)
846 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
847 DirectSoundDevice
*device
= This
->device
;
848 TRACE("(%p,%p)\n", iface
, status
);
850 if (status
== NULL
) {
851 WARN("invalid parameter: status == NULL\n");
852 return DSERR_INVALIDPARAM
;
856 if ((device
->state
== STATE_STARTING
) ||
857 (device
->state
== STATE_PLAYING
))
858 *status
|= DSBSTATUS_PLAYING
| DSBSTATUS_LOOPING
;
860 TRACE("status=%x\n", *status
);
865 static HRESULT WINAPI
PrimaryBufferImpl_GetFormat(IDirectSoundBuffer
*iface
, WAVEFORMATEX
*lpwf
,
866 DWORD wfsize
, DWORD
*wfwritten
)
869 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
870 DirectSoundDevice
*device
= This
->device
;
871 TRACE("(%p,%p,%d,%p)\n", iface
, lpwf
, wfsize
, wfwritten
);
873 size
= sizeof(WAVEFORMATEX
) + device
->primary_pwfx
->cbSize
;
875 if (lpwf
) { /* NULL is valid */
876 if (wfsize
>= size
) {
877 CopyMemory(lpwf
,device
->primary_pwfx
,size
);
881 WARN("invalid parameter: wfsize too small\n");
884 return DSERR_INVALIDPARAM
;
888 *wfwritten
= sizeof(WAVEFORMATEX
) + device
->primary_pwfx
->cbSize
;
890 WARN("invalid parameter: wfwritten == NULL\n");
891 return DSERR_INVALIDPARAM
;
898 static HRESULT WINAPI
PrimaryBufferImpl_Lock(IDirectSoundBuffer
*iface
, DWORD writecursor
,
899 DWORD writebytes
, void **lplpaudioptr1
, DWORD
*audiobytes1
, void **lplpaudioptr2
,
900 DWORD
*audiobytes2
, DWORD flags
)
903 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
904 DirectSoundDevice
*device
= This
->device
;
905 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
918 return DSERR_INVALIDPARAM
;
920 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
921 WARN("failed priority check!\n");
922 return DSERR_PRIOLEVELNEEDED
;
925 /* when this flag is set, writecursor is meaningless and must be calculated */
926 if (flags
& DSBLOCK_FROMWRITECURSOR
) {
927 /* GetCurrentPosition does too much magic to duplicate here */
928 hres
= IDirectSoundBuffer_GetCurrentPosition(iface
, NULL
, &writecursor
);
930 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
935 /* when this flag is set, writebytes is meaningless and must be set */
936 if (flags
& DSBLOCK_ENTIREBUFFER
)
937 writebytes
= device
->buflen
;
939 if (writecursor
>= device
->buflen
) {
940 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
941 writecursor
, device
->buflen
);
942 return DSERR_INVALIDPARAM
;
945 if (writebytes
> device
->buflen
) {
946 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
947 writebytes
, device
->buflen
);
948 return DSERR_INVALIDPARAM
;
951 if (writecursor
+writebytes
<= device
->buflen
) {
952 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
953 *audiobytes1
= writebytes
;
955 *(LPBYTE
*)lplpaudioptr2
= NULL
;
958 TRACE("->%d.0\n",writebytes
);
960 *(LPBYTE
*)lplpaudioptr1
= device
->buffer
+writecursor
;
961 *audiobytes1
= device
->buflen
-writecursor
;
963 *(LPBYTE
*)lplpaudioptr2
= device
->buffer
;
965 *audiobytes2
= writebytes
-(device
->buflen
-writecursor
);
966 TRACE("->%d.%d\n",*audiobytes1
,audiobytes2
?*audiobytes2
:0);
971 static HRESULT WINAPI
PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer
*iface
, DWORD newpos
)
973 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
974 TRACE("(%p,%d)\n",This
,newpos
);
976 /* You cannot set the position of the primary buffer */
977 WARN("invalid call\n");
978 return DSERR_INVALIDCALL
;
981 static HRESULT WINAPI
PrimaryBufferImpl_SetPan(IDirectSoundBuffer
*iface
, LONG pan
)
983 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
984 DirectSoundDevice
*device
= This
->device
;
987 TRACE("(%p,%d)\n", iface
, pan
);
989 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
990 WARN("control unavailable\n");
991 return DSERR_CONTROLUNAVAIL
;
994 if ((pan
> DSBPAN_RIGHT
) || (pan
< DSBPAN_LEFT
)) {
995 WARN("invalid parameter: pan = %d\n", pan
);
996 return DSERR_INVALIDPARAM
;
1000 EnterCriticalSection(&device
->mixlock
);
1002 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
1004 LeaveCriticalSection(&device
->mixlock
);
1005 WARN("GetChannelVolume failed: %08x\n", hr
);
1009 if(device
->pwfx
->nChannels
> 1){
1010 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
1012 LeaveCriticalSection(&device
->mixlock
);
1013 WARN("GetChannelVolume failed: %08x\n", hr
);
1019 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
1020 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
1022 DSOUND_AmpFactorToVolPan(&device
->volpan
);
1023 if (pan
!= device
->volpan
.lPan
) {
1024 device
->volpan
.lPan
=pan
;
1025 DSOUND_RecalcVolPan(&device
->volpan
);
1027 lvol
= (float)((DWORD
)(device
->volpan
.dwTotalLeftAmpFactor
& 0xFFFF) / (float)0xFFFF);
1028 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 0, lvol
);
1030 LeaveCriticalSection(&device
->mixlock
);
1031 WARN("SetChannelVolume failed: %08x\n", hr
);
1035 if(device
->pwfx
->nChannels
> 1){
1036 rvol
= (float)((DWORD
)(device
->volpan
.dwTotalRightAmpFactor
& 0xFFFF) / (float)0xFFFF);
1037 hr
= IAudioStreamVolume_SetChannelVolume(device
->volume
, 1, rvol
);
1039 LeaveCriticalSection(&device
->mixlock
);
1040 WARN("SetChannelVolume failed: %08x\n", hr
);
1046 LeaveCriticalSection(&device
->mixlock
);
1052 static HRESULT WINAPI
PrimaryBufferImpl_GetPan(IDirectSoundBuffer
*iface
, LONG
*pan
)
1054 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1055 DirectSoundDevice
*device
= This
->device
;
1058 TRACE("(%p,%p)\n", iface
, pan
);
1060 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
)) {
1061 WARN("control unavailable\n");
1062 return DSERR_CONTROLUNAVAIL
;
1066 WARN("invalid parameter: pan == NULL\n");
1067 return DSERR_INVALIDPARAM
;
1070 EnterCriticalSection(&device
->mixlock
);
1072 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 0, &lvol
);
1074 LeaveCriticalSection(&device
->mixlock
);
1075 WARN("GetChannelVolume failed: %08x\n", hr
);
1079 if(device
->pwfx
->nChannels
> 1){
1080 hr
= IAudioStreamVolume_GetChannelVolume(device
->volume
, 1, &rvol
);
1082 LeaveCriticalSection(&device
->mixlock
);
1083 WARN("GetChannelVolume failed: %08x\n", hr
);
1089 device
->volpan
.dwTotalLeftAmpFactor
= ((UINT16
)(lvol
* (DWORD
)0xFFFF));
1090 device
->volpan
.dwTotalRightAmpFactor
= ((UINT16
)(rvol
* (DWORD
)0xFFFF));
1092 DSOUND_AmpFactorToVolPan(&device
->volpan
);
1093 *pan
= device
->volpan
.lPan
;
1095 LeaveCriticalSection(&device
->mixlock
);
1100 static HRESULT WINAPI
PrimaryBufferImpl_Unlock(IDirectSoundBuffer
*iface
, void *p1
, DWORD x1
,
1103 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1104 DirectSoundDevice
*device
= This
->device
;
1105 TRACE("(%p,%p,%d,%p,%d)\n", iface
, p1
, x1
, p2
, x2
);
1107 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
1108 WARN("failed priority check!\n");
1109 return DSERR_PRIOLEVELNEEDED
;
1112 if ((p1
&& ((BYTE
*)p1
< device
->buffer
|| (BYTE
*)p1
>= device
->buffer
+ device
->buflen
)) ||
1113 (p2
&& ((BYTE
*)p2
< device
->buffer
|| (BYTE
*)p2
>= device
->buffer
+ device
->buflen
)))
1114 return DSERR_INVALIDPARAM
;
1119 static HRESULT WINAPI
PrimaryBufferImpl_Restore(IDirectSoundBuffer
*iface
)
1121 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1122 FIXME("(%p):stub\n",This
);
1126 static HRESULT WINAPI
PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer
*iface
, DWORD
*freq
)
1128 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1129 DirectSoundDevice
*device
= This
->device
;
1130 TRACE("(%p,%p)\n", iface
, freq
);
1133 WARN("invalid parameter: freq == NULL\n");
1134 return DSERR_INVALIDPARAM
;
1137 if (!(This
->dsbd
.dwFlags
& DSBCAPS_CTRLFREQUENCY
)) {
1138 WARN("control unavailable\n");
1139 return DSERR_CONTROLUNAVAIL
;
1142 *freq
= device
->pwfx
->nSamplesPerSec
;
1143 TRACE("-> %d\n", *freq
);
1148 static HRESULT WINAPI
PrimaryBufferImpl_Initialize(IDirectSoundBuffer
*iface
, IDirectSound
*dsound
,
1149 const DSBUFFERDESC
*dbsd
)
1151 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1152 WARN("(%p) already initialized\n", This
);
1153 return DSERR_ALREADYINITIALIZED
;
1156 static HRESULT WINAPI
PrimaryBufferImpl_GetCaps(IDirectSoundBuffer
*iface
, DSBCAPS
*caps
)
1158 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1159 DirectSoundDevice
*device
= This
->device
;
1160 TRACE("(%p,%p)\n", iface
, caps
);
1163 WARN("invalid parameter: caps == NULL\n");
1164 return DSERR_INVALIDPARAM
;
1167 if (caps
->dwSize
< sizeof(*caps
)) {
1168 WARN("invalid parameter: caps->dwSize = %d\n", caps
->dwSize
);
1169 return DSERR_INVALIDPARAM
;
1172 caps
->dwFlags
= This
->dsbd
.dwFlags
;
1173 caps
->dwBufferBytes
= device
->buflen
;
1175 /* Windows reports these as zero */
1176 caps
->dwUnlockTransferRate
= 0;
1177 caps
->dwPlayCpuOverhead
= 0;
1182 static HRESULT WINAPI
PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer
*iface
, REFIID riid
,
1185 IDirectSoundBufferImpl
*This
= impl_from_IDirectSoundBuffer(iface
);
1187 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(riid
), ppobj
);
1189 if (ppobj
== NULL
) {
1190 WARN("invalid parameter\n");
1191 return E_INVALIDARG
;
1194 *ppobj
= NULL
; /* assume failure */
1196 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
1197 IsEqualGUID(riid
, &IID_IDirectSoundBuffer
) ) {
1198 IDirectSoundBuffer_AddRef(iface
);
1203 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1204 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1205 if ( IsEqualGUID( &IID_IDirectSoundBuffer8
, riid
) ) {
1206 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1207 return E_NOINTERFACE
;
1210 if ( IsEqualGUID( &IID_IDirectSoundNotify
, riid
) ) {
1211 ERR("app requested IDirectSoundNotify on primary buffer\n");
1212 /* FIXME: should we support this? */
1213 return E_NOINTERFACE
;
1216 if ( IsEqualGUID( &IID_IDirectSound3DBuffer
, riid
) ) {
1217 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1218 return E_NOINTERFACE
;
1221 if ( IsEqualGUID( &IID_IDirectSound3DListener
, riid
) ) {
1222 *ppobj
= &This
->IDirectSound3DListener_iface
;
1223 IDirectSound3DListener_AddRef(&This
->IDirectSound3DListener_iface
);
1227 if ( IsEqualGUID( &IID_IKsPropertySet
, riid
) ) {
1228 *ppobj
= &This
->IKsPropertySet_iface
;
1229 IKsPropertySet_AddRef(&This
->IKsPropertySet_iface
);
1233 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
1234 return E_NOINTERFACE
;
1237 static const IDirectSoundBufferVtbl dspbvt
=
1239 PrimaryBufferImpl_QueryInterface
,
1240 PrimaryBufferImpl_AddRef
,
1241 PrimaryBufferImpl_Release
,
1242 PrimaryBufferImpl_GetCaps
,
1243 PrimaryBufferImpl_GetCurrentPosition
,
1244 PrimaryBufferImpl_GetFormat
,
1245 PrimaryBufferImpl_GetVolume
,
1246 PrimaryBufferImpl_GetPan
,
1247 PrimaryBufferImpl_GetFrequency
,
1248 PrimaryBufferImpl_GetStatus
,
1249 PrimaryBufferImpl_Initialize
,
1250 PrimaryBufferImpl_Lock
,
1251 PrimaryBufferImpl_Play
,
1252 PrimaryBufferImpl_SetCurrentPosition
,
1253 PrimaryBufferImpl_SetFormat
,
1254 PrimaryBufferImpl_SetVolume
,
1255 PrimaryBufferImpl_SetPan
,
1256 PrimaryBufferImpl_SetFrequency
,
1257 PrimaryBufferImpl_Stop
,
1258 PrimaryBufferImpl_Unlock
,
1259 PrimaryBufferImpl_Restore
1262 HRESULT
primarybuffer_create(DirectSoundDevice
*device
, IDirectSoundBufferImpl
**ppdsb
,
1263 const DSBUFFERDESC
*dsbd
)
1265 IDirectSoundBufferImpl
*dsb
;
1266 TRACE("%p,%p,%p)\n",device
,ppdsb
,dsbd
);
1268 if (dsbd
->lpwfxFormat
) {
1269 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1271 return DSERR_INVALIDPARAM
;
1274 dsb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(*dsb
));
1277 WARN("out of memory\n");
1279 return DSERR_OUTOFMEMORY
;
1286 dsb
->device
= device
;
1287 dsb
->IDirectSoundBuffer8_iface
.lpVtbl
= (IDirectSoundBuffer8Vtbl
*)&dspbvt
;
1288 dsb
->IDirectSound3DListener_iface
.lpVtbl
= &ds3dlvt
;
1289 dsb
->IKsPropertySet_iface
.lpVtbl
= &iksbvt
;
1292 /* IDirectSound3DListener */
1293 device
->ds3dl
.dwSize
= sizeof(DS3DLISTENER
);
1294 device
->ds3dl
.vPosition
.x
= 0.0;
1295 device
->ds3dl
.vPosition
.y
= 0.0;
1296 device
->ds3dl
.vPosition
.z
= 0.0;
1297 device
->ds3dl
.vVelocity
.x
= 0.0;
1298 device
->ds3dl
.vVelocity
.y
= 0.0;
1299 device
->ds3dl
.vVelocity
.z
= 0.0;
1300 device
->ds3dl
.vOrientFront
.x
= 0.0;
1301 device
->ds3dl
.vOrientFront
.y
= 0.0;
1302 device
->ds3dl
.vOrientFront
.z
= 1.0;
1303 device
->ds3dl
.vOrientTop
.x
= 0.0;
1304 device
->ds3dl
.vOrientTop
.y
= 1.0;
1305 device
->ds3dl
.vOrientTop
.z
= 0.0;
1306 device
->ds3dl
.flDistanceFactor
= DS3D_DEFAULTDISTANCEFACTOR
;
1307 device
->ds3dl
.flRolloffFactor
= DS3D_DEFAULTROLLOFFFACTOR
;
1308 device
->ds3dl
.flDopplerFactor
= DS3D_DEFAULTDOPPLERFACTOR
;
1309 device
->ds3dl_need_recalc
= TRUE
;
1311 TRACE("Created primary buffer at %p\n", dsb
);
1312 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1313 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1314 device
->pwfx
->wFormatTag
, device
->pwfx
->nChannels
,
1315 device
->pwfx
->nSamplesPerSec
, device
->pwfx
->nAvgBytesPerSec
,
1316 device
->pwfx
->nBlockAlign
, device
->pwfx
->wBitsPerSample
,
1317 device
->pwfx
->cbSize
);
1319 IDirectSoundBuffer_AddRef(&dsb
->IDirectSoundBuffer8_iface
);