Tone.js/Tone/signal/Pow.ts

85 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-09-16 22:18:03 +00:00
import { WaveShaper, WaveShaperMappingFn } from "./WaveShaper";
2019-10-25 20:54:33 +00:00
import { optionsFromArguments } from "../core/util/Defaults";
import { SignalOperator } from "./SignalOperator";
2019-10-25 20:54:33 +00:00
import { ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
export interface PowOptions extends ToneAudioNodeOptions {
value: number;
}
2019-09-16 22:18:03 +00:00
/**
2019-09-20 21:55:36 +00:00
* Pow applies an exponent to the incoming signal. The incoming signal must be AudioRange [-1, 1]
2019-09-16 22:18:03 +00:00
*
2019-09-20 21:55:36 +00:00
* @example
* const pow = new Tone.Pow(2);
* const sig = new Tone.Signal(0.5).connect(pow);
2019-10-25 20:54:33 +00:00
* // output of pow is 0.25.
2019-09-20 22:51:45 +00:00
* @category Signal
2019-09-16 22:18:03 +00:00
*/
export class Pow extends SignalOperator<PowOptions> {
2019-09-16 22:18:03 +00:00
readonly name: string = "Pow";
private _exponent: number;
2019-09-16 22:18:03 +00:00
private _exponentScaler: WaveShaper;
2019-09-16 22:18:03 +00:00
input: WaveShaper;
2019-09-16 22:18:03 +00:00
output: WaveShaper;
2019-09-16 22:18:03 +00:00
/**
* @param value Constant exponent value to use
*/
constructor(value?: number);
constructor(options?: Partial<PowOptions>);
2019-09-16 22:18:03 +00:00
constructor() {
super(Object.assign(optionsFromArguments(Pow.getDefaults(), arguments, ["value"])));
const options = optionsFromArguments(Pow.getDefaults(), arguments, ["value"]);
this._exponentScaler = this.input = this.output = new WaveShaper({
context: this.context,
mapping: this._expFunc(options.value),
length: 8192,
});
2019-09-16 22:18:03 +00:00
this._exponent = options.value;
2019-09-16 22:18:03 +00:00
}
static getDefaults(): PowOptions {
return Object.assign(SignalOperator.getDefaults(), {
2019-09-16 22:18:03 +00:00
value: 1,
});
}
/**
* the function which maps the waveshaper
* @param exponent exponent value
2019-09-16 22:18:03 +00:00
*/
private _expFunc(exponent: number): WaveShaperMappingFn {
2019-09-16 22:18:03 +00:00
return (val: number) => {
return Math.pow(Math.abs(val), exponent);
2019-09-16 22:18:03 +00:00
};
}
2019-09-20 21:55:36 +00:00
/**
2019-10-23 03:04:52 +00:00
* The value of the exponent.
*/
get value(): number {
2019-09-16 22:18:03 +00:00
return this._exponent;
}
set value(exponent: number) {
this._exponent = exponent;
2019-09-16 22:18:03 +00:00
this._exponentScaler.setMap(this._expFunc(this._exponent));
}
/**
* Clean up.
*/
dispose(): this {
super.dispose();
this._exponentScaler.dispose();
return this;
}
2019-09-20 21:55:36 +00:00
}