Tone.js/Tone/signal/Divide.js

96 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-10-31 01:34:53 +00:00
define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/signal/Inverse", "Tone/signal/Multiply"],
function(Tone){
"use strict";
/**
* @class Divide by a value or signal.
* input 0: numerator. input 1: divisor.
2014-10-31 01:34:53 +00:00
*
* @extends {Tone.SignalBase}
2014-10-31 01:34:53 +00:00
* @constructor
* @param {number=} divisor if no value is provided, Tone.Divide will divide the first
* and second inputs.
2014-12-02 06:42:08 +00:00
* @param {number} [precision=3] the precision of the calculation
2014-10-31 01:34:53 +00:00
*/
Tone.Divide = function(divisor, precision){
Tone.call(this, 2, 0);
2014-10-31 01:34:53 +00:00
/**
* the denominator value
* @type {Tone.Signal}
* @private
*/
this._denominator = null;
2014-10-31 01:34:53 +00:00
/**
* the inverse
* @type {Tone}
*/
this._inverse = new Tone.Inverse(precision);
/**
* multiply input 0 by the inverse
* @type {Tone.Multiply}
*/
this._mult = new Tone.Multiply();
if (isFinite(divisor)){
this._denominator = new Tone.Signal(divisor);
this._denominator.connect(this._inverse);
2014-10-31 01:34:53 +00:00
}
this.input[1] = this._inverse;
this._inverse.connect(this._mult, 0, 1);
this.input[0] = this.output = this._mult.input[0];
};
Tone.extend(Tone.Divide, Tone.SignalBase);
2014-10-31 01:34:53 +00:00
/**
* The value being divided from the incoming signal. Note, that
* if Divide was constructed without a divisor, it expects
* that the signals to numberator will be connected to input 0 and
* the denominator to input 1 and therefore will throw an error when
* trying to set/get the value.
*
* @memberOf Tone.Divide#
* @type {number}
* @name value
2014-10-31 01:34:53 +00:00
*/
Object.defineProperty(Tone.Divide.prototype, "value", {
get : function(){
if (this._denominator !== null){
return this._denominator.value;
} else {
throw new Error("cannot switch from signal to number");
}
},
set : function(value){
if (this._denominator !== null){
this._denominator.value = value;
} else {
throw new Error("cannot switch from signal to number");
}
2014-10-31 01:34:53 +00:00
}
});
2014-10-31 01:34:53 +00:00
/**
* clean up
2015-02-02 03:56:33 +00:00
* @returns {Tone.Divide} `this`
2014-10-31 01:34:53 +00:00
*/
Tone.Divide.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
if (this._denominator){
this._denominator.dispose();
this._denominator = null;
2014-10-31 01:34:53 +00:00
}
this._inverse.dispose();
this._inverse = null;
this._mult.dispose();
this._mult = null;
2015-02-02 03:56:33 +00:00
return this;
2014-10-31 01:34:53 +00:00
};
return Tone.Divide;
});