Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Stop calling a 16-bit rounding step clipping, and say the memory on the page

Bitti389302120e81
diff --git a/src/components/workspace/ExportView.test.tsx b/src/components/workspace/ExportView.test.tsx
index e014e74..c0ee105 100644
--- a/src/components/workspace/ExportView.test.tsx
+++ b/src/components/workspace/ExportView.test.tsx
@@ -2,6 +2,7 @@
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
+import audioFixture from "../../../fixtures/project-v1-audio-mix.json";
import externalFixture from "../../../fixtures/project-v1-external-media.json";
import { createProjectConfig, parseProjectConfig, type ProjectConfig, type TimelineClip } from "../../lib/project";
import { ExportView } from "./ExportView";
@@ -86,6 +87,45 @@ describe("the export view on media the project does not contain", () => {
});
});
+/* The mix is built whole in memory before the file is opened and held until the
+ last frame — 11.5 MB a minute, 1.4 GB for an hour — and the panel that states
+ bitrate, size estimate, compositor and codec said nothing at all about it. */
+describe("what the page says the soundtrack will cost", () => {
+ const withAudioSupport = (body: () => void) => {
+ const scope = globalThis as unknown as Record<string, unknown>;
+ const saved = { AudioEncoder: scope.AudioEncoder, AudioData: scope.AudioData, OfflineAudioContext: scope.OfflineAudioContext };
+ // detectExportSupport asks for exactly these three; nothing here calls them.
+ scope.AudioEncoder = class {};
+ scope.AudioData = class {};
+ scope.OfflineAudioContext = class {};
+ try {
+ body();
+ } finally {
+ for (const [name, value] of Object.entries(saved)) {
+ if (value === undefined) delete scope[name];
+ else scope[name] = value;
+ }
+ }
+ };
+
+ it("states the memory the mix holds, next to the bitrate and the size estimate", () => {
+ withAudioSupport(() => {
+ render(<ExportView config={parseProjectConfig(audioFixture)} folderPath="D:\\tmp\\harbour" />);
+ // Two clips on an unmuted audio track; the third is on a muted one.
+ expect(screen.getByText("AAC · 2 clips mixed")).toBeTruthy();
+ expect(screen.getByText(/1\.5 MB of memory held while it renders/)).toBeTruthy();
+ });
+ });
+
+ it("says nothing about a mix it is not going to make", () => {
+ // No AudioEncoder here, so there is no mix and no memory to disclose —
+ // stating a cost that will not be paid would be its own kind of lie.
+ render(<ExportView config={parseProjectConfig(audioFixture)} folderPath="D:\\tmp\\harbour" />);
+ expect(screen.getByText("this webview has no AudioEncoder")).toBeTruthy();
+ expect(screen.queryByText(/of memory held while it renders/)).toBeNull();
+ });
+});
+
describe("what the page claims about the compositor", () => {
it("does not promise WebGPU before anything has asked for an adapter", async () => {
render(<ExportView config={project([clip("a", 0, 2_000)])} folderPath="/tmp/project" />);
diff --git a/src/lib/exportPipeline.test.ts b/src/lib/exportPipeline.test.ts
index 11116c7..e9227e0 100644
--- a/src/lib/exportPipeline.test.ts
+++ b/src/lib/exportPipeline.test.ts
@@ -272,7 +272,11 @@ function parseWav(bytes: ArrayBuffer): { channels: Float32Array[]; sampleRate: n
const channels = Array.from({ length: channelCount }, () => new Float32Array(frames));
for (let frame = 0; frame < frames; frame += 1) {
for (let channel = 0; channel < channelCount; channel += 1) {
- channels[channel][frame] = view.getInt16(data.start + (frame * channelCount + channel) * 2, true) / 32768;
+ /* 32767.5, not 32768: measured in Chrome, int16 16384 decodes to
+ 0.50001525878906250, so the range maps symmetrically onto -1..1 and
+ two half-scale clips sum to one 16-bit step OVER full scale. Getting
+ this wrong here hid exactly that case. */
+ channels[channel][frame] = view.getInt16(data.start + (frame * channelCount + channel) * 2, true) / 32767.5;
}
}
return { channels, sampleRate };
@@ -891,6 +895,11 @@ describe("mixing real sound into a real export", () => {
]);
expect(result.audioDetail).toMatch(/2 clips run past the end of their sound/);
expect(result.audioProblems).toEqual([]);
+ /* Both clips sit at exactly half scale, so where they overlap the sum is
+ one 16-bit step over full scale and every sample of it is clamped.
+ That is not clipping worth announcing: the message would have advised
+ pulling down by "0.0 dB", which is not advice. */
+ expect(result.audioDetail).not.toMatch(/clipped/);
/* And the silence is really there. The head sounds 0-1500 ms of its
2000 ms; the reprise runs 1000-3000 ms and its sound stops at 2000. */
diff --git a/src/lib/exportPipeline.ts b/src/lib/exportPipeline.ts
index 238ed2a..e87b1e2 100644
--- a/src/lib/exportPipeline.ts
+++ b/src/lib/exportPipeline.ts
@@ -494,6 +494,19 @@ export interface AudioMix {
peak: number;
}
+/** How far over full scale the mix has to go before clipping is worth a
+ * sentence: a tenth of a decibel.
+ *
+ * Not an arbitrary tolerance. Measured in Chrome, a 16-bit sample decodes as
+ * `value / 32767.5`, so two clips sitting at exactly half scale sum to
+ * 1.0000305 — one 16-bit step over — and the old code called that "clipped"
+ * for every sample of the overlap while advising the user to pull down by
+ * "0.0 dB". Below a tenth of a decibel the advice this message gives rounds
+ * to nothing and the overshoot is 90 dB under the signal. The samples are
+ * still clamped, because the encoder needs them in range; what changes is
+ * that a hair over full scale is no longer announced as damage. */
+const CLIPPING_WORTH_SAYING = 10 ** (0.1 / 20);
+
/** Decodes and mixes every audio segment in the plan into one stereo buffer.
* Null when the plan has no sound in it at all. */
async function mixAudio(options: {
@@ -745,7 +758,7 @@ function describeSoundtrack(
const parts = [
`${soundtrack.used} audio ${soundtrack.used === 1 ? "clip" : "clips"} mixed to ${probe?.detail ?? AUDIO_CODEC}`,
];
- if (soundtrack.clippedSamples > 0) {
+ if (soundtrack.clippedSamples > 0 && soundtrack.peak > CLIPPING_WORTH_SAYING) {
const share = (soundtrack.clippedSamples / (soundtrack.frames * AUDIO_CHANNELS)) * 100;
parts.push(
`the mix peaked at ${soundtrack.peak.toFixed(2)} of full scale and ${soundtrack.clippedSamples.toLocaleString()} samples (${share.toFixed(share < 0.1 ? 3 : 1)}%) were clipped flat — pull the overlapping clips down by about ${(20 * Math.log10(soundtrack.peak)).toFixed(1)} dB to avoid it`,