Tone.js/src/components/Envelope.js

120 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-03-11 23:27:46 +00:00
///////////////////////////////////////////////////////////////////////////////
//
// Envelope
//
// ADR envelope generator attaches to an AudioParam
///////////////////////////////////////////////////////////////////////////////
2014-03-16 17:33:56 +00:00
Tone.Envelope = function(attack, decay, sustain, release, audioParam, minOutput, maxOutput){
2014-03-11 23:27:46 +00:00
//extend Unit
2014-03-16 17:33:56 +00:00
Tone.call(this);
2014-03-11 23:27:46 +00:00
2014-03-15 05:02:33 +00:00
//pass audio through
2014-03-11 23:27:46 +00:00
this.input.connect(this.output);
2014-03-15 05:02:33 +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);
2014-03-24 17:41:08 +00:00
this.sustain = this.defaultArg(.5);
// this.setSustain(this.defaultArg(sustain, .1));
this.min = this.defaultArg(minOutput, 0);
this.max = this.defaultArg(maxOutput, 1);
2014-03-15 05:02:33 +00:00
//set the initial value
2014-03-11 23:27:46 +00:00
this.param.value = this.min;
}
2014-03-16 17:33:56 +00:00
Tone.extend(Tone.Envelope, Tone);
2014-03-11 23:27:46 +00:00
//attack->decay->sustain
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.triggerAttack = function(time){
2014-03-11 23:27:46 +00:00
var startVal = this.min;
if (!time){
startVal = this.param.value;
}
time = this.defaultArg(time, this.now());
2014-03-11 23:27:46 +00:00
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);
}
2014-03-24 17:41:08 +00:00
//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-15 05:02:33 +00:00
//triggers the release of the envelope
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.triggerRelease = function(time){
2014-03-11 23:27:46 +00:00
var startVal = this.param.value;
if (time){
startVal = (this.max - this.min) * this.sustain + this.min;
}
time = this.defaultArg(time, this.now());
2014-03-11 23:27:46 +00:00
this.param.cancelScheduledValues(time);
this.param.setValueAtTime(startVal, time);
this.param.linearRampToValueAtTime(this.min, time + this.release);
}
2014-03-24 17:41:08 +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-11 23:27:46 +00:00
///////////////////////////////////////////////////////////////////////////////
// SET VALUES
///////////////////////////////////////////////////////////////////////////////
//@param {number} attack (seconds)
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.setAttack = function(attack){
2014-03-11 23:27:46 +00:00
this.attack = attack;
}
//@param {number} decay (seconds)
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.setDecay = function(decay){
2014-03-11 23:27:46 +00:00
this.decay = decay;
}
//@param {number} release (seconds)
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.setRelease = function(release){
2014-03-11 23:27:46 +00:00
this.release = release;
}
2014-03-15 05:02:33 +00:00
//@param {number} sustain as a percentage (0-1);
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.setSustain = function(sustain){
2014-03-24 17:41:08 +00:00
// this.sustain = this.gainToPowScale(sustain);
this.sustain = sustain;
2014-03-11 23:27:46 +00:00
}
//@param {number} min
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.setMin = function(min){
2014-03-11 23:27:46 +00:00
this.min = min;
}
//@param {number} max
2014-03-16 17:33:56 +00:00
Tone.Envelope.prototype.setMax = function(max){
2014-03-11 23:27:46 +00:00
this.max = max;
}