dsound: Move resampling away from mixer thread.
[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 if (ds3db_ds3db.vVelocity != dsb->device->ds3dl.vVelocity)
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                 DSOUND_RecalcFormat(dsb);
300                 DSOUND_MixToTemporary(dsb, 0, dsb->buflen);
301         }
302 #endif  
303         
304         /* time for remix */
305         DSOUND_RecalcVolPan(&dsb->volpan);
306 }
307
308 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
309 {
310         TRACE("(%p)\n",dsb);
311
312         DSOUND_Calc3DBuffer(dsb);
313 }
314
315 static void DSOUND_ChangeListener(IDirectSound3DListenerImpl *ds3dl)
316 {
317         int i;
318         TRACE("(%p)\n",ds3dl);
319         for (i = 0; i < ds3dl->device->nrofbuffers; i++)
320         {
321                 /* check if this buffer is waiting for recalculation */
322                 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
323                 {
324                         DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
325                 }
326         }
327 }
328
329 /*******************************************************************************
330  *              IDirectSound3DBuffer
331  */
332
333 /* IUnknown methods */
334 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
335         LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
336 {
337         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
338
339         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
340         return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
341 }
342
343 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
344 {
345     IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
346     ULONG ref = InterlockedIncrement(&(This->ref));
347     TRACE("(%p) ref was %d\n", This, ref - 1);
348     return ref;
349 }
350
351 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
352 {
353     IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
354     ULONG ref = InterlockedDecrement(&(This->ref));
355     TRACE("(%p) ref was %d\n", This, ref + 1);
356
357     if (!ref) {
358         This->dsb->ds3db = NULL;
359         IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
360         HeapFree(GetProcessHeap(), 0, This);
361         TRACE("(%p) released\n", This);
362     }
363     return ref;
364 }
365
366 /* IDirectSound3DBuffer methods */
367 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
368         LPDIRECTSOUND3DBUFFER iface,
369         LPDS3DBUFFER lpDs3dBuffer)
370 {
371         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
372         TRACE("(%p,%p)\n",This,lpDs3dBuffer);
373
374         if (lpDs3dBuffer == NULL) {
375                 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
376                 return DSERR_INVALIDPARAM;
377         }
378
379         if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
380                 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
381                 return DSERR_INVALIDPARAM;
382         }
383         
384         TRACE("returning: all parameters\n");
385         *lpDs3dBuffer = This->dsb->ds3db_ds3db;
386         return DS_OK;
387 }
388
389 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
390         LPDIRECTSOUND3DBUFFER iface,
391         LPDWORD lpdwInsideConeAngle,
392         LPDWORD lpdwOutsideConeAngle)
393 {
394         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
395         TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
396                 This->dsb->ds3db_ds3db.dwInsideConeAngle, This->dsb->ds3db_ds3db.dwOutsideConeAngle);
397         *lpdwInsideConeAngle = This->dsb->ds3db_ds3db.dwInsideConeAngle;
398         *lpdwOutsideConeAngle = This->dsb->ds3db_ds3db.dwOutsideConeAngle;
399         return DS_OK;
400 }
401
402 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
403         LPDIRECTSOUND3DBUFFER iface,
404         LPD3DVECTOR lpvConeOrientation)
405 {
406         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
407         TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
408                 This->dsb->ds3db_ds3db.vConeOrientation.x,
409                 This->dsb->ds3db_ds3db.vConeOrientation.y,
410                 This->dsb->ds3db_ds3db.vConeOrientation.z);
411         *lpvConeOrientation = This->dsb->ds3db_ds3db.vConeOrientation;
412         return DS_OK;
413 }
414
415 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
416         LPDIRECTSOUND3DBUFFER iface,
417         LPLONG lplConeOutsideVolume)
418 {
419         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
420         TRACE("returning: Cone Outside Volume = %d\n", This->dsb->ds3db_ds3db.lConeOutsideVolume);
421         *lplConeOutsideVolume = This->dsb->ds3db_ds3db.lConeOutsideVolume;
422         return DS_OK;
423 }
424
425 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
426         LPDIRECTSOUND3DBUFFER iface,
427         LPD3DVALUE lpfMaxDistance)
428 {
429         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
430         TRACE("returning: Max Distance = %f\n", This->dsb->ds3db_ds3db.flMaxDistance);
431         *lpfMaxDistance = This->dsb->ds3db_ds3db.flMaxDistance;
432         return DS_OK;
433 }
434
435 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
436         LPDIRECTSOUND3DBUFFER iface,
437         LPD3DVALUE lpfMinDistance)
438 {
439         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
440         TRACE("returning: Min Distance = %f\n", This->dsb->ds3db_ds3db.flMinDistance);
441         *lpfMinDistance = This->dsb->ds3db_ds3db.flMinDistance;
442         return DS_OK;
443 }
444
445 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
446         LPDIRECTSOUND3DBUFFER iface,
447         LPDWORD lpdwMode)
448 {
449         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
450         TRACE("returning: Mode = %d\n", This->dsb->ds3db_ds3db.dwMode);
451         *lpdwMode = This->dsb->ds3db_ds3db.dwMode;
452         return DS_OK;
453 }
454
455 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
456         LPDIRECTSOUND3DBUFFER iface,
457         LPD3DVECTOR lpvPosition)
458 {
459         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
460         TRACE("returning: Position vector = (%f,%f,%f)\n",
461                 This->dsb->ds3db_ds3db.vPosition.x,
462                 This->dsb->ds3db_ds3db.vPosition.y,
463                 This->dsb->ds3db_ds3db.vPosition.z);
464         *lpvPosition = This->dsb->ds3db_ds3db.vPosition;
465         return DS_OK;
466 }
467
468 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
469         LPDIRECTSOUND3DBUFFER iface,
470         LPD3DVECTOR lpvVelocity)
471 {
472         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
473         TRACE("returning: Velocity vector = (%f,%f,%f)\n",
474                 This->dsb->ds3db_ds3db.vVelocity.x,
475                 This->dsb->ds3db_ds3db.vVelocity.y,
476                 This->dsb->ds3db_ds3db.vVelocity.z);
477         *lpvVelocity = This->dsb->ds3db_ds3db.vVelocity;
478         return DS_OK;
479 }
480
481 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
482         LPDIRECTSOUND3DBUFFER iface,
483         LPCDS3DBUFFER lpcDs3dBuffer,
484         DWORD dwApply)
485 {
486         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
487         DWORD status = DSERR_INVALIDPARAM;
488         TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
489
490         if (lpcDs3dBuffer == NULL) {
491                 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
492                 return status;
493         }
494
495         if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
496                 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
497                 return status;
498         }
499
500         TRACE("setting: all parameters; dwApply = %d\n", dwApply);
501         This->dsb->ds3db_ds3db = *lpcDs3dBuffer;
502
503         if (dwApply == DS3D_IMMEDIATE)
504         {
505                 DSOUND_Mix3DBuffer(This->dsb);
506         }
507         This->dsb->ds3db_need_recalc = TRUE;
508         status = DS_OK;
509
510         return status;
511 }
512
513 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
514         LPDIRECTSOUND3DBUFFER iface,
515         DWORD dwInsideConeAngle,
516         DWORD dwOutsideConeAngle,
517         DWORD dwApply)
518 {
519         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
520         TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
521                 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
522         This->dsb->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
523         This->dsb->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
524         if (dwApply == DS3D_IMMEDIATE)
525         {
526                 DSOUND_Mix3DBuffer(This->dsb);
527         }
528         This->dsb->ds3db_need_recalc = TRUE;
529         return DS_OK;
530 }
531
532 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
533         LPDIRECTSOUND3DBUFFER iface,
534         D3DVALUE x, D3DVALUE y, D3DVALUE z,
535         DWORD dwApply)
536 {
537         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
538         TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
539         This->dsb->ds3db_ds3db.vConeOrientation.x = x;
540         This->dsb->ds3db_ds3db.vConeOrientation.y = y;
541         This->dsb->ds3db_ds3db.vConeOrientation.z = z;
542         if (dwApply == DS3D_IMMEDIATE)
543         {
544                 This->dsb->ds3db_need_recalc = FALSE;
545                 DSOUND_Mix3DBuffer(This->dsb);
546         }
547         This->dsb->ds3db_need_recalc = TRUE;
548         return DS_OK;
549 }
550
551 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
552         LPDIRECTSOUND3DBUFFER iface,
553         LONG lConeOutsideVolume,
554         DWORD dwApply)
555 {
556         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
557         TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
558         This->dsb->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
559         if (dwApply == DS3D_IMMEDIATE)
560         {
561                 This->dsb->ds3db_need_recalc = FALSE;
562                 DSOUND_Mix3DBuffer(This->dsb);
563         }
564         This->dsb->ds3db_need_recalc = TRUE;
565         return DS_OK;
566 }
567
568 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
569         LPDIRECTSOUND3DBUFFER iface,
570         D3DVALUE fMaxDistance,
571         DWORD dwApply)
572 {
573         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
574         TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
575         This->dsb->ds3db_ds3db.flMaxDistance = fMaxDistance;
576         if (dwApply == DS3D_IMMEDIATE)
577         {
578                 This->dsb->ds3db_need_recalc = FALSE;
579                 DSOUND_Mix3DBuffer(This->dsb);
580         }
581         This->dsb->ds3db_need_recalc = TRUE;
582         return DS_OK;
583 }
584
585 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
586         LPDIRECTSOUND3DBUFFER iface,
587         D3DVALUE fMinDistance,
588         DWORD dwApply)
589 {
590         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
591         TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
592         This->dsb->ds3db_ds3db.flMinDistance = fMinDistance;
593         if (dwApply == DS3D_IMMEDIATE)
594         {
595                 This->dsb->ds3db_need_recalc = FALSE;
596                 DSOUND_Mix3DBuffer(This->dsb);
597         }
598         This->dsb->ds3db_need_recalc = TRUE;
599         return DS_OK;
600 }
601
602 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
603         LPDIRECTSOUND3DBUFFER iface,
604         DWORD dwMode,
605         DWORD dwApply)
606 {
607         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
608         TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
609         This->dsb->ds3db_ds3db.dwMode = dwMode;
610         if (dwApply == DS3D_IMMEDIATE)
611         {
612                 This->dsb->ds3db_need_recalc = FALSE;
613                 DSOUND_Mix3DBuffer(This->dsb);
614         }
615         This->dsb->ds3db_need_recalc = TRUE;
616         return DS_OK;
617 }
618
619 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
620         LPDIRECTSOUND3DBUFFER iface,
621         D3DVALUE x, D3DVALUE y, D3DVALUE z,
622         DWORD dwApply)
623 {
624         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
625         TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
626         This->dsb->ds3db_ds3db.vPosition.x = x;
627         This->dsb->ds3db_ds3db.vPosition.y = y;
628         This->dsb->ds3db_ds3db.vPosition.z = z;
629         if (dwApply == DS3D_IMMEDIATE)
630         {
631                 This->dsb->ds3db_need_recalc = FALSE;
632                 DSOUND_Mix3DBuffer(This->dsb);
633         }
634         This->dsb->ds3db_need_recalc = TRUE;
635         return DS_OK;
636 }
637
638 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
639         LPDIRECTSOUND3DBUFFER iface,
640         D3DVALUE x, D3DVALUE y, D3DVALUE z,
641         DWORD dwApply)
642 {
643         IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
644         TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
645         This->dsb->ds3db_ds3db.vVelocity.x = x;
646         This->dsb->ds3db_ds3db.vVelocity.y = y;
647         This->dsb->ds3db_ds3db.vVelocity.z = z;
648         if (dwApply == DS3D_IMMEDIATE)
649         {
650                 This->dsb->ds3db_need_recalc = FALSE;
651                 DSOUND_Mix3DBuffer(This->dsb);
652         }
653         This->dsb->ds3db_need_recalc = TRUE;
654         return DS_OK;
655 }
656
657 static const IDirectSound3DBufferVtbl ds3dbvt =
658 {
659         /* IUnknown methods */
660         IDirectSound3DBufferImpl_QueryInterface,
661         IDirectSound3DBufferImpl_AddRef,
662         IDirectSound3DBufferImpl_Release,
663         /* IDirectSound3DBuffer methods */
664         IDirectSound3DBufferImpl_GetAllParameters,
665         IDirectSound3DBufferImpl_GetConeAngles,
666         IDirectSound3DBufferImpl_GetConeOrientation,
667         IDirectSound3DBufferImpl_GetConeOutsideVolume,
668         IDirectSound3DBufferImpl_GetMaxDistance,
669         IDirectSound3DBufferImpl_GetMinDistance,
670         IDirectSound3DBufferImpl_GetMode,
671         IDirectSound3DBufferImpl_GetPosition,
672         IDirectSound3DBufferImpl_GetVelocity,
673         IDirectSound3DBufferImpl_SetAllParameters,
674         IDirectSound3DBufferImpl_SetConeAngles,
675         IDirectSound3DBufferImpl_SetConeOrientation,
676         IDirectSound3DBufferImpl_SetConeOutsideVolume,
677         IDirectSound3DBufferImpl_SetMaxDistance,
678         IDirectSound3DBufferImpl_SetMinDistance,
679         IDirectSound3DBufferImpl_SetMode,
680         IDirectSound3DBufferImpl_SetPosition,
681         IDirectSound3DBufferImpl_SetVelocity,
682 };
683
684 HRESULT IDirectSound3DBufferImpl_Create(
685         IDirectSoundBufferImpl *dsb,
686         IDirectSound3DBufferImpl **pds3db)
687 {
688         IDirectSound3DBufferImpl *ds3db;
689         TRACE("(%p,%p)\n",dsb,pds3db);
690
691         ds3db = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*ds3db));
692
693         if (ds3db == NULL) {
694                 WARN("out of memory\n");
695                 *pds3db = 0;
696                 return DSERR_OUTOFMEMORY;
697         }
698
699         ds3db->ref = 0;
700         ds3db->dsb = dsb;
701         ds3db->lpVtbl = &ds3dbvt;
702
703         ds3db->dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
704         ds3db->dsb->ds3db_ds3db.vPosition.x = 0.0;
705         ds3db->dsb->ds3db_ds3db.vPosition.y = 0.0;
706         ds3db->dsb->ds3db_ds3db.vPosition.z = 0.0;
707         ds3db->dsb->ds3db_ds3db.vVelocity.x = 0.0;
708         ds3db->dsb->ds3db_ds3db.vVelocity.y = 0.0;
709         ds3db->dsb->ds3db_ds3db.vVelocity.z = 0.0;
710         ds3db->dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
711         ds3db->dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
712         ds3db->dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
713         ds3db->dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
714         ds3db->dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
715         ds3db->dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
716         ds3db->dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
717         ds3db->dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
718         ds3db->dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
719
720         ds3db->dsb->ds3db_need_recalc = TRUE;
721
722         IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
723
724         *pds3db = ds3db;
725         return S_OK;
726 }
727
728 HRESULT IDirectSound3DBufferImpl_Destroy(
729     IDirectSound3DBufferImpl *pds3db)
730 {
731     TRACE("(%p)\n",pds3db);
732
733     while (IDirectSound3DBufferImpl_Release((LPDIRECTSOUND3DBUFFER)pds3db) > 0);
734
735     return S_OK;
736 }
737
738 /*******************************************************************************
739  *            IDirectSound3DListener
740  */
741
742 /* IUnknown methods */
743 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
744         LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
745 {
746         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
747
748         TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
749
750         if (ppobj == NULL) {
751                 WARN("invalid parameter\n");
752                 return E_INVALIDARG;
753         }
754
755         *ppobj = NULL;  /* assume failure */
756
757         if ( IsEqualGUID(riid, &IID_IUnknown) ||
758              IsEqualGUID(riid, &IID_IDirectSound3DListener ) ) {
759                 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This);
760                 *ppobj = This;
761                 return S_OK;
762         }
763
764         if ( IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
765                 if (!This->device->primary)
766                         PrimaryBufferImpl_Create(This->device, &(This->device->primary), &(This->device->dsbd));
767                 if (This->device->primary) {
768                         *ppobj = This->device->primary;
769                         IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)*ppobj);
770                         return S_OK;
771                 }
772         }
773
774         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
775         return E_NOINTERFACE;
776 }
777
778 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
779 {
780     IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
781     ULONG ref = InterlockedIncrement(&(This->ref));
782     TRACE("(%p) ref was %d\n", This, ref - 1);
783     return ref;
784 }
785
786 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
787 {
788     IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
789     ULONG ref = InterlockedDecrement(&(This->ref));
790     TRACE("(%p) ref was %d\n", This, ref + 1);
791
792     if (!ref) {
793         This->device->listener = 0;
794         HeapFree(GetProcessHeap(), 0, This);
795         TRACE("(%p) released\n", This);
796     }
797     return ref;
798 }
799
800 /* IDirectSound3DListener methods */
801 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
802         LPDIRECTSOUND3DLISTENER iface,
803         LPDS3DLISTENER lpDS3DL)
804 {
805         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
806         TRACE("(%p,%p)\n",This,lpDS3DL);
807
808         if (lpDS3DL == NULL) {
809                 WARN("invalid parameter: lpDS3DL == NULL\n");
810                 return DSERR_INVALIDPARAM;
811         }
812
813         if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
814                 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
815                 return DSERR_INVALIDPARAM;
816         }
817         
818         TRACE("returning: all parameters\n");
819         *lpDS3DL = This->device->ds3dl;
820         return DS_OK;
821 }
822
823 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
824         LPDIRECTSOUND3DLISTENER iface,
825         LPD3DVALUE lpfDistanceFactor)
826 {
827         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
828         TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
829         *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
830         return DS_OK;
831 }
832
833 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
834         LPDIRECTSOUND3DLISTENER iface,
835         LPD3DVALUE lpfDopplerFactor)
836 {
837         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
838         TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
839         *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
840         return DS_OK;
841 }
842
843 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
844         LPDIRECTSOUND3DLISTENER iface,
845         LPD3DVECTOR lpvOrientFront,
846         LPD3DVECTOR lpvOrientTop)
847 {
848         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
849         TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
850         This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
851         This->device->ds3dl.vOrientTop.z);
852         *lpvOrientFront = This->device->ds3dl.vOrientFront;
853         *lpvOrientTop = This->device->ds3dl.vOrientTop;
854         return DS_OK;
855 }
856
857 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
858         LPDIRECTSOUND3DLISTENER iface,
859         LPD3DVECTOR lpvPosition)
860 {
861         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
862         TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
863         *lpvPosition = This->device->ds3dl.vPosition;
864         return DS_OK;
865 }
866
867 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
868         LPDIRECTSOUND3DLISTENER iface,
869         LPD3DVALUE lpfRolloffFactor)
870 {
871         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
872         TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
873         *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
874         return DS_OK;
875 }
876
877 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
878         LPDIRECTSOUND3DLISTENER iface,
879         LPD3DVECTOR lpvVelocity)
880 {
881         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
882         TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
883         *lpvVelocity = This->device->ds3dl.vVelocity;
884         return DS_OK;
885 }
886
887 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
888         LPDIRECTSOUND3DLISTENER iface,
889         LPCDS3DLISTENER lpcDS3DL,
890         DWORD dwApply)
891 {
892         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
893         TRACE("setting: all parameters; dwApply = %d\n", dwApply);
894         This->device->ds3dl = *lpcDS3DL;
895         if (dwApply == DS3D_IMMEDIATE)
896         {
897                 This->device->ds3dl_need_recalc = FALSE;
898                 DSOUND_ChangeListener(This);
899         }
900         This->device->ds3dl_need_recalc = TRUE;
901         return DS_OK;
902 }
903
904 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
905         LPDIRECTSOUND3DLISTENER iface,
906         D3DVALUE fDistanceFactor,
907         DWORD dwApply)
908 {
909         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
910         TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
911         This->device->ds3dl.flDistanceFactor = fDistanceFactor;
912         if (dwApply == DS3D_IMMEDIATE)
913         {
914                 This->device->ds3dl_need_recalc = FALSE;
915                 DSOUND_ChangeListener(This);
916         }
917         This->device->ds3dl_need_recalc = TRUE;
918         return DS_OK;
919 }
920
921 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
922         LPDIRECTSOUND3DLISTENER iface,
923         D3DVALUE fDopplerFactor,
924         DWORD dwApply)
925 {
926         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
927         TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
928         This->device->ds3dl.flDopplerFactor = fDopplerFactor;
929         if (dwApply == DS3D_IMMEDIATE)
930         {
931                 This->device->ds3dl_need_recalc = FALSE;
932                 DSOUND_ChangeListener(This);
933         }
934         This->device->ds3dl_need_recalc = TRUE;
935         return DS_OK;
936 }
937
938 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
939         LPDIRECTSOUND3DLISTENER iface,
940         D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
941         D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
942         DWORD dwApply)
943 {
944         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
945         TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
946         xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
947         This->device->ds3dl.vOrientFront.x = xFront;
948         This->device->ds3dl.vOrientFront.y = yFront;
949         This->device->ds3dl.vOrientFront.z = zFront;
950         This->device->ds3dl.vOrientTop.x = xTop;
951         This->device->ds3dl.vOrientTop.y = yTop;
952         This->device->ds3dl.vOrientTop.z = zTop;
953         if (dwApply == DS3D_IMMEDIATE)
954         {
955                 This->device->ds3dl_need_recalc = FALSE;
956                 DSOUND_ChangeListener(This);
957         }
958         This->device->ds3dl_need_recalc = TRUE;
959         return DS_OK;
960 }
961
962 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
963         LPDIRECTSOUND3DLISTENER iface,
964         D3DVALUE x, D3DVALUE y, D3DVALUE z,
965         DWORD dwApply)
966 {
967         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
968         TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
969         This->device->ds3dl.vPosition.x = x;
970         This->device->ds3dl.vPosition.y = y;
971         This->device->ds3dl.vPosition.z = z;
972         if (dwApply == DS3D_IMMEDIATE)
973         {
974                 This->device->ds3dl_need_recalc = FALSE;
975                 DSOUND_ChangeListener(This);
976         }
977         This->device->ds3dl_need_recalc = TRUE;
978         return DS_OK;
979 }
980
981 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
982         LPDIRECTSOUND3DLISTENER iface,
983         D3DVALUE fRolloffFactor,
984         DWORD dwApply)
985 {
986         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
987         TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
988         This->device->ds3dl.flRolloffFactor = fRolloffFactor;
989         if (dwApply == DS3D_IMMEDIATE)
990         {
991                 This->device->ds3dl_need_recalc = FALSE;
992                 DSOUND_ChangeListener(This);
993         }
994         This->device->ds3dl_need_recalc = TRUE;
995         return DS_OK;
996 }
997
998 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
999         LPDIRECTSOUND3DLISTENER iface,
1000         D3DVALUE x, D3DVALUE y, D3DVALUE z,
1001         DWORD dwApply)
1002 {
1003         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1004         TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
1005         This->device->ds3dl.vVelocity.x = x;
1006         This->device->ds3dl.vVelocity.y = y;
1007         This->device->ds3dl.vVelocity.z = z;
1008         if (dwApply == DS3D_IMMEDIATE)
1009         {
1010                 This->device->ds3dl_need_recalc = FALSE;
1011                 DSOUND_ChangeListener(This);
1012         }
1013         This->device->ds3dl_need_recalc = TRUE;
1014         return DS_OK;
1015 }
1016
1017 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
1018         LPDIRECTSOUND3DLISTENER iface)
1019 {
1020         IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1021         TRACE("\n");
1022         DSOUND_ChangeListener(This);
1023         return DS_OK;
1024 }
1025
1026 static const IDirectSound3DListenerVtbl ds3dlvt =
1027 {
1028         /* IUnknown methods */
1029         IDirectSound3DListenerImpl_QueryInterface,
1030         IDirectSound3DListenerImpl_AddRef,
1031         IDirectSound3DListenerImpl_Release,
1032         /* IDirectSound3DListener methods */
1033         IDirectSound3DListenerImpl_GetAllParameter,
1034         IDirectSound3DListenerImpl_GetDistanceFactor,
1035         IDirectSound3DListenerImpl_GetDopplerFactor,
1036         IDirectSound3DListenerImpl_GetOrientation,
1037         IDirectSound3DListenerImpl_GetPosition,
1038         IDirectSound3DListenerImpl_GetRolloffFactor,
1039         IDirectSound3DListenerImpl_GetVelocity,
1040         IDirectSound3DListenerImpl_SetAllParameters,
1041         IDirectSound3DListenerImpl_SetDistanceFactor,
1042         IDirectSound3DListenerImpl_SetDopplerFactor,
1043         IDirectSound3DListenerImpl_SetOrientation,
1044         IDirectSound3DListenerImpl_SetPosition,
1045         IDirectSound3DListenerImpl_SetRolloffFactor,
1046         IDirectSound3DListenerImpl_SetVelocity,
1047         IDirectSound3DListenerImpl_CommitDeferredSettings,
1048 };
1049
1050 HRESULT IDirectSound3DListenerImpl_Create(
1051         DirectSoundDevice * device,
1052         IDirectSound3DListenerImpl ** ppdsl)
1053 {
1054         IDirectSound3DListenerImpl *pdsl;
1055         TRACE("(%p,%p)\n",device,ppdsl);
1056
1057         pdsl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*pdsl));
1058
1059         if (pdsl == NULL) {
1060                 WARN("out of memory\n");
1061                 *ppdsl = 0;
1062                 return DSERR_OUTOFMEMORY;
1063         }
1064
1065         pdsl->ref = 0;
1066         pdsl->lpVtbl = &ds3dlvt;
1067
1068         pdsl->device = device;
1069
1070         pdsl->device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1071         pdsl->device->ds3dl.vPosition.x = 0.0;
1072         pdsl->device->ds3dl.vPosition.y = 0.0;
1073         pdsl->device->ds3dl.vPosition.z = 0.0;
1074         pdsl->device->ds3dl.vVelocity.x = 0.0;
1075         pdsl->device->ds3dl.vVelocity.y = 0.0;
1076         pdsl->device->ds3dl.vVelocity.z = 0.0;
1077         pdsl->device->ds3dl.vOrientFront.x = 0.0;
1078         pdsl->device->ds3dl.vOrientFront.y = 0.0;
1079         pdsl->device->ds3dl.vOrientFront.z = 1.0;
1080         pdsl->device->ds3dl.vOrientTop.x = 0.0;
1081         pdsl->device->ds3dl.vOrientTop.y = 1.0;
1082         pdsl->device->ds3dl.vOrientTop.z = 0.0;
1083         pdsl->device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1084         pdsl->device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1085         pdsl->device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1086
1087         pdsl->device->ds3dl_need_recalc = TRUE;
1088
1089         *ppdsl = pdsl;
1090         return S_OK;
1091 }