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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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+.
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
41 #include <math.h> /* Insomnia - pow() function */
43 #define NONAMELESSUNION
44 #define NONAMELESSSTRUCT
52 #include "wine/debug.h"
55 #include "dsound_private.h"
57 /* default intensity level for human ears */
58 #define DEFAULT_INTENSITY 0.000000000001f
59 /* default velocity of sound in the air */
60 #define DEFAULT_VELOCITY 340
62 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
64 /*******************************************************************************
68 /* scalar product (i believe it's called dot product in english) */
69 static inline D3DVALUE ScalarProduct (LPD3DVECTOR a, LPD3DVECTOR b)
72 c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
73 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
78 /* vector product (i believe it's called cross product in english */
79 static inline D3DVECTOR VectorProduct (LPD3DVECTOR a, LPD3DVECTOR b)
82 c.x = (a->y*b->z) - (a->z*b->y);
83 c.y = (a->z*b->x) - (a->x*b->z);
84 c.z = (a->x*b->y) - (a->y*b->x);
85 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
90 /* magnitude (length) of vector */
91 static inline D3DVALUE VectorMagnitude (LPD3DVECTOR a)
94 l = sqrt (ScalarProduct (a, a));
95 TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
99 /* conversion between radians and degrees */
100 static inline D3DVALUE RadToDeg (D3DVALUE angle)
103 newangle = angle * (360/(2*M_PI));
104 TRACE("%f rad = %f deg\n", angle, newangle);
108 /* angle between vectors - deg version */
109 static inline D3DVALUE AngleBetweenVectorsDeg (LPD3DVECTOR a, LPD3DVECTOR b)
111 D3DVALUE la, lb, product, angle, cos;
112 /* definition of scalar product: a*b = |a|*|b|*cos...therefore: */
113 product = ScalarProduct (a,b);
114 la = VectorMagnitude (a);
115 lb = VectorMagnitude (b);
116 cos = product/(la*lb);
118 /* we now have angle in radians */
119 angle = RadToDeg(angle);
120 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f degrees\n", a->x, a->y, a->z, b->x,
125 /* angle between vectors - rad version */
126 static inline D3DVALUE AngleBetweenVectorsRad (LPD3DVECTOR a, LPD3DVECTOR b)
128 D3DVALUE la, lb, product, angle, cos;
129 /* definition of scalar product: a*b = |a|*|b|*cos...therefore: */
130 product = ScalarProduct (a,b);
131 la = VectorMagnitude (a);
132 lb = VectorMagnitude (b);
133 cos = product/(la*lb);
135 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians\n", a->x, a->y, a->z, b->x,
140 /* calculates vector between two points */
141 static inline D3DVECTOR VectorBetweenTwoPoints (LPD3DVECTOR a, LPD3DVECTOR b)
147 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
148 b->z, c.x, c.y, c.z);
152 /* calculates the length of vector's projection on another vector */
153 static inline D3DVALUE ProjectVector (LPD3DVECTOR a, LPD3DVECTOR p)
155 D3DVALUE prod, result;
156 prod = ScalarProduct(a, p);
157 result = prod/VectorMagnitude(p);
158 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
163 /*******************************************************************************
164 * 3D Buffer and Listener mixing
167 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
169 /* volume, at which the sound will be played after all calcs. */
170 D3DVALUE lVolume = 0;
171 /* intensity (used for distance related stuff) */
174 /* stuff for distance related stuff calc. */
176 D3DVALUE flDistance = 0;
177 /* panning related stuff */
180 /* doppler shift related stuff */
182 D3DVALUE flFreq, flBufferVel, flListenerVel;
187 /* initial buffer volume */
188 lVolume = dsb->ds3db_lVolume;
190 switch (dsb->ds3db_ds3db.dwMode)
192 case DS3DMODE_DISABLE:
193 TRACE("3D processing disabled\n");
194 /* this one is here only to eliminate annoying warning message */
195 DSOUND_RecalcVolPan (&dsb->volpan);
196 DSOUND_ForceRemix (dsb);
198 case DS3DMODE_NORMAL:
199 TRACE("Normal 3D processing mode\n");
200 /* we need to calculate distance between buffer and listener*/
201 vDistance = VectorBetweenTwoPoints(&dsb->ds3db_ds3db.vPosition, &dsb->device->ds3dl.vPosition);
202 flDistance = VectorMagnitude (&vDistance);
204 case DS3DMODE_HEADRELATIVE:
205 TRACE("Head-relative 3D processing mode\n");
206 /* distance between buffer and listener is same as buffer's position */
207 flDistance = VectorMagnitude (&dsb->ds3db_ds3db.vPosition);
211 if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
213 /* some apps don't want you to hear too distant sounds... */
214 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
216 dsb->volpan.lVolume = DSBVOLUME_MIN;
217 DSOUND_RecalcVolPan (&dsb->volpan);
218 /* i guess mixing here would be a waste of power */
222 flDistance = dsb->ds3db_ds3db.flMaxDistance;
225 if (flDistance < dsb->ds3db_ds3db.flMinDistance)
226 flDistance = dsb->ds3db_ds3db.flMinDistance;
228 /* 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 */
229 lVolume += 10000; /* ms likes working with negative volume...i don't */
230 lVolume /= 1000; /* convert hundreths of dB into B */
231 /* intensity level (loudness) = log10(Intensity/DefaultIntensity)...therefore */
232 flIntensity = pow(10,lVolume)*DEFAULT_INTENSITY;
233 flTemp = (flDistance/dsb->ds3db_ds3db.flMinDistance)*(flDistance/dsb->ds3db_ds3db.flMinDistance);
234 flIntensity /= flTemp;
235 lVolume = log10(flIntensity/DEFAULT_INTENSITY);
236 lVolume *= 1000; /* convert back to hundreths of dB */
237 lVolume -= 10000; /* we need to do it in ms way */
238 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
241 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
242 if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
244 TRACE("conning: cones not set\n");
248 /* calculate angle */
249 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistance);
250 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
251 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
253 /* my test show that for my way of calc., we need only half of angles */
254 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
255 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
257 if (flAngle < dwInsideConeAngle)
258 flAngle = dwInsideConeAngle;
259 /* min (app defined) volume */
260 if (flAngle > dwOutsideConeAngle)
261 flAngle = dwOutsideConeAngle;
262 /* this probably isn't the right thing, but it's ok for the time being */
263 lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
265 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
266 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
268 dsb->volpan.lVolume = lVolume;
271 if (dsb->device->ds3dl.vPosition.x == dsb->ds3db_ds3db.vPosition.x &&
272 dsb->device->ds3dl.vPosition.y == dsb->ds3db_ds3db.vPosition.y &&
273 dsb->device->ds3dl.vPosition.z == dsb->ds3db_ds3db.vPosition.z) {
274 dsb->volpan.lPan = 0;
279 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
280 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
281 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
282 /* for now, we'll use "linear formula" (which is probably incorrect); if someone has it in book, correct it */
283 dsb->volpan.lPan = 10000*2*flAngle/M_PI - 10000;
285 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
287 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
290 if ((VectorMagnitude(&ds3db.vVelocity) == 0) && (VectorMagnitude(&dsb->device->ds3dl.vVelocity) == 0))
292 TRACE("doppler: Buffer and Listener don't have velocities\n");
296 /* calculate length of ds3db.vVelocity component which causes Doppler Effect
297 NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
298 if buffer moves AWAY from listener, it's velocity component is POSITIVE */
299 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
300 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
301 NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
302 if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
303 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
304 /* formula taken from Gianicoli D.: Physics, 4th edition: */
305 /* FIXME: replace dsb->freq with appropriate frequency ! */
306 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
307 TRACE("doppler: Buffer velocity (component) = %lf, Listener velocity (component) = %lf => Doppler shift: %ld Hz -> %lf Hz\n", flBufferVel, flListenerVel,
309 /* FIXME: replace following line with correct frequency setting ! */
315 DSOUND_RecalcVolPan(&dsb->volpan);
318 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
322 DSOUND_Calc3DBuffer(dsb);
323 DSOUND_ForceRemix(dsb);
326 static void DSOUND_ChangeListener(IDirectSound3DListenerImpl *ds3dl)
329 TRACE("(%p)\n",ds3dl);
330 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
332 /* some buffers don't have 3d buffer (Ultima IX seems to
333 crash without the following line) */
334 if (ds3dl->device->buffers[i]->ds3db == NULL)
336 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
338 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
343 /*******************************************************************************
344 * IDirectSound3DBuffer
347 /* IUnknown methods */
348 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
349 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
351 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
353 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
354 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
357 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
359 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
360 ULONG ref = InterlockedIncrement(&(This->ref));
361 TRACE("(%p) ref was %d\n", This, ref - 1);
365 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
367 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
368 ULONG ref = InterlockedDecrement(&(This->ref));
369 TRACE("(%p) ref was %d\n", This, ref + 1);
372 This->dsb->ds3db = NULL;
373 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
374 HeapFree(GetProcessHeap(), 0, This);
375 TRACE("(%p) released\n", This);
380 /* IDirectSound3DBuffer methods */
381 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
382 LPDIRECTSOUND3DBUFFER iface,
383 LPDS3DBUFFER lpDs3dBuffer)
385 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
386 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
388 if (lpDs3dBuffer == NULL) {
389 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
390 return DSERR_INVALIDPARAM;
393 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
394 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
395 return DSERR_INVALIDPARAM;
398 TRACE("returning: all parameters\n");
399 *lpDs3dBuffer = This->dsb->ds3db_ds3db;
403 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
404 LPDIRECTSOUND3DBUFFER iface,
405 LPDWORD lpdwInsideConeAngle,
406 LPDWORD lpdwOutsideConeAngle)
408 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
409 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
410 This->dsb->ds3db_ds3db.dwInsideConeAngle, This->dsb->ds3db_ds3db.dwOutsideConeAngle);
411 *lpdwInsideConeAngle = This->dsb->ds3db_ds3db.dwInsideConeAngle;
412 *lpdwOutsideConeAngle = This->dsb->ds3db_ds3db.dwOutsideConeAngle;
416 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
417 LPDIRECTSOUND3DBUFFER iface,
418 LPD3DVECTOR lpvConeOrientation)
420 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
421 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
422 This->dsb->ds3db_ds3db.vConeOrientation.x,
423 This->dsb->ds3db_ds3db.vConeOrientation.y,
424 This->dsb->ds3db_ds3db.vConeOrientation.z);
425 *lpvConeOrientation = This->dsb->ds3db_ds3db.vConeOrientation;
429 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
430 LPDIRECTSOUND3DBUFFER iface,
431 LPLONG lplConeOutsideVolume)
433 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
434 TRACE("returning: Cone Outside Volume = %d\n", This->dsb->ds3db_ds3db.lConeOutsideVolume);
435 *lplConeOutsideVolume = This->dsb->ds3db_ds3db.lConeOutsideVolume;
439 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
440 LPDIRECTSOUND3DBUFFER iface,
441 LPD3DVALUE lpfMaxDistance)
443 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
444 TRACE("returning: Max Distance = %f\n", This->dsb->ds3db_ds3db.flMaxDistance);
445 *lpfMaxDistance = This->dsb->ds3db_ds3db.flMaxDistance;
449 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
450 LPDIRECTSOUND3DBUFFER iface,
451 LPD3DVALUE lpfMinDistance)
453 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
454 TRACE("returning: Min Distance = %f\n", This->dsb->ds3db_ds3db.flMinDistance);
455 *lpfMinDistance = This->dsb->ds3db_ds3db.flMinDistance;
459 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
460 LPDIRECTSOUND3DBUFFER iface,
463 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
464 TRACE("returning: Mode = %d\n", This->dsb->ds3db_ds3db.dwMode);
465 *lpdwMode = This->dsb->ds3db_ds3db.dwMode;
469 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
470 LPDIRECTSOUND3DBUFFER iface,
471 LPD3DVECTOR lpvPosition)
473 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
474 TRACE("returning: Position vector = (%f,%f,%f)\n",
475 This->dsb->ds3db_ds3db.vPosition.x,
476 This->dsb->ds3db_ds3db.vPosition.y,
477 This->dsb->ds3db_ds3db.vPosition.z);
478 *lpvPosition = This->dsb->ds3db_ds3db.vPosition;
482 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
483 LPDIRECTSOUND3DBUFFER iface,
484 LPD3DVECTOR lpvVelocity)
486 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
487 TRACE("returning: Velocity vector = (%f,%f,%f)\n",
488 This->dsb->ds3db_ds3db.vVelocity.x,
489 This->dsb->ds3db_ds3db.vVelocity.y,
490 This->dsb->ds3db_ds3db.vVelocity.z);
491 *lpvVelocity = This->dsb->ds3db_ds3db.vVelocity;
495 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
496 LPDIRECTSOUND3DBUFFER iface,
497 LPCDS3DBUFFER lpcDs3dBuffer,
500 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
501 DWORD status = DSERR_INVALIDPARAM;
502 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
504 if (lpcDs3dBuffer == NULL) {
505 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
509 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
510 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
514 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
515 This->dsb->ds3db_ds3db = *lpcDs3dBuffer;
517 if (dwApply == DS3D_IMMEDIATE)
519 DSOUND_Mix3DBuffer(This->dsb);
521 This->dsb->ds3db_need_recalc = TRUE;
527 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
528 LPDIRECTSOUND3DBUFFER iface,
529 DWORD dwInsideConeAngle,
530 DWORD dwOutsideConeAngle,
533 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
534 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
535 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
536 This->dsb->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
537 This->dsb->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
538 if (dwApply == DS3D_IMMEDIATE)
540 DSOUND_Mix3DBuffer(This->dsb);
542 This->dsb->ds3db_need_recalc = TRUE;
546 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
547 LPDIRECTSOUND3DBUFFER iface,
548 D3DVALUE x, D3DVALUE y, D3DVALUE z,
551 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
552 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
553 This->dsb->ds3db_ds3db.vConeOrientation.x = x;
554 This->dsb->ds3db_ds3db.vConeOrientation.y = y;
555 This->dsb->ds3db_ds3db.vConeOrientation.z = z;
556 if (dwApply == DS3D_IMMEDIATE)
558 This->dsb->ds3db_need_recalc = FALSE;
559 DSOUND_Mix3DBuffer(This->dsb);
561 This->dsb->ds3db_need_recalc = TRUE;
565 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
566 LPDIRECTSOUND3DBUFFER iface,
567 LONG lConeOutsideVolume,
570 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
571 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
572 This->dsb->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
573 if (dwApply == DS3D_IMMEDIATE)
575 This->dsb->ds3db_need_recalc = FALSE;
576 DSOUND_Mix3DBuffer(This->dsb);
578 This->dsb->ds3db_need_recalc = TRUE;
582 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(
583 LPDIRECTSOUND3DBUFFER iface,
584 D3DVALUE fMaxDistance,
587 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
588 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
589 This->dsb->ds3db_ds3db.flMaxDistance = fMaxDistance;
590 if (dwApply == DS3D_IMMEDIATE)
592 This->dsb->ds3db_need_recalc = FALSE;
593 DSOUND_Mix3DBuffer(This->dsb);
595 This->dsb->ds3db_need_recalc = TRUE;
599 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(
600 LPDIRECTSOUND3DBUFFER iface,
601 D3DVALUE fMinDistance,
604 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
605 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
606 This->dsb->ds3db_ds3db.flMinDistance = fMinDistance;
607 if (dwApply == DS3D_IMMEDIATE)
609 This->dsb->ds3db_need_recalc = FALSE;
610 DSOUND_Mix3DBuffer(This->dsb);
612 This->dsb->ds3db_need_recalc = TRUE;
616 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(
617 LPDIRECTSOUND3DBUFFER iface,
621 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
622 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
623 This->dsb->ds3db_ds3db.dwMode = dwMode;
624 if (dwApply == DS3D_IMMEDIATE)
626 This->dsb->ds3db_need_recalc = FALSE;
627 DSOUND_Mix3DBuffer(This->dsb);
629 This->dsb->ds3db_need_recalc = TRUE;
633 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(
634 LPDIRECTSOUND3DBUFFER iface,
635 D3DVALUE x, D3DVALUE y, D3DVALUE z,
638 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
639 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
640 This->dsb->ds3db_ds3db.vPosition.x = x;
641 This->dsb->ds3db_ds3db.vPosition.y = y;
642 This->dsb->ds3db_ds3db.vPosition.z = z;
643 if (dwApply == DS3D_IMMEDIATE)
645 This->dsb->ds3db_need_recalc = FALSE;
646 DSOUND_Mix3DBuffer(This->dsb);
648 This->dsb->ds3db_need_recalc = TRUE;
652 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
653 LPDIRECTSOUND3DBUFFER iface,
654 D3DVALUE x, D3DVALUE y, D3DVALUE z,
657 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
658 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
659 This->dsb->ds3db_ds3db.vVelocity.x = x;
660 This->dsb->ds3db_ds3db.vVelocity.y = y;
661 This->dsb->ds3db_ds3db.vVelocity.z = z;
662 if (dwApply == DS3D_IMMEDIATE)
664 This->dsb->ds3db_need_recalc = FALSE;
665 DSOUND_Mix3DBuffer(This->dsb);
667 This->dsb->ds3db_need_recalc = TRUE;
671 static const IDirectSound3DBufferVtbl ds3dbvt =
673 /* IUnknown methods */
674 IDirectSound3DBufferImpl_QueryInterface,
675 IDirectSound3DBufferImpl_AddRef,
676 IDirectSound3DBufferImpl_Release,
677 /* IDirectSound3DBuffer methods */
678 IDirectSound3DBufferImpl_GetAllParameters,
679 IDirectSound3DBufferImpl_GetConeAngles,
680 IDirectSound3DBufferImpl_GetConeOrientation,
681 IDirectSound3DBufferImpl_GetConeOutsideVolume,
682 IDirectSound3DBufferImpl_GetMaxDistance,
683 IDirectSound3DBufferImpl_GetMinDistance,
684 IDirectSound3DBufferImpl_GetMode,
685 IDirectSound3DBufferImpl_GetPosition,
686 IDirectSound3DBufferImpl_GetVelocity,
687 IDirectSound3DBufferImpl_SetAllParameters,
688 IDirectSound3DBufferImpl_SetConeAngles,
689 IDirectSound3DBufferImpl_SetConeOrientation,
690 IDirectSound3DBufferImpl_SetConeOutsideVolume,
691 IDirectSound3DBufferImpl_SetMaxDistance,
692 IDirectSound3DBufferImpl_SetMinDistance,
693 IDirectSound3DBufferImpl_SetMode,
694 IDirectSound3DBufferImpl_SetPosition,
695 IDirectSound3DBufferImpl_SetVelocity,
698 HRESULT IDirectSound3DBufferImpl_Create(
699 IDirectSoundBufferImpl *dsb,
700 IDirectSound3DBufferImpl **pds3db)
702 IDirectSound3DBufferImpl *ds3db;
703 TRACE("(%p,%p)\n",dsb,pds3db);
705 ds3db = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*ds3db));
708 WARN("out of memory\n");
710 return DSERR_OUTOFMEMORY;
715 ds3db->lpVtbl = &ds3dbvt;
717 ds3db->dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
718 ds3db->dsb->ds3db_ds3db.vPosition.x = 0.0;
719 ds3db->dsb->ds3db_ds3db.vPosition.y = 0.0;
720 ds3db->dsb->ds3db_ds3db.vPosition.z = 0.0;
721 ds3db->dsb->ds3db_ds3db.vVelocity.x = 0.0;
722 ds3db->dsb->ds3db_ds3db.vVelocity.y = 0.0;
723 ds3db->dsb->ds3db_ds3db.vVelocity.z = 0.0;
724 ds3db->dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
725 ds3db->dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
726 ds3db->dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
727 ds3db->dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
728 ds3db->dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
729 ds3db->dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
730 ds3db->dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
731 ds3db->dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
732 ds3db->dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
734 ds3db->dsb->ds3db_need_recalc = TRUE;
736 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
742 HRESULT IDirectSound3DBufferImpl_Destroy(
743 IDirectSound3DBufferImpl *pds3db)
745 TRACE("(%p)\n",pds3db);
747 while (IDirectSound3DBufferImpl_Release((LPDIRECTSOUND3DBUFFER)pds3db) > 0);
752 /*******************************************************************************
753 * IDirectSound3DListener
756 /* IUnknown methods */
757 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
758 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
760 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
762 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
765 WARN("invalid parameter\n");
769 *ppobj = NULL; /* assume failure */
771 if ( IsEqualGUID(riid, &IID_IUnknown) ||
772 IsEqualGUID(riid, &IID_IDirectSound3DListener ) ) {
773 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This);
778 if ( IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
779 if (!This->device->primary)
780 PrimaryBufferImpl_Create(This->device, &(This->device->primary), &(This->device->dsbd));
781 if (This->device->primary) {
782 *ppobj = This->device->primary;
783 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)*ppobj);
788 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
789 return E_NOINTERFACE;
792 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
794 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
795 ULONG ref = InterlockedIncrement(&(This->ref));
796 TRACE("(%p) ref was %d\n", This, ref - 1);
800 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
802 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
803 ULONG ref = InterlockedDecrement(&(This->ref));
804 TRACE("(%p) ref was %d\n", This, ref + 1);
807 This->device->listener = 0;
808 HeapFree(GetProcessHeap(), 0, This);
809 TRACE("(%p) released\n", This);
814 /* IDirectSound3DListener methods */
815 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
816 LPDIRECTSOUND3DLISTENER iface,
817 LPDS3DLISTENER lpDS3DL)
819 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
820 TRACE("(%p,%p)\n",This,lpDS3DL);
822 if (lpDS3DL == NULL) {
823 WARN("invalid parameter: lpDS3DL == NULL\n");
824 return DSERR_INVALIDPARAM;
827 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
828 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
829 return DSERR_INVALIDPARAM;
832 TRACE("returning: all parameters\n");
833 *lpDS3DL = This->device->ds3dl;
837 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
838 LPDIRECTSOUND3DLISTENER iface,
839 LPD3DVALUE lpfDistanceFactor)
841 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
842 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
843 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
847 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
848 LPDIRECTSOUND3DLISTENER iface,
849 LPD3DVALUE lpfDopplerFactor)
851 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
852 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
853 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
857 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
858 LPDIRECTSOUND3DLISTENER iface,
859 LPD3DVECTOR lpvOrientFront,
860 LPD3DVECTOR lpvOrientTop)
862 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
863 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
864 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
865 This->device->ds3dl.vOrientTop.z);
866 *lpvOrientFront = This->device->ds3dl.vOrientFront;
867 *lpvOrientTop = This->device->ds3dl.vOrientTop;
871 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
872 LPDIRECTSOUND3DLISTENER iface,
873 LPD3DVECTOR lpvPosition)
875 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
876 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
877 *lpvPosition = This->device->ds3dl.vPosition;
881 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
882 LPDIRECTSOUND3DLISTENER iface,
883 LPD3DVALUE lpfRolloffFactor)
885 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
886 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
887 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
891 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
892 LPDIRECTSOUND3DLISTENER iface,
893 LPD3DVECTOR lpvVelocity)
895 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
896 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
897 *lpvVelocity = This->device->ds3dl.vVelocity;
901 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
902 LPDIRECTSOUND3DLISTENER iface,
903 LPCDS3DLISTENER lpcDS3DL,
906 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
907 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
908 This->device->ds3dl = *lpcDS3DL;
909 if (dwApply == DS3D_IMMEDIATE)
911 This->device->ds3dl_need_recalc = FALSE;
912 DSOUND_ChangeListener(This);
914 This->device->ds3dl_need_recalc = TRUE;
918 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
919 LPDIRECTSOUND3DLISTENER iface,
920 D3DVALUE fDistanceFactor,
923 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
924 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
925 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
926 if (dwApply == DS3D_IMMEDIATE)
928 This->device->ds3dl_need_recalc = FALSE;
929 DSOUND_ChangeListener(This);
931 This->device->ds3dl_need_recalc = TRUE;
935 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
936 LPDIRECTSOUND3DLISTENER iface,
937 D3DVALUE fDopplerFactor,
940 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
941 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
942 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
943 if (dwApply == DS3D_IMMEDIATE)
945 This->device->ds3dl_need_recalc = FALSE;
946 DSOUND_ChangeListener(This);
948 This->device->ds3dl_need_recalc = TRUE;
952 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
953 LPDIRECTSOUND3DLISTENER iface,
954 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
955 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
958 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
959 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
960 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
961 This->device->ds3dl.vOrientFront.x = xFront;
962 This->device->ds3dl.vOrientFront.y = yFront;
963 This->device->ds3dl.vOrientFront.z = zFront;
964 This->device->ds3dl.vOrientTop.x = xTop;
965 This->device->ds3dl.vOrientTop.y = yTop;
966 This->device->ds3dl.vOrientTop.z = zTop;
967 if (dwApply == DS3D_IMMEDIATE)
969 This->device->ds3dl_need_recalc = FALSE;
970 DSOUND_ChangeListener(This);
972 This->device->ds3dl_need_recalc = TRUE;
976 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
977 LPDIRECTSOUND3DLISTENER iface,
978 D3DVALUE x, D3DVALUE y, D3DVALUE z,
981 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
982 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
983 This->device->ds3dl.vPosition.x = x;
984 This->device->ds3dl.vPosition.y = y;
985 This->device->ds3dl.vPosition.z = z;
986 if (dwApply == DS3D_IMMEDIATE)
988 This->device->ds3dl_need_recalc = FALSE;
989 DSOUND_ChangeListener(This);
991 This->device->ds3dl_need_recalc = TRUE;
995 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
996 LPDIRECTSOUND3DLISTENER iface,
997 D3DVALUE fRolloffFactor,
1000 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1001 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
1002 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
1003 if (dwApply == DS3D_IMMEDIATE)
1005 This->device->ds3dl_need_recalc = FALSE;
1006 DSOUND_ChangeListener(This);
1008 This->device->ds3dl_need_recalc = TRUE;
1012 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
1013 LPDIRECTSOUND3DLISTENER iface,
1014 D3DVALUE x, D3DVALUE y, D3DVALUE z,
1017 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1018 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
1019 This->device->ds3dl.vVelocity.x = x;
1020 This->device->ds3dl.vVelocity.y = y;
1021 This->device->ds3dl.vVelocity.z = z;
1022 if (dwApply == DS3D_IMMEDIATE)
1024 This->device->ds3dl_need_recalc = FALSE;
1025 DSOUND_ChangeListener(This);
1027 This->device->ds3dl_need_recalc = TRUE;
1031 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
1032 LPDIRECTSOUND3DLISTENER iface)
1034 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1036 DSOUND_ChangeListener(This);
1040 static const IDirectSound3DListenerVtbl ds3dlvt =
1042 /* IUnknown methods */
1043 IDirectSound3DListenerImpl_QueryInterface,
1044 IDirectSound3DListenerImpl_AddRef,
1045 IDirectSound3DListenerImpl_Release,
1046 /* IDirectSound3DListener methods */
1047 IDirectSound3DListenerImpl_GetAllParameter,
1048 IDirectSound3DListenerImpl_GetDistanceFactor,
1049 IDirectSound3DListenerImpl_GetDopplerFactor,
1050 IDirectSound3DListenerImpl_GetOrientation,
1051 IDirectSound3DListenerImpl_GetPosition,
1052 IDirectSound3DListenerImpl_GetRolloffFactor,
1053 IDirectSound3DListenerImpl_GetVelocity,
1054 IDirectSound3DListenerImpl_SetAllParameters,
1055 IDirectSound3DListenerImpl_SetDistanceFactor,
1056 IDirectSound3DListenerImpl_SetDopplerFactor,
1057 IDirectSound3DListenerImpl_SetOrientation,
1058 IDirectSound3DListenerImpl_SetPosition,
1059 IDirectSound3DListenerImpl_SetRolloffFactor,
1060 IDirectSound3DListenerImpl_SetVelocity,
1061 IDirectSound3DListenerImpl_CommitDeferredSettings,
1064 HRESULT IDirectSound3DListenerImpl_Create(
1065 DirectSoundDevice * device,
1066 IDirectSound3DListenerImpl ** ppdsl)
1068 IDirectSound3DListenerImpl *pdsl;
1069 TRACE("(%p,%p)\n",device,ppdsl);
1071 pdsl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*pdsl));
1074 WARN("out of memory\n");
1076 return DSERR_OUTOFMEMORY;
1080 pdsl->lpVtbl = &ds3dlvt;
1082 pdsl->device = device;
1084 pdsl->device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1085 pdsl->device->ds3dl.vPosition.x = 0.0;
1086 pdsl->device->ds3dl.vPosition.y = 0.0;
1087 pdsl->device->ds3dl.vPosition.z = 0.0;
1088 pdsl->device->ds3dl.vVelocity.x = 0.0;
1089 pdsl->device->ds3dl.vVelocity.y = 0.0;
1090 pdsl->device->ds3dl.vVelocity.z = 0.0;
1091 pdsl->device->ds3dl.vOrientFront.x = 0.0;
1092 pdsl->device->ds3dl.vOrientFront.y = 0.0;
1093 pdsl->device->ds3dl.vOrientFront.z = 1.0;
1094 pdsl->device->ds3dl.vOrientTop.x = 0.0;
1095 pdsl->device->ds3dl.vOrientTop.y = 1.0;
1096 pdsl->device->ds3dl.vOrientTop.z = 0.0;
1097 pdsl->device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1098 pdsl->device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1099 pdsl->device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1101 pdsl->device->ds3dl_need_recalc = TRUE;