2014-04-11 23:17:01 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-06-15 23:35:00 +00:00
|
|
|
/**
|
2014-06-16 23:59:58 +00:00
|
|
|
* Envelope
|
2014-06-20 05:23:35 +00:00
|
|
|
* ADR envelope generator attaches to an AudioParam or AudioNode
|
2014-06-16 23:59:58 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
2014-08-23 20:38:06 +00:00
|
|
|
* @param {Tone.Time|Object=} attack
|
2014-06-16 23:59:58 +00:00
|
|
|
* @param {Tone.Time=} decay
|
|
|
|
* @param {number=} sustain a percentage (0-1) of the full amplitude
|
|
|
|
* @param {Tone.Time=} release
|
|
|
|
* @param {number=} minOutput the lowest point of the envelope
|
|
|
|
* @param {number=} maxOutput the highest point of the envelope
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-04-11 23:17:01 +00:00
|
|
|
Tone.Envelope = function(attack, decay, sustain, release, minOutput, maxOutput){
|
2014-08-23 20:38:06 +00:00
|
|
|
|
|
|
|
//get all of the defaults
|
|
|
|
var params = this._defaults;
|
|
|
|
if (arguments.length === 1 && typeof attack === "object"){
|
|
|
|
params = this.defaultArg(attack, this._defaults);
|
|
|
|
attack = params.attack;
|
|
|
|
}
|
2014-08-20 20:50:07 +00:00
|
|
|
|
2014-08-23 20:38:06 +00:00
|
|
|
/**
|
|
|
|
* the output
|
|
|
|
* @type {GainNode}
|
|
|
|
*/
|
2014-08-20 20:50:07 +00:00
|
|
|
this.output = this.context.createGain();
|
2014-03-26 19:38:21 +00:00
|
|
|
|
2014-08-23 20:38:06 +00:00
|
|
|
/**
|
|
|
|
* the attack time in seconds
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.attack = this.toSeconds(this.defaultArg(attack, params.attack));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the decay time in seconds
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.decay = this.toSeconds(this.defaultArg(decay, params.decay));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the sustain is a value between 0-1
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.sustain = this.toSeconds(this.defaultArg(sustain, params.sustain));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the release time in seconds
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.release = this.toSeconds(this.defaultArg(release, params.release));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the minimum output of the envelope
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.min = this.defaultArg(minOutput, params.min);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the maximum output of the envelope
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.max = this.defaultArg(maxOutput, params.max);
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-08-23 20:38:06 +00:00
|
|
|
/**
|
|
|
|
* the control signal
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._control = new Tone.Signal(this.min);
|
2014-04-11 23:17:01 +00:00
|
|
|
|
|
|
|
//connections
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.connect(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
|
|
|
|
2014-08-23 20:38:06 +00:00
|
|
|
/**
|
|
|
|
* the default parameters
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.Envelope.prototype._defaults = {
|
|
|
|
"attack" : 0.01,
|
|
|
|
"decay" : 0.1,
|
|
|
|
"sustain" : 0.5,
|
|
|
|
"release" : 1,
|
|
|
|
"min" : 0,
|
|
|
|
"max" : 1
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set all of the parameters in bulk
|
|
|
|
* @param {Object} param the name of member as the key
|
|
|
|
* and the value as the value
|
|
|
|
*/
|
|
|
|
Tone.Envelope.prototype.set = function(params){
|
|
|
|
if (!this.isUndef(params.attack)) this.attack = params.attack;
|
|
|
|
if (!this.isUndef(params.decay)) this.decay = params.decay;
|
|
|
|
if (!this.isUndef(params.sustain)) this.sustain = params.sustain;
|
|
|
|
if (!this.isUndef(params.release)) this.release = params.release;
|
|
|
|
if (!this.isUndef(params.min)) this.min = params.min;
|
|
|
|
if (!this.isUndef(params.max)) this.max = params.max;
|
|
|
|
};
|
|
|
|
|
2014-06-15 23:35:00 +00:00
|
|
|
/**
|
|
|
|
* attack->decay->sustain linear ramp
|
2014-06-21 17:06:12 +00:00
|
|
|
* @param {Tone.Time=} time
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-04-06 00:47:59 +00:00
|
|
|
Tone.Envelope.prototype.triggerAttack = function(time){
|
2014-06-21 17:06:12 +00:00
|
|
|
var sustainVal = (this.max - this.min) * this.sustain + this.min;
|
2014-04-06 00:47:59 +00:00
|
|
|
if (!time){
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.linearRampToValueNow(this.max, this.attack);
|
|
|
|
this._control.linearRampToValueAtTime(sustainVal, this.now() + this.attack + this.decay);
|
2014-06-21 17:06:12 +00:00
|
|
|
} else {
|
|
|
|
var startVal = this.min;
|
|
|
|
time = this.toSeconds(time);
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.cancelScheduledValues(time);
|
|
|
|
this._control.setValueAtTime(startVal, time);
|
|
|
|
this._control.linearRampToValueAtTime(this.max, time + this.attack);
|
|
|
|
this._control.linearRampToValueAtTime(sustainVal, time + this.attack + this.decay);
|
2014-04-06 00:47:59 +00:00
|
|
|
}
|
2014-06-15 21:37:55 +00:00
|
|
|
};
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-06-15 23:35:00 +00:00
|
|
|
/**
|
2014-06-21 17:06:12 +00:00
|
|
|
* attack->decay->sustain exponential attack and linear decay
|
|
|
|
* @param {Tone.Time=} time
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-06-20 05:23:35 +00:00
|
|
|
Tone.Envelope.prototype.triggerExponentialAttack = function(time){
|
2014-06-21 17:06:12 +00:00
|
|
|
var sustainVal = (this.max - this.min) * this.sustain + this.min;
|
2014-04-06 00:47:59 +00:00
|
|
|
if (!time){
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.exponentialRampToValueNow(this.max, this.attack);
|
|
|
|
this._control.linearRampToValueAtTime(sustainVal, this.now() + this.attack + this.decay);
|
2014-06-21 17:06:12 +00:00
|
|
|
} else {
|
|
|
|
var startVal = this.min;
|
|
|
|
time = this.toSeconds(time);
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.cancelScheduledValues(time);
|
|
|
|
this._control.setValueAtTime(startVal, time);
|
|
|
|
this._control.exponentialRampToValueAtTime(this.max, time + this.attack);
|
|
|
|
this._control.linearRampToValueAtTime(sustainVal, time + this.attack + this.decay);
|
2014-04-06 00:47:59 +00:00
|
|
|
}
|
2014-06-15 21:37:55 +00:00
|
|
|
};
|
2014-06-15 23:35:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* triggers the release of the envelope with a linear ramp
|
2014-06-21 17:06:12 +00:00
|
|
|
* @param {Tone.Time=} time
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-04-06 00:47:59 +00:00
|
|
|
Tone.Envelope.prototype.triggerRelease = function(time){
|
|
|
|
if (time){
|
2014-06-21 17:06:12 +00:00
|
|
|
//if there's a time, start at the sustain value
|
2014-07-22 16:48:22 +00:00
|
|
|
var startVal = (this.max - this.min) * this.sustain + this.min;
|
2014-06-21 17:06:12 +00:00
|
|
|
time = this.toSeconds(time);
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.cancelScheduledValues(time);
|
|
|
|
this._control.setValueAtTime(startVal, time);
|
|
|
|
this._control.linearRampToValueAtTime(this.min, time + this.toSeconds(this.release));
|
2014-06-21 17:06:12 +00:00
|
|
|
} else {
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.linearRampToValueNow(this.min, this.toSeconds(this.release));
|
2014-04-06 00:47:59 +00:00
|
|
|
}
|
2014-06-15 21:37:55 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-06-15 23:35:00 +00:00
|
|
|
/**
|
|
|
|
* triggers the release of the envelope with an exponential ramp
|
2014-06-16 23:59:58 +00:00
|
|
|
*
|
2014-06-21 17:06:12 +00:00
|
|
|
* @param {Tone.Time=} time
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-06-20 05:23:35 +00:00
|
|
|
Tone.Envelope.prototype.triggerExponentialRelease = function(time){
|
2014-04-06 00:47:59 +00:00
|
|
|
if (time){
|
2014-06-21 17:06:12 +00:00
|
|
|
//if there's a time, start at the sustain value
|
2014-07-22 16:48:22 +00:00
|
|
|
var startVal = (this.max - this.min) * this.sustain + this.min;
|
2014-06-21 17:06:12 +00:00
|
|
|
time = this.toSeconds(time);
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.cancelScheduledValues(time);
|
|
|
|
this._control.setValueAtTime(startVal, time);
|
|
|
|
this._control.exponentialRampToValueAtTime(this.min, time + this.toSeconds(this.release));
|
2014-06-21 17:06:12 +00:00
|
|
|
} else {
|
2014-08-23 20:38:06 +00:00
|
|
|
this._control.exponentialRampToValueNow(this.min, this.toSeconds(this.release));
|
2014-04-06 00:47:59 +00:00
|
|
|
}
|
2014-06-15 21:37:55 +00:00
|
|
|
};
|
2014-04-11 23:17:01 +00:00
|
|
|
|
2014-06-15 23:35:00 +00:00
|
|
|
/**
|
2014-08-23 20:38:06 +00:00
|
|
|
* borrows the connect method from {@link Tone.Signal}
|
|
|
|
*
|
|
|
|
* @function
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-08-23 20:38:06 +00:00
|
|
|
Tone.Envelope.prototype.connect = Tone.Signal.prototype.connect;
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-06-20 05:23:35 +00:00
|
|
|
/**
|
|
|
|
* disconnect and dispose
|
|
|
|
*/
|
|
|
|
Tone.Envelope.prototype.dispose = function(){
|
2014-08-23 20:38:06 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
|
|
|
this._control.dispose();
|
|
|
|
this._control = null;
|
2014-06-20 05:23:35 +00:00
|
|
|
};
|
|
|
|
|
2014-04-06 00:47:59 +00:00
|
|
|
return Tone.Envelope;
|
|
|
|
});
|