From 020a6bc75fb1467a518c32c2d132298747693d8a Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 23 Nov 2025 20:31:31 -0600 Subject: [PATCH 1/7] First pass at transient processor --- packages/core/controls.mjs | 2 + packages/superdough/worklets.mjs | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index 809a45ab2..80a244efc 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -2584,3 +2584,5 @@ export const scrub = register( }, false, ); + +export const { transient } = registerControl(['transient', 'transsustain', 'transattack']); diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index bf633a63b..334e2d829 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -11,6 +11,9 @@ const PI = Math.PI; const TWO_PI = 2 * PI; const INVSR = 1 / sampleRate; +const timeToCoeff = (t) => Math.exp(-INVSR / t); +const dbToLin = (db) => Math.pow(10, db / 20); + const clamp = (num, min, max) => Math.min(Math.max(num, min), max); const mod = (n, m) => ((n % m) + m) % m; const lerp = (a, b, n) => n * (b - a) + a; @@ -1383,3 +1386,70 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor { } registerProcessor('wavetable-oscillator-processor', WavetableOscillatorProcessor); + +class TransientProcessor extends AudioWorkletProcessor { + static get parameterDescriptors() { + return []; + } + + constructor(options) { + super(); + this.gainCoeff = timeToCoeff(0.2); + this.avgGain = 1; + let { + attackTime = 0.003, + sustainTime = 0.08, + attack = 0, + sustain = 0, + sensitivity = 0.1, + mix = 1, + } = options.processorOptions; + attackTime = clamp(attackTime, 0.0005, 0.05); + sustainTime = clamp(sustainTime, 0.01, 0.5); + this.attackCoeff = timeToCoeff(attackTime); + this.sustainCoeff = timeToCoeff(sustainTime); + this.attackAmt = clamp(attack, -1, 1); + this.sustainAmt = clamp(sustain, -1, 1); + this.scaling = 0.5 + 5 * clamp(sensitivity, 0, 1); + this.mix = clamp(mix, 0, 1); + } + + process(inputs, outputs, _params) { + const input = inputs[0]; + const output = outputs[0]; + if (!input || !output) { + return true; + } + const channels = output.length; + this.attackEnv ??= new Float32Array(channels); + this.sustainEnv ??= new Float32Array(channels); + let avgGain = this.avgGain; + for (let ch = 0; ch < channels; ch++) { + const inCh = input[ch]; + const outCh = output[ch]; + let attEnv = this.attackEnv[ch]; + let susEnv = this.sustainEnv[ch]; + for (let i = 0; i < blockSize; i++) { + const x = Math.abs(inCh[i]); + attEnv = lerp(attEnv, x, this.attackCoeff); + susEnv = lerp(susEnv, x, this.sustainCoeff); + const peakiness = clamp((this.scaling * (attEnv - susEnv)) / (susEnv + 1e-6), -1.5, 1.5); + const attScale = Math.max(0, peakiness); + const susScale = Math.max(0, -peakiness); + const attackGain = dbToLin(this.attackAmt * attScale * 18); // max 18db + const sustainGain = dbToLin(this.sustainAmt * susScale * 36); // max 36db + const gain = clamp(attackGain * sustainGain, 0, 8); + avgGain = lerp(avgGain, gain, this.gainCoeff); + const makeup = avgGain > 1e-3 ? 1 / avgGain : 1; + const wet = x * gain * makeup; + outCh[i] = lerp(x, wet, this.mix); + } + this.attackEnv[ch] = attEnv; + this.sustainEnv[ch] = susEnv; + } + this.avgGain = avgGain; + return true; + } +} + +registerProcessor('transient-processor', TransientProcessor); From 68840de188622e302276aa7dd31f8c895e6c6887 Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 23 Nov 2025 20:58:28 -0600 Subject: [PATCH 2/7] Working version --- packages/superdough/superdough.mjs | 19 +++++++++++++++++++ packages/superdough/worklets.mjs | 13 ++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 650f9c2ce..acf6edef2 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -451,6 +451,9 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) compressorKnee, compressorAttack, compressorRelease, + transient, + transsustain, + transattack, } = value; delaytime = delaytime ?? cycleToSeconds(delaysync, cps); @@ -532,6 +535,22 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) chain.push(sourceNode); stretch !== undefined && chain.push(getWorklet(ac, 'phase-vocoder-processor', { pitchFactor: stretch })); + transient !== undefined && + chain.push( + getWorklet( + ac, + 'transient-processor', + {}, + { + processorOptions: { + attack: transient, + sustain: transsustain, + attackTime: transattack, + }, + }, + ), + ); + // gain stage chain.push(gainNode(gain)); diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 334e2d829..6af7dffc1 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -11,7 +11,7 @@ const PI = Math.PI; const TWO_PI = 2 * PI; const INVSR = 1 / sampleRate; -const timeToCoeff = (t) => Math.exp(-INVSR / t); +const timeToCoeff = (t) => 1 - Math.exp(-INVSR / t); const dbToLin = (db) => Math.pow(10, db / 20); const clamp = (num, min, max) => Math.min(Math.max(num, min), max); @@ -1420,17 +1420,16 @@ class TransientProcessor extends AudioWorkletProcessor { if (!input || !output) { return true; } - const channels = output.length; + const channels = input.length; this.attackEnv ??= new Float32Array(channels); this.sustainEnv ??= new Float32Array(channels); let avgGain = this.avgGain; for (let ch = 0; ch < channels; ch++) { - const inCh = input[ch]; - const outCh = output[ch]; let attEnv = this.attackEnv[ch]; let susEnv = this.sustainEnv[ch]; - for (let i = 0; i < blockSize; i++) { - const x = Math.abs(inCh[i]); + for (let n = 0; n < blockSize; n++) { + const sample = input[ch][n]; + const x = Math.abs(sample); attEnv = lerp(attEnv, x, this.attackCoeff); susEnv = lerp(susEnv, x, this.sustainCoeff); const peakiness = clamp((this.scaling * (attEnv - susEnv)) / (susEnv + 1e-6), -1.5, 1.5); @@ -1442,7 +1441,7 @@ class TransientProcessor extends AudioWorkletProcessor { avgGain = lerp(avgGain, gain, this.gainCoeff); const makeup = avgGain > 1e-3 ? 1 / avgGain : 1; const wet = x * gain * makeup; - outCh[i] = lerp(x, wet, this.mix); + output[ch][n] = lerp(sample, wet, this.mix); } this.attackEnv[ch] = attEnv; this.sustainEnv[ch] = susEnv; From df047fd48dbb4df27ca596f2ca085eeb2fb86c4d Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 23 Nov 2025 22:28:10 -0600 Subject: [PATCH 3/7] Optimizations --- packages/core/controls.mjs | 14 +++++++++++++- packages/superdough/superdough.mjs | 2 -- packages/superdough/worklets.mjs | 16 +++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index 80a244efc..bca7036ad 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -2585,4 +2585,16 @@ export const scrub = register( false, ); -export const { transient } = registerControl(['transient', 'transsustain', 'transattack']); +/** + * Transient shaper. Gives independent control over the emphasis on transients + * and sustains + * + * @name transient + * @param {number | Pattern} attack Emphasis on transients; between -1 (deaccentuate) and 1 (accentuate) + * @param {number | Pattern} sustain Emphasis on the sustains; between -1 (deaccentuate) and 1 (accentuate) + * @example + * s("bd").transient("<-1 -0.5 0 0.5 1>") + * @example + * s("hh*16").bank("tr909").transient("<-1:1 1:-1>") + */ +export const { transient } = registerControl(['transient', 'transsustain']); diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index acf6edef2..ac6ca7322 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -453,7 +453,6 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) compressorRelease, transient, transsustain, - transattack, } = value; delaytime = delaytime ?? cycleToSeconds(delaysync, cps); @@ -545,7 +544,6 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) processorOptions: { attack: transient, sustain: transsustain, - attackTime: transattack, }, }, ), diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 6af7dffc1..6b370166c 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1412,6 +1412,10 @@ class TransientProcessor extends AudioWorkletProcessor { this.sustainAmt = clamp(sustain, -1, 1); this.scaling = 0.5 + 5 * clamp(sensitivity, 0, 1); this.mix = clamp(mix, 0, 1); + const maxAttackDb = this.attackAmt * 6; + const maxSustainDb = this.sustainAmt * 36; + this.attackMaxGain = dbToLin(maxAttackDb) - 1; // increasing from 1 + this.sustainMaxGain = dbToLin(maxSustainDb) - 1; } process(inputs, outputs, _params) { @@ -1433,15 +1437,17 @@ class TransientProcessor extends AudioWorkletProcessor { attEnv = lerp(attEnv, x, this.attackCoeff); susEnv = lerp(susEnv, x, this.sustainCoeff); const peakiness = clamp((this.scaling * (attEnv - susEnv)) / (susEnv + 1e-6), -1.5, 1.5); - const attScale = Math.max(0, peakiness); - const susScale = Math.max(0, -peakiness); - const attackGain = dbToLin(this.attackAmt * attScale * 18); // max 18db - const sustainGain = dbToLin(this.sustainAmt * susScale * 36); // max 36db + const attScale = peakiness > 0 ? peakiness : 0; + const susScale = peakiness < 0 ? -peakiness : 0; + const attackGain = 1 + attScale * this.attackMaxGain; + const sustainGain = 1 + susScale * this.sustainMaxGain; const gain = clamp(attackGain * sustainGain, 0, 8); avgGain = lerp(avgGain, gain, this.gainCoeff); const makeup = avgGain > 1e-3 ? 1 / avgGain : 1; const wet = x * gain * makeup; - output[ch][n] = lerp(sample, wet, this.mix); + let y = lerp(sample, wet, this.mix); + y /= (1 + Math.abs(y)); // soft clip + output[ch][n] = y; } this.attackEnv[ch] = attEnv; this.sustainEnv[ch] = susEnv; From be36898803a59f201d3d3db4e79bf35a2cc53edb Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 23 Nov 2025 22:29:33 -0600 Subject: [PATCH 4/7] Formatting and examples --- packages/superdough/worklets.mjs | 2 +- test/__snapshots__/examples.test.mjs.snap | 78 +++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 6b370166c..e5dd0a0d7 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1446,7 +1446,7 @@ class TransientProcessor extends AudioWorkletProcessor { const makeup = avgGain > 1e-3 ? 1 / avgGain : 1; const wet = x * gain * makeup; let y = lerp(sample, wet, this.mix); - y /= (1 + Math.abs(y)); // soft clip + y /= 1 + Math.abs(y); // soft clip output[ch][n] = y; } this.attackEnv[ch] = attEnv; diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index 31e4bddd3..66777d5e1 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -11410,6 +11410,84 @@ exports[`runs examples > example "tour" example index 0 1`] = ` ] `; +exports[`runs examples > example "transient" example index 0 1`] = ` +[ + "[ 0/1 → 1/1 | s:bd transient:-1 ]", + "[ 1/1 → 2/1 | s:bd transient:-0.5 ]", + "[ 2/1 → 3/1 | s:bd transient:0 ]", + "[ 3/1 → 4/1 | s:bd transient:0.5 ]", +] +`; + +exports[`runs examples > example "transient" example index 1 1`] = ` +[ + "[ 0/1 → 1/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 1/16 → 1/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 1/8 → 3/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 3/16 → 1/4 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 1/4 → 5/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 5/16 → 3/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 3/8 → 7/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 7/16 → 1/2 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 1/2 → 9/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 9/16 → 5/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 5/8 → 11/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 11/16 → 3/4 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 3/4 → 13/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 13/16 → 7/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 7/8 → 15/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 15/16 → 1/1 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 1/1 → 17/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 17/16 → 9/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 9/8 → 19/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 19/16 → 5/4 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 5/4 → 21/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 21/16 → 11/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 11/8 → 23/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 23/16 → 3/2 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 3/2 → 25/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 25/16 → 13/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 13/8 → 27/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 27/16 → 7/4 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 7/4 → 29/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 29/16 → 15/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 15/8 → 31/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 31/16 → 2/1 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 2/1 → 33/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 33/16 → 17/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 17/8 → 35/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 35/16 → 9/4 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 9/4 → 37/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 37/16 → 19/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 19/8 → 39/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 39/16 → 5/2 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 5/2 → 41/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 41/16 → 21/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 21/8 → 43/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 43/16 → 11/4 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 11/4 → 45/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 45/16 → 23/8 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 23/8 → 47/16 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 47/16 → 3/1 | s:hh bank:tr909 transient:-1 transsustain:1 ]", + "[ 3/1 → 49/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 49/16 → 25/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 25/8 → 51/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 51/16 → 13/4 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 13/4 → 53/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 53/16 → 27/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 27/8 → 55/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 55/16 → 7/2 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 7/2 → 57/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 57/16 → 29/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 29/8 → 59/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 59/16 → 15/4 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 15/4 → 61/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 61/16 → 31/8 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 31/8 → 63/16 | s:hh bank:tr909 transient:1 transsustain:-1 ]", + "[ 63/16 → 4/1 | s:hh bank:tr909 transient:1 transsustain:-1 ]", +] +`; + exports[`runs examples > example "transpose" example index 0 1`] = ` [ "[ 0/1 → 1/4 | note:C2 ]", From 6c01c16a0cbd4dda8f4fe6081746824d79972701 Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 23 Nov 2025 22:39:21 -0600 Subject: [PATCH 5/7] Fix typo --- packages/superdough/worklets.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index e5dd0a0d7..67d5e1641 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1436,7 +1436,7 @@ class TransientProcessor extends AudioWorkletProcessor { const x = Math.abs(sample); attEnv = lerp(attEnv, x, this.attackCoeff); susEnv = lerp(susEnv, x, this.sustainCoeff); - const peakiness = clamp((this.scaling * (attEnv - susEnv)) / (susEnv + 1e-6), -1.5, 1.5); + const peakiness = clamp((this.scaling * (attEnv - susEnv)) / (susEnv + 1e-6), -1, 1); const attScale = peakiness > 0 ? peakiness : 0; const susScale = peakiness < 0 ? -peakiness : 0; const attackGain = 1 + attScale * this.attackMaxGain; @@ -1444,7 +1444,7 @@ class TransientProcessor extends AudioWorkletProcessor { const gain = clamp(attackGain * sustainGain, 0, 8); avgGain = lerp(avgGain, gain, this.gainCoeff); const makeup = avgGain > 1e-3 ? 1 / avgGain : 1; - const wet = x * gain * makeup; + const wet = sample * gain * makeup; let y = lerp(sample, wet, this.mix); y /= 1 + Math.abs(y); // soft clip output[ch][n] = y; From 36587ae74732fa69f58704b2a0f3a0ebfbf8e96a Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 23 Nov 2025 22:55:36 -0600 Subject: [PATCH 6/7] Switch back to original version --- packages/superdough/worklets.mjs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 67d5e1641..d88e9dea2 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1412,10 +1412,6 @@ class TransientProcessor extends AudioWorkletProcessor { this.sustainAmt = clamp(sustain, -1, 1); this.scaling = 0.5 + 5 * clamp(sensitivity, 0, 1); this.mix = clamp(mix, 0, 1); - const maxAttackDb = this.attackAmt * 6; - const maxSustainDb = this.sustainAmt * 36; - this.attackMaxGain = dbToLin(maxAttackDb) - 1; // increasing from 1 - this.sustainMaxGain = dbToLin(maxSustainDb) - 1; } process(inputs, outputs, _params) { @@ -1436,11 +1432,11 @@ class TransientProcessor extends AudioWorkletProcessor { const x = Math.abs(sample); attEnv = lerp(attEnv, x, this.attackCoeff); susEnv = lerp(susEnv, x, this.sustainCoeff); - const peakiness = clamp((this.scaling * (attEnv - susEnv)) / (susEnv + 1e-6), -1, 1); + const peakiness = clamp((this.scaling * (attEnv - susEnv)) / (susEnv + 1e-6), -1.5, 1.5); const attScale = peakiness > 0 ? peakiness : 0; const susScale = peakiness < 0 ? -peakiness : 0; - const attackGain = 1 + attScale * this.attackMaxGain; - const sustainGain = 1 + susScale * this.sustainMaxGain; + const attackGain = dbToLin(this.attackAmt * attScale * 18); + const sustainGain = dbToLin(this.sustainAmt * susScale * 36); const gain = clamp(attackGain * sustainGain, 0, 8); avgGain = lerp(avgGain, gain, this.gainCoeff); const makeup = avgGain > 1e-3 ? 1 / avgGain : 1; From 358b1e3f563ffe6bafd33689b1fec8e7c02c4ecc Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 7 Dec 2025 13:28:57 -0600 Subject: [PATCH 7/7] Make sure processor turns off; fix array sizes for JIT --- packages/superdough/superdough.mjs | 2 ++ packages/superdough/worklets.mjs | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 52bf332e3..b4df1f734 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -544,6 +544,8 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) processorOptions: { attack: transient, sustain: transsustain, + begin: t, + end: endWithRelease, }, }, ), diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index d190cf41f..a59aeb756 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1404,6 +1404,8 @@ class TransientProcessor extends AudioWorkletProcessor { sustain = 0, sensitivity = 0.1, mix = 1, + begin = 0, + end = 0, } = options.processorOptions; attackTime = clamp(attackTime, 0.0005, 0.05); sustainTime = clamp(sustainTime, 0.01, 0.5); @@ -1413,17 +1415,26 @@ class TransientProcessor extends AudioWorkletProcessor { this.sustainAmt = clamp(sustain, -1, 1); this.scaling = 0.5 + 5 * clamp(sensitivity, 0, 1); this.mix = clamp(mix, 0, 1); + this.begin = begin; + this.end = end; + this.attackEnv = new Float32Array(2); // assume stereo + this.sustainEnv = new Float32Array(2); } process(inputs, outputs, _params) { const input = inputs[0]; const output = outputs[0]; - if (!input || !output) { + if (currentTime >= this.end) { + return false; + } + if (currentTime <= this.begin) { return true; } const channels = input.length; - this.attackEnv ??= new Float32Array(channels); - this.sustainEnv ??= new Float32Array(channels); + if (channels > this.attackEnv.length) { + this.attackEnv = new Float32Array(channels); + this.sustainEnv = new Float32Array(channels); + } let avgGain = this.avgGain; for (let ch = 0; ch < channels; ch++) { let attEnv = this.attackEnv[ch];