{
  "$type": "at.atmosynth.module",
  "descriptionVersion": 1,
  "moduleId": "pulse",
  "versionKey": "4",
  "name": "Pulse oscillator",
  "description": "A square wave whose two halves need not be equal. Width is how much of each cycle is spent high: at a half it is the ordinary square, and away from a half it thins towards a reedy, hollow tone. Width is an input as well as a knob, and moving it with a slow wave is pulse width modulation — the shimmer a single oscillator makes when it sounds like more than one, and the thing the Juno-60 is built on. Detune works as it does on the other oscillators: the knob and what is patched into it can each move the pitch four octaves. This module carries code: the vocabulary's own square is fixed at half and its options cannot reach a width.",
  "audioInputs": [
    {
      "id": "width",
      "label": "Width"
    },
    {
      "id": "detune",
      "label": "Detune"
    }
  ],
  "audioOutputs": [
    {
      "id": "out",
      "label": "Out"
    }
  ],
  "midiInputs": [
    {
      "id": "midi",
      "label": "Note"
    }
  ],
  "nodes": [
    {
      "id": "note",
      "kind": "midiInput",
      "options": {
        "midiInput": "midi"
      }
    },
    {
      "id": "gen",
      "kind": "dsp",
      "options": {
        "code": "// A pulse whose width can be moved, which the node vocabulary has no way to\n// make: its square is fixed at half and its options cannot reach a duty cycle.\n// The edges are softened with PolyBLEP, so a pulse played high does not alias\n// into a chord of frequencies nobody asked for.\n//\n// Every version of the pulse plays this one program. Versions 1 to 3 turned\n// Detune into a ratio through `Math.pow` on every sample, although it almost\n// never moves; version 4 worked it out again only when the value in cents did,\n// and read `sampleRate` once a call rather than once a sample (in a browser's\n// audio thread it is an accessor on the global scope, a call out of the script\n// for each read). Every sample came out the number it did, so all four carry\n// version 4's way of working it out.\nimport { AtmoProcessor } from \"atmosynth:dsp\";\n\nfunction polyBlep(t, dt) {\n  if (t < dt) {\n    const x = t / dt;\n    return x + x - x * x - 1;\n  }\n  if (t > 1 - dt) {\n    const x = (t - 1) / dt;\n    return x * x + x + x + 1;\n  }\n  return 0;\n}\n\nexport default class AtmosynthPulse extends AtmoProcessor {\n  static spec = 1;\n  static inputs = [];\n  static outputs = [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    frequency: { min: 0, max: 20000, default: 0, rate: \"a\", smooth: 0 },\n    detune: { min: -4800, max: 4800, default: 0, rate: \"a\", smooth: 0 },\n    // Never fully open or fully closed: at either the pulse is a flat line.\n    width: { min: 0.02, max: 0.98, default: 0.5, rate: \"a\", smooth: 0 },\n  };\n\n  constructor() {\n    super();\n    this.phase = 0;\n    // The detune the ratio below was last worked out for.\n    this.cents = 0;\n    this.ratio = 1;\n  }\n\n  process(inputs, outputs, params) {\n    const channel = outputs[0][0];\n    const frequency = params.frequency;\n    const detune = params.detune;\n    const width = params.width;\n    let phase = this.phase;\n    const rate = sampleRate;\n\n    for (let index = 0; index < channel.length; index += 1) {\n      const cents = detune[index];\n      if (cents !== this.cents) {\n        this.cents = cents;\n        this.ratio = Math.pow(2, cents / 1200);\n      }\n      const hz = frequency[index] * this.ratio;\n      const high = width[index];\n      const step = hz / rate;\n\n      let value = phase < high ? 1 : -1;\n      value += polyBlep(phase, step);\n      value -= polyBlep((phase + 1 - high) % 1, step);\n      channel[index] = value;\n\n      phase += step;\n      if (phase >= 1) phase -= 1;\n    }\n    this.phase = phase;\n    return true;\n  }\n}\n"
      }
    },
    {
      "id": "widthin",
      "kind": "moduleInput",
      "options": {
        "input": "width"
      }
    },
    {
      "id": "widthdepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "detunein",
      "kind": "moduleInput",
      "options": {
        "input": "detune"
      }
    },
    {
      "id": "detunedepth",
      "kind": "gain",
      "options": {}
    },
    {
      "id": "out",
      "kind": "moduleOutput",
      "options": {
        "output": "out"
      }
    }
  ],
  "connections": [
    {
      "from": {
        "node": "note",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "frequency"
      }
    },
    {
      "from": {
        "node": "gen",
        "output": 0
      },
      "to": {
        "node": "out",
        "input": 0
      }
    },
    {
      "from": {
        "node": "widthin",
        "output": 0
      },
      "to": {
        "node": "widthdepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "widthdepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "width"
      }
    },
    {
      "from": {
        "node": "detunein",
        "output": 0
      },
      "to": {
        "node": "detunedepth",
        "input": 0
      }
    },
    {
      "from": {
        "node": "detunedepth",
        "output": 0
      },
      "to": {
        "node": "gen",
        "param": "detune"
      }
    }
  ],
  "automation": [],
  "parameters": [
    {
      "id": "width",
      "label": "Width",
      "range": {
        "min": "0.02",
        "max": "0.98"
      },
      "default": "0.5",
      "targets": [
        {
          "node": "gen",
          "param": "width"
        }
      ]
    },
    {
      "id": "widthAmount",
      "label": "Width amount",
      "range": {
        "min": "0",
        "max": "0.48"
      },
      "default": "0.3",
      "targets": [
        {
          "node": "widthdepth",
          "param": "gain"
        }
      ]
    },
    {
      "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-09-30T00:00:00.000Z"
}