Open source CapCut alternative.
TypeScript
fix(site): bill stems once and master mono alike [rebuild]
David Lec5a175bfcdfe
diff --git a/site/src/clients/chatgpt/server/mcp.ts b/site/src/clients/chatgpt/server/mcp.tsindex f347a367..6d4fc477 100644--- a/site/src/clients/chatgpt/server/mcp.ts+++ b/site/src/clients/chatgpt/server/mcp.ts@@ -429,7 +429,7 @@ export function createChatgptServer({title: "Export a project",description:- `Render the saved project to an MP4 the user downloads from the Donkey Cut card. preset: ${DOC_EXPORT_PRESETS.join(" | ")} (original matches the footage; tiktok is best quality 1080p; fast is a smaller 1080p; light is a 720p draft). loudness masters the mix to an integrated target under a true-peak ceiling: ${LOUDNESS_CHOICES.map((c) => (c.id === "off" ? "off (the mix as it plays)" : `${c.id} ${cutLoudness().targets[c.id]} LUFS (${c.detail})`)).join(", ")}; the true-peak ceiling is ${cutLoudness().truePeakCeiling} dBTP; omitted uses the account default (${cutLoudness().defaultTarget}). stems: true also writes one 24-bit WAV per lane (dialogue from the video clips, then each soundtrack lane), full length and unmastered, zipped as "<name> stems.zip" beside the video in the project's exports. Free; the files count against the account's cloud storage and are listed in the project's exports. A render can outlast this call — finish it with get_export_status.`,+ `Render the saved project to an MP4 the user downloads from the Donkey Cut card. preset: ${DOC_EXPORT_PRESETS.join(" | ")} (original matches the footage; tiktok is best quality 1080p; fast is a smaller 1080p; light is a 720p draft). loudness masters the mix to an integrated target under a true-peak ceiling: ${LOUDNESS_CHOICES.map((c) => (c.id === "off" ? "off (the mix as it plays)" : `${c.id} ${cutLoudness().targets[c.id]} LUFS (${c.detail})`)).join(", ")}; the true-peak ceiling is ${cutLoudness().truePeakCeiling} dBTP; omitted masters to ${cutLoudness().defaultTarget}. stems: true also writes one 24-bit WAV per lane (dialogue from the video clips, then each soundtrack lane), full length and unmastered, zipped as "<name> stems.zip" beside the video in the project's exports. Free; the files count against the account's cloud storage and are listed in the project's exports. A render can outlast this call — finish it with get_export_status.`,inputSchema: z.object({projectId: idSchema,preset: z.enum(DOC_EXPORT_PRESETS).optional(),diff --git a/site/src/clients/chatgpt/server/projects.test.ts b/site/src/clients/chatgpt/server/projects.test.tsindex 331fc738..af565fc0 100644--- a/site/src/clients/chatgpt/server/projects.test.ts+++ b/site/src/clients/chatgpt/server/projects.test.ts@@ -96,6 +96,18 @@ describe("ChatGPT cloud projects", () => {},});});+ test("an export that never asked for stems reports none, even beside an older zip of its name", async () => {+ const { tools, db } = createTestContext();+ const row = { id: "job", kind: "export", projectId: "mine", state: "done", progress: 1, outputKey: "cut/owner/mine/exports/Trip.mp4", outName: "Trip.mp4" };+ db.cutRenderJob.findFirst.mockResolvedValue({ ...row, spec: { fromDoc: { preset: "original" } } });+ db.cutMediaObject.findFirst.mockImplementation(async (args: unknown) => {+ const where = (args as { where: { r2Key?: string; fileName?: string } }).where;+ return where.r2Key ? { r2Key: where.r2Key } : { fileName: where.fileName };+ });+ expect((await tools.exportStatus("job")).view.export?.stems).toBeUndefined();+ db.cutRenderJob.findFirst.mockResolvedValue({ ...row, spec: { fromDoc: { preset: "original", stems: true } } });+ expect((await tools.exportStatus("job")).view.export?.stems).toBe("Trip stems.zip");+ });test("collected artifacts are reported as expired with no playback URL", async () => {const { tools, db } = createTestContext();db.cutRenderJob.findFirst.mockResolvedValue({diff --git a/site/src/clients/chatgpt/server/projects.ts b/site/src/clients/chatgpt/server/projects.tsindex 4b854b69..2c7136ea 100644--- a/site/src/clients/chatgpt/server/projects.ts+++ b/site/src/clients/chatgpt/server/projects.ts@@ -8,7 +8,7 @@ import { ADOPT_COMMAND, type AdoptedAsset } from "@/cut/lib/commandBatch";import { NO_CARD_OPEN, queueCommands, waitForJob, type CommandCall, type CommandJobResult, type JobRow } from "@/cut/server/cloud/commands";import { needsWorker, queueDocExport, queueImportUrl } from "@/cut/server/cloud/jobs";import type { DocExportAudio } from "@/cut/lib/exportPresets";-import { stemsArchiveName } from "@/cut/lib/exportDelivery";+import { jobWantsStems, stemsArchiveName } from "@/cut/lib/exportDelivery";import { cutLimitsFor } from "@/cut/server/cloud/limits";import { previewFromDoc } from "@/cut/server/cloud/previewJobs";import { mediaObjectUrl, mediaUrlLifetime } from "@/cut/server/cloud/mediaCdn";@@ -215,8 +215,9 @@ export function projectTools(view.export.error = "The export file was deleted. Export again.";return { view, playback: null, download: null };}- // Stems land beside the video under its name, in the project's exports.- if (row.outName) {+ // Stems land beside the video under its name, in the project's exports,+ // when this job asked for them.+ if (row.outName && jobWantsStems(row.spec)) {const stems = await db.cutMediaObject.findFirst({where: {userId: identity.userId,diff --git a/site/src/cut/components/TopBar.tools.ts b/site/src/cut/components/TopBar.tools.tsindex b3c39e77..f6ab2f5d 100644--- a/site/src/cut/components/TopBar.tools.ts+++ b/site/src/cut/components/TopBar.tools.ts@@ -9,7 +9,6 @@ import { OUTPUT_SPACES } from "@donkeycut/effects-kit";import { obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";import { EXPORT_RESOLUTIONS } from "@/cut/lib/exportPresets";import { LOUDNESS_CHOICES } from "@/cut/lib/loudnessSettings";-import { SETTINGS } from "@/lib/config/registry";import { GUIDE_IDS, GUIDE_PRESETS } from "@/cut/lib/guides";export const TOP_BAR_TOOLS = [@@ -57,7 +56,7 @@ export const TOP_BAR_TOOLS = [{name: "open_export",description:- `Open the export dialog: Best uses original resolution, frame rate, supported H.264/HEVC codec and audio settings. Whole files, trims and compatible sequences copy compressed video; joined audio is encoded at source settings. Effects or incompatible joins render with source settings. Other presets are Share, Social 4K, Small and Master. Controls include file name, whole video or selection range, MP4/MOV, H.264/HEVC/ProRes, resolution (${EXPORT_RESOLUTIONS.map((r) => r.label).join(", ")}, Source), frame rate, quality or custom bitrate, AAC/PCM, the project's color space (SDR, or HDR as HLG or PQ — a 10-bit HEVC or ProRes file; H.264 greys out), an SRT captions file, Loudness (${LOUDNESS_CHOICES.map((c) => (c.id === "off" ? c.label : `${c.label} ${SETTINGS.cutLoudness.default.targets[c.id]} LUFS`)).join(", ")} by default: one gain sets the mix's integrated loudness, and a limiter holds true peaks under ${SETTINGS.cutLoudness.default.truePeakCeiling} dBTP), and Stems (a zip of one 24-bit WAV per lane, the video clips' dialogue then each soundtrack lane, full length and unmastered, saved beside the video). measure_level with mix: true reads the mix's loudness first. Exporting itself stays a user action.`,+ `Open the export dialog: Best uses original resolution, frame rate, supported H.264/HEVC codec and audio settings. Whole files, trims and compatible sequences copy compressed video; joined audio is encoded at source settings. Effects or incompatible joins render with source settings. Other presets are Share, Social 4K, Small and Master. Controls include file name, whole video or selection range, MP4/MOV, H.264/HEVC/ProRes, resolution (${EXPORT_RESOLUTIONS.map((r) => r.label).join(", ")}, Source), frame rate, quality or custom bitrate, AAC/PCM, the project's color space (SDR, or HDR as HLG or PQ — a 10-bit HEVC or ProRes file; H.264 greys out), an SRT captions file, Loudness (${LOUDNESS_CHOICES.map((c) => `${c.label}: ${c.detail}`).join("; ")}. Each target's LUFS, the true-peak ceiling and the choice an export starts on are account settings the dialog shows. One gain sets the mix's integrated loudness, and a limiter holds true peaks under the ceiling), and Stems (a zip of one 24-bit WAV per lane, the video clips' dialogue then each soundtrack lane, full length and unmastered, saved beside the video). measure_level with mix: true reads the mix's loudness first. Exporting itself stays a user action.`,inputSchema: obj({}),},] as const satisfies readonly AiToolDef[];diff --git a/site/src/cut/lib/cloudTranscribe.ts b/site/src/cut/lib/cloudTranscribe.tsindex ef96255a..5e38b57d 100644--- a/site/src/cut/lib/cloudTranscribe.ts+++ b/site/src/cut/lib/cloudTranscribe.ts@@ -15,6 +15,7 @@ import { renderMix as mixAudio } from "./audioMix";import { apiFetch } from "./backend";import { cloudBackend } from "./backend/cloud";import { mediaUrl, type SubtitleCue } from "./types";+import { wavHeader } from "./wav";/** Mirror of the engine's TranscribeSpec (server/transcribe.ts) minus projectId. */export interface CloudTranscribeSpec {@@ -72,22 +73,7 @@ export function renderMix(/** 16-bit PCM WAV (RIFF) from mono 16 kHz float samples. */export function encodeWav(samples: Float32Array): Blob {const data = new DataView(new ArrayBuffer(44 + samples.length * 2));- const ascii = (off: number, s: string) => {- for (let i = 0; i < s.length; i++) data.setUint8(off + i, s.charCodeAt(i));- };- ascii(0, "RIFF");- data.setUint32(4, 36 + samples.length * 2, true);- ascii(8, "WAVE");- ascii(12, "fmt ");- data.setUint32(16, 16, true);- data.setUint16(20, 1, true); // PCM- data.setUint16(22, 1, true); // mono- data.setUint32(24, RATE, true);- data.setUint32(28, RATE * 2, true);- data.setUint16(32, 2, true);- data.setUint16(34, 16, true);- ascii(36, "data");- data.setUint32(40, samples.length * 2, true);+ new Uint8Array(data.buffer).set(wavHeader(samples.length, 1, RATE, 16));for (let i = 0; i < samples.length; i++) {const v = Math.max(-1, Math.min(1, samples[i]));data.setInt16(44 + i * 2, v < 0 ? v * 0x8000 : v * 0x7fff, true);diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.tsindex 39a7d803..8e05b852 100644--- a/site/src/cut/lib/exportClient.ts+++ b/site/src/cut/lib/exportClient.ts@@ -16,7 +16,7 @@ import {reserveBrowserExportJob,updateBrowserExportJob,} from "./backend/browser/exportJobs";-import { exportsDir, projectDir, writeFileAt, readFileAt, saveExport } from "./backend/browser/opfs";+import { deleteExport as dropStoredExport, exportsDir, projectDir, writeFileAt, readFileAt, saveExport } from "./backend/browser/opfs";import { holdRegistered, registerBlobFile, releaseRegistered, resolveRegisteredBlob } from "./backend/browser/registry";import { captureCloudBackend } from "./backend/cloud";import { downloadFile, downloadFromUrl } from "./download";@@ -1646,10 +1646,11 @@ function cutUsesSoundFeatures(doc: ExportDoc): boolean {async function assertEngineCarries(settings: ExportSettings,doc: ExportDoc,- /** The engine encodes this one in ffmpeg; a tab render only hands it the- * finished files. */- engineRenders = true+ /** Who encodes the file: the engine in ffmpeg, or this tab, which only+ * hands the engine the finished files. */+ renderer: "engine" | "tab"): Promise<void> {+ const engineRenders = renderer === "engine";const wants: [EngineFeature, boolean, string][] = [// An older engine would deliver the mix unmastered and drop the stems.["export.loudness", engineRenders && settings.loudness !== undefined, "master the loudness"],@@ -1690,7 +1691,7 @@ export async function createExportJob(): Promise<string> {const backend = projectOperation(projectId).backend;const outName = exportOutName(settings);- if (backend.kind === "local") await assertEngineCarries(settings, doc);+ if (backend.kind === "local") await assertEngineCarries(settings, doc, "engine");const payload = await buildExportPayload(projectId, doc, settings, "export");const res = await postExport(projectId, payload, outName, backend);const body = await apiJson<{ id?: string }>(res);@@ -1841,7 +1842,7 @@ async function runEngineExport(opts: NonNullable<Parameters<typeof runBrowserExport>[3]>,backend: CutBackend): Promise<string> {- await assertEngineCarries(settings, doc, false);+ await assertEngineCarries(settings, doc, "tab");const claim = await backend.fetch("/api/cut/export/client", {method: "POST",headers: { "Content-Type": "application/json" },@@ -1941,7 +1942,9 @@ async function runEngineExport(}/** A browser-resident project's stems, rendered in this tab and shelved- * beside the video they go with. */+ * beside the video they go with. The two land together: stems that fail or+ * are canceled take the saved video with them. A failure is final, since the+ * cloud fallback mixes its stems in this same tab. */async function saveStems(projectId: string,doc: ExportDoc,@@ -1960,6 +1963,11 @@ async function saveStems(} finally {void stems.discard();}+ } catch (err) {+ await dropStoredExport(projectId, videoName);+ if (err instanceof DOMException && err.name === "AbortError") throw err;+ if (err instanceof ExportRefusedError) throw err;+ throw new ExportRefusedError(`The stems could not be saved: ${err instanceof Error ? err.message : String(err)}`);} finally {releaseRegistered(`/api/cut/projects/${projectId}/`);}diff --git a/site/src/cut/lib/exportDelivery.ts b/site/src/cut/lib/exportDelivery.tsindex 9168fa98..87c7b15e 100644--- a/site/src/cut/lib/exportDelivery.ts+++ b/site/src/cut/lib/exportDelivery.ts@@ -132,21 +132,26 @@ export function isDeliveryName(name: string): boolean {return EXPORT_CONTAINERS.some((c) => lower.endsWith(c.ext)) || isStemsName(name);}-/** The stems archive that rides beside a video: "Trip.mp4" → "Trip stems.zip". */-export const STEMS_SUFFIX = " stems.zip";export const STEMS_MIME = "application/zip";+/** The stems archive that rides beside a video: "Trip.mp4" → "Trip stems.zip". */export function stemsArchiveName(videoName: string): string {- return `${exportBaseName(videoName)}${STEMS_SUFFIX}`;+ return `${exportBaseName(videoName)} stems.zip`;}export function isStemsName(name: string): boolean {return name.toLowerCase().endsWith(".zip");}-/** The media type of a delivered file, by its name. */-export function deliveryMime(name: string): string {- return isStemsName(name) ? STEMS_MIME : containerOfName(name).mime;+/** What an export lands: the file alone, or the file with its stems zip+ * beside it, whose name it claims too. */+export type ExportFiles = "file" | "fileAndStems";++/** Whether a stored export job asked for stems, however it was queued: a+ * tab render's claim, a document export, or a built spec with a stem plan. */+export function jobWantsStems(stored: unknown): boolean {+ const s = stored as { stems?: boolean; fromDoc?: { stems?: boolean }; spec?: { stemPlan?: unknown[] } } | null;+ return !!(s?.stems || s?.fromDoc?.stems || s?.spec?.stemPlan?.length);}/**diff --git a/site/src/cut/lib/exportRender.ts b/site/src/cut/lib/exportRender.tsindex 5af419b1..4a9e3873 100644--- a/site/src/cut/lib/exportRender.ts+++ b/site/src/cut/lib/exportRender.ts@@ -33,7 +33,7 @@ import { drawBlock } from "./blockSource";import { audioFxSpans } from "./audioEffects";import { duckWindows, renderMix, type MixClip, type MixItem, type MixSpec } from "./audioMix";import { masterInPlace } from "./loudness";-import { wavBytes, wavChunks, writeStoredZip, type StemDef } from "./stems";+import { STEM_CHANNELS, STEM_RATE, wavBytes, wavChunks, writeStoredZip, type StemDef } from "./stems";import { specSound } from "./soundSource";import { FrameCompositor, MISSING_FRAME, type Frame } from "./composite";import { ensureClipLuts, sourceLookup } from "./lutBuild";@@ -1211,12 +1211,11 @@ export async function renderStemsArchive(stems: StemDef[],opts: Pick<RenderOptions, "resolve" | "signal">): Promise<RenderedExport> {- if (stems.length === 0) throw new Error("Nothing in this cut makes a sound to split into stems.");const duration = projectDuration(doc);const from = Math.max(0, settings.range?.start ?? 0);const span = deliverySpan(settings.range, duration);- const rate = settings.audioSampleRate ?? AUDIO_RATE;- const count = settings.audioChannels ?? AUDIO_CHANNELS;+ const rate = STEM_RATE;+ const count = STEM_CHANNELS;const spec = mixSpecFor(doc, opts.resolve);// The length the export's own mix comes out at, slice included.const total = Math.max(1, Math.ceil(duration * rate));diff --git a/site/src/cut/lib/loudness.test.ts b/site/src/cut/lib/loudness.test.tsindex d1d22aa5..4ec5672d 100644--- a/site/src/cut/lib/loudness.test.ts+++ b/site/src/cut/lib/loudness.test.ts@@ -78,7 +78,7 @@ describe("integrated loudness", () => {test("a mono source weighted as the two channels it plays on reads as that stereo pair", () => {const x = sine(1000, -20, 10);- const mono = new LoudnessMeter(RATE, 1, { weights: [2] });+ const mono = new LoudnessMeter(RATE, 1);mono.push([x]);const pair = measureLoudness(stereo(x), RATE);expect(Math.abs(mono.result().integratedLufs - pair.integratedLufs)).toBeLessThan(0.01);@@ -269,6 +269,14 @@ describe("master in place", () => {expect(Math.abs(after.integratedLufs - -14)).toBeLessThan(1);});+ test("a mono delivery lands as loud as the stereo delivery of the same sound", async () => {+ const mono = [sine(1000, -30, 5)];+ const pair = stereo(sine(1000, -30, 5));+ const one = await masterInPlace(mono, { sampleRate: RATE, targetLufs: -14, ceilingDbtp: -1 });+ const two = await masterInPlace(pair, { sampleRate: RATE, targetLufs: -14, ceilingDbtp: -1 });+ expect(one.plan.gainDb).toBeCloseTo(two.plan.gainDb, 2);+ });+test("a clean gain when no limiting is needed", async () => {const channels = stereo(sine(1000, -30, 5));const report = await masterInPlace(channels, { sampleRate: RATE, targetLufs: -23, ceilingDbtp: -1 });diff --git a/site/src/cut/lib/loudness.ts b/site/src/cut/lib/loudness.tsindex 84fd1089..c1544ae1 100644--- a/site/src/cut/lib/loudness.ts+++ b/site/src/cut/lib/loudness.ts@@ -40,7 +40,7 @@ const BLOCK_STEPS = 4;const SHORT_TERM_STEPS = 30;/** One K-weighting biquad, as a/b with a0 = 1. */-export interface Biquad {+interface Biquad {b: [number, number, number];a: [number, number, number];}@@ -80,10 +80,13 @@ export function kWeighting(sampleRate: number): [Biquad, Biquad] {}/** Channel weights for a layout: front channels count once, the surrounds- * 1.41 (+1.5 dB), and the LFE not at all. Mono and stereo are all ones. */-export function channelWeights(channels: number): number[] {+ * 1.41 (+1.5 dB), and the LFE not at all. Mono counts as the two speakers+ * it plays on, so a mono file and the stereo file of the same sound read+ * the same. */+function channelWeights(channels: number): number[] {// L R C LFE Ls Rsif (channels === 6) return [1, 1, 1, 0, 1.41, 1.41];+ if (channels === 1) return [2];return Array.from({ length: channels }, () => 1);}@@ -238,12 +241,11 @@ export class LoudnessMeter {constructor(readonly sampleRate: number,- readonly channels: number,- opts: { weights?: number[] } = {}+ readonly channels: number) {this.stages = kWeighting(sampleRate);this.state = Array.from({ length: channels }, () => new Float64Array(8));- this.weights = opts.weights ?? channelWeights(channels);+ this.weights = channelWeights(channels);this.stepLength = Math.round(sampleRate * STEP_S);this.interp = new Interpolator(sampleRate);// The stream opens on silence: the first samples interpolate against@@ -340,19 +342,8 @@ export class LoudnessMeter {ext.set(tail, 0);truePeak = Math.max(truePeak, scanBetween(this.interp, ext, truePeak));}- const steps = this.steps;- const blocks: number[] = [];- for (let j = 0; j + BLOCK_STEPS <= steps.length; j++) {- let p = 0;- for (let k = 0; k < BLOCK_STEPS; k++) p += steps[j + k];- blocks.push(p / BLOCK_STEPS);- }- const shortTerm: number[] = [];- for (let j = 0; j + SHORT_TERM_STEPS <= steps.length; j++) {- let p = 0;- for (let k = 0; k < SHORT_TERM_STEPS; k++) p += steps[j + k];- shortTerm.push(p / SHORT_TERM_STEPS);- }+ const blocks = windowMeans(this.steps, BLOCK_STEPS);+ const shortTerm = windowMeans(this.steps, SHORT_TERM_STEPS);return {integratedLufs: integratedFrom(blocks),truePeakDbtp: 20 * Math.log10(truePeak || 1e-10),@@ -365,6 +356,17 @@ export class LoudnessMeter {}}+/** The mean power of every `n`-step window, one step apart. */+function windowMeans(steps: number[], n: number): number[] {+ const means: number[] = [];+ for (let j = 0; j + n <= steps.length; j++) {+ let p = 0;+ for (let k = 0; k < n; k++) p += steps[j + k];+ means.push(p / n);+ }+ return means;+}+/** The loudest point between samples in `x` that could beat `floor`. Points* whose window cannot reach it are never interpolated. */function scanBetween(interp: Interpolator, x: Float32Array, floor: number): number {@@ -396,7 +398,7 @@ export function integratedFrom(blocks: number[]): number {/** Loudness range from short-term powers (EBU Tech 3342): gate at −70 LUFS* and 20 LU under the mean, then the 10th to the 95th percentile. */-export function rangeFrom(shortTerm: number[]): number {+function rangeFrom(shortTerm: number[]): number {const abs = fromLufs(ABSOLUTE_GATE);const loud = shortTerm.filter((p) => p > abs);if (loud.length === 0) return 0;@@ -427,13 +429,24 @@ export function measureLoudness(channels: ArrayLike<number>[], sampleRate: numbe* whole mix measured on the page's thread. */export async function measureLoudnessSliced(channels: Float32Array[], sampleRate: number): Promise<LoudnessMeasure> {const meter = new LoudnessMeter(sampleRate, channels.length);+ for await (const slice of halfSeconds(channels, sampleRate, "view")) meter.push(slice);+ return meter.result();+}++/** How a slice of a buffer comes out: a view onto it, or a copy that later+ * writes to the buffer cannot change. */+type SliceKind = "view" | "copy";++/** Planar buffers in half-second slices, yielding the thread after each, so+ * a long mix never holds it for the whole of its length. */+async function* halfSeconds(channels: Float32Array[], sampleRate: number, kind: SliceKind): AsyncGenerator<Float32Array[]> {const slice = Math.max(1, Math.round(sampleRate / 2));const length = channels[0]?.length ?? 0;for (let i = 0; i < length; i += slice) {- meter.push(channels.map((c) => c.subarray(i, Math.min(length, i + slice))));+ const end = Math.min(length, i + slice);+ yield channels.map((c) => (kind === "view" ? c.subarray(i, end) : c.slice(i, end)));await new Promise<void>((r) => setTimeout(r, 0));}- return meter.result();}/** A measurement rounded for a reply: two decimals, and null where there is@@ -452,7 +465,7 @@ export function roundedLoudness(m: LoudnessMeasure) {/** What a master does to a mix: one static gain, and the limiter only when* the gained mix would cross the ceiling. */-export interface MasterPlan {+interface MasterPlan {gainDb: number;limit: boolean;/** The true peak the gain alone would leave, dBTP. */@@ -702,19 +715,10 @@ export async function masterInPlace(channels: Float32Array[],opts: { sampleRate: number; targetLufs: number; ceilingDbtp: number }): Promise<MasterReport> {- const slice = Math.max(1, Math.round(opts.sampleRate / 2));- const length = channels[0]?.length ?? 0;- const pause = () => new Promise<void>((r) => setTimeout(r, 0));let at = 0;return masterStream(- async function* () {- for (let i = 0; i < length; i += slice) {- // A copy: the limiter's writes land behind the read, on the same- // arrays.- yield channels.map((c) => c.slice(i, Math.min(length, i + slice)));- await pause();- }- },+ // Copies: the limiter's writes land behind the read, on the same arrays.+ () => halfSeconds(channels, opts.sampleRate, "copy"),(chunk) => {chunk.forEach((c, k) => channels[k].set(c, at));at += chunk[0].length;diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex e6555ed0..4ccbaa3d 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -1382,11 +1382,7 @@ export async function measureSourceLevel(const n = chunk.channels[0]?.length ?? 0;if (n > 0) {const rate = chunk.sampleRate;- const meter = (metered.meter ??= new LoudnessMeter(- rate,- chunk.channels.length,- chunk.channels.length === 1 ? { weights: [2] } : {}- ));+ const meter = (metered.meter ??= new LoudnessMeter(rate, chunk.channels.length));// A decode opens on the packet before `from` and may run past `to`;// only the asked span is measured.const a = Math.max(0, Math.round((from - chunk.timestamp) * rate));diff --git a/site/src/cut/lib/stems.test.ts b/site/src/cut/lib/stems.test.tsindex f63c3555..2ad7e1b6 100644--- a/site/src/cut/lib/stems.test.ts+++ b/site/src/cut/lib/stems.test.ts@@ -3,7 +3,8 @@ import { spawnSync } from "node:child_process";import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import os from "node:os";import path from "node:path";-import { crc32, pcm24, planStems, wavBytes, wavChunks, wavHeader, writeStoredZip } from "./stems";+import { crc32, pcm24, planStems, wavBytes, wavChunks, writeStoredZip } from "./stems";+import { wavHeader } from "./wav";const asset = (id: string, over: Record<string, unknown> = {}) => ({ id, type: "audio" as const, name: `${id}.mp3`, ...over });const video = (id: string) => ({ id, type: "video" as const, name: `${id}.mp4` });@@ -62,7 +63,7 @@ describe("stem plan", () => {describe("WAV", () => {test("a 24-bit header that counts its data", () => {- const h = new DataView(wavHeader(48000, 2, 48000).buffer);+ const h = new DataView(wavHeader(48000, 2, 48000, 24).buffer);expect(h.getUint32(40, true)).toBe(48000 * 2 * 3);expect(h.getUint16(34, true)).toBe(24);expect(wavBytes(48000, 2)).toBe(44 + 48000 * 6);diff --git a/site/src/cut/lib/stems.ts b/site/src/cut/lib/stems.tsindex e032427e..04a7b524 100644--- a/site/src/cut/lib/stems.ts+++ b/site/src/cut/lib/stems.ts@@ -13,6 +13,7 @@*/import { assetIsSilent, type AudioClip, type MediaAsset, type VideoClip } from "./types";+import { wavHeader } from "./wav";/** One stem: what it is called, the file it lands in, and what it carries —* the sound of every video clip (`lane` null), or one soundtrack lane. */@@ -88,37 +89,18 @@ export function planStems(doc: StemDoc): StemDef[] {/** Stems are 24-bit: headroom for a mix that peaks over full scale before* its master, and the width an audio editor works in. */-export const STEM_BYTES_PER_SAMPLE = 3;+const STEM_BYTES_PER_SAMPLE = 3;++/** Every stem's rate and layout, whichever renderer prints it: 48 kHz+ * stereo, what an audio editor opens a session at. */+export const STEM_RATE = 48000;+export const STEM_CHANNELS = 2;/** The size of a WAV file holding `frames` frames. */export function wavBytes(frames: number, channels: number): number {return 44 + frames * channels * STEM_BYTES_PER_SAMPLE;}-/** A 24-bit PCM WAV header. RIFF counts in 32 bits, so a stem past 4 GB- * (about four hours of stereo at 48 kHz) is refused. */-export function wavHeader(frames: number, channels: number, sampleRate: number): Uint8Array {- const data = frames * channels * STEM_BYTES_PER_SAMPLE;- if (data + 36 > 0xffffffff) throw new Error("A stem this long does not fit in a WAV file.");- const b = new Uint8Array(44);- const v = new DataView(b.buffer);- const ascii = (at: number, s: string) => [...s].forEach((ch, i) => (b[at + i] = ch.charCodeAt(0)));- ascii(0, "RIFF");- v.setUint32(4, 36 + data, true);- ascii(8, "WAVE");- ascii(12, "fmt ");- v.setUint32(16, 16, true);- v.setUint16(20, 1, true);- v.setUint16(22, channels, true);- v.setUint32(24, sampleRate, true);- v.setUint32(28, sampleRate * channels * STEM_BYTES_PER_SAMPLE, true);- v.setUint16(32, channels * STEM_BYTES_PER_SAMPLE, true);- v.setUint16(34, STEM_BYTES_PER_SAMPLE * 8, true);- ascii(36, "data");- v.setUint32(40, data, true);- return b;-}-/** Planar float samples [from, to) as interleaved 24-bit PCM. Past ±1 is* clamped; a sample that is not there (a stem shorter than the delivery)* is silence. */@@ -161,7 +143,7 @@ export function crc32(data: Uint8Array, crc = 0): number {/** A file in a stored (uncompressed) zip. PCM barely compresses, so every* entry is stored and the archive is written straight through, its size and* checksum known before its bytes go out. */-export interface ZipEntry {+interface ZipEntry {name: string;size: number;crc: number;@@ -187,7 +169,7 @@ const writer = (size: number) => {/** The local header that goes before an entry's bytes. A file of 4 GB or* more carries its sizes in a Zip64 field. */-export function zipLocalHeader(entry: ZipEntry): Uint8Array {+function zipLocalHeader(entry: ZipEntry): Uint8Array {const name = new TextEncoder().encode(entry.name);const big = entry.size >= MAX32;const w = writer(30 + name.length + (big ? 20 : 0));@@ -215,7 +197,7 @@ export function zipLocalHeader(entry: ZipEntry): Uint8Array {/** The central directory and end records for entries whose local headers* start at `offsets`, the directory itself starting at `at`. Zip64 records* join when a size or an offset passes 4 GB. */-export function zipDirectory(entries: ZipEntry[], offsets: number[], at: number): Uint8Array {+function zipDirectory(entries: ZipEntry[], offsets: number[], at: number): Uint8Array {const enc = new TextEncoder();const parts: Uint8Array[] = [];entries.forEach((e, i) => {@@ -334,7 +316,7 @@ export async function* wavChunks(count: number,sampleRate: number): AsyncGenerator<Uint8Array> {- yield wavHeader(frames, count, sampleRate);+ yield wavHeader(frames, count, sampleRate, STEM_BYTES_PER_SAMPLE * 8);const slice = Math.max(1, sampleRate);for (let i = 0; i < frames; i += slice) {yield pcm24(channels, i, Math.min(frames, i + slice), count);diff --git a/site/src/cut/lib/tts.ts b/site/src/cut/lib/tts.tsindex c48516ee..920d1c10 100644--- a/site/src/cut/lib/tts.ts+++ b/site/src/cut/lib/tts.ts@@ -2,6 +2,7 @@import { geminiModelRoleNames } from "@/lib/inference/gemini-models";import { bytesFromBase64 } from "./bytes";+import { wavHeader } from "./wav";import { readHostedError } from "./hostedError";import { hostedPost } from "./hosted";import { cutJudge } from "./chatRuntime";@@ -293,24 +294,7 @@ function assembleWav(clips: PcmClip[], offsets: number[]): Blob {data[i] = Math.max(-32768, Math.min(32767, mix[i]));}- const header = new DataView(new ArrayBuffer(44));- const write = (at: number, s: string) => {- for (let i = 0; i < s.length; i++) header.setUint8(at + i, s.charCodeAt(i));- };- write(0, "RIFF");- header.setUint32(4, 36 + data.byteLength, true);- write(8, "WAVE");- write(12, "fmt ");- header.setUint32(16, 16, true);- header.setUint16(20, 1, true); // PCM- header.setUint16(22, 1, true); // mono- header.setUint32(24, rate, true);- header.setUint32(28, rate * 2, true);- header.setUint16(32, 2, true);- header.setUint16(34, 16, true);- write(36, "data");- header.setUint32(40, data.byteLength, true);- return new Blob([header.buffer, data.buffer], { type: "audio/wav" });+ return new Blob([wavHeader(total, 1, rate, 16), data.buffer], { type: "audio/wav" });}export interface SpeechLayout {diff --git a/site/src/cut/lib/wav.ts b/site/src/cut/lib/wav.tsnew file mode 100644index 00000000..44d110bf--- /dev/null+++ b/site/src/cut/lib/wav.ts@@ -0,0 +1,36 @@+/** How a WAV's samples are coded: integer PCM or IEEE float. */+export type WavCoding = "pcm" | "float";++/** The WAV format tags RIFF names each coding by. */+const FORMAT_TAG: Record<WavCoding, number> = { pcm: 1, float: 3 };++/** The 44-byte RIFF header in front of `frames` interleaved frames. RIFF+ * counts in 32 bits, so data past 4 GB is refused. */+export function wavHeader(+ frames: number,+ channels: number,+ sampleRate: number,+ bits: number,+ coding: WavCoding = "pcm"+): Uint8Array<ArrayBuffer> {+ const bytes = bits / 8;+ const data = frames * channels * bytes;+ if (data + 36 > 0xffffffff) throw new Error("Audio this long does not fit in a WAV file.");+ const b = new Uint8Array(44);+ const v = new DataView(b.buffer);+ const ascii = (at: number, s: string) => [...s].forEach((ch, i) => (b[at + i] = ch.charCodeAt(0)));+ ascii(0, "RIFF");+ v.setUint32(4, 36 + data, true);+ ascii(8, "WAVE");+ ascii(12, "fmt ");+ v.setUint32(16, 16, true);+ v.setUint16(20, FORMAT_TAG[coding], true);+ v.setUint16(22, channels, true);+ v.setUint32(24, sampleRate, true);+ v.setUint32(28, sampleRate * channels * bytes, true);+ v.setUint16(32, channels * bytes, true);+ v.setUint16(34, bits, true);+ ascii(36, "data");+ v.setUint32(40, data, true);+ return b;+}diff --git a/site/src/cut/server/cloud/exportJobs.test.ts b/site/src/cut/server/cloud/exportJobs.test.tsnew file mode 100644index 00000000..97861d95--- /dev/null+++ b/site/src/cut/server/cloud/exportJobs.test.ts@@ -0,0 +1,77 @@+import { beforeEach, expect, mock, test } from "bun:test";+import { stubModule } from "@/lib/testing/stubModule";++// A browser render that lands with stems registers each file at its own size,+// and the account is charged their sum once. A released render takes only the+// files no row registered: a stems zip from an older export with the same name+// stays.+type Row = { id: string; kind: string; projectId: string; outName: string; state: string; spec: unknown };+let job: Row;+const sizes = new Map<string, number>();+const registered = new Set<string>();+const created: { r2Key: string; bytes: bigint }[] = [];+const deleted: string[] = [];+const added: number[] = [];+const tx = {+ cutMediaObject: {+ createMany: mock(async ({ data }: { data: { r2Key: string; bytes: bigint }[] }) => {+ created.push(...data);+ return { count: data.length };+ }),+ },+ cutRenderJob: { update: mock(async () => ({})) },+};+const prisma = {+ cutRenderJob: {+ findFirst: mock(async () => job),+ updateMany: mock(async () => ({ count: 1 })),+ },+ cutMediaObject: {+ findMany: mock(async ({ where }: { where: { r2Key: { in: string[] } } }) =>+ where.r2Key.in.filter((k) => registered.has(k)).map((r2Key) => ({ r2Key }))+ ),+ },+ $transaction: mock(async (fn: (t: typeof tx) => Promise<unknown>) => fn(tx)),+};+await stubModule<typeof import("@/lib/prisma")>("@/lib/prisma", import.meta.url, { prisma: prisma as never });+await stubModule<typeof import("./r2")>("./r2", import.meta.url, {+ head: (async (key: string) => (sizes.has(key) ? { bytes: sizes.get(key)!, mime: "", etag: "" } : null)) as never,+ del: (async (keys: string[]) => { deleted.push(...keys); }) as never,+ projectExportKey: ((userId: string, projectId: string, name: string) => `cut/${userId}/${projectId}/exports/${name}`) as never,+});+await stubModule<typeof import("./usage")>("./usage", import.meta.url, {+ quotaCheck: (async () => null) as never,+ addUsage: (async (_tx: unknown, _user: string, delta: number) => { added.push(delta); }) as never,+});+const { jobsCloud } = await import("./jobs");++const MB = 1024 * 1024;+const post = (body: unknown) => new Request("http://x", { method: "POST", body: JSON.stringify(body) });++beforeEach(() => {+ sizes.clear();+ registered.clear();+ created.length = 0;+ deleted.length = 0;+ added.length = 0;+});++test("a render with stems registers each file at its own size and charges the sum once", async () => {+ job = { id: "j", kind: "export", projectId: "p", outName: "Trip.mp4", state: "running", spec: { client: true, stems: true } };+ sizes.set("cut/u/p/exports/Trip.mp4", 50 * MB);+ sizes.set("cut/u/p/exports/Trip stems.zip", 400 * MB);+ const res = await jobsCloud.exportClientComplete("u", post({ jobId: "j" }));+ expect(res.status).toBe(200);+ expect(created.map((c) => [c.r2Key, Number(c.bytes)])).toEqual([+ ["cut/u/p/exports/Trip.mp4", 50 * MB],+ ["cut/u/p/exports/Trip stems.zip", 400 * MB],+ ]);+ expect(added).toEqual([450 * MB]);+});++test("a released plain render leaves a registered stems zip of the same name", async () => {+ job = { id: "j", kind: "export", projectId: "p", outName: "Trip.mp4", state: "running", spec: { client: true } };+ registered.add("cut/u/p/exports/Trip stems.zip");+ await jobsCloud.exportClientRelease("u", "j");+ expect(deleted).toEqual(["cut/u/p/exports/Trip.mp4"]);+});diff --git a/site/src/cut/server/cloud/jobs.ts b/site/src/cut/server/cloud/jobs.tsindex 105e0645..7181e798 100644--- a/site/src/cut/server/cloud/jobs.ts+++ b/site/src/cut/server/cloud/jobs.ts@@ -10,7 +10,7 @@ import { getProject } from "./projects";import { MEDIA_REDIRECT_HEADERS, mediaObjectUrl, mediaUrlLifetime } from "./mediaCdn";import { del, head, overlayKey, overlayPrefix, presignPut, projectExportKey } from "./r2";import { contentTypeFor } from "../serveFile";-import { containerOfName, deliveryContainer, exportBaseName, specMediaFiles, STEMS_MIME, stemsArchiveName, type ExportContainer } from "../../lib/exportDelivery";+import { containerOfName, deliveryContainer, exportBaseName, jobWantsStems, specMediaFiles, STEMS_MIME, stemsArchiveName, type ExportContainer, type ExportFiles } from "../../lib/exportDelivery";import { LOUDNESS_IDS, type LoudnessId } from "../../lib/loudnessSettings";import type { DocExportAudio } from "../../lib/exportPresets";import { addUsage, outputByteCeiling, quotaCheck } from "./usage";@@ -143,13 +143,17 @@ async function dropUnregisteredExport(row: Pick<JobRow, "projectId" | "outName">): Promise<void> {if (!row.projectId || !row.outName) return;- const key = projectExportKey(userId, row.projectId, row.outName);- const registered = await prisma.cutMediaObject.findUnique({- where: { r2Key: key },- select: { id: true },++ // Stems land before the file and go with it. Each key is weighed on its own:+ // a plain export can share its stems' name with a zip an older export+ // registered, and that zip stays.+ const keys = [row.outName, stemsArchiveName(row.outName)].map((name) => projectExportKey(userId, row.projectId!, name));+ const registered = await prisma.cutMediaObject.findMany({+ where: { r2Key: { in: keys } },+ select: { r2Key: true },});- // Stems land before the file, and go with it.- if (!registered) await del([key, projectExportKey(userId, row.projectId, stemsArchiveName(row.outName))]);+ const kept = new Set(registered.map((r) => r.r2Key));+ await del(keys.filter((k) => !kept.has(k)));}/** Engine job status ("queued" | "running" | "done" | "error") from a row's@@ -194,8 +198,8 @@ async function exportName(projectId: string,baseName: string,container: ExportContainer | undefined,- tx: Prisma.TransactionClient | typeof prisma = prisma,- stems = false+ tx: Prisma.TransactionClient | typeof prisma,+ claim: ExportFiles) {// The spec's container names the extension, the same way the engine's// `containerExtension` does; a name alone (a tab's own claim) says it by its@@ -221,7 +225,7 @@ async function exportName(]);for (let n = 1; ; n++) {const candidate = n === 1 ? `${base}${ext}` : `${base} ${n}${ext}`;- if (!taken.has(candidate) && !(stems && taken.has(stemsArchiveName(candidate)))) return candidate;+ if (!taken.has(candidate) && !(claim === "fileAndStems" && taken.has(stemsArchiveName(candidate)))) return candidate;}}@@ -242,7 +246,7 @@ export async function queueDocExport(// What the render may land. The worker holds its own output to it, so a// queued export cannot pass this gate and then write past the account's cap.const maxBytes = await exportByteCeiling(userId);- const outName = await exportName(userId, projectId, project.name, undefined, prisma, audio.stems === true);+ const outName = await exportName(userId, projectId, project.name, undefined, prisma, audio.stems ? "fileAndStems" : "file");const row = await prisma.cutRenderJob.create({data: {userId,@@ -478,7 +482,7 @@ export const jobsCloud = {body.outName?.trim() || project?.name || "export",body.spec.container,prisma,- (body.spec.stemPlan?.length ?? 0) > 0+ body.spec.stemPlan?.length ? "fileAndStems" : "file"): target === "hls"? "master.m3u8"@@ -570,7 +574,7 @@ export const jobsCloud = {async exportClientPresign(userId: string, req: Request) {try {const body = (await req.json()) as { projectId?: string; outName?: string; bytes?: number; stems?: boolean };- const stems = body.stems === true;+ const claim: ExportFiles = body.stems === true ? "fileAndStems" : "file";const projectId = body.projectId;if (!projectId) return err("projectId is required.", 400);if (!(await getProject(userId, projectId))) return err("Project not found.", 400);@@ -591,7 +595,7 @@ export const jobsCloud = {// completion would then fail on the unique key with the render already// spent. The row is the reservation.const row = await prisma.$transaction(async (tx) => {- const outName = await exportName(userId, projectId, body.outName ?? "export.mp4", undefined, tx, stems);+ const outName = await exportName(userId, projectId, body.outName ?? "export.mp4", undefined, tx, claim);return tx.cutRenderJob.create({data: {userId,@@ -601,7 +605,7 @@ export const jobsCloud = {// nothing ever claims it.state: "running",progress: 0,- spec: stems ? { client: true, stems: true } : { client: true },+ spec: claim === "fileAndStems" ? { client: true, stems: true } : { client: true },outName,},});@@ -609,7 +613,7 @@ export const jobsCloud = {const key = projectExportKey(userId, projectId, row.outName!);const type = containerOfName(row.outName!).mime;// The stems zip lands beside the file, under the name claimed with it.- const stemsUrl = stems+ const stemsUrl = claim === "fileAndStems"? await presignPut(projectExportKey(userId, projectId, stemsArchiveName(row.outName!)), STEMS_MIME): undefined;return Response.json({@@ -642,7 +646,10 @@ export const jobsCloud = {if (!body.jobId) return err("jobId is required.", 400);// The row the presign reserved carries the project and the name, so the// client cannot claim a different one at completion time.- const row = await findJob(userId, body.jobId);+ const row = await prisma.cutRenderJob.findFirst({+ where: { id: body.jobId, userId },+ select: { ...jobRowSelect, spec: true },+ });if (!row || row.kind !== "export" || !row.projectId || !row.outName) {return err("Export not found.", 400);}@@ -651,15 +658,18 @@ export const jobsCloud = {const key = projectExportKey(userId, projectId, outName);const object = await head(key);if (!object) return err("The export was not uploaded.", 400);- // A render that asked for stems uploaded them before the file.- const spec = (- await prisma.cutRenderJob.findUnique({ where: { id: row.id }, select: { spec: true } })- )?.spec as { stems?: boolean } | null;- const stemsName = spec?.stems ? stemsArchiveName(outName) : null;- const stemsKey = stemsName ? projectExportKey(userId, projectId, stemsName) : null;- const stemsObject = stemsKey ? await head(stemsKey) : null;- if (stemsKey && !stemsObject) return err("The stems were not uploaded.", 400);- const bytes = object.bytes + (stemsObject?.bytes ?? 0);++ // A render that asked for stems uploaded them before the file. Each file+ // is its own row at its own size, so deleting one frees what it held.+ const files = [{ r2Key: key, fileName: outName, mime: containerOfName(outName).mime, bytes: object.bytes }];+ if (jobWantsStems(row.spec)) {+ const stemsName = stemsArchiveName(outName);+ const stemsKey = projectExportKey(userId, projectId, stemsName);+ const stemsObject = await head(stemsKey);+ if (!stemsObject) return err("The stems were not uploaded.", 400);+ files.push({ r2Key: stemsKey, fileName: stemsName, mime: STEMS_MIME, bytes: stemsObject.bytes });+ }+ const bytes = files.reduce((sum, f) => sum + f.bytes, 0);// The account is charged what landed, so the quota is checked against it.// A file past the ceiling is dropped rather than charged: the row would// otherwise put the account over its cap for good, and the bytes it holds@@ -674,32 +684,16 @@ export const jobsCloud = {return over;}await prisma.$transaction(async (tx) => {- await tx.cutMediaObject.create({- data: {+ await tx.cutMediaObject.createMany({+ data: files.map((f) => ({+ ...f,userId,projectId,- r2Key: key,- fileName: outName,- mime: containerOfName(outName).mime,- bytes: BigInt(bytes),+ bytes: BigInt(f.bytes),kind: "export",uploadState: "complete",- },+ })),});- if (stemsKey && stemsName && stemsObject) {- await tx.cutMediaObject.create({- data: {- userId,- projectId,- r2Key: stemsKey,- fileName: stemsName,- mime: STEMS_MIME,- bytes: BigInt(stemsObject.bytes),- kind: "export",- uploadState: "complete",- },- });- }await addUsage(tx, userId, bytes);await tx.cutRenderJob.update({where: { id: row.id },diff --git a/site/src/cut/server/exportPipeline.test.ts b/site/src/cut/server/exportPipeline.test.tsindex d8b53185..7987afa7 100644--- a/site/src/cut/server/exportPipeline.test.ts+++ b/site/src/cut/server/exportPipeline.test.ts@@ -27,6 +27,8 @@ const probedDims = new Map<string, { width: number; height: number }>();/** The masters and stem packs the last run asked for. */let mastered: { input: string; output: string; opts: Parameters<ExportPipelineIO["masterRawMix"]>[2] }[] = [];let packed: { files: { path: string; name: string }[]; output: string }[] = [];+/** A pass to cancel the job during, the way the dock's cancel lands mid-render. */+let cancelDuring: "master" | "stems" | null = null;const runsFor = async (over: Partial<ExportSpec>): Promise<string[][]> => {const ffmpegCalls: string[][] = [];@@ -55,10 +57,12 @@ const runsFor = async (over: Partial<ExportSpec>): Promise<string[][]> => {codec === "hevc" ? "libx265" : codec.startsWith("prores") ? "prores_ks" : "libx264",masterRawMix: async (input, output, opts) => {mastered.push({ input, output, opts });+ if (cancelDuring === "master") job.error = "Export canceled.";return {} as Awaited<ReturnType<ExportPipelineIO["masterRawMix"]>>;},packStems: async (files, output) => {packed.push({ files, output });+ if (cancelDuring === "stems") job.error = "Export canceled.";},runFfmpeg: async (_job, args) => {ffmpegCalls.push(args);@@ -1555,6 +1559,25 @@ describe("a mastered delivery", () => {expect(mux[mux.length - 1]).toBe("/tmp/graph-test/out.mp4");});+ test("a mono delivery masters its mono mix, the layout the tab masters", async () => {+ const runs = await runsFor({ clips: [clip("a.mp4")], loudness: -14, truePeakCeiling: -1, audioChannels: 1, audioSampleRate: 48000 });+ const enc = runs.find((a) => a.includes("-filter_complex"))!;+ const mix = enc.slice(enc.indexOf("pcm_f32le"));+ expect(arg(mix, "-ac")).toBe("1");+ expect(arg(mix, "-ar")).toBe("48000");+ expect(mastered[0].opts).toMatchObject({ sampleRate: 48000, channels: 1 });+ expect(arg(runs[runs.length - 1], "-ac")).toBe("1");+ });++ test("a cancel during the master ends the render before the file is written", async () => {+ cancelDuring = "master";+ try {+ await expect(runsFor({ clips: [clip("a.mp4")], loudness: -14, truePeakCeiling: -1 })).rejects.toThrow("Export canceled.");+ } finally {+ cancelDuring = null;+ }+ });+test("an unmastered delivery keeps its one-pass sound and plain remux", async () => {const runs = await runsFor({ clips: [clip("a.mp4")] });expect(mastered).toEqual([]);@@ -1623,6 +1646,23 @@ describe("stems", () => {expect(graph.find((l) => l.endsWith("[stem2]"))).toContain("atrim=start=1.000:end=3.000");});+ test("a cancel while the stems pack ends the render before the file is written", async () => {+ cancelDuring = "stems";+ try {+ await expect(runsFor({ ...stemSpec, duration: 4 })).rejects.toThrow("Export canceled.");+ } finally {+ cancelDuring = null;+ }+ });++ test("stems print at 48 kHz stereo whatever the delivery carries", async () => {+ const runs = await runsFor({ ...stemSpec, duration: 4, audioChannels: 1, audioSampleRate: 44100 });+ const enc = runs.find((a) => a.includes("-filter_complex"))!;+ const stem = enc.slice(enc.indexOf("pcm_s24le"));+ expect(arg(stem, "-ar")).toBe("48000");+ expect(arg(stem, "-ac")).toBe("2");+ });+test("a spec that asks for stems renders even when its source could copy", async () => {const runs = await runsFor({ ...stemSpec, sourceSegments: [{ file: "a.mp4", from: 0, to: 4 }], target: "export" });expect(runs.some((a) => a.includes("-filter_complex"))).toBe(true);diff --git a/site/src/cut/server/exportPipeline.ts b/site/src/cut/server/exportPipeline.tsindex b4d45fd7..f5ab209e 100644--- a/site/src/cut/server/exportPipeline.ts+++ b/site/src/cut/server/exportPipeline.ts@@ -14,7 +14,7 @@ import { withSpecColors } from "./fileColor";import { alignRecording } from "./soundAlign";import { shiftSpan, type SpecSound } from "../lib/soundSource";import { masterRawMix, packStems } from "./exportAudio";-import type { StemDef } from "../lib/stems";+import { STEM_CHANNELS, STEM_RATE, type StemDef } from "../lib/stems";import { CLIP_MAX_ZOOM, regionPx, TRANSITION_XFADE, TRANSITION_ZOOM, type ColorGrade, type TransitionStyle } from "../lib/types";import { audioFxFilters, buildClipLut, buildTransferLut, CLARITY_EPS, compositeSpaceFor, detailActive, detailGain, detailRadius, effectFilterLines, graphicsToHlg, hexToHlgHex, hlgToPq, isAudioEffect, lookFilterLines, lutToCube, mirrorRetimable, parseLutFile, recipeKey, retimeOf, shortestTurn, slowRuns, srcSpan, sortedKeys, soundFilters, type ChainChroma, type ClipColorRecipe, type ClipEffect, type ClipSound, type CodeFormat, type OutputSpace, type OverlayKey, type ParsedLut, type Retime, type SpeedNode } from "@donkeycut/effects-kit";@@ -394,8 +394,6 @@ export interface ExportSpec {/** The rate the graph mixes at: every read resamples to it. */const MIX_RATE = 44100;-/** The rate stems are written at, the rate the tab's own stems mix at. */-const STEM_RATE = 48000;/** What the pipeline needs from its caller's job record: the staging dir the* overlay PNGs were written into, the output path, and the mutable fields the@@ -768,6 +766,11 @@ export function runFfmpeg(});}+/** End a render whose job was canceled or timed out between passes. */+function stopIfCanceled(job: RenderHandle) {+ if (job.error) throw new Error(job.error);+}+/** The pipeline's edges into the world: staged-media checks, list-file* writes, stream probes, the encoder probe, and the ffmpeg runs. `runExport`* takes them as a parameter so the filtergraph tests can build the real graph@@ -2219,7 +2222,7 @@ export async function runExport();} else {filters.push(- `anullsrc=r=44100:cl=stereo,atrim=0:${num(dur)},asetpts=PTS-STARTPTS[a${j}]`+ `anullsrc=r=${MIX_RATE}:cl=stereo,atrim=0:${num(dur)},asetpts=PTS-STARTPTS[a${j}]`);}});@@ -2923,7 +2926,7 @@ export async function runExport(const stemLabels = stemPlan.map((_, i) => {const ins = stemInputs[i];let label = `stm${i}`;- if (ins.length === 0) filters.push(`anullsrc=r=44100:cl=stereo,atrim=0:${num(spec.duration)}[${label}]`);+ if (ins.length === 0) filters.push(`anullsrc=r=${MIX_RATE}:cl=stereo,atrim=0:${num(spec.duration)}[${label}]`);else if (ins.length === 1) label = ins[0];else {filters.push(@@ -2943,16 +2946,21 @@ export async function runExport("-map", `[${label}]`,"-c:a", "pcm_s24le","-ar", String(STEM_RATE),- "-ac", "2",+ "-ac", String(STEM_CHANNELS),"-t", num(span),stemFiles[i].path,]);// A mastered delivery takes its sound out of this pass as raw float, so the// master can run the tab's own loudness code over it before it is encoded.+ // The raw mix comes out in the delivery's own rate and layout, so the+ // master weighs the same channels the tab's master does: a mono delivery+ // is measured as mono, a 5.1 one with its surround weights.const master = spec.loudness !== undefined;if (master && spec.truePeakCeiling === undefined) throw new Error("A mastered export needs its true-peak ceiling.");const mixPath = path.join(job.tmpDir, "mix.f32");+ const mixRate = spec.audioSampleRate ?? MIX_RATE;+ const mixChannels = spec.audioChannels ?? 2;const enc = await io.videoEncoder(spec.codec ?? "h264");@@ -2974,23 +2982,30 @@ export async function runExport("-t", num(span),encodePath,...(master- ? ["-map", `[${aLabel}]`, "-c:a", "pcm_f32le", "-ar", String(MIX_RATE), "-ac", "2", "-f", "f32le", "-t", num(span), mixPath]+ ? ["-map", `[${aLabel}]`, "-c:a", "pcm_f32le", "-ar", String(mixRate), "-ac", String(mixChannels), "-f", "f32le", "-t", num(span), mixPath]: []),...stemOutputs,],(t) => (job.progress = Math.min(0.99, t / Math.max(0.1, span))));+ // The master and the stems pack run in this process, out of a cancel's+ // reach, so the job is checked after each: a canceled render never goes on+ // to write its file.const masteredPath = path.join(job.tmpDir, "master.f32");if (master) {await io.masterRawMix(mixPath, masteredPath, {- sampleRate: MIX_RATE,- channels: 2,+ sampleRate: mixRate,+ channels: mixChannels,targetLufs: spec.loudness!,ceilingDbtp: spec.truePeakCeiling!,});+ stopIfCanceled(job);+ }+ if (stemPlan.length > 0) {+ await io.packStems(stemFiles, job.stemsPath!);+ stopIfCanceled(job);}- if (stemPlan.length > 0) await io.packStems(stemFiles, job.stemsPath!);// ffmpeg's autorotation already baked each source's display matrix into the// pixels, so the encode's frames are upright. But for a complex filtergraph it@@ -3007,7 +3022,7 @@ export async function runExport("-i", encodePath,...(master? [- "-f", "f32le", "-ar", String(MIX_RATE), "-ac", "2", "-i", masteredPath,+ "-f", "f32le", "-ar", String(mixRate), "-ac", String(mixChannels), "-i", masteredPath,"-map", "0:v", "-map", "1:a", "-c:v", "copy", ...audioCodecArgs(spec), "-t", num(span),]: ["-map", "0", "-c", "copy"]),diff --git a/site/src/cut/server/http/export.ts b/site/src/cut/server/http/export.tsindex fc3e0aaa..12c36599 100644--- a/site/src/cut/server/http/export.ts+++ b/site/src/cut/server/http/export.ts@@ -48,7 +48,7 @@ export const exportApi = {stems?: boolean;};if (!body.projectId) return Response.json({ error: "Missing project." }, { status: 400 });- const job = await createClientJob(body.projectId, body.container, body.name?.trim() || undefined, body.stems === true);+ const job = await createClientJob(body.projectId, body.container, body.name?.trim() || undefined, body.stems === true ? "fileAndStems" : "file");if (job.status === "error") return Response.json({ error: job.error }, { status: 400 });return Response.json({ id: job.id, outName: job.outName });} catch (e) {diff --git a/site/src/cut/server/jobs.test.ts b/site/src/cut/server/jobs.test.tsindex 74190935..5ec498dd 100644--- a/site/src/cut/server/jobs.test.ts+++ b/site/src/cut/server/jobs.test.ts@@ -115,7 +115,7 @@ describe("stems beside a tab render", () => {});test("the stems land under the video's name, before the video settles the job", async () => {- const job = await jobs.createClientJob(projectId, "mp4", undefined, true);+ const job = await jobs.createClientJob(projectId, "mp4", undefined, "fileAndStems");expect(job.outName).toBe("Stem Render.mp4");expect(await jobs.completeClientStems(job.id, bytes("zip bytes"))).not.toBeNull();const zip = path.join(projects.exportsDir(projectId), "Stem Render stems.zip");@@ -130,7 +130,7 @@ describe("stems beside a tab render", () => {const plain = await jobs.createClientJob(projectId, "mp4");expect(plain.outName).toBe("Stem Render 2.mp4");jobs.releaseClientJob(plain.id);- const stemmed = await jobs.createClientJob(projectId, "mp4", undefined, true);+ const stemmed = await jobs.createClientJob(projectId, "mp4", undefined, "fileAndStems");expect(stemmed.outName).toBe("Stem Render 3.mp4");jobs.releaseClientJob(stemmed.id);});@@ -140,7 +140,7 @@ describe("stems beside a tab render", () => {expect(await jobs.completeClientStems(plain.id, bytes("zip"))).toBeNull();jobs.releaseClientJob(plain.id);- const job = await jobs.createClientJob(projectId, "mov", undefined, true);+ const job = await jobs.createClientJob(projectId, "mov", undefined, "fileAndStems");await jobs.completeClientStems(job.id, bytes("zip"));const zip = path.join(projects.exportsDir(projectId), jobs.getJob(job.id)!.outName.replace(/\.mov$/, " stems.zip"));expect(await exists(zip)).toBe(true);@@ -151,6 +151,33 @@ describe("stems beside a tab render", () => {});});+describe("an engine render canceled late", () => {+ test("a render that finishes after its cancel ends canceled and leaves no file", async () => {+ const projectId = (await projects.createProject("Late Cancel")).id;+ let finish = () => {};+ const render = spyOn(pipeline, "runExport").mockImplementation(async (job) => {+ // The encode pass has exited; the master runs on in this process.+ job.proc = { kill: () => true } as unknown as pipeline.RenderHandle["proc"];+ await new Promise<void>((resolve) => (finish = resolve));+ await writeFile(job.outPath, "mp4");+ });+ try {+ const form = new FormData();+ form.set("spec", JSON.stringify({ projectId, target: "export" }));+ const job = await jobs.createJob(form);+ while (job.status === "queued") await new Promise((r) => setTimeout(r, 5));+ jobs.cancelJob(job.id);+ finish();+ await new Promise((r) => setTimeout(r, 20));+ expect(job.status).toBe("error");+ expect(job.error).toBe("Export canceled.");+ expect(await exists(job.outPath)).toBe(false);+ } finally {+ render.mockRestore();+ }+ });+});+describe("preview publication", () => {async function request(projectId: string) {const form = new FormData();diff --git a/site/src/cut/server/jobs.ts b/site/src/cut/server/jobs.tsindex edc1c08d..baee6975 100644--- a/site/src/cut/server/jobs.ts+++ b/site/src/cut/server/jobs.ts@@ -5,7 +5,7 @@ import path from "node:path";import { assertLocalRuntime } from "./local-only";import { createJobRegistry } from "./jobRegistry";import { containerExtension, runExport, type ExportSpec } from "./exportPipeline";-import { exportBaseName, stemsArchiveName } from "../lib/exportDelivery";+import { exportBaseName, stemsArchiveName, type ExportFiles } from "../lib/exportDelivery";import { exportsDir, mediaPath, projectDir, readProject, setActiveJobGuard } from "./projects";import { errorMessage } from "./util";@@ -140,6 +140,9 @@ function startRun(job: Job, spec: ExportSpec) {job.startedAt = Date.now();void runExport(job, spec, (file) => mediaPath(spec.projectId, file)).then(async () => {+ // A cancel that landed after the last pass settled the row already;+ // its file goes the way a failed render's does.+ if (job.status !== "running") throw new Error(job.error ?? "Export canceled.");if (job.target === "preview") await publishPreview(job);job.status = "done";})@@ -217,7 +220,7 @@ export function cancelJob(id: string) {/** Export file named after the project, with a " 2", " 3"… suffix when the* name is already taken by a file on disk or an export still in flight. An* export with stems needs its stems' name free too. */-async function exportName(projectId: string, projectName: string, ext: string, stems = false) {+async function exportName(projectId: string, projectName: string, ext: string, files: ExportFiles) {const base = exportBaseName(projectName);const taken = new Set(await readdir(exportsDir(projectId)).catch(() => [] as string[])@@ -232,7 +235,7 @@ async function exportName(projectId: string, projectName: string, ext: string, s}for (let n = 1; ; n++) {const candidate = n === 1 ? `${base}${ext}` : `${base} ${n}${ext}`;- if (!taken.has(candidate) && !(stems && taken.has(stemsArchiveName(candidate)))) return candidate;+ if (!taken.has(candidate) && !(files === "fileAndStems" && taken.has(stemsArchiveName(candidate)))) return candidate;}}@@ -240,10 +243,10 @@ async function exportName(projectId: string, projectName: string, ext: string, s// its outName is assigned, so two jobs racing through their first awaits could// otherwise both claim "<Project>.mp4" and overwrite each other's render.let namingQueue: Promise<unknown> = Promise.resolve();-function claimExportName(job: Job, projectName: string, ext: string, stems = false): Promise<void> {+function claimExportName(job: Job, projectName: string, ext: string, files: ExportFiles): Promise<void> {const claim = namingQueue.then(async () => {- job.outName = await exportName(job.projectId, projectName, ext, stems);- if (stems) job.stemsPath = path.join(exportsDir(job.projectId), stemsArchiveName(job.outName));+ job.outName = await exportName(job.projectId, projectName, ext, files);+ if (files === "fileAndStems") job.stemsPath = path.join(exportsDir(job.projectId), stemsArchiveName(job.outName));});namingQueue = claim.catch(() => {});return claim;@@ -299,7 +302,7 @@ export async function createJob(form: FormData): Promise<Job> {if (!doc) throw new Error("Project not found.");job.projectName = doc.name;if (preview) job.outName = "preview.mp4";- else await claimExportName(job, spec.name ?? doc.name, containerExtension(spec), !!spec.stemPlan?.length);+ else await claimExportName(job, spec.name ?? doc.name, containerExtension(spec), spec.stemPlan?.length ? "fileAndStems" : "file");job.outPath = path.join(preview ? path.join(projectDir(spec.projectId), ".previews") : exportsDir(spec.projectId),preview ? `${job.id}.mp4` : job.outName@@ -351,8 +354,8 @@ export async function createClientJob(projectId: string,container: ExportSpec["container"],name?: string,- /** The tab hands in a stems zip beside the file. */- stems = false+ /** Whether the tab hands in a stems zip beside the file. */+ files: ExportFiles = "file"): Promise<Job> {assertLocalRuntime();sweepClientJobs();@@ -377,7 +380,7 @@ export async function createClientJob(const doc = await readProject(projectId);if (!doc) throw new Error("Project not found.");job.projectName = doc.name;- await claimExportName(job, name ?? doc.name, containerExtension({ container }), stems);+ await claimExportName(job, name ?? doc.name, containerExtension({ container }), files);job.outPath = path.join(exportsDir(projectId), job.outName);await mkdir(path.dirname(job.outPath), { recursive: true });} catch (err) {diff --git a/site/src/cut/server/retimeAudio.ts b/site/src/cut/server/retimeAudio.tsindex 7428222e..423fe0dd 100644--- a/site/src/cut/server/retimeAudio.ts+++ b/site/src/cut/server/retimeAudio.ts@@ -14,6 +14,7 @@import { srcSpan, type Retime } from "@donkeycut/effects-kit";import { fitSpan } from "../lib/retimeFit";+import { wavHeader } from "../lib/wav";import { num } from "./util";/** The bake's sample rate: what the graphs resample every input to. */@@ -109,25 +110,8 @@ export async function bakeRetimedAudio(export function wavFloat32(channels: Float32Array[], sampleRate: number): Uint8Array {const n = channels[0]?.length ?? 0;const ch = Math.max(1, channels.length);- const dataBytes = n * ch * 4;- const buf = new ArrayBuffer(44 + dataBytes);- const view = new DataView(buf);- const ascii = (at: number, s: string) => {- for (let i = 0; i < s.length; i++) view.setUint8(at + i, s.charCodeAt(i));- };- ascii(0, "RIFF");- view.setUint32(4, 36 + dataBytes, true);- ascii(8, "WAVE");- ascii(12, "fmt ");- view.setUint32(16, 16, true);- view.setUint16(20, 3, true); // IEEE float- view.setUint16(22, ch, true);- view.setUint32(24, sampleRate, true);- view.setUint32(28, sampleRate * ch * 4, true);- view.setUint16(32, ch * 4, true);- view.setUint16(34, 32, true);- ascii(36, "data");- view.setUint32(40, dataBytes, true);+ const buf = new ArrayBuffer(44 + n * ch * 4);+ new Uint8Array(buf).set(wavHeader(n, ch, sampleRate, 32, "float"));const samples = new Float32Array(buf, 44, n * ch);for (let i = 0; i < n; i++)for (let c = 0; c < ch; c++) samples[i * ch + c] = channels[c]?.[i] ?? 0;diff --git a/site/src/cut/worker/exportJob.ts b/site/src/cut/worker/exportJob.tsindex e835c554..de9b1b3a 100644--- a/site/src/cut/worker/exportJob.ts+++ b/site/src/cut/worker/exportJob.ts@@ -9,7 +9,7 @@ import { prisma, registerObjectIn, storageTransaction, type ClaimedJob } from ".import { deleteObjects, downloadToFile, exportKey, mediaKey, mimeFor, uploadFile } from "./r2";import { overlayKey } from "../server/cloud/r2";import { STORAGE_FULL } from "../lib/operationFailure";-import { specMediaFiles, stemsArchiveName } from "../lib/exportDelivery";+import { specMediaFiles, STEMS_MIME, stemsArchiveName } from "../lib/exportDelivery";import { runnerSession } from "./session";import { bindCutColor } from "../lib/colorSettings";import { bindCutSound } from "../lib/soundSettings";@@ -213,30 +213,28 @@ export async function runExportJob((await stat(handle.outPath)).size + (handle.stemsPath ? (await stat(handle.stemsPath)).size : 0);if (size > stored.maxBytes) throw new Error(STORAGE_FULL);}- const stemsKey = stemsName ? exportKey(job.userId, projectId, stemsName) : null;- const uploaded = [key, ...(stemsKey ? [stemsKey] : [])];+ // The file and, when asked for, its stems: each its own object and row,+ // charged at its own size.+ const files = [+ { key, path: handle.outPath, fileName: outName, mime, kind: preview ? "preview" : "export" },+ ...(stemsName+ ? [{ key: exportKey(job.userId, projectId, stemsName), path: handle.stemsPath!, fileName: stemsName, mime: STEMS_MIME, kind: "export" }]+ : []),+ ] as const;+ const uploaded = files.map((f) => f.key);try {- const bytes = await uploadFile(key, handle.outPath, mime);- const stemsBytes = stemsKey ? await uploadFile(stemsKey, handle.stemsPath!, mimeFor(stemsName!)) : 0;+ const sizes: number[] = [];+ for (const f of files) sizes.push(await uploadFile(f.key, f.path, f.mime));await storageTransaction(async (tx) => {- await registerObjectIn(tx, {- userId: job.userId,- projectId,- r2Key: key,- fileName: outName,- mime,- bytes,- kind: preview ? "preview" : "export",- });- if (stemsKey) {+ for (const [i, f] of files.entries()) {await registerObjectIn(tx, {userId: job.userId,projectId,- r2Key: stemsKey,- fileName: stemsName!,- mime: mimeFor(stemsName!),- bytes: stemsBytes,- kind: "export",+ r2Key: f.key,+ fileName: f.fileName,+ mime: f.mime,+ bytes: sizes[i],+ kind: f.kind,});}});diff --git a/site/src/cut/worker/r2.ts b/site/src/cut/worker/r2.tsindex 0d967468..d48225d3 100644--- a/site/src/cut/worker/r2.ts+++ b/site/src/cut/worker/r2.ts@@ -163,8 +163,6 @@ const MIME_BY_EXT: Record<string, string> = {".m3u8": "application/vnd.apple.mpegurl",".ts": "video/mp2t",".m4s": "video/iso.segment",- // An export's stems.- ".zip": "application/zip",};export function mimeFor(fileName: string): string {