{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "filter-ota",
  "versionKey": "1",
  "name": "OTA cascade filter",
  "description": "A low-pass that sings, built as the Prophet-5's and the Juno-106's are: four poles in a row, each an OTA charging a capacitor with a buffer after it, and a feedback round all four. It is the smoother cousin of the ladder: the tone it sings is rounder, and the bass under its peak stays as Resonance is turned up — from nothing to 0.9 Resonance the low end falls by about four decibels where the ladder's falls by thirteen, so a resonant sweep keeps its body. 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 — a round one, nearly a sine, which keeps its pitch as it swells. 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, so Resonance means the same at every slope. Every stage saturates, as an OTA does, on what it has still to catch up with, so a loud sound driven in thickens and slows. 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. 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, Slope and all, and at the default Resonance it plays at the ladder's level, so a patch moves between the two by changing the module; with no resonance it is quieter than the ladder, and turned high a little louder, by the bass it keeps. 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": "// An OTA cascade, built as the CEM3320 in a Prophet-5 and the IR3109 in a\n// Juno-106 are: four one-pole low-passes in a row, each an operational\n// transconductance amplifier charging a capacitor and a buffer after it, and\n// the last one's output fed back to the first. Being buffered, no stage pulls\n// on the one before it, so the four poles sit together at the cutoff as a\n// transistor ladder's do, and a tap after each is the sound at 6, 12, 18 or 24\n// decibels an octave: Slope is which, and the feedback goes round all four\n// whichever it is, as on the ladder.\n//\n// Four poles at the cutoff are half a cycle round there and take the level\n// down to a quarter, so a feedback of four exactly makes up for them: k is\n// four times Resonance, as on the ladder — below one it rings, at one it is on\n// the edge, above it it sings, at the cutoff the knob names.\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 by 1 + k. Where the sound and the feedback\n// meet, the sound is pushed in harder as Resonance rises, by 1 + k/2, which\n// gives half of that back, so turned from nothing to 0.9 the bass falls by\n// four decibels where the ladder's falls by thirteen: the peak stands out over\n// a low end that stays. The output is half of what the last\n// stage carries, which plays it within a decibel or two of the ladder at the\n// default Resonance and at 0.9, and sings it at the ladder's level.\n//\n// An OTA moves its capacitor by the tanh of the difference between what\n// arrives and what the capacitor holds, so every stage saturates on what it\n// has still to catch up with: a loud or fast sound finds the four slower to\n// move, which is the slew a driven polysynth filter thickens with. The stages\n// see small differences, and their curve is four times as wide as the\n// ladder's; what holds a singing filter at a level is the input stage, the\n// OTA the sound and the feedback meet at, through a curve two and a half times\n// as wide. Four poles come after it, and they take what it bends away from the\n// tone it sings, which is why that tone is round — its third harmonic eight to\n// ten decibels further down than the ladder's — and why it holds its pitch as\n// it swells, where a curve inside every stage drags it flat. Drive is how hard\n// the sound is pushed into the input stage, and stays out of the feedback, so\n// how hard the filter is driven does not move the point at which it starts to\n// 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. Each curve is taken as the\n// straight line from nothing to where it stands when the chain is first worked\n// out with no curves at all, and the chain is worked out once more through\n// those lines. Every divisor in it is one plus things that cannot be negative,\n// 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 wide the input stage's curve is beside the ladder's, and each pole's.\nconst INPUT = 2.5;\nconst STAGE = 4;\n\nexport default class AtmosynthOta 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    // 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    // The four poles' integrators, first to last.\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: what the\n    // input stage hands the first pole, and each pole.\n    this.u = 0;\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 c4 the poles'. Each pole\n  // is a share of what arrives plus what it holds, so the last is a share of\n  // what the input stage hands on plus what the four hold, and the feedback\n  // from it is folded into that.\n  solve(g, k, x, c0, c1, c2, c3, c4) {\n    const g1 = g * c1;\n    const g2 = g * c2;\n    const g3 = g * c3;\n    const g4 = g * c4;\n    const a1 = g1 / (1 + g1);\n    const a2 = g2 / (1 + g2);\n    const a3 = g3 / (1 + g3);\n    const a4 = g4 / (1 + g4);\n    const b1 = this.s1 / (1 + g1);\n    const b2 = this.s2 / (1 + g2);\n    const b3 = this.s3 / (1 + g3);\n    const b4 = this.s4 / (1 + g4);\n    // The last pole as a share of the input stage's output, plus what the\n    // chain holds.\n    const share = a1 * a2 * a3 * a4;\n    const held = ((b1 * a2 + b2) * a3 + b3) * a4 + b4;\n    const u = (c0 * (x - k * held)) / (1 + c0 * k * share);\n    this.u = u;\n    this.y1 = a1 * u + b1;\n    this.y2 = a2 * this.y1 + b2;\n    this.y3 = a3 * this.y2 + b3;\n    this.y4 = a4 * this.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 tap = clamp(Math.round(parameters.slope[0]), 0, 3);\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 so the cutoff lands where the knob\n        // says rather than flat of it.\n        this.coefficient = Math.tan((Math.PI * hz) / contextRate);\n      }\n      const g = this.coefficient;\n      const k = 4 * parameters.resonance[index];\n\n      // The sound, pushed in harder as Resonance rises to give back half the\n      // bass the feedback takes, and what starts it singing, as on the\n      // ladder: a millionth of full scale of noise, a hundred and twenty\n      // decibels down, which a filter handed digital silence would otherwise\n      // never have.\n      const x =\n        (1 + 0.5 * k) * parameters.drive[index] * source[index] +\n        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, 1);\n      this.solve(\n        g,\n        k,\n        x,\n        slope((x - k * this.y4) / INPUT),\n        slope((this.u - this.y1) / STAGE),\n        slope((this.y1 - this.y2) / STAGE),\n        slope((this.y2 - this.y3) / STAGE),\n        slope((this.y3 - this.y4) / STAGE),\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      // What its curves leave off centre, taken off under 5 Hz.\n      const raw = 0.5 * (tap === 0 ? this.y4 : tap === 1 ? this.y3 : tap === 2 ? this.y2 : this.y1);\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": "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-14T00:00:00.000Z"
}