{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "filter-sem",
  "versionKey": "1",
  "name": "State-variable filter",
  "description": "A filter with four shapes on one knob, built as the Oberheim SEM's is: two poles in a loop, which hold a low-pass, a band-pass and a high-pass at once, and a notch that is the low-pass and the high-pass added. Mode is which of them is heard — low-pass at nothing, band-pass at one, high-pass at two and the notch at three — and between two of them it is a mix of both, so turning it while a note sounds moves the sound through the shapes rather than switching from one to the next. Twelve decibels an octave, gentler than the ladder's twenty-four, and full where the ladder goes thin: turning Resonance up takes nothing away from below the cutoff. The high-pass resonates and sings as the low-pass does, so this is the resonant high-pass as well. Below one Resonance emphasises what is at the cutoff; at one the filter rings on at the cutoff for as long as it is left; above one it holds a tone of its own with nothing played into it at all. The tone is round, close to a sine, louder the further Resonance is turned, and held at that level by the filter itself rather than clipped. At the notch the tone it sings is the one it takes out, so a singing filter falls quiet there. 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. Drive is how hard the sound is pushed into the filter: at one gently, and above it its input and both of its integrators saturate, as a transistor filter's do, so a driven filter thickens and compresses and a resonance pushed into it is held down. It is kept out of the resonance, 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. 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. Mode is an input too, and its depth how far a signal there moves it — an LFO into it sweeps the sound from one shape to the next and back, and an envelope or an automation lane walks it through them over a note or a phrase. However far it is pushed, it stays between the low-pass and the notch. This module carries code: nothing in the vocabulary can be fed back into itself a sample at a time, and a biquad's resonance rings after something excites it and then stops.",
  "audioInputs": [
    {
      "id": "in",
      "label": "In"
    },
    {
      "id": "cutoff",
      "label": "Cutoff"
    },
    {
      "id": "resonance",
      "label": "Resonance"
    },
    {
      "id": "mode",
      "label": "Mode"
    }
  ],
  "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 state-variable filter, built as the Oberheim SEM's is: two integrators in\n// a loop, the second's output fed back to the first's input and the first's\n// fed back through a damping, so one circuit holds a low-pass, a band-pass and\n// a high-pass at once, and the notch is the low-pass and the high-pass added.\n// Mode is which of them is listened to, and it is a knob rather than a switch:\n// low-pass at nothing, band-pass at one, high-pass at two and the notch at\n// three, each two neighbours mixed sample for sample between, so turning it\n// moves the sound through the shapes rather than jumping from one to the\n// next. The SEM's own knob ran from low-pass through the notch to high-pass,\n// with the band-pass on a switch beside it; here all four are on one travel,\n// and it is an input as well, so an LFO, an envelope or a lane can sweep it.\n//\n// Two poles, twelve decibels an octave: gentler than a ladder's four, and a\n// different instrument rather than a lesser one. Nothing takes the bass away\n// as it resonates, because a state-variable low-pass passes what is below its\n// cutoff at its own level whatever the damping — which is why the SEM stays\n// full where a ladder turned up goes thin. And the high-pass sings as the\n// low-pass does, where a biquad's high-pass rings at its Q and then stops.\n//\n// Resonance is the damping taken away: at nothing the filter is as damped as\n// two poles get without ringing, at one there is none left and the loop rings\n// on at the cutoff for ever, and past one the damping has turned round and\n// feeds the ring. What holds it there is a saturating stage in the damping\n// path: for a quiet signal it is a straight line and the damping is what the\n// knob says, and as the band-pass grows the stage gives back less than it is\n// handed, which is more damping. So past one the filter sings at the level\n// where the two meet — a round tone, close to a sine, rather than a ring that\n// grows until something clips it. The SEM itself stops short of singing; this\n// goes the step further, as the ladder does, and does it softly.\n//\n// Both integrators saturate, as a transistor integrator does: what it is\n// handed reaches its capacitor through a curve, so a loud signal finds the\n// filter able to move only so fast, and a driven one thickens and a resonance\n// pushed into it is held down. Their curve is twice as wide as the damping's,\n// which is gently: singing as loud as it goes, the filter has slowed its own\n// integrators by a few per cent and is two thirds of a semitone flat, where\n// with the two curves alike it would be nearly two semitones. Drive is how\n// hard the sound is pushed in, through the input stage's own curve, so pushing\n// it bends and compresses rather than only getting louder; it is kept out of\n// the damping, so how hard the filter is driven does not move the point at\n// which it starts to sing.\n//\n// Solved with no sample's delay round the loop, as Zavalishin's\n// topology-preserving transform does it: the integrators are trapezoidal and\n// the loop is worked out for this sample's own values, so the cutoff lands\n// where the knob says at any height without running at twice the rate as the\n// ladder has to. A curve inside a loop solved that way would need the loop\n// worked out again until it settled; instead each curve is taken as the\n// straight line from nothing to where it stands when the loop is first worked\n// out with no curves at all. That is near enough, costs one more division, and\n// always comes out with an answer: however the lines lean, what the loop is\n// divided by stays well clear of nothing.\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// A tanh, near enough, as the ladder has it: x(27 + x²) / (27 + 9x²), which\n// leaves the origin at the slope a tanh does and arrives at one at three with\n// no slope left, where it stays. It is arithmetic and nothing else, so the\n// plugin works it out to the same double, which two libraries' tanh need not.\nfunction saturate(value) {\n  return value < -3 ? -1 : value > 3 ? 1 : (value * (27 + value * value)) / (27 + 9 * value * value);\n}\n\n// The same curve over what it is handed: the slope of the straight line from\n// nothing to where the curve is at `value`. One at nothing and less further\n// out, and written so that it divides only by what is past three, so nothing\n// has a slope as well.\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 hard the damping's stage holds a loud band-pass down: the damping it\n// adds is this times how far its curve has bent. Four lets the loudest it\n// sings, at a resonance of 1.2, reach about seven tenths of full scale.\nconst HOLD = 4;\n// How much wider the integrators' curve is than the damping's.\nconst ROOM = 2;\n\nexport default class AtmosynthSem 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    // 0 low-pass, 1 band-pass, 2 high-pass, 3 notch, and the mixes between.\n    mode: { min: 0, max: 3, default: 0, rate: \"a\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    // The two integrators: the band-pass's and the low-pass's.\n    this.s1 = 0;\n    this.s2 = 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  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    let s1 = this.s1;\n    let s2 = this.s2;\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      // The damping: two at a resonance of nothing, none at one, and past one\n      // a little less than none.\n      const k = 2 - 2 * parameters.resonance[index];\n\n      // What starts it, as on the ladder: an analogue filter is pushed into\n      // singing by its own noise, and this one, handed digital silence, would\n      // sit at exactly nothing for ever. A millionth of full scale is a\n      // hundred and twenty decibels down, and enough to grow into the tone\n      // within a note.\n      const x = saturate(\n        parameters.drive[index] * source[index] + 1e-6 * this.rng.bipolar(),\n      );\n\n      // Where the loop would stand with every curve a straight line.\n      let high = (x - (k + g) * s1 - s2) / (1 + g * (k + g));\n      let band = g * high + s1;\n      // Each curve as the line to where it stands there: the first\n      // integrator's through what reaches it, the second's and the damping's\n      // through the band-pass.\n      const g1 = g * slope(high / ROOM);\n      const g2 = g * slope(band / ROOM);\n      const damping = k + HOLD * (1 - slope(band));\n      // And the loop worked out again through them.\n      high = (x - (damping + g2) * s1 - s2) / (1 + g1 * (damping + g2));\n      band = g1 * high + s1;\n      s1 = band + g1 * high;\n      const low = g2 * band + s2;\n      s2 = low + g2 * band;\n\n      // However far the Mode input pushes it, it stays between the low-pass\n      // and the notch: past either end the mix would run on beyond a shape\n      // into a sound none of the four is.\n      const mode = clamp(parameters.mode[index], 0, 3);\n      // What its curves leave off centre, taken off under 5 Hz.\n      const raw =\n        mode < 1\n          ? low + mode * (band - low)\n          : mode < 2\n            ? band + (mode - 1) * (high - band)\n            : high + (mode - 2) * low;\n      centred = raw - lastRaw + pole * centred;\n      lastRaw = raw;\n      channel[index] = centred;\n    }\n\n    this.s1 = s1;\n    this.s2 = s2;\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": {}
    },
    {
      "id": "modein",
      "kind": "moduleInput",
      "options": {
        "input": "mode"
      }
    },
    {
      "id": "modedepth",
      "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"
      }
    },
    {
      "from": {
        "node": "modein",
        "output": 0
      },
      "to": {
        "node": "modedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "modedepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "mode"
      }
    }
  ],
  "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": "mode",
      "label": "Mode (0 low-pass, 1 band-pass, 2 high-pass, 3 notch)",
      "range": {
        "min": "0",
        "max": "3"
      },
      "default": "0",
      "targets": [
        {
          "node": "gen",
          "param": "mode"
        }
      ]
    },
    {
      "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"
        }
      ]
    },
    {
      "id": "modeAmount",
      "label": "Mode depth",
      "range": {
        "min": "-3",
        "max": "3"
      },
      "default": "1",
      "targets": [
        {
          "node": "modedepth",
          "param": "gain"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-10-14T00:00:00.000Z"
}