cleaning up

This commit is contained in:
Jade Rowland 2023-11-07 20:23:45 -05:00
parent 9cbf3079f3
commit 0edd54dee3

View file

@ -113,69 +113,46 @@ function getDelay(orbit, delaytime, delayfeedback, t) {
return delays[orbit]; return delays[orbit];
} }
const createFilter2 = (ctx, cutoff, Q) => { let phaserLFOs = {};
const filter = ctx.createBiquadFilter();
function getPhaser(orbit, speed = 1, depth = 0.5, t) {
//gain
const ac = getAudioContext();
const lfoGain = ac.createGain();
lfoGain.gain.value = 2000;
//lfo
if (phaserLFOs[orbit] == null) {
const lfo = ac.createOscillator();
lfo.frequency.value = speed;
lfo.type = 'sine';
lfo.start();
phaserLFOs[orbit] = lfo;
}
if (phaserLFOs[orbit].frequency.value !== speed) {
phaserLFOs[orbit].frequency.setValueAtTime(speed, t);
}
phaserLFOs[orbit].connect(lfoGain);
//filters
const numStages = 2; //num of filters in series
let fOffset = 0;
const filterChain = [];
for (let i = 0; i < numStages; i++) {
const filter = ac.createBiquadFilter();
filter.type = 'notch'; filter.type = 'notch';
filter.gain.value = 1; filter.gain.value = 1;
filter.frequency.value = cutoff; filter.frequency.value = 1000 + fOffset;
filter.Q.value = Q; filter.Q.value = 2 - Math.min(Math.max(depth * 2, 0), 1.9);
return filter;
};
const createOscillator = (ctx, freq) => { lfoGain.connect(filter.detune);
const osc = ctx.createOscillator(); fOffset += 282;
osc.frequency.value = freq; if (i > 0) {
osc.type = 'sine'; filterChain[i - 1].connect(filter);
return osc;
};
const createGain = (ctx, gain) => {
const gainNode = ctx.createGain();
gainNode.gain.value = gain;
return gainNode;
};
const createLFO = (ctx, freq, gain) => {
const osc = createOscillator(ctx, freq);
const gainNode = createGain(ctx, gain);
osc.start();
osc.connect(gainNode);
return gainNode;
};
let phasers = {};
function getPhaser(orbit, speed = 1, depth = 0.5) {
if (!delays[orbit]) {
const ac = getAudioContext();
let lfo;
const makeupGain = ac.createGain();
if (lfo == null) {
lfo = createLFO(ac, speed, 2000);
} }
const numStages = 2; filterChain.push(filter);
let fOffset = 0;
for (let i = 0; i < numStages; i++) {
const gain = ac.createGain();
gain.gain.value = 1 / numStages;
const filter = createFilter2(ac, 1000 + fOffset, 2 - Math.min(Math.max(depth * 2, 0), 1.9));
makeupGain.connect(filter);
lfo.connect(filter.detune);
filter.connect(gain);
gain.connect(makeupGain);
fOffset += 200 + Math.pow(i, 2);
} }
makeupGain.gain.value = 1; // how much makeup gain to add? return filterChain[filterChain.length - 1];
makeupGain.connect(getDestination());
phasers[orbit] = makeupGain;
}
console.log(phasers);
// delays[orbit].delayTime.value !== delaytime && delays[orbit].delayTime.setValueAtTime(delaytime, t);
// delays[orbit].feedback.value !== delayfeedback && delays[orbit].feedback.setValueAtTime(delayfeedback, t);
return phasers[orbit];
} }
let reverbs = {}; let reverbs = {};
@ -453,18 +430,17 @@ export const superdough = async (value, deadline, hapDuration) => {
panner.pan.value = 2 * pan - 1; panner.pan.value = 2 * pan - 1;
chain.push(panner); chain.push(panner);
} }
// phaser
if (phaser !== undefined) {
const phaserFX = getPhaser(orbit, phaser, phaserDepth, t);
chain.push(phaserFX);
}
// last gain // last gain
const post = gainNode(postgain); const post = gainNode(postgain);
chain.push(post); chain.push(post);
post.connect(getDestination()); post.connect(getDestination());
let phaserSend;
if (phaser != null) {
const phaserFX = getPhaser(orbit, phaser, phaserDepth);
phaserSend = effectSend(post, phaserFX, 0.99);
}
// delay // delay
let delaySend; let delaySend;
if (delay > 0 && delaytime > 0 && delayfeedback > 0) { if (delay > 0 && delaytime > 0 && delayfeedback > 0) {
@ -501,7 +477,7 @@ export const superdough = async (value, deadline, hapDuration) => {
// toDisconnect = all the node that should be disconnected in onended callback // toDisconnect = all the node that should be disconnected in onended callback
// this is crucial for performance // this is crucial for performance
toDisconnect = chain.concat([phaserSend, delaySend, reverbSend, analyserSend]); toDisconnect = chain.concat([delaySend, reverbSend, analyserSend]);
}; };
export const superdoughTrigger = (t, hap, ct, cps) => superdough(hap, t - ct, hap.duration / cps, cps); export const superdoughTrigger = (t, hap, ct, cps) => superdough(hap, t - ct, hap.duration / cps, cps);