kernel32: Print a fixme only once.
[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 "winreg.h"
50 #include "winternl.h"
51 #include "mmddk.h"
52 #include "wine/debug.h"
53 #include "dsound.h"
54 #include "dsdriver.h"
55 #include "dsound_private.h"
56
57 /* default intensity level for human ears */
58 #define DEFAULT_INTENSITY 0.000000000001f
59 /* default velocity of sound in the air */
60 #define DEFAULT_VELOCITY 340
61
62 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
63
64 /*******************************************************************************
65  *              Auxiliary functions
66  */
67
68 /* scalar product (i believe it's called dot product in english) */
69 static inline D3DVALUE ScalarProduct (LPD3DVECTOR a, LPD3DVECTOR b)
70 {
71         D3DVALUE c;
72         c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
73         TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
74               b->z, c);
75         return c;
76 }
77
78 /* vector product (i believe it's called cross product in english */
79 static inline D3DVECTOR VectorProduct (LPD3DVECTOR a, LPD3DVECTOR b)
80 {
81         D3DVECTOR c;
82         c.x = (a->y*b->z) - (a->z*b->y);
83         c.y = (a->z*b->x) - (a->x*b->z);
84         c.z = (a->x*b->y) - (a->y*b->x);
85         TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
86               b->z, c.x, c.y, c.z);
87         return c;
88 }
89
90 /* magnitude (length) of vector */
91 static inline D3DVALUE VectorMagnitude (LPD3DVECTOR a)
92 {
93         D3DVALUE l;
94         l = sqrt (ScalarProduct (a, a));
95         TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
96         return l;
97 }
98
99 /* conversion between radians and degrees */
100 static inline D3DVALUE RadToDeg (D3DVALUE angle)
101 {
102         D3DVALUE newangle;
103         newangle = angle * (360/(2*M_PI));
104         TRACE("%f rad = %f deg\n", angle, newangle);
105         return newangle;
106 }
107
108 /* angle between vectors - deg version */
109 static inline D3DVALUE AngleBetweenVectorsDeg (LPD3DVECTOR a, LPD3DVECTOR b)
110 {
111         D3DVALUE la, lb, product, angle, cos;
112         /* definition of scalar product: a*b = |a|*|b|*cos...therefore: */
113         product = ScalarProduct (a,b);
114         la = VectorMagnitude (a);
115         lb = VectorMagnitude (b);
116         cos = product/(la*lb);
117         angle = acos(cos);
118         /* we now have angle in radians */
119         angle = RadToDeg(angle);
120         TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f degrees\n",  a->x, a->y, a->z, b->x,
121               b->y, b->z, angle);
122         return angle;   
123 }
124
125 /* angle between vectors - rad version */
126 static inline D3DVALUE AngleBetweenVectorsRad (LPD3DVECTOR a, LPD3DVECTOR b)
127 {
128         D3DVALUE la, lb, product, angle, cos;
129         /* definition of scalar product: a*b = |a|*|b|*cos...therefore: */
130         product = ScalarProduct (a,b);
131         la = VectorMagnitude (a);
132         lb = VectorMagnitude (b);
133         cos = product/(la*lb);
134         angle = acos(cos);
135         TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians\n",  a->x, a->y, a->z, b->x,
136               b->y, b->z, angle);
137         return angle;   
138 }
139
140 /* calculates vector between two points */
141 static inline D3DVECTOR VectorBetweenTwoPoints (LPD3DVECTOR a, LPD3DVECTOR b)
142 {
143         D3DVECTOR c;
144         c.x = b->x - a->x;
145         c.y = b->y - a->y;
146         c.z = b->z - a->z;
147         TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
148               b->z, c.x, c.y, c.z);
149         return c;
150 }
151
152 /* calculates the length of vector's projection on another vector */
153 static inline D3DVALUE ProjectVector (LPD3DVECTOR a, LPD3DVECTOR p)
154 {
155         D3DVALUE prod, result;
156         prod = ScalarProduct(a, p);
157         result = prod/VectorMagnitude(p);
158         TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
159               p->y, p->z, result);
160         return result;
161 }
162
163 /*******************************************************************************
164  *              3D Buffer and Listener mixing
165  */
166
167 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
168 {
169         /* volume, at which the sound will be played after all calcs. */
170         D3DVALUE lVolume = 0;
171         /* intensity (used for distance related stuff) */
172         double flIntensity;
173         double flTemp;
174         /* stuff for distance related stuff calc. */
175         D3DVECTOR vDistance;
176         D3DVALUE flDistance = 0;
177         /* panning related stuff */
178         D3DVALUE flAngle;
179         D3DVECTOR vLeft;
180         /* doppler shift related stuff */
181 #if 0
182         D3DVALUE flFreq, flBufferVel, flListenerVel;
183 #endif
184
185         TRACE("(%p)\n",dsb);
186
187         /* initial buffer volume */
188         lVolume = dsb->ds3db_lVolume;
189         
190         switch (dsb->ds3db_ds3db.dwMode)
191         {
192                 case DS3DMODE_DISABLE:
193                         TRACE("3D processing disabled\n");
194                         /* this one is here only to eliminate annoying warning message */
195                         DSOUND_RecalcVolPan (&dsb->volpan);
196                         DSOUND_ForceRemix (dsb);
197                         break;
198                 case DS3DMODE_NORMAL:
199                         TRACE("Normal 3D processing mode\n");
200                         /* we need to calculate distance between buffer and listener*/
201                         vDistance = VectorBetweenTwoPoints(&dsb->ds3db_ds3db.vPosition, &dsb->device->ds3dl.vPosition);
202                         flDistance = VectorMagnitude (&vDistance);
203                         break;
204                 case DS3DMODE_HEADRELATIVE:
205                         TRACE("Head-relative 3D processing mode\n");
206                         /* distance between buffer and listener is same as buffer's position */
207                         flDistance = VectorMagnitude (&dsb->ds3db_ds3db.vPosition);
208                         break;
209         }
210         
211         if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
212         {
213                 /* some apps don't want you to hear too distant sounds... */
214                 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
215                 {
216                         dsb->volpan.lVolume = DSBVOLUME_MIN;
217                         DSOUND_RecalcVolPan (&dsb->volpan);             
218                         /* i guess mixing here would be a waste of power */
219                         return;
220                 }
221                 else
222                         flDistance = dsb->ds3db_ds3db.flMaxDistance;
223         }               
224
225         if (flDistance < dsb->ds3db_ds3db.flMinDistance)
226                 flDistance = dsb->ds3db_ds3db.flMinDistance;
227         
228         /* the following formula is taken from my physics book. I think it's ok for the *real* world...i hope m$ does it that way */
229         lVolume += 10000; /* ms likes working with negative volume...i don't */
230         lVolume /= 1000; /* convert hundreths of dB into B */
231         /* intensity level (loudness) = log10(Intensity/DefaultIntensity)...therefore */
232         flIntensity = pow(10,lVolume)*DEFAULT_INTENSITY;        
233         flTemp = (flDistance/dsb->ds3db_ds3db.flMinDistance)*(flDistance/dsb->ds3db_ds3db.flMinDistance);
234         flIntensity /= flTemp;
235         lVolume = log10(flIntensity/DEFAULT_INTENSITY);
236         lVolume *= 1000; /* convert back to hundreths of dB */
237         lVolume -= 10000; /* we need to do it in ms way */
238         TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
239
240         /* conning */
241         /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
242         if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
243         {
244                 TRACE("conning: cones not set\n");
245         }
246         else
247         {
248                 /* calculate angle */
249                 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistance);
250                 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
251                 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
252                 {
253                         /* my test show that for my way of calc., we need only half of angles */
254                         DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
255                         DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
256                         /* full volume */
257                         if (flAngle < dwInsideConeAngle)
258                                 flAngle = dwInsideConeAngle;
259                         /* min (app defined) volume */
260                         if (flAngle > dwOutsideConeAngle)
261                                 flAngle = dwOutsideConeAngle;
262                         /* this probably isn't the right thing, but it's ok for the time being */
263                         lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
264                 }
265                 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
266                        flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
267         }
268         dsb->volpan.lVolume = lVolume;
269         
270         /* panning */
271         if (dsb->device->ds3dl.vPosition.x == dsb->ds3db_ds3db.vPosition.x &&
272             dsb->device->ds3dl.vPosition.y == dsb->ds3db_ds3db.vPosition.y &&
273             dsb->device->ds3dl.vPosition.z == dsb->ds3db_ds3db.vPosition.z) {
274                 dsb->volpan.lPan = 0;
275                 flAngle = 0.0;
276         }
277         else
278         {
279                 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
280                 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
281                 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
282                 /* for now, we'll use "linear formula" (which is probably incorrect); if someone has it in book, correct it */
283                 dsb->volpan.lPan = 10000*2*flAngle/M_PI - 10000;
284         }
285         TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
286
287         /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
288 #if 0   
289         /* doppler shift*/
290         if ((VectorMagnitude(&ds3db.vVelocity) == 0) && (VectorMagnitude(&dsb->device->ds3dl.vVelocity) == 0))
291         {
292                 TRACE("doppler: Buffer and Listener don't have velocities\n");
293         }
294         else
295         {
296                 /* calculate length of ds3db.vVelocity component which causes Doppler Effect
297                    NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
298                          if buffer moves AWAY from listener, it's velocity component is POSITIVE */
299                 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
300                 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
301                    NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
302                          if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
303                 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
304                 /* formula taken from Gianicoli D.: Physics, 4th edition: */
305                 /* FIXME: replace dsb->freq with appropriate frequency ! */
306                 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
307                 TRACE("doppler: Buffer velocity (component) = %lf, Listener velocity (component) = %lf => Doppler shift: %ld Hz -> %lf Hz\n", flBufferVel, flListenerVel,
308                       dsb->freq, flFreq);
309                 /* FIXME: replace following line with correct frequency setting ! */
310                 dsb->freq = flFreq;
311         }
312 #endif  
313         
314         /* time for remix */
315         DSOUND_RecalcVolPan(&dsb->volpan);
316 }
317
318 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
319 {
320         TRACE("(%p)\n",dsb);
321
322         DSOUND_Calc3DBuffer(dsb);
323         DSOUND_ForceRemix(dsb);                 
324 }
325
326 static void DSOUND_ChangeListener(IDirectSound3DListenerImpl *ds3dl)
327 {
328         int i;
329         TRACE("(%p)\n",ds3dl);
330         for (i = 0; i < ds3dl->device->nrofbuffers; i++)
331         {
332                 /* some buffers don't have 3d buffer (Ultima IX seems to
333                 crash without the following line) */
334                 if (ds3dl->device->buffers[i]->ds3db == NULL)
335                         continue;
336                 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
337                 {
338                         DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
339                 }
340         }
341 }
342
343 /*******************************************************************************
344  *              IDirectSound3DBuffer
345  */
346
347 /* IUnknown methods */
348 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
349         LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
350 {
351         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
352
353         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
354         return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
355 }
356
357 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
358 {
359     IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
360     ULONG ref = InterlockedIncrement(&(This->ref));
361     TRACE("(%p) ref was %d\n", This, ref - 1);
362     return ref;
363 }
364
365 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
366 {
367     IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
368     ULONG ref = InterlockedDecrement(&(This->ref));
369     TRACE("(%p) ref was %d\n", This, ref + 1);
370
371     if (!ref) {
372         This->dsb->ds3db = NULL;
373         IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
374         HeapFree(GetProcessHeap(), 0, This);
375         TRACE("(%p) released\n", This);
376     }
377     return ref;
378 }
379
380 /* IDirectSound3DBuffer methods */
381 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
382         LPDIRECTSOUND3DBUFFER iface,
383         LPDS3DBUFFER lpDs3dBuffer)
384 {
385         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
386         TRACE("(%p,%p)\n",This,lpDs3dBuffer);
387
388         if (lpDs3dBuffer == NULL) {
389                 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
390                 return DSERR_INVALIDPARAM;
391         }
392
393         if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
394                 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
395                 return DSERR_INVALIDPARAM;
396         }
397         
398         TRACE("returning: all parameters\n");
399         *lpDs3dBuffer = This->dsb->ds3db_ds3db;
400         return DS_OK;
401 }
402
403 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
404         LPDIRECTSOUND3DBUFFER iface,
405         LPDWORD lpdwInsideConeAngle,
406         LPDWORD lpdwOutsideConeAngle)
407 {
408         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
409         TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
410                 This->dsb->ds3db_ds3db.dwInsideConeAngle, This->dsb->ds3db_ds3db.dwOutsideConeAngle);
411         *lpdwInsideConeAngle = This->dsb->ds3db_ds3db.dwInsideConeAngle;
412         *lpdwOutsideConeAngle = This->dsb->ds3db_ds3db.dwOutsideConeAngle;
413         return DS_OK;
414 }
415
416 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
417         LPDIRECTSOUND3DBUFFER iface,
418         LPD3DVECTOR lpvConeOrientation)
419 {
420         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
421         TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
422                 This->dsb->ds3db_ds3db.vConeOrientation.x,
423                 This->dsb->ds3db_ds3db.vConeOrientation.y,
424                 This->dsb->ds3db_ds3db.vConeOrientation.z);
425         *lpvConeOrientation = This->dsb->ds3db_ds3db.vConeOrientation;
426         return DS_OK;
427 }
428
429 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
430         LPDIRECTSOUND3DBUFFER iface,
431         LPLONG lplConeOutsideVolume)
432 {
433         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
434         TRACE("returning: Cone Outside Volume = %d\n", This->dsb->ds3db_ds3db.lConeOutsideVolume);
435         *lplConeOutsideVolume = This->dsb->ds3db_ds3db.lConeOutsideVolume;
436         return DS_OK;
437 }
438
439 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
440         LPDIRECTSOUND3DBUFFER iface,
441         LPD3DVALUE lpfMaxDistance)
442 {
443         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
444         TRACE("returning: Max Distance = %f\n", This->dsb->ds3db_ds3db.flMaxDistance);
445         *lpfMaxDistance = This->dsb->ds3db_ds3db.flMaxDistance;
446         return DS_OK;
447 }
448
449 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
450         LPDIRECTSOUND3DBUFFER iface,
451         LPD3DVALUE lpfMinDistance)
452 {
453         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
454         TRACE("returning: Min Distance = %f\n", This->dsb->ds3db_ds3db.flMinDistance);
455         *lpfMinDistance = This->dsb->ds3db_ds3db.flMinDistance;
456         return DS_OK;
457 }
458
459 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
460         LPDIRECTSOUND3DBUFFER iface,
461         LPDWORD lpdwMode)
462 {
463         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
464         TRACE("returning: Mode = %d\n", This->dsb->ds3db_ds3db.dwMode);
465         *lpdwMode = This->dsb->ds3db_ds3db.dwMode;
466         return DS_OK;
467 }
468
469 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
470         LPDIRECTSOUND3DBUFFER iface,
471         LPD3DVECTOR lpvPosition)
472 {
473         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
474         TRACE("returning: Position vector = (%f,%f,%f)\n",
475                 This->dsb->ds3db_ds3db.vPosition.x,
476                 This->dsb->ds3db_ds3db.vPosition.y,
477                 This->dsb->ds3db_ds3db.vPosition.z);
478         *lpvPosition = This->dsb->ds3db_ds3db.vPosition;
479         return DS_OK;
480 }
481
482 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
483         LPDIRECTSOUND3DBUFFER iface,
484         LPD3DVECTOR lpvVelocity)
485 {
486         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
487         TRACE("returning: Velocity vector = (%f,%f,%f)\n",
488                 This->dsb->ds3db_ds3db.vVelocity.x,
489                 This->dsb->ds3db_ds3db.vVelocity.y,
490                 This->dsb->ds3db_ds3db.vVelocity.z);
491         *lpvVelocity = This->dsb->ds3db_ds3db.vVelocity;
492         return DS_OK;
493 }
494
495 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
496         LPDIRECTSOUND3DBUFFER iface,
497         LPCDS3DBUFFER lpcDs3dBuffer,
498         DWORD dwApply)
499 {
500         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
501         DWORD status = DSERR_INVALIDPARAM;
502         TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
503
504         if (lpcDs3dBuffer == NULL) {
505                 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
506                 return status;
507         }
508
509         if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
510                 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
511                 return status;
512         }
513
514         TRACE("setting: all parameters; dwApply = %d\n", dwApply);
515         This->dsb->ds3db_ds3db = *lpcDs3dBuffer;
516
517         if (dwApply == DS3D_IMMEDIATE)
518         {
519                 DSOUND_Mix3DBuffer(This->dsb);
520         }
521         This->dsb->ds3db_need_recalc = TRUE;
522         status = DS_OK;
523
524         return status;
525 }
526
527 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
528         LPDIRECTSOUND3DBUFFER iface,
529         DWORD dwInsideConeAngle,
530         DWORD dwOutsideConeAngle,
531         DWORD dwApply)
532 {
533         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
534         TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
535                 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
536         This->dsb->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
537         This->dsb->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
538         if (dwApply == DS3D_IMMEDIATE)
539         {
540                 DSOUND_Mix3DBuffer(This->dsb);
541         }
542         This->dsb->ds3db_need_recalc = TRUE;
543         return DS_OK;
544 }
545
546 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
547         LPDIRECTSOUND3DBUFFER iface,
548         D3DVALUE x, D3DVALUE y, D3DVALUE z,
549         DWORD dwApply)
550 {
551         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
552         TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
553         This->dsb->ds3db_ds3db.vConeOrientation.x = x;
554         This->dsb->ds3db_ds3db.vConeOrientation.y = y;
555         This->dsb->ds3db_ds3db.vConeOrientation.z = z;
556         if (dwApply == DS3D_IMMEDIATE)
557         {
558                 This->dsb->ds3db_need_recalc = FALSE;
559                 DSOUND_Mix3DBuffer(This->dsb);
560         }
561         This->dsb->ds3db_need_recalc = TRUE;
562         return DS_OK;
563 }
564
565 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
566         LPDIRECTSOUND3DBUFFER iface,
567         LONG lConeOutsideVolume,
568         DWORD dwApply)
569 {
570         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
571         TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
572         This->dsb->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
573         if (dwApply == DS3D_IMMEDIATE)
574         {
575                 This->dsb->ds3db_need_recalc = FALSE;
576                 DSOUND_Mix3DBuffer(This->dsb);
577         }
578         This->dsb->ds3db_need_recalc = TRUE;
579         return DS_OK;
580 }
581
582 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
583         LPDIRECTSOUND3DBUFFER iface,
584         D3DVALUE fMaxDistance,
585         DWORD dwApply)
586 {
587         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
588         TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
589         This->dsb->ds3db_ds3db.flMaxDistance = fMaxDistance;
590         if (dwApply == DS3D_IMMEDIATE)
591         {
592                 This->dsb->ds3db_need_recalc = FALSE;
593                 DSOUND_Mix3DBuffer(This->dsb);
594         }
595         This->dsb->ds3db_need_recalc = TRUE;
596         return DS_OK;
597 }
598
599 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
600         LPDIRECTSOUND3DBUFFER iface,
601         D3DVALUE fMinDistance,
602         DWORD dwApply)
603 {
604         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
605         TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
606         This->dsb->ds3db_ds3db.flMinDistance = fMinDistance;
607         if (dwApply == DS3D_IMMEDIATE)
608         {
609                 This->dsb->ds3db_need_recalc = FALSE;
610                 DSOUND_Mix3DBuffer(This->dsb);
611         }
612         This->dsb->ds3db_need_recalc = TRUE;
613         return DS_OK;
614 }
615
616 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
617         LPDIRECTSOUND3DBUFFER iface,
618         DWORD dwMode,
619         DWORD dwApply)
620 {
621         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
622         TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
623         This->dsb->ds3db_ds3db.dwMode = dwMode;
624         if (dwApply == DS3D_IMMEDIATE)
625         {
626                 This->dsb->ds3db_need_recalc = FALSE;
627                 DSOUND_Mix3DBuffer(This->dsb);
628         }
629         This->dsb->ds3db_need_recalc = TRUE;
630         return DS_OK;
631 }
632
633 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
634         LPDIRECTSOUND3DBUFFER iface,
635         D3DVALUE x, D3DVALUE y, D3DVALUE z,
636         DWORD dwApply)
637 {
638         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
639         TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
640         This->dsb->ds3db_ds3db.vPosition.x = x;
641         This->dsb->ds3db_ds3db.vPosition.y = y;
642         This->dsb->ds3db_ds3db.vPosition.z = z;
643         if (dwApply == DS3D_IMMEDIATE)
644         {
645                 This->dsb->ds3db_need_recalc = FALSE;
646                 DSOUND_Mix3DBuffer(This->dsb);
647         }
648         This->dsb->ds3db_need_recalc = TRUE;
649         return DS_OK;
650 }
651
652 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
653         LPDIRECTSOUND3DBUFFER iface,
654         D3DVALUE x, D3DVALUE y, D3DVALUE z,
655         DWORD dwApply)
656 {
657         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
658         TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
659         This->dsb->ds3db_ds3db.vVelocity.x = x;
660         This->dsb->ds3db_ds3db.vVelocity.y = y;
661         This->dsb->ds3db_ds3db.vVelocity.z = z;
662         if (dwApply == DS3D_IMMEDIATE)
663         {
664                 This->dsb->ds3db_need_recalc = FALSE;
665                 DSOUND_Mix3DBuffer(This->dsb);
666         }
667         This->dsb->ds3db_need_recalc = TRUE;
668         return DS_OK;
669 }
670
671 static const IDirectSound3DBufferVtbl ds3dbvt =
672 {
673         /* IUnknown methods */
674         IDirectSound3DBufferImpl_QueryInterface,
675         IDirectSound3DBufferImpl_AddRef,
676         IDirectSound3DBufferImpl_Release,
677         /* IDirectSound3DBuffer methods */
678         IDirectSound3DBufferImpl_GetAllParameters,
679         IDirectSound3DBufferImpl_GetConeAngles,
680         IDirectSound3DBufferImpl_GetConeOrientation,
681         IDirectSound3DBufferImpl_GetConeOutsideVolume,
682         IDirectSound3DBufferImpl_GetMaxDistance,
683         IDirectSound3DBufferImpl_GetMinDistance,
684         IDirectSound3DBufferImpl_GetMode,
685         IDirectSound3DBufferImpl_GetPosition,
686         IDirectSound3DBufferImpl_GetVelocity,
687         IDirectSound3DBufferImpl_SetAllParameters,
688         IDirectSound3DBufferImpl_SetConeAngles,
689         IDirectSound3DBufferImpl_SetConeOrientation,
690         IDirectSound3DBufferImpl_SetConeOutsideVolume,
691         IDirectSound3DBufferImpl_SetMaxDistance,
692         IDirectSound3DBufferImpl_SetMinDistance,
693         IDirectSound3DBufferImpl_SetMode,
694         IDirectSound3DBufferImpl_SetPosition,
695         IDirectSound3DBufferImpl_SetVelocity,
696 };
697
698 HRESULT IDirectSound3DBufferImpl_Create(
699         IDirectSoundBufferImpl *dsb,
700         IDirectSound3DBufferImpl **pds3db)
701 {
702         IDirectSound3DBufferImpl *ds3db;
703         TRACE("(%p,%p)\n",dsb,pds3db);
704
705         ds3db = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*ds3db));
706
707         if (ds3db == NULL) {
708                 WARN("out of memory\n");
709                 *pds3db = 0;
710                 return DSERR_OUTOFMEMORY;
711         }
712
713         ds3db->ref = 0;
714         ds3db->dsb = dsb;
715         ds3db->lpVtbl = &ds3dbvt;
716
717         ds3db->dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
718         ds3db->dsb->ds3db_ds3db.vPosition.x = 0.0;
719         ds3db->dsb->ds3db_ds3db.vPosition.y = 0.0;
720         ds3db->dsb->ds3db_ds3db.vPosition.z = 0.0;
721         ds3db->dsb->ds3db_ds3db.vVelocity.x = 0.0;
722         ds3db->dsb->ds3db_ds3db.vVelocity.y = 0.0;
723         ds3db->dsb->ds3db_ds3db.vVelocity.z = 0.0;
724         ds3db->dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
725         ds3db->dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
726         ds3db->dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
727         ds3db->dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
728         ds3db->dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
729         ds3db->dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
730         ds3db->dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
731         ds3db->dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
732         ds3db->dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
733
734         ds3db->dsb->ds3db_need_recalc = TRUE;
735
736         IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
737
738         *pds3db = ds3db;
739         return S_OK;
740 }
741
742 HRESULT IDirectSound3DBufferImpl_Destroy(
743     IDirectSound3DBufferImpl *pds3db)
744 {
745     TRACE("(%p)\n",pds3db);
746
747     while (IDirectSound3DBufferImpl_Release((LPDIRECTSOUND3DBUFFER)pds3db) > 0);
748
749     return S_OK;
750 }
751
752 /*******************************************************************************
753  *            IDirectSound3DListener
754  */
755
756 /* IUnknown methods */
757 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
758         LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
759 {
760         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
761
762         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
763
764         if (ppobj == NULL) {
765                 WARN("invalid parameter\n");
766                 return E_INVALIDARG;
767         }
768
769         *ppobj = NULL;  /* assume failure */
770
771         if ( IsEqualGUID(riid, &IID_IUnknown) ||
772              IsEqualGUID(riid, &IID_IDirectSound3DListener ) ) {
773                 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This);
774                 *ppobj = This;
775                 return S_OK;
776         }
777
778         if ( IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
779                 if (!This->device->primary)
780                         PrimaryBufferImpl_Create(This->device, &(This->device->primary), &(This->device->dsbd));
781                 if (This->device->primary) {
782                         *ppobj = This->device->primary;
783                         IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)*ppobj);
784                         return S_OK;
785                 }
786         }
787
788         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
789         return E_NOINTERFACE;
790 }
791
792 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
793 {
794     IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
795     ULONG ref = InterlockedIncrement(&(This->ref));
796     TRACE("(%p) ref was %d\n", This, ref - 1);
797     return ref;
798 }
799
800 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
801 {
802     IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
803     ULONG ref = InterlockedDecrement(&(This->ref));
804     TRACE("(%p) ref was %d\n", This, ref + 1);
805
806     if (!ref) {
807         This->device->listener = 0;
808         HeapFree(GetProcessHeap(), 0, This);
809         TRACE("(%p) released\n", This);
810     }
811     return ref;
812 }
813
814 /* IDirectSound3DListener methods */
815 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
816         LPDIRECTSOUND3DLISTENER iface,
817         LPDS3DLISTENER lpDS3DL)
818 {
819         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
820         TRACE("(%p,%p)\n",This,lpDS3DL);
821
822         if (lpDS3DL == NULL) {
823                 WARN("invalid parameter: lpDS3DL == NULL\n");
824                 return DSERR_INVALIDPARAM;
825         }
826
827         if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
828                 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
829                 return DSERR_INVALIDPARAM;
830         }
831         
832         TRACE("returning: all parameters\n");
833         *lpDS3DL = This->device->ds3dl;
834         return DS_OK;
835 }
836
837 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
838         LPDIRECTSOUND3DLISTENER iface,
839         LPD3DVALUE lpfDistanceFactor)
840 {
841         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
842         TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
843         *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
844         return DS_OK;
845 }
846
847 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
848         LPDIRECTSOUND3DLISTENER iface,
849         LPD3DVALUE lpfDopplerFactor)
850 {
851         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
852         TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
853         *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
854         return DS_OK;
855 }
856
857 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
858         LPDIRECTSOUND3DLISTENER iface,
859         LPD3DVECTOR lpvOrientFront,
860         LPD3DVECTOR lpvOrientTop)
861 {
862         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
863         TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
864         This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
865         This->device->ds3dl.vOrientTop.z);
866         *lpvOrientFront = This->device->ds3dl.vOrientFront;
867         *lpvOrientTop = This->device->ds3dl.vOrientTop;
868         return DS_OK;
869 }
870
871 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
872         LPDIRECTSOUND3DLISTENER iface,
873         LPD3DVECTOR lpvPosition)
874 {
875         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
876         TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
877         *lpvPosition = This->device->ds3dl.vPosition;
878         return DS_OK;
879 }
880
881 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
882         LPDIRECTSOUND3DLISTENER iface,
883         LPD3DVALUE lpfRolloffFactor)
884 {
885         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
886         TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
887         *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
888         return DS_OK;
889 }
890
891 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
892         LPDIRECTSOUND3DLISTENER iface,
893         LPD3DVECTOR lpvVelocity)
894 {
895         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
896         TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
897         *lpvVelocity = This->device->ds3dl.vVelocity;
898         return DS_OK;
899 }
900
901 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
902         LPDIRECTSOUND3DLISTENER iface,
903         LPCDS3DLISTENER lpcDS3DL,
904         DWORD dwApply)
905 {
906         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
907         TRACE("setting: all parameters; dwApply = %d\n", dwApply);
908         This->device->ds3dl = *lpcDS3DL;
909         if (dwApply == DS3D_IMMEDIATE)
910         {
911                 This->device->ds3dl_need_recalc = FALSE;
912                 DSOUND_ChangeListener(This);
913         }
914         This->device->ds3dl_need_recalc = TRUE;
915         return DS_OK;
916 }
917
918 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
919         LPDIRECTSOUND3DLISTENER iface,
920         D3DVALUE fDistanceFactor,
921         DWORD dwApply)
922 {
923         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
924         TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
925         This->device->ds3dl.flDistanceFactor = fDistanceFactor;
926         if (dwApply == DS3D_IMMEDIATE)
927         {
928                 This->device->ds3dl_need_recalc = FALSE;
929                 DSOUND_ChangeListener(This);
930         }
931         This->device->ds3dl_need_recalc = TRUE;
932         return DS_OK;
933 }
934
935 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
936         LPDIRECTSOUND3DLISTENER iface,
937         D3DVALUE fDopplerFactor,
938         DWORD dwApply)
939 {
940         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
941         TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
942         This->device->ds3dl.flDopplerFactor = fDopplerFactor;
943         if (dwApply == DS3D_IMMEDIATE)
944         {
945                 This->device->ds3dl_need_recalc = FALSE;
946                 DSOUND_ChangeListener(This);
947         }
948         This->device->ds3dl_need_recalc = TRUE;
949         return DS_OK;
950 }
951
952 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
953         LPDIRECTSOUND3DLISTENER iface,
954         D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
955         D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
956         DWORD dwApply)
957 {
958         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
959         TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
960         xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
961         This->device->ds3dl.vOrientFront.x = xFront;
962         This->device->ds3dl.vOrientFront.y = yFront;
963         This->device->ds3dl.vOrientFront.z = zFront;
964         This->device->ds3dl.vOrientTop.x = xTop;
965         This->device->ds3dl.vOrientTop.y = yTop;
966         This->device->ds3dl.vOrientTop.z = zTop;
967         if (dwApply == DS3D_IMMEDIATE)
968         {
969                 This->device->ds3dl_need_recalc = FALSE;
970                 DSOUND_ChangeListener(This);
971         }
972         This->device->ds3dl_need_recalc = TRUE;
973         return DS_OK;
974 }
975
976 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
977         LPDIRECTSOUND3DLISTENER iface,
978         D3DVALUE x, D3DVALUE y, D3DVALUE z,
979         DWORD dwApply)
980 {
981         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
982         TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
983         This->device->ds3dl.vPosition.x = x;
984         This->device->ds3dl.vPosition.y = y;
985         This->device->ds3dl.vPosition.z = z;
986         if (dwApply == DS3D_IMMEDIATE)
987         {
988                 This->device->ds3dl_need_recalc = FALSE;
989                 DSOUND_ChangeListener(This);
990         }
991         This->device->ds3dl_need_recalc = TRUE;
992         return DS_OK;
993 }
994
995 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
996         LPDIRECTSOUND3DLISTENER iface,
997         D3DVALUE fRolloffFactor,
998         DWORD dwApply)
999 {
1000         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1001         TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
1002         This->device->ds3dl.flRolloffFactor = fRolloffFactor;
1003         if (dwApply == DS3D_IMMEDIATE)
1004         {
1005                 This->device->ds3dl_need_recalc = FALSE;
1006                 DSOUND_ChangeListener(This);
1007         }
1008         This->device->ds3dl_need_recalc = TRUE;
1009         return DS_OK;
1010 }
1011
1012 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
1013         LPDIRECTSOUND3DLISTENER iface,
1014         D3DVALUE x, D3DVALUE y, D3DVALUE z,
1015         DWORD dwApply)
1016 {
1017         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1018         TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
1019         This->device->ds3dl.vVelocity.x = x;
1020         This->device->ds3dl.vVelocity.y = y;
1021         This->device->ds3dl.vVelocity.z = z;
1022         if (dwApply == DS3D_IMMEDIATE)
1023         {
1024                 This->device->ds3dl_need_recalc = FALSE;
1025                 DSOUND_ChangeListener(This);
1026         }
1027         This->device->ds3dl_need_recalc = TRUE;
1028         return DS_OK;
1029 }
1030
1031 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
1032         LPDIRECTSOUND3DLISTENER iface)
1033 {
1034         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1035         TRACE("\n");
1036         DSOUND_ChangeListener(This);
1037         return DS_OK;
1038 }
1039
1040 static const IDirectSound3DListenerVtbl ds3dlvt =
1041 {
1042         /* IUnknown methods */
1043         IDirectSound3DListenerImpl_QueryInterface,
1044         IDirectSound3DListenerImpl_AddRef,
1045         IDirectSound3DListenerImpl_Release,
1046         /* IDirectSound3DListener methods */
1047         IDirectSound3DListenerImpl_GetAllParameter,
1048         IDirectSound3DListenerImpl_GetDistanceFactor,
1049         IDirectSound3DListenerImpl_GetDopplerFactor,
1050         IDirectSound3DListenerImpl_GetOrientation,
1051         IDirectSound3DListenerImpl_GetPosition,
1052         IDirectSound3DListenerImpl_GetRolloffFactor,
1053         IDirectSound3DListenerImpl_GetVelocity,
1054         IDirectSound3DListenerImpl_SetAllParameters,
1055         IDirectSound3DListenerImpl_SetDistanceFactor,
1056         IDirectSound3DListenerImpl_SetDopplerFactor,
1057         IDirectSound3DListenerImpl_SetOrientation,
1058         IDirectSound3DListenerImpl_SetPosition,
1059         IDirectSound3DListenerImpl_SetRolloffFactor,
1060         IDirectSound3DListenerImpl_SetVelocity,
1061         IDirectSound3DListenerImpl_CommitDeferredSettings,
1062 };
1063
1064 HRESULT IDirectSound3DListenerImpl_Create(
1065         DirectSoundDevice * device,
1066         IDirectSound3DListenerImpl ** ppdsl)
1067 {
1068         IDirectSound3DListenerImpl *pdsl;
1069         TRACE("(%p,%p)\n",device,ppdsl);
1070
1071         pdsl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*pdsl));
1072
1073         if (pdsl == NULL) {
1074                 WARN("out of memory\n");
1075                 *ppdsl = 0;
1076                 return DSERR_OUTOFMEMORY;
1077         }
1078
1079         pdsl->ref = 0;
1080         pdsl->lpVtbl = &ds3dlvt;
1081
1082         pdsl->device = device;
1083
1084         pdsl->device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1085         pdsl->device->ds3dl.vPosition.x = 0.0;
1086         pdsl->device->ds3dl.vPosition.y = 0.0;
1087         pdsl->device->ds3dl.vPosition.z = 0.0;
1088         pdsl->device->ds3dl.vVelocity.x = 0.0;
1089         pdsl->device->ds3dl.vVelocity.y = 0.0;
1090         pdsl->device->ds3dl.vVelocity.z = 0.0;
1091         pdsl->device->ds3dl.vOrientFront.x = 0.0;
1092         pdsl->device->ds3dl.vOrientFront.y = 0.0;
1093         pdsl->device->ds3dl.vOrientFront.z = 1.0;
1094         pdsl->device->ds3dl.vOrientTop.x = 0.0;
1095         pdsl->device->ds3dl.vOrientTop.y = 1.0;
1096         pdsl->device->ds3dl.vOrientTop.z = 0.0;
1097         pdsl->device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1098         pdsl->device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1099         pdsl->device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1100
1101         pdsl->device->ds3dl_need_recalc = TRUE;
1102
1103         *ppdsl = pdsl;
1104         return S_OK;
1105 }