@thi.ng/dsp
Version:
Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils
47 lines (46 loc) • 1.06 kB
JavaScript
import { isNumber } from "@thi.ng/checks/is-number";
import { Add, add } from "./add.js";
import { AGen } from "./agen.js";
import { Const } from "./const.js";
import { sum } from "./sum.js";
const osc = (osc2, freq, amp, dc, phase) => new Osc(osc2, freq, amp, dc, phase);
const modOsc = (osc2, freq, fmod, amod = 1, dc, phase) => new Osc(
osc2,
sum(
isNumber(fmod) ? new Const(fmod) : fmod,
isNumber(freq) ? add(freq) : freq
),
amod,
dc,
phase
);
class Osc extends AGen {
constructor(_osc, freq, amp = 1, _dc = 0, phase = 0) {
super(0);
this._osc = _osc;
this._dc = _dc;
isNumber(freq) ? this.setFreq(freq, phase) : this.setFreq(freq);
this.setAmp(amp);
}
_phase;
_amp;
next() {
return this._val = this._osc(
this._phase.next(),
1,
this._amp.next(),
this._dc
);
}
setFreq(freq, phase) {
this._phase = isNumber(freq) ? new Add(freq, phase || 0) : freq;
}
setAmp(amp) {
this._amp = isNumber(amp) ? new Const(amp) : amp;
}
}
export {
Osc,
modOsc,
osc
};