oleaut32: Remove unneeded NONAMELESSXXX directives.
[wine.git] / dlls / dsound / sound3d.c
blob9a0226a69a3fe671a39f2d9640c78ee9d2cfc96c
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 #include "windef.h"
44 #include "winbase.h"
45 #include "winuser.h"
46 #include "mmsystem.h"
47 #include "winternl.h"
48 #include "mmddk.h"
49 #include "wine/debug.h"
50 #include "dsound.h"
51 #include "dsound_private.h"
53 /* default velocity of sound in the air */
54 #define DEFAULT_VELOCITY 340
56 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
58 /*******************************************************************************
59 * Auxiliary functions
62 /* scalar product (I believe it's called dot product in English) */
63 static inline D3DVALUE ScalarProduct (const D3DVECTOR *a, const D3DVECTOR *b)
65 D3DVALUE c;
66 c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
67 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
68 b->z, c);
69 return c;
72 /* vector product (I believe it's called cross product in English */
73 static inline D3DVECTOR VectorProduct (const D3DVECTOR *a, const D3DVECTOR *b)
75 D3DVECTOR c;
76 c.x = (a->y*b->z) - (a->z*b->y);
77 c.y = (a->z*b->x) - (a->x*b->z);
78 c.z = (a->x*b->y) - (a->y*b->x);
79 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
80 b->z, c.x, c.y, c.z);
81 return c;
84 /* magnitude (length) of vector */
85 static inline D3DVALUE VectorMagnitude (const D3DVECTOR *a)
87 D3DVALUE l;
88 l = sqrt (ScalarProduct (a, a));
89 TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
90 return l;
93 /* conversion between radians and degrees */
94 static inline D3DVALUE RadToDeg (D3DVALUE angle)
96 D3DVALUE newangle;
97 newangle = angle * (360/(2*M_PI));
98 TRACE("%f rad = %f deg\n", angle, newangle);
99 return newangle;
102 /* angle between vectors - rad version */
103 static inline D3DVALUE AngleBetweenVectorsRad (const D3DVECTOR *a, const D3DVECTOR *b)
105 D3DVALUE la, lb, product, angle, cos;
106 /* definition of scalar product: a*b = |a|*|b|*cos... therefore: */
107 product = ScalarProduct (a,b);
108 la = VectorMagnitude (a);
109 lb = VectorMagnitude (b);
110 if (!la || !lb)
111 return 0;
113 cos = product/(la*lb);
114 angle = acos(cos);
115 if (cos < 0.0f) { angle -= M_PI; }
116 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians (%f degrees)\n", a->x, a->y, a->z, b->x,
117 b->y, b->z, angle, RadToDeg(angle));
118 return angle;
121 static inline D3DVALUE AngleBetweenVectorsDeg (const D3DVECTOR *a, const D3DVECTOR *b)
123 return RadToDeg(AngleBetweenVectorsRad(a, b));
126 /* calculates vector between two points */
127 static inline D3DVECTOR VectorBetweenTwoPoints (const D3DVECTOR *a, const D3DVECTOR *b)
129 D3DVECTOR c;
130 c.x = b->x - a->x;
131 c.y = b->y - a->y;
132 c.z = b->z - a->z;
133 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
134 b->z, c.x, c.y, c.z);
135 return c;
138 /* calculates the length of vector's projection on another vector */
139 static inline D3DVALUE ProjectVector (const D3DVECTOR *a, const D3DVECTOR *p)
141 D3DVALUE prod, result;
142 prod = ScalarProduct(a, p);
143 result = prod/VectorMagnitude(p);
144 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
145 p->y, p->z, result);
146 return result;
149 /*******************************************************************************
150 * 3D Buffer and Listener mixing
153 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
155 /* volume, at which the sound will be played after all calcs. */
156 D3DVALUE lVolume = 0;
157 /* stuff for distance related stuff calc. */
158 D3DVECTOR vDistance;
159 D3DVALUE flDistance = 0;
160 /* panning related stuff */
161 D3DVALUE flAngle, flAngle2;
162 D3DVECTOR vLeft;
163 int i, num_main_speakers;
164 float a, ingain;
165 /* doppler shift related stuff */
167 TRACE("(%p)\n",dsb);
169 /* initial buffer volume */
170 lVolume = dsb->ds3db_lVolume;
172 switch (dsb->ds3db_ds3db.dwMode)
174 case DS3DMODE_DISABLE:
175 TRACE("3D processing disabled\n");
176 /* this one is here only to eliminate annoying warning message */
177 DSOUND_RecalcVolPan (&dsb->volpan);
178 return;
179 case DS3DMODE_NORMAL:
180 TRACE("Normal 3D processing mode\n");
181 /* we need to calculate distance between buffer and listener*/
182 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
183 flDistance = VectorMagnitude (&vDistance);
184 break;
185 case DS3DMODE_HEADRELATIVE:
186 TRACE("Head-relative 3D processing mode\n");
187 /* distance between buffer and listener is same as buffer's position */
188 vDistance = dsb->ds3db_ds3db.vPosition;
189 flDistance = VectorMagnitude (&vDistance);
190 break;
193 if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
195 /* some apps don't want you to hear too distant sounds... */
196 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
198 dsb->volpan.lVolume = DSBVOLUME_MIN;
199 DSOUND_RecalcVolPan (&dsb->volpan);
200 /* i guess mixing here would be a waste of power */
201 return;
203 else
204 flDistance = dsb->ds3db_ds3db.flMaxDistance;
207 if (flDistance < dsb->ds3db_ds3db.flMinDistance)
208 flDistance = dsb->ds3db_ds3db.flMinDistance;
210 /* attenuation proportional to the distance squared, converted to millibels as in lVolume*/
211 lVolume -= log10(flDistance/dsb->ds3db_ds3db.flMinDistance * flDistance/dsb->ds3db_ds3db.flMinDistance)*1000;
212 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
214 /* conning */
215 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
216 if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
218 TRACE("conning: cones not set\n");
220 else
222 D3DVECTOR vDistanceInv;
224 vDistanceInv.x = -vDistance.x;
225 vDistanceInv.y = -vDistance.y;
226 vDistanceInv.z = -vDistance.z;
228 /* calculate angle */
229 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistanceInv);
230 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
231 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
233 /* my test show that for my way of calc., we need only half of angles */
234 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
235 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
236 if (dwOutsideConeAngle == dwInsideConeAngle)
237 ++dwOutsideConeAngle;
239 /* full volume */
240 if (flAngle < dwInsideConeAngle)
241 flAngle = dwInsideConeAngle;
242 /* min (app defined) volume */
243 if (flAngle > dwOutsideConeAngle)
244 flAngle = dwOutsideConeAngle;
245 /* this probably isn't the right thing, but it's ok for the time being */
246 lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;
248 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
249 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
251 dsb->volpan.lVolume = lVolume;
253 ingain = pow(2.0, dsb->volpan.lVolume / 600.0) * 0xffff;
255 if (dsb->device->pwfx->nChannels == 1)
257 dsb->volpan.dwTotalAmpFactor[0] = ingain;
258 return;
261 /* panning */
262 if (vDistance.x == 0.0f && vDistance.y == 0.0f && vDistance.z == 0.0f)
263 flAngle = 0.0;
264 else
266 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
267 flAngle = AngleBetweenVectorsRad(&dsb->device->ds3dl.vOrientFront, &vDistance);
268 flAngle2 = AngleBetweenVectorsRad(&vLeft, &vDistance);
270 /* AngleBetweenVectorsRad performs a dot product, which gives us the cosine of the angle
271 * between two vectors. Unfortunately, because cos(theta) = cos(-theta), we've no idea from
272 * this whether the sound is to our left or to our right. We have to perform another dot
273 * product, with a vector at right angles to the initial one, to get the correct angle.
274 * The angle should be between -180 degrees and 180 degrees. */
275 if (flAngle < 0.0f) { flAngle += M_PI; }
276 if (flAngle2 > 0.0f) { flAngle = -flAngle; }
278 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
280 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
281 if(0)
283 D3DVALUE flFreq, flBufferVel, flListenerVel;
284 /* doppler shift*/
285 if (!VectorMagnitude(&dsb->ds3db_ds3db.vVelocity) && !VectorMagnitude(&dsb->device->ds3dl.vVelocity))
287 TRACE("doppler: Buffer and Listener don't have velocities\n");
289 else if (!(dsb->ds3db_ds3db.vVelocity.x == dsb->device->ds3dl.vVelocity.x &&
290 dsb->ds3db_ds3db.vVelocity.y == dsb->device->ds3dl.vVelocity.y &&
291 dsb->ds3db_ds3db.vVelocity.z == dsb->device->ds3dl.vVelocity.z))
293 /* calculate length of ds3db_ds3db.vVelocity component which causes Doppler Effect
294 NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE
295 if buffer moves AWAY from listener, it's velocity component is POSITIVE */
296 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
297 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
298 NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE
299 if listener moves AWAY from buffer, it's velocity component is NEGATIVE */
300 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
301 /* formula taken from Gianicoli D.: Physics, 4th edition: */
302 /* FIXME: replace dsb->freq with appropriate frequency ! */
303 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
304 TRACE("doppler: Buffer velocity (component) = %f, Listener velocity (component) = %f => Doppler shift: %d Hz -> %f Hz\n",
305 flBufferVel, flListenerVel, dsb->freq, flFreq);
306 /* FIXME: replace following line with correct frequency setting ! */
307 dsb->freq = flFreq;
308 DSOUND_RecalcFormat(dsb);
312 for (i = 0; i < dsb->device->pwfx->nChannels; i++)
313 dsb->volpan.dwTotalAmpFactor[i] = 0;
315 num_main_speakers = dsb->device->pwfx->nChannels;
317 if (dsb->device->lfe_channel != -1) {
318 dsb->volpan.dwTotalAmpFactor[dsb->device->lfe_channel] = ingain;
319 num_main_speakers--;
322 /* adapted from OpenAL's Alc/panning.c */
323 for (i = 0; i < num_main_speakers - 1; i++)
325 if(flAngle >= dsb->device->speaker_angles[i] && flAngle < dsb->device->speaker_angles[i+1])
327 /* Sound is between speakers i and i+1 */
328 a = (flAngle-dsb->device->speaker_angles[i]) / (dsb->device->speaker_angles[i+1]-dsb->device->speaker_angles[i]);
329 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i]] = sqrtf(1.0f-a) * ingain;
330 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i+1]] = sqrtf(a) * ingain;
331 return;
335 /* Sound is between last and first speakers */
336 if (flAngle < dsb->device->speaker_angles[0]) { flAngle += M_PI*2.0f; }
337 a = (flAngle-dsb->device->speaker_angles[i]) / (M_PI*2.0f + dsb->device->speaker_angles[0]-dsb->device->speaker_angles[i]);
338 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i]] = sqrtf(1.0f-a) * ingain;
339 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[0]] = sqrtf(a) * ingain;
342 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
344 TRACE("(%p)\n",dsb);
346 DSOUND_Calc3DBuffer(dsb);
349 static void DSOUND_ChangeListener(IDirectSoundBufferImpl *ds3dl)
351 int i;
352 TRACE("(%p)\n",ds3dl);
353 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
355 /* check if this buffer is waiting for recalculation */
356 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
358 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
363 /*******************************************************************************
364 * IDirectSound3DBuffer
366 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
368 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DBuffer_iface);
371 /* IUnknown methods */
372 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(IDirectSound3DBuffer *iface,
373 REFIID riid, void **ppobj)
375 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
377 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
379 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
382 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(IDirectSound3DBuffer *iface)
384 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
385 ULONG ref = InterlockedIncrement(&This->ref3D);
387 TRACE("(%p) ref was %d\n", This, ref - 1);
389 if(ref == 1)
390 InterlockedIncrement(&This->numIfaces);
392 return ref;
395 static ULONG WINAPI IDirectSound3DBufferImpl_Release(IDirectSound3DBuffer *iface)
397 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
398 ULONG ref = InterlockedDecrement(&This->ref3D);
400 TRACE("(%p) ref was %d\n", This, ref + 1);
402 if (!ref && !InterlockedDecrement(&This->numIfaces))
403 secondarybuffer_destroy(This);
405 return ref;
408 /* IDirectSound3DBuffer methods */
409 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(IDirectSound3DBuffer *iface,
410 DS3DBUFFER *lpDs3dBuffer)
412 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
414 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
416 if (lpDs3dBuffer == NULL) {
417 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
418 return DSERR_INVALIDPARAM;
421 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
422 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
423 return DSERR_INVALIDPARAM;
426 TRACE("returning: all parameters\n");
427 *lpDs3dBuffer = This->ds3db_ds3db;
428 return DS_OK;
431 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(IDirectSound3DBuffer *iface,
432 DWORD *lpdwInsideConeAngle, DWORD *lpdwOutsideConeAngle)
434 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
436 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
437 This->ds3db_ds3db.dwInsideConeAngle, This->ds3db_ds3db.dwOutsideConeAngle);
438 *lpdwInsideConeAngle = This->ds3db_ds3db.dwInsideConeAngle;
439 *lpdwOutsideConeAngle = This->ds3db_ds3db.dwOutsideConeAngle;
440 return DS_OK;
443 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(IDirectSound3DBuffer *iface,
444 D3DVECTOR *lpvConeOrientation)
446 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
448 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
449 This->ds3db_ds3db.vConeOrientation.x,
450 This->ds3db_ds3db.vConeOrientation.y,
451 This->ds3db_ds3db.vConeOrientation.z);
452 *lpvConeOrientation = This->ds3db_ds3db.vConeOrientation;
453 return DS_OK;
456 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(IDirectSound3DBuffer *iface,
457 LONG *lplConeOutsideVolume)
459 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
461 TRACE("returning: Cone Outside Volume = %d\n", This->ds3db_ds3db.lConeOutsideVolume);
462 *lplConeOutsideVolume = This->ds3db_ds3db.lConeOutsideVolume;
463 return DS_OK;
466 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(IDirectSound3DBuffer *iface,
467 D3DVALUE *lpfMaxDistance)
469 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
471 TRACE("returning: Max Distance = %f\n", This->ds3db_ds3db.flMaxDistance);
472 *lpfMaxDistance = This->ds3db_ds3db.flMaxDistance;
473 return DS_OK;
476 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(IDirectSound3DBuffer *iface,
477 D3DVALUE *lpfMinDistance)
479 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
481 TRACE("returning: Min Distance = %f\n", This->ds3db_ds3db.flMinDistance);
482 *lpfMinDistance = This->ds3db_ds3db.flMinDistance;
483 return DS_OK;
486 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(IDirectSound3DBuffer *iface,
487 DWORD *lpdwMode)
489 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
491 TRACE("returning: Mode = %d\n", This->ds3db_ds3db.dwMode);
492 *lpdwMode = This->ds3db_ds3db.dwMode;
493 return DS_OK;
496 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(IDirectSound3DBuffer *iface,
497 D3DVECTOR *lpvPosition)
499 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
501 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3db_ds3db.vPosition.x,
502 This->ds3db_ds3db.vPosition.y, This->ds3db_ds3db.vPosition.z);
503 *lpvPosition = This->ds3db_ds3db.vPosition;
504 return DS_OK;
507 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(IDirectSound3DBuffer *iface,
508 D3DVECTOR *lpvVelocity)
510 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
512 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3db_ds3db.vVelocity.x,
513 This->ds3db_ds3db.vVelocity.y, This->ds3db_ds3db.vVelocity.z);
514 *lpvVelocity = This->ds3db_ds3db.vVelocity;
515 return DS_OK;
518 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(IDirectSound3DBuffer *iface,
519 const DS3DBUFFER *lpcDs3dBuffer, DWORD dwApply)
521 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
522 DWORD status = DSERR_INVALIDPARAM;
524 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
526 if (lpcDs3dBuffer == NULL) {
527 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
528 return status;
531 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
532 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
533 return status;
536 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
537 This->ds3db_ds3db = *lpcDs3dBuffer;
539 if (dwApply == DS3D_IMMEDIATE)
541 DSOUND_Mix3DBuffer(This);
543 This->ds3db_need_recalc = TRUE;
544 status = DS_OK;
546 return status;
549 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(IDirectSound3DBuffer *iface,
550 DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD dwApply)
552 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
554 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
555 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
556 This->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
557 This->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
558 if (dwApply == DS3D_IMMEDIATE)
559 DSOUND_Mix3DBuffer(This);
560 This->ds3db_need_recalc = TRUE;
561 return DS_OK;
564 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(IDirectSound3DBuffer *iface,
565 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
567 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
569 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
570 This->ds3db_ds3db.vConeOrientation.x = x;
571 This->ds3db_ds3db.vConeOrientation.y = y;
572 This->ds3db_ds3db.vConeOrientation.z = z;
573 if (dwApply == DS3D_IMMEDIATE)
575 This->ds3db_need_recalc = FALSE;
576 DSOUND_Mix3DBuffer(This);
578 This->ds3db_need_recalc = TRUE;
579 return DS_OK;
582 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(IDirectSound3DBuffer *iface,
583 LONG lConeOutsideVolume, DWORD dwApply)
585 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
587 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
588 This->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
589 if (dwApply == DS3D_IMMEDIATE)
591 This->ds3db_need_recalc = FALSE;
592 DSOUND_Mix3DBuffer(This);
594 This->ds3db_need_recalc = TRUE;
595 return DS_OK;
598 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(IDirectSound3DBuffer *iface,
599 D3DVALUE fMaxDistance, DWORD dwApply)
601 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
603 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
604 This->ds3db_ds3db.flMaxDistance = fMaxDistance;
605 if (dwApply == DS3D_IMMEDIATE)
607 This->ds3db_need_recalc = FALSE;
608 DSOUND_Mix3DBuffer(This);
610 This->ds3db_need_recalc = TRUE;
611 return DS_OK;
614 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(IDirectSound3DBuffer *iface,
615 D3DVALUE fMinDistance, DWORD dwApply)
617 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
619 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
620 This->ds3db_ds3db.flMinDistance = fMinDistance;
621 if (dwApply == DS3D_IMMEDIATE)
623 This->ds3db_need_recalc = FALSE;
624 DSOUND_Mix3DBuffer(This);
626 This->ds3db_need_recalc = TRUE;
627 return DS_OK;
630 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(IDirectSound3DBuffer *iface, DWORD dwMode,
631 DWORD dwApply)
633 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
635 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
636 This->ds3db_ds3db.dwMode = dwMode;
637 if (dwApply == DS3D_IMMEDIATE)
639 This->ds3db_need_recalc = FALSE;
640 DSOUND_Mix3DBuffer(This);
642 This->ds3db_need_recalc = TRUE;
643 return DS_OK;
646 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x,
647 D3DVALUE y, D3DVALUE z, DWORD dwApply)
649 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
651 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
652 This->ds3db_ds3db.vPosition.x = x;
653 This->ds3db_ds3db.vPosition.y = y;
654 This->ds3db_ds3db.vPosition.z = z;
655 if (dwApply == DS3D_IMMEDIATE)
657 This->ds3db_need_recalc = FALSE;
658 DSOUND_Mix3DBuffer(This);
660 This->ds3db_need_recalc = TRUE;
661 return DS_OK;
664 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(IDirectSound3DBuffer *iface,
665 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
667 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
669 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
670 This->ds3db_ds3db.vVelocity.x = x;
671 This->ds3db_ds3db.vVelocity.y = y;
672 This->ds3db_ds3db.vVelocity.z = z;
673 if (dwApply == DS3D_IMMEDIATE)
675 This->ds3db_need_recalc = FALSE;
676 DSOUND_Mix3DBuffer(This);
678 This->ds3db_need_recalc = TRUE;
679 return DS_OK;
682 const IDirectSound3DBufferVtbl ds3dbvt =
684 /* IUnknown methods */
685 IDirectSound3DBufferImpl_QueryInterface,
686 IDirectSound3DBufferImpl_AddRef,
687 IDirectSound3DBufferImpl_Release,
688 /* IDirectSound3DBuffer methods */
689 IDirectSound3DBufferImpl_GetAllParameters,
690 IDirectSound3DBufferImpl_GetConeAngles,
691 IDirectSound3DBufferImpl_GetConeOrientation,
692 IDirectSound3DBufferImpl_GetConeOutsideVolume,
693 IDirectSound3DBufferImpl_GetMaxDistance,
694 IDirectSound3DBufferImpl_GetMinDistance,
695 IDirectSound3DBufferImpl_GetMode,
696 IDirectSound3DBufferImpl_GetPosition,
697 IDirectSound3DBufferImpl_GetVelocity,
698 IDirectSound3DBufferImpl_SetAllParameters,
699 IDirectSound3DBufferImpl_SetConeAngles,
700 IDirectSound3DBufferImpl_SetConeOrientation,
701 IDirectSound3DBufferImpl_SetConeOutsideVolume,
702 IDirectSound3DBufferImpl_SetMaxDistance,
703 IDirectSound3DBufferImpl_SetMinDistance,
704 IDirectSound3DBufferImpl_SetMode,
705 IDirectSound3DBufferImpl_SetPosition,
706 IDirectSound3DBufferImpl_SetVelocity,
710 /*******************************************************************************
711 * IDirectSound3DListener
713 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
715 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DListener_iface);
719 /* IUnknown methods */
720 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(IDirectSound3DListener *iface,
721 REFIID riid, void **ppobj)
723 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
725 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
727 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
730 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(IDirectSound3DListener *iface)
732 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
733 ULONG ref = InterlockedIncrement(&This->ref3D);
735 TRACE("(%p) ref was %d\n", This, ref - 1);
737 if(ref == 1)
738 InterlockedIncrement(&This->numIfaces);
740 return ref;
743 static ULONG WINAPI IDirectSound3DListenerImpl_Release(IDirectSound3DListener *iface)
745 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
746 ULONG ref;
748 ref = capped_refcount_dec(&This->ref3D);
749 if(!ref)
750 capped_refcount_dec(&This->numIfaces);
752 TRACE("(%p) ref is now %d\n", This, ref);
754 return ref;
757 /* IDirectSound3DListener methods */
758 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
759 DS3DLISTENER *lpDS3DL)
761 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
763 TRACE("(%p,%p)\n",This,lpDS3DL);
765 if (lpDS3DL == NULL) {
766 WARN("invalid parameter: lpDS3DL == NULL\n");
767 return DSERR_INVALIDPARAM;
770 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
771 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
772 return DSERR_INVALIDPARAM;
775 TRACE("returning: all parameters\n");
776 *lpDS3DL = This->device->ds3dl;
777 return DS_OK;
780 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
781 D3DVALUE *lpfDistanceFactor)
783 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
785 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
786 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
787 return DS_OK;
790 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
791 D3DVALUE *lpfDopplerFactor)
793 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
795 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
796 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
797 return DS_OK;
800 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
801 D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
803 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
805 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
806 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
807 This->device->ds3dl.vOrientTop.z);
808 *lpvOrientFront = This->device->ds3dl.vOrientFront;
809 *lpvOrientTop = This->device->ds3dl.vOrientTop;
810 return DS_OK;
813 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
814 D3DVECTOR *lpvPosition)
816 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
818 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
819 *lpvPosition = This->device->ds3dl.vPosition;
820 return DS_OK;
823 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
824 D3DVALUE *lpfRolloffFactor)
826 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
828 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
829 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
830 return DS_OK;
833 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
834 D3DVECTOR *lpvVelocity)
836 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
838 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
839 *lpvVelocity = This->device->ds3dl.vVelocity;
840 return DS_OK;
843 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
844 const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
846 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
848 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
849 This->device->ds3dl = *lpcDS3DL;
850 if (dwApply == DS3D_IMMEDIATE)
852 This->device->ds3dl_need_recalc = FALSE;
853 DSOUND_ChangeListener(This);
855 This->device->ds3dl_need_recalc = TRUE;
856 return DS_OK;
859 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
860 D3DVALUE fDistanceFactor, DWORD dwApply)
862 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
864 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
865 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
866 if (dwApply == DS3D_IMMEDIATE)
868 This->device->ds3dl_need_recalc = FALSE;
869 DSOUND_ChangeListener(This);
871 This->device->ds3dl_need_recalc = TRUE;
872 return DS_OK;
875 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
876 D3DVALUE fDopplerFactor, DWORD dwApply)
878 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
880 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
881 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
882 if (dwApply == DS3D_IMMEDIATE)
884 This->device->ds3dl_need_recalc = FALSE;
885 DSOUND_ChangeListener(This);
887 This->device->ds3dl_need_recalc = TRUE;
888 return DS_OK;
891 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
892 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
893 D3DVALUE zTop, DWORD dwApply)
895 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
897 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
898 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
899 This->device->ds3dl.vOrientFront.x = xFront;
900 This->device->ds3dl.vOrientFront.y = yFront;
901 This->device->ds3dl.vOrientFront.z = zFront;
902 This->device->ds3dl.vOrientTop.x = xTop;
903 This->device->ds3dl.vOrientTop.y = yTop;
904 This->device->ds3dl.vOrientTop.z = zTop;
905 if (dwApply == DS3D_IMMEDIATE)
907 This->device->ds3dl_need_recalc = FALSE;
908 DSOUND_ChangeListener(This);
910 This->device->ds3dl_need_recalc = TRUE;
911 return DS_OK;
914 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
915 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
917 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
919 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
920 This->device->ds3dl.vPosition.x = x;
921 This->device->ds3dl.vPosition.y = y;
922 This->device->ds3dl.vPosition.z = z;
923 if (dwApply == DS3D_IMMEDIATE)
925 This->device->ds3dl_need_recalc = FALSE;
926 DSOUND_ChangeListener(This);
928 This->device->ds3dl_need_recalc = TRUE;
929 return DS_OK;
932 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
933 D3DVALUE fRolloffFactor, DWORD dwApply)
935 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
937 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
938 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
939 if (dwApply == DS3D_IMMEDIATE)
941 This->device->ds3dl_need_recalc = FALSE;
942 DSOUND_ChangeListener(This);
944 This->device->ds3dl_need_recalc = TRUE;
945 return DS_OK;
948 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
949 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
951 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
953 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
954 This->device->ds3dl.vVelocity.x = x;
955 This->device->ds3dl.vVelocity.y = y;
956 This->device->ds3dl.vVelocity.z = z;
957 if (dwApply == DS3D_IMMEDIATE)
959 This->device->ds3dl_need_recalc = FALSE;
960 DSOUND_ChangeListener(This);
962 This->device->ds3dl_need_recalc = TRUE;
963 return DS_OK;
966 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
968 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
970 TRACE("\n");
971 DSOUND_ChangeListener(This);
972 return DS_OK;
975 const IDirectSound3DListenerVtbl ds3dlvt =
977 /* IUnknown methods */
978 IDirectSound3DListenerImpl_QueryInterface,
979 IDirectSound3DListenerImpl_AddRef,
980 IDirectSound3DListenerImpl_Release,
981 /* IDirectSound3DListener methods */
982 IDirectSound3DListenerImpl_GetAllParameter,
983 IDirectSound3DListenerImpl_GetDistanceFactor,
984 IDirectSound3DListenerImpl_GetDopplerFactor,
985 IDirectSound3DListenerImpl_GetOrientation,
986 IDirectSound3DListenerImpl_GetPosition,
987 IDirectSound3DListenerImpl_GetRolloffFactor,
988 IDirectSound3DListenerImpl_GetVelocity,
989 IDirectSound3DListenerImpl_SetAllParameters,
990 IDirectSound3DListenerImpl_SetDistanceFactor,
991 IDirectSound3DListenerImpl_SetDopplerFactor,
992 IDirectSound3DListenerImpl_SetOrientation,
993 IDirectSound3DListenerImpl_SetPosition,
994 IDirectSound3DListenerImpl_SetRolloffFactor,
995 IDirectSound3DListenerImpl_SetVelocity,
996 IDirectSound3DListenerImpl_CommitDeferredSettings,