2019-08-04 19:53:11 +00:00
|
|
|
import { 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 { Signal } from "./Signal";
|
|
|
|
import { SignalOperator } from "./SignalOperator";
|
|
|
|
|
2019-08-04 18:45:49 +00:00
|
|
|
export interface ScaleOptions extends ToneAudioNodeOptions {
|
2019-08-04 19:53:11 +00:00
|
|
|
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
|
2019-10-25 20:54:33 +00:00
|
|
|
* import { Scale, Signal } from "tone";
|
|
|
|
* const scale = new Scale(50, 100);
|
|
|
|
* const signal = new Signal(0.5).connect(scale);
|
|
|
|
* // 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-08-04 18:45:49 +00:00
|
|
|
export class Scale extends SignalOperator<ScaleOptions> {
|
2019-08-04 17:08:43 +00:00
|
|
|
|
|
|
|
readonly name: string = "Scale";
|
|
|
|
|
2019-08-04 19:53:11 +00:00
|
|
|
readonly input = new Multiply({
|
2019-08-04 17:08:43 +00:00
|
|
|
context: this.context,
|
|
|
|
value: 1,
|
|
|
|
});
|
|
|
|
|
2019-08-04 19:53:11 +00:00
|
|
|
readonly output = new Add({
|
2019-08-04 17:08:43 +00:00
|
|
|
context: this.context,
|
|
|
|
value: 0,
|
|
|
|
});
|
|
|
|
|
2019-08-04 19:53:11 +00:00
|
|
|
private _outputMin: number;
|
|
|
|
private _outputMax: 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.
|
|
|
|
*/
|
2019-08-04 19:53:11 +00:00
|
|
|
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() {
|
2019-08-04 19:53:11 +00:00
|
|
|
super(Object.assign(optionsFromArguments(Scale.getDefaults(), arguments, ["min", "max"])));
|
2019-08-04 17:08:43 +00:00
|
|
|
|
2019-08-04 19:53:11 +00:00
|
|
|
const options = optionsFromArguments(Scale.getDefaults(), arguments, ["min", "max"]);
|
|
|
|
this._outputMin = options.min;
|
|
|
|
this._outputMax = options.max;
|
2019-08-04 17:08:43 +00:00
|
|
|
|
2019-08-04 18:45:49 +00:00
|
|
|
this.input.connect(this.output);
|
2019-08-04 17:08:43 +00:00
|
|
|
this._setRange();
|
|
|
|
}
|
|
|
|
|
2019-08-04 18:45:49 +00:00
|
|
|
static getDefaults(): ScaleOptions {
|
2019-08-04 19:53:11 +00:00
|
|
|
return Object.assign(SignalOperator.getDefaults(), {
|
|
|
|
max: 1,
|
|
|
|
min: 0,
|
2019-08-04 17:08:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-04 19:53:11 +00:00
|
|
|
/**
|
|
|
|
* The minimum output value. This number is output when the value input value is 0.
|
|
|
|
*/
|
2019-08-04 18:45:49 +00:00
|
|
|
get min(): number {
|
2019-08-04 17:08:43 +00:00
|
|
|
return this._outputMin;
|
|
|
|
}
|
2019-08-04 18:45:49 +00:00
|
|
|
set min(min) {
|
2019-08-04 17:08:43 +00:00
|
|
|
this._outputMin = min;
|
|
|
|
this._setRange();
|
|
|
|
}
|
|
|
|
|
2019-08-04 19:53:11 +00:00
|
|
|
/**
|
|
|
|
* The maximum output value. This number is output when the value input value is 1.
|
|
|
|
*/
|
2019-08-04 18:45:49 +00:00
|
|
|
get max(): number {
|
2019-08-04 17:08:43 +00:00
|
|
|
return this._outputMax;
|
|
|
|
}
|
2019-08-04 18:45:49 +00:00
|
|
|
set max(max) {
|
2019-08-04 17:08:43 +00:00
|
|
|
this._outputMax = max;
|
|
|
|
this._setRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* set the values
|
2019-08-04 17:08:43 +00:00
|
|
|
*/
|
|
|
|
private _setRange(): void {
|
|
|
|
this.output.value = this._outputMin;
|
2019-08-04 18:45:49 +00:00
|
|
|
this.input.value = this._outputMax - this._outputMin;
|
2019-08-04 17:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this.input.dispose();
|
|
|
|
this.output.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|