From 9bb1039bbdc48ffde021e2d58ccbd39398ac491a Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Wed, 22 May 2024 23:57:04 -0400 Subject: [PATCH 1/2] works great --- packages/superdough/superdough.mjs | 35 +++--- packages/superdough/worklets.mjs | 166 +++++++++++++++++++++++------ 2 files changed, 153 insertions(+), 48 deletions(-) diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index c0ba96e06..2d2351dbf 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -144,26 +144,25 @@ function getDelay(orbit, delaytime, delayfeedback, t) { return delays[orbit]; } -// each orbit will have its own lfo -const phaserLFOs = {}; -function getPhaser(orbit, t, speed = 1, depth = 0.5, centerFrequency = 1000, sweep = 2000) { +function getPhaser(time, end, frequency = 1, depth = 0.5, centerFrequency = 1000, sweep = 2000) { //gain const ac = getAudioContext(); const lfoGain = ac.createGain(); - lfoGain.gain.value = sweep; - - //LFO - if (phaserLFOs[orbit] == null) { - phaserLFOs[orbit] = ac.createOscillator(); - phaserLFOs[orbit].frequency.value = speed; - phaserLFOs[orbit].type = 'sine'; - phaserLFOs[orbit].start(); - } - - phaserLFOs[orbit].connect(lfoGain); - if (phaserLFOs[orbit].frequency.value != speed) { - phaserLFOs[orbit].frequency.setValueAtTime(speed, t); - } + lfoGain.gain.value = sweep * 2; + // centerFrequency = centerFrequency * 2; + // sweep = sweep * 1.5; + + const lfo = getWorklet(ac, 'lfo-processor', { + frequency, + depth: 1, + skew: 0, + phaseoffset: 0, + time, + end, + shape: 1, + dcoffset: -0.5, + }); + lfo.connect(lfoGain); //filters const numStages = 2; //num of filters in series @@ -484,7 +483,7 @@ export const superdough = async (value, t, hapDuration) => { } // phaser if (phaser !== undefined && phaserdepth > 0) { - const phaserFX = getPhaser(orbit, t, phaser, phaserdepth, phasercenter, phasersweep); + const phaserFX = getPhaser(t, t + hapDuration, phaser, phaserdepth, phasercenter, phasersweep); chain.push(phaserFX); } diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index a1f524ca5..de1d29ea3 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1,7 +1,140 @@ // 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 - +import { clamp, _mod } from './util.mjs'; +// const clamp = (num, min, max) => Math.min(Math.max(num, min), max); const blockSize = 128; +// adjust waveshape to remove frequencies above nyquist to prevent aliasing +// referenced from https://www.kvraudio.com/forum/viewtopic.php?t=375517 +function polyBlep(phase, dt) { + // 0 <= phase < 1 + if (phase < dt) { + phase /= dt; + // 2 * (phase - phase^2/2 - 0.5) + return phase + phase - phase * phase - 1; + } + + // -1 < phase < 0 + else if (phase > 1 - dt) { + phase = (phase - 1) / dt; + // 2 * (phase^2/2 + phase + 0.5) + return phase * phase + phase + phase + 1; + } + + // 0 otherwise + else { + return 0; + } +} + +const waveshapes = { + tri(phase, skew = 0.5) { + const x = 1 - skew; + if (phase >= skew) { + return 1 / x - phase / x; + } + return phase / skew; + }, + sine(phase) { + return Math.sin(Math.PI * 2 * phase) * 0.5 + 0.5; + // return Math.sin(Math.PI * 2 * phase); + }, + ramp(phase) { + return phase; + }, + saw(phase) { + return 1 - phase; + }, + + square(phase, skew = 0.5) { + if (phase >= skew) { + return 0; + } + return 1; + }, + custom(phase, values = [0, 1]) { + const numParts = values.length - 1; + const currPart = Math.floor(phase * numParts); + + const partLength = 1 / numParts; + const startVal = clamp(values[currPart], 0, 1); + const endVal = clamp(values[currPart + 1], 0, 1); + const y2 = endVal; + const y1 = startVal; + const x1 = 0; + const x2 = partLength; + const slope = (y2 - y1) / (x2 - x1); + return slope * (phase - partLength * currPart) + startVal; + }, + sawblep(phase, dt) { + const v = 2 * phase - 1; + return v - polyBlep(phase, dt); + }, +}; + +const waveShapeNames = Object.keys(waveshapes); +class LFOProcessor extends AudioWorkletProcessor { + static get parameterDescriptors() { + return [ + { name: 'time', defaultValue: 0 }, + { name: 'end', defaultValue: 0 }, + { name: 'frequency', defaultValue: 0.5 }, + { name: 'skew', defaultValue: 0.5 }, + { name: 'depth', defaultValue: 1 }, + { name: 'phaseoffset', defaultValue: 0 }, + { name: 'shape', defaultValue: 0 }, + { name: 'dcoffset', defaultValue: 0 }, + ]; + } + + constructor() { + super(); + this.phase; + } + + incrementPhase(dt) { + this.phase += dt; + if (this.phase > 1.0) { + this.phase = this.phase - 1; + } + } + + process(inputs, outputs, parameters) { + // eslint-disable-next-line no-undef + if (currentTime >= parameters.end[0]) { + return false; + } + + const output = outputs[0]; + const frequency = parameters['frequency'][0]; + + const time = parameters['time'][0]; + const depth = parameters['depth'][0]; + const skew = parameters['skew'][0]; + const phaseoffset = parameters['phaseoffset'][0]; + + const dcoffset = parameters['dcoffset'][0]; + const shape = waveShapeNames[parameters['shape'][0]]; + + const blockSize = output[0].length ?? 0; + + if (this.phase == null) { + this.phase = _mod(time * frequency + phaseoffset, 1); + } + // eslint-disable-next-line no-undef + const dt = frequency / sampleRate; + for (let n = 0; n < blockSize; n++) { + for (let i = 0; i < output.length; i++) { + const modval = (waveshapes[shape](this.phase, skew) + dcoffset) * depth; + output[i][n] = modval; + } + this.incrementPhase(dt); + } + + return true; + } +} +registerProcessor('lfo-processor', LFOProcessor); + class CoarseProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { return [{ name: 'coarse', defaultValue: 1 }]; @@ -142,34 +275,7 @@ class DistortProcessor extends AudioWorkletProcessor { } registerProcessor('distort-processor', DistortProcessor); -// adjust waveshape to remove frequencies above nyquist to prevent aliasing -// referenced from https://www.kvraudio.com/forum/viewtopic.php?t=375517 -const polyBlep = (phase, dt) => { - // 0 <= phase < 1 - if (phase < dt) { - phase /= dt; - // 2 * (phase - phase^2/2 - 0.5) - return phase + phase - phase * phase - 1; - } - - // -1 < phase < 0 - else if (phase > 1 - dt) { - phase = (phase - 1) / dt; - // 2 * (phase^2/2 + phase + 0.5) - return phase * phase + phase + phase + 1; - } - - // 0 otherwise - else { - return 0; - } -}; - -const saw = (phase, dt) => { - const v = 2 * phase - 1; - return v - polyBlep(phase, dt); -}; - +// SUPERSAW function lerp(a, b, n) { return n * (b - a) + a; } @@ -269,7 +375,7 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor { for (let i = 0; i < output[0].length; i++) { this.phase[n] = this.phase[n] ?? Math.random(); - const v = saw(this.phase[n], dt); + const v = waveshapes.sawblep(this.phase[n], dt); output[0][i] = output[0][i] + v * gainL; output[1][i] = output[1][i] + v * gainR; From 98cb128fa2b6e224e0fe645544884a8fd804fdf1 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Thu, 23 May 2024 00:01:22 -0400 Subject: [PATCH 2/2] remove unessecary comment --- packages/superdough/worklets.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index de1d29ea3..6c44b9794 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -36,7 +36,6 @@ const waveshapes = { }, sine(phase) { return Math.sin(Math.PI * 2 * phase) * 0.5 + 0.5; - // return Math.sin(Math.PI * 2 * phase); }, ramp(phase) { return phase;