{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "filter-ladder",
  "versionKey": "5",
  "name": "Ladder filter",
  "description": "A low-pass that sings, built as a transistor ladder is. Four poles with a feedback around them, and Resonance is how much: below one it emphasises what is at the cutoff, at one the feedback exactly makes up for what the poles take off, and above one the filter holds a tone of its own with nothing played into it at all — the whistle the Moogs are asked for, and the one thing the ordinary Filter cannot do however far its resonance is pushed. Slope is which pole the sound is taken after: 24 decibels an octave after the fourth, and 18, 12 or 6 after the third, second or first. The feedback goes round all four whichever it is, as it does on a Sub 37, so Resonance means the same at every slope and a gentler one keeps more of what is above the cutoff around the same peak. Nothing makes up the bass the feedback takes away: the further Resonance is turned the thinner the low end, which is how the filters this one follows sound. Cutoff and Cutoff depth work as they do on the ordinary Filter, in cents and negative to invert, and the depth reaches 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. Drive is how hard the sound is pushed into the filter, and each of its four stages saturates, as a transistor ladder's do: at one gently, and above it into a thick, compressed sound whose resonance swells and is held down by every stage it goes round rather than clipped on the way in. It is kept out of the feedback, so driving the filter does not move the point at which it starts to sing. 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. Resonance is an input as well, and its depth how far a signal there moves it — an envelope into it makes the filter sing at the start of a note and settle as it decays. However far it is pushed, it stays inside the knob's own range.",
  "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 ladder filter: four poles, and a feedback around them deep enough that the\n// filter sings on its own. The node vocabulary cannot reach it — a\n// BiquadFilter is linear and unconditionally stable, so however far its Q is\n// pushed it rings after something excites it and then stops. The filters this\n// one is modelled on do not stop: past the point where the feedback makes up\n// for what the four poles take off, they hold a tone of their own at the\n// cutoff, and on those instruments that tone is played rather than avoided.\n//\n// Four one-pole lowpasses in series, the last one's output subtracted from what\n// goes in. A one-pole turns the signal an eighth of a cycle at its own cutoff,\n// so at the cutoff the four together are half a cycle round and the\n// subtraction has become an addition; each one also takes the level down to\n// about seven tenths there, so a feedback of four exactly makes up for the four\n// of them. That is what Resonance is scaled against: below one the filter\n// rings, at one it is on the edge, above it it sings.\n//\n// Every stage saturates, as every stage of a transistor ladder does. A stage\n// there is a pair of transistors charging a capacitor, and the current they can\n// push into it is the difference of two tanh curves, one of what arrives and\n// one of what the capacitor already holds. So each pole here moves by the tanh\n// of its input less the tanh of its own state, where version 4 moved each by\n// the plain difference and saturated once, at the input. For a quiet signal a\n// tanh is the straight line it starts as, so nothing a quiet signal meets has\n// changed: the cutoff, where the filter starts to sing and how it rings below\n// that are version 4's. A loud one finds every stage able to move only so\n// fast, so a driven filter thickens and compresses through all four rather\n// than clipping at the door, and a resonance pushed into it is held down by\n// every stage it goes round. What holds a tone that sings on its own at a\n// level, rather than doubling every cycle until it is a square, is now all\n// four of them.\n//\n// The output is taken after the fourth pole, or the third, second or first —\n// 24, 18, 12 or 6 decibels an octave — and the feedback after the fourth\n// whichever it is. That is how the Sub 37 does it: the loop stays four poles\n// round, so Resonance means the same at every slope and only what is listened\n// to moves. Nothing makes up the bass the feedback takes away, so the more it\n// resonates the thinner the low end, which is the sound and not a fault.\n//\n// Twice per sample, because the feedback is a sample behind: at a high cutoff a\n// whole sample is a large enough part of the cycle to drag the pitch flat, and\n// running the loop at twice the rate halves that error.\nimport { AtmoProcessor, Rng } from \"atmosynth:dsp\";\n\nfunction clamp(value, low, high) {\n  return value < low ? low : value > high ? high : value;\n}\n\n// A tanh, near enough: x(27 + x²) / (27 + 9x²), which leaves the origin at the\n// slope a tanh does and arrives at one at three with no slope left, where it\n// stays. Sixteen Math.tanh a sample took two and a half times what version 4\n// cost the audio thread, and nearly all of it was those calls; this is a few\n// multiplications and one division, which is less than version 4 cost with its\n// two. And it is arithmetic and nothing else, so the plugin works it out to the\n// same double, which two libraries' tanh need not do.\nfunction saturate(value) {\n  return value < -3 ? -1 : value > 3 ? 1 : (value * (27 + value * value)) / (27 + 9 * value * value);\n}\n\nexport default class AtmosynthLadder 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, twice what version 4 allowed, so an envelope\n    // into the 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 first tanh, and only there: the feedback is left alone, so how\n    // hard the filter is driven does not move the point at which it starts to\n    // sing.\n    drive: { min: 0.1, max: 10, default: 1, rate: \"a\", smooth: 0 },\n    // Which pole the output is taken after, by the Slope choice's position:\n    // 24, 18, 12 and 6 decibels an octave are the fourth, third, second and\n    // first. Rounded, since a choice turned by a controller glides between\n    // positions on its way to one.\n    slope: { min: 0, max: 3, default: 0, rate: \"k\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    // One state per pole, and the fourth pole's output as the feedback reads it.\n    this.s1 = 0;\n    this.s2 = 0;\n    this.s3 = 0;\n    this.s4 = 0;\n    this.last = 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 dither below, drawn from the node's own generator.\n    this.rng = new Rng(this.seed);\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 tap = clamp(Math.round(parameters.slope[0]), 0, 3);\n    let s1 = this.s1;\n    let s2 = this.s2;\n    let s3 = this.s3;\n    let s4 = this.s4;\n    let last = this.last;\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 one-pole coefficient at twice the sample rate, prewarped so the\n        // cutoff lands where the knob says rather than flat of it.\n        const g = Math.tan((Math.PI * hz) / (contextRate * 2));\n        this.coefficient = g / (1 + g);\n      }\n      const k = 4 * parameters.resonance[index];\n      const a = this.coefficient;\n\n      // What starts it. An analogue filter is pushed into oscillation by its own\n      // noise; this one, handed digital silence, would sit at exactly zero for\n      // ever — and a filter that sings only once something else has sounded is\n      // not the thing being modelled. A millionth of full scale is a hundred and\n      // twenty decibels down, inaudible on its own and enough to grow into the\n      // tone within a note.\n      const x = parameters.drive[index] * source[index] + 1e-6 * this.rng.bipolar();\n\n      let y1 = 0;\n      let y2 = 0;\n      let y3 = 0;\n      for (let pass = 0; pass < 2; pass += 1) {\n        // Each pole a trapezoidal one-pole, moved by the tanh of what arrives\n        // less the tanh of what it holds.\n        let step = (saturate(x - k * last) - saturate(s1)) * a;\n        y1 = step + s1;\n        s1 = y1 + step;\n        step = (saturate(y1) - saturate(s2)) * a;\n        y2 = step + s2;\n        s2 = y2 + step;\n        step = (saturate(y2) - saturate(s3)) * a;\n        y3 = step + s3;\n        s3 = y3 + step;\n        step = (saturate(y3) - saturate(s4)) * a;\n        last = step + s4;\n        s4 = last + step;\n      }\n      channel[index] = tap === 0 ? last : tap === 1 ? y3 : tap === 2 ? y2 : y1;\n    }\n\n    this.s1 = s1;\n    this.s2 = s2;\n    this.s3 = s3;\n    this.s4 = s4;\n    this.last = last;\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": "slope",
      "label": "Slope (per octave)",
      "values": [
        "24 dB",
        "18 dB",
        "12 dB",
        "6 dB"
      ],
      "default": "24 dB",
      "targets": [
        {
          "node": "gen",
          "param": "slope"
        }
      ]
    },
    {
      "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-12T00:00:00.000Z"
}