Tone.js/Tone/signal/Abs.ts

52 lines
938 B
TypeScript
Raw Normal View History

2019-07-11 21:20:11 +00:00
import { ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
import { SignalOperator } from "./SignalOperator";
import { WaveShaper } from "./WaveShaper";
/**
2019-08-27 15:53:14 +00:00
* Return the absolute value of an incoming signal.
2019-07-11 21:20:11 +00:00
*
2019-08-27 15:53:14 +00:00
* @example
2019-07-11 21:20:11 +00:00
* var signal = new Tone.Signal(-1);
* var abs = new Tone.Abs();
* signal.connect(abs);
* //the output of abs is 1.
2019-09-16 14:15:23 +00:00
* @category Signal
2019-07-11 21:20:11 +00:00
*/
export class Abs extends SignalOperator<ToneAudioNodeOptions> {
2019-09-04 23:18:44 +00:00
readonly name: string = "Abs";
2019-07-11 21:20:11 +00:00
/**
* The node which converts the audio ranges
*/
private _abs = new WaveShaper({
context: this.context,
mapping: val => {
if (Math.abs(val) < 0.001) {
return 0;
} else {
return Math.abs(val);
}
},
});
/**
* The AudioRange input [-1, 1]
*/
input = this._abs;
/**
* The output range [0, 1]
*/
output = this._abs;
/**
2019-09-14 20:39:18 +00:00
* clean up
2019-07-11 21:20:11 +00:00
*/
dispose(): this {
super.dispose();
this._abs.dispose();
return this;
}
}