Tone.js/Tone/signal/Scale.ts

120 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-10-30 03:13:22 +00:00
import { InputNode, OutputNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
import { optionsFromArguments } from "../core/util/Defaults";
2019-08-04 17:08:43 +00:00
import { Add } from "./Add";
import { Multiply } from "./Multiply";
import { SignalOperator } from "./SignalOperator";
export interface ScaleOptions extends ToneAudioNodeOptions {
min: number;
max: number;
2019-08-04 17:08:43 +00:00
}
/**
2019-08-27 15:53:14 +00:00
* Performs a linear scaling on an input signal.
* Scales a NormalRange input to between
* outputMin and outputMax.
2019-08-04 17:08:43 +00:00
*
2019-08-27 15:53:14 +00:00
* @example
* const scale = new Tone.Scale(50, 100);
* const signal = new Tone.Signal(0.5).connect(scale);
2019-10-25 20:54:33 +00:00
* // the output of scale equals 75
2019-09-16 14:15:23 +00:00
* @category Signal
2019-08-04 17:08:43 +00:00
*/
2019-10-30 03:13:22 +00:00
export class Scale<Options extends ScaleOptions = ScaleOptions> extends SignalOperator<Options> {
2019-08-04 17:08:43 +00:00
readonly name: string = "Scale";
2019-10-30 03:13:22 +00:00
input: InputNode;
output: OutputNode;
2019-08-04 17:08:43 +00:00
2019-10-30 03:13:22 +00:00
/**
* Hold the multiple
*/
protected _mult: Multiply;
/**
* Hold the adder
*/
protected _add: Add;
/**
* Private reference to the min value
*/
private _min: number;
2019-08-04 17:08:43 +00:00
2019-10-30 03:13:22 +00:00
/**
* Private reference to the max value
*/
private _max: number;
2019-08-04 17:08:43 +00:00
2019-08-27 15:53:14 +00:00
/**
* @param min The output value when the input is 0.
* @param max The output value when the input is 1.
*/
constructor(min?: number, max?: number);
2019-08-27 15:53:14 +00:00
constructor(options?: Partial<ScaleOptions>);
2019-08-04 17:08:43 +00:00
constructor() {
super(Object.assign(optionsFromArguments(Scale.getDefaults(), arguments, ["min", "max"])));
const options = optionsFromArguments(Scale.getDefaults(), arguments, ["min", "max"]);
2019-10-30 03:13:22 +00:00
this._mult = this.input = new Multiply({
context: this.context,
value: options.max - options.min,
});
2019-10-30 03:13:22 +00:00
this._add = this.output = new Add({
context: this.context,
value: options.min,
});
this._min = options.min;
this._max = options.max;
2019-08-04 17:08:43 +00:00
this.input.connect(this.output);
2019-08-04 17:08:43 +00:00
}
static getDefaults(): ScaleOptions {
return Object.assign(SignalOperator.getDefaults(), {
max: 1,
min: 0,
2019-08-04 17:08:43 +00:00
});
}
/**
* The minimum output value. This number is output when the value input value is 0.
*/
get min(): number {
2019-10-30 03:13:22 +00:00
return this._min;
2019-08-04 17:08:43 +00:00
}
set min(min) {
2019-10-30 03:13:22 +00:00
this._min = min;
2019-08-04 17:08:43 +00:00
this._setRange();
}
/**
2019-10-30 03:13:22 +00:00
* The maximum output value. This number is output when the value input value is 1.
*/
get max(): number {
2019-10-30 03:13:22 +00:00
return this._max;
2019-08-04 17:08:43 +00:00
}
set max(max) {
2019-10-30 03:13:22 +00:00
this._max = max;
2019-08-04 17:08:43 +00:00
this._setRange();
}
/**
2019-09-14 20:39:18 +00:00
* set the values
2019-08-04 17:08:43 +00:00
*/
private _setRange(): void {
2019-10-30 03:13:22 +00:00
this._add.value = this._min;
this._mult.value = this._max - this._min;
2019-08-04 17:08:43 +00:00
}
dispose(): this {
super.dispose();
2019-10-30 03:13:22 +00:00
this._add.dispose();
this._mult.dispose();
2019-08-04 17:08:43 +00:00
return this;
}
}