1149 lines
33 KiB
JavaScript
1149 lines
33 KiB
JavaScript
/*
|
|
superdough.mjs - <short description TODO>
|
|
Copyright (C) 2022 Strudel contributors - see <https://codeberg.org/uzu/strudel/src/branch/main/packages/superdough/superdough.mjs>
|
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import './feedbackdelay.mjs';
|
|
import './reverb.mjs';
|
|
import './vowel.mjs';
|
|
import { clamp, nanFallback, _mod, cycleToSeconds, secondsToCycle } from './util.mjs';
|
|
import workletsUrl from './worklets.mjs?audioworklet';
|
|
import {
|
|
createFilter,
|
|
gainNode,
|
|
getCompressor,
|
|
getWorklet,
|
|
webAudioTimeout,
|
|
getADSRValues,
|
|
getParamADSR,
|
|
} from './helpers.mjs';
|
|
import { map } from 'nanostores';
|
|
import { logger, errorLogger } from './logger.mjs';
|
|
import { loadBuffer } from './sampler.mjs';
|
|
|
|
export const DEFAULT_MAX_POLYPHONY = 128;
|
|
const DEFAULT_AUDIO_DEVICE_NAME = 'System Standard';
|
|
|
|
let maxPolyphony = DEFAULT_MAX_POLYPHONY;
|
|
|
|
export function setMaxPolyphony(polyphony) {
|
|
maxPolyphony = parseInt(polyphony) ?? DEFAULT_MAX_POLYPHONY;
|
|
}
|
|
|
|
let multiChannelOrbits = false;
|
|
export function setMultiChannelOrbits(bool) {
|
|
multiChannelOrbits = bool == true;
|
|
}
|
|
|
|
function getModulationShapeInput(val) {
|
|
if (typeof val === 'number') {
|
|
return val % 5;
|
|
}
|
|
return { tri: 0, triangle: 0, sine: 1, ramp: 2, saw: 3, square: 4 }[val] ?? 0;
|
|
}
|
|
|
|
export const soundMap = map();
|
|
|
|
export function registerSound(key, onTrigger, data = {}) {
|
|
key = key.toLowerCase().replace(/\s+/g, '_');
|
|
soundMap.setKey(key, { onTrigger, data });
|
|
}
|
|
|
|
let gainCurveFunc = (val) => val;
|
|
|
|
export function applyGainCurve(val) {
|
|
return gainCurveFunc(val);
|
|
}
|
|
|
|
export function setGainCurve(newGainCurveFunc) {
|
|
gainCurveFunc = newGainCurveFunc;
|
|
}
|
|
|
|
function aliasBankMap(aliasMap) {
|
|
// Make all bank keys lower case for case insensitivity
|
|
for (const key in aliasMap) {
|
|
aliasMap[key.toLowerCase()] = aliasMap[key];
|
|
}
|
|
|
|
// Look through every sound...
|
|
const soundDictionary = soundMap.get();
|
|
for (const key in soundDictionary) {
|
|
// Check if the sound is part of a bank...
|
|
const [bank, suffix] = key.split('_');
|
|
if (!suffix) continue;
|
|
|
|
// Check if the bank is aliased...
|
|
const aliasValue = aliasMap[bank];
|
|
if (aliasValue) {
|
|
if (typeof aliasValue === 'string') {
|
|
// Alias a single alias
|
|
soundDictionary[`${aliasValue}_${suffix}`.toLowerCase()] = soundDictionary[key];
|
|
} else if (Array.isArray(aliasValue)) {
|
|
// Alias multiple aliases
|
|
for (const alias of aliasValue) {
|
|
soundDictionary[`${alias}_${suffix}`.toLowerCase()] = soundDictionary[key];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update the sound map!
|
|
// We need to destructure here to trigger the update
|
|
soundMap.set({ ...soundDictionary });
|
|
}
|
|
|
|
async function aliasBankPath(path) {
|
|
const response = await fetch(path);
|
|
const aliasMap = await response.json();
|
|
aliasBankMap(aliasMap);
|
|
}
|
|
|
|
/**
|
|
* Register an alias for a bank of sounds.
|
|
* Optionally accepts a single argument map of bank aliases.
|
|
* Optionally accepts a single argument string of a path to a JSON file containing bank aliases.
|
|
* @param {string} bank - The bank to alias
|
|
* @param {string} alias - The alias to use for the bank
|
|
*/
|
|
export async function aliasBank(...args) {
|
|
switch (args.length) {
|
|
case 1:
|
|
if (typeof args[0] === 'string') {
|
|
return aliasBankPath(args[0]);
|
|
} else {
|
|
return aliasBankMap(args[0]);
|
|
}
|
|
case 2:
|
|
return aliasBankMap({ [args[0]]: args[1] });
|
|
default:
|
|
throw new Error('aliasMap expects 1 or 2 arguments, received ' + args.length);
|
|
}
|
|
}
|
|
|
|
export function getSound(s) {
|
|
if (typeof s !== 'string') {
|
|
console.warn(`getSound: expected string got "${s}". fall back to triangle`);
|
|
return soundMap.get().triangle; // is this good?
|
|
}
|
|
return soundMap.get()[s.toLowerCase()];
|
|
}
|
|
|
|
export const getAudioDevices = async () => {
|
|
await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
let mediaDevices = await navigator.mediaDevices.enumerateDevices();
|
|
mediaDevices = mediaDevices.filter((device) => device.kind === 'audiooutput' && device.deviceId !== 'default');
|
|
const devicesMap = new Map();
|
|
devicesMap.set(DEFAULT_AUDIO_DEVICE_NAME, '');
|
|
mediaDevices.forEach((device) => {
|
|
devicesMap.set(device.label, device.deviceId);
|
|
});
|
|
return devicesMap;
|
|
};
|
|
|
|
let defaultDefaultValues = {
|
|
s: 'triangle',
|
|
gain: 0.8,
|
|
postgain: 1,
|
|
density: '.03',
|
|
ftype: '12db',
|
|
fanchor: 0,
|
|
resonance: 1,
|
|
hresonance: 1,
|
|
bandq: 1,
|
|
channels: [1, 2],
|
|
phaserdepth: 0.75,
|
|
shapevol: 1,
|
|
distortvol: 1,
|
|
delay: 0,
|
|
byteBeatExpression: '0',
|
|
delayfeedback: 0.5,
|
|
delaysync: 3 / 16,
|
|
orbit: 1,
|
|
i: 1,
|
|
velocity: 1,
|
|
fft: 8,
|
|
};
|
|
|
|
const defaultDefaultDefaultValues = Object.freeze({ ...defaultDefaultValues });
|
|
|
|
export function setDefault(control, value) {
|
|
// const main = getControlName(control); // we cant do this because superdough is independent of strudel/core
|
|
defaultDefaultValues[control] = value;
|
|
}
|
|
|
|
export function resetDefaults() {
|
|
defaultDefaultValues = { ...defaultDefaultDefaultValues };
|
|
}
|
|
|
|
let defaultControls = new Map(Object.entries(defaultDefaultValues));
|
|
|
|
export function setDefaultValue(key, value) {
|
|
defaultControls.set(key, value);
|
|
}
|
|
export function getDefaultValue(key) {
|
|
return defaultControls.get(key);
|
|
}
|
|
export function setDefaultValues(defaultsobj) {
|
|
Object.keys(defaultsobj).forEach((key) => {
|
|
setDefaultValue(key, defaultsobj[key]);
|
|
});
|
|
}
|
|
export function resetDefaultValues() {
|
|
defaultControls = new Map(Object.entries(defaultDefaultValues));
|
|
}
|
|
export function setVersionDefaults(version) {
|
|
resetDefaultValues();
|
|
if (version === '1.0') {
|
|
setDefaultValue('fanchor', 0.5);
|
|
}
|
|
}
|
|
|
|
export const resetLoadedSounds = () => soundMap.set({});
|
|
|
|
let audioContext;
|
|
|
|
export const setDefaultAudioContext = () => {
|
|
audioContext = new AudioContext();
|
|
return audioContext;
|
|
};
|
|
|
|
export const getAudioContext = () => {
|
|
if (!audioContext) {
|
|
return setDefaultAudioContext();
|
|
}
|
|
|
|
return audioContext;
|
|
};
|
|
|
|
export function getAudioContextCurrentTime() {
|
|
return getAudioContext().currentTime;
|
|
}
|
|
|
|
let workletsLoading;
|
|
function loadWorklets() {
|
|
if (!workletsLoading) {
|
|
const audioCtx = getAudioContext();
|
|
workletsLoading = audioCtx.audioWorklet.addModule(workletsUrl);
|
|
}
|
|
|
|
return workletsLoading;
|
|
}
|
|
|
|
// this function should be called on first user interaction (to avoid console warning)
|
|
export async function initAudio(options = {}) {
|
|
const {
|
|
disableWorklets = false,
|
|
maxPolyphony,
|
|
audioDeviceName = DEFAULT_AUDIO_DEVICE_NAME,
|
|
multiChannelOrbits = false,
|
|
} = options;
|
|
|
|
setMaxPolyphony(maxPolyphony);
|
|
setMultiChannelOrbits(multiChannelOrbits);
|
|
if (typeof window === 'undefined') {
|
|
return;
|
|
}
|
|
|
|
const audioCtx = getAudioContext();
|
|
|
|
if (audioDeviceName != null && audioDeviceName != DEFAULT_AUDIO_DEVICE_NAME) {
|
|
try {
|
|
const devices = await getAudioDevices();
|
|
const id = devices.get(audioDeviceName);
|
|
const isValidID = (id ?? '').length > 0;
|
|
if (audioCtx.sinkId !== id && isValidID) {
|
|
await audioCtx.setSinkId(id);
|
|
}
|
|
logger(
|
|
`[superdough] Audio Device set to ${audioDeviceName}, it might take a few seconds before audio plays on all output channels`,
|
|
);
|
|
} catch {
|
|
logger('[superdough] failed to set audio interface', 'warning');
|
|
}
|
|
}
|
|
|
|
await audioCtx.resume();
|
|
if (disableWorklets) {
|
|
logger('[superdough]: AudioWorklets disabled with disableWorklets');
|
|
return;
|
|
}
|
|
try {
|
|
await loadWorklets();
|
|
logger('[superdough] AudioWorklets loaded');
|
|
} catch (err) {
|
|
console.warn('could not load AudioWorklet effects', err);
|
|
}
|
|
logger('[superdough] ready');
|
|
}
|
|
let audioReady;
|
|
export async function initAudioOnFirstClick(options) {
|
|
if (!audioReady) {
|
|
audioReady = new Promise((resolve) => {
|
|
document.addEventListener('click', async function listener() {
|
|
document.removeEventListener('click', listener);
|
|
await initAudio(options);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
return audioReady;
|
|
}
|
|
|
|
let delays = {};
|
|
const maxfeedback = 0.98;
|
|
|
|
let channelMerger, destinationGain;
|
|
//update the output channel configuration to match user's audio device
|
|
export function initializeAudioOutput() {
|
|
const audioContext = getAudioContext();
|
|
const maxChannelCount = audioContext.destination.maxChannelCount;
|
|
audioContext.destination.channelCount = maxChannelCount;
|
|
channelMerger = new ChannelMergerNode(audioContext, { numberOfInputs: audioContext.destination.channelCount });
|
|
destinationGain = new GainNode(audioContext);
|
|
channelMerger.connect(destinationGain);
|
|
destinationGain.connect(audioContext.destination);
|
|
}
|
|
|
|
// input: AudioNode, channels: ?Array<int>
|
|
export const connectToDestination = (input, channels = [0, 1]) => {
|
|
const ctx = getAudioContext();
|
|
if (channelMerger == null) {
|
|
initializeAudioOutput();
|
|
}
|
|
//This upmix can be removed if correct channel counts are set throughout the app,
|
|
// and then strudel could theoretically support surround sound audio files
|
|
const stereoMix = new StereoPannerNode(ctx);
|
|
input.connect(stereoMix);
|
|
|
|
const splitter = new ChannelSplitterNode(ctx, {
|
|
numberOfOutputs: stereoMix.channelCount,
|
|
});
|
|
stereoMix.connect(splitter);
|
|
channels.forEach((ch, i) => {
|
|
splitter.connect(channelMerger, i % stereoMix.channelCount, ch % ctx.destination.channelCount);
|
|
});
|
|
};
|
|
|
|
export const panic = () => {
|
|
if (destinationGain == null) {
|
|
return;
|
|
}
|
|
destinationGain.gain.linearRampToValueAtTime(0, getAudioContext().currentTime + 0.01);
|
|
destinationGain = null;
|
|
channelMerger == null;
|
|
};
|
|
|
|
function getDelay(orbit, delaytime, delayfeedback, t, channels) {
|
|
if (delayfeedback > maxfeedback) {
|
|
//logger(`delayfeedback was clamped to ${maxfeedback} to save your ears`);
|
|
}
|
|
delayfeedback = clamp(delayfeedback, 0, 0.98);
|
|
if (!orbits[orbit].delayNode) {
|
|
const ac = getAudioContext();
|
|
const dly = ac.createFeedbackDelay(1, delaytime, delayfeedback);
|
|
dly.start?.(t); // for some reason, this throws when audion extension is installed..
|
|
connectToOrbit(dly, orbit);
|
|
orbits[orbit].delayNode = dly;
|
|
}
|
|
orbits[orbit].delayNode.delayTime.value !== delaytime &&
|
|
orbits[orbit].delayNode.delayTime.setValueAtTime(delaytime, t);
|
|
orbits[orbit].delayNode.feedback.value !== delayfeedback &&
|
|
orbits[orbit].delayNode.feedback.setValueAtTime(delayfeedback, t);
|
|
return orbits[orbit].delayNode;
|
|
}
|
|
|
|
export function getLfo(audioContext, properties = {}) {
|
|
// Extract some params we need for deriving other params
|
|
const { begin, shape = 0, ...props } = properties;
|
|
const lfoprops = {
|
|
time: begin,
|
|
shape: getModulationShapeInput(shape),
|
|
...props,
|
|
};
|
|
|
|
return getWorklet(audioContext, 'lfo-processor', lfoprops);
|
|
}
|
|
|
|
function getPhaser(begin, end, frequency = 1, depth = 0.5, centerFrequency = 1000, sweep = 2000) {
|
|
const ac = getAudioContext();
|
|
const lfoGain = getLfo(ac, { frequency, depth: sweep * 2, begin, end });
|
|
|
|
//filters
|
|
const numStages = 2; //num of filters in series
|
|
let fOffset = 0;
|
|
const filterChain = [];
|
|
for (let i = 0; i < numStages; i++) {
|
|
const filter = ac.createBiquadFilter();
|
|
filter.type = 'notch';
|
|
filter.gain.value = 1;
|
|
filter.frequency.value = centerFrequency + fOffset;
|
|
filter.Q.value = 2 - Math.min(Math.max(depth * 2, 0), 1.9);
|
|
|
|
lfoGain.connect(filter.detune);
|
|
fOffset += 282;
|
|
if (i > 0) {
|
|
filterChain[i - 1].connect(filter);
|
|
}
|
|
filterChain.push(filter);
|
|
}
|
|
return filterChain[filterChain.length - 1];
|
|
}
|
|
|
|
function getFilterType(ftype) {
|
|
ftype = ftype ?? 0;
|
|
const filterTypes = ['12db', 'ladder', '24db'];
|
|
return typeof ftype === 'number' ? filterTypes[Math.floor(_mod(ftype, filterTypes.length))] : ftype;
|
|
}
|
|
|
|
//type orbit {
|
|
// gain: number,
|
|
// reverbNode: reverbNode
|
|
// delayNode: delayNode
|
|
//}
|
|
let orbits = {};
|
|
function connectToOrbit(node, orbit) {
|
|
if (orbits[orbit] == null) {
|
|
errorLogger(new Error('target orbit does not exist'), 'superdough');
|
|
}
|
|
node.connect(orbits[orbit].gain);
|
|
}
|
|
|
|
function setOrbit(audioContext, orbit, channels) {
|
|
if (orbits[orbit] == null) {
|
|
orbits[orbit] = {
|
|
gain: new GainNode(audioContext, { gain: 1 }),
|
|
};
|
|
connectToDestination(orbits[orbit].gain, channels);
|
|
}
|
|
}
|
|
function duckOrbit(audioContext, targetOrbit, t, attacktime = 0.1, duckdepth = 1) {
|
|
const targetArr = [targetOrbit].flat();
|
|
|
|
targetArr.forEach((target) => {
|
|
if (orbits[target] == null) {
|
|
errorLogger(new Error(`duck target orbit ${target} does not exist`), 'superdough');
|
|
return;
|
|
}
|
|
webAudioTimeout(
|
|
audioContext,
|
|
() => {
|
|
orbits[target].gain.gain.cancelScheduledValues(t);
|
|
const currVal = orbits[target].gain.gain.value;
|
|
orbits[target].gain.gain.linearRampToValueAtTime(clamp(1 - Math.pow(duckdepth, 0.5), 0.01, currVal), t);
|
|
orbits[target].gain.gain.exponentialRampToValueAtTime(1, t + Math.max(0.002, attacktime));
|
|
},
|
|
0,
|
|
t - 0.01,
|
|
);
|
|
});
|
|
}
|
|
|
|
let hasChanged = (now, before) => now !== undefined && now !== before;
|
|
function getReverb(orbit, duration, fade, lp, dim, ir) {
|
|
// If no reverb has been created for a given orbit, create one
|
|
if (!orbits[orbit].reverbNode) {
|
|
const ac = getAudioContext();
|
|
const reverb = ac.createReverb(duration, fade, lp, dim, ir);
|
|
connectToOrbit(reverb, orbit);
|
|
orbits[orbit].reverbNode = reverb;
|
|
}
|
|
if (
|
|
hasChanged(duration, orbits[orbit].reverbNode.duration) ||
|
|
hasChanged(fade, orbits[orbit].reverbNode.fade) ||
|
|
hasChanged(lp, orbits[orbit].reverbNode.lp) ||
|
|
hasChanged(dim, orbits[orbit].reverbNode.dim) ||
|
|
orbits[orbit].reverbNode.ir !== ir
|
|
) {
|
|
// only regenerate when something has changed
|
|
// avoids endless regeneration on things like
|
|
// stack(s("a"), s("b").rsize(8)).room(.5)
|
|
// this only works when args may stay undefined until here
|
|
// setting default values breaks this
|
|
orbits[orbit].reverbNode.generate(duration, fade, lp, dim, ir);
|
|
}
|
|
return orbits[orbit].reverbNode;
|
|
}
|
|
|
|
export let analysers = {},
|
|
analysersData = {};
|
|
|
|
export function getAnalyserById(id, fftSize = 1024, smoothingTimeConstant = 0.5) {
|
|
if (!analysers[id]) {
|
|
// make sure this doesn't happen too often as it piles up garbage
|
|
const analyserNode = getAudioContext().createAnalyser();
|
|
analyserNode.fftSize = fftSize;
|
|
analyserNode.smoothingTimeConstant = smoothingTimeConstant;
|
|
// getDestination().connect(analyserNode);
|
|
analysers[id] = analyserNode;
|
|
analysersData[id] = new Float32Array(analysers[id].frequencyBinCount);
|
|
}
|
|
if (analysers[id].fftSize !== fftSize) {
|
|
analysers[id].fftSize = fftSize;
|
|
analysersData[id] = new Float32Array(analysers[id].frequencyBinCount);
|
|
}
|
|
return analysers[id];
|
|
}
|
|
|
|
export function getAnalyzerData(type = 'time', id = 1) {
|
|
const getter = {
|
|
time: () => analysers[id]?.getFloatTimeDomainData(analysersData[id]),
|
|
frequency: () => analysers[id]?.getFloatFrequencyData(analysersData[id]),
|
|
}[type];
|
|
if (!getter) {
|
|
throw new Error(`getAnalyzerData: ${type} not supported. use one of ${Object.keys(getter).join(', ')}`);
|
|
}
|
|
getter();
|
|
return analysersData[id];
|
|
}
|
|
|
|
function effectSend(input, effect, wet) {
|
|
const send = gainNode(wet);
|
|
input.connect(send);
|
|
send.connect(effect);
|
|
return send;
|
|
}
|
|
|
|
export function resetGlobalEffects() {
|
|
orbits = {};
|
|
analysers = {};
|
|
analysersData = {};
|
|
lfos = {};
|
|
nodes = {};
|
|
}
|
|
|
|
function _getNodeParam(node, name) {
|
|
// Worklet case
|
|
if (node?.parameters) {
|
|
const p = node.parameters.get(name);
|
|
if (p instanceof AudioParam) {
|
|
return p;
|
|
}
|
|
}
|
|
// Built-in node case
|
|
const p = node?.[name];
|
|
if (p instanceof AudioParam) {
|
|
return p;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function _getNodeParams(node) {
|
|
const params = new Set();
|
|
// Worklet case
|
|
if (node?.parameters) {
|
|
node.parameters.forEach((_v, k) => params.add(k));
|
|
}
|
|
// Guesses based on common parameters
|
|
['gain', 'frequency', 'detune', 'Q', 'pan', 'playbackRate', 'delayTime'].forEach((k) => {
|
|
if (node?.[k] instanceof AudioParam) params.add(k);
|
|
});
|
|
return Array.from(params);
|
|
}
|
|
|
|
/**
|
|
* Split parameters (which might be arrays -- implying multiple-parameter modulation -- or a single number) into independent
|
|
* objects which only account for single-parameter modulation
|
|
*
|
|
* @param {Object} params - Dictionary of modulation parameters.
|
|
* @returns {Object[]} - Array of parameter objects, one per parameter modulation
|
|
*/
|
|
function _splitParams(params) {
|
|
const num = ['num', 'target', 'param'] // names used to indicate individual parameter modulations
|
|
.map((k) => [params[k] ?? 0].flat().length)
|
|
.reduce((a, v) => Math.max(a, v), 1);
|
|
|
|
const individualParams = [];
|
|
for (let i = 0; i < num; i++) {
|
|
const paramsI = {};
|
|
for (const k in params) {
|
|
const flatV = [params[k]].flat();
|
|
if (flatV.length !== num && flatV.length !== 1) {
|
|
errorLogger(
|
|
new Error(
|
|
`Could not set up modulations. We derived ${num} items, but ${k}: ${JSON.stringify(flatV)} has length ${flatV.length} (needs 1 or ${num}).`,
|
|
),
|
|
'superdough',
|
|
);
|
|
return [];
|
|
}
|
|
paramsI[k] = flatV[i] ?? flatV[0];
|
|
}
|
|
individualParams.push(paramsI);
|
|
}
|
|
return individualParams;
|
|
}
|
|
|
|
/**
|
|
* Given a node name and the name of a parameter on that node, attempt to retrieve
|
|
* all nodes corresponding to the name and their associated parameters
|
|
*
|
|
* Note that we say nodes, plural, because some nodes have multiple sub-nodes, like a
|
|
* 24db filter which is two filters in series
|
|
*
|
|
* @param {string} targetName - Name of the node to modulate parameters on (e.g. `lpf`, `source`, etc.)
|
|
* @param {string} paramName - Name of the parameter to modulate on that node
|
|
* @returns {AudioParam[]} - Array of audio parameter objects for modulation
|
|
*
|
|
*/
|
|
function _getTargetParams(targetName, paramName) {
|
|
const targetNodes = nodes[targetName];
|
|
if (!targetNodes) {
|
|
const keys = Object.keys(nodes);
|
|
errorLogger(
|
|
new Error(
|
|
`Could not connect to target '${targetName}' — it does not exist. Available targets: ${keys.join(', ')}`,
|
|
),
|
|
'superdough',
|
|
);
|
|
return [];
|
|
}
|
|
|
|
const audioParams = [];
|
|
targetNodes.forEach((targetNode) => {
|
|
const targetParam = _getNodeParam(targetNode, paramName);
|
|
if (!targetParam) {
|
|
const available = _getNodeParams(targetNode);
|
|
errorLogger(
|
|
new Error(
|
|
`Could not connect to parameter '${paramName}' on '${targetName}'. Available parameters: ${available.join(', ')}`,
|
|
),
|
|
'superdough',
|
|
);
|
|
return;
|
|
}
|
|
audioParams.push(targetParam);
|
|
});
|
|
return audioParams;
|
|
}
|
|
|
|
function _setWorkletParamsAtTime(audioParams, params, time) {
|
|
for (const [name, value] of params) {
|
|
if (value == null) continue;
|
|
const p = audioParams.get(name);
|
|
if (p.cancelAndHoldAtTime) p.cancelAndHoldAtTime(time);
|
|
else p.cancelScheduledValues(time);
|
|
p.setValueAtTime(value, time);
|
|
}
|
|
}
|
|
|
|
let lfos = {};
|
|
function _connectLFO(params) {
|
|
const {
|
|
frequency = 1,
|
|
synced = 0,
|
|
cps = 0.5,
|
|
num = 1, // default to LFO 1
|
|
target,
|
|
param,
|
|
begin,
|
|
...filteredParams
|
|
} = params;
|
|
filteredParams['frequency'] = synced ? frequency / cps : frequency;
|
|
let lfoNode = lfos[num];
|
|
if (lfoNode == null) {
|
|
const ac = getAudioContext();
|
|
lfoNode = getLfo(ac, filteredParams);
|
|
lfos[num] = lfoNode;
|
|
}
|
|
const targets = _getTargetParams(target, param);
|
|
targets.forEach((target) => lfoNode.connect(target));
|
|
_setWorkletParamsAtTime(lfoNode.parameters, Object.entries(filteredParams), begin);
|
|
}
|
|
|
|
function _connectEnvelope(params) {
|
|
const { target, param, envDepth, begin, end, attack, decay, sustain, release, curve, ...filteredParams } = params;
|
|
const targets = _getTargetParams(target, param);
|
|
const [att, dec, sus, rel] = getADSRValues([attack, decay, sustain, release], curve, [0.005, 0.14, 0, 0.1]);
|
|
targets.forEach((targetParam) => {
|
|
const currentValue = targetParam.value;
|
|
const min = currentValue;
|
|
const max = currentValue + envDepth;
|
|
getParamADSR(targetParam, att, dec, sus, rel, min, max, begin, end, curve);
|
|
});
|
|
}
|
|
|
|
function connectModulators(params, modulatorType) {
|
|
// We break down params specifying multiple modulators into a set of parameters for
|
|
// a single one
|
|
const individualParams = _splitParams(params);
|
|
if (modulatorType === 'lfo') {
|
|
individualParams.forEach(_connectLFO);
|
|
} else if (modulatorType === 'envelope') {
|
|
individualParams.forEach(_connectEnvelope);
|
|
}
|
|
}
|
|
|
|
let activeSoundSources = new Map();
|
|
//music programs/audio gear usually increments inputs/outputs from 1, we need to subtract 1 from the input because the webaudio API channels start at 0
|
|
|
|
function mapChannelNumbers(channels) {
|
|
return (Array.isArray(channels) ? channels : [channels]).map((ch) => ch - 1);
|
|
}
|
|
|
|
let nodes = {};
|
|
|
|
export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5) => {
|
|
// new: t is always expected to be the absolute target onset time
|
|
const ac = getAudioContext();
|
|
|
|
let { stretch } = value;
|
|
if (stretch != null) {
|
|
//account for phase vocoder latency
|
|
const latency = 0.04;
|
|
t = t - latency;
|
|
}
|
|
if (typeof value !== 'object') {
|
|
throw new Error(
|
|
`expected hap.value to be an object, but got "${value}". Hint: append .note() or .s() to the end`,
|
|
'error',
|
|
);
|
|
}
|
|
|
|
// duration is passed as value too..
|
|
value.duration = hapDuration;
|
|
// calculate absolute time
|
|
|
|
if (t < ac.currentTime) {
|
|
console.warn(
|
|
`[superdough]: cannot schedule sounds in the past (target: ${t.toFixed(2)}, now: ${ac.currentTime.toFixed(2)})`,
|
|
);
|
|
return;
|
|
}
|
|
// destructure
|
|
let {
|
|
tremolo,
|
|
tremolosync,
|
|
tremolodepth = 1,
|
|
tremoloskew,
|
|
tremolophase = 0,
|
|
tremoloshape,
|
|
s = getDefaultValue('s'),
|
|
bank,
|
|
source,
|
|
gain = getDefaultValue('gain'),
|
|
postgain = getDefaultValue('postgain'),
|
|
density = getDefaultValue('density'),
|
|
duckorbit,
|
|
duckattack,
|
|
duckdepth,
|
|
// filters
|
|
fanchor = getDefaultValue('fanchor'),
|
|
drive = 0.69,
|
|
release = 0,
|
|
// low pass
|
|
cutoff,
|
|
lpenv,
|
|
lpattack,
|
|
lpdecay,
|
|
lpsustain,
|
|
lprelease,
|
|
resonance = getDefaultValue('resonance'),
|
|
// high pass
|
|
hpenv,
|
|
hcutoff,
|
|
hpattack,
|
|
hpdecay,
|
|
hpsustain,
|
|
hprelease,
|
|
hresonance = getDefaultValue('hresonance'),
|
|
// band pass
|
|
bpenv,
|
|
bandf,
|
|
bpattack,
|
|
bpdecay,
|
|
bpsustain,
|
|
bprelease,
|
|
bandq = getDefaultValue('bandq'),
|
|
|
|
//phaser
|
|
phaserrate: phaser,
|
|
phaserdepth = getDefaultValue('phaserdepth'),
|
|
phasersweep,
|
|
phasercenter,
|
|
//
|
|
coarse,
|
|
|
|
crush,
|
|
dry,
|
|
shape,
|
|
shapevol = getDefaultValue('shapevol'),
|
|
distort,
|
|
distortvol = getDefaultValue('distortvol'),
|
|
pan,
|
|
vowel,
|
|
delay = getDefaultValue('delay'),
|
|
delayfeedback = getDefaultValue('delayfeedback'),
|
|
delaysync = getDefaultValue('delaysync'),
|
|
delaytime,
|
|
orbit = getDefaultValue('orbit'),
|
|
room,
|
|
roomfade,
|
|
roomlp,
|
|
roomdim,
|
|
roomsize,
|
|
ir,
|
|
i = getDefaultValue('i'),
|
|
velocity = getDefaultValue('velocity'),
|
|
analyze, // analyser wet
|
|
fft = getDefaultValue('fft'), // fftSize 0 - 10
|
|
compressor: compressorThreshold,
|
|
compressorRatio,
|
|
compressorKnee,
|
|
compressorAttack,
|
|
compressorRelease,
|
|
lfoNum,
|
|
lfoTarget,
|
|
lfoParam,
|
|
lfoRate,
|
|
lfoDepth,
|
|
lfoDCOffset,
|
|
lfoShape,
|
|
lfoSkew,
|
|
lfoCurve,
|
|
lfoSynced,
|
|
envTarget,
|
|
envParam,
|
|
envAttack,
|
|
envDecay,
|
|
envSustain,
|
|
envRelease,
|
|
envCurve,
|
|
envDepth,
|
|
} = value;
|
|
|
|
delaytime = delaytime ?? cycleToSeconds(delaysync, cps);
|
|
|
|
const orbitChannels = mapChannelNumbers(
|
|
multiChannelOrbits && orbit > 0 ? [orbit * 2 - 1, orbit * 2] : getDefaultValue('channels'),
|
|
);
|
|
|
|
const channels = value.channels != null ? mapChannelNumbers(value.channels) : orbitChannels;
|
|
setOrbit(ac, orbit, channels, t, cycle, cps);
|
|
|
|
if (duckorbit != null) {
|
|
duckOrbit(ac, duckorbit, t, duckattack, duckdepth);
|
|
}
|
|
|
|
gain = applyGainCurve(nanFallback(gain, 1));
|
|
postgain = applyGainCurve(postgain);
|
|
shapevol = applyGainCurve(shapevol);
|
|
distortvol = applyGainCurve(distortvol);
|
|
delay = applyGainCurve(delay);
|
|
velocity = applyGainCurve(velocity);
|
|
tremolodepth = applyGainCurve(tremolodepth);
|
|
gain *= velocity; // velocity currently only multiplies with gain. it might do other things in the future
|
|
|
|
const end = t + hapDuration;
|
|
const endWithRelease = end + release;
|
|
const chainID = Math.round(Math.random() * 1000000);
|
|
|
|
// oldest audio nodes will be destroyed if maximum polyphony is exceeded
|
|
for (let i = 0; i <= activeSoundSources.size - maxPolyphony; i++) {
|
|
const ch = activeSoundSources.entries().next();
|
|
const source = ch.value[1];
|
|
const chainID = ch.value[0];
|
|
const endTime = t + 0.25;
|
|
source?.node?.gain?.linearRampToValueAtTime(0, endTime);
|
|
source?.stop?.(endTime);
|
|
activeSoundSources.delete(chainID);
|
|
}
|
|
|
|
let audioNodes = [];
|
|
|
|
if (['-', '~', '_'].includes(s)) {
|
|
return;
|
|
}
|
|
if (bank && s) {
|
|
s = `${bank}_${s}`;
|
|
value.s = s;
|
|
}
|
|
|
|
// get source AudioNode
|
|
let sourceNode;
|
|
if (source) {
|
|
sourceNode = source(t, value, hapDuration, cps);
|
|
nodes['source'] = [sourceNode];
|
|
} else if (getSound(s)) {
|
|
const { onTrigger } = getSound(s);
|
|
const onEnded = () => {
|
|
audioNodes.forEach((n) => n?.disconnect());
|
|
activeSoundSources.delete(chainID);
|
|
};
|
|
const soundHandle = await onTrigger(t, value, onEnded);
|
|
|
|
if (soundHandle) {
|
|
sourceNode = soundHandle.node;
|
|
activeSoundSources.set(chainID, soundHandle);
|
|
nodes['source'] = [soundHandle.oscillator];
|
|
}
|
|
} else {
|
|
throw new Error(`sound ${s} not found! Is it loaded?`);
|
|
}
|
|
if (!sourceNode) {
|
|
// if onTrigger does not return anything, we will just silently skip
|
|
// this can be used for things like speed(0) in the sampler
|
|
return;
|
|
}
|
|
|
|
if (ac.currentTime > t) {
|
|
logger('[webaudio] skip hap: still loading', ac.currentTime - t);
|
|
return;
|
|
}
|
|
const chain = []; // audio nodes that will be connected to each other sequentially
|
|
chain.push(sourceNode);
|
|
stretch !== undefined && chain.push(getWorklet(ac, 'phase-vocoder-processor', { pitchFactor: stretch }));
|
|
|
|
// gain stage
|
|
const initialGain = gainNode(gain);
|
|
nodes['gain'] = [initialGain];
|
|
chain.push(initialGain);
|
|
|
|
//filter
|
|
const ftype = getFilterType(value.ftype);
|
|
if (cutoff !== undefined) {
|
|
const lp = () =>
|
|
createFilter(
|
|
ac,
|
|
'lowpass',
|
|
cutoff,
|
|
resonance,
|
|
lpattack,
|
|
lpdecay,
|
|
lpsustain,
|
|
lprelease,
|
|
lpenv,
|
|
t,
|
|
end,
|
|
fanchor,
|
|
ftype,
|
|
drive,
|
|
);
|
|
const lp1 = lp();
|
|
nodes['lpf'] = [lp1];
|
|
chain.push(lp1);
|
|
if (ftype === '24db') {
|
|
const lp2 = lp();
|
|
nodes['lpf'].push(lp2);
|
|
chain.push(lp2);
|
|
}
|
|
}
|
|
|
|
if (hcutoff !== undefined) {
|
|
const hp = () =>
|
|
createFilter(
|
|
ac,
|
|
'highpass',
|
|
hcutoff,
|
|
hresonance,
|
|
hpattack,
|
|
hpdecay,
|
|
hpsustain,
|
|
hprelease,
|
|
hpenv,
|
|
t,
|
|
end,
|
|
fanchor,
|
|
);
|
|
const hp1 = hp();
|
|
nodes['hpf'] = [hp1];
|
|
chain.push(hp1);
|
|
if (ftype === '24db') {
|
|
const hp2 = hp();
|
|
nodes['hpf'].push(hp1);
|
|
chain.push(hp2);
|
|
}
|
|
}
|
|
|
|
if (bandf !== undefined) {
|
|
let bp = () =>
|
|
createFilter(ac, 'bandpass', bandf, bandq, bpattack, bpdecay, bpsustain, bprelease, bpenv, t, end, fanchor);
|
|
const bp1 = bp();
|
|
nodes['bpf'] = [bp1];
|
|
chain.push(bp1);
|
|
if (ftype === '24db') {
|
|
const bp2 = bp();
|
|
nodes['bpf'].push(bp2);
|
|
chain.push(bp2);
|
|
}
|
|
}
|
|
|
|
if (vowel !== undefined) {
|
|
const vowelFilter = ac.createVowelFilter(vowel);
|
|
nodes['vowel'] = [vowelFilter];
|
|
chain.push(vowelFilter);
|
|
}
|
|
|
|
// effects
|
|
if (coarse !== undefined) {
|
|
const coarseNode = getWorklet(ac, 'coarse-processor', { coarse });
|
|
nodes['coarse'] = [coarseNode];
|
|
chain.push(coarseNode);
|
|
}
|
|
if (crush !== undefined) {
|
|
const crushNode = getWorklet(ac, 'crush-processor', { crush });
|
|
nodes['crush'] = [crushNode];
|
|
chain.push(crushNode);
|
|
}
|
|
if (shape !== undefined) {
|
|
const shapeNode = getWorklet(ac, 'shape-processor', { shape });
|
|
nodes['shape'] = [shapeNode];
|
|
chain.push(shapeNode);
|
|
}
|
|
if (distort !== undefined) {
|
|
const distortNode = getWorklet(ac, 'distort-processor', { distort });
|
|
nodes['distort'] = [distortNode];
|
|
chain.push(distortNode);
|
|
}
|
|
|
|
if (tremolosync != null) {
|
|
tremolo = cps * tremolosync;
|
|
}
|
|
|
|
if (tremolo !== undefined) {
|
|
// Allow clipping of modulator for more dynamic possiblities, and to prevent speaker overload
|
|
// EX: a triangle waveform will clip like this /-\ when the depth is above 1
|
|
const gain = Math.max(1 - tremolodepth, 0);
|
|
const amGain = new GainNode(ac, { gain });
|
|
|
|
const time = cycle / cps;
|
|
const lfo = getLfo(ac, {
|
|
skew: tremoloskew ?? (tremoloshape != null ? 0.5 : 1),
|
|
frequency: tremolo,
|
|
depth: tremolodepth,
|
|
time,
|
|
dcoffset: 0,
|
|
shape: tremoloshape,
|
|
phaseoffset: tremolophase,
|
|
min: 0,
|
|
max: 1,
|
|
curve: 1.5,
|
|
begin: t,
|
|
end: endWithRelease,
|
|
});
|
|
lfo.connect(amGain.gain);
|
|
chain.push(amGain);
|
|
}
|
|
|
|
if (compressorThreshold !== undefined) {
|
|
const compressorNode = getCompressor(
|
|
ac,
|
|
compressorThreshold,
|
|
compressorRatio,
|
|
compressorKnee,
|
|
compressorAttack,
|
|
compressorRelease,
|
|
);
|
|
nodes['compressor'] = [compressorNode];
|
|
chain.push(compressorNode);
|
|
}
|
|
|
|
// panning
|
|
if (pan !== undefined) {
|
|
const panner = ac.createStereoPanner();
|
|
panner.pan.value = 2 * pan - 1;
|
|
chain.push(panner);
|
|
}
|
|
// phaser
|
|
if (phaser !== undefined && phaserdepth > 0) {
|
|
const phaserFX = getPhaser(t, endWithRelease, phaser, phaserdepth, phasercenter, phasersweep);
|
|
nodes['phaser'] = [phaserFX];
|
|
chain.push(phaserFX);
|
|
}
|
|
|
|
// last gain
|
|
const post = new GainNode(ac, { gain: postgain });
|
|
nodes['post'] = [post];
|
|
chain.push(post);
|
|
|
|
// delay
|
|
let delaySend;
|
|
if (delay > 0 && delaytime > 0 && delayfeedback > 0) {
|
|
const delayNode = getDelay(orbit, delaytime, delayfeedback, t, orbitChannels);
|
|
nodes['delay'] = [delayNode];
|
|
delaySend = effectSend(post, delayNode, delay);
|
|
audioNodes.push(delaySend);
|
|
}
|
|
// reverb
|
|
let reverbSend;
|
|
if (room > 0) {
|
|
let roomIR;
|
|
if (ir !== undefined) {
|
|
let url;
|
|
let sample = getSound(ir);
|
|
if (Array.isArray(sample)) {
|
|
url = sample.data.samples[i % sample.data.samples.length];
|
|
} else if (typeof sample === 'object') {
|
|
url = Object.values(sample.data.samples).flat()[i % Object.values(sample.data.samples).length];
|
|
}
|
|
roomIR = await loadBuffer(url, ac, ir, 0);
|
|
}
|
|
const reverbNode = getReverb(orbit, roomsize, roomfade, roomlp, roomdim, roomIR, orbitChannels);
|
|
nodes['room'] = [reverbNode];
|
|
reverbSend = effectSend(post, reverbNode, room);
|
|
audioNodes.push(reverbSend);
|
|
}
|
|
|
|
// analyser
|
|
let analyserSend;
|
|
if (analyze) {
|
|
const analyserNode = getAnalyserById(analyze, 2 ** (fft + 5));
|
|
analyserSend = effectSend(post, analyserNode, 1);
|
|
audioNodes.push(analyserSend);
|
|
}
|
|
if (dry != null) {
|
|
dry = applyGainCurve(dry);
|
|
const dryGain = new GainNode(ac, { gain: dry });
|
|
chain.push(dryGain);
|
|
connectToOrbit(dryGain, orbit);
|
|
} else {
|
|
connectToOrbit(post, orbit);
|
|
}
|
|
|
|
// connect chain elements together
|
|
chain.slice(1).reduce((last, current) => last.connect(current), chain[0]);
|
|
audioNodes = audioNodes.concat(chain);
|
|
|
|
// finally, now that `nodes` is populated, set up LFOs and envelopes
|
|
if (lfoTarget !== undefined && lfoParam !== undefined) {
|
|
connectModulators(
|
|
{
|
|
num: lfoNum,
|
|
target: lfoTarget,
|
|
param: lfoParam,
|
|
frequency: lfoRate,
|
|
depth: lfoDepth,
|
|
dcoffset: lfoDCOffset ?? 0, // override default value of 0.5
|
|
shape: lfoShape,
|
|
skew: lfoSkew,
|
|
curve: lfoCurve,
|
|
persistent: 1,
|
|
begin: t,
|
|
synced: lfoSynced,
|
|
cps: cps,
|
|
},
|
|
'lfo',
|
|
);
|
|
}
|
|
if (envTarget !== undefined && envParam !== undefined) {
|
|
connectModulators(
|
|
{
|
|
target: envTarget,
|
|
param: envParam,
|
|
envDepth,
|
|
attack: envAttack,
|
|
decay: envDecay,
|
|
sustain: envSustain,
|
|
release: envRelease,
|
|
curve: envCurve,
|
|
begin: t,
|
|
end: endWithRelease,
|
|
},
|
|
'envelope',
|
|
);
|
|
}
|
|
};
|
|
|
|
export const superdoughTrigger = (t, hap, ct, cps) => {
|
|
superdough(hap, t - ct, hap.duration / cps, cps);
|
|
};
|