Added support for multiple mixers.
[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 IDirectSound3DBufferVtbl 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 = 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 HRESULT WINAPI IDirectSound3DBufferImpl_Destroy(
774     IDirectSound3DBufferImpl *pds3db)
775 {
776     TRACE("(%p)\n",pds3db);
777
778     while (IDirectSound3DBufferImpl_Release((LPDIRECTSOUND3DBUFFER)pds3db) > 0);
779
780     return S_OK;
781 }
782
783 /*******************************************************************************
784  *            IDirectSound3DListener
785  */
786
787 /* IUnknown methods */
788 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
789         LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
790 {
791         ICOM_THIS(IDirectSound3DListenerImpl,iface);
792
793         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
794
795         if (ppobj == NULL) {
796                 WARN("invalid parameter\n");
797                 return E_INVALIDARG;
798         }
799
800         *ppobj = NULL;  /* assume failure */
801
802         if ( IsEqualGUID(riid, &IID_IUnknown) ||
803              IsEqualGUID(riid, &IID_IDirectSound3DListener ) ) {
804                 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This);
805                 *ppobj = This;
806                 return S_OK;
807         }
808
809         if ( IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
810                 if (!This->dsound->primary)
811                         PrimaryBufferImpl_Create(This->dsound, &(This->dsound->primary), &(This->dsound->dsbd));
812                 if (This->dsound->primary) {
813                         *ppobj = This->dsound->primary;
814                         IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)*ppobj);
815                         return S_OK;
816                 }
817         }
818
819         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
820         return E_NOINTERFACE;
821 }
822
823 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
824 {
825         ICOM_THIS(IDirectSound3DListenerImpl,iface);
826         ULONG ref;
827         TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
828
829         ref = InterlockedIncrement(&This->ref);
830
831         if (!ref) {
832                 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
833         }
834
835         return ref;
836 }
837
838 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
839 {
840         ICOM_THIS(IDirectSound3DListenerImpl,iface);
841         ULONG ulReturn;
842
843         TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
844         ulReturn = InterlockedDecrement(&This->ref);
845
846         /* Free all resources */
847         if( ulReturn == 0 ) {
848                 This->dsound->listener = 0;
849                 HeapFree(GetProcessHeap(),0,This);
850                 TRACE("(%p) released\n",This);
851         }
852
853         return ulReturn;
854 }
855
856 /* IDirectSound3DListener methods */
857 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
858         LPDIRECTSOUND3DLISTENER iface,
859         LPDS3DLISTENER lpDS3DL)
860 {
861         ICOM_THIS(IDirectSound3DListenerImpl,iface);
862         TRACE("(%p,%p)\n",This,lpDS3DL);
863
864         if (lpDS3DL == NULL) {
865                 WARN("invalid parameter: lpDS3DL == NULL\n");
866                 return DSERR_INVALIDPARAM;
867         }
868
869         if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
870                 WARN("invalid parameter: lpDS3DL->dwSize = %ld < %d\n",lpDS3DL->dwSize, sizeof(*lpDS3DL));
871                 return DSERR_INVALIDPARAM;
872         }
873         
874         TRACE("returning: all parameters\n");
875         *lpDS3DL = This->dsound->ds3dl;
876         return DS_OK;
877 }
878
879 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
880         LPDIRECTSOUND3DLISTENER iface,
881         LPD3DVALUE lpfDistanceFactor)
882 {
883         ICOM_THIS(IDirectSound3DListenerImpl,iface);
884         TRACE("returning: Distance Factor = %f\n", This->dsound->ds3dl.flDistanceFactor);
885         *lpfDistanceFactor = This->dsound->ds3dl.flDistanceFactor;
886         return DS_OK;
887 }
888
889 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
890         LPDIRECTSOUND3DLISTENER iface,
891         LPD3DVALUE lpfDopplerFactor)
892 {
893         ICOM_THIS(IDirectSound3DListenerImpl,iface);
894         TRACE("returning: Doppler Factor = %f\n", This->dsound->ds3dl.flDopplerFactor);
895         *lpfDopplerFactor = This->dsound->ds3dl.flDopplerFactor;
896         return DS_OK;
897 }
898
899 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
900         LPDIRECTSOUND3DLISTENER iface,
901         LPD3DVECTOR lpvOrientFront,
902         LPD3DVECTOR lpvOrientTop)
903 {
904         ICOM_THIS(IDirectSound3DListenerImpl,iface);
905         TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->dsound->ds3dl.vOrientFront.x, \
906         This->dsound->ds3dl.vOrientFront.y, This->dsound->ds3dl.vOrientFront.z, This->dsound->ds3dl.vOrientTop.x, This->dsound->ds3dl.vOrientTop.y, \
907         This->dsound->ds3dl.vOrientTop.z);
908         *lpvOrientFront = This->dsound->ds3dl.vOrientFront;
909         *lpvOrientTop = This->dsound->ds3dl.vOrientTop;
910         return DS_OK;
911 }
912
913 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
914         LPDIRECTSOUND3DLISTENER iface,
915         LPD3DVECTOR lpvPosition)
916 {
917         ICOM_THIS(IDirectSound3DListenerImpl,iface);
918         TRACE("returning: Position vector = (%f,%f,%f)\n", This->dsound->ds3dl.vPosition.x, This->dsound->ds3dl.vPosition.y, This->dsound->ds3dl.vPosition.z);
919         *lpvPosition = This->dsound->ds3dl.vPosition;
920         return DS_OK;
921 }
922
923 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
924         LPDIRECTSOUND3DLISTENER iface,
925         LPD3DVALUE lpfRolloffFactor)
926 {
927         ICOM_THIS(IDirectSound3DListenerImpl,iface);
928         TRACE("returning: RolloffFactor = %f\n", This->dsound->ds3dl.flRolloffFactor);
929         *lpfRolloffFactor = This->dsound->ds3dl.flRolloffFactor;
930         return DS_OK;
931 }
932
933 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
934         LPDIRECTSOUND3DLISTENER iface,
935         LPD3DVECTOR lpvVelocity)
936 {
937         ICOM_THIS(IDirectSound3DListenerImpl,iface);
938         TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->dsound->ds3dl.vVelocity.x, This->dsound->ds3dl.vVelocity.y, This->dsound->ds3dl.vVelocity.z);
939         *lpvVelocity = This->dsound->ds3dl.vVelocity;
940         return DS_OK;
941 }
942
943 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
944         LPDIRECTSOUND3DLISTENER iface,
945         LPCDS3DLISTENER lpcDS3DL,
946         DWORD dwApply)
947 {
948         ICOM_THIS(IDirectSound3DListenerImpl,iface);
949         TRACE("setting: all parameters; dwApply = %ld\n", dwApply);
950         This->dsound->ds3dl = *lpcDS3DL;
951         if (dwApply == DS3D_IMMEDIATE)
952         {
953                 This->dsound->ds3dl_need_recalc = FALSE;
954                 DSOUND_ChangeListener(This);
955         }
956         This->dsound->ds3dl_need_recalc = TRUE;
957         return DS_OK;
958 }
959
960 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
961         LPDIRECTSOUND3DLISTENER iface,
962         D3DVALUE fDistanceFactor,
963         DWORD dwApply)
964 {
965         ICOM_THIS(IDirectSound3DListenerImpl,iface);
966         TRACE("setting: Distance Factor = %f; dwApply = %ld\n", fDistanceFactor, dwApply);
967         This->dsound->ds3dl.flDistanceFactor = fDistanceFactor;
968         if (dwApply == DS3D_IMMEDIATE)
969         {
970                 This->dsound->ds3dl_need_recalc = FALSE;
971                 DSOUND_ChangeListener(This);
972         }
973         This->dsound->ds3dl_need_recalc = TRUE;
974         return DS_OK;
975 }
976
977 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
978         LPDIRECTSOUND3DLISTENER iface,
979         D3DVALUE fDopplerFactor,
980         DWORD dwApply)
981 {
982         ICOM_THIS(IDirectSound3DListenerImpl,iface);
983         TRACE("setting: Doppler Factor = %f; dwApply = %ld\n", fDopplerFactor, dwApply);
984         This->dsound->ds3dl.flDopplerFactor = fDopplerFactor;
985         if (dwApply == DS3D_IMMEDIATE)
986         {
987                 This->dsound->ds3dl_need_recalc = FALSE;
988                 DSOUND_ChangeListener(This);
989         }
990         This->dsound->ds3dl_need_recalc = TRUE;
991         return DS_OK;
992 }
993
994 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
995         LPDIRECTSOUND3DLISTENER iface,
996         D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
997         D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
998         DWORD dwApply)
999 {
1000         ICOM_THIS(IDirectSound3DListenerImpl,iface);
1001         TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %ld\n", \
1002         xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
1003         This->dsound->ds3dl.vOrientFront.x = xFront;
1004         This->dsound->ds3dl.vOrientFront.y = yFront;
1005         This->dsound->ds3dl.vOrientFront.z = zFront;
1006         This->dsound->ds3dl.vOrientTop.x = xTop;
1007         This->dsound->ds3dl.vOrientTop.y = yTop;
1008         This->dsound->ds3dl.vOrientTop.z = zTop;
1009         if (dwApply == DS3D_IMMEDIATE)
1010         {
1011                 This->dsound->ds3dl_need_recalc = FALSE;
1012                 DSOUND_ChangeListener(This);
1013         }
1014         This->dsound->ds3dl_need_recalc = TRUE;
1015         return DS_OK;
1016 }
1017
1018 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
1019         LPDIRECTSOUND3DLISTENER iface,
1020         D3DVALUE x, D3DVALUE y, D3DVALUE z,
1021         DWORD dwApply)
1022 {
1023         ICOM_THIS(IDirectSound3DListenerImpl,iface);
1024         TRACE("setting: Position vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
1025         This->dsound->ds3dl.vPosition.x = x;
1026         This->dsound->ds3dl.vPosition.y = y;
1027         This->dsound->ds3dl.vPosition.z = z;
1028         if (dwApply == DS3D_IMMEDIATE)
1029         {
1030                 This->dsound->ds3dl_need_recalc = FALSE;
1031                 DSOUND_ChangeListener(This);
1032         }
1033         This->dsound->ds3dl_need_recalc = TRUE;
1034         return DS_OK;
1035 }
1036
1037 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
1038         LPDIRECTSOUND3DLISTENER iface,
1039         D3DVALUE fRolloffFactor,
1040         DWORD dwApply)
1041 {
1042         ICOM_THIS(IDirectSound3DListenerImpl,iface);
1043         TRACE("setting: Rolloff Factor = %f; dwApply = %ld\n", fRolloffFactor, dwApply);
1044         This->dsound->ds3dl.flRolloffFactor = fRolloffFactor;
1045         if (dwApply == DS3D_IMMEDIATE)
1046         {
1047                 This->dsound->ds3dl_need_recalc = FALSE;
1048                 DSOUND_ChangeListener(This);
1049         }
1050         This->dsound->ds3dl_need_recalc = TRUE;
1051         return DS_OK;
1052 }
1053
1054 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
1055         LPDIRECTSOUND3DLISTENER iface,
1056         D3DVALUE x, D3DVALUE y, D3DVALUE z,
1057         DWORD dwApply)
1058 {
1059         ICOM_THIS(IDirectSound3DListenerImpl,iface);
1060         TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
1061         This->dsound->ds3dl.vVelocity.x = x;
1062         This->dsound->ds3dl.vVelocity.y = y;
1063         This->dsound->ds3dl.vVelocity.z = z;
1064         if (dwApply == DS3D_IMMEDIATE)
1065         {
1066                 This->dsound->ds3dl_need_recalc = FALSE;
1067                 DSOUND_ChangeListener(This);
1068         }
1069         This->dsound->ds3dl_need_recalc = TRUE;
1070         return DS_OK;
1071 }
1072
1073 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
1074         LPDIRECTSOUND3DLISTENER iface)
1075 {
1076         ICOM_THIS(IDirectSound3DListenerImpl,iface);
1077         TRACE("\n");
1078         DSOUND_ChangeListener(This);
1079         return DS_OK;
1080 }
1081
1082 static IDirectSound3DListenerVtbl ds3dlvt =
1083 {
1084         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1085         /* IUnknown methods */
1086         IDirectSound3DListenerImpl_QueryInterface,
1087         IDirectSound3DListenerImpl_AddRef,
1088         IDirectSound3DListenerImpl_Release,
1089         /* IDirectSound3DListener methods */
1090         IDirectSound3DListenerImpl_GetAllParameter,
1091         IDirectSound3DListenerImpl_GetDistanceFactor,
1092         IDirectSound3DListenerImpl_GetDopplerFactor,
1093         IDirectSound3DListenerImpl_GetOrientation,
1094         IDirectSound3DListenerImpl_GetPosition,
1095         IDirectSound3DListenerImpl_GetRolloffFactor,
1096         IDirectSound3DListenerImpl_GetVelocity,
1097         IDirectSound3DListenerImpl_SetAllParameters,
1098         IDirectSound3DListenerImpl_SetDistanceFactor,
1099         IDirectSound3DListenerImpl_SetDopplerFactor,
1100         IDirectSound3DListenerImpl_SetOrientation,
1101         IDirectSound3DListenerImpl_SetPosition,
1102         IDirectSound3DListenerImpl_SetRolloffFactor,
1103         IDirectSound3DListenerImpl_SetVelocity,
1104         IDirectSound3DListenerImpl_CommitDeferredSettings,
1105 };
1106
1107 HRESULT WINAPI IDirectSound3DListenerImpl_Create(
1108         PrimaryBufferImpl *This,
1109         IDirectSound3DListenerImpl **pdsl)
1110 {
1111         IDirectSound3DListenerImpl *dsl;
1112         TRACE("(%p,%p)\n",This,pdsl);
1113
1114         dsl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsl));
1115
1116         if (dsl == NULL) {
1117                 WARN("out of memory\n");
1118                 *pdsl = 0;
1119                 return DSERR_OUTOFMEMORY;
1120         }
1121
1122         dsl->ref = 0;
1123         dsl->lpVtbl = &ds3dlvt;
1124
1125         dsl->dsound = This->dsound;
1126
1127         dsl->dsound->ds3dl.dwSize = sizeof(DS3DLISTENER);
1128         dsl->dsound->ds3dl.vPosition.x = 0.0;
1129         dsl->dsound->ds3dl.vPosition.y = 0.0;
1130         dsl->dsound->ds3dl.vPosition.z = 0.0;
1131         dsl->dsound->ds3dl.vVelocity.x = 0.0;
1132         dsl->dsound->ds3dl.vVelocity.y = 0.0;
1133         dsl->dsound->ds3dl.vVelocity.z = 0.0;
1134         dsl->dsound->ds3dl.vOrientFront.x = 0.0;
1135         dsl->dsound->ds3dl.vOrientFront.y = 0.0;
1136         dsl->dsound->ds3dl.vOrientFront.z = 1.0;
1137         dsl->dsound->ds3dl.vOrientTop.x = 0.0;
1138         dsl->dsound->ds3dl.vOrientTop.y = 1.0;
1139         dsl->dsound->ds3dl.vOrientTop.z = 0.0;
1140         dsl->dsound->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1141         dsl->dsound->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1142         dsl->dsound->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1143
1144         dsl->dsound->ds3dl_need_recalc = TRUE;
1145
1146         *pdsl = dsl;
1147         return S_OK;
1148 }