Tone.js/Tone/signal/Multiply.ts

77 lines
1.8 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";
/**
* Multiply two incoming signals. Or, if a number is given in the constructor,
* multiplies the incoming signal by that value.
*
* @example
* const mult = new Multiply();
* const sigA = new Tone.Signal(3);
* const sigB = new Tone.Signal(4);
* sigA.connect(mult);
* sigB.connect(mult.factor);
* //output of mult is 12.
2019-08-27 15:53:14 +00:00
* @example
2019-07-11 21:11:17 +00:00
* const mult = new Multiply(10);
* const sig = new Tone.Signal(2).connect(mult);
* //the output of mult is 20.
2019-09-16 14:15:23 +00:00
* @category Signal
2019-07-11 21:11:17 +00:00
*/
2019-07-15 19:37:25 +00:00
export class Multiply extends Signal<number> {
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-09-14 22:12:44 +00:00
private _mult: Gain = new Gain({ context: this.context });
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
*/
input = this._mult;
/**
* The product of the input and {@link factor}
*/
output = this._mult;
/**
* The multiplication factor. Can be set directly or a signal can be connected to it.
*/
2019-07-15 19:37:25 +00:00
factor: Param<number>;
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);
2019-08-27 15:53:14 +00:00
constructor(options?: Partial<SignalOptions<number>>);
2019-07-11 21:11:17 +00:00
constructor() {
super(Object.assign(optionsFromArguments(Multiply.getDefaults(), arguments, ["value"])));
const options = optionsFromArguments(Multiply.getDefaults(), arguments, ["value"]);
2019-07-15 19:37:25 +00:00
this.factor = this._param = this._mult.gain as unknown as Param<number>;
2019-07-11 21:11:17 +00:00
this.factor.setValueAtTime(options.value, 0);
}
2019-07-15 19:37:25 +00:00
static getDefaults(): SignalOptions<number> {
2019-07-11 21:11:17 +00:00
return Object.assign(Signal.getDefaults(), {
value: 0,
});
}
dispose(): this {
super.dispose();
this._mult.dispose();
return this;
}
}