{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "filter-ms20",
  "versionKey": "1",
  "name": "Sallen-Key filter pair",
  "description": "A resonant high-pass and then a resonant low-pass, built as the Korg MS-20's are: two filters of two poles each, one after the other, each with its own cutoff and its own resonance. With the high-pass at the bottom, where it starts, it is a low-pass of twelve decibels an octave; brought up under the low-pass's cutoff, the two are a band-pass as wide as the gap between them; and either can be made to sing. 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. Turning it up takes nothing away from the bass, as it does on the ladder, so the peak stands out over a full low end. What holds a singing filter at a level is a pair of diodes clipping its feedback, so the tone it sings has an edge on it, and a loud sound played into a resonance turned high is clipped at the peak while the rest of it comes through — the MS-20's scream. Drive is how hard the sound is pushed into the filter: at one a sound at full scale passes nearly clean, and above it it thickens and compresses. 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, and the high-pass's cutoff has a depth and an input of its own; Key tracking moves both cutoffs with the note played, which needs the note routed here. Resonance is an input as well, for each filter, and its depth how far a signal there moves it. However far it is pushed, it stays inside the knob's own range. The low-pass's knobs are the ladder's, without Slope, and it plays at about the ladder's level, 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"
    },
    {
      "id": "hpCutoff",
      "label": "High-pass cutoff"
    },
    {
      "id": "hpResonance",
      "label": "High-pass 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": "// Two filters in a row, built as the Korg MS-20's are: a resonant high-pass\n// and then a resonant low-pass, each of two poles with a cutoff and a\n// resonance of its own. With the high-pass at the bottom it is a low-pass;\n// with the two set apart it is a band-pass as wide as the gap between them;\n// and either can be made to sing.\n//\n// Each is a Sallen-Key filter, as the MS-20's are: two one-poles in a row,\n// with the output fed back to between them. The low-pass's feedback goes\n// through a one-pole high-pass and the high-pass's through a one-pole\n// low-pass, so it is in phase only at the cutoff and is nothing at the far end\n// of the passband. Where a ladder's feedback takes the bass away, then, this\n// one leaves it where it was however far Resonance is turned, and the peak\n// stands that much further above it. For a quiet signal each is\n//   low-pass  1 / (s² + (2 - K)s + 1)\n//   high-pass s² / (s² + (2 - K)s + 1)\n// in units of its cutoff, where K is twice Resonance: at one the damping is\n// gone and it rings on at the cutoff, and past one it sings.\n//\n// What holds it singing at a level is the MS-20's own: a pair of diodes in\n// the feedback, which clip it. For a quiet signal they are not there; as the\n// ring grows they give back less than they are handed, until the feedback\n// makes up for the two poles exactly and no more. They clip what goes round\n// the loop rather than a stage the sound passes through, so the tone it sings\n// has an edge on it — its third harmonic thirty decibels down at 1.2, where\n// the ladder's is forty — and a loud sound played in with Resonance up is\n// clipped at the peak while the rest of it passes, which is the scream. The\n// diodes' curve is the ladder's rational tanh, topping out at DIODES.\n//\n// The output is half what the low-pass gives. With no resonance that is the\n// ladder's own level at the cutoff, twelve decibels down, and the two stay\n// within a decibel of each other there as Resonance rises, so this one plays\n// and sings near the ladder's and the diode ladder's and they swap in a patch\n// without a jump; what it keeps that they lose is the bass under the peak.\n// Drive is how hard the sound is pushed in, through an input stage whose curve\n// is three times as wide as the ladder's: at one a sound at full scale comes\n// through nearly clean, and driven as hard as it goes it comes out, half taken\n// off, about as loud as the ladder and the diode ladder do. It is kept out of\n// the feedback, 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 either loop, as filter-sem's is: the\n// one-poles are trapezoidal and each loop is worked out for this sample's own\n// values, so a cutoff lands where its knob says at any height. The diodes are\n// taken as the straight line from nothing to where they stand when the loop\n// is first worked out without them, and the loop worked out once more through\n// that line. What each loop is divided by is one less a share of the feedback\n// that is never more than six tenths, 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²), which leaves the\n// origin at the slope a tanh does and arrives at one at three with no slope\n// left, where it stays. It is arithmetic and nothing else, so the plugin works\n// it out to the same double.\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 as the slope of the straight line from nothing to where it is\n// at `value`: one at nothing and less further out, written so that it divides\n// only by what is past three.\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 diodes let the feedback go, which is where their curve tops out.\n// Eight tenths lets the loudest either filter sings, at a resonance of 1.2,\n// come out near the ladder's.\nconst DIODES = 0.8;\n// How much wider the input stage's curve is than the ladder's.\nconst ROOM = 3;\n\nexport default class AtmosynthMs20 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    // The low-pass's, which are the ladder's.\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    // The high-pass's, which are the low-pass's with its cutoff at the\n    // bottom, where it takes nothing away.\n    hpFrequency: { min: 20, max: 20000, default: 20, rate: \"a\", smooth: 0 },\n    hpDetune: { min: -9600, max: 9600, default: 0, rate: \"a\", smooth: 0 },\n    hpResonance: { min: 0, max: 1.2, default: 0.4, rate: \"a\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    // The high-pass's three one-poles: the first, the second, and the\n    // low-pass its feedback goes through.\n    this.h1 = 0;\n    this.h2 = 0;\n    this.h3 = 0;\n    // The low-pass's: the first, the second, and the high-pass its feedback\n    // goes through.\n    this.l1 = 0;\n    this.l2 = 0;\n    this.l3 = 0;\n    // Each cutoff and detune its coefficients below were last worked out for:\n    // not yet any, which no value equals.\n    this.frequency = 0 / 0;\n    this.detune = 0 / 0;\n    this.lowGain = 0;\n    this.lowHold = 0;\n    this.hpFrequency = 0 / 0;\n    this.hpDetune = 0 / 0;\n    this.highGain = 0;\n    this.highHold = 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\n    for (let index = 0; index < channel.length; index += 1) {\n      // A Math.pow and a Math.tan for each, worked out again only when they\n      // would come out different. A trapezoidal one-pole takes `gain` of what\n      // arrives and `hold` of what it holds, prewarped so the cutoff lands\n      // where the knob says rather than flat of it.\n      const hpFrequency = parameters.hpFrequency[index];\n      const hpDetune = parameters.hpDetune[index];\n      if (hpFrequency !== this.hpFrequency || hpDetune !== this.hpDetune) {\n        this.hpFrequency = hpFrequency;\n        this.hpDetune = hpDetune;\n        const hz = clamp(hpFrequency * Math.pow(2, hpDetune / 1200), 20, ceiling);\n        const g = Math.tan((Math.PI * hz) / contextRate);\n        this.highGain = g / (1 + g);\n        this.highHold = 1 / (1 + g);\n      }\n      const frequency = parameters.frequency[index];\n      const detune = parameters.detune[index];\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        const g = Math.tan((Math.PI * hz) / contextRate);\n        this.lowGain = g / (1 + g);\n        this.lowHold = 1 / (1 + g);\n      }\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. Into both filters, so the\n      // low-pass sings with the high-pass set far above it.\n      const dither = 1e-6 * this.rng.bipolar();\n      const x =\n        ROOM * saturate((parameters.drive[index] * source[index] + dither) / ROOM);\n\n      // The high-pass. Its first one-pole's high-pass, then the second's of\n      // that plus the feedback, which is the low-pass of the output through\n      // the diodes.\n      let gain = this.highGain;\n      let hold = this.highHold;\n      let k = 2 * parameters.hpResonance[index];\n      const first = hold * (x - this.h1);\n      // Where it stands with the diodes a straight line, then with them as the\n      // line to where they stand there, and again.\n      let high = (hold * (first - this.h2 + k * hold * this.h3)) / (1 - hold * k * gain);\n      let diodes = slope((k * (gain * high + hold * this.h3)) / DIODES);\n      high = (hold * (first - this.h2 + diodes * k * hold * this.h3)) / (1 - hold * diodes * k * gain);\n      const fed = gain * high + hold * this.h3;\n      this.h1 = 2 * (x - first) - this.h1;\n      this.h2 = 2 * (first + diodes * k * fed - high) - this.h2;\n      this.h3 = 2 * fed - this.h3;\n\n      // The low-pass, the same way round: its first one-pole's low-pass, then\n      // the second's of that plus the feedback, which is the high-pass of the\n      // output through the diodes.\n      gain = this.lowGain;\n      hold = this.lowHold;\n      k = 2 * parameters.resonance[index];\n      const low1 = gain * (high + dither) + hold * this.l1;\n      let low = (gain * low1 + hold * this.l2 - gain * k * hold * this.l3) / (1 - gain * k * hold);\n      diodes = slope((k * hold * (low - this.l3)) / DIODES);\n      low = (gain * low1 + hold * this.l2 - gain * diodes * k * hold * this.l3) / (1 - gain * diodes * k * hold);\n      const back = hold * (low - this.l3);\n      this.l1 = 2 * low1 - this.l1;\n      this.l2 = 2 * low - this.l2;\n      this.l3 = 2 * (low - back) - this.l3;\n\n      // What its curves leave off centre, taken off under 5 Hz.\n      const raw = 0.5 * low;\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": {}
    },
    {
      "id": "hpcutoffin",
      "kind": "moduleInput",
      "options": {
        "input": "hpCutoff"
      }
    },
    {
      "id": "hpcutoffdepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "hpresonancein",
      "kind": "moduleInput",
      "options": {
        "input": "hpResonance"
      }
    },
    {
      "id": "hpresonancedepth",
      "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": "trackdepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "hpFrequency"
      }
    },
    {
      "from": {
        "node": "hpcutoffin",
        "output": 0
      },
      "to": {
        "node": "hpcutoffdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "hpcutoffdepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "hpDetune"
      }
    },
    {
      "from": {
        "node": "hpresonancein",
        "output": 0
      },
      "to": {
        "node": "hpresonancedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "hpresonancedepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "hpResonance"
      }
    }
  ],
  "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"
        }
      ]
    },
    {
      "id": "hpCutoff",
      "label": "High-pass cutoff (Hz)",
      "range": {
        "min": "20",
        "max": "18000"
      },
      "default": "20",
      "targets": [
        {
          "node": "gen",
          "param": "hpFrequency"
        }
      ],
      "scale": "log"
    },
    {
      "id": "hpResonance",
      "label": "High-pass resonance (sings above one)",
      "range": {
        "min": "0",
        "max": "1.2"
      },
      "default": "0.4",
      "targets": [
        {
          "node": "gen",
          "param": "hpResonance"
        }
      ]
    },
    {
      "id": "hpCutoffAmount",
      "label": "High-pass cutoff depth (cents)",
      "range": {
        "min": "-9600",
        "max": "9600"
      },
      "default": "2400",
      "targets": [
        {
          "node": "hpcutoffdepth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "hpResonanceAmount",
      "label": "High-pass resonance depth",
      "range": {
        "min": "-1.2",
        "max": "1.2"
      },
      "default": "0.4",
      "targets": [
        {
          "node": "hpresonancedepth",
          "param": "gain"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-10-14T00:00:00.000Z"
}