Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix delayed playback of generated audio (#14671)
Lee Jackson03c76c6954b3
diff --git a/studio/frontend/src/features/audio/api.ts b/studio/frontend/src/features/audio/api.tsindex 58d52f28c34..1fd639e63bb 100644--- a/studio/frontend/src/features/audio/api.ts+++ b/studio/frontend/src/features/audio/api.ts@@ -294,11 +294,15 @@ export async function fetchClipBlob(url: string): Promise<Blob> {return response.blob();}-export async function fetchClipObjectUrl(- url: string,-): Promise<{ url: string; bytes: number; blob: Blob }> {- const blob = await fetchClipBlob(url);- return { url: URL.createObjectURL(blob), bytes: blob.size, blob };+/** Let the media element buffer and seek without downloading the entire WAV first. */+export async function fetchGalleryAudioSignedUrl(id: string): Promise<string> {+ const response = await authFetch(+ `/api/inference/audio/gallery/${encodeURIComponent(id)}/signed-url`,+ );+ const body = await parseJson<{ url?: string }>(response);+ if (!body.url) throw new Error("The server returned no audio link.");+ // Media elements bypass authFetch; desktop URLs need the backend origin.+ return apiUrl(body.url);}export async function listTranscripts(diff --git a/studio/frontend/src/features/audio/audio-page.tsx b/studio/frontend/src/features/audio/audio-page.tsxindex ee0c14e52e4..252b370a2f1 100644--- a/studio/frontend/src/features/audio/audio-page.tsx+++ b/studio/frontend/src/features/audio/audio-page.tsx@@ -440,6 +440,7 @@ export function AudioPage({srcById,peaksById,ensureClipSrc,+ retryClipSrc,refreshGallery,loadMore,selectClip,@@ -2333,6 +2334,7 @@ export function AudioPage({clips: visibleClips,selectedClip,selectedClipSrc,+ retryClipSrc,srcById,handleDownloadClip,handleDeleteClip,diff --git a/studio/frontend/src/features/audio/audio-playback-links.ts b/studio/frontend/src/features/audio/audio-playback-links.tsnew file mode 100644index 00000000000..12a95644cd4--- /dev/null+++ b/studio/frontend/src/features/audio/audio-playback-links.ts@@ -0,0 +1,50 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// Under the server's one-hour link lifetime; only a new selection renews, never a playing source.+const LINK_REFRESH_MS = 50 * 60 * 1000;++export class AudioPlaybackLinks {+ private readonly entries = new Map<string, { url: string; mintedAt: number }>();+ private readonly retried = new Set<string>();++ get(id: string): string | undefined {+ return this.entries.get(id)?.url;+ }++ set(id: string, url: string, now = Date.now()): void {+ this.entries.set(id, { url, mintedAt: now });+ }++ isFresh(id: string, now = Date.now()): boolean {+ const entry = this.entries.get(id);+ return entry !== undefined && now - entry.mintedAt < LINK_REFRESH_MS;+ }++ /** Ignore old-source errors and bound retries; a recovered link may itself expire later. */+ retry(id: string, failedUrl: string, now = Date.now()): boolean {+ if (+ this.get(id) !== failedUrl ||+ (this.retried.has(id) && this.isFresh(id, now))+ ) return false;+ this.retried.add(id);+ this.entries.delete(id);+ return true;+ }++ delete(id: string): void {+ this.entries.delete(id);+ this.retried.delete(id);+ }++ clear(): void {+ this.entries.clear();+ this.retried.clear();+ }++ toRecord(): Record<string, string> {+ return Object.fromEntries(+ [...this.entries].map(([id, entry]) => [id, entry.url]),+ );+ }+}diff --git a/studio/frontend/src/features/audio/audio-workspace-constants.ts b/studio/frontend/src/features/audio/audio-workspace-constants.tsindex 75e73b983aa..73328f0969f 100644--- a/studio/frontend/src/features/audio/audio-workspace-constants.ts+++ b/studio/frontend/src/features/audio/audio-workspace-constants.ts@@ -9,4 +9,3 @@ export const RECORDING_MAX_SECONDS = 30 * 60;export const TTS_MAX_TOKENS = 8192;// Max tokens caps output only and the prompt shares the context, so TTS_MAX_TOKENS itself is unreachable.export const TTS_PROMPT_CONTEXT_RESERVE = 2048;-export const CLIP_BLOB_BUDGET_BYTES = 64 * 1024 * 1024;diff --git a/studio/frontend/src/features/audio/hooks/use-audio-gallery.tsx b/studio/frontend/src/features/audio/hooks/use-audio-gallery.tsxindex 7f44ecbb04f..1670ca08987 100644--- a/studio/frontend/src/features/audio/hooks/use-audio-gallery.tsx+++ b/studio/frontend/src/features/audio/hooks/use-audio-gallery.tsx@@ -4,7 +4,6 @@import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { useSettingsDialogStore } from "@/features/settings";import { useStripReorder } from "@/hooks/use-strip-reorder";-import { BlobUrlCache } from "@/lib/blob-url-cache";import { copyToClipboard } from "@/lib/copy-to-clipboard";import { loadGalleryUntil } from "@/lib/gallery-deep-link";import {@@ -26,14 +25,14 @@ import {deleteAudioClip,deleteAudioGroup,fetchClipBlob,- fetchClipObjectUrl,+ fetchGalleryAudioSignedUrl,listAudioGallery,moveAudioClip,setAudioClipFlags,} from "../api";import { fetchGalleryWindow, mergeGalleryPage } from "../audio-page-policy";+import { AudioPlaybackLinks } from "../audio-playback-links";import {- CLIP_BLOB_BUDGET_BYTES,MAX_PAGE_SIZE,PAGE_SIZE,} from "../audio-workspace-constants";@@ -49,14 +48,14 @@ export const galleryCache: {hasMore: boolean;nextCursor: AudioGalleryCursor | null;selectedId: string | null;- srcById: BlobUrlCache;+ srcById: AudioPlaybackLinks;peaksById: Map<string, number[] | null>;} = {clips: [],hasMore: false,nextCursor: null,selectedId: null,- srcById: new BlobUrlCache(CLIP_BLOB_BUDGET_BYTES),+ srcById: new AudioPlaybackLinks(),peaksById: new Map(),};@@ -90,34 +89,49 @@ export function useAudioGallery({ReadonlyMap<string, number[] | null>>(galleryCache.peaksById);const clipSrcLoads = useRef<Map<string, Promise<void>>>(new Map());+ const clipPeaksLoads = useRef<Map<string, Promise<void>>>(new Map());++ const ensureClipPeaks = useCallback(async (clip: AudioGalleryClip) => {+ if (galleryCache.peaksById.has(clip.id)) return;+ const pending = clipPeaksLoads.current.get(clip.id);+ if (pending) return pending;+ const load = (async () => {+ try {+ const blob = await fetchClipBlob(clip.url);+ if (!galleryCache.clips.some((candidate) => candidate.id === clip.id)) return;+ const { peaks } = await decodePeaks(blob);+ if (!galleryCache.clips.some((candidate) => candidate.id === clip.id)) return;+ galleryCache.peaksById.set(clip.id, peaks);+ setPeaksById(new Map(galleryCache.peaksById));+ } catch {+ // Optional decoration.+ }+ })();+ clipPeaksLoads.current.set(clip.id, load);+ try {+ await load;+ } finally {+ if (clipPeaksLoads.current.get(clip.id) === load) {+ clipPeaksLoads.current.delete(clip.id);+ }+ }+ }, []);const ensureClipSrc = useCallback(async (clip: AudioGalleryClip) => {const cached = galleryCache.srcById.get(clip.id);if (cached) {- galleryCache.srcById.touch(clip.id);+ void ensureClipPeaks(clip);return;}const pending = clipSrcLoads.current.get(clip.id);if (pending) return pending;const load = (async () => {try {- const fetched = await fetchClipObjectUrl(clip.url);- // A delete can finish mid-fetch: do not revive its cache entry.- if (!galleryCache.clips.some((candidate) => candidate.id === clip.id)) {- URL.revokeObjectURL(fetched.url);- return;- }- galleryCache.srcById.set(clip.id, fetched.url, fetched.bytes);- galleryCache.srcById.prune(- galleryCache.selectedId ? [galleryCache.selectedId] : [],- );+ const url = await fetchGalleryAudioSignedUrl(clip.id);+ if (!galleryCache.clips.some((candidate) => candidate.id === clip.id)) return;+ galleryCache.srcById.set(clip.id, url);setSrcById(galleryCache.srcById.toRecord());- if (!galleryCache.peaksById.has(clip.id)) {- void decodePeaks(fetched.blob).then(({ peaks }) => {- galleryCache.peaksById.set(clip.id, peaks);- setPeaksById(new Map(galleryCache.peaksById));- });- }+ void ensureClipPeaks(clip);} catch {toast.error("Could not load this audio clip. Try selecting it again.");}@@ -130,7 +144,17 @@ export function useAudioGallery({clipSrcLoads.current.delete(clip.id);}}- }, []);+ }, [ensureClipPeaks]);++ const retryClipSrc = useCallback(+ (clip: AudioGalleryClip, failedUrl: string) => {+ if (!galleryCache.srcById.retry(clip.id, failedUrl)) return;+ // Unmount first: a same-second remint returns the same URL, which would not reload.+ setSrcById(galleryCache.srcById.toRecord());+ void ensureClipSrc(clip);+ },+ [ensureClipSrc],+ );const refreshGallery = useCallback(async (@@ -253,6 +277,11 @@ export function useAudioGallery({const selectClip = useCallback((id: string, keepFallback = false) => {+ // Only a new selection renews a near-expiry link: refreshes never swap a playing source.+ if (galleryCache.selectedId !== id && !galleryCache.srcById.isFresh(id)) {+ galleryCache.srcById.delete(id);+ setSrcById(galleryCache.srcById.toRecord());+ }galleryCache.selectedId = id;setSelectedId(id);const clip = galleryCache.clips.find((candidate) => candidate.id === id);@@ -269,8 +298,10 @@ export function useAudioGallery({const dropClip = useCallback((id: string) => {galleryCache.srcById.delete(id);+ galleryCache.peaksById.delete(id);setSrcById(galleryCache.srcById.toRecord());- // Drop the row now: refreshGallery swallows a failed GET without setClips, leaving it on a revoked URL.+ setPeaksById(new Map(galleryCache.peaksById));+ // Drop the row now: a failed gallery refresh must not leave the deleted clip visible.galleryCache.clips = galleryCache.clips.filter((clip) => clip.id !== id);setClips(galleryCache.clips);if (galleryCache.selectedId === id) {@@ -456,7 +487,10 @@ export function useAudioGallery({.filter((clip) => clipWorkflow(clip) === workflow).map((clip) => clip.id),);- for (const id of cleared) galleryCache.srcById.delete(id);+ for (const id of cleared) {+ galleryCache.srcById.delete(id);+ galleryCache.peaksById.delete(id);+ }if (galleryCache.selectedId && cleared.has(galleryCache.selectedId)) {galleryCache.selectedId = null;}@@ -465,16 +499,19 @@ export function useAudioGallery({);setClips(galleryCache.clips);setSrcById(galleryCache.srcById.toRecord());+ setPeaksById(new Map(galleryCache.peaksById));setSelectedId(galleryCache.selectedId);await refreshGallery();return;}galleryCache.srcById.clear();+ galleryCache.peaksById.clear();galleryCache.selectedId = null;// Drop the cached list and state first: refreshGallery merges into the cache and swallows a failed GET.galleryCache.clips = [];setClips([]);setSrcById({});+ setPeaksById(new Map());setSelectedId(null);await refreshGallery();} catch (error) {@@ -486,11 +523,10 @@ export function useAudioGallery({const handleDownloadClip = useCallback((clip: AudioGalleryClip) => {- const src = srcById[clip.id];- if (!src) return;- void saveAudio(clipFileName(clip), src, () => fetchClipBlob(clip.url));+ // Saving must keep working after a playback link expires.+ void saveAudio(clipFileName(clip), null, () => fetchClipBlob(clip.url));},- [srcById],+ [],);const handleDownloadFallbackClip = useCallback(() => {@@ -507,7 +543,7 @@ export function useAudioGallery({(clip: AudioGalleryClip) =>saveAudio(clipFileName(clip),- galleryCache.srcById.get(clip.id) ?? null,+ null,() => fetchClipBlob(clip.url),),[],@@ -532,6 +568,7 @@ export function useAudioGallery({srcById,peaksById,ensureClipSrc,+ retryClipSrc,refreshGallery,loadMore,selectClip,diff --git a/studio/frontend/src/features/audio/pages/tts-workspace.tsx b/studio/frontend/src/features/audio/pages/tts-workspace.tsxindex a253b76cfbb..dec02d7e714 100644--- a/studio/frontend/src/features/audio/pages/tts-workspace.tsx+++ b/studio/frontend/src/features/audio/pages/tts-workspace.tsx@@ -450,6 +450,7 @@ export function TtsOutput({clips,selectedClip,selectedClipSrc,+ retryClipSrc,srcById,peaksById,handleDownloadClip,@@ -486,6 +487,7 @@ export function TtsOutput({AudioGallery,| "srcById"| "peaksById"+ | "retryClipSrc"| "handleDownloadClip"| "handleDeleteClip"| "fallbackClip"@@ -633,7 +635,20 @@ export function TtsOutput({{pending ? (<PendingClipCard {...pending} />) : selectedClip ? (- <div className="flex w-full flex-col gap-2">+ <div+ className="flex w-full flex-col gap-2"+ onErrorCapture={(event) => {+ // Only this clip's link: source players and a previous selection's late errors are not ours to retry.+ const media = event.target;+ if (+ selectedClipSrc &&+ media instanceof HTMLMediaElement &&+ media.getAttribute("src") === selectedClipSrc+ ) {+ retryClipSrc(selectedClip, selectedClipSrc);+ }+ }}+ ><VariationChipssiblings={siblings}selectedId={selectedClip.id}diff --git a/studio/frontend/tests/audio-page-policy.test.ts b/studio/frontend/tests/audio-page-policy.test.tsindex 2a5ce402493..3a19504b866 100644--- a/studio/frontend/tests/audio-page-policy.test.ts+++ b/studio/frontend/tests/audio-page-policy.test.ts@@ -440,7 +440,7 @@ test("leaving Audio cancels an owned TTS load without touching a pre-request protest("history-only downloads save the fetched blob without caching a URL", () => {assert.match(audioPageSource,- /handleDownloadClipById[\s\S]*galleryCache\.srcById\.get\(clip\.id\) \?\? null,\s*\(\) => fetchClipBlob\(clip\.url\),/,+ /handleDownloadClipById[\s\S]*saveAudio\(\s*clipFileName\(clip\),\s*null,\s*\(\) => fetchClipBlob\(clip\.url\),/,);assert.doesNotMatch(audioPageSource,diff --git a/studio/frontend/tests/audio-playback-links.test.ts b/studio/frontend/tests/audio-playback-links.test.tsnew file mode 100644index 00000000000..b919b38390b--- /dev/null+++ b/studio/frontend/tests/audio-playback-links.test.ts@@ -0,0 +1,60 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";+import { AudioPlaybackLinks } from "../src/features/audio/audio-playback-links.ts";++test("an expired selection can be refreshed without discarding a playing source", () => {+ const links = new AudioPlaybackLinks();+ links.set("song", "/song?token=first", 1000);+ assert.equal(links.isFresh("song", 1000 + 49 * 60 * 1000), true);+ assert.equal(links.isFresh("song", 1000 + 50 * 60 * 1000), false);+ assert.equal(links.get("song"), "/song?token=first");+ assert.deepEqual(links.toRecord(), { song: "/song?token=first" });+});++test("a media failure retries the current source once, including after the link is replaced", () => {+ const links = new AudioPlaybackLinks();+ links.set("song", "/song?token=first");+ assert.equal(links.retry("song", "/song?token=older"), false);+ assert.equal(links.retry("song", "/song?token=first"), true);+ assert.equal(links.get("song"), undefined);+ links.set("song", "/song?token=second");+ assert.equal(links.retry("song", "/song?token=first"), false);+ assert.equal(links.retry("song", "/song?token=second"), false);+ assert.equal(links.get("song"), "/song?token=second");+});++test("dropping a clip or clearing the gallery drops its retry history too", () => {+ const links = new AudioPlaybackLinks();+ links.set("song", "/song?token=first");+ links.retry("song", "/song?token=first");+ links.delete("song");+ links.set("song", "/song?token=second");+ assert.equal(links.retry("song", "/song?token=second"), true);+ links.set("other", "/other?token=first");+ links.clear();+ assert.deepEqual(links.toRecord(), {});+ assert.equal(links.isFresh("other"), false);+ links.set("song", "/song?token=third");+ assert.equal(links.retry("song", "/song?token=third"), true);+});++test("a recovered link can be renewed again when it expires later", () => {+ const links = new AudioPlaybackLinks();+ links.set("song", "/song?token=first", 0);+ assert.equal(links.retry("song", "/song?token=first", 1000), true);+ links.set("song", "/song?token=second", 1000);+ assert.equal(links.retry("song", "/song?token=second", 2000), false);+ assert.equal(+ links.retry("song", "/song?token=second", 1000 + 50 * 60 * 1000),+ true,+ );+ links.set("song", "/song?token=third", 1000 + 50 * 60 * 1000);+ assert.equal(links.retry("song", "/song?token=second"), false);+ assert.equal(+ links.retry("song", "/song?token=third", 2000 + 50 * 60 * 1000),+ false,+ );+});