Game Development Community

Easy way to control sound volume

by Jason McIntosh · in Torque Game Builder · 06/29/2005 (10:09 am) · 0 replies

I thought this little snippet might be useful to someone.
function playSound( %datablock )
{
	%handle = alxPlay( %datablock );
	if ( %datablock.volume !$= "" )
		alxSourcef( %handle, "AL_GAIN", %datablock.volume );
	return %handle;
}
It returns the OpenAL handle, just like alxPlay(). What you do is add a "volume" field to the sound datablock, like so:
new AudioProfile( desertBreeze_snd )
{
	filename = "game/arenas/desert/audio/breeze.wav";
	description = Loop_Audio;
	preload = false;
	volume = 0.9; // we look for this and set it if present
};
And when you play the sound, it will be played at the specified volume. I found it annoying that I couldn't adjust the volume on individual sounds, so I created this function.