Tone.js/Tone/signal/Multiply.ts

86 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-07-11 21:11:17 +00:00
import { Gain } from "../core/context/Gain";
2019-07-17 17:12:21 +00:00
import { Param } from "../core/context/Param";
import { optionsFromArguments } from "../core/util/Defaults";
2019-07-11 21:11:17 +00:00
import { Signal, SignalOptions } from "./Signal";
2019-12-15 07:19:25 +00:00
import { InputNode, OutputNode } from "../core/context/ToneAudioNode";
2019-07-11 21:11:17 +00:00
/**
* Multiply two incoming signals. Or, if a number is given in the constructor,
* multiplies the incoming signal by that value.
*
* @example
2019-10-25 20:54:33 +00:00
* // multiply two signals
* const mult = new Tone.Multiply();
* const sigA = new Tone.Signal(3);
* const sigB = new Tone.Signal(4);
2019-07-11 21:11:17 +00:00
* sigA.connect(mult);
* sigB.connect(mult.factor);
2019-10-25 20:54:33 +00:00
* // output of mult is 12.
2019-08-27 15:53:14 +00:00
* @example
2019-10-25 20:54:33 +00:00
* // multiply a signal and a number
* const mult = new Tone.Multiply(10);
* const sig = new Tone.Signal(2).connect(mult);
2019-10-25 20:54:33 +00:00
* // the output of mult is 20.
2019-09-16 14:15:23 +00:00
* @category Signal
2019-07-11 21:11:17 +00:00
*/
export class Multiply<TypeName extends "number" | "positive" = "number"> extends Signal<TypeName> {
2019-07-11 21:11:17 +00:00
2019-09-04 23:18:44 +00:00
readonly name: string = "Multiply";
2019-07-11 21:11:17 +00:00
/**
* Indicates if the value should be overridden on connection
*/
readonly override = false;
/**
* the input gain node
*/
2019-12-15 07:19:25 +00:00
private _mult: Gain;
2019-07-11 21:11:17 +00:00
/**
2019-09-08 17:48:31 +00:00
* The multiplicand input.
2019-07-11 21:11:17 +00:00
*/
2019-12-15 07:19:25 +00:00
input: InputNode;
2019-07-11 21:11:17 +00:00
/**
2020-05-19 01:13:22 +00:00
* The product of the input and [[factor]]
2019-07-11 21:11:17 +00:00
*/
2019-12-15 07:19:25 +00:00
output: OutputNode;
2019-07-11 21:11:17 +00:00
/**
* The multiplication factor. Can be set directly or a signal can be connected to it.
*/
factor: Param<TypeName>;
2019-07-11 21:11:17 +00:00
2019-08-27 15:53:14 +00:00
/**
* @param value Constant value to multiple
*/
2019-07-11 21:11:17 +00:00
constructor(value?: number);
constructor(options?: Partial<SignalOptions<TypeName>>);
2019-07-11 21:11:17 +00:00
constructor() {
super(Object.assign(optionsFromArguments(Multiply.getDefaults(), arguments, ["value"])));
const options = optionsFromArguments(Multiply.getDefaults(), arguments, ["value"]);
this._mult = this.input = this.output = new Gain({
2019-12-15 07:19:25 +00:00
context: this.context,
minValue: options.minValue,
maxValue: options.maxValue,
});
this.factor = this._param = this._mult.gain as unknown as Param<TypeName>;
2019-07-11 21:11:17 +00:00
this.factor.setValueAtTime(options.value, 0);
}
static getDefaults(): SignalOptions<any> {
2019-07-11 21:11:17 +00:00
return Object.assign(Signal.getDefaults(), {
value: 0,
});
}
dispose(): this {
super.dispose();
this._mult.dispose();
return this;
}
}