{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "contour",
  "versionKey": "1",
  "name": "Contour",
  "description": "A shape a note makes for something else to follow, as a Moog’s contour makes it: it rises from nothing to one and falls back, each stage a capacitor charging or emptying rather than a straight line. It makes no sound. Patch it into a modulation input — a filter’s Cutoff above all — and how far it moves what it reaches is set there.\n\nDelay waits before it begins. Attack is the time from nothing to one: it charges fast off the mark and arrives still moving, which is the snap a hardware envelope has and a straight line lacks. Hold keeps it at one. Decay falls to the Sustain level, which it holds while the key is down, and Release falls to nothing once the key is let go, from wherever the shape had got to, mid-attack included.\n\nDecay and Release are the time a whole fall from one to nothing takes. What each sets is how fast the shape falls, as on an analog envelope, so a decay to a sustain of a half, or a release from below one, arrives sooner than its knob says.\n\nLoop, while the key is held, takes the decay back round to the delay and the attack rather than resting at the sustain, so a held note becomes a slow wave of the contour’s own shape, falling to the sustain level each time round. Let go, it releases from wherever it was.\n\nVelocity is how much the strength a key was struck with counts. At nothing it counts for nothing; at one a key struck at half strength takes the shape half as high. A bass that barks only when it is dug into is a filter this opens, with Velocity turned up. A note slid into under a glide is the same note moved, so the shape carries on where it was and keeps the strength it was struck with; a new note starts from nothing.\n\nThis module carries code because an envelope the vocabulary can draw is laid down by the clock at the moment a key goes down: it cannot stop an attack where it reaches one from wherever it began, nor go round again for as long as a key nobody can see the end of is held.",
  "audioInputs": [],
  "audioOutputs": [
    {
      "id": "out",
      "label": "Out"
    }
  ],
  "midiInputs": [
    {
      "id": "midi",
      "label": "Note"
    }
  ],
  "nodes": [
    {
      "id": "note",
      "kind": "midiInput",
      "options": {
        "midiInput": "midi",
        "signal": "velocity"
      }
    },
    {
      "id": "gate",
      "kind": "constantSource",
      "options": {}
    },
    {
      "id": "contour",
      "kind": "dsp",
      "options": {
        "code": "// The contour of an analog synthesizer: delay, attack, hold, decay, sustain\n// and release, each stage a capacitor charging or discharging rather than a\n// straight line, and a loop that goes back round the first four for as long\n// as the key is held. The vocabulary's own envelope is breakpoints laid down\n// in advance, at the moment a key goes down, by the clock: it can draw a\n// straight line or a ratio and nothing else, and it cannot decide anything\n// by where the shape has got to. An attack that charges from wherever it is\n// until it reaches one, and a loop that comes round again for as long as a\n// hand stays on a key nobody can see the end of, are both decisions of that\n// kind, made a sample at a time.\n//\n// **The attack** charges towards 1.3 and is stopped at one: an RC charge cut\n// off before it flattens, fast off the mark and still moving when it arrives,\n// which is the snap of a hardware envelope that a straight line lacks. Its\n// knob is the time from nothing to one, so from wherever a retrigger caught\n// it, it arrives sooner.\n//\n// **The decay and the release** fall towards their target and a thousandth\n// past it — sixty decibels down — and are stopped on it, so each arrives in\n// a time the knob says rather than approaching for ever. That time is the\n// whole of a fall from one to nothing: the knob sets how fast the capacitor\n// discharges, as an analog envelope's does, so a decay to a sustain of a half,\n// or a release from below full, takes less. A release starts from wherever the\n// key let go of the shape, mid-attack included.\n//\n// **Delay** waits before the attack and **Hold** stays at one after it, both\n// counted in samples. **Loop**, while the key is down, takes the decay back\n// round to the delay and the attack rather than resting at the sustain, so a\n// held key is a slow wave of the contour's own shape; let go, it releases from\n// wherever it was.\n//\n// **The gate** is a level that is one while the key is held, which the module\n// makes from an automation on a constant: the one way a module is told a key\n// went down and came back up. Its last point is the release's time, so the\n// voice is held open for as long as the release lasts and no longer. **What\n// comes out** is the contour times `1 − amount + amount × velocity`, where\n// `velocity` is how hard the key was struck, from nothing to one, and `amount`\n// the Velocity knob; and beside it the same less one, for a gain whose own\n// gain is one to be opened by exactly the contour, from nothing.\n//\n// Every coefficient is worked out again only when the knob it depends on\n// moves, and `sampleRate` is read once a block.\n//\n// Written again in DSP JS, it computes every sample the number it did: a knob\n// not yet seen is held as `0 / 0`, which no reading of it equals, and each\n// output is the one channel the browser gave it.\nimport { AtmoProcessor } from \"atmosynth:dsp\";\n\n/** How far past one the attack aims, as a share of its swing. */\nconst OVERSHOOT = 0.3;\n/** How far past its target a fall aims, and so how close it comes before it is there. */\nconst SETTLE = 0.001;\n/** How many time constants a whole swing takes to arrive, rising and falling. */\nconst RISE = Math.log((1 + OVERSHOOT) / OVERSHOOT);\nconst FALL = Math.log((1 + SETTLE) / SETTLE);\n\nconst IDLE = 0;\nconst DELAY = 1;\nconst ATTACK = 2;\nconst HOLD = 3;\nconst DECAY = 4;\nconst SUSTAIN = 5;\nconst RELEASE = 6;\n\n/** What a knob not yet seen is held as, which no reading of it equals. */\nconst UNSEEN = 0 / 0;\n\nexport default class AtmosynthContour extends AtmoProcessor {\n  static spec = 1;\n  static inputs = [];\n  static outputs = [1, 1];\n  // Not smoothed here: a knob's move reaches these already eased, and the\n  // gate and the velocity are followed exactly.\n  static params = {\n    // One while the key is held, from the module's gate.\n    gate: { min: 0, max: 1, default: 0, rate: \"a\", smooth: 0 },\n    // How hard the key was struck, from the module's note input.\n    velocity: { min: 0, max: 1, default: 0, rate: \"a\", smooth: 0 },\n    // The four times, in milliseconds.\n    delay: { min: 0, max: 10000, default: 0, rate: \"k\", smooth: 0 },\n    attack: { min: 0, max: 10000, default: 5, rate: \"k\", smooth: 0 },\n    hold: { min: 0, max: 10000, default: 0, rate: \"k\", smooth: 0 },\n    decay: { min: 0, max: 10000, default: 600, rate: \"k\", smooth: 0 },\n    sustain: { min: 0, max: 1, default: 0.5, rate: \"k\", smooth: 0 },\n    release: { min: 0, max: 10000, default: 400, rate: \"k\", smooth: 0 },\n    // 0 off, 1 on.\n    loop: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n    // How much velocity counts, from nothing to everything.\n    amount: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    this.stage = IDLE;\n    this.level = 0;\n    /** Samples still to wait in a delay or a hold. */\n    this.left = 0;\n    /** Whether the gate was high a sample ago. */\n    this.open = false;\n    /** The knobs each coefficient was last worked out for, and what it came to. */\n    this.delayMs = UNSEEN;\n    this.delaySamples = 0;\n    this.attackMs = UNSEEN;\n    this.attackCoef = 0;\n    this.attackBase = 0;\n    this.holdMs = UNSEEN;\n    this.holdSamples = 0;\n    this.decayMs = UNSEEN;\n    this.sustain = UNSEEN;\n    this.decayCoef = 0;\n    this.decayBase = 0;\n    this.releaseMs = UNSEEN;\n    this.releaseCoef = 0;\n    this.releaseBase = 0;\n  }\n\n  process(inputs, outputs, parameters) {\n    const shape = outputs[0][0];\n    const opening = outputs[1][0];\n    const gate = parameters.gate;\n    const velocity = parameters.velocity;\n    const rate = sampleRate;\n\n    const delay = parameters.delay[0];\n    if (delay !== this.delayMs) {\n      this.delayMs = delay;\n      this.delaySamples = Math.round((delay * rate) / 1000);\n    }\n    const attack = parameters.attack[0];\n    if (attack !== this.attackMs) {\n      this.attackMs = attack;\n      this.attackCoef = Math.exp(-RISE / ((attack * rate) / 1000));\n      this.attackBase = (1 + OVERSHOOT) * (1 - this.attackCoef);\n    }\n    const hold = parameters.hold[0];\n    if (hold !== this.holdMs) {\n      this.holdMs = hold;\n      this.holdSamples = Math.round((hold * rate) / 1000);\n    }\n    const decay = parameters.decay[0];\n    const sustain = parameters.sustain[0];\n    if (decay !== this.decayMs || sustain !== this.sustain) {\n      this.decayMs = decay;\n      this.sustain = sustain;\n      this.decayCoef = Math.exp(-FALL / ((decay * rate) / 1000));\n      this.decayBase = (sustain - SETTLE) * (1 - this.decayCoef);\n    }\n    const release = parameters.release[0];\n    if (release !== this.releaseMs) {\n      this.releaseMs = release;\n      this.releaseCoef = Math.exp(-FALL / ((release * rate) / 1000));\n      this.releaseBase = -SETTLE * (1 - this.releaseCoef);\n    }\n    const looping = parameters.loop[0] >= 0.5;\n    const amount = parameters.amount[0];\n    const kept = 1 - amount;\n\n    let stage = this.stage;\n    let level = this.level;\n    let left = this.left;\n    for (let index = 0; index < shape.length; index += 1) {\n      // A key down starts the delay from wherever the shape is; a key up\n      // releases it from there.\n      const high = gate[index] >= 0.5;\n      if (high !== this.open) {\n        this.open = high;\n        if (high) {\n          stage = DELAY;\n          left = this.delaySamples;\n        } else if (stage !== IDLE) {\n          stage = RELEASE;\n        }\n      }\n\n      // A stage that ends hands this same sample to the one after it, so a\n      // delay or a hold of nothing costs nothing.\n      if (stage === DELAY) {\n        if (left > 0) left -= 1;\n        else stage = ATTACK;\n      }\n      if (stage === ATTACK) {\n        level = this.attackBase + level * this.attackCoef;\n        if (level >= 1) {\n          level = 1;\n          stage = HOLD;\n          left = this.holdSamples;\n        }\n      } else if (stage === HOLD) {\n        if (left > 0) left -= 1;\n        else stage = DECAY;\n      }\n      if (stage === DECAY) {\n        level = this.decayBase + level * this.decayCoef;\n        if (level <= sustain) {\n          level = sustain;\n          stage = looping ? DELAY : SUSTAIN;\n          left = this.delaySamples;\n        }\n      } else if (stage === SUSTAIN) {\n        level = sustain;\n        if (looping) {\n          stage = DELAY;\n          left = this.delaySamples;\n        }\n      } else if (stage === RELEASE) {\n        level = this.releaseBase + level * this.releaseCoef;\n        if (level <= 0) {\n          level = 0;\n          stage = IDLE;\n        }\n      }\n\n      const value = level * (kept + amount * velocity[index]);\n      shape[index] = value;\n      opening[index] = value - 1;\n    }\n    this.stage = stage;\n    this.level = level;\n    this.left = left;\n    return true;\n  }\n}\n"
      }
    },
    {
      "id": "out",
      "kind": "moduleOutput",
      "options": {
        "output": "out"
      }
    }
  ],
  "connections": [
    {
      "from": {
        "node": "note",
        "output": 0
      },
      "to": {
        "node": "contour",
        "param": "velocity"
      }
    },
    {
      "from": {
        "node": "gate",
        "output": 0
      },
      "to": {
        "node": "contour",
        "param": "gate"
      }
    },
    {
      "from": {
        "node": "contour",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    }
  ],
  "automation": [
    {
      "id": "gate",
      "label": "Gate",
      "target": {
        "node": "gate",
        "param": "offset"
      },
      "breakpoints": [
        {
          "time": 0,
          "value": "1",
          "curve": "step"
        },
        {
          "time": 0,
          "value": "0",
          "curve": "step"
        },
        {
          "time": 400,
          "value": "0",
          "curve": "step"
        }
      ],
      "sustainAt": 0
    }
  ],
  "parameters": [
    {
      "id": "delay",
      "label": "Delay (ms)",
      "range": {
        "min": "0",
        "max": "10000"
      },
      "default": "0",
      "targets": [
        {
          "node": "contour",
          "param": "delay"
        }
      ]
    },
    {
      "id": "attack",
      "label": "Attack (ms to full)",
      "range": {
        "min": "1",
        "max": "10000"
      },
      "default": "5",
      "targets": [
        {
          "node": "contour",
          "param": "attack"
        }
      ],
      "scale": "log"
    },
    {
      "id": "hold",
      "label": "Hold (ms at full)",
      "range": {
        "min": "0",
        "max": "10000"
      },
      "default": "0",
      "targets": [
        {
          "node": "contour",
          "param": "hold"
        }
      ]
    },
    {
      "id": "decay",
      "label": "Decay (ms for a full fall)",
      "range": {
        "min": "1",
        "max": "10000"
      },
      "default": "600",
      "targets": [
        {
          "node": "contour",
          "param": "decay"
        }
      ],
      "scale": "log"
    },
    {
      "id": "sustain",
      "label": "Sustain level",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "contour",
          "param": "sustain"
        }
      ]
    },
    {
      "id": "release",
      "label": "Release (ms for a full fall)",
      "range": {
        "min": "1",
        "max": "10000"
      },
      "default": "400",
      "targets": [
        {
          "node": "contour",
          "param": "release"
        },
        {
          "automation": "gate",
          "breakpoint": 2,
          "field": "time"
        }
      ],
      "scale": "log"
    },
    {
      "id": "loop",
      "label": "Loop",
      "values": [
        "off",
        "on"
      ],
      "default": "off",
      "targets": [
        {
          "node": "contour",
          "param": "loop"
        }
      ]
    },
    {
      "id": "velocity",
      "label": "Velocity",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0",
      "targets": [
        {
          "node": "contour",
          "param": "amount"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-10-12T00:00:00.000Z"
}