Tone.js/Tone/component/ScaledEnvelope.js

112 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-11-30 02:36:32 +00:00
define(["Tone/core/Tone", "Tone/component/Envelope", "Tone/signal/Scale"],
function(Tone){
"use strict";
/**
* @class An envelope which can be scaled to any range.
* Useful for applying an envelope to a filter
*
* @extends {Tone.Envelope}
* @constructor
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
*/
Tone.ScaledEnvelope = function(){
//get all of the defaults
var options = this.optionsObject(arguments, ["attack", "decay", "sustain", "release"], Tone.Envelope.defaults);
Tone.Envelope.call(this, options);
options = this.defaultArg(options, Tone.ScaledEnvelope.defaults);
2014-12-06 21:48:57 +00:00
/**
* scale the incoming signal by an exponent
* @type {Tone.Pow}
* @private
*/
this._exp = this.output = new Tone.Pow(options.exponent);
/**
2014-11-30 02:36:32 +00:00
* scale the signal to the desired range
* @type {Tone.Multiply}
* @private
*/
2014-11-30 02:36:32 +00:00
this._scale = this.output = new Tone.Scale(options.min, options.max);
2014-12-06 21:48:57 +00:00
this._sig.chain(this._exp, this._scale);
};
Tone.extend(Tone.ScaledEnvelope, Tone.Envelope);
/**
* the default parameters
* @static
*/
Tone.ScaledEnvelope.defaults = {
"min" : 0,
"max" : 1,
2014-12-06 21:48:57 +00:00
"exponent" : 1
};
/**
* The envelope's min output value.
* @memberOf Tone.ScaledEnvelope#
* @type {number}
* @name min
2015-02-02 17:49:13 +00:00
*/
Object.defineProperty(Tone.ScaledEnvelope.prototype, "min", {
get : function(){
return this._scale.min;
},
set : function(min){
this._scale.min = min;
}
});
2014-12-06 21:48:57 +00:00
/**
* The envelope's max output value.
* @memberOf Tone.ScaledEnvelope#
* @type {number}
* @name max
2014-12-06 21:48:57 +00:00
*/
Object.defineProperty(Tone.ScaledEnvelope.prototype, "max", {
get : function(){
return this._scale.max;
},
set : function(max){
this._scale.max = max;
}
});
2015-02-02 17:49:13 +00:00
/**
* the envelope's exponent
* @memberOf Tone.ScaledEnvelope#
* @type {number}
* @name exponent
2015-02-02 17:49:13 +00:00
*/
Object.defineProperty(Tone.ScaledEnvelope.prototype, "exponent", {
get : function(){
return this._exp.value;
},
set : function(exp){
this._exp.value = exp;
}
});
/**
* clean up
2015-02-02 17:49:13 +00:00
* @returns {Tone.ScaledEnvelope} `this`
*/
Tone.ScaledEnvelope.prototype.dispose = function(){
Tone.Envelope.prototype.dispose.call(this);
2014-11-30 02:36:32 +00:00
this._scale.dispose();
this._scale = null;
2014-12-06 21:48:57 +00:00
this._exp.dispose();
this._exp = null;
2015-02-02 17:49:13 +00:00
return this;
};
return Tone.ScaledEnvelope;
});