Code format and a typo on lfoNum
This commit is contained in:
parent
d8f0d70908
commit
6a9739bed5
2 changed files with 69 additions and 66 deletions
|
|
@ -9,7 +9,15 @@ import './reverb.mjs';
|
||||||
import './vowel.mjs';
|
import './vowel.mjs';
|
||||||
import { clamp, nanFallback, _mod, cycleToSeconds, secondsToCycle } from './util.mjs';
|
import { clamp, nanFallback, _mod, cycleToSeconds, secondsToCycle } from './util.mjs';
|
||||||
import workletsUrl from './worklets.mjs?audioworklet';
|
import workletsUrl from './worklets.mjs?audioworklet';
|
||||||
import { createFilter, gainNode, getCompressor, getWorklet, webAudioTimeout, getADSRValues, getParamADSR } from './helpers.mjs';
|
import {
|
||||||
|
createFilter,
|
||||||
|
gainNode,
|
||||||
|
getCompressor,
|
||||||
|
getWorklet,
|
||||||
|
webAudioTimeout,
|
||||||
|
getADSRValues,
|
||||||
|
getParamADSR,
|
||||||
|
} from './helpers.mjs';
|
||||||
import { map } from 'nanostores';
|
import { map } from 'nanostores';
|
||||||
import { logger, errorLogger } from './logger.mjs';
|
import { logger, errorLogger } from './logger.mjs';
|
||||||
import { loadBuffer } from './sampler.mjs';
|
import { loadBuffer } from './sampler.mjs';
|
||||||
|
|
@ -527,8 +535,9 @@ function _getNodeParams(node) {
|
||||||
node.parameters.forEach((_v, k) => params.add(k));
|
node.parameters.forEach((_v, k) => params.add(k));
|
||||||
}
|
}
|
||||||
// Guesses based on common parameters
|
// Guesses based on common parameters
|
||||||
["gain", "frequency", "detune", "Q", "pan", "playbackRate", "delayTime"]
|
['gain', 'frequency', 'detune', 'Q', 'pan', 'playbackRate', 'delayTime'].forEach((k) => {
|
||||||
.forEach((k) => { if (node?.[k] instanceof AudioParam) params.add(k); });
|
if (node?.[k] instanceof AudioParam) params.add(k);
|
||||||
|
});
|
||||||
return Array.from(params);
|
return Array.from(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -540,7 +549,7 @@ function _getNodeParams(node) {
|
||||||
* @returns {Object[]} - Array of parameter objects, one per parameter modulation
|
* @returns {Object[]} - Array of parameter objects, one per parameter modulation
|
||||||
*/
|
*/
|
||||||
function _splitParams(params, countKeys) {
|
function _splitParams(params, countKeys) {
|
||||||
const num = ["num", "target", "parameter"] // names used to indicate individual parameter modulations
|
const num = ['num', 'target', 'parameter'] // names used to indicate individual parameter modulations
|
||||||
.map((k) => [params[k] ?? 0].flat().length)
|
.map((k) => [params[k] ?? 0].flat().length)
|
||||||
.reduce((a, v) => Math.max(a, v), 1);
|
.reduce((a, v) => Math.max(a, v), 1);
|
||||||
|
|
||||||
|
|
@ -552,9 +561,9 @@ function _splitParams(params, countKeys) {
|
||||||
if (flatV.length !== num && flatV.length !== 1) {
|
if (flatV.length !== num && flatV.length !== 1) {
|
||||||
errorLogger(
|
errorLogger(
|
||||||
new Error(
|
new Error(
|
||||||
`Could not set up modulations. We derived ${num} items, but ${k}: ${JSON.stringify(flatV)} has length ${flatV.length} (needs 1 or ${num}).`
|
`Could not set up modulations. We derived ${num} items, but ${k}: ${JSON.stringify(flatV)} has length ${flatV.length} (needs 1 or ${num}).`,
|
||||||
),
|
),
|
||||||
'superdough'
|
'superdough',
|
||||||
);
|
);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
@ -582,8 +591,10 @@ function _getTargetParams(targetName, paramName) {
|
||||||
if (!targetNodes) {
|
if (!targetNodes) {
|
||||||
const keys = Object.keys(nodes);
|
const keys = Object.keys(nodes);
|
||||||
errorLogger(
|
errorLogger(
|
||||||
new Error(`Could not connect to target '${targetName}' — it does not exist. Available targets: ${keys.join(", ")}`),
|
new Error(
|
||||||
'superdough'
|
`Could not connect to target '${targetName}' — it does not exist. Available targets: ${keys.join(', ')}`,
|
||||||
|
),
|
||||||
|
'superdough',
|
||||||
);
|
);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
@ -595,9 +606,9 @@ function _getTargetParams(targetName, paramName) {
|
||||||
const available = _getNodeParams(targetNode);
|
const available = _getNodeParams(targetNode);
|
||||||
errorLogger(
|
errorLogger(
|
||||||
new Error(
|
new Error(
|
||||||
`Could not connect to parameter '${paramName}' on '${targetName}'. Available parameters: ${available.join(", ")}`
|
`Could not connect to parameter '${paramName}' on '${targetName}'. Available parameters: ${available.join(', ')}`,
|
||||||
),
|
),
|
||||||
'superdough'
|
'superdough',
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -629,7 +640,7 @@ function _connectLFO(params) {
|
||||||
...filteredParams
|
...filteredParams
|
||||||
} = params;
|
} = params;
|
||||||
filteredParams['frequency'] = synced ? frequency / cps : frequency;
|
filteredParams['frequency'] = synced ? frequency / cps : frequency;
|
||||||
let lfoNode = lfos[lfoNum];
|
let lfoNode = lfos[num];
|
||||||
if (lfoNode == null) {
|
if (lfoNode == null) {
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
lfoNode = getLfo(ac, filteredParams);
|
lfoNode = getLfo(ac, filteredParams);
|
||||||
|
|
@ -646,30 +657,9 @@ function _connectLFO(params) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function _connectEnvelope(params) {
|
function _connectEnvelope(params) {
|
||||||
const {
|
const { target, param, envDepth, begin, end, attack, decay, sustain, release, curve, ...filteredParams } = params;
|
||||||
target,
|
|
||||||
param,
|
|
||||||
envDepth,
|
|
||||||
begin,
|
|
||||||
end,
|
|
||||||
attack,
|
|
||||||
decay,
|
|
||||||
sustain,
|
|
||||||
release,
|
|
||||||
curve,
|
|
||||||
...filteredParams
|
|
||||||
} = params;
|
|
||||||
const targets = _getTargetParams(target, param);
|
const targets = _getTargetParams(target, param);
|
||||||
const [att, dec, sus, rel] = getADSRValues(
|
const [att, dec, sus, rel] = getADSRValues([attack, decay, sustain, release], curve, [0.005, 0.14, 0, 0.1]);
|
||||||
[
|
|
||||||
attack,
|
|
||||||
decay,
|
|
||||||
sustain,
|
|
||||||
release,
|
|
||||||
],
|
|
||||||
curve,
|
|
||||||
[0.005, 0.14, 0, 0.1]
|
|
||||||
);
|
|
||||||
targets.forEach((targetParam) => {
|
targets.forEach((targetParam) => {
|
||||||
const currentValue = targetParam.value;
|
const currentValue = targetParam.value;
|
||||||
const min = currentValue;
|
const min = currentValue;
|
||||||
|
|
@ -682,9 +672,9 @@ function connectModulators(params, modulatorType) {
|
||||||
// We break down params specifying multiple modulators into a set of parameters for
|
// We break down params specifying multiple modulators into a set of parameters for
|
||||||
// a single one
|
// a single one
|
||||||
const individualParams = _splitParams(params);
|
const individualParams = _splitParams(params);
|
||||||
if (modulatorType === "lfo") {
|
if (modulatorType === 'lfo') {
|
||||||
individualParams.forEach(_connectLFO);
|
individualParams.forEach(_connectLFO);
|
||||||
} else if (modulatorType === "envelope") {
|
} else if (modulatorType === 'envelope') {
|
||||||
individualParams.forEach(_connectEnvelope);
|
individualParams.forEach(_connectEnvelope);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1041,7 +1031,14 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compressorThreshold !== undefined) {
|
if (compressorThreshold !== undefined) {
|
||||||
const compressorNode = getCompressor(ac, compressorThreshold, compressorRatio, compressorKnee, compressorAttack, compressorRelease);
|
const compressorNode = getCompressor(
|
||||||
|
ac,
|
||||||
|
compressorThreshold,
|
||||||
|
compressorRatio,
|
||||||
|
compressorKnee,
|
||||||
|
compressorAttack,
|
||||||
|
compressorRelease,
|
||||||
|
);
|
||||||
nodes['compressor'] = compressorNode;
|
nodes['compressor'] = compressorNode;
|
||||||
chain.push(compressorNode);
|
chain.push(compressorNode);
|
||||||
}
|
}
|
||||||
|
|
@ -1114,35 +1111,41 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
||||||
|
|
||||||
// finally, now that `nodes` is populated, set up LFOs and envelopes
|
// finally, now that `nodes` is populated, set up LFOs and envelopes
|
||||||
if (lfoTarget !== undefined && lfoParam !== undefined) {
|
if (lfoTarget !== undefined && lfoParam !== undefined) {
|
||||||
connectModulators({
|
connectModulators(
|
||||||
num: lfoNum,
|
{
|
||||||
target: lfoTarget,
|
num: lfoNum,
|
||||||
param: lfoParam,
|
target: lfoTarget,
|
||||||
frequency: lfoRate,
|
param: lfoParam,
|
||||||
depth: lfoDepth,
|
frequency: lfoRate,
|
||||||
dcoffset: lfoDCOffset ?? 0, // override default value of 0.5
|
depth: lfoDepth,
|
||||||
shape: lfoShape,
|
dcoffset: lfoDCOffset ?? 0, // override default value of 0.5
|
||||||
skew: lfoSkew,
|
shape: lfoShape,
|
||||||
curve: lfoCurve,
|
skew: lfoSkew,
|
||||||
persistent: 1,
|
curve: lfoCurve,
|
||||||
begin: t,
|
persistent: 1,
|
||||||
synced: lfoSynced,
|
begin: t,
|
||||||
cps: cps,
|
synced: lfoSynced,
|
||||||
}, "lfo");
|
cps: cps,
|
||||||
|
},
|
||||||
|
'lfo',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (envTarget !== undefined && envParam !== undefined) {
|
if (envTarget !== undefined && envParam !== undefined) {
|
||||||
connectModulators({
|
connectModulators(
|
||||||
target: envTarget,
|
{
|
||||||
param: envParam,
|
target: envTarget,
|
||||||
envDepth,
|
param: envParam,
|
||||||
attack: envAttack,
|
envDepth,
|
||||||
decay: envDecay,
|
attack: envAttack,
|
||||||
sustain: envSustain,
|
decay: envDecay,
|
||||||
release: envRelease,
|
sustain: envSustain,
|
||||||
curve: envCurve,
|
release: envRelease,
|
||||||
begin: t,
|
curve: envCurve,
|
||||||
end: endWithRelease,
|
begin: t,
|
||||||
}, "envelope");
|
end: endWithRelease,
|
||||||
|
},
|
||||||
|
'envelope',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ class LFOProcessor extends AudioWorkletProcessor {
|
||||||
const begin = parameters['begin'][0];
|
const begin = parameters['begin'][0];
|
||||||
const end = parameters['end'][0];
|
const end = parameters['end'][0];
|
||||||
const persistent = parameters['persistent'][0];
|
const persistent = parameters['persistent'][0];
|
||||||
if ((persistent < 0.5) && currentTime >= end) {
|
if (persistent < 0.5 && currentTime >= end) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (currentTime <= begin) {
|
if (currentTime <= begin) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue