From dde20960f34b34166cfa209572844b2c07f92ead Mon Sep 17 00:00:00 2001 From: Filipe Herculano Date: Sun, 4 Aug 2019 13:08:43 -0400 Subject: [PATCH] converting Scale to typescript --- Tone/signal/Scale.test.ts | 58 +++++++++++++++++++++ Tone/signal/Scale.ts | 104 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 Tone/signal/Scale.test.ts create mode 100644 Tone/signal/Scale.ts diff --git a/Tone/signal/Scale.test.ts b/Tone/signal/Scale.test.ts new file mode 100644 index 00000000..dfc85b4e --- /dev/null +++ b/Tone/signal/Scale.test.ts @@ -0,0 +1,58 @@ +import { expect } from "chai"; +import { BasicTests } from "test/helper/Basic"; +import { connectFrom, connectTo } from "test/helper/Connect"; +import { ConstantOutput } from "test/helper/ConstantOutput"; +import { Scale } from "./Scale"; +import { Signal } from "./Signal"; + +describe("Scale", () => { + + BasicTests(Scale); + + context("Scaling", () => { + + it("handles input and output connections", () => { + const scale = new Scale({ outputMin: 0, outputMax: 100 }); + connectFrom().connect(scale); + scale.connect(connectTo()); + scale.dispose(); + }); + + it("can set the min and max values", () => { + const scale = new Scale({ outputMin: 0, outputMax: 100 }); + scale.setMin = -0.01; + expect(scale.getMin).to.be.closeTo(-0.01, 0.001); + scale.setMax = 1000; + expect(scale.getMax).to.be.closeTo(1000, 0.001); + scale.dispose(); + }); + + it("scales to the min when the input is 0", () => { + return ConstantOutput(() => { + const signal = new Signal(0); + const scale = new Scale({ outputMin: -10, outputMax: 8 }); + signal.connect(scale); + scale.toDestination(); + }, -10); + }); + + it("scales to the max when the input is 1", () => { + return ConstantOutput(() => { + const signal = new Signal(1); + const scale = new Scale({ outputMin: -10, outputMax: 0 }); + scale.setMax = 8; + signal.connect(scale); + scale.toDestination(); + }, 8); + }); + + it("scales an input of 0.5 to 15 (10, 20)", () => { + return ConstantOutput(() => { + const signal = new Signal(0.5); + const scale = new Scale({ outputMin: 10, outputMax: 20 }); + signal.connect(scale); + scale.toDestination(); + }, 15); + }); + }); +}); diff --git a/Tone/signal/Scale.ts b/Tone/signal/Scale.ts new file mode 100644 index 00000000..a473334e --- /dev/null +++ b/Tone/signal/Scale.ts @@ -0,0 +1,104 @@ +import { ToneAudioNodeOptions } from "Tone/core/context/ToneAudioNode"; +import { optionsFromArguments } from "Tone/core/util/Defaults"; +import { Add } from "./Add"; +import { Multiply } from "./Multiply"; +import { Signal } from "./Signal"; +import { SignalOperator } from "./SignalOperator"; + +export interface ScaleOptions extends ToneAudioNodeOptions { + value: Type; + outputMin: number; + outputMax: number; +} + +/** + * @class Performs a linear scaling on an input signal. + * Scales a NormalRange input to between + * outputMin and outputMax. + * + * @constructor + * @extends {SignalOperator} + * @param outputMin The output value when the input is 0. + * @param outputMax The output value when the input is 1. + * @example + * var scale = new Scale(50, 100); + * var signal = new Signal(0.5).connect(scale); + * //the output of scale equals 75 + */ +export class Scale extends SignalOperator> { + + readonly name: string = "Scale"; + + input = new Multiply({ + context: this.context, + value: 1, + }); + + output = new Add({ + context: this.context, + value: 0, + }); + + value: number = 0; + + private _outputMin!: number; + + private _outputMax!: number; + + constructor(options?: Partial>); + // tslint:disable-next-line: unified-signatures + constructor(value?: number); + constructor() { + super(Object.assign(optionsFromArguments(Scale.getDefaults(), arguments, ["value"]))); + + const options = optionsFromArguments(Scale.getDefaults(), arguments, ["outputMin", "outputMax"]); + this._outputMin = options.outputMin; + this._outputMax = options.outputMax; + + this.connect(this.output); + this._setRange(); + } + + static getDefaults(): ScaleOptions { + return Object.assign(Signal.getDefaults(), { + outputMax: 1, + outputMin: 0, + }); + } + + get getMin(): number { + return this._outputMin; + } + + set setMin(min: number) { + this._outputMin = min; + this._setRange(); + } + + get getMax(): number { + return this._outputMax; + } + + set setMax(max: number) { + this._outputMax = max; + this._setRange(); + } + + /** + * set the values + */ + private _setRange(): void { + this.output.value = this._outputMin; + this.value = this._outputMax - this._outputMin; + } + + /** + * Clean up. + */ + dispose(): this { + super.dispose(); + this.input.dispose(); + this.output.dispose(); + return this; + } +}