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