2014-11-04 05:44:16 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/signal/Pow"], function(Tone){
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-09-04 04:41:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-06-15 23:35:00 +00:00
|
|
|
/**
|
2014-11-04 05:44:16 +00:00
|
|
|
* @class ADSR envelope generator attaches to an AudioParam or Signal.
|
2014-06-16 23:59:58 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
2014-12-02 06:42:08 +00:00
|
|
|
* @param {Tone.Time|Object} [attack=0.01] the attack time in seconds
|
|
|
|
* @param {Tone.Time} [decay=0.1] the decay time in seconds
|
|
|
|
* @param {number} [sustain=0.5] a percentage (0-1) of the full amplitude
|
|
|
|
* @param {Tone.Time} [release=1] the release time in seconds
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-08-24 23:28:42 +00:00
|
|
|
Tone.Envelope = function(){
|
2014-08-23 20:38:06 +00:00
|
|
|
|
|
|
|
//get all of the defaults
|
2014-08-25 14:23:37 +00:00
|
|
|
var options = this.optionsObject(arguments, ["attack", "decay", "sustain", "release"], Tone.Envelope.defaults);
|
2014-08-24 16:12:40 +00:00
|
|
|
|
2014-08-23 20:38:06 +00:00
|
|
|
/**
|
2015-02-06 22:49:04 +00:00
|
|
|
* The attack time
|
2014-12-02 15:39:19 +00:00
|
|
|
* @type {Tone.Time}
|
2014-08-23 20:38:06 +00:00
|
|
|
*/
|
2014-10-02 17:23:04 +00:00
|
|
|
this.attack = options.attack;
|
2014-08-23 20:38:06 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-06 22:49:04 +00:00
|
|
|
* The decay time
|
2014-12-02 15:39:19 +00:00
|
|
|
* @type {Tone.Time}
|
2014-08-23 20:38:06 +00:00
|
|
|
*/
|
2014-10-02 17:23:04 +00:00
|
|
|
this.decay = options.decay;
|
2014-08-23 20:38:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the sustain is a value between 0-1
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-10-02 17:23:04 +00:00
|
|
|
this.sustain = options.sustain;
|
2014-08-23 20:38:06 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-06 22:49:04 +00:00
|
|
|
* The release time
|
2014-12-02 15:39:19 +00:00
|
|
|
* @type {Tone.Time}
|
2014-08-23 20:38:06 +00:00
|
|
|
*/
|
2014-10-02 17:23:04 +00:00
|
|
|
this.release = options.release;
|
2014-08-23 20:38:06 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-02 01:54:40 +00:00
|
|
|
* the signal
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
* @private
|
2014-08-23 20:38:06 +00:00
|
|
|
*/
|
2014-12-06 21:48:57 +00:00
|
|
|
this._sig = this.output = new Tone.Signal(0);
|
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
|
2015-02-06 22:49:04 +00:00
|
|
|
* @const
|
2014-08-23 20:38:06 +00:00
|
|
|
*/
|
2014-08-25 14:23:37 +00:00
|
|
|
Tone.Envelope.defaults = {
|
2014-08-23 20:38:06 +00:00
|
|
|
"attack" : 0.01,
|
|
|
|
"decay" : 0.1,
|
|
|
|
"sustain" : 0.5,
|
|
|
|
"release" : 1,
|
|
|
|
};
|
|
|
|
|
2014-11-02 01:54:40 +00:00
|
|
|
/**
|
|
|
|
* the envelope time multipler
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.Envelope.prototype._timeMult = 0.25;
|
|
|
|
|
2014-06-15 23:35:00 +00:00
|
|
|
/**
|
2015-02-02 17:49:13 +00:00
|
|
|
* attack->decay->sustain linear ramp
|
|
|
|
* @param {Tone.Time} [time=now]
|
|
|
|
* @param {number} [velocity=1] the velocity of the envelope scales the vales.
|
2014-09-04 02:36:56 +00:00
|
|
|
* number between 0-1
|
2015-02-02 17:49:13 +00:00
|
|
|
* @returns {Tone.Envelope} `this`
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-09-04 02:36:56 +00:00
|
|
|
Tone.Envelope.prototype.triggerAttack = function(time, velocity){
|
|
|
|
velocity = this.defaultArg(velocity, 1);
|
2014-10-02 17:23:04 +00:00
|
|
|
var attack = this.toSeconds(this.attack);
|
|
|
|
var decay = this.toSeconds(this.decay);
|
2014-11-02 01:54:40 +00:00
|
|
|
var scaledMax = velocity;
|
2014-12-08 16:02:22 +00:00
|
|
|
var sustainVal = this.sustain * scaledMax;
|
2014-09-03 21:31:29 +00:00
|
|
|
time = this.toSeconds(time);
|
2014-11-02 01:54:40 +00:00
|
|
|
this._sig.cancelScheduledValues(time);
|
|
|
|
this._sig.setTargetAtTime(scaledMax, time, attack * this._timeMult);
|
|
|
|
this._sig.setTargetAtTime(sustainVal, time + attack, decay * this._timeMult);
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-15 21:37:55 +00:00
|
|
|
};
|
2014-06-15 23:35:00 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-02 17:49:13 +00:00
|
|
|
* triggers the release of the envelope with a linear ramp
|
|
|
|
* @param {Tone.Time} [time=now]
|
|
|
|
* @returns {Tone.Envelope} `this`
|
2014-06-15 23:35:00 +00:00
|
|
|
*/
|
2014-04-06 00:47:59 +00:00
|
|
|
Tone.Envelope.prototype.triggerRelease = function(time){
|
2014-09-03 21:31:29 +00:00
|
|
|
time = this.toSeconds(time);
|
2014-11-02 01:54:40 +00:00
|
|
|
this._sig.cancelScheduledValues(time);
|
2014-10-02 17:23:04 +00:00
|
|
|
var release = this.toSeconds(this.release);
|
2014-11-02 01:54:40 +00:00
|
|
|
this._sig.setTargetAtTime(0, time, release * this._timeMult);
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-15 21:37:55 +00:00
|
|
|
};
|
2014-04-11 23:17:01 +00:00
|
|
|
|
2014-09-20 19:18:36 +00:00
|
|
|
/**
|
|
|
|
* trigger the attack and release after a sustain time
|
|
|
|
* @param {Tone.Time} duration the duration of the note
|
2014-12-02 06:42:08 +00:00
|
|
|
* @param {Tone.Time} [time=now] the time of the attack
|
|
|
|
* @param {number} [velocity=1] the velocity of the note
|
2015-02-02 17:49:13 +00:00
|
|
|
* @returns {Tone.Envelope} `this`
|
2014-09-20 19:18:36 +00:00
|
|
|
*/
|
|
|
|
Tone.Envelope.prototype.triggerAttackRelease = function(duration, time, velocity) {
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
this.triggerAttack(time, velocity);
|
|
|
|
this.triggerRelease(time + this.toSeconds(duration));
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-09-20 19:18:36 +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
|
2015-02-02 17:49:13 +00:00
|
|
|
* @returns {Tone.Envelope} `this`
|
2014-06-20 05:23:35 +00:00
|
|
|
*/
|
|
|
|
Tone.Envelope.prototype.dispose = function(){
|
2014-08-23 20:38:06 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
2014-11-02 01:54:40 +00:00
|
|
|
this._sig.dispose();
|
|
|
|
this._sig = null;
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-20 05:23:35 +00:00
|
|
|
};
|
|
|
|
|
2014-04-06 00:47:59 +00:00
|
|
|
return Tone.Envelope;
|
|
|
|
});
|