VerifyCertification is not always implemented.
[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         IDirectSound3DBufferImpl *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         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
382         TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
383         return InterlockedIncrement(&(This->ref));
384 }
385
386 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
387 {
388         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
389         ULONG ulReturn;
390
391         TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
392         ulReturn = InterlockedDecrement(&(This->ref));
393         if (!ulReturn) {
394                 This->dsb->ds3db = NULL;
395                 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
396                 HeapFree(GetProcessHeap(),0,This);
397                 TRACE("(%p) released\n",This);
398         }
399
400         return ulReturn;
401 }
402
403 /* IDirectSound3DBuffer methods */
404 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
405         LPDIRECTSOUND3DBUFFER iface,
406         LPDS3DBUFFER lpDs3dBuffer)
407 {
408         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
409         TRACE("(%p,%p)\n",This,lpDs3dBuffer);
410
411         if (lpDs3dBuffer == NULL) {
412                 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
413                 return DSERR_INVALIDPARAM;
414         }
415
416         if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
417                 WARN("invalid parameter: lpDs3dBuffer->dwSize = %ld < %d\n",lpDs3dBuffer->dwSize, sizeof(*lpDs3dBuffer));
418                 return DSERR_INVALIDPARAM;
419         }
420         
421         TRACE("returning: all parameters\n");
422         *lpDs3dBuffer = This->dsb->ds3db_ds3db;
423         return DS_OK;
424 }
425
426 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
427         LPDIRECTSOUND3DBUFFER iface,
428         LPDWORD lpdwInsideConeAngle,
429         LPDWORD lpdwOutsideConeAngle)
430 {
431         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
432         TRACE("returning: Inside Cone Angle = %ld degrees; Outside Cone Angle = %ld degrees\n",
433                 This->dsb->ds3db_ds3db.dwInsideConeAngle, This->dsb->ds3db_ds3db.dwOutsideConeAngle);
434         *lpdwInsideConeAngle = This->dsb->ds3db_ds3db.dwInsideConeAngle;
435         *lpdwOutsideConeAngle = This->dsb->ds3db_ds3db.dwOutsideConeAngle;
436         return DS_OK;
437 }
438
439 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
440         LPDIRECTSOUND3DBUFFER iface,
441         LPD3DVECTOR lpvConeOrientation)
442 {
443         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
444         TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
445                 This->dsb->ds3db_ds3db.vConeOrientation.x,
446                 This->dsb->ds3db_ds3db.vConeOrientation.y,
447                 This->dsb->ds3db_ds3db.vConeOrientation.z);
448         *lpvConeOrientation = This->dsb->ds3db_ds3db.vConeOrientation;
449         return DS_OK;
450 }
451
452 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
453         LPDIRECTSOUND3DBUFFER iface,
454         LPLONG lplConeOutsideVolume)
455 {
456         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
457         TRACE("returning: Cone Outside Volume = %ld\n", This->dsb->ds3db_ds3db.lConeOutsideVolume);
458         *lplConeOutsideVolume = This->dsb->ds3db_ds3db.lConeOutsideVolume;
459         return DS_OK;
460 }
461
462 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
463         LPDIRECTSOUND3DBUFFER iface,
464         LPD3DVALUE lpfMaxDistance)
465 {
466         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
467         TRACE("returning: Max Distance = %f\n", This->dsb->ds3db_ds3db.flMaxDistance);
468         *lpfMaxDistance = This->dsb->ds3db_ds3db.flMaxDistance;
469         return DS_OK;
470 }
471
472 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
473         LPDIRECTSOUND3DBUFFER iface,
474         LPD3DVALUE lpfMinDistance)
475 {
476         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
477         TRACE("returning: Min Distance = %f\n", This->dsb->ds3db_ds3db.flMinDistance);
478         *lpfMinDistance = This->dsb->ds3db_ds3db.flMinDistance;
479         return DS_OK;
480 }
481
482 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
483         LPDIRECTSOUND3DBUFFER iface,
484         LPDWORD lpdwMode)
485 {
486         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
487         TRACE("returning: Mode = %ld\n", This->dsb->ds3db_ds3db.dwMode);
488         *lpdwMode = This->dsb->ds3db_ds3db.dwMode;
489         return DS_OK;
490 }
491
492 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
493         LPDIRECTSOUND3DBUFFER iface,
494         LPD3DVECTOR lpvPosition)
495 {
496         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
497         TRACE("returning: Position vector = (%f,%f,%f)\n",
498                 This->dsb->ds3db_ds3db.vPosition.x,
499                 This->dsb->ds3db_ds3db.vPosition.y,
500                 This->dsb->ds3db_ds3db.vPosition.z);
501         *lpvPosition = This->dsb->ds3db_ds3db.vPosition;
502         return DS_OK;
503 }
504
505 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
506         LPDIRECTSOUND3DBUFFER iface,
507         LPD3DVECTOR lpvVelocity)
508 {
509         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
510         TRACE("returning: Velocity vector = (%f,%f,%f)\n",
511                 This->dsb->ds3db_ds3db.vVelocity.x,
512                 This->dsb->ds3db_ds3db.vVelocity.y,
513                 This->dsb->ds3db_ds3db.vVelocity.z);
514         *lpvVelocity = This->dsb->ds3db_ds3db.vVelocity;
515         return DS_OK;
516 }
517
518 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
519         LPDIRECTSOUND3DBUFFER iface,
520         LPCDS3DBUFFER lpcDs3dBuffer,
521         DWORD dwApply)
522 {
523         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
524         DWORD status = DSERR_INVALIDPARAM;
525         TRACE("(%p,%p,%lx)\n",iface,lpcDs3dBuffer,dwApply);
526
527         if (lpcDs3dBuffer == NULL) {
528                 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
529                 return status;
530         }
531
532         if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
533                 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %ld != %d\n",
534                         lpcDs3dBuffer->dwSize, sizeof(DS3DBUFFER));
535                 return status;
536         }
537
538         TRACE("setting: all parameters; dwApply = %ld\n", dwApply);
539         This->dsb->ds3db_ds3db = *lpcDs3dBuffer;
540
541         if (dwApply == DS3D_IMMEDIATE)
542         {
543                 DSOUND_Mix3DBuffer(This->dsb);
544         }
545         This->dsb->ds3db_need_recalc = TRUE;
546         status = DS_OK;
547
548         return status;
549 }
550
551 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
552         LPDIRECTSOUND3DBUFFER iface,
553         DWORD dwInsideConeAngle,
554         DWORD dwOutsideConeAngle,
555         DWORD dwApply)
556 {
557         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
558         TRACE("setting: Inside Cone Angle = %ld; Outside Cone Angle = %ld; dwApply = %ld\n",
559                 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
560         This->dsb->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
561         This->dsb->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
562         if (dwApply == DS3D_IMMEDIATE)
563         {
564                 DSOUND_Mix3DBuffer(This->dsb);
565         }
566         This->dsb->ds3db_need_recalc = TRUE;
567         return DS_OK;
568 }
569
570 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
571         LPDIRECTSOUND3DBUFFER iface,
572         D3DVALUE x, D3DVALUE y, D3DVALUE z,
573         DWORD dwApply)
574 {
575         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
576         TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
577         This->dsb->ds3db_ds3db.vConeOrientation.x = x;
578         This->dsb->ds3db_ds3db.vConeOrientation.y = y;
579         This->dsb->ds3db_ds3db.vConeOrientation.z = z;
580         if (dwApply == DS3D_IMMEDIATE)
581         {
582                 This->dsb->ds3db_need_recalc = FALSE;
583                 DSOUND_Mix3DBuffer(This->dsb);
584         }
585         This->dsb->ds3db_need_recalc = TRUE;
586         return DS_OK;
587 }
588
589 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
590         LPDIRECTSOUND3DBUFFER iface,
591         LONG lConeOutsideVolume,
592         DWORD dwApply)
593 {
594         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
595         TRACE("setting: ConeOutsideVolume = %ld; dwApply = %ld\n", lConeOutsideVolume, dwApply);
596         This->dsb->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
597         if (dwApply == DS3D_IMMEDIATE)
598         {
599                 This->dsb->ds3db_need_recalc = FALSE;
600                 DSOUND_Mix3DBuffer(This->dsb);
601         }
602         This->dsb->ds3db_need_recalc = TRUE;
603         return DS_OK;
604 }
605
606 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
607         LPDIRECTSOUND3DBUFFER iface,
608         D3DVALUE fMaxDistance,
609         DWORD dwApply)
610 {
611         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
612         TRACE("setting: MaxDistance = %f; dwApply = %ld\n", fMaxDistance, dwApply);
613         This->dsb->ds3db_ds3db.flMaxDistance = fMaxDistance;
614         if (dwApply == DS3D_IMMEDIATE)
615         {
616                 This->dsb->ds3db_need_recalc = FALSE;
617                 DSOUND_Mix3DBuffer(This->dsb);
618         }
619         This->dsb->ds3db_need_recalc = TRUE;
620         return DS_OK;
621 }
622
623 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
624         LPDIRECTSOUND3DBUFFER iface,
625         D3DVALUE fMinDistance,
626         DWORD dwApply)
627 {
628         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
629         TRACE("setting: MinDistance = %f; dwApply = %ld\n", fMinDistance, dwApply);
630         This->dsb->ds3db_ds3db.flMinDistance = fMinDistance;
631         if (dwApply == DS3D_IMMEDIATE)
632         {
633                 This->dsb->ds3db_need_recalc = FALSE;
634                 DSOUND_Mix3DBuffer(This->dsb);
635         }
636         This->dsb->ds3db_need_recalc = TRUE;
637         return DS_OK;
638 }
639
640 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
641         LPDIRECTSOUND3DBUFFER iface,
642         DWORD dwMode,
643         DWORD dwApply)
644 {
645         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
646         TRACE("setting: Mode = %ld; dwApply = %ld\n", dwMode, dwApply);
647         This->dsb->ds3db_ds3db.dwMode = dwMode;
648         if (dwApply == DS3D_IMMEDIATE)
649         {
650                 This->dsb->ds3db_need_recalc = FALSE;
651                 DSOUND_Mix3DBuffer(This->dsb);
652         }
653         This->dsb->ds3db_need_recalc = TRUE;
654         return DS_OK;
655 }
656
657 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
658         LPDIRECTSOUND3DBUFFER iface,
659         D3DVALUE x, D3DVALUE y, D3DVALUE z,
660         DWORD dwApply)
661 {
662         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
663         TRACE("setting: Position vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
664         This->dsb->ds3db_ds3db.vPosition.x = x;
665         This->dsb->ds3db_ds3db.vPosition.y = y;
666         This->dsb->ds3db_ds3db.vPosition.z = z;
667         if (dwApply == DS3D_IMMEDIATE)
668         {
669                 This->dsb->ds3db_need_recalc = FALSE;
670                 DSOUND_Mix3DBuffer(This->dsb);
671         }
672         This->dsb->ds3db_need_recalc = TRUE;
673         return DS_OK;
674 }
675
676 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
677         LPDIRECTSOUND3DBUFFER iface,
678         D3DVALUE x, D3DVALUE y, D3DVALUE z,
679         DWORD dwApply)
680 {
681         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
682         TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
683         This->dsb->ds3db_ds3db.vVelocity.x = x;
684         This->dsb->ds3db_ds3db.vVelocity.y = y;
685         This->dsb->ds3db_ds3db.vVelocity.z = z;
686         if (dwApply == DS3D_IMMEDIATE)
687         {
688                 This->dsb->ds3db_need_recalc = FALSE;
689                 DSOUND_Mix3DBuffer(This->dsb);
690         }
691         This->dsb->ds3db_need_recalc = TRUE;
692         return DS_OK;
693 }
694
695 static IDirectSound3DBufferVtbl ds3dbvt =
696 {
697         /* IUnknown methods */
698         IDirectSound3DBufferImpl_QueryInterface,
699         IDirectSound3DBufferImpl_AddRef,
700         IDirectSound3DBufferImpl_Release,
701         /* IDirectSound3DBuffer methods */
702         IDirectSound3DBufferImpl_GetAllParameters,
703         IDirectSound3DBufferImpl_GetConeAngles,
704         IDirectSound3DBufferImpl_GetConeOrientation,
705         IDirectSound3DBufferImpl_GetConeOutsideVolume,
706         IDirectSound3DBufferImpl_GetMaxDistance,
707         IDirectSound3DBufferImpl_GetMinDistance,
708         IDirectSound3DBufferImpl_GetMode,
709         IDirectSound3DBufferImpl_GetPosition,
710         IDirectSound3DBufferImpl_GetVelocity,
711         IDirectSound3DBufferImpl_SetAllParameters,
712         IDirectSound3DBufferImpl_SetConeAngles,
713         IDirectSound3DBufferImpl_SetConeOrientation,
714         IDirectSound3DBufferImpl_SetConeOutsideVolume,
715         IDirectSound3DBufferImpl_SetMaxDistance,
716         IDirectSound3DBufferImpl_SetMinDistance,
717         IDirectSound3DBufferImpl_SetMode,
718         IDirectSound3DBufferImpl_SetPosition,
719         IDirectSound3DBufferImpl_SetVelocity,
720 };
721
722 HRESULT WINAPI IDirectSound3DBufferImpl_Create(
723         IDirectSoundBufferImpl *dsb,
724         IDirectSound3DBufferImpl **pds3db)
725 {
726         IDirectSound3DBufferImpl *ds3db;
727         TRACE("(%p,%p)\n",dsb,pds3db);
728
729         ds3db = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*ds3db));
730
731         if (ds3db == NULL) {
732                 WARN("out of memory\n");
733                 *pds3db = 0;
734                 return DSERR_OUTOFMEMORY;
735         }
736
737         ds3db->ref = 0;
738         ds3db->dsb = dsb;
739         ds3db->lpVtbl = &ds3dbvt;
740
741         ds3db->dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
742         ds3db->dsb->ds3db_ds3db.vPosition.x = 0.0;
743         ds3db->dsb->ds3db_ds3db.vPosition.y = 0.0;
744         ds3db->dsb->ds3db_ds3db.vPosition.z = 0.0;
745         ds3db->dsb->ds3db_ds3db.vVelocity.x = 0.0;
746         ds3db->dsb->ds3db_ds3db.vVelocity.y = 0.0;
747         ds3db->dsb->ds3db_ds3db.vVelocity.z = 0.0;
748         ds3db->dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
749         ds3db->dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
750         ds3db->dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
751         ds3db->dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
752         ds3db->dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
753         ds3db->dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
754         ds3db->dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
755         ds3db->dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
756         ds3db->dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
757
758         ds3db->dsb->ds3db_need_recalc = TRUE;
759
760         IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
761
762         *pds3db = ds3db;
763         return S_OK;
764 }
765
766 HRESULT WINAPI IDirectSound3DBufferImpl_Destroy(
767     IDirectSound3DBufferImpl *pds3db)
768 {
769     TRACE("(%p)\n",pds3db);
770
771     while (IDirectSound3DBufferImpl_Release((LPDIRECTSOUND3DBUFFER)pds3db) > 0);
772
773     return S_OK;
774 }
775
776 /*******************************************************************************
777  *            IDirectSound3DListener
778  */
779
780 /* IUnknown methods */
781 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
782         LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
783 {
784         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
785
786         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
787
788         if (ppobj == NULL) {
789                 WARN("invalid parameter\n");
790                 return E_INVALIDARG;
791         }
792
793         *ppobj = NULL;  /* assume failure */
794
795         if ( IsEqualGUID(riid, &IID_IUnknown) ||
796              IsEqualGUID(riid, &IID_IDirectSound3DListener ) ) {
797                 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This);
798                 *ppobj = This;
799                 return S_OK;
800         }
801
802         if ( IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
803                 if (!This->dsound->primary)
804                         PrimaryBufferImpl_Create(This->dsound, &(This->dsound->primary), &(This->dsound->dsbd));
805                 if (This->dsound->primary) {
806                         *ppobj = This->dsound->primary;
807                         IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)*ppobj);
808                         return S_OK;
809                 }
810         }
811
812         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
813         return E_NOINTERFACE;
814 }
815
816 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
817 {
818         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
819         TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
820         return InterlockedIncrement(&(This->ref));
821 }
822
823 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
824 {
825         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
826         ULONG ulReturn;
827
828         TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
829         ulReturn = InterlockedDecrement(&(This->ref));
830
831         /* Free all resources */
832         if( ulReturn == 0 ) {
833                 This->dsound->listener = 0;
834                 HeapFree(GetProcessHeap(),0,This);
835                 TRACE("(%p) released\n",This);
836         }
837
838         return ulReturn;
839 }
840
841 /* IDirectSound3DListener methods */
842 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
843         LPDIRECTSOUND3DLISTENER iface,
844         LPDS3DLISTENER lpDS3DL)
845 {
846         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
847         TRACE("(%p,%p)\n",This,lpDS3DL);
848
849         if (lpDS3DL == NULL) {
850                 WARN("invalid parameter: lpDS3DL == NULL\n");
851                 return DSERR_INVALIDPARAM;
852         }
853
854         if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
855                 WARN("invalid parameter: lpDS3DL->dwSize = %ld < %d\n",lpDS3DL->dwSize, sizeof(*lpDS3DL));
856                 return DSERR_INVALIDPARAM;
857         }
858         
859         TRACE("returning: all parameters\n");
860         *lpDS3DL = This->dsound->ds3dl;
861         return DS_OK;
862 }
863
864 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
865         LPDIRECTSOUND3DLISTENER iface,
866         LPD3DVALUE lpfDistanceFactor)
867 {
868         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
869         TRACE("returning: Distance Factor = %f\n", This->dsound->ds3dl.flDistanceFactor);
870         *lpfDistanceFactor = This->dsound->ds3dl.flDistanceFactor;
871         return DS_OK;
872 }
873
874 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
875         LPDIRECTSOUND3DLISTENER iface,
876         LPD3DVALUE lpfDopplerFactor)
877 {
878         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
879         TRACE("returning: Doppler Factor = %f\n", This->dsound->ds3dl.flDopplerFactor);
880         *lpfDopplerFactor = This->dsound->ds3dl.flDopplerFactor;
881         return DS_OK;
882 }
883
884 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
885         LPDIRECTSOUND3DLISTENER iface,
886         LPD3DVECTOR lpvOrientFront,
887         LPD3DVECTOR lpvOrientTop)
888 {
889         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
890         TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->dsound->ds3dl.vOrientFront.x, \
891         This->dsound->ds3dl.vOrientFront.y, This->dsound->ds3dl.vOrientFront.z, This->dsound->ds3dl.vOrientTop.x, This->dsound->ds3dl.vOrientTop.y, \
892         This->dsound->ds3dl.vOrientTop.z);
893         *lpvOrientFront = This->dsound->ds3dl.vOrientFront;
894         *lpvOrientTop = This->dsound->ds3dl.vOrientTop;
895         return DS_OK;
896 }
897
898 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
899         LPDIRECTSOUND3DLISTENER iface,
900         LPD3DVECTOR lpvPosition)
901 {
902         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
903         TRACE("returning: Position vector = (%f,%f,%f)\n", This->dsound->ds3dl.vPosition.x, This->dsound->ds3dl.vPosition.y, This->dsound->ds3dl.vPosition.z);
904         *lpvPosition = This->dsound->ds3dl.vPosition;
905         return DS_OK;
906 }
907
908 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
909         LPDIRECTSOUND3DLISTENER iface,
910         LPD3DVALUE lpfRolloffFactor)
911 {
912         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
913         TRACE("returning: RolloffFactor = %f\n", This->dsound->ds3dl.flRolloffFactor);
914         *lpfRolloffFactor = This->dsound->ds3dl.flRolloffFactor;
915         return DS_OK;
916 }
917
918 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
919         LPDIRECTSOUND3DLISTENER iface,
920         LPD3DVECTOR lpvVelocity)
921 {
922         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
923         TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->dsound->ds3dl.vVelocity.x, This->dsound->ds3dl.vVelocity.y, This->dsound->ds3dl.vVelocity.z);
924         *lpvVelocity = This->dsound->ds3dl.vVelocity;
925         return DS_OK;
926 }
927
928 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
929         LPDIRECTSOUND3DLISTENER iface,
930         LPCDS3DLISTENER lpcDS3DL,
931         DWORD dwApply)
932 {
933         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
934         TRACE("setting: all parameters; dwApply = %ld\n", dwApply);
935         This->dsound->ds3dl = *lpcDS3DL;
936         if (dwApply == DS3D_IMMEDIATE)
937         {
938                 This->dsound->ds3dl_need_recalc = FALSE;
939                 DSOUND_ChangeListener(This);
940         }
941         This->dsound->ds3dl_need_recalc = TRUE;
942         return DS_OK;
943 }
944
945 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
946         LPDIRECTSOUND3DLISTENER iface,
947         D3DVALUE fDistanceFactor,
948         DWORD dwApply)
949 {
950         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
951         TRACE("setting: Distance Factor = %f; dwApply = %ld\n", fDistanceFactor, dwApply);
952         This->dsound->ds3dl.flDistanceFactor = fDistanceFactor;
953         if (dwApply == DS3D_IMMEDIATE)
954         {
955                 This->dsound->ds3dl_need_recalc = FALSE;
956                 DSOUND_ChangeListener(This);
957         }
958         This->dsound->ds3dl_need_recalc = TRUE;
959         return DS_OK;
960 }
961
962 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
963         LPDIRECTSOUND3DLISTENER iface,
964         D3DVALUE fDopplerFactor,
965         DWORD dwApply)
966 {
967         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
968         TRACE("setting: Doppler Factor = %f; dwApply = %ld\n", fDopplerFactor, dwApply);
969         This->dsound->ds3dl.flDopplerFactor = fDopplerFactor;
970         if (dwApply == DS3D_IMMEDIATE)
971         {
972                 This->dsound->ds3dl_need_recalc = FALSE;
973                 DSOUND_ChangeListener(This);
974         }
975         This->dsound->ds3dl_need_recalc = TRUE;
976         return DS_OK;
977 }
978
979 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
980         LPDIRECTSOUND3DLISTENER iface,
981         D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
982         D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
983         DWORD dwApply)
984 {
985         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
986         TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %ld\n", \
987         xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
988         This->dsound->ds3dl.vOrientFront.x = xFront;
989         This->dsound->ds3dl.vOrientFront.y = yFront;
990         This->dsound->ds3dl.vOrientFront.z = zFront;
991         This->dsound->ds3dl.vOrientTop.x = xTop;
992         This->dsound->ds3dl.vOrientTop.y = yTop;
993         This->dsound->ds3dl.vOrientTop.z = zTop;
994         if (dwApply == DS3D_IMMEDIATE)
995         {
996                 This->dsound->ds3dl_need_recalc = FALSE;
997                 DSOUND_ChangeListener(This);
998         }
999         This->dsound->ds3dl_need_recalc = TRUE;
1000         return DS_OK;
1001 }
1002
1003 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
1004         LPDIRECTSOUND3DLISTENER iface,
1005         D3DVALUE x, D3DVALUE y, D3DVALUE z,
1006         DWORD dwApply)
1007 {
1008         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1009         TRACE("setting: Position vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
1010         This->dsound->ds3dl.vPosition.x = x;
1011         This->dsound->ds3dl.vPosition.y = y;
1012         This->dsound->ds3dl.vPosition.z = z;
1013         if (dwApply == DS3D_IMMEDIATE)
1014         {
1015                 This->dsound->ds3dl_need_recalc = FALSE;
1016                 DSOUND_ChangeListener(This);
1017         }
1018         This->dsound->ds3dl_need_recalc = TRUE;
1019         return DS_OK;
1020 }
1021
1022 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
1023         LPDIRECTSOUND3DLISTENER iface,
1024         D3DVALUE fRolloffFactor,
1025         DWORD dwApply)
1026 {
1027         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1028         TRACE("setting: Rolloff Factor = %f; dwApply = %ld\n", fRolloffFactor, dwApply);
1029         This->dsound->ds3dl.flRolloffFactor = fRolloffFactor;
1030         if (dwApply == DS3D_IMMEDIATE)
1031         {
1032                 This->dsound->ds3dl_need_recalc = FALSE;
1033                 DSOUND_ChangeListener(This);
1034         }
1035         This->dsound->ds3dl_need_recalc = TRUE;
1036         return DS_OK;
1037 }
1038
1039 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
1040         LPDIRECTSOUND3DLISTENER iface,
1041         D3DVALUE x, D3DVALUE y, D3DVALUE z,
1042         DWORD dwApply)
1043 {
1044         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1045         TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
1046         This->dsound->ds3dl.vVelocity.x = x;
1047         This->dsound->ds3dl.vVelocity.y = y;
1048         This->dsound->ds3dl.vVelocity.z = z;
1049         if (dwApply == DS3D_IMMEDIATE)
1050         {
1051                 This->dsound->ds3dl_need_recalc = FALSE;
1052                 DSOUND_ChangeListener(This);
1053         }
1054         This->dsound->ds3dl_need_recalc = TRUE;
1055         return DS_OK;
1056 }
1057
1058 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
1059         LPDIRECTSOUND3DLISTENER iface)
1060 {
1061         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1062         TRACE("\n");
1063         DSOUND_ChangeListener(This);
1064         return DS_OK;
1065 }
1066
1067 static IDirectSound3DListenerVtbl ds3dlvt =
1068 {
1069         /* IUnknown methods */
1070         IDirectSound3DListenerImpl_QueryInterface,
1071         IDirectSound3DListenerImpl_AddRef,
1072         IDirectSound3DListenerImpl_Release,
1073         /* IDirectSound3DListener methods */
1074         IDirectSound3DListenerImpl_GetAllParameter,
1075         IDirectSound3DListenerImpl_GetDistanceFactor,
1076         IDirectSound3DListenerImpl_GetDopplerFactor,
1077         IDirectSound3DListenerImpl_GetOrientation,
1078         IDirectSound3DListenerImpl_GetPosition,
1079         IDirectSound3DListenerImpl_GetRolloffFactor,
1080         IDirectSound3DListenerImpl_GetVelocity,
1081         IDirectSound3DListenerImpl_SetAllParameters,
1082         IDirectSound3DListenerImpl_SetDistanceFactor,
1083         IDirectSound3DListenerImpl_SetDopplerFactor,
1084         IDirectSound3DListenerImpl_SetOrientation,
1085         IDirectSound3DListenerImpl_SetPosition,
1086         IDirectSound3DListenerImpl_SetRolloffFactor,
1087         IDirectSound3DListenerImpl_SetVelocity,
1088         IDirectSound3DListenerImpl_CommitDeferredSettings,
1089 };
1090
1091 HRESULT WINAPI IDirectSound3DListenerImpl_Create(
1092         PrimaryBufferImpl *This,
1093         IDirectSound3DListenerImpl **pdsl)
1094 {
1095         IDirectSound3DListenerImpl *dsl;
1096         TRACE("(%p,%p)\n",This,pdsl);
1097
1098         dsl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsl));
1099
1100         if (dsl == NULL) {
1101                 WARN("out of memory\n");
1102                 *pdsl = 0;
1103                 return DSERR_OUTOFMEMORY;
1104         }
1105
1106         dsl->ref = 0;
1107         dsl->lpVtbl = &ds3dlvt;
1108
1109         dsl->dsound = This->dsound;
1110
1111         dsl->dsound->ds3dl.dwSize = sizeof(DS3DLISTENER);
1112         dsl->dsound->ds3dl.vPosition.x = 0.0;
1113         dsl->dsound->ds3dl.vPosition.y = 0.0;
1114         dsl->dsound->ds3dl.vPosition.z = 0.0;
1115         dsl->dsound->ds3dl.vVelocity.x = 0.0;
1116         dsl->dsound->ds3dl.vVelocity.y = 0.0;
1117         dsl->dsound->ds3dl.vVelocity.z = 0.0;
1118         dsl->dsound->ds3dl.vOrientFront.x = 0.0;
1119         dsl->dsound->ds3dl.vOrientFront.y = 0.0;
1120         dsl->dsound->ds3dl.vOrientFront.z = 1.0;
1121         dsl->dsound->ds3dl.vOrientTop.x = 0.0;
1122         dsl->dsound->ds3dl.vOrientTop.y = 1.0;
1123         dsl->dsound->ds3dl.vOrientTop.z = 0.0;
1124         dsl->dsound->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1125         dsl->dsound->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1126         dsl->dsound->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1127
1128         dsl->dsound->ds3dl_need_recalc = TRUE;
1129
1130         *pdsl = dsl;
1131         return S_OK;
1132 }