Core2D [Spat]ial Sound System
BlitzMax Forums/BlitzMax Programming/Core2D [Spat]ial Sound System| 
 | ||
| The SetChannelPan command is pretty neat, but especially neat when you pair it up with a pointer and dynamically pan to a moving object on screen! Here's what I call [Spat]ial Sound System. Try to use a sound that is 5 seconds or longer so you can really hear it pan in real-time as you move the mouse left to right. Please test and comment. Thanks. 
' [Spat]ial Sound System (part of the Core2D Nirvana Sound Module)
' by Chroma
Private
Global _hgw
Global _spats
Global _spatList:TList = CreateList()
Public
Type TSpat
	Field chn:TChannel
	Field xInt:Int Ptr
	Field xFlt:Float Ptr
	
	Function Create:TSpat(chn:TChannel,xFlt:Float Ptr,xInt:Int Ptr)
		Local spat:TSpat = New TSpat
		spat.chn = chn
		spat.xInt = xInt
		spat.xFlt = xFlt
		Return spat
	End Function	
End Type
Function SpatInit(gfxWidth)
	_hgw = gfxwidth / 2
End Function
Function PlaySpat(sound:TSound,xFlt:Float Ptr,xInt:Int Ptr=Null,track=0)
	If Not _hgw RuntimeError("ALERT: SpatInit(graphicsWidth) is Not set.")
	Local x#,pan#,chn:TChannel
	If xFlt <> Null x = xFlt[0] Else x = xInt[0]
	pan = x / (_hgw / 2.0) - 1
	chn = AllocChannel()
	SetChannelPan(chn,pan)
	PlaySound(sound,chn)
	If track
		_spatList.AddLast(TSpat.Create(chn,xFlt,xInt))
		_spats:+1
	EndIf
End Function
Function UpdateSpats()
	If _spats
		Local spat:TSpat,x#,pan#
		For spat = EachIn _spatList
			If spat.xFlt <> Null x = spat.xFlt[0] Else x = spat.xInt[0]
			pan   = x / (_hgw  / 2.0) - 1
			SetChannelPan(spat.chn,pan)
			If Not ChannelPlaying(spat.chn)
				_spatList.Remove(spat)
				_spats:-1
			EndIf
		Next
	EndIf
End Function
'===TEST===
Graphics 1024,768
SpatInit(1024)										'set this to your graphics width or virtual resolution width
Local snd:TSound = LoadSound("chest_open.wav")				'put in your own sound, try to use a longer sound
Local mxFloat:Float									'must use Float variable with Float Ptr
Local mxInt:Int										'must use Int variable with Int Ptr
While Not KeyHit(KEY_ESCAPE)
Cls
	mxFloat = MouseX()	
	mxInt = MouseX()
	my = MouseY()
	
	
	DrawText "Mouse: "+MouseX()+","+MouseY(),10,10
	
	'-->You MUST put Varptr() in there for your x position variable
	If MouseHit(1) PlaySpat(snd,Varptr(mxFloat),Null,True)		'Like this for Float vars
	'If MouseHit(1) PlaySpat(snd,Null,Varptr(mxInt),True)		'Like this for Int vars
	
	UpdateSpats()
Flip
Wend
Last edited 2010 |