flocking
Version:
Creative audio synthesis for the Web
88 lines (75 loc) • 2.78 kB
JavaScript
/*
* Flocking Listening Unit Generators
* https://github.com/continuing-creativity/flocking
*
* Copyright 2011-2014, Colin Clark
* Dual licensed under the MIT and GPL Version 2 licenses.
*/
/*global require*/
/*jshint white: false, newcap: true, regexp: true, browser: true,
forin: false, nomen: true, bitwise: false, maxerr: 100,
indent: 4, plusplus: false, curly: true, eqeqeq: true,
freeze: true, latedef: true, noarg: true, nonew: true, quotmark: double, undef: true,
unused: true, strict: true, asi: false, boss: false, evil: false, expr: false,
funcscope: false*/
var fluid = fluid || require("infusion"),
flock = fluid.registerNamespace("flock");
(function () {
"use strict";
flock.ugen.amplitude = function (inputs, output, options) {
var that = flock.ugen(inputs, output, options);
that.gen = function (numSamps) {
var m = that.model,
source = that.inputs.source.output,
out = that.output,
prevAtt = m.attackTime,
nextAtt = that.inputs.attack.output[0],
prevRel = m.releaseTime,
nextRel = that.inputs.release.output[0],
prevVal = m.prevVal,
attCoef = m.attackCoef,
relCoef = m.releaseCoef,
i,
val,
coef;
// Convert 60 dB attack and release times to coefficients if they've changed.
if (nextAtt !== prevAtt) {
m.attackTime = nextAtt;
attCoef = m.attackCoef =
nextAtt === 0.0 ? 0.0 : Math.exp(flock.LOG01 / (nextAtt * m.sampleRate));
}
if (nextRel !== prevRel) {
m.releaseTime = nextRel;
relCoef = m.releaseCoef =
(nextRel === 0.0) ? 0.0 : Math.exp(flock.LOG01 / (nextRel * m.sampleRate));
}
for (i = 0; i < numSamps; i++) {
val = Math.abs(source[i]);
coef = val < prevVal ? relCoef : attCoef;
out[i] = prevVal = val + (prevVal - val) * coef;
}
m.unscaledValue = m.prevVal = prevVal;
that.mulAdd(numSamps);
m.value = flock.ugen.lastOutputValue(numSamps, out);
};
that.onInputChanged();
return that;
};
flock.ugenDefaults("flock.ugen.amplitude", {
rate: "audio",
inputs: {
source: null,
attack: 0.01,
release: 0.01,
mul: null,
add: null
},
ugenOptions: {
model: {
prevVal: 0.0,
unscaledValue: 0.0,
value: 0.0
}
}
});
}());