Code archives/Audio/DistantSound()

This code has been declared by its author to be Public Domain code.

Download source code

DistantSound() by aab2004
EmitSound wasn't working for me so i developed this simple version which works rather well:

distantSound(soundHandle,cam,entity,range#=500,volume#=1,inview#=0.7)

soundhandle-The sound loaded with loadsound, that you wish to play

cam-the camera handle that you wish the sound to play to
entity-the emitting entity

range-the range apon which the sound can no longer be heard(ie has dissipated completely)[default is 500]

volume-The max starting volume. This also alters the dissipation of the sound: increasing the volume will cut the sound off before it fades completely [default is 1]

inview#- This determines the (percentage/100) of volume retained while within the cameras view: in other words if not in view, that much of the volume will be lost. [defaults to 0.7]

The sounds are therefore quiet from a distance, loud close up, and quieter when out of view
Function distantSound(soundHandle,cam,entity,range#=500,volume#=1,inview#=0.7)
	If range#<1 range#=1
	;If Not EntityInView(entity,cam) ChannelVolume soundHandle,0.5
	
;	dis#=Sqr(Abs(EntityX(entity)-EntityX(cam))*Abs(EntityX(entity)-EntityX(cam))+Abs(EntityY(entity)-EntityY(cam))*Abs(EntityY(entity)-EntityY(cam))+Abs(EntityZ(entity)-EntityZ(cam))*Abs(EntityZ(entity)-EntityZ(cam)))
dis#=entitydistance(cam,entity)	
	If 1-dis#/range#>0 And 1-dis#/range#<1
		SoundVolume soundHandle,volume#*(1-dis#/range#)*((1-inview)+(Float#(EntityInView(entity,cam))*inview))
		PlaySound soundHandle

	EndIf
	
End Function






;This one Checks for Co-ordinates rather than entities
Function distantSoundCoOrd(soundHandle,x1,y1,z1,x2,y2,z2,range#=500,volume#=1,inview#=0.7)
	If range#<1 range#=1
	;If Not EntityInView(entity,cam) ChannelVolume soundHandle,0.5
	
	dis#=Sqr(Abs(x2-x1)*Abs(x2-x1))+Abs(y2-y1)*Abs(y2-y1)+Abs(z2-z1)*Abs(z2-z1)

	If 1-dis#/range#>0 And 1-dis#/range#<1
		SoundVolume soundHandle,volume#*(1-dis#/range#)*((1-inview)+(Float#(EntityInView(entity,cam))*inview))
		PlaySound soundHandle

	EndIf
	
End Function

Comments

None.

Code Archives Forum