Tone.js/Tone/signal/Subtract.ts

73 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-07-25 03:17:47 +00:00
import { connectSeries } from "../core/Connect";
import { Gain } from "../core/context/Gain";
import { Param } from "../core/context/Param";
import { optionsFromArguments } from "../core/util/Defaults";
import { Negate } from "../signal/Negate";
import { Signal, SignalOptions } from "../signal/Signal";
/**
* Subtract the signal connected to the input is subtracted from the signal connected
* The subtrahend.
*
* @example
* var sub = new Subtract(1);
* var sig = new Tone.Signal(4).connect(sub);
* //the output of sub is 3.
* @example
* var sub = new Subtract();
* var sigA = new Tone.Signal(10);
* var sigB = new Tone.Signal(2.5);
* sigA.connect(sub);
* sigB.connect(sub.subtrahend);
* //output of sub is 7.5
2019-09-16 14:15:23 +00:00
* @category Signal
2019-07-25 03:17:47 +00:00
*/
export class Subtract extends Signal {
override = false;
2019-09-04 23:18:44 +00:00
readonly name: string = "Subtract";
2019-07-25 03:17:47 +00:00
/**
2019-09-14 20:39:18 +00:00
* the summing node
2019-07-25 03:17:47 +00:00
*/
private _sum: Gain = new Gain({ context: this.context });
2019-09-08 17:49:28 +00:00
readonly input: Gain = this._sum;
readonly output: Gain = this._sum;
2019-07-25 03:17:47 +00:00
/**
2019-09-14 20:39:18 +00:00
* Negate the input of the second input before connecting it to the summing node.
2019-07-25 03:17:47 +00:00
*/
2019-09-14 22:12:44 +00:00
private _neg: Negate = new Negate({ context: this.context });
2019-07-25 03:17:47 +00:00
/**
* The value which is subtracted from the main signal
*/
subtrahend: Param<number> = this._param;
2019-08-27 15:53:14 +00:00
/**
* @param value The value to subtract from the incoming signal. If the value
2019-09-14 20:39:18 +00:00
* is omitted, it will subtract the second signal from the first.
2019-08-27 15:53:14 +00:00
*/
2019-07-25 03:17:47 +00:00
constructor(value?: number);
2019-08-27 15:53:14 +00:00
constructor(options?: Partial<SignalOptions<number>>);
2019-07-25 03:17:47 +00:00
constructor() {
super(Object.assign(optionsFromArguments(Subtract.getDefaults(), arguments, ["value"])));
connectSeries(this._constantSource, this._neg, this._sum);
}
static getDefaults(): SignalOptions<number> {
return Object.assign(Signal.getDefaults(), {
value: 0,
});
}
dispose(): this {
super.dispose();
this._neg.dispose();
2019-08-04 14:18:45 +00:00
this._sum.dispose();
2019-07-25 03:17:47 +00:00
return this;
}
}