Skip to content

Commit

Permalink
Update fade to use elapsed time to fix inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfire committed Jan 19, 2018
1 parent 97391fa commit 11fe8b0
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/howler.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,28 +1166,20 @@
_startFadeInterval: function(sound, from, to, len, id, isGroup) {
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;
var stepVol = diff / steps;

// Since browsers clamp timeouts to 4ms, we need to clamp our steps to that too.
if (stepLen < 4) {
steps = Math.ceil(steps * stepLen * 0.25);
stepLen = 4;
stepVol = diff / steps;
}
var diff = to - from;
var steps = Math.abs(diff / 0.01);
var stepLen = Math.max(4, (steps > 0) ? len / steps : len);
var lastTick = Date.now();

// Store the value being faded to.
sound._fadeTo = to;

// Update the volume value on each interval tick.
sound._interval = setInterval(function() {
// Update the volume amount, but only if the volume should change.
if (steps > 0) {
vol += (dir === 'in' ? stepVol : -stepVol);
}
// Update the volume based on the time since the last tick.
var tick = (Date.now() - lastTick) / len;
lastTick = Date.now();
vol += diff * tick;

// Make sure the volume is in the right bounds.
vol = Math.max(0, vol);
Expand Down

0 comments on commit 11fe8b0

Please sign in to comment.