-
-
Save NHQ/4325215 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all values are of type [x, y] | |
// and are coordinates, x being time, y being amplitude | |
// ie. env = require('./')([1/16, 1], [1/32, .9], [3/32, .9], [1/2, 0]) | |
// returns an amplitude to multiply yr sample by | |
module.exports = function(a, d, s, r){ | |
function e(t, a, d, s, r){ | |
if (arguments.length) init(a, d, s, r); | |
var z; | |
return compute(); | |
function compute(){ | |
z = t % e._duration; | |
if (z <= e.a[0]) return e.a.slope(z); | |
else if (z <= e.a[0] + e.d[0]) return e.d.slope(z - (e.a[0])); | |
else if (z <= e.a[0] + e.d[0] + e.s[0]) return e.s.slope(z - (e.a[0] + e.d[0])); | |
else if (z <= e._duration) return e.r.slope(z - (e.a[0] + e.d[0] + e.s[0])); | |
else return 1 | |
}; | |
}; | |
init(a, d, s, r); | |
return e; | |
function init(a, d, s, r){ | |
a.slope = slope([0, a[0]], [0, a[1]] ) | |
d.slope = slope([a[0], d[0]], [a[1], d[1]] ) | |
s.slope = slope([d[0], s[0]], [d[1], s[1]] ) | |
r.slope = slope([s[0], r[0]], [s[1], r[1]] ) | |
e.a = a; | |
e.d = d; | |
e.s = s; | |
e.r = r || [0.0]; | |
e.duration = function(){ | |
return this.a[0] + this.d[0] + this.s[0] + this.r[0]; | |
}; | |
e._duration = e.duration(); | |
function slope(x, y){ | |
var m = (y[1] - y[0]) / (x[1] - 0); | |
m = (isNaN(m)) ? 0 : m; | |
return function(t){ | |
return (m * (t - x[1])) + y[1] | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment