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