Fix some primary buffer locking
[dsound-openal.git] / primary.c
blob4b9cd2524b4e73b17996738387f22c9330e7b235
1 /* DirectSound COM interface
3 * Copyright 2009 Maarten Lankhorst
5 * Some code taken from the original dsound-openal implementation
6 * Copyright 2007-2009 Chris Robinson
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
25 #ifdef __WINESRC__
27 #define COBJMACROS
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "vfwmsgs.h"
36 #include "mmsystem.h"
37 #include "winternl.h"
38 #include "mmddk.h"
39 #include "wine/debug.h"
40 #include "dsound.h"
42 #include "dsound_private.h"
44 #include "mmreg.h"
45 #include "ks.h"
46 #include "ksmedia.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
50 #else
52 #define WINVER 0x0600
53 #include <windows.h>
54 #include <dsound.h>
56 #include "dsound_private.h"
58 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
59 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
61 #ifndef E_PROP_ID_UNSUPPORTED
62 #define E_PROP_ID_UNSUPPORTED ((HRESULT)0x80070490)
63 #endif
65 #endif
67 static const IDirectSoundBufferVtbl DS8Primary_Vtbl;
68 static const IDirectSound3DListenerVtbl DS8Primary3D_Vtbl;
69 static const IKsPropertySetVtbl DS8PrimaryProp_Vtbl;
72 static inline DS8Primary *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
74 return CONTAINING_RECORD(iface, DS8Primary, IDirectSoundBuffer_iface);
77 static inline DS8Primary *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
79 return CONTAINING_RECORD(iface, DS8Primary, IDirectSound3DListener_iface);
82 static inline DS8Primary *impl_from_IKsPropertySet(IKsPropertySet *iface)
84 return CONTAINING_RECORD(iface, DS8Primary, IKsPropertySet_iface);
88 static void AL_APIENTRY wrap_DeferUpdates(void)
89 { alcSuspendContext(alcGetCurrentContext()); }
90 static void AL_APIENTRY wrap_ProcessUpdates(void)
91 { alcProcessContext(alcGetCurrentContext()); }
94 static void trigger_elapsed_notifies(DS8Buffer *buf, DWORD lastpos, DWORD curpos)
96 DWORD i;
97 for(i = 0; i < buf->nnotify; ++i)
99 DSBPOSITIONNOTIFY *not = &buf->notify[i];
100 HANDLE event = not->hEventNotify;
101 DWORD ofs = not->dwOffset;
103 if(ofs == (DWORD)DSBPN_OFFSETSTOP)
104 continue;
106 /* Wraparound case */
107 if(curpos < lastpos)
109 if(ofs < curpos || ofs >= lastpos)
110 SetEvent(event);
111 continue;
114 /* Normal case */
115 if(ofs >= lastpos && ofs < curpos)
116 SetEvent(event);
120 static void trigger_stop_notifies(DS8Buffer *buf)
122 DWORD i;
123 for(i = 0; i < buf->nnotify; ++i)
125 DSBPOSITIONNOTIFY *not = &buf->notify[i];
126 if(not->dwOffset == (DWORD)DSBPN_OFFSETSTOP)
127 SetEvent(not->hEventNotify);
131 static DWORD CALLBACK DS8Primary_thread(void *dwUser)
133 DS8Primary *prim = (DS8Primary*)dwUser;
134 DWORD i, active_notifies;
135 MSG msg;
137 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
139 TRACE("Primary buffer (%p) message loop start\n", prim);
140 while(GetMessageA(&msg, NULL, 0, 0))
142 if(msg.message != WM_USER)
143 continue;
145 EnterCriticalSection(prim->crst);
146 setALContext(prim->ctx);
148 for(i = 0;i < prim->nnotifies;)
150 DS8Buffer *buf = prim->notifies[i];
151 IDirectSoundBuffer8 *dsb = &buf->IDirectSoundBuffer8_iface;
152 DWORD status=0, curpos=buf->lastpos;
154 IDirectSoundBuffer8_GetStatus(dsb, &status);
155 IDirectSoundBuffer8_GetCurrentPosition(dsb, &curpos, NULL);
156 if(buf->lastpos != curpos)
158 trigger_elapsed_notifies(buf, buf->lastpos, curpos);
159 buf->lastpos = curpos;
161 if(!(status&DSBSTATUS_PLAYING))
163 /* Remove this buffer from list and put another at the
164 * current position; don't increment i
166 trigger_stop_notifies(buf);
167 prim->notifies[i] = prim->notifies[--prim->nnotifies];
168 continue;
170 i++;
172 active_notifies = i;
174 /* OpenAL doesn't support our lovely buffer extensions
175 * so just make sure enough buffers are queued
177 if(!prim->SupportedExt[SOFT_BUFFER_SAMPLES] &&
178 !prim->SupportedExt[SOFT_BUFFER_SUB_DATA] &&
179 !prim->SupportedExt[EXT_STATIC_BUFFER])
181 for(i = 0;i < prim->nbuffers;++i)
183 DS8Buffer *buf = prim->buffers[i];
184 ALint done = 0, queued = QBUFFERS, state = AL_PLAYING;
185 ALuint which, ofs;
187 if(buf->buffer->numsegs == 1 || !buf->isplaying)
188 continue;
190 alGetSourcei(buf->source, AL_SOURCE_STATE, &state);
191 alGetSourcei(buf->source, AL_BUFFERS_QUEUED, &queued);
192 alGetSourcei(buf->source, AL_BUFFERS_PROCESSED, &done);
194 queued -= done;
195 while(done--)
196 alSourceUnqueueBuffers(buf->source, 1, &which);
197 while(queued < QBUFFERS)
199 which = buf->buffer->buffers[buf->curidx];
200 ofs = buf->curidx*buf->buffer->segsize;
201 if(buf->curidx < buf->buffer->numsegs-1)
202 alBufferData(which, buf->buffer->buf_format,
203 buf->buffer->data + ofs, buf->buffer->segsize,
204 buf->buffer->format.Format.nSamplesPerSec);
205 else
206 alBufferData(which, buf->buffer->buf_format,
207 buf->buffer->data + ofs, buf->buffer->lastsegsize,
208 buf->buffer->format.Format.nSamplesPerSec);
210 alSourceQueueBuffers(buf->source, 1, &which);
211 buf->curidx = (buf->curidx+1)%buf->buffer->numsegs;
212 queued++;
214 if(!buf->curidx && !buf->islooping)
216 buf->isplaying = FALSE;
217 break;
220 if(state != AL_PLAYING)
222 if(!queued)
224 IDirectSoundBuffer8_Stop(&buf->IDirectSoundBuffer8_iface);
225 continue;
227 alSourcePlay(buf->source);
230 checkALError();
232 else if(active_notifies == 0 && prim->timer_id)
234 TRACE("No more notifies, killing timer\n");
235 timeKillEvent(prim->timer_id);
236 prim->timer_id = 0;
237 timeEndPeriod(prim->timer_res);
240 popALContext();
241 LeaveCriticalSection(prim->crst);
243 TRACE("Primary buffer (%p) message loop quit\n", prim);
245 if(prim->timer_id)
247 timeKillEvent(prim->timer_id);
248 prim->timer_id = 0;
249 timeEndPeriod(prim->timer_res);
250 TRACE("Killed timer\n");
253 return 0;
256 static void CALLBACK DS8Primary_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
257 DWORD_PTR dw1, DWORD_PTR dw2)
259 (void)timerID;
260 (void)msg;
261 (void)dw1;
262 (void)dw2;
263 PostThreadMessageA(dwUser, WM_USER, 0, 0);
266 void DS8Primary_starttimer(DS8Primary *prim)
268 DWORD triggertime, res = DS_TIME_RES;
269 ALint refresh = FAKE_REFRESH_COUNT;
270 TIMECAPS time;
272 if(prim->timer_id)
273 return;
275 timeGetDevCaps(&time, sizeof(TIMECAPS));
277 alcGetIntegerv(prim->parent->device, ALC_REFRESH, 1, &refresh);
278 checkALCError(prim->parent->device);
280 triggertime = 1000 / refresh / 2;
281 if(triggertime < time.wPeriodMin)
282 triggertime = time.wPeriodMin;
283 TRACE("Calling timer every %"LONGFMT"u ms for %i refreshes per second\n", triggertime, refresh);
285 if (res < time.wPeriodMin)
286 res = time.wPeriodMin;
287 if (timeBeginPeriod(res) == TIMERR_NOCANDO)
288 WARN("Could not set minimum resolution, don't expect sound\n");
290 prim->timer_res = res;
291 prim->timer_id = timeSetEvent(triggertime, res, DS8Primary_timer, prim->thread_id, TIME_PERIODIC|TIME_KILL_SYNCHRONOUS);
296 HRESULT DS8Primary_PreInit(DS8Primary *This, DS8Impl *parent)
298 DS3DLISTENER *listener;
299 WAVEFORMATEX *wfx;
300 HRESULT hr;
302 This->IDirectSoundBuffer_iface.lpVtbl = (IDirectSoundBufferVtbl*)&DS8Primary_Vtbl;
303 This->IDirectSound3DListener_iface.lpVtbl = (IDirectSound3DListenerVtbl*)&DS8Primary3D_Vtbl;
304 This->IKsPropertySet_iface.lpVtbl = (IKsPropertySetVtbl*)&DS8PrimaryProp_Vtbl;
306 This->parent = parent;
307 This->crst = &parent->share->crst;
308 This->ctx = parent->share->ctx;
309 This->SupportedExt = parent->share->SupportedExt;
310 This->ExtAL = &parent->share->ExtAL;
311 This->sources = parent->share->sources;
312 This->auxslot = parent->share->auxslot;
314 /* Allocate enough for a WAVEFORMATEXTENSIBLE */
315 wfx = &This->format.Format;
317 wfx->wFormatTag = WAVE_FORMAT_PCM;
318 wfx->nChannels = 2;
319 wfx->wBitsPerSample = 8;
320 wfx->nSamplesPerSec = 22050;
321 wfx->nBlockAlign = wfx->wBitsPerSample * wfx->nChannels / 8;
322 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
323 wfx->cbSize = 0;
325 This->stopped = TRUE;
327 /* Apparently primary buffer size is always 32k,
328 * tested on windows with 192k 24 bits sound @ 6 channels
329 * where it will run out in 60 ms and it isn't pointer aligned
331 This->buf_size = 32768;
333 if(This->SupportedExt[SOFT_DEFERRED_UPDATES])
335 This->DeferUpdates = This->ExtAL->DeferUpdatesSOFT;
336 This->ProcessUpdates = This->ExtAL->ProcessUpdatesSOFT;
338 else
340 This->DeferUpdates = wrap_DeferUpdates;
341 This->ProcessUpdates = wrap_ProcessUpdates;
344 This->eax_prop = EnvironmentDefaults[EAX_ENVIRONMENT_GENERIC];
345 if(This->SupportedExt[EXT_EFX] && This->auxslot != 0)
347 ALint revid = alGetEnumValue("AL_EFFECT_REVERB");
348 if(revid != 0 && revid != -1)
350 This->ExtAL->GenEffects(1, &This->effect);
351 This->ExtAL->Effecti(This->effect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
352 checkALError();
356 /* Make sure DS3DListener defaults are applied to OpenAL */
357 listener = &This->listen;
358 listener->dwSize = sizeof(*listener);
359 listener->vPosition.x = 0.0;
360 listener->vPosition.y = 0.0;
361 listener->vPosition.z = 0.0;
362 listener->vVelocity.x = 0.0;
363 listener->vVelocity.y = 0.0;
364 listener->vVelocity.z = 0.0;
365 listener->vOrientFront.x = 0.0;
366 listener->vOrientFront.y = 0.0;
367 listener->vOrientFront.z = 1.0;
368 listener->vOrientTop.x = 0.0;
369 listener->vOrientTop.y = 1.0;
370 listener->vOrientTop.z = 0.0;
371 listener->flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
372 listener->flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
373 listener->flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
374 hr = IDirectSound3DListener_SetAllParameters(&This->IDirectSound3DListener_iface, listener, DS3D_IMMEDIATE);
375 if(FAILED(hr))
376 ERR("Could not set 3d parameters: %08"LONGFMT"x\n", hr);
378 This->sizenotifies = This->sizebuffers = parent->share->max_sources;
380 hr = DSERR_OUTOFMEMORY;
381 This->buffers = HeapAlloc(GetProcessHeap(), 0, This->sizebuffers*sizeof(*This->buffers));
382 This->notifies = HeapAlloc(GetProcessHeap(), 0, This->sizenotifies*sizeof(*This->notifies));
383 if(!This->buffers || !This->notifies)
384 goto fail;
386 This->thread_hdl = CreateThread(NULL, 0, DS8Primary_thread, This, 0, &This->thread_id);
387 if(This->thread_hdl == NULL)
388 goto fail;
390 return S_OK;
392 fail:
393 DS8Primary_Clear(This);
394 return hr;
397 void DS8Primary_Clear(DS8Primary *This)
399 TRACE("Clearing primary %p\n", This);
401 if(!This->parent)
402 return;
404 if(This->thread_hdl)
406 PostThreadMessageA(This->thread_id, WM_QUIT, 0, 0);
407 if(WaitForSingleObject(This->thread_hdl, 1000) != WAIT_OBJECT_0)
409 /* HACK: Apparently, if the device is initialized (thus the primary
410 * buffer has PreInit called) then immediately deleted (the primary
411 * buffer has Clear called), the WM_QUIT message gets sent before
412 * the thread has a chance to run which apparently prevents it from
413 * receiving the message.
414 * If the wait attempt fails, try sending the message again. */
415 PostThreadMessageA(This->thread_id, WM_QUIT, 0, 0);
416 if(WaitForSingleObject(This->thread_hdl, 1000) != WAIT_OBJECT_0)
417 ERR("Thread wait timed out\n");
419 CloseHandle(This->thread_hdl);
422 setALContext(This->ctx);
423 if(This->effect)
424 This->ExtAL->DeleteEffects(1, &This->effect);
425 popALContext();
426 while(This->nbuffers--)
427 DS8Buffer_Destroy(This->buffers[This->nbuffers]);
429 HeapFree(GetProcessHeap(), 0, This->notifies);
430 HeapFree(GetProcessHeap(), 0, This->buffers);
431 memset(This, 0, sizeof(*This));
434 static HRESULT WINAPI DS8Primary_QueryInterface(IDirectSoundBuffer *iface, REFIID riid, LPVOID *ppv)
436 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
438 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
440 *ppv = NULL;
441 if(IsEqualIID(riid, &IID_IUnknown) ||
442 IsEqualIID(riid, &IID_IDirectSoundBuffer))
443 *ppv = &This->IDirectSoundBuffer_iface;
444 else if(IsEqualIID(riid, &IID_IDirectSound3DListener))
446 if((This->flags&DSBCAPS_CTRL3D))
447 *ppv = &This->IDirectSound3DListener_iface;
449 else
450 FIXME("Unhandled GUID: %s\n", debugstr_guid(riid));
452 if(*ppv)
454 IUnknown_AddRef((IUnknown*)*ppv);
455 return S_OK;
458 return E_NOINTERFACE;
461 static ULONG WINAPI DS8Primary_AddRef(IDirectSoundBuffer *iface)
463 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
464 LONG ret;
466 ret = InterlockedIncrement(&This->ref);
467 if(ret == 1) This->flags = 0;
469 return ret;
472 static ULONG WINAPI DS8Primary_Release(IDirectSoundBuffer *iface)
474 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
475 LONG ret;
477 ret = InterlockedDecrement(&This->ref);
479 return ret;
482 static HRESULT WINAPI DS8Primary_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
484 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
486 TRACE("(%p)->(%p)\n", iface, caps);
488 if(!caps || caps->dwSize < sizeof(*caps))
490 WARN("Invalid DSBCAPS (%p, %"LONGFMT"u)\n", caps, caps ? caps->dwSize : 0);
491 return DSERR_INVALIDPARAM;
494 EnterCriticalSection(This->crst);
495 caps->dwFlags = This->flags;
496 caps->dwBufferBytes = This->buf_size;
497 caps->dwUnlockTransferRate = 0;
498 caps->dwPlayCpuOverhead = 0;
499 LeaveCriticalSection(This->crst);
501 return DS_OK;
504 static HRESULT WINAPI DS8Primary_GetCurrentPosition(IDirectSoundBuffer *iface, DWORD *playpos, DWORD *curpos)
506 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
507 HRESULT hr = DSERR_PRIOLEVELNEEDED;
509 EnterCriticalSection(This->crst);
510 if(This->write_emu)
511 hr = IDirectSoundBuffer8_GetCurrentPosition(This->write_emu, playpos, curpos);
512 LeaveCriticalSection(This->crst);
514 return hr;
517 static HRESULT WINAPI DS8Primary_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *wfx, DWORD allocated, DWORD *written)
519 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
520 HRESULT hr = S_OK;
521 UINT size;
523 if(!wfx && !written)
525 WARN("Cannot report format or format size\n");
526 return DSERR_INVALIDPARAM;
529 EnterCriticalSection(This->crst);
530 size = sizeof(This->format.Format) + This->format.Format.cbSize;
531 if(written)
532 *written = size;
533 if(wfx)
535 if(allocated < size)
536 hr = DSERR_INVALIDPARAM;
537 else
538 memcpy(wfx, &This->format.Format, size);
540 LeaveCriticalSection(This->crst);
542 return hr;
545 static HRESULT WINAPI DS8Primary_GetVolume(IDirectSoundBuffer *iface, LONG *volume)
547 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
548 ALfloat gain;
550 TRACE("(%p)->(%p)\n", iface, volume);
552 if(!volume)
553 return DSERR_INVALIDPARAM;
554 *volume = 0;
556 if(!(This->flags&DSBCAPS_CTRLVOLUME))
557 return DSERR_CONTROLUNAVAIL;
559 setALContext(This->ctx);
560 alGetListenerf(AL_GAIN, &gain);
561 checkALError();
562 popALContext();
564 *volume = clampI(gain_to_mB(gain), DSBVOLUME_MIN, DSBVOLUME_MAX);
565 return DS_OK;
568 static HRESULT WINAPI DS8Primary_GetPan(IDirectSoundBuffer *iface, LONG *pan)
570 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
571 HRESULT hr = DS_OK;
573 WARN("(%p)->(%p): semi-stub\n", iface, pan);
575 if(!pan)
576 return DSERR_INVALIDPARAM;
578 EnterCriticalSection(This->crst);
579 if(This->write_emu)
580 hr = IDirectSoundBuffer8_GetPan(This->write_emu, pan);
581 else if(!(This->flags & DSBCAPS_CTRLPAN))
582 hr = DSERR_CONTROLUNAVAIL;
583 else
584 *pan = 0;
585 LeaveCriticalSection(This->crst);
587 return hr;
590 static HRESULT WINAPI DS8Primary_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
592 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
593 HRESULT hr = DS_OK;
595 WARN("(%p)->(%p): semi-stub\n", iface, freq);
597 if(!freq)
598 return DSERR_INVALIDPARAM;
600 if(!(This->flags&DSBCAPS_CTRLFREQUENCY))
601 return DSERR_CONTROLUNAVAIL;
603 EnterCriticalSection(This->crst);
604 *freq = This->format.Format.nSamplesPerSec;
605 LeaveCriticalSection(This->crst);
607 return hr;
610 static HRESULT WINAPI DS8Primary_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
612 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
614 TRACE("(%p)->(%p)\n", iface, status);
616 if(!status)
617 return DSERR_INVALIDPARAM;
619 EnterCriticalSection(This->crst);
620 *status = DSBSTATUS_PLAYING|DSBSTATUS_LOOPING;
621 if((This->flags&DSBCAPS_LOCDEFER))
622 *status |= DSBSTATUS_LOCHARDWARE;
624 if(This->stopped)
626 DWORD i, state;
627 HRESULT hr;
629 for(i = 0;i < This->nbuffers;++i)
631 hr = IDirectSoundBuffer8_GetStatus(&This->buffers[i]->IDirectSoundBuffer8_iface, &state);
632 if(SUCCEEDED(hr) && (state&DSBSTATUS_PLAYING))
633 break;
635 if(i == This->nbuffers)
637 /* Primary stopped and no buffers playing.. */
638 *status = 0;
641 LeaveCriticalSection(This->crst);
643 return DS_OK;
646 static HRESULT WINAPI DS8Primary_Initialize(IDirectSoundBuffer *iface, IDirectSound *ds, const DSBUFFERDESC *desc)
648 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
649 HRESULT hr;
651 TRACE("(%p)->(%p, %p)\n", iface, ds, desc);
653 if(!desc || desc->lpwfxFormat || desc->dwBufferBytes)
655 WARN("Bad DSBDESC for primary buffer\n");
656 return DSERR_INVALIDPARAM;
658 if((desc->dwFlags&DSBCAPS_CTRLFX) ||
659 (desc->dwFlags&DSBCAPS_CTRLPOSITIONNOTIFY) ||
660 (desc->dwFlags&DSBCAPS_LOCSOFTWARE))
662 WARN("Bad dwFlags %08"LONGFMT"x\n", desc->dwFlags);
663 return DSERR_INVALIDPARAM;
666 /* Should be 0 if not initialized */
667 if(This->flags)
668 return DSERR_ALREADYINITIALIZED;
670 hr = DS_OK;
671 if(This->parent->prio_level == DSSCL_WRITEPRIMARY)
673 DSBUFFERDESC emudesc;
674 DS8Buffer *emu;
676 if(This->write_emu)
678 ERR("There shouldn't be a write_emu!\n");
679 IDirectSoundBuffer8_Release(This->write_emu);
680 This->write_emu = NULL;
683 memset(&emudesc, 0, sizeof(emudesc));
684 emudesc.dwSize = sizeof(emudesc);
685 emudesc.dwFlags = DSBCAPS_LOCHARDWARE | (desc->dwFlags&DSBCAPS_CTRLPAN);
686 /* Dont play last incomplete sample */
687 emudesc.dwBufferBytes = This->buf_size - (This->buf_size%This->format.Format.nBlockAlign);
688 emudesc.lpwfxFormat = &This->format.Format;
690 hr = DS8Buffer_Create(&emu, This, NULL);
691 if(SUCCEEDED(hr))
693 This->write_emu = &emu->IDirectSoundBuffer8_iface;
694 hr = IDirectSoundBuffer8_Initialize(This->write_emu, ds, &emudesc);
695 if(FAILED(hr))
697 IDirectSoundBuffer8_Release(This->write_emu);
698 This->write_emu = NULL;
703 if(SUCCEEDED(hr))
704 This->flags = desc->dwFlags | DSBCAPS_LOCHARDWARE;
705 return hr;
708 static HRESULT WINAPI DS8Primary_Lock(IDirectSoundBuffer *iface, DWORD ofs, DWORD bytes, void **ptr1, DWORD *len1, void **ptr2, DWORD *len2, DWORD flags)
710 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
711 HRESULT hr = DSERR_PRIOLEVELNEEDED;
713 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %p, %p, %p, %p, %"LONGFMT"u)\n", iface, ofs, bytes, ptr1, len1, ptr2, len2, flags);
715 EnterCriticalSection(This->crst);
716 if(This->write_emu)
717 hr = IDirectSoundBuffer8_Lock(This->write_emu, ofs, bytes, ptr1, len1, ptr2, len2, flags);
718 LeaveCriticalSection(This->crst);
720 return hr;
723 static HRESULT WINAPI DS8Primary_Play(IDirectSoundBuffer *iface, DWORD res1, DWORD res2, DWORD flags)
725 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
726 HRESULT hr;
728 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %"LONGFMT"u)\n", iface, res1, res2, flags);
730 if(!(flags & DSBPLAY_LOOPING))
732 WARN("Flags (%08"LONGFMT"x) not set to DSBPLAY_LOOPING\n", flags);
733 return DSERR_INVALIDPARAM;
736 EnterCriticalSection(This->crst);
737 hr = S_OK;
738 if(This->write_emu)
739 hr = IDirectSoundBuffer8_Play(This->write_emu, res1, res2, flags);
740 if(SUCCEEDED(hr))
741 This->stopped = FALSE;
742 LeaveCriticalSection(This->crst);
744 return hr;
747 static HRESULT WINAPI DS8Primary_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD pos)
749 WARN("(%p)->(%"LONGFMT"u)\n", iface, pos);
750 return DSERR_INVALIDCALL;
753 /* Just assume the format is crap, and clean up the damage */
754 static void copy_waveformat(WAVEFORMATEX *wfx, const WAVEFORMATEX *from)
756 if(from->wFormatTag == WAVE_FORMAT_PCM)
758 wfx->cbSize = 0;
759 if(from->wBitsPerSample == 8 ||
760 from->wBitsPerSample == 16 ||
761 from->wBitsPerSample == 24 ||
762 from->wBitsPerSample == 32)
763 wfx->wBitsPerSample = from->wBitsPerSample;
765 else if(from->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
767 WAVEFORMATEXTENSIBLE *wfe = (WAVEFORMATEXTENSIBLE*)wfx;
768 const WAVEFORMATEXTENSIBLE *fromx = (const WAVEFORMATEXTENSIBLE*)from;
769 DWORD size = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
771 /* Fail silently.. */
772 if(from->cbSize < size)
773 return;
774 if(!fromx->Samples.wValidBitsPerSample &&
775 !fromx->Format.wBitsPerSample)
776 return;
778 if(!IsEqualGUID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM) &&
779 !IsEqualGUID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))
781 ERR("Unhandled extensible format: %s\n", debugstr_guid(&wfe->SubFormat));
782 return;
785 wfe->Format.wBitsPerSample = from->wBitsPerSample;
786 wfe->Samples.wValidBitsPerSample = fromx->Samples.wValidBitsPerSample;
787 if(!wfe->Samples.wValidBitsPerSample)
788 wfe->Samples.wValidBitsPerSample = wfe->Format.wBitsPerSample;
789 wfe->Format.cbSize = size;
790 wfe->dwChannelMask = fromx->dwChannelMask;
791 wfe->SubFormat = fromx->SubFormat;
793 else
795 ERR("Unhandled format tag %04x\n", from->wFormatTag);
796 return;
799 if(from->nChannels)
800 wfx->nChannels = from->nChannels;
801 wfx->wFormatTag = from->wFormatTag;
802 if(from->nSamplesPerSec >= DSBFREQUENCY_MIN &&
803 from->nSamplesPerSec <= DSBFREQUENCY_MAX)
804 wfx->nSamplesPerSec = from->nSamplesPerSec;
805 wfx->nBlockAlign = wfx->wBitsPerSample * wfx->nChannels / 8;
806 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
809 static HRESULT WINAPI DS8Primary_SetFormat(IDirectSoundBuffer *iface, const WAVEFORMATEX *wfx)
811 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
812 HRESULT hr = S_OK;
813 ALCint freq;
815 TRACE("(%p)->(%p)\n", iface, wfx);
817 if(!wfx)
819 WARN("Missing format\n");
820 return DSERR_INVALIDPARAM;
823 EnterCriticalSection(This->crst);
825 if(This->parent->prio_level < DSSCL_PRIORITY)
827 hr = DSERR_PRIOLEVELNEEDED;
828 goto out;
831 TRACE("Requested primary format:\n"
832 " FormatTag = %04x\n"
833 " Channels = %u\n"
834 " SamplesPerSec = %"LONGFMT"u\n"
835 " AvgBytesPerSec = %"LONGFMT"u\n"
836 " BlockAlign = %u\n"
837 " BitsPerSample = %u\n",
838 wfx->wFormatTag, wfx->nChannels,
839 wfx->nSamplesPerSec, wfx->nAvgBytesPerSec,
840 wfx->nBlockAlign, wfx->wBitsPerSample);
842 copy_waveformat(&This->format.Format, wfx);
844 freq = This->format.Format.nSamplesPerSec;
845 alcGetIntegerv(This->parent->device, ALC_FREQUENCY, 1, &freq);
846 checkALCError(This->parent->device);
848 This->format.Format.nSamplesPerSec = freq;
849 This->format.Format.nAvgBytesPerSec = This->format.Format.nBlockAlign *
850 This->format.Format.nSamplesPerSec;
852 if(This->write_emu)
854 DS8Buffer *buf;
855 DSBUFFERDESC desc;
857 memset(&desc, 0, sizeof(desc));
858 desc.dwSize = sizeof(desc);
859 desc.dwFlags = DSBCAPS_LOCHARDWARE|DSBCAPS_CTRLPAN;
860 desc.dwBufferBytes = This->buf_size - (This->buf_size % This->format.Format.nBlockAlign);
861 desc.lpwfxFormat = &This->format.Format;
863 hr = DS8Buffer_Create(&buf, This, NULL);
864 if(FAILED(hr))
865 goto out;
867 hr = IDirectSoundBuffer8_Initialize(&buf->IDirectSoundBuffer8_iface, &This->parent->IDirectSound_iface, &desc);
868 if(FAILED(hr))
869 DS8Buffer_Destroy(buf);
870 else
872 IDirectSoundBuffer8_Release(This->write_emu);
873 This->write_emu = &buf->IDirectSoundBuffer8_iface;
877 out:
878 LeaveCriticalSection(This->crst);
879 return hr;
882 static HRESULT WINAPI DS8Primary_SetVolume(IDirectSoundBuffer *iface, LONG vol)
884 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
886 TRACE("(%p)->(%"LONGFMT"d)\n", iface, vol);
888 if(vol > DSBVOLUME_MAX || vol < DSBVOLUME_MIN)
890 WARN("Invalid volume (%"LONGFMT"d)\n", vol);
891 return DSERR_INVALIDPARAM;
894 if(!(This->flags&DSBCAPS_CTRLVOLUME))
895 return DSERR_CONTROLUNAVAIL;
897 setALContext(This->ctx);
898 alListenerf(AL_GAIN, mB_to_gain(vol));
899 popALContext();
901 return DS_OK;
904 static HRESULT WINAPI DS8Primary_SetPan(IDirectSoundBuffer *iface, LONG pan)
906 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
907 HRESULT hr;
909 TRACE("(%p)->(%"LONGFMT"d)\n", iface, pan);
911 if(pan > DSBPAN_RIGHT || pan < DSBPAN_LEFT)
913 WARN("invalid parameter: pan = %"LONGFMT"d\n", pan);
914 return DSERR_INVALIDPARAM;
917 EnterCriticalSection(This->crst);
918 if(!(This->flags&DSBCAPS_CTRLPAN))
920 WARN("control unavailable\n");
921 hr = DSERR_CONTROLUNAVAIL;
923 else if(This->write_emu)
924 hr = IDirectSoundBuffer8_SetPan(This->write_emu, pan);
925 else
927 FIXME("Not supported\n");
928 hr = E_NOTIMPL;
930 LeaveCriticalSection(This->crst);
932 return hr;
935 static HRESULT WINAPI DS8Primary_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
937 WARN("(%p)->(%"LONGFMT"u)\n", iface, freq);
938 return DSERR_CONTROLUNAVAIL;
941 static HRESULT WINAPI DS8Primary_Stop(IDirectSoundBuffer *iface)
943 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
944 HRESULT hr = S_OK;
946 TRACE("(%p)->()\n", iface);
948 EnterCriticalSection(This->crst);
949 if(This->write_emu)
950 hr = IDirectSoundBuffer8_Stop(This->write_emu);
951 if(SUCCEEDED(hr))
952 This->stopped = TRUE;
953 LeaveCriticalSection(This->crst);
955 return hr;
958 static HRESULT WINAPI DS8Primary_Unlock(IDirectSoundBuffer *iface, void *ptr1, DWORD len1, void *ptr2, DWORD len2)
960 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
961 HRESULT hr = DSERR_INVALIDCALL;
963 TRACE("(%p)->(%p, %"LONGFMT"u, %p, %"LONGFMT"u)\n", iface, ptr1, len1, ptr2, len2);
965 EnterCriticalSection(This->crst);
966 if(This->write_emu)
967 hr = IDirectSoundBuffer8_Unlock(This->write_emu, ptr1, len1, ptr2, len2);
968 LeaveCriticalSection(This->crst);
970 return hr;
973 static HRESULT WINAPI DS8Primary_Restore(IDirectSoundBuffer *iface)
975 DS8Primary *This = impl_from_IDirectSoundBuffer(iface);
976 HRESULT hr = S_OK;
978 TRACE("(%p)->()\n", iface);
980 EnterCriticalSection(This->crst);
981 if(This->write_emu)
982 hr = IDirectSoundBuffer8_Restore(This->write_emu);
983 LeaveCriticalSection(This->crst);
985 return hr;
988 static const IDirectSoundBufferVtbl DS8Primary_Vtbl =
990 DS8Primary_QueryInterface,
991 DS8Primary_AddRef,
992 DS8Primary_Release,
993 DS8Primary_GetCaps,
994 DS8Primary_GetCurrentPosition,
995 DS8Primary_GetFormat,
996 DS8Primary_GetVolume,
997 DS8Primary_GetPan,
998 DS8Primary_GetFrequency,
999 DS8Primary_GetStatus,
1000 DS8Primary_Initialize,
1001 DS8Primary_Lock,
1002 DS8Primary_Play,
1003 DS8Primary_SetCurrentPosition,
1004 DS8Primary_SetFormat,
1005 DS8Primary_SetVolume,
1006 DS8Primary_SetPan,
1007 DS8Primary_SetFrequency,
1008 DS8Primary_Stop,
1009 DS8Primary_Unlock,
1010 DS8Primary_Restore
1014 static HRESULT WINAPI DS8Primary3D_QueryInterface(IDirectSound3DListener *iface, REFIID riid, void **ppv)
1016 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1017 return DS8Primary_QueryInterface(&This->IDirectSoundBuffer_iface, riid, ppv);
1020 static ULONG WINAPI DS8Primary3D_AddRef(IDirectSound3DListener *iface)
1022 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1023 LONG ret;
1025 ret = InterlockedIncrement(&This->ds3d_ref);
1026 TRACE("new refcount %"LONGFMT"d\n", ret);
1028 return ret;
1031 static ULONG WINAPI DS8Primary3D_Release(IDirectSound3DListener *iface)
1033 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1034 LONG ret;
1036 ret = InterlockedDecrement(&This->ds3d_ref);
1037 TRACE("new refcount %"LONGFMT"d\n", ret);
1039 return ret;
1043 static HRESULT WINAPI DS8Primary3D_GetAllParameters(IDirectSound3DListener *iface, DS3DLISTENER *listener)
1045 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1047 TRACE("(%p)->(%p)\n", iface, listener);
1049 if(!listener || listener->dwSize < sizeof(*listener))
1051 WARN("Invalid DS3DLISTENER %p %"LONGFMT"u\n", listener, listener ? listener->dwSize : 0);
1052 return DSERR_INVALIDPARAM;
1055 EnterCriticalSection(This->crst);
1056 setALContext(This->ctx);
1057 IDirectSound3DListener_GetPosition(iface, &listener->vPosition);
1058 IDirectSound3DListener_GetVelocity(iface, &listener->vVelocity);
1059 IDirectSound3DListener_GetOrientation(iface, &listener->vOrientFront, &listener->vOrientTop);
1060 IDirectSound3DListener_GetDistanceFactor(iface, &listener->flDistanceFactor);
1061 IDirectSound3DListener_GetRolloffFactor(iface, &listener->flRolloffFactor);
1062 IDirectSound3DListener_GetDopplerFactor(iface, &listener->flDopplerFactor);
1063 popALContext();
1064 LeaveCriticalSection(This->crst);
1066 return DS_OK;
1069 static HRESULT WINAPI DS8Primary3D_GetDistanceFactor(IDirectSound3DListener *iface, D3DVALUE *distancefactor)
1071 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1073 TRACE("(%p)->(%p)\n", iface, distancefactor);
1075 if(!distancefactor)
1077 WARN("Invalid parameter %p\n", distancefactor);
1078 return DSERR_INVALIDPARAM;
1081 setALContext(This->ctx);
1082 *distancefactor = 343.3f/alGetFloat(AL_SPEED_OF_SOUND);
1083 checkALError();
1084 popALContext();
1086 return S_OK;
1089 static HRESULT WINAPI DS8Primary3D_GetDopplerFactor(IDirectSound3DListener *iface, D3DVALUE *dopplerfactor)
1091 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1093 TRACE("(%p)->(%p)\n", iface, dopplerfactor);
1095 if(!dopplerfactor)
1097 WARN("Invalid parameter %p\n", dopplerfactor);
1098 return DSERR_INVALIDPARAM;
1101 setALContext(This->ctx);
1102 *dopplerfactor = alGetFloat(AL_DOPPLER_FACTOR);
1103 checkALError();
1104 popALContext();
1106 return S_OK;
1109 static HRESULT WINAPI DS8Primary3D_GetOrientation(IDirectSound3DListener *iface, D3DVECTOR *front, D3DVECTOR *top)
1111 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1112 ALfloat orient[6];
1114 TRACE("(%p)->(%p, %p)\n", iface, front, top);
1116 if(!front || !top)
1118 WARN("Invalid parameter %p %p\n", front, top);
1119 return DSERR_INVALIDPARAM;
1122 setALContext(This->ctx);
1123 alGetListenerfv(AL_ORIENTATION, orient);
1124 checkALError();
1125 popALContext();
1127 front->x = orient[0];
1128 front->y = orient[1];
1129 front->z = -orient[2];
1130 top->x = orient[3];
1131 top->y = orient[4];
1132 top->z = -orient[5];
1133 return S_OK;
1136 static HRESULT WINAPI DS8Primary3D_GetPosition(IDirectSound3DListener *iface, D3DVECTOR *pos)
1138 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1139 ALfloat alpos[3];
1141 TRACE("(%p)->(%p)\n", iface, pos);
1143 if(!pos)
1145 WARN("Invalid parameter %p\n", pos);
1146 return DSERR_INVALIDPARAM;
1149 setALContext(This->ctx);
1150 alGetListenerfv(AL_POSITION, alpos);
1151 checkALError();
1152 popALContext();
1154 pos->x = alpos[0];
1155 pos->y = alpos[1];
1156 pos->z = -alpos[2];
1157 return S_OK;
1160 static HRESULT WINAPI DS8Primary3D_GetRolloffFactor(IDirectSound3DListener *iface, D3DVALUE *rollofffactor)
1162 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1164 TRACE("(%p)->(%p)\n", iface, rollofffactor);
1166 if(!rollofffactor)
1168 WARN("Invalid parameter %p\n", rollofffactor);
1169 return DSERR_INVALIDPARAM;
1172 EnterCriticalSection(This->crst);
1173 *rollofffactor = This->rollofffactor;
1174 LeaveCriticalSection(This->crst);
1176 return S_OK;
1179 static HRESULT WINAPI DS8Primary3D_GetVelocity(IDirectSound3DListener *iface, D3DVECTOR *velocity)
1181 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1182 ALfloat vel[3];
1184 TRACE("(%p)->(%p)\n", iface, velocity);
1186 if(!velocity)
1188 WARN("Invalid parameter %p\n", velocity);
1189 return DSERR_INVALIDPARAM;
1192 setALContext(This->ctx);
1193 alGetListenerfv(AL_VELOCITY, vel);
1194 checkALError();
1195 popALContext();
1197 velocity->x = vel[0];
1198 velocity->y = vel[1];
1199 velocity->z = -vel[2];
1200 return S_OK;
1203 static HRESULT WINAPI DS8Primary3D_SetAllParameters(IDirectSound3DListener *iface, const DS3DLISTENER *listen, DWORD apply)
1205 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1207 TRACE("(%p)->(%p, %"LONGFMT"u)\n", iface, listen, apply);
1209 if(!listen || listen->dwSize < sizeof(*listen))
1211 WARN("Invalid parameter %p %"LONGFMT"u\n", listen, listen ? listen->dwSize : 0);
1212 return DSERR_INVALIDPARAM;
1215 if(listen->flDistanceFactor > DS3D_MAXDISTANCEFACTOR ||
1216 listen->flDistanceFactor < DS3D_MINDISTANCEFACTOR)
1218 WARN("Invalid distance factor (%f)\n", listen->flDistanceFactor);
1219 return DSERR_INVALIDPARAM;
1222 if(listen->flDopplerFactor > DS3D_MAXDOPPLERFACTOR ||
1223 listen->flDopplerFactor < DS3D_MINDOPPLERFACTOR)
1225 WARN("Invalid doppler factor (%f)\n", listen->flDopplerFactor);
1226 return DSERR_INVALIDPARAM;
1229 if(listen->flRolloffFactor < DS3D_MINROLLOFFFACTOR ||
1230 listen->flRolloffFactor > DS3D_MAXROLLOFFFACTOR)
1232 WARN("Invalid rolloff factor (%f)\n", listen->flRolloffFactor);
1233 return DSERR_INVALIDPARAM;
1236 EnterCriticalSection(This->crst);
1237 setALContext(This->ctx);
1238 IDirectSound3DListener_SetPosition(iface, listen->vPosition.x, listen->vPosition.y, listen->vPosition.z, apply);
1239 IDirectSound3DListener_SetVelocity(iface, listen->vVelocity.x, listen->vVelocity.y, listen->vVelocity.z, apply);
1240 IDirectSound3DListener_SetOrientation(iface, listen->vOrientFront.x, listen->vOrientFront.y, listen->vOrientFront.z,
1241 listen->vOrientTop.x, listen->vOrientTop.y, listen->vOrientTop.z, apply);
1242 IDirectSound3DListener_SetDistanceFactor(iface, listen->flDistanceFactor, apply);
1243 IDirectSound3DListener_SetRolloffFactor(iface, listen->flRolloffFactor, apply);
1244 IDirectSound3DListener_SetDopplerFactor(iface, listen->flDopplerFactor, apply);
1245 popALContext();
1246 LeaveCriticalSection(This->crst);
1248 return S_OK;
1251 static HRESULT WINAPI DS8Primary3D_SetDistanceFactor(IDirectSound3DListener *iface, D3DVALUE factor, DWORD apply)
1253 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1255 TRACE("(%p)->(%f, %"LONGFMT"u)\n", iface, factor, apply);
1257 if(factor < DS3D_MINDISTANCEFACTOR ||
1258 factor > DS3D_MAXDISTANCEFACTOR)
1260 WARN("Invalid parameter %f\n", factor);
1261 return DSERR_INVALIDPARAM;
1264 if(apply == DS3D_DEFERRED)
1266 EnterCriticalSection(This->crst);
1267 This->listen.flDistanceFactor = factor;
1268 This->dirty.bit.distancefactor = 1;
1269 LeaveCriticalSection(This->crst);
1271 else
1273 setALContext(This->ctx);
1274 alSpeedOfSound(343.3f/factor);
1275 if(This->SupportedExt[EXT_EFX])
1276 alListenerf(AL_METERS_PER_UNIT, factor);
1277 checkALError();
1278 popALContext();
1281 return S_OK;
1284 static HRESULT WINAPI DS8Primary3D_SetDopplerFactor(IDirectSound3DListener *iface, D3DVALUE factor, DWORD apply)
1286 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1288 TRACE("(%p)->(%f, %"LONGFMT"u)\n", iface, factor, apply);
1290 if(factor < DS3D_MINDOPPLERFACTOR ||
1291 factor > DS3D_MAXDOPPLERFACTOR)
1293 WARN("Invalid parameter %f\n", factor);
1294 return DSERR_INVALIDPARAM;
1297 if(apply == DS3D_DEFERRED)
1299 EnterCriticalSection(This->crst);
1300 This->listen.flDopplerFactor = factor;
1301 This->dirty.bit.dopplerfactor = 1;
1302 LeaveCriticalSection(This->crst);
1304 else
1306 setALContext(This->ctx);
1307 alDopplerFactor(factor);
1308 checkALError();
1309 popALContext();
1312 return S_OK;
1315 static HRESULT WINAPI DS8Primary3D_SetOrientation(IDirectSound3DListener *iface, D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop, DWORD apply)
1317 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1319 TRACE("(%p)->(%f, %f, %f, %f, %f, %f, %"LONGFMT"u)\n", iface, xFront, yFront, zFront, xTop, yTop, zTop, apply);
1321 if(apply == DS3D_DEFERRED)
1323 EnterCriticalSection(This->crst);
1324 This->listen.vOrientFront.x = xFront;
1325 This->listen.vOrientFront.y = yFront;
1326 This->listen.vOrientFront.z = zFront;
1327 This->listen.vOrientTop.x = xTop;
1328 This->listen.vOrientTop.y = yTop;
1329 This->listen.vOrientTop.z = zTop;
1330 This->dirty.bit.orientation = 1;
1331 LeaveCriticalSection(This->crst);
1333 else
1335 ALfloat orient[6] = {
1336 xFront, yFront, -zFront,
1337 xTop, yTop, -zTop
1339 setALContext(This->ctx);
1340 alListenerfv(AL_ORIENTATION, orient);
1341 checkALError();
1342 popALContext();
1345 return S_OK;
1348 static HRESULT WINAPI DS8Primary3D_SetPosition(IDirectSound3DListener *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
1350 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1352 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", iface, x, y, z, apply);
1354 if(apply == DS3D_DEFERRED)
1356 EnterCriticalSection(This->crst);
1357 This->listen.vPosition.x = x;
1358 This->listen.vPosition.y = y;
1359 This->listen.vPosition.z = z;
1360 This->dirty.bit.pos = 1;
1361 LeaveCriticalSection(This->crst);
1363 else
1365 setALContext(This->ctx);
1366 alListener3f(AL_POSITION, x, y, -z);
1367 checkALError();
1368 popALContext();
1371 return S_OK;
1374 static HRESULT WINAPI DS8Primary3D_SetRolloffFactor(IDirectSound3DListener *iface, D3DVALUE factor, DWORD apply)
1376 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1378 TRACE("(%p)->(%f, %"LONGFMT"u)\n", iface, factor, apply);
1380 if(factor < DS3D_MINROLLOFFFACTOR ||
1381 factor > DS3D_MAXROLLOFFFACTOR)
1383 WARN("Invalid parameter %f\n", factor);
1384 return DSERR_INVALIDPARAM;
1387 EnterCriticalSection(This->crst);
1388 if(apply == DS3D_DEFERRED)
1390 This->listen.flRolloffFactor = factor;
1391 This->dirty.bit.rollofffactor = 1;
1393 else
1395 DWORD i;
1397 setALContext(This->ctx);
1398 for(i = 0;i < This->nbuffers;++i)
1400 if(This->buffers[i]->ds3dmode != DS3DMODE_DISABLE)
1401 alSourcef(This->buffers[i]->source, AL_ROLLOFF_FACTOR, factor);
1403 checkALError();
1404 popALContext();
1406 This->rollofffactor = factor;
1408 LeaveCriticalSection(This->crst);
1410 return S_OK;
1413 static HRESULT WINAPI DS8Primary3D_SetVelocity(IDirectSound3DListener *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
1415 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1417 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", iface, x, y, z, apply);
1419 if(apply == DS3D_DEFERRED)
1421 EnterCriticalSection(This->crst);
1422 This->listen.vVelocity.x = x;
1423 This->listen.vVelocity.y = y;
1424 This->listen.vVelocity.z = z;
1425 This->dirty.bit.vel = 1;
1426 LeaveCriticalSection(This->crst);
1428 else
1430 setALContext(This->ctx);
1431 alListener3f(AL_VELOCITY, x, y, -z);
1432 checkALError();
1433 popALContext();
1436 return S_OK;
1439 static HRESULT WINAPI DS8Primary3D_CommitDeferredSettings(IDirectSound3DListener *iface)
1441 DS8Primary *This = impl_from_IDirectSound3DListener(iface);
1442 const DS3DLISTENER *listen = &This->listen;
1443 DWORD i;
1445 EnterCriticalSection(This->crst);
1446 setALContext(This->ctx);
1447 This->DeferUpdates();
1449 if(This->dirty.bit.pos)
1450 alListener3f(AL_POSITION, listen->vPosition.x, listen->vPosition.y, -listen->vPosition.z);
1451 if(This->dirty.bit.vel)
1452 alListener3f(AL_VELOCITY, listen->vVelocity.x, listen->vVelocity.y, -listen->vVelocity.z);
1453 if(This->dirty.bit.orientation)
1455 ALfloat orient[6] = {
1456 listen->vOrientFront.x, listen->vOrientFront.y, -listen->vOrientFront.z,
1457 listen->vOrientTop.x, listen->vOrientTop.y, -listen->vOrientTop.z
1459 alListenerfv(AL_ORIENTATION, orient);
1461 if(This->dirty.bit.distancefactor)
1463 alSpeedOfSound(343.3f/listen->flDistanceFactor);
1464 if(This->SupportedExt[EXT_EFX])
1465 alListenerf(AL_METERS_PER_UNIT, listen->flDistanceFactor);
1468 if(This->dirty.bit.rollofffactor)
1470 ALfloat rolloff = This->rollofffactor;
1471 for(i = 0;i < This->nbuffers;++i)
1473 DS8Buffer *buf = This->buffers[i];
1474 if(buf->ds3dmode != DS3DMODE_DISABLE)
1475 alSourcef(buf->source, AL_ROLLOFF_FACTOR, rolloff);
1479 if(This->dirty.bit.dopplerfactor)
1480 alDopplerFactor(listen->flDopplerFactor);
1482 if(This->dirty.bit.effect)
1483 This->ExtAL->AuxiliaryEffectSloti(This->auxslot, AL_EFFECTSLOT_EFFECT, This->effect);
1485 /* checkALError is here for debugging */
1486 checkALError();
1488 TRACE("Dirty flags was: 0x%02x\n", This->dirty.flags);
1489 This->dirty.flags = 0;
1491 for(i = 0;i < This->nbuffers;++i)
1493 DS8Buffer *buf = This->buffers[i];
1495 if(!buf->dirty.flags)
1496 continue;
1498 if(buf->dirty.bit.pos)
1499 alSource3f(buf->source, AL_POSITION,
1500 buf->ds3dbuffer.vPosition.x,
1501 buf->ds3dbuffer.vPosition.y,
1502 -buf->ds3dbuffer.vPosition.z);
1503 if(buf->dirty.bit.vel)
1504 alSource3f(buf->source, AL_VELOCITY,
1505 buf->ds3dbuffer.vVelocity.x,
1506 buf->ds3dbuffer.vVelocity.y,
1507 -buf->ds3dbuffer.vVelocity.z);
1508 if(buf->dirty.bit.cone_angles)
1510 alSourcei(buf->source, AL_CONE_INNER_ANGLE,
1511 buf->ds3dbuffer.dwInsideConeAngle);
1512 alSourcei(buf->source, AL_CONE_OUTER_ANGLE,
1513 buf->ds3dbuffer.dwOutsideConeAngle);
1515 if(buf->dirty.bit.cone_orient)
1516 alSource3f(buf->source, AL_DIRECTION,
1517 buf->ds3dbuffer.vConeOrientation.x,
1518 buf->ds3dbuffer.vConeOrientation.y,
1519 -buf->ds3dbuffer.vConeOrientation.z);
1520 if(buf->dirty.bit.cone_outsidevolume)
1521 alSourcef(buf->source, AL_CONE_OUTER_GAIN,
1522 mB_to_gain(buf->ds3dbuffer.lConeOutsideVolume));
1523 if(buf->dirty.bit.min_distance)
1524 alSourcef(buf->source, AL_REFERENCE_DISTANCE, buf->ds3dbuffer.flMinDistance);
1525 if(buf->dirty.bit.max_distance)
1526 alSourcef(buf->source, AL_MAX_DISTANCE, buf->ds3dbuffer.flMaxDistance);
1527 if(buf->dirty.bit.mode)
1529 buf->ds3dmode = buf->ds3dbuffer.dwMode;
1530 alSourcei(buf->source, AL_SOURCE_RELATIVE,
1531 (buf->ds3dmode!=DS3DMODE_NORMAL) ? AL_TRUE : AL_FALSE);
1532 alSourcef(buf->source, AL_ROLLOFF_FACTOR,
1533 (buf->ds3dmode==DS3DMODE_DISABLE) ? 0.0f : This->rollofffactor);
1535 buf->dirty.flags = 0;
1537 checkALError();
1539 This->ProcessUpdates();
1540 popALContext();
1541 LeaveCriticalSection(This->crst);
1543 return S_OK;
1546 static const IDirectSound3DListenerVtbl DS8Primary3D_Vtbl =
1548 DS8Primary3D_QueryInterface,
1549 DS8Primary3D_AddRef,
1550 DS8Primary3D_Release,
1551 DS8Primary3D_GetAllParameters,
1552 DS8Primary3D_GetDistanceFactor,
1553 DS8Primary3D_GetDopplerFactor,
1554 DS8Primary3D_GetOrientation,
1555 DS8Primary3D_GetPosition,
1556 DS8Primary3D_GetRolloffFactor,
1557 DS8Primary3D_GetVelocity,
1558 DS8Primary3D_SetAllParameters,
1559 DS8Primary3D_SetDistanceFactor,
1560 DS8Primary3D_SetDopplerFactor,
1561 DS8Primary3D_SetOrientation,
1562 DS8Primary3D_SetPosition,
1563 DS8Primary3D_SetRolloffFactor,
1564 DS8Primary3D_SetVelocity,
1565 DS8Primary3D_CommitDeferredSettings
1568 /* NOTE: Although the app handles listener properties through secondary buffers,
1569 * we pass the requests to the primary buffer though a propertyset interface.
1570 * These methods are not exposed to the app. */
1571 static HRESULT WINAPI DS8PrimaryProp_QueryInterface(IKsPropertySet *iface, REFIID riid, void **ppv)
1573 DS8Primary *This = impl_from_IKsPropertySet(iface);
1574 return DS8Primary_QueryInterface(&This->IDirectSoundBuffer_iface, riid, ppv);
1577 static ULONG WINAPI DS8PrimaryProp_AddRef(IKsPropertySet *iface)
1579 DS8Primary *This = impl_from_IKsPropertySet(iface);
1580 LONG ret;
1582 ret = InterlockedIncrement(&This->prop_ref);
1583 TRACE("new refcount %"LONGFMT"d\n", ret);
1585 return ret;
1588 static ULONG WINAPI DS8PrimaryProp_Release(IKsPropertySet *iface)
1590 DS8Primary *This = impl_from_IKsPropertySet(iface);
1591 LONG ret;
1593 ret = InterlockedDecrement(&This->prop_ref);
1594 TRACE("new refcount %"LONGFMT"d\n", ret);
1596 return ret;
1599 static HRESULT WINAPI DS8PrimaryProp_Get(IKsPropertySet *iface,
1600 REFGUID guidPropSet, ULONG dwPropID,
1601 LPVOID pInstanceData, ULONG cbInstanceData,
1602 LPVOID pPropData, ULONG cbPropData,
1603 PULONG pcbReturned)
1605 DS8Primary *This = impl_from_IKsPropertySet(iface);
1606 HRESULT res = E_PROP_ID_UNSUPPORTED;
1607 (void)pInstanceData;
1608 (void)cbInstanceData;
1610 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_ListenerProperties))
1612 EnterCriticalSection(This->crst);
1614 if(This->effect == 0)
1615 res = E_PROP_ID_UNSUPPORTED;
1616 else switch(dwPropID)
1618 case DSPROPERTY_EAXLISTENER_ALLPARAMETERS:
1619 res = DSERR_INVALIDPARAM;
1620 if(cbPropData >= sizeof(EAXLISTENERPROPERTIES))
1622 union {
1623 void *v;
1624 EAXLISTENERPROPERTIES *props;
1625 } data = { pPropData };
1627 *data.props = This->eax_prop;
1628 *pcbReturned = sizeof(EAXLISTENERPROPERTIES);
1629 res = DS_OK;
1631 break;
1633 case DSPROPERTY_EAXLISTENER_ROOM:
1634 res = DSERR_INVALIDPARAM;
1635 if(cbPropData >= sizeof(LONG))
1637 union {
1638 void *v;
1639 LONG *l;
1640 } data = { pPropData };
1642 *data.l = This->eax_prop.lRoom;
1643 *pcbReturned = sizeof(LONG);
1644 res = DS_OK;
1646 break;
1647 case DSPROPERTY_EAXLISTENER_ROOMHF:
1648 res = DSERR_INVALIDPARAM;
1649 if(cbPropData >= sizeof(LONG))
1651 union {
1652 void *v;
1653 LONG *l;
1654 } data = { pPropData };
1656 *data.l = This->eax_prop.lRoomHF;
1657 *pcbReturned = sizeof(LONG);
1658 res = DS_OK;
1660 break;
1662 case DSPROPERTY_EAXLISTENER_ROOMROLLOFFFACTOR:
1663 res = DSERR_INVALIDPARAM;
1664 if(cbPropData >= sizeof(FLOAT))
1666 union {
1667 void *v;
1668 FLOAT *fl;
1669 } data = { pPropData };
1671 *data.fl = This->eax_prop.flRoomRolloffFactor;
1672 *pcbReturned = sizeof(FLOAT);
1673 res = DS_OK;
1675 break;
1677 case DSPROPERTY_EAXLISTENER_ENVIRONMENT:
1678 res = DSERR_INVALIDPARAM;
1679 if(cbPropData >= sizeof(DWORD))
1681 union {
1682 void *v;
1683 DWORD *dw;
1684 } data = { pPropData };
1686 *data.dw = This->eax_prop.dwEnvironment;
1687 *pcbReturned = sizeof(DWORD);
1688 res = DS_OK;
1690 break;
1692 case DSPROPERTY_EAXLISTENER_ENVIRONMENTSIZE:
1693 res = DSERR_INVALIDPARAM;
1694 if(cbPropData >= sizeof(FLOAT))
1696 union {
1697 void *v;
1698 FLOAT *fl;
1699 } data = { pPropData };
1701 *data.fl = This->eax_prop.flEnvironmentSize;
1702 *pcbReturned = sizeof(FLOAT);
1703 res = DS_OK;
1705 break;
1706 case DSPROPERTY_EAXLISTENER_ENVIRONMENTDIFFUSION:
1707 res = DSERR_INVALIDPARAM;
1708 if(cbPropData >= sizeof(FLOAT))
1710 union {
1711 void *v;
1712 FLOAT *fl;
1713 } data = { pPropData };
1715 *data.fl = This->eax_prop.flEnvironmentDiffusion;
1716 *pcbReturned = sizeof(FLOAT);
1717 res = DS_OK;
1719 break;
1721 case DSPROPERTY_EAXLISTENER_AIRABSORPTIONHF:
1722 res = DSERR_INVALIDPARAM;
1723 if(cbPropData >= sizeof(FLOAT))
1725 union {
1726 void *v;
1727 FLOAT *fl;
1728 } data = { pPropData };
1730 *data.fl = This->eax_prop.flAirAbsorptionHF;
1731 *pcbReturned = sizeof(FLOAT);
1732 res = DS_OK;
1734 break;
1736 case DSPROPERTY_EAXLISTENER_FLAGS:
1737 res = DSERR_INVALIDPARAM;
1738 if(cbPropData >= sizeof(DWORD))
1740 union {
1741 void *v;
1742 DWORD *dw;
1743 } data = { pPropData };
1745 *data.dw = This->eax_prop.dwFlags;
1746 *pcbReturned = sizeof(DWORD);
1747 res = DS_OK;
1749 break;
1751 default:
1752 FIXME("Unhandled propid: 0x%08"LONGFMT"x\n", dwPropID);
1753 break;
1756 LeaveCriticalSection(This->crst);
1758 else
1759 FIXME("Unhandled propset: %s\n", debugstr_guid(guidPropSet));
1761 return res;
1764 static HRESULT WINAPI DS8PrimaryProp_Set(IKsPropertySet *iface,
1765 REFGUID guidPropSet, ULONG dwPropID,
1766 LPVOID pInstanceData, ULONG cbInstanceData,
1767 LPVOID pPropData, ULONG cbPropData)
1769 DS8Primary *This = impl_from_IKsPropertySet(iface);
1770 HRESULT res = E_PROP_ID_UNSUPPORTED;
1771 (void)pInstanceData;
1772 (void)cbInstanceData;
1774 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_ListenerProperties))
1776 DWORD propid = dwPropID & ~DSPROPERTY_EAXLISTENER_DEFERRED;
1777 BOOL immediate = !(dwPropID&DSPROPERTY_EAXLISTENER_DEFERRED);
1779 EnterCriticalSection(This->crst);
1780 setALContext(This->ctx);
1782 if(This->effect == 0)
1783 res = E_PROP_ID_UNSUPPORTED;
1784 else switch(propid)
1786 case 0: /* 0 = not setting any property, just apply */
1787 res = DS_OK;
1788 break;
1790 case DSPROPERTY_EAXLISTENER_ALLPARAMETERS:
1791 do_allparams:
1792 res = DSERR_INVALIDPARAM;
1793 if(cbPropData >= sizeof(EAXLISTENERPROPERTIES))
1795 union {
1796 const void *v;
1797 const EAXLISTENERPROPERTIES *props;
1798 } data = { pPropData };
1800 /* FIXME: Need to validate property values... Ignore? Clamp? Error? */
1801 This->eax_prop = *data.props;
1802 This->ExtAL->Effectf(This->effect, AL_REVERB_DENSITY,
1803 (data.props->flEnvironmentSize < 2.0f) ?
1804 (data.props->flEnvironmentSize - 1.0f) : 1.0f);
1805 This->ExtAL->Effectf(This->effect, AL_REVERB_DIFFUSION,
1806 data.props->flEnvironmentDiffusion);
1808 This->ExtAL->Effectf(This->effect, AL_REVERB_GAIN,
1809 mB_to_gain(data.props->lRoom));
1810 This->ExtAL->Effectf(This->effect, AL_REVERB_GAINHF,
1811 mB_to_gain(data.props->lRoomHF));
1813 This->ExtAL->Effectf(This->effect, AL_REVERB_ROOM_ROLLOFF_FACTOR,
1814 data.props->flRoomRolloffFactor);
1816 This->ExtAL->Effectf(This->effect, AL_REVERB_DECAY_TIME,
1817 data.props->flDecayTime);
1818 This->ExtAL->Effectf(This->effect, AL_REVERB_DECAY_HFRATIO,
1819 data.props->flDecayHFRatio);
1821 This->ExtAL->Effectf(This->effect, AL_REVERB_REFLECTIONS_GAIN,
1822 mB_to_gain(data.props->lReflections));
1823 This->ExtAL->Effectf(This->effect, AL_REVERB_REFLECTIONS_DELAY,
1824 data.props->flReflectionsDelay);
1826 This->ExtAL->Effectf(This->effect, AL_REVERB_LATE_REVERB_GAIN,
1827 mB_to_gain(data.props->lReverb));
1828 This->ExtAL->Effectf(This->effect, AL_REVERB_LATE_REVERB_DELAY,
1829 data.props->flReverbDelay);
1831 This->ExtAL->Effectf(This->effect, AL_REVERB_AIR_ABSORPTION_GAINHF,
1832 mB_to_gain(data.props->flAirAbsorptionHF));
1834 This->ExtAL->Effecti(This->effect, AL_REVERB_DECAY_HFLIMIT,
1835 (data.props->dwFlags&EAXLISTENERFLAGS_DECAYHFLIMIT) ?
1836 AL_TRUE : AL_FALSE);
1838 checkALError();
1840 This->dirty.bit.effect = 1;
1841 res = DS_OK;
1843 break;
1845 case DSPROPERTY_EAXLISTENER_ROOM:
1846 res = DSERR_INVALIDPARAM;
1847 if(cbPropData >= sizeof(LONG))
1849 union {
1850 const void *v;
1851 const LONG *l;
1852 } data = { pPropData };
1854 This->eax_prop.lRoom = *data.l;
1855 This->ExtAL->Effectf(This->effect, AL_REVERB_GAIN,
1856 mB_to_gain(This->eax_prop.lRoom));
1857 checkALError();
1859 This->dirty.bit.effect = 1;
1860 res = DS_OK;
1862 break;
1863 case DSPROPERTY_EAXLISTENER_ROOMHF:
1864 res = DSERR_INVALIDPARAM;
1865 if(cbPropData >= sizeof(LONG))
1867 union {
1868 const void *v;
1869 const LONG *l;
1870 } data = { pPropData };
1872 This->eax_prop.lRoomHF = *data.l;
1873 This->ExtAL->Effectf(This->effect, AL_REVERB_GAINHF,
1874 mB_to_gain(This->eax_prop.lRoomHF));
1875 checkALError();
1877 This->dirty.bit.effect = 1;
1878 res = DS_OK;
1880 break;
1882 case DSPROPERTY_EAXLISTENER_ROOMROLLOFFFACTOR:
1883 res = DSERR_INVALIDPARAM;
1884 if(cbPropData >= sizeof(FLOAT))
1886 union {
1887 const void *v;
1888 const FLOAT *fl;
1889 } data = { pPropData };
1891 This->eax_prop.flRoomRolloffFactor = *data.fl;
1892 This->ExtAL->Effectf(This->effect, AL_REVERB_ROOM_ROLLOFF_FACTOR,
1893 This->eax_prop.flRoomRolloffFactor);
1894 checkALError();
1896 This->dirty.bit.effect = 1;
1897 res = DS_OK;
1899 break;
1901 case DSPROPERTY_EAXLISTENER_ENVIRONMENT:
1902 res = DSERR_INVALIDPARAM;
1903 if(cbPropData >= sizeof(DWORD))
1905 union {
1906 const void *v;
1907 const DWORD *dw;
1908 } data = { pPropData };
1910 if(*data.dw < EAX_ENVIRONMENT_COUNT)
1912 /* Get the environment index's default and pass it down to
1913 * ALLPARAMETERS */
1914 propid = DSPROPERTY_EAXLISTENER_ALLPARAMETERS;
1915 pPropData = (void*)&EnvironmentDefaults[*data.dw];
1916 cbPropData = sizeof(EnvironmentDefaults[*data.dw]);
1917 goto do_allparams;
1920 break;
1922 case DSPROPERTY_EAXLISTENER_ENVIRONMENTSIZE:
1923 res = DSERR_INVALIDPARAM;
1924 if(cbPropData >= sizeof(FLOAT))
1926 union {
1927 const void *v;
1928 const FLOAT *fl;
1929 } data = { pPropData };
1931 if(*data.fl >= 1.0f && *data.fl <= 100.0f)
1933 double scale = (*data.fl)/This->eax_prop.flEnvironmentSize;
1935 This->eax_prop.flEnvironmentSize = *data.fl;
1937 if((This->eax_prop.dwFlags&EAXLISTENERFLAGS_DECAYTIMESCALE))
1939 This->eax_prop.flDecayTime *= scale;
1940 This->eax_prop.flDecayTime = clampF(This->eax_prop.flDecayTime, 0.1f, 20.0f);
1942 if((This->eax_prop.dwFlags&EAXLISTENERFLAGS_REFLECTIONSSCALE))
1944 This->eax_prop.lReflections += gain_to_mB(1.0/scale);
1945 This->eax_prop.lReflections = clampI(This->eax_prop.lReflections, -10000, 1000);
1947 if((This->eax_prop.dwFlags&EAXLISTENERFLAGS_REFLECTIONSDELAYSCALE))
1949 This->eax_prop.flReflectionsDelay *= scale;
1950 This->eax_prop.flReflectionsDelay = clampF(This->eax_prop.flReflectionsDelay, 0.0f, 0.3f);
1952 if((This->eax_prop.dwFlags&EAXLISTENERFLAGS_REVERBSCALE))
1954 This->eax_prop.lReverb += gain_to_mB(1.0/scale);
1955 This->eax_prop.lReverb = clampI(This->eax_prop.lReverb, -10000, 2000);
1957 if((This->eax_prop.dwFlags&EAXLISTENERFLAGS_REVERBDELAYSCALE))
1959 This->eax_prop.flReverbDelay *= scale;
1960 This->eax_prop.flReverbDelay = clampF(This->eax_prop.flReverbDelay, 0.0f, 0.1f);
1963 /* Pass the updated environment properties down to ALLPARAMETERS */
1964 propid = DSPROPERTY_EAXLISTENER_ALLPARAMETERS;
1965 pPropData = (void*)&This->eax_prop;
1966 cbPropData = sizeof(This->eax_prop);
1967 goto do_allparams;
1970 break;
1971 case DSPROPERTY_EAXLISTENER_ENVIRONMENTDIFFUSION:
1972 res = DSERR_INVALIDPARAM;
1973 if(cbPropData >= sizeof(FLOAT))
1975 union {
1976 const void *v;
1977 const FLOAT *fl;
1978 } data = { pPropData };
1980 This->eax_prop.flEnvironmentDiffusion = *data.fl;
1981 This->ExtAL->Effectf(This->effect, AL_REVERB_DIFFUSION,
1982 This->eax_prop.flEnvironmentDiffusion);
1983 checkALError();
1985 This->dirty.bit.effect = 1;
1986 res = DS_OK;
1988 break;
1990 case DSPROPERTY_EAXLISTENER_AIRABSORPTIONHF:
1991 res = DSERR_INVALIDPARAM;
1992 if(cbPropData >= sizeof(FLOAT))
1994 union {
1995 const void *v;
1996 const FLOAT *fl;
1997 } data = { pPropData };
1999 This->eax_prop.flAirAbsorptionHF = *data.fl;
2000 This->ExtAL->Effectf(This->effect, AL_REVERB_AIR_ABSORPTION_GAINHF,
2001 mB_to_gain(This->eax_prop.flAirAbsorptionHF));
2002 checkALError();
2004 This->dirty.bit.effect = 1;
2005 res = DS_OK;
2007 break;
2009 case DSPROPERTY_EAXLISTENER_FLAGS:
2010 res = DSERR_INVALIDPARAM;
2011 if(cbPropData >= sizeof(DWORD))
2013 union {
2014 const void *v;
2015 const DWORD *dw;
2016 } data = { pPropData };
2018 This->eax_prop.dwFlags = *data.dw;
2019 This->ExtAL->Effecti(This->effect, AL_REVERB_DECAY_HFLIMIT,
2020 (This->eax_prop.dwFlags&EAXLISTENERFLAGS_DECAYHFLIMIT) ?
2021 AL_TRUE : AL_FALSE);
2022 checkALError();
2024 This->dirty.bit.effect = 1;
2025 res = DS_OK;
2027 break;
2029 default:
2030 FIXME("Unhandled propid: 0x%08"LONGFMT"x\n", propid);
2031 break;
2034 if(res == DS_OK && immediate)
2035 IDirectSound3DListener_CommitDeferredSettings(&This->IDirectSound3DListener_iface);
2037 popALContext();
2038 LeaveCriticalSection(This->crst);
2040 else
2041 FIXME("Unhandled propset: %s\n", debugstr_guid(guidPropSet));
2043 return res;
2046 static HRESULT WINAPI DS8PrimaryProp_QuerySupport(IKsPropertySet *iface,
2047 REFGUID guidPropSet, ULONG dwPropID,
2048 PULONG pTypeSupport)
2050 DS8Primary *This = impl_from_IKsPropertySet(iface);
2051 HRESULT res = E_PROP_ID_UNSUPPORTED;
2053 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_ListenerProperties))
2055 EnterCriticalSection(This->crst);
2057 if(This->effect == 0)
2058 res = E_PROP_ID_UNSUPPORTED;
2059 else if(dwPropID == DSPROPERTY_EAXLISTENER_ALLPARAMETERS ||
2060 dwPropID == DSPROPERTY_EAXLISTENER_ROOM ||
2061 dwPropID == DSPROPERTY_EAXLISTENER_ROOMHF ||
2062 dwPropID == DSPROPERTY_EAXLISTENER_ROOMROLLOFFFACTOR ||
2063 dwPropID == DSPROPERTY_EAXLISTENER_ENVIRONMENT ||
2064 dwPropID == DSPROPERTY_EAXLISTENER_ENVIRONMENTSIZE ||
2065 dwPropID == DSPROPERTY_EAXLISTENER_ENVIRONMENTDIFFUSION ||
2066 dwPropID == DSPROPERTY_EAXLISTENER_AIRABSORPTIONHF ||
2067 dwPropID == DSPROPERTY_EAXLISTENER_FLAGS)
2069 *pTypeSupport = KSPROPERTY_SUPPORT_GET|KSPROPERTY_SUPPORT_SET;
2070 res = DS_OK;
2072 else
2073 FIXME("Unhandled propid: 0x%08"LONGFMT"x\n", dwPropID);
2075 LeaveCriticalSection(This->crst);
2077 else
2078 FIXME("Unhandled propset: %s\n", debugstr_guid(guidPropSet));
2080 return res;
2083 static const IKsPropertySetVtbl DS8PrimaryProp_Vtbl =
2085 DS8PrimaryProp_QueryInterface,
2086 DS8PrimaryProp_AddRef,
2087 DS8PrimaryProp_Release,
2088 DS8PrimaryProp_Get,
2089 DS8PrimaryProp_Set,
2090 DS8PrimaryProp_QuerySupport