Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4325197 to your computer and use it in GitHub Desktop.
Save anonymous/4325197 to your computer and use it in GitHub Desktop.
simple linear envelope
// values are zero-one normalized
// all values are of type [x, y]
// and arr coordinates, x being time, y being amplitude
// returns an amplitude
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