{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "tape-stop",
  "versionKey": "1",
  "name": "Tape stop",
  "description": "A tape slowing to a halt under whatever it was playing, and spinning back up again. Stop at nothing is the signal passing straight through, sample for sample; turn it past half way and the tape loses speed over Stop time, the pitch falling and the music dragging with it until it stops dead and goes quiet. Turn it back under half way and the tape comes back up to speed over Start time, the pitch rising out of nothing, and once it is running it crosses back to the music as it is now, since slowing down left it behind. A Start time of nothing comes straight back instead. Both times are counted in beats against the one tempo the whole instrument keeps — the same one the arpeggiator and the sequencer count against — so a stop of one is a beat long and a stop of four is a bar, and moving the tempo moves them with it. Curve is how the speed goes: at nothing it falls evenly, which is a hand on a turntable; turned up, most of it goes in the first moments and the rest is a long low growl, which is a heavy platter coasting to a halt. Stop is an input as well, with a depth for how far a signal there moves it, which is what it is played by: a controller, a square LFO stopping the tape on every bar, a sample and hold stopping it at random. Let go part way down and the tape spins back up from where it had got to; let go once it has halted and it starts again from the music playing now, because nothing was heard at the halt to jump from. It carries a small piece of code, because where the tape is is its speed added up over time, and nothing else here adds up. It holds sound that outlives the note that made it, so it is added shared — per voice, each note would take its own tape with it when it ended.",
  "audioInputs": [
    {
      "id": "in",
      "label": "In"
    },
    {
      "id": "stop",
      "label": "Stop"
    }
  ],
  "audioOutputs": [
    {
      "id": "out",
      "label": "Out"
    }
  ],
  "midiInputs": [],
  "nodes": [
    {
      "id": "in",
      "kind": "moduleInput",
      "options": {
        "input": "in"
      }
    },
    {
      "id": "stopin",
      "kind": "moduleInput",
      "options": {
        "input": "stop"
      }
    },
    {
      "id": "depth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "beat",
      "kind": "constantSource",
      "options": {
        "signal": "beat"
      }
    },
    {
      "id": "stoptime",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "starttime",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "tape",
      "kind": "dsp",
      "options": {
        "code": "// A tape slowing to a halt under what it was playing, and spinning back up.\n//\n// Everything that comes in is written to a line, and what goes out is read\n// from it by a head that moves at the tape's speed. Running, the speed is one\n// and the head sits on the sample just written, so what leaves is exactly what\n// arrived. Stopped, the speed falls to nothing over Stop time, and the head\n// falls behind as it slows: the pitch drops and the music drags, which is the\n// whole of a tape stop. Let go, the speed comes back over Start time, and the\n// head is then behind the music by however much the slowing cost; it\n// crossfades back to the sample just written, and the tape is running again.\n//\n// This is code for one reason. Where the head is, is the speed added up over\n// time, and nothing in the vocabulary adds anything up. A delay line whose\n// time is ramped does slow the sound it reads, but only along a ramp laid down\n// in advance, and a ramp cannot be laid down by a Stop arriving from an LFO or\n// a controller at a moment nobody knew about beforehand.\n//\n// Released from a halt, the head starts from the sample just written rather\n// than from where it stopped: nothing was heard at the halt, so the jump is\n// silent, and the spin up plays the music as it is now rather than a stretch\n// of it from seconds ago. Released part way down, the head spins back up from\n// where it is, since a jump there would be heard.\n//\n// The same shape is used both ways, so reversing half way is seamless: a\n// phase runs from one (full speed) to nothing (halted) at the rate each time\n// sets, and the speed is the phase raised to a power Curve sets — one is a\n// steady brake, three is a heavy platter coasting, most of its speed gone in\n// the first moments and the rest a long growl.\n//\n// Both sides are always played. The worklet this replaces made a second line\n// only when a second channel arrived, so a mono patch never held one; here the\n// input is always two channels, a mono source arriving on both, and the two\n// lines play the same thing, so a mono source sounds as it did. Where nothing\n// arrived at all, the worklet played one side and let the other stand; here both\n// go on playing the silence.\nimport { AtmoProcessor } from \"atmosynth:dsp\";\n\n// Longest stop or start the knobs and the slowest tempo can ask for, in\n// seconds: four beats at twenty to the minute. A whole stop leaves the head at\n// most three quarters of that behind, so the line holds it with room. The two\n// time parameters' top is this number.\nconst LONGEST = 12;\n// A time of nothing would be a click; this is short enough to be heard as a\n// cut, and long enough not to be heard as one.\nconst SHORTEST = 0.005;\n// How long the head takes to cross back to the sample just written once the\n// tape is up to speed. The two are different stretches of the music, so the\n// crossfade keeps their power rather than their sum.\nconst CROSSFADE = 0.03;\n// The speed under which the tape fades to silence. What it plays down there is\n// under hearing already, and a halted head would otherwise hold whatever\n// sample it stopped on, which is a step to a constant.\nconst QUIET = 0.04;\n\nfunction clamp(value, low, high) {\n  return value < low ? low : value > high ? high : value;\n}\n\nexport default class AtmosynthTapeStop extends AtmoProcessor {\n  static spec = 1;\n  static inputs = [2];\n  static outputs = [2];\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    // Stopped above half way: the knob plus whatever the Stop input brings,\n    // which can take it past either end, hence the width.\n    stop: { min: -1000, max: 1000, default: 0, rate: \"a\", smooth: 0 },\n    // The two times in seconds — the beat times each knob, worked out in the\n    // graph from the one tempo the instrument keeps. Nothing of their own,\n    // because what arrives at a param is added to its value.\n    stopTime: { min: 0, max: 12, default: 0, rate: \"k\", smooth: 0 },\n    startTime: { min: 0, max: 12, default: 0, rate: \"k\", smooth: 0 },\n    curve: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    this.size = Math.ceil(LONGEST * sampleRate) + 2;\n    // One line per side.\n    this.left = new Float32Array(this.size);\n    this.right = new Float32Array(this.size);\n    this.write = 0;\n    this.phase = 1;\n    // How far behind the sample just written the head is, in samples.\n    this.lag = 0;\n    // While crossing back: how far behind the second head is, which started\n    // on the sample just written, and how far across the crossing has got.\n    this.fresh = -1;\n    this.across = 0;\n  }\n\n  process(inputs, outputs, parameters) {\n    const input = inputs[0];\n    const output = outputs[0];\n    const size = this.size;\n    const frames = output[0].length;\n    const deepest = size - 2;\n\n    const down = 1 / (Math.max(parameters.stopTime[0], SHORTEST) * sampleRate);\n    const up = 1 / (Math.max(parameters.startTime[0], SHORTEST) * sampleRate);\n    const power = 1 + 2 * clamp(parameters.curve[0], 0, 1);\n    const crossing = 1 / (CROSSFADE * sampleRate);\n    const stops = parameters.stop;\n\n    let write = this.write;\n    let phase = this.phase;\n    let lag = this.lag;\n    let fresh = this.fresh;\n    let across = this.across;\n    for (let index = 0; index < frames; index += 1) {\n      const stopping = stops[index] > 0.5;\n      if (stopping) {\n        phase = Math.max(0, phase - down);\n      } else if (phase < 1) {\n        if (phase === 0) {\n          lag = 0;\n          fresh = -1;\n          across = 0;\n        }\n        phase = Math.min(1, phase + up);\n      }\n      const speed = Math.pow(phase, power);\n      const level = Math.min(1, speed / QUIET);\n\n      // Up to speed and still behind: start crossing back.\n      if (!stopping && phase === 1 && lag > 0 && fresh < 0) {\n        fresh = 0;\n        across = 0;\n      }\n\n      for (let side = 0; side < 2; side += 1) {\n        const line = side === 0 ? this.left : this.right;\n        line[write] = input[side][index];\n        let heard = 0;\n        if (level > 0) {\n          heard = read(line, write, lag, size);\n          if (fresh >= 0) {\n            const angle = across * (Math.PI / 2);\n            heard = heard * Math.cos(angle) + read(line, write, fresh, size) * Math.sin(angle);\n          }\n        }\n        output[side][index] = level * heard;\n      }\n\n      // Both heads move at the tape's speed, so each falls behind by what\n      // that is short of one.\n      lag = Math.min(deepest, lag + 1 - speed);\n      if (fresh >= 0) {\n        fresh = Math.min(deepest, fresh + 1 - speed);\n        across += crossing;\n        if (across >= 1) {\n          lag = fresh;\n          fresh = -1;\n          across = 0;\n        }\n      }\n      write = write + 1 >= size ? 0 : write + 1;\n    }\n    this.write = write;\n    this.phase = phase;\n    this.lag = lag;\n    this.fresh = fresh;\n    this.across = across;\n    return true;\n  }\n}\n\n// What a line holds `behind` samples before `write`, between two samples where\n// that is not a whole number. At no lag at all it is the sample just written.\nfunction read(line, write, behind, size) {\n  let at = write - behind;\n  if (at < 0) at += size;\n  const whole = Math.floor(at);\n  const fraction = at - whole;\n  if (fraction === 0) return line[whole];\n  const next = whole + 1 >= size ? 0 : whole + 1;\n  return line[whole] + (line[next] - line[whole]) * fraction;\n}\n",
        "tail": true
      }
    },
    {
      "id": "out",
      "kind": "moduleOutput",
      "options": {
        "output": "out"
      }
    }
  ],
  "connections": [
    {
      "from": {
        "node": "in",
        "output": 0
      },
      "to": {
        "node": "tape",
        "input": 0
      }
    },
    {
      "from": {
        "node": "tape",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "beat",
        "output": 0
      },
      "to": {
        "node": "stoptime",
        "input": 0
      }
    },
    {
      "from": {
        "node": "beat",
        "output": 0
      },
      "to": {
        "node": "starttime",
        "input": 0
      }
    },
    {
      "from": {
        "node": "stoptime",
        "output": 0
      },
      "to": {
        "node": "tape",
        "param": "stopTime"
      }
    },
    {
      "from": {
        "node": "starttime",
        "output": 0
      },
      "to": {
        "node": "tape",
        "param": "startTime"
      }
    },
    {
      "from": {
        "node": "stopin",
        "output": 0
      },
      "to": {
        "node": "depth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "depth",
        "output": 0
      },
      "to": {
        "node": "tape",
        "param": "stop"
      }
    }
  ],
  "automation": [],
  "parameters": [
    {
      "id": "stop",
      "label": "Stop",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0",
      "targets": [
        {
          "node": "tape",
          "param": "stop"
        }
      ]
    },
    {
      "id": "stopAmount",
      "label": "Stop amount",
      "range": {
        "min": "-1",
        "max": "1"
      },
      "default": "1",
      "targets": [
        {
          "node": "depth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "time",
      "label": "Stop time (beats)",
      "range": {
        "min": "0.0625",
        "max": "4"
      },
      "default": "1",
      "targets": [
        {
          "node": "stoptime",
          "param": "gain"
        }
      ]
    },
    {
      "id": "start",
      "label": "Start time (beats)",
      "range": {
        "min": "0",
        "max": "4"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "starttime",
          "param": "gain"
        }
      ]
    },
    {
      "id": "curve",
      "label": "Curve",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0",
      "targets": [
        {
          "node": "tape",
          "param": "curve"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-09-29T00:00:00.000Z"
}