From ad43259353a962fb48ec2374a5363715a77e816f Mon Sep 17 00:00:00 2001 From: Aria Date: Fri, 5 Dec 2025 13:39:52 -0600 Subject: [PATCH] Switch to omod --- packages/core/pattern.mjs | 57 +++++++++---------- packages/soundfonts/fontloader.mjs | 2 +- packages/superdough/helpers.mjs | 4 +- packages/superdough/superdough.mjs | 89 +++++++----------------------- 4 files changed, 52 insertions(+), 100 deletions(-) diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index d1875a964..f1cd06535 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -3726,7 +3726,8 @@ const resolveConfigKey = (funcName, key) => { addConfigAlias('lfo', 'target', 't'); addConfigAlias('lfo', 'param', 'p'); addConfigAlias('lfo', 'rate', 'r'); -addConfigAlias('lfo', 'depth', 'dep', 'dp', 'd'); +addConfigAlias('lfo', 'depth', 'dep', 'dr'); +addConfigAlias('lfo', 'depthabs', 'da'); addConfigAlias('lfo', 'dcoffset', 'dc'); addConfigAlias('lfo', 'shape', 'sh'); addConfigAlias('lfo', 'skew', 'sk'); @@ -3737,21 +3738,23 @@ addConfigAlias('env', 'attack', 'att', 'a'); addConfigAlias('env', 'decay', 'dec', 'd'); addConfigAlias('env', 'sustain', 'sus', 's'); addConfigAlias('env', 'release', 'rel', 'r'); -addConfigAlias('env', 'depth', 'dep', 'dp'); +addConfigAlias('env', 'depth', 'dep', 'dr'); +addConfigAlias('env', 'depthabs', 'da'); addConfigAlias('env', 'acurve', 'ac'); addConfigAlias('env', 'dcurve', 'dc'); addConfigAlias('env', 'rcurve', 'rc'); -addConfigAlias('send', 'id'); -addConfigAlias('send', 'target', 't'); -addConfigAlias('send', 'depth', 'dep', 'dp', 'd'); -addConfigAlias('send', 'dc'); +addConfigAlias('omod', 'orbit', 'o'); +addConfigAlias('omod', 'target', 't'); +addConfigAlias('omod', 'depth', 'dep', 'dr'); +addConfigAlias('omod', 'depthabs', 'da'); +addConfigAlias('omod', 'dc'); Pattern.prototype.modulate = function (type, config, idx) { if (config == null || typeof config !== 'object') { return this; } - if (!['lfo', 'send', 'env'].includes(type)) { - logger(`[core] Modulation type ${type} not found. Please use one of 'lfo', 'env', 'send'`); + if (!['lfo', 'env', 'omod'].includes(type)) { + logger(`[core] Modulation type ${type} not found. Please use one of 'lfo', 'env', 'omod'`); return this; } let output = this; @@ -3775,19 +3778,18 @@ Pattern.prototype.modulate = function (type, config, idx) { /** * Configures an LFO. Can be called in sequence like pat.lfo(...).lfo(...) to set up multiple LFOs * - * * @name lfo * @memberof Pattern * @param {Object} config LFO configuration. - * @param {string | Pattern} [config.target] Node (and parameter if specified like `lpf.frequency`) to modulate. Aliases: target, t + * @param {string | Pattern} [config.target] Node (and parameter if specified like `lpf.frequency`) to modulate. Aliases: t * @param {number | Pattern} [config.rate] Modulation rate. Aliases: rate, r - * @param {number | Pattern} [config.depth] Modulation depth. Aliases: dep, dp, d + * @param {number | Pattern} [config.depth] Relative modulation depth. Aliases: dep, dr + * @param {number | Pattern} [config.depthabs] Absolute modulation depth. Aliases: da * @param {number | Pattern} [config.dcoffset] DC offset / bias for the waveform. Aliases: dc * @param {number | Pattern} [config.shape] Waveform shape index. Aliases: sh * @param {number | Pattern} [config.skew] Waveform skew amount. Aliases: sk * @param {number | Pattern} [config.curve] Exponential curve amount. Aliases: c * @param {number | Pattern} [config.sync] Tempo-synced modulation rate. Aliases: s - * @param {number | null} idx Index of the LFO slot to overwrite. Omit to append a new LFO * @returns Pattern */ Pattern.prototype.lfo = function (config, idx) { @@ -3798,12 +3800,12 @@ export const lfo = (config) => pure({}).lfo(config); /** * Configures an envelope. Can be called in sequence like pat.env(...).env(...) to set up multiple envelopes * - * * @name env * @memberof Pattern * @param {Object} config Envelope configuration. - * @param {string | Pattern} [config.target] Node (and parameter if specified like `lpf.frequency`) to modulate. Aliases: target, t - * @param {number | Pattern} [config.depth] Modulation depth. Aliases: dep, dp + * @param {string | Pattern} [config.target] Node (and parameter if specified like `lpf.frequency`) to modulate. Aliases: t + * @param {number | Pattern} [config.depth] Relative modulation depth. Aliases: dep, dr + * @param {number | Pattern} [config.depthabs] Absolute modulation depth. Aliases: da * @param {number | Pattern} [config.attack] Time to reach depth. Aliases: att, a * @param {number | Pattern} [config.decay] Time to reach sustain. Aliases: dec, d * @param {number | Pattern} [config.sustain] Sustain depth. Aliases: sus, s @@ -3811,7 +3813,6 @@ export const lfo = (config) => pure({}).lfo(config); * @param {number | Pattern} [config.acurve] Snappiness of attack curve (-1 = relaxed, 1 = snappy). Aliases: ac * @param {number | Pattern} [config.dcurve] Snappiness of decay curve (-1 = relaxed, 1 = snappy). Aliases: dc * @param {number | Pattern} [config.rcurve] Snappiness of release curve (-1 = relaxed, 1 = snappy). Aliases: rc - * @param {number | null} idx Index of the envelope slot to overwrite. Omit to append a new envelope * @returns Pattern */ Pattern.prototype.env = function (config, idx) { @@ -3820,21 +3821,21 @@ Pattern.prototype.env = function (config, idx) { export const env = (config) => pure({}).env(config); /** - * Sends the output of this pattern to a parameter on another pattern. - * Can be called in sequence like pat.send(...).send(...) to send to multiple parameters + * Modulates with the output from a given `orbit` + * Can be called in sequence like pat.obus(...).obus(...) to set up multiple modulators * - * - * @name send + * @name omod * @memberof Pattern - * @param {Object} config Send configuration. - * @param {string | Pattern} [config.id] Pattern id to modulate - * @param {string | Pattern} [config.target] Node (and parameter if specified like `lpf.frequency`) to modulate. Aliases: target, t - * @param {number | Pattern} [config.depth] Modulation depth. Aliases:dep, dp, d + * @param {Object} config Orbit bus configuration. + * @param {string | Pattern} [config.orbit] Orbit to get modulation signal from + * @param {string | Pattern} [config.target] Node (and parameter if specified like `lpf.frequency`) to modulate. Aliases: t + * @param {number | Pattern} [config.depth] Relative modulation depth. Aliases: dep, dr + * @param {number | Pattern} [config.depthabs] Absolute modulation depth. Aliases: da + * @param {number | Pattern} [config.ratio] Modulation ratio. Aliases: rat * @param {number | Pattern} [config.dc] DC offset prior to application - * @param {number | null} idx Index of the send slot to overwrite. Omit to append a new send * @returns Pattern */ -Pattern.prototype.send = function (config, idx) { - return this.modulate('send', config, idx); +Pattern.prototype.omod = function (config, idx) { + return this.modulate('omod', config, idx); }; -export const send = (config) => pure({}).send(config); +export const omod = (config) => pure({}).omod(config); diff --git a/packages/soundfonts/fontloader.mjs b/packages/soundfonts/fontloader.mjs index ea5b6818f..62ea5f806 100644 --- a/packages/soundfonts/fontloader.mjs +++ b/packages/soundfonts/fontloader.mjs @@ -176,7 +176,7 @@ export function registerSoundfonts() { vibratoOscillator?.stop(); node.disconnect(); onended(); - }; + }); return { node, stop, source: bufferSource }; }, { type: 'soundfont', prebake: true, fonts }, diff --git a/packages/superdough/helpers.mjs b/packages/superdough/helpers.mjs index 1cd292b9d..33c5a52b4 100644 --- a/packages/superdough/helpers.mjs +++ b/packages/superdough/helpers.mjs @@ -111,13 +111,11 @@ export function getEnvelope(audioContext, properties = {}) { export function getLfo(audioContext, properties = {}) { // Extract some params we need for deriving other params - const { begin, shape = 0, ...props } = properties; + const { shape = 0, ...props } = properties; const lfoprops = { - time: begin, shape: getModulationShapeInput(shape), ...props, }; - return getWorklet(audioContext, 'lfo-processor', lfoprops); } diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 44304e3c3..0a091b156 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -13,12 +13,10 @@ import { createFilter, effectSend, gainNode, - getADSRValues, getCompressor, getDistortion, getEnvelope, getLfo, - getParamADSR, getWorklet, webAudioTimeout, } from './helpers.mjs'; @@ -365,7 +363,6 @@ export function resetGlobalEffects() { controller?.reset(); analysers = {}; analysersData = {}; - idToNodes = {}; } function _getNodeParam(node, name) { @@ -455,12 +452,15 @@ function _getTargetParams(nodes, target) { } function connectLFO(idx, params, nodeTracker) { - const { rate = 1, sync, cps, target, ...filteredParams } = params; + // TODO: figure out min/max values and how to handle depth and depthabs here. + const { rate = 1, sync, cps, cycle, target, ...filteredParams } = params; filteredParams['frequency'] = sync !== undefined ? sync / cps : rate; + filteredParams['time'] = cycle / cps; const ac = getAudioContext(); const lfoNode = getLfo(ac, filteredParams); nodeTracker[`lfo${idx}`] = [lfoNode]; _getTargetParams(nodeTracker, target).forEach((t) => lfoNode.connect(t)); + return lfoNode; } function connectEnvelope(idx, params, nodeTracker) { @@ -474,10 +474,12 @@ function connectEnvelope(idx, params, nodeTracker) { }); nodeTracker[`env${idx}`] = [envNode]; _getTargetParams(nodeTracker, target).forEach((t) => envNode.connect(t)); + return envNode; } -function connectSendModulator(params, signal, nodeTracker, chainID) { +function connectOrbitModulator(params, nodeTracker) { const ac = getAudioContext(); + const signal = controller.getOrbit(params.orbit).output; const dc = new ConstantSourceNode(ac, { offset: params.dc ?? 0 }); dc.start(params.begin); const shifted = dc.connect(gainNode(1)); @@ -491,15 +493,6 @@ function connectSendModulator(params, signal, nodeTracker, chainID) { 0, params.begin, ); - webAudioTimeout( - ac, - () => { - modulator.disconnect(); - delete pendingConnections[params.id][chainID]; - }, - 0, - params.end + 0.05, - ); return { modulator, nodes: [dc, shifted, modulator] }; } @@ -510,8 +503,6 @@ function mapChannelNumbers(channels) { return (Array.isArray(channels) ? channels : [channels]).map((ch) => ch - 1); } -let idToNodes = {}; -let pendingConnections = {}; export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) => { const nodes = {}; // new: t is always expected to be the absolute target onset time @@ -641,7 +632,7 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) activeSoundSources.delete(chainID); } - let audioNodes = []; + const audioNodes = []; if (['-', '~', '_'].includes(s)) { return; @@ -939,26 +930,28 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) // connect chain elements together chain.slice(1).reduce((last, current) => last.connect(current), chain[0]); - audioNodes = audioNodes.concat(chain); + audioNodes.push(...chain); // finally, now that `nodes` is populated, set up modulators if (value.lfo) { for (const [idx, params] of Object.entries(value.lfo)) { - connectLFO( + const lfo = connectLFO( idx, { ...params, cps, + cycle, begin: t, end: endWithRelease, }, nodes, ); + audioNodes.push(lfo); } } if (value.env) { for (const [idx, params] of Object.entries(value.env)) { - connectEnvelope( + const env = connectEnvelope( idx, { ...params, @@ -967,57 +960,17 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) }, nodes, ); + audioNodes.push(env); } } - if (value.id) { - idToNodes[value.id] = new WeakRef(nodes); - } - if (value.send) { - for (const p of value.send) { - const modNodes = idToNodes[p.id].deref(); - if (!modNodes) { - logger( - `[superdough] Could not connect to pattern ${p.id} -- make sure a pattern with this name exists. Available targets: ${Object.keys(idToNodes).join(', ')}`, - ); - delete idToNodes[p.id]; - } else { - const { modulator, nodes: nodesToCleanup } = connectSendModulator( - { ...p, begin: t, end: endWithRelease }, - post, - modNodes, - chainID, - ); - audioNodes = audioNodes.concat(nodesToCleanup); - pendingConnections[p.id] ??= {}; - pendingConnections[p.id][chainID] = new WeakRef([modulator, p.target, endWithRelease]); - } - } - } - if (value.id in pendingConnections) { - for (const data of Object.values(pendingConnections[value.id])) { - const derefData = data.deref(); - if (!derefData) { - delete pendingConnections[value.id]; - continue; - } - const [modulator, target, end] = derefData; - const targets = _getTargetParams(nodes, target); - webAudioTimeout( - ac, - () => { - targets.forEach((target) => modulator.connect(target)); - }, - 0, - t, - ); - webAudioTimeout( - ac, - () => { - modulator.disconnect(); - }, - 0, - end, + if (value.omod) { + for (const p of value.omod) { + const { nodes: nodesToCleanup } = connectOrbitModulator( + { ...p, begin: t, end: endWithRelease }, + nodes, + chainID, ); + audioNodes.push(...nodesToCleanup); } } };