Tone.js/Tone/signal/Normalize.js

108 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-11-30 02:37:21 +00:00
define(["Tone/core/Tone", "Tone/signal/Add", "Tone/signal/Multiply"], function(Tone){
"use strict";
/**
2015-06-19 04:52:04 +00:00
* @class Normalize takes an input min and max and maps it linearly to NormalRange [0,1]
2014-11-30 02:37:21 +00:00
*
* @extends {Tone.SignalBase}
2014-11-30 02:37:21 +00:00
* @constructor
* @param {number} inputMin the min input value
* @param {number} inputMax the max input value
2015-02-27 18:40:35 +00:00
* @example
2015-06-14 05:17:09 +00:00
* var norm = new Tone.Normalize(2, 4);
* var sig = new Tone.Signal(3).connect(norm);
* //output of norm is 0.5.
2014-11-30 02:37:21 +00:00
*/
Tone.Normalize = function(inputMin, inputMax){
/**
* the min input value
* @type {number}
* @private
*/
this._inputMin = this.defaultArg(inputMin, 0);
/**
* the max input value
* @type {number}
* @private
*/
this._inputMax = this.defaultArg(inputMax, 1);
/**
* subtract the min from the input
* @type {Tone.Add}
* @private
*/
this._sub = this.input = new Tone.Add(0);
/**
* divide by the difference between the input and output
* @type {Tone.Multiply}
* @private
*/
this._div = this.output = new Tone.Multiply(1);
this._sub.connect(this._div);
this._setRange();
};
Tone.extend(Tone.Normalize, Tone.SignalBase);
2014-11-30 02:37:21 +00:00
/**
* The minimum value the input signal will reach.
* @memberOf Tone.Normalize#
* @type {number}
* @name min
2014-11-30 02:37:21 +00:00
*/
Object.defineProperty(Tone.Normalize.prototype, "min", {
get : function(){
return this._inputMin;
},
set : function(min){
this._inputMin = min;
this._setRange();
}
});
2014-11-30 02:37:21 +00:00
/**
* The maximum value the input signal will reach.
* @memberOf Tone.Normalize#
* @type {number}
* @name max
2014-11-30 02:37:21 +00:00
*/
Object.defineProperty(Tone.Normalize.prototype, "max", {
get : function(){
return this._inputMax;
},
set : function(max){
this._inputMax = max;
this._setRange();
}
});
2014-11-30 02:37:21 +00:00
/**
* set the values
* @private
*/
Tone.Normalize.prototype._setRange = function() {
this._sub.value = -this._inputMin;
this._div.value = 1 / (this._inputMax - this._inputMin);
2014-11-30 02:37:21 +00:00
};
/**
* clean up
* @returns {Tone.Normalize} this
2014-11-30 02:37:21 +00:00
*/
Tone.Normalize.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._sub.dispose();
this._sub = null;
this._div.dispose();
this._div = null;
2015-02-02 03:56:33 +00:00
return this;
2014-11-30 02:37:21 +00:00
};
return Tone.Normalize;
});