dsound: If 3d sound is disabled, just return (Coverity).
[wine.git] / dlls / dsound / sound3d.c
blob439cfaca9eeb17fbf37f39cc6f68bb1385d80667
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., 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+.
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 <stdarg.h>
41 #include <math.h> /* Insomnia - pow() function */
43 #define NONAMELESSUNION
44 #define NONAMELESSSTRUCT
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winuser.h"
48 #include "mmsystem.h"
49 #include "winternl.h"
50 #include "mmddk.h"
51 #include "wine/debug.h"
52 #include "dsound.h"
53 #include "dsound_private.h"
55 /* default velocity of sound in the air */
56 #define DEFAULT_VELOCITY 340
58 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
60 /*******************************************************************************
61 * Auxiliary functions
64 /* scalar product (I believe it's called dot product in English) */
65 static inline D3DVALUE ScalarProduct (const D3DVECTOR *a, const D3DVECTOR *b)
67 D3DVALUE c;
68 c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
69 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
70 b->z, c);
71 return c;
74 /* vector product (I believe it's called cross product in English */
75 static inline D3DVECTOR VectorProduct (const D3DVECTOR *a, const D3DVECTOR *b)
77 D3DVECTOR c;
78 c.x = (a->y*b->z) - (a->z*b->y);
79 c.y = (a->z*b->x) - (a->x*b->z);
80 c.z = (a->x*b->y) - (a->y*b->x);
81 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
82 b->z, c.x, c.y, c.z);
83 return c;
86 /* magnitude (length) of vector */
87 static inline D3DVALUE VectorMagnitude (const D3DVECTOR *a)
89 D3DVALUE l;
90 l = sqrt (ScalarProduct (a, a));
91 TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
92 return l;
95 /* conversion between radians and degrees */
96 static inline D3DVALUE RadToDeg (D3DVALUE angle)
98 D3DVALUE newangle;
99 newangle = angle * (360/(2*M_PI));
100 TRACE("%f rad = %f deg\n", angle, newangle);
101 return newangle;
104 /* angle between vectors - rad version */
105 static inline D3DVALUE AngleBetweenVectorsRad (const D3DVECTOR *a, const D3DVECTOR *b)
107 D3DVALUE la, lb, product, angle, cos;
108 /* definition of scalar product: a*b = |a|*|b|*cos... therefore: */
109 product = ScalarProduct (a,b);
110 la = VectorMagnitude (a);
111 lb = VectorMagnitude (b);
112 if (!la || !lb)
113 return 0;
115 cos = product/(la*lb);
116 angle = acos(cos);
117 if (cos < 0.0f) { angle -= M_PI; }
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));
120 return 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)
131 D3DVECTOR c;
132 c.x = b->x - a->x;
133 c.y = b->y - a->y;
134 c.z = b->z - a->z;
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);
137 return c;
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,
147 p->y, p->z, result);
148 return result;
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. */
160 D3DVECTOR vDistance;
161 D3DVALUE flDistance = 0;
162 /* panning related stuff */
163 D3DVALUE flAngle, flAngle2;
164 D3DVECTOR vLeft;
165 int i, num_main_speakers;
166 float a, ingain;
167 /* doppler shift related stuff */
169 TRACE("(%p)\n",dsb);
171 /* initial buffer volume */
172 lVolume = dsb->ds3db_lVolume;
174 switch (dsb->ds3db_ds3db.dwMode)
176 case DS3DMODE_DISABLE:
177 TRACE("3D processing disabled\n");
178 /* this one is here only to eliminate annoying warning message */
179 DSOUND_RecalcVolPan (&dsb->volpan);
180 return;
181 case DS3DMODE_NORMAL:
182 TRACE("Normal 3D processing mode\n");
183 /* we need to calculate distance between buffer and listener*/
184 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
185 flDistance = VectorMagnitude (&vDistance);
186 break;
187 case DS3DMODE_HEADRELATIVE:
188 TRACE("Head-relative 3D processing mode\n");
189 /* distance between buffer and listener is same as buffer's position */
190 vDistance = dsb->ds3db_ds3db.vPosition;
191 flDistance = VectorMagnitude (&vDistance);
192 break;
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 */
203 return;
205 else
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);
216 /* conning */
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");
222 else
224 D3DVECTOR vDistanceInv;
226 vDistanceInv.x = -vDistance.x;
227 vDistanceInv.y = -vDistance.y;
228 vDistanceInv.z = -vDistance.z;
230 /* calculate angle */
231 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistanceInv);
232 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
233 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
235 /* my test show that for my way of calc., we need only half of angles */
236 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
237 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
238 if (dwOutsideConeAngle == dwInsideConeAngle)
239 ++dwOutsideConeAngle;
241 /* full volume */
242 if (flAngle < dwInsideConeAngle)
243 flAngle = dwInsideConeAngle;
244 /* min (app defined) volume */
245 if (flAngle > dwOutsideConeAngle)
246 flAngle = dwOutsideConeAngle;
247 /* this probably isn't the right thing, but it's ok for the time being */
248 lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
250 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
251 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
253 dsb->volpan.lVolume = lVolume;
255 ingain = pow(2.0, dsb->volpan.lVolume / 600.0) * 0xffff;
257 if (dsb->device->pwfx->nChannels == 1)
259 dsb->volpan.dwTotalAmpFactor[0] = ingain;
260 return;
263 /* panning */
264 if (vDistance.x == 0.0f && vDistance.y == 0.0f && vDistance.z == 0.0f)
265 flAngle = 0.0;
266 else
268 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
269 flAngle = AngleBetweenVectorsRad(&dsb->device->ds3dl.vOrientFront, &vDistance);
270 flAngle2 = AngleBetweenVectorsRad(&vLeft, &vDistance);
272 /* AngleBetweenVectorsRad performs a dot product, which gives us the cosine of the angle
273 * between two vectors. Unfortunately, because cos(theta) = cos(-theta), we've no idea from
274 * this whether the sound is to our left or to our right. We have to perform another dot
275 * product, with a vector at right angles to the initial one, to get the correct angle.
276 * The angle should be between -180 degrees and 180 degrees. */
277 if (flAngle < 0.0f) { flAngle += M_PI; }
278 if (flAngle2 > 0.0f) { flAngle = -flAngle; }
280 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
282 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
283 if(0)
285 D3DVALUE flFreq, flBufferVel, flListenerVel;
286 /* doppler shift*/
287 if (!VectorMagnitude(&dsb->ds3db_ds3db.vVelocity) && !VectorMagnitude(&dsb->device->ds3dl.vVelocity))
289 TRACE("doppler: Buffer and Listener don't have velocities\n");
291 else if (!(dsb->ds3db_ds3db.vVelocity.x == dsb->device->ds3dl.vVelocity.x &&
292 dsb->ds3db_ds3db.vVelocity.y == dsb->device->ds3dl.vVelocity.y &&
293 dsb->ds3db_ds3db.vVelocity.z == dsb->device->ds3dl.vVelocity.z))
295 /* calculate length of ds3db_ds3db.vVelocity component which causes Doppler Effect
296 NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
297 if buffer moves AWAY from listener, it's velocity component is POSITIVE */
298 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
299 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
300 NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
301 if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
302 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
303 /* formula taken from Gianicoli D.: Physics, 4th edition: */
304 /* FIXME: replace dsb->freq with appropriate frequency ! */
305 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
306 TRACE("doppler: Buffer velocity (component) = %f, Listener velocity (component) = %f => Doppler shift: %d Hz -> %f Hz\n",
307 flBufferVel, flListenerVel, dsb->freq, flFreq);
308 /* FIXME: replace following line with correct frequency setting ! */
309 dsb->freq = flFreq;
310 DSOUND_RecalcFormat(dsb);
314 for (i = 0; i < dsb->device->pwfx->nChannels; i++)
315 dsb->volpan.dwTotalAmpFactor[i] = 0;
317 num_main_speakers = dsb->device->pwfx->nChannels;
319 if (dsb->device->lfe_channel != -1) {
320 dsb->volpan.dwTotalAmpFactor[dsb->device->lfe_channel] = ingain;
321 num_main_speakers--;
324 /* adapted from OpenAL's Alc/panning.c */
325 for (i = 0; i < num_main_speakers - 1; i++)
327 if(flAngle >= dsb->device->speaker_angles[i] && flAngle < dsb->device->speaker_angles[i+1])
329 /* Sound is between speakers i and i+1 */
330 a = (flAngle-dsb->device->speaker_angles[i]) / (dsb->device->speaker_angles[i+1]-dsb->device->speaker_angles[i]);
331 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i]] = sqrtf(1.0f-a) * ingain;
332 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i+1]] = sqrtf(a) * ingain;
333 return;
337 /* Sound is between last and first speakers */
338 if (flAngle < dsb->device->speaker_angles[0]) { flAngle += M_PI*2.0f; }
339 a = (flAngle-dsb->device->speaker_angles[i]) / (M_PI*2.0f + dsb->device->speaker_angles[0]-dsb->device->speaker_angles[i]);
340 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i]] = sqrtf(1.0f-a) * ingain;
341 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[0]] = sqrtf(a) * ingain;
344 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
346 TRACE("(%p)\n",dsb);
348 DSOUND_Calc3DBuffer(dsb);
351 static void DSOUND_ChangeListener(IDirectSoundBufferImpl *ds3dl)
353 int i;
354 TRACE("(%p)\n",ds3dl);
355 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
357 /* check if this buffer is waiting for recalculation */
358 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
360 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
365 /*******************************************************************************
366 * IDirectSound3DBuffer
368 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
370 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DBuffer_iface);
373 /* IUnknown methods */
374 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(IDirectSound3DBuffer *iface,
375 REFIID riid, void **ppobj)
377 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
379 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
381 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
384 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(IDirectSound3DBuffer *iface)
386 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
387 ULONG ref = InterlockedIncrement(&This->ref3D);
389 TRACE("(%p) ref was %d\n", This, ref - 1);
391 if(ref == 1)
392 InterlockedIncrement(&This->numIfaces);
394 return ref;
397 static ULONG WINAPI IDirectSound3DBufferImpl_Release(IDirectSound3DBuffer *iface)
399 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
400 ULONG ref = InterlockedDecrement(&This->ref3D);
402 TRACE("(%p) ref was %d\n", This, ref + 1);
404 if (!ref && !InterlockedDecrement(&This->numIfaces))
405 secondarybuffer_destroy(This);
407 return ref;
410 /* IDirectSound3DBuffer methods */
411 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(IDirectSound3DBuffer *iface,
412 DS3DBUFFER *lpDs3dBuffer)
414 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
416 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
418 if (lpDs3dBuffer == NULL) {
419 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
420 return DSERR_INVALIDPARAM;
423 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
424 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
425 return DSERR_INVALIDPARAM;
428 TRACE("returning: all parameters\n");
429 *lpDs3dBuffer = This->ds3db_ds3db;
430 return DS_OK;
433 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(IDirectSound3DBuffer *iface,
434 DWORD *lpdwInsideConeAngle, DWORD *lpdwOutsideConeAngle)
436 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
438 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
439 This->ds3db_ds3db.dwInsideConeAngle, This->ds3db_ds3db.dwOutsideConeAngle);
440 *lpdwInsideConeAngle = This->ds3db_ds3db.dwInsideConeAngle;
441 *lpdwOutsideConeAngle = This->ds3db_ds3db.dwOutsideConeAngle;
442 return DS_OK;
445 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(IDirectSound3DBuffer *iface,
446 D3DVECTOR *lpvConeOrientation)
448 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
450 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
451 This->ds3db_ds3db.vConeOrientation.x,
452 This->ds3db_ds3db.vConeOrientation.y,
453 This->ds3db_ds3db.vConeOrientation.z);
454 *lpvConeOrientation = This->ds3db_ds3db.vConeOrientation;
455 return DS_OK;
458 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(IDirectSound3DBuffer *iface,
459 LONG *lplConeOutsideVolume)
461 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
463 TRACE("returning: Cone Outside Volume = %d\n", This->ds3db_ds3db.lConeOutsideVolume);
464 *lplConeOutsideVolume = This->ds3db_ds3db.lConeOutsideVolume;
465 return DS_OK;
468 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(IDirectSound3DBuffer *iface,
469 D3DVALUE *lpfMaxDistance)
471 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
473 TRACE("returning: Max Distance = %f\n", This->ds3db_ds3db.flMaxDistance);
474 *lpfMaxDistance = This->ds3db_ds3db.flMaxDistance;
475 return DS_OK;
478 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(IDirectSound3DBuffer *iface,
479 D3DVALUE *lpfMinDistance)
481 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
483 TRACE("returning: Min Distance = %f\n", This->ds3db_ds3db.flMinDistance);
484 *lpfMinDistance = This->ds3db_ds3db.flMinDistance;
485 return DS_OK;
488 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(IDirectSound3DBuffer *iface,
489 DWORD *lpdwMode)
491 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
493 TRACE("returning: Mode = %d\n", This->ds3db_ds3db.dwMode);
494 *lpdwMode = This->ds3db_ds3db.dwMode;
495 return DS_OK;
498 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(IDirectSound3DBuffer *iface,
499 D3DVECTOR *lpvPosition)
501 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
503 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3db_ds3db.vPosition.x,
504 This->ds3db_ds3db.vPosition.y, This->ds3db_ds3db.vPosition.z);
505 *lpvPosition = This->ds3db_ds3db.vPosition;
506 return DS_OK;
509 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(IDirectSound3DBuffer *iface,
510 D3DVECTOR *lpvVelocity)
512 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
514 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3db_ds3db.vVelocity.x,
515 This->ds3db_ds3db.vVelocity.y, This->ds3db_ds3db.vVelocity.z);
516 *lpvVelocity = This->ds3db_ds3db.vVelocity;
517 return DS_OK;
520 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(IDirectSound3DBuffer *iface,
521 const DS3DBUFFER *lpcDs3dBuffer, DWORD dwApply)
523 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
524 DWORD status = DSERR_INVALIDPARAM;
526 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
528 if (lpcDs3dBuffer == NULL) {
529 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
530 return status;
533 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
534 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
535 return status;
538 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
539 This->ds3db_ds3db = *lpcDs3dBuffer;
541 if (dwApply == DS3D_IMMEDIATE)
543 DSOUND_Mix3DBuffer(This);
545 This->ds3db_need_recalc = TRUE;
546 status = DS_OK;
548 return status;
551 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(IDirectSound3DBuffer *iface,
552 DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD dwApply)
554 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
556 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
557 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
558 This->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
559 This->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
560 if (dwApply == DS3D_IMMEDIATE)
561 DSOUND_Mix3DBuffer(This);
562 This->ds3db_need_recalc = TRUE;
563 return DS_OK;
566 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(IDirectSound3DBuffer *iface,
567 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
569 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
571 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
572 This->ds3db_ds3db.vConeOrientation.x = x;
573 This->ds3db_ds3db.vConeOrientation.y = y;
574 This->ds3db_ds3db.vConeOrientation.z = z;
575 if (dwApply == DS3D_IMMEDIATE)
577 This->ds3db_need_recalc = FALSE;
578 DSOUND_Mix3DBuffer(This);
580 This->ds3db_need_recalc = TRUE;
581 return DS_OK;
584 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(IDirectSound3DBuffer *iface,
585 LONG lConeOutsideVolume, DWORD dwApply)
587 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
589 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
590 This->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
591 if (dwApply == DS3D_IMMEDIATE)
593 This->ds3db_need_recalc = FALSE;
594 DSOUND_Mix3DBuffer(This);
596 This->ds3db_need_recalc = TRUE;
597 return DS_OK;
600 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(IDirectSound3DBuffer *iface,
601 D3DVALUE fMaxDistance, DWORD dwApply)
603 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
605 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
606 This->ds3db_ds3db.flMaxDistance = fMaxDistance;
607 if (dwApply == DS3D_IMMEDIATE)
609 This->ds3db_need_recalc = FALSE;
610 DSOUND_Mix3DBuffer(This);
612 This->ds3db_need_recalc = TRUE;
613 return DS_OK;
616 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(IDirectSound3DBuffer *iface,
617 D3DVALUE fMinDistance, DWORD dwApply)
619 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
621 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
622 This->ds3db_ds3db.flMinDistance = fMinDistance;
623 if (dwApply == DS3D_IMMEDIATE)
625 This->ds3db_need_recalc = FALSE;
626 DSOUND_Mix3DBuffer(This);
628 This->ds3db_need_recalc = TRUE;
629 return DS_OK;
632 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(IDirectSound3DBuffer *iface, DWORD dwMode,
633 DWORD dwApply)
635 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
637 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
638 This->ds3db_ds3db.dwMode = dwMode;
639 if (dwApply == DS3D_IMMEDIATE)
641 This->ds3db_need_recalc = FALSE;
642 DSOUND_Mix3DBuffer(This);
644 This->ds3db_need_recalc = TRUE;
645 return DS_OK;
648 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x,
649 D3DVALUE y, D3DVALUE z, DWORD dwApply)
651 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
653 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
654 This->ds3db_ds3db.vPosition.x = x;
655 This->ds3db_ds3db.vPosition.y = y;
656 This->ds3db_ds3db.vPosition.z = z;
657 if (dwApply == DS3D_IMMEDIATE)
659 This->ds3db_need_recalc = FALSE;
660 DSOUND_Mix3DBuffer(This);
662 This->ds3db_need_recalc = TRUE;
663 return DS_OK;
666 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(IDirectSound3DBuffer *iface,
667 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
669 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
671 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
672 This->ds3db_ds3db.vVelocity.x = x;
673 This->ds3db_ds3db.vVelocity.y = y;
674 This->ds3db_ds3db.vVelocity.z = z;
675 if (dwApply == DS3D_IMMEDIATE)
677 This->ds3db_need_recalc = FALSE;
678 DSOUND_Mix3DBuffer(This);
680 This->ds3db_need_recalc = TRUE;
681 return DS_OK;
684 const IDirectSound3DBufferVtbl ds3dbvt =
686 /* IUnknown methods */
687 IDirectSound3DBufferImpl_QueryInterface,
688 IDirectSound3DBufferImpl_AddRef,
689 IDirectSound3DBufferImpl_Release,
690 /* IDirectSound3DBuffer methods */
691 IDirectSound3DBufferImpl_GetAllParameters,
692 IDirectSound3DBufferImpl_GetConeAngles,
693 IDirectSound3DBufferImpl_GetConeOrientation,
694 IDirectSound3DBufferImpl_GetConeOutsideVolume,
695 IDirectSound3DBufferImpl_GetMaxDistance,
696 IDirectSound3DBufferImpl_GetMinDistance,
697 IDirectSound3DBufferImpl_GetMode,
698 IDirectSound3DBufferImpl_GetPosition,
699 IDirectSound3DBufferImpl_GetVelocity,
700 IDirectSound3DBufferImpl_SetAllParameters,
701 IDirectSound3DBufferImpl_SetConeAngles,
702 IDirectSound3DBufferImpl_SetConeOrientation,
703 IDirectSound3DBufferImpl_SetConeOutsideVolume,
704 IDirectSound3DBufferImpl_SetMaxDistance,
705 IDirectSound3DBufferImpl_SetMinDistance,
706 IDirectSound3DBufferImpl_SetMode,
707 IDirectSound3DBufferImpl_SetPosition,
708 IDirectSound3DBufferImpl_SetVelocity,
712 /*******************************************************************************
713 * IDirectSound3DListener
715 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
717 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DListener_iface);
721 /* IUnknown methods */
722 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(IDirectSound3DListener *iface,
723 REFIID riid, void **ppobj)
725 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
727 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
729 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
732 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(IDirectSound3DListener *iface)
734 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
735 ULONG ref = InterlockedIncrement(&This->ref3D);
737 TRACE("(%p) ref was %d\n", This, ref - 1);
739 if(ref == 1)
740 InterlockedIncrement(&This->numIfaces);
742 return ref;
745 static ULONG WINAPI IDirectSound3DListenerImpl_Release(IDirectSound3DListener *iface)
747 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
748 ULONG ref;
750 ref = capped_refcount_dec(&This->ref3D);
751 if(!ref)
752 capped_refcount_dec(&This->numIfaces);
754 TRACE("(%p) ref is now %d\n", This, ref);
756 return ref;
759 /* IDirectSound3DListener methods */
760 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
761 DS3DLISTENER *lpDS3DL)
763 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
765 TRACE("(%p,%p)\n",This,lpDS3DL);
767 if (lpDS3DL == NULL) {
768 WARN("invalid parameter: lpDS3DL == NULL\n");
769 return DSERR_INVALIDPARAM;
772 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
773 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
774 return DSERR_INVALIDPARAM;
777 TRACE("returning: all parameters\n");
778 *lpDS3DL = This->device->ds3dl;
779 return DS_OK;
782 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
783 D3DVALUE *lpfDistanceFactor)
785 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
787 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
788 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
789 return DS_OK;
792 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
793 D3DVALUE *lpfDopplerFactor)
795 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
797 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
798 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
799 return DS_OK;
802 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
803 D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
805 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
807 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
808 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
809 This->device->ds3dl.vOrientTop.z);
810 *lpvOrientFront = This->device->ds3dl.vOrientFront;
811 *lpvOrientTop = This->device->ds3dl.vOrientTop;
812 return DS_OK;
815 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
816 D3DVECTOR *lpvPosition)
818 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
820 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
821 *lpvPosition = This->device->ds3dl.vPosition;
822 return DS_OK;
825 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
826 D3DVALUE *lpfRolloffFactor)
828 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
830 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
831 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
832 return DS_OK;
835 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
836 D3DVECTOR *lpvVelocity)
838 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
840 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
841 *lpvVelocity = This->device->ds3dl.vVelocity;
842 return DS_OK;
845 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
846 const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
848 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
850 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
851 This->device->ds3dl = *lpcDS3DL;
852 if (dwApply == DS3D_IMMEDIATE)
854 This->device->ds3dl_need_recalc = FALSE;
855 DSOUND_ChangeListener(This);
857 This->device->ds3dl_need_recalc = TRUE;
858 return DS_OK;
861 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
862 D3DVALUE fDistanceFactor, DWORD dwApply)
864 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
866 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
867 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
868 if (dwApply == DS3D_IMMEDIATE)
870 This->device->ds3dl_need_recalc = FALSE;
871 DSOUND_ChangeListener(This);
873 This->device->ds3dl_need_recalc = TRUE;
874 return DS_OK;
877 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
878 D3DVALUE fDopplerFactor, DWORD dwApply)
880 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
882 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
883 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
884 if (dwApply == DS3D_IMMEDIATE)
886 This->device->ds3dl_need_recalc = FALSE;
887 DSOUND_ChangeListener(This);
889 This->device->ds3dl_need_recalc = TRUE;
890 return DS_OK;
893 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
894 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
895 D3DVALUE zTop, DWORD dwApply)
897 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
899 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
900 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
901 This->device->ds3dl.vOrientFront.x = xFront;
902 This->device->ds3dl.vOrientFront.y = yFront;
903 This->device->ds3dl.vOrientFront.z = zFront;
904 This->device->ds3dl.vOrientTop.x = xTop;
905 This->device->ds3dl.vOrientTop.y = yTop;
906 This->device->ds3dl.vOrientTop.z = zTop;
907 if (dwApply == DS3D_IMMEDIATE)
909 This->device->ds3dl_need_recalc = FALSE;
910 DSOUND_ChangeListener(This);
912 This->device->ds3dl_need_recalc = TRUE;
913 return DS_OK;
916 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
917 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
919 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
921 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
922 This->device->ds3dl.vPosition.x = x;
923 This->device->ds3dl.vPosition.y = y;
924 This->device->ds3dl.vPosition.z = z;
925 if (dwApply == DS3D_IMMEDIATE)
927 This->device->ds3dl_need_recalc = FALSE;
928 DSOUND_ChangeListener(This);
930 This->device->ds3dl_need_recalc = TRUE;
931 return DS_OK;
934 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
935 D3DVALUE fRolloffFactor, DWORD dwApply)
937 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
939 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
940 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
941 if (dwApply == DS3D_IMMEDIATE)
943 This->device->ds3dl_need_recalc = FALSE;
944 DSOUND_ChangeListener(This);
946 This->device->ds3dl_need_recalc = TRUE;
947 return DS_OK;
950 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
951 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
953 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
955 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
956 This->device->ds3dl.vVelocity.x = x;
957 This->device->ds3dl.vVelocity.y = y;
958 This->device->ds3dl.vVelocity.z = z;
959 if (dwApply == DS3D_IMMEDIATE)
961 This->device->ds3dl_need_recalc = FALSE;
962 DSOUND_ChangeListener(This);
964 This->device->ds3dl_need_recalc = TRUE;
965 return DS_OK;
968 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
970 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
972 TRACE("\n");
973 DSOUND_ChangeListener(This);
974 return DS_OK;
977 const IDirectSound3DListenerVtbl ds3dlvt =
979 /* IUnknown methods */
980 IDirectSound3DListenerImpl_QueryInterface,
981 IDirectSound3DListenerImpl_AddRef,
982 IDirectSound3DListenerImpl_Release,
983 /* IDirectSound3DListener methods */
984 IDirectSound3DListenerImpl_GetAllParameter,
985 IDirectSound3DListenerImpl_GetDistanceFactor,
986 IDirectSound3DListenerImpl_GetDopplerFactor,
987 IDirectSound3DListenerImpl_GetOrientation,
988 IDirectSound3DListenerImpl_GetPosition,
989 IDirectSound3DListenerImpl_GetRolloffFactor,
990 IDirectSound3DListenerImpl_GetVelocity,
991 IDirectSound3DListenerImpl_SetAllParameters,
992 IDirectSound3DListenerImpl_SetDistanceFactor,
993 IDirectSound3DListenerImpl_SetDopplerFactor,
994 IDirectSound3DListenerImpl_SetOrientation,
995 IDirectSound3DListenerImpl_SetPosition,
996 IDirectSound3DListenerImpl_SetRolloffFactor,
997 IDirectSound3DListenerImpl_SetVelocity,
998 IDirectSound3DListenerImpl_CommitDeferredSettings,