Tone.js/Tone/component/Envelope.js

109 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-03-11 23:27:46 +00:00
///////////////////////////////////////////////////////////////////////////////
//
// Envelope
//
// ADR envelope generator attaches to an AudioParam
///////////////////////////////////////////////////////////////////////////////
define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
2014-03-11 23:27:46 +00:00
2014-03-15 05:02:33 +00:00
Tone.Envelope = function(attack, decay, sustain, release, minOutput, maxOutput){
2014-04-06 00:47:59 +00:00
//extend Unit
Tone.call(this);
2014-03-26 19:38:21 +00:00
2014-04-06 00:47:59 +00:00
//set the parameters
2014-06-15 21:37:55 +00:00
this.attack = this.defaultArg(attack, 0.01);
this.decay = this.defaultArg(decay, 0.1);
2014-04-06 00:47:59 +00:00
this.release = this.defaultArg(release, 1);
2014-06-15 21:37:55 +00:00
this.sustain = this.defaultArg(sustain, 0.5);
2014-03-11 23:27:46 +00:00
2014-04-06 00:47:59 +00:00
this.min = this.defaultArg(minOutput, 0);
this.max = this.defaultArg(maxOutput, 1);
//the control signal
this.control = new Tone.Signal(this.min);
//connections
this.chain(this.control, this.output);
2014-06-15 21:37:55 +00:00
};
2014-03-11 23:27:46 +00:00
2014-06-15 21:37:55 +00:00
Tone.extend(Tone.Envelope);
2014-04-06 00:47:59 +00:00
//attack->decay->sustain
2014-06-15 21:37:55 +00:00
//@param {Tone.Time} time
2014-04-06 00:47:59 +00:00
Tone.Envelope.prototype.triggerAttack = function(time){
var startVal = this.min;
if (!time){
startVal = this.control.getValue();
2014-04-06 00:47:59 +00:00
}
time = this.defaultArg(time, this.now());
time = this.toSeconds(time);
this.control.cancelScheduledValues(time);
this.control.setValueAtTime(startVal, time);
var attackTime = this.toSeconds(this.attack);
var decayTime = this.toSeconds(this.decay);
this.control.linearRampToValueAtTime(this.max, time + attackTime);
2014-04-06 00:47:59 +00:00
var sustainVal = (this.max - this.min) * this.sustain + this.min;
this.control.linearRampToValueAtTime(sustainVal, time + attackTime + decayTime);
2014-06-15 21:37:55 +00:00
};
2014-04-06 00:47:59 +00:00
//attack->decay->sustain
Tone.Envelope.prototype.triggerAttackExp = function(time){
var startVal = this.min;
if (!time){
startVal = this.control.getValue();
2014-04-06 00:47:59 +00:00
}
time = this.toSeconds(time);
this.control.cancelScheduledValues(time);
this.control.setValueAtTime(startVal, time);
var attackTime = this.toSeconds(this.attack);
var decayTime = this.toSeconds(this.decay);
this.control.linearRampToValueAtTime(this.max, time + attackTime);
2014-04-06 00:47:59 +00:00
var sustainVal = (this.max - this.min) * this.sustain + this.min;
this.control.exponentialRampToValueAtTime(sustainVal, time + attackTime + decayTime);
2014-06-15 21:37:55 +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.triggerRelease = function(time){
var startVal = this.control.getValue();
2014-04-06 00:47:59 +00:00
if (time){
startVal = (this.max - this.min) * this.sustain + this.min;
}
time = this.defaultArg(time, this.now());
time = this.toSeconds(time);
this.control.cancelScheduledValues(time);
this.control.setValueAtTime(startVal, time);
this.control.linearRampToValueAtTime(this.min, time + this.toSeconds(this.release));
2014-06-15 21:37:55 +00:00
};
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.control.getValue();
2014-04-06 00:47:59 +00:00
if (time){
startVal = (this.max - this.min) * this.sustain + this.min;
}
time = this.defaultArg(time, this.now());
time = this.toSeconds(time);
this.control.cancelScheduledValues(time);
this.control.setValueAtTime(startVal, time);
this.control.exponentialRampToValueAtTime(this.min, time + this.toSeconds(this.release));
2014-06-15 21:37:55 +00:00
};
//@private
//pointer to the parent's connect method
Tone.Envelope.prototype._connect = Tone.prototype.connect;
//triggers the release of the envelope
Tone.Envelope.prototype.connect = function(param){
if (param instanceof AudioParam){
//set the initial value
2014-04-16 04:23:58 +00:00
param.value = 0;
}
this._connect(param);
2014-06-15 21:37:55 +00:00
};
2014-04-06 00:47:59 +00:00
return Tone.Envelope;
});