Skip to content

Commit

Permalink
Refactor fade to eliminate bind memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfire committed Aug 15, 2017
1 parent 5d0753d commit 245e7aa
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions src/howler.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,16 +1077,6 @@
*/
fade: function(from, to, len, id) {
var self = this;
var diff = Math.abs(from - to);
var dir = from > to ? 'out' : 'in';
var steps = diff / 0.01;
var stepLen = (steps > 0) ? len / steps : len;

// Since browsers clamp timeouts to 4ms, we need to clamp our steps to that too.
if (stepLen < 4) {
steps = Math.ceil(steps / (4 / stepLen));
stepLen = 4;
}

// If the sound hasn't loaded, add it to the load queue to fade when capable.
if (self._state !== 'loaded') {
Expand Down Expand Up @@ -1125,43 +1115,66 @@
sound._node.gain.linearRampToValueAtTime(to, end);
}

var vol = from;
sound._interval = setInterval(function(soundId, sound) {
// Update the volume amount, but only if the volume should change.
if (steps > 0) {
vol += (dir === 'in' ? 0.01 : -0.01);
}
self._startFadeInterval(sound, from, to, len);
}
}

return self;
},

/**
* Starts the internal interval to fade a sound.
* @param {Object} sound Reference to sound to fade.
* @param {Number} from The value to fade from (0.0 to 1.0).
* @param {Number} to The volume to fade to (0.0 to 1.0).
* @param {Number} len Time in milliseconds to fade.
*/
_startFadeInterval: function(sound, from, to, len) {
var self = this;
var vol = from;
var dir = from > to ? 'out' : 'in';
var diff = Math.abs(from - to);
var steps = diff / 0.01;
var stepLen = (steps > 0) ? len / steps : len;

// Since browsers clamp timeouts to 4ms, we need to clamp our steps to that too.
if (stepLen < 4) {
steps = Math.ceil(steps / (4 / stepLen));
stepLen = 4;
}

// Make sure the volume is in the right bounds.
vol = Math.max(0, vol);
vol = Math.min(1, vol);
sound._interval = setInterval(function() {
// Update the volume amount, but only if the volume should change.
if (steps > 0) {
vol += (dir === 'in' ? 0.01 : -0.01);
}

// Round to within 2 decimal points.
vol = Math.round(vol * 100) / 100;
// Make sure the volume is in the right bounds.
vol = Math.max(0, vol);
vol = Math.min(1, vol);

// Change the volume.
if (self._webAudio) {
if (typeof id === 'undefined') {
self._volume = vol;
}
// Round to within 2 decimal points.
vol = Math.round(vol * 100) / 100;

sound._volume = vol;
} else {
self.volume(vol, soundId, true);
}
// Change the volume.
if (self._webAudio) {
if (typeof id === 'undefined') {
self._volume = vol;
}

// When the fade is complete, stop it and fire event.
if ((to < from && vol <= to) || (to > from && vol >= to)) {
clearInterval(sound._interval);
sound._interval = null;
self.volume(to, soundId);
self._emit('fade', soundId);
}
}.bind(self, ids[i], sound), stepLen);
sound._volume = vol;
} else {
self.volume(vol, sound._id, true);
}
}

return self;
// When the fade is complete, stop it and fire event.
if ((to < from && vol <= to) || (to > from && vol >= to)) {
clearInterval(sound._interval);
sound._interval = null;
self.volume(to, sound._id);
self._emit('fade', sound._id);
}
}, stepLen);
},

/**
Expand Down

0 comments on commit 245e7aa

Please sign in to comment.