[DSOUND]
[reactos.git] / reactos / dll / directx / wine / dsound / sound3d.c
1 /* DirectSound
2 *
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>
7 *
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.
12 *
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.
17 *
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
21 */
22 /*
23 * Most thread locking is complete. There may be a few race
24 * conditions still lurking.
25 *
26 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
27 * and a Turtle Beach Tropez+.
28 *
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
38 */
39
40 #include "dsound_private.h"
41
42 /* default velocity of sound in the air */
43 #define DEFAULT_VELOCITY 340
44
45 /*******************************************************************************
46 * Auxiliary functions
47 */
48
49 /* scalar product (I believe it's called dot product in English) */
50 static inline D3DVALUE ScalarProduct (const D3DVECTOR *a, const D3DVECTOR *b)
51 {
52 D3DVALUE c;
53 c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
54 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
55 b->z, c);
56 return c;
57 }
58
59 /* vector product (I believe it's called cross product in English */
60 static inline D3DVECTOR VectorProduct (const D3DVECTOR *a, const D3DVECTOR *b)
61 {
62 D3DVECTOR c;
63 c.x = (a->y*b->z) - (a->z*b->y);
64 c.y = (a->z*b->x) - (a->x*b->z);
65 c.z = (a->x*b->y) - (a->y*b->x);
66 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
67 b->z, c.x, c.y, c.z);
68 return c;
69 }
70
71 /* magnitude (length) of vector */
72 static inline D3DVALUE VectorMagnitude (const D3DVECTOR *a)
73 {
74 D3DVALUE l;
75 l = sqrt (ScalarProduct (a, a));
76 TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
77 return l;
78 }
79
80 /* conversion between radians and degrees */
81 static inline D3DVALUE RadToDeg (D3DVALUE angle)
82 {
83 D3DVALUE newangle;
84 newangle = angle * (360/(2*M_PI));
85 TRACE("%f rad = %f deg\n", angle, newangle);
86 return newangle;
87 }
88
89 /* angle between vectors - rad version */
90 static inline D3DVALUE AngleBetweenVectorsRad (const D3DVECTOR *a, const D3DVECTOR *b)
91 {
92 D3DVALUE la, lb, product, angle, cos;
93 /* definition of scalar product: a*b = |a|*|b|*cos... therefore: */
94 product = ScalarProduct (a,b);
95 la = VectorMagnitude (a);
96 lb = VectorMagnitude (b);
97 if (!la || !lb)
98 return 0;
99
100 cos = product/(la*lb);
101 angle = acos(cos);
102 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians (%f degrees)\n", a->x, a->y, a->z, b->x,
103 b->y, b->z, angle, RadToDeg(angle));
104 return angle;
105 }
106
107 static inline D3DVALUE AngleBetweenVectorsDeg (const D3DVECTOR *a, const D3DVECTOR *b)
108 {
109 return RadToDeg(AngleBetweenVectorsRad(a, b));
110 }
111
112 /* calculates vector between two points */
113 static inline D3DVECTOR VectorBetweenTwoPoints (const D3DVECTOR *a, const D3DVECTOR *b)
114 {
115 D3DVECTOR c;
116 c.x = b->x - a->x;
117 c.y = b->y - a->y;
118 c.z = b->z - a->z;
119 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
120 b->z, c.x, c.y, c.z);
121 return c;
122 }
123
124 /* calculates the length of vector's projection on another vector */
125 static inline D3DVALUE ProjectVector (const D3DVECTOR *a, const D3DVECTOR *p)
126 {
127 D3DVALUE prod, result;
128 prod = ScalarProduct(a, p);
129 result = prod/VectorMagnitude(p);
130 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
131 p->y, p->z, result);
132 return result;
133 }
134
135 /*******************************************************************************
136 * 3D Buffer and Listener mixing
137 */
138
139 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
140 {
141 /* volume, at which the sound will be played after all calcs. */
142 D3DVALUE lVolume = 0;
143 /* stuff for distance related stuff calc. */
144 D3DVECTOR vDistance;
145 D3DVALUE flDistance = 0;
146 /* panning related stuff */
147 D3DVALUE flAngle;
148 D3DVECTOR vLeft;
149 /* doppler shift related stuff */
150
151 TRACE("(%p)\n",dsb);
152
153 /* initial buffer volume */
154 lVolume = dsb->ds3db_lVolume;
155
156 switch (dsb->ds3db_ds3db.dwMode)
157 {
158 case DS3DMODE_DISABLE:
159 TRACE("3D processing disabled\n");
160 /* this one is here only to eliminate annoying warning message */
161 DSOUND_RecalcVolPan (&dsb->volpan);
162 break;
163 case DS3DMODE_NORMAL:
164 TRACE("Normal 3D processing mode\n");
165 /* we need to calculate distance between buffer and listener*/
166 vDistance = VectorBetweenTwoPoints(&dsb->ds3db_ds3db.vPosition, &dsb->device->ds3dl.vPosition);
167 flDistance = VectorMagnitude (&vDistance);
168 break;
169 case DS3DMODE_HEADRELATIVE:
170 TRACE("Head-relative 3D processing mode\n");
171 /* distance between buffer and listener is same as buffer's position */
172 flDistance = VectorMagnitude (&dsb->ds3db_ds3db.vPosition);
173 break;
174 }
175
176 if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
177 {
178 /* some apps don't want you to hear too distant sounds... */
179 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
180 {
181 dsb->volpan.lVolume = DSBVOLUME_MIN;
182 DSOUND_RecalcVolPan (&dsb->volpan);
183 /* i guess mixing here would be a waste of power */
184 return;
185 }
186 else
187 flDistance = dsb->ds3db_ds3db.flMaxDistance;
188 }
189
190 if (flDistance < dsb->ds3db_ds3db.flMinDistance)
191 flDistance = dsb->ds3db_ds3db.flMinDistance;
192
193 /* attenuation proportional to the distance squared, converted to millibels as in lVolume*/
194 lVolume -= log10(flDistance/dsb->ds3db_ds3db.flMinDistance * flDistance/dsb->ds3db_ds3db.flMinDistance)*1000;
195 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
196
197 /* conning */
198 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
199 if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
200 {
201 TRACE("conning: cones not set\n");
202 }
203 else
204 {
205 /* calculate angle */
206 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistance);
207 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
208 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
209 {
210 /* my test show that for my way of calc., we need only half of angles */
211 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
212 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
213 if (dwOutsideConeAngle == dwInsideConeAngle)
214 ++dwOutsideConeAngle;
215
216 /* full volume */
217 if (flAngle < dwInsideConeAngle)
218 flAngle = dwInsideConeAngle;
219 /* min (app defined) volume */
220 if (flAngle > dwOutsideConeAngle)
221 flAngle = dwOutsideConeAngle;
222 /* this probably isn't the right thing, but it's ok for the time being */
223 lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
224 }
225 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
226 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
227 }
228 dsb->volpan.lVolume = lVolume;
229
230 /* panning */
231 if (dsb->device->ds3dl.vPosition.x == dsb->ds3db_ds3db.vPosition.x &&
232 dsb->device->ds3dl.vPosition.y == dsb->ds3db_ds3db.vPosition.y &&
233 dsb->device->ds3dl.vPosition.z == dsb->ds3db_ds3db.vPosition.z) {
234 dsb->volpan.lPan = 0;
235 flAngle = 0.0;
236 }
237 else
238 {
239 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
240 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
241 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
242 /* for now, we'll use "linear formula" (which is probably incorrect); if someone has it in book, correct it */
243 dsb->volpan.lPan = 10000*2*flAngle/M_PI - 10000;
244 }
245 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
246
247 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
248 if(0)
249 {
250 D3DVALUE flFreq, flBufferVel, flListenerVel;
251 /* doppler shift*/
252 if (!VectorMagnitude(&dsb->ds3db_ds3db.vVelocity) && !VectorMagnitude(&dsb->device->ds3dl.vVelocity))
253 {
254 TRACE("doppler: Buffer and Listener don't have velocities\n");
255 }
256 else if (!(dsb->ds3db_ds3db.vVelocity.x == dsb->device->ds3dl.vVelocity.x &&
257 dsb->ds3db_ds3db.vVelocity.y == dsb->device->ds3dl.vVelocity.y &&
258 dsb->ds3db_ds3db.vVelocity.z == dsb->device->ds3dl.vVelocity.z))
259 {
260 /* calculate length of ds3db_ds3db.vVelocity component which causes Doppler Effect
261 NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
262 if buffer moves AWAY from listener, it's velocity component is POSITIVE */
263 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
264 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
265 NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
266 if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
267 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
268 /* formula taken from Gianicoli D.: Physics, 4th edition: */
269 /* FIXME: replace dsb->freq with appropriate frequency ! */
270 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
271 TRACE("doppler: Buffer velocity (component) = %f, Listener velocity (component) = %f => Doppler shift: %d Hz -> %f Hz\n",
272 flBufferVel, flListenerVel, dsb->freq, flFreq);
273 /* FIXME: replace following line with correct frequency setting ! */
274 dsb->freq = flFreq;
275 DSOUND_RecalcFormat(dsb);
276 }
277 }
278
279 /* time for remix */
280 DSOUND_RecalcVolPan(&dsb->volpan);
281 }
282
283 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
284 {
285 TRACE("(%p)\n",dsb);
286
287 DSOUND_Calc3DBuffer(dsb);
288 }
289
290 static void DSOUND_ChangeListener(IDirectSoundBufferImpl *ds3dl)
291 {
292 int i;
293 TRACE("(%p)\n",ds3dl);
294 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
295 {
296 /* check if this buffer is waiting for recalculation */
297 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
298 {
299 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
300 }
301 }
302 }
303
304 /*******************************************************************************
305 * IDirectSound3DBuffer
306 */
307 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
308 {
309 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DBuffer_iface);
310 }
311
312 /* IUnknown methods */
313 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(IDirectSound3DBuffer *iface,
314 REFIID riid, void **ppobj)
315 {
316 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
317
318 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
319
320 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
321 }
322
323 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(IDirectSound3DBuffer *iface)
324 {
325 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
326 ULONG ref = InterlockedIncrement(&This->ref3D);
327
328 TRACE("(%p) ref was %d\n", This, ref - 1);
329
330 if(ref == 1)
331 InterlockedIncrement(&This->numIfaces);
332
333 return ref;
334 }
335
336 static ULONG WINAPI IDirectSound3DBufferImpl_Release(IDirectSound3DBuffer *iface)
337 {
338 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
339 ULONG ref = InterlockedDecrement(&This->ref3D);
340
341 TRACE("(%p) ref was %d\n", This, ref + 1);
342
343 if (!ref && !InterlockedDecrement(&This->numIfaces))
344 secondarybuffer_destroy(This);
345
346 return ref;
347 }
348
349 /* IDirectSound3DBuffer methods */
350 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(IDirectSound3DBuffer *iface,
351 DS3DBUFFER *lpDs3dBuffer)
352 {
353 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
354
355 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
356
357 if (lpDs3dBuffer == NULL) {
358 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
359 return DSERR_INVALIDPARAM;
360 }
361
362 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
363 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
364 return DSERR_INVALIDPARAM;
365 }
366
367 TRACE("returning: all parameters\n");
368 *lpDs3dBuffer = This->ds3db_ds3db;
369 return DS_OK;
370 }
371
372 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(IDirectSound3DBuffer *iface,
373 DWORD *lpdwInsideConeAngle, DWORD *lpdwOutsideConeAngle)
374 {
375 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
376
377 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
378 This->ds3db_ds3db.dwInsideConeAngle, This->ds3db_ds3db.dwOutsideConeAngle);
379 *lpdwInsideConeAngle = This->ds3db_ds3db.dwInsideConeAngle;
380 *lpdwOutsideConeAngle = This->ds3db_ds3db.dwOutsideConeAngle;
381 return DS_OK;
382 }
383
384 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(IDirectSound3DBuffer *iface,
385 D3DVECTOR *lpvConeOrientation)
386 {
387 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
388
389 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
390 This->ds3db_ds3db.vConeOrientation.x,
391 This->ds3db_ds3db.vConeOrientation.y,
392 This->ds3db_ds3db.vConeOrientation.z);
393 *lpvConeOrientation = This->ds3db_ds3db.vConeOrientation;
394 return DS_OK;
395 }
396
397 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(IDirectSound3DBuffer *iface,
398 LONG *lplConeOutsideVolume)
399 {
400 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
401
402 TRACE("returning: Cone Outside Volume = %d\n", This->ds3db_ds3db.lConeOutsideVolume);
403 *lplConeOutsideVolume = This->ds3db_ds3db.lConeOutsideVolume;
404 return DS_OK;
405 }
406
407 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(IDirectSound3DBuffer *iface,
408 D3DVALUE *lpfMaxDistance)
409 {
410 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
411
412 TRACE("returning: Max Distance = %f\n", This->ds3db_ds3db.flMaxDistance);
413 *lpfMaxDistance = This->ds3db_ds3db.flMaxDistance;
414 return DS_OK;
415 }
416
417 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(IDirectSound3DBuffer *iface,
418 D3DVALUE *lpfMinDistance)
419 {
420 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
421
422 TRACE("returning: Min Distance = %f\n", This->ds3db_ds3db.flMinDistance);
423 *lpfMinDistance = This->ds3db_ds3db.flMinDistance;
424 return DS_OK;
425 }
426
427 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(IDirectSound3DBuffer *iface,
428 DWORD *lpdwMode)
429 {
430 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
431
432 TRACE("returning: Mode = %d\n", This->ds3db_ds3db.dwMode);
433 *lpdwMode = This->ds3db_ds3db.dwMode;
434 return DS_OK;
435 }
436
437 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(IDirectSound3DBuffer *iface,
438 D3DVECTOR *lpvPosition)
439 {
440 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
441
442 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3db_ds3db.vPosition.x,
443 This->ds3db_ds3db.vPosition.y, This->ds3db_ds3db.vPosition.z);
444 *lpvPosition = This->ds3db_ds3db.vPosition;
445 return DS_OK;
446 }
447
448 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(IDirectSound3DBuffer *iface,
449 D3DVECTOR *lpvVelocity)
450 {
451 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
452
453 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3db_ds3db.vVelocity.x,
454 This->ds3db_ds3db.vVelocity.y, This->ds3db_ds3db.vVelocity.z);
455 *lpvVelocity = This->ds3db_ds3db.vVelocity;
456 return DS_OK;
457 }
458
459 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(IDirectSound3DBuffer *iface,
460 const DS3DBUFFER *lpcDs3dBuffer, DWORD dwApply)
461 {
462 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
463 DWORD status = DSERR_INVALIDPARAM;
464
465 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
466
467 if (lpcDs3dBuffer == NULL) {
468 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
469 return status;
470 }
471
472 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
473 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
474 return status;
475 }
476
477 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
478 This->ds3db_ds3db = *lpcDs3dBuffer;
479
480 if (dwApply == DS3D_IMMEDIATE)
481 {
482 DSOUND_Mix3DBuffer(This);
483 }
484 This->ds3db_need_recalc = TRUE;
485 status = DS_OK;
486
487 return status;
488 }
489
490 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(IDirectSound3DBuffer *iface,
491 DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD dwApply)
492 {
493 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
494
495 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
496 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
497 This->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
498 This->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
499 if (dwApply == DS3D_IMMEDIATE)
500 DSOUND_Mix3DBuffer(This);
501 This->ds3db_need_recalc = TRUE;
502 return DS_OK;
503 }
504
505 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(IDirectSound3DBuffer *iface,
506 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
507 {
508 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
509
510 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
511 This->ds3db_ds3db.vConeOrientation.x = x;
512 This->ds3db_ds3db.vConeOrientation.y = y;
513 This->ds3db_ds3db.vConeOrientation.z = z;
514 if (dwApply == DS3D_IMMEDIATE)
515 {
516 This->ds3db_need_recalc = FALSE;
517 DSOUND_Mix3DBuffer(This);
518 }
519 This->ds3db_need_recalc = TRUE;
520 return DS_OK;
521 }
522
523 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(IDirectSound3DBuffer *iface,
524 LONG lConeOutsideVolume, DWORD dwApply)
525 {
526 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
527
528 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
529 This->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
530 if (dwApply == DS3D_IMMEDIATE)
531 {
532 This->ds3db_need_recalc = FALSE;
533 DSOUND_Mix3DBuffer(This);
534 }
535 This->ds3db_need_recalc = TRUE;
536 return DS_OK;
537 }
538
539 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(IDirectSound3DBuffer *iface,
540 D3DVALUE fMaxDistance, DWORD dwApply)
541 {
542 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
543
544 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
545 This->ds3db_ds3db.flMaxDistance = fMaxDistance;
546 if (dwApply == DS3D_IMMEDIATE)
547 {
548 This->ds3db_need_recalc = FALSE;
549 DSOUND_Mix3DBuffer(This);
550 }
551 This->ds3db_need_recalc = TRUE;
552 return DS_OK;
553 }
554
555 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(IDirectSound3DBuffer *iface,
556 D3DVALUE fMinDistance, DWORD dwApply)
557 {
558 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
559
560 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
561 This->ds3db_ds3db.flMinDistance = fMinDistance;
562 if (dwApply == DS3D_IMMEDIATE)
563 {
564 This->ds3db_need_recalc = FALSE;
565 DSOUND_Mix3DBuffer(This);
566 }
567 This->ds3db_need_recalc = TRUE;
568 return DS_OK;
569 }
570
571 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(IDirectSound3DBuffer *iface, DWORD dwMode,
572 DWORD dwApply)
573 {
574 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
575
576 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
577 This->ds3db_ds3db.dwMode = dwMode;
578 if (dwApply == DS3D_IMMEDIATE)
579 {
580 This->ds3db_need_recalc = FALSE;
581 DSOUND_Mix3DBuffer(This);
582 }
583 This->ds3db_need_recalc = TRUE;
584 return DS_OK;
585 }
586
587 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x,
588 D3DVALUE y, D3DVALUE z, DWORD dwApply)
589 {
590 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
591
592 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
593 This->ds3db_ds3db.vPosition.x = x;
594 This->ds3db_ds3db.vPosition.y = y;
595 This->ds3db_ds3db.vPosition.z = z;
596 if (dwApply == DS3D_IMMEDIATE)
597 {
598 This->ds3db_need_recalc = FALSE;
599 DSOUND_Mix3DBuffer(This);
600 }
601 This->ds3db_need_recalc = TRUE;
602 return DS_OK;
603 }
604
605 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(IDirectSound3DBuffer *iface,
606 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
607 {
608 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
609
610 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
611 This->ds3db_ds3db.vVelocity.x = x;
612 This->ds3db_ds3db.vVelocity.y = y;
613 This->ds3db_ds3db.vVelocity.z = z;
614 if (dwApply == DS3D_IMMEDIATE)
615 {
616 This->ds3db_need_recalc = FALSE;
617 DSOUND_Mix3DBuffer(This);
618 }
619 This->ds3db_need_recalc = TRUE;
620 return DS_OK;
621 }
622
623 const IDirectSound3DBufferVtbl ds3dbvt =
624 {
625 /* IUnknown methods */
626 IDirectSound3DBufferImpl_QueryInterface,
627 IDirectSound3DBufferImpl_AddRef,
628 IDirectSound3DBufferImpl_Release,
629 /* IDirectSound3DBuffer methods */
630 IDirectSound3DBufferImpl_GetAllParameters,
631 IDirectSound3DBufferImpl_GetConeAngles,
632 IDirectSound3DBufferImpl_GetConeOrientation,
633 IDirectSound3DBufferImpl_GetConeOutsideVolume,
634 IDirectSound3DBufferImpl_GetMaxDistance,
635 IDirectSound3DBufferImpl_GetMinDistance,
636 IDirectSound3DBufferImpl_GetMode,
637 IDirectSound3DBufferImpl_GetPosition,
638 IDirectSound3DBufferImpl_GetVelocity,
639 IDirectSound3DBufferImpl_SetAllParameters,
640 IDirectSound3DBufferImpl_SetConeAngles,
641 IDirectSound3DBufferImpl_SetConeOrientation,
642 IDirectSound3DBufferImpl_SetConeOutsideVolume,
643 IDirectSound3DBufferImpl_SetMaxDistance,
644 IDirectSound3DBufferImpl_SetMinDistance,
645 IDirectSound3DBufferImpl_SetMode,
646 IDirectSound3DBufferImpl_SetPosition,
647 IDirectSound3DBufferImpl_SetVelocity,
648 };
649
650
651 /*******************************************************************************
652 * IDirectSound3DListener
653 */
654 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
655 {
656 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DListener_iface);
657 }
658
659
660 /* IUnknown methods */
661 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(IDirectSound3DListener *iface,
662 REFIID riid, void **ppobj)
663 {
664 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
665
666 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
667
668 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
669 }
670
671 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(IDirectSound3DListener *iface)
672 {
673 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
674 ULONG ref = InterlockedIncrement(&This->ref3D);
675
676 TRACE("(%p) ref was %d\n", This, ref - 1);
677
678 if(ref == 1)
679 InterlockedIncrement(&This->numIfaces);
680
681 return ref;
682 }
683
684 static ULONG WINAPI IDirectSound3DListenerImpl_Release(IDirectSound3DListener *iface)
685 {
686 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
687 ULONG ref;
688
689 ref = capped_refcount_dec(&This->ref3D);
690 if(!ref)
691 capped_refcount_dec(&This->numIfaces);
692
693 TRACE("(%p) ref is now %d\n", This, ref);
694
695 return ref;
696 }
697
698 /* IDirectSound3DListener methods */
699 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
700 DS3DLISTENER *lpDS3DL)
701 {
702 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
703
704 TRACE("(%p,%p)\n",This,lpDS3DL);
705
706 if (lpDS3DL == NULL) {
707 WARN("invalid parameter: lpDS3DL == NULL\n");
708 return DSERR_INVALIDPARAM;
709 }
710
711 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
712 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
713 return DSERR_INVALIDPARAM;
714 }
715
716 TRACE("returning: all parameters\n");
717 *lpDS3DL = This->device->ds3dl;
718 return DS_OK;
719 }
720
721 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
722 D3DVALUE *lpfDistanceFactor)
723 {
724 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
725
726 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
727 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
728 return DS_OK;
729 }
730
731 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
732 D3DVALUE *lpfDopplerFactor)
733 {
734 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
735
736 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
737 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
738 return DS_OK;
739 }
740
741 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
742 D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
743 {
744 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
745
746 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
747 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
748 This->device->ds3dl.vOrientTop.z);
749 *lpvOrientFront = This->device->ds3dl.vOrientFront;
750 *lpvOrientTop = This->device->ds3dl.vOrientTop;
751 return DS_OK;
752 }
753
754 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
755 D3DVECTOR *lpvPosition)
756 {
757 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
758
759 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
760 *lpvPosition = This->device->ds3dl.vPosition;
761 return DS_OK;
762 }
763
764 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
765 D3DVALUE *lpfRolloffFactor)
766 {
767 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
768
769 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
770 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
771 return DS_OK;
772 }
773
774 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
775 D3DVECTOR *lpvVelocity)
776 {
777 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
778
779 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
780 *lpvVelocity = This->device->ds3dl.vVelocity;
781 return DS_OK;
782 }
783
784 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
785 const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
786 {
787 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
788
789 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
790 This->device->ds3dl = *lpcDS3DL;
791 if (dwApply == DS3D_IMMEDIATE)
792 {
793 This->device->ds3dl_need_recalc = FALSE;
794 DSOUND_ChangeListener(This);
795 }
796 This->device->ds3dl_need_recalc = TRUE;
797 return DS_OK;
798 }
799
800 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
801 D3DVALUE fDistanceFactor, DWORD dwApply)
802 {
803 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
804
805 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
806 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
807 if (dwApply == DS3D_IMMEDIATE)
808 {
809 This->device->ds3dl_need_recalc = FALSE;
810 DSOUND_ChangeListener(This);
811 }
812 This->device->ds3dl_need_recalc = TRUE;
813 return DS_OK;
814 }
815
816 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
817 D3DVALUE fDopplerFactor, DWORD dwApply)
818 {
819 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
820
821 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
822 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
823 if (dwApply == DS3D_IMMEDIATE)
824 {
825 This->device->ds3dl_need_recalc = FALSE;
826 DSOUND_ChangeListener(This);
827 }
828 This->device->ds3dl_need_recalc = TRUE;
829 return DS_OK;
830 }
831
832 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
833 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
834 D3DVALUE zTop, DWORD dwApply)
835 {
836 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
837
838 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
839 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
840 This->device->ds3dl.vOrientFront.x = xFront;
841 This->device->ds3dl.vOrientFront.y = yFront;
842 This->device->ds3dl.vOrientFront.z = zFront;
843 This->device->ds3dl.vOrientTop.x = xTop;
844 This->device->ds3dl.vOrientTop.y = yTop;
845 This->device->ds3dl.vOrientTop.z = zTop;
846 if (dwApply == DS3D_IMMEDIATE)
847 {
848 This->device->ds3dl_need_recalc = FALSE;
849 DSOUND_ChangeListener(This);
850 }
851 This->device->ds3dl_need_recalc = TRUE;
852 return DS_OK;
853 }
854
855 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
856 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
857 {
858 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
859
860 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
861 This->device->ds3dl.vPosition.x = x;
862 This->device->ds3dl.vPosition.y = y;
863 This->device->ds3dl.vPosition.z = z;
864 if (dwApply == DS3D_IMMEDIATE)
865 {
866 This->device->ds3dl_need_recalc = FALSE;
867 DSOUND_ChangeListener(This);
868 }
869 This->device->ds3dl_need_recalc = TRUE;
870 return DS_OK;
871 }
872
873 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
874 D3DVALUE fRolloffFactor, DWORD dwApply)
875 {
876 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
877
878 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
879 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
880 if (dwApply == DS3D_IMMEDIATE)
881 {
882 This->device->ds3dl_need_recalc = FALSE;
883 DSOUND_ChangeListener(This);
884 }
885 This->device->ds3dl_need_recalc = TRUE;
886 return DS_OK;
887 }
888
889 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
890 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
891 {
892 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
893
894 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
895 This->device->ds3dl.vVelocity.x = x;
896 This->device->ds3dl.vVelocity.y = y;
897 This->device->ds3dl.vVelocity.z = z;
898 if (dwApply == DS3D_IMMEDIATE)
899 {
900 This->device->ds3dl_need_recalc = FALSE;
901 DSOUND_ChangeListener(This);
902 }
903 This->device->ds3dl_need_recalc = TRUE;
904 return DS_OK;
905 }
906
907 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
908 {
909 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
910
911 TRACE("\n");
912 DSOUND_ChangeListener(This);
913 return DS_OK;
914 }
915
916 const IDirectSound3DListenerVtbl ds3dlvt =
917 {
918 /* IUnknown methods */
919 IDirectSound3DListenerImpl_QueryInterface,
920 IDirectSound3DListenerImpl_AddRef,
921 IDirectSound3DListenerImpl_Release,
922 /* IDirectSound3DListener methods */
923 IDirectSound3DListenerImpl_GetAllParameter,
924 IDirectSound3DListenerImpl_GetDistanceFactor,
925 IDirectSound3DListenerImpl_GetDopplerFactor,
926 IDirectSound3DListenerImpl_GetOrientation,
927 IDirectSound3DListenerImpl_GetPosition,
928 IDirectSound3DListenerImpl_GetRolloffFactor,
929 IDirectSound3DListenerImpl_GetVelocity,
930 IDirectSound3DListenerImpl_SetAllParameters,
931 IDirectSound3DListenerImpl_SetDistanceFactor,
932 IDirectSound3DListenerImpl_SetDopplerFactor,
933 IDirectSound3DListenerImpl_SetOrientation,
934 IDirectSound3DListenerImpl_SetPosition,
935 IDirectSound3DListenerImpl_SetRolloffFactor,
936 IDirectSound3DListenerImpl_SetVelocity,
937 IDirectSound3DListenerImpl_CommitDeferredSettings,
938 };