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