Tone.js/Tone/signal/Subtract.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-02-23 05:30:53 +00:00
define(["Tone/core/Tone", "Tone/signal/Add", "Tone/signal/Negate", "Tone/signal/Signal"], function(Tone){
"use strict";
/**
* @class Subtract a signal and a number or two signals.
* input 0 : minuend.
* input 1 : subtrahend
*
2015-02-23 05:30:53 +00:00
* @extends {Tone.Signal}
* @constructor
* @param {number=} value value to subtract from the incoming signal. If the value
* is omitted, it will subtract the second signal from the first
2015-02-27 18:40:35 +00:00
* @example
* var sub = new Tone.Subtract(1);
* var sig = new Tone.Signal(4).connect(sub);
* //the output of sub is 3.
*/
Tone.Subtract = function(value){
Tone.call(this, 2, 0);
/**
2015-02-23 05:30:53 +00:00
* the summing node
* @type {GainNode}
* @private
*/
2015-02-23 05:30:53 +00:00
this._sum = this.input[0] = this.output = this.context.createGain();
/**
2015-02-23 05:30:53 +00:00
* negate the input of the second input before connecting it
* to the summing node.
* @type {Tone.Negate}
* @private
*/
2015-02-23 05:30:53 +00:00
this._neg = new Tone.Negate();
2015-02-23 05:30:53 +00:00
/**
* the node where the value is set
* @private
* @type {Tone.Signal}
*/
this._value = this.input[1] = new Tone.Signal(value);
2015-02-23 05:30:53 +00:00
this._value.chain(this._neg, this._sum);
};
2015-02-23 05:30:53 +00:00
Tone.extend(Tone.Subtract, Tone.Signal);
2015-02-02 17:50:18 +00:00
/**
* clean up
2015-02-02 03:56:33 +00:00
* @returns {Tone.SignalBase} `this`
*/
Tone.Subtract.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._neg.dispose();
this._neg = null;
2015-02-23 05:30:53 +00:00
this._sum.disconnect();
this._sum = null;
this._value.dispose();
this._value = null;
2015-02-02 03:56:33 +00:00
return this;
};
return Tone.Subtract;
});