dsound: Merge IDirectSound3DBuffer into the secondary buffer object.
[wine] / dlls / 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 <stdarg.h>
41 #include <math.h>       /* Insomnia - pow() function */
42
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"
54
55 /* default velocity of sound in the air */
56 #define DEFAULT_VELOCITY 340
57
58 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
59
60 /*******************************************************************************
61  *              Auxiliary functions
62  */
63
64 /* scalar product (I believe it's called dot product in English) */
65 static inline D3DVALUE ScalarProduct (const D3DVECTOR *a, const D3DVECTOR *b)
66 {
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;
72 }
73
74 /* vector product (I believe it's called cross product in English */
75 static inline D3DVECTOR VectorProduct (const D3DVECTOR *a, const D3DVECTOR *b)
76 {
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;
84 }
85
86 /* magnitude (length) of vector */
87 static inline D3DVALUE VectorMagnitude (const D3DVECTOR *a)
88 {
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;
93 }
94
95 /* conversion between radians and degrees */
96 static inline D3DVALUE RadToDeg (D3DVALUE angle)
97 {
98         D3DVALUE newangle;
99         newangle = angle * (360/(2*M_PI));
100         TRACE("%f rad = %f deg\n", angle, newangle);
101         return newangle;
102 }
103
104 /* angle between vectors - rad version */
105 static inline D3DVALUE AngleBetweenVectorsRad (const D3DVECTOR *a, const D3DVECTOR *b)
106 {
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;
114
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;   
120 }
121
122 static inline D3DVALUE AngleBetweenVectorsDeg (const D3DVECTOR *a, const D3DVECTOR *b)
123 {
124         return RadToDeg(AngleBetweenVectorsRad(a, b));
125 }
126
127 /* calculates vector between two points */
128 static inline D3DVECTOR VectorBetweenTwoPoints (const D3DVECTOR *a, const D3DVECTOR *b)
129 {
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;
137 }
138
139 /* calculates the length of vector's projection on another vector */
140 static inline D3DVALUE ProjectVector (const D3DVECTOR *a, const D3DVECTOR *p)
141 {
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;
148 }
149
150 /*******************************************************************************
151  *              3D Buffer and Listener mixing
152  */
153
154 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
155 {
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 */
165
166         TRACE("(%p)\n",dsb);
167
168         /* initial buffer volume */
169         lVolume = dsb->ds3db_lVolume;
170         
171         switch (dsb->ds3db_ds3db.dwMode)
172         {
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;
189         }
190         
191         if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
192         {
193                 /* some apps don't want you to hear too distant sounds... */
194                 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
195                 {
196                         dsb->volpan.lVolume = DSBVOLUME_MIN;
197                         DSOUND_RecalcVolPan (&dsb->volpan);             
198                         /* i guess mixing here would be a waste of power */
199                         return;
200                 }
201                 else
202                         flDistance = dsb->ds3db_ds3db.flMaxDistance;
203         }               
204
205         if (flDistance < dsb->ds3db_ds3db.flMinDistance)
206                 flDistance = dsb->ds3db_ds3db.flMinDistance;
207         
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);
211
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)
215         {
216                 TRACE("conning: cones not set\n");
217         }
218         else
219         {
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)
224                 {
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;
230
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;
239                 }
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);
242         }
243         dsb->volpan.lVolume = lVolume;
244         
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;
251         }
252         else
253         {
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;
259         }
260         TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
261
262         /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
263 if(0)
264 {
265         D3DVALUE flFreq, flBufferVel, flListenerVel;
266         /* doppler shift*/
267         if (!VectorMagnitude(&dsb->ds3db_ds3db.vVelocity) && !VectorMagnitude(&dsb->device->ds3dl.vVelocity))
268         {
269                 TRACE("doppler: Buffer and Listener don't have velocities\n");
270         }
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))
274         {
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);
291         }
292 }
293         
294         /* time for remix */
295         DSOUND_RecalcVolPan(&dsb->volpan);
296 }
297
298 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
299 {
300         TRACE("(%p)\n",dsb);
301
302         DSOUND_Calc3DBuffer(dsb);
303 }
304
305 static void DSOUND_ChangeListener(IDirectSoundBufferImpl *ds3dl)
306 {
307         int i;
308         TRACE("(%p)\n",ds3dl);
309         for (i = 0; i < ds3dl->device->nrofbuffers; i++)
310         {
311                 /* check if this buffer is waiting for recalculation */
312                 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
313                 {
314                         DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
315                 }
316         }
317 }
318
319 /*******************************************************************************
320  *              IDirectSound3DBuffer
321  */
322 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
323 {
324     return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DBuffer_iface);
325 }
326
327 /* IUnknown methods */
328 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(IDirectSound3DBuffer *iface,
329         REFIID riid, void **ppobj)
330 {
331     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
332
333     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
334
335     return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
336 }
337
338 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(IDirectSound3DBuffer *iface)
339 {
340     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
341     ULONG ref = InterlockedIncrement(&This->ref3D);
342
343     TRACE("(%p) ref was %d\n", This, ref - 1);
344
345     if(ref == 1)
346         InterlockedIncrement(&This->numIfaces);
347
348     return ref;
349 }
350
351 static ULONG WINAPI IDirectSound3DBufferImpl_Release(IDirectSound3DBuffer *iface)
352 {
353     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
354     ULONG ref = InterlockedDecrement(&This->ref3D);
355
356     TRACE("(%p) ref was %d\n", This, ref + 1);
357
358     if (!ref && !InterlockedDecrement(&This->numIfaces))
359         secondarybuffer_destroy(This);
360
361     return ref;
362 }
363
364 /* IDirectSound3DBuffer methods */
365 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(IDirectSound3DBuffer *iface,
366         DS3DBUFFER *lpDs3dBuffer)
367 {
368         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
369
370         TRACE("(%p,%p)\n",This,lpDs3dBuffer);
371
372         if (lpDs3dBuffer == NULL) {
373                 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
374                 return DSERR_INVALIDPARAM;
375         }
376
377         if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
378                 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
379                 return DSERR_INVALIDPARAM;
380         }
381         
382         TRACE("returning: all parameters\n");
383         *lpDs3dBuffer = This->ds3db_ds3db;
384         return DS_OK;
385 }
386
387 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(IDirectSound3DBuffer *iface,
388         DWORD *lpdwInsideConeAngle, DWORD *lpdwOutsideConeAngle)
389 {
390     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
391
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;
397 }
398
399 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(IDirectSound3DBuffer *iface,
400         D3DVECTOR *lpvConeOrientation)
401 {
402     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
403
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;
410 }
411
412 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(IDirectSound3DBuffer *iface,
413         LONG *lplConeOutsideVolume)
414 {
415     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
416
417     TRACE("returning: Cone Outside Volume = %d\n", This->ds3db_ds3db.lConeOutsideVolume);
418     *lplConeOutsideVolume = This->ds3db_ds3db.lConeOutsideVolume;
419     return DS_OK;
420 }
421
422 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(IDirectSound3DBuffer *iface,
423         D3DVALUE *lpfMaxDistance)
424 {
425     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
426
427     TRACE("returning: Max Distance = %f\n", This->ds3db_ds3db.flMaxDistance);
428     *lpfMaxDistance = This->ds3db_ds3db.flMaxDistance;
429     return DS_OK;
430 }
431
432 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(IDirectSound3DBuffer *iface,
433         D3DVALUE *lpfMinDistance)
434 {
435     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
436
437     TRACE("returning: Min Distance = %f\n", This->ds3db_ds3db.flMinDistance);
438     *lpfMinDistance = This->ds3db_ds3db.flMinDistance;
439     return DS_OK;
440 }
441
442 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(IDirectSound3DBuffer *iface,
443         DWORD *lpdwMode)
444 {
445     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
446
447     TRACE("returning: Mode = %d\n", This->ds3db_ds3db.dwMode);
448     *lpdwMode = This->ds3db_ds3db.dwMode;
449     return DS_OK;
450 }
451
452 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(IDirectSound3DBuffer *iface,
453         D3DVECTOR *lpvPosition)
454 {
455     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
456
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;
461 }
462
463 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(IDirectSound3DBuffer *iface,
464         D3DVECTOR *lpvVelocity)
465 {
466     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
467
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;
472 }
473
474 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(IDirectSound3DBuffer *iface,
475         const DS3DBUFFER *lpcDs3dBuffer, DWORD dwApply)
476 {
477         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
478         DWORD status = DSERR_INVALIDPARAM;
479
480         TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
481
482         if (lpcDs3dBuffer == NULL) {
483                 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
484                 return status;
485         }
486
487         if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
488                 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
489                 return status;
490         }
491
492         TRACE("setting: all parameters; dwApply = %d\n", dwApply);
493         This->ds3db_ds3db = *lpcDs3dBuffer;
494
495         if (dwApply == DS3D_IMMEDIATE)
496         {
497                 DSOUND_Mix3DBuffer(This);
498         }
499         This->ds3db_need_recalc = TRUE;
500         status = DS_OK;
501
502         return status;
503 }
504
505 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(IDirectSound3DBuffer *iface,
506         DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD dwApply)
507 {
508     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
509
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;
518 }
519
520 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(IDirectSound3DBuffer *iface,
521         D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
522 {
523     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
524
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)
530     {
531         This->ds3db_need_recalc = FALSE;
532         DSOUND_Mix3DBuffer(This);
533     }
534     This->ds3db_need_recalc = TRUE;
535     return DS_OK;
536 }
537
538 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(IDirectSound3DBuffer *iface,
539         LONG lConeOutsideVolume, DWORD dwApply)
540 {
541     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
542
543     TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
544     This->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
545     if (dwApply == DS3D_IMMEDIATE)
546     {
547         This->ds3db_need_recalc = FALSE;
548         DSOUND_Mix3DBuffer(This);
549     }
550     This->ds3db_need_recalc = TRUE;
551     return DS_OK;
552 }
553
554 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(IDirectSound3DBuffer *iface,
555         D3DVALUE fMaxDistance, DWORD dwApply)
556 {
557     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
558
559     TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
560     This->ds3db_ds3db.flMaxDistance = fMaxDistance;
561     if (dwApply == DS3D_IMMEDIATE)
562     {
563         This->ds3db_need_recalc = FALSE;
564         DSOUND_Mix3DBuffer(This);
565     }
566     This->ds3db_need_recalc = TRUE;
567     return DS_OK;
568 }
569
570 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(IDirectSound3DBuffer *iface,
571         D3DVALUE fMinDistance, DWORD dwApply)
572 {
573     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
574
575     TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
576     This->ds3db_ds3db.flMinDistance = fMinDistance;
577     if (dwApply == DS3D_IMMEDIATE)
578     {
579         This->ds3db_need_recalc = FALSE;
580         DSOUND_Mix3DBuffer(This);
581     }
582     This->ds3db_need_recalc = TRUE;
583     return DS_OK;
584 }
585
586 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(IDirectSound3DBuffer *iface, DWORD dwMode,
587         DWORD dwApply)
588 {
589     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
590
591     TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
592     This->ds3db_ds3db.dwMode = dwMode;
593     if (dwApply == DS3D_IMMEDIATE)
594     {
595         This->ds3db_need_recalc = FALSE;
596         DSOUND_Mix3DBuffer(This);
597     }
598     This->ds3db_need_recalc = TRUE;
599     return DS_OK;
600 }
601
602 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x,
603         D3DVALUE y, D3DVALUE z, DWORD dwApply)
604 {
605     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
606
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)
612     {
613         This->ds3db_need_recalc = FALSE;
614         DSOUND_Mix3DBuffer(This);
615     }
616     This->ds3db_need_recalc = TRUE;
617     return DS_OK;
618 }
619
620 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(IDirectSound3DBuffer *iface,
621         D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
622 {
623     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
624
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)
630     {
631         This->ds3db_need_recalc = FALSE;
632         DSOUND_Mix3DBuffer(This);
633     }
634     This->ds3db_need_recalc = TRUE;
635     return DS_OK;
636 }
637
638 const IDirectSound3DBufferVtbl ds3dbvt =
639 {
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,
663 };
664
665
666 /*******************************************************************************
667  *            IDirectSound3DListener
668  */
669 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
670 {
671     return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DListener_iface);
672 }
673
674
675 /* IUnknown methods */
676 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(IDirectSound3DListener *iface,
677         REFIID riid, void **ppobj)
678 {
679         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
680
681         TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
682
683         return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
684 }
685
686 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(IDirectSound3DListener *iface)
687 {
688     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
689     ULONG ref = InterlockedIncrement(&This->ref3D);
690
691     TRACE("(%p) ref was %d\n", This, ref - 1);
692
693     if(ref == 1)
694         InterlockedIncrement(&This->numIfaces);
695
696     return ref;
697 }
698
699 static ULONG WINAPI IDirectSound3DListenerImpl_Release(IDirectSound3DListener *iface)
700 {
701     IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
702     ULONG ref = InterlockedDecrement(&This->ref3D);
703
704     TRACE("(%p) ref was %d\n", This, ref + 1);
705
706     if (!ref && !InterlockedDecrement(&This->numIfaces))
707             primarybuffer_destroy(This);
708
709     return ref;
710 }
711
712 /* IDirectSound3DListener methods */
713 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
714         DS3DLISTENER *lpDS3DL)
715 {
716         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
717
718         TRACE("(%p,%p)\n",This,lpDS3DL);
719
720         if (lpDS3DL == NULL) {
721                 WARN("invalid parameter: lpDS3DL == NULL\n");
722                 return DSERR_INVALIDPARAM;
723         }
724
725         if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
726                 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
727                 return DSERR_INVALIDPARAM;
728         }
729         
730         TRACE("returning: all parameters\n");
731         *lpDS3DL = This->device->ds3dl;
732         return DS_OK;
733 }
734
735 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
736         D3DVALUE *lpfDistanceFactor)
737 {
738         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
739
740         TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
741         *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
742         return DS_OK;
743 }
744
745 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
746         D3DVALUE *lpfDopplerFactor)
747 {
748         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
749
750         TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
751         *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
752         return DS_OK;
753 }
754
755 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
756         D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
757 {
758         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
759
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;
766 }
767
768 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
769         D3DVECTOR *lpvPosition)
770 {
771         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
772
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;
776 }
777
778 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
779         D3DVALUE *lpfRolloffFactor)
780 {
781         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
782
783         TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
784         *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
785         return DS_OK;
786 }
787
788 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
789         D3DVECTOR *lpvVelocity)
790 {
791         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
792
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;
796 }
797
798 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
799         const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
800 {
801         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
802
803         TRACE("setting: all parameters; dwApply = %d\n", dwApply);
804         This->device->ds3dl = *lpcDS3DL;
805         if (dwApply == DS3D_IMMEDIATE)
806         {
807                 This->device->ds3dl_need_recalc = FALSE;
808                 DSOUND_ChangeListener(This);
809         }
810         This->device->ds3dl_need_recalc = TRUE;
811         return DS_OK;
812 }
813
814 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
815         D3DVALUE fDistanceFactor, DWORD dwApply)
816 {
817         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
818
819         TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
820         This->device->ds3dl.flDistanceFactor = fDistanceFactor;
821         if (dwApply == DS3D_IMMEDIATE)
822         {
823                 This->device->ds3dl_need_recalc = FALSE;
824                 DSOUND_ChangeListener(This);
825         }
826         This->device->ds3dl_need_recalc = TRUE;
827         return DS_OK;
828 }
829
830 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
831         D3DVALUE fDopplerFactor, DWORD dwApply)
832 {
833         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
834
835         TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
836         This->device->ds3dl.flDopplerFactor = fDopplerFactor;
837         if (dwApply == DS3D_IMMEDIATE)
838         {
839                 This->device->ds3dl_need_recalc = FALSE;
840                 DSOUND_ChangeListener(This);
841         }
842         This->device->ds3dl_need_recalc = TRUE;
843         return DS_OK;
844 }
845
846 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
847         D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
848         D3DVALUE zTop, DWORD dwApply)
849 {
850         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
851
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)
861         {
862                 This->device->ds3dl_need_recalc = FALSE;
863                 DSOUND_ChangeListener(This);
864         }
865         This->device->ds3dl_need_recalc = TRUE;
866         return DS_OK;
867 }
868
869 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
870         D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
871 {
872         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
873
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)
879         {
880                 This->device->ds3dl_need_recalc = FALSE;
881                 DSOUND_ChangeListener(This);
882         }
883         This->device->ds3dl_need_recalc = TRUE;
884         return DS_OK;
885 }
886
887 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
888         D3DVALUE fRolloffFactor, DWORD dwApply)
889 {
890         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
891
892         TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
893         This->device->ds3dl.flRolloffFactor = fRolloffFactor;
894         if (dwApply == DS3D_IMMEDIATE)
895         {
896                 This->device->ds3dl_need_recalc = FALSE;
897                 DSOUND_ChangeListener(This);
898         }
899         This->device->ds3dl_need_recalc = TRUE;
900         return DS_OK;
901 }
902
903 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
904         D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
905 {
906         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
907
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)
913         {
914                 This->device->ds3dl_need_recalc = FALSE;
915                 DSOUND_ChangeListener(This);
916         }
917         This->device->ds3dl_need_recalc = TRUE;
918         return DS_OK;
919 }
920
921 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
922 {
923         IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
924
925         TRACE("\n");
926         DSOUND_ChangeListener(This);
927         return DS_OK;
928 }
929
930 const IDirectSound3DListenerVtbl ds3dlvt =
931 {
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,
952 };