mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 08:38:00 +00:00
Merge branch 'pr/522' into typescript
This commit is contained in:
commit
1122628ab2
2 changed files with 117 additions and 0 deletions
51
Tone/signal/Add.test.ts
Normal file
51
Tone/signal/Add.test.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { BasicTests } from "test/helper/Basic";
|
||||
import { connectFrom, connectTo } from "test/helper/Connect";
|
||||
import { ConstantOutput } from "test/helper/ConstantOutput";
|
||||
import { Add } from "./Add";
|
||||
import { Signal } from "./Signal";
|
||||
|
||||
describe("Add", () => {
|
||||
|
||||
BasicTests(Add);
|
||||
|
||||
context("Addition", () => {
|
||||
|
||||
it("handles input and output connections", () => {
|
||||
const add = new Add();
|
||||
connectFrom().connect(add);
|
||||
connectFrom().connect(add, 0);
|
||||
connectFrom().connect(add, 1);
|
||||
add.connect(connectTo());
|
||||
add.dispose();
|
||||
});
|
||||
|
||||
it("correctly sums a signal and a number", () => {
|
||||
return ConstantOutput(() => {
|
||||
const signal = new Signal(0);
|
||||
const adder = new Add(3);
|
||||
signal.connect(adder);
|
||||
adder.toDestination();
|
||||
}, 3);
|
||||
});
|
||||
|
||||
it("can handle negative values", () => {
|
||||
return ConstantOutput(() => {
|
||||
const signal = new Signal(10);
|
||||
const adder = new Add(-1);
|
||||
signal.connect(adder);
|
||||
adder.toDestination();
|
||||
}, 9);
|
||||
});
|
||||
|
||||
it("can sum two signals", () => {
|
||||
return ConstantOutput(() => {
|
||||
const sigA = new Signal(1);
|
||||
const sigB = new Signal(4);
|
||||
const adder = new Add();
|
||||
sigA.connect(adder, 0, 0);
|
||||
sigB.connect(adder, 0, 1);
|
||||
adder.toDestination();
|
||||
}, 5);
|
||||
});
|
||||
});
|
||||
});
|
66
Tone/signal/Add.ts
Normal file
66
Tone/signal/Add.ts
Normal file
|
@ -0,0 +1,66 @@
|
|||
import { Gain } from "Tone/core";
|
||||
import { connectSeries } from "Tone/core/Connect";
|
||||
import { optionsFromArguments } from "Tone/core/util/Defaults";
|
||||
import { Signal, SignalOptions } from "./Signal";
|
||||
|
||||
/**
|
||||
* @class Add a signal and a number or two signals. When no value is
|
||||
* passed into the constructor, Tone.Add will sum <code>input[0]</code>
|
||||
* and <code>input[1]</code>. If a value is passed into the constructor,
|
||||
* the it will be added to the input.
|
||||
*
|
||||
* @constructor
|
||||
* @extends {Signal}
|
||||
* @param value If no value is provided, Tone.Add will sum the first
|
||||
* and second inputs.
|
||||
* @example
|
||||
* var signal = new Signal(2);
|
||||
* var add = new Add(2);
|
||||
* signal.connect(add);
|
||||
* //the output of add equals 4
|
||||
* @example
|
||||
* //if constructed with no arguments
|
||||
* //it will add the first and second inputs
|
||||
* var add = new Add();
|
||||
* var sig0 = new Signal(3).connect(add, 0, 0);
|
||||
* var sig1 = new Signal(4).connect(add, 0, 1);
|
||||
* //the output of add equals 7.
|
||||
*/
|
||||
export class Add extends Signal {
|
||||
|
||||
override = false;
|
||||
|
||||
name = "Add";
|
||||
|
||||
/**
|
||||
* the summing node
|
||||
*/
|
||||
private _sum: Gain = new Gain({ context: this.context });
|
||||
input = this._sum;
|
||||
output = this._sum;
|
||||
|
||||
constructor(options?: Partial<SignalOptions<number>>);
|
||||
// tslint:disable-next-line: unified-signatures
|
||||
constructor(value?: number);
|
||||
constructor() {
|
||||
super(Object.assign(optionsFromArguments(Add.getDefaults(), arguments, ["value"])));
|
||||
const options = optionsFromArguments(Add.getDefaults(), arguments, ["value"]);
|
||||
|
||||
connectSeries(this._constantSource, this._sum);
|
||||
}
|
||||
|
||||
static getDefaults(): SignalOptions<number> {
|
||||
return Object.assign(Signal.getDefaults(), {
|
||||
value: 0,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up.
|
||||
*/
|
||||
dispose(): this {
|
||||
super.dispose();
|
||||
this._sum.disconnect();
|
||||
return this;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue