Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Audio: lengths round to tenths, and a clip's length is read once per file without blocking the server

Claudea6a649a636a0
diff --git a/src/lib/av/audio-tools.ts b/src/lib/av/audio-tools.ts
index c99ab69d..54dfab2c 100644
--- a/src/lib/av/audio-tools.ts
+++ b/src/lib/av/audio-tools.ts
@@ -80,7 +80,8 @@ export interface JoinInput {
/** "2:05", "0:07.5" */
export function clock(seconds: number): string {
- const s = Math.max(0, seconds);
+ // Rounded to tenths first, so 7.96 s reads 0:08, not 0:07.10.
+ const s = Math.round(Math.max(0, seconds) * 10) / 10;
const whole = Math.floor(s + 1e-6);
const tenth = Math.round((s - whole) * 10);
return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, "0")}${tenth && s < 60 ? `.${tenth}` : ""}`;
diff --git a/src/server/audio-tools.ts b/src/server/audio-tools.ts
index bfd5161e..d719ad61 100644
--- a/src/server/audio-tools.ts
+++ b/src/server/audio-tools.ts
@@ -1,4 +1,5 @@
import "server-only";
+import fs from "node:fs/promises";
import { z } from "zod";
import { planJoin, planLoop, CROSSFADE_MAX, JOIN_MAX_CLIPS, LOOP_MAX_SECONDS, type AudioToolPreview, type ToolPlan } from "@/lib/av/audio-tools";
import type { Engine, Generation } from "@/lib/catalog/types";
@@ -6,7 +7,7 @@ import { probeMedia } from "@/lib/media-probe";
import { createPipeline } from "./chains";
import { previewChained } from "./generations";
import { engineMode } from "./settings";
-import { readRef } from "./storage";
+import { absolutePath } from "./storage";
/**
* The Audio hub's Loop / extend and Join tools on the server: each clip's length is read from its own header
@@ -44,15 +45,40 @@ export type AudioToolRequest = z.infer<typeof AudioToolBody>;
export class AudioToolError extends Error {}
-/** A clip's length from its header (WAV, FLAC, MP3, M4A), else the browser's. */
+declare global {
+ var __fuobeAudioLengths: Map<string, number | null> | undefined;
+}
+/**
+ * Lengths already read, by file and version (path, size, modified time): the page asks for a price after
+ * every settled change, and a Join of big WAVs would read every file again each time.
+ */
+const lengths = (globalThis.__fuobeAudioLengths ??= new Map<string, number | null>());
+const LENGTHS_KEPT = 500;
+
+/** A clip's length from its header (WAV, FLAC, MP3, M4A), else the browser's. Read without blocking the server. */
async function lengthOf(url: string, given?: number): Promise<number | undefined> {
+ let file: string;
+ let key: string;
try {
- const info = probeMedia((await readRef(url)).data);
- if (info?.duration && info.duration > 0) return Math.round(info.duration * 1000) / 1000;
+ file = absolutePath(decodeURIComponent(url.slice("/api/files/".length)));
+ const st = await fs.stat(file);
+ if (!st.isFile()) throw new Error("not a file");
+ key = `${file}:${st.size}:${st.mtimeMs}`;
} catch {
throw new AudioToolError("That audio file is gone — add it again");
}
- return given;
+ if (!lengths.has(key)) {
+ let data: Buffer;
+ try {
+ data = await fs.readFile(file);
+ } catch {
+ throw new AudioToolError("That audio file is gone — add it again");
+ }
+ const info = probeMedia(data);
+ if (lengths.size >= LENGTHS_KEPT) lengths.delete(lengths.keys().next().value!);
+ lengths.set(key, info?.duration && info.duration > 0 ? Math.round(info.duration * 1000) / 1000 : null);
+ }
+ return lengths.get(key) ?? given;
}
async function plan(body: AudioToolRequest): Promise<ToolPlan & { engine: Engine }> {
diff --git a/tests/audio-tools.test.ts b/tests/audio-tools.test.ts
index 01a9beeb..b2102480 100644
--- a/tests/audio-tools.test.ts
+++ b/tests/audio-tools.test.ts
@@ -123,6 +123,11 @@ describe("Loop / extend and Join plans", () => {
expect(outputActions(g, 0).find((a) => a.id === "loop-audio")).toMatchObject({ label: "Loop / extend", placement: "menu", href: "/audio/loop?audio=%2Fapi%2Ffiles%2Fo%2Fbed.flac" });
expect(clock(61.5)).toBe("1:01");
expect(clock(7.5)).toBe("0:07.5");
+ // Tenths round with the seconds: 7.968 s is 0:08, not 0:07.10.
+ expect(clock(7.968)).toBe("0:08");
+ expect(clock(3.99)).toBe("0:04");
+ expect(clock(59.96)).toBe("1:00");
+ expect(clock(7.44)).toBe("0:07.4");
});
});
@@ -134,6 +139,13 @@ describe("POST /api/audio-tools (demo mode)", () => {
expect(preview).toEqual({ summary: "Loops it to 0:20 with 3 crossfaded seams (1 s each), then fades out over the last 1 s.", notes: [], length: 20, steps: 1, costUsd: 0, demo: true });
const cloud = await (await post({ tool: "loop", engine: "cloud", audio, duration: 20 }, true)).json();
expect(cloud).toMatchObject({ length: 24, steps: 1, demo: true });
+ // A length read once is kept for that file as it is; a file written again is read again.
+ const { absolutePath } = await import("@/server/storage");
+ const fileOf = (url: string) => absolutePath(decodeURIComponent(url.slice("/api/files/".length)));
+ const other = upload(6, "other.wav");
+ expect((await (await post({ tool: "loop", engine: "open", audio: other, duration: 20 }, true)).json()).length).toBe(20);
+ fs.writeFileSync(fileOf(other), fs.readFileSync(fileOf(upload(25))));
+ expect((await (await post({ tool: "loop", engine: "open", audio: other, duration: 20 }, true)).json()).error).toBe("The clip is already 0:25 long — pick a longer length");
const res = await post({ tool: "loop", engine: "open", audio, duration: 20, crossfade: 1 });
expect(res.status).toBe(201);
const run = await res.json();