Tone.js/Tone/component/Envelope.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-03-11 23:27:46 +00:00
///////////////////////////////////////////////////////////////////////////////
//
// Envelope
//
// ADR envelope generator attaches to an AudioParam
///////////////////////////////////////////////////////////////////////////////
2014-04-06 20:51:30 +00:00
define(["Tone/core/Tone"], function(Tone){
2014-03-11 23:27:46 +00:00
2014-03-15 05:02:33 +00:00
2014-04-06 00:47:59 +00:00
Tone.Envelope = function(attack, decay, sustain, release, audioParam, minOutput, maxOutput){
//extend Unit
Tone.call(this);
2014-03-26 19:38:21 +00:00
2014-04-06 00:47:59 +00:00
//pass audio through
this.input.connect(this.output);
2014-03-11 23:27:46 +00:00
2014-04-06 00:47:59 +00:00
//set the parameters
this.param = this.defaultArg(audioParam, this.input.gain);
this.attack = this.defaultArg(attack, .01);
this.decay = this.defaultArg(decay, .1);
this.release = this.defaultArg(release, 1);
this.sustain = this.defaultArg(.5);
2014-03-11 23:27:46 +00:00
2014-04-06 00:47:59 +00:00
// this.setSustain(this.defaultArg(sustain, .1));
this.min = this.defaultArg(minOutput, 0);
this.max = this.defaultArg(maxOutput, 1);
//set the initial value
this.param.value = this.min;
2014-03-11 23:27:46 +00:00
}
2014-04-06 00:47:59 +00:00
Tone.extend(Tone.Envelope, Tone);
//attack->decay->sustain
Tone.Envelope.prototype.triggerAttack = function(time){
var startVal = this.min;
if (!time){
startVal = this.param.value;
}
time = this.defaultArg(time, this.now());
this.param.cancelScheduledValues(time);
this.param.setValueAtTime(startVal, time);
this.param.linearRampToValueAtTime(this.max, time + this.attack);
var sustainVal = (this.max - this.min) * this.sustain + this.min;
this.param.linearRampToValueAtTime(sustainVal, time + this.decay + this.attack);
}
//attack->decay->sustain
Tone.Envelope.prototype.triggerAttackExp = function(time){
var startVal = this.min;
if (!time){
startVal = this.param.value;
}
time = this.defaultArg(time, this.now());
this.param.cancelScheduledValues(time);
this.param.setValueAtTime(startVal, time);
this.param.exponentialRampToValueAtTime(this.max, time + this.attack);
var sustainVal = (this.max - this.min) * this.sustain + this.min;
this.param.exponentialRampToValueAtTime(sustainVal, time + this.decay + this.attack);
2014-03-24 17:41:08 +00:00
}
2014-04-06 00:47:59 +00:00
//triggers the release of the envelope
Tone.Envelope.prototype.triggerRelease = function(time){
var startVal = this.param.value;
if (time){
startVal = (this.max - this.min) * this.sustain + this.min;
}
time = this.defaultArg(time, this.now());
this.param.cancelScheduledValues(time);
this.param.setValueAtTime(startVal, time);
this.param.linearRampToValueAtTime(this.min, time + this.release);
2014-03-11 23:27:46 +00:00
}
2014-03-24 17:41:08 +00:00
2014-04-06 00:47:59 +00:00
//triggers the release of the envelope
Tone.Envelope.prototype.triggerReleaseExp = function(time){
var startVal = this.param.value;
if (time){
startVal = (this.max - this.min) * this.sustain + this.min;
}
time = this.defaultArg(time, this.now());
this.param.cancelScheduledValues(time);
this.param.setValueAtTime(startVal, time);
this.param.exponentialRampToValueAtTime(this.min, time + this.release);
2014-03-24 17:41:08 +00:00
}
2014-04-06 00:47:59 +00:00
return Tone.Envelope;
});