msvcp90: Fix usage of fpos<>::off vs fpos<>::pos.
[wine.git] / dlls / dsound / sound3d.c
blob604d19be5f9eed08c91c4e19fea9e3643e0545cb
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 TransGaming Technologies, Inc.
6 * Copyright 2002-2003 Rok Mandeljc <rok.mandeljc@gimb.org>
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 * Most thread locking is complete. There may be a few race
24 * conditions still lurking.
26 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
27 * and a Turtle Beach Tropez+.
29 * TODO:
30 * Implement SetCooperativeLevel properly (need to address focus issues)
31 * Implement DirectSound3DBuffers (stubs in place)
32 * Use hardware 3D support if available
33 * Add critical section locking inside Release and AddRef methods
34 * Handle static buffers - put those in hardware, non-static not in hardware
35 * Hardware DuplicateSoundBuffer
36 * Proper volume calculation, and setting volume in HEL primary buffer
37 * Optimize WINMM and negotiate fragment size, decrease DS_HEL_MARGIN
40 #include <stdarg.h>
41 #include <math.h> /* Insomnia - pow() function */
43 #define NONAMELESSUNION
44 #define NONAMELESSSTRUCT
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winuser.h"
48 #include "mmsystem.h"
49 #include "winternl.h"
50 #include "mmddk.h"
51 #include "wine/debug.h"
52 #include "dsound.h"
53 #include "dsound_private.h"
55 /* default velocity of sound in the air */
56 #define DEFAULT_VELOCITY 340
58 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
60 /*******************************************************************************
61 * Auxiliary functions
64 /* scalar product (I believe it's called dot product in English) */
65 static inline D3DVALUE ScalarProduct (const D3DVECTOR *a, const D3DVECTOR *b)
67 D3DVALUE c;
68 c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
69 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
70 b->z, c);
71 return c;
74 /* vector product (I believe it's called cross product in English */
75 static inline D3DVECTOR VectorProduct (const D3DVECTOR *a, const D3DVECTOR *b)
77 D3DVECTOR c;
78 c.x = (a->y*b->z) - (a->z*b->y);
79 c.y = (a->z*b->x) - (a->x*b->z);
80 c.z = (a->x*b->y) - (a->y*b->x);
81 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
82 b->z, c.x, c.y, c.z);
83 return c;
86 /* magnitude (length) of vector */
87 static inline D3DVALUE VectorMagnitude (const D3DVECTOR *a)
89 D3DVALUE l;
90 l = sqrt (ScalarProduct (a, a));
91 TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
92 return l;
95 /* conversion between radians and degrees */
96 static inline D3DVALUE RadToDeg (D3DVALUE angle)
98 D3DVALUE newangle;
99 newangle = angle * (360/(2*M_PI));
100 TRACE("%f rad = %f deg\n", angle, newangle);
101 return newangle;
104 /* angle between vectors - rad version */
105 static inline D3DVALUE AngleBetweenVectorsRad (const D3DVECTOR *a, const D3DVECTOR *b)
107 D3DVALUE la, lb, product, angle, cos;
108 /* definition of scalar product: a*b = |a|*|b|*cos... therefore: */
109 product = ScalarProduct (a,b);
110 la = VectorMagnitude (a);
111 lb = VectorMagnitude (b);
112 if (!la || !lb)
113 return 0;
115 cos = product/(la*lb);
116 angle = acos(cos);
117 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians (%f degrees)\n", a->x, a->y, a->z, b->x,
118 b->y, b->z, angle, RadToDeg(angle));
119 return angle;
122 static inline D3DVALUE AngleBetweenVectorsDeg (const D3DVECTOR *a, const D3DVECTOR *b)
124 return RadToDeg(AngleBetweenVectorsRad(a, b));
127 /* calculates vector between two points */
128 static inline D3DVECTOR VectorBetweenTwoPoints (const D3DVECTOR *a, const D3DVECTOR *b)
130 D3DVECTOR c;
131 c.x = b->x - a->x;
132 c.y = b->y - a->y;
133 c.z = b->z - a->z;
134 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
135 b->z, c.x, c.y, c.z);
136 return c;
139 /* calculates the length of vector's projection on another vector */
140 static inline D3DVALUE ProjectVector (const D3DVECTOR *a, const D3DVECTOR *p)
142 D3DVALUE prod, result;
143 prod = ScalarProduct(a, p);
144 result = prod/VectorMagnitude(p);
145 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
146 p->y, p->z, result);
147 return result;
150 /*******************************************************************************
151 * 3D Buffer and Listener mixing
154 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
156 /* volume, at which the sound will be played after all calcs. */
157 D3DVALUE lVolume = 0;
158 /* stuff for distance related stuff calc. */
159 D3DVECTOR vDistance;
160 D3DVALUE flDistance = 0;
161 /* panning related stuff */
162 D3DVALUE flAngle;
163 D3DVECTOR vLeft;
164 /* doppler shift related stuff */
166 TRACE("(%p)\n",dsb);
168 /* initial buffer volume */
169 lVolume = dsb->ds3db_lVolume;
171 switch (dsb->ds3db_ds3db.dwMode)
173 case DS3DMODE_DISABLE:
174 TRACE("3D processing disabled\n");
175 /* this one is here only to eliminate annoying warning message */
176 DSOUND_RecalcVolPan (&dsb->volpan);
177 break;
178 case DS3DMODE_NORMAL:
179 TRACE("Normal 3D processing mode\n");
180 /* we need to calculate distance between buffer and listener*/
181 vDistance = VectorBetweenTwoPoints(&dsb->ds3db_ds3db.vPosition, &dsb->device->ds3dl.vPosition);
182 flDistance = VectorMagnitude (&vDistance);
183 break;
184 case DS3DMODE_HEADRELATIVE:
185 TRACE("Head-relative 3D processing mode\n");
186 /* distance between buffer and listener is same as buffer's position */
187 flDistance = VectorMagnitude (&dsb->ds3db_ds3db.vPosition);
188 break;
191 if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
193 /* some apps don't want you to hear too distant sounds... */
194 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
196 dsb->volpan.lVolume = DSBVOLUME_MIN;
197 DSOUND_RecalcVolPan (&dsb->volpan);
198 /* i guess mixing here would be a waste of power */
199 return;
201 else
202 flDistance = dsb->ds3db_ds3db.flMaxDistance;
205 if (flDistance < dsb->ds3db_ds3db.flMinDistance)
206 flDistance = dsb->ds3db_ds3db.flMinDistance;
208 /* attenuation proportional to the distance squared, converted to millibels as in lVolume*/
209 lVolume -= log10(flDistance/dsb->ds3db_ds3db.flMinDistance * flDistance/dsb->ds3db_ds3db.flMinDistance)*1000;
210 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
212 /* conning */
213 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
214 if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
216 TRACE("conning: cones not set\n");
218 else
220 /* calculate angle */
221 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistance);
222 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
223 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
225 /* my test show that for my way of calc., we need only half of angles */
226 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
227 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
228 if (dwOutsideConeAngle == dwInsideConeAngle)
229 ++dwOutsideConeAngle;
231 /* full volume */
232 if (flAngle < dwInsideConeAngle)
233 flAngle = dwInsideConeAngle;
234 /* min (app defined) volume */
235 if (flAngle > dwOutsideConeAngle)
236 flAngle = dwOutsideConeAngle;
237 /* this probably isn't the right thing, but it's ok for the time being */
238 lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
240 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
241 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
243 dsb->volpan.lVolume = lVolume;
245 /* panning */
246 if (dsb->device->ds3dl.vPosition.x == dsb->ds3db_ds3db.vPosition.x &&
247 dsb->device->ds3dl.vPosition.y == dsb->ds3db_ds3db.vPosition.y &&
248 dsb->device->ds3dl.vPosition.z == dsb->ds3db_ds3db.vPosition.z) {
249 dsb->volpan.lPan = 0;
250 flAngle = 0.0;
252 else
254 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
255 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
256 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
257 /* for now, we'll use "linear formula" (which is probably incorrect); if someone has it in book, correct it */
258 dsb->volpan.lPan = 10000*2*flAngle/M_PI - 10000;
260 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
262 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
263 if(0)
265 D3DVALUE flFreq, flBufferVel, flListenerVel;
266 /* doppler shift*/
267 if (!VectorMagnitude(&dsb->ds3db_ds3db.vVelocity) && !VectorMagnitude(&dsb->device->ds3dl.vVelocity))
269 TRACE("doppler: Buffer and Listener don't have velocities\n");
271 else if (!(dsb->ds3db_ds3db.vVelocity.x == dsb->device->ds3dl.vVelocity.x &&
272 dsb->ds3db_ds3db.vVelocity.y == dsb->device->ds3dl.vVelocity.y &&
273 dsb->ds3db_ds3db.vVelocity.z == dsb->device->ds3dl.vVelocity.z))
275 /* calculate length of ds3db_ds3db.vVelocity component which causes Doppler Effect
276 NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
277 if buffer moves AWAY from listener, it's velocity component is POSITIVE */
278 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
279 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
280 NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
281 if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
282 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
283 /* formula taken from Gianicoli D.: Physics, 4th edition: */
284 /* FIXME: replace dsb->freq with appropriate frequency ! */
285 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
286 TRACE("doppler: Buffer velocity (component) = %f, Listener velocity (component) = %f => Doppler shift: %d Hz -> %f Hz\n",
287 flBufferVel, flListenerVel, dsb->freq, flFreq);
288 /* FIXME: replace following line with correct frequency setting ! */
289 dsb->freq = flFreq;
290 DSOUND_RecalcFormat(dsb);
294 /* time for remix */
295 DSOUND_RecalcVolPan(&dsb->volpan);
298 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
300 TRACE("(%p)\n",dsb);
302 DSOUND_Calc3DBuffer(dsb);
305 static void DSOUND_ChangeListener(IDirectSoundBufferImpl *ds3dl)
307 int i;
308 TRACE("(%p)\n",ds3dl);
309 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
311 /* check if this buffer is waiting for recalculation */
312 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
314 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
319 /*******************************************************************************
320 * IDirectSound3DBuffer
322 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
324 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DBuffer_iface);
327 /* IUnknown methods */
328 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(IDirectSound3DBuffer *iface,
329 REFIID riid, void **ppobj)
331 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
333 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
335 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
338 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(IDirectSound3DBuffer *iface)
340 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
341 ULONG ref = InterlockedIncrement(&This->ref3D);
343 TRACE("(%p) ref was %d\n", This, ref - 1);
345 if(ref == 1)
346 InterlockedIncrement(&This->numIfaces);
348 return ref;
351 static ULONG WINAPI IDirectSound3DBufferImpl_Release(IDirectSound3DBuffer *iface)
353 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
354 ULONG ref = InterlockedDecrement(&This->ref3D);
356 TRACE("(%p) ref was %d\n", This, ref + 1);
358 if (!ref && !InterlockedDecrement(&This->numIfaces))
359 secondarybuffer_destroy(This);
361 return ref;
364 /* IDirectSound3DBuffer methods */
365 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(IDirectSound3DBuffer *iface,
366 DS3DBUFFER *lpDs3dBuffer)
368 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
370 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
372 if (lpDs3dBuffer == NULL) {
373 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
374 return DSERR_INVALIDPARAM;
377 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
378 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
379 return DSERR_INVALIDPARAM;
382 TRACE("returning: all parameters\n");
383 *lpDs3dBuffer = This->ds3db_ds3db;
384 return DS_OK;
387 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(IDirectSound3DBuffer *iface,
388 DWORD *lpdwInsideConeAngle, DWORD *lpdwOutsideConeAngle)
390 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
392 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
393 This->ds3db_ds3db.dwInsideConeAngle, This->ds3db_ds3db.dwOutsideConeAngle);
394 *lpdwInsideConeAngle = This->ds3db_ds3db.dwInsideConeAngle;
395 *lpdwOutsideConeAngle = This->ds3db_ds3db.dwOutsideConeAngle;
396 return DS_OK;
399 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(IDirectSound3DBuffer *iface,
400 D3DVECTOR *lpvConeOrientation)
402 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
404 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
405 This->ds3db_ds3db.vConeOrientation.x,
406 This->ds3db_ds3db.vConeOrientation.y,
407 This->ds3db_ds3db.vConeOrientation.z);
408 *lpvConeOrientation = This->ds3db_ds3db.vConeOrientation;
409 return DS_OK;
412 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(IDirectSound3DBuffer *iface,
413 LONG *lplConeOutsideVolume)
415 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
417 TRACE("returning: Cone Outside Volume = %d\n", This->ds3db_ds3db.lConeOutsideVolume);
418 *lplConeOutsideVolume = This->ds3db_ds3db.lConeOutsideVolume;
419 return DS_OK;
422 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(IDirectSound3DBuffer *iface,
423 D3DVALUE *lpfMaxDistance)
425 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
427 TRACE("returning: Max Distance = %f\n", This->ds3db_ds3db.flMaxDistance);
428 *lpfMaxDistance = This->ds3db_ds3db.flMaxDistance;
429 return DS_OK;
432 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(IDirectSound3DBuffer *iface,
433 D3DVALUE *lpfMinDistance)
435 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
437 TRACE("returning: Min Distance = %f\n", This->ds3db_ds3db.flMinDistance);
438 *lpfMinDistance = This->ds3db_ds3db.flMinDistance;
439 return DS_OK;
442 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(IDirectSound3DBuffer *iface,
443 DWORD *lpdwMode)
445 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
447 TRACE("returning: Mode = %d\n", This->ds3db_ds3db.dwMode);
448 *lpdwMode = This->ds3db_ds3db.dwMode;
449 return DS_OK;
452 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(IDirectSound3DBuffer *iface,
453 D3DVECTOR *lpvPosition)
455 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
457 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3db_ds3db.vPosition.x,
458 This->ds3db_ds3db.vPosition.y, This->ds3db_ds3db.vPosition.z);
459 *lpvPosition = This->ds3db_ds3db.vPosition;
460 return DS_OK;
463 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(IDirectSound3DBuffer *iface,
464 D3DVECTOR *lpvVelocity)
466 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
468 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3db_ds3db.vVelocity.x,
469 This->ds3db_ds3db.vVelocity.y, This->ds3db_ds3db.vVelocity.z);
470 *lpvVelocity = This->ds3db_ds3db.vVelocity;
471 return DS_OK;
474 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(IDirectSound3DBuffer *iface,
475 const DS3DBUFFER *lpcDs3dBuffer, DWORD dwApply)
477 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
478 DWORD status = DSERR_INVALIDPARAM;
480 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
482 if (lpcDs3dBuffer == NULL) {
483 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
484 return status;
487 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
488 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
489 return status;
492 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
493 This->ds3db_ds3db = *lpcDs3dBuffer;
495 if (dwApply == DS3D_IMMEDIATE)
497 DSOUND_Mix3DBuffer(This);
499 This->ds3db_need_recalc = TRUE;
500 status = DS_OK;
502 return status;
505 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(IDirectSound3DBuffer *iface,
506 DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD dwApply)
508 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
510 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
511 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
512 This->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
513 This->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
514 if (dwApply == DS3D_IMMEDIATE)
515 DSOUND_Mix3DBuffer(This);
516 This->ds3db_need_recalc = TRUE;
517 return DS_OK;
520 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(IDirectSound3DBuffer *iface,
521 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
523 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
525 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
526 This->ds3db_ds3db.vConeOrientation.x = x;
527 This->ds3db_ds3db.vConeOrientation.y = y;
528 This->ds3db_ds3db.vConeOrientation.z = z;
529 if (dwApply == DS3D_IMMEDIATE)
531 This->ds3db_need_recalc = FALSE;
532 DSOUND_Mix3DBuffer(This);
534 This->ds3db_need_recalc = TRUE;
535 return DS_OK;
538 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(IDirectSound3DBuffer *iface,
539 LONG lConeOutsideVolume, DWORD dwApply)
541 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
543 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
544 This->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
545 if (dwApply == DS3D_IMMEDIATE)
547 This->ds3db_need_recalc = FALSE;
548 DSOUND_Mix3DBuffer(This);
550 This->ds3db_need_recalc = TRUE;
551 return DS_OK;
554 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(IDirectSound3DBuffer *iface,
555 D3DVALUE fMaxDistance, DWORD dwApply)
557 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
559 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
560 This->ds3db_ds3db.flMaxDistance = fMaxDistance;
561 if (dwApply == DS3D_IMMEDIATE)
563 This->ds3db_need_recalc = FALSE;
564 DSOUND_Mix3DBuffer(This);
566 This->ds3db_need_recalc = TRUE;
567 return DS_OK;
570 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(IDirectSound3DBuffer *iface,
571 D3DVALUE fMinDistance, DWORD dwApply)
573 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
575 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
576 This->ds3db_ds3db.flMinDistance = fMinDistance;
577 if (dwApply == DS3D_IMMEDIATE)
579 This->ds3db_need_recalc = FALSE;
580 DSOUND_Mix3DBuffer(This);
582 This->ds3db_need_recalc = TRUE;
583 return DS_OK;
586 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(IDirectSound3DBuffer *iface, DWORD dwMode,
587 DWORD dwApply)
589 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
591 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
592 This->ds3db_ds3db.dwMode = dwMode;
593 if (dwApply == DS3D_IMMEDIATE)
595 This->ds3db_need_recalc = FALSE;
596 DSOUND_Mix3DBuffer(This);
598 This->ds3db_need_recalc = TRUE;
599 return DS_OK;
602 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x,
603 D3DVALUE y, D3DVALUE z, DWORD dwApply)
605 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
607 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
608 This->ds3db_ds3db.vPosition.x = x;
609 This->ds3db_ds3db.vPosition.y = y;
610 This->ds3db_ds3db.vPosition.z = z;
611 if (dwApply == DS3D_IMMEDIATE)
613 This->ds3db_need_recalc = FALSE;
614 DSOUND_Mix3DBuffer(This);
616 This->ds3db_need_recalc = TRUE;
617 return DS_OK;
620 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(IDirectSound3DBuffer *iface,
621 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
623 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
625 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
626 This->ds3db_ds3db.vVelocity.x = x;
627 This->ds3db_ds3db.vVelocity.y = y;
628 This->ds3db_ds3db.vVelocity.z = z;
629 if (dwApply == DS3D_IMMEDIATE)
631 This->ds3db_need_recalc = FALSE;
632 DSOUND_Mix3DBuffer(This);
634 This->ds3db_need_recalc = TRUE;
635 return DS_OK;
638 const IDirectSound3DBufferVtbl ds3dbvt =
640 /* IUnknown methods */
641 IDirectSound3DBufferImpl_QueryInterface,
642 IDirectSound3DBufferImpl_AddRef,
643 IDirectSound3DBufferImpl_Release,
644 /* IDirectSound3DBuffer methods */
645 IDirectSound3DBufferImpl_GetAllParameters,
646 IDirectSound3DBufferImpl_GetConeAngles,
647 IDirectSound3DBufferImpl_GetConeOrientation,
648 IDirectSound3DBufferImpl_GetConeOutsideVolume,
649 IDirectSound3DBufferImpl_GetMaxDistance,
650 IDirectSound3DBufferImpl_GetMinDistance,
651 IDirectSound3DBufferImpl_GetMode,
652 IDirectSound3DBufferImpl_GetPosition,
653 IDirectSound3DBufferImpl_GetVelocity,
654 IDirectSound3DBufferImpl_SetAllParameters,
655 IDirectSound3DBufferImpl_SetConeAngles,
656 IDirectSound3DBufferImpl_SetConeOrientation,
657 IDirectSound3DBufferImpl_SetConeOutsideVolume,
658 IDirectSound3DBufferImpl_SetMaxDistance,
659 IDirectSound3DBufferImpl_SetMinDistance,
660 IDirectSound3DBufferImpl_SetMode,
661 IDirectSound3DBufferImpl_SetPosition,
662 IDirectSound3DBufferImpl_SetVelocity,
666 /*******************************************************************************
667 * IDirectSound3DListener
669 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
671 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DListener_iface);
675 /* IUnknown methods */
676 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(IDirectSound3DListener *iface,
677 REFIID riid, void **ppobj)
679 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
681 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
683 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
686 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(IDirectSound3DListener *iface)
688 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
689 ULONG ref = InterlockedIncrement(&This->ref3D);
691 TRACE("(%p) ref was %d\n", This, ref - 1);
693 if(ref == 1)
694 InterlockedIncrement(&This->numIfaces);
696 return ref;
699 static ULONG WINAPI IDirectSound3DListenerImpl_Release(IDirectSound3DListener *iface)
701 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
702 ULONG ref;
704 ref = capped_refcount_dec(&This->ref3D);
705 if(!ref)
706 capped_refcount_dec(&This->numIfaces);
708 TRACE("(%p) ref is now %d\n", This, ref);
710 return ref;
713 /* IDirectSound3DListener methods */
714 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
715 DS3DLISTENER *lpDS3DL)
717 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
719 TRACE("(%p,%p)\n",This,lpDS3DL);
721 if (lpDS3DL == NULL) {
722 WARN("invalid parameter: lpDS3DL == NULL\n");
723 return DSERR_INVALIDPARAM;
726 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
727 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
728 return DSERR_INVALIDPARAM;
731 TRACE("returning: all parameters\n");
732 *lpDS3DL = This->device->ds3dl;
733 return DS_OK;
736 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
737 D3DVALUE *lpfDistanceFactor)
739 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
741 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
742 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
743 return DS_OK;
746 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
747 D3DVALUE *lpfDopplerFactor)
749 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
751 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
752 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
753 return DS_OK;
756 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
757 D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
759 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
761 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
762 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
763 This->device->ds3dl.vOrientTop.z);
764 *lpvOrientFront = This->device->ds3dl.vOrientFront;
765 *lpvOrientTop = This->device->ds3dl.vOrientTop;
766 return DS_OK;
769 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
770 D3DVECTOR *lpvPosition)
772 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
774 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
775 *lpvPosition = This->device->ds3dl.vPosition;
776 return DS_OK;
779 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
780 D3DVALUE *lpfRolloffFactor)
782 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
784 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
785 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
786 return DS_OK;
789 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
790 D3DVECTOR *lpvVelocity)
792 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
794 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
795 *lpvVelocity = This->device->ds3dl.vVelocity;
796 return DS_OK;
799 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
800 const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
802 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
804 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
805 This->device->ds3dl = *lpcDS3DL;
806 if (dwApply == DS3D_IMMEDIATE)
808 This->device->ds3dl_need_recalc = FALSE;
809 DSOUND_ChangeListener(This);
811 This->device->ds3dl_need_recalc = TRUE;
812 return DS_OK;
815 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
816 D3DVALUE fDistanceFactor, DWORD dwApply)
818 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
820 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
821 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
822 if (dwApply == DS3D_IMMEDIATE)
824 This->device->ds3dl_need_recalc = FALSE;
825 DSOUND_ChangeListener(This);
827 This->device->ds3dl_need_recalc = TRUE;
828 return DS_OK;
831 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
832 D3DVALUE fDopplerFactor, DWORD dwApply)
834 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
836 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
837 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
838 if (dwApply == DS3D_IMMEDIATE)
840 This->device->ds3dl_need_recalc = FALSE;
841 DSOUND_ChangeListener(This);
843 This->device->ds3dl_need_recalc = TRUE;
844 return DS_OK;
847 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
848 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
849 D3DVALUE zTop, DWORD dwApply)
851 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
853 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
854 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
855 This->device->ds3dl.vOrientFront.x = xFront;
856 This->device->ds3dl.vOrientFront.y = yFront;
857 This->device->ds3dl.vOrientFront.z = zFront;
858 This->device->ds3dl.vOrientTop.x = xTop;
859 This->device->ds3dl.vOrientTop.y = yTop;
860 This->device->ds3dl.vOrientTop.z = zTop;
861 if (dwApply == DS3D_IMMEDIATE)
863 This->device->ds3dl_need_recalc = FALSE;
864 DSOUND_ChangeListener(This);
866 This->device->ds3dl_need_recalc = TRUE;
867 return DS_OK;
870 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
871 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
873 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
875 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
876 This->device->ds3dl.vPosition.x = x;
877 This->device->ds3dl.vPosition.y = y;
878 This->device->ds3dl.vPosition.z = z;
879 if (dwApply == DS3D_IMMEDIATE)
881 This->device->ds3dl_need_recalc = FALSE;
882 DSOUND_ChangeListener(This);
884 This->device->ds3dl_need_recalc = TRUE;
885 return DS_OK;
888 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
889 D3DVALUE fRolloffFactor, DWORD dwApply)
891 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
893 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
894 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
895 if (dwApply == DS3D_IMMEDIATE)
897 This->device->ds3dl_need_recalc = FALSE;
898 DSOUND_ChangeListener(This);
900 This->device->ds3dl_need_recalc = TRUE;
901 return DS_OK;
904 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
905 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
907 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
909 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
910 This->device->ds3dl.vVelocity.x = x;
911 This->device->ds3dl.vVelocity.y = y;
912 This->device->ds3dl.vVelocity.z = z;
913 if (dwApply == DS3D_IMMEDIATE)
915 This->device->ds3dl_need_recalc = FALSE;
916 DSOUND_ChangeListener(This);
918 This->device->ds3dl_need_recalc = TRUE;
919 return DS_OK;
922 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
924 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
926 TRACE("\n");
927 DSOUND_ChangeListener(This);
928 return DS_OK;
931 const IDirectSound3DListenerVtbl ds3dlvt =
933 /* IUnknown methods */
934 IDirectSound3DListenerImpl_QueryInterface,
935 IDirectSound3DListenerImpl_AddRef,
936 IDirectSound3DListenerImpl_Release,
937 /* IDirectSound3DListener methods */
938 IDirectSound3DListenerImpl_GetAllParameter,
939 IDirectSound3DListenerImpl_GetDistanceFactor,
940 IDirectSound3DListenerImpl_GetDopplerFactor,
941 IDirectSound3DListenerImpl_GetOrientation,
942 IDirectSound3DListenerImpl_GetPosition,
943 IDirectSound3DListenerImpl_GetRolloffFactor,
944 IDirectSound3DListenerImpl_GetVelocity,
945 IDirectSound3DListenerImpl_SetAllParameters,
946 IDirectSound3DListenerImpl_SetDistanceFactor,
947 IDirectSound3DListenerImpl_SetDopplerFactor,
948 IDirectSound3DListenerImpl_SetOrientation,
949 IDirectSound3DListenerImpl_SetPosition,
950 IDirectSound3DListenerImpl_SetRolloffFactor,
951 IDirectSound3DListenerImpl_SetVelocity,
952 IDirectSound3DListenerImpl_CommitDeferredSettings,