Skip to content

AI media harness for planning, generating, and editing locally

TypeScript
33.0 KB
Raw Blame
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
import { loadActiveGeneratorId, saveActiveGeneratorId, selectedGeneratorTemplate, subscribeActiveGenerators } from "../../lib/settings";
import type { ProjectAsset, ProjectConfig } from "../../lib/project";
import { isTauri, readMediaFileUrl } from "../../lib/persistence";
import { loadGeneratorTemplateSettings, subscribeGeneratorTemplates, templateSupportsMusic, templateUsable, generatorPathFields, type GeneratorTemplate } from "../../lib/settings";
import { YUE2_DEFAULT_STEPS, duplicateSong, musicSettingsForModel, moveSong, newMusicFolder, newSong, newSongVariation, removeMusicFolder, songReference, updateSong, type MusicSettings, type MusicSong } from "../../lib/music";
import { isWorkActive, MAX_MUSIC_BATCH_COUNT, type WorkItem } from "../../lib/workQueue";
import { ComboBox, CommandBarButton, Flyout, InfoBar, ProgressBar, ProgressRing, PropRow, PropSection, Splitter, useContextMenu, usePaneSize, type MenuEntry } from "../ui";
import { Add16, Audio16, ChevronDown14, Copy16, Delete16, Edit16, FolderAdd16, FolderOpen16, GridView16, ListView16, Pause16, Play16, References16, Sparkle16, Stop14 } from "../ui/icons";
import { MusicPlaybackControls } from "./MusicPlaybackControls";
import { generatorOptions } from "../generatorOptions";
import { SliderNumberInput, GENERATION_STEPS_RANGE } from "./SliderNumberInput";
import type { ConfigUpdate } from "./TimelineView";
import { GeneratorSelector } from "./GeneratorSelector";
import { ProjectStatus } from "./ProjectStatus";
import { PREVIEW_PLAYBACK_STARTED } from "../../lib/mediaPlayback";
import { ReferenceImage } from "./ReferenceImage";

const time = (seconds: number) => `${Math.floor(seconds / 60)}:${Math.floor(seconds % 60).toString().padStart(2, "0")}`;
const SONG_DRAG_TYPE = "application/x-slopus-music-song";

export function MusicView({ config, folderPath, onChange, onGenerate, onGenerateCover, onCancel, workItems, openSong, onSongOpened, active = true, onPlayingChange }: {
  active?: boolean; onPlayingChange?: (playing: boolean) => void;
  openSong?: { id: string }; onSongOpened?: () => void;
  config: ProjectConfig; folderPath: string; onChange: (update: ConfigUpdate, key?: string) => void;
  onGenerate: (songId: string, template: GeneratorTemplate, count: number) => string | void; onCancel: (id: string) => void;
  onGenerateCover?: (songId: string) => void;
  workItems: readonly WorkItem[];
}) {
  const [templates, setTemplates] = useState(() => loadGeneratorTemplateSettings().templates.filter(templateSupportsMusic));
  useEffect(() => subscribeGeneratorTemplates(() => setTemplates(loadGeneratorTemplateSettings().templates.filter(templateSupportsMusic))), []);
  const folders = config.musicFolders ?? [];
  const [folderId, setFolderId] = useState<string | null>(null);
  const currentFolder = folders.find((folder) => folder.id === folderId);
  const currentFolderId = currentFolder?.id ?? null;
  const visibleFolders = folders.filter((folder) => (folder.parentId ?? null) === currentFolderId);
  const breadcrumbs: { id: string | null; name: string }[] = [];
  const visited = new Set<string>();
  let ancestor = currentFolder;
  while (ancestor && !visited.has(ancestor.id)) {
    visited.add(ancestor.id); breadcrumbs.unshift({ id: ancestor.id, name: ancestor.name });
    const parentId = ancestor.parentId;
    ancestor = folders.find((folder) => folder.id === parentId);
  }
  breadcrumbs.unshift({ id: null, name: "Music" });
  const visibleSongs = (config.musicSongs ?? []).filter((song) => (song.folderId ?? null) === currentFolderId);
  const [creatingFolder, setCreatingFolder] = useState(false);
  const [folderName, setFolderName] = useState("");
  const [dropFolder, setDropFolder] = useState<string>();
  // null explicitly clears selection; undefined keeps the initial first-item fallback.
  const [selectedId, setSelectedId] = useState<string | null | undefined>(config.musicSongs?.[0]?.id);
  useEffect(() => { if (openSong) { setFolderId(config.musicSongs?.find((song) => song.id === openSong.id)?.folderId ?? null); setSelectedId(openSong.id); onSongOpened?.(); } }, [openSong]);
  const selected = selectedId === null ? undefined : visibleSongs.find((song) => song.id === selectedId) ?? visibleSongs[0];
  const activeGeneratorId = useSyncExternalStore(subscribeActiveGenerators, () => loadActiveGeneratorId("music"));
  const chosenTemplate = selectedGeneratorTemplate("music", loadGeneratorTemplateSettings(), activeGeneratorId);
  const template = templates.find((item) => item.id === chosenTemplate.id) ?? templates[0];
  const [view, setView] = useState<"grid" | "list">(() => localStorage.getItem("slopus.music.view") === "list" ? "list" : "grid");
  const inspector = usePaneSize("music.inspector", 340, { min: 280, max: 560 });
  const menu = useContextMenu();
  useEffect(() => { if (!active) menu.close(); }, [active, menu.close]);
  const [error, setError] = useState<string | null>(null);
  const [generationCount, setGenerationCount] = useState("1");
  const [generationOptionsOpen, setGenerationOptionsOpen] = useState(false);
  const [generationOptionsWidth, setGenerationOptionsWidth] = useState<number>();
  const generationControls = useRef<HTMLDivElement>(null);
  const generationOptionsButton = useRef<HTMLButtonElement>(null);
  const generationCountInput = useRef<HTMLInputElement>(null);
  const count = Number(generationCount);
  const validCount = Number.isInteger(count) && count >= 1 && count <= MAX_MUSIC_BATCH_COUNT;
  const activeWork = selected && workItems.find((item) => item.sceneId === selected.id && isWorkActive(item));
  const busy = Boolean(activeWork);
  useEffect(() => { setGenerationOptionsOpen(false); }, [active, selected?.id, busy]);
  const templateReady = (candidate: GeneratorTemplate) => templateUsable(candidate) && generatorPathFields(candidate).every((field) => !field.required || candidate.paths[field.id].trim() || candidate.sources?.[field.id]?.length);
  const ready = template && templateReady(template);
  const generateDisabled = !ready || !isTauri() || !selected?.style.trim();
  const generatorStatus = !isTauri()
    ? { state: "demo", label: "Music generator unavailable", detail: "Generate music in the desktop app." }
    : !ready ? { state: "modelsMissing", label: "Model weights missing", detail: "Download or select music model weights in Settings → Generators." }
      : { state: "ready", label: "Music generator ready", detail: template.name };
  const edit = (patch: Partial<MusicSong>) => {
    if (!selected || busy || Object.entries(patch).every(([key, value]) => JSON.stringify(value) === JSON.stringify(selected[key as keyof MusicSong]))) return;
    if (selected.assetId || selected.generation || selected.status === "completed") {
      const draft = { ...newSongVariation(selected, config.musicSongs ?? []), ...patch };
      onChange((current) => ({ ...current, musicSongs: current.musicSongs?.flatMap((song) => song.id === selected.id ? [song, draft] : [song]) }), `music:${draft.id}`);
      setSelectedId(draft.id);
    } else {
      onChange((current) => updateSong(current, selected.id, patch), `music:${selected.id}`);
    }
  };
  const setting = (patch: Partial<MusicSettings>) => { if (selected) edit({ settings: { ...selected.settings, ...patch } }); };
  const add = () => {
    const song = newSong(template?.id, Math.min(100, template?.defaultSteps ?? YUE2_DEFAULT_STEPS), currentFolderId);
    if (template) song.settings = musicSettingsForModel(song.settings, template.modelType);
    onChange((current) => ({ ...current, musicSongs: [...(current.musicSongs ?? []), song] }));
    setSelectedId(song.id); setError(null);
  };
  const beginFolder = () => { setCreatingFolder(true); setFolderName(""); };
  const newItems = (): MenuEntry[] => [
    { label: "New song", icon: <Add16 />, onSelect: add },
    { label: "New folder", icon: <FolderAdd16 />, onSelect: beginFolder },
  ];
  const folderContext = (event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>, id: string | null) => {
    const folder = folders.find((item) => item.id === id);
    menu.open(event, [...newItems(), ...(folder ? [{ separator: true } as const,
      { label: "Remove folder", icon: <Delete16 />, danger: true, onSelect: () => {
        onChange((current) => removeMusicFolder(current, folder.id));
        if (currentFolderId === folder.id) setFolderId(folder.parentId ?? null);
      } }] : [])], { "aria-label": folder ? `${folder.name} actions` : "Music actions" });
  };
  const createFolder = (event: React.FormEvent) => {
    event.preventDefault();
    try {
      const folder = newMusicFolder(folderName, currentFolderId);
      onChange((current) => ({ ...current, musicFolders: [...(current.musicFolders ?? []), folder] }));
      setFolderName(""); setCreatingFolder(false); setFolderId(folder.id); setError(null);
    } catch { setError("Enter a folder name of 1 to 200 characters."); }
  };
  const dropSong = (event: React.DragEvent, destination: string | null) => {
    event.preventDefault(); setDropFolder(undefined);
    try {
      const data = JSON.parse(event.dataTransfer.getData(SONG_DRAG_TYPE));
      if (data.projectId !== config.id || !config.musicSongs?.some((song) => song.id === data.songId)) return;
      onChange((current) => moveSong(current, data.songId, destination));
    } catch { /* Ignore unrelated or malformed drag data. */ }
  };
  const browseFolder = (id: string | null) => {
    setFolderId(id); setSelectedId(undefined); setCreatingFolder(false); setFolderName("");
  };
  const folderDropProps = (id: string | null) => ({
    "data-drop-target": dropFolder === (id ?? "") ? "true" : undefined,
    onDragOver: (event: React.DragEvent<HTMLElement>) => {
      if (event.dataTransfer.types.includes(SONG_DRAG_TYPE)) { event.preventDefault(); event.dataTransfer.dropEffect = "move"; setDropFolder(id ?? ""); }
    },
    onDragLeave: () => setDropFolder(undefined),
    onDrop: (event: React.DragEvent<HTMLElement>) => dropSong(event, id),
  });
  const chooseGenerator = (id: string) => {
    const next = templates.find((item) => item.id === id);
    saveActiveGeneratorId("music", id);
    if (next && selected) edit({ generatorId: id, steps: Math.min(100, next.defaultSteps), settings: musicSettingsForModel(selected.settings, next.modelType) });
  };
  const generate = () => {
    if (!selected || !template || !validCount) return;
    setError(null);
    try {
      const id = onGenerate(selected.id, template, count);
      setGenerationCount("1"); setGenerationOptionsOpen(false);
      if (id) setSelectedId(id);
    } catch (reason) { setError(String(reason)); }
  };

  const audio = useRef<HTMLAudioElement>(null);
  const [playerId, setPlayerId] = useState<string | null>(null);
  const [playlist, setPlaylist] = useState<string[]>([]);
  const playerAsset = config.assets.find((asset) => asset.id === playerId);
  const playerSong = playerAsset && config.musicSongs?.find((song) => song.assetId === playerAsset.id);
  const [url, setUrl] = useState<string | null>(null);
  const [playing, setPlaying] = useState(false);
  const [position, setPosition] = useState(0);
  const [length, setLength] = useState(0);
  const [volume, setVolume] = useState(0.8);
  const playWhenReady = useRef(false);
  const interrupted = useRef(false);
  useEffect(() => { onPlayingChange?.(playing); }, [playing, onPlayingChange]);
  useEffect(() => {
    const media = audio.current;
    const pause = () => {
      interrupted.current = true;
      playWhenReady.current = false;
      media?.pause();
      setPlaying(false);
    };
    window.addEventListener(PREVIEW_PLAYBACK_STARTED, pause);
    return () => {
      window.removeEventListener(PREVIEW_PLAYBACK_STARTED, pause);
      interrupted.current = true;
      playWhenReady.current = false;
      media?.pause();
    };
  }, []);
  const [playbackError, setPlaybackError] = useState<string | null>(null);
  const visiblePlayable = visibleSongs.flatMap((song) => {
    const asset = config.assets.find((item) => item.id === song.assetId && item.kind === "audio" && item.relativePath);
    return asset ? [{ song, asset }] : [];
  });
  // Capture the folder's order when playback starts. Browsing other folders
  // does not change what plays next; drafts and unavailable assets are skipped.
  const playable = playlist.length ? playlist.flatMap((id) => {
    if (!config.musicSongs?.some((song) => song.assetId === id)) return [];
    const asset = config.assets.find((item) => item.id === id && item.kind === "audio" && item.relativePath);
    return asset ? [{ asset }] : [];
  }) : visiblePlayable;
  useEffect(() => {
    let disposed = false, owned: string | null = null;
    audio.current?.pause(); setPlaying(false); setPosition(0); setLength(0); setUrl(null); setPlaybackError(null);
    if (playerAsset) void readMediaFileUrl(folderPath, playerAsset, "audio/wav").then((value) => {
      if (disposed) { if (value) URL.revokeObjectURL(value); return; }
      owned = value; setUrl(value);
      if (!value) { playWhenReady.current = false; setPlaybackError("This song's audio file is unavailable."); }
    }).catch((reason) => { if (!disposed) { playWhenReady.current = false; setPlaybackError(String(reason)); } });
    return () => { disposed = true; if (owned) URL.revokeObjectURL(owned); };
  }, [folderPath, playerAsset?.id, playerAsset?.relativePath]);
  useEffect(() => { if (audio.current) audio.current.volume = volume; }, [volume, url]);
  const play = async () => {
    try { await audio.current?.play(); } catch (reason) { if (!interrupted.current) { setPlaybackError(`Could not play song: ${String(reason)}`); setPlaying(false); } }
  };
  const playAsset = (asset: ProjectAsset) => {
    interrupted.current = false;
    setPlaybackError(null);
    if (asset.id === playerId && url) void play();
    else { playWhenReady.current = true; setPlayerId(asset.id); }
  };
  const startSong = (asset: ProjectAsset) => {
    setPlaylist(visiblePlayable.map((item) => item.asset.id));
    playAsset(asset);
  };
  const move = (direction: number) => {
    if (!playable.length) return;
    if (!playlist.length) setPlaylist(visiblePlayable.map((item) => item.asset.id));
    const index = playable.findIndex(({ asset }) => asset.id === playerId);
    playAsset(playable[(Math.max(0, index) + direction + playable.length) % playable.length].asset);
  };
  const toggle = () => {
    if (playing) { audio.current?.pause(); return; }
    const asset = playerAsset ?? visiblePlayable.find(({ song }) => song.id === selected?.id)?.asset ?? visiblePlayable[0]?.asset;
    if (asset) { if (playerAsset) playAsset(asset); else startSong(asset); }
  };
  const context = (event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>, song: MusicSong) => {
    const asset = config.assets.find((item) => item.id === song.assetId);
    menu.open(event, [...newItems(), { separator: true },
      { label: "Duplicate", icon: <Copy16 />, onSelect: () => {
        const draft = duplicateSong(song);
        onChange((current) => ({ ...current, musicSongs: current.musicSongs?.flatMap((item) => item.id === song.id ? [item, draft] : [item]) }));
        if (templates.some((item) => item.id === draft.generatorId)) saveActiveGeneratorId("music", draft.generatorId);
        setSelectedId(draft.id); setError(null);
      } },
      { label: song.coverRelativePath ? "Regenerate album cover" : "Generate album cover", icon: <Sparkle16 />,
        disabled: !isTauri() || !onGenerateCover || workItems.some((item) => item.sceneId === song.id && isWorkActive(item)),
        onSelect: () => { setError(null); try { onGenerateCover?.(song.id); } catch (reason) { setError(String(reason)); } } },
      { label: "Create Reference", icon: <References16 />, disabled: !asset?.relativePath || (asset.durationMs ?? 0) < 2000,
      onSelect: () => {
        try {
          const reference = songReference(song, asset!);
          onChange((current) => ({ ...current, references: [...current.references, reference] }));
        } catch (reason) { setError(String(reason)); }
      } }, { separator: true }, { label: "Remove song", icon: <Delete16 />, danger: true,
        disabled: workItems.some((item) => item.sceneId === song.id && isWorkActive(item)),
        onSelect: () => {
          onChange((current) => ({ ...current, musicSongs: current.musicSongs?.filter((item) => item.id !== song.id) }));
          if (selectedId === song.id) setSelectedId(undefined);
          if (song.assetId && playerId === song.assetId) { playWhenReady.current = false; audio.current?.pause(); setPlayerId(null); }
          setPlaylist((current) => current.filter((id) => id !== song.assetId));
        } }], { "aria-label": `${song.title} actions` });
  };

  return <div className="music-view" style={inspector.style}>
    <div className="music-toolbar" role="toolbar" aria-label="Music tools">
      <GeneratorSelector aria-label="Generator" data-tooltip={template?.name ?? "Generator"} status={generatorStatus}
        value={template?.id ?? ""} placeholder="No music generators" disabled={busy || !templates.length}
        options={generatorOptions(templates, (item) => ({ disabled: !templateReady(item), description: !templateReady(item) ? "Download in Settings" : undefined }))}
        onChange={chooseGenerator} />
      <CommandBarButton icon={<Add16 />} label="New song" showLabel onClick={add} />
      <CommandBarButton icon={<FolderAdd16 />} label="New folder" showLabel onClick={beginFolder} />
      <div className="music-toolbar__views" role="group" aria-label="Song view">
        {(["grid", "list"] as const).map((mode) => <button type="button" className="icon-button" key={mode} aria-label={`${mode === "grid" ? "Grid" : "List"} view`} aria-pressed={view === mode}
          onClick={() => { setView(mode); localStorage.setItem("slopus.music.view", mode); }}>{mode === "grid" ? <GridView16 /> : <ListView16 />}</button>)}
      </div>
    </div>
    <div className="music-body">
      <div className="music-library" tabIndex={0} aria-label="Music library"
        onClick={(event) => { if (!(event.target as Element).closest(".music-song, button, form")) setSelectedId(null); }}
        onContextMenu={(event) => { if (!(event.target as HTMLElement).closest("input, textarea")) folderContext(event, currentFolderId); }}
        onKeyDown={(event) => { if (event.target === event.currentTarget && (event.key === "ContextMenu" || event.shiftKey && event.key === "F10")) folderContext(event, currentFolderId); }}>
        <div className="music-library__heading">
          <nav aria-label="Music path"><h2 className="music-path">
            {breadcrumbs.map((folder, index) => <span key={folder.id ?? "root"}>
              {index > 0 && <span className="music-path__separator"> / </span>}
              <button type="button" aria-current={currentFolderId === folder.id ? "page" : undefined}
                onClick={() => browseFolder(folder.id)} {...folderDropProps(folder.id)}
                onContextMenu={(event) => folderContext(event, folder.id)}
                onKeyDown={(event) => { if (event.key === "ContextMenu" || event.shiftKey && event.key === "F10") folderContext(event, folder.id); }}>{folder.name}</button>
            </span>)}
          </h2></nav>
          <span>{visibleFolders.length > 0 && `${visibleFolders.length} folders · `}{visibleSongs.length} songs</span>
        </div>
        {creatingFolder && <form className="music-new-folder" onSubmit={createFolder}>
          <input className="text-field" aria-label="Folder name" placeholder="Folder name" autoFocus maxLength={200} value={folderName} onChange={(event) => setFolderName(event.target.value)} />
          <button type="submit" className="primary-button" disabled={!folderName.trim()}>Create folder</button>
          <button type="button" className="secondary-button" onClick={() => setCreatingFolder(false)}>Cancel</button>
        </form>}
        {!visibleSongs.length && !visibleFolders.length ? <div className="music-empty"><Audio16 /><h3>{currentFolder ? "This folder is empty" : "Create your first song"}</h3><p>Add a song, describe its style and write lyrics, or leave lyrics empty for an instrumental.</p><button type="button" className="primary-button" onClick={add}><Add16 />New song</button></div> :
          <div className={`music-songs music-songs--${view}`} role="list" aria-label="Music contents">
            {visibleFolders.map((folder) => <div className="music-song music-folder" role="listitem" key={`folder:${folder.id}`}
              {...folderDropProps(folder.id)} onContextMenu={(event) => folderContext(event, folder.id)}>
              <button type="button" className="music-song__select" aria-label={`Open ${folder.name}`} onClick={() => browseFolder(folder.id)}
                onKeyDown={(event) => { if (event.key === "ContextMenu" || event.shiftKey && event.key === "F10") folderContext(event, folder.id); }}>
                <span className="music-song__art" aria-hidden="true"><FolderOpen16 /></span>
                <span className="music-song__info"><strong>{folder.name}</strong></span>
              </button>
            </div>)}
            {visibleSongs.map((song, index) => {
              const asset = config.assets.find((item) => item.id === song.assetId && item.kind === "audio" && item.relativePath);
              const work = workItems.find((item) => item.sceneId === song.id && isWorkActive(item));
              const generated = Boolean(song.assetId || song.generation || song.status === "completed");
              const state = asset ? "playable" : generated ? "unavailable" : "draft";
              return <div className={`music-song music-song--${state}${selected?.id === song.id ? " music-song--selected" : ""}`} role="listitem" key={`song:${song.id}`}
                draggable onDragStart={(event) => { event.dataTransfer.effectAllowed = "move"; event.dataTransfer.setData(SONG_DRAG_TYPE, JSON.stringify({ projectId: config.id, songId: song.id })); }} onDragEnd={() => setDropFolder(undefined)}
                onContextMenu={(event) => context(event, song)}>
                <button type="button" className="music-song__select" aria-label={`Select ${song.title}`} aria-pressed={selected?.id === song.id} aria-describedby={`music-status-${song.id}`}
                  onClick={() => setSelectedId(song.id)} onDoubleClick={() => { if (asset) startSong(asset); }}
                  onKeyDown={(event) => { if (event.key === "ContextMenu" || event.shiftKey && event.key === "F10") context(event, song); }}>
                  <span className="music-song__art" aria-hidden="true">{song.coverRelativePath
                    ? <ReferenceImage folderPath={folderPath} relativePath={song.coverRelativePath} alt="" />
                    : <>{generated ? <Audio16 /> : <Edit16 />}<span>{String(index + 1).padStart(2, "0")}</span></>}</span>
                  <span className="music-song__info"><strong>{song.title}</strong><span>{song.style || "Add a style to get started"}</span></span>
                  <span className="music-song__status" id={`music-status-${song.id}`}>{work ? `${work.detail} · ${Math.round(work.progress * 100)}%` : asset ? `Ready · ${time((asset.durationMs ?? 0) / 1000)}` : generated ? "Audio unavailable" : song.status === "draft" ? "Draft · Not generated" : song.status}</span>
                </button>
                {work ? <span className="music-song__play music-song__progress"><ProgressRing size={20} value={work.status !== "queued" && work.progress > 0 ? work.progress * 100 : undefined} aria-label={`${song.title} generation progress`} /></span>
                  : asset && <button type="button" className="icon-button music-song__play" aria-label={`Play ${song.title}`} onClick={() => startSong(asset)}><Play16 /></button>}
              </div>;
            })}
          </div>}
      </div>
      <Splitter {...inspector.splitterProps} reverse aria-label="Resize music inspector" aria-controls="music-inspector" />
      <aside id="music-inspector" className="music-inspector" aria-label="Song settings">
        {selected ? <>
          <div className="music-inspector__fields">
            <PropSection title="Song">
              <PropRow label="Title"><input className="text-field" aria-label="Song title" value={selected.title} maxLength={200} disabled={busy} onChange={(event) => edit({ title: event.target.value || "New song" })} /></PropRow>
              <label className="music-prompt">Style<textarea className="text-field" aria-label="Music style" value={selected.style} maxLength={10000} rows={3} disabled={busy} placeholder="Genre, mood, instruments and vocals…" onChange={(event) => edit({ style: event.target.value })} /></label>
              <label className="music-prompt">Lyrics<textarea className="text-field music-lyrics" aria-label="Music lyrics" value={selected.settings.lyrics} maxLength={30000} rows={15} disabled={busy} placeholder="[Verse]… Leave empty for an instrumental." onChange={(event) => setting({ lyrics: event.target.value })} /></label>
            </PropSection>
            <PropSection title="Generation" persistKey="music.generation">
              <PropRow label="Duration"><SliderNumberInput aria-label="Song duration" minimum={2} maximum={300} step={1} suffix="s" value={selected.settings.duration} disabled={busy} onCommit={(duration) => setting({ duration })} /></PropRow>
              <PropRow label="Steps"><SliderNumberInput aria-label="Music steps" {...GENERATION_STEPS_RANGE} value={selected.steps} disabled={busy} onCommit={(steps) => edit({ steps })} /></PropRow>
              <PropRow label="Seed" tooltip="Use -1 for a random seed."><input className="text-field" aria-label="Music seed" type="number" min={-1} max={Number.MAX_SAFE_INTEGER} step={1} value={selected.seed} disabled={busy} onChange={(event) => { const seed = event.target.valueAsNumber; if (Number.isSafeInteger(seed) && seed >= -1) edit({ seed }); }} /></PropRow>
            </PropSection>
            <PropSection title="Advanced" persistKey="music.advanced" defaultOpen={false}>
              {template?.modelType === "yue2" && <PropRow label="Flow sampler"><ComboBox aria-label="Music flow sampler" value={musicSettingsForModel(selected.settings, "yue2").flow_sampler} disabled={busy}
                options={[{ value: "dpmpp_2m", label: "DPM++ 2M" }, { value: "midpoint", label: "Midpoint" }]}
                onChange={(flow_sampler) => setting({ flow_sampler: flow_sampler as MusicSettings["flow_sampler"] })} /></PropRow>}
              {template?.modelType === "yue2" && <PropRow label="Planning"><ComboBox aria-label="Music planning" value={selected.settings.planning} disabled={busy} options={[{ value: "full", label: "Full (melody and chords)" }, { value: "melody", label: "Melody" }, { value: "off", label: "Off" }]} onChange={(planning) => setting({ planning: planning as MusicSettings["planning"], ...(planning === "off" ? { abc: "" } : {}) })} /></PropRow>}
              {([
                ["top_k", "Top K", 0, 1024, 1], ["semantic_guidance", "Guidance", 0, 5, 1],
                ...(template?.modelType === "yue2" ? [["temperature", "Temperature", 0, 2, 0.05], ["top_p", "Top P", 0.05, 1, 0.05], ["repetition_penalty", "Repetition penalty", 1, 2, 0.05]] : [["flow_guidance", "Flow guidance", 0, 5, 0.1]]),
              ] as [keyof MusicSettings, string, number, number, number][]).map(([key, label, minimum, maximum, step]) => <PropRow key={key} label={label} tooltip={key === "top_k" || key.includes("guidance") ? "Zero uses the model default." : undefined}>
                <SliderNumberInput aria-label={label} minimum={minimum} maximum={maximum} step={step} integer={key === "top_k"} value={Number(selected.settings[key])} disabled={busy} onCommit={(value) => setting({ [key]: key === "semantic_guidance" && value > 0 && value < 1 ? 1 : value })} /></PropRow>)}
              {template?.modelType === "yue2" && selected.settings.planning !== "off" && <>
                <PropRow label="Planning tokens"><SliderNumberInput aria-label="Planning tokens" minimum={32} maximum={4096} step={32} integer value={selected.settings.abc_max_tokens} disabled={busy} onCommit={(abc_max_tokens) => setting({ abc_max_tokens })} /></PropRow>
                <label className="music-prompt">ABC (optional)<textarea className="text-field" aria-label="Music ABC" value={selected.settings.abc} maxLength={30000} rows={3} disabled={busy} onChange={(event) => setting({ abc: event.target.value })} /></label>
              </>}
            </PropSection>
          </div>
          <div className="inspector-actions">
            {activeWork ? <button type="button" className="primary-button" disabled={activeWork.cancelling || activeWork.status === "encoding"} onClick={() => onCancel(activeWork.id)}><Stop14 aria-hidden="true" />{activeWork.cancelling ? "Cancelling…" : "Cancel"}</button> :
              <div ref={generationControls} className="image-generation-controls image-generation-controls--split">
                <button type="button" className="primary-button image-generate-button" data-tooltip={generatorStatus.state !== "ready" ? generatorStatus.detail : undefined}
                  disabled={generateDisabled || !validCount} onClick={generate}><Sparkle16 aria-hidden="true" />{count > 1 && validCount ? `Generate (${count})` : "Generate"}</button>
                <button ref={generationOptionsButton} type="button" className="primary-button image-generation-options" aria-label="Generation options"
                  aria-haspopup="dialog" aria-expanded={generationOptionsOpen} data-tooltip="Generation options" disabled={generateDisabled}
                  onClick={() => {
                    setGenerationOptionsWidth(generationControls.current?.getBoundingClientRect().width);
                    setGenerationOptionsOpen(!generationOptionsOpen);
                  }}><ChevronDown14 aria-hidden="true" /></button>
                <Flyout open={generationOptionsOpen && active} anchor={generationOptionsButton} initialFocus={generationCountInput} onClose={() => setGenerationOptionsOpen(false)}
                  placement="top-end" width={generationOptionsWidth} aria-label="Generation options" className="image-generation-options-panel">
                  <PropRow label="Count" tooltip="Different seeds for each song. Resets after generating." htmlFor="music-generation-count">
                    <input ref={generationCountInput} id="music-generation-count" className="text-field" type="number" min={1} max={MAX_MUSIC_BATCH_COUNT} step={1}
                      value={generationCount} aria-invalid={!validCount} aria-describedby={!validCount ? "music-generation-count-help" : undefined} onChange={(event) => setGenerationCount(event.target.value)} />
                  </PropRow>
                  <p hidden={validCount} id="music-generation-count-help">{`Enter a whole number from 1 to ${MAX_MUSIC_BATCH_COUNT}.`}</p>
                </Flyout>
              </div>}
          </div>
        </> : <p className="music-inspector__empty">{visibleSongs.length ? "Select a song to edit its lyrics, style and generation settings." : "Add a song to edit its lyrics, style and generation settings."}</p>}
      </aside>
      {error && <InfoBar className="music-error" severity="error" title="Couldn’t update music" message={error} onClose={() => setError(null)} />}
      {(activeWork || selected?.error) && <div className="music-generation-status" role={selected?.error ? "alert" : "status"}>
        {activeWork && <><ProgressBar value={activeWork.progress * 100} aria-label="Music generation progress" /><span>{activeWork.detail}</span></>}
        {selected?.error && <span>{selected.error}</span>}
      </div>}
      <div className="music-player" aria-label="Music player">
        <MusicPlaybackControls title={playerAsset?.name} folderPath={folderPath} coverRelativePath={playerSong?.coverRelativePath} playing={playing} position={position} duration={length} volume={volume}
          canPlay={Boolean(playable.length || playerAsset)} canNavigate={Boolean(playable.length)} canSeek={Boolean(url && length)}
          onToggle={toggle} onPrevious={() => move(-1)} onNext={() => move(1)} onVolume={setVolume}
          onSeek={(value) => { if (audio.current) audio.current.currentTime = value; setPosition(value); }} />
        {playbackError && <p className="music-player__error" role="alert">{playbackError}</p>}
        <audio ref={audio} src={url ?? undefined} preload="metadata" onPlay={() => { if (interrupted.current) audio.current?.pause(); else setPlaying(true); }} onPause={() => setPlaying(false)}
          onLoadedMetadata={(event) => { const duration = event.currentTarget.duration; setLength(Number.isFinite(duration) ? duration : 0); }}
          onCanPlay={() => { if (playWhenReady.current) { playWhenReady.current = false; void play(); } }}
          onTimeUpdate={(event) => setPosition(event.currentTarget.currentTime)} onEnded={() => { setPlaying(false); if (interrupted.current) return; const index = playable.findIndex(({ asset }) => asset.id === playerId); if (index >= 0 && index + 1 < playable.length) playAsset(playable[index + 1].asset); }}
          onError={() => { if (url) { playWhenReady.current = false; setPlaying(false); setPlaybackError("This song could not be played."); } }} />
      </div>
    </div>
    {active && <ProjectStatus label="Music status">
      <span>{config.musicSongs?.length ?? 0} songs</span>
      {generatorStatus.state !== "ready" && <span className="generator-status__runtime">{generatorStatus.detail}</span>}
    </ProjectStatus>}
    {active && menu.element}
  </div>;
}