{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "osc-analog",
  "versionKey": "1",
  "name": "Analog oscillator",
  "description": "The oscillator of an analog monosynth as one module: a wave that travels from a triangle to a narrow pulse, a sub oscillator under it, and the two things that make a hardware oscillator sound unlike a digital one — it does not start each note where the last one started, and its pitch never quite sits still.\n\nWave travels triangle, sawtooth, square at nothing to two, each two neighbours mixed sample for sample between, so turning it is a sound changing shape rather than being swapped. From two to three the square narrows instead, from half of each cycle to a tenth: thinner, more nasal, and quieter as it goes. Wave is an input as well, and Wave amount is how far a signal there moves it — an envelope on it brightens the start of a note, and an LFO on it past two is pulse width modulation.\n\nThe sub is a square one octave below, or two, and not an oscillator of its own: it changes sides each time the wave comes round, or every second time, so it cannot drift against it. Sub level mixes it into Out. Sub is an output too, carrying the sub alone at full level whatever Sub level says, so it can have a filter of its own.\n\nPhase is where a note starts its wave. At free each note starts somewhere else in its cycle, as an analog oscillator does, since it never stopped running between notes: no two attacks are quite alike, and two of these never meet the same way twice. At reset every note starts where the other oscillators here start theirs, the triangle and sawtooth at nothing on their way up and the square at the start of its high half, so every attack is the same one — what a tight bass or a pluck wants.\n\nDrift is how far the pitch wanders, in cents: slowly, a new direction every third of a second or so, glided to rather than stepped. It is what makes two analog oscillators beat against each other and a chord of them breathe. At nothing the pitch is the note's.\n\nSync is an input: whatever is patched there starts the wave over on its rising zero crossings, the rule the sync oscillator's Sync input answers to, and the pitch heard becomes that while Detune moves the timbre. While something syncs it the sub divides that instead, so it stays under the pitch heard. An empty Sync input does nothing.\n\nDetune works as on the other oscillators: the knob reaches four octaves either side of the note, so 1200 is an octave switch, and what is patched into Detune moves it as far again by Detune amount.\n\nThis module carries code because the vocabulary's oscillator has no phase anything can reach: nothing can divide its cycles into a sub, tell it where to start or start it over. Every edge and corner is band-limited where it falls between two samples, so the top of the keyboard stays a sound rather than folding into tones at no harmonic of anything.",
  "audioInputs": [
    {
      "id": "wave",
      "label": "Wave"
    },
    {
      "id": "sync",
      "label": "Sync"
    },
    {
      "id": "detune",
      "label": "Detune"
    }
  ],
  "audioOutputs": [
    {
      "id": "out",
      "label": "Out"
    },
    {
      "id": "sub",
      "label": "Sub"
    }
  ],
  "midiInputs": [
    {
      "id": "midi",
      "label": "Note"
    }
  ],
  "nodes": [
    {
      "id": "note",
      "kind": "midiInput",
      "options": {
        "midiInput": "midi"
      }
    },
    {
      "id": "gen",
      "kind": "dsp",
      "options": {
        "code": "// The oscillator of an analog monosynth, as one module: a wave that morphs\n// from a triangle through a sawtooth and a square to a narrow pulse, a sub\n// oscillator locked to it, a phase that does not start where the last note's\n// did, and a pitch that never quite sits still. The vocabulary's own\n// `oscillator` reaches none of the four. It has no phase anything can read,\n// so nothing can count its cycles to divide them into a sub; nothing can tell\n// it where to start, and every voice built for a note starts it at nothing;\n// nothing can start it over, so it cannot be synced; and its waves are four\n// fixed shapes, so a knob between them is a crossfade of four oscillators\n// that only sound like one while they stay in step.\n//\n// **Wave** travels triangle, sawtooth, square at nought to two, each pair\n// mixed sample for sample between, and from two to three the square narrows\n// from a half of the cycle to a tenth of it rather than fading into anything.\n// A tenth because the pulse loses its fundamental fast below it — its level\n// falls with the sine of the width times pi, a third of the square's at a\n// tenth and a sixth at a twentieth — and because its two edges must stay\n// further apart than the two samples each one's correction spans, which at a\n// tenth they do up to C7 and at a twentieth only up to C6. A pulse spends more\n// of its cycle low than high, so it sits off centre by that much; the offset\n// is taken off as the width moves, rather than by a filter after it, so a\n// narrow pulse starts a note centred instead of thumping into place.\n//\n// Every wave starts where the platform's other oscillators start theirs: the\n// triangle and the sawtooth at nothing on their way up, the square at the\n// start of its high half. So **Phase** at reset starts every note exactly as\n// an `oscillator` would, and free starts each one somewhere else in its\n// cycle, drawn from the module's generator — which is what an analog\n// oscillator does, since it never stopped running between notes. Two notes in\n// a row, or two of these detuned against each other, then never start their\n// attack the same way twice.\n//\n// **The sub** is a square one octave or two below, and is the main phase\n// divided rather than an oscillator of its own: it changes sides every time\n// the main phase comes round, or every second time, so it cannot drift\n// against it. It has an output of its own, at full level whatever the mix is,\n// and the module mixes it into Out through a gain outside this code.\n//\n// **Drift** is a slow wander of the pitch, in cents: a new target from the\n// dice every three tenths of a second, approached through a one-pole with the\n// same time constant, worked out every 32 samples rather than every one,\n// since nothing that slow needs more. It is the instability that makes two\n// analog oscillators beat against each other, and at nothing it is gone.\n//\n// **Sync** takes the phase back to its start on the rising zero crossings of\n// whatever is patched there, as the sync oscillator's Sync input does, a\n// sample late and never a fraction of one jittery. While it is driving, the\n// pitch heard is the thing driving it, so that is what the sub divides; when\n// it lets go, the sub divides the main phase's own turns again.\n//\n// Every discontinuity is corrected where it falls between two samples: a\n// polyBLEP scaled by however far the wave jumped for the sawtooth's fall, the\n// pulse's two edges, the sub's edges and a reset, and that residual\n// integrated once — a polyBLAMP — for the triangle's corners. The output\n// leaves through a one-pole at 5 Hz, as the sync oscillator's does and for\n// its reason: a wave cut off mid-cycle no longer sits either side of nothing.\nimport { AtmoProcessor, Rng } from \"atmosynth:dsp\";\n\n/** The narrowest the pulse gets, as a fraction of the cycle spent high. */\nconst NARROWEST = 0.1;\n/** Samples between two steps of the drift. */\nconst DRIFT_TICK = 32;\n/** Seconds between two of its targets, and its time constant. */\nconst DRIFT_SECONDS = 0.3;\n\n/** No more than a wave a sample, and never backwards; nothing at all where it is not finite. */\nfunction limit(step) {\n  return !(step - step === 0) ? 0 : step > 0.49 ? 0.49 : step < 0 ? 0 : step;\n}\n\nexport default class AtmosynthAnalog extends AtmoProcessor {\n  static spec = 1;\n  static inputs = [];\n  // Out, then the sub on its own.\n  static outputs = [1, 1];\n  // Not smoothed here: a knob's move reaches these already eased, and the\n  // note and the inputs wired in are followed exactly.\n  static params = {\n    // The note, in hertz, off the module's MIDI input.\n    frequency: { min: 0, max: 24000, default: 0, rate: \"a\", smooth: 0 },\n    // The knob and whatever is patched into Detune, together.\n    detune: { min: -9600, max: 9600, default: 0, rate: \"a\", smooth: 0 },\n    // 0 triangle, 1 sawtooth, 2 square, 3 narrow pulse, and every place between.\n    wave: { min: 0, max: 3, default: 1, rate: \"a\", smooth: 0 },\n    // Whatever is patched into Sync. Silence never crosses, so an input with\n    // nothing in it starts nothing over.\n    reset: { min: -1000, max: 1000, default: 0, rate: \"a\", smooth: 0 },\n    // How far the pitch wanders, in cents.\n    drift: { min: 0, max: 15, default: 3, rate: \"a\", smooth: 0 },\n    // 0 one octave down, 1 two.\n    octave: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n    // 0 free, 1 reset. Read once, when the note begins.\n    phase: { min: 0, max: 1, default: 0, rate: \"k\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    // Where a free note starts and the drift's targets. Nothing is drawn\n    // until the first call, which is when the note begins.\n    this.rng = new Rng(this.seed);\n    this.phase = 0;\n    /** The main phase's turns, counted round four: the sub is read off it. */\n    this.count = 0;\n    /** Whether the first block has drawn where this note starts. */\n    this.begun = false;\n    /** The Detune the ratio was last worked out for. */\n    this.cents = 0;\n    this.ratio = 1;\n    /** What the Wave knob was last worked out for (not a number until it has\n     *  been, so the first sample works it out), and what it came to. */\n    this.wave = 0 / 0;\n    this.tri = 0;\n    this.saw = 1;\n    this.pulse = 0;\n    this.width = 0.5;\n    this.offset = 0;\n    this.start = 0;\n    /** The Sync input as it was a sample ago, and samples it still drives for. */\n    this.lastLevel = 0;\n    this.external = 0;\n    /** Band-limiting owed to this sample and the next, for each output. */\n    this.owed = 0;\n    this.carry = 0;\n    this.subOwed = 0;\n    this.subCarry = 0;\n    /** The 5 Hz one-pole on the way out, and the two samples it remembers. */\n    this.pole = 1 - (2 * Math.PI * 5) / sampleRate;\n    this.lastRaw = 0;\n    this.centred = 0;\n    /** The drift: samples to its next step and to its next target, where it is\n     *  heading, where it has got to, and the ratio that comes to. */\n    this.tick = 0;\n    this.every = DRIFT_SECONDS * sampleRate;\n    this.wait = this.every;\n    this.glide = 1 - Math.exp(-DRIFT_TICK / this.every);\n    this.target = 0;\n    this.wander = 0;\n    this.drifted = 1;\n  }\n\n  /** The Wave knob's travel, as weights on three waves and a pulse's width. */\n  shape(position) {\n    const place = position < 0 ? 0 : position > 3 ? 3 : position;\n    this.tri = place < 1 ? 1 - place : 0;\n    this.saw = place < 1 ? place : place < 2 ? 2 - place : 0;\n    this.pulse = place < 1 ? 0 : place < 2 ? place - 1 : 1;\n    this.width = place < 2 ? 0.5 : 0.5 - (place - 2) * (0.5 - NARROWEST);\n    // A pulse high for a fraction w of its cycle sits 2w − 1 off centre.\n    this.offset = this.pulse * (2 * this.width - 1);\n    this.start = this.level(0);\n  }\n\n  /** The wave at a phase, before any band-limiting. */\n  level(phase) {\n    let value = 0;\n    if (this.tri !== 0) {\n      const turned = phase < 0.75 ? phase + 0.25 : phase - 0.75;\n      value = this.tri * (1 - 4 * Math.abs(turned - 0.5));\n    }\n    if (this.saw !== 0) value += this.saw * (phase < 0.5 ? 2 * phase : 2 * phase - 2);\n    if (this.pulse !== 0) value += this.pulse * (phase < this.width ? 1 : -1) - this.offset;\n    return value;\n  }\n\n  /** A band-limited step of `height`, `when` samples after this instant. */\n  jump(height, when) {\n    const half = height / 2;\n    this.owed += half * (1 - when) * (1 - when);\n    this.carry -= half * when * when;\n  }\n\n  /** The same for the sub's own output. */\n  subJump(height, when) {\n    const half = height / 2;\n    this.subOwed += half * (1 - when) * (1 - when);\n    this.subCarry -= half * when * when;\n  }\n\n  /** A band-limited corner: the slope changes by `change` a sample. */\n  bend(change, when) {\n    const sixth = change / 6;\n    this.owed += sixth * (1 - when) * (1 - when) * (1 - when);\n    this.carry += sixth * when * when * when;\n  }\n\n  /** One more turn for the sub to divide, `when` samples after this instant. */\n  turn(mask, when) {\n    const before = (this.count & mask) !== 0 ? -1 : 1;\n    this.count = (this.count + 1) & 3;\n    const after = (this.count & mask) !== 0 ? -1 : 1;\n    if (after !== before) this.subJump(after - before, when);\n  }\n\n  process(inputs, outputs, params) {\n    const channel = outputs[0][0];\n    const below = outputs[1][0];\n    const frequency = params.frequency;\n    const detune = params.detune;\n    const wave = params.wave;\n    const reset = params.reset;\n    const drift = params.drift;\n    const mask = params.octave[0] < 0.5 ? 1 : 2;\n    const rate = sampleRate;\n\n    if (!this.begun) {\n      this.begun = true;\n      if (params.phase[0] < 0.5) {\n        this.phase = this.rng.uniform();\n        this.count = Math.floor(this.rng.uniform() * 4);\n        this.wander = this.rng.bipolar();\n      }\n      this.target = this.rng.bipolar();\n    }\n\n    for (let index = 0; index < channel.length; index += 1) {\n      const position = wave[index];\n      if (!(position === this.wave)) {\n        this.shape(position);\n        this.wave = position;\n      }\n      const cents = detune[index];\n      if (cents !== this.cents) {\n        this.cents = cents;\n        this.ratio = Math.pow(2, cents / 1200);\n      }\n      if (this.tick === 0) {\n        this.tick = DRIFT_TICK;\n        this.wait -= DRIFT_TICK;\n        if (this.wait <= 0) {\n          this.wait += this.every;\n          this.target = this.rng.bipolar();\n        }\n        this.wander += (this.target - this.wander) * this.glide;\n        this.drifted = Math.pow(2, (drift[index] * this.wander) / 1200);\n      }\n      this.tick -= 1;\n      const step = limit((frequency[index] * this.ratio * this.drifted) / rate);\n\n      // The Sync input's rising zero crossings, placed the same fraction into\n      // this sample as they fell into the last.\n      let syncAt = -1;\n      const level = reset[index];\n      if (this.lastLevel < 0 && level >= 0) {\n        syncAt = -this.lastLevel / (level - this.lastLevel);\n        if (syncAt >= 1) syncAt = 0.999999;\n        this.external = rate | 0;\n      }\n      this.lastLevel = level;\n      const driven = this.external > 0;\n      if (driven) this.external -= 1;\n\n      // What the last sample's discontinuities left for this one.\n      this.owed = this.carry;\n      this.carry = 0;\n      this.subOwed = this.subCarry;\n      this.subCarry = 0;\n\n      // Both outputs at this instant, before the interval to the next one is\n      // walked for everything that happens in it.\n      const value = this.level(this.phase);\n      const sub = (this.count & mask) !== 0 ? -1 : 1;\n\n      const tri = this.tri;\n      const saw = this.saw;\n      const pulse = this.pulse;\n      const width = this.width;\n      let phase = this.phase;\n      let where = 0;\n      for (let guard = 0; guard < 8; guard += 1) {\n        let boundary = 1;\n        if (tri !== 0) {\n          if (phase < 0.25) boundary = 0.25;\n          else if (phase < 0.75) boundary = 0.75;\n        }\n        if (saw !== 0 && phase < 0.5 && 0.5 < boundary) boundary = 0.5;\n        if (pulse !== 0 && phase < width && width < boundary) boundary = width;\n        let when = step > 0 ? where + (boundary - phase) / step : 2;\n        if (when < where) when = where;\n\n        if (syncAt >= where && syncAt <= when) {\n          // Back to the start, from wherever the wave had got to, and the\n          // triangle's slope with it where it was on its way down.\n          phase += (syncAt - where) * step;\n          where = syncAt;\n          syncAt = -1;\n          this.jump(this.start - this.level(phase), where);\n          if (tri !== 0 && phase >= 0.25 && phase < 0.75) this.bend(8 * tri * step, where);\n          phase = 0;\n          this.turn(mask, where);\n          continue;\n        }\n        if (when > 1) break;\n\n        where = when;\n        phase = boundary;\n        if (tri !== 0) {\n          if (boundary === 0.25) this.bend(-8 * tri * step, where);\n          else if (boundary === 0.75) this.bend(8 * tri * step, where);\n        }\n        if (saw !== 0 && boundary === 0.5) this.jump(-2 * saw, where);\n        if (pulse !== 0 && boundary === width) this.jump(-2 * pulse, where);\n        if (boundary === 1) {\n          if (pulse !== 0) this.jump(2 * pulse, where);\n          phase = 0;\n          if (!driven) this.turn(mask, where);\n        }\n      }\n      this.phase = phase + (1 - where) * step;\n\n      const raw = value + this.owed;\n      this.centred = raw - this.lastRaw + this.pole * this.centred;\n      this.lastRaw = raw;\n      channel[index] = this.centred;\n      below[index] = sub + this.subOwed;\n    }\n    return true;\n  }\n}\n"
      }
    },
    {
      "id": "wavein",
      "kind": "moduleInput",
      "options": {
        "input": "wave"
      }
    },
    {
      "id": "wavedepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "syncin",
      "kind": "moduleInput",
      "options": {
        "input": "sync"
      }
    },
    {
      "id": "detunein",
      "kind": "moduleInput",
      "options": {
        "input": "detune"
      }
    },
    {
      "id": "detunedepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "sublevel",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "out",
      "kind": "moduleOutput",
      "options": {
        "output": "out"
      }
    },
    {
      "id": "subout",
      "kind": "moduleOutput",
      "options": {
        "output": "sub"
      }
    }
  ],
  "connections": [
    {
      "from": {
        "node": "note",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "frequency"
      }
    },
    {
      "from": {
        "node": "gen",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "gen",
        "output": 1
      },
      "to": {
        "node": "sublevel",
        "input": 0
      }
    },
    {
      "from": {
        "node": "sublevel",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "gen",
        "output": 1
      },
      "to": {
        "node": "subout",
        "input": 0
      }
    },
    {
      "from": {
        "node": "wavein",
        "output": 0
      },
      "to": {
        "node": "wavedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "wavedepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "wave"
      }
    },
    {
      "from": {
        "node": "syncin",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "reset"
      }
    },
    {
      "from": {
        "node": "detunein",
        "output": 0
      },
      "to": {
        "node": "detunedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "detunedepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "detune"
      }
    }
  ],
  "automation": [],
  "parameters": [
    {
      "id": "wave",
      "label": "Wave (0 triangle, 1 saw, 2 square, 3 narrow pulse)",
      "range": {
        "min": "0",
        "max": "3"
      },
      "default": "1",
      "targets": [
        {
          "node": "gen",
          "param": "wave"
        }
      ]
    },
    {
      "id": "waveAmount",
      "label": "Wave amount",
      "range": {
        "min": "0",
        "max": "3"
      },
      "default": "1",
      "targets": [
        {
          "node": "wavedepth",
          "param": "gain"
        }
      ]
    },
    {
      "id": "subLevel",
      "label": "Sub level",
      "range": {
        "min": "0",
        "max": "1"
      },
      "default": "0",
      "targets": [
        {
          "node": "sublevel",
          "param": "gain"
        }
      ]
    },
    {
      "id": "subOctave",
      "label": "Sub octave",
      "values": [
        "1 octave down",
        "2 octaves down"
      ],
      "default": "1 octave down",
      "targets": [
        {
          "node": "gen",
          "param": "octave"
        }
      ]
    },
    {
      "id": "phase",
      "label": "Phase",
      "values": [
        "free",
        "reset"
      ],
      "default": "free",
      "targets": [
        {
          "node": "gen",
          "param": "phase"
        }
      ]
    },
    {
      "id": "drift",
      "label": "Drift (cents)",
      "range": {
        "min": "0",
        "max": "15"
      },
      "default": "3",
      "targets": [
        {
          "node": "gen",
          "param": "drift"
        }
      ]
    },
    {
      "id": "detune",
      "label": "Detune (cents)",
      "range": {
        "min": "-4800",
        "max": "4800"
      },
      "default": "0",
      "targets": [
        {
          "node": "gen",
          "param": "detune"
        }
      ]
    },
    {
      "id": "detuneAmount",
      "label": "Detune amount (cents)",
      "range": {
        "min": "0",
        "max": "4800"
      },
      "default": "50",
      "targets": [
        {
          "node": "detunedepth",
          "param": "gain"
        }
      ]
    }
  ],
  "sampleSlots": [],
  "createdAt": "2026-10-12T00:00:00.000Z"
}