HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Improved Sound Control System

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
soundsystemcontrolimproved

Problem

After a bit of work, I've changed my code a lot from the previous question:

I was recommended by George Mauer and schism to make a better structural design, so hopefully this new build is a bit sleeker in design. I will note that my main target is optimal performance. But like Mr. Mauer said, it is most likely negligible in this particular case.

What I've attempted since is removing duplicate code at the same time optimizing performance and fixing design. Here is what I came up with:

```
var MediaMaster = new (function(){
var sounds = {};// a container of Audio instances and names(keys) as a reference to their respective values.

// Master Controll of sounds
var liveSounds = [],
instanceVolume = {},
globalVolume = 1;

var SB = {muted: false, previousVolume:1};
this.soundBatch = this.sBatch = SB;
SB.toggleMute = function(){
if( SB.muted === false ){
// mute everything
liveSounds.forEach(function(snd){
snd.previousVolume = snd.volume;
snd.volume = 0;
});
SB.previousVolume = globalVolume;
globalVolume = 0;
return (SB.muted = true);
}
//unmute everything
liveSounds.forEach(function(snd){
snd.volume = snd.previousVolume;
delete snd.previousVolume;
});
globalVolume = SB.previousVolume;
return (SB.muted = false);
};

// Hide specific soundBatch functionality
(function(SB){
function killSound(snd){
snd.pause();
snd.onended();
snd.currentTime = 0;
}
function globalVolumeChange(v){ globalVolume = Math.pow(v,2);}
function globalStop(){liveSounds.forEach(function(snd){killSound(snd)});}
function globalPause(){ liveSounds.forEach(function(snd){snd.pause();}); }
function globalGetLive(){return liveSounds.slice();}
//Applies a callback to anything that passes the filter
function instanceApply(name, callback){
var match = sounds[name].src;
liveSounds.forEach(function(snd){
if( snd.src !==

Solution

Mainly style comments:

-
var SB = {muted: false, previousVolume:1}: you should have whitespace after your last colon.

-
Math.pow(v,2): you should have a space after the comma.

  • In the following four lines, you should have extra whitespace in between your brackets:



function globalVolumeChange(v){ globalVolume = Math.pow(v,2);}
function globalStop(){liveSounds.forEach(function(snd){killSound(snd)});}
function globalPause(){ liveSounds.forEach(function(snd){snd.pause();}); }
function globalGetLive(){return liveSounds.slice();}


  • if( snd.src !== match ) return;: you should wrap this in curly brackets, as it can have undesired effects



  • You should avoid having extra space in your if-else statement brackets like in the point above, and in a few different places.

Code Snippets

function globalVolumeChange(v){ globalVolume = Math.pow(v,2);}
function globalStop(){liveSounds.forEach(function(snd){killSound(snd)});}
function globalPause(){ liveSounds.forEach(function(snd){snd.pause();}); }
function globalGetLive(){return liveSounds.slice();}

Context

StackExchange Code Review Q#96138, answer score: 3

Revisions (0)

No revisions yet.