mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 00:27:58 +00:00
convert to ts
This commit is contained in:
parent
8a9c8f2edf
commit
8e551ff755
2 changed files with 124 additions and 0 deletions
47
Tone/signal/Multiply.test.ts
Normal file
47
Tone/signal/Multiply.test.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import { expect } from "chai";
|
||||
import { BasicTests } from "test/helper/Basic";
|
||||
import { connectFrom, connectTo } from "test/helper/Connect";
|
||||
// import Test from "test/helper/Test";
|
||||
import { ConstantOutput } from "test/helper/ConstantOutput";
|
||||
import { Multiply } from "./Multiply";
|
||||
// import Multiply from "Tone/signal/Multiply";
|
||||
import { Signal } from "./Signal";
|
||||
// import Oscillator from "Tone/source/Oscillator";
|
||||
|
||||
describe("Multiply", () => {
|
||||
|
||||
BasicTests(Multiply);
|
||||
|
||||
describe("Multiplication", () => {
|
||||
|
||||
it("handles input and output connections", () => {
|
||||
const mult = new Multiply();
|
||||
connectFrom().connect(mult, 0);
|
||||
connectFrom().connect(mult.factor);
|
||||
mult.connect(connectTo());
|
||||
mult.dispose();
|
||||
});
|
||||
|
||||
it("correctly multiplys a signal and a scalar", () => {
|
||||
return ConstantOutput(() => {
|
||||
const signal = new Signal(2);
|
||||
const mult = new Multiply(10);
|
||||
expect(mult.value).to.equal(10);
|
||||
signal.connect(mult);
|
||||
mult.toMaster();
|
||||
}, 20);
|
||||
});
|
||||
|
||||
it("can multiply two signals", () => {
|
||||
return ConstantOutput(() => {
|
||||
const sigA = new Signal(3);
|
||||
const sigB = new Signal(5);
|
||||
const mult = new Multiply();
|
||||
sigA.connect(mult);
|
||||
sigB.connect(mult.factor);
|
||||
mult.toMaster();
|
||||
}, 15);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
77
Tone/signal/Multiply.ts
Normal file
77
Tone/signal/Multiply.ts
Normal file
|
@ -0,0 +1,77 @@
|
|||
import { Param } from "Tone/core/context/Param";
|
||||
import { optionsFromArguments } from "Tone/core/util/Defaults";
|
||||
import { Gain } from "../core/context/Gain";
|
||||
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.
|
||||
*
|
||||
* @param {number=} value Constant value to multiple
|
||||
* @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.
|
||||
* @example
|
||||
* const mult = new Multiply(10);
|
||||
* const sig = new Tone.Signal(2).connect(mult);
|
||||
* //the output of mult is 20.
|
||||
*/
|
||||
export class Multiply extends Signal<"number"> {
|
||||
|
||||
readonly name = "Multiply";
|
||||
|
||||
/**
|
||||
* Indicates if the value should be overridden on connection
|
||||
*/
|
||||
readonly override = false;
|
||||
|
||||
/**
|
||||
* the input gain node
|
||||
*/
|
||||
private _mult: Gain = new Gain({ context : this.context });
|
||||
|
||||
/**
|
||||
* The multiplcant input.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
factor: Param<"number">;
|
||||
|
||||
constructor(options?: Partial<SignalOptions>);
|
||||
// tslint:disable-next-line: unified-signatures
|
||||
constructor(value?: number);
|
||||
constructor() {
|
||||
super(Object.assign(optionsFromArguments(Multiply.getDefaults(), arguments, ["value"])));
|
||||
const options = optionsFromArguments(Multiply.getDefaults(), arguments, ["value"]);
|
||||
|
||||
this.factor = this._param = this._mult.gain as unknown as Param<"number">;
|
||||
this.factor.setValueAtTime(options.value, 0);
|
||||
}
|
||||
|
||||
static getDefaults(): SignalOptions {
|
||||
return Object.assign(Signal.getDefaults(), {
|
||||
value: 0,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* clean up
|
||||
*/
|
||||
dispose(): this {
|
||||
super.dispose();
|
||||
this._mult.dispose();
|
||||
return this;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue