Code archives/Audio/PlaySound (better)

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

Download source code

PlaySound (better) by UnderwoodNullium2008
By using a sound's x and y coordinates, this function pans the sound, and decreases the volume the further away it is from the middle of the screen. There is also a 'spread' variable that controls the volume 'dropoff'...

Also, it changes the pitch of the sound to make each sound a little different from the past one. The distance formula is needed, so it's added as well.

*I am also Frostbyte for everyone at blitzcodebase.co.uk, so I'm not stealing!
Function PlaySoundBetter(sound:TSound,soundx:Int,soundy:Int,originx:Int,originy:Int,volspread:Float)

	Local channel:TChannel
	Local volume:Float
	Local panvalue:Float

	channel:TChannel = AllocChannel()				        ' allocate a channel

	volume#   = (1 - ((GetDistance(originx,originy,soundx,soundy) * (1 - volspread#)) / 100))
	panvalue# = (2 * ((soundx / GraphicsWidth())) - 1)

		If panvalue# < -1 panvalue# = -1
		If panvalue# > 1 panvalue# = 1

	SetChannelRate(channel:TChannel,Rnd(.8,1.2))				' make each sound original
	SetChannelVolume(channel:TChannel,volume#)
	SetChannelPan(channel:TChannel,panvalue#)

		If volume# > 0 PlaySound(sound:TSound,channel:TChannel)        ' play sound

End Function


Function GetDistance:Float(x1#,y1#,x2#,y2#)

	Return(Sqr(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))))

End Function

Comments

None.

Code Archives Forum