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
51 #include "wine/debug.h"
54 #include "dsound_private.h"
56 /* default velocity of sound in the air */
57 #define DEFAULT_VELOCITY 340
59 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
61 /*******************************************************************************
65 /* scalar product (I believe it's called dot product in English) */
66 static inline D3DVALUE ScalarProduct (const D3DVECTOR *a, const D3DVECTOR *b)
69 c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
70 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
75 /* vector product (I believe it's called cross product in English */
76 static inline D3DVECTOR VectorProduct (const D3DVECTOR *a, const D3DVECTOR *b)
79 c.x = (a->y*b->z) - (a->z*b->y);
80 c.y = (a->z*b->x) - (a->x*b->z);
81 c.z = (a->x*b->y) - (a->y*b->x);
82 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
87 /* magnitude (length) of vector */
88 static inline D3DVALUE VectorMagnitude (const D3DVECTOR *a)
91 l = sqrt (ScalarProduct (a, a));
92 TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
96 /* conversion between radians and degrees */
97 static inline D3DVALUE RadToDeg (D3DVALUE angle)
100 newangle = angle * (360/(2*M_PI));
101 TRACE("%f rad = %f deg\n", angle, newangle);
105 /* angle between vectors - rad version */
106 static inline D3DVALUE AngleBetweenVectorsRad (const D3DVECTOR *a, const D3DVECTOR *b)
108 D3DVALUE la, lb, product, angle, cos;
109 /* definition of scalar product: a*b = |a|*|b|*cos... therefore: */
110 product = ScalarProduct (a,b);
111 la = VectorMagnitude (a);
112 lb = VectorMagnitude (b);
116 cos = product/(la*lb);
118 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians (%f degrees)\n", a->x, a->y, a->z, b->x,
119 b->y, b->z, angle, RadToDeg(angle));
123 static inline D3DVALUE AngleBetweenVectorsDeg (const D3DVECTOR *a, const D3DVECTOR *b)
125 return RadToDeg(AngleBetweenVectorsRad(a, b));
128 /* calculates vector between two points */
129 static inline D3DVECTOR VectorBetweenTwoPoints (const D3DVECTOR *a, const D3DVECTOR *b)
135 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
136 b->z, c.x, c.y, c.z);
140 /* calculates the length of vector's projection on another vector */
141 static inline D3DVALUE ProjectVector (const D3DVECTOR *a, const D3DVECTOR *p)
143 D3DVALUE prod, result;
144 prod = ScalarProduct(a, p);
145 result = prod/VectorMagnitude(p);
146 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
151 /*******************************************************************************
152 * 3D Buffer and Listener mixing
155 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
157 /* volume, at which the sound will be played after all calcs. */
158 D3DVALUE lVolume = 0;
159 /* stuff for distance related stuff calc. */
161 D3DVALUE flDistance = 0;
162 /* panning related stuff */
165 /* doppler shift related stuff */
167 D3DVALUE flFreq, flBufferVel, flListenerVel;
172 /* initial buffer volume */
173 lVolume = dsb->ds3db_lVolume;
175 switch (dsb->ds3db_ds3db.dwMode)
177 case DS3DMODE_DISABLE:
178 TRACE("3D processing disabled\n");
179 /* this one is here only to eliminate annoying warning message */
180 DSOUND_RecalcVolPan (&dsb->volpan);
182 case DS3DMODE_NORMAL:
183 TRACE("Normal 3D processing mode\n");
184 /* we need to calculate distance between buffer and listener*/
185 vDistance = VectorBetweenTwoPoints(&dsb->ds3db_ds3db.vPosition, &dsb->device->ds3dl.vPosition);
186 flDistance = VectorMagnitude (&vDistance);
188 case DS3DMODE_HEADRELATIVE:
189 TRACE("Head-relative 3D processing mode\n");
190 /* distance between buffer and listener is same as buffer's position */
191 flDistance = VectorMagnitude (&dsb->ds3db_ds3db.vPosition);
195 if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
197 /* some apps don't want you to hear too distant sounds... */
198 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
200 dsb->volpan.lVolume = DSBVOLUME_MIN;
201 DSOUND_RecalcVolPan (&dsb->volpan);
202 /* i guess mixing here would be a waste of power */
206 flDistance = dsb->ds3db_ds3db.flMaxDistance;
209 if (flDistance < dsb->ds3db_ds3db.flMinDistance)
210 flDistance = dsb->ds3db_ds3db.flMinDistance;
212 /* attenuation proportional to the distance squared, converted to millibels as in lVolume*/
213 lVolume -= log10(flDistance/dsb->ds3db_ds3db.flMinDistance * flDistance/dsb->ds3db_ds3db.flMinDistance)*1000;
214 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
217 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
218 if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
220 TRACE("conning: cones not set\n");
224 /* calculate angle */
225 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistance);
226 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
227 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
229 /* my test show that for my way of calc., we need only half of angles */
230 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
231 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
232 if (dwOutsideConeAngle == dwInsideConeAngle)
233 ++dwOutsideConeAngle;
236 if (flAngle < dwInsideConeAngle)
237 flAngle = dwInsideConeAngle;
238 /* min (app defined) volume */
239 if (flAngle > dwOutsideConeAngle)
240 flAngle = dwOutsideConeAngle;
241 /* this probably isn't the right thing, but it's ok for the time being */
242 lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
244 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
245 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
247 dsb->volpan.lVolume = lVolume;
250 if (dsb->device->ds3dl.vPosition.x == dsb->ds3db_ds3db.vPosition.x &&
251 dsb->device->ds3dl.vPosition.y == dsb->ds3db_ds3db.vPosition.y &&
252 dsb->device->ds3dl.vPosition.z == dsb->ds3db_ds3db.vPosition.z) {
253 dsb->volpan.lPan = 0;
258 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
259 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
260 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
261 /* for now, we'll use "linear formula" (which is probably incorrect); if someone has it in book, correct it */
262 dsb->volpan.lPan = 10000*2*flAngle/M_PI - 10000;
264 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
266 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
269 if ((VectorMagnitude(&ds3db_ds3db.vVelocity) == 0) && (VectorMagnitude(&dsb->device->ds3dl.vVelocity) == 0))
271 TRACE("doppler: Buffer and Listener don't have velocities\n");
273 else if (ds3db_ds3db.vVelocity != dsb->device->ds3dl.vVelocity)
275 /* calculate length of ds3db_ds3db.vVelocity component which causes Doppler Effect
276 NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
277 if buffer moves AWAY from listener, it's velocity component is POSITIVE */
278 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
279 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
280 NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
281 if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
282 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
283 /* formula taken from Gianicoli D.: Physics, 4th edition: */
284 /* FIXME: replace dsb->freq with appropriate frequency ! */
285 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
286 TRACE("doppler: Buffer velocity (component) = %lf, Listener velocity (component) = %lf => Doppler shift: %ld Hz -> %lf Hz\n", flBufferVel, flListenerVel,
288 /* FIXME: replace following line with correct frequency setting ! */
290 DSOUND_RecalcFormat(dsb);
291 DSOUND_MixToTemporary(dsb, 0, dsb->buflen);
296 DSOUND_RecalcVolPan(&dsb->volpan);
299 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
303 DSOUND_Calc3DBuffer(dsb);
306 static void DSOUND_ChangeListener(IDirectSound3DListenerImpl *ds3dl)
309 TRACE("(%p)\n",ds3dl);
310 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
312 /* check if this buffer is waiting for recalculation */
313 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
315 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
320 /*******************************************************************************
321 * IDirectSound3DBuffer
324 /* IUnknown methods */
325 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(
326 LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj)
328 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
330 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
331 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
334 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface)
336 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
337 ULONG ref = InterlockedIncrement(&(This->ref));
339 TRACE("(%p) ref was %d\n", This, ref - 1);
342 InterlockedIncrement(&This->dsb->numIfaces);
347 static ULONG WINAPI IDirectSound3DBufferImpl_Release(LPDIRECTSOUND3DBUFFER iface)
349 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
350 ULONG ref = InterlockedDecrement(&(This->ref));
351 TRACE("(%p) ref was %d\n", This, ref + 1);
354 This->dsb->ds3db = NULL;
355 if (!InterlockedDecrement(&This->dsb->numIfaces))
356 secondarybuffer_destroy(This->dsb);
357 HeapFree(GetProcessHeap(), 0, This);
358 TRACE("(%p) released\n", This);
363 /* IDirectSound3DBuffer methods */
364 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(
365 LPDIRECTSOUND3DBUFFER iface,
366 LPDS3DBUFFER lpDs3dBuffer)
368 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
369 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
371 if (lpDs3dBuffer == NULL) {
372 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
373 return DSERR_INVALIDPARAM;
376 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
377 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
378 return DSERR_INVALIDPARAM;
381 TRACE("returning: all parameters\n");
382 *lpDs3dBuffer = This->dsb->ds3db_ds3db;
386 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(
387 LPDIRECTSOUND3DBUFFER iface,
388 LPDWORD lpdwInsideConeAngle,
389 LPDWORD lpdwOutsideConeAngle)
391 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
392 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
393 This->dsb->ds3db_ds3db.dwInsideConeAngle, This->dsb->ds3db_ds3db.dwOutsideConeAngle);
394 *lpdwInsideConeAngle = This->dsb->ds3db_ds3db.dwInsideConeAngle;
395 *lpdwOutsideConeAngle = This->dsb->ds3db_ds3db.dwOutsideConeAngle;
399 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(
400 LPDIRECTSOUND3DBUFFER iface,
401 LPD3DVECTOR lpvConeOrientation)
403 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
404 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
405 This->dsb->ds3db_ds3db.vConeOrientation.x,
406 This->dsb->ds3db_ds3db.vConeOrientation.y,
407 This->dsb->ds3db_ds3db.vConeOrientation.z);
408 *lpvConeOrientation = This->dsb->ds3db_ds3db.vConeOrientation;
412 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(
413 LPDIRECTSOUND3DBUFFER iface,
414 LPLONG lplConeOutsideVolume)
416 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
417 TRACE("returning: Cone Outside Volume = %d\n", This->dsb->ds3db_ds3db.lConeOutsideVolume);
418 *lplConeOutsideVolume = This->dsb->ds3db_ds3db.lConeOutsideVolume;
422 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(
423 LPDIRECTSOUND3DBUFFER iface,
424 LPD3DVALUE lpfMaxDistance)
426 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
427 TRACE("returning: Max Distance = %f\n", This->dsb->ds3db_ds3db.flMaxDistance);
428 *lpfMaxDistance = This->dsb->ds3db_ds3db.flMaxDistance;
432 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(
433 LPDIRECTSOUND3DBUFFER iface,
434 LPD3DVALUE lpfMinDistance)
436 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
437 TRACE("returning: Min Distance = %f\n", This->dsb->ds3db_ds3db.flMinDistance);
438 *lpfMinDistance = This->dsb->ds3db_ds3db.flMinDistance;
442 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(
443 LPDIRECTSOUND3DBUFFER iface,
446 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
447 TRACE("returning: Mode = %d\n", This->dsb->ds3db_ds3db.dwMode);
448 *lpdwMode = This->dsb->ds3db_ds3db.dwMode;
452 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(
453 LPDIRECTSOUND3DBUFFER iface,
454 LPD3DVECTOR lpvPosition)
456 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
457 TRACE("returning: Position vector = (%f,%f,%f)\n",
458 This->dsb->ds3db_ds3db.vPosition.x,
459 This->dsb->ds3db_ds3db.vPosition.y,
460 This->dsb->ds3db_ds3db.vPosition.z);
461 *lpvPosition = This->dsb->ds3db_ds3db.vPosition;
465 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(
466 LPDIRECTSOUND3DBUFFER iface,
467 LPD3DVECTOR lpvVelocity)
469 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
470 TRACE("returning: Velocity vector = (%f,%f,%f)\n",
471 This->dsb->ds3db_ds3db.vVelocity.x,
472 This->dsb->ds3db_ds3db.vVelocity.y,
473 This->dsb->ds3db_ds3db.vVelocity.z);
474 *lpvVelocity = This->dsb->ds3db_ds3db.vVelocity;
478 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(
479 LPDIRECTSOUND3DBUFFER iface,
480 LPCDS3DBUFFER lpcDs3dBuffer,
483 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
484 DWORD status = DSERR_INVALIDPARAM;
485 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
487 if (lpcDs3dBuffer == NULL) {
488 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
492 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
493 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
497 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
498 This->dsb->ds3db_ds3db = *lpcDs3dBuffer;
500 if (dwApply == DS3D_IMMEDIATE)
502 DSOUND_Mix3DBuffer(This->dsb);
504 This->dsb->ds3db_need_recalc = TRUE;
510 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(
511 LPDIRECTSOUND3DBUFFER iface,
512 DWORD dwInsideConeAngle,
513 DWORD dwOutsideConeAngle,
516 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
517 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
518 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
519 This->dsb->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
520 This->dsb->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
521 if (dwApply == DS3D_IMMEDIATE)
523 DSOUND_Mix3DBuffer(This->dsb);
525 This->dsb->ds3db_need_recalc = TRUE;
529 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(
530 LPDIRECTSOUND3DBUFFER iface,
531 D3DVALUE x, D3DVALUE y, D3DVALUE z,
534 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
535 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
536 This->dsb->ds3db_ds3db.vConeOrientation.x = x;
537 This->dsb->ds3db_ds3db.vConeOrientation.y = y;
538 This->dsb->ds3db_ds3db.vConeOrientation.z = z;
539 if (dwApply == DS3D_IMMEDIATE)
541 This->dsb->ds3db_need_recalc = FALSE;
542 DSOUND_Mix3DBuffer(This->dsb);
544 This->dsb->ds3db_need_recalc = TRUE;
548 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(
549 LPDIRECTSOUND3DBUFFER iface,
550 LONG lConeOutsideVolume,
553 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
554 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
555 This->dsb->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
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_SetMaxDistance(
566 LPDIRECTSOUND3DBUFFER iface,
567 D3DVALUE fMaxDistance,
570 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
571 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
572 This->dsb->ds3db_ds3db.flMaxDistance = fMaxDistance;
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_SetMinDistance(
583 LPDIRECTSOUND3DBUFFER iface,
584 D3DVALUE fMinDistance,
587 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
588 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
589 This->dsb->ds3db_ds3db.flMinDistance = fMinDistance;
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_SetMode(
600 LPDIRECTSOUND3DBUFFER iface,
604 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
605 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
606 This->dsb->ds3db_ds3db.dwMode = dwMode;
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_SetPosition(
617 LPDIRECTSOUND3DBUFFER iface,
618 D3DVALUE x, D3DVALUE y, D3DVALUE z,
621 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
622 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
623 This->dsb->ds3db_ds3db.vPosition.x = x;
624 This->dsb->ds3db_ds3db.vPosition.y = y;
625 This->dsb->ds3db_ds3db.vPosition.z = z;
626 if (dwApply == DS3D_IMMEDIATE)
628 This->dsb->ds3db_need_recalc = FALSE;
629 DSOUND_Mix3DBuffer(This->dsb);
631 This->dsb->ds3db_need_recalc = TRUE;
635 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(
636 LPDIRECTSOUND3DBUFFER iface,
637 D3DVALUE x, D3DVALUE y, D3DVALUE z,
640 IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;
641 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
642 This->dsb->ds3db_ds3db.vVelocity.x = x;
643 This->dsb->ds3db_ds3db.vVelocity.y = y;
644 This->dsb->ds3db_ds3db.vVelocity.z = z;
645 if (dwApply == DS3D_IMMEDIATE)
647 This->dsb->ds3db_need_recalc = FALSE;
648 DSOUND_Mix3DBuffer(This->dsb);
650 This->dsb->ds3db_need_recalc = TRUE;
654 static const IDirectSound3DBufferVtbl ds3dbvt =
656 /* IUnknown methods */
657 IDirectSound3DBufferImpl_QueryInterface,
658 IDirectSound3DBufferImpl_AddRef,
659 IDirectSound3DBufferImpl_Release,
660 /* IDirectSound3DBuffer methods */
661 IDirectSound3DBufferImpl_GetAllParameters,
662 IDirectSound3DBufferImpl_GetConeAngles,
663 IDirectSound3DBufferImpl_GetConeOrientation,
664 IDirectSound3DBufferImpl_GetConeOutsideVolume,
665 IDirectSound3DBufferImpl_GetMaxDistance,
666 IDirectSound3DBufferImpl_GetMinDistance,
667 IDirectSound3DBufferImpl_GetMode,
668 IDirectSound3DBufferImpl_GetPosition,
669 IDirectSound3DBufferImpl_GetVelocity,
670 IDirectSound3DBufferImpl_SetAllParameters,
671 IDirectSound3DBufferImpl_SetConeAngles,
672 IDirectSound3DBufferImpl_SetConeOrientation,
673 IDirectSound3DBufferImpl_SetConeOutsideVolume,
674 IDirectSound3DBufferImpl_SetMaxDistance,
675 IDirectSound3DBufferImpl_SetMinDistance,
676 IDirectSound3DBufferImpl_SetMode,
677 IDirectSound3DBufferImpl_SetPosition,
678 IDirectSound3DBufferImpl_SetVelocity,
681 HRESULT IDirectSound3DBufferImpl_Create(
682 IDirectSoundBufferImpl *dsb,
683 IDirectSound3DBufferImpl **pds3db)
685 IDirectSound3DBufferImpl *ds3db;
686 TRACE("(%p,%p)\n",dsb,pds3db);
688 ds3db = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*ds3db));
691 WARN("out of memory\n");
693 return DSERR_OUTOFMEMORY;
698 ds3db->lpVtbl = &ds3dbvt;
700 ds3db->dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
701 ds3db->dsb->ds3db_ds3db.vPosition.x = 0.0;
702 ds3db->dsb->ds3db_ds3db.vPosition.y = 0.0;
703 ds3db->dsb->ds3db_ds3db.vPosition.z = 0.0;
704 ds3db->dsb->ds3db_ds3db.vVelocity.x = 0.0;
705 ds3db->dsb->ds3db_ds3db.vVelocity.y = 0.0;
706 ds3db->dsb->ds3db_ds3db.vVelocity.z = 0.0;
707 ds3db->dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
708 ds3db->dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
709 ds3db->dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
710 ds3db->dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
711 ds3db->dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
712 ds3db->dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
713 ds3db->dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
714 ds3db->dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
715 ds3db->dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
717 ds3db->dsb->ds3db_need_recalc = TRUE;
723 HRESULT IDirectSound3DBufferImpl_Destroy(
724 IDirectSound3DBufferImpl *pds3db)
726 TRACE("(%p)\n",pds3db);
728 while (IDirectSound3DBufferImpl_Release((LPDIRECTSOUND3DBUFFER)pds3db) > 0);
733 /*******************************************************************************
734 * IDirectSound3DListener
737 /* IUnknown methods */
738 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(
739 LPDIRECTSOUND3DLISTENER iface, REFIID riid, LPVOID *ppobj)
741 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
743 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
746 WARN("invalid parameter\n");
750 *ppobj = NULL; /* assume failure */
752 if ( IsEqualGUID(riid, &IID_IUnknown) ||
753 IsEqualGUID(riid, &IID_IDirectSound3DListener ) ) {
754 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)This);
759 if ( IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
760 *ppobj = &This->device->primary->IDirectSoundBuffer8_iface;
761 IDirectSoundBuffer8_AddRef(&This->device->primary->IDirectSoundBuffer8_iface);
765 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
766 return E_NOINTERFACE;
769 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(LPDIRECTSOUND3DLISTENER iface)
771 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
772 ULONG ref = InterlockedIncrement(&(This->ref));
774 TRACE("(%p) ref was %d\n", This, ref - 1);
777 InterlockedIncrement(&This->device->primary->numIfaces);
782 static ULONG WINAPI IDirectSound3DListenerImpl_Release(LPDIRECTSOUND3DLISTENER iface)
784 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
785 ULONG ref = InterlockedDecrement(&(This->ref));
786 TRACE("(%p) ref was %d\n", This, ref + 1);
789 This->device->listener = 0;
790 HeapFree(GetProcessHeap(), 0, This);
791 if (!InterlockedDecrement(&This->device->primary->numIfaces))
792 primarybuffer_destroy(This->device->primary);
793 TRACE("(%p) released\n", This);
798 /* IDirectSound3DListener methods */
799 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(
800 LPDIRECTSOUND3DLISTENER iface,
801 LPDS3DLISTENER lpDS3DL)
803 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
804 TRACE("(%p,%p)\n",This,lpDS3DL);
806 if (lpDS3DL == NULL) {
807 WARN("invalid parameter: lpDS3DL == NULL\n");
808 return DSERR_INVALIDPARAM;
811 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
812 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
813 return DSERR_INVALIDPARAM;
816 TRACE("returning: all parameters\n");
817 *lpDS3DL = This->device->ds3dl;
821 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(
822 LPDIRECTSOUND3DLISTENER iface,
823 LPD3DVALUE lpfDistanceFactor)
825 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
826 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
827 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
831 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(
832 LPDIRECTSOUND3DLISTENER iface,
833 LPD3DVALUE lpfDopplerFactor)
835 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
836 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
837 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
841 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(
842 LPDIRECTSOUND3DLISTENER iface,
843 LPD3DVECTOR lpvOrientFront,
844 LPD3DVECTOR lpvOrientTop)
846 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
847 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
848 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
849 This->device->ds3dl.vOrientTop.z);
850 *lpvOrientFront = This->device->ds3dl.vOrientFront;
851 *lpvOrientTop = This->device->ds3dl.vOrientTop;
855 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(
856 LPDIRECTSOUND3DLISTENER iface,
857 LPD3DVECTOR lpvPosition)
859 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
860 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
861 *lpvPosition = This->device->ds3dl.vPosition;
865 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(
866 LPDIRECTSOUND3DLISTENER iface,
867 LPD3DVALUE lpfRolloffFactor)
869 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
870 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
871 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
875 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(
876 LPDIRECTSOUND3DLISTENER iface,
877 LPD3DVECTOR lpvVelocity)
879 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
880 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
881 *lpvVelocity = This->device->ds3dl.vVelocity;
885 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(
886 LPDIRECTSOUND3DLISTENER iface,
887 LPCDS3DLISTENER lpcDS3DL,
890 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
891 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
892 This->device->ds3dl = *lpcDS3DL;
893 if (dwApply == DS3D_IMMEDIATE)
895 This->device->ds3dl_need_recalc = FALSE;
896 DSOUND_ChangeListener(This);
898 This->device->ds3dl_need_recalc = TRUE;
902 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(
903 LPDIRECTSOUND3DLISTENER iface,
904 D3DVALUE fDistanceFactor,
907 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
908 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
909 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
910 if (dwApply == DS3D_IMMEDIATE)
912 This->device->ds3dl_need_recalc = FALSE;
913 DSOUND_ChangeListener(This);
915 This->device->ds3dl_need_recalc = TRUE;
919 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(
920 LPDIRECTSOUND3DLISTENER iface,
921 D3DVALUE fDopplerFactor,
924 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
925 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
926 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
927 if (dwApply == DS3D_IMMEDIATE)
929 This->device->ds3dl_need_recalc = FALSE;
930 DSOUND_ChangeListener(This);
932 This->device->ds3dl_need_recalc = TRUE;
936 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(
937 LPDIRECTSOUND3DLISTENER iface,
938 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront,
939 D3DVALUE xTop, D3DVALUE yTop, D3DVALUE zTop,
942 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
943 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
944 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
945 This->device->ds3dl.vOrientFront.x = xFront;
946 This->device->ds3dl.vOrientFront.y = yFront;
947 This->device->ds3dl.vOrientFront.z = zFront;
948 This->device->ds3dl.vOrientTop.x = xTop;
949 This->device->ds3dl.vOrientTop.y = yTop;
950 This->device->ds3dl.vOrientTop.z = zTop;
951 if (dwApply == DS3D_IMMEDIATE)
953 This->device->ds3dl_need_recalc = FALSE;
954 DSOUND_ChangeListener(This);
956 This->device->ds3dl_need_recalc = TRUE;
960 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(
961 LPDIRECTSOUND3DLISTENER iface,
962 D3DVALUE x, D3DVALUE y, D3DVALUE z,
965 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
966 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
967 This->device->ds3dl.vPosition.x = x;
968 This->device->ds3dl.vPosition.y = y;
969 This->device->ds3dl.vPosition.z = z;
970 if (dwApply == DS3D_IMMEDIATE)
972 This->device->ds3dl_need_recalc = FALSE;
973 DSOUND_ChangeListener(This);
975 This->device->ds3dl_need_recalc = TRUE;
979 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(
980 LPDIRECTSOUND3DLISTENER iface,
981 D3DVALUE fRolloffFactor,
984 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
985 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
986 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
987 if (dwApply == DS3D_IMMEDIATE)
989 This->device->ds3dl_need_recalc = FALSE;
990 DSOUND_ChangeListener(This);
992 This->device->ds3dl_need_recalc = TRUE;
996 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(
997 LPDIRECTSOUND3DLISTENER iface,
998 D3DVALUE x, D3DVALUE y, D3DVALUE z,
1001 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1002 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
1003 This->device->ds3dl.vVelocity.x = x;
1004 This->device->ds3dl.vVelocity.y = y;
1005 This->device->ds3dl.vVelocity.z = z;
1006 if (dwApply == DS3D_IMMEDIATE)
1008 This->device->ds3dl_need_recalc = FALSE;
1009 DSOUND_ChangeListener(This);
1011 This->device->ds3dl_need_recalc = TRUE;
1015 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(
1016 LPDIRECTSOUND3DLISTENER iface)
1018 IDirectSound3DListenerImpl *This = (IDirectSound3DListenerImpl *)iface;
1020 DSOUND_ChangeListener(This);
1024 static const IDirectSound3DListenerVtbl ds3dlvt =
1026 /* IUnknown methods */
1027 IDirectSound3DListenerImpl_QueryInterface,
1028 IDirectSound3DListenerImpl_AddRef,
1029 IDirectSound3DListenerImpl_Release,
1030 /* IDirectSound3DListener methods */
1031 IDirectSound3DListenerImpl_GetAllParameter,
1032 IDirectSound3DListenerImpl_GetDistanceFactor,
1033 IDirectSound3DListenerImpl_GetDopplerFactor,
1034 IDirectSound3DListenerImpl_GetOrientation,
1035 IDirectSound3DListenerImpl_GetPosition,
1036 IDirectSound3DListenerImpl_GetRolloffFactor,
1037 IDirectSound3DListenerImpl_GetVelocity,
1038 IDirectSound3DListenerImpl_SetAllParameters,
1039 IDirectSound3DListenerImpl_SetDistanceFactor,
1040 IDirectSound3DListenerImpl_SetDopplerFactor,
1041 IDirectSound3DListenerImpl_SetOrientation,
1042 IDirectSound3DListenerImpl_SetPosition,
1043 IDirectSound3DListenerImpl_SetRolloffFactor,
1044 IDirectSound3DListenerImpl_SetVelocity,
1045 IDirectSound3DListenerImpl_CommitDeferredSettings,
1048 HRESULT IDirectSound3DListenerImpl_Create(
1049 DirectSoundDevice * device,
1050 IDirectSound3DListenerImpl ** ppdsl)
1052 IDirectSound3DListenerImpl *pdsl;
1053 TRACE("(%p,%p)\n",device,ppdsl);
1055 pdsl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*pdsl));
1058 WARN("out of memory\n");
1060 return DSERR_OUTOFMEMORY;
1064 pdsl->lpVtbl = &ds3dlvt;
1066 pdsl->device = device;
1068 pdsl->device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1069 pdsl->device->ds3dl.vPosition.x = 0.0;
1070 pdsl->device->ds3dl.vPosition.y = 0.0;
1071 pdsl->device->ds3dl.vPosition.z = 0.0;
1072 pdsl->device->ds3dl.vVelocity.x = 0.0;
1073 pdsl->device->ds3dl.vVelocity.y = 0.0;
1074 pdsl->device->ds3dl.vVelocity.z = 0.0;
1075 pdsl->device->ds3dl.vOrientFront.x = 0.0;
1076 pdsl->device->ds3dl.vOrientFront.y = 0.0;
1077 pdsl->device->ds3dl.vOrientFront.z = 1.0;
1078 pdsl->device->ds3dl.vOrientTop.x = 0.0;
1079 pdsl->device->ds3dl.vOrientTop.y = 1.0;
1080 pdsl->device->ds3dl.vOrientTop.z = 0.0;
1081 pdsl->device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1082 pdsl->device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1083 pdsl->device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1085 pdsl->device->ds3dl_need_recalc = TRUE;