{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "filter-comb",
  "versionKey": "3",
  "name": "Comb filter",
  "description": "A filter tuned to a pitch rather than set to a cutoff. It delays the sound by exactly one period of that pitch and adds it back to itself, so everything at the pitch and its harmonics reinforces and everything between cancels — a whole row of peaks and notches at once, which is the sound of a short room, a cardboard tube, or a plucked string depending on how much of it goes round again. Resonance is how much: near one it rings on into a pitch of its own long after the sound that started it, and below nothing the feedback is subtracted instead, which shifts every notch by half a spacing and leaves the hollow, woody sound an octave below where you would expect it. Damping takes a little more of the top off each time round, so the ring dies from the treble down as a string does rather than all at once. Pitch is an input as well as a knob and Pitch depth is how far, in cents — an LFO patched in is a flanger you wired yourself — and Key tracking moves the tuning with the note played, which needs the note routed here: at one the comb rings in tune with whatever is played through it. This module carries code, for the one thing the node vocabulary cannot do: a comb's delay is one divided by its pitch, and nothing in that vocabulary divides. Resonance is an input as well, and its depth how far a signal there moves it; the comb holds what arrives inside the knob's own range, so the ring never runs away.",
  "audioInputs": [
    {
      "id": "in",
      "label": "In"
    },
    {
      "id": "pitch",
      "label": "Pitch"
    },
    {
      "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 comb filter: one delay line fed back into itself, tuned in hertz.\n//\n// The delay and the feedback are both ordinary nodes, and the sound is\n// reachable without code — the Delay module is that patch. What is not\n// reachable is the knob. A comb's delay is the *reciprocal* of the pitch it\n// rings at, and the node vocabulary sums and scales signals; it does not\n// divide one. So nothing built from it can put a frequency on the knob, sweep\n// the tuning evenly in cents, or take its pitch from the note played — and a\n// comb that cannot be tuned is a very short delay, which the library already\n// has. The reciprocal is the whole reason this one carries code.\n//\n// One period of delay, read back with the two samples either side of the\n// fractional position mixed in proportion: at 3 kHz a period is sixteen\n// samples, and rounding to the nearer of them would put the ringing a\n// semitone out and step audibly as it swept.\n//\n// The feedback runs through a single pole of lowpass, so each pass round the\n// loop loses a little more of the top than the last. That is what a plucked\n// string does and what a bare comb does not: undamped, the highest partials\n// ring exactly as long as the lowest, which sounds like a metal tube rather\n// than anything with a body.\n//\n// Every version plays this one program. Version 4 was version 3 doing less of\n// the same work, and every sample it put out was the number it was:\n// `sampleRate` was read on every sample, twice, and in a worklet it is not a\n// variable but an accessor on the global scope, a call out of the script for\n// each read, and most of what versions 1 to 3 cost the audio thread. Version 4\n// read it once a block, and worked the period, a `Math.pow` and a division,\n// out again only when the pitch or its detune moved. All four carry version\n// 4's way of working it out.\n//\n// What arrives is channel 0 of the input, and the input is two channels wide\n// so that a stereo source keeps its left, as a worklet handed what arrived\n// did. An input nothing sounds into reads as zeros, which is what the worklet\n// added in place of a source that was not there.\nimport { AtmoProcessor } from \"atmosynth:dsp\";\n\n/** What a knob is held as before it is first seen, which no reading of it equals. */\nconst UNSEEN = 0 / 0;\n\nfunction clamp(value, low, high) {\n  return value < low ? low : value > high ? high : value;\n}\n\nexport default class AtmosynthComb extends AtmoProcessor {\n  static spec = 1;\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 into them is followed exactly.\n  static params = {\n    frequency: { min: 20, max: 8000, default: 220, rate: \"a\", smooth: 0 },\n    detune: { min: -4800, max: 4800, default: 0, rate: \"a\", smooth: 0 },\n    // Negative is the other polarity, not less of the same one: the feedback\n    // subtracted rather than added moves every notch half a spacing and\n    // leaves the odd harmonics, which is the hollow, half-pitched sound.\n    resonance: { min: -0.95, max: 0.95, default: 0.6, rate: \"a\", smooth: 0 },\n    damping: { min: 0, max: 1, default: 0.2, rate: \"a\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    // A twentieth of a second of line, which is one period of the lowest\n    // tuning the knob reaches, at whatever rate this context runs at.\n    this.line = new Float32Array(Math.ceil(sampleRate / 20) + 4);\n    this.write = 0;\n    this.damped = 0;\n    /** The pitch and detune the period below was last worked out for. */\n    this.frequency = UNSEEN;\n    this.detune = UNSEEN;\n    this.period = 2;\n  }\n\n  process(inputs, outputs, parameters) {\n    const channel = outputs[0][0];\n    const source = inputs[0][0];\n    const frequencies = parameters.frequency;\n    const detunes = parameters.detune;\n    const resonances = parameters.resonance;\n    const dampings = parameters.damping;\n    const line = this.line;\n    const size = line.length;\n    const contextRate = sampleRate;\n\n    for (let index = 0; index < channel.length; index += 1) {\n      const frequency = frequencies[index];\n      const detune = detunes[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, contextRate / 4);\n        // The reciprocal: one period of that pitch, in samples.\n        this.period = clamp(contextRate / hz, 2, size - 2);\n      }\n      const period = this.period;\n      let read = this.write - period;\n      if (read < 0) read += size;\n      const whole = Math.floor(read);\n      const next = whole + 1 >= size ? 0 : whole + 1;\n      const fraction = read - whole;\n      const delayed = line[whole] + (line[next] - line[whole]) * fraction;\n\n      // At nothing the pole passes everything and the comb rings evenly; at\n      // one it keeps a fiftieth of each pass, and the ring is over almost\n      // before it starts.\n      const opening = 1 - dampings[index] * 0.98;\n      this.damped += opening * (delayed - this.damped);\n\n      const value = source[index] + resonances[index] * this.damped;\n      line[this.write] = value;\n      this.write = this.write + 1 >= size ? 0 : this.write + 1;\n      channel[index] = value;\n    }\n    return true;\n  }\n}\n"
      }
    },
    {
      "id": "pitchin",
      "kind": "moduleInput",
      "options": {
        "input": "pitch"
      }
    },
    {
      "id": "pitchdepth",
      "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": "pitchin",
        "output": 0
      },
      "to": {
        "node": "pitchdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "pitchdepth",
        "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": "pitch",
      "label": "Pitch (Hz)",
      "range": {
        "min": "20",
        "max": "8000"
      },
      "default": "220",
      "targets": [
        {
          "node": "gen",
          "param": "frequency"
        }
      ],
      "scale": "log"
    },
    {
      "id": "resonance",
      "label": "Resonance (below nothing to invert)",
      "range": {
        "min": "-0.95",
        "max": "0.95"
      },
      "default": "0.6",
      "targets": [
        {
          "node": "gen",
          "param": "resonance"
        }
      ]
    },
    {
      "id": "damping",
      "label": "Damping",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.2",
      "targets": [
        {
          "node": "gen",
          "param": "damping"
        }
      ]
    },
    {
      "id": "pitchAmount",
      "label": "Pitch depth (cents)",
      "range": {
        "min": "-4800",
        "max": "4800"
      },
      "default": "1200",
      "targets": [
        {
          "node": "pitchdepth",
          "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": "0.5",
      "targets": [
        {
          "node": "trim",
          "param": "gain"
        }
      ]
    },
    {
      "id": "resonanceAmount",
      "label": "Resonance depth",
      "range": {
        "min": "-1.9",
        "max": "1.9"
      },
      "default": "0.4",
      "targets": [
        {
          "node": "resonancedepth",
          "param": "gain"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-09-25T00:00:00.000Z"
}