UNPKG

2.02 kBJavaScriptView Raw
1import { optionsFromArguments } from "../core/util/Defaults.js";
2import { Add } from "./Add.js";
3import { Multiply } from "./Multiply.js";
4import { SignalOperator } from "./SignalOperator.js";
5/**
6 * Performs a linear scaling on an input signal.
7 * Scales a NormalRange input to between
8 * outputMin and outputMax.
9 *
10 * @example
11 * const scale = new Tone.Scale(50, 100);
12 * const signal = new Tone.Signal(0.5).connect(scale);
13 * // the output of scale equals 75
14 * @category Signal
15 */
16export class Scale extends SignalOperator {
17 constructor() {
18 const options = optionsFromArguments(Scale.getDefaults(), arguments, [
19 "min",
20 "max",
21 ]);
22 super(options);
23 this.name = "Scale";
24 this._mult = this.input = new Multiply({
25 context: this.context,
26 value: options.max - options.min,
27 });
28 this._add = this.output = new Add({
29 context: this.context,
30 value: options.min,
31 });
32 this._min = options.min;
33 this._max = options.max;
34 this.input.connect(this.output);
35 }
36 static getDefaults() {
37 return Object.assign(SignalOperator.getDefaults(), {
38 max: 1,
39 min: 0,
40 });
41 }
42 /**
43 * The minimum output value. This number is output when the value input value is 0.
44 */
45 get min() {
46 return this._min;
47 }
48 set min(min) {
49 this._min = min;
50 this._setRange();
51 }
52 /**
53 * The maximum output value. This number is output when the value input value is 1.
54 */
55 get max() {
56 return this._max;
57 }
58 set max(max) {
59 this._max = max;
60 this._setRange();
61 }
62 /**
63 * set the values
64 */
65 _setRange() {
66 this._add.value = this._min;
67 this._mult.value = this._max - this._min;
68 }
69 dispose() {
70 super.dispose();
71 this._add.dispose();
72 this._mult.dispose();
73 return this;
74 }
75}
76//# sourceMappingURL=Scale.js.map
\No newline at end of file