This commit is contained in:
Jade (Rose) Rowland 2024-02-27 23:38:37 -05:00
parent dd64bf0fc9
commit 1416a0de53
2 changed files with 47 additions and 66 deletions

View File

@ -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(

View File

@ -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;
});
}
}