Make a wavefolder
A folding distortion, in one line.A wavefolder folds the waveform back on itself instead of clipping it. Harmonics pile up fast, and it sounds wild, way more alive than overdrive. The whole thing is one line of math.
Add a device
Open the shelf and add a Device. You get a gain device: an input, an output, and a Gain knob. Run some sound through it.
Fold it
Open index.ts and find the line in process that writes the output. Swap the plain
multiply for a sine fold:
const fold = ctx.modulatedParam(0, i);
for (let ch = 0; ch < 2; ch++) {
ctx.output(ch, i, Mathf.sin(ctx.input(ch, i) * fold));
}Save and play. That's a wavefolder. Push the knob up and the tone folds over and over into something metallic and rich. Drive it from an envelope or LFO and it really moves.
Make the knob a Fold control
In describe, rename the knob and give it room to get nasty:
// id, name, short name, min, max, default, modulatable
m.addParam(new ParamDef("fold", "Fold", "Fold", 1.0, 12.0, 3.0, true));Save. Low Fold is gentle, high Fold is chaos.
The finished device
import {
Device,
Manifest,
ParamDef,
Ctx,
audioIn,
audioOut,
register,
} from "@msh/device-sdk/device/assembly";
class Node extends Device {
describe(): Manifest {
const m = new Manifest("Wavefolder");
m.addParam(new ParamDef("fold", "Fold", "Fold", 1.0, 12.0, 3.0, true));
m.addPort(audioIn("In", 2));
m.addPort(audioOut("Out", 2));
return m;
}
process(ctx: Ctx): void {
for (let i = 0; i < ctx.n; i++) {
const fold = ctx.modulatedParam(0, i);
for (let ch = 0; ch < 2; ch++) {
ctx.output(ch, i, Mathf.sin(ctx.input(ch, i) * fold));
}
}
}
}
register(new Node());
export * from "@msh/device-sdk/device/assembly";Next: make a synth, or the how-to guides.