Sound Controller
by Steven Hine · in Torque Game Builder · 01/07/2009 (5:57 am) · 1 replies
I've made this sound controller for you all. Create an empty object in your scene and name it pAudioController with audioController as the class. You only have to keep track of which channel the sound is playing on. The first four channels are reserved for other game sounds. I wouldn't use this controller for simple sound effects that are short.
I put this at the top of my audio.cs file:
$GameAudio = 0;
$MenuAudio = 1;
In the type field use one of these for AudioDescriptions.
Here is how to play a sound using the controller:
"spawner" is a unique name that you don't have to worry about. Just give each sound a different name.
true is for looping.
true is for preload.
1.0 = is the volume to start at.
"creepySound.ogg" is the file of the sound.
%this.soundChannel is the channel the sound is playing on. It will need to be passed to pAudioController to fade sounds. Here is how to fade a sound:
Pretty simple I think. I will be adding more to this, but check it out. Just create a script file called audioController.cs and add this code:
I put this at the top of my audio.cs file:
$GameAudio = 0;
$MenuAudio = 1;
In the type field use one of these for AudioDescriptions.
datablock AudioDescription(AudioNonLooping)
{
volume = 0.50;
isLooping = false;
is3D = false;
type = $GameAudio;
};Here is how to play a sound using the controller:
%this.soundChannel = pAudioController.playSound("spawner", true, true, 1.0, "creepySound.ogg");"spawner" is a unique name that you don't have to worry about. Just give each sound a different name.
true is for looping.
true is for preload.
1.0 = is the volume to start at.
"creepySound.ogg" is the file of the sound.
%this.soundChannel is the channel the sound is playing on. It will need to be passed to pAudioController to fade sounds. Here is how to fade a sound:
if(isPlayerInsight(%this.owner.getPosition(), %this.playDistance) && !%this.soundPlaying)
{
echo("Spawner playing sound.");
%this.Sound = pAudioController.playSound( "spawner", true, true, 1.0, "theylivemaster.ogg");
%this.soundPlaying = true;
}else if(!isPlayerInsight(%this.owner.getPosition(), %this.playDistance) && %this.soundPlaying)
{
pAudioController.fade(%this.Sound);
%this.soundPlaying = false;
}Pretty simple I think. I will be adding more to this, but check it out. Just create a script file called audioController.cs and add this code:
About the author
Torque Owner Steven Hine
function audioController::onLevelLoaded(%this, %sceneGraph) { //creaating SimSet if(!isObject(AudioSimSet)) { %this.AudioSimSet = new SimSet(%this.AudioSimSet); } //create 12 channels and set playing to false for(%i = 0; %i < 12; %i++) { %newObj = new SimObject("channel" @ %i); %this.AudioSimSet.add(%newObj); %channel = %this.AudioSimSet.getObject(%i); //reserve channels 1-4(0-3) if(%i < 4) { %channel.playing = true; %channel.looping = true; %channel.fade = false; } else { %channel.playing = false; %channel.fade = false; } } %this.enableUpdateCallback(); } function audioController::onUpdate(%this) { //check for nonlooping sounds playing that have stopped and fading channels for(%i = 0; %i < 12; %i++) { %sound = %this.AudioSimSet.getObject(%i); //looping check if(!%sound.looping) { if(%sound.playing) { echo("Checking Channel:" SPC %sound.getName() SPC "sound playing:" SPC %sound.playing); //check sound %sound.playing = alxIsPlaying(%sound.soundName); if(!%sound.playing) echo("Stopped"); } } //fade check if(%sound.fade) { %this.decreaseVolume(%i); } } } function audioController::CreateProfile(%this, %name, %volume, %filename, %description) { if (!isobject(%name @ "AudioProfile") ) { new AudioProfile(%name @ "AudioProfile") { volume = %volume ; filename = "~/data/audio/" @ %filename; description = %description; }; echo(%name @ "AudioProfile" SPC "Profile created"); } return %name @ "AudioProfile"; } function audioController::CreateDescription(%this, %name) { if (!isobject(%name @ "AudioDes")) { new AudioDescription( %name @ "AudioDes") { }; echo(%name @ "AudioDes" SPC "Description created"); } return %name @ "AudioDes"; } function audioController::playSound(%this, %name, %isLooping, %preload, %volume, %filename) { %playChannel = 0; //get the total count of simobjects %channelCount = %this.AudioSimSet.getCount(); //find unused channel that is not playing for(%ii = 0; %ii < %channelCount; %ii++) { if(!%this.AudioSimSet.getObject(%ii).playing) { echo("Channel found breaking:" SPC %ii); %playChannel = %ii; break; } } echo("Channel found" SPC %playChannel SPC "Looping:" SPC %isLooping); %soundDes = %this.CreateDescription(%name); //set description %soundDes.volume = %volume; %soundDes.isLooping = %isLooping; %soundDes.is3D = false; %soundDes.isStreaming = false; %soundDes.preload = %preload; %soundDes.type = %playChannel; %soundToPlay = %this.CreateProfile(%name, %volume, %filename, %soundDes); //add to simset %channel = %this.AudioSimSet.getObject(%playChannel); %channel.playing = true; %channel.looping = %isLooping; %channel.soundName = alxPlay(%soundToPlay); return %playChannel; } function audioController::fade(%this, %channel) { %SimChannel = %this.AudioSimSet.getObject(%channel); %simChannel.fade = true; } function audioController::decreaseVolume(%this, %channel) { %stillPlaying = true; //decrease volume until stop %vol = alxGetChannelVolume( %channel ); if(%vol > 0) { %vol -= 0.025; echo("decreasing volume"); } alxSetChannelVolume( %channel , %vol ); if(%vol <= 0.25) { %this.stopSound(%channel); %stillPlaying = false; alxSetChannelVolume( %channel , 1.0 ); } return %stillPlaying; } function audioController::stopSound(%this, %channel) { echo("Stopping channel"); %soundChannel = %this.AudioSimSet.getObject(%channel); alxStop(%soundChannel.soundName); %soundChannel.playing = false; %soundChannel.fade = false; } function audioController::listPlayingChannels(%this) { %channelCount = %this.AudioSimSet.getCount(); for(%ii = 0; %ii < %channelCount; %ii++) { if(%this.AudioSimSet.getObject(%ii).playing) { echo("Channel:" SPC %this.AudioSimSet.getObject(%ii).getName() SPC "is playing"); } } }