2
0
Fork 0
mirror of https://github.com/Tonejs/Tone.js synced 2025-02-14 19:48:28 +00:00
Tone.js/Tone/signal/Subtract.ts

77 lines
2 KiB
TypeScript
Raw Normal View History

2019-07-24 23:17:47 -04: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
2019-10-25 16:54:33 -04:00
* import { Signal, Subtract } from "tone";
* // subtract a scalar from a signal
* const sub = new Subtract(1);
* const sig = new Signal(4).connect(sub);
* // the output of sub is 3.
2019-07-24 23:17:47 -04:00
* @example
2019-10-25 16:54:33 -04:00
* import { Signal, Subtract } from "tone";
* // subtract two signals
* const sub = new Subtract();
* const sigA = new Signal(10);
* const sigB = new Signal(2.5);
2019-07-24 23:17:47 -04:00
* sigA.connect(sub);
* sigB.connect(sub.subtrahend);
2019-10-25 16:54:33 -04:00
* // output of sub is 7.5
2019-09-16 10:15:23 -04:00
* @category Signal
2019-07-24 23:17:47 -04:00
*/
export class Subtract extends Signal {
override = false;
2019-09-04 19:18:44 -04:00
readonly name: string = "Subtract";
2019-07-24 23:17:47 -04:00
/**
2019-09-14 16:39:18 -04:00
* the summing node
2019-07-24 23:17:47 -04:00
*/
private _sum: Gain = new Gain({ context: this.context });
2019-09-08 13:49:28 -04:00
readonly input: Gain = this._sum;
readonly output: Gain = this._sum;
2019-07-24 23:17:47 -04:00
/**
2019-09-14 16:39:18 -04:00
* Negate the input of the second input before connecting it to the summing node.
2019-07-24 23:17:47 -04:00
*/
2019-09-14 18:12:44 -04:00
private _neg: Negate = new Negate({ context: this.context });
2019-07-24 23:17:47 -04:00
/**
* The value which is subtracted from the main signal
*/
subtrahend: Param<number> = this._param;
2019-08-27 08:53:14 -07:00
/**
* @param value The value to subtract from the incoming signal. If the value
2019-09-14 16:39:18 -04:00
* is omitted, it will subtract the second signal from the first.
2019-08-27 08:53:14 -07:00
*/
2019-07-24 23:17:47 -04:00
constructor(value?: number);
2019-08-27 08:53:14 -07:00
constructor(options?: Partial<SignalOptions<number>>);
2019-07-24 23:17:47 -04: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 10:18:45 -04:00
this._sum.dispose();
2019-07-24 23:17:47 -04:00
return this;
}
}