Scale and ScaleExp scale from gain range only

This commit is contained in:
Yotam Mann 2014-11-29 21:18:04 -05:00
parent 15c33fdecd
commit d3bb514dde
3 changed files with 156 additions and 233 deletions

View file

@ -4,126 +4,74 @@ define(["Tone/core/Tone", "Tone/signal/Add", "Tone/signal/Multiply", "Tone/signa
/**
* @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.
*
* @description If only two arguments are provided, the inputMin and inputMax are set to -1 and 1
* Scales a normal gain input range [0,1] to between
* outputMin and outputMax
*
* @constructor
* @extends {Tone}
* @param {number} inputMin
* @param {number} inputMax
* @param {number=} outputMin
* @param {number=} outputMax
* @param {number} [outputMin=0]
* @param {number} [outputMax=1]
*/
Tone.Scale = function(inputMin, inputMax, outputMin, outputMax){
Tone.call(this);
//if there are only two args
if (arguments.length == 2){
outputMin = inputMin;
outputMax = inputMax;
inputMin = -1;
inputMax = 1;
}
Tone.Scale = function(outputMin, outputMax){
/**
* @private
* @type {number}
*/
this._inputMin = inputMin;
/**
* @private
* @type {number}
*/
this._inputMax = inputMax;
/**
* @private
* @type {number}
*/
this._outputMin = outputMin;
this._outputMin = this.defaultArg(outputMin, 0);
/**
* @private
* @type {number}
*/
this._outputMax = outputMax;
this._outputMax = this.defaultArg(outputMax, 1);
/**
* @private
* @type {Tone.Add}
*/
this._plusInput = this.input = new Tone.Add(0);
/**
* @private
* @type {Tone.Multiply}
* @private
*/
this._scale = new Tone.Multiply(1);
this._scale = this.input = new Tone.Multiply(1);
/**
* @private
* @type {Tone.Add}
* @private
*/
this._plusOutput = this.output = new Tone.Add(0);
this._add = this.output = new Tone.Add(0);
//connections
this.chain(this._plusInput, this._scale, this._plusOutput);
//set the scaling values
this._setScalingParameters();
this._scale.connect(this._add);
this._setRange();
};
Tone.extend(Tone.Scale);
/**
* set the scaling parameters
*
* set the minimum output value
* @param {number} min the minimum output value
*/
Tone.Scale.prototype.setMin = function(min){
this._outputMin = min;
this._setRange();
};
/**
* set the minimum output value
* @param {number} min the minimum output value
*/
Tone.Scale.prototype.setMax = function(max){
this._outputMax = max;
this._setRange();
};
/**
* set the values
* @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();
Tone.Scale.prototype._setRange = function() {
this._add.setValue(this._outputMin);
this._scale.setValue(this._outputMax - this._outputMin);
};
/**
@ -138,11 +86,9 @@ define(["Tone/core/Tone", "Tone/signal/Add", "Tone/signal/Multiply", "Tone/signa
*/
Tone.Scale.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._plusInput.dispose();
this._plusOutput.dispose();
this._add.dispose();
this._add = null;
this._scale.dispose();
this._plusInput = null;
this._plusOutput = null;
this._scale = null;
};

View file

@ -1,179 +1,71 @@
define(["Tone/core/Tone", "Tone/signal/Add", "Tone/signal/Multiply", "Tone/signal/Signal", "Tone/signal/Pow"],
define(["Tone/core/Tone", "Tone/signal/Scale", "Tone/signal/Pow"],
function(Tone){
/**
* @class performs an exponential scaling on an input signal.
* Scales from the input range of inputMin to inputMax
* Scales a normal gain range [0,1] exponentially
* to the output range of outputMin to outputMax.
*
* @description If only two arguments are provided, the inputMin and inputMax are set to -1 and 1
*
* @constructor
* @extends {Tone}
* @param {number} inputMin
* @param {number} inputMax
* @param {number} outputMin
* @param {number=} outputMax
* @param {number=} [exponent=2] the exponent which scales the incoming signal
* @extends {Tone.Scale}
* @param {number} [outputMin=0]
* @param {number} [outputMax=1]
* @param {number} [exponent=2] the exponent which scales the incoming signal
*/
Tone.ScaleExp = function(inputMin, inputMax, outputMin, outputMax, exponent){
Tone.ScaleExp = function(outputMin, outputMax, exponent){
//if there are only two args
if (arguments.length === 2){
outputMin = inputMin;
outputMax = inputMax;
exponent = 2;
inputMin = -1;
inputMax = 1;
} else if (arguments.length === 3){
exponent = outputMin;
outputMin = inputMin;
outputMax = inputMax;
inputMin = -1;
inputMax = 1;
}
/**
/**
* scale the input to the output range
* @type {Tone.Scale}
* @private
* @type {number}
*/
this._inputMin = inputMin;
/**
* @private
* @type {number}
*/
this._inputMax = inputMax;
/**
* @private
* @type {number}
*/
this._outputMin = outputMin;
/**
* @private
* @type {number}
*/
this._outputMax = outputMax;
/**
* @private
* @type {Tone.Add}
*/
this._plusInput = this.input = new Tone.Add(0);
/**
* @private
* @type {Tone.Multiply}
*/
this._normalize = new Tone.Multiply(1);
/**
* @private
* @type {Tone.Multiply}
*/
this._scale = new Tone.Multiply(1);
/**
* @private
* @type {Tone.Add}
*/
this._plusOutput = this.output = new Tone.Add(0);
this._scale = this.output = new Tone.Scale(outputMin, outputMax);
/**
* @private
* @type {Tone.Pow}
* @private
*/
this._expScaler = new Tone.Pow(this.defaultArg(exponent, 2));
this._exp = this.input = new Tone.Pow(this.defaultArg(exponent, 2));
//connections
this.chain(this._plusInput, this._normalize, this._expScaler, this._scale, this._plusOutput);
//set the scaling values
this._setScalingParameters();
this._exp.connect(this._scale);
};
Tone.extend(Tone.ScaleExp);
/**
* set the scaling parameters
*
* @private
*/
Tone.ScaleExp.prototype._setScalingParameters = function(){
//components
this._plusInput.setValue(-this._inputMin);
this._scale.setValue((this._outputMax - this._outputMin));
this._normalize.setValue(1 / (this._inputMax - this._inputMin));
this._plusOutput.setValue(this._outputMin);
};
Tone.extend(Tone.ScaleExp, Tone.Scale);
/**
* set the exponential scaling curve
* @param {number} exp the exponent to raise the incoming signal to
*/
Tone.ScaleExp.prototype.setExponent = function(exp){
this._expScaler.setExponent(exp);
this._exp.setExponent(exp);
};
/**
* set the input min value
* @param {number} val
* set the minimum output value
* @param {number} min the minimum output value
*/
Tone.ScaleExp.prototype.setInputMin = function(val){
this._inputMin = val;
this._setScalingParameters();
Tone.ScaleExp.prototype.setMin = function(min){
this._scale.setMin(min);
};
/**
* set the input max value
* @param {number} val
* set the minimum output value
* @param {number} min the minimum output value
*/
Tone.ScaleExp.prototype.setInputMax = function(val){
this._inputMax = val;
this._setScalingParameters();
Tone.ScaleExp.prototype.setMax = function(max){
this._scale.setMax(max);
};
/**
* set the output min value
* @param {number} val
*/
Tone.ScaleExp.prototype.setOutputMin = function(val){
this._outputMin = val;
this._setScalingParameters();
};
/**
* set the output max value
* @param {number} val
*/
Tone.ScaleExp.prototype.setOutputMax = function(val){
this._outputMax = val;
this._setScalingParameters();
};
/**
* borrows connect from {@link Tone.Signal}
*
* @function
*/
Tone.ScaleExp.prototype.connect = Tone.Signal.prototype.connect;
/**
* clean up
*/
Tone.ScaleExp.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._plusInput.dispose();
this._plusOutput.dispose();
this._normalize.dispose();
this._scale.dispose();
this._expScaler.disconnect();
this._plusInput = null;
this._plusOutput = null;
this._scale = null;
this._normalize = null;
this._expScaler = null;
this._exp.dispose();
this._exp = null;
};

View file

@ -4,9 +4,9 @@ define(["tests/Core", "chai", "Tone/signal/Signal", "Tone/signal/Add", "Tone/sig
"Tone/signal/Scale", "Tone/source/Oscillator", "Tone/core/Master", "Tone/signal/Abs", "Tone/signal/Negate",
"Tone/signal/Max", "Tone/signal/Min", "Tone/signal/Clip", "Tone/signal/ScaleExp",
"Tone/signal/Modulo", "tests/Common", "Tone/signal/Subtract", "Tone/signal/Inverse", "Tone/signal/Divide",
"Tone/signal/Pow"],
"Tone/signal/Pow", "Tone/signal/Normalize", "Tone/signal/AudioToGain"],
function(core, chai, Signal, Add, Multiply, Scale, Oscillator, Master, Abs, Negate, Max,
Min, Clip, ScaleExp, Modulo, Test, Subtract, Inverse, Divide, Pow){
Min, Clip, ScaleExp, Modulo, Test, Subtract, Inverse, Divide, Pow, Normalize, AudioToGain){
var expect = chai.expect;
@ -220,7 +220,7 @@ function(core, chai, Signal, Add, Multiply, Scale, Oscillator, Master, Abs, Nega
it("handles input and output connections", function(){
Test.onlineContext();
var scale = new Scale(0, 1, 0, 100);
var scale = new Scale(0, 100);
Test.acceptsInputAndOutput(scale);
scale.dispose();
});
@ -229,12 +229,12 @@ function(core, chai, Signal, Add, Multiply, Scale, Oscillator, Master, Abs, Nega
//make an oscillator to drive the signal
var osc, scale;
Test.offlineTest(0.2, function(dest){
osc = new Oscillator(1000);
scale = new Scale(-1, 1, 10, 20);
osc = new Signal(0.5);
scale = new Scale(10, 20);
osc.connect(scale);
scale.connect(dest);
}, function(sample){
expect(sample).to.be.within(10, 20);
expect(sample).to.equal(15);
}, function(){
osc.dispose();
scale.dispose();
@ -255,7 +255,7 @@ function(core, chai, Signal, Add, Multiply, Scale, Oscillator, Master, Abs, Nega
it("handles input and output connections", function(){
Test.onlineContext();
var scale = new ScaleExp(0, 1, 0, 100);
var scale = new ScaleExp(0, 100);
Test.acceptsInputAndOutput(scale);
scale.dispose();
});
@ -264,7 +264,7 @@ function(core, chai, Signal, Add, Multiply, Scale, Oscillator, Master, Abs, Nega
var signal, scale;
Test.offlineTest(0.2, function(dest){
signal = new Signal(0.5);
scale = new ScaleExp(0, 1, 0, 1, 2);
scale = new ScaleExp(0, 1, 2);
signal.connect(scale);
scale.connect(dest);
}, function(sample){
@ -851,4 +851,89 @@ function(core, chai, Signal, Add, Multiply, Scale, Oscillator, Master, Abs, Nega
});
});
describe("Tone.Normalize", function(){
this.timeout(maxTimeout);
it("can be created and disposed", function(){
var s = new Normalize();
s.dispose();
Test.wasDisposed(s);
});
it("handles input and output connections", function(){
Test.onlineContext();
var norm = new Normalize();
Test.acceptsInputAndOutput(norm);
norm.dispose();
});
it("normalizes an oscillator to 0,1", function(done){
//make an oscillator to drive the signal
var osc, norm;
Test.offlineTest(0.2, function(dest){
osc = new Oscillator(1000);
norm = new Normalize(-1, 1);
osc.connect(norm);
norm.connect(dest);
}, function(sample){
expect(sample).to.be.within(0, 1);
}, function(){
osc.dispose();
norm.dispose();
done();
});
});
it("normalizes an input", function(done){
//make an oscillator to drive the signal
var sig, norm;
Test.offlineTest(0.2, function(dest){
sig = new Signal(1000);
norm = new Normalize(0, 1000);
sig.connect(norm);
norm.connect(dest);
}, function(sample){
expect(sample).to.equal(1);
}, function(){
sig.dispose();
norm.dispose();
done();
});
});
});
describe("Tone.AudioToGain", function(){
this.timeout(maxTimeout);
it("can be created and disposed", function(){
var a2g = new AudioToGain();
a2g.dispose();
Test.wasDisposed(a2g);
});
it("handles input and output connections", function(){
Test.onlineContext();
var a2g = new AudioToGain();
Test.acceptsInputAndOutput(a2g);
a2g.dispose();
});
it("normalizes an oscillator to 0,1", function(done){
//make an oscillator to drive the signal
var osc, a2g;
Test.offlineTest(0.2, function(dest){
osc = new Oscillator(1000);
a2g = new AudioToGain();
osc.connect(a2g);
a2g.connect(dest);
}, function(sample){
expect(sample).to.be.within(0, 1);
}, function(){
osc.dispose();
a2g.dispose();
done();
});
});
});
});