wined3d: Use glFinish() for synchronisation when cleaning up a destroyed context...
[wine.git] / dlls / dsound / sound3d.c
blob06dbf58ca0b055c6e5572edb76d33af926ae3e3d
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 "mmddk.h"
48 #include "wine/debug.h"
49 #include "dsound.h"
50 #include "dsound_private.h"
52 /* default velocity of sound in the air */
53 #define DEFAULT_VELOCITY 340
55 WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);
57 /*******************************************************************************
58 * Auxiliary functions
61 /* scalar product (I believe it's called dot product in English) */
62 static inline D3DVALUE ScalarProduct (const D3DVECTOR *a, const D3DVECTOR *b)
64 D3DVALUE c;
65 c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
66 TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y,
67 b->z, c);
68 return c;
71 /* vector product (I believe it's called cross product in English */
72 static inline D3DVECTOR VectorProduct (const D3DVECTOR *a, const D3DVECTOR *b)
74 D3DVECTOR c;
75 c.x = (a->y*b->z) - (a->z*b->y);
76 c.y = (a->z*b->x) - (a->x*b->z);
77 c.z = (a->x*b->y) - (a->y*b->x);
78 TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
79 b->z, c.x, c.y, c.z);
80 return c;
83 /* magnitude (length) of vector */
84 static inline D3DVALUE VectorMagnitude (const D3DVECTOR *a)
86 D3DVALUE l;
87 l = sqrt (ScalarProduct (a, a));
88 TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);
89 return l;
92 /* conversion between radians and degrees */
93 static inline D3DVALUE RadToDeg (D3DVALUE angle)
95 D3DVALUE newangle;
96 newangle = angle * (360/(2*M_PI));
97 TRACE("%f rad = %f deg\n", angle, newangle);
98 return newangle;
101 /* angle between vectors - rad version */
102 static inline D3DVALUE AngleBetweenVectorsRad (const D3DVECTOR *a, const D3DVECTOR *b)
104 D3DVALUE la, lb, product, angle, cos;
105 /* definition of scalar product: a*b = |a|*|b|*cos... therefore: */
106 product = ScalarProduct (a,b);
107 la = VectorMagnitude (a);
108 lb = VectorMagnitude (b);
109 if (!la || !lb)
110 return 0;
112 cos = product/(la*lb);
113 if(cos > 1.f){
114 angle = 0;
115 }else if(cos < -1.f){
116 angle = M_PI;
117 }else{
118 angle = acos(cos);
120 TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians (%f degrees)\n", a->x, a->y, a->z, b->x,
121 b->y, b->z, angle, RadToDeg(angle));
122 return angle;
125 static inline D3DVALUE AngleBetweenVectorsDeg (const D3DVECTOR *a, const D3DVECTOR *b)
127 return RadToDeg(AngleBetweenVectorsRad(a, b));
130 /* calculates vector between two points */
131 static inline D3DVECTOR VectorBetweenTwoPoints (const D3DVECTOR *a, const D3DVECTOR *b)
133 D3DVECTOR c;
134 c.x = b->x - a->x;
135 c.y = b->y - a->y;
136 c.z = b->z - a->z;
137 TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,
138 b->z, c.x, c.y, c.z);
139 return c;
142 /* calculates the length of vector's projection on another vector */
143 static inline D3DVALUE ProjectVector (const D3DVECTOR *a, const D3DVECTOR *p)
145 D3DVALUE prod, result;
146 prod = ScalarProduct(a, p);
147 result = prod/VectorMagnitude(p);
148 TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,
149 p->y, p->z, result);
150 return result;
153 /*******************************************************************************
154 * 3D Buffer and Listener mixing
157 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb)
159 /* volume, at which the sound will be played after all calcs. */
160 D3DVALUE lVolume = 0;
161 /* stuff for distance related stuff calc. */
162 D3DVECTOR vDistance;
163 D3DVALUE flDistance = 0;
164 /* panning related stuff */
165 D3DVALUE flAngle, flAngle2;
166 D3DVECTOR vLeft;
167 int i, num_main_speakers;
168 float a, ingain;
169 /* doppler shift related stuff */
171 TRACE("(%p)\n",dsb);
173 /* initial buffer volume */
174 lVolume = dsb->ds3db_lVolume;
176 switch (dsb->ds3db_ds3db.dwMode)
178 case DS3DMODE_NORMAL:
179 TRACE("Normal 3D processing mode\n");
180 /* we need to calculate distance between buffer and listener*/
181 vDistance = VectorBetweenTwoPoints(&dsb->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);
182 flDistance = VectorMagnitude (&vDistance);
183 break;
184 case DS3DMODE_HEADRELATIVE:
185 TRACE("Head-relative 3D processing mode\n");
186 /* distance between buffer and listener is same as buffer's position */
187 vDistance = dsb->ds3db_ds3db.vPosition;
188 flDistance = VectorMagnitude (&vDistance);
189 break;
190 default:
191 TRACE("3D processing disabled\n");
192 /* this one is here only to eliminate annoying warning message */
193 dsb->volpan.lVolume = dsb->ds3db_lVolume;
194 DSOUND_RecalcVolPan (&dsb->volpan);
195 return;
198 if (flDistance > dsb->ds3db_ds3db.flMaxDistance)
200 /* some apps don't want you to hear too distant sounds... */
201 if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)
203 dsb->volpan.lVolume = DSBVOLUME_MIN;
204 DSOUND_RecalcVolPan (&dsb->volpan);
205 /* i guess mixing here would be a waste of power */
206 return;
208 else
209 flDistance = dsb->ds3db_ds3db.flMaxDistance;
212 if (flDistance < dsb->ds3db_ds3db.flMinDistance)
213 flDistance = dsb->ds3db_ds3db.flMinDistance;
215 flDistance = dsb->ds3db_ds3db.flMinDistance + (flDistance - dsb->ds3db_ds3db.flMinDistance) * dsb->device->ds3dl.flRolloffFactor;
217 /* attenuation proportional to the distance squared, converted to millibels as in lVolume*/
218 lVolume -= log10(flDistance/dsb->ds3db_ds3db.flMinDistance * flDistance/dsb->ds3db_ds3db.flMinDistance)*1000;
219 TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %d to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);
221 /* conning */
222 /* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/
223 if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)
225 TRACE("conning: cones not set\n");
227 else
229 D3DVECTOR vDistanceInv;
231 vDistanceInv.x = -vDistance.x;
232 vDistanceInv.y = -vDistance.y;
233 vDistanceInv.z = -vDistance.z;
235 /* calculate angle */
236 flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistanceInv);
237 /* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */
238 if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)
240 /* my test show that for my way of calc., we need only half of angles */
241 DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;
242 DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;
243 if (dwOutsideConeAngle == dwInsideConeAngle)
244 ++dwOutsideConeAngle;
246 /* full volume */
247 if (flAngle < dwInsideConeAngle)
248 flAngle = dwInsideConeAngle;
249 /* min (app defined) volume */
250 if (flAngle > dwOutsideConeAngle)
251 flAngle = dwOutsideConeAngle;
252 /* this probably isn't the right thing, but it's ok for the time being */
253 lVolume += ((flAngle - dwInsideConeAngle)/(dwOutsideConeAngle - dwInsideConeAngle)) * dsb->ds3db_ds3db.lConeOutsideVolume;
255 TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %d deg; OutsideConeAngle(/2) = %d deg; ConeOutsideVolume = %d => adjusting volume to %f\n",
256 flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);
258 dsb->volpan.lVolume = lVolume;
260 ingain = pow(2.0, dsb->volpan.lVolume / 600.0) * 0xffff;
262 if (dsb->device->pwfx->nChannels == 1)
264 dsb->volpan.dwTotalAmpFactor[0] = ingain;
265 return;
268 /* panning */
269 if (vDistance.x == 0.0f && vDistance.y == 0.0f && vDistance.z == 0.0f)
270 flAngle = 0.0;
271 else
273 vLeft = VectorProduct(&dsb->device->ds3dl.vOrientFront, &dsb->device->ds3dl.vOrientTop);
274 /* To calculate angle to sound source we need to:
275 * 1) Get angle between vDistance and a plane on which angle to sound source should be 0.
276 * Such a plane is given by vectors vOrientFront and vOrientTop, and angle between vector
277 * and a plane equals to M_PI_2 - angle between vector and normal to this plane (vLeft in this case).
278 * 2) Determine if the source is behind or in front of us by calculating angle between vDistance
279 * and vOrientFront.
281 flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);
282 flAngle2 = AngleBetweenVectorsRad(&dsb->device->ds3dl.vOrientFront, &vDistance);
283 if (flAngle2 > M_PI_2)
284 flAngle = -flAngle;
285 flAngle -= M_PI_2;
286 if (flAngle < -M_PI)
287 flAngle += 2*M_PI;
289 TRACE("panning: Angle = %f rad, lPan = %d\n", flAngle, dsb->volpan.lPan);
291 /* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */
292 if(0)
294 D3DVALUE flFreq, flBufferVel, flListenerVel;
295 /* doppler shift*/
296 if (!VectorMagnitude(&dsb->ds3db_ds3db.vVelocity) && !VectorMagnitude(&dsb->device->ds3dl.vVelocity))
298 TRACE("doppler: Buffer and Listener don't have velocities\n");
300 else if (!(dsb->ds3db_ds3db.vVelocity.x == dsb->device->ds3dl.vVelocity.x &&
301 dsb->ds3db_ds3db.vVelocity.y == dsb->device->ds3dl.vVelocity.y &&
302 dsb->ds3db_ds3db.vVelocity.z == dsb->device->ds3dl.vVelocity.z))
304 /* calculate length of ds3db_ds3db.vVelocity component which causes Doppler Effect
305 NOTE: if buffer moves TOWARDS the listener, its velocity component is NEGATIVE
306 if buffer moves AWAY from listener, its velocity component is POSITIVE */
307 flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);
308 /* calculate length of ds3dl.vVelocity component which causes Doppler Effect
309 NOTE: if listener moves TOWARDS the buffer, its velocity component is POSITIVE
310 if listener moves AWAY from buffer, its velocity component is NEGATIVE */
311 flListenerVel = ProjectVector(&dsb->device->ds3dl.vVelocity, &vDistance);
312 /* formula taken from Gianicoli D.: Physics, 4th edition: */
313 /* FIXME: replace dsb->freq with appropriate frequency ! */
314 flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));
315 TRACE("doppler: Buffer velocity (component) = %f, Listener velocity (component) = %f => Doppler shift: %d Hz -> %f Hz\n",
316 flBufferVel, flListenerVel, dsb->freq, flFreq);
317 /* FIXME: replace following line with correct frequency setting ! */
318 dsb->freq = flFreq;
319 DSOUND_RecalcFormat(dsb);
323 for (i = 0; i < dsb->device->pwfx->nChannels; i++)
324 dsb->volpan.dwTotalAmpFactor[i] = 0;
326 num_main_speakers = dsb->device->pwfx->nChannels;
328 if (dsb->device->lfe_channel != -1) {
329 dsb->volpan.dwTotalAmpFactor[dsb->device->lfe_channel] = ingain;
330 num_main_speakers--;
333 /* adapted from OpenAL's Alc/panning.c */
334 for (i = 0; i < num_main_speakers - 1; i++)
336 if(flAngle >= dsb->device->speaker_angles[i] && flAngle < dsb->device->speaker_angles[i+1])
338 /* Sound is between speakers i and i+1 */
339 a = (flAngle-dsb->device->speaker_angles[i]) / (dsb->device->speaker_angles[i+1]-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[i+1]] = sqrtf(a) * ingain;
342 return;
346 /* Sound is between last and first speakers */
347 if (flAngle < dsb->device->speaker_angles[0]) { flAngle += M_PI*2.0f; }
348 a = (flAngle-dsb->device->speaker_angles[i]) / (M_PI*2.0f + dsb->device->speaker_angles[0]-dsb->device->speaker_angles[i]);
349 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[i]] = sqrtf(1.0f-a) * ingain;
350 dsb->volpan.dwTotalAmpFactor[dsb->device->speaker_num[0]] = sqrtf(a) * ingain;
353 static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb)
355 TRACE("(%p)\n",dsb);
357 DSOUND_Calc3DBuffer(dsb);
360 static void DSOUND_ChangeListener(IDirectSoundBufferImpl *ds3dl)
362 int i;
363 TRACE("(%p)\n",ds3dl);
364 for (i = 0; i < ds3dl->device->nrofbuffers; i++)
366 /* check if this buffer is waiting for recalculation */
367 if (ds3dl->device->buffers[i]->ds3db_need_recalc)
369 DSOUND_Mix3DBuffer(ds3dl->device->buffers[i]);
374 /*******************************************************************************
375 * IDirectSound3DBuffer
377 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
379 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DBuffer_iface);
382 /* IUnknown methods */
383 static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(IDirectSound3DBuffer *iface,
384 REFIID riid, void **ppobj)
386 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
388 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
390 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
393 static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(IDirectSound3DBuffer *iface)
395 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
396 ULONG ref = InterlockedIncrement(&This->ref3D);
398 TRACE("(%p) ref %d\n", This, ref);
400 if(ref == 1)
401 InterlockedIncrement(&This->numIfaces);
403 return ref;
406 static ULONG WINAPI IDirectSound3DBufferImpl_Release(IDirectSound3DBuffer *iface)
408 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
409 ULONG ref = InterlockedDecrement(&This->ref3D);
411 TRACE("(%p) ref %d\n", This, ref);
413 if (!ref && !InterlockedDecrement(&This->numIfaces))
414 secondarybuffer_destroy(This);
416 return ref;
419 /* IDirectSound3DBuffer methods */
420 static HRESULT WINAPI IDirectSound3DBufferImpl_GetAllParameters(IDirectSound3DBuffer *iface,
421 DS3DBUFFER *lpDs3dBuffer)
423 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
425 TRACE("(%p,%p)\n",This,lpDs3dBuffer);
427 if (lpDs3dBuffer == NULL) {
428 WARN("invalid parameter: lpDs3dBuffer == NULL\n");
429 return DSERR_INVALIDPARAM;
432 if (lpDs3dBuffer->dwSize < sizeof(*lpDs3dBuffer)) {
433 WARN("invalid parameter: lpDs3dBuffer->dwSize = %d\n",lpDs3dBuffer->dwSize);
434 return DSERR_INVALIDPARAM;
437 TRACE("returning: all parameters\n");
438 *lpDs3dBuffer = This->ds3db_ds3db;
439 return DS_OK;
442 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeAngles(IDirectSound3DBuffer *iface,
443 DWORD *lpdwInsideConeAngle, DWORD *lpdwOutsideConeAngle)
445 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
447 TRACE("returning: Inside Cone Angle = %d degrees; Outside Cone Angle = %d degrees\n",
448 This->ds3db_ds3db.dwInsideConeAngle, This->ds3db_ds3db.dwOutsideConeAngle);
449 *lpdwInsideConeAngle = This->ds3db_ds3db.dwInsideConeAngle;
450 *lpdwOutsideConeAngle = This->ds3db_ds3db.dwOutsideConeAngle;
451 return DS_OK;
454 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOrientation(IDirectSound3DBuffer *iface,
455 D3DVECTOR *lpvConeOrientation)
457 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
459 TRACE("returning: Cone Orientation vector = (%f,%f,%f)\n",
460 This->ds3db_ds3db.vConeOrientation.x,
461 This->ds3db_ds3db.vConeOrientation.y,
462 This->ds3db_ds3db.vConeOrientation.z);
463 *lpvConeOrientation = This->ds3db_ds3db.vConeOrientation;
464 return DS_OK;
467 static HRESULT WINAPI IDirectSound3DBufferImpl_GetConeOutsideVolume(IDirectSound3DBuffer *iface,
468 LONG *lplConeOutsideVolume)
470 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
472 TRACE("returning: Cone Outside Volume = %d\n", This->ds3db_ds3db.lConeOutsideVolume);
473 *lplConeOutsideVolume = This->ds3db_ds3db.lConeOutsideVolume;
474 return DS_OK;
477 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMaxDistance(IDirectSound3DBuffer *iface,
478 D3DVALUE *lpfMaxDistance)
480 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
482 TRACE("returning: Max Distance = %f\n", This->ds3db_ds3db.flMaxDistance);
483 *lpfMaxDistance = This->ds3db_ds3db.flMaxDistance;
484 return DS_OK;
487 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMinDistance(IDirectSound3DBuffer *iface,
488 D3DVALUE *lpfMinDistance)
490 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
492 TRACE("returning: Min Distance = %f\n", This->ds3db_ds3db.flMinDistance);
493 *lpfMinDistance = This->ds3db_ds3db.flMinDistance;
494 return DS_OK;
497 static HRESULT WINAPI IDirectSound3DBufferImpl_GetMode(IDirectSound3DBuffer *iface,
498 DWORD *lpdwMode)
500 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
502 TRACE("returning: Mode = %d\n", This->ds3db_ds3db.dwMode);
503 *lpdwMode = This->ds3db_ds3db.dwMode;
504 return DS_OK;
507 static HRESULT WINAPI IDirectSound3DBufferImpl_GetPosition(IDirectSound3DBuffer *iface,
508 D3DVECTOR *lpvPosition)
510 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
512 TRACE("returning: Position vector = (%f,%f,%f)\n", This->ds3db_ds3db.vPosition.x,
513 This->ds3db_ds3db.vPosition.y, This->ds3db_ds3db.vPosition.z);
514 *lpvPosition = This->ds3db_ds3db.vPosition;
515 return DS_OK;
518 static HRESULT WINAPI IDirectSound3DBufferImpl_GetVelocity(IDirectSound3DBuffer *iface,
519 D3DVECTOR *lpvVelocity)
521 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
523 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->ds3db_ds3db.vVelocity.x,
524 This->ds3db_ds3db.vVelocity.y, This->ds3db_ds3db.vVelocity.z);
525 *lpvVelocity = This->ds3db_ds3db.vVelocity;
526 return DS_OK;
529 static HRESULT WINAPI IDirectSound3DBufferImpl_SetAllParameters(IDirectSound3DBuffer *iface,
530 const DS3DBUFFER *lpcDs3dBuffer, DWORD dwApply)
532 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
533 DWORD status = DSERR_INVALIDPARAM;
535 TRACE("(%p,%p,%x)\n",iface,lpcDs3dBuffer,dwApply);
537 if (lpcDs3dBuffer == NULL) {
538 WARN("invalid parameter: lpcDs3dBuffer == NULL\n");
539 return status;
542 if (lpcDs3dBuffer->dwSize != sizeof(DS3DBUFFER)) {
543 WARN("invalid parameter: lpcDs3dBuffer->dwSize = %d\n", lpcDs3dBuffer->dwSize);
544 return status;
547 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
548 This->ds3db_ds3db = *lpcDs3dBuffer;
550 if (dwApply == DS3D_IMMEDIATE)
552 DSOUND_Mix3DBuffer(This);
554 This->ds3db_need_recalc = TRUE;
555 status = DS_OK;
557 return status;
560 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeAngles(IDirectSound3DBuffer *iface,
561 DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD dwApply)
563 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
565 TRACE("setting: Inside Cone Angle = %d; Outside Cone Angle = %d; dwApply = %d\n",
566 dwInsideConeAngle, dwOutsideConeAngle, dwApply);
567 This->ds3db_ds3db.dwInsideConeAngle = dwInsideConeAngle;
568 This->ds3db_ds3db.dwOutsideConeAngle = dwOutsideConeAngle;
569 if (dwApply == DS3D_IMMEDIATE)
570 DSOUND_Mix3DBuffer(This);
571 This->ds3db_need_recalc = TRUE;
572 return DS_OK;
575 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOrientation(IDirectSound3DBuffer *iface,
576 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
578 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
580 TRACE("setting: Cone Orientation vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
581 This->ds3db_ds3db.vConeOrientation.x = x;
582 This->ds3db_ds3db.vConeOrientation.y = y;
583 This->ds3db_ds3db.vConeOrientation.z = z;
584 if (dwApply == DS3D_IMMEDIATE)
586 This->ds3db_need_recalc = FALSE;
587 DSOUND_Mix3DBuffer(This);
589 This->ds3db_need_recalc = TRUE;
590 return DS_OK;
593 static HRESULT WINAPI IDirectSound3DBufferImpl_SetConeOutsideVolume(IDirectSound3DBuffer *iface,
594 LONG lConeOutsideVolume, DWORD dwApply)
596 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
598 TRACE("setting: ConeOutsideVolume = %d; dwApply = %d\n", lConeOutsideVolume, dwApply);
599 This->ds3db_ds3db.lConeOutsideVolume = lConeOutsideVolume;
600 if (dwApply == DS3D_IMMEDIATE)
602 This->ds3db_need_recalc = FALSE;
603 DSOUND_Mix3DBuffer(This);
605 This->ds3db_need_recalc = TRUE;
606 return DS_OK;
609 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMaxDistance(IDirectSound3DBuffer *iface,
610 D3DVALUE fMaxDistance, DWORD dwApply)
612 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
614 TRACE("setting: MaxDistance = %f; dwApply = %d\n", fMaxDistance, dwApply);
615 This->ds3db_ds3db.flMaxDistance = fMaxDistance;
616 if (dwApply == DS3D_IMMEDIATE)
618 This->ds3db_need_recalc = FALSE;
619 DSOUND_Mix3DBuffer(This);
621 This->ds3db_need_recalc = TRUE;
622 return DS_OK;
625 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMinDistance(IDirectSound3DBuffer *iface,
626 D3DVALUE fMinDistance, DWORD dwApply)
628 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
630 TRACE("setting: MinDistance = %f; dwApply = %d\n", fMinDistance, dwApply);
631 This->ds3db_ds3db.flMinDistance = fMinDistance;
632 if (dwApply == DS3D_IMMEDIATE)
634 This->ds3db_need_recalc = FALSE;
635 DSOUND_Mix3DBuffer(This);
637 This->ds3db_need_recalc = TRUE;
638 return DS_OK;
641 static HRESULT WINAPI IDirectSound3DBufferImpl_SetMode(IDirectSound3DBuffer *iface, DWORD dwMode,
642 DWORD dwApply)
644 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
646 TRACE("setting: Mode = %d; dwApply = %d\n", dwMode, dwApply);
647 This->ds3db_ds3db.dwMode = dwMode;
648 if (dwApply == DS3D_IMMEDIATE)
650 This->ds3db_need_recalc = FALSE;
651 DSOUND_Mix3DBuffer(This);
653 This->ds3db_need_recalc = TRUE;
654 return DS_OK;
657 static HRESULT WINAPI IDirectSound3DBufferImpl_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x,
658 D3DVALUE y, D3DVALUE z, DWORD dwApply)
660 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
662 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
663 This->ds3db_ds3db.vPosition.x = x;
664 This->ds3db_ds3db.vPosition.y = y;
665 This->ds3db_ds3db.vPosition.z = z;
666 if (dwApply == DS3D_IMMEDIATE)
668 This->ds3db_need_recalc = FALSE;
669 DSOUND_Mix3DBuffer(This);
671 This->ds3db_need_recalc = TRUE;
672 return DS_OK;
675 static HRESULT WINAPI IDirectSound3DBufferImpl_SetVelocity(IDirectSound3DBuffer *iface,
676 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
678 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DBuffer(iface);
680 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
681 This->ds3db_ds3db.vVelocity.x = x;
682 This->ds3db_ds3db.vVelocity.y = y;
683 This->ds3db_ds3db.vVelocity.z = z;
684 if (dwApply == DS3D_IMMEDIATE)
686 This->ds3db_need_recalc = FALSE;
687 DSOUND_Mix3DBuffer(This);
689 This->ds3db_need_recalc = TRUE;
690 return DS_OK;
693 const IDirectSound3DBufferVtbl ds3dbvt =
695 /* IUnknown methods */
696 IDirectSound3DBufferImpl_QueryInterface,
697 IDirectSound3DBufferImpl_AddRef,
698 IDirectSound3DBufferImpl_Release,
699 /* IDirectSound3DBuffer methods */
700 IDirectSound3DBufferImpl_GetAllParameters,
701 IDirectSound3DBufferImpl_GetConeAngles,
702 IDirectSound3DBufferImpl_GetConeOrientation,
703 IDirectSound3DBufferImpl_GetConeOutsideVolume,
704 IDirectSound3DBufferImpl_GetMaxDistance,
705 IDirectSound3DBufferImpl_GetMinDistance,
706 IDirectSound3DBufferImpl_GetMode,
707 IDirectSound3DBufferImpl_GetPosition,
708 IDirectSound3DBufferImpl_GetVelocity,
709 IDirectSound3DBufferImpl_SetAllParameters,
710 IDirectSound3DBufferImpl_SetConeAngles,
711 IDirectSound3DBufferImpl_SetConeOrientation,
712 IDirectSound3DBufferImpl_SetConeOutsideVolume,
713 IDirectSound3DBufferImpl_SetMaxDistance,
714 IDirectSound3DBufferImpl_SetMinDistance,
715 IDirectSound3DBufferImpl_SetMode,
716 IDirectSound3DBufferImpl_SetPosition,
717 IDirectSound3DBufferImpl_SetVelocity,
721 /*******************************************************************************
722 * IDirectSound3DListener
724 static inline IDirectSoundBufferImpl *impl_from_IDirectSound3DListener(IDirectSound3DListener *iface)
726 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSound3DListener_iface);
730 /* IUnknown methods */
731 static HRESULT WINAPI IDirectSound3DListenerImpl_QueryInterface(IDirectSound3DListener *iface,
732 REFIID riid, void **ppobj)
734 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
736 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
738 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
741 static ULONG WINAPI IDirectSound3DListenerImpl_AddRef(IDirectSound3DListener *iface)
743 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
744 ULONG ref = InterlockedIncrement(&This->ref3D);
746 TRACE("(%p) ref %d\n", This, ref);
748 if(ref == 1)
749 InterlockedIncrement(&This->numIfaces);
751 return ref;
754 static ULONG WINAPI IDirectSound3DListenerImpl_Release(IDirectSound3DListener *iface)
756 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
757 ULONG ref;
759 ref = capped_refcount_dec(&This->ref3D);
760 if(!ref)
761 capped_refcount_dec(&This->numIfaces);
763 TRACE("(%p) ref %d\n", This, ref);
765 return ref;
768 /* IDirectSound3DListener methods */
769 static HRESULT WINAPI IDirectSound3DListenerImpl_GetAllParameter(IDirectSound3DListener *iface,
770 DS3DLISTENER *lpDS3DL)
772 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
774 TRACE("(%p,%p)\n",This,lpDS3DL);
776 if (lpDS3DL == NULL) {
777 WARN("invalid parameter: lpDS3DL == NULL\n");
778 return DSERR_INVALIDPARAM;
781 if (lpDS3DL->dwSize < sizeof(*lpDS3DL)) {
782 WARN("invalid parameter: lpDS3DL->dwSize = %d\n",lpDS3DL->dwSize);
783 return DSERR_INVALIDPARAM;
786 TRACE("returning: all parameters\n");
787 *lpDS3DL = This->device->ds3dl;
788 return DS_OK;
791 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDistanceFactor(IDirectSound3DListener *iface,
792 D3DVALUE *lpfDistanceFactor)
794 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
796 TRACE("returning: Distance Factor = %f\n", This->device->ds3dl.flDistanceFactor);
797 *lpfDistanceFactor = This->device->ds3dl.flDistanceFactor;
798 return DS_OK;
801 static HRESULT WINAPI IDirectSound3DListenerImpl_GetDopplerFactor(IDirectSound3DListener *iface,
802 D3DVALUE *lpfDopplerFactor)
804 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
806 TRACE("returning: Doppler Factor = %f\n", This->device->ds3dl.flDopplerFactor);
807 *lpfDopplerFactor = This->device->ds3dl.flDopplerFactor;
808 return DS_OK;
811 static HRESULT WINAPI IDirectSound3DListenerImpl_GetOrientation(IDirectSound3DListener *iface,
812 D3DVECTOR *lpvOrientFront, D3DVECTOR *lpvOrientTop)
814 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
816 TRACE("returning: OrientFront vector = (%f,%f,%f); OrientTop vector = (%f,%f,%f)\n", This->device->ds3dl.vOrientFront.x,
817 This->device->ds3dl.vOrientFront.y, This->device->ds3dl.vOrientFront.z, This->device->ds3dl.vOrientTop.x, This->device->ds3dl.vOrientTop.y,
818 This->device->ds3dl.vOrientTop.z);
819 *lpvOrientFront = This->device->ds3dl.vOrientFront;
820 *lpvOrientTop = This->device->ds3dl.vOrientTop;
821 return DS_OK;
824 static HRESULT WINAPI IDirectSound3DListenerImpl_GetPosition(IDirectSound3DListener *iface,
825 D3DVECTOR *lpvPosition)
827 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
829 TRACE("returning: Position vector = (%f,%f,%f)\n", This->device->ds3dl.vPosition.x, This->device->ds3dl.vPosition.y, This->device->ds3dl.vPosition.z);
830 *lpvPosition = This->device->ds3dl.vPosition;
831 return DS_OK;
834 static HRESULT WINAPI IDirectSound3DListenerImpl_GetRolloffFactor(IDirectSound3DListener *iface,
835 D3DVALUE *lpfRolloffFactor)
837 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
839 TRACE("returning: RolloffFactor = %f\n", This->device->ds3dl.flRolloffFactor);
840 *lpfRolloffFactor = This->device->ds3dl.flRolloffFactor;
841 return DS_OK;
844 static HRESULT WINAPI IDirectSound3DListenerImpl_GetVelocity(IDirectSound3DListener *iface,
845 D3DVECTOR *lpvVelocity)
847 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
849 TRACE("returning: Velocity vector = (%f,%f,%f)\n", This->device->ds3dl.vVelocity.x, This->device->ds3dl.vVelocity.y, This->device->ds3dl.vVelocity.z);
850 *lpvVelocity = This->device->ds3dl.vVelocity;
851 return DS_OK;
854 static HRESULT WINAPI IDirectSound3DListenerImpl_SetAllParameters(IDirectSound3DListener *iface,
855 const DS3DLISTENER *lpcDS3DL, DWORD dwApply)
857 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
859 TRACE("setting: all parameters; dwApply = %d\n", dwApply);
860 This->device->ds3dl = *lpcDS3DL;
861 if (dwApply == DS3D_IMMEDIATE)
863 This->device->ds3dl_need_recalc = FALSE;
864 DSOUND_ChangeListener(This);
866 This->device->ds3dl_need_recalc = TRUE;
867 return DS_OK;
870 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDistanceFactor(IDirectSound3DListener *iface,
871 D3DVALUE fDistanceFactor, DWORD dwApply)
873 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
875 TRACE("setting: Distance Factor = %f; dwApply = %d\n", fDistanceFactor, dwApply);
876 This->device->ds3dl.flDistanceFactor = fDistanceFactor;
877 if (dwApply == DS3D_IMMEDIATE)
879 This->device->ds3dl_need_recalc = FALSE;
880 DSOUND_ChangeListener(This);
882 This->device->ds3dl_need_recalc = TRUE;
883 return DS_OK;
886 static HRESULT WINAPI IDirectSound3DListenerImpl_SetDopplerFactor(IDirectSound3DListener *iface,
887 D3DVALUE fDopplerFactor, DWORD dwApply)
889 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
891 TRACE("setting: Doppler Factor = %f; dwApply = %d\n", fDopplerFactor, dwApply);
892 This->device->ds3dl.flDopplerFactor = fDopplerFactor;
893 if (dwApply == DS3D_IMMEDIATE)
895 This->device->ds3dl_need_recalc = FALSE;
896 DSOUND_ChangeListener(This);
898 This->device->ds3dl_need_recalc = TRUE;
899 return DS_OK;
902 static HRESULT WINAPI IDirectSound3DListenerImpl_SetOrientation(IDirectSound3DListener *iface,
903 D3DVALUE xFront, D3DVALUE yFront, D3DVALUE zFront, D3DVALUE xTop, D3DVALUE yTop,
904 D3DVALUE zTop, DWORD dwApply)
906 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
908 TRACE("setting: Front vector = (%f,%f,%f); Top vector = (%f,%f,%f); dwApply = %d\n",
909 xFront, yFront, zFront, xTop, yTop, zTop, dwApply);
910 This->device->ds3dl.vOrientFront.x = xFront;
911 This->device->ds3dl.vOrientFront.y = yFront;
912 This->device->ds3dl.vOrientFront.z = zFront;
913 This->device->ds3dl.vOrientTop.x = xTop;
914 This->device->ds3dl.vOrientTop.y = yTop;
915 This->device->ds3dl.vOrientTop.z = zTop;
916 if (dwApply == DS3D_IMMEDIATE)
918 This->device->ds3dl_need_recalc = FALSE;
919 DSOUND_ChangeListener(This);
921 This->device->ds3dl_need_recalc = TRUE;
922 return DS_OK;
925 static HRESULT WINAPI IDirectSound3DListenerImpl_SetPosition(IDirectSound3DListener *iface,
926 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
928 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
930 TRACE("setting: Position vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
931 This->device->ds3dl.vPosition.x = x;
932 This->device->ds3dl.vPosition.y = y;
933 This->device->ds3dl.vPosition.z = z;
934 if (dwApply == DS3D_IMMEDIATE)
936 This->device->ds3dl_need_recalc = FALSE;
937 DSOUND_ChangeListener(This);
939 This->device->ds3dl_need_recalc = TRUE;
940 return DS_OK;
943 static HRESULT WINAPI IDirectSound3DListenerImpl_SetRolloffFactor(IDirectSound3DListener *iface,
944 D3DVALUE fRolloffFactor, DWORD dwApply)
946 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
948 TRACE("setting: Rolloff Factor = %f; dwApply = %d\n", fRolloffFactor, dwApply);
949 This->device->ds3dl.flRolloffFactor = fRolloffFactor;
950 if (dwApply == DS3D_IMMEDIATE)
952 This->device->ds3dl_need_recalc = FALSE;
953 DSOUND_ChangeListener(This);
955 This->device->ds3dl_need_recalc = TRUE;
956 return DS_OK;
959 static HRESULT WINAPI IDirectSound3DListenerImpl_SetVelocity(IDirectSound3DListener *iface,
960 D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD dwApply)
962 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
964 TRACE("setting: Velocity vector = (%f,%f,%f); dwApply = %d\n", x, y, z, dwApply);
965 This->device->ds3dl.vVelocity.x = x;
966 This->device->ds3dl.vVelocity.y = y;
967 This->device->ds3dl.vVelocity.z = z;
968 if (dwApply == DS3D_IMMEDIATE)
970 This->device->ds3dl_need_recalc = FALSE;
971 DSOUND_ChangeListener(This);
973 This->device->ds3dl_need_recalc = TRUE;
974 return DS_OK;
977 static HRESULT WINAPI IDirectSound3DListenerImpl_CommitDeferredSettings(IDirectSound3DListener *iface)
979 IDirectSoundBufferImpl *This = impl_from_IDirectSound3DListener(iface);
981 TRACE("\n");
982 DSOUND_ChangeListener(This);
983 return DS_OK;
986 const IDirectSound3DListenerVtbl ds3dlvt =
988 /* IUnknown methods */
989 IDirectSound3DListenerImpl_QueryInterface,
990 IDirectSound3DListenerImpl_AddRef,
991 IDirectSound3DListenerImpl_Release,
992 /* IDirectSound3DListener methods */
993 IDirectSound3DListenerImpl_GetAllParameter,
994 IDirectSound3DListenerImpl_GetDistanceFactor,
995 IDirectSound3DListenerImpl_GetDopplerFactor,
996 IDirectSound3DListenerImpl_GetOrientation,
997 IDirectSound3DListenerImpl_GetPosition,
998 IDirectSound3DListenerImpl_GetRolloffFactor,
999 IDirectSound3DListenerImpl_GetVelocity,
1000 IDirectSound3DListenerImpl_SetAllParameters,
1001 IDirectSound3DListenerImpl_SetDistanceFactor,
1002 IDirectSound3DListenerImpl_SetDopplerFactor,
1003 IDirectSound3DListenerImpl_SetOrientation,
1004 IDirectSound3DListenerImpl_SetPosition,
1005 IDirectSound3DListenerImpl_SetRolloffFactor,
1006 IDirectSound3DListenerImpl_SetVelocity,
1007 IDirectSound3DListenerImpl_CommitDeferredSettings,