Tone.js/Tone/signal/Threshold.js

98 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-08-24 19:47:59 +00:00
define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
2014-07-02 19:37:17 +00:00
"use strict";
2014-07-02 19:37:17 +00:00
/**
2014-07-04 17:47:56 +00:00
* @class Threshold an incoming signal. the signal is assumed to be in the normal range (-1 to 1)
* Creates a threshold value such that signal above the value will equal 1,
* and below will equal 0.
2014-10-03 17:07:53 +00:00
*
* @deprecated use Tone.GreaterThan or Tone.GreaterThanZero instead. Threshold will be removed in r4.
2014-07-02 19:37:17 +00:00
*
* @constructor
2014-07-02 21:33:00 +00:00
* @param {number=} [thresh=0] threshold value above which the output will equal 1
2014-07-02 19:37:17 +00:00
* and below which the output will equal 0
* @extends {Tone}
*/
Tone.Threshold = function(thresh){
2014-10-03 17:07:53 +00:00
console.warn("Tone.Threshold has been deprecated. Use Tone.GreaterThan or Tone.GreaterThanZero");
2014-07-02 21:07:40 +00:00
2014-07-02 19:37:17 +00:00
/**
* @type {WaveShaperNode}
* @private
*/
this._thresh = this.context.createWaveShaper();
2014-07-02 21:07:40 +00:00
/**
* make doubly sure that the input is thresholded by
* passing it through two waveshapers
*
* @type {WaveShaperNode}
* @private
*/
this._doubleThresh = this.context.createWaveShaper();
2014-07-02 19:37:17 +00:00
/**
* @type {WaveShaperNode}
*/
2014-07-02 21:07:40 +00:00
this.input = this._thresh;
this.output = this._doubleThresh;
this._thresh.connect(this._doubleThresh);
2014-07-02 19:37:17 +00:00
2014-07-02 21:07:40 +00:00
this._setThresh(this._thresh, this.defaultArg(thresh, 0));
2014-09-14 19:34:37 +00:00
this._setThresh(this._doubleThresh, 0.5);
2014-07-02 19:37:17 +00:00
};
Tone.extend(Tone.Threshold);
/**
* @param {number} thresh
* @private
*/
2014-07-02 21:07:40 +00:00
Tone.Threshold.prototype._setThresh = function(component, thresh){
2014-08-27 19:22:21 +00:00
var curveLength = 1023;
2014-07-02 19:37:17 +00:00
var curve = new Float32Array(curveLength);
for (var i = 0; i < curveLength; i++){
2014-07-02 21:07:40 +00:00
var normalized = (i / (curveLength - 1)) * 2 - 1;
2014-07-02 19:37:17 +00:00
var val;
if (normalized < thresh){
val = 0;
} else {
val = 1;
}
curve[i] = val;
}
2014-07-02 21:07:40 +00:00
component.curve = curve;
};
/**
* sets the threshold value
*
* @param {number} thresh number must be between -1 and 1
*/
Tone.Threshold.prototype.setThreshold = function(thresh){
this._setThresh(this._thresh, thresh);
2014-07-02 19:37:17 +00:00
};
/**
* borrows the method from {@link Tone.Signal}
*
* @function
*/
Tone.Threshold.prototype.connect = Tone.Signal.prototype.connect;
2014-07-02 19:47:05 +00:00
/**
* dispose method
*/
Tone.Threshold.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
2014-07-02 19:47:05 +00:00
this._thresh.disconnect();
2014-07-02 21:07:40 +00:00
this._doubleThresh.disconnect();
2014-07-02 19:47:05 +00:00
this._thresh = null;
2014-07-02 21:07:40 +00:00
this._doubleThresh = null;
2014-07-02 19:47:05 +00:00
};
2014-07-02 19:37:17 +00:00
return Tone.Threshold;
});