signal comparison

This commit is contained in:
Yotam Mann 2014-07-03 22:59:16 -04:00
parent ce4d75db50
commit 465d83f308
3 changed files with 134 additions and 0 deletions

2
.gitignore vendored
View file

@ -8,3 +8,5 @@ examples/scratch.html
*.sublime-project
build/node_modules/*
TODO.txt

View file

@ -0,0 +1,90 @@
define(["Tone/core/Tone", "Tone/signal/Threshold"], function(Tone){
/**
* Output 1 if the signal is greater than the value, otherwise outputs 0
*
* @constructor
* @extends {Tone}
* @param {number=} [value=0] the value to compare to the incoming signal
*/
Tone.GreaterThan = function(value){
/**
* @type {WaveShaperNode}
* @private
*/
this._gt = this.context.createWaveShaper();
/**
* @type {WaveShaperNode}
* @private
*/
this._thresh = new Tone.Threshold(0.001);
/**
* subtract the value from the incoming signal
*
* @type {Tone.Add}
* @private
*/
this._adder = new Tone.Add(this.defaultArg(-value, 0));
/**
* alias for the adder
* @type {Tone.Add}
*/
this.input = this._adder;
/**
* alias for the thresh
* @type {Tone.Threshold}
*/
this.output = this._thresh;
//connect
this.chain(this._adder, this._gt, this._thresh);
//setup waveshaper
this._setGreaterThanZero();
};
Tone.extend(Tone.GreaterThan);
/**
* @private
*/
Tone.GreaterThan.prototype._setGreaterThanZero = function(){
var curveLength = 1024;
var curve = new Float32Array(curveLength);
for (var i = 0; i < curveLength; i++){
var normalized = (i / (curveLength)) * 2 - 1;
if (normalized > 0){
curve[i] = 1;
} else {
curve[i] = 0;
}
}
this._gt.curve = curve;
};
/**
* set the value to compare to
*
* @param {number} value
*/
Tone.GreaterThan.prototype.setValue = function(value){
this._adder.setValue(-value);
};
/**
* dispose method
*/
Tone.GreaterThan.prototype.dispose = function(){
this._gt.disconnect();
this._adder.disconnect();
this._thresh.dispose();
this._gt = null;
this._adder = null;
this._thresh = null;
};
return Tone.GreaterThan;
});

42
Tone/signal/LessThan.js Normal file
View file

@ -0,0 +1,42 @@
define(["Tone/core/Tone", "Tone/signal/GreaterThan"], function(Tone){
/**
* Output 1 if the signal is less than the value, otherwise outputs 0
*
* @constructor
* @extends {Tone}
* @param {number} value the value to compare the incoming signal to
*/
Tone.LessThan = function(value){
/**
*
* @type {Tone.GreaterThan}
* @private
*/
this._gt = new Tone.GreaterThan(-value);
/**
* @type {Tone.GreaterThan}
*/
this.input = this.output = this._gt;
};
Tone.extend(Tone.LessThan);
/**
* @param {number} value
*/
Tone.LessThan.prototype.setValue = function(value){
this._gt.setValue(-value);
};
/**
* dispose method
*/
Tone.LessThan.prototype.dispose = function(){
this._gt.dispose();
this._gt = null;
};
return Tone.GreaterThan;
});