Skip to content

Commit

Permalink
Merge pull request #1110 from daslyfe/phasermodulation
Browse files Browse the repository at this point in the history
Calculate phaser modulation phase based on time
  • Loading branch information
daslyfe authored May 26, 2024
2 parents cad2730 + 026cbe0 commit bf73610
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 48 deletions.
35 changes: 17 additions & 18 deletions packages/superdough/superdough.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,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
Expand Down Expand Up @@ -485,7 +484,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);
}

Expand Down
165 changes: 135 additions & 30 deletions packages/superdough/worklets.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,139 @@
// 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 } from './util.mjs';
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;
},
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 }];
Expand Down Expand Up @@ -213,34 +345,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;
}
Expand Down Expand Up @@ -340,7 +445,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;
Expand Down

0 comments on commit bf73610

Please sign in to comment.