2014-08-24 17:19:49 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/signal/Add", "Tone/signal/Multiply", "Tone/signal/Signal"], function(Tone){
|
2014-09-04 04:41:40 +00:00
|
|
|
|
|
|
|
"use strict";
|
2014-06-17 17:01:06 +00:00
|
|
|
|
2014-06-16 23:57:55 +00:00
|
|
|
/**
|
2014-07-04 17:47:56 +00:00
|
|
|
* @class performs a linear scaling on an input signal.
|
|
|
|
* Scales from the input range of inputMin to inputMax
|
|
|
|
* to the output range of outputMin to outputMax.
|
2014-06-16 23:57:55 +00:00
|
|
|
*
|
2014-07-04 17:47:56 +00:00
|
|
|
* @description If only two arguments are provided, the inputMin and inputMax are set to -1 and 1
|
2014-06-16 23:57:55 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
|
|
|
* @param {number} inputMin
|
|
|
|
* @param {number} inputMax
|
|
|
|
* @param {number=} outputMin
|
|
|
|
* @param {number=} outputMax
|
|
|
|
*/
|
2014-04-16 23:56:18 +00:00
|
|
|
Tone.Scale = function(inputMin, inputMax, outputMin, outputMax){
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.call(this);
|
|
|
|
|
2014-06-16 23:57:55 +00:00
|
|
|
//if there are only two args
|
2014-04-16 23:56:18 +00:00
|
|
|
if (arguments.length == 2){
|
|
|
|
outputMin = inputMin;
|
|
|
|
outputMax = inputMax;
|
|
|
|
inputMin = -1;
|
|
|
|
inputMax = 1;
|
|
|
|
}
|
2014-06-16 23:57:55 +00:00
|
|
|
|
2014-11-04 06:21:42 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-06-16 23:57:55 +00:00
|
|
|
this._inputMin = inputMin;
|
2014-11-04 06:21:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-06-16 23:57:55 +00:00
|
|
|
this._inputMax = inputMax;
|
2014-11-04 06:21:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-06-16 23:57:55 +00:00
|
|
|
this._outputMin = outputMin;
|
2014-11-04 06:21:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-06-16 23:57:55 +00:00
|
|
|
this._outputMax = outputMax;
|
|
|
|
|
2014-11-04 06:21:42 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {Tone.Add}
|
|
|
|
*/
|
|
|
|
this._plusInput = this.input = new Tone.Add(0);
|
2014-06-16 23:57:55 +00:00
|
|
|
|
2014-11-04 06:21:42 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {Tone.Multiply}
|
|
|
|
*/
|
2014-06-16 23:57:55 +00:00
|
|
|
this._scale = new Tone.Multiply(1);
|
2014-11-04 06:21:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {Tone.Add}
|
|
|
|
*/
|
|
|
|
this._plusOutput = this.output = new Tone.Add(0);
|
2014-04-05 22:05:42 +00:00
|
|
|
|
|
|
|
//connections
|
2014-11-04 06:21:42 +00:00
|
|
|
this.chain(this._plusInput, this._scale, this._plusOutput);
|
2014-06-16 23:57:55 +00:00
|
|
|
|
|
|
|
//set the scaling values
|
|
|
|
this._setScalingParameters();
|
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
|
|
|
Tone.extend(Tone.Scale);
|
|
|
|
|
2014-06-16 23:57:55 +00:00
|
|
|
/**
|
|
|
|
* set the scaling parameters
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.Scale.prototype._setScalingParameters = function(){
|
|
|
|
//components
|
|
|
|
this._plusInput.setValue(-this._inputMin);
|
|
|
|
this._scale.setValue((this._outputMax - this._outputMin)/(this._inputMax - this._inputMin));
|
|
|
|
this._plusOutput.setValue(this._outputMin);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the input min value
|
|
|
|
* @param {number} val
|
|
|
|
*/
|
|
|
|
Tone.Scale.prototype.setInputMin = function(val){
|
|
|
|
this._inputMin = val;
|
|
|
|
this._setScalingParameters();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the input max value
|
|
|
|
* @param {number} val
|
|
|
|
*/
|
|
|
|
Tone.Scale.prototype.setInputMax = function(val){
|
|
|
|
this._inputMax = val;
|
|
|
|
this._setScalingParameters();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the output min value
|
|
|
|
* @param {number} val
|
|
|
|
*/
|
|
|
|
Tone.Scale.prototype.setOutputMin = function(val){
|
|
|
|
this._outputMin = val;
|
|
|
|
this._setScalingParameters();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the output max value
|
|
|
|
* @param {number} val
|
|
|
|
*/
|
|
|
|
Tone.Scale.prototype.setOutputMax = function(val){
|
|
|
|
this._outputMax = val;
|
|
|
|
this._setScalingParameters();
|
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-08-24 17:19:49 +00:00
|
|
|
/**
|
|
|
|
* borrows connect from {@link Tone.Signal}
|
|
|
|
*
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
Tone.Scale.prototype.connect = Tone.Signal.prototype.connect;
|
|
|
|
|
2014-06-20 04:38:14 +00:00
|
|
|
/**
|
|
|
|
* clean up
|
|
|
|
*/
|
|
|
|
Tone.Scale.prototype.dispose = function(){
|
2014-09-06 22:55:11 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
2014-06-20 04:38:14 +00:00
|
|
|
this._plusInput.dispose();
|
|
|
|
this._plusOutput.dispose();
|
|
|
|
this._scale.dispose();
|
|
|
|
this._plusInput = null;
|
|
|
|
this._plusOutput = null;
|
|
|
|
this._scale = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
return Tone.Scale;
|
|
|
|
});
|