mirror of
https://codeberg.org/uzu/strudel
synced 2026-07-23 05:33:13 -04:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c407412c6 | |||
| 4c99f4866b | |||
| 099288545e | |||
| 5738020ac2 | |||
| 1478ff51a7 | |||
| 8314023c7a | |||
| d50af71ba1 | |||
| 0664f90178 | |||
| 4321814d36 | |||
| 6610713018 | |||
| 600ab0a83e |
@@ -1,3 +1,12 @@
|
|||||||
|
/*
|
||||||
|
audioContext.mjs - Audio Context manager
|
||||||
|
|
||||||
|
Sets up a common and accessible audio context for all superdough operations
|
||||||
|
|
||||||
|
Copyright (C) 2025 Strudel contributors - see <https://codeberg.org/uzu/strudel/src/branch/main/packages/superdough/audiocontext.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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
let audioContext;
|
let audioContext;
|
||||||
|
|
||||||
export const setDefaultAudioContext = () => {
|
export const setDefaultAudioContext = () => {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { getAudioContext } from './audioContext.mjs';
|
import { getAudioContext } from './audioContext.mjs';
|
||||||
import { logger } from './logger.mjs';
|
import { logger } from './logger.mjs';
|
||||||
import { getNoiseBuffer } from './noise.mjs';
|
import { getNoiseBuffer } from './noise.mjs';
|
||||||
|
import { getNodeFromPool, markWorkletAsDead } from './nodePools.mjs';
|
||||||
import { clamp, nanFallback, midiToFreq, noteToMidi } from './util.mjs';
|
import { clamp, nanFallback, midiToFreq, noteToMidi } from './util.mjs';
|
||||||
|
|
||||||
export const noises = ['pink', 'white', 'brown', 'crackle'];
|
export const noises = ['pink', 'white', 'brown', 'crackle'];
|
||||||
@@ -36,6 +37,16 @@ export function getWorklet(ac, processor, params, config) {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getPooledWorklet(ac, processor, params, constructorOptions, reInitOptions) {
|
||||||
|
const node = getNodeFromPool(processor, () => new AudioWorkletNode(ac, processor, constructorOptions), params);
|
||||||
|
node.port.postMessage({ type: 'initialize', payload: reInitOptions });
|
||||||
|
node.port.onmessage = (e) => {
|
||||||
|
if (e.data.type === 'died') markWorkletAsDead(node);
|
||||||
|
node.port.onmessage = null;
|
||||||
|
};
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
export const getParamADSR = (
|
export const getParamADSR = (
|
||||||
param,
|
param,
|
||||||
attack,
|
attack,
|
||||||
@@ -124,10 +135,11 @@ export function getLfo(audioContext, begin, end, properties = {}) {
|
|||||||
...props,
|
...props,
|
||||||
};
|
};
|
||||||
|
|
||||||
return getWorklet(audioContext, 'lfo-processor', lfoprops);
|
return getPooledWorklet(audioContext, 'lfo-processor', lfoprops);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCompressor(ac, threshold, ratio, knee, attack, release) {
|
export function getCompressor(ac, threshold, ratio, knee, attack, release) {
|
||||||
|
const node = getNodeFromPool('compressor', () => new DynamicsCompressorNode(ac, {}));
|
||||||
const options = {
|
const options = {
|
||||||
threshold: threshold ?? -3,
|
threshold: threshold ?? -3,
|
||||||
ratio: ratio ?? 10,
|
ratio: ratio ?? 10,
|
||||||
@@ -135,7 +147,11 @@ export function getCompressor(ac, threshold, ratio, knee, attack, release) {
|
|||||||
attack: attack ?? 0.005,
|
attack: attack ?? 0.005,
|
||||||
release: release ?? 0.05,
|
release: release ?? 0.05,
|
||||||
};
|
};
|
||||||
return new DynamicsCompressorNode(ac, options);
|
const now = ac.currentTime;
|
||||||
|
Object.entries(options).forEach(([key, value]) => {
|
||||||
|
node[key].setValueAtTime(value, now);
|
||||||
|
});
|
||||||
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// changes the default values of the envelope based on what parameters the user has defined
|
// changes the default values of the envelope based on what parameters the user has defined
|
||||||
@@ -211,13 +227,16 @@ export function createFilter(context, start, end, params, cps, cycle) {
|
|||||||
|
|
||||||
let frequencyParam, filter;
|
let frequencyParam, filter;
|
||||||
if (model === 'ladder') {
|
if (model === 'ladder') {
|
||||||
filter = getWorklet(context, 'ladder-processor', { frequency, q, drive });
|
filter = getPooledWorklet(context, 'ladder-processor', { frequency, q, drive });
|
||||||
frequencyParam = filter.parameters.get('frequency');
|
frequencyParam = filter.parameters.get('frequency');
|
||||||
} else {
|
} else {
|
||||||
filter = context.createBiquadFilter();
|
const factory = () => context.createBiquadFilter();
|
||||||
|
filter = getNodeFromPool('filter', factory);
|
||||||
filter.type = type;
|
filter.type = type;
|
||||||
filter.Q.value = q;
|
const now = context.currentTime;
|
||||||
filter.frequency.value = frequency;
|
Object.entries({ Q: q, frequency }).forEach(([key, value]) => {
|
||||||
|
filter[key].setValueAtTime(value, now);
|
||||||
|
});
|
||||||
frequencyParam = filter.frequency;
|
frequencyParam = filter.frequency;
|
||||||
}
|
}
|
||||||
const envelopeValues = [params.attack, params.decay, params.sustain, params.release];
|
const envelopeValues = [params.attack, params.decay, params.sustain, params.release];
|
||||||
@@ -548,8 +567,9 @@ export const getDistortionAlgorithm = (algo) => {
|
|||||||
return distortionAlgorithms[name];
|
return distortionAlgorithms[name];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDistortion = (distort, postgain, algorithm) => {
|
export const getDistortion = (fullParams) => {
|
||||||
return getWorklet(getAudioContext(), 'distort-processor', { distort, postgain }, { processorOptions: { algorithm } });
|
const { algorithm, ...params } = fullParams;
|
||||||
|
return getPooledWorklet(getAudioContext(), 'distort-processor', params, {}, { algorithm });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getFrequencyFromValue = (value, defaultNote = 36) => {
|
export const getFrequencyFromValue = (value, defaultNote = 36) => {
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
nodePools.mjs - Helper functions related to pooling and re-using audio nodes
|
||||||
|
|
||||||
|
Copyright (C) 2025 Strudel contributors - see <https://codeberg.org/uzu/strudel/src/branch/main/packages/superdough/nodePools.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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const nodePools = new Map();
|
||||||
|
const POOL_KEY = Symbol('nodePoolKey');
|
||||||
|
const IS_WORKLET_DEAD = Symbol('nodePoolIsWorkletDead');
|
||||||
|
const MAX_POOL_SIZE = 64;
|
||||||
|
|
||||||
|
export const isPoolable = (node) => !!node[POOL_KEY];
|
||||||
|
|
||||||
|
export const releaseNodeToPool = (node) => {
|
||||||
|
node.disconnect();
|
||||||
|
if (node instanceof AudioScheduledSourceNode) {
|
||||||
|
// not reusable
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (node[IS_WORKLET_DEAD]) {
|
||||||
|
// Worklet already terminated, don't pool it
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const key = node[POOL_KEY];
|
||||||
|
if (key == null) return;
|
||||||
|
const pool = nodePools.get(key) ?? [];
|
||||||
|
if (pool.length < MAX_POOL_SIZE) {
|
||||||
|
pool.push(new WeakRef(node));
|
||||||
|
nodePools.set(key, pool);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const markWorkletAsDead = (worklet) => (worklet[IS_WORKLET_DEAD] = true);
|
||||||
|
|
||||||
|
// Attempt to get node from the pool. If this fails, fall back
|
||||||
|
// to building it with the factory
|
||||||
|
export const getNodeFromPool = (key, factory, params = {}) => {
|
||||||
|
const pool = nodePools.get(key) ?? [];
|
||||||
|
let node;
|
||||||
|
while (pool.length) {
|
||||||
|
const ref = pool.pop();
|
||||||
|
node = ref?.deref();
|
||||||
|
if (node != null && !node[IS_WORKLET_DEAD]) break;
|
||||||
|
}
|
||||||
|
if (node == null || node[IS_WORKLET_DEAD]) {
|
||||||
|
node = factory();
|
||||||
|
}
|
||||||
|
node[POOL_KEY] = key;
|
||||||
|
const paramMap = new Map();
|
||||||
|
if (node instanceof AudioWorkletNode) {
|
||||||
|
for (const [name, param] of node.parameters.entries()) {
|
||||||
|
paramMap.set(name, param);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (const name of Object.getOwnPropertyNames(node)) {
|
||||||
|
const value = node[name];
|
||||||
|
if (value instanceof AudioParam) {
|
||||||
|
paramMap.set(name, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const now = node.context?.currentTime ?? 0;
|
||||||
|
paramMap.forEach((param, name) => {
|
||||||
|
param.cancelScheduledValues(now);
|
||||||
|
// Set values from `params` or restore defaults
|
||||||
|
const target = params[name] !== undefined ? params[name] : param.defaultValue;
|
||||||
|
param.setValueAtTime(target, now);
|
||||||
|
});
|
||||||
|
return node;
|
||||||
|
};
|
||||||
@@ -1,22 +1,23 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
import { makeReusable } from './worklets-common.mjs';
|
||||||
|
|
||||||
// sourced from https://github.com/olvb/phaze/tree/master?tab=readme-ov-file
|
// sourced from https://github.com/olvb/phaze/tree/master?tab=readme-ov-file
|
||||||
const WEBAUDIO_BLOCK_SIZE = 128;
|
const WEBAUDIO_BLOCK_SIZE = 128;
|
||||||
|
|
||||||
/** Overlap-Add Node */
|
/** Overlap-Add Node */
|
||||||
class OLAProcessor extends AudioWorkletProcessor {
|
class OLAProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
super(options);
|
super(options);
|
||||||
this.started = false;
|
|
||||||
this.nbInputs = options.numberOfInputs;
|
this.nbInputs = options.numberOfInputs;
|
||||||
this.nbOutputs = options.numberOfOutputs;
|
this.nbOutputs = options.numberOfOutputs;
|
||||||
|
|
||||||
this.blockSize = options.processorOptions.blockSize;
|
this.blockSize = options.processorOptions.blockSize;
|
||||||
// TODO for now, the only support hop size is the size of a web audio block
|
// TODO for now, the only support hop size is the size of a web audio block
|
||||||
this.hopSize = WEBAUDIO_BLOCK_SIZE;
|
this.hopSize = WEBAUDIO_BLOCK_SIZE;
|
||||||
|
|
||||||
this.nbOverlaps = this.blockSize / this.hopSize;
|
this.nbOverlaps = this.blockSize / this.hopSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialize() {
|
||||||
// pre-allocate input buffers (will be reallocated if needed)
|
// pre-allocate input buffers (will be reallocated if needed)
|
||||||
this.inputBuffers = new Array(this.nbInputs);
|
this.inputBuffers = new Array(this.nbInputs);
|
||||||
this.inputBuffersHead = new Array(this.nbInputs);
|
this.inputBuffersHead = new Array(this.nbInputs);
|
||||||
@@ -54,7 +55,6 @@ class OLAProcessor extends AudioWorkletProcessor {
|
|||||||
|
|
||||||
allocateInputChannels(inputIndex, nbChannels) {
|
allocateInputChannels(inputIndex, nbChannels) {
|
||||||
// allocate input buffers
|
// allocate input buffers
|
||||||
|
|
||||||
this.inputBuffers[inputIndex] = new Array(nbChannels);
|
this.inputBuffers[inputIndex] = new Array(nbChannels);
|
||||||
for (let i = 0; i < nbChannels; i++) {
|
for (let i = 0; i < nbChannels; i++) {
|
||||||
this.inputBuffers[inputIndex][i] = new Float32Array(this.blockSize + WEBAUDIO_BLOCK_SIZE);
|
this.inputBuffers[inputIndex][i] = new Float32Array(this.blockSize + WEBAUDIO_BLOCK_SIZE);
|
||||||
@@ -157,15 +157,8 @@ class OLAProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
process(inputs, outputs, params) {
|
processActive(inputs, outputs, params) {
|
||||||
const input = inputs[0];
|
|
||||||
const hasInput = !(input[0] === undefined);
|
|
||||||
if (this.started && !hasInput) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.started = hasInput;
|
|
||||||
this.reallocateChannelsIfNeeded(inputs, outputs);
|
this.reallocateChannelsIfNeeded(inputs, outputs);
|
||||||
|
|
||||||
this.readInputs(inputs);
|
this.readInputs(inputs);
|
||||||
this.shiftInputBuffers();
|
this.shiftInputBuffers();
|
||||||
this.prepareInputBuffersToSend();
|
this.prepareInputBuffersToSend();
|
||||||
@@ -173,7 +166,6 @@ class OLAProcessor extends AudioWorkletProcessor {
|
|||||||
this.handleOutputBuffersToRetrieve();
|
this.handleOutputBuffersToRetrieve();
|
||||||
this.writeOutputs(outputs);
|
this.writeOutputs(outputs);
|
||||||
this.shiftOutputBuffers();
|
this.shiftOutputBuffers();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,16 @@ import './reverb.mjs';
|
|||||||
import './vowel.mjs';
|
import './vowel.mjs';
|
||||||
import { nanFallback, _mod, cycleToSeconds, pickAndRename } from './util.mjs';
|
import { nanFallback, _mod, cycleToSeconds, pickAndRename } from './util.mjs';
|
||||||
import workletsUrl from './worklets.mjs?audioworklet';
|
import workletsUrl from './worklets.mjs?audioworklet';
|
||||||
import { createFilter, gainNode, getCompressor, getDistortion, getLfo, getWorklet, effectSend } from './helpers.mjs';
|
import {
|
||||||
|
createFilter,
|
||||||
|
effectSend,
|
||||||
|
gainNode,
|
||||||
|
getCompressor,
|
||||||
|
getDistortion,
|
||||||
|
getLfo,
|
||||||
|
getPooledWorklet,
|
||||||
|
} from './helpers.mjs';
|
||||||
|
import { getNodeFromPool, isPoolable, releaseNodeToPool } from './nodePools.mjs';
|
||||||
import { map } from 'nanostores';
|
import { map } from 'nanostores';
|
||||||
import { logger } from './logger.mjs';
|
import { logger } from './logger.mjs';
|
||||||
import { loadBuffer } from './sampler.mjs';
|
import { loadBuffer } from './sampler.mjs';
|
||||||
@@ -294,7 +303,7 @@ function getPhaser(time, end, frequency = 1, depth = 0.5, centerFrequency = 1000
|
|||||||
let fOffset = 0;
|
let fOffset = 0;
|
||||||
const filterChain = [];
|
const filterChain = [];
|
||||||
for (let i = 0; i < numStages; i++) {
|
for (let i = 0; i < numStages; i++) {
|
||||||
const filter = ac.createBiquadFilter();
|
const filter = getNodeFromPool('filter', () => ac.createBiquadFilter());
|
||||||
filter.type = 'notch';
|
filter.type = 'notch';
|
||||||
filter.gain.value = 1;
|
filter.gain.value = 1;
|
||||||
filter.frequency.value = centerFrequency + fOffset;
|
filter.frequency.value = centerFrequency + fOffset;
|
||||||
@@ -506,7 +515,7 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
|||||||
} else if (getSound(s)) {
|
} else if (getSound(s)) {
|
||||||
const { onTrigger } = getSound(s);
|
const { onTrigger } = getSound(s);
|
||||||
const onEnded = () => {
|
const onEnded = () => {
|
||||||
audioNodes.forEach((n) => n?.disconnect());
|
audioNodes.forEach((n) => (isPoolable(n) ? releaseNodeToPool(n) : n?.disconnect()));
|
||||||
activeSoundSources.delete(chainID);
|
activeSoundSources.delete(chainID);
|
||||||
};
|
};
|
||||||
const soundHandle = await onTrigger(t, value, onEnded, cps);
|
const soundHandle = await onTrigger(t, value, onEnded, cps);
|
||||||
@@ -530,7 +539,10 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
|||||||
}
|
}
|
||||||
const chain = []; // audio nodes that will be connected to each other sequentially
|
const chain = []; // audio nodes that will be connected to each other sequentially
|
||||||
chain.push(sourceNode);
|
chain.push(sourceNode);
|
||||||
stretch !== undefined && chain.push(getWorklet(ac, 'phase-vocoder-processor', { pitchFactor: stretch }));
|
stretch !== undefined &&
|
||||||
|
chain.push(
|
||||||
|
getPooledWorklet(ac, 'phase-vocoder-processor', { pitchFactor: stretch, begin: t, end: endWithRelease }),
|
||||||
|
);
|
||||||
|
|
||||||
// gain stage
|
// gain stage
|
||||||
chain.push(gainNode(gain));
|
chain.push(gainNode(gain));
|
||||||
@@ -643,10 +655,13 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// effects
|
// effects
|
||||||
coarse !== undefined && chain.push(getWorklet(ac, 'coarse-processor', { coarse }));
|
coarse !== undefined &&
|
||||||
crush !== undefined && chain.push(getWorklet(ac, 'crush-processor', { crush }));
|
chain.push(getPooledWorklet(ac, 'coarse-processor', { coarse, begin: t, end: endWithRelease }));
|
||||||
shape !== undefined && chain.push(getWorklet(ac, 'shape-processor', { shape, postgain: shapevol }));
|
crush !== undefined && chain.push(getPooledWorklet(ac, 'crush-processor', { crush, begin: t, end: endWithRelease }));
|
||||||
distort !== undefined && chain.push(getDistortion(distort, distortvol, distorttype));
|
shape !== undefined &&
|
||||||
|
chain.push(getPooledWorklet(ac, 'shape-processor', { shape, postgain: shapevol, begin: t, end: endWithRelease }));
|
||||||
|
distort !== undefined &&
|
||||||
|
chain.push(getDistortion({ distort, postgain: distortvol, algorithm: distorttype, begin: t, end: endWithRelease }));
|
||||||
|
|
||||||
if (tremolosync != null) {
|
if (tremolosync != null) {
|
||||||
tremolo = cps * tremolosync;
|
tremolo = cps * tremolosync;
|
||||||
|
|||||||
@@ -1,3 +1,12 @@
|
|||||||
|
/*
|
||||||
|
superdoughoutput.mjs - Output controller for superdough
|
||||||
|
|
||||||
|
Handles setting up and mixing to the outputs as well as all global (orbit) effects
|
||||||
|
|
||||||
|
Copyright (C) 2025 Strudel contributors - see <https://codeberg.org/uzu/strudel/src/branch/main/packages/superdough/superdoughoutput.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 { effectSend, getWorklet, webAudioTimeout } from './helpers.mjs';
|
import { effectSend, getWorklet, webAudioTimeout } from './helpers.mjs';
|
||||||
import { errorLogger } from './logger.mjs';
|
import { errorLogger } from './logger.mjs';
|
||||||
import { clamp } from './util.mjs';
|
import { clamp } from './util.mjs';
|
||||||
|
|||||||
@@ -9,14 +9,15 @@ import {
|
|||||||
getLfo,
|
getLfo,
|
||||||
getParamADSR,
|
getParamADSR,
|
||||||
getPitchEnvelope,
|
getPitchEnvelope,
|
||||||
|
getPooledWorklet,
|
||||||
getVibratoOscillator,
|
getVibratoOscillator,
|
||||||
getWorklet,
|
|
||||||
noises,
|
noises,
|
||||||
releaseAudioNode,
|
releaseAudioNode,
|
||||||
webAudioTimeout,
|
webAudioTimeout,
|
||||||
} from './helpers.mjs';
|
} from './helpers.mjs';
|
||||||
import { logger } from './logger.mjs';
|
import { logger } from './logger.mjs';
|
||||||
import { getNoiseMix, getNoiseOscillator } from './noise.mjs';
|
import { getNoiseMix, getNoiseOscillator } from './noise.mjs';
|
||||||
|
import { releaseNodeToPool } from './nodePools.mjs';
|
||||||
|
|
||||||
const waveforms = ['triangle', 'square', 'sawtooth', 'sine', 'user'];
|
const waveforms = ['triangle', 'square', 'sawtooth', 'sine', 'user'];
|
||||||
const waveformAliases = [
|
const waveformAliases = [
|
||||||
@@ -164,22 +165,14 @@ export function registerSynthSounds() {
|
|||||||
const end = holdend + release + 0.01;
|
const end = holdend + release + 0.01;
|
||||||
const voices = clamp(unison, 1, 100);
|
const voices = clamp(unison, 1, 100);
|
||||||
let panspread = voices > 1 ? clamp(spread, 0, 1) : 0;
|
let panspread = voices > 1 ? clamp(spread, 0, 1) : 0;
|
||||||
let o = getWorklet(
|
const params = {
|
||||||
ac,
|
frequency,
|
||||||
'supersaw-oscillator',
|
begin,
|
||||||
{
|
end,
|
||||||
frequency,
|
freqspread: detune,
|
||||||
begin,
|
panspread,
|
||||||
end,
|
};
|
||||||
freqspread: detune,
|
const o = getPooledWorklet(ac, 'supersaw-oscillator', params, { outputChannelCount: [2] }, { voices });
|
||||||
voices,
|
|
||||||
panspread,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
outputChannelCount: [2],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const gainAdjustment = 1 / Math.sqrt(voices);
|
const gainAdjustment = 1 / Math.sqrt(voices);
|
||||||
getPitchEnvelope(o.parameters.get('detune'), value, begin, holdend);
|
getPitchEnvelope(o.parameters.get('detune'), value, begin, holdend);
|
||||||
const vibratoOscillator = getVibratoOscillator(o.parameters.get('detune'), value, begin);
|
const vibratoOscillator = getVibratoOscillator(o.parameters.get('detune'), value, begin);
|
||||||
@@ -192,7 +185,7 @@ export function registerSynthSounds() {
|
|||||||
let timeoutNode = webAudioTimeout(
|
let timeoutNode = webAudioTimeout(
|
||||||
ac,
|
ac,
|
||||||
() => {
|
() => {
|
||||||
releaseAudioNode(o);
|
releaseNodeToPool(o);
|
||||||
onended();
|
onended();
|
||||||
fm?.stop();
|
fm?.stop();
|
||||||
vibratoOscillator?.stop();
|
vibratoOscillator?.stop();
|
||||||
@@ -246,7 +239,7 @@ export function registerSynthSounds() {
|
|||||||
const holdend = begin + duration;
|
const holdend = begin + duration;
|
||||||
const end = holdend + release + 0.01;
|
const end = holdend + release + 0.01;
|
||||||
|
|
||||||
let o = getWorklet(
|
let o = getPooledWorklet(
|
||||||
ac,
|
ac,
|
||||||
'byte-beat-processor',
|
'byte-beat-processor',
|
||||||
{
|
{
|
||||||
@@ -313,7 +306,7 @@ export function registerSynthSounds() {
|
|||||||
);
|
);
|
||||||
const holdend = begin + duration;
|
const holdend = begin + duration;
|
||||||
const end = holdend + release + 0.01;
|
const end = holdend + release + 0.01;
|
||||||
let o = getWorklet(
|
let o = getPooledWorklet(
|
||||||
ac,
|
ac,
|
||||||
'pulse-oscillator',
|
'pulse-oscillator',
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import {
|
|||||||
getFrequencyFromValue,
|
getFrequencyFromValue,
|
||||||
getParamADSR,
|
getParamADSR,
|
||||||
getPitchEnvelope,
|
getPitchEnvelope,
|
||||||
|
getPooledWorklet,
|
||||||
getVibratoOscillator,
|
getVibratoOscillator,
|
||||||
getWorklet,
|
|
||||||
releaseAudioNode,
|
|
||||||
webAudioTimeout,
|
webAudioTimeout,
|
||||||
} from './helpers.mjs';
|
} from './helpers.mjs';
|
||||||
|
import { releaseNodeToPool } from './nodePools.mjs';
|
||||||
import { logger } from './logger.mjs';
|
import { logger } from './logger.mjs';
|
||||||
|
|
||||||
export const Warpmode = Object.freeze({
|
export const Warpmode = Object.freeze({
|
||||||
@@ -225,24 +225,19 @@ export async function onTriggerSynth(t, value, onended, tables, cps, frameLen) {
|
|||||||
}
|
}
|
||||||
const endWithRelease = holdEnd + release;
|
const endWithRelease = holdEnd + release;
|
||||||
const envEnd = endWithRelease + 0.01;
|
const envEnd = endWithRelease + 0.01;
|
||||||
const source = getWorklet(
|
payload.voices = Math.max(value.unison ?? 1, 1);
|
||||||
ac,
|
const params = {
|
||||||
'wavetable-oscillator-processor',
|
begin: t,
|
||||||
{
|
end: envEnd,
|
||||||
begin: t,
|
frequency,
|
||||||
end: envEnd,
|
freqspread: value.detune,
|
||||||
frequency,
|
position: value.wt,
|
||||||
freqspread: value.detune,
|
warp: value.warp,
|
||||||
position: value.wt,
|
warpMode: warpmode,
|
||||||
warp: value.warp,
|
panspread: value.spread,
|
||||||
warpMode: warpmode,
|
phaserand: (value.wtphaserand ?? value.unison > 1) ? 1 : 0,
|
||||||
voices: Math.max(value.unison ?? 1, 1),
|
};
|
||||||
panspread: value.spread,
|
const source = getPooledWorklet(ac, 'wavetable-oscillator-processor', params, { outputChannelCount: [2] }, payload);
|
||||||
phaserand: (value.wtphaserand ?? value.unison > 1) ? 1 : 0,
|
|
||||||
},
|
|
||||||
{ outputChannelCount: [2] },
|
|
||||||
);
|
|
||||||
source.port.postMessage({ type: 'table', payload });
|
|
||||||
if (ac.currentTime > t) {
|
if (ac.currentTime > t) {
|
||||||
logger(`[wavetable] still loading sound "${s}:${n}"`, 'highlight');
|
logger(`[wavetable] still loading sound "${s}:${n}"`, 'highlight');
|
||||||
return;
|
return;
|
||||||
@@ -319,7 +314,7 @@ export async function onTriggerSynth(t, value, onended, tables, cps, frameLen) {
|
|||||||
const timeoutNode = webAudioTimeout(
|
const timeoutNode = webAudioTimeout(
|
||||||
ac,
|
ac,
|
||||||
() => {
|
() => {
|
||||||
releaseAudioNode(source);
|
releaseNodeToPool(source);
|
||||||
vibratoOscillator?.stop();
|
vibratoOscillator?.stop();
|
||||||
fm?.stop();
|
fm?.stop();
|
||||||
wtPosModulators?.disconnect();
|
wtPosModulators?.disconnect();
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
worklets-common.mjs - Common worklet code
|
||||||
|
|
||||||
|
Copyright (C) 2025 Strudel contributors - see <https://codeberg.org/uzu/strudel/src/branch/main/packages/superdough/worklets-common.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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Mixin to make class "reusable" (i.e. have a begin and end and properly handle `initialize` messages)
|
||||||
|
export const makeReusable = (Base) =>
|
||||||
|
class extends Base {
|
||||||
|
static get parameterDescriptors() {
|
||||||
|
return [
|
||||||
|
...(super.parameterDescriptors ?? []),
|
||||||
|
{
|
||||||
|
name: 'begin',
|
||||||
|
defaultValue: 0,
|
||||||
|
min: 0,
|
||||||
|
max: Number.POSITIVE_INFINITY,
|
||||||
|
automationRate: 'k-rate',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'end',
|
||||||
|
defaultValue: 0,
|
||||||
|
min: 0,
|
||||||
|
max: Number.POSITIVE_INFINITY,
|
||||||
|
automationRate: 'k-rate',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(options) {
|
||||||
|
super(options);
|
||||||
|
this.isAlive = true;
|
||||||
|
this.graceSeconds = options?.processorOptions?.graceSeconds ?? 0.5;
|
||||||
|
|
||||||
|
this.port.onmessage = (e) => {
|
||||||
|
const { type, payload } = e.data || {};
|
||||||
|
if (type === 'initialize') {
|
||||||
|
this.initialize(payload);
|
||||||
|
} else if (this.handlePortMessage) {
|
||||||
|
this.handlePortMessage(type, payload, e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
initialize(_options) {
|
||||||
|
// defined on subclasses
|
||||||
|
}
|
||||||
|
|
||||||
|
processActive(_inputs, _outputs, _params) {
|
||||||
|
// processing subclasses should do when we're between begin/end
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
process(inputs, outputs, params) {
|
||||||
|
const begin = params.begin[0];
|
||||||
|
const end = params.end[0];
|
||||||
|
if (currentTime >= end + this.graceSeconds) {
|
||||||
|
if (this.isAlive) {
|
||||||
|
this.port.postMessage({ type: 'died' });
|
||||||
|
this.isAlive = false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (currentTime < begin || currentTime >= end) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return this.processActive(inputs, outputs, params);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
// coarse, crush, and shape processors adapted from dktr0's webdirt: https://github.com/dktr0/WebDirt/blob/5ce3d698362c54d6e1b68acc47eb2955ac62c793/dist/AudioWorklets.js
|
// 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
|
// LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE
|
||||||
// TOFIX: THIS FILE DOES NOT SUPPORT IMPORTS ON DEPOLYMENT
|
// TOFIX: THIS FILE DOES NOT SUPPORT IMPORTS ON DEPLOYMENT
|
||||||
|
|
||||||
import OLAProcessor from './ola-processor';
|
import OLAProcessor from './ola-processor';
|
||||||
import FFT from './fft.js';
|
import FFT from './fft.js';
|
||||||
import { getDistortionAlgorithm } from './helpers.mjs';
|
import { getDistortionAlgorithm } from './helpers.mjs';
|
||||||
|
import { makeReusable } from './worklets-common.mjs';
|
||||||
|
|
||||||
const blockSize = 128;
|
const blockSize = 128;
|
||||||
const PI = Math.PI;
|
const PI = Math.PI;
|
||||||
@@ -108,12 +109,12 @@ const waveshapes = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const waveShapeNames = Object.keys(waveshapes);
|
const waveShapeNames = Object.keys(waveshapes);
|
||||||
class LFOProcessor extends AudioWorkletProcessor {
|
|
||||||
|
class LFOProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
{ name: 'begin', defaultValue: 0 },
|
...super.parameterDescriptors,
|
||||||
{ name: 'time', defaultValue: 0 },
|
{ name: 'time', defaultValue: 0 },
|
||||||
{ name: 'end', defaultValue: 0 },
|
|
||||||
{ name: 'frequency', defaultValue: 0.5 },
|
{ name: 'frequency', defaultValue: 0.5 },
|
||||||
{ name: 'skew', defaultValue: 0.5 },
|
{ name: 'skew', defaultValue: 0.5 },
|
||||||
{ name: 'depth', defaultValue: 1 },
|
{ name: 'depth', defaultValue: 1 },
|
||||||
@@ -126,9 +127,8 @@ class LFOProcessor extends AudioWorkletProcessor {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
initialize() {
|
||||||
super();
|
this.phase = null;
|
||||||
this.phase;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
incrementPhase(dt) {
|
incrementPhase(dt) {
|
||||||
@@ -138,15 +138,7 @@ class LFOProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
process(_inputs, outputs, parameters) {
|
processActive(_inputs, outputs, parameters) {
|
||||||
const begin = parameters['begin'][0];
|
|
||||||
if (currentTime >= parameters.end[0]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (currentTime <= begin) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
const frequency = parameters['frequency'][0];
|
const frequency = parameters['frequency'][0];
|
||||||
|
|
||||||
@@ -182,26 +174,14 @@ class LFOProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
registerProcessor('lfo-processor', LFOProcessor);
|
registerProcessor('lfo-processor', LFOProcessor);
|
||||||
|
|
||||||
class CoarseProcessor extends AudioWorkletProcessor {
|
class CoarseProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [{ name: 'coarse', defaultValue: 1 }];
|
return [...super.parameterDescriptors, { name: 'coarse', defaultValue: 1 }];
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
processActive(inputs, outputs, parameters) {
|
||||||
super();
|
|
||||||
this.started = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
process(inputs, outputs, parameters) {
|
|
||||||
const input = inputs[0];
|
const input = inputs[0];
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
|
|
||||||
const hasInput = !(input[0] === undefined);
|
|
||||||
if (this.started && !hasInput) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.started = hasInput;
|
|
||||||
|
|
||||||
let coarse = parameters.coarse[0] ?? 0;
|
let coarse = parameters.coarse[0] ?? 0;
|
||||||
coarse = Math.max(1, coarse);
|
coarse = Math.max(1, coarse);
|
||||||
for (let n = 0; n < blockSize; n++) {
|
for (let n = 0; n < blockSize; n++) {
|
||||||
@@ -214,26 +194,14 @@ class CoarseProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
registerProcessor('coarse-processor', CoarseProcessor);
|
registerProcessor('coarse-processor', CoarseProcessor);
|
||||||
|
|
||||||
class CrushProcessor extends AudioWorkletProcessor {
|
class CrushProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [{ name: 'crush', defaultValue: 0 }];
|
return [...super.parameterDescriptors, { name: 'crush', defaultValue: 0 }];
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
processActive(inputs, outputs, parameters) {
|
||||||
super();
|
|
||||||
this.started = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
process(inputs, outputs, parameters) {
|
|
||||||
const input = inputs[0];
|
const input = inputs[0];
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
|
|
||||||
const hasInput = !(input[0] === undefined);
|
|
||||||
if (this.started && !hasInput) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.started = hasInput;
|
|
||||||
|
|
||||||
let crush = parameters.crush[0] ?? 8;
|
let crush = parameters.crush[0] ?? 8;
|
||||||
crush = Math.max(1, crush);
|
crush = Math.max(1, crush);
|
||||||
|
|
||||||
@@ -248,29 +216,14 @@ class CrushProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
registerProcessor('crush-processor', CrushProcessor);
|
registerProcessor('crush-processor', CrushProcessor);
|
||||||
|
|
||||||
class ShapeProcessor extends AudioWorkletProcessor {
|
class ShapeProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [...super.parameterDescriptors, { name: 'shape', defaultValue: 0 }, { name: 'postgain', defaultValue: 1 }];
|
||||||
{ name: 'shape', defaultValue: 0 },
|
|
||||||
{ name: 'postgain', defaultValue: 1 },
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
processActive(inputs, outputs, parameters) {
|
||||||
super();
|
|
||||||
this.started = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
process(inputs, outputs, parameters) {
|
|
||||||
const input = inputs[0];
|
const input = inputs[0];
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
|
|
||||||
const hasInput = !(input[0] === undefined);
|
|
||||||
if (this.started && !hasInput) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.started = hasInput;
|
|
||||||
|
|
||||||
let shape = parameters.shape[0];
|
let shape = parameters.shape[0];
|
||||||
shape = shape < 1 ? shape : 1.0 - 4e-10;
|
shape = shape < 1 ? shape : 1.0 - 4e-10;
|
||||||
shape = (2.0 * shape) / (1.0 - shape);
|
shape = (2.0 * shape) / (1.0 - shape);
|
||||||
@@ -354,17 +307,17 @@ class DJFProcessor extends AudioWorkletProcessor {
|
|||||||
registerProcessor('djf-processor', DJFProcessor);
|
registerProcessor('djf-processor', DJFProcessor);
|
||||||
|
|
||||||
//adapted from https://github.com/TheBouteillacBear/webaudioworklet-wasm?tab=MIT-1-ov-file
|
//adapted from https://github.com/TheBouteillacBear/webaudioworklet-wasm?tab=MIT-1-ov-file
|
||||||
class LadderProcessor extends AudioWorkletProcessor {
|
class LadderProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
|
...super.parameterDescriptors,
|
||||||
{ name: 'frequency', defaultValue: 500 },
|
{ name: 'frequency', defaultValue: 500 },
|
||||||
{ name: 'q', defaultValue: 1 },
|
{ name: 'q', defaultValue: 1 },
|
||||||
{ name: 'drive', defaultValue: 0.69 },
|
{ name: 'drive', defaultValue: 0.69 },
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
initialize(_options) {
|
||||||
super();
|
|
||||||
this.started = false;
|
this.started = false;
|
||||||
this.p0 = [0, 0];
|
this.p0 = [0, 0];
|
||||||
this.p1 = [0, 0];
|
this.p1 = [0, 0];
|
||||||
@@ -375,17 +328,9 @@ class LadderProcessor extends AudioWorkletProcessor {
|
|||||||
this.p34 = [0, 0];
|
this.p34 = [0, 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
process(inputs, outputs, parameters) {
|
processActive(inputs, outputs, parameters) {
|
||||||
const input = inputs[0];
|
const input = inputs[0];
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
|
|
||||||
const hasInput = !(input[0] === undefined);
|
|
||||||
if (this.started && !hasInput) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.started = hasInput;
|
|
||||||
|
|
||||||
const resonance = parameters.q[0];
|
const resonance = parameters.q[0];
|
||||||
const drive = clamp(Math.exp(parameters.drive[0]), 0.1, 2000);
|
const drive = clamp(Math.exp(parameters.drive[0]), 0.1, 2000);
|
||||||
|
|
||||||
@@ -418,29 +363,18 @@ class LadderProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
registerProcessor('ladder-processor', LadderProcessor);
|
registerProcessor('ladder-processor', LadderProcessor);
|
||||||
|
|
||||||
class DistortProcessor extends AudioWorkletProcessor {
|
class DistortProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [...super.parameterDescriptors, { name: 'distort', defaultValue: 0 }, { name: 'postgain', defaultValue: 1 }];
|
||||||
{ name: 'distort', defaultValue: 0 },
|
|
||||||
{ name: 'postgain', defaultValue: 1 },
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor({ processorOptions }) {
|
initialize(options) {
|
||||||
super();
|
this.algorithm = getDistortionAlgorithm(options?.algorithm);
|
||||||
this.started = false;
|
|
||||||
this.algorithm = getDistortionAlgorithm(processorOptions.algorithm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process(inputs, outputs, parameters) {
|
processActive(inputs, outputs, parameters) {
|
||||||
const input = inputs[0];
|
const input = inputs[0];
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
|
|
||||||
const hasInput = !(input[0] === undefined);
|
|
||||||
if (this.started && !hasInput) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.started = hasInput;
|
|
||||||
for (let n = 0; n < blockSize; n++) {
|
for (let n = 0; n < blockSize; n++) {
|
||||||
const postgain = clamp(pv(parameters.postgain, n), 0.001, 1);
|
const postgain = clamp(pv(parameters.postgain, n), 0.001, 1);
|
||||||
const shape = Math.expm1(pv(parameters.distort, n));
|
const shape = Math.expm1(pv(parameters.distort, n));
|
||||||
@@ -455,33 +389,15 @@ class DistortProcessor extends AudioWorkletProcessor {
|
|||||||
registerProcessor('distort-processor', DistortProcessor);
|
registerProcessor('distort-processor', DistortProcessor);
|
||||||
|
|
||||||
// SUPERSAW
|
// SUPERSAW
|
||||||
class SuperSawOscillatorProcessor extends AudioWorkletProcessor {
|
class SuperSawOscillatorProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this.phase = [];
|
|
||||||
}
|
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
{
|
...super.parameterDescriptors,
|
||||||
name: 'begin',
|
|
||||||
defaultValue: 0,
|
|
||||||
max: Number.POSITIVE_INFINITY,
|
|
||||||
min: 0,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
name: 'end',
|
|
||||||
defaultValue: 0,
|
|
||||||
max: Number.POSITIVE_INFINITY,
|
|
||||||
min: 0,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
{
|
||||||
name: 'frequency',
|
name: 'frequency',
|
||||||
defaultValue: 440,
|
defaultValue: 440,
|
||||||
min: Number.EPSILON,
|
min: Number.EPSILON,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: 'panspread',
|
name: 'panspread',
|
||||||
defaultValue: 0.4,
|
defaultValue: 0.4,
|
||||||
@@ -498,26 +414,14 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
min: 0,
|
min: 0,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
|
||||||
name: 'voices',
|
|
||||||
defaultValue: 5,
|
|
||||||
min: 1,
|
|
||||||
automationRate: 'k-rate',
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
process(_input, outputs, params) {
|
initialize(options) {
|
||||||
if (currentTime >= params.end[0]) {
|
this.phase = [];
|
||||||
// should terminate
|
this.voices = options?.voices ?? 5;
|
||||||
return false;
|
}
|
||||||
}
|
processActive(_input, outputs, params) {
|
||||||
if (currentTime <= params.begin[0]) {
|
|
||||||
// keep alive
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
const voices = params.voices[0]; // k-rate
|
|
||||||
for (let i = 0; i < output[0].length; i++) {
|
for (let i = 0; i < output[0].length; i++) {
|
||||||
const detune = pv(params.detune, i);
|
const detune = pv(params.detune, i);
|
||||||
const freqspread = pv(params.freqspread, i);
|
const freqspread = pv(params.freqspread, i);
|
||||||
@@ -527,8 +431,8 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
let freq = pv(params.frequency, i);
|
let freq = pv(params.frequency, i);
|
||||||
// Main detuning
|
// Main detuning
|
||||||
freq = applySemitoneDetuneToFrequency(freq, detune / 100);
|
freq = applySemitoneDetuneToFrequency(freq, detune / 100);
|
||||||
const detuner = getDetuner(voices, freqspread);
|
const detuner = getDetuner(this.voices, freqspread);
|
||||||
for (let n = 0; n < voices; n++) {
|
for (let n = 0; n < this.voices; n++) {
|
||||||
// Individual voice detuning
|
// Individual voice detuning
|
||||||
const freqVoice = applySemitoneDetuneToFrequency(freq, detuner(n));
|
const freqVoice = applySemitoneDetuneToFrequency(freq, detuner(n));
|
||||||
// We must wrap this here because it is passed into sawblep below which
|
// We must wrap this here because it is passed into sawblep below which
|
||||||
@@ -572,12 +476,7 @@ function genHannWindow(length) {
|
|||||||
|
|
||||||
class PhaseVocoderProcessor extends OLAProcessor {
|
class PhaseVocoderProcessor extends OLAProcessor {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [...super.parameterDescriptors, { name: 'pitchFactor', defaultValue: 1.0 }];
|
||||||
{
|
|
||||||
name: 'pitchFactor',
|
|
||||||
defaultValue: 1.0,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
@@ -585,9 +484,13 @@ class PhaseVocoderProcessor extends OLAProcessor {
|
|||||||
blockSize: BUFFERED_BLOCK_SIZE,
|
blockSize: BUFFERED_BLOCK_SIZE,
|
||||||
};
|
};
|
||||||
super(options);
|
super(options);
|
||||||
this.timeCursor = 0;
|
|
||||||
this.fftSize = this.blockSize;
|
this.fftSize = this.blockSize;
|
||||||
this.invfftSize = 1 / this.fftSize;
|
this.invfftSize = 1 / this.fftSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialize(options) {
|
||||||
|
super.initialize(options);
|
||||||
|
this.timeCursor = 0;
|
||||||
this.hannWindow = genHannWindow(this.fftSize);
|
this.hannWindow = genHannWindow(this.fftSize);
|
||||||
// prepare FFT and pre-allocate buffers
|
// prepare FFT and pre-allocate buffers
|
||||||
this.fft = new FFT(this.fftSize);
|
this.fft = new FFT(this.fftSize);
|
||||||
@@ -718,9 +621,8 @@ class PhaseVocoderProcessor extends OLAProcessor {
|
|||||||
registerProcessor('phase-vocoder-processor', PhaseVocoderProcessor);
|
registerProcessor('phase-vocoder-processor', PhaseVocoderProcessor);
|
||||||
|
|
||||||
// Adapted from https://www.musicdsp.org/en/latest/Effects/221-band-limited-pwm-generator.html
|
// Adapted from https://www.musicdsp.org/en/latest/Effects/221-band-limited-pwm-generator.html
|
||||||
class PulseOscillatorProcessor extends AudioWorkletProcessor {
|
class PulseOscillatorProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
constructor() {
|
initialize() {
|
||||||
super();
|
|
||||||
this.phi = -PI; // phase
|
this.phi = -PI; // phase
|
||||||
this.Y0 = 0; // feedback memories
|
this.Y0 = 0; // feedback memories
|
||||||
this.Y1 = 0;
|
this.Y1 = 0;
|
||||||
@@ -732,20 +634,7 @@ class PulseOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
|
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
{
|
...super.parameterDescriptors,
|
||||||
name: 'begin',
|
|
||||||
defaultValue: 0,
|
|
||||||
max: Number.POSITIVE_INFINITY,
|
|
||||||
min: 0,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
name: 'end',
|
|
||||||
defaultValue: 0,
|
|
||||||
max: Number.POSITIVE_INFINITY,
|
|
||||||
min: 0,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
{
|
||||||
name: 'frequency',
|
name: 'frequency',
|
||||||
defaultValue: 440,
|
defaultValue: 440,
|
||||||
@@ -766,20 +655,10 @@ class PulseOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
process(inputs, outputs, params) {
|
processActive(inputs, outputs, params) {
|
||||||
if (this.disconnected) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (currentTime <= params.begin[0]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (currentTime >= params.end[0]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const output = outputs[0];
|
const output = outputs[0];
|
||||||
let env = 1,
|
let env = 1,
|
||||||
dphi;
|
dphi;
|
||||||
|
|
||||||
for (let i = 0; i < (output[0].length ?? 0); i++) {
|
for (let i = 0; i < (output[0].length ?? 0); i++) {
|
||||||
const pw = (1 - clamp(pv(params.pulsewidth, i), -0.99, 0.99)) * PI;
|
const pw = (1 - clamp(pv(params.pulsewidth, i), -0.99, 0.99)) * PI;
|
||||||
const detune = pv(params.detune, i);
|
const detune = pv(params.detune, i);
|
||||||
@@ -862,28 +741,23 @@ function getByteBeatFunc(codetext) {
|
|||||||
return new Function(...mathParams, 't', `return 0,\n${codetext || 0};`).bind(globalThis, ...byteBeatHelperFuncs);
|
return new Function(...mathParams, 't', `return 0,\n${codetext || 0};`).bind(globalThis, ...byteBeatHelperFuncs);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ByteBeatProcessor extends AudioWorkletProcessor {
|
class ByteBeatProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
constructor() {
|
initialize(options) {
|
||||||
super();
|
let codeText = options?.codeText;
|
||||||
this.port.onmessage = (event) => {
|
const byteBeatStartTime = options?.byteBeatStartTime;
|
||||||
let { codeText } = event.data;
|
if (byteBeatStartTime != null) {
|
||||||
const { byteBeatStartTime } = event.data;
|
this.t = 0;
|
||||||
if (byteBeatStartTime != null) {
|
this.initialOffset = Math.floor(byteBeatStartTime);
|
||||||
this.t = 0;
|
}
|
||||||
this.initialOffset = Math.floor(byteBeatStartTime);
|
//Optimization pulled from dollchan.net: https://github.com/Chasyxx/EnBeat_NEW, it seemed important
|
||||||
}
|
//Optimize code like eval(unescape(escape`XXXX`.replace(/u(..)/g,"$1%")))
|
||||||
|
codeText = codeText
|
||||||
//Optimization pulled from dollchan.net: https://github.com/Chasyxx/EnBeat_NEW, it seemed important
|
.trim()
|
||||||
//Optimize code like eval(unescape(escape`XXXX`.replace(/u(..)/g,"$1%")))
|
.replace(
|
||||||
codeText = codeText
|
/^eval\(unescape\(escape(?:`|\('|\("|\(`)(.*?)(?:`|'\)|"\)|`\)).replace\(\/u\(\.\.\)\/g,["'`]\$1%["'`]\)\)\)$/,
|
||||||
.trim()
|
(match, m1) => unescape(escape(m1).replace(/u(..)/g, '$1%')),
|
||||||
.replace(
|
);
|
||||||
/^eval\(unescape\(escape(?:`|\('|\("|\(`)(.*?)(?:`|'\)|"\)|`\)).replace\(\/u\(\.\.\)\/g,["'`]\$1%["'`]\)\)\)$/,
|
this.func = getByteBeatFunc(codeText);
|
||||||
(match, m1) => unescape(escape(m1).replace(/u(..)/g, '$1%')),
|
|
||||||
);
|
|
||||||
|
|
||||||
this.func = getByteBeatFunc(codeText);
|
|
||||||
};
|
|
||||||
this.initialOffset = 0;
|
this.initialOffset = 0;
|
||||||
this.t = null;
|
this.t = null;
|
||||||
this.func = null;
|
this.func = null;
|
||||||
@@ -891,12 +765,7 @@ class ByteBeatProcessor extends AudioWorkletProcessor {
|
|||||||
|
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
{
|
...super.parameterDescriptors,
|
||||||
name: 'begin',
|
|
||||||
defaultValue: 0,
|
|
||||||
max: Number.POSITIVE_INFINITY,
|
|
||||||
min: 0,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'frequency',
|
name: 'frequency',
|
||||||
defaultValue: 440,
|
defaultValue: 440,
|
||||||
@@ -908,25 +777,10 @@ class ByteBeatProcessor extends AudioWorkletProcessor {
|
|||||||
min: Number.NEGATIVE_INFINITY,
|
min: Number.NEGATIVE_INFINITY,
|
||||||
max: Number.POSITIVE_INFINITY,
|
max: Number.POSITIVE_INFINITY,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: 'end',
|
|
||||||
defaultValue: 0,
|
|
||||||
max: Number.POSITIVE_INFINITY,
|
|
||||||
min: 0,
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
process(inputs, outputs, params) {
|
processActive(inputs, outputs, params) {
|
||||||
if (this.disconnected) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (currentTime <= params.begin[0]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (currentTime >= params.end[0]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (this.t == null) {
|
if (this.t == null) {
|
||||||
this.t = params.begin[0] * sampleRate;
|
this.t = params.begin[0] * sampleRate;
|
||||||
}
|
}
|
||||||
@@ -952,11 +806,9 @@ class ByteBeatProcessor extends AudioWorkletProcessor {
|
|||||||
|
|
||||||
registerProcessor('byte-beat-processor', ByteBeatProcessor);
|
registerProcessor('byte-beat-processor', ByteBeatProcessor);
|
||||||
|
|
||||||
class EnvelopeProcessor extends AudioWorkletProcessor {
|
class EnvelopeProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
{ name: 'begin', defaultValue: 0 },
|
|
||||||
{ name: 'end', defaultValue: 0 },
|
|
||||||
{ name: 'attack', defaultValue: 0.005, minValue: 0 },
|
{ name: 'attack', defaultValue: 0.005, minValue: 0 },
|
||||||
{ name: 'decay', defaultValue: 0.14, minValue: 0 },
|
{ name: 'decay', defaultValue: 0.14, minValue: 0 },
|
||||||
{ name: 'sustain', defaultValue: 0, minValue: 0, maxValue: 1 },
|
{ name: 'sustain', defaultValue: 0, minValue: 0, maxValue: 1 },
|
||||||
@@ -1116,56 +968,36 @@ function brownian(x, oct = 4) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tablesCache = {};
|
const tablesCache = {};
|
||||||
class WavetableOscillatorProcessor extends AudioWorkletProcessor {
|
class WavetableOscillatorProcessor extends makeReusable(AudioWorkletProcessor) {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [
|
return [
|
||||||
{ name: 'begin', defaultValue: 0, min: 0, max: Number.POSITIVE_INFINITY },
|
...super.parameterDescriptors,
|
||||||
{ name: 'end', defaultValue: 0, min: 0, max: Number.POSITIVE_INFINITY },
|
|
||||||
{ name: 'frequency', defaultValue: 440, min: Number.EPSILON },
|
{ name: 'frequency', defaultValue: 440, min: Number.EPSILON },
|
||||||
{ name: 'detune', defaultValue: 0 },
|
{ name: 'detune', defaultValue: 0 },
|
||||||
{ name: 'freqspread', defaultValue: 0.18, min: 0 },
|
{ name: 'freqspread', defaultValue: 0.18, min: 0 },
|
||||||
{ name: 'position', defaultValue: 0, min: 0, max: 1 },
|
{ name: 'position', defaultValue: 0, min: 0, max: 1 },
|
||||||
{ name: 'warp', defaultValue: 0, min: 0, max: 1 },
|
{ name: 'warp', defaultValue: 0, min: 0, max: 1 },
|
||||||
{ name: 'warpMode', defaultValue: 0 },
|
{ name: 'warpMode', defaultValue: 0 },
|
||||||
{ name: 'voices', defaultValue: 1, min: 1, automationRate: 'k-rate' },
|
|
||||||
{ name: 'panspread', defaultValue: 0.7, min: 0, max: 1 },
|
{ name: 'panspread', defaultValue: 0.7, min: 0, max: 1 },
|
||||||
{ name: 'phaserand', defaultValue: 0, min: 0, max: 1 },
|
{ name: 'phaserand', defaultValue: 0, min: 0, max: 1 },
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(options) {
|
initialize(options) {
|
||||||
super(options);
|
this.table = null;
|
||||||
this.frameLen = 0;
|
this.frameLen = null;
|
||||||
this.numFrames = 0;
|
this.numFrames = null;
|
||||||
this.phase = [];
|
this.phase = [];
|
||||||
|
if (options?.key) {
|
||||||
this.port.onmessage = (e) => {
|
const key = options.key;
|
||||||
const { type, payload } = e.data || {};
|
this.frameLen = options.frameLen;
|
||||||
if (type === 'table') {
|
if (!tablesCache[key]) {
|
||||||
const key = payload.key;
|
tablesCache[key] = options.frames;
|
||||||
this.frameLen = payload.frameLen;
|
|
||||||
if (!tablesCache[key]) {
|
|
||||||
const tables = [payload.frames];
|
|
||||||
let table = tables[0];
|
|
||||||
for (let level = 1; level < 1; level++) {
|
|
||||||
const nextLen = table.length >> 1;
|
|
||||||
const nextTable = table.map((frame) => {
|
|
||||||
const avg = new Float32Array(nextLen);
|
|
||||||
for (let i = 0; i < nextLen; i++) {
|
|
||||||
avg[i] = (frame[2 * i] + frame[2 * i + 1]) / 2;
|
|
||||||
}
|
|
||||||
return avg;
|
|
||||||
});
|
|
||||||
tables.push(nextTable);
|
|
||||||
table = nextTable;
|
|
||||||
if (nextLen <= 32) break;
|
|
||||||
}
|
|
||||||
tablesCache[key] = tables;
|
|
||||||
}
|
|
||||||
this.tables = tablesCache[key];
|
|
||||||
this.numFrames = this.tables[0].length;
|
|
||||||
}
|
}
|
||||||
};
|
this.table = tablesCache[key];
|
||||||
|
this.numFrames = this.table.length;
|
||||||
|
}
|
||||||
|
this.voices = options?.voices ?? 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_mirror(x) {
|
_mirror(x) {
|
||||||
@@ -1310,30 +1142,14 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
return a + (b - a) * frac;
|
return a + (b - a) * frac;
|
||||||
}
|
}
|
||||||
|
|
||||||
_chooseMip(dphi) {
|
processActive(_inputs, outputs, parameters) {
|
||||||
const approxHarm = clamp(dphi, 1e-6, 64);
|
|
||||||
let level = 0;
|
|
||||||
while (level + 1 < (this.tables?.length || 1) && approxHarm < this.tables[level][0].length / 8) {
|
|
||||||
level++;
|
|
||||||
}
|
|
||||||
return level;
|
|
||||||
}
|
|
||||||
|
|
||||||
process(_inputs, outputs, parameters) {
|
|
||||||
if (currentTime >= parameters.end[0]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (currentTime <= parameters.begin[0]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const outL = outputs[0][0];
|
const outL = outputs[0][0];
|
||||||
const outR = outputs[0][1] || outputs[0][0];
|
const outR = outputs[0][1] || outputs[0][0];
|
||||||
if (!this.tables) {
|
if (!this.table) {
|
||||||
outL.fill(0);
|
outL.fill(0);
|
||||||
if (outR !== outL) outR.set(outL);
|
if (outR !== outL) outR.set(outL);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const voices = parameters.voices[0]; // k-rate
|
|
||||||
for (let i = 0; i < outL.length; i++) {
|
for (let i = 0; i < outL.length; i++) {
|
||||||
const detune = pv(parameters.detune, i);
|
const detune = pv(parameters.detune, i);
|
||||||
const freqspread = pv(parameters.freqspread, i);
|
const freqspread = pv(parameters.freqspread, i);
|
||||||
@@ -1344,14 +1160,14 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
const warpAmount = clamp(pv(parameters.warp, i), 0, 1);
|
const warpAmount = clamp(pv(parameters.warp, i), 0, 1);
|
||||||
const warpMode = pv(parameters.warpMode, i);
|
const warpMode = pv(parameters.warpMode, i);
|
||||||
const phaseRand = clamp(pv(parameters.phaserand, i), 0, 1);
|
const phaseRand = clamp(pv(parameters.phaserand, i), 0, 1);
|
||||||
const panspread = voices > 1 ? clamp(pv(parameters.panspread, i), 0, 1) : 0;
|
const panspread = this.voices > 1 ? clamp(pv(parameters.panspread, i), 0, 1) : 0;
|
||||||
const gain1 = Math.sqrt(0.5 - 0.5 * panspread);
|
const gain1 = Math.sqrt(0.5 - 0.5 * panspread);
|
||||||
const gain2 = Math.sqrt(0.5 + 0.5 * panspread);
|
const gain2 = Math.sqrt(0.5 + 0.5 * panspread);
|
||||||
let f = pv(parameters.frequency, i);
|
let f = pv(parameters.frequency, i);
|
||||||
f = applySemitoneDetuneToFrequency(f, detune / 100); // overall detune
|
f = applySemitoneDetuneToFrequency(f, detune / 100); // overall detune
|
||||||
const normalizer = 1 / Math.sqrt(voices);
|
const normalizer = 1 / Math.sqrt(this.voices);
|
||||||
const detuner = getDetuner(voices, freqspread);
|
const detuner = getDetuner(this.voices, freqspread);
|
||||||
for (let n = 0; n < voices; n++) {
|
for (let n = 0; n < this.voices; n++) {
|
||||||
const isOdd = (n & 1) == 1;
|
const isOdd = (n & 1) == 1;
|
||||||
let gainL = gain1;
|
let gainL = gain1;
|
||||||
let gainR = gain2;
|
let gainR = gain2;
|
||||||
@@ -1362,14 +1178,12 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
const fVoice = applySemitoneDetuneToFrequency(f, detuner(n)); // voice detune
|
const fVoice = applySemitoneDetuneToFrequency(f, detuner(n)); // voice detune
|
||||||
const dPhase = fVoice * INVSR;
|
const dPhase = fVoice * INVSR;
|
||||||
const level = this._chooseMip(dPhase);
|
|
||||||
const table = this.tables[level];
|
|
||||||
|
|
||||||
// warp phase then sample
|
// warp phase then sample
|
||||||
this.phase[n] = this.phase[n] ?? Math.random() * phaseRand;
|
this.phase[n] = this.phase[n] ?? Math.random() * phaseRand;
|
||||||
const ph = this._warpPhase(this.phase[n], warpAmount, warpMode);
|
const ph = this._warpPhase(this.phase[n], warpAmount, warpMode);
|
||||||
const s0 = this._sampleFrame(table[fIdx], ph);
|
const s0 = this._sampleFrame(this.table[fIdx], ph);
|
||||||
const s1 = this._sampleFrame(table[Math.min(this.numFrames - 1, fIdx + 1)], ph);
|
const s1 = this._sampleFrame(this.table[Math.min(this.numFrames - 1, fIdx + 1)], ph);
|
||||||
let s = lerp(s0, s1, interpT);
|
let s = lerp(s0, s1, interpT);
|
||||||
if (warpMode === WarpMode.FLIP && this.phase[n] < warpAmount) {
|
if (warpMode === WarpMode.FLIP && this.phase[n] < warpAmount) {
|
||||||
s = -s;
|
s = -s;
|
||||||
|
|||||||
Reference in New Issue
Block a user