This is my sound system that allows for the following functionality:
- One master volume that controls every sound.
- A function to stop/pause every playing sound
- A function to control every instance of a sounds volume(@creation)
- A function to stop/pause every instance of the same sound
- Sounds that are playing are kept in a list to keep track of.
- retrieve a list of specified sounds playing
- Retrieve a list of every sound playing
How could I possibly improve the code, or the system itself? I could possibly allow the user to define layers to encapsulate sectors of sound and not just the same instance. I could probably make a user.
// exponentially decrease sounds
this.Sound.masterVolume = function(v){ masterVolume = Math.pow(v,2);};
this.Sound.instanceVolume = function(name, v){ instanceVolume[name] = Math.pow(v,2);};
this.Sound.masterStop = function(){
liveSounds.forEach(function(snd){
snd.pause();
snd.onended();
snd.currentTime = 0;
});
}
this.Sound.masterPause = function(){
liveSounds.forEach(function(snd){
snd.pause();
});
};
this.Sound.instanceStop = function( name ){
var match = sounds[name].src;
liveSounds.forEach(function(snd){
if( snd.src !== match ) return;
snd.pause();
snd.onended();
snd.currentTime = 0;
});
};
this.Sound.instancePause = function( name ){
var match = sounds[name].src;
liveSounds.forEach(function(snd){
if( snd.src !== match ) return;
snd.pause();
});
}
this.Sound.instanceGetLive = function( name ){
return liveSounds.filter(function(snd){
return( snd.src === match );
});
}
this.Sound.masterGetLive = function(){
return liveSounds.splice();//only a copy.
}
this.sound = function(name){
var snd = new Audio( sounds[name].src ),
that = this;
snd.volume = instanceVolume[name] || 1;
snd.play = function(){
this.volume *= masterVolume;
Audio.prototype.play.apply(this);
liveSounds.push(this);
this.onended = function(){ liveSounds.splice( liveSounds.indexOf(snd), 1 );};
};
return snd;
};
this
was, as well as stuff like the context formasterVolume
andinstanceVolume
-- are these globals? Having the enclosing closure would be super useful for reviewing. \$\endgroup\$