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