From 1416a0de5386099f23af796fc5b159150b73603b Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Tue, 27 Feb 2024 23:38:37 -0500 Subject: [PATCH] working --- packages/superdough/superdough.mjs | 3 +- packages/superdough/worklets.mjs | 110 ++++++++++++----------------- 2 files changed, 47 insertions(+), 66 deletions(-) diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index f5674f2c..2eb42720 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -452,7 +452,8 @@ export const superdough = async (value, deadline, hapDuration) => { // effects coarse !== undefined && chain.push(getWorklet(ac, 'coarse-processor', { coarse })); crush !== undefined && chain.push(getWorklet(ac, 'crush-processor', { crush })); - shape !== undefined && chain.push(getWorklet(ac, 'shape-processor', { shape })); + const shapeInput = Array.isArray(shape) ? { shape: shape[0], postgain: shape[1] } : { shape }; + shape !== undefined && chain.push(getWorklet(ac, 'shape-processor', shapeInput)); compressorThreshold !== undefined && chain.push( diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 7bb43f87..43e58e33 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1,7 +1,23 @@ -// LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE -// all the credit goes to dktr0's webdirt: https://github.com/dktr0/WebDirt/blob/5ce3d698362c54d6e1b68acc47eb2955ac62c793/dist/AudioWorklets.js -// <3 +const processSample = (inputs, outputs, processBlock) => { + const input = inputs[0]; + const output = outputs[0]; + const blockSize = 128; + if (input == null || output == null) { + return false; + } + for (let n = 0; n < blockSize; n++) { + input.forEach((inChannel, i) => { + const outChannel = output[i % output.length]; + const block = inChannel[n]; + outChannel[n] = processBlock(block, n, inChannel, outChannel); + }); + } + return true; +}; + +// coarse, crush, and shape processors adapted from dktr0's webdirt: https://github.com/dktr0/WebDirt/blob/5ce3d698362c54d6e1b68acc47eb2955ac62c793/dist/AudioWorklets.js +// LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE class CoarseProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { return [{ name: 'coarse', defaultValue: 1 }]; @@ -9,25 +25,15 @@ class CoarseProcessor extends AudioWorkletProcessor { constructor() { super(); - this.notStarted = true; } process(inputs, outputs, parameters) { - const input = inputs[0]; - const output = outputs[0]; - const coarse = parameters.coarse; - const blockSize = 128; - const hasInput = !(input[0] === undefined); - if (hasInput) { - this.notStarted = false; - output[0][0] = input[0][0]; - for (let n = 1; n < blockSize; n++) { - for (let o = 0; o < output.length; o++) { - output[o][n] = n % coarse == 0 ? input[0][n] : output[o][n - 1]; - } - } - } - return this.notStarted || hasInput; + let coarse = parameters.coarse[0] ?? 0; + coarse = Math.min(128, Math.max(1, Math.round(coarse * 128))); + return processSample(inputs, outputs, (block, n, inChannel, outChannel) => { + const value = n % coarse === 0 ? block : outChannel[n - 1]; + return value; + }); } } @@ -40,68 +46,42 @@ class CrushProcessor extends AudioWorkletProcessor { constructor() { super(); - this.notStarted = true; } process(inputs, outputs, parameters) { - const input = inputs[0]; - const output = outputs[0]; - const crush = parameters.crush; - const blockSize = 128; - const hasInput = !(input[0] === undefined); - if (hasInput) { - this.notStarted = false; - if (crush.length === 1) { - const x = Math.pow(2, crush[0] - 1); - for (let n = 0; n < blockSize; n++) { - const value = Math.round(input[0][n] * x) / x; - for (let o = 0; o < output.length; o++) { - output[o][n] = value; - } - } - } else { - for (let n = 0; n < blockSize; n++) { - let x = Math.pow(2, crush[n] - 1); - const value = Math.round(input[0][n] * x) / x; - for (let o = 0; o < output.length; o++) { - output[o][n] = value; - } - } - } - } - return this.notStarted || hasInput; + const bitMax = 16; + const bitMin = 1; + let crush = parameters.crush[0] ?? 8; + crush = Math.max(bitMin, bitMax - crush * bitMax); + + return processSample(inputs, outputs, (block) => { + const x = Math.pow(2, crush - 1); + return Math.round(block * x) / x; + }); } } registerProcessor('crush-processor', CrushProcessor); class ShapeProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { - return [{ name: 'shape', defaultValue: 0 }]; + return [ + { name: 'shape', defaultValue: 0 }, + { name: 'postgain', defaultValue: 1 }, + ]; } constructor() { super(); - this.notStarted = true; } process(inputs, outputs, parameters) { - const input = inputs[0]; - const output = outputs[0]; - const shape0 = parameters.shape[0]; - const shape1 = shape0 < 1 ? shape0 : 1.0 - 4e-10; - const shape = (2.0 * shape1) / (1.0 - shape1); - const blockSize = 128; - const hasInput = !(input[0] === undefined); - if (hasInput) { - this.notStarted = false; - for (let n = 0; n < blockSize; n++) { - const value = ((1 + shape) * input[0][n]) / (1 + shape * Math.abs(input[0][n])); - for (let o = 0; o < output.length; o++) { - output[o][n] = value; - } - } - } - return this.notStarted || hasInput; + let shape_param = parameters.shape[0]; + const postgain = Math.max(0.001, Math.min(1, parameters.postgain[0])); + const shape = shape_param * 100; + return processSample(inputs, outputs, (block, n) => { + const val = ((1 + shape) * block) / (1 + shape * Math.abs(block)); + return val * postgain; + }); } }