Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor asset thumbnail rendering and introduce audio peaks worker

konstantin-paulus394d547202ae
diff --git a/apps/web/src/components/sidebar-left/asset-item.tsx b/apps/web/src/components/sidebar-left/asset-item.tsx
index 87c1fdf0..82be83b9 100644
--- a/apps/web/src/components/sidebar-left/asset-item.tsx
+++ b/apps/web/src/components/sidebar-left/asset-item.tsx
@@ -130,7 +130,7 @@ export function LazyAssetItem(props: LazyAssetItemProps) {
class="relative aspect-video w-full overflow-clip rounded bg-muted after:pointer-events-none after:absolute after:inset-0 after:rounded after:opacity-0 after:ring-2 after:ring-inset after:ring-ring after:z-10 data-[selected=true]:after:opacity-100"
>
<Show when={isVisible()}>
- <AssetThumbnail asset={props.asset} class="h-full w-full" cache={library()?.cache} />
+ <AssetThumbnail asset={props.asset} class="absolute inset-0" cache={library()?.cache} />
</Show>
<Show when={assetDuration()}>
{(duration) => (
diff --git a/apps/web/src/components/ui/asset-thumbnail.tsx b/apps/web/src/components/ui/asset-thumbnail.tsx
index 1efaad9a..545d1ae5 100644
--- a/apps/web/src/components/ui/asset-thumbnail.tsx
+++ b/apps/web/src/components/ui/asset-thumbnail.tsx
@@ -79,7 +79,7 @@ function MediaThumbnail(props: { asset: Asset; width: number; cache?: AssetCache
<img
src={url()!}
alt=""
- class="w-full h-full object-cover select-none"
+ class="absolute inset-0 size-full object-cover select-none"
/>
</Show>
);
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index f4891d08..2b5c48c9 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -216,12 +216,12 @@ class Engine {
if (this.ownsAudioContext) {
this.audioContext.close();
}
- this.world.destroy();
- this.unsubscribeEventListeners();
- this.world.get(PointerEvents)!.queue.length = 0;
+ this.unsubscribeEventListeners();
this.unsubscribe.forEach(unsubscribe => unsubscribe());
this.unsubscribe.length = 0;
+ this.world.get(PointerEvents)?.queue.splice(0);
+ this.world.destroy();
}
private unsubscribeEventListeners(): void {
diff --git a/packages/assets/src/derive/peaks-worker.ts b/packages/assets/src/derive/peaks-worker.ts
new file mode 100644
index 00000000..f727db3c
--- /dev/null
+++ b/packages/assets/src/derive/peaks-worker.ts
@@ -0,0 +1,94 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// The rough waveform of a media file, computed off the main thread: a fixed
+// number of bars, each the peak amplitude (across channels, gamma-lifted so
+// quiet passages still show) of its slice of the file, as a byte. WebCodecs
+// decodes elsewhere already, but PCM formats (WAV) are unpacked in JS and
+// would stall the UI. One worker per request: a file and a bar count in,
+// the peaks out.
+
+import { ALL_FORMATS, AudioSampleSink, BlobSource, Input } from 'mediabunny';
+
+export interface PeaksRequest {
+ file: Blob;
+ bars: number;
+}
+
+export type PeaksResponse = { peaks: Uint8ClampedArray | null } | { error: string };
+
+// The package is typed against the DOM lib, so the worker scope is narrowed by hand.
+const scope = self as unknown as {
+ onmessage: ((event: MessageEvent<PeaksRequest>) => void) | null;
+ postMessage(message: PeaksResponse, transfer?: Transferable[]): void;
+};
+
+scope.onmessage = async ({ data: { file, bars } }) => {
+ try {
+ const peaks = await computePeaks(file, bars);
+ scope.postMessage({ peaks }, peaks ? [peaks.buffer as ArrayBuffer] : []);
+ } catch (error) {
+ scope.postMessage({ error: error instanceof Error ? error.message : String(error) });
+ }
+};
+
+/** The peaks of `file`'s primary audio track, or null when it has none. */
+async function computePeaks(file: Blob, bars: number): Promise<Uint8ClampedArray | null> {
+ const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(file) });
+ try {
+ const track = await input.getPrimaryAudioTrack();
+ if (!track) return null;
+
+ const sink = new AudioSampleSink(track);
+ const chunks: Uint8ClampedArray[] = [];
+
+ for await (const sample of sink.samples()) {
+ let floats: Float32Array;
+ let channels: number;
+ let frames: number;
+ try {
+ const size = sample.allocationSize({ format: 'f32', planeIndex: 0 });
+ floats = new Float32Array(size / Float32Array.BYTES_PER_ELEMENT);
+ sample.copyTo(floats, { format: 'f32', planeIndex: 0 });
+ channels = sample.numberOfChannels;
+ frames = floats.length / channels;
+ } finally {
+ sample.close();
+ }
+
+ const framePeaks = new Uint8ClampedArray(frames);
+ for (let f = 0; f < frames; f++) {
+ let max = 0;
+ for (let ch = 0; ch < channels; ch++) {
+ max = Math.max(max, Math.pow(Math.abs(floats[f * channels + ch]!), 0.8));
+ }
+ framePeaks[f] = Math.floor(max * 255);
+ }
+ chunks.push(framePeaks);
+ }
+
+ if (chunks.length === 0) return null;
+
+ const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
+ const merged = new Uint8ClampedArray(total);
+ let offset = 0;
+ for (const chunk of chunks) {
+ merged.set(chunk, offset);
+ offset += chunk.length;
+ }
+
+ const peaks = new Uint8ClampedArray(bars);
+ const ratio = total / bars;
+ for (let i = 0; i < bars; i++) {
+ const start = Math.floor(i * ratio);
+ const end = Math.min(total, Math.floor((i + 1) * ratio));
+ let max = 0;
+ for (let j = start; j < end; j++) max = Math.max(max, merged[j]!);
+ peaks[i] = max;
+ }
+ return peaks;
+ } finally {
+ input.dispose();
+ }
+}
diff --git a/packages/assets/src/derive/peaks.ts b/packages/assets/src/derive/peaks.ts
index 0667dff1..7a7455f9 100644
--- a/packages/assets/src/derive/peaks.ts
+++ b/packages/assets/src/derive/peaks.ts
@@ -2,72 +2,23 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-// The rough waveform of a media file: a fixed number of bars, each the peak
-// amplitude (across channels, gamma-lifted so quiet passages still show) of
-// its slice of the file, as a byte. Decodes the whole audio track once; the
-// AssetCache keeps the result.
-
-import { ALL_FORMATS, AudioSampleSink, BlobSource, Input } from 'mediabunny';
+import type { PeaksRequest, PeaksResponse } from './peaks-worker';
/** How many bars a rough waveform has. */
export const PEAK_BARS = 128;
-/** The peaks of `file`'s primary audio track, or null when it has none. */
-export async function derivePeaks(file: Blob, bars = PEAK_BARS): Promise<Uint8ClampedArray | null> {
- const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(file) });
- try {
- const track = await input.getPrimaryAudioTrack();
- if (!track) return null;
-
- const sink = new AudioSampleSink(track);
- const chunks: Uint8ClampedArray[] = [];
-
- for await (const sample of sink.samples()) {
- let floats: Float32Array;
- let channels: number;
- let frames: number;
- try {
- const size = sample.allocationSize({ format: 'f32', planeIndex: 0 });
- floats = new Float32Array(size / Float32Array.BYTES_PER_ELEMENT);
- sample.copyTo(floats, { format: 'f32', planeIndex: 0 });
- channels = sample.numberOfChannels;
- frames = floats.length / channels;
- } finally {
- sample.close();
- }
-
- const framePeaks = new Uint8ClampedArray(frames);
- for (let f = 0; f < frames; f++) {
- let max = 0;
- for (let ch = 0; ch < channels; ch++) {
- max = Math.max(max, Math.pow(Math.abs(floats[f * channels + ch]!), 0.8));
- }
- framePeaks[f] = Math.floor(max * 255);
- }
- chunks.push(framePeaks);
- }
-
- if (chunks.length === 0) return null;
-
- const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
- const merged = new Uint8ClampedArray(total);
- let offset = 0;
- for (const chunk of chunks) {
- merged.set(chunk, offset);
- offset += chunk.length;
- }
-
- const peaks = new Uint8ClampedArray(bars);
- const ratio = total / bars;
- for (let i = 0; i < bars; i++) {
- const start = Math.floor(i * ratio);
- const end = Math.min(total, Math.floor((i + 1) * ratio));
- let max = 0;
- for (let j = start; j < end; j++) max = Math.max(max, merged[j]!);
- peaks[i] = max;
- }
- return peaks;
- } finally {
- input.dispose();
- }
+/**
+ * The peaks of `file`'s primary audio track, or null when it has none.
+ * Decoded in a worker of its own, gone when it answers.
+ */
+export function derivePeaks(file: Blob, bars = PEAK_BARS): Promise<Uint8ClampedArray | null> {
+ const worker = new Worker(new URL('./peaks-worker.ts', import.meta.url), { type: 'module' });
+ return new Promise<Uint8ClampedArray | null>((resolve, reject) => {
+ worker.onmessage = ({ data }: MessageEvent<PeaksResponse>) => {
+ if ('error' in data) reject(new Error(data.error));
+ else resolve(data.peaks);
+ };
+ worker.onerror = (event) => reject(new Error(event.message || 'The peaks worker failed'));
+ worker.postMessage({ file, bars } satisfies PeaksRequest);
+ }).finally(() => worker.terminate());
}
diff --git a/scripts/dev-desktop.mjs b/scripts/dev-desktop.mjs
index c9eccb52..a5cf8262 100644
--- a/scripts/dev-desktop.mjs
+++ b/scripts/dev-desktop.mjs
@@ -5,7 +5,8 @@
// One command to develop the desktop app from source. It:
// 1. builds the CLI, so a linked `dapi` (see symlink:create) runs the
// latest code and the app's headless server matches it;
-// 2. starts the web dev server (Vite on :5173);
+// 2. starts the web dev server (Vite on :5173), first reclaiming the port
+// from a Vite left behind by an earlier run that did not come down;
// 3. waits for that server, then launches Electron, which loads it.
// Ctrl-C tears the whole tree down.
@@ -16,7 +17,8 @@ import { fileURLToPath } from "node:url";
const ROOT = fileURLToPath(new URL("..", import.meta.url));
const BIN = join(ROOT, "node_modules", ".bin");
-const DEV_URL = "http://localhost:5173";
+const DEV_PORT = 5173;
+const DEV_URL = `http://localhost:${DEV_PORT}`;
const children = [];
let shuttingDown = false;
@@ -77,11 +79,66 @@ for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, () => shutdown(0));
}
+/** PIDs listening on a TCP port (macOS/Linux via lsof); [] when none or unknown. */
+function listeners(port) {
+ try {
+ const out = execFileSync("lsof", ["-nP", "-t", `-iTCP:${port}`, "-sTCP:LISTEN"], { stdio: ["ignore", "pipe", "ignore"] });
+ return out.toString().split("\n").map((line) => Number(line.trim())).filter(Boolean);
+ } catch {
+ return []; // lsof exits 1 when nothing listens.
+ }
+}
+
+/** The command line of a process, or "" when it is gone. */
+function commandOf(pid) {
+ try {
+ return execFileSync("ps", ["-o", "command=", "-p", String(pid)], { stdio: ["ignore", "pipe", "ignore"] }).toString().trim();
+ } catch {
+ return "";
+ }
+}
+
+/**
+ * Frees the dev port. A Vite left over from an earlier run of this repo
+ * (Ctrl-C'd terminal, crashed Electron, a detached child) is killed and the
+ * port awaited; anything else on the port is not ours to touch, so we say
+ * what it is and stop.
+ */
+async function reclaimPort(port) {
+ const pids = listeners(port);
+ if (!pids.length) return;
+ for (const pid of pids) {
+ const command = commandOf(pid);
+ if (!command.includes("vite") || !command.includes(ROOT.replace(/\/$/, ""))) {
+ console.error(`[dev:desktop] port ${port} is in use by another process (pid ${pid}): ${command || "unknown"}`);
+ process.exit(1);
+ }
+ console.log(`[dev:desktop] port ${port} held by a stale vite (pid ${pid}); stopping it…`);
+ try {
+ process.kill(pid, "SIGTERM");
+ } catch {
+ // Already gone.
+ }
+ }
+ const deadline = Date.now() + 5000;
+ while (listeners(port).length) {
+ if (Date.now() > deadline) {
+ for (const pid of listeners(port)) {
+ try { process.kill(pid, "SIGKILL"); } catch { /* gone */ }
+ }
+ await new Promise((r) => setTimeout(r, 200));
+ break;
+ }
+ await new Promise((r) => setTimeout(r, 100));
+ }
+}
+
// 1. Build the CLI (blocking) so `dapi` and the app agree on the latest code.
console.log("[dev:desktop] building CLI…");
execFileSync("npm", ["run", "build", "--workspace=@diffusionstudio/cli"], { stdio: "inherit" });
-// 2. Start the web dev server.
+// 2. Start the web dev server, on a port that is free.
+await reclaimPort(DEV_PORT);
console.log("[dev:desktop] starting web dev server…");
run("web", "vite", [], join(ROOT, "apps", "web"));