2024-05-03 18:31:14 +00:00
|
|
|
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.
|
2024-05-03 18:31:14 +00:00
|
|
|
*
|
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);
|
2020-09-02 20:53:38 +00:00
|
|
|
* @category Signal
|
2019-11-14 00:34:38 +00:00
|
|
|
*/
|
|
|
|
export class GreaterThan extends Signal<"number"> {
|
2024-05-03 18:31:14 +00:00
|
|
|
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
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
readonly comparator: Param<"number">;
|
2020-04-17 02:24:18 +00:00
|
|
|
|
2019-11-14 00:34:38 +00:00
|
|
|
/**
|
|
|
|
* @param value The value to compare to
|
|
|
|
*/
|
|
|
|
constructor(value?: number);
|
|
|
|
constructor(options?: Partial<GreaterThanOptions>);
|
|
|
|
constructor() {
|
2024-05-03 18:31:14 +00:00
|
|
|
const options = optionsFromArguments(
|
|
|
|
GreaterThan.getDefaults(),
|
|
|
|
arguments,
|
|
|
|
["value"]
|
|
|
|
);
|
2024-05-06 14:55:55 +00:00
|
|
|
super(options);
|
2019-11-14 00:34:38 +00:00
|
|
|
|
|
|
|
this._subtract = this.input = new Subtract({
|
|
|
|
context: this.context,
|
2024-05-03 18:31:14 +00:00
|
|
|
value: options.value,
|
|
|
|
});
|
|
|
|
this._gtz = this.output = new GreaterThanZero({
|
|
|
|
context: this.context,
|
2019-11-14 00:34:38 +00:00
|
|
|
});
|
2020-04-17 02:24:18 +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;
|
|
|
|
}
|
|
|
|
}
|