Move algorithm into constructor

This commit is contained in:
Aria
2025-09-24 17:32:03 -07:00
parent 69b034349a
commit f0669ce3ba
2 changed files with 10 additions and 6 deletions
+7 -2
View File
@@ -403,6 +403,11 @@ export const getDistortionAlgorithm = (algo) => {
};
export const getDistortion = (distort, postgain, algorithm) => {
const { _algorithm, index } = getDistortionAlgorithm(algorithm);
return getWorklet(getAudioContext(), 'distort-processor', { distort, postgain, algorithm: index });
const { index } = getDistortionAlgorithm(algorithm);
return getWorklet(
getAudioContext(),
'distort-processor',
{ distort, postgain },
{ processorOptions: { algorithm: index } },
);
};
+3 -4
View File
@@ -348,13 +348,13 @@ class DistortProcessor extends AudioWorkletProcessor {
return [
{ name: 'distort', defaultValue: 0 },
{ name: 'postgain', defaultValue: 1 },
{ name: 'algorithm', defaultValue: 0, min: 0 },
];
}
constructor() {
constructor({ processorOptions }) {
super();
this.started = false;
this.algorithm = getDistortionAlgorithm(processorOptions.algorithm).algorithm;
}
process(inputs, outputs, parameters) {
@@ -366,13 +366,12 @@ class DistortProcessor extends AudioWorkletProcessor {
return false;
}
this.started = hasInput;
const { algorithm } = getDistortionAlgorithm(pv(parameters.algorithm, 0)); // do not allow audio rate algo changes
for (let n = 0; n < blockSize; n++) {
const postgain = clamp(pv(parameters.postgain, n), 0.001, 1);
const shape = Math.expm1(pv(parameters.distort, n));
for (let ch = 0; ch < input.length; ch++) {
const x = input[ch][n];
output[ch][n] = postgain * algorithm(x, shape);
output[ch][n] = postgain * this.algorithm(x, shape);
}
}
return true;