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 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
64 REFERENCE_TIME prebuf_rt;
67 TRACE("(%p, %d)\n", device, forcewave);
70 IAudioClient_Release(device->client);
71 device->client = NULL;
74 IAudioRenderClient_Release(device->render);
75 device->render = NULL;
78 IAudioClock_Release(device->clock);
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);
89 WARN("Activate failed: %08x\n", 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);
100 IAudioClient_Release(device->client);
101 device->client = NULL;
102 WARN("Initialize failed: %08x\n", hres);
106 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
107 (void**)&device->render);
109 IAudioClient_Release(device->client);
110 device->client = NULL;
111 WARN("GetService failed: %08x\n", hres);
115 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
116 (void**)&device->clock);
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);
126 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
127 (void**)&device->volume);
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);
142 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
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) */
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 */
172 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, device->buflen);
174 newbuf = HeapAlloc(GetProcessHeap(),0, device->buflen);
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 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
190 (device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
191 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)device->pwfx)->SubFormat,
192 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
193 device->mixfunction = mixfunctions[4];
194 device->normfunction = normfunctions[4];
196 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
197 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
200 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
201 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
202 device->last_pos_bytes = device->playing_offs_bytes = device->in_mmdev_bytes = device->playpos = device->mixpos = 0;
207 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
211 TRACE("(%p)\n", device);
214 hr = IAudioClient_Stop(device->client);
216 WARN("Stop failed: %08x\n", hr);
219 /* clear the queue */
220 device->in_mmdev_bytes = 0;
223 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
226 TRACE("(%p)\n", device);
228 device->buflen = ds_hel_buflen;
229 err = DSOUND_PrimaryOpen(device);
232 WARN("DSOUND_PrimaryOpen failed\n");
236 device->state = STATE_STOPPED;
240 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
242 TRACE("(%p)\n", device);
245 EnterCriticalSection(&(device->mixlock));
247 DSOUND_PrimaryClose(device);
249 if(device->primary && (device->primary->ref || device->primary->numIfaces))
250 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
252 HeapFree(GetProcessHeap(), 0, device->primary);
253 device->primary = NULL;
255 HeapFree(GetProcessHeap(),0,device->pwfx);
258 LeaveCriticalSection(&(device->mixlock));
264 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
268 TRACE("(%p)\n", device);
270 hr = IAudioClient_Start(device->client);
272 WARN("Start failed: %08x\n", hr);
279 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
283 TRACE("(%p)\n", device);
285 hr = IAudioClient_Stop(device->client);
287 WARN("Stop failed: %08x\n", hr);
294 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
296 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
298 /* check if playpos was requested */
300 *playpos = device->playing_offs_bytes;
302 /* check if writepos was requested */
304 /* the writepos is the first non-queued position */
305 *writepos = (device->playing_offs_bytes + device->in_mmdev_bytes) % device->buflen;
307 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
311 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
313 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
314 return sizeof(WAVEFORMATEX);
316 return sizeof(WAVEFORMATEX) + wfex->cbSize;
319 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
321 DWORD size = DSOUND_GetFormatSize(wfex);
322 LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
324 WARN("out of memory\n");
325 } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
326 CopyMemory(pwfx, wfex, size);
328 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
330 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
331 WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
332 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample/8;
334 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
335 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
336 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
342 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
344 HRESULT err = DSERR_BUFFERLOST;
346 WAVEFORMATEX *old_fmt;
347 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
348 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
350 TRACE("(%p,%p)\n", device, passed_fmt);
352 if (device->priolevel == DSSCL_NORMAL) {
353 WARN("failed priority check!\n");
354 return DSERR_PRIOLEVELNEEDED;
357 /* Let's be pedantic! */
358 if (passed_fmt == NULL) {
359 WARN("invalid parameter: passed_fmt==NULL!\n");
360 return DSERR_INVALIDPARAM;
362 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
363 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
364 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
365 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
366 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
368 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
369 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
370 passed_fmt->nAvgBytesPerSec == 0 ||
371 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
372 return DSERR_INVALIDPARAM;
374 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
375 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
376 return DSERR_INVALIDPARAM;
380 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
381 EnterCriticalSection(&(device->mixlock));
383 old_fmt = device->pwfx;
384 device->pwfx = DSOUND_CopyFormat(passed_fmt);
385 fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
386 if (device->pwfx == NULL) {
387 device->pwfx = old_fmt;
389 err = DSERR_OUTOFMEMORY;
393 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
394 if(fmtex->Samples.wValidBitsPerSample == 0){
395 TRACE("Correcting 0 valid bits per sample\n");
396 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
400 DSOUND_PrimaryClose(device);
402 err = DSOUND_ReopenDevice(device, FALSE);
406 /* requested format failed, so try others */
407 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
408 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
409 device->pwfx->wBitsPerSample = 32;
410 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
411 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
413 err = DSOUND_ReopenDevice(device, FALSE);
418 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
419 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
420 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
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);
425 err = DSOUND_ReopenDevice(device, FALSE);
430 device->pwfx->wBitsPerSample = 32;
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);
439 device->pwfx->wBitsPerSample = 16;
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);
448 device->pwfx->wBitsPerSample = 8;
449 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
450 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
451 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
452 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
453 err = DSOUND_ReopenDevice(device, FALSE);
457 device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
458 device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
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);
467 device->pwfx->wBitsPerSample = 32;
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);
476 device->pwfx->wBitsPerSample = 16;
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);
485 device->pwfx->wBitsPerSample = 8;
486 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
487 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
488 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
489 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
490 err = DSOUND_ReopenDevice(device, FALSE);
494 WARN("No formats could be opened\n");
498 err = DSOUND_PrimaryOpen(device);
500 WARN("DSOUND_PrimaryOpen failed\n");
504 if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
506 DSOUND_PrimaryClose(device);
507 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
508 err = DSOUND_ReopenDevice(device, TRUE);
510 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
511 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
512 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
515 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
516 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
517 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
519 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
520 (device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
521 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)device->pwfx)->SubFormat,
522 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
523 device->mixfunction = mixfunctions[4];
524 device->normfunction = normfunctions[4];
526 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
527 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
530 if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
531 old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
532 old_fmt->nChannels != device->pwfx->nChannels) {
533 IDirectSoundBufferImpl** dsb = device->buffers;
534 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
536 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
538 (*dsb)->freqAdjust = (*dsb)->freq / (float)device->pwfx->nSamplesPerSec;
539 DSOUND_RecalcFormat((*dsb));
541 RtlReleaseResource(&(*dsb)->lock);
547 LeaveCriticalSection(&(device->mixlock));
548 RtlReleaseResource(&(device->buffer_list_lock));
551 HeapFree(GetProcessHeap(), 0, old_fmt);
555 /*******************************************************************************
558 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
560 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
561 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
564 /* This sets this format for the primary buffer only */
565 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
566 const WAVEFORMATEX *wfex)
568 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
569 TRACE("(%p,%p)\n", iface, wfex);
570 return primarybuffer_SetFormat(This->device, wfex);
573 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
575 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
576 DirectSoundDevice *device = This->device;
580 TRACE("(%p,%d)\n", iface, vol);
582 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
583 WARN("control unavailable\n");
584 return DSERR_CONTROLUNAVAIL;
587 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
588 WARN("invalid parameter: vol = %d\n", vol);
589 return DSERR_INVALIDPARAM;
593 EnterCriticalSection(&device->mixlock);
595 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
597 LeaveCriticalSection(&device->mixlock);
598 WARN("GetChannelVolume failed: %08x\n", hr);
602 if(device->pwfx->nChannels > 1){
603 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
605 LeaveCriticalSection(&device->mixlock);
606 WARN("GetChannelVolume failed: %08x\n", hr);
612 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
613 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
615 DSOUND_AmpFactorToVolPan(&device->volpan);
616 if (vol != device->volpan.lVolume) {
617 device->volpan.lVolume=vol;
618 DSOUND_RecalcVolPan(&device->volpan);
619 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
620 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
622 LeaveCriticalSection(&device->mixlock);
623 WARN("SetChannelVolume failed: %08x\n", hr);
627 if(device->pwfx->nChannels > 1){
628 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
629 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
631 LeaveCriticalSection(&device->mixlock);
632 WARN("SetChannelVolume failed: %08x\n", hr);
638 LeaveCriticalSection(&(device->mixlock));
644 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
646 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
647 DirectSoundDevice *device = This->device;
650 TRACE("(%p,%p)\n", iface, vol);
652 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
653 WARN("control unavailable\n");
654 return DSERR_CONTROLUNAVAIL;
658 WARN("invalid parameter: vol = NULL\n");
659 return DSERR_INVALIDPARAM;
662 EnterCriticalSection(&device->mixlock);
664 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
666 LeaveCriticalSection(&device->mixlock);
667 WARN("GetChannelVolume failed: %08x\n", hr);
671 if(device->pwfx->nChannels > 1){
672 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
674 LeaveCriticalSection(&device->mixlock);
675 WARN("GetChannelVolume failed: %08x\n", hr);
681 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
682 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
684 DSOUND_AmpFactorToVolPan(&device->volpan);
685 *vol = device->volpan.lVolume;
687 LeaveCriticalSection(&device->mixlock);
692 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
694 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
695 TRACE("(%p,%d)\n",This,freq);
697 /* You cannot set the frequency of the primary buffer */
698 WARN("control unavailable\n");
699 return DSERR_CONTROLUNAVAIL;
702 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
703 DWORD reserved2, DWORD flags)
705 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
706 DirectSoundDevice *device = This->device;
707 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
709 if (!(flags & DSBPLAY_LOOPING)) {
710 WARN("invalid parameter: flags = %08x\n", flags);
711 return DSERR_INVALIDPARAM;
715 EnterCriticalSection(&(device->mixlock));
717 if (device->state == STATE_STOPPED)
718 device->state = STATE_STARTING;
719 else if (device->state == STATE_STOPPING)
720 device->state = STATE_PLAYING;
722 LeaveCriticalSection(&(device->mixlock));
728 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
730 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
731 DirectSoundDevice *device = This->device;
732 TRACE("(%p)\n", iface);
735 EnterCriticalSection(&(device->mixlock));
737 if (device->state == STATE_PLAYING)
738 device->state = STATE_STOPPING;
739 else if (device->state == STATE_STARTING)
740 device->state = STATE_STOPPED;
742 LeaveCriticalSection(&(device->mixlock));
748 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
750 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
751 ULONG ref = InterlockedIncrement(&(This->ref));
752 TRACE("(%p) ref was %d\n", This, ref - 1);
754 InterlockedIncrement(&This->numIfaces);
758 /* Decreases *out by 1 to no less than 0.
759 * Returns the new value of *out. */
760 LONG capped_refcount_dec(LONG *out)
767 oldref = InterlockedCompareExchange(out, ref - 1, ref);
768 } while(oldref != ref);
772 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
774 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
777 ref = capped_refcount_dec(&This->ref);
779 capped_refcount_dec(&This->numIfaces);
781 TRACE("(%p) primary ref is now %d\n", This, ref);
786 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
787 DWORD *playpos, DWORD *writepos)
790 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
791 DirectSoundDevice *device = This->device;
792 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
795 EnterCriticalSection(&(device->mixlock));
797 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
799 WARN("DSOUND_PrimaryGetPosition failed\n");
800 LeaveCriticalSection(&(device->mixlock));
804 if (device->state != STATE_STOPPED)
805 /* apply the documented 10ms lead to writepos */
806 *writepos += device->writelead;
807 while (*writepos >= device->buflen) *writepos -= device->buflen;
810 LeaveCriticalSection(&(device->mixlock));
813 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
817 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
819 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
820 DirectSoundDevice *device = This->device;
821 TRACE("(%p,%p)\n", iface, status);
823 if (status == NULL) {
824 WARN("invalid parameter: status == NULL\n");
825 return DSERR_INVALIDPARAM;
829 if ((device->state == STATE_STARTING) ||
830 (device->state == STATE_PLAYING))
831 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
833 TRACE("status=%x\n", *status);
838 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
839 DWORD wfsize, DWORD *wfwritten)
842 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
843 DirectSoundDevice *device = This->device;
844 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
846 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
848 if (lpwf) { /* NULL is valid */
849 if (wfsize >= size) {
850 CopyMemory(lpwf,device->pwfx,size);
854 WARN("invalid parameter: wfsize too small\n");
857 return DSERR_INVALIDPARAM;
861 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
863 WARN("invalid parameter: wfwritten == NULL\n");
864 return DSERR_INVALIDPARAM;
871 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
872 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
873 DWORD *audiobytes2, DWORD flags)
876 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
877 DirectSoundDevice *device = This->device;
878 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
891 return DSERR_INVALIDPARAM;
893 if (device->priolevel != DSSCL_WRITEPRIMARY) {
894 WARN("failed priority check!\n");
895 return DSERR_PRIOLEVELNEEDED;
898 /* when this flag is set, writecursor is meaningless and must be calculated */
899 if (flags & DSBLOCK_FROMWRITECURSOR) {
900 /* GetCurrentPosition does too much magic to duplicate here */
901 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
903 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
908 /* when this flag is set, writebytes is meaningless and must be set */
909 if (flags & DSBLOCK_ENTIREBUFFER)
910 writebytes = device->buflen;
912 if (writecursor >= device->buflen) {
913 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
914 writecursor, device->buflen);
915 return DSERR_INVALIDPARAM;
918 if (writebytes > device->buflen) {
919 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
920 writebytes, device->buflen);
921 return DSERR_INVALIDPARAM;
924 if (writecursor+writebytes <= device->buflen) {
925 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
926 *audiobytes1 = writebytes;
928 *(LPBYTE*)lplpaudioptr2 = NULL;
931 TRACE("->%d.0\n",writebytes);
933 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
934 *audiobytes1 = device->buflen-writecursor;
936 *(LPBYTE*)lplpaudioptr2 = device->buffer;
938 *audiobytes2 = writebytes-(device->buflen-writecursor);
939 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
944 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
946 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
947 TRACE("(%p,%d)\n",This,newpos);
949 /* You cannot set the position of the primary buffer */
950 WARN("invalid call\n");
951 return DSERR_INVALIDCALL;
954 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
956 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
957 DirectSoundDevice *device = This->device;
960 TRACE("(%p,%d)\n", iface, pan);
962 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
963 WARN("control unavailable\n");
964 return DSERR_CONTROLUNAVAIL;
967 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
968 WARN("invalid parameter: pan = %d\n", pan);
969 return DSERR_INVALIDPARAM;
973 EnterCriticalSection(&device->mixlock);
975 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
977 LeaveCriticalSection(&device->mixlock);
978 WARN("GetChannelVolume failed: %08x\n", hr);
982 if(device->pwfx->nChannels > 1){
983 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
985 LeaveCriticalSection(&device->mixlock);
986 WARN("GetChannelVolume failed: %08x\n", hr);
992 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
993 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
995 DSOUND_AmpFactorToVolPan(&device->volpan);
996 if (pan != device->volpan.lPan) {
997 device->volpan.lPan=pan;
998 DSOUND_RecalcVolPan(&device->volpan);
1000 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
1001 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
1003 LeaveCriticalSection(&device->mixlock);
1004 WARN("SetChannelVolume failed: %08x\n", hr);
1008 if(device->pwfx->nChannels > 1){
1009 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
1010 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
1012 LeaveCriticalSection(&device->mixlock);
1013 WARN("SetChannelVolume failed: %08x\n", hr);
1019 LeaveCriticalSection(&device->mixlock);
1025 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1027 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1028 DirectSoundDevice *device = This->device;
1031 TRACE("(%p,%p)\n", iface, pan);
1033 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1034 WARN("control unavailable\n");
1035 return DSERR_CONTROLUNAVAIL;
1039 WARN("invalid parameter: pan == NULL\n");
1040 return DSERR_INVALIDPARAM;
1043 EnterCriticalSection(&device->mixlock);
1045 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1047 LeaveCriticalSection(&device->mixlock);
1048 WARN("GetChannelVolume failed: %08x\n", hr);
1052 if(device->pwfx->nChannels > 1){
1053 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1055 LeaveCriticalSection(&device->mixlock);
1056 WARN("GetChannelVolume failed: %08x\n", hr);
1062 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1063 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1065 DSOUND_AmpFactorToVolPan(&device->volpan);
1066 *pan = device->volpan.lPan;
1068 LeaveCriticalSection(&device->mixlock);
1073 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1076 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1077 DirectSoundDevice *device = This->device;
1078 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1080 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1081 WARN("failed priority check!\n");
1082 return DSERR_PRIOLEVELNEEDED;
1085 if((p1 && ((BYTE*)p1 < device->buffer ||
1086 (BYTE*)p1 >= device->buffer + device->buflen)) ||
1087 (p2 && ((BYTE*)p2 < device->buffer ||
1088 (BYTE*)p2 >= device->buffer + device->buflen)))
1089 return DSERR_INVALIDPARAM;
1094 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1096 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1097 FIXME("(%p):stub\n",This);
1101 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1103 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1104 DirectSoundDevice *device = This->device;
1105 TRACE("(%p,%p)\n", iface, freq);
1108 WARN("invalid parameter: freq == NULL\n");
1109 return DSERR_INVALIDPARAM;
1112 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1113 WARN("control unavailable\n");
1114 return DSERR_CONTROLUNAVAIL;
1117 *freq = device->pwfx->nSamplesPerSec;
1118 TRACE("-> %d\n", *freq);
1123 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1124 const DSBUFFERDESC *dbsd)
1126 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1127 WARN("(%p) already initialized\n", This);
1128 return DSERR_ALREADYINITIALIZED;
1131 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1133 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1134 DirectSoundDevice *device = This->device;
1135 TRACE("(%p,%p)\n", iface, caps);
1138 WARN("invalid parameter: caps == NULL\n");
1139 return DSERR_INVALIDPARAM;
1142 if (caps->dwSize < sizeof(*caps)) {
1143 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1144 return DSERR_INVALIDPARAM;
1147 caps->dwFlags = This->dsbd.dwFlags;
1148 caps->dwBufferBytes = device->buflen;
1150 /* Windows reports these as zero */
1151 caps->dwUnlockTransferRate = 0;
1152 caps->dwPlayCpuOverhead = 0;
1157 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1160 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1162 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1164 if (ppobj == NULL) {
1165 WARN("invalid parameter\n");
1166 return E_INVALIDARG;
1169 *ppobj = NULL; /* assume failure */
1171 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1172 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1173 IDirectSoundBuffer_AddRef(iface);
1178 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1179 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1180 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1181 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1182 return E_NOINTERFACE;
1185 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1186 ERR("app requested IDirectSoundNotify on primary buffer\n");
1187 /* FIXME: should we support this? */
1188 return E_NOINTERFACE;
1191 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1192 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1193 return E_NOINTERFACE;
1196 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1197 *ppobj = &This->IDirectSound3DListener_iface;
1198 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1202 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1203 *ppobj = &This->IKsPropertySet_iface;
1204 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1208 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1209 return E_NOINTERFACE;
1212 static const IDirectSoundBufferVtbl dspbvt =
1214 PrimaryBufferImpl_QueryInterface,
1215 PrimaryBufferImpl_AddRef,
1216 PrimaryBufferImpl_Release,
1217 PrimaryBufferImpl_GetCaps,
1218 PrimaryBufferImpl_GetCurrentPosition,
1219 PrimaryBufferImpl_GetFormat,
1220 PrimaryBufferImpl_GetVolume,
1221 PrimaryBufferImpl_GetPan,
1222 PrimaryBufferImpl_GetFrequency,
1223 PrimaryBufferImpl_GetStatus,
1224 PrimaryBufferImpl_Initialize,
1225 PrimaryBufferImpl_Lock,
1226 PrimaryBufferImpl_Play,
1227 PrimaryBufferImpl_SetCurrentPosition,
1228 PrimaryBufferImpl_SetFormat,
1229 PrimaryBufferImpl_SetVolume,
1230 PrimaryBufferImpl_SetPan,
1231 PrimaryBufferImpl_SetFrequency,
1232 PrimaryBufferImpl_Stop,
1233 PrimaryBufferImpl_Unlock,
1234 PrimaryBufferImpl_Restore
1237 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1238 const DSBUFFERDESC *dsbd)
1240 IDirectSoundBufferImpl *dsb;
1241 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1243 if (dsbd->lpwfxFormat) {
1244 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1246 return DSERR_INVALIDPARAM;
1249 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1252 WARN("out of memory\n");
1254 return DSERR_OUTOFMEMORY;
1261 dsb->device = device;
1262 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1263 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1264 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1267 /* IDirectSound3DListener */
1268 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1269 device->ds3dl.vPosition.x = 0.0;
1270 device->ds3dl.vPosition.y = 0.0;
1271 device->ds3dl.vPosition.z = 0.0;
1272 device->ds3dl.vVelocity.x = 0.0;
1273 device->ds3dl.vVelocity.y = 0.0;
1274 device->ds3dl.vVelocity.z = 0.0;
1275 device->ds3dl.vOrientFront.x = 0.0;
1276 device->ds3dl.vOrientFront.y = 0.0;
1277 device->ds3dl.vOrientFront.z = 1.0;
1278 device->ds3dl.vOrientTop.x = 0.0;
1279 device->ds3dl.vOrientTop.y = 1.0;
1280 device->ds3dl.vOrientTop.z = 0.0;
1281 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1282 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1283 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1284 device->ds3dl_need_recalc = TRUE;
1286 TRACE("Created primary buffer at %p\n", dsb);
1287 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1288 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1289 device->pwfx->wFormatTag, device->pwfx->nChannels,
1290 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1291 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1292 device->pwfx->cbSize);
1294 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);