{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "reverb",
  "versionKey": "3",
  "name": "Reverb",
  "description": "A room computed rather than recorded, with every control a real one. What comes in is reflected off the walls of a modelled space, spread by a diffuser and sent round eight delay lines that feed each other, losing a little each time round — so the tail can be as short or as long as you ask, and changes while it rings.\n\nSpace chooses the room, from a small ambience to a cathedral, and the decay time that goes with it; Decay rate scales that time from a quarter to four times. Style is the kind of reverb: modern is clean and natural, vintage is the grainier, darker sound of an early digital unit with a wandering modulation, and plate is a sheet of steel, dense and bright from the first moment with no walls to reflect off. A change of style fades one room out and the next in. Pre-delay is the gap before the room answers.\n\nCharacter moves from a clean, transparent tail, through a lively one whose reflections and echoes stand out, to a moving, chorused one. Brightness is how much the air and the walls take off the top: dull and absorbent at one end, ringing at the other. Distance is where you stand: near, the reflections are loud and bright and the tail arrives with them; far, they are fainter and the tail takes longer to build. Thickness makes the tail denser and holds a loud one down with a soft knee. Stereo width runs from mono, through the room as it is, to wider than it.\n\nThe decay can be shaped by frequency: Low decay and High decay scale how long the lows and the highs last against the middle, below and above the frequencies beside them. Low cut and High cut take the bottom and the top off what the room returns. Freeze holds the tail as it is, for as long as it is on, and lets nothing new into it. It is an input as well: what arrives there freezes the room while it is above one half, so a gate or a square LFO holds the tail in time and lets it go again, and a negative depth turns a Freeze that is on off instead. Ducking turns the room down while something is arriving, by up to the decibels it says, so a part stays clear and the tail fills the gaps. Auto gate cuts the tail off once the input has been quiet for the hold time — the gated sound of an eighties drum.\n\nDry and Wet are summed, each set on its own; the dry signal never passes through the room. Its tail outlives the note that made it — give it one instance for the whole patch, or each note takes its room with it. Wet is an input as well, with a depth for how far a signal there moves it: an envelope into it lets the room bloom as a note ends.",
  "audioInputs": [
    {
      "id": "in",
      "label": "In"
    },
    {
      "id": "wet",
      "label": "Wet"
    },
    {
      "id": "freeze",
      "label": "Freeze"
    }
  ],
  "audioOutputs": [
    {
      "id": "out",
      "label": "Out"
    }
  ],
  "midiInputs": [],
  "nodes": [
    {
      "id": "in",
      "kind": "moduleInput",
      "options": {
        "input": "in"
      }
    },
    {
      "id": "dry",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "room",
      "kind": "dsp",
      "options": {
        "code": "// A room computed rather than recorded: early reflections, a diffuser, and\n// eight delay lines feeding each other through a Hadamard matrix, losing a\n// little of each band every time round.\n//\n// This is code because the decay is a feedback gain per line that depends on\n// that line's length, the decay time and the band, and the vocabulary can set\n// a gain to a knob but not to ten to the power of a knob divided by a length.\n// It is also what lets every control be real: Space changes the lines'\n// lengths and the decay with them, Decay rate scales the time, and the decay\n// equaliser is three gains per line rather than a filter after the fact.\n//\n// What it is handed and what it returns is the wet signal only. The dry path\n// and the cuts after it are the graph's, so a dry signal never passes through\n// anything here, and the output is always two sides: a mono note comes back\n// as a room around it.\n//\n// Every sample is computed with addition, multiplication, division and square\n// roots, which give the same double on every engine. Sines, exponentials and\n// powers are taken once a block, for coefficients, so a port elsewhere differs\n// from this at most in the last bit of a coefficient rather than by a drifting\n// phase. Nothing is allocated once the processor is built.\n//\n// Versions 3 and 4 play this one program. Version 4 read `sampleRate` once a\n// block where version 3 read it in the gate on every sample, and every step\n// it worked out was the same double, so both carry version 4's way of working\n// them out.\n//\n// The tables of lengths, reflections and signs are typed arrays the\n// constructor fills, since a module's top level holds only numbers; they are\n// described where they are filled.\nimport { AtmoProcessor, copy, fill } from \"atmosynth:dsp\";\n\nconst LINES = 8;\n// The diffuser the sound passes before it reaches the lines: four allpasses\n// a side.\nconst DIFFUSERS = 4;\n// Early reflections, ten a side.\nconst TAPS = 10;\n// The longest room line at a size of one, in seconds: the last of the room's\n// lengths below.\nconst LONGEST_ROOM_LINE = 0.0731;\n// The tail is heard from the end of each line and from two points along it,\n// so a long line is heard before the sound has been all the way round, and a\n// large space fills in sooner. Each sum is a different row of the Hadamard\n// matrix, so the sides and the taps are uncorrelated.\nconst OUTPUT_TAP_NEAR = 0.31;\nconst OUTPUT_TAP_FAR = 0.67;\nconst TAP_LEVEL = 0.5;\n\n// The largest Space makes the room lines this many times their length, and\n// the smallest this many; the plate's span is narrower.\nconst ROOM_SMALLEST = 0.18;\nconst ROOM_LARGEST = 2.3;\nconst PLATE_SMALLEST = 0.5;\nconst PLATE_LARGEST = 1.6;\n// The decay Space chooses runs from a small ambience to a cathedral.\nconst SHORTEST_DECAY = 0.2;\nconst LONGEST_DECAY = 10;\n// The deepest modulation, in seconds, at a Character of one.\nconst DEEPEST = 0.003;\n// The `predelay` parameter's top is this number.\nconst LONGEST_PREDELAY = 0.5;\n// Reflections arrive within this, at the largest Space.\nconst REFLECTION_TIME = 0.09;\n// How much later than the reflections the tail may start, at full Distance.\nconst LATEST_TAIL = 0.05;\n// How long a line takes to follow Space to a new length.\nconst GLIDE = 0.05;\n// The diffuser grows with Space, from this share of its length to this.\nconst DIFFUSER_SMALLEST = 0.6;\nconst DIFFUSER_LARGEST = 1.6;\n// How much of the diffused sound is heard directly, as the start of the\n// tail, before the lines have gone round once.\nconst ONSET = 0.5;\n// How long a change of Style takes to fade the room out, and the new one in.\nconst SWAP = 0.01;\n// How often the vintage modulation picks somewhere new to wander to.\nconst WANDER = 0.05;\n// The input follower, and the gate it opens.\nconst ATTACK = 0.002;\nconst RELEASE = 0.2;\n// The gate listens faster than the ducking does: it has to hear the input\n// stop, not how loud it was.\nconst GATE_RELEASE = 0.01;\nconst GATE_FLOOR = 0.003;\nconst GATE_OPEN = 0.003;\nconst GATE_CLOSE = 0.06;\n// Under this a line holds nothing: a tail decayed past hearing stops costing\n// subnormal arithmetic.\nconst NOTHING = 1e-30;\n\nfunction clamp(value, low, high) {\n  return value < low ? low : value > high ? high : value;\n}\n\n// Eight of a table's entries, from `at` on.\nfunction eight(table, at, a, b, c, d, e, f, g, h) {\n  table[at] = a;\n  table[at + 1] = b;\n  table[at + 2] = c;\n  table[at + 3] = d;\n  table[at + 4] = e;\n  table[at + 5] = f;\n  table[at + 6] = g;\n  table[at + 7] = h;\n}\n\n// Ten of a table's entries, from `at` on.\nfunction ten(table, at, a, b, c, d, e, f, g, h, i, j) {\n  eight(table, at, a, b, c, d, e, f, g, h);\n  table[at + 8] = i;\n  table[at + 9] = j;\n}\n\n// What one block's knobs decide. Two of these are kept and swapped, and the\n// samples of a block move from the last one to the next.\nclass Settings {\n  constructor() {\n    this.vintage = false;\n    this.lengths = new Float64Array(LINES);\n    this.gains = new Float64Array(LINES * 3);\n    this.turnCos = new Float64Array(LINES);\n    this.turnSin = new Float64Array(LINES);\n    this.taps = new Int32Array(TAPS * 2);\n    this.diffusers = new Int32Array(DIFFUSERS * 2);\n    this.depth = 0;\n    this.early = 0;\n    this.feed = 0;\n    this.later = 0;\n    this.predelay = 0;\n    this.toneEarly = 0;\n    this.toneIn = 0;\n    this.diffusion = 0;\n    this.density = 0;\n    this.drive = 0;\n    this.width = 0;\n    this.lowSplit = 0;\n    this.highSplit = 0;\n    this.duck = 0;\n    this.gate = false;\n    this.hold = 0;\n  }\n}\n\nexport default class AtmosynthReverb 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 each\n  // block moves across from where the last one left off.\n  static params = {\n    space: { min: 0, max: 1, default: 0.5, rate: \"k\", smooth: 0 },\n    decay: { min: 0.25, max: 4, default: 1, rate: \"k\", smooth: 0 },\n    predelay: { min: 0, max: 0.5, default: 0.02, rate: \"k\", smooth: 0 },\n    style: { min: 0, max: 2, default: 0, rate: \"k\", smooth: 0 },\n    character: { min: 0, max: 1, default: 0.3, rate: \"k\", smooth: 0 },\n    brightness: { min: 0, max: 1, default: 0.5, rate: \"k\", smooth: 0 },\n    distance: { min: 0, max: 1, default: 0.3, rate: \"k\", smooth: 0 },\n    thickness: { min: 0, max: 1, default: 0.5, rate: \"k\", smooth: 0 },\n    width: { min: 0, max: 1.5, default: 1, rate: \"k\", smooth: 0 },\n    lowDecay: { min: 0.25, max: 4, default: 1, rate: \"k\", smooth: 0 },\n    lowFreq: { min: 20, max: 2000, default: 250, rate: \"k\", smooth: 0 },\n    highDecay: { min: 0.25, max: 4, default: 1, rate: \"k\", smooth: 0 },\n    highFreq: { min: 500, max: 16000, default: 4000, rate: \"k\", smooth: 0 },\n    freeze: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n    ducking: { min: 0, max: 30, default: 0, rate: \"k\", smooth: 0 },\n    gate: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n    gateHold: { min: 0, max: 2, default: 0.2, rate: \"k\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    const rate = sampleRate;\n    // Line lengths at a size of one, in seconds: spread so no two share a\n    // common period, which is what keeps a tail from ringing at one pitch.\n    this.roomLines = new Float64Array(LINES);\n    eight(this.roomLines, 0, 0.0301, 0.0353, 0.0419, 0.0473, 0.0539, 0.0593, 0.0667, LONGEST_ROOM_LINE);\n    // A plate is a small, dense thing: short lines, close together.\n    this.plateLines = new Float64Array(LINES);\n    eight(this.plateLines, 0, 0.0113, 0.0137, 0.0161, 0.0193, 0.0229, 0.0257, 0.0283, 0.0319);\n    // An allpass inside each line, which multiplies how many echoes a pass makes\n    // without changing how much is lost. Fixed lengths, so Space never moves them.\n    this.loopAllpasses = new Float64Array(LINES);\n    eight(this.loopAllpasses, 0, 0.0031, 0.0037, 0.0043, 0.0049, 0.0053, 0.0059, 0.0067, 0.0073);\n    // The diffusers' lengths, a little different on each side so the two\n    // sides do not start alike.\n    this.roomDiffusers = new Float64Array(DIFFUSERS * 2);\n    eight(this.roomDiffusers, 0, 0.0047, 0.0036, 0.0127, 0.0093, 0.0051, 0.0033, 0.0119, 0.0101);\n    this.plateDiffusers = new Float64Array(DIFFUSERS * 2);\n    eight(this.plateDiffusers, 0, 0.0014, 0.0011, 0.0042, 0.0031, 0.0016, 0.001, 0.0039, 0.0034);\n    // The early reflections: when each arrives, as a share of the reflection\n    // time, and how loud, with the sign of the wall that sent it.\n    this.tapTimes = new Float64Array(TAPS * 2);\n    ten(this.tapTimes, 0, 0.043, 0.117, 0.19, 0.268, 0.341, 0.452, 0.557, 0.668, 0.79, 0.94);\n    ten(this.tapTimes, TAPS, 0.071, 0.139, 0.223, 0.297, 0.389, 0.488, 0.603, 0.712, 0.851, 0.97);\n    this.tapGains = new Float64Array(TAPS * 2);\n    ten(this.tapGains, 0, 0.84, -0.71, 0.62, -0.55, 0.47, -0.42, 0.35, -0.3, 0.24, -0.18);\n    ten(this.tapGains, TAPS, 0.8, -0.68, 0.6, -0.52, 0.45, -0.39, 0.33, -0.28, 0.22, -0.17);\n    // Six rows of the Hadamard matrix, a row after the other, for the sums\n    // the tail is heard through.\n    this.signs = new Float64Array(LINES * 6);\n    eight(this.signs, 0, 1, -1, 1, -1, 1, -1, 1, -1);\n    eight(this.signs, LINES, 1, 1, -1, -1, 1, 1, -1, -1);\n    eight(this.signs, LINES * 2, 1, 1, 1, 1, -1, -1, -1, -1);\n    eight(this.signs, LINES * 3, 1, -1, -1, 1, 1, -1, -1, 1);\n    eight(this.signs, LINES * 4, 1, -1, 1, -1, -1, 1, -1, 1);\n    eight(this.signs, LINES * 5, 1, 1, -1, -1, -1, -1, 1, 1);\n    // How fast each line's modulation turns, in hertz, before Character speeds it.\n    this.rates = new Float64Array(LINES);\n    eight(this.rates, 0, 0.13, 0.19, 0.23, 0.29, 0.31, 0.37, 0.41, 0.47);\n\n    // One line a side holding what arrived, for the pre-delay, the\n    // reflections and the tail's later start to read from.\n    this.heldSize = Math.ceil((LONGEST_PREDELAY + REFLECTION_TIME + LATEST_TAIL) * rate) + 4;\n    this.held = [new Float64Array(this.heldSize), new Float64Array(this.heldSize)];\n    this.written = 0;\n\n    this.lineSize = Math.ceil((LONGEST_ROOM_LINE * ROOM_LARGEST + 2 * DEEPEST * 1.5) * rate) + 4;\n    const lineSize = this.lineSize;\n    this.lines = [\n      new Float64Array(lineSize),\n      new Float64Array(lineSize),\n      new Float64Array(lineSize),\n      new Float64Array(lineSize),\n      new Float64Array(lineSize),\n      new Float64Array(lineSize),\n      new Float64Array(lineSize),\n      new Float64Array(lineSize),\n    ];\n    this.loopSizes = new Int32Array(LINES);\n    for (let i = 0; i < LINES; i += 1) {\n      this.loopSizes[i] = Math.round(this.loopAllpasses[i] * rate);\n    }\n    const loopSizes = this.loopSizes;\n    this.loops = [\n      new Float64Array(loopSizes[0]),\n      new Float64Array(loopSizes[1]),\n      new Float64Array(loopSizes[2]),\n      new Float64Array(loopSizes[3]),\n      new Float64Array(loopSizes[4]),\n      new Float64Array(loopSizes[5]),\n      new Float64Array(loopSizes[6]),\n      new Float64Array(loopSizes[7]),\n    ];\n    this.lineWrite = 0;\n    this.loopAt = new Int32Array(LINES);\n    // Where each line reads, in samples, gliding to what Space asks for.\n    this.lengths = new Float64Array(LINES);\n    // Each line's two band splitters and what it gave this sample.\n    this.lows = new Float64Array(LINES);\n    this.highs = new Float64Array(LINES);\n    this.outs = new Float64Array(LINES);\n    // What each line's allpass read gave last, which the next one needs.\n    this.lastRead = new Float64Array(LINES);\n    this.mixed = new Float64Array(LINES);\n    // A quadrature oscillator per line: the modulation, turned by rotation\n    // rather than by taking a sine each sample.\n    this.cos = new Float64Array(LINES);\n    this.sin = new Float64Array(LINES);\n    for (let i = 0; i < LINES; i += 1) {\n      const phase = (i * 2 * Math.PI) / LINES;\n      this.cos[i] = Math.cos(phase);\n      this.sin[i] = Math.sin(phase);\n    }\n    // The vintage modulation: a random walk each line follows, from a\n    // generator with a fixed seed, so the room is the same room every time.\n    // Its own, not the host's `seed`, which this never reads.\n    this.walk = 0x2545f491;\n    this.wander = new Float64Array(LINES);\n    this.wanderTo = new Float64Array(LINES);\n    this.countdown = 0;\n\n    // The diffusers, a side after the other, each big enough for either\n    // style's length.\n    this.diffuserSizes = new Int32Array(DIFFUSERS * 2);\n    this.diffuserAt = new Int32Array(DIFFUSERS * 2);\n    for (let j = 0; j < DIFFUSERS * 2; j += 1) {\n      this.diffuserSizes[j] =\n        Math.round(Math.max(this.roomDiffusers[j], this.plateDiffusers[j]) * DIFFUSER_LARGEST * rate) + 1;\n    }\n    const diffuserSizes = this.diffuserSizes;\n    this.diffusers = [\n      new Float64Array(diffuserSizes[0]),\n      new Float64Array(diffuserSizes[1]),\n      new Float64Array(diffuserSizes[2]),\n      new Float64Array(diffuserSizes[3]),\n      new Float64Array(diffuserSizes[4]),\n      new Float64Array(diffuserSizes[5]),\n      new Float64Array(diffuserSizes[6]),\n      new Float64Array(diffuserSizes[7]),\n    ];\n\n    this.was = new Settings();\n    this.next = new Settings();\n    this.started = false;\n    this.style = 0;\n    this.fade = 1;\n    this.toneIn = new Float64Array(2);\n    this.toneIn2 = new Float64Array(2);\n    this.toneEarly = new Float64Array(2);\n    this.envelope = 0;\n    this.heard = 0;\n    this.gateLevel = 1;\n    this.quiet = 0;\n    this.glide = 1 - Math.exp(-1 / (GLIDE * rate));\n    this.attack = 1 - Math.exp(-1 / (ATTACK * rate));\n    this.release = 1 - Math.exp(-1 / (RELEASE * rate));\n    this.gateRelease = 1 - Math.exp(-1 / (GATE_RELEASE * rate));\n  }\n\n  // What this block's knobs decide, written into `s`.\n  settle(s, parameters, style) {\n    const rate = sampleRate;\n    const plate = style === 2;\n    const vintage = style === 1;\n    const space = clamp(parameters.space[0], 0, 1);\n    const character = clamp(parameters.character[0], 0, 1);\n    const brightness = clamp(parameters.brightness[0], 0, 1);\n    const distance = clamp(parameters.distance[0], 0, 1);\n    const thickness = clamp(parameters.thickness[0], 0, 1);\n    const frozen = parameters.freeze[0] >= 0.5;\n    s.vintage = vintage;\n\n    const small = plate ? PLATE_SMALLEST : ROOM_SMALLEST;\n    const large = plate ? PLATE_LARGEST : ROOM_LARGEST;\n    const scale = small * Math.pow(large / small, space);\n    const decay =\n      SHORTEST_DECAY * Math.pow(LONGEST_DECAY / SHORTEST_DECAY, space) * clamp(parameters.decay[0], 0.25, 4);\n    // Air takes the top off a room faster than its middle; Brightness moves\n    // that from a dull, absorbent room to one that rings at the top. A plate\n    // is steel and rings brighter, and is thin at the bottom.\n    const air = (plate ? 0.65 : 0.5) * Math.pow(2, (brightness - 0.5) * 2.5);\n    const low = clamp(parameters.lowDecay[0], 0.25, 4) * (plate ? 0.7 : 1);\n    const high = clamp(parameters.highDecay[0], 0.25, 4) * air * (vintage ? 0.6 : 1);\n\n    const base = plate ? this.plateLines : this.roomLines;\n    for (let i = 0; i < LINES; i += 1) {\n      s.lengths[i] = base[i] * scale * rate;\n      // What one pass round this line costs, so that sixty decibels are lost\n      // in the decay time: the line and the allpass inside it both count.\n      const pass = base[i] * scale + this.loopAllpasses[i];\n      s.gains[i * 3] = frozen ? 1 : Math.pow(10, (-3 * pass) / (decay * low));\n      s.gains[i * 3 + 1] = frozen ? 1 : Math.pow(10, (-3 * pass) / decay);\n      s.gains[i * 3 + 2] = frozen ? 1 : Math.pow(10, (-3 * pass) / (decay * high));\n    }\n\n    // Character: clean at nothing; at half, the reflections stand out and\n    // the diffuser lets echoes through; at one, a chorus of moving lines.\n    const lively = 1 - Math.abs(2 * character - 1);\n    s.depth = character * character * DEEPEST * rate * (vintage ? 1.5 : 1);\n    const speed = 1 + 3 * character * character;\n    for (let i = 0; i < LINES; i += 1) {\n      const turn = (2 * Math.PI * this.rates[i] * speed) / rate;\n      s.turnCos[i] = Math.cos(turn);\n      s.turnSin[i] = Math.sin(turn);\n    }\n\n    // Distance: near, the reflections are loud and bright and the tail\n    // arrives with them; far, they are fainter and duller, and the tail\n    // takes longer to build.\n    const reach = plate ? 0 : (0.008 + REFLECTION_TIME * space) * rate;\n    for (let j = 0; j < TAPS * 2; j += 1) s.taps[j] = Math.round(this.tapTimes[j] * reach);\n    s.early = frozen || plate ? 0 : (1.1 - distance) * (0.55 + 0.6 * lively) * 0.5;\n    s.toneEarly = 1 - Math.exp((-2 * Math.PI * (16000 - 14000 * distance)) / rate);\n    s.later = distance * (0.005 + LATEST_TAIL * space) * rate;\n    s.toneIn = 1 - Math.exp((-2 * Math.PI * (vintage ? 7000 : 3000 * Math.pow(6, brightness))) / rate);\n    const lengths = plate ? this.plateDiffusers : this.roomDiffusers;\n    const grown = DIFFUSER_SMALLEST * Math.pow(DIFFUSER_LARGEST / DIFFUSER_SMALLEST, space);\n    for (let j = 0; j < DIFFUSERS * 2; j += 1) {\n      s.diffusers[j] = Math.max(1, Math.min(this.diffuserSizes[j] - 1, Math.round(lengths[j] * grown * rate)));\n    }\n    s.diffusion = clamp(0.45 + 0.3 * thickness + 0.12 * distance - 0.25 * lively, 0.2, 0.8);\n    // Thickness: a denser loop, and a softer knee on what goes round.\n    s.density = 0.2 + 0.45 * thickness;\n    // Frozen, nothing may be lost going round, so the knee is let go of.\n    s.drive = frozen ? 0 : thickness * thickness * 1.5;\n    // A longer tail is a louder one, as in a real room, but only a little: fed\n    // as it is, a ten-second decay at four times would sit far above the note\n    // that made it. At Wet one the room is a few decibels under the dry\n    // sound, whatever its size. Frozen, it is fed nothing.\n    s.feed = frozen ? 0 : 0.6 / Math.pow(1 + decay, 0.25);\n    s.predelay = clamp(parameters.predelay[0], 0, LONGEST_PREDELAY) * rate;\n    s.width = clamp(parameters.width[0], 0, 1.5);\n    s.lowSplit = 1 - Math.exp((-2 * Math.PI * clamp(parameters.lowFreq[0], 20, 2000)) / rate);\n    s.highSplit = 1 - Math.exp((-2 * Math.PI * clamp(parameters.highFreq[0], 500, 16000)) / rate);\n    s.duck = 1 - Math.pow(10, -clamp(parameters.ducking[0], 0, 30) / 20);\n    s.gate = parameters.gate[0] >= 0.5;\n    s.hold = clamp(parameters.gateHold[0], 0, 2) * rate;\n  }\n\n  process(inputs, outputs, parameters) {\n    const input = inputs[0];\n    const output = outputs[0];\n    const frames = output[0].length;\n    // Read once a block. In a worklet `sampleRate` is not a variable but an\n    // accessor on the global scope, a call out of the script for every read,\n    // and the gate read it on every sample. Each step is the same double it\n    // was when it was worked out in place.\n    const rate = sampleRate;\n    const opening = 1 / (GATE_OPEN * rate);\n    const closing = 1 / (GATE_CLOSE * rate);\n    const wandering = Math.round(WANDER * rate);\n\n    // A change of style fades the room out, swaps it while nothing is\n    // heard, and fades the new one in: two rooms of different shapes have no\n    // sample in common to move between.\n    const asked = Math.round(clamp(parameters.style[0], 0, 2));\n    if (!this.started) this.style = asked;\n    let restart = !this.started;\n    if (asked !== this.style && this.fade <= 0) {\n      this.style = asked;\n      restart = true;\n      // The new room starts empty. Its lines are other lengths, and reading\n      // the old room's sound at them is a step that would be fed back and\n      // heard on every pass; the old room has faded out already.\n      for (let j = 0; j < DIFFUSERS * 2; j += 1) fill(this.diffusers[j], 0);\n      for (let i = 0; i < LINES; i += 1) fill(this.lines[i], 0);\n      for (let i = 0; i < LINES; i += 1) fill(this.loops[i], 0);\n      fill(this.lows, 0);\n      fill(this.lastRead, 0);\n      fill(this.highs, 0);\n      fill(this.toneEarly, 0);\n    }\n    const swapping = asked !== this.style;\n    const fadeFrom = this.fade;\n    const fadeStep = frames / (SWAP * rate);\n    const fadeTo = swapping ? Math.max(0, fadeFrom - fadeStep) : Math.min(1, fadeFrom + fadeStep);\n    this.fade = fadeTo;\n\n    // The first block starts where its knobs are, and so does the first after\n    // a swap; every other one moves across the block from where the last one\n    // left off.\n    const was = this.next;\n    const next = this.was;\n    this.settle(next, parameters, this.style);\n    if (restart) {\n      this.settle(was, parameters, this.style);\n      copy(this.lengths, next.lengths);\n      this.started = true;\n    }\n    this.was = was;\n    this.next = next;\n\n    const held = this.held;\n    const heldSize = this.heldSize;\n    const lines = this.lines;\n    const lineSize = this.lineSize;\n    const loops = this.loops;\n    const loopSizes = this.loopSizes;\n    const loopAt = this.loopAt;\n    const lengths = this.lengths;\n    const lows = this.lows;\n    const highs = this.highs;\n    const outs = this.outs;\n    const mixed = this.mixed;\n    const cos = this.cos;\n    const sin = this.sin;\n    const wander = this.wander;\n    const wanderTo = this.wanderTo;\n    const lastRead = this.lastRead;\n    const diffusers = this.diffusers;\n    const diffuserSizes = this.diffuserSizes;\n    const diffuserAt = this.diffuserAt;\n    const toneIn = this.toneIn;\n    const toneIn2 = this.toneIn2;\n    const toneEarly = this.toneEarly;\n    const tapGains = this.tapGains;\n    const signs = this.signs;\n    const scale = 1 / Math.sqrt(LINES);\n    const step = 1 / frames;\n    // A mono note arrives on both sides, and nothing as silence on both.\n    const left = input[0];\n    const right = input[1];\n    const vintage = next.vintage;\n\n    let written = this.written;\n    let lineWrite = this.lineWrite;\n    let envelope = this.envelope;\n    let heard = this.heard;\n    let gateLevel = this.gateLevel;\n    let quiet = this.quiet;\n    for (let index = 0; index < frames; index += 1) {\n      const t = (index + 1) * step;\n\n      // What arrives, a mono note heard on both sides, and how loud it is.\n      const inL = left[index];\n      const inR = right[index];\n      const loudest = Math.max(Math.abs(inL), Math.abs(inR));\n      envelope += (loudest - envelope) * (loudest > envelope ? this.attack : this.release);\n      heard += (loudest - heard) * (loudest > heard ? this.attack : this.gateRelease);\n\n      // The tone of what goes in: one pole, or two for the vintage style's\n      // narrower converters.\n      const tone = was.toneIn + (next.toneIn - was.toneIn) * t;\n      toneIn[0] += (inL - toneIn[0]) * tone;\n      toneIn[1] += (inR - toneIn[1]) * tone;\n      toneIn2[0] += (toneIn[0] - toneIn2[0]) * tone;\n      toneIn2[1] += (toneIn[1] - toneIn2[1]) * tone;\n      held[0][written] = vintage ? toneIn2[0] : toneIn[0];\n      held[1][written] = vintage ? toneIn2[1] : toneIn[1];\n\n      // Every reflection is the pre-delay plus a whole number of samples, so\n      // they share where between two samples they fall.\n      const predelay = was.predelay + (next.predelay - was.predelay) * t;\n      let at = written - predelay;\n      if (at < 0) at += heldSize;\n      if (at >= heldSize) at -= heldSize;\n      const whole = Math.floor(at);\n      const fraction = at - whole;\n      const early = was.early + (next.early - was.early) * t;\n      const toneE = was.toneEarly + (next.toneEarly - was.toneEarly) * t;\n      const later = was.later + (next.later - was.later) * t;\n      // A fresh room is fed in as it is faded in: a running sound reaching an\n      // empty diffuser all at once is a click.\n      const faded = fadeFrom + (fadeTo - fadeFrom) * t;\n      const feed = (was.feed + (next.feed - was.feed) * t) * faded;\n      const diffusion = was.diffusion + (next.diffusion - was.diffusion) * t;\n\n      let fedL = 0;\n      let fedR = 0;\n      let earlyL = 0;\n      let earlyR = 0;\n      for (let side = 0; side < 2; side += 1) {\n        const line = held[side];\n        let sum = 0;\n        if (early > 0) {\n          for (let j = side * TAPS; j < side * TAPS + TAPS; j += 1) {\n            let a = whole - next.taps[j];\n            if (a < 0) a += heldSize;\n            const b = a + 1 >= heldSize ? 0 : a + 1;\n            sum += tapGains[j] * (line[a] + (line[b] - line[a]) * fraction);\n          }\n        }\n        toneEarly[side] += (sum * early - toneEarly[side]) * toneE;\n\n        // What the tail is fed, through the diffuser.\n        let x = read(line, written, predelay + later, heldSize) * feed;\n        for (let j = side * DIFFUSERS; j < side * DIFFUSERS + DIFFUSERS; j += 1) {\n          const size = diffuserSizes[j];\n          let from = diffuserAt[j] - next.diffusers[j];\n          if (from < 0) from += size;\n          const chain = diffusers[j];\n          const delayed = chain[from];\n          const v = x + diffusion * delayed;\n          chain[diffuserAt[j]] = Math.abs(v) < NOTHING ? 0 : v;\n          x = delayed - diffusion * v;\n          diffuserAt[j] = diffuserAt[j] + 1 >= size ? 0 : diffuserAt[j] + 1;\n        }\n        if (side === 0) {\n          earlyL = toneEarly[0];\n          fedL = x;\n        } else {\n          earlyR = toneEarly[1];\n          fedR = x;\n        }\n      }\n\n      // The tail: read every line, lose what this pass loses in each band,\n      // thicken it.\n      const depth = was.depth + (next.depth - was.depth) * t;\n      const density = was.density + (next.density - was.density) * t;\n      const drive = was.drive + (next.drive - was.drive) * t;\n      const lowSplit = was.lowSplit + (next.lowSplit - was.lowSplit) * t;\n      const highSplit = was.highSplit + (next.highSplit - was.highSplit) * t;\n      if (vintage) {\n        if (this.countdown <= 0) {\n          for (let i = 0; i < LINES; i += 1) {\n            this.walk = (Math.imul(this.walk, 1664525) + 1013904223) | 0;\n            wanderTo[i] = ((this.walk >>> 8) / 8388608) * 2 - 1;\n          }\n          this.countdown = wandering;\n        }\n        this.countdown -= 1;\n      }\n      for (let i = 0; i < LINES; i += 1) {\n        lengths[i] += (next.lengths[i] - lengths[i]) * this.glide;\n        let swing = 0;\n        if (vintage) {\n          wander[i] += (wanderTo[i] - wander[i]) * 0.0005;\n          swing = wander[i];\n        } else {\n          const c = cos[i] * next.turnCos[i] - sin[i] * next.turnSin[i];\n          sin[i] = sin[i] * next.turnCos[i] + cos[i] * next.turnSin[i];\n          cos[i] = c;\n          swing = sin[i];\n        }\n        let y = passAt(lines[i], lineWrite, Math.max(2, lengths[i] + depth * (1 + swing)), lineSize, lastRead[i]);\n        lastRead[i] = y;\n\n        // Three bands, each losing what its decay time says it should.\n        lows[i] += (y - lows[i]) * lowSplit;\n        highs[i] += (y - highs[i]) * highSplit;\n        const low = lows[i];\n        const top = y - highs[i];\n        const g = i * 3;\n        const gl = was.gains[g] + (next.gains[g] - was.gains[g]) * t;\n        const gm = was.gains[g + 1] + (next.gains[g + 1] - was.gains[g + 1]) * t;\n        const gh = was.gains[g + 2] + (next.gains[g + 2] - was.gains[g + 2]) * t;\n        y = gl * low + gm * (y - low - top) + gh * top;\n\n        // Denser: an allpass inside the loop. Thicker: a soft knee on what\n        // goes round, so a loud tail is held down rather than clipped.\n        const loop = loops[i];\n        const delayed = loop[loopAt[i]];\n        const v = y + density * delayed;\n        loop[loopAt[i]] = Math.abs(v) < NOTHING ? 0 : v;\n        y = delayed - density * v;\n        loopAt[i] = loopAt[i] + 1 >= loopSizes[i] ? 0 : loopAt[i] + 1;\n        if (drive > 0) y = y / Math.sqrt(1 + drive * y * y);\n        outs[i] = y;\n      }\n\n      // Hadamard: every line feeds every other, at equal strength, losing\n      // nothing in the mixing; and each takes its share of what is fed.\n      hadamard(outs, mixed, scale);\n      for (let i = 0; i < LINES; i += 1) {\n        const v = mixed[i] + (i % 2 === 0 ? fedL : fedR) * (i % 4 < 2 ? 1 : -1);\n        lines[i][lineWrite] = Math.abs(v) < NOTHING ? 0 : v;\n      }\n\n      let lateL = 0;\n      let lateR = 0;\n      for (let i = 0; i < LINES; i += 1) {\n        const a = read(lines[i], lineWrite, Math.max(1, lengths[i] * OUTPUT_TAP_NEAR), lineSize) * TAP_LEVEL;\n        const b = read(lines[i], lineWrite, Math.max(1, lengths[i] * OUTPUT_TAP_FAR), lineSize) * TAP_LEVEL;\n        lateL += signs[i] * outs[i] + signs[LINES * 2 + i] * a + signs[LINES * 4 + i] * b;\n        lateR += signs[LINES + i] * outs[i] + signs[LINES * 3 + i] * a + signs[LINES * 5 + i] * b;\n      }\n      let l = earlyL + fedL * ONSET + lateL * scale;\n      let r = earlyR + fedR * ONSET + lateR * scale;\n\n      // Width: the difference between the sides, from none to more than is\n      // there.\n      const width = was.width + (next.width - was.width) * t;\n      const middle = (l + r) * 0.5;\n      const apart = (l - r) * 0.5 * width;\n      l = middle + apart;\n      r = middle - apart;\n\n      // Ducking: the room steps back while something is arriving.\n      const duck = was.duck + (next.duck - was.duck) * t;\n      const ducked = 1 - duck * Math.min(1, envelope * 4);\n\n      // The gate: open while there is input, held for a while after it\n      // stops, then let go quickly.\n      if (next.gate) {\n        quiet = heard > GATE_FLOOR ? 0 : quiet + 1;\n        gateLevel =\n          quiet <= next.hold\n            ? Math.min(1, gateLevel + opening)\n            : Math.max(0, gateLevel - closing);\n      } else {\n        quiet = 0;\n        gateLevel = Math.min(1, gateLevel + opening);\n      }\n\n      const level = ducked * gateLevel * faded;\n      output[0][index] = l * level;\n      output[1][index] = r * level;\n\n      written = written + 1 >= heldSize ? 0 : written + 1;\n      lineWrite = lineWrite + 1 >= lineSize ? 0 : lineWrite + 1;\n    }\n\n    // Rotations drift from the unit circle by a rounding each turn; bring\n    // them back once a block.\n    for (let i = 0; i < LINES; i += 1) {\n      const radius = Math.sqrt(cos[i] * cos[i] + sin[i] * sin[i]);\n      cos[i] /= radius;\n      sin[i] /= radius;\n    }\n    this.written = written;\n    this.lineWrite = lineWrite;\n    this.envelope = envelope;\n    this.heard = heard;\n    this.gateLevel = gateLevel;\n    this.quiet = quiet;\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.\nfunction read(line, write, behind, size) {\n  let at = write - behind;\n  if (at < 0) at += size;\n  // A hair behind the start, wrapped, can round to the size itself.\n  if (at >= size) at -= size;\n  const whole = Math.floor(at);\n  const next = whole + 1 >= size ? 0 : whole + 1;\n  return line[whole] + (line[next] - line[whole]) * (at - whole);\n}\n\n// What a line in the loop holds `behind` samples before `write`: a whole\n// number of samples back, and the rest of the way by a first-order allpass.\n// A straight line between two samples is a gentle low-pass, and in a loop it\n// is applied every time round, so a frozen room would lose its top and then\n// the rest; an allpass delays by the fraction and changes no frequency's\n// level. Its fraction is kept between a half and one and a half, which keeps\n// its pole well away from the top of the spectrum.\nfunction passAt(line, write, behind, size, previous) {\n  const whole = Math.floor(behind - 0.5);\n  const fraction = behind - whole;\n  const eta = (1 - fraction) / (1 + fraction);\n  let a = write - whole;\n  if (a < 0) a += size;\n  const b = a === 0 ? size - 1 : a - 1;\n  return eta * line[a] + line[b] - eta * previous;\n}\n\n// The eight-point Hadamard transform of `x` into `y`, scaled to keep energy.\nfunction hadamard(x, y, scale) {\n  const a0 = x[0] + x[1];\n  const a1 = x[0] - x[1];\n  const a2 = x[2] + x[3];\n  const a3 = x[2] - x[3];\n  const a4 = x[4] + x[5];\n  const a5 = x[4] - x[5];\n  const a6 = x[6] + x[7];\n  const a7 = x[6] - x[7];\n  const b0 = a0 + a2;\n  const b1 = a1 + a3;\n  const b2 = a0 - a2;\n  const b3 = a1 - a3;\n  const b4 = a4 + a6;\n  const b5 = a5 + a7;\n  const b6 = a4 - a6;\n  const b7 = a5 - a7;\n  y[0] = (b0 + b4) * scale;\n  y[1] = (b1 + b5) * scale;\n  y[2] = (b2 + b6) * scale;\n  y[3] = (b3 + b7) * scale;\n  y[4] = (b0 - b4) * scale;\n  y[5] = (b1 - b5) * scale;\n  y[6] = (b2 - b6) * scale;\n  y[7] = (b3 - b7) * scale;\n}\n",
        "tail": true
      }
    },
    {
      "id": "lowcut",
      "kind": "biquadFilter",
      "options": {
        "type": "highpass"
      }
    },
    {
      "id": "highcut",
      "kind": "biquadFilter",
      "options": {
        "type": "lowpass"
      }
    },
    {
      "id": "wet",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "out",
      "kind": "moduleOutput",
      "options": {
        "output": "out"
      }
    },
    {
      "id": "wetin",
      "kind": "moduleInput",
      "options": {
        "input": "wet"
      }
    },
    {
      "id": "wetdepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "freezein",
      "kind": "moduleInput",
      "options": {
        "input": "freeze"
      }
    },
    {
      "id": "freezedepth",
      "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": "room",
        "input": 0
      }
    },
    {
      "from": {
        "node": "room",
        "output": 0
      },
      "to": {
        "node": "lowcut",
        "input": 0
      }
    },
    {
      "from": {
        "node": "lowcut",
        "output": 0
      },
      "to": {
        "node": "highcut",
        "input": 0
      }
    },
    {
      "from": {
        "node": "highcut",
        "output": 0
      },
      "to": {
        "node": "wet",
        "input": 0
      }
    },
    {
      "from": {
        "node": "wet",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "wetin",
        "output": 0
      },
      "to": {
        "node": "wetdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "wetdepth",
        "output": 0
      },
      "to": {
        "node": "wet",
        "param": "gain"
      }
    },
    {
      "from": {
        "node": "freezein",
        "output": 0
      },
      "to": {
        "node": "freezedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "freezedepth",
        "output": 0
      },
      "to": {
        "node": "room",
        "param": "freeze"
      }
    }
  ],
  "automation": [],
  "parameters": [
    {
      "id": "space",
      "label": "Space",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "room",
          "param": "space"
        }
      ]
    },
    {
      "id": "decay",
      "label": "Decay rate",
      "range": {
        "min": "0.25",
        "max": "4"
      },
      "default": "1",
      "targets": [
        {
          "node": "room",
          "param": "decay"
        }
      ],
      "scale": "log"
    },
    {
      "id": "predelay",
      "label": "Pre-delay (s)",
      "range": {
        "min": "0",
        "max": "0.5"
      },
      "default": "0.02",
      "targets": [
        {
          "node": "room",
          "param": "predelay"
        }
      ]
    },
    {
      "id": "style",
      "label": "Style",
      "values": [
        "modern",
        "vintage",
        "plate"
      ],
      "default": "modern",
      "targets": [
        {
          "node": "room",
          "param": "style"
        }
      ]
    },
    {
      "id": "character",
      "label": "Character",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.3",
      "targets": [
        {
          "node": "room",
          "param": "character"
        }
      ]
    },
    {
      "id": "brightness",
      "label": "Brightness",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "room",
          "param": "brightness"
        }
      ]
    },
    {
      "id": "distance",
      "label": "Distance",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.3",
      "targets": [
        {
          "node": "room",
          "param": "distance"
        }
      ]
    },
    {
      "id": "thickness",
      "label": "Thickness",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "room",
          "param": "thickness"
        }
      ]
    },
    {
      "id": "width",
      "label": "Stereo width",
      "range": {
        "min": "0",
        "max": "1.5"
      },
      "default": "1",
      "targets": [
        {
          "node": "room",
          "param": "width"
        }
      ]
    },
    {
      "id": "lowDecay",
      "label": "Low decay",
      "range": {
        "min": "0.25",
        "max": "4"
      },
      "default": "1",
      "targets": [
        {
          "node": "room",
          "param": "lowDecay"
        }
      ],
      "scale": "log"
    },
    {
      "id": "lowFreq",
      "label": "Low decay below (Hz)",
      "range": {
        "min": "20",
        "max": "2000"
      },
      "default": "250",
      "targets": [
        {
          "node": "room",
          "param": "lowFreq"
        }
      ],
      "scale": "log"
    },
    {
      "id": "highDecay",
      "label": "High decay",
      "range": {
        "min": "0.25",
        "max": "4"
      },
      "default": "1",
      "targets": [
        {
          "node": "room",
          "param": "highDecay"
        }
      ],
      "scale": "log"
    },
    {
      "id": "highFreq",
      "label": "High decay above (Hz)",
      "range": {
        "min": "500",
        "max": "16000"
      },
      "default": "4000",
      "targets": [
        {
          "node": "room",
          "param": "highFreq"
        }
      ],
      "scale": "log"
    },
    {
      "id": "lowCut",
      "label": "Low cut (Hz)",
      "range": {
        "min": "10",
        "max": "1000"
      },
      "default": "10",
      "targets": [
        {
          "node": "lowcut",
          "param": "frequency"
        }
      ],
      "scale": "log"
    },
    {
      "id": "highCut",
      "label": "High cut (Hz)",
      "range": {
        "min": "1000",
        "max": "20000"
      },
      "default": "20000",
      "targets": [
        {
          "node": "highcut",
          "param": "frequency"
        }
      ],
      "scale": "log"
    },
    {
      "id": "freeze",
      "label": "Freeze",
      "values": [
        "off",
        "on"
      ],
      "default": "off",
      "targets": [
        {
          "node": "room",
          "param": "freeze"
        }
      ]
    },
    {
      "id": "freezeAmount",
      "label": "Freeze amount",
      "range": {
        "min": "-1",
        "max": "1"
      },
      "default": "1",
      "targets": [
        {
          "node": "freezedepth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "ducking",
      "label": "Ducking (dB)",
      "range": {
        "min": "0",
        "max": "30"
      },
      "default": "0",
      "targets": [
        {
          "node": "room",
          "param": "ducking"
        }
      ]
    },
    {
      "id": "gate",
      "label": "Auto gate",
      "values": [
        "off",
        "on"
      ],
      "default": "off",
      "targets": [
        {
          "node": "room",
          "param": "gate"
        }
      ]
    },
    {
      "id": "gateHold",
      "label": "Gate hold (s)",
      "range": {
        "min": "0",
        "max": "2"
      },
      "default": "0.2",
      "targets": [
        {
          "node": "room",
          "param": "gateHold"
        }
      ]
    },
    {
      "id": "dry",
      "label": "Dry",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "1",
      "targets": [
        {
          "node": "dry",
          "param": "gain"
        }
      ]
    },
    {
      "id": "wet",
      "label": "Wet",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0.4",
      "targets": [
        {
          "node": "wet",
          "param": "gain"
        }
      ]
    },
    {
      "id": "wetAmount",
      "label": "Wet amount",
      "range": {
        "min": "-1",
        "max": "1"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "wetdepth",
          "param": "gain"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-10-08T00:00:00.000Z"
}