From e55b7c6ce338302691cbe16e1666e928ec8c7732 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sun, 30 Mar 2025 00:55:12 -0400 Subject: [PATCH] mostly working --- packages/superdough/sampler.mjs | 1 + packages/superdough/superdough.mjs | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/superdough/sampler.mjs b/packages/superdough/sampler.mjs index f5e46d6b..45f6b33e 100644 --- a/packages/superdough/sampler.mjs +++ b/packages/superdough/sampler.mjs @@ -278,6 +278,7 @@ export const samples = async (sampleMap, baseUrl = sampleMap._base || '', option const cutGroups = []; + export async function onTriggerSample(t, value, onended, bank, resolveUrl) { let { s, diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index fc81f565..c6daabbf 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -374,6 +374,9 @@ export function resetGlobalEffects() { analysersData = {}; } +let allAudioNodeChains = []; +let maxPolyphony = 12; + export const superdough = async (value, t, hapDuration) => { const ac = getAudioContext(); t = typeof t === 'string' && t.startsWith('=') ? Number(t.slice(1)) : ac.currentTime + t; @@ -472,14 +475,30 @@ export const superdough = async (value, t, hapDuration) => { } = value; gain = nanFallback(gain, 1); + console.info(allAudioNodeChains.length) + + for (let i = 0; i <= allAudioNodeChains.length - maxPolyphony; i++) { + const chain = allAudioNodeChains.shift() + chain.forEach(node => node?.disconnect()) + } + // if (allAudioNodeChains.length > maxPolyphony) { + + + // allAudioNodeChains.slice(0, allAudioNodeChains.length - maxPolyphony).flat().forEach((node) => { + // node.disconnect(); + // }); + // console.info(allAudioNodes.length) + // } //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 toDisconnect = []; // audio nodes that will be disconnected when the source has ended + let audioNodes = []; + // audio nodes that will be disconnected when the source has ended + const onended = () => { - toDisconnect.forEach((n) => n?.disconnect()); + audioNodes.forEach((n) => n?.disconnect()); }; if (bank && s) { s = `${bank}_${s}`; @@ -654,9 +673,10 @@ export const superdough = async (value, t, hapDuration) => { // connect chain elements together chain.slice(1).reduce((last, current) => last.connect(current), chain[0]); - // toDisconnect = all the node that should be disconnected in onended callback + // audioNodes = all the node that should be disconnected in onended callback // this is crucial for performance - toDisconnect = chain.concat([delaySend, reverbSend, analyserSend]); + audioNodes = chain.concat([delaySend, reverbSend, analyserSend]); + allAudioNodeChains.push(audioNodes) }; export const superdoughTrigger = (t, hap, ct, cps) => {