Tone.js/Tone/signal/GreaterThan.ts

94 lines
2.3 KiB
TypeScript
Raw Normal View History

import { ToneAudioNode } from "../core/context/ToneAudioNode.js";
import { optionsFromArguments } from "../core/util/Defaults.js";
import { Subtract } from "./Subtract.js";
import { Signal, SignalOptions } from "./Signal.js";
import { GreaterThanZero } from "./GreaterThanZero.js";
import { readOnly } from "../core/util/Interface.js";
import { Param } from "../core/context/Param.js";
2019-11-14 00:34:38 +00:00
export type GreaterThanOptions = SignalOptions<"number">;
/**
* Output 1 if the signal is greater than the value, otherwise outputs 0.
* can compare two signals or a signal and a number.
*
2019-11-14 00:34:38 +00:00
* @example
2020-07-26 20:55:06 +00:00
* return Tone.Offline(() => {
* const gt = new Tone.GreaterThan(2).toDestination();
* const sig = new Tone.Signal(4).connect(gt);
* }, 0.1, 1);
* @category Signal
2019-11-14 00:34:38 +00:00
*/
export class GreaterThan extends Signal<"number"> {
readonly name: string = "GreaterThan";
2019-11-14 00:34:38 +00:00
readonly override: boolean = false;
readonly input: ToneAudioNode;
readonly output: ToneAudioNode;
/**
* compare that amount to zero after subtracting
*/
private _gtz: GreaterThanZero;
/**
* Subtract the value from the input node
*/
private _subtract: Subtract;
/**
2020-04-17 03:08:21 +00:00
* The signal to compare to the incoming signal against.
* @example
2020-07-26 20:55:06 +00:00
* return Tone.Offline(() => {
* // change the comparison value
* const gt = new Tone.GreaterThan(1.5).toDestination();
* const signal = new Tone.Signal(1).connect(gt);
* gt.comparator.setValueAtTime(0.5, 0.1);
* }, 0.5, 1);
2019-11-14 00:34:38 +00:00
*/
readonly comparator: Param<"number">;
2019-11-14 00:34:38 +00:00
/**
* @param value The value to compare to
*/
constructor(value?: number);
constructor(options?: Partial<GreaterThanOptions>);
constructor() {
const options = optionsFromArguments(
GreaterThan.getDefaults(),
arguments,
["value"]
);
super(options);
2019-11-14 00:34:38 +00:00
this._subtract = this.input = new Subtract({
context: this.context,
value: options.value,
});
this._gtz = this.output = new GreaterThanZero({
context: this.context,
2019-11-14 00:34:38 +00:00
});
2019-11-14 00:34:38 +00:00
this.comparator = this._param = this._subtract.subtrahend;
readOnly(this, "comparator");
// connect
this._subtract.connect(this._gtz);
}
static getDefaults(): GreaterThanOptions {
return Object.assign(Signal.getDefaults(), {
value: 0,
});
}
dispose(): this {
super.dispose();
this._gtz.dispose();
this._subtract.dispose();
this.comparator.dispose();
return this;
}
}