From 11fe8b0ec6632de79a4cff11ce270faddb8fe93d Mon Sep 17 00:00:00 2001 From: James Simpson Date: Fri, 19 Jan 2018 17:16:57 -0600 Subject: [PATCH] Update fade to use elapsed time to fix inconsistencies Fixes #885 --- src/howler.core.js | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/howler.core.js b/src/howler.core.js index 286b05f8..bb3b89e0 100644 --- a/src/howler.core.js +++ b/src/howler.core.js @@ -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);