mirror of
https://github.com/eliasstepanik/strudel.git
synced 2026-01-11 21:58:37 +00:00
Merge pull request #1110 from daslyfe/phasermodulation
Calculate phaser modulation phase based on time
This commit is contained in:
commit
bf73610b0a
@ -136,26 +136,25 @@ function getDelay(orbit, delaytime, delayfeedback, t) {
|
|||||||
return delays[orbit];
|
return delays[orbit];
|
||||||
}
|
}
|
||||||
|
|
||||||
// each orbit will have its own lfo
|
function getPhaser(time, end, frequency = 1, depth = 0.5, centerFrequency = 1000, sweep = 2000) {
|
||||||
const phaserLFOs = {};
|
|
||||||
function getPhaser(orbit, t, speed = 1, depth = 0.5, centerFrequency = 1000, sweep = 2000) {
|
|
||||||
//gain
|
//gain
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
const lfoGain = ac.createGain();
|
const lfoGain = ac.createGain();
|
||||||
lfoGain.gain.value = sweep;
|
lfoGain.gain.value = sweep * 2;
|
||||||
|
// centerFrequency = centerFrequency * 2;
|
||||||
|
// sweep = sweep * 1.5;
|
||||||
|
|
||||||
//LFO
|
const lfo = getWorklet(ac, 'lfo-processor', {
|
||||||
if (phaserLFOs[orbit] == null) {
|
frequency,
|
||||||
phaserLFOs[orbit] = ac.createOscillator();
|
depth: 1,
|
||||||
phaserLFOs[orbit].frequency.value = speed;
|
skew: 0,
|
||||||
phaserLFOs[orbit].type = 'sine';
|
phaseoffset: 0,
|
||||||
phaserLFOs[orbit].start();
|
time,
|
||||||
}
|
end,
|
||||||
|
shape: 1,
|
||||||
phaserLFOs[orbit].connect(lfoGain);
|
dcoffset: -0.5,
|
||||||
if (phaserLFOs[orbit].frequency.value != speed) {
|
});
|
||||||
phaserLFOs[orbit].frequency.setValueAtTime(speed, t);
|
lfo.connect(lfoGain);
|
||||||
}
|
|
||||||
|
|
||||||
//filters
|
//filters
|
||||||
const numStages = 2; //num of filters in series
|
const numStages = 2; //num of filters in series
|
||||||
@ -485,7 +484,7 @@ export const superdough = async (value, t, hapDuration) => {
|
|||||||
}
|
}
|
||||||
// phaser
|
// phaser
|
||||||
if (phaser !== undefined && phaserdepth > 0) {
|
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);
|
chain.push(phaserFX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,139 @@
|
|||||||
// 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
|
||||||
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;
|
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 {
|
class CoarseProcessor extends AudioWorkletProcessor {
|
||||||
static get parameterDescriptors() {
|
static get parameterDescriptors() {
|
||||||
return [{ name: 'coarse', defaultValue: 1 }];
|
return [{ name: 'coarse', defaultValue: 1 }];
|
||||||
@ -213,34 +345,7 @@ class DistortProcessor extends AudioWorkletProcessor {
|
|||||||
}
|
}
|
||||||
registerProcessor('distort-processor', DistortProcessor);
|
registerProcessor('distort-processor', DistortProcessor);
|
||||||
|
|
||||||
// adjust waveshape to remove frequencies above nyquist to prevent aliasing
|
// SUPERSAW
|
||||||
// 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);
|
|
||||||
};
|
|
||||||
|
|
||||||
function lerp(a, b, n) {
|
function lerp(a, b, n) {
|
||||||
return n * (b - a) + a;
|
return n * (b - a) + a;
|
||||||
}
|
}
|
||||||
@ -340,7 +445,7 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor {
|
|||||||
|
|
||||||
for (let i = 0; i < output[0].length; i++) {
|
for (let i = 0; i < output[0].length; i++) {
|
||||||
this.phase[n] = this.phase[n] ?? Math.random();
|
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[0][i] = output[0][i] + v * gainL;
|
||||||
output[1][i] = output[1][i] + v * gainR;
|
output[1][i] = output[1][i] + v * gainR;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user