[Source/Feedback] AS2 sound management class
Hey guys. For the first time in ages I've got the point of putting sound in a game (oh my!), so I decided to actually go all out and use a 'proper' sound management class for the event. I started by searching the various internets, including FK for clues or hell, even a full blown source, and found that there was surprisingly little out there on the subject.
So I wrote my own.
I figure if I had trouble finding resources, especially specific for games, then some of you guys must also. So I'm going to share my code with you guys, feel free to make use of it and if you make any changes or additions I'd appreciate if you posted them here, if that's not too much to ask :)
Keep in mind this is only designed to handle sound effects, not music loops, cross fading or anything like that. There's plenty more you could add to this, but I wanted to keep it at just the basics for now: play, volume and pan. It's written as an AS2 class, so to use it just copy from below, paste into soundEngine.as and put it with the rest of your classes.
the code:
Code:
class soundEngine {
// declaring our variables
// soundsArray: this array stores all the soundFX used in the game
private var soundsArray:Array = [];
// soundChannels: this array stores the currently playing sounds and acts
// as a cache for recently played sounds
private var soundChannels:Array = [];
// number of channels: this is how many sound channels we will use, you can
// add more or less as per your needs. I'm going with 16.
private var noOfChannels:Number = 16;
// function is called to initiate the sound engine
public function initSounds (){
// populate soundsArray the first value is the sound's linkageID the second value
// is the name we will refer to it in the engine. This allows us to swap out sound
// effects without affecting previously written code.
soundsArray = [["door_00.WAV","doorToggle"],["switch_00.WAV","switchToggle"]]; // and so on...
// clear the channels
soundChannels = [];
// create the channel MC's. Because flash needs a unique MC container to give
// sounds objects their own pan/volume/etc properties. go figure.
_root.createEmptyMovieClip("sounds", 9);
for (var i=0; i < noOfChannels; i++){
_root.sounds.createEmptyMovieClip("channel"+i, i);
}
}
// this will be the function we call to play a sound. Pramaters required are:
// id: this is the name we give each soundFX in the soundsArray (see above)
// vol and pan are pretty straight forward
public function playSound (id,vol,pan){
var done:Boolean = false;
// first we'll check if there's a copy of the desired sound in an open channel
for (var i in soundChannels){
if (soundChannels[i][0] == id & soundChannels[i][1].position == soundChannels[i][1].duration){
// we've found one and it's not playing, excellent, all we have to
// do is play it and we're done.
doSound(i,vol,pan);
done=true;
break
}
}
if (!done){
// well we didn't find the sound already, lets create one.
// first we'll make sure there's room in the channels array
if (soundChannels.length >= noOfChannels) {
// there's not, so we remove the first one which isn't playing
for (var j in soundChannels){
if(soundChannels[j][1].position == soundChannels[j][1].duration){
soundChannels.splice(j,1);
done=true;
break
}
}
}
// start by finding the desired soundFX in the soundsArray
for (var i in soundsArray){
if (soundsArray[i][1] == id){
// it's there, now we create the new sound
var n = soundChannels.push([id]) - 1; // push it's name to the channels array
soundChannels[n][1]= new Sound(_root.sounds["channel"+n]); // create the sound
soundChannels[n][1].attachSound(soundsArray[i][0]); // and attach it
doSound(n,vol,pan); // now play
break
}
}
}
}
// function sets the volume, pan then plays the desired sound
private function doSound(i,vol,pan){
var channel = soundChannels[i][1];
channel.setVolume(vol ? vol : 100);
channel.setPan(pan ? pan : 0);
channel.start();
}
}
and now a code snipet of how to use it:
Code:
// on your main timeline
var soundUtil = new soundEngine;
soundUtil.initSounds();
Code:
// and later on in your game code
_root.soundUtil.playSound("doorToggle",100,0);
// and so on...
This will play the sound we titled "doorToggle" at 100 volume paned to the center. Easy ;)