{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "delay-analog",
  "versionKey": "1",
  "name": "Analog delay",
  "description": "A delay whose repeats are an analog machine's rather than copies: each comes back through what such a machine puts round its loop, so it is darker, softer and rounder than the one before. Type is which machine. BBD is a bucket-brigade delay, as in an Electro-Harmonix Memory Man: a row of buckets handed along at a clock, so a longer delay is a slower clock and a darker line — a quarter of a second rolls off above three kilohertz, two seconds below one — and a loud repeat is rounded off as the buckets fill. Tape is a tape echo: the tape wavers in speed, a slow wow that wanders and a quick flutter, so the repeats waver in pitch by a few cents and no two are alike; the playback head lifts the bass round 110 Hz; the record head saturates a little lopsided, as tape does; and the repeats lose their top, more the slower the tape runs for a longer delay. Delay time is how long a repeat waits. A change of it arrives over a moment, as a clock or a motor slews, so the repeats already sounding bend down in pitch as it lengthens and up as it shortens. Feedback is how much of each repeat goes round again: short of one they die away, at one they fade only slowly, and past one the delay sings on its own, growing until the machine's own saturation holds it at a warm, round level — never a runaway. Tone darkens the loop, so every repeat once more than the last. Dry level is what came in and Wet level the repeats, summed rather than crossfaded. Delay time, Feedback and Wet are inputs as well as knobs, each with a depth for how far a signal there moves it, and the time input moves the time at once, so an LFO there bends the repeats at its own rate. Every knob the plain delay has is the same knob here, so a patch can trade one for the other. The tail outlives the note that made it, so it is added shared, one for the whole patch, as an effect at the end of the chain is. This module carries code, because a bucket brigade darkens with its clock, which is the number of buckets divided by the delay, and nothing else here can divide; and because the tape's wander is a random signal, which nothing else here makes.",
  "audioInputs": [
    {
      "id": "in",
      "label": "In"
    },
    {
      "id": "time",
      "label": "Time"
    },
    {
      "id": "feedback",
      "label": "Feedback"
    },
    {
      "id": "wet",
      "label": "Wet"
    }
  ],
  "audioOutputs": [
    {
      "id": "out",
      "label": "Out"
    }
  ],
  "midiInputs": [],
  "nodes": [
    {
      "id": "in",
      "kind": "moduleInput",
      "options": {
        "input": "in"
      }
    },
    {
      "id": "dry",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "line",
      "kind": "dsp",
      "options": {
        "code": "// A delay whose repeats are an analog machine's: one line, read behind where\n// it is written and fed back into itself through what such a machine puts\n// round its loop, so each repeat passes it all once more than the repeat\n// before and comes back darker, softer and rounder.\n//\n// Type is which machine. A bucket brigade is a row of capacitors handing what\n// they hold along at a clock — two MN3005s in a row, 8192 buckets, as the\n// Electro-Harmonix Memory Man has — so its delay is the buckets over twice the\n// clock, and a longer delay is a slower clock. What reaches the end is held a\n// clock tick at a time, and a hold loses the top by how long it holds, half\n// its power at 0.443 of the clock: a short delay is bright and a long one\n// dark, the hold losing half its power at seven kilohertz at a quarter of a\n// second and under one at two seconds. Around the line are the filters every\n// such line needs, two poles before it so nothing the clock cannot carry gets\n// in and two after to take the steps of the hold away, and those darken it\n// for good.\n//\n// A tape echo is a loop of tape running past a record head and then a\n// playback head, so its delay is the distance between them over the tape's\n// speed, and a longer delay is a slower tape. Slow tape loses its top too, by\n// how short a wavelength it can hold: here by the square root of how much\n// slower it runs than at a third of a second, where it loses half its power\n// at six kilohertz. The playback head's bump lifts the bass round a hundred\n// and ten hertz, as a head's width against the wavelengths it reads does. The\n// record head saturates as tape does, a little lopsided, which is where\n// tape's even harmonics come from. And the tape never runs quite steady: a\n// slow wow and a fast flutter move the delay a little, and the wow wanders as\n// a capstan and a worn pinch roller do, drawn from the module's own dice, so\n// no two passes over the same sound are the same and two renders handed the\n// same seed are.\n//\n// Feedback is how much of each repeat goes round again. Everything round the\n// loop gives back at most what it is handed — the tape's bump is a lift of\n// the bass over the rest rather than above it — so at one the loudest part of\n// a repeat comes back as loud as it went, and short of one every repeat dies\n// away. Past one the loop grows, until the curve on what is written into the\n// line holds it: a warm, round howl held at a level, never a runaway, which is\n// what an analog delay turned past its top does.\n//\n// Tone is a low-pass in the loop, so it darkens every repeat once more than\n// the one before. The line is read between samples by a cubic through the\n// four nearest (Catmull-Rom): the time moves — with the wow, the time input\n// and a turn of the knob — and a straight line between two samples is a\n// low-pass that deepens as the reading falls half way between them, so the\n// top of every repeat would flutter as the reading crossed samples, and\n// compound round the loop. A cubic keeps the top flat to within a fraction of\n// a decibel to a quarter of the rate and has no state to smear when the\n// reading moves fast, as an all-pass reading would.\n//\n// A change of Delay time arrives over a few hundredths of a second and never\n// faster than half a sample a sample, as a clock or a motor slews: the repeats\n// already in the line bend down in pitch as it lengthens and up as it\n// shortens, and never play backwards. The time input moves the time at once,\n// so an LFO wired there is heard at its own rate.\n//\n// Every curve is the ladder's rational tanh and the tape's speed is a square\n// root, arithmetic both engines work out to the same double; the cutoffs that\n// follow the time are prewarped by a rational tangent rather than a library\n// one, since they move every sample.\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 longest and shortest delay the line is read at, in seconds: the knob's\n// two seconds with room for what the time input and the wow add. The line is\n// sized for the longest when it is built and never again.\nconst LONGEST = 2.1;\nconst SHORTEST = 0.002;\n// How long a change of time or of type takes to settle, in seconds, and the\n// fastest a change of time moves the reading, in samples a sample.\nconst GLIDE = 0.08;\nconst SWITCH = 0.03;\nconst SLEW = 0.5;\n\n// The bucket brigade: its buckets, where a hold loses half its power as a\n// share of its clock, and the filters either side of the line.\nconst STAGES = 8192;\nconst HOLD = 0.443;\nconst BBD_CORNER = 8000;\n// The tape: where it loses its top at a third of a second, the amplifiers\n// either side of it, the head's bump and how far it lifts, and how lopsided\n// the record head's curve is.\nconst TAPE_TOP = 6000;\nconst TAPE_AT = 0.3;\nconst TAPE_CORNER = 16000;\nconst BUMP = 110;\nconst LIFT = 0.4;\nconst BIAS = 0.15;\n// The wow, the flutter and the wander, in seconds of delay either way, and\n// how fast each goes in hertz.\nconst WOW = 0.0006;\nconst WOW_RATE = 0.55;\nconst FLUTTER = 0.00003;\nconst FLUTTER_RATE = 6.3;\nconst WANDER = 0.0005;\nconst WANDER_RATE = 0.7;\n// What keeps the loop's lopsided curve from building a level of its own.\nconst LOW_CUT = 20;\n\n// The ladder's tanh, near enough: x(27 + x²) / (27 + 9x²), arriving at one at\n// three with no slope left. Arithmetic and nothing else, so the plugin works it\n// out to the same double.\nfunction soft(value) {\n  return value > 3 ? 1 : value < -3 ? -1 : (value * (27 + value * value)) / (27 + 9 * value * value);\n}\n\n// Its slope where it is leaned to, 9(9 − x²)² / (27 + 9x²)², so the tape's\n// lopsided curve can be brought back to a slope of one for a quiet sound and\n// the edge of singing stays at a Feedback of one.\nfunction lean(x) {\n  const near = 9 - x * x;\n  const below = 27 + 9 * x * x;\n  return (9 * near * near) / (below * below);\n}\n\n// The tangent a one-pole is prewarped by, near enough: x(15 − x²) / (15 − 6x²),\n// within a few per cent of it up to where the cutoffs stop, at 0.45 of the rate.\nfunction warp(x) {\n  return (x * (15 - x * x)) / (15 - 6 * x * x);\n}\n\n// A sine, near enough, from a phase that runs from nothing to one: two\n// parabolas, arithmetic and nothing else.\nfunction wave(phase) {\n  return phase < 0.5 ? 16 * phase * (0.5 - phase) : 16 * (phase - 0.5) * (phase - 1);\n}\n\nexport default class AtmosynthAnalogDelay extends AtmoProcessor {\n  static spec = 1;\n  static inputs = [2];\n  static outputs = [1];\n  // Not smoothed here: the time glides below, a knob's move reaches these\n  // already eased, and the time input is followed exactly.\n  static params = {\n    // 0 the bucket brigade, 1 the tape, by the Type choice's position.\n    type: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n    // The knob, which glides.\n    time: { min: 0.003, max: 2, default: 0.3, rate: \"a\", smooth: 0 },\n    // What the time input adds, in seconds, at once.\n    timeMod: { min: -2, max: 2, default: 0, rate: \"a\", smooth: 0 },\n    feedback: { min: 0, max: 1.2, default: 0.35, rate: \"a\", smooth: 0 },\n    tone: { min: 0, max: 1, default: 0.7, rate: \"k\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    this.rng = new Rng(this.seed);\n    // Four samples longer than the longest delay, for the four a cubic reads.\n    this.size = Math.ceil(LONGEST * sampleRate) + 4;\n    this.line = new Float64Array(this.size);\n    this.write = 0;\n    // The time and the type as they stand, gliding to the knobs'; set\n    // outright the first time, so the delay does not glide in from nothing.\n    this.settled = false;\n    this.time = 0;\n    this.tape = 0;\n    this.glide = 1 / (GLIDE * sampleRate);\n    this.switch = 1 / (SWITCH * sampleRate);\n    this.slew = SLEW / sampleRate;\n    this.ceiling = 0.45 * sampleRate;\n    // The two machines' fixed filters, worked out once.\n    let g = Math.tan((Math.PI * BBD_CORNER) / sampleRate);\n    this.bbdCorner = g / (1 + g);\n    g = Math.tan((Math.PI * TAPE_CORNER) / sampleRate);\n    this.tapeCorner = g / (1 + g);\n    this.bump = Math.tan((Math.PI * BUMP) / sampleRate);\n    g = Math.tan((Math.PI * LOW_CUT) / sampleRate);\n    this.lowCut = g / (1 + g);\n    this.wanderRate = WANDER_RATE / sampleRate;\n    this.wanderSmooth = (2 * Math.PI * WANDER_RATE) / sampleRate;\n    this.wowRate = WOW_RATE / sampleRate;\n    this.flutterRate = FLUTTER_RATE / sampleRate;\n    // The tone the coefficient below was last worked out for.\n    this.tone = 0 / 0;\n    this.toneCorner = 0;\n    // The integrators: two poles before the line, the hold or the tape's top,\n    // two poles after, the head's bump, the low cut and the tone.\n    this.before1 = 0;\n    this.before2 = 0;\n    this.top = 0;\n    this.after1 = 0;\n    this.after2 = 0;\n    this.bump1 = 0;\n    this.bump2 = 0;\n    this.low = 0;\n    this.toned = 0;\n    // The tape's movement: where the wow and the flutter are, and the wander\n    // heading for a place drawn at random and smoothed twice on its way.\n    this.wowPhase = 0;\n    this.flutterPhase = 0;\n    this.wanderPhase = 0;\n    this.goal = 0;\n    this.wander1 = 0;\n    this.wander2 = 0;\n  }\n\n  // The line read `behind` samples before the one about to be written.\n  read(behind) {\n    const line = this.line;\n    const size = this.size;\n    let at = this.write - behind;\n    if (at < 0) at += size;\n    const whole = Math.floor(at);\n    const fraction = at - whole;\n    const y0 = line[whole === 0 ? size - 1 : whole - 1];\n    const y1 = line[whole];\n    const y2 = line[whole + 1 >= size ? whole + 1 - size : whole + 1];\n    const y3 = line[whole + 2 >= size ? whole + 2 - size : whole + 2];\n    const c1 = 0.5 * (y2 - y0);\n    const c2 = y0 - 2.5 * y1 + 2 * y2 - 0.5 * y3;\n    const c3 = 0.5 * (y3 - y0) + 1.5 * (y1 - y2);\n    return ((c3 * fraction + c2) * fraction + c1) * fraction + y1;\n  }\n\n  process(inputs, outputs, parameters) {\n    const input = inputs[0];\n    const channel = outputs[0][0];\n    const frames = channel.length;\n    // In a worklet `sampleRate` is an accessor on the global scope rather than\n    // a variable, so it is read once a block.\n    const contextRate = sampleRate;\n    const type = clamp(Math.round(parameters.type[0]), 0, 1);\n    const times = parameters.time;\n    const shifts = parameters.timeMod;\n    const feedbacks = parameters.feedback;\n    if (!this.settled) {\n      this.settled = true;\n      this.time = times[0];\n      this.tape = type;\n      this.goal = this.rng.bipolar();\n    }\n    // A Math.pow and a Math.tan, worked out again only when the tone moves:\n    // from 400 Hz at nothing to 18 kHz at one, evenly in octaves.\n    const tone = parameters.tone[0];\n    if (tone !== this.tone) {\n      this.tone = tone;\n      const hz = Math.min(400 * Math.pow(2, tone * 5.5), this.ceiling);\n      const g = Math.tan((Math.PI * hz) / contextRate);\n      this.toneCorner = g / (1 + g);\n    }\n    const toneCorner = this.toneCorner;\n    const bump = this.bump;\n\n    for (let index = 0; index < frames; index += 1) {\n      // A mono source arrives on both sides, and nothing as silence on both.\n      const left = input[0][index];\n      const right = input[1][index];\n\n      // The time and the type glide to the knobs', and settle there exactly.\n      const target = times[index];\n      const step = (target - this.time) * this.glide;\n      this.time += step > this.slew ? this.slew : step < -this.slew ? -this.slew : step;\n      if (Math.abs(target - this.time) < 1e-12) this.time = target;\n      this.tape += (type - this.tape) * this.switch;\n      if (Math.abs(type - this.tape) < 1e-9) this.tape = type;\n      const tape = this.tape;\n\n      // The tape's movement: the wander heads for a place drawn at random,\n      // a new one a little more often than once a second, and the wow speeds\n      // up and slows down with it.\n      this.wanderPhase += this.wanderRate;\n      if (this.wanderPhase >= 1) {\n        this.wanderPhase -= 1;\n        this.goal = this.rng.bipolar();\n      }\n      this.wander1 += (this.goal - this.wander1) * this.wanderSmooth;\n      this.wander2 += (this.wander1 - this.wander2) * this.wanderSmooth;\n      const wander = this.wander2;\n      this.wowPhase += this.wowRate * (1 + 0.3 * wander);\n      if (this.wowPhase >= 1) this.wowPhase -= 1;\n      this.flutterPhase += this.flutterRate;\n      if (this.flutterPhase >= 1) this.flutterPhase -= 1;\n      const movement =\n        WOW * wave(this.wowPhase) + WANDER * wander + FLUTTER * (1 + 0.5 * wander) * wave(this.flutterPhase);\n      const shift = shifts[index];\n      const delay = clamp(this.time + shift + tape * movement, SHORTEST, LONGEST);\n\n      // What the playback end hands back.\n      let wet = this.read(delay * contextRate);\n\n      // The hold at the line's clock, or the tape's top at its speed: each\n      // one over the delay, and each darker as it lengthens.\n      const held = (HOLD * STAGES) / (2 * delay);\n      const worn = TAPE_TOP * Math.sqrt(TAPE_AT / delay);\n      const cutoff = Math.min(held + tape * (worn - held), this.ceiling);\n      const g = warp((Math.PI * cutoff) / contextRate);\n      let v = (wet - this.top) * (g / (1 + g));\n      wet = v + this.top;\n      this.top = wet + v;\n\n      // The two poles after the line: the bucket brigade's, or the tape's\n      // playback amplifier, nearly open.\n      const corner = this.bbdCorner + tape * (this.tapeCorner - this.bbdCorner);\n      v = (wet - this.after1) * corner;\n      wet = v + this.after1;\n      this.after1 = wet + v;\n      v = (wet - this.after2) * corner;\n      wet = v + this.after2;\n      this.after2 = wet + v;\n\n      // The head's bump: a band-pass at a hundred and ten hertz, its peak at\n      // one, added and the sum brought back down so the bump is at one and\n      // the rest under it. Always worked out, so a change of type finds it\n      // running.\n      const high = (wet - (1 + bump) * this.bump1 - this.bump2) / (1 + bump * (1 + bump));\n      const band = bump * high + this.bump1;\n      this.bump1 = band + bump * high;\n      const lowered = bump * band + this.bump2;\n      this.bump2 = lowered + bump * band;\n      wet = (wet + tape * LIFT * band) / (1 + tape * LIFT);\n\n      // The low cut, then the tone.\n      v = (wet - this.low) * this.lowCut;\n      const below = v + this.low;\n      this.low = below + v;\n      wet -= below;\n      v = (wet - this.toned) * toneCorner;\n      wet = v + this.toned;\n      this.toned = wet + v;\n      channel[index] = wet;\n\n      // What goes into the line: what arrived and what came round, through\n      // the two poles before it, rounded as the buckets fill or the tape\n      // saturates — lopsided by the tape's bias, and nothing out for nothing in.\n      const feedback = feedbacks[index];\n      const into = 0.5 * (left + right) + feedback * wet;\n      v = (into - this.before1) * corner;\n      const once = v + this.before1;\n      this.before1 = once + v;\n      v = (once - this.before2) * corner;\n      const twice = v + this.before2;\n      this.before2 = twice + v;\n      const bias = tape * BIAS;\n      this.line[this.write] = (soft(twice + bias) - soft(bias)) / lean(bias);\n      this.write = this.write + 1 >= this.size ? 0 : this.write + 1;\n    }\n    return true;\n  }\n}\n",
        "tail": true
      }
    },
    {
      "id": "wet",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "out",
      "kind": "moduleOutput",
      "options": {
        "output": "out"
      }
    },
    {
      "id": "timein",
      "kind": "moduleInput",
      "options": {
        "input": "time"
      }
    },
    {
      "id": "timedepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "feedbackin",
      "kind": "moduleInput",
      "options": {
        "input": "feedback"
      }
    },
    {
      "id": "feedbackdepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "wetin",
      "kind": "moduleInput",
      "options": {
        "input": "wet"
      }
    },
    {
      "id": "wetdepth",
      "kind": "gain",
      "options": {}
    }
  ],
  "connections": [
    {
      "from": {
        "node": "in",
        "output": 0
      },
      "to": {
        "node": "dry",
        "input": 0
      }
    },
    {
      "from": {
        "node": "dry",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "in",
        "output": 0
      },
      "to": {
        "node": "line",
        "input": 0
      }
    },
    {
      "from": {
        "node": "line",
        "output": 0
      },
      "to": {
        "node": "wet",
        "input": 0
      }
    },
    {
      "from": {
        "node": "wet",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "timein",
        "output": 0
      },
      "to": {
        "node": "timedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "timedepth",
        "output": 0
      },
      "to": {
        "node": "line",
        "param": "timeMod"
      }
    },
    {
      "from": {
        "node": "feedbackin",
        "output": 0
      },
      "to": {
        "node": "feedbackdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "feedbackdepth",
        "output": 0
      },
      "to": {
        "node": "line",
        "param": "feedback"
      }
    },
    {
      "from": {
        "node": "wetin",
        "output": 0
      },
      "to": {
        "node": "wetdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "wetdepth",
        "output": 0
      },
      "to": {
        "node": "wet",
        "param": "gain"
      }
    }
  ],
  "automation": [],
  "parameters": [
    {
      "id": "type",
      "label": "Type",
      "values": [
        "BBD",
        "tape"
      ],
      "default": "BBD",
      "targets": [
        {
          "node": "line",
          "param": "type"
        }
      ]
    },
    {
      "id": "time",
      "label": "Delay time (s)",
      "range": {
        "min": "0.003",
        "max": "2"
      },
      "default": "0.3",
      "targets": [
        {
          "node": "line",
          "param": "time"
        }
      ]
    },
    {
      "id": "timeAmount",
      "label": "Time depth (s)",
      "range": {
        "min": "-0.05",
        "max": "0.05"
      },
      "default": "0.002",
      "targets": [
        {
          "node": "timedepth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "feedback",
      "label": "Feedback (sings above one)",
      "range": {
        "min": "0",
        "max": "1.2"
      },
      "default": "0.35",
      "targets": [
        {
          "node": "line",
          "param": "feedback"
        }
      ]
    },
    {
      "id": "tone",
      "label": "Tone",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.7",
      "targets": [
        {
          "node": "line",
          "param": "tone"
        }
      ]
    },
    {
      "id": "dry",
      "label": "Dry level",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "1",
      "targets": [
        {
          "node": "dry",
          "param": "gain"
        }
      ]
    },
    {
      "id": "wet",
      "label": "Wet level",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "wet",
          "param": "gain"
        }
      ]
    },
    {
      "id": "feedbackAmount",
      "label": "Feedback depth",
      "range": {
        "min": "-1.2",
        "max": "1.2"
      },
      "default": "0.3",
      "targets": [
        {
          "node": "feedbackdepth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "wetAmount",
      "label": "Wet depth",
      "range": {
        "min": "-1",
        "max": "1"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "wetdepth",
          "param": "gain"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-10-14T00:00:00.000Z"
}