Tone.js/Tone/signal/EqualZero.js

75 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-08-27 19:22:21 +00:00
define(["Tone/core/Tone", "Tone/signal/Threshold", "Tone/signal/Signal"],
function(Tone){
2014-07-02 19:37:08 +00:00
"use strict";
2014-07-02 19:37:08 +00:00
/**
2014-07-04 17:47:56 +00:00
* @class Output 1 if the signal is equal to 0, otherwise outputs 0
2014-07-02 19:37:08 +00:00
*
* @constructor
* @extends {Tone}
*/
2014-07-02 23:18:00 +00:00
Tone.EqualZero = function(){
2014-07-02 19:37:08 +00:00
/**
* @type {WaveShaperNode}
* @private
*/
this._equals = this.context.createWaveShaper();
/**
* @type {WaveShaperNode}
* @private
*/
2014-07-02 21:08:03 +00:00
this._thresh = new Tone.Threshold(1);
2014-07-02 19:37:08 +00:00
/**
* @type {WaveShaperNode}
*/
this.input = this._equals;
this._equals.connect(this._thresh);
this.output = this._thresh;
this._setEquals();
};
2014-07-02 23:18:00 +00:00
Tone.extend(Tone.EqualZero);
2014-07-02 19:37:08 +00:00
/**
* @private
*/
2014-07-02 23:18:00 +00:00
Tone.EqualZero.prototype._setEquals = function(){
2014-08-27 19:22:21 +00:00
var curveLength = 1023;
2014-07-02 19:37:08 +00:00
var curve = new Float32Array(curveLength);
for (var i = 0; i < curveLength; i++){
2014-08-27 19:22:21 +00:00
var normalized = (i / (curveLength - 1)) * 2 - 1;
2014-07-03 03:38:20 +00:00
if (normalized === 0){
curve[i] = 1;
2014-07-02 19:37:08 +00:00
} else {
2014-07-03 03:38:20 +00:00
curve[i] = 0;
2014-07-02 19:37:08 +00:00
}
}
this._equals.curve = curve;
};
/**
* borrows the method from {@link Tone.Signal}
*
* @function
*/
Tone.EqualZero.prototype.connect = Tone.Signal.prototype.connect;
2014-07-02 19:47:05 +00:00
/**
* dispose method
*/
2014-07-02 23:18:00 +00:00
Tone.EqualZero.prototype.dispose = function(){
2014-07-02 19:47:05 +00:00
this._equals.disconnect();
this._thresh.dispose();
this._equals = null;
this._thresh = null;
};
2014-07-02 23:18:00 +00:00
return Tone.EqualZero;
2014-07-02 19:37:08 +00:00
});