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