gdiplus: Add support for JPEG encoding.
[wine/multimedia.git] / dlls / dsound / sound3d.c
blobc57aa2ec00a1e911714040a6c5850b625e286e40
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 = InterlockedDecrement(&This->ref3D);
704 TRACE("(%p) ref was %d\n", This, ref + 1);
706 if (!ref && !InterlockedDecrement(&This->numIfaces))
707 primarybuffer_destroy(This);
709 return ref;
712 /* IDirectSound3DListener methods */
713 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
714 DS3DLISTENER *lpDS3DL)
716 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
718 TRACE("(%p,%p)\n",This,lpDS3DL);
720 if (lpDS3DL == NULL) {
721 WARN("invalid parameter: lpDS3DL == NULL\n");
722 return DSERR_INVALIDPARAM;
725 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
726 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
727 return DSERR_INVALIDPARAM;
730 TRACE("returning: all parameters\n");
731 *lpDS3DL = This->device->ds3dl;
732 return DS_OK;
735 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
736 D3DVALUE *lpfDistanceFactor)
738 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
740 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
741 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
742 return DS_OK;
745 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
746 D3DVALUE *lpfDopplerFactor)
748 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
750 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
751 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
752 return DS_OK;
755 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
756 D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
758 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
760 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
761 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
762 This->device->ds3dl.vOrientTop.z);
763 *lpvOrientFront = This->device->ds3dl.vOrientFront;
764 *lpvOrientTop = This->device->ds3dl.vOrientTop;
765 return DS_OK;
768 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
769 D3DVECTOR *lpvPosition)
771 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
773 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
774 *lpvPosition = This->device->ds3dl.vPosition;
775 return DS_OK;
778 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
779 D3DVALUE *lpfRolloffFactor)
781 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
783 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
784 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
785 return DS_OK;
788 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
789 D3DVECTOR *lpvVelocity)
791 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
793 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
794 *lpvVelocity = This->device->ds3dl.vVelocity;
795 return DS_OK;
798 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
799 const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
801 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
803 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
804 This->device->ds3dl = *lpcDS3DL;
805 if (dwApply == DS3D_IMMEDIATE)
807 This->device->ds3dl_need_recalc = FALSE;
808 DSOUND_ChangeListener(This);
810 This->device->ds3dl_need_recalc = TRUE;
811 return DS_OK;
814 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
815 D3DVALUE fDistanceFactor, DWORD dwApply)
817 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
819 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
820 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
821 if (dwApply == DS3D_IMMEDIATE)
823 This->device->ds3dl_need_recalc = FALSE;
824 DSOUND_ChangeListener(This);
826 This->device->ds3dl_need_recalc = TRUE;
827 return DS_OK;
830 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
831 D3DVALUE fDopplerFactor, DWORD dwApply)
833 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
835 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
836 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
837 if (dwApply == DS3D_IMMEDIATE)
839 This->device->ds3dl_need_recalc = FALSE;
840 DSOUND_ChangeListener(This);
842 This->device->ds3dl_need_recalc = TRUE;
843 return DS_OK;
846 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
847 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
848 D3DVALUE zTop, DWORD dwApply)
850 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
852 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
853 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
854 This->device->ds3dl.vOrientFront.x = xFront;
855 This->device->ds3dl.vOrientFront.y = yFront;
856 This->device->ds3dl.vOrientFront.z = zFront;
857 This->device->ds3dl.vOrientTop.x = xTop;
858 This->device->ds3dl.vOrientTop.y = yTop;
859 This->device->ds3dl.vOrientTop.z = zTop;
860 if (dwApply == DS3D_IMMEDIATE)
862 This->device->ds3dl_need_recalc = FALSE;
863 DSOUND_ChangeListener(This);
865 This->device->ds3dl_need_recalc = TRUE;
866 return DS_OK;
869 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
870 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
872 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
874 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
875 This->device->ds3dl.vPosition.x = x;
876 This->device->ds3dl.vPosition.y = y;
877 This->device->ds3dl.vPosition.z = z;
878 if (dwApply == DS3D_IMMEDIATE)
880 This->device->ds3dl_need_recalc = FALSE;
881 DSOUND_ChangeListener(This);
883 This->device->ds3dl_need_recalc = TRUE;
884 return DS_OK;
887 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
888 D3DVALUE fRolloffFactor, DWORD dwApply)
890 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
892 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
893 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
894 if (dwApply == DS3D_IMMEDIATE)
896 This->device->ds3dl_need_recalc = FALSE;
897 DSOUND_ChangeListener(This);
899 This->device->ds3dl_need_recalc = TRUE;
900 return DS_OK;
903 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
904 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
906 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
908 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
909 This->device->ds3dl.vVelocity.x = x;
910 This->device->ds3dl.vVelocity.y = y;
911 This->device->ds3dl.vVelocity.z = z;
912 if (dwApply == DS3D_IMMEDIATE)
914 This->device->ds3dl_need_recalc = FALSE;
915 DSOUND_ChangeListener(This);
917 This->device->ds3dl_need_recalc = TRUE;
918 return DS_OK;
921 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
923 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
925 TRACE("\n");
926 DSOUND_ChangeListener(This);
927 return DS_OK;
930 const IDirectSound3DListenerVtbl ds3dlvt =
932 /* IUnknown methods */
933 IDirectSound3DListenerImpl_QueryInterface,
934 IDirectSound3DListenerImpl_AddRef,
935 IDirectSound3DListenerImpl_Release,
936 /* IDirectSound3DListener methods */
937 IDirectSound3DListenerImpl_GetAllParameter,
938 IDirectSound3DListenerImpl_GetDistanceFactor,
939 IDirectSound3DListenerImpl_GetDopplerFactor,
940 IDirectSound3DListenerImpl_GetOrientation,
941 IDirectSound3DListenerImpl_GetPosition,
942 IDirectSound3DListenerImpl_GetRolloffFactor,
943 IDirectSound3DListenerImpl_GetVelocity,
944 IDirectSound3DListenerImpl_SetAllParameters,
945 IDirectSound3DListenerImpl_SetDistanceFactor,
946 IDirectSound3DListenerImpl_SetDopplerFactor,
947 IDirectSound3DListenerImpl_SetOrientation,
948 IDirectSound3DListenerImpl_SetPosition,
949 IDirectSound3DListenerImpl_SetRolloffFactor,
950 IDirectSound3DListenerImpl_SetVelocity,
951 IDirectSound3DListenerImpl_CommitDeferredSettings,