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