Skip to content

Commit

Permalink
Improve timing of short fades
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfire authored and bhaskarp-vg committed Feb 7, 2018
1 parent 5f36f9e commit a64db09
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/howler.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,11 +1169,13 @@
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 / (4 / stepLen));
steps = Math.ceil(steps * stepLen * 0.25);
stepLen = 4;
stepVol = diff / steps;
}

// Store the value being faded to.
Expand All @@ -1183,7 +1185,7 @@
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);
vol += (dir === 'in' ? stepVol : -stepVol);
}

// Make sure the volume is in the right bounds.
Expand Down

0 comments on commit a64db09

Please sign in to comment.