{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "filter-diode",
  "versionKey": "1",
  "name": "Diode ladder filter",
  "description": "A low-pass that squelches, built as the TB-303's is: four poles joined by diodes rather than kept apart by transistors, so each pulls on its neighbours and no two of them sit in the same place. With no resonance it starts to fall gently from well below the cutoff and steepens past it, darker than the ladder at the same setting; turned up, a peak rises out of that slope, and an envelope sweeping the cutoff with Resonance high is the rubbery squelch of an acid line. Below one Resonance emphasises what is at the cutoff, at one the filter rings on for as long as it is left, and above one it holds a tone of its own with nothing played into it at all. The further Resonance is turned the thinner the low end, as on the ladder and by about as much: the feedback takes the bass, and half of what it takes is made up after the filter. Every stage saturates, as diodes do, so a resonance pushed hard or a loud sound driven in finds the filter slower to move, and the tone it sings sags in pitch as it swells. Drive is how hard the sound is pushed into the filter: at one gently, and above it into a thick, compressed sound whose resonance is held down while the bass under it comes through. It is kept out of the feedback, so driving the filter does not move the point at which it starts to sing. What its curves leave off centre on a sawtooth or a narrow pulse is taken off below any note, so what comes out sits on centre. Cutoff and Cutoff depth work as they do on the ladder, in cents and negative to invert, eight octaves either way, so an envelope can open a filter set low all the way to the top; Key tracking moves the cutoff with the note played, which needs the note routed here. Resonance is an input as well, and its depth how far a signal there moves it. However far it is pushed, it stays inside the knob's own range. Its knobs are the ladder's, without Slope, so a patch moves between the two by changing the module. This module carries code: nothing in the vocabulary can be fed back into itself a sample at a time, which is the whole of what a filter like this is.",
  "audioInputs": [
    {
      "id": "in",
      "label": "In"
    },
    {
      "id": "cutoff",
      "label": "Cutoff"
    },
    {
      "id": "resonance",
      "label": "Resonance"
    }
  ],
  "audioOutputs": [
    {
      "id": "out",
      "label": "Out"
    }
  ],
  "midiInputs": [
    {
      "id": "midi",
      "label": "Note"
    }
  ],
  "nodes": [
    {
      "id": "in",
      "kind": "moduleInput",
      "options": {
        "input": "in"
      }
    },
    {
      "id": "gen",
      "kind": "dsp",
      "options": {
        "code": "// A diode ladder, built as the TB-303's filter is: four capacitors in a\n// chain, each joined to the next by a pair of diodes rather than buffered from\n// it by a transistor, and the top one's voltage fed back to the bottom.\n// Because nothing buffers one stage from the next, each capacitor is pulled\n// by its neighbour above as well as pushed by the one below, and the four\n// poles come out nothing alike: one at an eighth of the cutoff, one a little\n// below it and two above it, where a transistor ladder has four in one place.\n// That is the sound. With no resonance the filter starts to fall gently from\n// well below the cutoff and steepens past it; turned up, the peak stands out\n// of a slope rather than off a flat floor, and an envelope sweeping it is the\n// squelch.\n//\n// The chain, for a quiet signal, with the bottom capacitor twice the others:\n//   y1' = (u - y1 - (y1 - y2)) / 2\n//   y2' = (y1 - y2) - (y2 - y3)\n//   y3' = (y2 - y3) - (y3 - y4)\n//   y4' = (y3 - y4)\n// in units of the cutoff, where u is the sound less k times y4. Round the loop\n// it is half a cycle late at the square root of eleven twelfths of the cutoff,\n// and there it takes the level down by 1193/72, about 16.6: that much feedback\n// exactly makes up for it, so Resonance is k over 1193/72 and the knob means\n// what the ladder's does — below one it rings, at one it is on the edge, above\n// it it sings. The cutoff is scaled up by the same square root, so it sings at\n// the cutoff the knob names rather than a few per cent flat of it.\n//\n// What the feedback takes, it takes from the bass: below the cutoff the loop\n// is in phase, so the low end falls as Resonance rises, by 1 + k. Left at\n// that, a diode ladder turned to 0.9 would be twenty-four decibels down in the\n// bass and as much quieter all told; the output makes up half of it in\n// decibels, the square root of 1 + k, which lands the bass where the ladder\n// loses its own, about twelve decibels at 0.9, and the level and the singing\n// near the ladder's, so the two swap in a patch without a jump in loudness.\n// Being outside the loop, the make-up changes nothing about where it sings.\n//\n// Every stage saturates, as diodes do: what passes from one capacitor to the\n// next is a curve of the difference across the pair between them, which for a\n// quiet signal is the straight line it starts as and for a loud one gives\n// back less. So a resonance pushed hard, or a loud sound driven in, finds the\n// chain slower to move, and the tone it sings sags in pitch as it swells —\n// the rubber in it. The diodes' curve is narrower than the input's, three\n// tenths of it, as a diode's knee is close to the signal it carries, which\n// is what lets the diodes be heard at all: the differences between\n// neighbouring stages are small. The input stage, the pair the sound and the\n// feedback meet at, has the ladder's curve and holds the singing at a level;\n// Drive is how hard the sound is pushed into it, and stays out of the\n// feedback, so how hard the filter is driven does not move the point at which\n// it starts to sing.\n//\n// Solved with no sample's delay round the loop, as filter-sem's is: the\n// integrators are trapezoidal and the chain, feedback and all, is worked out\n// for this sample's own values, so the cutoff lands where the knob says at any\n// height without running at twice the rate. A curve inside a loop solved that\n// way would need it worked out again until it settled; instead each curve is\n// taken as the straight line from nothing to where it stands when the chain is\n// first worked out with no curves at all, and the chain is worked out once\n// more through those lines. Every divisor in it is one plus things that cannot\n// be negative, so there is always an answer.\n//\n// Every curve here is the same either side of nothing, and still the filter\n// leaves a sawtooth off centre. A curve like that keeps a wave centred only\n// while the wave spends as long at each height as at the same depth below,\n// which a sawtooth does going in and no longer does once the poles have moved\n// its harmonics against each other, and which a narrow pulse never does. The\n// lean grows with the square of the level, and every voice of a chord leans\n// the same way, so a chord adds it up. So the output leaves through a one-pole\n// at 5 Hz, as the analog oscillator's does, which takes the lean off below\n// any note and leaves what is played as it was.\nimport { AtmoProcessor, Rng } from \"atmosynth:dsp\";\n\nfunction clamp(value, low, high) {\n  return value < low ? low : value > high ? high : value;\n}\n\n// The ladder's tanh, near enough: x(27 + x²) / (27 + 9x²), arriving at one at\n// three with no slope left. As the slope of the straight line from nothing to\n// where the curve is at `value`: one at nothing and less further out, written\n// so it divides only by what is past three. It is arithmetic and nothing\n// else, so the plugin works it out to the same double.\nfunction slope(value) {\n  const size = value < 0 ? -value : value;\n  return size > 3 ? 1 / size : (27 + size * size) / (27 + 9 * size * size);\n}\n\n// How far the cutoff is scaled for the loop to turn half a cycle there.\nconst TUNE = Math.sqrt(12 / 11);\n// The feedback at which it is on the edge of singing.\nconst EDGE = 1193 / 72;\n// How narrow the diodes' curve is beside the input's.\nconst DIODE = 0.3;\n\nexport default class AtmosynthDiode extends AtmoProcessor {\n  static spec = 1;\n  // Channel 0 of what arrives: a mono source arrives on both sides, and a\n  // stereo one is heard by its left.\n  static inputs = [2];\n  static outputs = [1];\n  // Not smoothed here: a knob's move reaches these already eased, and what is\n  // wired in is followed exactly.\n  static params = {\n    frequency: { min: 20, max: 20000, default: 1200, rate: \"a\", smooth: 0 },\n    // Eight octaves either way, as on the ladder, so an envelope into the\n    // cutoff from a low setting can open the filter the whole way.\n    detune: { min: -9600, max: 9600, default: 0, rate: \"a\", smooth: 0 },\n    resonance: { min: 0, max: 1.2, default: 0.4, rate: \"a\", smooth: 0 },\n    // Into the input stage's curve, and only there.\n    drive: { min: 0.1, max: 10, default: 1, rate: \"a\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    // The four capacitors' integrators, bottom to top.\n    this.s1 = 0;\n    this.s2 = 0;\n    this.s3 = 0;\n    this.s4 = 0;\n    // Where the chain stands this sample, as `solve` leaves it.\n    this.y1 = 0;\n    this.y2 = 0;\n    this.y3 = 0;\n    this.y4 = 0;\n    // The cutoff and detune the coefficient below was last worked out for:\n    // not yet any, which no value equals.\n    this.frequency = 0 / 0;\n    this.detune = 0 / 0;\n    this.coefficient = 0;\n    /** The 5 Hz one-pole on the way out, and the two samples it remembers. */\n    this.pole = 1 - (2 * Math.PI * 5) / sampleRate;\n    this.lastRaw = 0;\n    this.centred = 0;\n    // The dither below, drawn from the node's own generator.\n    this.rng = new Rng(this.seed);\n  }\n\n  // The chain worked out for this sample, each curve taken as a straight line\n  // of the slope given: c0 the input stage's, c1 to c3 the diodes' from the\n  // bottom. From the top down each capacitor is a share of the one below it\n  // plus what it holds, so the bottom one alone is left to find, with the\n  // feedback from the top folded in.\n  solve(g, k, x, c0, c1, c2, c3) {\n    const g3 = g * c3;\n    const a4 = g3 / (1 + g3);\n    const b4 = this.s4 / (1 + g3);\n    const g2 = g * c2;\n    const d3 = 1 + g2 + g3 - g3 * a4;\n    const a3 = g2 / d3;\n    const b3 = (g3 * b4 + this.s3) / d3;\n    const g1 = g * c1;\n    const d2 = 1 + g2 + g1 - g2 * a3;\n    const a2 = g1 / d2;\n    const b2 = (g2 * b3 + this.s2) / d2;\n    // The bottom capacitor is twice the others, so it moves at half the rate.\n    const h = 0.5 * g;\n    // The top as a share of the bottom, plus what the chain holds.\n    const share = a4 * a3 * a2;\n    const held = a4 * (a3 * b2 + b3) + b4;\n    const y1 =\n      (h * c0 * (x - k * held) + h * c1 * b2 + this.s1) /\n      (1 + h + h * c1 - h * c1 * a2 + h * c0 * k * share);\n    const y2 = a2 * y1 + b2;\n    const y3 = a3 * y2 + b3;\n    this.y1 = y1;\n    this.y2 = y2;\n    this.y3 = y3;\n    this.y4 = a4 * y3 + b4;\n  }\n\n  process(inputs, outputs, parameters) {\n    const channel = outputs[0][0];\n    const source = inputs[0][0];\n    // In a browser's audio thread `sampleRate` is an accessor on the global\n    // scope rather than a variable, so it is read once a call.\n    const contextRate = sampleRate;\n    const ceiling = contextRate * 0.45;\n    const pole = this.pole;\n    let lastRaw = this.lastRaw;\n    let centred = this.centred;\n\n    for (let index = 0; index < channel.length; index += 1) {\n      const frequency = parameters.frequency[index];\n      const detune = parameters.detune[index];\n      // A Math.pow and a Math.tan, worked out again only when they would come\n      // out different.\n      if (frequency !== this.frequency || detune !== this.detune) {\n        this.frequency = frequency;\n        this.detune = detune;\n        const hz = clamp(frequency * Math.pow(2, detune / 1200), 20, ceiling);\n        // The integrators' gain, prewarped at the cutoff so it sings where\n        // the knob says, and scaled for the loop to turn half a cycle there.\n        this.coefficient = Math.tan((Math.PI * hz) / contextRate) * TUNE;\n      }\n      const g = this.coefficient;\n      const k = EDGE * parameters.resonance[index];\n\n      // What starts it, as on the ladder: a millionth of full scale of noise,\n      // a hundred and twenty decibels down, which a filter handed digital\n      // silence would otherwise never have.\n      const x = parameters.drive[index] * source[index] + 1e-6 * this.rng.bipolar();\n\n      // The chain with every curve a straight line, then each curve as the\n      // line to where it stands there, and the chain again.\n      this.solve(g, k, x, 1, 1, 1, 1);\n      this.solve(\n        g,\n        k,\n        x,\n        slope(x - k * this.y4),\n        slope((this.y1 - this.y2) / DIODE),\n        slope((this.y2 - this.y3) / DIODE),\n        slope((this.y3 - this.y4) / DIODE),\n      );\n      this.s1 = 2 * this.y1 - this.s1;\n      this.s2 = 2 * this.y2 - this.s2;\n      this.s3 = 2 * this.y3 - this.s3;\n      this.s4 = 2 * this.y4 - this.s4;\n\n      // Half the bass the feedback took, made up after the loop.\n      // What its curves leave off centre, taken off under 5 Hz.\n      const raw = Math.sqrt(1 + k) * this.y4;\n      centred = raw - lastRaw + pole * centred;\n      lastRaw = raw;\n      channel[index] = centred;\n    }\n\n    this.lastRaw = lastRaw;\n    this.centred = centred;\n    return true;\n  }\n}\n"
      }
    },
    {
      "id": "cutoffin",
      "kind": "moduleInput",
      "options": {
        "input": "cutoff"
      }
    },
    {
      "id": "cutoffdepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "note",
      "kind": "midiInput",
      "options": {
        "midiInput": "midi"
      }
    },
    {
      "id": "trackdepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "trim",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "out",
      "kind": "moduleOutput",
      "options": {
        "output": "out"
      }
    },
    {
      "id": "resonancein",
      "kind": "moduleInput",
      "options": {
        "input": "resonance"
      }
    },
    {
      "id": "resonancedepth",
      "kind": "gain",
      "options": {}
    }
  ],
  "connections": [
    {
      "from": {
        "node": "in",
        "output": 0
      },
      "to": {
        "node": "gen",
        "input": 0
      }
    },
    {
      "from": {
        "node": "gen",
        "output": 0
      },
      "to": {
        "node": "trim",
        "input": 0
      }
    },
    {
      "from": {
        "node": "trim",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "cutoffin",
        "output": 0
      },
      "to": {
        "node": "cutoffdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "cutoffdepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "detune"
      }
    },
    {
      "from": {
        "node": "note",
        "output": 0
      },
      "to": {
        "node": "trackdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "trackdepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "frequency"
      }
    },
    {
      "from": {
        "node": "resonancein",
        "output": 0
      },
      "to": {
        "node": "resonancedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "resonancedepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "resonance"
      }
    }
  ],
  "automation": [],
  "parameters": [
    {
      "id": "cutoff",
      "label": "Cutoff (Hz)",
      "range": {
        "min": "20",
        "max": "18000"
      },
      "default": "1200",
      "targets": [
        {
          "node": "gen",
          "param": "frequency"
        }
      ],
      "scale": "log"
    },
    {
      "id": "resonance",
      "label": "Resonance (sings above one)",
      "range": {
        "min": "0",
        "max": "1.2"
      },
      "default": "0.4",
      "targets": [
        {
          "node": "gen",
          "param": "resonance"
        }
      ]
    },
    {
      "id": "drive",
      "label": "Drive",
      "range": {
        "min": "0.1",
        "max": "10"
      },
      "default": "1",
      "targets": [
        {
          "node": "gen",
          "param": "drive"
        }
      ]
    },
    {
      "id": "cutoffAmount",
      "label": "Cutoff depth (cents)",
      "range": {
        "min": "-9600",
        "max": "9600"
      },
      "default": "2400",
      "targets": [
        {
          "node": "cutoffdepth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "keyTrack",
      "label": "Key tracking",
      "range": {
        "min": "0",
        "max": "2"
      },
      "default": "0",
      "targets": [
        {
          "node": "trackdepth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "level",
      "label": "Level",
      "range": {
        "min": "0",
        "max": "2"
      },
      "default": "1",
      "targets": [
        {
          "node": "trim",
          "param": "gain"
        }
      ]
    },
    {
      "id": "resonanceAmount",
      "label": "Resonance depth",
      "range": {
        "min": "-1.2",
        "max": "1.2"
      },
      "default": "0.4",
      "targets": [
        {
          "node": "resonancedepth",
          "param": "gain"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-10-14T00:00:00.000Z"
}