⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sound3d.c

📁 一个类似windows
💻 C
📖 第 1 页 / 共 3 页
字号:
/*  			DirectSound * * Copyright 1998 Marcus Meissner * Copyright 1998 Rob Riggs * Copyright 2000-2001 TransGaming Technologies, Inc. * Copyright 2002-2003 Rok Mandeljc <rok.mandeljc@gimb.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *//* * Most thread locking is complete. There may be a few race * conditions still lurking. * * Tested with a Soundblaster clone, a Gravis UltraSound Classic, * and a Turtle Beach Tropez+. * * TODO: *	Implement SetCooperativeLevel properly (need to address focus issues) *	Implement DirectSound3DBuffers (stubs in place) *	Use hardware 3D support if available *      Add critical section locking inside Release and AddRef methods *      Handle static buffers - put those in hardware, non-static not in hardware *      Hardware DuplicateSoundBuffer *      Proper volume calculation, and setting volume in HEL primary buffer *      Optimize WINMM and negotiate fragment size, decrease DS_HEL_MARGIN */#include <stdarg.h>#include <math.h>	/* Insomnia - pow() function */#define NONAMELESSUNION#define NONAMELESSSTRUCT#include "windef.h"#include "winbase.h"#include "mmsystem.h"#include "winreg.h"#include "winternl.h"#include "mmddk.h"#include "wine/debug.h"#include "dsound.h"#include "dsdriver.h"#include "dsound_private.h"/* default intensity level for human ears */#define DEFAULT_INTENSITY 0.000000000001f/* default velocity of sound in the air */#define DEFAULT_VELOCITY 340WINE_DEFAULT_DEBUG_CHANNEL(dsound3d);/******************************************************************************* *              Auxiliary functions *//* scalar product (i believe it's called dot product in english) */static inline D3DVALUE ScalarProduct (LPD3DVECTOR a, LPD3DVECTOR b){	D3DVALUE c;	c = (a->x*b->x) + (a->y*b->y) + (a->z*b->z);	TRACE("(%f,%f,%f) * (%f,%f,%f) = %f)\n", a->x, a->y, a->z, b->x, b->y, \	      b->z, c);	return c;}/* vector product (i believe it's called cross product in english */static inline D3DVECTOR VectorProduct (LPD3DVECTOR a, LPD3DVECTOR b){	D3DVECTOR c;	c.x = (a->y*b->z) - (a->z*b->y);	c.y = (a->z*b->x) - (a->x*b->z);	c.z = (a->x*b->y) - (a->y*b->x);	TRACE("(%f,%f,%f) x (%f,%f,%f) = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y, \	      b->z, c.x, c.y, c.z);	return c;}/* magnitude (length) of vector */static inline D3DVALUE VectorMagnitude (LPD3DVECTOR a){	D3DVALUE l;	l = sqrt (ScalarProduct (a, a));	TRACE("|(%f,%f,%f)| = %f\n", a->x, a->y, a->z, l);	return l;}/* conversion between radians and degrees */static inline D3DVALUE RadToDeg (D3DVALUE angle){	D3DVALUE newangle;	newangle = angle * (360/(2*M_PI));	TRACE("%f rad = %f deg\n", angle, newangle);	return newangle;}/* conversion between degrees and radians */static inline D3DVALUE DegToRad (D3DVALUE angle){	D3DVALUE newangle;	newangle = angle * (2*M_PI/360);	TRACE("%f deg = %f rad\n", angle, newangle);	return newangle;}/* angle between vectors - deg version */static inline D3DVALUE AngleBetweenVectorsDeg (LPD3DVECTOR a, LPD3DVECTOR b){	D3DVALUE la, lb, product, angle, cos;	/* definition of scalar product: a*b = |a|*|b|*cos...therefore: */	product = ScalarProduct (a,b);	la = VectorMagnitude (a);	lb = VectorMagnitude (b);	cos = product/(la*lb);	angle = acos(cos);	/* we now have angle in radians */	angle = RadToDeg(angle);	TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f degrees\n",  a->x, a->y, a->z, b->x,	      b->y, b->z, angle);	return angle;	}/* angle between vectors - rad version */static inline D3DVALUE AngleBetweenVectorsRad (LPD3DVECTOR a, LPD3DVECTOR b){	D3DVALUE la, lb, product, angle, cos;	/* definition of scalar product: a*b = |a|*|b|*cos...therefore: */	product = ScalarProduct (a,b);	la = VectorMagnitude (a);	lb = VectorMagnitude (b);	cos = product/(la*lb);	angle = acos(cos);	TRACE("angle between (%f,%f,%f) and (%f,%f,%f) = %f radians\n",  a->x, a->y, a->z, b->x,	      b->y, b->z, angle);	return angle;	}/* calculates vector between two points */static inline D3DVECTOR VectorBetweenTwoPoints (LPD3DVECTOR a, LPD3DVECTOR b){	D3DVECTOR c;	c.x = b->x - a->x;	c.y = b->y - a->y;	c.z = b->z - a->z;	TRACE("A (%f,%f,%f), B (%f,%f,%f), AB = (%f,%f,%f)\n", a->x, a->y, a->z, b->x, b->y,	      b->z, c.x, c.y, c.z);	return c;}/* calculates the length of vector's projection on another vector */static inline D3DVALUE ProjectVector (LPD3DVECTOR a, LPD3DVECTOR p){	D3DVALUE prod, result;	prod = ScalarProduct(a, p);	result = prod/VectorMagnitude(p);	TRACE("length projection of (%f,%f,%f) on (%f,%f,%f) = %f\n", a->x, a->y, a->z, p->x,              p->y, p->z, result);	return result;}/******************************************************************************* *              3D Buffer and Listener mixing */void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb){	/* volume, at which the sound will be played after all calcs. */	D3DVALUE lVolume = 0;	/* intensity (used for distance related stuff) */	double flIntensity;	double flTemp;	/* stuff for distance related stuff calc. */	D3DVECTOR vDistance;	D3DVALUE flDistance = 0;	/* panning related stuff */	D3DVALUE flAngle;	D3DVECTOR vLeft;	/* doppler shift related stuff */#if 0	D3DVALUE flFreq, flBufferVel, flListenerVel;#endif	TRACE("(%p)\n",dsb);	/* initial buffer volume */	lVolume = dsb->ds3db_lVolume;		switch (dsb->ds3db_ds3db.dwMode)	{		case DS3DMODE_DISABLE:			TRACE("3D processing disabled\n");			/* this one is here only to eliminate annoying warning message */			DSOUND_RecalcVolPan (&dsb->volpan);			DSOUND_ForceRemix (dsb);			break;		case DS3DMODE_NORMAL:			TRACE("Normal 3D processing mode\n");			/* we need to calculate distance between buffer and listener*/			vDistance = VectorBetweenTwoPoints(&dsb->ds3db_ds3db.vPosition, &dsb->dsound->device->ds3dl.vPosition);			flDistance = VectorMagnitude (&vDistance);			break;		case DS3DMODE_HEADRELATIVE:			TRACE("Head-relative 3D processing mode\n");			/* distance between buffer and listener is same as buffer's position */			flDistance = VectorMagnitude (&dsb->ds3db_ds3db.vPosition);			break;	}		if (flDistance > dsb->ds3db_ds3db.flMaxDistance)	{		/* some apps don't want you to hear too distant sounds... */		if (dsb->dsbd.dwFlags & DSBCAPS_MUTE3DATMAXDISTANCE)		{			dsb->volpan.lVolume = DSBVOLUME_MIN;			DSOUND_RecalcVolPan (&dsb->volpan);					/* i guess mixing here would be a waste of power */			return;		}		else			flDistance = dsb->ds3db_ds3db.flMaxDistance;	}			if (flDistance < dsb->ds3db_ds3db.flMinDistance)		flDistance = dsb->ds3db_ds3db.flMinDistance;		/* the following formula is taken from my physics book. I think it's ok for the *real* world...i hope m$ does it that way */	lVolume += 10000; /* ms likes working with negative volume...i don't */	lVolume /= 1000; /* convert hundreths of dB into B */	/* intensity level (loudness) = log10(Intensity/DefaultIntensity)...therefore */	flIntensity = pow(10,lVolume)*DEFAULT_INTENSITY;		flTemp = (flDistance/dsb->ds3db_ds3db.flMinDistance)*(flDistance/dsb->ds3db_ds3db.flMinDistance);	flIntensity /= flTemp;	lVolume = log10(flIntensity/DEFAULT_INTENSITY);	lVolume *= 1000; /* convert back to hundreths of dB */	lVolume -= 10000; /* we need to do it in ms way */	TRACE("dist. att: Distance = %f, MinDistance = %f => adjusting volume %ld to %f\n", flDistance, dsb->ds3db_ds3db.flMinDistance, dsb->ds3db_lVolume, lVolume);	/* conning */	/* sometimes it happens that vConeOrientation vector = (0,0,0); in this case angle is "nan" and it's useless*/	if (dsb->ds3db_ds3db.vConeOrientation.x == 0 && dsb->ds3db_ds3db.vConeOrientation.y == 0 && dsb->ds3db_ds3db.vConeOrientation.z == 0)	{		TRACE("conning: cones not set\n");	}	else	{		/* calculate angle */		flAngle = AngleBetweenVectorsDeg(&dsb->ds3db_ds3db.vConeOrientation, &vDistance);		/* if by any chance it happens that OutsideConeAngle = InsideConeAngle (that means that conning has no effect) */		if (dsb->ds3db_ds3db.dwInsideConeAngle != dsb->ds3db_ds3db.dwOutsideConeAngle)		{			/* my test show that for my way of calc., we need only half of angles */			DWORD dwInsideConeAngle = dsb->ds3db_ds3db.dwInsideConeAngle/2;			DWORD dwOutsideConeAngle = dsb->ds3db_ds3db.dwOutsideConeAngle/2;			/* full volume */			if (flAngle < dwInsideConeAngle)				flAngle = dwInsideConeAngle;			/* min (app defined) volume */			if (flAngle > dwOutsideConeAngle)				flAngle = dwOutsideConeAngle;			/* this probably isn't the right thing, but it's ok for the time being */			lVolume += ((dsb->ds3db_ds3db.lConeOutsideVolume)/((dwOutsideConeAngle) - (dwInsideConeAngle))) * flAngle;		}		TRACE("conning: Angle = %f deg; InsideConeAngle(/2) = %ld deg; OutsideConeAngle(/2) = %ld deg; ConeOutsideVolume = %ld => adjusting volume to %f\n",		       flAngle, dsb->ds3db_ds3db.dwInsideConeAngle/2, dsb->ds3db_ds3db.dwOutsideConeAngle/2, dsb->ds3db_ds3db.lConeOutsideVolume, lVolume);	}	dsb->volpan.lVolume = lVolume;		/* panning */	if (dsb->dsound->device->ds3dl.vPosition.x == dsb->ds3db_ds3db.vPosition.x &&	    dsb->dsound->device->ds3dl.vPosition.y == dsb->ds3db_ds3db.vPosition.y &&	    dsb->dsound->device->ds3dl.vPosition.z == dsb->ds3db_ds3db.vPosition.z) {		dsb->volpan.lPan = 0;		flAngle = 0.0;	}	else	{		vDistance = VectorBetweenTwoPoints(&dsb->dsound->device->ds3dl.vPosition, &dsb->ds3db_ds3db.vPosition);		vLeft = VectorProduct(&dsb->dsound->device->ds3dl.vOrientFront, &dsb->dsound->device->ds3dl.vOrientTop);		flAngle = AngleBetweenVectorsRad(&vLeft, &vDistance);		/* for now, we'll use "linear formula" (which is probably incorrect); if someone has it in book, correct it */		dsb->volpan.lPan = 10000*2*flAngle/M_PI - 10000;	}	TRACE("panning: Angle = %f rad, lPan = %ld\n", flAngle, dsb->volpan.lPan);	/* FIXME: Doppler Effect disabled since i have no idea which frequency to change and how to do it */#if 0		/* doppler shift*/	if ((VectorMagnitude(&ds3db.vVelocity) == 0) && (VectorMagnitude(&dsb->dsound->device->ds3dl.vVelocity) == 0))	{		TRACE("doppler: Buffer and Listener don't have velocities\n");	}	else	{		/* calculate length of ds3db.vVelocity component which causes Doppler Effect		   NOTE: if buffer moves TOWARDS the listener, it's velocity component is NEGATIVE		         if buffer moves AWAY from listener, it's velocity component is POSITIVE */		flBufferVel = ProjectVector(&dsb->ds3db_ds3db.vVelocity, &vDistance);		/* calculate length of ds3dl.vVelocity component which causes Doppler Effect		   NOTE: if listener moves TOWARDS the buffer, it's velocity component is POSITIVE		         if listener moves AWAY from buffer, it's velocity component is NEGATIVE */		flListenerVel = ProjectVector(&dsb->dsound->device->ds3dl.vVelocity, &vDistance);		/* formula taken from Gianicoli D.: Physics, 4th edition: */		/* FIXME: replace dsb->freq with appropriate frequency ! */		flFreq = dsb->freq * ((DEFAULT_VELOCITY + flListenerVel)/(DEFAULT_VELOCITY + flBufferVel));		TRACE("doppler: Buffer velocity (component) = %lf, Listener velocity (component) = %lf => Doppler shift: %ld Hz -> %lf Hz\n", flBufferVel, flListenerVel, \		      dsb->freq, flFreq);		/* FIXME: replace following line with correct frequency setting ! */		dsb->freq = flFreq;	}#endif			/* time for remix */	DSOUND_RecalcVolPan(&dsb->volpan);}static void DSOUND_Mix3DBuffer(IDirectSoundBufferImpl *dsb){	TRACE("(%p)\n",dsb);	DSOUND_Calc3DBuffer(dsb);	DSOUND_ForceRemix(dsb);			}static void WINAPI DSOUND_ChangeListener(IDirectSound3DListenerImpl *ds3dl){	int i;	TRACE("(%p)\n",ds3dl);	for (i = 0; i < ds3dl->dsound->device->nrofbuffers; i++)	{		/* some buffers don't have 3d buffer (Ultima IX seems to		crash without the following line) */		if (ds3dl->dsound->device->buffers[i]->ds3db == NULL)			continue;		if (ds3dl->dsound->device->buffers[i]->ds3db_need_recalc)		{			DSOUND_Mix3DBuffer(ds3dl->dsound->device->buffers[i]);		}	}}/******************************************************************************* *              IDirectSound3DBuffer *//* IUnknown methods */static HRESULT WINAPI IDirectSound3DBufferImpl_QueryInterface(	LPDIRECTSOUND3DBUFFER iface, REFIID riid, LPVOID *ppobj){	IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;	TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);	return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);}static ULONG WINAPI IDirectSound3DBufferImpl_AddRef(LPDIRECTSOUND3DBUFFER iface){    IDirectSound3DBufferImpl *This = (IDirectSound3DBufferImpl *)iface;    ULONG ref = InterlockedIncrement(&(This->ref));    TRACE("(%p) ref was %ld\n", This, ref - 1);    return ref;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -