ramp cut audio node

This commit is contained in:
Jade (Rose) Rowland 2025-03-30 16:33:47 -04:00
parent 8962e5a4f0
commit 6f9bcc53dc
5 changed files with 51 additions and 32 deletions

View file

@ -379,7 +379,7 @@ export function resetGlobalEffects() {
analysersData = {};
}
let allAudioNodeChains = new Map();
let activeSoundSources = new Map();
export const superdough = async (value, t, hapDuration) => {
const ac = getAudioContext();
@ -480,30 +480,24 @@ export const superdough = async (value, t, hapDuration) => {
gain = nanFallback(gain, 1);
const chainID = Math.round(Math.random() * 10000);
const chainID = Math.round(Math.random() * 1000000);
// oldest audio nodes will be removed if maximum polyphony is exceeded
for (let i = 0; i <= allAudioNodeChains.size - maxPolyphony; i++) {
const ch = allAudioNodeChains.entries().next();
const key = ch.value[0];
const chain = ch.value[1];
if (key == null) {
continue;
}
chain?.forEach((node) => node?.disconnect());
allAudioNodeChains.delete(key);
// oldest audio nodes will be destroyed if maximum polyphony is exceeded
for (let i = 0; i <= activeSoundSources.size - maxPolyphony; i++) {
const ch = activeSoundSources.entries().next();
const source = ch.value[1];
const chainID = ch.value[0];
const endTime = t + .25;
source?.node?.gain?.linearRampToValueAtTime(0, endTime);
source?.stop?.(endTime);
activeSoundSources.delete(chainID);
}
//music programs/audio gear usually increments inputs/outputs from 1, so imitate that behavior
channels = (Array.isArray(channels) ? channels : [channels]).map((ch) => ch - 1);
gain *= velocity; // velocity currently only multiplies with gain. it might do other things in the future
let audioNodes = [];
const onended = () => {
const chain = allAudioNodeChains.get(chainID);
chain?.forEach((n) => n?.disconnect());
allAudioNodeChains.delete(chainID);
};
if (bank && s) {
s = `${bank}_${s}`;
value.s = s;
@ -515,10 +509,15 @@ export const superdough = async (value, t, hapDuration) => {
sourceNode = source(t, value, hapDuration);
} else if (getSound(s)) {
const { onTrigger } = getSound(s);
const soundHandle = await onTrigger(t, value, onended);
const onEnded = () => {
audioNodes.forEach((n) => n?.disconnect());
activeSoundSources.delete(chainID);
};
const soundHandle = await onTrigger(t, value, onEnded);
if (soundHandle) {
sourceNode = soundHandle.node;
soundHandle.stop(t + hapDuration);
activeSoundSources.set(chainID, soundHandle);
}
} else {
throw new Error(`sound ${s} not found! Is it loaded?`);
@ -648,6 +647,7 @@ export const superdough = async (value, t, hapDuration) => {
if (delay > 0 && delaytime > 0 && delayfeedback > 0) {
const delyNode = getDelay(orbit, delaytime, delayfeedback, t);
delaySend = effectSend(post, delyNode, delay);
audioNodes.push(delaySend);
}
// reverb
let reverbSend;
@ -665,6 +665,7 @@ export const superdough = async (value, t, hapDuration) => {
}
const reverbNode = getReverb(orbit, roomsize, roomfade, roomlp, roomdim, roomIR);
reverbSend = effectSend(post, reverbNode, room);
audioNodes.push(reverbSend);
}
// analyser
@ -672,11 +673,13 @@ export const superdough = async (value, t, hapDuration) => {
if (analyze) {
const analyserNode = getAnalyserById(analyze, 2 ** (fft + 5));
analyserSend = effectSend(post, analyserNode, 1);
audioNodes.push(analyserSend);
}
// connect chain elements together
chain.slice(1).reduce((last, current) => last.connect(current), chain[0]);
allAudioNodeChains.set(chainID, [...chain, delaySend, reverbSend, analyserSend]);
audioNodes = audioNodes.concat(chain);
// activeSoundSources.set(chainID, [...chain, delaySend, reverbSend, analyserSend].filter(Boolean));
};
export const superdoughTrigger = (t, hap, ct, cps) => {