winebus.sys: Watch for hid raw device addition and removal.
[wine.git] / dlls / dsound / sound3d.c
blobb416cdc01e9edf583be7f55bc52e96a38cd85aef
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 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians (%f degrees)\n", a->x, a->y, a->z, b->x,
116 b->y, b->z, angle, RadToDeg(angle));
117 return angle;
120 static inline D3DVALUE AngleBetweenVectorsDeg (const D3DVECTOR *a, const D3DVECTOR *b)
122 return RadToDeg(AngleBetweenVectorsRad(a, b));
125 /* calculates vector between two points */
126 static inline D3DVECTOR VectorBetweenTwoPoints (const D3DVECTOR *a, const D3DVECTOR *b)
128 D3DVECTOR c;
129 c.x = b->x - a->x;
130 c.y = b->y - a->y;
131 c.z = b->z - a->z;
132 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
133 b->z, c.x, c.y, c.z);
134 return c;
137 /* calculates the length of vector's projection on another vector */
138 static inline D3DVALUE ProjectVector (const D3DVECTOR *a, const D3DVECTOR *p)
140 D3DVALUE prod, result;
141 prod = ScalarProduct(a, p);
142 result = prod/VectorMagnitude(p);
143 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
144 p->y, p->z, result);
145 return result;
148 /*******************************************************************************
149 * 3D Buffer and Listener mixing
152 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
154 /* volume, at which the sound will be played after all calcs. */
155 D3DVALUE lVolume = 0;
156 /* stuff for distance related stuff calc. */
157 D3DVECTOR vDistance;
158 D3DVALUE flDistance = 0;
159 /* panning related stuff */
160 D3DVALUE flAngle, flAngle2;
161 D3DVECTOR vLeft;
162 int i, num_main_speakers;
163 float a, ingain;
164 /* doppler shift related stuff */
166 TRACE("(%p)\n",dsb);
168 /* initial buffer volume */
169 lVolume = dsb->ds3db_lVolume;
171 switch (dsb->ds3db_ds3db.dwMode)
173 case DS3DMODE_NORMAL:
174 TRACE("Normal 3D processing mode\n");
175 /* we need to calculate distance between buffer and listener*/
176 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
177 flDistance = VectorMagnitude (&vDistance);
178 break;
179 case DS3DMODE_HEADRELATIVE:
180 TRACE("Head-relative 3D processing mode\n");
181 /* distance between buffer and listener is same as buffer's position */
182 vDistance = dsb->ds3db_ds3db.vPosition;
183 flDistance = VectorMagnitude (&vDistance);
184 break;
185 default:
186 TRACE("3D processing disabled\n");
187 /* this one is here only to eliminate annoying warning message */
188 DSOUND_RecalcVolPan (&dsb->volpan);
189 return;
192 if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
194 /* some apps don't want you to hear too distant sounds... */
195 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
197 dsb->volpan.lVolume = DSBVOLUME_MIN;
198 DSOUND_RecalcVolPan (&dsb->volpan);
199 /* i guess mixing here would be a waste of power */
200 return;
202 else
203 flDistance = dsb->ds3db_ds3db.flMaxDistance;
206 if (flDistance < dsb->ds3db_ds3db.flMinDistance)
207 flDistance = dsb->ds3db_ds3db.flMinDistance;
209 /* attenuation proportional to the distance squared, converted to millibels as in lVolume*/
210 lVolume -= log10(flDistance/dsb->ds3db_ds3db.flMinDistance * flDistance/dsb->ds3db_ds3db.flMinDistance)*1000 * dsb->device->ds3dl.flRolloffFactor;
211 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
213 /* conning */
214 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
215 if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
217 TRACE("conning: cones not set\n");
219 else
221 D3DVECTOR vDistanceInv;
223 vDistanceInv.x = -vDistance.x;
224 vDistanceInv.y = -vDistance.y;
225 vDistanceInv.z = -vDistance.z;
227 /* calculate angle */
228 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistanceInv);
229 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
230 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
232 /* my test show that for my way of calc., we need only half of angles */
233 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
234 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
235 if (dwOutsideConeAngle == dwInsideConeAngle)
236 ++dwOutsideConeAngle;
238 /* full volume */
239 if (flAngle < dwInsideConeAngle)
240 flAngle = dwInsideConeAngle;
241 /* min (app defined) volume */
242 if (flAngle > dwOutsideConeAngle)
243 flAngle = dwOutsideConeAngle;
244 /* this probably isn't the right thing, but it's ok for the time being */
245 lVolume += ((flAngle - dwInsideConeAngle)/(dwOutsideConeAngle - dwInsideConeAngle)) * dsb->ds3db_ds3db.lConeOutsideVolume;
247 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
248 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
250 dsb->volpan.lVolume = lVolume;
252 ingain = pow(2.0, dsb->volpan.lVolume / 600.0) * 0xffff;
254 if (dsb->device->pwfx->nChannels == 1)
256 dsb->volpan.dwTotalAmpFactor[0] = ingain;
257 return;
260 /* panning */
261 if (vDistance.x == 0.0f && vDistance.y == 0.0f && vDistance.z == 0.0f)
262 flAngle = 0.0;
263 else
265 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
266 /* To calculate angle to sound source we need to:
267 * 1) Get angle between vDistance and a plane on which angle to sound source should be 0.
268 * Such a plane is given by vectors vOrientFront and vOrientTop, and angle between vector
269 * and a plane equals to M_PI_2 - angle between vector and normal to this plane (vLeft in this case).
270 * 2) Determine if the source is behind or in front of us by calculating angle between vDistance
271 * and vOrientFront.
273 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
274 flAngle2 = AngleBetweenVectorsRad(&dsb->device->ds3dl.vOrientFront, &vDistance);
275 if (flAngle2 > M_PI_2)
276 flAngle = -flAngle;
277 flAngle -= M_PI_2;
278 if (flAngle < -M_PI)
279 flAngle += 2*M_PI;
281 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
283 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
284 if(0)
286 D3DVALUE flFreq, flBufferVel, flListenerVel;
287 /* doppler shift*/
288 if (!VectorMagnitude(&dsb->ds3db_ds3db.vVelocity) && !VectorMagnitude(&dsb->device->ds3dl.vVelocity))
290 TRACE("doppler: Buffer and Listener don't have velocities\n");
292 else if (!(dsb->ds3db_ds3db.vVelocity.x == dsb->device->ds3dl.vVelocity.x &&
293 dsb->ds3db_ds3db.vVelocity.y == dsb->device->ds3dl.vVelocity.y &&
294 dsb->ds3db_ds3db.vVelocity.z == dsb->device->ds3dl.vVelocity.z))
296 /* calculate length of ds3db_ds3db.vVelocity component which causes Doppler Effect
297 NOTE: if buffer moves TOWARDS the listener, its velocity component is NEGATIVE
298 if buffer moves AWAY from listener, its velocity component is POSITIVE */
299 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
300 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
301 NOTE: if listener moves TOWARDS the buffer, its velocity component is POSITIVE
302 if listener moves AWAY from buffer, its velocity component is NEGATIVE */
303 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
304 /* formula taken from Gianicoli D.: Physics, 4th edition: */
305 /* FIXME: replace dsb->freq with appropriate frequency ! */
306 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
307 TRACE("doppler: Buffer velocity (component) = %f, Listener velocity (component) = %f => Doppler shift: %d Hz -> %f Hz\n",
308 flBufferVel, flListenerVel, dsb->freq, flFreq);
309 /* FIXME: replace following line with correct frequency setting ! */
310 dsb->freq = flFreq;
311 DSOUND_RecalcFormat(dsb);
315 for (i = 0; i < dsb->device->pwfx->nChannels; i++)
316 dsb->volpan.dwTotalAmpFactor[i] = 0;
318 num_main_speakers = dsb->device->pwfx->nChannels;
320 if (dsb->device->lfe_channel != -1) {
321 dsb->volpan.dwTotalAmpFactor[dsb->device->lfe_channel] = ingain;
322 num_main_speakers--;
325 /* adapted from OpenAL's Alc/panning.c */
326 for (i = 0; i < num_main_speakers - 1; i++)
328 if(flAngle >= dsb->device->speaker_angles[i] && flAngle < dsb->device->speaker_angles[i+1])
330 /* Sound is between speakers i and i+1 */
331 a = (flAngle-dsb->device->speaker_angles[i]) / (dsb->device->speaker_angles[i+1]-dsb->device->speaker_angles[i]);
332 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i]] = sqrtf(1.0f-a) * ingain;
333 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i+1]] = sqrtf(a) * ingain;
334 return;
338 /* Sound is between last and first speakers */
339 if (flAngle < dsb->device->speaker_angles[0]) { flAngle += M_PI*2.0f; }
340 a = (flAngle-dsb->device->speaker_angles[i]) / (M_PI*2.0f + dsb->device->speaker_angles[0]-dsb->device->speaker_angles[i]);
341 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i]] = sqrtf(1.0f-a) * ingain;
342 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[0]] = sqrtf(a) * ingain;
345 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
347 TRACE("(%p)\n",dsb);
349 DSOUND_Calc3DBuffer(dsb);
352 static void DSOUND_ChangeListener(IDirectSoundBufferImpl *ds3dl)
354 int i;
355 TRACE("(%p)\n",ds3dl);
356 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
358 /* check if this buffer is waiting for recalculation */
359 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
361 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
366 /*******************************************************************************
367 * IDirectSound3DBuffer
369 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
371 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DBuffer_iface);
374 /* IUnknown methods */
375 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(IDirectSound3DBuffer *iface,
376 REFIID riid, void **ppobj)
378 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
380 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
382 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
385 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(IDirectSound3DBuffer *iface)
387 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
388 ULONG ref = InterlockedIncrement(&This->ref3D);
390 TRACE("(%p) ref was %d\n", This, ref - 1);
392 if(ref == 1)
393 InterlockedIncrement(&This->numIfaces);
395 return ref;
398 static ULONG WINAPI IDirectSound3DBufferImpl_Release(IDirectSound3DBuffer *iface)
400 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
401 ULONG ref = InterlockedDecrement(&This->ref3D);
403 TRACE("(%p) ref was %d\n", This, ref + 1);
405 if (!ref && !InterlockedDecrement(&This->numIfaces))
406 secondarybuffer_destroy(This);
408 return ref;
411 /* IDirectSound3DBuffer methods */
412 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(IDirectSound3DBuffer *iface,
413 DS3DBUFFER *lpDs3dBuffer)
415 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
417 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
419 if (lpDs3dBuffer == NULL) {
420 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
421 return DSERR_INVALIDPARAM;
424 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
425 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
426 return DSERR_INVALIDPARAM;
429 TRACE("returning: all parameters\n");
430 *lpDs3dBuffer = This->ds3db_ds3db;
431 return DS_OK;
434 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(IDirectSound3DBuffer *iface,
435 DWORD *lpdwInsideConeAngle, DWORD *lpdwOutsideConeAngle)
437 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
439 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
440 This->ds3db_ds3db.dwInsideConeAngle, This->ds3db_ds3db.dwOutsideConeAngle);
441 *lpdwInsideConeAngle = This->ds3db_ds3db.dwInsideConeAngle;
442 *lpdwOutsideConeAngle = This->ds3db_ds3db.dwOutsideConeAngle;
443 return DS_OK;
446 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(IDirectSound3DBuffer *iface,
447 D3DVECTOR *lpvConeOrientation)
449 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
451 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
452 This->ds3db_ds3db.vConeOrientation.x,
453 This->ds3db_ds3db.vConeOrientation.y,
454 This->ds3db_ds3db.vConeOrientation.z);
455 *lpvConeOrientation = This->ds3db_ds3db.vConeOrientation;
456 return DS_OK;
459 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(IDirectSound3DBuffer *iface,
460 LONG *lplConeOutsideVolume)
462 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
464 TRACE("returning: Cone Outside Volume = %d\n", This->ds3db_ds3db.lConeOutsideVolume);
465 *lplConeOutsideVolume = This->ds3db_ds3db.lConeOutsideVolume;
466 return DS_OK;
469 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(IDirectSound3DBuffer *iface,
470 D3DVALUE *lpfMaxDistance)
472 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
474 TRACE("returning: Max Distance = %f\n", This->ds3db_ds3db.flMaxDistance);
475 *lpfMaxDistance = This->ds3db_ds3db.flMaxDistance;
476 return DS_OK;
479 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(IDirectSound3DBuffer *iface,
480 D3DVALUE *lpfMinDistance)
482 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
484 TRACE("returning: Min Distance = %f\n", This->ds3db_ds3db.flMinDistance);
485 *lpfMinDistance = This->ds3db_ds3db.flMinDistance;
486 return DS_OK;
489 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(IDirectSound3DBuffer *iface,
490 DWORD *lpdwMode)
492 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
494 TRACE("returning: Mode = %d\n", This->ds3db_ds3db.dwMode);
495 *lpdwMode = This->ds3db_ds3db.dwMode;
496 return DS_OK;
499 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(IDirectSound3DBuffer *iface,
500 D3DVECTOR *lpvPosition)
502 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
504 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3db_ds3db.vPosition.x,
505 This->ds3db_ds3db.vPosition.y, This->ds3db_ds3db.vPosition.z);
506 *lpvPosition = This->ds3db_ds3db.vPosition;
507 return DS_OK;
510 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(IDirectSound3DBuffer *iface,
511 D3DVECTOR *lpvVelocity)
513 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
515 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3db_ds3db.vVelocity.x,
516 This->ds3db_ds3db.vVelocity.y, This->ds3db_ds3db.vVelocity.z);
517 *lpvVelocity = This->ds3db_ds3db.vVelocity;
518 return DS_OK;
521 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(IDirectSound3DBuffer *iface,
522 const DS3DBUFFER *lpcDs3dBuffer, DWORD dwApply)
524 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
525 DWORD status = DSERR_INVALIDPARAM;
527 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
529 if (lpcDs3dBuffer == NULL) {
530 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
531 return status;
534 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
535 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
536 return status;
539 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
540 This->ds3db_ds3db = *lpcDs3dBuffer;
542 if (dwApply == DS3D_IMMEDIATE)
544 DSOUND_Mix3DBuffer(This);
546 This->ds3db_need_recalc = TRUE;
547 status = DS_OK;
549 return status;
552 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(IDirectSound3DBuffer *iface,
553 DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD dwApply)
555 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
557 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
558 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
559 This->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
560 This->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
561 if (dwApply == DS3D_IMMEDIATE)
562 DSOUND_Mix3DBuffer(This);
563 This->ds3db_need_recalc = TRUE;
564 return DS_OK;
567 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(IDirectSound3DBuffer *iface,
568 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
570 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
572 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
573 This->ds3db_ds3db.vConeOrientation.x = x;
574 This->ds3db_ds3db.vConeOrientation.y = y;
575 This->ds3db_ds3db.vConeOrientation.z = z;
576 if (dwApply == DS3D_IMMEDIATE)
578 This->ds3db_need_recalc = FALSE;
579 DSOUND_Mix3DBuffer(This);
581 This->ds3db_need_recalc = TRUE;
582 return DS_OK;
585 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(IDirectSound3DBuffer *iface,
586 LONG lConeOutsideVolume, DWORD dwApply)
588 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
590 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
591 This->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
592 if (dwApply == DS3D_IMMEDIATE)
594 This->ds3db_need_recalc = FALSE;
595 DSOUND_Mix3DBuffer(This);
597 This->ds3db_need_recalc = TRUE;
598 return DS_OK;
601 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(IDirectSound3DBuffer *iface,
602 D3DVALUE fMaxDistance, DWORD dwApply)
604 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
606 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
607 This->ds3db_ds3db.flMaxDistance = fMaxDistance;
608 if (dwApply == DS3D_IMMEDIATE)
610 This->ds3db_need_recalc = FALSE;
611 DSOUND_Mix3DBuffer(This);
613 This->ds3db_need_recalc = TRUE;
614 return DS_OK;
617 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(IDirectSound3DBuffer *iface,
618 D3DVALUE fMinDistance, DWORD dwApply)
620 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
622 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
623 This->ds3db_ds3db.flMinDistance = fMinDistance;
624 if (dwApply == DS3D_IMMEDIATE)
626 This->ds3db_need_recalc = FALSE;
627 DSOUND_Mix3DBuffer(This);
629 This->ds3db_need_recalc = TRUE;
630 return DS_OK;
633 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(IDirectSound3DBuffer *iface, DWORD dwMode,
634 DWORD dwApply)
636 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
638 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
639 This->ds3db_ds3db.dwMode = dwMode;
640 if (dwApply == DS3D_IMMEDIATE)
642 This->ds3db_need_recalc = FALSE;
643 DSOUND_Mix3DBuffer(This);
645 This->ds3db_need_recalc = TRUE;
646 return DS_OK;
649 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x,
650 D3DVALUE y, D3DVALUE z, DWORD dwApply)
652 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
654 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
655 This->ds3db_ds3db.vPosition.x = x;
656 This->ds3db_ds3db.vPosition.y = y;
657 This->ds3db_ds3db.vPosition.z = z;
658 if (dwApply == DS3D_IMMEDIATE)
660 This->ds3db_need_recalc = FALSE;
661 DSOUND_Mix3DBuffer(This);
663 This->ds3db_need_recalc = TRUE;
664 return DS_OK;
667 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(IDirectSound3DBuffer *iface,
668 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
670 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
672 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
673 This->ds3db_ds3db.vVelocity.x = x;
674 This->ds3db_ds3db.vVelocity.y = y;
675 This->ds3db_ds3db.vVelocity.z = z;
676 if (dwApply == DS3D_IMMEDIATE)
678 This->ds3db_need_recalc = FALSE;
679 DSOUND_Mix3DBuffer(This);
681 This->ds3db_need_recalc = TRUE;
682 return DS_OK;
685 const IDirectSound3DBufferVtbl ds3dbvt =
687 /* IUnknown methods */
688 IDirectSound3DBufferImpl_QueryInterface,
689 IDirectSound3DBufferImpl_AddRef,
690 IDirectSound3DBufferImpl_Release,
691 /* IDirectSound3DBuffer methods */
692 IDirectSound3DBufferImpl_GetAllParameters,
693 IDirectSound3DBufferImpl_GetConeAngles,
694 IDirectSound3DBufferImpl_GetConeOrientation,
695 IDirectSound3DBufferImpl_GetConeOutsideVolume,
696 IDirectSound3DBufferImpl_GetMaxDistance,
697 IDirectSound3DBufferImpl_GetMinDistance,
698 IDirectSound3DBufferImpl_GetMode,
699 IDirectSound3DBufferImpl_GetPosition,
700 IDirectSound3DBufferImpl_GetVelocity,
701 IDirectSound3DBufferImpl_SetAllParameters,
702 IDirectSound3DBufferImpl_SetConeAngles,
703 IDirectSound3DBufferImpl_SetConeOrientation,
704 IDirectSound3DBufferImpl_SetConeOutsideVolume,
705 IDirectSound3DBufferImpl_SetMaxDistance,
706 IDirectSound3DBufferImpl_SetMinDistance,
707 IDirectSound3DBufferImpl_SetMode,
708 IDirectSound3DBufferImpl_SetPosition,
709 IDirectSound3DBufferImpl_SetVelocity,
713 /*******************************************************************************
714 * IDirectSound3DListener
716 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
718 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DListener_iface);
722 /* IUnknown methods */
723 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(IDirectSound3DListener *iface,
724 REFIID riid, void **ppobj)
726 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
728 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
730 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
733 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(IDirectSound3DListener *iface)
735 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
736 ULONG ref = InterlockedIncrement(&This->ref3D);
738 TRACE("(%p) ref was %d\n", This, ref - 1);
740 if(ref == 1)
741 InterlockedIncrement(&This->numIfaces);
743 return ref;
746 static ULONG WINAPI IDirectSound3DListenerImpl_Release(IDirectSound3DListener *iface)
748 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
749 ULONG ref;
751 ref = capped_refcount_dec(&This->ref3D);
752 if(!ref)
753 capped_refcount_dec(&This->numIfaces);
755 TRACE("(%p) ref is now %d\n", This, ref);
757 return ref;
760 /* IDirectSound3DListener methods */
761 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
762 DS3DLISTENER *lpDS3DL)
764 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
766 TRACE("(%p,%p)\n",This,lpDS3DL);
768 if (lpDS3DL == NULL) {
769 WARN("invalid parameter: lpDS3DL == NULL\n");
770 return DSERR_INVALIDPARAM;
773 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
774 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
775 return DSERR_INVALIDPARAM;
778 TRACE("returning: all parameters\n");
779 *lpDS3DL = This->device->ds3dl;
780 return DS_OK;
783 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
784 D3DVALUE *lpfDistanceFactor)
786 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
788 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
789 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
790 return DS_OK;
793 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
794 D3DVALUE *lpfDopplerFactor)
796 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
798 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
799 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
800 return DS_OK;
803 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
804 D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
806 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
808 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
809 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
810 This->device->ds3dl.vOrientTop.z);
811 *lpvOrientFront = This->device->ds3dl.vOrientFront;
812 *lpvOrientTop = This->device->ds3dl.vOrientTop;
813 return DS_OK;
816 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
817 D3DVECTOR *lpvPosition)
819 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
821 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
822 *lpvPosition = This->device->ds3dl.vPosition;
823 return DS_OK;
826 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
827 D3DVALUE *lpfRolloffFactor)
829 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
831 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
832 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
833 return DS_OK;
836 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
837 D3DVECTOR *lpvVelocity)
839 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
841 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
842 *lpvVelocity = This->device->ds3dl.vVelocity;
843 return DS_OK;
846 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
847 const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
849 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
851 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
852 This->device->ds3dl = *lpcDS3DL;
853 if (dwApply == DS3D_IMMEDIATE)
855 This->device->ds3dl_need_recalc = FALSE;
856 DSOUND_ChangeListener(This);
858 This->device->ds3dl_need_recalc = TRUE;
859 return DS_OK;
862 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
863 D3DVALUE fDistanceFactor, DWORD dwApply)
865 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
867 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
868 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
869 if (dwApply == DS3D_IMMEDIATE)
871 This->device->ds3dl_need_recalc = FALSE;
872 DSOUND_ChangeListener(This);
874 This->device->ds3dl_need_recalc = TRUE;
875 return DS_OK;
878 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
879 D3DVALUE fDopplerFactor, DWORD dwApply)
881 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
883 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
884 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
885 if (dwApply == DS3D_IMMEDIATE)
887 This->device->ds3dl_need_recalc = FALSE;
888 DSOUND_ChangeListener(This);
890 This->device->ds3dl_need_recalc = TRUE;
891 return DS_OK;
894 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
895 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
896 D3DVALUE zTop, DWORD dwApply)
898 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
900 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
901 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
902 This->device->ds3dl.vOrientFront.x = xFront;
903 This->device->ds3dl.vOrientFront.y = yFront;
904 This->device->ds3dl.vOrientFront.z = zFront;
905 This->device->ds3dl.vOrientTop.x = xTop;
906 This->device->ds3dl.vOrientTop.y = yTop;
907 This->device->ds3dl.vOrientTop.z = zTop;
908 if (dwApply == DS3D_IMMEDIATE)
910 This->device->ds3dl_need_recalc = FALSE;
911 DSOUND_ChangeListener(This);
913 This->device->ds3dl_need_recalc = TRUE;
914 return DS_OK;
917 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
918 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
920 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
922 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
923 This->device->ds3dl.vPosition.x = x;
924 This->device->ds3dl.vPosition.y = y;
925 This->device->ds3dl.vPosition.z = z;
926 if (dwApply == DS3D_IMMEDIATE)
928 This->device->ds3dl_need_recalc = FALSE;
929 DSOUND_ChangeListener(This);
931 This->device->ds3dl_need_recalc = TRUE;
932 return DS_OK;
935 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
936 D3DVALUE fRolloffFactor, DWORD dwApply)
938 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
940 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
941 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
942 if (dwApply == DS3D_IMMEDIATE)
944 This->device->ds3dl_need_recalc = FALSE;
945 DSOUND_ChangeListener(This);
947 This->device->ds3dl_need_recalc = TRUE;
948 return DS_OK;
951 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
952 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
954 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
956 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
957 This->device->ds3dl.vVelocity.x = x;
958 This->device->ds3dl.vVelocity.y = y;
959 This->device->ds3dl.vVelocity.z = z;
960 if (dwApply == DS3D_IMMEDIATE)
962 This->device->ds3dl_need_recalc = FALSE;
963 DSOUND_ChangeListener(This);
965 This->device->ds3dl_need_recalc = TRUE;
966 return DS_OK;
969 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
971 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
973 TRACE("\n");
974 DSOUND_ChangeListener(This);
975 return DS_OK;
978 const IDirectSound3DListenerVtbl ds3dlvt =
980 /* IUnknown methods */
981 IDirectSound3DListenerImpl_QueryInterface,
982 IDirectSound3DListenerImpl_AddRef,
983 IDirectSound3DListenerImpl_Release,
984 /* IDirectSound3DListener methods */
985 IDirectSound3DListenerImpl_GetAllParameter,
986 IDirectSound3DListenerImpl_GetDistanceFactor,
987 IDirectSound3DListenerImpl_GetDopplerFactor,
988 IDirectSound3DListenerImpl_GetOrientation,
989 IDirectSound3DListenerImpl_GetPosition,
990 IDirectSound3DListenerImpl_GetRolloffFactor,
991 IDirectSound3DListenerImpl_GetVelocity,
992 IDirectSound3DListenerImpl_SetAllParameters,
993 IDirectSound3DListenerImpl_SetDistanceFactor,
994 IDirectSound3DListenerImpl_SetDopplerFactor,
995 IDirectSound3DListenerImpl_SetOrientation,
996 IDirectSound3DListenerImpl_SetPosition,
997 IDirectSound3DListenerImpl_SetRolloffFactor,
998 IDirectSound3DListenerImpl_SetVelocity,
999 IDirectSound3DListenerImpl_CommitDeferredSettings,