dsound: Ensure primary buffer's buffer is large enough to hold the entire prebuffer.
[wine.git] / dlls / dsound / primary.c
blob7fed78e564aa2265bf5847117af2f7f0c5258557
1 /* DirectSound
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
21 * TODO:
22 * When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23 * it leaves dsound in unusable (not really open) state.
26 #include <stdarg.h>
28 #define COBJMACROS
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "mmsystem.h"
35 #include "winternl.h"
36 #include "mmddk.h"
37 #include "wine/debug.h"
38 #include "dsound.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
43 static DWORD DSOUND_fraglen(DirectSoundDevice *device)
45 REFERENCE_TIME period;
46 HRESULT hr;
47 DWORD ret;
49 hr = IAudioClient_GetDevicePeriod(device->client, &period, NULL);
50 if(FAILED(hr)){
51 /* just guess at 10ms */
52 WARN("GetDevicePeriod failed: %08x\n", hr);
53 ret = MulDiv(device->pwfx->nBlockAlign, device->pwfx->nSamplesPerSec, 100);
54 }else
55 ret = MulDiv(device->pwfx->nSamplesPerSec * device->pwfx->nBlockAlign, period, 10000000);
57 ret -= ret % device->pwfx->nBlockAlign;
58 return ret;
61 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
63 UINT prebuf_frames;
64 REFERENCE_TIME prebuf_rt;
65 HRESULT hres;
67 TRACE("(%p, %d)\n", device, forcewave);
69 if(device->client){
70 IAudioClient_Release(device->client);
71 device->client = NULL;
73 if(device->render){
74 IAudioRenderClient_Release(device->render);
75 device->render = NULL;
77 if(device->clock){
78 IAudioClock_Release(device->clock);
79 device->clock = NULL;
81 if(device->volume){
82 IAudioStreamVolume_Release(device->volume);
83 device->volume = NULL;
86 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
87 CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
88 if(FAILED(hres)){
89 WARN("Activate failed: %08x\n", hres);
90 return hres;
93 prebuf_frames = device->prebuf * DSOUND_fraglen(device) / device->pwfx->nBlockAlign;
94 prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
96 hres = IAudioClient_Initialize(device->client,
97 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST,
98 prebuf_rt, 0, device->pwfx, NULL);
99 if(FAILED(hres)){
100 IAudioClient_Release(device->client);
101 device->client = NULL;
102 WARN("Initialize failed: %08x\n", hres);
103 return hres;
106 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
107 (void**)&device->render);
108 if(FAILED(hres)){
109 IAudioClient_Release(device->client);
110 device->client = NULL;
111 WARN("GetService failed: %08x\n", hres);
112 return hres;
115 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
116 (void**)&device->clock);
117 if(FAILED(hres)){
118 IAudioClient_Release(device->client);
119 IAudioRenderClient_Release(device->render);
120 device->client = NULL;
121 device->render = NULL;
122 WARN("GetService failed: %08x\n", hres);
123 return hres;
126 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
127 (void**)&device->volume);
128 if(FAILED(hres)){
129 IAudioClient_Release(device->client);
130 IAudioRenderClient_Release(device->render);
131 IAudioClock_Release(device->clock);
132 device->client = NULL;
133 device->render = NULL;
134 device->clock = NULL;
135 WARN("GetService failed: %08x\n", hres);
136 return hres;
139 return S_OK;
142 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
144 LPBYTE newbuf;
146 TRACE("(%p)\n", device);
148 device->fraglen = DSOUND_fraglen(device);
150 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
151 on windows this size is always fixed (tested on win-xp) */
152 if (!device->buflen)
153 device->buflen = ds_hel_buflen;
154 device->buflen -= device->buflen % device->pwfx->nBlockAlign;
155 while(device->buflen < device->fraglen * device->prebuf){
156 device->buflen += ds_hel_buflen;
157 device->buflen -= device->buflen % device->pwfx->nBlockAlign;
160 device->helfrags = device->buflen / device->fraglen;
162 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
163 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
164 if (!device->mix_buffer)
165 return DSERR_OUTOFMEMORY;
167 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
168 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
170 /* reallocate emulated primary buffer */
171 if (device->buffer)
172 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, device->buflen);
173 else
174 newbuf = HeapAlloc(GetProcessHeap(),0, device->buflen);
176 if (!newbuf) {
177 ERR("failed to allocate primary buffer\n");
178 return DSERR_OUTOFMEMORY;
179 /* but the old buffer might still exist and must be re-prepared */
182 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
184 device->buffer = newbuf;
186 TRACE("buflen: %u, fraglen: %u, helfrags: %u, mix_buffer_len: %u\n",
187 device->buflen, device->fraglen, device->helfrags, device->mix_buffer_len);
189 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
190 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
191 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
192 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
193 device->last_pos_bytes = device->playing_offs_bytes = device->in_mmdev_bytes = device->playpos = device->mixpos = 0;
194 return DS_OK;
198 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
200 HRESULT hr;
202 TRACE("(%p)\n", device);
204 if(device->client){
205 hr = IAudioClient_Stop(device->client);
206 if(FAILED(hr))
207 WARN("Stop failed: %08x\n", hr);
210 /* clear the queue */
211 device->in_mmdev_bytes = 0;
214 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
216 HRESULT err = DS_OK;
217 TRACE("(%p)\n", device);
219 device->buflen = ds_hel_buflen;
220 err = DSOUND_PrimaryOpen(device);
222 if (err != DS_OK) {
223 WARN("DSOUND_PrimaryOpen failed\n");
224 return err;
227 device->state = STATE_STOPPED;
228 return DS_OK;
231 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
233 TRACE("(%p)\n", device);
235 /* **** */
236 EnterCriticalSection(&(device->mixlock));
238 DSOUND_PrimaryClose(device);
240 if(device->primary && (device->primary->ref || device->primary->numIfaces))
241 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
243 HeapFree(GetProcessHeap(), 0, device->primary);
244 device->primary = NULL;
246 HeapFree(GetProcessHeap(),0,device->pwfx);
247 device->pwfx=NULL;
249 LeaveCriticalSection(&(device->mixlock));
250 /* **** */
252 return DS_OK;
255 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
257 HRESULT hr;
259 TRACE("(%p)\n", device);
261 hr = IAudioClient_Start(device->client);
262 if(FAILED(hr)){
263 WARN("Start failed: %08x\n", hr);
264 return hr;
267 return DS_OK;
270 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
272 HRESULT hr;
274 TRACE("(%p)\n", device);
276 hr = IAudioClient_Stop(device->client);
277 if(FAILED(hr)){
278 WARN("Stop failed: %08x\n", hr);
279 return hr;
282 return DS_OK;
285 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
287 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
289 /* check if playpos was requested */
290 if (playpos)
291 *playpos = device->playing_offs_bytes;
293 /* check if writepos was requested */
294 if (writepos)
295 /* the writepos is the first non-queued position */
296 *writepos = (device->playing_offs_bytes + device->in_mmdev_bytes) % device->buflen;
298 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
299 return DS_OK;
302 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
304 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
305 return sizeof(WAVEFORMATEX);
306 else
307 return sizeof(WAVEFORMATEX) + wfex->cbSize;
310 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
312 DWORD size = DSOUND_GetFormatSize(wfex);
313 LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
314 if (pwfx == NULL) {
315 WARN("out of memory\n");
316 } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
317 CopyMemory(pwfx, wfex, size);
318 } else {
319 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
320 pwfx->cbSize=0;
321 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
322 WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
323 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample/8;
325 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
326 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
327 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
330 return pwfx;
333 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
335 HRESULT err = DSERR_BUFFERLOST;
336 int i;
337 WAVEFORMATEX *old_fmt;
338 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
339 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
341 TRACE("(%p,%p)\n", device, passed_fmt);
343 if (device->priolevel == DSSCL_NORMAL) {
344 WARN("failed priority check!\n");
345 return DSERR_PRIOLEVELNEEDED;
348 /* Let's be pedantic! */
349 if (passed_fmt == NULL) {
350 WARN("invalid parameter: passed_fmt==NULL!\n");
351 return DSERR_INVALIDPARAM;
353 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
354 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
355 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
356 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
357 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
359 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
360 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
361 passed_fmt->nAvgBytesPerSec == 0 ||
362 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
363 return DSERR_INVALIDPARAM;
365 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
366 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
367 return DSERR_INVALIDPARAM;
370 /* **** */
371 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
372 EnterCriticalSection(&(device->mixlock));
374 old_fmt = device->pwfx;
375 device->pwfx = DSOUND_CopyFormat(passed_fmt);
376 fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
377 if (device->pwfx == NULL) {
378 device->pwfx = old_fmt;
379 old_fmt = NULL;
380 err = DSERR_OUTOFMEMORY;
381 goto done;
384 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
385 if(fmtex->Samples.wValidBitsPerSample == 0){
386 TRACE("Correcting 0 valid bits per sample\n");
387 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
391 DSOUND_PrimaryClose(device);
393 err = DSOUND_ReopenDevice(device, FALSE);
394 if(SUCCEEDED(err))
395 goto opened;
397 /* requested format failed, so try others */
398 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
399 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
400 device->pwfx->wBitsPerSample = 32;
401 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
402 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
404 err = DSOUND_ReopenDevice(device, FALSE);
405 if(SUCCEEDED(err))
406 goto opened;
409 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
410 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
411 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
412 device->pwfx->wBitsPerSample = 32;
413 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
414 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
416 err = DSOUND_ReopenDevice(device, FALSE);
417 if(SUCCEEDED(err))
418 goto opened;
421 device->pwfx->wBitsPerSample = 32;
422 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
423 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
424 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
425 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
426 err = DSOUND_ReopenDevice(device, FALSE);
427 if(SUCCEEDED(err))
428 goto opened;
430 device->pwfx->wBitsPerSample = 16;
431 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
432 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
433 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
434 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
435 err = DSOUND_ReopenDevice(device, FALSE);
436 if(SUCCEEDED(err))
437 goto opened;
439 device->pwfx->wBitsPerSample = 8;
440 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
441 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
442 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
443 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
444 err = DSOUND_ReopenDevice(device, FALSE);
445 if(SUCCEEDED(err))
446 goto opened;
448 device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
449 device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
450 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
451 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
452 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
453 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
454 err = DSOUND_ReopenDevice(device, FALSE);
455 if(SUCCEEDED(err))
456 goto opened;
458 device->pwfx->wBitsPerSample = 32;
459 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
460 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
461 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
462 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
463 err = DSOUND_ReopenDevice(device, FALSE);
464 if(SUCCEEDED(err))
465 goto opened;
467 device->pwfx->wBitsPerSample = 16;
468 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
469 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
470 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
471 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
472 err = DSOUND_ReopenDevice(device, FALSE);
473 if(SUCCEEDED(err))
474 goto opened;
476 device->pwfx->wBitsPerSample = 8;
477 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
478 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
479 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
480 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
481 err = DSOUND_ReopenDevice(device, FALSE);
482 if(SUCCEEDED(err))
483 goto opened;
485 WARN("No formats could be opened\n");
486 goto done;
488 opened:
489 err = DSOUND_PrimaryOpen(device);
490 if (err != DS_OK) {
491 WARN("DSOUND_PrimaryOpen failed\n");
492 goto done;
495 if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
497 DSOUND_PrimaryClose(device);
498 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
499 err = DSOUND_ReopenDevice(device, TRUE);
500 if (FAILED(err))
501 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
502 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
503 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
506 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
507 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
508 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
509 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
510 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
512 if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
513 old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
514 old_fmt->nChannels != device->pwfx->nChannels) {
515 IDirectSoundBufferImpl** dsb = device->buffers;
516 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
517 /* **** */
518 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
520 (*dsb)->freqAdjust = (*dsb)->freq / (float)device->pwfx->nSamplesPerSec;
521 DSOUND_RecalcFormat((*dsb));
522 (*dsb)->primary_mixpos = 0;
524 RtlReleaseResource(&(*dsb)->lock);
525 /* **** */
529 done:
530 LeaveCriticalSection(&(device->mixlock));
531 RtlReleaseResource(&(device->buffer_list_lock));
532 /* **** */
534 HeapFree(GetProcessHeap(), 0, old_fmt);
535 return err;
538 /*******************************************************************************
539 * PrimaryBuffer
541 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
543 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
544 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
547 /* This sets this format for the primary buffer only */
548 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
549 const WAVEFORMATEX *wfex)
551 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
552 TRACE("(%p,%p)\n", iface, wfex);
553 return primarybuffer_SetFormat(This->device, wfex);
556 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
558 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
559 DirectSoundDevice *device = This->device;
560 HRESULT hr;
561 float lvol, rvol;
563 TRACE("(%p,%d)\n", iface, vol);
565 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
566 WARN("control unavailable\n");
567 return DSERR_CONTROLUNAVAIL;
570 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
571 WARN("invalid parameter: vol = %d\n", vol);
572 return DSERR_INVALIDPARAM;
575 /* **** */
576 EnterCriticalSection(&device->mixlock);
578 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
579 if(FAILED(hr)){
580 LeaveCriticalSection(&device->mixlock);
581 WARN("GetChannelVolume failed: %08x\n", hr);
582 return hr;
585 if(device->pwfx->nChannels > 1){
586 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
587 if(FAILED(hr)){
588 LeaveCriticalSection(&device->mixlock);
589 WARN("GetChannelVolume failed: %08x\n", hr);
590 return hr;
592 }else
593 rvol = 1;
595 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
596 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
598 DSOUND_AmpFactorToVolPan(&device->volpan);
599 if (vol != device->volpan.lVolume) {
600 device->volpan.lVolume=vol;
601 DSOUND_RecalcVolPan(&device->volpan);
602 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
603 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
604 if(FAILED(hr)){
605 LeaveCriticalSection(&device->mixlock);
606 WARN("SetChannelVolume failed: %08x\n", hr);
607 return hr;
610 if(device->pwfx->nChannels > 1){
611 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
612 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
613 if(FAILED(hr)){
614 LeaveCriticalSection(&device->mixlock);
615 WARN("SetChannelVolume failed: %08x\n", hr);
616 return hr;
621 LeaveCriticalSection(&(device->mixlock));
622 /* **** */
624 return DS_OK;
627 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
629 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
630 DirectSoundDevice *device = This->device;
631 float lvol, rvol;
632 HRESULT hr;
633 TRACE("(%p,%p)\n", iface, vol);
635 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
636 WARN("control unavailable\n");
637 return DSERR_CONTROLUNAVAIL;
640 if (vol == NULL) {
641 WARN("invalid parameter: vol = NULL\n");
642 return DSERR_INVALIDPARAM;
645 EnterCriticalSection(&device->mixlock);
647 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
648 if(FAILED(hr)){
649 LeaveCriticalSection(&device->mixlock);
650 WARN("GetChannelVolume failed: %08x\n", hr);
651 return hr;
654 if(device->pwfx->nChannels > 1){
655 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
656 if(FAILED(hr)){
657 LeaveCriticalSection(&device->mixlock);
658 WARN("GetChannelVolume failed: %08x\n", hr);
659 return hr;
661 }else
662 rvol = 1;
664 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
665 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
667 DSOUND_AmpFactorToVolPan(&device->volpan);
668 *vol = device->volpan.lVolume;
670 LeaveCriticalSection(&device->mixlock);
672 return DS_OK;
675 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
677 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
678 TRACE("(%p,%d)\n",This,freq);
680 /* You cannot set the frequency of the primary buffer */
681 WARN("control unavailable\n");
682 return DSERR_CONTROLUNAVAIL;
685 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
686 DWORD reserved2, DWORD flags)
688 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
689 DirectSoundDevice *device = This->device;
690 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
692 if (!(flags & DSBPLAY_LOOPING)) {
693 WARN("invalid parameter: flags = %08x\n", flags);
694 return DSERR_INVALIDPARAM;
697 /* **** */
698 EnterCriticalSection(&(device->mixlock));
700 if (device->state == STATE_STOPPED)
701 device->state = STATE_STARTING;
702 else if (device->state == STATE_STOPPING)
703 device->state = STATE_PLAYING;
705 LeaveCriticalSection(&(device->mixlock));
706 /* **** */
708 return DS_OK;
711 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
713 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
714 DirectSoundDevice *device = This->device;
715 TRACE("(%p)\n", iface);
717 /* **** */
718 EnterCriticalSection(&(device->mixlock));
720 if (device->state == STATE_PLAYING)
721 device->state = STATE_STOPPING;
722 else if (device->state == STATE_STARTING)
723 device->state = STATE_STOPPED;
725 LeaveCriticalSection(&(device->mixlock));
726 /* **** */
728 return DS_OK;
731 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
733 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
734 ULONG ref = InterlockedIncrement(&(This->ref));
735 TRACE("(%p) ref was %d\n", This, ref - 1);
736 if(ref == 1)
737 InterlockedIncrement(&This->numIfaces);
738 return ref;
741 /* Decreases *out by 1 to no less than 0.
742 * Returns the new value of *out. */
743 LONG capped_refcount_dec(LONG *out)
745 LONG ref, oldref;
746 do {
747 ref = *out;
748 if(!ref)
749 return 0;
750 oldref = InterlockedCompareExchange(out, ref - 1, ref);
751 } while(oldref != ref);
752 return ref - 1;
755 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
757 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
758 ULONG ref;
760 ref = capped_refcount_dec(&This->ref);
761 if(!ref)
762 capped_refcount_dec(&This->numIfaces);
764 TRACE("(%p) primary ref is now %d\n", This, ref);
766 return ref;
769 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
770 DWORD *playpos, DWORD *writepos)
772 HRESULT hres;
773 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
774 DirectSoundDevice *device = This->device;
775 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
777 /* **** */
778 EnterCriticalSection(&(device->mixlock));
780 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
781 if (hres != DS_OK) {
782 WARN("DSOUND_PrimaryGetPosition failed\n");
783 LeaveCriticalSection(&(device->mixlock));
784 return hres;
786 if (writepos) {
787 if (device->state != STATE_STOPPED)
788 /* apply the documented 10ms lead to writepos */
789 *writepos += device->writelead;
790 while (*writepos >= device->buflen) *writepos -= device->buflen;
793 LeaveCriticalSection(&(device->mixlock));
794 /* **** */
796 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
797 return DS_OK;
800 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
802 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
803 DirectSoundDevice *device = This->device;
804 TRACE("(%p,%p)\n", iface, status);
806 if (status == NULL) {
807 WARN("invalid parameter: status == NULL\n");
808 return DSERR_INVALIDPARAM;
811 *status = 0;
812 if ((device->state == STATE_STARTING) ||
813 (device->state == STATE_PLAYING))
814 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
816 TRACE("status=%x\n", *status);
817 return DS_OK;
821 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
822 DWORD wfsize, DWORD *wfwritten)
824 DWORD size;
825 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
826 DirectSoundDevice *device = This->device;
827 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
829 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
831 if (lpwf) { /* NULL is valid */
832 if (wfsize >= size) {
833 CopyMemory(lpwf,device->pwfx,size);
834 if (wfwritten)
835 *wfwritten = size;
836 } else {
837 WARN("invalid parameter: wfsize too small\n");
838 if (wfwritten)
839 *wfwritten = 0;
840 return DSERR_INVALIDPARAM;
842 } else {
843 if (wfwritten)
844 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
845 else {
846 WARN("invalid parameter: wfwritten == NULL\n");
847 return DSERR_INVALIDPARAM;
851 return DS_OK;
854 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
855 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
856 DWORD *audiobytes2, DWORD flags)
858 HRESULT hres;
859 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
860 DirectSoundDevice *device = This->device;
861 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
862 iface,
863 writecursor,
864 writebytes,
865 lplpaudioptr1,
866 audiobytes1,
867 lplpaudioptr2,
868 audiobytes2,
869 flags,
870 GetTickCount()
873 if (!audiobytes1)
874 return DSERR_INVALIDPARAM;
876 if (device->priolevel != DSSCL_WRITEPRIMARY) {
877 WARN("failed priority check!\n");
878 return DSERR_PRIOLEVELNEEDED;
881 /* when this flag is set, writecursor is meaningless and must be calculated */
882 if (flags & DSBLOCK_FROMWRITECURSOR) {
883 /* GetCurrentPosition does too much magic to duplicate here */
884 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
885 if (hres != DS_OK) {
886 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
887 return hres;
891 /* when this flag is set, writebytes is meaningless and must be set */
892 if (flags & DSBLOCK_ENTIREBUFFER)
893 writebytes = device->buflen;
895 if (writecursor >= device->buflen) {
896 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
897 writecursor, device->buflen);
898 return DSERR_INVALIDPARAM;
901 if (writebytes > device->buflen) {
902 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
903 writebytes, device->buflen);
904 return DSERR_INVALIDPARAM;
907 if (writecursor+writebytes <= device->buflen) {
908 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
909 *audiobytes1 = writebytes;
910 if (lplpaudioptr2)
911 *(LPBYTE*)lplpaudioptr2 = NULL;
912 if (audiobytes2)
913 *audiobytes2 = 0;
914 TRACE("->%d.0\n",writebytes);
915 } else {
916 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
917 *audiobytes1 = device->buflen-writecursor;
918 if (lplpaudioptr2)
919 *(LPBYTE*)lplpaudioptr2 = device->buffer;
920 if (audiobytes2)
921 *audiobytes2 = writebytes-(device->buflen-writecursor);
922 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
924 return DS_OK;
927 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
929 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
930 TRACE("(%p,%d)\n",This,newpos);
932 /* You cannot set the position of the primary buffer */
933 WARN("invalid call\n");
934 return DSERR_INVALIDCALL;
937 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
939 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
940 DirectSoundDevice *device = This->device;
941 float lvol, rvol;
942 HRESULT hr;
943 TRACE("(%p,%d)\n", iface, pan);
945 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
946 WARN("control unavailable\n");
947 return DSERR_CONTROLUNAVAIL;
950 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
951 WARN("invalid parameter: pan = %d\n", pan);
952 return DSERR_INVALIDPARAM;
955 /* **** */
956 EnterCriticalSection(&device->mixlock);
958 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
959 if(FAILED(hr)){
960 LeaveCriticalSection(&device->mixlock);
961 WARN("GetChannelVolume failed: %08x\n", hr);
962 return hr;
965 if(device->pwfx->nChannels > 1){
966 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
967 if(FAILED(hr)){
968 LeaveCriticalSection(&device->mixlock);
969 WARN("GetChannelVolume failed: %08x\n", hr);
970 return hr;
972 }else
973 rvol = 1;
975 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
976 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
978 DSOUND_AmpFactorToVolPan(&device->volpan);
979 if (pan != device->volpan.lPan) {
980 device->volpan.lPan=pan;
981 DSOUND_RecalcVolPan(&device->volpan);
983 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
984 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
985 if(FAILED(hr)){
986 LeaveCriticalSection(&device->mixlock);
987 WARN("SetChannelVolume failed: %08x\n", hr);
988 return hr;
991 if(device->pwfx->nChannels > 1){
992 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
993 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
994 if(FAILED(hr)){
995 LeaveCriticalSection(&device->mixlock);
996 WARN("SetChannelVolume failed: %08x\n", hr);
997 return hr;
1002 LeaveCriticalSection(&device->mixlock);
1003 /* **** */
1005 return DS_OK;
1008 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1010 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1011 DirectSoundDevice *device = This->device;
1012 float lvol, rvol;
1013 HRESULT hr;
1014 TRACE("(%p,%p)\n", iface, pan);
1016 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1017 WARN("control unavailable\n");
1018 return DSERR_CONTROLUNAVAIL;
1021 if (pan == NULL) {
1022 WARN("invalid parameter: pan == NULL\n");
1023 return DSERR_INVALIDPARAM;
1026 EnterCriticalSection(&device->mixlock);
1028 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1029 if(FAILED(hr)){
1030 LeaveCriticalSection(&device->mixlock);
1031 WARN("GetChannelVolume failed: %08x\n", hr);
1032 return hr;
1035 if(device->pwfx->nChannels > 1){
1036 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1037 if(FAILED(hr)){
1038 LeaveCriticalSection(&device->mixlock);
1039 WARN("GetChannelVolume failed: %08x\n", hr);
1040 return hr;
1042 }else
1043 rvol = 1;
1045 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1046 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1048 DSOUND_AmpFactorToVolPan(&device->volpan);
1049 *pan = device->volpan.lPan;
1051 LeaveCriticalSection(&device->mixlock);
1053 return DS_OK;
1056 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1057 void *p2, DWORD x2)
1059 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1060 DirectSoundDevice *device = This->device;
1061 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1063 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1064 WARN("failed priority check!\n");
1065 return DSERR_PRIOLEVELNEEDED;
1068 if((p1 && ((BYTE*)p1 < device->buffer ||
1069 (BYTE*)p1 >= device->buffer + device->buflen)) ||
1070 (p2 && ((BYTE*)p2 < device->buffer ||
1071 (BYTE*)p2 >= device->buffer + device->buflen)))
1072 return DSERR_INVALIDPARAM;
1074 return DS_OK;
1077 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1079 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1080 FIXME("(%p):stub\n",This);
1081 return DS_OK;
1084 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1086 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1087 DirectSoundDevice *device = This->device;
1088 TRACE("(%p,%p)\n", iface, freq);
1090 if (freq == NULL) {
1091 WARN("invalid parameter: freq == NULL\n");
1092 return DSERR_INVALIDPARAM;
1095 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1096 WARN("control unavailable\n");
1097 return DSERR_CONTROLUNAVAIL;
1100 *freq = device->pwfx->nSamplesPerSec;
1101 TRACE("-> %d\n", *freq);
1103 return DS_OK;
1106 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1107 const DSBUFFERDESC *dbsd)
1109 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1110 WARN("(%p) already initialized\n", This);
1111 return DSERR_ALREADYINITIALIZED;
1114 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1116 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1117 DirectSoundDevice *device = This->device;
1118 TRACE("(%p,%p)\n", iface, caps);
1120 if (caps == NULL) {
1121 WARN("invalid parameter: caps == NULL\n");
1122 return DSERR_INVALIDPARAM;
1125 if (caps->dwSize < sizeof(*caps)) {
1126 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1127 return DSERR_INVALIDPARAM;
1130 caps->dwFlags = This->dsbd.dwFlags;
1131 caps->dwBufferBytes = device->buflen;
1133 /* Windows reports these as zero */
1134 caps->dwUnlockTransferRate = 0;
1135 caps->dwPlayCpuOverhead = 0;
1137 return DS_OK;
1140 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1141 void **ppobj)
1143 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1145 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1147 if (ppobj == NULL) {
1148 WARN("invalid parameter\n");
1149 return E_INVALIDARG;
1152 *ppobj = NULL; /* assume failure */
1154 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1155 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1156 IDirectSoundBuffer_AddRef(iface);
1157 *ppobj = iface;
1158 return S_OK;
1161 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1162 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1163 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1164 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1165 return E_NOINTERFACE;
1168 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1169 ERR("app requested IDirectSoundNotify on primary buffer\n");
1170 /* FIXME: should we support this? */
1171 return E_NOINTERFACE;
1174 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1175 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1176 return E_NOINTERFACE;
1179 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1180 *ppobj = &This->IDirectSound3DListener_iface;
1181 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1182 return S_OK;
1185 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1186 *ppobj = &This->IKsPropertySet_iface;
1187 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1188 return S_OK;
1191 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1192 return E_NOINTERFACE;
1195 static const IDirectSoundBufferVtbl dspbvt =
1197 PrimaryBufferImpl_QueryInterface,
1198 PrimaryBufferImpl_AddRef,
1199 PrimaryBufferImpl_Release,
1200 PrimaryBufferImpl_GetCaps,
1201 PrimaryBufferImpl_GetCurrentPosition,
1202 PrimaryBufferImpl_GetFormat,
1203 PrimaryBufferImpl_GetVolume,
1204 PrimaryBufferImpl_GetPan,
1205 PrimaryBufferImpl_GetFrequency,
1206 PrimaryBufferImpl_GetStatus,
1207 PrimaryBufferImpl_Initialize,
1208 PrimaryBufferImpl_Lock,
1209 PrimaryBufferImpl_Play,
1210 PrimaryBufferImpl_SetCurrentPosition,
1211 PrimaryBufferImpl_SetFormat,
1212 PrimaryBufferImpl_SetVolume,
1213 PrimaryBufferImpl_SetPan,
1214 PrimaryBufferImpl_SetFrequency,
1215 PrimaryBufferImpl_Stop,
1216 PrimaryBufferImpl_Unlock,
1217 PrimaryBufferImpl_Restore
1220 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1221 const DSBUFFERDESC *dsbd)
1223 IDirectSoundBufferImpl *dsb;
1224 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1226 if (dsbd->lpwfxFormat) {
1227 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1228 *ppdsb = NULL;
1229 return DSERR_INVALIDPARAM;
1232 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1234 if (dsb == NULL) {
1235 WARN("out of memory\n");
1236 *ppdsb = NULL;
1237 return DSERR_OUTOFMEMORY;
1240 dsb->ref = 0;
1241 dsb->ref3D = 0;
1242 dsb->refiks = 0;
1243 dsb->numIfaces = 0;
1244 dsb->device = device;
1245 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1246 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1247 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1248 dsb->dsbd = *dsbd;
1250 /* IDirectSound3DListener */
1251 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1252 device->ds3dl.vPosition.x = 0.0;
1253 device->ds3dl.vPosition.y = 0.0;
1254 device->ds3dl.vPosition.z = 0.0;
1255 device->ds3dl.vVelocity.x = 0.0;
1256 device->ds3dl.vVelocity.y = 0.0;
1257 device->ds3dl.vVelocity.z = 0.0;
1258 device->ds3dl.vOrientFront.x = 0.0;
1259 device->ds3dl.vOrientFront.y = 0.0;
1260 device->ds3dl.vOrientFront.z = 1.0;
1261 device->ds3dl.vOrientTop.x = 0.0;
1262 device->ds3dl.vOrientTop.y = 1.0;
1263 device->ds3dl.vOrientTop.z = 0.0;
1264 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1265 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1266 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1267 device->ds3dl_need_recalc = TRUE;
1269 TRACE("Created primary buffer at %p\n", dsb);
1270 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1271 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1272 device->pwfx->wFormatTag, device->pwfx->nChannels,
1273 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1274 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1275 device->pwfx->cbSize);
1277 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1278 *ppdsb = dsb;
1279 return S_OK;