Skip to content

Instantly share code, notes, and snippets.

@catfact
Last active April 5, 2024 00:38
Show Gist options
  • Save catfact/3053c5f7068a2a87a15d58ddc56b611e to your computer and use it in GitHub Desktop.
Save catfact/3053c5f7068a2a87a15d58ddc56b611e to your computer and use it in GitHub Desktop.
scrappy interpolated delay in JSFX
// This effect Copyright (C) 2024 Ezra Buchla
// License: CC BY-SA 4.0 - https://creativecommons.org/licenses/by-sa/4.0/deed.en
desc: micro delay
// tags: delay
// author: a moth object
slider1:0<0,1000,1>Integer Sample Delay
slider2:0<0,1,0.001>Sub-sample Interpolation
slider3:0<0,2,1>Interpolation Mode
in_pin:left input
in_pin:right input
out_pin:left output
out_pin:right output
@init
pos = 0;
pos1 = 0;
pos2 = 0;
pos3 = 0;
// ... i don't really get how buffer variables work
// so, just making this an arbitrary offset that is bigger than the max delay
apbuf = 3000;
function readLinear(buf, buf1, channel)
(
x0 = buf[channel];
x1 = buf1[channel];
x0 + (f * (x1-x0));
);
function readCubic(buf, buf1, buf2, buf3, channel)
(
x0 = buf[channel];
x1 = buf1[channel];
x2 = buf2[channel];
x3 = buf3[channel];
c0 = x1;
c1 = 0.5 * (x2 - x0);
c2 = x0 - 2.5 * x1 + 2. * x2 - 0.5 * x3;
c3 = 0.5 * (x3 - x0) + 1.5 * (x1 - x2);
((c3 * f + c2) * f + c1) * f + c0;
);
function readAllpass(buf, buf1, channel) // tried: local (zy)
(
x0 = buf[channel];
x1 = buf1[channel];
// y = f * (x0 - zy[channel]) + x1;
// zy[channel] = y;
y = f * (x0 - apbuf[channel]) + x1;
apbuf[channel] = y;
y;
);
@slider
frames = slider1;
f = slider2;
mode = slider3;
@sample
// pointers to interleaved history
buf = pos*2;
buf1 = pos1*2;
buf2 = pos2*2;
buf3 = pos3*2;
del0 = slider3 == 0 ? readLinear(buf, buf1, 0) : (slider3 == 1 ? readCubic(buf, buf1, buf2, buf3, 0) : readAllpass(buf, buf1, 0));
del1 = slider3 == 0 ? readLinear(buf, buf1, 1) : (slider3 == 1 ? readCubic(buf, buf1, buf2, buf3, 1) : readAllpass(buf, buf1, 1));
// store input
buf[0] = spl0;
buf[1] = spl1;
// increment buffer positions
pos3 = pos2;
pos2 = pos1;
pos1 = pos;
(pos += 1) >= frames ? pos = 0;
// write output
spl0 = del0;
spl1 = del1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment