diff --git a/src/main.js b/src/main.js index 3356ed0..4783ed7 100644 --- a/src/main.js +++ b/src/main.js @@ -207,7 +207,9 @@ import { return songKey ? { songKey, ...extra } : { ...extra }; } - // ── Teardown ── + /** + * Cleans up the stem mixer, stops playback, and restores core audio control. + */ function teardown() { // Defensive reset: a new song load starting mid-takeover (or after a // crash before the flag was cleared) must not leave togglePlay() @@ -772,7 +774,15 @@ import { } } - // ── Main entry: called after song_info arrives ── + /** + * Initializes stem playback for the loaded song. + * + * Loads audio stems, selects a playback strategy (streaming for WAV audio, + * full decode for OGG), and builds the audio graph for stem mixing. If any + * step fails, playback degrades to native HTML5 audio. For songs with no stems + * or on unavailable hardware, emits a provider-ready event without building + * the stem graph. + */ async function onSongReady() { console.log('[stems debug] onSongReady called, gen was:', S.loadGeneration); teardown(); @@ -959,6 +969,11 @@ import { else window._stemsRerouteInProgress = false; } + /** + * Generates a deterministic signature for the current song and its stem configuration. + * @param {Object} info - Optional song metadata containing stems and filename. + * @return {string} A signature string that uniquely identifies the song state. + */ function songInfoSignature(info) { const stems = Array.isArray(info && info.stems) ? info.stems : []; const filename = (info && info.filename) || (window.slopsmith && window.slopsmith.currentSong && window.slopsmith.currentSong.filename) || S.currentFilename || ''; diff --git a/src/streaming.js b/src/streaming.js index 6497d4a..a04ab20 100644 --- a/src/streaming.js +++ b/src/streaming.js @@ -286,7 +286,15 @@ export async function repositionStream(targetSec) { // runs asynchronously), false on failure. On failure it does NOT teardown() // (that would bump S.loadGeneration and hide the failure from onSongReady's // supersession check) — the caller tears down + falls back. A `false` return -// with `gen === S.loadGeneration` is a real failure; a stale gen is supersession. +/** + * Set up bounded-memory streaming playback for the provided WAV stems. + * @param {Array} stems - Stem descriptors containing stream URLs and metadata. + * @param {Response} probeResp - Response containing the first stem. + * @param {string|null} fullUrl - Optional URL for a full-mix WAV stream. + * @param {number} gen - Load generation used to discard superseded setup operations. + * @param {AbortSignal} [signal=S.abortController.signal] - Signal used to cancel stream requests. + * @return {Promise} `true` when streaming playback is initialized, `false` when setup fails or is superseded. + */ export async function setupStreaming(stems, probeResp, fullUrl, gen, signal = S.abortController.signal) { // 1. Open readers + parse headers for every stem (and the full mix). let restResps = []; diff --git a/src/transport.js b/src/transport.js index 5762a86..dc6430d 100644 --- a/src/transport.js +++ b/src/transport.js @@ -169,6 +169,10 @@ export function flushPendingPlayResolvers() { } } +/** + * Starts playback from the current position and dispatches a `play` event when playback begins. + * @returns {void} + */ export function transportPlay() { resumeCtx(); if (!S.buffersReady) { S.pendingPlay = true; return; }