3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23 * it leaves dsound in unusable (not really open) state.
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
37 #include "wine/debug.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
43 static DWORD DSOUND_fraglen(DirectSoundDevice *device)
45 REFERENCE_TIME period;
49 hr = IAudioClient_GetDevicePeriod(device->client, &period, NULL);
51 /* just guess at 10ms */
52 WARN("GetDevicePeriod failed: %08x\n", hr);
53 ret = MulDiv(device->pwfx->nBlockAlign, device->pwfx->nSamplesPerSec, 100);
55 ret = MulDiv(device->pwfx->nSamplesPerSec * device->pwfx->nBlockAlign, period, 10000000);
57 ret -= ret % device->pwfx->nBlockAlign;
61 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
63 TRACE("(%p)\n", device);
65 device->fraglen = DSOUND_fraglen(device);
66 device->helfrags = device->buflen / device->fraglen;
67 TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
69 /* calculate the 10ms write lead */
70 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
73 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
76 REFERENCE_TIME prebuf_rt;
79 TRACE("(%p, %d)\n", device, forcewave);
82 IAudioClient_Release(device->client);
83 device->client = NULL;
86 IAudioRenderClient_Release(device->render);
87 device->render = NULL;
90 IAudioClock_Release(device->clock);
94 IAudioStreamVolume_Release(device->volume);
95 device->volume = NULL;
98 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
99 CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
101 WARN("Activate failed: %08x\n", hres);
105 prebuf_frames = device->prebuf * DSOUND_fraglen(device) / device->pwfx->nBlockAlign;
106 prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
108 hres = IAudioClient_Initialize(device->client,
109 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST,
110 prebuf_rt, 0, device->pwfx, NULL);
112 IAudioClient_Release(device->client);
113 device->client = NULL;
114 WARN("Initialize failed: %08x\n", hres);
118 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
119 (void**)&device->render);
121 IAudioClient_Release(device->client);
122 device->client = NULL;
123 WARN("GetService failed: %08x\n", hres);
127 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
128 (void**)&device->clock);
130 IAudioClient_Release(device->client);
131 IAudioRenderClient_Release(device->render);
132 device->client = NULL;
133 device->render = NULL;
134 WARN("GetService failed: %08x\n", hres);
138 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
139 (void**)&device->volume);
141 IAudioClient_Release(device->client);
142 IAudioRenderClient_Release(device->render);
143 IAudioClock_Release(device->clock);
144 device->client = NULL;
145 device->render = NULL;
146 device->clock = NULL;
147 WARN("GetService failed: %08x\n", hres);
154 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
159 TRACE("(%p)\n", device);
161 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
162 on windows this size is always fixed (tested on win-xp) */
164 device->buflen = ds_hel_buflen;
165 buflen = device->buflen;
166 buflen -= buflen % device->pwfx->nBlockAlign;
167 device->buflen = buflen;
169 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
170 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
171 if (!device->mix_buffer)
172 return DSERR_OUTOFMEMORY;
174 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
175 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
177 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
179 /* reallocate emulated primary buffer */
181 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
183 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
186 ERR("failed to allocate primary buffer\n");
187 return DSERR_OUTOFMEMORY;
188 /* but the old buffer might still exist and must be re-prepared */
191 DSOUND_RecalcPrimary(device);
193 device->buffer = newbuf;
195 TRACE("fraglen=%d\n", device->fraglen);
197 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
198 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
199 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
200 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
201 device->last_pos_bytes = device->playing_offs_bytes = device->in_mmdev_bytes = device->playpos = device->mixpos = 0;
206 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
210 TRACE("(%p)\n", device);
213 hr = IAudioClient_Stop(device->client);
215 WARN("Stop failed: %08x\n", hr);
218 /* clear the queue */
219 device->in_mmdev_bytes = 0;
222 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
225 TRACE("(%p)\n", device);
227 device->buflen = ds_hel_buflen;
228 err = DSOUND_PrimaryOpen(device);
231 WARN("DSOUND_PrimaryOpen failed\n");
235 device->state = STATE_STOPPED;
239 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
241 TRACE("(%p)\n", device);
244 EnterCriticalSection(&(device->mixlock));
246 DSOUND_PrimaryClose(device);
248 if(device->primary && (device->primary->ref || device->primary->numIfaces))
249 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
251 HeapFree(GetProcessHeap(), 0, device->primary);
252 device->primary = NULL;
254 HeapFree(GetProcessHeap(),0,device->pwfx);
257 LeaveCriticalSection(&(device->mixlock));
263 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
267 TRACE("(%p)\n", device);
269 hr = IAudioClient_Start(device->client);
271 WARN("Start failed: %08x\n", hr);
278 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
282 TRACE("(%p)\n", device);
284 hr = IAudioClient_Stop(device->client);
286 WARN("Stop failed: %08x\n", hr);
293 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
295 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
297 /* check if playpos was requested */
299 *playpos = device->playing_offs_bytes;
301 /* check if writepos was requested */
303 /* the writepos is the first non-queued position */
304 *writepos = (device->playing_offs_bytes + device->in_mmdev_bytes) % device->buflen;
306 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
310 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
312 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
313 return sizeof(WAVEFORMATEX);
315 return sizeof(WAVEFORMATEX) + wfex->cbSize;
318 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
320 DWORD size = DSOUND_GetFormatSize(wfex);
321 LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
323 WARN("out of memory\n");
324 } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
325 CopyMemory(pwfx, wfex, size);
327 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
329 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
330 WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
331 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample/8;
333 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
334 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
335 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
341 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
343 HRESULT err = DSERR_BUFFERLOST;
345 WAVEFORMATEX *old_fmt;
346 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
347 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
349 TRACE("(%p,%p)\n", device, passed_fmt);
351 if (device->priolevel == DSSCL_NORMAL) {
352 WARN("failed priority check!\n");
353 return DSERR_PRIOLEVELNEEDED;
356 /* Let's be pedantic! */
357 if (passed_fmt == NULL) {
358 WARN("invalid parameter: passed_fmt==NULL!\n");
359 return DSERR_INVALIDPARAM;
361 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
362 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
363 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
364 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
365 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
367 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
368 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
369 passed_fmt->nAvgBytesPerSec == 0 ||
370 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
371 return DSERR_INVALIDPARAM;
373 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
374 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
375 return DSERR_INVALIDPARAM;
379 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
380 EnterCriticalSection(&(device->mixlock));
382 old_fmt = device->pwfx;
383 device->pwfx = DSOUND_CopyFormat(passed_fmt);
384 fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
385 if (device->pwfx == NULL) {
386 device->pwfx = old_fmt;
388 err = DSERR_OUTOFMEMORY;
392 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
393 if(fmtex->Samples.wValidBitsPerSample == 0){
394 TRACE("Correcting 0 valid bits per sample\n");
395 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
399 DSOUND_PrimaryClose(device);
401 err = DSOUND_ReopenDevice(device, FALSE);
405 /* requested format failed, so try others */
406 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
407 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
408 device->pwfx->wBitsPerSample = 32;
409 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
410 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
412 err = DSOUND_ReopenDevice(device, FALSE);
417 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
418 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
419 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
420 device->pwfx->wBitsPerSample = 32;
421 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
422 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
424 err = DSOUND_ReopenDevice(device, FALSE);
429 device->pwfx->wBitsPerSample = 32;
430 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
431 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
432 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
433 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
434 err = DSOUND_ReopenDevice(device, FALSE);
438 device->pwfx->wBitsPerSample = 16;
439 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
440 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
441 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
442 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
443 err = DSOUND_ReopenDevice(device, FALSE);
447 device->pwfx->wBitsPerSample = 8;
448 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
449 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
450 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
451 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
452 err = DSOUND_ReopenDevice(device, FALSE);
456 device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
457 device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
458 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
459 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
460 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
461 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
462 err = DSOUND_ReopenDevice(device, FALSE);
466 device->pwfx->wBitsPerSample = 32;
467 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
468 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
469 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
470 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
471 err = DSOUND_ReopenDevice(device, FALSE);
475 device->pwfx->wBitsPerSample = 16;
476 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
477 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
478 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
479 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
480 err = DSOUND_ReopenDevice(device, FALSE);
484 device->pwfx->wBitsPerSample = 8;
485 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
486 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
487 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
488 fmtex->Samples.wValidBitsPerSample = device->pwfx->wBitsPerSample;
489 err = DSOUND_ReopenDevice(device, FALSE);
493 WARN("No formats could be opened\n");
497 err = DSOUND_PrimaryOpen(device);
499 WARN("DSOUND_PrimaryOpen failed\n");
503 if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
505 DSOUND_PrimaryClose(device);
506 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
507 err = DSOUND_ReopenDevice(device, TRUE);
509 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
510 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
511 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
514 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
515 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
516 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
517 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
518 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
520 if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
521 old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
522 old_fmt->nChannels != device->pwfx->nChannels) {
523 IDirectSoundBufferImpl** dsb = device->buffers;
524 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
526 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
528 (*dsb)->freqAdjust = (*dsb)->freq / (float)device->pwfx->nSamplesPerSec;
529 DSOUND_RecalcFormat((*dsb));
530 (*dsb)->primary_mixpos = 0;
532 RtlReleaseResource(&(*dsb)->lock);
538 LeaveCriticalSection(&(device->mixlock));
539 RtlReleaseResource(&(device->buffer_list_lock));
542 HeapFree(GetProcessHeap(), 0, old_fmt);
546 /*******************************************************************************
549 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
551 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
552 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
555 /* This sets this format for the primary buffer only */
556 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
557 const WAVEFORMATEX *wfex)
559 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
560 TRACE("(%p,%p)\n", iface, wfex);
561 return primarybuffer_SetFormat(This->device, wfex);
564 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
566 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
567 DirectSoundDevice *device = This->device;
571 TRACE("(%p,%d)\n", iface, vol);
573 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
574 WARN("control unavailable\n");
575 return DSERR_CONTROLUNAVAIL;
578 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
579 WARN("invalid parameter: vol = %d\n", vol);
580 return DSERR_INVALIDPARAM;
584 EnterCriticalSection(&device->mixlock);
586 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
588 LeaveCriticalSection(&device->mixlock);
589 WARN("GetChannelVolume failed: %08x\n", hr);
593 if(device->pwfx->nChannels > 1){
594 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
596 LeaveCriticalSection(&device->mixlock);
597 WARN("GetChannelVolume failed: %08x\n", hr);
603 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
604 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
606 DSOUND_AmpFactorToVolPan(&device->volpan);
607 if (vol != device->volpan.lVolume) {
608 device->volpan.lVolume=vol;
609 DSOUND_RecalcVolPan(&device->volpan);
610 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
611 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
613 LeaveCriticalSection(&device->mixlock);
614 WARN("SetChannelVolume failed: %08x\n", hr);
618 if(device->pwfx->nChannels > 1){
619 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
620 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
622 LeaveCriticalSection(&device->mixlock);
623 WARN("SetChannelVolume failed: %08x\n", hr);
629 LeaveCriticalSection(&(device->mixlock));
635 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
637 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
638 DirectSoundDevice *device = This->device;
641 TRACE("(%p,%p)\n", iface, vol);
643 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
644 WARN("control unavailable\n");
645 return DSERR_CONTROLUNAVAIL;
649 WARN("invalid parameter: vol = NULL\n");
650 return DSERR_INVALIDPARAM;
653 EnterCriticalSection(&device->mixlock);
655 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
657 LeaveCriticalSection(&device->mixlock);
658 WARN("GetChannelVolume failed: %08x\n", hr);
662 if(device->pwfx->nChannels > 1){
663 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
665 LeaveCriticalSection(&device->mixlock);
666 WARN("GetChannelVolume failed: %08x\n", hr);
672 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
673 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
675 DSOUND_AmpFactorToVolPan(&device->volpan);
676 *vol = device->volpan.lVolume;
678 LeaveCriticalSection(&device->mixlock);
683 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
685 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
686 TRACE("(%p,%d)\n",This,freq);
688 /* You cannot set the frequency of the primary buffer */
689 WARN("control unavailable\n");
690 return DSERR_CONTROLUNAVAIL;
693 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
694 DWORD reserved2, DWORD flags)
696 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
697 DirectSoundDevice *device = This->device;
698 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
700 if (!(flags & DSBPLAY_LOOPING)) {
701 WARN("invalid parameter: flags = %08x\n", flags);
702 return DSERR_INVALIDPARAM;
706 EnterCriticalSection(&(device->mixlock));
708 if (device->state == STATE_STOPPED)
709 device->state = STATE_STARTING;
710 else if (device->state == STATE_STOPPING)
711 device->state = STATE_PLAYING;
713 LeaveCriticalSection(&(device->mixlock));
719 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
721 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
722 DirectSoundDevice *device = This->device;
723 TRACE("(%p)\n", iface);
726 EnterCriticalSection(&(device->mixlock));
728 if (device->state == STATE_PLAYING)
729 device->state = STATE_STOPPING;
730 else if (device->state == STATE_STARTING)
731 device->state = STATE_STOPPED;
733 LeaveCriticalSection(&(device->mixlock));
739 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
741 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
742 ULONG ref = InterlockedIncrement(&(This->ref));
743 TRACE("(%p) ref was %d\n", This, ref - 1);
745 InterlockedIncrement(&This->numIfaces);
749 /* Decreases *out by 1 to no less than 0.
750 * Returns the new value of *out. */
751 LONG capped_refcount_dec(LONG *out)
758 oldref = InterlockedCompareExchange(out, ref - 1, ref);
759 } while(oldref != ref);
763 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
765 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
768 ref = capped_refcount_dec(&This->ref);
770 capped_refcount_dec(&This->numIfaces);
772 TRACE("(%p) primary ref is now %d\n", This, ref);
777 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
778 DWORD *playpos, DWORD *writepos)
781 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
782 DirectSoundDevice *device = This->device;
783 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
786 EnterCriticalSection(&(device->mixlock));
788 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
790 WARN("DSOUND_PrimaryGetPosition failed\n");
791 LeaveCriticalSection(&(device->mixlock));
795 if (device->state != STATE_STOPPED)
796 /* apply the documented 10ms lead to writepos */
797 *writepos += device->writelead;
798 while (*writepos >= device->buflen) *writepos -= device->buflen;
801 LeaveCriticalSection(&(device->mixlock));
804 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
808 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
810 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
811 DirectSoundDevice *device = This->device;
812 TRACE("(%p,%p)\n", iface, status);
814 if (status == NULL) {
815 WARN("invalid parameter: status == NULL\n");
816 return DSERR_INVALIDPARAM;
820 if ((device->state == STATE_STARTING) ||
821 (device->state == STATE_PLAYING))
822 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
824 TRACE("status=%x\n", *status);
829 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
830 DWORD wfsize, DWORD *wfwritten)
833 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
834 DirectSoundDevice *device = This->device;
835 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
837 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
839 if (lpwf) { /* NULL is valid */
840 if (wfsize >= size) {
841 CopyMemory(lpwf,device->pwfx,size);
845 WARN("invalid parameter: wfsize too small\n");
848 return DSERR_INVALIDPARAM;
852 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
854 WARN("invalid parameter: wfwritten == NULL\n");
855 return DSERR_INVALIDPARAM;
862 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
863 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
864 DWORD *audiobytes2, DWORD flags)
867 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
868 DirectSoundDevice *device = This->device;
869 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
882 return DSERR_INVALIDPARAM;
884 if (device->priolevel != DSSCL_WRITEPRIMARY) {
885 WARN("failed priority check!\n");
886 return DSERR_PRIOLEVELNEEDED;
889 /* when this flag is set, writecursor is meaningless and must be calculated */
890 if (flags & DSBLOCK_FROMWRITECURSOR) {
891 /* GetCurrentPosition does too much magic to duplicate here */
892 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
894 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
899 /* when this flag is set, writebytes is meaningless and must be set */
900 if (flags & DSBLOCK_ENTIREBUFFER)
901 writebytes = device->buflen;
903 if (writecursor >= device->buflen) {
904 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
905 writecursor, device->buflen);
906 return DSERR_INVALIDPARAM;
909 if (writebytes > device->buflen) {
910 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
911 writebytes, device->buflen);
912 return DSERR_INVALIDPARAM;
915 if (writecursor+writebytes <= device->buflen) {
916 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
917 *audiobytes1 = writebytes;
919 *(LPBYTE*)lplpaudioptr2 = NULL;
922 TRACE("->%d.0\n",writebytes);
924 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
925 *audiobytes1 = device->buflen-writecursor;
927 *(LPBYTE*)lplpaudioptr2 = device->buffer;
929 *audiobytes2 = writebytes-(device->buflen-writecursor);
930 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
935 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
937 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
938 TRACE("(%p,%d)\n",This,newpos);
940 /* You cannot set the position of the primary buffer */
941 WARN("invalid call\n");
942 return DSERR_INVALIDCALL;
945 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
947 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
948 DirectSoundDevice *device = This->device;
951 TRACE("(%p,%d)\n", iface, pan);
953 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
954 WARN("control unavailable\n");
955 return DSERR_CONTROLUNAVAIL;
958 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
959 WARN("invalid parameter: pan = %d\n", pan);
960 return DSERR_INVALIDPARAM;
964 EnterCriticalSection(&device->mixlock);
966 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
968 LeaveCriticalSection(&device->mixlock);
969 WARN("GetChannelVolume failed: %08x\n", hr);
973 if(device->pwfx->nChannels > 1){
974 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
976 LeaveCriticalSection(&device->mixlock);
977 WARN("GetChannelVolume failed: %08x\n", hr);
983 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
984 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
986 DSOUND_AmpFactorToVolPan(&device->volpan);
987 if (pan != device->volpan.lPan) {
988 device->volpan.lPan=pan;
989 DSOUND_RecalcVolPan(&device->volpan);
991 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
992 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
994 LeaveCriticalSection(&device->mixlock);
995 WARN("SetChannelVolume failed: %08x\n", hr);
999 if(device->pwfx->nChannels > 1){
1000 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
1001 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
1003 LeaveCriticalSection(&device->mixlock);
1004 WARN("SetChannelVolume failed: %08x\n", hr);
1010 LeaveCriticalSection(&device->mixlock);
1016 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1018 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1019 DirectSoundDevice *device = This->device;
1022 TRACE("(%p,%p)\n", iface, pan);
1024 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1025 WARN("control unavailable\n");
1026 return DSERR_CONTROLUNAVAIL;
1030 WARN("invalid parameter: pan == NULL\n");
1031 return DSERR_INVALIDPARAM;
1034 EnterCriticalSection(&device->mixlock);
1036 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1038 LeaveCriticalSection(&device->mixlock);
1039 WARN("GetChannelVolume failed: %08x\n", hr);
1043 if(device->pwfx->nChannels > 1){
1044 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1046 LeaveCriticalSection(&device->mixlock);
1047 WARN("GetChannelVolume failed: %08x\n", hr);
1053 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1054 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1056 DSOUND_AmpFactorToVolPan(&device->volpan);
1057 *pan = device->volpan.lPan;
1059 LeaveCriticalSection(&device->mixlock);
1064 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1067 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1068 DirectSoundDevice *device = This->device;
1069 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1071 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1072 WARN("failed priority check!\n");
1073 return DSERR_PRIOLEVELNEEDED;
1076 if((p1 && ((BYTE*)p1 < device->buffer ||
1077 (BYTE*)p1 >= device->buffer + device->buflen)) ||
1078 (p2 && ((BYTE*)p2 < device->buffer ||
1079 (BYTE*)p2 >= device->buffer + device->buflen)))
1080 return DSERR_INVALIDPARAM;
1085 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1087 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1088 FIXME("(%p):stub\n",This);
1092 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1094 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1095 DirectSoundDevice *device = This->device;
1096 TRACE("(%p,%p)\n", iface, freq);
1099 WARN("invalid parameter: freq == NULL\n");
1100 return DSERR_INVALIDPARAM;
1103 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1104 WARN("control unavailable\n");
1105 return DSERR_CONTROLUNAVAIL;
1108 *freq = device->pwfx->nSamplesPerSec;
1109 TRACE("-> %d\n", *freq);
1114 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1115 const DSBUFFERDESC *dbsd)
1117 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1118 WARN("(%p) already initialized\n", This);
1119 return DSERR_ALREADYINITIALIZED;
1122 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1124 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1125 DirectSoundDevice *device = This->device;
1126 TRACE("(%p,%p)\n", iface, caps);
1129 WARN("invalid parameter: caps == NULL\n");
1130 return DSERR_INVALIDPARAM;
1133 if (caps->dwSize < sizeof(*caps)) {
1134 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1135 return DSERR_INVALIDPARAM;
1138 caps->dwFlags = This->dsbd.dwFlags;
1139 caps->dwBufferBytes = device->buflen;
1141 /* Windows reports these as zero */
1142 caps->dwUnlockTransferRate = 0;
1143 caps->dwPlayCpuOverhead = 0;
1148 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1151 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1153 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1155 if (ppobj == NULL) {
1156 WARN("invalid parameter\n");
1157 return E_INVALIDARG;
1160 *ppobj = NULL; /* assume failure */
1162 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1163 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1164 IDirectSoundBuffer_AddRef(iface);
1169 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1170 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1171 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1172 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1173 return E_NOINTERFACE;
1176 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1177 ERR("app requested IDirectSoundNotify on primary buffer\n");
1178 /* FIXME: should we support this? */
1179 return E_NOINTERFACE;
1182 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1183 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1184 return E_NOINTERFACE;
1187 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1188 *ppobj = &This->IDirectSound3DListener_iface;
1189 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1193 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1194 *ppobj = &This->IKsPropertySet_iface;
1195 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1199 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1200 return E_NOINTERFACE;
1203 static const IDirectSoundBufferVtbl dspbvt =
1205 PrimaryBufferImpl_QueryInterface,
1206 PrimaryBufferImpl_AddRef,
1207 PrimaryBufferImpl_Release,
1208 PrimaryBufferImpl_GetCaps,
1209 PrimaryBufferImpl_GetCurrentPosition,
1210 PrimaryBufferImpl_GetFormat,
1211 PrimaryBufferImpl_GetVolume,
1212 PrimaryBufferImpl_GetPan,
1213 PrimaryBufferImpl_GetFrequency,
1214 PrimaryBufferImpl_GetStatus,
1215 PrimaryBufferImpl_Initialize,
1216 PrimaryBufferImpl_Lock,
1217 PrimaryBufferImpl_Play,
1218 PrimaryBufferImpl_SetCurrentPosition,
1219 PrimaryBufferImpl_SetFormat,
1220 PrimaryBufferImpl_SetVolume,
1221 PrimaryBufferImpl_SetPan,
1222 PrimaryBufferImpl_SetFrequency,
1223 PrimaryBufferImpl_Stop,
1224 PrimaryBufferImpl_Unlock,
1225 PrimaryBufferImpl_Restore
1228 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1229 const DSBUFFERDESC *dsbd)
1231 IDirectSoundBufferImpl *dsb;
1232 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1234 if (dsbd->lpwfxFormat) {
1235 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1237 return DSERR_INVALIDPARAM;
1240 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1243 WARN("out of memory\n");
1245 return DSERR_OUTOFMEMORY;
1252 dsb->device = device;
1253 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1254 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1255 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1258 /* IDirectSound3DListener */
1259 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1260 device->ds3dl.vPosition.x = 0.0;
1261 device->ds3dl.vPosition.y = 0.0;
1262 device->ds3dl.vPosition.z = 0.0;
1263 device->ds3dl.vVelocity.x = 0.0;
1264 device->ds3dl.vVelocity.y = 0.0;
1265 device->ds3dl.vVelocity.z = 0.0;
1266 device->ds3dl.vOrientFront.x = 0.0;
1267 device->ds3dl.vOrientFront.y = 0.0;
1268 device->ds3dl.vOrientFront.z = 1.0;
1269 device->ds3dl.vOrientTop.x = 0.0;
1270 device->ds3dl.vOrientTop.y = 1.0;
1271 device->ds3dl.vOrientTop.z = 0.0;
1272 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1273 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1274 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1275 device->ds3dl_need_recalc = TRUE;
1277 TRACE("Created primary buffer at %p\n", dsb);
1278 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1279 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1280 device->pwfx->wFormatTag, device->pwfx->nChannels,
1281 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1282 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1283 device->pwfx->cbSize);
1285 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);