Release 20030408.
[wine/gsoc-2012-control.git] / dlls / dsound / sound3d.c
blobba386665f455a698b4b37a1331d069264628cb2d
1 /* DirectSound
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>
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.
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Most thread locking is complete. There may be a few race
24 * conditions still lurking.
26 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
27 * and a Turtle Beach Tropez+.
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
40 #include "config.h"
41 #include <assert.h>
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <sys/fcntl.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #include <stdlib.h>
49 #include <string.h>
50 #include <math.h> /* Insomnia - pow() function */
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
54 #include "windef.h"
55 #include "winbase.h"
56 #include "wingdi.h"
57 #include "winuser.h"
58 #include "winerror.h"
59 #include "mmsystem.h"
60 #include "winternl.h"
61 #include "mmddk.h"
62 #include "wine/windef16.h"
63 #include "wine/debug.h"
64 #include "dsound.h"
65 #include "dsdriver.h"
66 #include "dsound_private.h"
68 /* default intensity level for human ears */
69 #define DEFAULT_INTENSITY 0.000000000001f
70 /* default velocity of sound in the air */
71 #define DEFAULT_VELOCITY 340
73 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
75 /*******************************************************************************
76 * Auxiliary functions
79 /* scalar product (i believe it's called dot product in english) */
80 static inline D3DVALUE ScalarProduct (LPD3DVECTOR a, LPD3DVECTOR b)
82 D3DVALUE c;
83 c = (a->u1.x*b->u1.x) + (a->u2.y*b->u2.y) + (a->u3.z*b->u3.z);
84 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->u1.x, a->u2.y, a->u3.z, b->u1.x, b->u2.y, \
85 b->u3.z, c);
86 return c;
89 /* vector product (i believe it's called cross product in english */
90 static inline D3DVECTOR VectorProduct (LPD3DVECTOR a, LPD3DVECTOR b)
92 D3DVECTOR c;
93 c.u1.x = (a->u2.y*b->u3.z) - (a->u3.z*b->u2.y);
94 c.u2.y = (a->u3.z*b->u1.x) - (a->u1.x*b->u3.z);
95 c.u3.z = (a->u1.x*b->u2.y) - (a->u2.y*b->u1.x);
96 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->u1.x, a->u2.y, a->u3.z, b->u1.x, b->u2.y, \
97 b->u3.z, c.u1.x, c.u2.y, c.u3.z);
98 return c;
101 /* magnitude (lenght) of vector */
102 static inline D3DVALUE VectorMagnitude (LPD3DVECTOR a)
104 D3DVALUE l;
105 l = sqrt (ScalarProduct (a, a));
106 TRACE("|(%f,%f,%f)| = %f\n", a->u1.x, a->u2.y, a->u3.z, l);
107 return l;
110 /* conversion between radians and degrees */
111 static inline D3DVALUE RadToDeg (D3DVALUE angle)
113 D3DVALUE newangle;
114 newangle = angle * (360/(2*M_PI));
115 TRACE("%f rad = %f deg\n", angle, newangle);
116 return newangle;
119 /* conversion between degrees and radians */
120 static inline D3DVALUE DegToRad (D3DVALUE angle)
122 D3DVALUE newangle;
123 newangle = angle * (2*M_PI/360);
124 TRACE("%f deg = %f rad\n", angle, newangle);
125 return newangle;
128 /* angle between vectors - deg version */
129 static inline D3DVALUE AngleBetweenVectorsDeg (LPD3DVECTOR a, LPD3DVECTOR b)
131 D3DVALUE la, lb, product, angle, cos;
132 /* definition of scalar product: a*b = |a|*|b|*cos...therefore: */
133 product = ScalarProduct (a,b);
134 la = VectorMagnitude (a);
135 lb = VectorMagnitude (b);
136 cos = product/(la*lb);
137 angle = acos(cos);
138 /* we now have angle in radians */
139 angle = RadToDeg(angle);
140 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f degrees\n", a->u1.x, a->u2.y, a->u3.z, b->u1.x,
141 b->u2.y, b->u3.z, angle);
142 return angle;
145 /* angle between vectors - rad version */
146 static inline D3DVALUE AngleBetweenVectorsRad (LPD3DVECTOR a, LPD3DVECTOR b)
148 D3DVALUE la, lb, product, angle, cos;
149 /* definition of scalar product: a*b = |a|*|b|*cos...therefore: */
150 product = ScalarProduct (a,b);
151 la = VectorMagnitude (a);
152 lb = VectorMagnitude (b);
153 cos = product/(la*lb);
154 angle = acos(cos);
155 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians\n", a->u1.x, a->u2.y, a->u3.z, b->u1.x,
156 b->u2.y, b->u3.z, angle);
157 return angle;
160 /* calculates vector between two points */
161 static inline D3DVECTOR VectorBetweenTwoPoints (LPD3DVECTOR a, LPD3DVECTOR b)
163 D3DVECTOR c;
164 c.u1.x = b->u1.x - a->u1.x;
165 c.u2.y = b->u2.y - a->u2.y;
166 c.u3.z = b->u3.z - a->u3.z;
167 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->u1.x, a->u2.y, a->u3.z, b->u1.x, b->u2.y,
168 b->u3.z, c.u1.x, c.u2.y, c.u3.z);
169 return c;
172 /* calculates the lenght of vector's projection on another vector */
173 static inline D3DVALUE ProjectVector (LPD3DVECTOR a, LPD3DVECTOR p)
175 D3DVALUE prod, result;
176 prod = ScalarProduct(a, p);
177 result = prod/VectorMagnitude(p);
178 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->u1.x, a->u2.y, a->u3.z, p->u1.x,
179 p->u2.y, p->u3.z, result);
180 return result;
183 /*******************************************************************************
184 * 3D Buffer and Listener mixing
187 static void WINAPI DSOUND_Mix3DBuffer(IDirectSound3DBufferImpl *ds3db)
189 IDirectSound3DListenerImpl *dsl;
191 /* volume, at which the sound will be played after all calcs. */
192 D3DVALUE lVolume = 0;
193 /* intensity (used for distance related stuff) */
194 double flIntensity;
195 double flTemp;
196 /* stuff for distance related stuff calc. */
197 D3DVECTOR vDistance;
198 D3DVALUE flDistance = 0;
199 /* panning related stuff */
200 D3DVALUE flAngle;
201 D3DVECTOR vLeft;
202 /* doppler shift related stuff */
203 #if 0
204 D3DVALUE flFreq, flBufferVel, flListenerVel;
205 #endif
207 if (ds3db->dsb->dsound->listener == NULL)
208 return;
209 dsl = ds3db->dsb->dsound->listener;
211 /* initial buffer volume */
212 lVolume = ds3db->lVolume;
214 switch (ds3db->ds3db.dwMode)
216 case DS3DMODE_DISABLE:
217 TRACE("3D processing disabled\n");
218 /* this one is here only to eliminate annoying warning message */
219 DSOUND_RecalcVolPan (&ds3db->dsb->volpan);
220 DSOUND_ForceRemix (ds3db->dsb);
221 break;
222 case DS3DMODE_NORMAL:
223 TRACE("Normal 3D processing mode\n");
224 /* we need to calculate distance between buffer and listener*/
225 vDistance = VectorBetweenTwoPoints(&ds3db->ds3db.vPosition, &dsl->ds3dl.vPosition);
226 flDistance = VectorMagnitude (&vDistance);
227 break;
228 case DS3DMODE_HEADRELATIVE:
229 TRACE("Head-relative 3D processing mode\n");
230 /* distance between buffer and listener is same as buffer's position */
231 flDistance = VectorMagnitude (&ds3db->ds3db.vPosition);
232 break;
235 if (flDistance > ds3db->ds3db.flMaxDistance)
237 /* some apps don't want you to hear too distant sounds... */
238 if (ds3db->dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
240 ds3db->dsb->volpan.lVolume = DSBVOLUME_MIN;
241 DSOUND_RecalcVolPan (&ds3db->dsb->volpan);
242 /* i guess mixing here would be a waste of power */
243 return;
245 else
246 flDistance = ds3db->ds3db.flMaxDistance;
248 if (flDistance < ds3db->ds3db.flMinDistance)
249 flDistance = ds3db->ds3db.flMinDistance;
251 /* the following formula is taken from my physics book. I think it's ok for the *real* world...i hope m$ does it that way */
252 lVolume += 10000; /* ms likes working with negative volume...i don't */
253 lVolume /= 1000; /* convert hundreths of dB into B */
254 /* intensity level (loudness) = log10(Intensity/DefaultIntensity)...therefore */
255 flIntensity = pow(10,lVolume)*DEFAULT_INTENSITY;
256 flTemp = (flDistance/ds3db->ds3db.flMinDistance)*(flDistance/ds3db->ds3db.flMinDistance);
257 flIntensity /= flTemp;
258 lVolume = log10(flIntensity/DEFAULT_INTENSITY);
259 lVolume *= 1000; /* convert back to hundreths of dB */
260 lVolume -= 10000; /* we need to do it in ms way */
261 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %ld to %f\n", flDistance, ds3db->ds3db.flMinDistance, ds3db->lVolume, lVolume);
263 /* conning */
264 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
265 if (ds3db->ds3db.vConeOrientation.u1.x == 0 && ds3db->ds3db.vConeOrientation.u2.y == 0 && ds3db->ds3db.vConeOrientation.u3.z == 0)
267 TRACE("conning: cones not set\n");
269 else
271 /* calculate angle */
272 flAngle = AngleBetweenVectorsDeg(&ds3db->ds3db.vConeOrientation, &vDistance);
273 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
274 if (ds3db->ds3db.dwInsideConeAngle != ds3db->ds3db.dwOutsideConeAngle)
276 /* my test show that for my way of calc., we need only half of angles */
277 DWORD dwInsideConeAngle = ds3db->ds3db.dwInsideConeAngle/2;
278 DWORD dwOutsideConeAngle = ds3db->ds3db.dwOutsideConeAngle/2;
279 /* full volume */
280 if (flAngle < dwInsideConeAngle)
281 flAngle = dwInsideConeAngle;
282 /* min (app defined) volume */
283 if (flAngle > dwOutsideConeAngle)
284 flAngle = dwOutsideConeAngle;
285 /* this probably isn't the right thing, but it's ok for the time being */
286 lVolume += ((ds3db->ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
288 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %ld deg; OutsideConeAngle(/2) = %ld deg; ConeOutsideVolume = %ld => adjusting volume to %f\n",
289 flAngle, ds3db->ds3db.dwInsideConeAngle/2, ds3db->ds3db.dwOutsideConeAngle/2, ds3db->ds3db.lConeOutsideVolume, lVolume);
291 ds3db->dsb->volpan.lVolume = lVolume;
293 /* panning */
294 vDistance = VectorBetweenTwoPoints(&dsl->ds3dl.vPosition, &ds3db->ds3db.vPosition);
295 vLeft = VectorProduct(&dsl->ds3dl.vOrientFront, &dsl->ds3dl.vOrientTop);
296 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
297 /* for now, we'll use "linear formula" (which is probably incorrect); if someone has it in book, correct it */
298 ds3db->dsb->volpan.lPan = 10000*2*flAngle/M_PI - 10000;
299 TRACE("panning: Angle = %f rad, lPan = %ld\n", flAngle, ds3db->dsb->volpan.lPan);
301 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
302 #if 0
303 /* doppler shift*/
304 if ((VectorMagnitude(&ds3db->ds3db.vVelocity) == 0) && (VectorMagnitude(&dsl->ds3dl.vVelocity) == 0))
306 TRACE("doppler: Buffer and Listener don't have velocities\n");
308 else
310 /* calculate lenght of ds3db.vVelocity component which causes Doppler Effect
311 NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
312 if buffer moves AWAY from listener, it's velocity component is POSITIVE */
313 flBufferVel = ProjectVector(&ds3db->ds3db.vVelocity, &vDistance);
314 /* calculate lenght of ds3dl.vVelocity component which causes Doppler Effect
315 NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
316 if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
317 flListenerVel = ProjectVector(&dsl->ds3dl.vVelocity, &vDistance);
318 /* formula taken from Gianicoli D.: Physics, 4th edition: */
319 /* FIXME: replace ds3db->dsb->freq with appropriate frequency ! */
320 flFreq = ds3db->dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
321 TRACE("doppler: Buffer velocity (component) = %lf, Listener velocity (component) = %lf => Doppler shift: %ld Hz -> %lf Hz\n", flBufferVel, flListenerVel, \
322 ds3db->dsb->freq, flFreq);
323 /* FIXME: replace following line with correct frequency setting ! */
324 ds3db->dsb->freq = flFreq;
326 #endif
328 /* time for remix */
329 DSOUND_RecalcVolPan (&ds3db->dsb->volpan);
330 DSOUND_ForceRemix (ds3db->dsb);
333 static void WINAPI DSOUND_ChangeListener(IDirectSound3DListenerImpl *ds3dl)
335 int i;
336 for (i = 0; i < ds3dl->dsb->dsound->nrofbuffers; i++)
338 /* some buffers don't have 3d buffer (Ultima IX seems to
339 crash without the following line) */
340 if (ds3dl->dsb->dsound->buffers[i]->ds3db == NULL)
341 continue;
342 if (ds3dl->dsb->dsound->buffers[i]->ds3db->need_recalc == TRUE)
344 DSOUND_Mix3DBuffer(ds3dl->dsb->dsound->buffers[i]->ds3db);
350 /*******************************************************************************
351 * IDirectSound3DBuffer
354 /* IUnknown methods */
355 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
356 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
358 ICOM_THIS(IDirectSound3DBufferImpl,iface);
360 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
361 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
364 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
366 ICOM_THIS(IDirectSound3DBufferImpl,iface);
367 ULONG ulReturn;
369 TRACE("(%p) ref was %ld\n", This, This->ref);
370 ulReturn = InterlockedIncrement(&This->ref);
371 if (ulReturn == 1)
372 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->dsb);
373 return ulReturn;
376 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
378 ICOM_THIS(IDirectSound3DBufferImpl,iface);
379 ULONG ulReturn;
381 TRACE("(%p) ref was %ld\n", This, This->ref);
383 ulReturn = InterlockedDecrement(&This->ref);
384 if(ulReturn)
385 return ulReturn;
387 if (This->dsb) {
388 BOOL std = (This->dsb->dsbd.dwFlags & DSBCAPS_CTRL3D);
390 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
392 if (std)
393 return 0; /* leave it to IDirectSoundBufferImpl_Release */
396 if (This->dsb->ds3db == This) This->dsb->ds3db = NULL;
398 DeleteCriticalSection(&This->lock);
400 HeapFree(GetProcessHeap(),0,This);
402 return 0;
405 /* IDirectSound3DBuffer methods */
406 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
407 LPDIRECTSOUND3DBUFFER iface,
408 LPDS3DBUFFER lpDs3dBuffer)
410 ICOM_THIS(IDirectSound3DBufferImpl,iface);
411 TRACE("returning: all parameters\n");
412 *lpDs3dBuffer = This->ds3db;
413 return DS_OK;
416 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
417 LPDIRECTSOUND3DBUFFER iface,
418 LPDWORD lpdwInsideConeAngle,
419 LPDWORD lpdwOutsideConeAngle)
421 ICOM_THIS(IDirectSound3DBufferImpl,iface);
422 TRACE("returning: Inside Cone Angle = %ld degrees; Outside Cone Angle = %ld degrees\n", This->ds3db.dwInsideConeAngle, This->ds3db.dwOutsideConeAngle);
423 *lpdwInsideConeAngle = This->ds3db.dwInsideConeAngle;
424 *lpdwOutsideConeAngle = This->ds3db.dwOutsideConeAngle;
425 return DS_OK;
428 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
429 LPDIRECTSOUND3DBUFFER iface,
430 LPD3DVECTOR lpvConeOrientation)
432 ICOM_THIS(IDirectSound3DBufferImpl,iface);
433 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n", This->ds3db.vConeOrientation.u1.x, This->ds3db.vConeOrientation.u2.y, This->ds3db.vConeOrientation.u3.z);
434 *lpvConeOrientation = This->ds3db.vConeOrientation;
435 return DS_OK;
438 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
439 LPDIRECTSOUND3DBUFFER iface,
440 LPLONG lplConeOutsideVolume)
442 ICOM_THIS(IDirectSound3DBufferImpl,iface);
443 TRACE("returning: Cone Outside Volume = %ld\n", This->ds3db.lConeOutsideVolume);
444 *lplConeOutsideVolume = This->ds3db.lConeOutsideVolume;
445 return DS_OK;
448 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
449 LPDIRECTSOUND3DBUFFER iface,
450 LPD3DVALUE lpfMaxDistance)
452 ICOM_THIS(IDirectSound3DBufferImpl,iface);
453 TRACE("returning: Max Distance = %f\n", This->ds3db.flMaxDistance);
454 *lpfMaxDistance = This->ds3db.flMaxDistance;
455 return DS_OK;
458 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
459 LPDIRECTSOUND3DBUFFER iface,
460 LPD3DVALUE lpfMinDistance)
462 ICOM_THIS(IDirectSound3DBufferImpl,iface);
463 TRACE("returning: Min Distance = %f\n", This->ds3db.flMinDistance);
464 *lpfMinDistance = This->ds3db.flMinDistance;
465 return DS_OK;
468 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
469 LPDIRECTSOUND3DBUFFER iface,
470 LPDWORD lpdwMode)
472 ICOM_THIS(IDirectSound3DBufferImpl,iface);
473 TRACE("returning: Mode = %ld\n", This->ds3db.dwMode);
474 *lpdwMode = This->ds3db.dwMode;
475 return DS_OK;
478 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
479 LPDIRECTSOUND3DBUFFER iface,
480 LPD3DVECTOR lpvPosition)
482 ICOM_THIS(IDirectSound3DBufferImpl,iface);
483 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3db.vPosition.u1.x, This->ds3db.vPosition.u2.y, This->ds3db.vPosition.u1.x);
484 *lpvPosition = This->ds3db.vPosition;
485 return DS_OK;
488 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
489 LPDIRECTSOUND3DBUFFER iface,
490 LPD3DVECTOR lpvVelocity)
492 ICOM_THIS(IDirectSound3DBufferImpl,iface);
493 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3db.vVelocity.u1.x, This->ds3db.vVelocity.u2.y, This->ds3db.vVelocity.u3.z);
494 *lpvVelocity = This->ds3db.vVelocity;
495 return DS_OK;
498 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
499 LPDIRECTSOUND3DBUFFER iface,
500 LPCDS3DBUFFER lpcDs3dBuffer,
501 DWORD dwApply)
503 ICOM_THIS(IDirectSound3DBufferImpl,iface);
504 TRACE("setting: all parameters; dwApply = %ld\n", dwApply);
505 This->ds3db = *lpcDs3dBuffer;
506 if (dwApply == DS3D_IMMEDIATE)
508 DSOUND_Mix3DBuffer(This);
510 This->need_recalc = TRUE;
511 return DS_OK;
514 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
515 LPDIRECTSOUND3DBUFFER iface,
516 DWORD dwInsideConeAngle,
517 DWORD dwOutsideConeAngle,
518 DWORD dwApply)
520 ICOM_THIS(IDirectSound3DBufferImpl,iface);
521 TRACE("setting: Inside Cone Angle = %ld; Outside Cone Angle = %ld; dwApply = %ld\n", dwInsideConeAngle, dwOutsideConeAngle, dwApply);
522 This->ds3db.dwInsideConeAngle = dwInsideConeAngle;
523 This->ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
524 if (dwApply == DS3D_IMMEDIATE)
526 DSOUND_Mix3DBuffer(This);
528 This->need_recalc = TRUE;
529 return DS_OK;
532 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
533 LPDIRECTSOUND3DBUFFER iface,
534 D3DVALUE x, D3DVALUE y, D3DVALUE z,
535 DWORD dwApply)
537 ICOM_THIS(IDirectSound3DBufferImpl,iface);
538 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
539 This->ds3db.vConeOrientation.u1.x = x;
540 This->ds3db.vConeOrientation.u2.y = y;
541 This->ds3db.vConeOrientation.u3.z = z;
542 if (dwApply == DS3D_IMMEDIATE)
544 This->need_recalc = FALSE;
545 DSOUND_Mix3DBuffer(This);
547 This->need_recalc = TRUE;
548 return DS_OK;
551 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
552 LPDIRECTSOUND3DBUFFER iface,
553 LONG lConeOutsideVolume,
554 DWORD dwApply)
556 ICOM_THIS(IDirectSound3DBufferImpl,iface);
557 TRACE("setting: ConeOutsideVolume = %ld; dwApply = %ld\n", lConeOutsideVolume, dwApply);
558 This->ds3db.lConeOutsideVolume = lConeOutsideVolume;
559 if (dwApply == DS3D_IMMEDIATE)
561 This->need_recalc = FALSE;
562 DSOUND_Mix3DBuffer(This);
564 This->need_recalc = TRUE;
565 return DS_OK;
568 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
569 LPDIRECTSOUND3DBUFFER iface,
570 D3DVALUE fMaxDistance,
571 DWORD dwApply)
573 ICOM_THIS(IDirectSound3DBufferImpl,iface);
574 TRACE("setting: MaxDistance = %f; dwApply = %ld\n", fMaxDistance, dwApply);
575 This->ds3db.flMaxDistance = fMaxDistance;
576 if (dwApply == DS3D_IMMEDIATE)
578 This->need_recalc = FALSE;
579 DSOUND_Mix3DBuffer(This);
581 This->need_recalc = TRUE;
582 return DS_OK;
585 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
586 LPDIRECTSOUND3DBUFFER iface,
587 D3DVALUE fMinDistance,
588 DWORD dwApply)
590 ICOM_THIS(IDirectSound3DBufferImpl,iface);
591 TRACE("setting: MinDistance = %f; dwApply = %ld\n", fMinDistance, dwApply);
592 This->ds3db.flMinDistance = fMinDistance;
593 if (dwApply == DS3D_IMMEDIATE)
595 This->need_recalc = FALSE;
596 DSOUND_Mix3DBuffer(This);
598 This->need_recalc = TRUE;
599 return DS_OK;
602 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
603 LPDIRECTSOUND3DBUFFER iface,
604 DWORD dwMode,
605 DWORD dwApply)
607 ICOM_THIS(IDirectSound3DBufferImpl,iface);
608 TRACE("setting: Mode = %ld; dwApply = %ld\n", dwMode, dwApply);
609 This->ds3db.dwMode = dwMode;
610 if (dwApply == DS3D_IMMEDIATE)
612 This->need_recalc = FALSE;
613 DSOUND_Mix3DBuffer(This);
615 This->need_recalc = TRUE;
616 return DS_OK;
619 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
620 LPDIRECTSOUND3DBUFFER iface,
621 D3DVALUE x, D3DVALUE y, D3DVALUE z,
622 DWORD dwApply)
624 ICOM_THIS(IDirectSound3DBufferImpl,iface);
625 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
626 This->ds3db.vPosition.u1.x = x;
627 This->ds3db.vPosition.u2.y = y;
628 This->ds3db.vPosition.u3.z = z;
629 if (dwApply == DS3D_IMMEDIATE)
631 This->need_recalc = FALSE;
632 DSOUND_Mix3DBuffer(This);
634 This->need_recalc = TRUE;
635 return DS_OK;
638 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
639 LPDIRECTSOUND3DBUFFER iface,
640 D3DVALUE x, D3DVALUE y, D3DVALUE z,
641 DWORD dwApply)
643 ICOM_THIS(IDirectSound3DBufferImpl,iface);
644 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
645 This->ds3db.vVelocity.u1.x = x;
646 This->ds3db.vVelocity.u2.y = y;
647 This->ds3db.vVelocity.u3.z = z;
648 if (dwApply == DS3D_IMMEDIATE)
650 This->need_recalc = FALSE;
651 DSOUND_Mix3DBuffer(This);
653 This->need_recalc = TRUE;
654 return DS_OK;
657 static ICOM_VTABLE(IDirectSound3DBuffer) ds3dbvt =
659 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
660 /* IUnknown methods */
661 IDirectSound3DBufferImpl_QueryInterface,
662 IDirectSound3DBufferImpl_AddRef,
663 IDirectSound3DBufferImpl_Release,
664 /* IDirectSound3DBuffer methods */
665 IDirectSound3DBufferImpl_GetAllParameters,
666 IDirectSound3DBufferImpl_GetConeAngles,
667 IDirectSound3DBufferImpl_GetConeOrientation,
668 IDirectSound3DBufferImpl_GetConeOutsideVolume,
669 IDirectSound3DBufferImpl_GetMaxDistance,
670 IDirectSound3DBufferImpl_GetMinDistance,
671 IDirectSound3DBufferImpl_GetMode,
672 IDirectSound3DBufferImpl_GetPosition,
673 IDirectSound3DBufferImpl_GetVelocity,
674 IDirectSound3DBufferImpl_SetAllParameters,
675 IDirectSound3DBufferImpl_SetConeAngles,
676 IDirectSound3DBufferImpl_SetConeOrientation,
677 IDirectSound3DBufferImpl_SetConeOutsideVolume,
678 IDirectSound3DBufferImpl_SetMaxDistance,
679 IDirectSound3DBufferImpl_SetMinDistance,
680 IDirectSound3DBufferImpl_SetMode,
681 IDirectSound3DBufferImpl_SetPosition,
682 IDirectSound3DBufferImpl_SetVelocity,
685 HRESULT WINAPI IDirectSound3DBufferImpl_Create(
686 IDirectSoundBufferImpl *This,
687 IDirectSound3DBufferImpl **pds3db)
689 IDirectSound3DBufferImpl *ds3db;
691 ds3db = (IDirectSound3DBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*ds3db));
692 ds3db->ref = 0;
693 ds3db->dsb = This;
694 ICOM_VTBL(ds3db) = &ds3dbvt;
695 InitializeCriticalSection(&ds3db->lock);
697 ds3db->ds3db.dwSize = sizeof(DS3DBUFFER);
698 ds3db->ds3db.vPosition.u1.x = 0.0;
699 ds3db->ds3db.vPosition.u2.y = 0.0;
700 ds3db->ds3db.vPosition.u3.z = 0.0;
701 ds3db->ds3db.vVelocity.u1.x = 0.0;
702 ds3db->ds3db.vVelocity.u2.y = 0.0;
703 ds3db->ds3db.vVelocity.u3.z = 0.0;
704 ds3db->ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
705 ds3db->ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
706 ds3db->ds3db.vConeOrientation.u1.x = 0.0;
707 ds3db->ds3db.vConeOrientation.u2.y = 0.0;
708 ds3db->ds3db.vConeOrientation.u3.z = 0.0;
709 ds3db->ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
710 ds3db->ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
711 ds3db->ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
712 ds3db->ds3db.dwMode = DS3DMODE_NORMAL;
714 *pds3db = ds3db;
715 return S_OK;
718 /*******************************************************************************
719 * IDirectSound3DListener
722 /* IUnknown methods */
723 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
724 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
726 ICOM_THIS(IDirectSound3DListenerImpl,iface);
728 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
729 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
732 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
734 ICOM_THIS(IDirectSound3DListenerImpl,iface);
735 return InterlockedIncrement(&This->ref);
738 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
740 ICOM_THIS(IDirectSound3DListenerImpl,iface);
741 ULONG ulReturn;
743 TRACE("(%p) ref was %ld\n", This, This->ref);
745 ulReturn = InterlockedDecrement(&This->ref);
747 /* Free all resources */
748 if( ulReturn == 0 ) {
749 if(This->dsb)
750 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
751 DeleteCriticalSection(&This->lock);
752 HeapFree(GetProcessHeap(),0,This);
755 return ulReturn;
758 /* IDirectSound3DListener methods */
759 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
760 LPDIRECTSOUND3DLISTENER iface,
761 LPDS3DLISTENER lpDS3DL)
763 ICOM_THIS(IDirectSound3DListenerImpl,iface);
764 TRACE("returning: all parameters\n");
765 *lpDS3DL = This->ds3dl;
766 return DS_OK;
769 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
770 LPDIRECTSOUND3DLISTENER iface,
771 LPD3DVALUE lpfDistanceFactor)
773 ICOM_THIS(IDirectSound3DListenerImpl,iface);
774 TRACE("returning: Distance Factor = %f\n", This->ds3dl.flDistanceFactor);
775 *lpfDistanceFactor = This->ds3dl.flDistanceFactor;
776 return DS_OK;
779 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
780 LPDIRECTSOUND3DLISTENER iface,
781 LPD3DVALUE lpfDopplerFactor)
783 ICOM_THIS(IDirectSound3DListenerImpl,iface);
784 TRACE("returning: Doppler Factor = %f\n", This->ds3dl.flDopplerFactor);
785 *lpfDopplerFactor = This->ds3dl.flDopplerFactor;
786 return DS_OK;
789 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
790 LPDIRECTSOUND3DLISTENER iface,
791 LPD3DVECTOR lpvOrientFront,
792 LPD3DVECTOR lpvOrientTop)
794 ICOM_THIS(IDirectSound3DListenerImpl,iface);
795 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->ds3dl.vOrientFront.u1.x, \
796 This->ds3dl.vOrientFront.u2.y, This->ds3dl.vOrientFront.u3.z, This->ds3dl.vOrientTop.u1.x, This->ds3dl.vOrientTop.u2.y, \
797 This->ds3dl.vOrientTop.u3.z);
798 *lpvOrientFront = This->ds3dl.vOrientFront;
799 *lpvOrientTop = This->ds3dl.vOrientTop;
800 return DS_OK;
803 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
804 LPDIRECTSOUND3DLISTENER iface,
805 LPD3DVECTOR lpvPosition)
807 ICOM_THIS(IDirectSound3DListenerImpl,iface);
808 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3dl.vPosition.u1.x, This->ds3dl.vPosition.u2.y, This->ds3dl.vPosition.u3.z);
809 *lpvPosition = This->ds3dl.vPosition;
810 return DS_OK;
813 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
814 LPDIRECTSOUND3DLISTENER iface,
815 LPD3DVALUE lpfRolloffFactor)
817 ICOM_THIS(IDirectSound3DListenerImpl,iface);
818 TRACE("returning: RolloffFactor = %f\n", This->ds3dl.flRolloffFactor);
819 *lpfRolloffFactor = This->ds3dl.flRolloffFactor;
820 return DS_OK;
823 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
824 LPDIRECTSOUND3DLISTENER iface,
825 LPD3DVECTOR lpvVelocity)
827 ICOM_THIS(IDirectSound3DListenerImpl,iface);
828 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3dl.vVelocity.u1.x, This->ds3dl.vVelocity.u2.y, This->ds3dl.vVelocity.u3.z);
829 *lpvVelocity = This->ds3dl.vVelocity;
830 return DS_OK;
833 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
834 LPDIRECTSOUND3DLISTENER iface,
835 LPCDS3DLISTENER lpcDS3DL,
836 DWORD dwApply)
838 ICOM_THIS(IDirectSound3DListenerImpl,iface);
839 TRACE("setting: all parameters; dwApply = %ld\n", dwApply);
840 This->ds3dl = *lpcDS3DL;
841 if (dwApply == DS3D_IMMEDIATE)
843 This->need_recalc = FALSE;
844 DSOUND_ChangeListener(This);
846 This->need_recalc = TRUE;
847 return DS_OK;
850 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
851 LPDIRECTSOUND3DLISTENER iface,
852 D3DVALUE fDistanceFactor,
853 DWORD dwApply)
855 ICOM_THIS(IDirectSound3DListenerImpl,iface);
856 TRACE("setting: Distance Factor = %f; dwApply = %ld\n", fDistanceFactor, dwApply);
857 This->ds3dl.flDistanceFactor = fDistanceFactor;
858 if (dwApply == DS3D_IMMEDIATE)
860 This->need_recalc = FALSE;
861 DSOUND_ChangeListener(This);
863 This->need_recalc = TRUE;
864 return DS_OK;
867 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
868 LPDIRECTSOUND3DLISTENER iface,
869 D3DVALUE fDopplerFactor,
870 DWORD dwApply)
872 ICOM_THIS(IDirectSound3DListenerImpl,iface);
873 TRACE("setting: Doppler Factor = %f; dwApply = %ld\n", fDopplerFactor, dwApply);
874 This->ds3dl.flDopplerFactor = fDopplerFactor;
875 if (dwApply == DS3D_IMMEDIATE)
877 This->need_recalc = FALSE;
878 DSOUND_ChangeListener(This);
880 This->need_recalc = TRUE;
881 return DS_OK;
884 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
885 LPDIRECTSOUND3DLISTENER iface,
886 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
887 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
888 DWORD dwApply)
890 ICOM_THIS(IDirectSound3DListenerImpl,iface);
891 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %ld\n", \
892 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
893 This->ds3dl.vOrientFront.u1.x = xFront;
894 This->ds3dl.vOrientFront.u2.y = yFront;
895 This->ds3dl.vOrientFront.u3.z = zFront;
896 This->ds3dl.vOrientTop.u1.x = xTop;
897 This->ds3dl.vOrientTop.u2.y = yTop;
898 This->ds3dl.vOrientTop.u3.z = zTop;
899 if (dwApply == DS3D_IMMEDIATE)
901 This->need_recalc = FALSE;
902 DSOUND_ChangeListener(This);
904 This->need_recalc = TRUE;
905 return DS_OK;
908 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
909 LPDIRECTSOUND3DLISTENER iface,
910 D3DVALUE x, D3DVALUE y, D3DVALUE z,
911 DWORD dwApply)
913 ICOM_THIS(IDirectSound3DListenerImpl,iface);
914 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
915 This->ds3dl.vPosition.u1.x = x;
916 This->ds3dl.vPosition.u2.y = y;
917 This->ds3dl.vPosition.u3.z = z;
918 if (dwApply == DS3D_IMMEDIATE)
920 This->need_recalc = FALSE;
921 DSOUND_ChangeListener(This);
923 This->need_recalc = TRUE;
924 return DS_OK;
927 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
928 LPDIRECTSOUND3DLISTENER iface,
929 D3DVALUE fRolloffFactor,
930 DWORD dwApply)
932 ICOM_THIS(IDirectSound3DListenerImpl,iface);
933 TRACE("setting: Rolloff Factor = %f; dwApply = %ld\n", fRolloffFactor, dwApply);
934 This->ds3dl.flRolloffFactor = fRolloffFactor;
935 if (dwApply == DS3D_IMMEDIATE)
937 This->need_recalc = FALSE;
938 DSOUND_ChangeListener(This);
940 This->need_recalc = TRUE;
941 return DS_OK;
944 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
945 LPDIRECTSOUND3DLISTENER iface,
946 D3DVALUE x, D3DVALUE y, D3DVALUE z,
947 DWORD dwApply)
949 ICOM_THIS(IDirectSound3DListenerImpl,iface);
950 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %ld\n", x, y, z, dwApply);
951 This->ds3dl.vVelocity.u1.x = x;
952 This->ds3dl.vVelocity.u2.y = y;
953 This->ds3dl.vVelocity.u3.z = z;
954 if (dwApply == DS3D_IMMEDIATE)
956 This->need_recalc = FALSE;
957 DSOUND_ChangeListener(This);
959 This->need_recalc = TRUE;
960 return DS_OK;
963 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
964 LPDIRECTSOUND3DLISTENER iface)
967 ICOM_THIS(IDirectSound3DListenerImpl,iface);
968 TRACE("\n");
969 DSOUND_ChangeListener(This);
970 return DS_OK;
973 static ICOM_VTABLE(IDirectSound3DListener) ds3dlvt =
975 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
976 /* IUnknown methods */
977 IDirectSound3DListenerImpl_QueryInterface,
978 IDirectSound3DListenerImpl_AddRef,
979 IDirectSound3DListenerImpl_Release,
980 /* IDirectSound3DListener methods */
981 IDirectSound3DListenerImpl_GetAllParameter,
982 IDirectSound3DListenerImpl_GetDistanceFactor,
983 IDirectSound3DListenerImpl_GetDopplerFactor,
984 IDirectSound3DListenerImpl_GetOrientation,
985 IDirectSound3DListenerImpl_GetPosition,
986 IDirectSound3DListenerImpl_GetRolloffFactor,
987 IDirectSound3DListenerImpl_GetVelocity,
988 IDirectSound3DListenerImpl_SetAllParameters,
989 IDirectSound3DListenerImpl_SetDistanceFactor,
990 IDirectSound3DListenerImpl_SetDopplerFactor,
991 IDirectSound3DListenerImpl_SetOrientation,
992 IDirectSound3DListenerImpl_SetPosition,
993 IDirectSound3DListenerImpl_SetRolloffFactor,
994 IDirectSound3DListenerImpl_SetVelocity,
995 IDirectSound3DListenerImpl_CommitDeferredSettings,
998 HRESULT WINAPI IDirectSound3DListenerImpl_Create(
999 PrimaryBufferImpl *This,
1000 IDirectSound3DListenerImpl **pdsl)
1002 IDirectSound3DListenerImpl *dsl;
1004 dsl = (IDirectSound3DListenerImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(*dsl));
1005 dsl->ref = 1;
1006 ICOM_VTBL(dsl) = &ds3dlvt;
1008 dsl->ds3dl.dwSize = sizeof(DS3DLISTENER);
1009 dsl->ds3dl.vPosition.u1.x = 0.0;
1010 dsl->ds3dl.vPosition.u2.y = 0.0;
1011 dsl->ds3dl.vPosition.u3.z = 0.0;
1012 dsl->ds3dl.vVelocity.u1.x = 0.0;
1013 dsl->ds3dl.vVelocity.u2.y = 0.0;
1014 dsl->ds3dl.vVelocity.u3.z = 0.0;
1015 dsl->ds3dl.vOrientFront.u1.x = 0.0;
1016 dsl->ds3dl.vOrientFront.u2.y = 0.0;
1017 dsl->ds3dl.vOrientFront.u3.z = 1.0;
1018 dsl->ds3dl.vOrientTop.u1.x = 0.0;
1019 dsl->ds3dl.vOrientTop.u2.y = 1.0;
1020 dsl->ds3dl.vOrientTop.u3.z = 0.0;
1021 dsl->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1022 dsl->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1023 dsl->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1025 InitializeCriticalSection(&dsl->lock);
1027 dsl->dsb = This;
1028 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This);
1030 *pdsl = dsl;
1031 return S_OK;