// Folder detail view, shows the moves in a single dataset, with // per-row Play / Share / Delete actions and a "+ New move" CTA back // into the wizard. // // Factory createFolder({ robot, Motor, Playback, Audio_, currentFolder, // VolumeRow }), the folder view embeds the volume slider in its // header (only place it currently lives in the UI), so VolumeRow gets // passed in. import { state, render } from "../lib/state.js"; import { fmtTime, slugify } from "../lib/util.js"; import { sniffWavBlob } from "../lib/audio.js"; import { runMigrationWithOverlay } from "../lib/audio-migrate.js"; import { listMoves, putMove, getMove, deleteMove, deleteDatasetAndMoves, putDataset, } from "../lib/db.js"; import { HF } from "../lib/hf.js"; import { Log } from "../lib/log.js"; import { busy, isBusy } from "../lib/busy.js"; import { flat16, ROBOT_AUDIO_PLAYBIN_LEAD_MS } from "../lib/motor.js"; import { safelyReturnToPose, MARIONETTE_DURATION_PRESET } from "../lib/animation-helpers.js"; import { syncBannerHtml, wireSyncBanner } from "./folders.js"; import { confirmDialog, promptDialog, dismissTransientOverlays, } from "./components/modal.js"; import { toast } from "./components/toast.js"; import { emptyState } from "./components/ui.js"; import { createSliderField } from "./components/slider-field.js"; import { createPlaybackReadout } from "./components/playback-readout.js"; import { createMoveCard } from "./components/move-card.js"; import { ICONS } from "./components/icons.js"; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); export function createFolder({ robot, Motor, Playback, Audio_, currentFolder, VolumeRow, stopAllActivity, }) { async function FolderView() { const folder = await currentFolder(); const wrap = document.createElement("div"); wrap.className = "folder-view"; if (!folder) { wrap.innerHTML = emptyState("Folder not found"); return wrap; } const moves = await listMoves(folder.id); state.moves = moves; // Folder identity now lives in the global top app bar: the title // (emoji + name) sits next to the Back chevron, and the folder-level // actions (rename / remove) become the topbar's "⋯" menu. The move // count is conveyed by the "Moves · N" section label / empty state // below, so no separate in-content header is needed. state.topbarTitle = `${folder.emoji || "🎭"} ${folder.name}`; state.topbarSubtitle = `${moves.length} move${moves.length === 1 ? "" : "s"}`; state.topbarMenu = [ { label: "Rename", iconHtml: ICONS.pencil, onClick: () => renameFolder(folder) }, { label: "Remove folder", iconHtml: ICONS.trash, kind: "danger", onClick: () => removeFolder(folder, moves.length) }, ]; // Bottom dock (thumb zone): the playback controls (volume + // per-folder audio sync) sit right above the primary CTA — record // into this folder. Grouping them here keeps the two tuning sliders // always reachable instead of scrolling to the bottom of the list. // Created here, appended last so it pins to the bottom of the view. const cta = document.createElement("div"); cta.className = "folder-cta cta-dock"; const playback = document.createElement("div"); playback.className = "folder-playback"; const pLabel = document.createElement("div"); pLabel.className = "section-label"; pLabel.textContent = "Playback"; playback.appendChild(pLabel); playback.appendChild(VolumeRow()); playback.appendChild(SyncOffsetRow(folder)); cta.appendChild(playback); const addBtn = document.createElement("button"); addBtn.className = "btn primary btn-block"; addBtn.id = "add"; addBtn.textContent = "+ Record a new move"; addBtn.onclick = () => { state.view = "wizard"; state.step = 1; state.wizardOrigin = "folder"; render(); }; cta.appendChild(addBtn); // Update banner — community datasets only, when stale upstream. // Actionable, so kept up top. Inserted async so the folder render // isn't blocked on a network round-trip. const updateSlot = document.createElement("div"); wrap.appendChild(updateSlot); if (folder.source === "community" && folder.repoId) { checkForUpdates(folder, updateSlot); } // Compress banner: only for datasets YOU own that still have WAV // sidecars upstream. Offers a one-click compress + re-publish. if (folder.repoId) { checkForCompression(folder, updateSlot); } // One-shot sync-architecture advisory banner. The actionable // control (per-folder Audio sync offset) lives in Playback // settings below; the banner stays visible so an out-of-sync // folder points the user there. Dismiss persists via localStorage. const bannerHost = document.createElement("div"); bannerHost.innerHTML = syncBannerHtml(); if (bannerHost.firstElementChild) { wrap.appendChild(bannerHost.firstElementChild); wireSyncBanner(wrap); } // Moves section. const body = document.createElement("div"); body.className = "folder-body"; const list = document.createElement("div"); list.className = "list"; if (moves.length === 0) { // Richer empty state (icon badge + title + hint) — the bare // one-line text read as unfinished. list.innerHTML = `
🎬
No moves yet
Tap “+ Record a new move” below to capture your first one.
`; } else { const label = document.createElement("div"); label.className = "section-label"; label.textContent = "Moves"; body.appendChild(label); for (const m of moves) list.appendChild(moveRow(m, folder)); } body.appendChild(list); wrap.appendChild(body); // Docked controls (playback sliders + primary action), last so the // dock pins to the bottom of the view. wrap.appendChild(cta); // Global topbar Back leaves the folder for home (after stopping any // in-progress playback / audio so nothing keeps running in the // background). state.onBack = async () => { await stopAllActivity(); dismissTransientOverlays(); Object.assign(state, { view: "home", selectedFolderId: null, highlightMoveId: null }); busy.playingMoveId = null; busy.isReturning = false; render(); }; return wrap; } // Per-folder audio-sync offset row. Added 2026-05-23 alongside the // upload-then-play architecture rollout: recordings made before the // cutover may carry baked-in WebRTC-streaming drift in their motion // timestamps, and a per-folder offset is the lean way to salvage // them. The author tunes once on their machine; the value persists // in the dataset record and is added to the global Settings offset // at play time. Default 0, new recordings need not touch it. // // Below the slider, a "Share this tuning with viewers" button appears // when the author has tuned the value away from what's currently // committed on HF. Click to publish a marionette.json-only commit; // viewers see the "Update available" banner on next visit and pick // up the new offset. The button is hidden when: // • the user isn't the author of this folder's HF repo, OR // • the folder has no HF repo yet (no moves shared), OR // • the local value matches what's already published. function SyncOffsetRow(folder) { const container = document.createElement("div"); // Read directly from the dataset record. Mutated on input; // putDataset persists (debounced) on change. const v = Number(folder.audioOffsetMs ?? 0); const fmt = (n) => `${n >= 0 ? "+" : ""}${n} ms`; // Debounce the persistence call; updating IndexedDB on every // input event is wasteful and could race on rapid drags. let saveTimer = null; const persist = () => putDataset(folder).catch((e) => Log.warn("save folder offset failed", { error: String(e) })); const field = createSliderField({ min: -1000, max: 1000, step: 25, value: v, icon: "🎚️", ariaLabel: "Audio sync offset for this folder", className: "volume-row", format: fmt, // Locked while a move is playing/returning: the offset is baked // into the daemon-side playback at start, so mid-play changes // wouldn't apply and would just confuse. The volume slider stays // live (device-side gain, safe to tweak during playback). disabled: isBusy(), onInput: (n) => { folder.audioOffsetMs = n; if (saveTimer) clearTimeout(saveTimer); saveTimer = setTimeout(persist, 250); refreshPublishVisibility(); }, onChange: (n) => { // Safety net for browsers that drop `input` on the custom // range thumb (some iOS Safari versions). Flushes any // pending save. folder.audioOffsetMs = n; if (saveTimer) { clearTimeout(saveTimer); saveTimer = null; } persist(); refreshPublishVisibility(); }, }); container.appendChild(field.el); // Publish button — hidden by default; conditions checked below. const publishRow = document.createElement("div"); publishRow.style.cssText = "padding: 6px 16px 0; display: none;"; publishRow.innerHTML = ` `; const publishBtn = publishRow.querySelector("button"); container.appendChild(publishRow); // The author may be us if the repoId namespace matches our HF // username. Resolved async (cached after first call). Falsy // (null) means "still checking" or "not author"; null → button // never shows. Cached so input events don't re-fetch. let isAuthor = null; (async () => { try { if (!folder.repoId || !robot._token) return; const myNamespace = await HF.getHfNamespace(robot._token); const repoAuthor = folder.repoId.split("/")[0]; isAuthor = (myNamespace === repoAuthor); refreshPublishVisibility(); } catch (e) { Log.warn("publish-tuning author check failed", { error: String(e) }); } })(); function refreshPublishVisibility() { if (!isAuthor) { publishRow.style.display = "none"; return; } // publishedAudioOffsetMs is set on every share commit + on // every successful publish-tuning. Default 0 for folders // shared before this field existed; they'll show the button // only when the author re-tunes (which is the right call). const published = Number(folder.publishedAudioOffsetMs ?? 0); const current = Number(folder.audioOffsetMs ?? 0); publishRow.style.display = (current !== published) ? "block" : "none"; } publishBtn.onclick = async () => { const value = Number(folder.audioOffsetMs ?? 0); publishBtn.disabled = true; const originalText = publishBtn.textContent; publishBtn.textContent = "Publishing…"; toast("Publishing offset to viewers…", "", { sticky: true }); try { await HF.publishFolderTuning(folder.repoId, value, robot._token); folder.publishedAudioOffsetMs = value; await putDataset(folder); Log.info("published folder tuning", { repoId: folder.repoId, audioOffsetMs: value, }); toast(`✓ Tuning shared (${value >= 0 ? "+" : ""}${value} ms)`, "success"); refreshPublishVisibility(); } catch (e) { Log.error("publish folder tuning failed", { error: String(e) }); toast(`Publish failed: ${e.message}`, "error"); publishBtn.disabled = false; publishBtn.textContent = originalText; } }; return container; } // Probe upstream for a newer commit and surface a banner with an // Update button if so. Fail-soft on network errors / 404 / etc. async function checkForUpdates(folder, slotEl) { let meta; try { meta = await HF.getDatasetMeta(folder.repoId, robot._token); } catch (e) { // Network or 404, silently skip the banner. If the repo // was deleted upstream, the user can still Remove the // folder via the footer button; no need to alarm them. return; } const upstream = meta.lastModified || meta.last_modified; if (!upstream) return; // Compare against communityLastModified if we recorded it at // import time (modern path), else fall back to createdAt // (legacy path: existing imports from before this feature). const anchor = folder.communityLastModified || new Date(folder.createdAt).toISOString(); if (Date.parse(upstream) <= Date.parse(anchor)) return; // up-to-date const banner = document.createElement("div"); banner.className = "card"; banner.style.cssText = "padding: 12px 14px; margin: 8px 16px; display: flex; align-items: center; gap: 12px;"; banner.innerHTML = `
Update available
`; slotEl.appendChild(banner); banner.querySelector("#do-update").onclick = () => updateFolder(folder, banner); } // Show a "Compress" banner when the dataset is yours (HF namespace matches) // and still has WAV sidecars upstream (i.e. not yet in the compressed // format). One click re-publishes it as Opus + 50 Hz. All network; bails // out silently on any error so it never blocks the folder view. async function checkForCompression(folder, slotEl) { if (!folder.repoId || !robot?._token) return; let mine; try { mine = await HF.getHfNamespace(robot._token); } catch { return; } if (folder.repoId.split("/")[0] !== mine) return; // not your dataset let files; try { files = await HF.listDatasetFiles(folder.repoId, robot._token); } catch { return; } if (!files.some((f) => f.path.endsWith(".wav"))) return; // already compressed const banner = document.createElement("div"); banner.className = "card"; banner.style.cssText = "padding: 12px 14px; margin: 8px 16px; display: flex; align-items: center; gap: 12px;"; banner.innerHTML = `
Compress this dataset (−90%, recommended)
`; slotEl.appendChild(banner); banner.querySelector("#do-compress").onclick = () => compressFolder(folder, banner); } async function compressFolder(folder, bannerEl) { if (isBusy()) { toast("Stop playback before compressing", "error"); return; } const moves = await listMoves(folder.id); if (!moves.length) { toast("No moves to compress.", "error"); return; } bannerEl?.querySelector("#do-compress")?.setAttribute("disabled", "true"); toast("Compressing…", "", { sticky: true }); try { const res = await HF.compressAndRepublish( folder.repoId, moves, robot._token, (done, total) => { if (done % 5 === 0 || done === total) { toast(`Compressing ${done}/${total}…`, "", { sticky: true }); } }, ); // compressAndRepublish already synced the local copies to the // published compressed version, so playback works right away — no // "Update" step. Record the new upstream timestamp so the // "Update available" banner doesn't fire for our own commit. try { const meta = await HF.getDatasetMeta(folder.repoId, robot._token); folder.communityLastModified = meta.lastModified || meta.last_modified || folder.communityLastModified; await putDataset(folder); } catch { /* best-effort; not fatal */ } toast( `✓ Compressed "${folder.name}" — ${res.moves} moves, removed ${res.deletedWav} WAV file(s).`, "success", ); bannerEl?.remove(); render(); } catch (e) { toast(`Compression failed: ${e.message}`, "error"); Log.error("compressAndRepublish failed", { repoId: folder.repoId, error: String(e) }); bannerEl?.querySelector("#do-compress")?.removeAttribute("disabled"); } } async function updateFolder(folder, bannerEl) { if (isBusy()) { toast("Stop playback before updating", "error"); return; } // Surgical update: keep any local recordings the user made // into this folder, refresh community-sourced moves to the // latest upstream version, and remove community moves whose // upstream entry was deleted. Replaces the old nuke-and-reimport // which was destroying local recordings filed inside community // folders. // // Identity model: every move carries `fromCommunity` set at // import time (lib/hf.js). Locally-recorded moves default to // false in the wizard. Move ids never collide because the // wizard `newId()`s every save and imports use the upstream // file stem as a stable key; the two namespaces don't overlap // unless someone hand-edits IndexedDB. const existing = await listMoves(folder.id); const prevCommunityIds = new Set( existing.filter((m) => m.fromCommunity === true).map((m) => m.id), ); // Local recordings filed inside this community folder — the // merge below preserves them; counted for the summary toast. const localOnlyCount = existing.length - prevCommunityIds.size; // No confirmation: the user clicked Update deliberately, and local // recordings in this folder are preserved by the merge below anyway. toast("Updating…", "", { sticky: true }); bannerEl?.querySelector("#do-update")?.setAttribute("disabled", "true"); try { const { importedIds } = await HF.importDataset( folder.repoId, robot._token, (done, total) => { if (done % 5 === 0 || done === total) { toast(`Updated ${done}/${total} moves…`, "", { progress: total ? done / total : 0 }); } }, ); // Orphans: community moves that existed locally before this // run but were NOT touched by the import. Either the // upstream owner deleted them, or the listing changed shape // (rare). Drop them so the folder mirrors upstream for the // community-sourced subset; local recordings stay. const orphanIds = [...prevCommunityIds].filter((id) => !importedIds.has(id)); for (const id of orphanIds) await deleteMove(id); const refreshed = [...importedIds].filter((id) => prevCommunityIds.has(id)).length; const added = importedIds.size - refreshed; const summary = []; if (refreshed) summary.push(`refreshed ${refreshed}`); if (added) summary.push(`added ${added}`); if (orphanIds.length) summary.push(`removed ${orphanIds.length}`); if (localOnlyCount) summary.push(`kept ${localOnlyCount} local`); const summaryStr = summary.length ? ` (${summary.join(", ")})` : ""; toast(`✓ "${folder.name}" updated${summaryStr}`, "success"); Log.info("surgical folder update", { folderId: folder.id, repoId: folder.repoId, refreshed, added, removed: orphanIds.length, keptLocal: localOnlyCount, }); render(); } catch (e) { Log.error("dataset update failed", { error: String(e), repoId: folder.repoId }); toast(`Update failed: ${e.message}`, "error"); // Re-enable the button so they can retry without leaving // and re-entering the folder. bannerEl?.querySelector("#do-update")?.removeAttribute("disabled"); } } async function renameFolder(folder) { const name = await promptDialog("Rename folder", "", folder.name || "", { confirmText: "Rename" }); if (name == null) return; const trimmed = name.trim(); if (!trimmed || trimmed === folder.name) return; folder.name = trimmed; await putDataset(folder); toast("Folder renamed", "success"); render(); } async function removeFolder(folder, moveCount) { const body = moveCount === 0 ? "The folder will be removed." : `All ${moveCount} move${moveCount === 1 ? "" : "s"} in this folder will be deleted.`; const ok = await confirmDialog( `Remove "${folder.name}"?`, body, { confirmText: "Remove", confirmKind: "danger" }, ); if (!ok) return; try { await deleteDatasetAndMoves(folder.id); toast(`Removed "${folder.name}"`); // Drop any cached community-list count for this repo so // the home view shows the fresh state. state.communityDatasets = null; Object.assign(state, { view: "home", selectedFolderId: null, highlightMoveId: null }); busy.playingMoveId = null; busy.isReturning = false; render(); } catch (e) { Log.error("removeFolder failed", { error: String(e), id: folder.id }); toast(`Couldn't remove: ${e.message}`, "error"); } } function moveRow(m, folder) { const audioLabel = m.audioMode === "silent" ? "silent" : m.audioMode === "upload" ? "file" : m.audioMode === "record_now" ? `record now · ${m.audioMic}` : m.audioMode === "record_live" ? "record live · device mic" : m.audioMode || "?"; const isPlaying = busy.playingMoveId === m.id; // Disabled if some OTHER move is playing or the post-move // return is still running. The currently-playing move's button // stays live so it can be used to stop. const playDisabled = !isPlaying && isBusy(); // Contextual actions on the move. Locally-created moves get the // Share / ✓ Shared toggle and a Delete button. Community-imported // moves get nothing here: the folder header already shows the // author, and community datasets are read-only mirrors (a re-sync // of the parent folder would restore any deleted move anyway - // use Remove folder to drop the whole dataset). const actions = []; if (!m.fromCommunity) { actions.push({ id: "share", kind: "primary", className: "move-action" + (m.shared ? " shared" : ""), html: m.shared ? `${ICONS.check}Shared` : `${ICONS.share}Share`, }); actions.push({ id: "rename", kind: "primary", className: "move-action icon-only", title: "Rename", ariaLabel: "Rename", html: ICONS.pencil, }); actions.push({ id: "delete", kind: "primary", className: "move-action icon-only", title: "Delete", ariaLabel: "Delete", html: ICONS.trash, }); } const card = createMoveCard({ icon: m.audioBlob ? "🎵" : "🎭", title: m.label, meta: `${fmtTime(m.duration)} · ${audioLabel}${(!m.fromCommunity && m.shared) ? " · shared" : ""}`, playing: isPlaying, playDisabled, onPlay: () => playMove(m), highlight: m.id === state.highlightMoveId, actions, }); const r = card.el; r.dataset.moveId = m.id; const deleteEl = card.actionEls.delete; if (deleteEl) { deleteEl.onclick = async () => { const ok = await confirmDialog(`Delete "${m.label}"?`, "This can't be undone.", { confirmText: "Delete", confirmKind: "danger" }); if (!ok) return; await deleteMove(m.id); if (state.highlightMoveId === m.id) state.highlightMoveId = null; render(); }; } const renameEl = card.actionEls.rename; if (renameEl) { renameEl.onclick = async () => { const oldLabel = m.label; const name = await promptDialog("Rename move", "", oldLabel || "", { confirmText: "Rename" }); const trimmed = (name || "").trim(); if (!trimmed || trimmed === oldLabel) return; m.label = trimmed; // Capture the currently-published slug so a later re-share // can delete the stale HF file (the data file is named after // the label). uploadMove keeps sharedStem fresh after that; // here we only seed it for moves shared before this existed. if (m.shared && !m.sharedStem) m.sharedStem = slugify(oldLabel); await putMove(m); render(); if (m.shared) { toast("Renamed. The new name reaches Hugging Face next time you re-share.", ""); } }; } const shareBtn = card.actionEls.share; if (shareBtn) { shareBtn.onclick = async () => { if (!robot._token) { toast("Sign in to Hugging Face to share.", "error"); return; } if (!m.motion?.time?.length && !m.audioBlob) { toast("Nothing to share, move is empty.", "error"); return; } // Resolve canonical namespace via whoAmI (cached). // robot.username can be the display name with spaces if // cached from a pre-fix session, using it as a repo // path triggers a 403 from HF. let username; try { username = await HF.getHfNamespace(robot._token); } catch (e) { toast(`Couldn't resolve your HF account: ${e.message}`, "error"); return; } let repoName; if (m.shared) { // Re-publish flow. The chip says ✓ Shared but the // user wants to push again, most commonly to // refresh the dataset's README / community tags // (any dataset created via JS Marionette before // 2026-05-19 lacks the community tag and is // invisible in the Community tab). const ok = await confirmDialog( `Re-publish "${m.label}"?`, "Re-uploads the move and refreshes the dataset README (including the community-discovery tag). Use this to make older shares appear in the Community tab.", { confirmText: "Re-publish" }, ); if (!ok) return; // Derive the repo name from the sharedUrl if we // have it; otherwise fall back to the folder slug. // This preserves whatever name the user originally // chose, even if they've since renamed the folder. const fromUrl = m.sharedUrl?.split("/datasets/")[1]?.split("/")[1]; repoName = fromUrl || slugify(folder.name || "marionette-moves"); } else if (folder?.repoId?.startsWith(`${username}/`)) { // The folder already publishes to a dataset we own // (repoId is recorded on first share) — share // straight into it, no dialog. Asking the user to // re-confirm the destination on every share of the // folder they're standing in was pure friction. repoName = folder.repoId.split("/")[1]; } else { // First share from this folder (or its repoId // points at someone else's community dataset): // the destination genuinely isn't established yet, // so ask once. The answer sticks via folder.repoId. const defaultRepo = slugify(folder.name || "marionette-moves"); repoName = await promptDialog( "Share to Hugging Face", `Upload "${m.label}" to a dataset under ${username}/…`, defaultRepo, { confirmText: "Upload" }, ); if (!repoName) return; } // The Share button itself carries the in-flight state // (disabled + spinner + "Uploading…"): it pins the // feedback to the row that's uploading, and the bottom // of the screen stays free for the playback dock. Only // the terminal outcome (shared / failed) goes to a toast. shareBtn.disabled = true; const originalChip = shareBtn.innerHTML; shareBtn.classList.add("is-busy"); shareBtn.innerHTML = `Uploading…`; try { // Pass the folder's current per-folder audio offset so // that the marionette.json sidecar at the repo root is // up to date for downloaders. Zero offset writes nothing // (no marionette.json), keeping unchanged datasets clean. // // protectedStems: slugs the folder's OTHER moves publish // under. uploadMove skips its rename cleanup when the old // slug matches one of these — slugify(label) is not // unique, so the "stale" files could be a sibling move's // live remote copy. const siblings = folder ? (await listMoves(folder.id)).filter((s) => s.id !== m.id) : []; const protectedStems = [...new Set( siblings.flatMap((s) => [slugify(s.label || ""), s.sharedStem]).filter(Boolean), )]; const url = await HF.uploadMove(m, repoName, robot._token, { audioOffsetMs: folder?.audioOffsetMs, protectedStems, // Feed the folder's moves to the metadata.jsonl // builder so every audio row in the repo keeps a // title/description, not just the move being shared. localMoves: siblings, }); m.shared = true; m.sharedUrl = url; await putMove(m); // Record the HF repo + the offset we just published // on this folder so the "Share this tuning" button in // SyncOffsetRow can detect when the slider drifts // from what's on HF. Author folders carry no // folder.repoId until first share, so this is also // where we capture it (parsed from the returned URL // — the same `username/repoBase` shape uploadMove // computes internally). if (folder) { const slug = url.split("/datasets/")[1] || null; if (slug) folder.repoId = slug; folder.publishedAudioOffsetMs = Number(folder.audioOffsetMs ?? 0); try { await putDataset(folder); } catch (e) { Log.warn("save folder repoId/published failed", { error: String(e) }); } } // Invalidate the community list cache so the next // time the user opens Community the new (or // refreshed) dataset shows up without needing the // refresh button. state.communityDatasets = null; toast(`✓ Shared → ${url.split("/datasets/")[1]}`, "success"); render(); } catch (e) { Log.error("share failed", { error: String(e) }); toast(`Share failed: ${e.message}`, "error"); shareBtn.disabled = false; shareBtn.classList.remove("is-busy"); shareBtn.innerHTML = originalChip; } }; } if (m.id === state.highlightMoveId) { setTimeout(() => r.scrollIntoView({ behavior: "smooth", block: "center" }), 80); } return r; } async function playMove(m) { // Pause/stop on the currently-playing move. if (busy.playingMoveId === m.id) { Playback.cancel(); Audio_.stopAll(); return; } // Refuse to start another move while one is playing or // returning. if (isBusy()) return; if (robot.state !== "streaming") { if (m.audioBlob) { // No robot, just preview the audio. const a = new Audio(URL.createObjectURL(m.audioBlob)); a.play().catch(() => {}); } toast("Not connected, audio-only preview.", "error"); return; } // Playable-as-is = canonical WAV or Ogg/Opus (the robot's GStreamer // decodes both). Only legacy containers (mp3/m4a/flac mislabeled as // .wav) get kicked through the migration sweep, which now converts // them to Opus. Ogg/Opus passes straight to playback. const playable = (c) => c.ok || c.reason === "ogg"; if (m.audioBlob) { let check = await sniffWavBlob(m.audioBlob); if (!playable(check)) { Log.warn("playMove found legacy audio, running migration", { moveId: m.id, label: m.label, detected: check.reason, mime: m.audioBlob.type, }); try { await runMigrationWithOverlay({ hf: HF, robot }); } catch (e) { toast(`Audio fix failed: ${e.message}`, "error"); Log.error("audio migration failed", { error: String(e) }); return; } // Re-read the move from the DB; its blob has been // rewritten in place. Re-sniff to make sure. const refreshed = await getMove(m.id); if (refreshed?.audioBlob) { m.audioBlob = refreshed.audioBlob; m.audioMime = refreshed.audioMime; check = await sniffWavBlob(m.audioBlob); } if (!playable(check)) { toast( `Could not convert this move's audio (still ${check.reason} after migration).`, "error", ); return; } } } busy.playingMoveId = m.id; state.highlightMoveId = m.id; // Fire-and-forget: the rebuild repaints the card into its playing // state, but we must NOT await it — a big folder's re-render decodes // every audio blob (listMoves) and would sit between the play tap // and the robot's first motion. The progress binder below re-resolves // its card on every event, so it catches the fresh card as soon as // the swap lands. render(); // All live feedback lives on the move's card (spinner + "Uploading // NN%" while the move streams to the daemon, then the top bar + // "M:SS / M:SS" once the daemon loop runs). No separate toast: the // card is the player. The readout re-resolves its card on every // event because render() above swaps the node asynchronously. const readout = createPlaybackReadout({ resolve: () => document.querySelector(`.move-card[data-move-id="${CSS.escape(m.id)}"]`), durationHint: m.duration, }); const hasMotion = m.motion?.time?.length > 0; try { if (hasMotion) { // Goto the move's first frame before daemon-side // playback takes over. Daemon's play_move starts at // frame[0], so we just goto frame[0] (no lead comp — // the daemon's 100 Hz inner loop has no network jitter // to compensate for). // // safelyReturnToPose is fine for a non-INIT target: it // (a) enables torque safely thanks to daemon-side // pinning (PR #1138), (b) computes a distance-scaled // duration via the SDK's three-channel helper, and // (c) dispatches the goto synchronously. We await // plan.duration + 50 ms slack so playMove only kicks // off once the daemon has settled at frame[0]. const frame0 = m.motion.set_target_data[0]; if (frame0?.head) { const plan = safelyReturnToPose(robot, { preset: MARIONETTE_DURATION_PRESET, target: { head: flat16(frame0.head), antennas: frame0.antennas ?? undefined, body_yaw: frame0.body_yaw ?? 0, }, }); await sleep(plan.duration * 1000 + 50); } } // Audio-only moves: motors are already at base + enabled // (default app state), so we don't need to prep anything. // Two audio paths: // - Robot output: upload the WAV alongside the move so // the DAEMON plays both audio and motion on a single // clock (no cross-network drift). Skip the // browser-side audio entirely. // - Device output: keep the browser-side Audio_.prepare // /start path (pre-decoded during upload, kicked off // by the daemon's "started" event). No audio upload // to the daemon in this case -- daemon plays motion // only. const audioPref = state.settings.audioOutput; const wantRobotAudio = (audioPref === "robot") && hasMotion && !!m.audioBlob; const wantDeviceAudio = (audioPref === "device") && hasMotion && !!m.audioBlob; // Pre-decode only for the device path -- robot path goes // to the daemon and doesn't need a browser-side source. const audioPrepPromise = wantDeviceAudio ? Audio_.prepare(m.audioBlob).catch((e) => { Log.warn("audio prepare failed", { error: String(e) }); return null; }) : null; let audioHandle = null; // User-tunable offset (ms). Sum of two layers: // 1. Global setting (localStorage `audioOffsetMs`), tuned // once per device in the Settings sheet, applies to // everything. // 2. Per-folder offset (`folder.audioOffsetMs`), added on // top, lets a folder author salvage legacy recordings // whose motion timestamps carried baked-in WebRTC // streaming drift. Default 0; new folders shouldn't // need it. // Both for device audio (delays the browser source) and // robot audio (shifts the daemon-side audio relative to // motion; positive = audio first). const folder = await currentFolder(); const audioOffsetMs = Number(localStorage.getItem("audioOffsetMs") || 0) + Number(folder?.audioOffsetMs || 0); if (hasMotion) { await Playback.play(m.motion, { description: m.label ?? "marionette move", audioBlob: wantRobotAudio ? m.audioBlob : null, // Robot output: compensate the playbin warmup so motion // doesn't lead the music, then layer the user's offset on // top. Device output routes audio through the browser // (no playbin), so it keeps the raw offset via onStarted. audioLeadMs: wantRobotAudio ? (ROBOT_AUDIO_PLAYBIN_LEAD_MS + audioOffsetMs) : 0, onProgress: (p) => readout.update(p), onStarted: async ({ has_audio, duration_s }) => { readout.start(duration_s); // Daemon-side audio? Nothing to do here -- // the daemon already triggered playbin in // lockstep with its motion loop. if (has_audio) return; // Device-side audio: kick off the pre-decoded // source. Sub-ms latency from this point. if (audioPrepPromise) { try { audioHandle = await audioPrepPromise; audioHandle?.start(audioOffsetMs); } catch (e) { Log.warn("audio start failed", { error: String(e) }); } } }, }); } else if (m.audioBlob) { // Audio-only: no upload phase, just play locally. audioHandle = await Audio_.play(m.audioBlob); readout.start(audioHandle?.duration || m.duration); if (audioHandle?.waitEnd) await audioHandle.waitEnd; } if (audioHandle) { try { await audioHandle.stop(); } catch {} } } catch (e) { Log.error("playback failed", { error: String(e), moveId: m.id }); toast(`Playback failed: ${e.message}`, "error"); } finally { readout.stop(); // Switch from "playing" to "returning" while the goto-base // runs. Everything stays disabled across this transition: // no flicker window for the user to fire another move. busy.playingMoveId = null; busy.isReturning = true; if (state.highlightMoveId === m.id) state.highlightMoveId = null; render(); // Diagnostic snapshot at the playback-end boundary. Mostly // useful when chasing reports of brutal end-of-move motion; // pair with the "safelyReturnToPose post-playback" log // emitted a few lines down to compare pose-at-boundary vs // pose-used-for-scaledDuration. Log.info("playback ended, entering return-to-base", { moveId: m.id, moveName: m.name, stateHeadXyzM: robot.robotState?.head ? [robot.robotState.head[3], robot.robotState.head[7], robot.robotState.head[11]] : null, stateAntennasDeg: Array.isArray(robot.robotState?.antennas) ? [ +(robot.robotState.antennas[0] * 180 / Math.PI).toFixed(1), +(robot.robotState.antennas[1] * 180 / Math.PI).toFixed(1), ] : null, stateBodyYawDeg: typeof robot.robotState?.body_yaw === "number" ? +(robot.robotState.body_yaw * 180 / Math.PI).toFixed(1) : null, isMoveRunning: robot.robotState?.is_move_running, motorMode: robot.robotState?.motor_mode, }); try { // safelyReturnToPose enables torque safely (daemon pins // to current present pose on PR #1138, so no jerk), // computes a three-channel scaledDuration, and // dispatches the goto. Returns synchronously with the // plan; we await plan.duration + 50 ms slack so the // UI's busy lock outlives the daemon-side interp. // // No pre-goto setTarget pin: that was the legacy // workaround that caused the antenna oscillation // (cached robot.robotState was up to ~500 ms stale, // pinning to that wrenched the motors to where they // used to be). The daemon handles its own pinning now. const plan = safelyReturnToPose(robot, { preset: MARIONETTE_DURATION_PRESET, }); Log.info("safelyReturnToPose post-playback", { plan }); await sleep(plan.duration * 1000 + 50); } catch (e) { Log.warn("safelyReturnToPose after playback failed", { error: String(e) }); } finally { busy.isReturning = false; render(); } } } return { FolderView }; }