@thi.ng/dsp
Version:
Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils
38 lines (37 loc) • 939 B
JavaScript
import { AGen } from "./agen.js";
const impulseTrain = (period, start) => new ImpulseTrain(1, 0, period, start);
const impulseTrainT = (on, off, period, start) => new ImpulseTrain(on, off, period, start);
const impulseTrainB = (period, start) => new ImpulseTrain(true, false, period, start);
class ImpulseTrain extends AGen {
constructor(_on, _off, _period, _pos = 0) {
super(_off);
this._on = _on;
this._off = _off;
this._period = _period;
this._pos = _pos;
this._startpos = --this._pos;
}
_startpos;
copy() {
return new ImpulseTrain(
this._on,
this._off,
this._period,
this._startpos + 1
);
}
reset() {
this._val = this._off;
this._pos = this._startpos;
return this;
}
next() {
return this._val = ++this._pos >= this._period ? (this._pos = 0, this._on) : this._off;
}
}
export {
ImpulseTrain,
impulseTrain,
impulseTrainB,
impulseTrainT
};