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