Tone.js/Tone/signal/Subtract.ts

75 lines
2 KiB
TypeScript
Raw Normal View History

import { connectSeries } from "../core/context/ToneAudioNode";
2019-07-25 03:17:47 +00:00
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 20:54:33 +00:00
* // subtract a scalar from a signal
* const sub = new Tone.Subtract(1);
* const sig = new Tone.Signal(4).connect(sub);
2019-10-25 20:54:33 +00:00
* // the output of sub is 3.
2019-07-25 03:17:47 +00:00
* @example
2019-10-25 20:54:33 +00:00
* // subtract two signals
* const sub = new Tone.Subtract();
* const sigA = new Tone.Signal(10);
* const sigB = new Tone.Signal(2.5);
2019-07-25 03:17:47 +00:00
* sigA.connect(sub);
* sigB.connect(sub.subtrahend);
2019-10-25 20:54:33 +00:00
* // 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-07-25 03:17:47 +00:00
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);
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"> {
2019-07-25 03:17:47 +00:00
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;
}
}