UNPKG

@thi.ng/dsp

Version:

Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils

47 lines (46 loc) 982 B
import { TAU } from "@thi.ng/math/api"; import { clamp05 } from "@thi.ng/math/interval"; import { AProc } from "./aproc.js"; const onepoleLP = (fc) => new OnePole("lp", fc); const onepoleHP = (fc) => new OnePole("hp", fc); class OnePole extends AProc { constructor(_type, _freq) { super(0); this._type = _type; this._freq = _freq; this.setFreq(_freq); } _a0; _b1; clear() { this._val = 0; } reset() { this.clear(); return this; } next(x) { return this._val = x * this._a0 + this._val * this._b1; } setFreq(fc) { this._freq = fc = clamp05(fc); if (this._type === "lp") { this._b1 = Math.exp(-TAU * fc); this._a0 = 1 - this._b1; } else { this._b1 = -Math.exp(-TAU * (0.5 - fc)); this._a0 = 1 + this._b1; } } filterCoeffs() { return { zeroes: [this._a0], poles: [1, this._type === "lp" ? this._b1 : -this._b1] }; } } export { OnePole, onepoleHP, onepoleLP };