jmod
Version:
audio / dsp enveloping with some added features
288 lines (238 loc) • 8.28 kB
JavaScript
;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var offset = require('set-it-off')
var h1 = document.createElement('h1');
h1.textContent = "WHERE AM I?";
//document.body.appendChild(h1);
var canvas = document.createElement('canvas')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
document.body.appendChild(canvas);
draw = canvas.getContext('2d')
offset.setParent(null, [-(window.innerWidth / 2), -(window.innerHeight/2)])
h1.style.position = 'absolute';
h1.style.left = offset.offset([0,0])[0] + 'px';
h1.style.top = offset.offset([0,0])[1] + 'px';
//h1.moveTo(0,0)
console.log(offset.offset([0,0]))
c = require('./')
var attack = [[0,0], [0,1], [1,1]]
var release = [[0,1], [.5,1], [1, .5]]
var sustain = [[0, .5], [0, 0], [.5, 1], [1, .5]]
//sustain = [[0, 0.5], [.5, .5], [1, .5]]
var decay = [[0,.5], [.5, .25], [1,0]]
var durations = [.25, .25, .4, .1]
var amod = {}
amod.curves = [attack, release, sustain, decay]//, release, sustain, decay];
amod.durations = durations
var x = c(amod)
console.log(x.envelope)
draw.lineWidth = 3
draw.beginPath()
draw.moveTo(offset.offset(0,0));
draw.strokeStyle = "black";
for(var i = 0; i < 1; i+=.001){
// console.log(x.envelope(i))
var off = offset.offset([0 + (window.innerWidth * i), window.innerHeight * x.envelope(i)])
draw.lineTo(off[0], off[1])
draw.stroke()
}
},{"./":2,"set-it-off":5}],2:[function(require,module,exports){
var nvelope = require('../nvelope');
module.exports = function(params){
var envelope = nvelope(params.curves, params.durations);
var sustain, cutoff;
if(params.sustain){ // refers to some segment of the envelope
if(Object.prototype.toString.call(params.sustain) === '[object Object]'){ // sustain is its own envelope
console.log('ehll;');
var dur = params.sustain.durations.reduce(function(e,i){return e + i}, 0);
var env = nvelope(params.sustain.curves, params.sustain.durations);
var diff = undefined
sustain = function(t){
if(diff === undefined) diff = Math.abs(t)
t = t - diff;
return env(t % dur)
}
}
else if(Array.isArray(params.sustain)){
var td = params.durations.reduce(function(e, i){return e + i}, 0);
var xd = params.durations.slice(params.sustain[0], params.sustain[0] + params.sustain.length)
var sd = xd.reduce(function(e, i){return e+i},0);
var start = params.durations.slice(0, params.sustain[0]).reduce(function(e,i){return e+i},0)
console.log(start, sd)
var diff = undefined;
sustain = function(t){
if(t < start) return envelope(t);
else{
if(diff === undefined) diff = Math.abs(t);
t = t - diff;
console.log((t % sd))
return envelope(start + (t % sd))
}
}
}
else if(!isNaN(params.sustain)){ // sustain is a static amplitude value
sustain = function(){return params.sustain}
}
}
return {envelope: envelope, sustain: sustain}
}
},{"../nvelope":9}],3:[function(require,module,exports){
module.exports = function(obj){
var curleft = curtop = 0;
if (obj.parentElement) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.parentElement);
}
return [curleft,curtop];
}
},{}],4:[function(require,module,exports){
module.exports = function(el, prop){
var propValue = document.defaultView.getComputedStyle(el).getPropertyValue(prop)
if(!propValue) throw new Error("No prop valueValue. Is the element appended to the document yet?")
if(!propValue) return false
return (parseInt(propValue) || 0)
}
},{}],5:[function(require,module,exports){
var getCSS = require('./getCSS');
var findPos = require('./findPos');
var prefix = require('./prefix').css
var elements = {};
var parent = {center : [window.innerWidth / 2, window.innerHeight / 2]}
var e = module.exports
e.setParent = function(el, offset){
if(!el && offset){
parent.center[0]+=offset[0];
parent.center[1]-=offset[1];
}
else{
if('string' == typeof el){
el = document.getElementById(el)
}
parent = objectify(el);
if(offset){
parent.center[0]+=offset[0];
parent.center[1]-=offset[1];
}
}
}
e.offset = function(x){
x[0] = (parent.center[0] + x[0]);
x[1] = (parent.center[1] - x[1]);
return x
}
HTMLElement.prototype.moveTo = function(x, y){
if('array' == typeof x) {
y = x[1];
x = x[1];
}
this.cartesian = [x, y]
var pos = findPos(this)
var z = objectify(this)
this.style['transform'] = 'translate(' + (parent.center[0] + x - (z.width / 2)) + 'px,' + (parent.center[1] - y - (z.height / 2)) + 'px)';
this.style[prefix + 'transform'] = 'translate(' + (parent.center[0] + x - (z.width / 2)) + 'px,' + (parent.center[1] - y - (z.height / 2)) + 'px)';
}
function offsetDOM(x){
x[0] = x[0] - parent.center[0]
x[1] = parent.center[1] - x[1]
return x
}
function objectify(el){
var obj = {el: el};
obj.width = parseFloat(getCSS(el, 'width'));
obj.height = parseFloat(getCSS(el, 'height'));
var p = obj.position = findPos(el);
obj.center = [p[0] + (obj.width / 2), p[1] + (obj.height / 2)];
obj.centerOffset = [obj.width / 2, obj.height / 2];
if(el.id.length > 0) elements[el.id] = obj;
return obj
}
},{"./findPos":3,"./getCSS":4,"./prefix":6}],6:[function(require,module,exports){
// from http://davidwalsh.name/vendor-prefix
var styles = window.getComputedStyle(document.documentElement, '')
var pre = (Array.prototype.slice
.call(styles)
.join('')
.match(/-(moz|webkit|ms)-/) || (styles.OLink === '' && ['', 'o'])
)[1]
var dom = ('WebKit|Moz|MS|O').match(new RegExp('(' + pre + ')', 'i'))[1];
module.exports = {
dom: dom,
lowercase: pre,
css: '-' + pre + '-',
js: pre[0].toUpperCase() + pre.substr(1)
}
},{}],7:[function(require,module,exports){
module.exports = function(start, dur, min, max){
if(!min) min = 0;
if(!max) max = 1;
var end = start + dur;
var d = end - start;
var r = max - min;
return function(time){
x = min + (time - start) * r / d
if(x > 1){
// console.log('pre', time, end)
if(time < end) x = Number('.' + x.toString().split('.').join(''))
// console.log('norm', x)
}
return x
}
}
},{}],8:[function(require,module,exports){
module.exports = function (pts) {
return function (t) {
for (var a = pts; a.length > 1; a = b){
for (var i = 0, b = [], j; i < a.length - 1; i++){
for (b[i] = [], j = 1; j < a[i].length; j++){
b[i][j] = a[i][j] * (1 - t) + a[i+1][j] * t;
}
}
}
return a[0][1];
}
}
},{}],9:[function(require,module,exports){
var amod = require('./amod');
var tnorm = require('../normalize-time');
module.exports = function(pts, durs){
pts = pts.map(amod)
var t = 0;
var totalDuration = durs.reduce(function(e,i){return e + i}, 0);
var tdNormFN = tnorm(t, totalDuration);
var s = 0;
var end = t + totalDuration;
var durFNS = durs.map(function(e,i){
var x = tnorm(t + s, e)
s += e;
return x
})
var dp = 0;
var durpercent = durs.map(function(e, i){
var x = (e / totalDuration) + dp;
dp+= (e / totalDuration)
return x
})
var tn, n, i, v = 0, fn = 0;
console.log(durpercent)
var envelope = function(t){
tn = tdNormFN(t);
if(0 > tn || tn > 1) return 0;
fn = durpercent.reduce(function(p, e, i, d){return ((d[i-1] || 0) <= tn && tn <= e) ? i : p}, 0)
console.log(t, tn, fn, durpercent)
v = pts[fn](durFNS[fn](t))
return v
}
return envelope
// probably deletable
function xenvelope(t, sustain){
tn = tdNormFN(t);
if(0 >= tn || tn >= 1) return 0;
if(tn > durpercent[fn]) fn = (fn + 1 > pts.length - 1 ? 0 : fn + 1)
v = pts[fn](durFNS[fn](t))
return v
}
}
},{"../normalize-time":7,"./amod":8}]},{},[1])
;