import { Show } from './Show.js';
import { TimelineStrip } from './ui/TimelineStrip.js';
import { ParamPanel } from './ui/ParamPanel.js';
import { formatTime } from './audio/decode.js';
import { describeLook } from './look/LookGenerator.js';
import { toHex } from './look/palette.js';
import { applyGrainToPost, describeGrain, GRAIN_MASKS, GRAIN_MODES } from './look/grain.js';
import { renderClickTrack, audioBufferToWavBlob } from './audio/metronome.js';
import { Exporter, exportSegment, downloadBlob, PRESETS, isSupported, probeExportCodec } from './export/Exporter.js';
import { subjectOf, groundOf, overlaysOf, stackCoverage } from './look/stack.js';
import { coverageOf as sceneCoverage } from './scenes/surface.js';
import { getLlmConfig, setLlmConfig } from './llm/config.js';
import { canInvoke, remainingText, getBudgetCounts } from './llm/budget.js';
import { requestSongTweak } from './llm/llmTweak.js';
import { synthesizeSong } from './audio/synth.js';
import { FeatureTrack } from './audio/FeatureTrack.js';
import { generateLook } from './look/LookGenerator.js';
import { SONGS } from './audio/songbank.js';
import { captureThumbnails } from './llm/thumbs.js';
const QUALITY = {
draft: 0.5, // half resolution — for scrubbing heavy stacks
full: 1.0,
};
const dom = {
canvas: document.getElementById('canvas'),
stage: document.getElementById('stage'),
overlay: document.getElementById('overlay'),
dropzone: document.getElementById('dropzone'),
fileInput: document.getElementById('file-input'),
audio: document.getElementById('audio'),
timelineCanvas: document.getElementById('timeline-canvas'),
timeDisplay: document.getElementById('time-display'),
sectionDisplay: document.getElementById('section-display'),
panelBody: document.getElementById('panel-body'),
panelTabs: document.getElementById('panel-tabs'),
hud: document.getElementById('hud'),
toast: document.getElementById('toast'),
osd: document.getElementById('btn-osd'),
play: document.getElementById('btn-play'),
thLabel: document.getElementById('th-label'),
thName: document.getElementById('th-name'),
thProgress: document.getElementById('th-progress'),
thStep: document.getElementById('th-step'),
thFill: document.getElementById('th-fill'),
changeTrack: document.getElementById('btn-change-track'),
exportWarn: document.getElementById('export-warn'),
exportWarnBody: document.getElementById('ew-body'),
exportWarnContinue: document.getElementById('ew-continue'),
exportWarnCancel: document.getElementById('ew-cancel'),
exportWarnDontAsk: document.getElementById('ew-dontask'),
};
// Show owns the single WebGL2 context. three@0.181 throws when the browser
// blocks context creation (WebGL2 disabled, headless shell without
// SwiftShader) — historically a blank page, since this runs at module scope.
// Fail loudly into the boot-error box instead, with the cause and the fix.
function showBootError(detail) {
const box = document.getElementById('boot-error');
const pre = document.getElementById('be-detail');
if (pre) pre.textContent = detail;
if (box) box.hidden = false;
}
function bootShow() {
try {
return new Show({ canvas: dom.canvas, width: 1280, height: 720 });
} catch (err) {
const msg = err && err.message ? err.message : String(err);
showBootError(
'The WebGL renderer could not start, so nothing on this page can run — ' +
'every preview, gallery and check renders through the same context.\n\n' +
msg,
);
// Tell the boot guard the startup story is over (it would otherwise
// report its own generic text, clobbering the specific cause above),
// then stop: a half-running app with no renderer is worse than none.
window.__FLOW_STATE_READY__ = 'failed';
throw err;
}
}
const state = {
show: bootShow(),
playing: false,
quality: 'full',
loopSection: -1,
tab: 'look',
hudVisible: false,
osdVisible: true,
busy: false,
rerollSalt: 0,
lastFrameTime: 0,
fps: 0,
};
const strip = new TimelineStrip(dom.timelineCanvas, { onSeek: seekTo });
const paramPanel = new ParamPanel(document.createElement('div'), onParamChange);
// ---------------------------------------------------------------- loading
async function loadFile(file, { seed = null, pins = null, frame = null, warm = null } = {}) {
if (state.busy) return;
state.busy = true;
stopPlayback();
// A dropped file carries no pins of its own — inherit the view settings
// from the URL (a demo deep-link's venueSel/viz), so the room and subject
// you came to see survive the new track. The bank path always passes an
// explicit pins object and is unaffected. Frame/warm never carry over:
// they address the old song's timeline.
const fromDrop = pins == null;
if (fromDrop) {
try {
const q = new URLSearchParams(location.search);
if (q.get('venueSel') || q.get('viz')) {
pins = { venueSel: q.get('venueSel'), viz: q.get('viz') };
}
} catch { /* non-browser — no pins */ }
}
if (dom.overlay) {
dom.overlay.hidden = true;
dom.overlay.style.display = 'none';
}
dom.thLabel.textContent = 'loading track';
dom.thName.hidden = false;
dom.thName.textContent = file.name.replace(/\.[^/.]+$/, '');
dom.thName.title = file.name;
dom.thProgress.hidden = false;
dom.changeTrack.hidden = true;
try {
await state.show.load(file, (stage, fraction) => {
dom.thStep.textContent = stage;
dom.thFill.style.width = `${Math.round((fraction || 0) * 100)}%`;
}, { seed, pins });
dom.audio.src = URL.createObjectURL(file);
dom.thLabel.textContent = 'loaded track';
dom.thName.textContent = state.show.fileName;
dom.thName.title = state.show.fileName;
dom.thProgress.hidden = true;
dom.changeTrack.hidden = false;
dom.changeTrack.textContent = 'change track';
strip.setShow(state.show);
if (dom.osd) dom.osd.classList.toggle('on', state.osdVisible);
resize();
// Deep link from a preview (filmstrip figure, venue-gallery card):
// pins ride into load() so the arc builds pinned layers once, then
// the exact frame with the probe's warm-up so stateful layers land
// converged like the probe render. Dropped files carry inherited
// pins (above) but always start at frame 0.
const deepLink = !!(pins && (pins.venueSel || pins.viz)) || Number.isFinite(frame);
// Prime before the landing seek on deep-link loads: programs link
// asynchronously and a draw against an unlinked program is wrong
// output — the probe pages prime for the same reason, and both sides
// must be linked for the frames to agree. Normal loads skip it.
if (deepLink) state.show.prime();
seekTo(Number.isFinite(frame) ? frame : 0,
Number.isFinite(warm) ? { warmup: warm } : {});
if (fromDrop) clearBankParamsFromUrl();
renderPanel();
document.title = `flow-state · ${state.show.fileName}`;
console.info('[flow-state]', describeLook(state.show.look));
} catch (err) {
const refused = err && err.code === 'COMPOSITION_REFUSED';
dom.thLabel.textContent = refused ? 'high agitation — refuses to render' : 'analysis failed';
const detail = refused
? `director refused song: ${err.message} — try another seed, or a calmer song (murk is dark-noisy bright-hiss; validator expected static bed + small floaters)`
: `failed: ${err.message}`;
dom.thStep.textContent = detail;
if (refused) dom.thStep.title = detail;
console.error(err);
dom.changeTrack.hidden = false;
dom.changeTrack.textContent = 'try again';
if (dom.overlay) {
dom.overlay.hidden = false;
dom.overlay.style.display = 'flex';
}
} finally {
state.busy = false;
}
}
if (dom.dropzone) dom.dropzone.addEventListener('click', () => dom.fileInput.click());
if (dom.changeTrack) dom.changeTrack.addEventListener('click', () => dom.fileInput.click());
dom.fileInput.addEventListener('change', (e) => {
if (e.target.files[0]) loadFile(e.target.files[0]);
});
/**
* A dropped file has no URL identity, so the bank addressing it by name
* (?song=&seed=&frame=&warm=) must go once the file loads — otherwise the
* URL keeps describing the previous song while another plays (and rerolls
* would persist a seed against the wrong name). View settings
* (?venue=&venueSel=&viz=) stay: they describe how you watch, not what.
* Uses replaceState — no reload, no history spam.
*/
function clearBankParamsFromUrl() {
try {
const q = new URLSearchParams(location.search);
if (!q.get('song') && !q.get('seed') && !q.get('frame') && !q.get('warm')) return;
q.delete('song');
q.delete('seed');
q.delete('frame');
q.delete('warm');
const rest = q.toString();
history.replaceState(null, '', rest ? `${location.pathname}?${rest}` : location.pathname);
} catch { /* non-browser or sandboxed iframe */ }
}
/**
* `?song=centre` — open a bank song straight from a debug page.
*
* The filmstrip reduces a song to nine stills; the only way to argue with that
* reading is to watch the thing move, and until now that meant remembering
* which of seventeen near-identically-named wavs to drag in. `&seed=` carries
* the strip's seed across so the video you watch is the one it measured.
* `&frame=` seeks to the exact frame after load (with warm-up), and
* `&venueSel=` re-applies the pinned venue shell — together they make a
* preview deep-link reproduce its image. `&viz=` pins the subject the same
* way, and `&warm=` replays the probe's own warm-up length so feedback
* lands identically converged; deep-link loads prime first so neither side
* renders against unlinked programs. Built by src/ui/preview.js.
*
* Dev-only in practice: `test/songs/` is served by vite from the project root
* and is not part of a build. The name is matched against a bare word rather
* than the bank list so the app does not have to import the synth.
*/
async function loadBankSong(name, seed) {
if (!/^[a-z0-9_-]+$/i.test(name)) return;
const url = `/test/songs/${name}.wav`;
try {
const res = await fetch(url);
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
const blob = await res.blob();
// Preview deep-links (?frame=, ?venueSel=) ride along: the app lands
// on the exact image the preview showed. See src/ui/preview.js.
const q = new URLSearchParams(location.search);
const frame = q.get('frame');
const warm = q.get('warm');
await loadFile(new File([blob], `${name}.wav`, { type: 'audio/wav' }), {
seed,
pins: { venueSel: q.get('venueSel'), viz: q.get('viz') },
frame: frame === null ? null : Number(frame),
warm: warm === null ? null : Number(warm),
});
} catch (err) {
dom.thLabel.textContent = 'no such song';
dom.thName.hidden = false;
dom.thName.textContent = name;
dom.thStep.textContent = `${url}: ${err.message} — run npm run build:songs`;
console.error(err);
}
}
document.addEventListener('dragover', (e) => e.preventDefault());
document.addEventListener('drop', (e) => {
e.preventDefault();
const file = [...e.dataTransfer.files].find(
(f) => f.type.startsWith('audio/') || /\.(mp3|flac|wav|ogg|m4a)$/i.test(f.name));
if (file) loadFile(file);
});
// ---------------------------------------------------------------- transport
function seekTo(frame, options = {}) {
if (!state.show.ready) return;
const clamped = Math.max(0, Math.min(state.show.frameCount - 1, Math.round(frame)));
dom.audio.currentTime = clamped / state.show.fps;
state.show.seek(clamped, options);
strip.setFrame(clamped);
}
/**
* Force the transport to a stopped state.
*
* Loading a track replaces `audio.src`, which stops playback without telling
* anyone — so `state.playing` stayed true, the button stayed on ❚❚, and the
* first click after a track change only toggled the flag back rather than
* starting anything. Every path that stops playback behind the UI's back has
* to come through here.
*/
function stopPlayback() {
state.playing = false;
dom.audio.pause();
dom.play.textContent = '▶';
}
function togglePlay() {
if (!state.show.ready) return;
state.playing = !state.playing;
if (state.playing) {
dom.audio.play().catch((err) => {
console.warn('[flow-state] playback failed:', err);
state.playing = false;
dom.play.textContent = '▶';
});
} else {
dom.audio.pause();
}
dom.play.textContent = state.playing ? '❚❚' : '▶';
}
function jumpSection(direction) {
if (!state.show.ready) return;
seekTo(state.show.track.boundaryFrame(state.show.timeline.frame, direction));
}
function toggleLoop() {
if (!state.show.ready) return;
const index = state.show.track.sectionIndexAt(state.show.timeline.frame);
state.loopSection = state.loopSection === index ? -1 : index;
strip.setLoopSection(state.loopSection);
}
dom.play.addEventListener('click', togglePlay);
document.getElementById('btn-prev-section').addEventListener('click', () => jumpSection(-1));
document.getElementById('btn-next-section').addEventListener('click', () => jumpSection(1));
document.getElementById('btn-loop').addEventListener('click', toggleLoop);
document.getElementById('btn-hud').addEventListener('click', () => {
state.hudVisible = !state.hudVisible;
dom.hud.hidden = !state.hudVisible;
});
if (dom.osd) dom.osd.addEventListener('click', () => setOSDVisible(!state.osdVisible));
document.getElementById('sel-quality').addEventListener('change', (e) => {
state.quality = e.target.value;
resize();
});
function setOSDVisible(visible) {
state.osdVisible = !!visible;
state.show.setOSDEnabled(state.osdVisible);
if (dom.osd) dom.osd.classList.toggle('on', state.osdVisible);
}
document.addEventListener('keydown', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT') return;
switch (e.key) {
case ' ': e.preventDefault(); togglePlay(); break;
case 'ArrowLeft': jumpSection(-1); break;
case 'ArrowRight': jumpSection(1); break;
case 'l': case 'L': toggleLoop(); break;
case 'd': case 'D':
state.hudVisible = !state.hudVisible;
dom.hud.hidden = !state.hudVisible;
break;
case 'o': case 'O': setOSDVisible(!state.osdVisible); break;
case ',': seekTo(state.show.timeline.frame - 1); break;
case '.': seekTo(state.show.timeline.frame + 1); break;
default: break;
}
});
// ---------------------------------------------------------------- look edits
/**
* Persist the current look seed into the URL so a reload restores it.
*
* Only meaningful with `?song=` (bank songs load by name; a dropped file has
* no URL identity, so a seed alone restores nothing). Uses replaceState — no
* reload, no history spam.
*/
function persistSeedInUrl() {
try {
const q = new URLSearchParams(location.search);
if (!q.get('song')) return;
q.set('seed', String(state.show.look.seed >>> 0));
history.replaceState(null, '', `${location.pathname}?${q}`);
} catch { /* non-browser or sandboxed iframe — seeding still works in-memory */ }
}
document.getElementById('btn-reroll').addEventListener('click', () => {
if (!state.show.ready) return;
state.show.reroll((state.show.look.seed ^ (++state.rerollSalt * 0x9e3779b9)) >>> 0);
persistSeedInUrl();
refreshAfterLookChange();
});
document.getElementById('btn-reroll-section').addEventListener('click', () => {
if (!state.show.ready) return;
const index = state.show.track.sectionIndexAt(state.show.timeline.frame);
state.show.rerollSection(index, ++state.rerollSalt);
refreshAfterLookChange();
});
document.getElementById('btn-lock').addEventListener('click', () => {
if (!state.show.ready) return;
const index = state.show.track.sectionIndexAt(state.show.timeline.frame);
const section = state.show.look.sections[index];
section.locked = !section.locked;
renderPanel();
});
function refreshAfterLookChange() {
state.show.seek(state.show.timeline.frame);
renderPanel();
}
function onParamChange(name, value) {
if (!state.show.ready) return;
const index = state.show.track.sectionIndexAt(state.show.timeline.frame);
state.show.setSectionParam(index, name, value);
}
// ---------------------------------------------------------------- panel
dom.panelTabs.addEventListener('click', (e) => {
const tab = e.target.dataset.tab;
if (!tab) return;
state.tab = tab;
[...dom.panelTabs.children].forEach((b) => b.classList.toggle('active', b.dataset.tab === tab));
renderPanel();
});
function renderPanel() {
if (!state.show.ready) return;
const show = state.show;
const index = show.track.sectionIndexAt(show.timeline.frame);
const section = show.look.sections[index];
dom.panelBody.innerHTML = '';
dom.panelBody.oninput = null;
if (state.tab === 'scene') {
paramPanel.container = dom.panelBody;
const subject = subjectOf(section.layers);
paramPanel.build(subject.module, subject.params);
// The section's stage visuals, with the one currently on screen marked.
// Params above edit the anchor (variant 0) — the image the section opens
// and returns to.
if (section.variants && section.variants.length > 1) {
const active = show.arc.state.variant || 0;
const shots = section.shots || [];
const list = document.createElement('div');
list.className = 'pp-reactive';
list.innerHTML = `
`).join('');
dom.panelBody.appendChild(list);
}
// The rest of the stack, named by the job each layer is doing rather
// than by its index: what is under the shot and what is over it are
// different questions, and the panel used to call both "layered over".
const ground = groundOf(section.layers);
const overlays = overlaysOf(section.layers);
if (ground || overlays.length) {
const row = (l, label) =>
`
Mixes clicks onto the detected beat grid. If they don't sit on
the beat, tempo detection is wrong and everything downstream inherits it.
`;
document.getElementById('btn-metronome').addEventListener('click', downloadClickTrack);
document.getElementById('link-venue-toggle')?.addEventListener('click', (e)=>{ e.preventDefault(); show.setVenueEnabled(!show.look.venueEnabled); renderPanel(); show.seek(show.timeline.frame, {warmup:false}); });
return;
}
if (state.tab === 'post') {
const post = show.look.post;
const fb = show.look.feedback;
const wide = new Set(['contrast', 'saturation', 'exposure']);
// Grain has its own block below: its fields are a mode, a mask id and a
// pixel size, none of which are a 0..1 slider.
const rows = Object.entries(post)
.filter(([key]) => !key.startsWith('grain'))
.map(([key, value]) => `
${fbRows}`;
dom.panelBody.oninput = (e) => {
const t = e.target;
if (t.dataset.post) {
post[t.dataset.post] = +t.value;
t.nextElementSibling.textContent = (+t.value).toFixed(2);
}
if (t.dataset.feedback) {
fb[t.dataset.feedback] = +t.value;
t.nextElementSibling.textContent = (+t.value).toFixed(3);
}
if (t.dataset.grain) {
const key = t.dataset.grain;
if (key === 'kinds') {
const kinds = new Set(show.look.grain.kinds);
if (t.checked) kinds.add(t.value); else kinds.delete(t.value);
show.look.grain.kinds = [...kinds];
} else {
show.look.grain[key] = t.tagName === 'SELECT' ? t.value : +t.value;
}
// Turning grain on for a track that was generated without it
// would otherwise select a mode and still show nothing.
if (show.look.grain.mode !== 'off' && show.look.grain.amount <= 0) {
show.look.grain.amount = 0.04;
}
applyGrainToPost(show.look.grain, post);
// The mode decides which of the other controls exist, so it is
// the one edit that has to rebuild the panel.
if (key === 'mode') { renderPanel(); return; }
if (t.nextElementSibling) {
t.nextElementSibling.textContent = (+t.value).toFixed(3);
}
const desc = dom.panelBody.querySelector('#grain-desc');
if (desc) desc.textContent = describeGrain(show.look.grain);
}
};
dom.panelBody.onchange = dom.panelBody.oninput;
return;
}
if (state.tab === 'export') {
dom.panelBody.innerHTML = `
export
${isSupported()
? 'WebCodecs available. Test render first — it uses the same encoder at the same settings.'
: 'WebCodecs VideoEncoder is unavailable here; export will not work in this browser.'}
`;
}
}
/**
* Grain controls for the post tab.
*
* Grain is the one part of the grade with a shape rather than a level — when it
* is present, how coarse it is, how often it refreshes and where it lands — so
* it gets its own block instead of five sliders that all read 0..1.
*/
function grainRows(grain) {
const slider = (key, min, max, step, value) => `
loud ${f.loudness.toFixed(2)} low ${f.bandLow.toFixed(2)} high ${f.bandHigh.toFixed(2)}
` +
`
beat ${f.beat.toFixed(2)} bar ${f.barPhase.toFixed(2)} flux ${f.flux.toFixed(2)}
`;
}
}
// Dev-only handle for the console and for browser automation. The render loop is
// requestAnimationFrame-driven, and rAF does not fire in some headless/automated
// contexts, so `tick()` provides a way to advance the app by hand.
if (import.meta.env && import.meta.env.DEV) {
window.__flowState = { state, dom, strip, tick: (t) => frame(t ?? 0), resize, seekTo, renderPanel };
}
requestAnimationFrame(frame);
resize();
// Tell the boot guard in index.html that the module graph made it all the way
// through. Without this the guard cannot distinguish "still starting" from
// "never going to start", and the failure it exists to catch is exactly the one
// that produces no error at all in the page.
window.__FLOW_STATE_READY__ = true;
const query = new URLSearchParams(location.search);
if (query.get('song')) {
const seed = query.get('seed');
loadBankSong(query.get('song'), seed === null ? null : (Number(seed) >>> 0));
}
// ---------------------------------------------------------------- LLM song tweak
let llmTweakInFlight = false;
let currentSongSpec = null; // last synthesizeSong spec (for bank songs); null for dropped files
function specForTrack(track) {
// For bank songs we have the original spec; for dropped files we derive
// a plausible spec from the measured summary so the LLM has something to edit.
if (currentSongSpec) return { ...currentSongSpec };
if (!track || !track.summary) return null;
const s = track.summary;
return {
bpm: Math.round(s.bpm || 120),
brightness: Math.max(0, Math.min(1, s.meanCentroid ?? 0.5)),
noise: Math.max(0, Math.min(1, s.meanFlatness ?? 0.15)),
noiseColour: null,
dynamics: Math.max(0, Math.min(1, s.dynamicRange ?? 0.6)),
transition: 0.5,
arrangement: 'classic',
duration: Math.round(track.duration || 120),
seed: track.summary ? 1 : 1,
};
}
function setLlmTweakButtonState() {
const btn = document.getElementById('btn-llm-tweak');
const gear = document.getElementById('btn-llm-config');
if (!btn) return;
const gate = canInvoke();
const cfg = getLlmConfig();
const ready = state.show && state.show.ready;
btn.disabled = llmTweakInFlight || !ready || !gate.ok;
if (!ready) btn.title = 'Load a track first';
else if (!cfg.isConfigured) btn.title = 'Set an AI Gateway API key — click ⚙️';
else if (!gate.ok) btn.title = gate.detail;
else btn.title = `Let an LLM tune the generated song · ${remainingText()} · click to review`;
if (gear) {
const dot = cfg.isConfigured ? '●' : '○';
gear.textContent = `${dot} ⚙️`;
gear.title = cfg.isConfigured
? `LLM: ${cfg.model} @ ${cfg.gatewayUrl} · ${remainingText()} — click to configure`
: 'LLM not configured — click to set gateway URL / model / API key';
gear.classList.toggle('llm-not-configured', !cfg.isConfigured);
}
}
function openLlmConfigDialog() {
const cfg = getLlmConfig();
const counts = getBudgetCounts();
const overlay = document.createElement('div');
overlay.className = 'llm-overlay';
overlay.innerHTML = `
LLM gateway settings
Stored in localStorage (flow-state:llm). Never committed. Leave API key empty to disable.
Gateway URL / model may also come from .env.local (VITE_AI_GATEWAY_URL / VITE_AI_MODEL); API key is browser-only via this dialog and never baked into the build. See .env.example.
Budget: ${counts.loadCount}/${counts.perLoad} this load · ${counts.sessionCount}/${counts.perSession} this session. Counters reset on reload / tab close.