Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Share music playback controls with Export

Bitti8ddc72deffa9
diff --git a/src/components/workspace/MusicExportView.test.tsx b/src/components/workspace/MusicExportView.test.tsx
index 8a92941..b72e829 100644
--- a/src/components/workspace/MusicExportView.test.tsx
+++ b/src/components/workspace/MusicExportView.test.tsx
@@ -90,3 +90,39 @@ it("has an empty state for drafts and stops preview playback on unmount", async
expect(audio.pause).toHaveBeenCalled();
expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:music");
});
+
+it("plays, seeks, adjusts volume and advances songs with the shared music controls", async () => {
+ vi.mocked(readMediaFileUrl).mockImplementation(async (_, asset) => `blob:${asset.relativePath}`);
+ vi.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(function (this: HTMLMediaElement) { this.dispatchEvent(new Event("play")); return Promise.resolve(); });
+ vi.mocked(HTMLMediaElement.prototype.pause).mockImplementation(function (this: HTMLMediaElement) { this.dispatchEvent(new Event("pause")); });
+ render(<MusicExportView config={config()} folderPath="D:/Music" />);
+ const audio = screen.getByLabelText("Music export playback") as HTMLAudioElement;
+ await waitFor(() => expect(audio).toHaveAttribute("src", "blob:media/audio.wav"));
+ expect(audio).not.toHaveAttribute("controls");
+ Object.defineProperty(audio, "duration", { configurable: true, value: 30 });
+ fireEvent.loadedMetadata(audio);
+ fireEvent.click(screen.getByRole("button", { name: "Play music" }));
+ expect(screen.getByRole("button", { name: "Pause music" })).toBeInTheDocument();
+ fireEvent.change(screen.getByRole("slider", { name: "Song playback position" }), { target: { value: "12" } });
+ expect(audio.currentTime).toBe(12);
+ fireEvent.change(screen.getByRole("slider", { name: "Music volume" }), { target: { value: "0.4" } });
+ expect(audio.volume).toBe(0.4);
+ fireEvent.click(screen.getByRole("button", { name: "Pause music" }));
+ expect(screen.getByRole("button", { name: "Play music" })).toBeInTheDocument();
+ fireEvent.click(screen.getByRole("button", { name: "Next song" }));
+ await waitFor(() => expect(audio).toHaveAttribute("src", "blob:media/second.wav"));
+ fireEvent.canPlay(audio);
+ expect(screen.getByRole("button", { name: "Export song Morning" })).toHaveAttribute("aria-pressed", "true");
+ expect(screen.getByRole("button", { name: "Pause music" })).toBeInTheDocument();
+ expect(audio.volume).toBe(0.4);
+ fireEvent.click(screen.getByRole("button", { name: "Previous song" }));
+ await waitFor(() => expect(audio).toHaveAttribute("src", "blob:media/audio.wav"));
+ fireEvent.canPlay(audio);
+ fireEvent.ended(audio);
+ await waitFor(() => expect(audio).toHaveAttribute("src", "blob:media/second.wav"));
+ fireEvent.canPlay(audio);
+ vi.mocked(audio.play).mockClear();
+ fireEvent.ended(audio);
+ expect(audio.play).not.toHaveBeenCalled();
+ expect(screen.getByRole("button", { name: "Play music" })).toBeInTheDocument();
+});
diff --git a/src/components/workspace/MusicExportView.tsx b/src/components/workspace/MusicExportView.tsx
index 85a339a..85c1e6a 100644
--- a/src/components/workspace/MusicExportView.tsx
+++ b/src/components/workspace/MusicExportView.tsx
@@ -6,6 +6,7 @@ import { MUSIC_EXPORT_BITRATES, MUSIC_EXPORT_FORMATS, musicExportSongs, musicExp
import { cancelExportJob, dismissExportOutcome, exportFraction, isExportActive, startMusicExportJob, useExportJob } from "../../lib/exportJob";
import { ComboBox, EmptyState, InfoBar, ProgressBar, PropRow, PropSection, Splitter, usePaneSize } from "../ui";
import { Audio16 } from "../ui/icons";
+import { MusicPlaybackControls } from "./MusicPlaybackControls";
const STORAGE_KEY = "slopus.music-export.v1";
function loadPreferences(): MusicExportSettings {
@@ -27,6 +28,11 @@ export function MusicExportView({ config, folderPath }: { config: ProjectConfig;
const [error, setError] = useState<string | null>(null);
const [url, setUrl] = useState<string | null>(null);
const audio = useRef<HTMLAudioElement>(null);
+ const playWhenReady = useRef(false);
+ const [playing, setPlaying] = useState(false);
+ const [position, setPosition] = useState(0);
+ const [duration, setDuration] = useState(0);
+ const [volume, setVolume] = useState(0.8);
const job = useExportJob(folderPath, "music");
const active = isExportActive(job);
const choose = (patch: Partial<MusicExportSettings>) => {
@@ -42,14 +48,31 @@ export function MusicExportView({ config, folderPath }: { config: ProjectConfig;
useEffect(() => {
let disposed = false, owned: string | null = null;
const player = audio.current;
- player?.pause(); setUrl(null); setError(null);
+ player?.pause(); setPlaying(false); setPosition(0); setDuration(0); setUrl(null); setError(null);
if (selected) void readMediaFileUrl(folderPath, selected.asset, selected.asset.mimeType ?? "audio/wav").then((value) => {
if (disposed) { if (value) URL.revokeObjectURL(value); return; }
owned = value; setUrl(value);
- if (!value) setError("This song's audio file is unavailable.");
- }).catch((reason) => { if (!disposed) setError(String(reason)); });
+ if (!value) { playWhenReady.current = false; setError("This song's audio file is unavailable."); }
+ }).catch((reason) => { if (!disposed) { playWhenReady.current = false; setError(String(reason)); } });
return () => { disposed = true; player?.pause(); if (owned) URL.revokeObjectURL(owned); };
}, [folderPath, selected?.asset.id, selected?.asset.relativePath]);
+ useEffect(() => { if (audio.current) audio.current.volume = volume; }, [volume, url]);
+ const play = async () => {
+ const player = audio.current;
+ const source = player?.src;
+ try { await player?.play(); }
+ catch (reason) {
+ if (player && audio.current === player && player.src === source) { setPlaying(false); setError(`Could not play song: ${String(reason)}`); }
+ }
+ };
+ const move = (direction: number, wrap = true) => {
+ const index = songs.findIndex(({ song }) => song.id === selected?.song.id);
+ if (index < 0 || (!wrap && index + direction >= songs.length)) return;
+ const next = songs[(index + direction + songs.length) % songs.length];
+ if (next.song.id === selected?.song.id) {
+ if (audio.current && url) { audio.current.currentTime = 0; setPosition(0); void play(); }
+ } else { playWhenReady.current = true; setSelectedId(next.song.id); }
+ };
const blocker = !selected ? "Generate a song in Music before exporting."
: !isTauri() ? "Exporting is available in the Slopus desktop app."
: support[settings.format] === undefined ? "Checking available audio formats…"
@@ -72,7 +95,7 @@ export function MusicExportView({ config, folderPath }: { config: ProjectConfig;
<div className="music-export-library">
{songs.length ? <div className="music-songs music-songs--list" role="list" aria-label="Songs to export">
{songs.map(({ song, asset, label }) => <div role="listitem" key={song.id} className={`music-song music-song--playable${selected?.song.id === song.id ? " music-song--selected" : ""}`}>
- <button type="button" className="music-song__select" aria-label={`Export song ${label}`} aria-pressed={selected?.song.id === song.id} onClick={() => setSelectedId(song.id)}>
+ <button type="button" className="music-song__select" aria-label={`Export song ${label}`} aria-pressed={selected?.song.id === song.id} onClick={() => { playWhenReady.current = false; setSelectedId(song.id); }}>
<span className="music-song__art" aria-hidden="true"><Audio16 /></span>
<span className="music-song__info"><strong>{label}</strong><span>{song.style}</span></span>
<span className="music-song__status">{((asset.durationMs ?? 0) / 1000).toFixed(1)} s</span>
@@ -80,7 +103,20 @@ export function MusicExportView({ config, folderPath }: { config: ProjectConfig;
</div>)}
</div> : <EmptyState icon={<Audio16 />} title="No music to export yet" description="Generate a song in the Music tab first." />}
</div>
- {selected && <div className="music-export-player"><span>{selected.song.title}</span><audio ref={audio} aria-label="Music export playback" controls src={url ?? undefined} preload="metadata" onError={() => { if (url) setError("This song could not be played."); }} /></div>}
+ {selected && <div className="music-player" aria-label="Music player">
+ <MusicPlaybackControls title={selected.song.title} playing={playing} position={position} duration={duration} volume={volume}
+ canPlay={Boolean(url)} canNavigate={Boolean(songs.length)} canSeek={Boolean(url && duration)}
+ onToggle={() => { if (playing) audio.current?.pause(); else void play(); }}
+ onPrevious={() => move(-1)} onNext={() => move(1)} onVolume={setVolume}
+ onSeek={(value) => { if (audio.current) audio.current.currentTime = value; setPosition(value); }} />
+ <audio ref={audio} aria-label="Music export playback" src={url ?? undefined} preload="metadata"
+ onPlay={() => setPlaying(true)} onPause={() => setPlaying(false)}
+ onLoadedMetadata={(event) => { const length = event.currentTarget.duration; setDuration(Number.isFinite(length) ? length : 0); }}
+ onCanPlay={() => { if (playWhenReady.current) { playWhenReady.current = false; void play(); } }}
+ onTimeUpdate={(event) => setPosition(event.currentTarget.currentTime)}
+ onEnded={() => { setPlaying(false); move(1, false); }}
+ onError={() => { if (url) { playWhenReady.current = false; setPlaying(false); setError("This song could not be played."); } }} />
+ </div>}
</section>
<Splitter {...pane.splitterProps} reverse aria-label="Resize export settings" aria-controls="music-export-settings" />
<section id="music-export-settings" className="export-settings" aria-label="Export settings">
diff --git a/src/components/workspace/MusicPlaybackControls.tsx b/src/components/workspace/MusicPlaybackControls.tsx
new file mode 100644
index 0000000..d34d7af
--- /dev/null
+++ b/src/components/workspace/MusicPlaybackControls.tsx
@@ -0,0 +1,25 @@
+import { Slider } from "../ui";
+import { Audio16, GoToEnd16, GoToStart16, Pause16, Play16, Speaker14 } from "../ui/icons";
+
+const time = (seconds: number) => `${Math.floor(seconds / 60)}:${Math.floor(seconds % 60).toString().padStart(2, "0")}`;
+
+export function MusicPlaybackControls({ title, playing, position, duration, volume, canPlay, canNavigate, canSeek,
+ onToggle, onPrevious, onNext, onSeek, onVolume }: {
+ title?: string; playing: boolean; position: number; duration: number; volume: number;
+ canPlay: boolean; canNavigate: boolean; canSeek: boolean;
+ onToggle: () => void; onPrevious: () => void; onNext: () => void;
+ onSeek: (position: number) => void; onVolume: (volume: number) => void;
+}) {
+ return <>
+ <div className="music-player__now"><Audio16 /><div><strong>{title ?? "No song playing"}</strong><span>{playing ? "Playing" : title ? "Paused" : "Choose a generated song"}</span></div></div>
+ <div className="music-player__transport">
+ <div className="music-player__buttons">
+ <button type="button" className="icon-button" aria-label="Previous song" disabled={!canNavigate} onClick={onPrevious}><GoToStart16 /></button>
+ <button type="button" className="music-player__toggle primary-button" aria-label={playing ? "Pause music" : "Play music"} disabled={!canPlay} onClick={onToggle}>{playing ? <Pause16 /> : <Play16 />}</button>
+ <button type="button" className="icon-button" aria-label="Next song" disabled={!canNavigate} onClick={onNext}><GoToEnd16 /></button>
+ </div>
+ <div className="music-player__seek"><span>{time(position)}</span><Slider aria-label="Song playback position" min={0} max={duration || 1} step={0.1} value={position} disabled={!canSeek} onChange={onSeek} /><span>{time(duration)}</span></div>
+ </div>
+ <div className="music-player__volume"><Speaker14 /><Slider aria-label="Music volume" min={0} max={1} step={0.01} value={volume} onChange={onVolume} /></div>
+ </>;
+}
diff --git a/src/components/workspace/MusicView.tsx b/src/components/workspace/MusicView.tsx
index cfa1fdf..c985023 100644
--- a/src/components/workspace/MusicView.tsx
+++ b/src/components/workspace/MusicView.tsx
@@ -4,8 +4,9 @@ import { isTauri, readMediaFileUrl } from "../../lib/persistence";
import { loadGeneratorTemplateSettings, subscribeGeneratorTemplates, templateSupportsMusic, templateUsable, generatorPathFields, type GeneratorTemplate } from "../../lib/settings";
import { musicSettingsForModel, moveSong, newMusicFolder, newSong, newSongVariation, removeMusicFolder, songReference, updateSong, type MusicSettings, type MusicSong } from "../../lib/music";
import { isWorkActive, type WorkItem } from "../../lib/workQueue";
-import { ComboBox, CommandBarButton, InfoBar, ProgressBar, ProgressRing, PropRow, PropSection, Slider, Splitter, useContextMenu, usePaneSize, type MenuEntry } from "../ui";
-import { Add16, Audio16, Delete16, Edit16, FolderAdd16, FolderOpen16, GoToEnd16, GoToStart16, GridView16, ListView16, Pause16, Play16, References16, Speaker14, Sparkle16, Stop14 } from "../ui/icons";
+import { ComboBox, CommandBarButton, InfoBar, ProgressBar, ProgressRing, PropRow, PropSection, Splitter, useContextMenu, usePaneSize, type MenuEntry } from "../ui";
+import { Add16, Audio16, 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";
@@ -325,12 +326,10 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
{selected?.error && <span>{selected.error}</span>}
</div>}
<div className="music-player" aria-label="Music player">
- <div className="music-player__now"><Audio16 /><div><strong>{playerAsset?.name ?? "No song playing"}</strong><span>{playing ? "Playing" : playerAsset ? "Paused" : "Choose a generated song"}</span></div></div>
- <div className="music-player__transport">
- <div className="music-player__buttons"><button type="button" className="icon-button" aria-label="Previous song" disabled={!playable.length} onClick={() => move(-1)}><GoToStart16 /></button><button type="button" className="music-player__toggle primary-button" aria-label={playing ? "Pause music" : "Play music"} disabled={!playable.length && !playerAsset} onClick={toggle}>{playing ? <Pause16 /> : <Play16 />}</button><button type="button" className="icon-button" aria-label="Next song" disabled={!playable.length} onClick={() => move(1)}><GoToEnd16 /></button></div>
- <div className="music-player__seek"><span>{time(position)}</span><Slider aria-label="Song playback position" min={0} max={length || 1} step={0.1} value={position} disabled={!url || !length} onChange={(value) => { if (audio.current) audio.current.currentTime = value; setPosition(value); }} /><span>{time(length)}</span></div>
- </div>
- <div className="music-player__volume"><Speaker14 /><Slider aria-label="Music volume" min={0} max={1} step={0.01} value={volume} onChange={setVolume} /></div>
+ <MusicPlaybackControls title={playerAsset?.name} 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); }}
diff --git a/src/styles/export.css b/src/styles/export.css
index 30ebf59..37882dd 100644
--- a/src/styles/export.css
+++ b/src/styles/export.css
@@ -54,9 +54,7 @@
}
.music-export-library { flex: 1; min-height: 0; overflow: auto; padding: var(--space-3); }
-.music-export-player { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); padding: var(--space-2) var(--space-3); border-top: 1px solid var(--line); background: var(--surface-pane); }
-.music-export-player > span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
-.music-export-player audio { flex: 2; width: 100%; min-width: 160px; height: 40px; }
+.export-preview > .music-player { flex: none; }
/* The stage is the screen plus its transport bar. It is also the element that
goes fullscreen, so the bar comes along. */