<!-- SS · part 14 -->
x-box').hidden = true; });

  // ---------------------------------------------------------------- AUDIO ENGINE
  var E = null; // built lazily after Tone loads
  var SOUNDS = { drums: ['808', 'Lo-fi', 'Chip'], bass: ['Sub', 'Saw', 'FM'], chords: ['Pad', 'Keys', 'Pluck'], lead: ['Saw', 'Square', 'Bell'] };
  function makeDrums(kit, out) {
    var d = {}, nodes = [];
    function keep(n) { nodes.push(n); return n; }
    if (kit === 'Lo-fi') {
      var lp = keep(new Tone.Filter(3200, 'lowpass').connect(out));
      d.kick = keep(new Tone.MembraneSynth({ pitchDecay: 0.03, octaves: 6, envelope: { attack: 0.001, decay: 0.35, sustain: 0, release: 0.1 } }).connect(lp));
      d.snare = keep(new Tone.NoiseSynth({ noise: { type: 'pink' }, envelope: { attack: 0.001, decay: 0.18, sustain: 0 } }).connect(lp));
      var hp = keep(new Tone.Filter(6000, 'highpass').connect(lp));
      d.hat = keep(new Tone.NoiseSynth({ noise: { type: 'white' }, envelope: { attack: 0.001, decay: 0.04, sustain: 0 }, volume: -10 }).connect(hp));
      d.kickNote = 'C1';
    } else if (kit === 'Chip') {
      d.kick = keep(new Tone.MembraneSynth({ oscillator: { type: 'square' }, pitchDecay: 0.02, octaves: 4, envelope: { attack: 0.001, decay: 0.18, sustain: 0 }, volume: -8 }).connect(out));
      d.snare = keep(new Tone.NoiseSynth({ noise: { type: 'brown' }, envelope: { attack: 0.001, decay: 0.09, sustain: 0 }, volume: 2 }).connect(out));
      var hp2 = keep(new Tone.Filter(8000, 'highpass').connect(out));
      d.hat = keep(new Tone.NoiseSynth({ noise: { type: 'white' }, envelope: { attack: 0.001, decay: 0.02, sustain: 0 }, volume: -6 }).connect(hp2));
      d.kickNote = 'C2';
    } else { // 808
      d.kick = keep(new Tone.MembraneSynth({ pitchDecay: 0.06, octaves: 8, envelope: { attack: 0.001, decay: 0.6, sustain: 0.01, release: 0.4 } }).connect(out));
      var sf = keep(new Tone.Filter(1800, 'bandpass').connect(out));
      d.snare = keep(new Tone.NoiseSynth({ noise: { type: 'white' }, envelope: { attack: 0.001, decay: 0.2, sustain: 0 }, volume: 6 }).connect(sf));
      var hp3 = keep(new Tone.Filter(7500, 'highpass').connect(out));
      d.hat = keep(new Tone.NoiseSynth({ noise: { type: 'white' }, envelope: { attack: 0.001, decay: 0.05, sustain: 0 }, volume: -4 }).connect(hp3));
      d.kickNote = 'A0';
    }
    d.dispose = function () { nodes.forEach(function (n) { try { n.dispose(); } catch (e) {} }); };
    d.play = function (row, time, vel) {
      if (row === 'kick') d.kick.triggerAttackRelease(d.kickNote, '8n', time, vel);
      else d[row].triggerAttackRelease(row === 'hat' ? '32n' : '16n', time, vel);
    };
    return d;
  }
  function makeSynth(track, kind, out) {
    var s;
    if (track === 'bass') {
      if (kind === 'Saw') s = new Tone.MonoSynth({ oscillator: { type: 'sawtooth' }, filter: { Q: 2, type: 'lowpass' }, envelope: { attack: 0.005, decay: 0.2, sustain: 0.5, release: 0.2 }, filterEnvelope: { attack: 0.001, decay: 0.15, sustain: 0.2, baseFrequency: 120, octaves: 3 } });
      else if (kind === 'FM') s = new Tone.FMSynth({ harmonicity: 1, modulationIndex: 6, envelope: { attack: 0.005, decay: 0.3, sustain: 0.4, release: 0.2 } });
      else s = new Tone.Synth({ oscillator: { type: 'sine' }, envelope: { attack: 0.005, decay: 0.4, sustain: 0.6, release: 0.3 }, volume: 4 });
    } else if (track === 'chords') {
      if (kind === 'Keys') s = new Tone.PolySynth(Tone.FMSynth, { harmonicity: 3, modulationIndex: 2, envelope: { attack: 0.005, decay: 0.8, sustain: 0.2, release: 0.8 } });
      else if (kind === 'Pluck') s = new Tone.PolySynth(Tone.Synth, { oscillator: { type: 'triangle' }, envelope: { attack: 0.002, decay: 0.25, sustain: 0, release: 0.2 } });
      else s = new Tone.PolySynth(Tone.Synth, { oscillator: { type: 'fatsawtooth', count: 3, spread: 20 }, envelope: { attack: 0.3, decay: 0.4, sustain: 0.7, release: 1.2 }, volume: -6 });
      s.maxPolyphony = 16;
    } else {
      if (kind === 'Square') s = new Tone.Synth({ oscillator: { type: 'square' }, envelope: { attack: 0.005, decay: 0.15, sustain: 0.3, release: 0.15 }, volume: -6 });
      else if (kind === 'Bell') s = new Tone.FMSynth({ harmonicity: 3.01, modulationIndex: 12, envelope: { attack: 0.001, decay: 1.2, sustain: 0, release: 1 }, modulationEnvelope: { attack: 0.001, decay: 0.6, sustain: 0, release: 0.5 } });
      else s = new Tone.MonoSynth({ oscillator: { type: 'sawtooth' }, filter: { Q: 1, type: 'lowpass' }, envelope: { attack: 0.01, decay: 0.2, sustain: 0.4, release: 0.25 }, filterEnvelope: { attack: 0.01, decay: 0.2, sustain: 0.4, baseFrequency: 600, octaves: 2.5 }, volume: -4 });
    }
    s.connect(out);
    return s;
  }
  function buildEngine() {
    if (E) return E;
    if (!window.Tone) { toast('Audio engine failed to load. Reload the page.'); return null; }
    var T = Tone.getTransport();
    E = { T: T, ch: {}, inst: {}, step: 0, bar: 0, players: [], voxBus: null };
    E.master = new Tone.Gain(0.9).toDestin