@thi.ng/dsp
Version:
Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils
41 lines (40 loc) • 1.08 kB
JavaScript
import { SYSTEM } from "@thi.ng/random/system";
import { AGen } from "./agen.js";
const AMP = [3.8024, 2.9694, 2.597, 3.087, 3.4006];
const PROB = [198e-5, 0.0128, 0.049, 0.17, 0.682];
const pinkNoise = (gain, rnd, amp, prob) => new PinkNoise(gain, rnd, amp, prob);
class PinkNoise extends AGen {
constructor(_gain = 1, _rnd = SYSTEM, _amp = AMP, prob = PROB) {
super(0);
this._gain = _gain;
this._rnd = _rnd;
this._amp = _amp;
this._gain /= _amp.reduce((acc, x) => acc + x, 0);
this._psum = prob.reduce(
(acc, x, i) => (acc.push(i > 0 ? acc[i - 1] + x : x), acc),
[]
);
this._bins = [0, 0, 0, 0, 0];
}
_bins;
_psum;
reset() {
this._bins.fill(0);
return this;
}
next() {
const { _bins, _rnd, _amp, _psum } = this;
const bin = _rnd.float();
for (let i = 0; i < 5; i++) {
if (bin <= _psum[i]) {
_bins[i] = _rnd.norm(_amp[i]);
break;
}
}
return this._val = this._gain * (_bins[0] + _bins[1] + _bins[2] + _bins[3] + _bins[4]);
}
}
export {
PinkNoise,
pinkNoise
};