5. Manual (engine.js) vs. Autoplay (engineAuto.js)

Capability engine.js (Manual) engineAuto.js (Autoplay)
Dummy Stage (?level=X) Renders stage silently until user clicks ▶ Play. Calls soundtrack.autoPlayLevel(idx) automatically on page load.
Custom Page Attribute Not active. Supports <body data-auto-level="0"> on any standalone page (such as template.html or game.html).
Browser Autoplay Policy Unlocks audio synchronously on button/link click. Attempts immediate playback; if AudioContext is suspended, queues track and starts on first pointerdown, keydown, or touchstart.
Hidden Diagnostic Bar Supported in both engines via Alt + D or ?debug=1.
Switching in index.html Use the Mode toggle switch in the navigation bar (persisted in localStorage) or pass ?engine=manual / ?engine=auto in the URL.

6. Core Engine Function & Method Reference (engine.js & engineAuto.js)

setStatus(msg: string): void & playWavBeep(freq = 440, durationSec = 0.4): void
Standalone Helpers  |  Parameters: msg (string), freq (Hz), durationSec (seconds) | Returns: void

setStatus(msg): Updates #audioStatus inside the hidden diagnostic bar (Alt + D / ?debug=1) whenever present on the page.

playWavBeep(freq, durationSec): Allocates a binary ArrayBuffer + DataView, writes a 44-byte mono 8-bit RIFF/WAVE header at 22050Hz, synthesizes a decaying square wave, and plays it via an HTML5 Audio Blob URL as a speaker fallback.

constructor(), unlockSync(): boolean & playTestBeep(): void
ChiptuneEngine Initialization & Hardware Wake-Up

constructor(): Initializes this.ctx (null), this.isPlaying (false), this.timer (null), this.activeNodes ([]), and this.pendingLevelIndex (null, in engineAuto.js).

unlockSync(): Lazily creates or resumes AudioContext inside a user gesture and plays a 1-sample silent buffer (createBuffer(1, 1, 22050)) to wake up mobile/OS audio daemons; returns true on success.

playTestBeep(): Calls unlockSync() and plays a 440Hz sine wave decaying from gain 0.4 to 0.001 over 0.45 seconds.

autoPlayLevel(index: number): void engineAuto.js ONLY
Parameters: index (number, 0–18) | Returns: void
Calls playLevel(index) immediately if this.ctx.state === 'running'. Otherwise stores index in this.pendingLevelIndex, updates setStatus(), and attaches a one-time resumePending() listener to pointerdown, keydown, and touchstart to launch playback on the user's first interaction.
playLevel(index: number): void & scheduleVoice(notes, waveType, volume, beatDur, startTime): number
ChiptuneEngine Two-Voice Sequencer & Oscillator Scheduler

playLevel(index): Calls stop() and unlockSync(), computes beatDuration = 60 / level.tempo, and runs a recursive loopTrack() timer with a 50ms lookahead buffer scheduling both melody (gain 0.22) and bass (gain 0.28).

scheduleVoice(...): Resolves frequencies from N (skipping '_' rests), schedules OscillatorNode + GainNode pairs with an 88% staccato decay envelope, pushes nodes to this.activeNodes, and returns total voice duration in seconds.

playSFX(type: string): number & stop(): void
Parameters: type (key in SFX_DATA) | Returns: redirectDelayMs (number, ms) / void

playSFX(type): Looks up SFX_DATA[type], calls stop() if sfx.stopMusic is true, synthesizes the effect using 'sweep', 'burst', 'arpeggio', or 'sequence' mode, and returns sfx.redirectDelayMs || 300 for delayed page links.

stop(): Sets this.isPlaying = false, clears this.pendingLevelIndex and clearTimeout(this.timer), stops every oscillator in this.activeNodes, and removes .active button highlights.

7. Quick Usage on New Pages (or Duplicate template.html)

Manual Playback + Link-Click SFX (engine.js):

<link id="themeStylesheet" rel="stylesheet" href="styles.css">
<script src="theme.js"></script>

<a href="about.html">About (Default Warp Pipe SFX)</a>
<a href="shop.html" data-sfx="coin">Shop (Coin SFX)</a>

<script src="sounds.js"></script>
<script src="engine.js"></script>

Automatic Track Playback on Load + Link-Click SFX (engineAuto.js):

<body data-auto-level="18">
  <a href="index.html" data-sfx="pipe">← Back to Hub</a>

  <script src="sounds.js"></script>
  <script src="engineAuto.js"></script>
</body>