@thi.ng/dsp
Version:
Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils
45 lines (44 loc) • 1.07 kB
JavaScript
import { PI } from "@thi.ng/math/api";
import { AProc } from "./aproc.js";
const waveShaper = (thresh, amp, map) => new WaveShaper(thresh, amp, map);
class WaveShaper extends AProc {
constructor(_coeff = 3, amp = true, _map = waveshapeSigmoid) {
super(0);
this._coeff = _coeff;
this._map = _map;
amp === true ? this.setAutoGain() : this.setAmp(amp);
}
_amp;
_autoGain;
next(x) {
return this._val = this._amp * this._map(x, this._coeff);
}
coeff() {
return this._coeff;
}
setCoeff(t) {
this._coeff = Math.max(t, 0);
this._autoGain && this.setAutoGain();
}
amp() {
return this._amp;
}
setAmp(a) {
this._amp = a;
this._autoGain = false;
}
setAutoGain() {
this._amp = 1 / this._map(1, this._coeff);
this._autoGain = true;
}
}
const waveshapeTan = (x, k) => Math.atan(k * x) / k;
const waveshapeSigmoid = (x, k) => 2 / (1 + Math.exp(-k * x)) - 1;
const waveshapeSin = (x, k) => Math.sin(PI / k * x);
export {
WaveShaper,
waveShaper,
waveshapeSigmoid,
waveshapeSin,
waveshapeTan
};