template-manager.js — Template System

Templates constrain which parameters are randomised. A template declares a set of locked params (pinned values that won't change) and a distortion configuration — everything else is free for randomisation.

Template Format

{
  "name": "Sine Kick",
  "description": "Clean sine with subtle shaping",
  "params": {
    "pitchStart": 220,
    "pitchCurve": "linear"
  },
  "locked": {
    "params": {
      "oscWaveform": "sine",
      "compThreshold": 0
    },
    "distortion": {
      "masterLocked": true,
      "stages": []
    }
  }
}
  • params (optional): Values set but not locked — used by the "None" template to reset everything to defaults without constraints
  • locked.params: Values set and locked — these won't be randomised
  • locked.distortion: Master lock status and stage definition

Functions

captureTemplate(name, params, lockedParams, pipeline) → template

Creates a template from the current lock state. Only params where lockedParams[key] === true are captured (with their current values). Captures pipeline.masterLocked and pipeline.toJSON() for the distortion config.

applyTemplate(tmpl, params, lockedParams, pipeline)

  1. Unlocks everything: all lockedParams set to false, pipeline locks cleared
  2. Sets base params: tmpl.params values copied (not locked)
  3. Sets locked params: tmpl.locked.params values copied and locked
  4. Configures distortion: masterLocked, stages replaced
  5. Updates activeTemplate: getActiveTemplate() returns this template

loadBuiltinTemplates(engineId) → [template]

Fetches presets/templates/{engineId}.json from the server. Returns an array with _builtin: true set on each entry.

loadUserTemplates / saveUserTemplate / deleteUserTemplate

CRUD for user-created templates stored in localStorage[synth_templates_{id}].

downloadTemplate(tmpl)

Triggers a JSON file download of the template.

buildTemplateListUI(container, templates, activeName, onSelect, onDelete)

Renders the template list in the sidebar. Built-in templates show name + description. User templates additionally show a delete ✕ button with "Sure?" confirmation. Clicking a template calls onSelect(tmpl).