Skip to content

Commit f6cf3b2

Browse files
vanceingallsclaude
andauthored
fix(core): retire worklet processors, retry failed registration, reuse FFT scratch (#3175)
* fix(core): remove the build-audio-fx-runtime.ts stray resurrected by a main merge An earlier merge with main brought this deleted file back (git's merge/delete handling on an unchanged-on-one-side file); package.json already points at build-inline-artifact.ts, so it sat unreachable and duplicating that file's config, both of which fallow flagged. * fix(studio): pull TimelineLanes under the 600-line cap TimelineLanes.tsx hit 620 lines. Extracted the three per-clip pointer gestures (resize-start, pointer-down move-arm, click/razor-split) into createClipGestureHandlers — one factory call per rendered clip instead of ~120 lines of inline handler bodies in the render loop. 529 lines now. * fix(studio): split the extracted pointerdown handler under the CRAP threshold Moving the ~120-line gesture logic into timelineClipGestureHandlers.ts concentrated it into two functions fallow flagged (onPointerDown at CRAP 63.6, onResizeStart at 31.6). Split the decision logic (which gesture a pointerdown implies) into a pure resolvePointerDownAction, then split its own intent-blocking check into isIntentBlocked. onResizeStart's guard moved into canStartResize. Every function now scores under 30. * fix(studio): drop the unused DomEditSelection import in PropertyPanelFlat CI caught it on PR #3026 (wa-12-panel-params); a later refactor in the stack removed the last use of the type here without removing the import. * fix(studio): close the typecheck and fallow gaps wa-18b-reschedule opened useAutomationLanes.ts's write() assumed gesture-scoped coalescing and a preview-only commit that useDomEditAttributeCommits.ts never grew — backported that option support from its own later commit so the two sides of the API agree. The paste path and its tests were missing the box selection's v0/v1 bounds a sibling commit added to AutomationSelection. The FX panel's carve controls still edited the six mechanism numbers (maxCutDb, bands, intelligibilityBias) after carveProfile() collapsed authoring to one Strength knob, so those fields no longer existed on HfCarveSettings; UI now edits strength, and analyseCarveBands is called with carveProfile(strength). Also closes fallow's complexity, dead-code and duplication findings on this PR's diff: extracted automationLaneDragMath.ts (pure group/point-move math) and useAutomationRangeDrag.ts (the marquee-select gesture) out of useAutomationLaneGestures.ts, pulled a couple of render-loop ternaries and a resolver into named functions, dropped an export nothing outside its file used, and shared a step-simplifier between audioCarve's two envelope builders. The edge-stretch vs. box-select priority test in TimelineAutomationLane.test was still pinning the pre-box-select rule (edge wins over a point sitting on it) that a sibling commit deliberately reversed — a point inside the box is now selected content, so grabbing it drags the group instead. Updated the test to the shipped rule instead of the old one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(core): cap the via conic's weight so an edge-clamped via point can't NaN A via point pulled out past the segment (viaX: 5, viaY: -3) clamps to (0.999, 0.001) — exactly on the steady region's edge, where edge - viaX is 0. viaConic divided by that zero to get an infinite weight, and shapeVia turned Infinity into NaN a few steps later (Infinity - Infinity in the quadratic coefficient). NaN reaching setValueCurveAtTime silences the automated parameter for the rest of the render. Capped the weight at 1e6 instead of leaving it unbounded — past that point the arc already reads as touching the via point, so nothing visible is lost. Also hardened shapeVia's existing denominator guard (`<= 0`) to `!(> 0)`, since NaN fails the original comparison and fell through it. Review by Miga (PR #3208). * fix(studio-server): fingerprint the proactive waveform cache key too The route already keys the waveform cache on the asset's size and mtime as well as its path, so a rebuilt-in-place file gets fresh peaks instead of stale ones. generateWaveformCache — the proactive path that runs on upload — still called buildWaveformCacheKey with the path alone, so it wrote to a different key than the route reads from (making the pre-generated cache never found) and kept the exact collision bug this fingerprint exists to fix on its own path. Review by Miga (PR #3211). * style(docs): run oxfmt on the /hyperframes-audio skill docs Table column widths had drifted out of alignment with oxfmt's own rules, failing format:check and blocking the Preflight gate every downstream branch inherits. Whitespace only, no content change. * fix(core): stop \b from missing underscore-separated names, guard clipsOverlap's negative duration \b treats `_` as a word character, so \bbed\b never matched bed_01, music_bed_loop, or theme_song, and \bvo\b/\bvox\b/\btts\b had the same gap — an underscore-separated bed classified as "unknown" and could end up offered as its own carve source. Replaced the short hints with a boundary that actually excludes letters and digits on both sides. clipsOverlap computed end = start + duration without guarding sign, so a negative duration put end before start — an interval that does not describe anything, and one specific case showed it silently dropping a real overlap (a shorter, earlier broken end rejected a clip that genuinely contained the point). Duration clamps to zero instead: a clip cannot un-play time, and a zero-length clip at its start is the sane reading of "duration nobody wrote down as positive." Review by Miga (PR #3212). * fix(studio): widen PropertyPanel's resetModules render timeout again The 20s margin (already once widened for the same reason) is timing out in CI's full-monorepo Test run — the resetModules()+fresh-import render this test needs is uncached and competes with every other package's test suite for the same worker pool, and the same test passes in well under 2s standalone. Went to 45s rather than re-tuning to whatever number happens to clear the current CI load, since that number moves every time CI gains a package. * fix(studio): stop the single-candidate auto-apply carve firing twice Two auto-apply effects both fire when sourceOptions.length === 1: the multi-candidate effect only guards length === 0, so a single candidate passes it too, and the single-candidate effect passes its own guard right after — both compute the same sources list and both call setCarve, so the common case (one narrator, one bed) triggered two decodes, two FFT runs, and two concurrent attribute writes for one decision. The multi-candidate effect now defers to its sibling for exactly one candidate, which already has its own detailed handling for that case. Review by Miga (PR #3213). * feat(core): carve against every voice over a bed, always (#3212) * feat(core): carve against every voice over a bed, always dynamically A bed usually runs under a whole sequence — a narrator, an interview answer, a second presenter — and carving against one of them left the others fighting it. `source` becomes `sources`, and `mixCarveSources` sums every voice onto the BED's clock before anything is measured. That is what keeps one analysis sufficient: the chain is fixed, so there is no per-voice filter to switch between, and bands drawn from all the speech there is with envelopes that rise wherever any of it happens answer the actual question — where and when is speech masking this bed. Summed rather than averaged: two people talking at once mask more than either alone. Audio before the bed starts is dropped rather than folded in at zero, since it plays over nothing and shifting it would put a cut where there is no voice. `dynamic` is gone. A fixed depth thins the bed through every pause, and once both have been heard there is no reason to want it, so every carve follows the speech. Two helpers the panel and the headless script now share instead of each carrying a copy — two definitions of "what does this name suggest" drift, and then the two disagree about which track is the voice: - `classifyAudioName` reads a track's kind from its id and filename together. `unknown` is deliberately common: treating an unrecognised name as "not a voice" would hide the one track somebody needs to pick. - `clipsOverlap` keeps out a voice that never plays while the bed does. An unwritten duration counts as unbounded, not zero — refusing a clip whose length the composition leaves to the media would drop the commonest case there is. Files written before this still load: a single `source` reads as a one-voice list, a stored `dynamic` is ignored, and an absent attribute means the defaults whole. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(core): stop \b from missing underscore-separated names, guard clipsOverlap's negative duration \b treats `_` as a word character, so \bbed\b never matched bed_01, music_bed_loop, or theme_song, and \bvo\b/\bvox\b/\btts\b had the same gap — an underscore-separated bed classified as "unknown" and could end up offered as its own carve source. Replaced the short hints with a boundary that actually excludes letters and digits on both sides. clipsOverlap computed end = start + duration without guarding sign, so a negative duration put end before start — an interval that does not describe anything, and one specific case showed it silently dropping a real overlap (a shorter, earlier broken end rejected a clip that genuinely contained the point). Duration clamps to zero instead: a clip cannot un-play time, and a zero-length clip at its start is the sane reading of "duration nobody wrote down as positive." Review by Miga (PR #3212). --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(studio): port the carve UI off the removed source/dynamic fields #3212 (accidentally squash-merged into this branch instead of main) changed HfCarveSettings from a single `source` + `dynamic` toggle to a `sources` list with dynamic mode removed outright — the multi-voice UI consumer that goes with that shape lands in the very next PR, so this branch was left with a type that no longer matched its own code. Minimal port, not the multi-voice redesign that PR does properly: the "Listen to" picker and analyse() treat sources[0] as the one voice this UI still understands, and every dynamic-mode branch (the automated envelope lanes, the toggle, the checkbox) is gone along with the field — a carve is now always the static value the analysis computes, matching what the type change made permanent. Test suite trimmed the same way: the automation-lane and toggle tests covered behavior that no longer exists. --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent ea03441 commit f6cf3b2

11 files changed

Lines changed: 280 additions & 18 deletions

File tree

.fallowrc.jsonc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,6 @@
149149
"file": "packages/studio/src/utils/studioHelpers.ts",
150150
"exports": ["resolveDroppedAssetDimensions"],
151151
},
152-
// Audio FX worklets sit near the bottom of the audio stack: the worklet
153-
// source and its test reset are consumed by the runtime and engine PRs
154-
// upstack, so a per-PR audit against the merge base sees them as unused.
155-
{
156-
"file": "packages/core/src/audio/audioFxWorklets.ts",
157-
"exports": ["AUDIO_FX_WORKLET_SOURCE", "__resetAudioFxWorkletsForTests"],
158-
},
159152
{
160153
"file": "packages/core/src/audio/audioFxGraph.ts",
161154
"exports": ["ensureAudioFxWorklets"],

packages/core/src/audio/audioFxGraph.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ describe("buildFxNode", () => {
155155
expect(workletNodes[0]!.messages[0]).toMatchObject({ threshold: -30 });
156156
});
157157

158+
it("tells a worklet processor to retire on dispose, not just disconnect it", () => {
159+
// Disconnecting leaves the processor alive — it lives until `process()`
160+
// returns false — so every rebuild that dropped a worklet effect left one
161+
// running on the audio thread for the rest of the session.
162+
workletNodes.length = 0;
163+
const h = buildFxNode(asCtx(ctx()), "compressor", defaultAudioFxParams("compressor"));
164+
h.dispose();
165+
expect(workletNodes[0]!.messages).toEqual([{ __hfDispose: true }]);
166+
});
167+
158168
it("rebuilds the saturation curve for the selected shape", () => {
159169
const c = ctx();
160170
buildFxNode(asCtx(c), "saturate", { ...defaultAudioFxParams("saturate"), type: "hard" });

packages/core/src/audio/audioFxGraph.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,17 @@ function workletBuilder(processor: string): Builder {
182182
input: node,
183183
output: node,
184184
update: (v) => node.port.postMessage({ ...v }),
185-
dispose: () => node.disconnect(),
185+
dispose: () => {
186+
// Disconnecting is not enough to retire an AudioWorkletProcessor: it
187+
// lives until its `process()` returns false, and these all returned
188+
// true unconditionally. So every chain rebuild that dropped a limiter,
189+
// compressor, gate or bitcrush left it running on the audio thread for
190+
// the rest of the session, and a few edits to a carved bed accumulated
191+
// a stack of them. The processors treat this message as their cue to
192+
// stop.
193+
node.port.postMessage({ __hfDispose: true });
194+
node.disconnect();
195+
},
186196
};
187197
};
188198
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { audioFxWorkletsReady, ensureAudioFxWorklets } from "./audioFxWorklets.js";
3+
4+
/** Just enough of a BaseAudioContext for the registration cache to key on. */
5+
const contextWith = (addModule: (url: string) => Promise<void>): BaseAudioContext =>
6+
({ audioWorklet: { addModule } }) as unknown as BaseAudioContext;
7+
8+
describe("ensureAudioFxWorklets", () => {
9+
it("registers once per context and reuses the result", async () => {
10+
const addModule = vi.fn(async () => undefined);
11+
const ctx = contextWith(addModule);
12+
13+
await ensureAudioFxWorklets(ctx);
14+
await ensureAudioFxWorklets(ctx);
15+
16+
expect(addModule).toHaveBeenCalledTimes(1);
17+
expect(audioFxWorkletsReady(ctx)).toBe(true);
18+
});
19+
20+
it("retries after a failure instead of replaying it forever", async () => {
21+
// The rejected promise used to stay in the cache, so every later attempt
22+
// got the same rejection back — the limiter, compressor, gate and bitcrush
23+
// were silent for the life of the context after one transient failure.
24+
const addModule = vi
25+
.fn<(url: string) => Promise<void>>()
26+
.mockRejectedValueOnce(new Error("module load failed"))
27+
.mockResolvedValue(undefined);
28+
const ctx = contextWith(addModule);
29+
30+
await expect(ensureAudioFxWorklets(ctx)).rejects.toThrow("module load failed");
31+
expect(audioFxWorkletsReady(ctx)).toBe(false);
32+
33+
await expect(ensureAudioFxWorklets(ctx)).resolves.toBeUndefined();
34+
expect(addModule).toHaveBeenCalledTimes(2);
35+
expect(audioFxWorkletsReady(ctx)).toBe(true);
36+
});
37+
38+
it("refuses a context with no AudioWorklet rather than hanging", async () => {
39+
const ctx = {} as BaseAudioContext;
40+
await expect(ensureAudioFxWorklets(ctx)).rejects.toThrow(/secure context/);
41+
});
42+
});
43+
44+
/**
45+
* A processor lives until its `process()` returns false — disconnecting the
46+
* node does not retire it. These all returned true unconditionally, so every
47+
* chain rebuild that dropped a worklet effect left it running on the audio
48+
* thread for the rest of the session.
49+
*
50+
* The source is taken from the data: URL registration actually hands to
51+
* `addModule`, so this also proves the URL carries what it claims to.
52+
*/
53+
describe("the worklet processors themselves", () => {
54+
/** Evaluate the registered module and hand back the processor classes by name. */
55+
async function loadProcessors(): Promise<Map<string, new (o: unknown) => Processor>> {
56+
let moduleSource = "";
57+
await ensureAudioFxWorklets(
58+
contextWith(async (url: string) => {
59+
moduleSource = atob(url.replace("data:text/javascript;base64,", ""));
60+
}),
61+
);
62+
const made = new Map<string, new (o: unknown) => Processor>();
63+
class Base {
64+
port = {
65+
onmessage: null as ((e: { data: unknown }) => void) | null,
66+
postMessage: (data: unknown) => this.port.onmessage?.({ data }),
67+
};
68+
}
69+
new Function("AudioWorkletProcessor", "registerProcessor", "sampleRate", moduleSource)(
70+
Base,
71+
(name: string, cls: new (o: unknown) => Processor) => made.set(name, cls),
72+
48000,
73+
);
74+
return made;
75+
}
76+
77+
interface Processor {
78+
port: { postMessage(data: unknown): void };
79+
process(inputs: Float32Array[][], outputs: Float32Array[][]): boolean;
80+
}
81+
82+
const block = (): Float32Array[][] => [[new Float32Array(128)]];
83+
84+
it("every processor keeps running until it is told to stop, then retires", async () => {
85+
const processors = await loadProcessors();
86+
expect([...processors.keys()]).toEqual([
87+
"hf-compressor",
88+
"hf-limiter",
89+
"hf-gate",
90+
"hf-bitcrush",
91+
]);
92+
93+
for (const [name, Cls] of processors) {
94+
const p = new Cls({ processorOptions: {} });
95+
expect(p.process(block(), block()), `${name} retired before it was disposed`).toBe(true);
96+
p.port.postMessage({ __hfDispose: true });
97+
expect(p.process(block(), block()), `${name} kept running after dispose`).toBe(false);
98+
// And it stays retired — a later parameter update must not revive it.
99+
p.port.postMessage({ mix: 0.5 });
100+
expect(p.process(block(), block()), `${name} came back to life`).toBe(false);
101+
}
102+
});
103+
});

packages/core/src/audio/audioFxWorklets.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ class HfCompressor extends AudioWorkletProcessor {
6060
this.p = o.processorOptions || {};
6161
this.env = new EnvBank(this.p.attack ?? 20, this.p.release ?? 250);
6262
this.port.onmessage = (e) => {
63+
if (e.data && e.data.__hfDispose) { this.dead = true; return; }
6364
this.p = { ...this.p, ...e.data };
6465
this.env.set(this.p.attack ?? 20, this.p.release ?? 250);
6566
};
6667
}
6768
process(inputs, outputs) {
69+
if (this.dead) return false;
6870
const i = inputs[0], o = outputs[0];
6971
if (!i || !i.length) return true;
7072
const p = this.p;
@@ -105,11 +107,13 @@ class HfLimiter extends AudioWorkletProcessor {
105107
this.p = o.processorOptions || {};
106108
this.env = new EnvBank(this.p.attack ?? 5, this.p.release ?? 50);
107109
this.port.onmessage = (e) => {
110+
if (e.data && e.data.__hfDispose) { this.dead = true; return; }
108111
this.p = { ...this.p, ...e.data };
109112
this.env.set(this.p.attack ?? 5, this.p.release ?? 50);
110113
};
111114
}
112115
process(inputs, outputs) {
116+
if (this.dead) return false;
113117
const i = inputs[0], o = outputs[0];
114118
if (!i || !i.length) return true;
115119
const ceiling = dbToLin(this.p.limit ?? -1);
@@ -136,11 +140,13 @@ class HfGate extends AudioWorkletProcessor {
136140
this.env = new EnvBank(this.p.attack ?? 1, this.p.release ?? 100);
137141
this.gains = [];
138142
this.port.onmessage = (e) => {
143+
if (e.data && e.data.__hfDispose) { this.dead = true; return; }
139144
this.p = { ...this.p, ...e.data };
140145
this.env.set(this.p.attack ?? 1, this.p.release ?? 100);
141146
};
142147
}
143148
process(inputs, outputs) {
149+
if (this.dead) return false;
144150
const i = inputs[0], o = outputs[0];
145151
if (!i || !i.length) return true;
146152
const p = this.p;
@@ -183,9 +189,13 @@ class HfBitcrush extends AudioWorkletProcessor {
183189
this.p = o.processorOptions || {};
184190
this.holds = [];
185191
this.held = [];
186-
this.port.onmessage = (e) => { this.p = { ...this.p, ...e.data }; };
192+
this.port.onmessage = (e) => {
193+
if (e.data && e.data.__hfDispose) { this.dead = true; return; }
194+
this.p = { ...this.p, ...e.data };
195+
};
187196
}
188197
process(inputs, outputs) {
198+
if (this.dead) return false;
189199
const i = inputs[0], o = outputs[0];
190200
if (!i || !i.length) return true;
191201
const p = this.p;
@@ -243,7 +253,16 @@ export function ensureAudioFxWorklets(ctx: BaseAudioContext): Promise<void> {
243253
)}`;
244254
await ctx.audioWorklet.addModule(url);
245255
readyContexts.add(ctx);
246-
})();
256+
})().catch((err: unknown) => {
257+
// A failed registration must not be remembered. `readyContexts` is only
258+
// written on success, so callers correctly keep asking — and every ask
259+
// replayed this same rejected promise, leaving the limiter, compressor,
260+
// gate and bitcrush silent for the life of the context with no way back.
261+
// One transient failure (a slow module load, a context still warming up)
262+
// permanently disabled half the rack.
263+
registered.delete(ctx);
264+
throw err;
265+
});
247266
registered.set(ctx, modulePromise);
248267
}
249268
return modulePromise;

packages/core/src/audioCarve.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,48 @@ describe("analyseCarveBands", () => {
250250
});
251251
});
252252

253+
/**
254+
* Both analysis loops reuse one pair of FFT scratch arrays across every window
255+
* rather than allocating a pair per hop — a 5-minute 48 kHz voiceover is ~7000
256+
* hops, so ~460 MB of transient Float64Array used to churn through the main
257+
* thread for one carve. `re` is fully overwritten each window, but `im` is only
258+
* ever added to, so it has to be cleared; missing that, the imaginary part
259+
* accumulates across windows and every spectrum after the first is wrong by a
260+
* growing amount.
261+
*/
262+
describe("reused FFT scratch across windows", () => {
263+
/** A steady tone on an exact bin centre (48000/4096 x 128), so every window is identical. */
264+
const steady = (seconds: number): Float32Array => {
265+
const n = Math.floor(SR * seconds);
266+
const out = new Float32Array(n);
267+
for (let i = 0; i < n; i++) out[i] = 0.5 * Math.sin((2 * Math.PI * 1500 * i) / SR);
268+
return out;
269+
};
270+
271+
it("measures the same bands however many windows the clip has", () => {
272+
// Every window carries the same spectrum, so the Welch average cannot
273+
// depend on how many were averaged — unless one window is contaminating
274+
// the next.
275+
const short = analyseCarveBands(steady(0.5), SR, PROFILE);
276+
const long = analyseCarveBands(steady(12), SR, PROFILE);
277+
expect(short.length).toBeGreaterThan(0);
278+
expect(long).toEqual(short);
279+
});
280+
281+
it("keeps a steady tone's dynamics envelope flat instead of drifting", () => {
282+
const [lane] = analyseCarveDynamics(steady(12), SR, [{ freq: 1600, gainDb: -8, q: 1.4 }]);
283+
const value = (t: number): number =>
284+
sampleAutomationLane({ target: "fx.n1.gain", points: lane!.points }, t);
285+
// Past the attack the cut has to sit still, because the signal does. A
286+
// window contaminated by the one before it grows the measured power over
287+
// the clip, and the envelope — which is relative to the band's own peak —
288+
// slides with it.
289+
expect(value(4)).toBeLessThan(-1);
290+
expect(value(8)).toBeCloseTo(value(4), 0);
291+
expect(value(11)).toBeCloseTo(value(4), 0);
292+
});
293+
});
294+
253295
describe("carveBandsToChain", () => {
254296
it("turns bands into peaking nodes carrying the analysed values", () => {
255297
const chain = carveBandsToChain([{ freq: 1000, gainDb: -6, q: 1.4 }]);

packages/core/src/audioCarve.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,23 @@ function powerSpectrum(
332332

333333
const bins = FRAME / 2 + 1;
334334
const acc = new Float64Array(bins);
335+
// Reused across hops. These used to be allocated inside the loop: a 5-minute
336+
// 48 kHz voiceover is ~7000 hops, so ~460 MB of transient Float64Array
337+
// churned through the main thread for a single carve. `re` is fully
338+
// overwritten below; only `im` has to be cleared.
339+
const re = new Float64Array(FRAME);
340+
const im = new Float64Array(FRAME);
341+
//
342+
// Every hop is still read. Striding them — Welch's average is supposed to
343+
// settle long before 7000 windows — was measured on a 5-minute voiceover and
344+
// moves the result: at strength 0.9 the chosen band set changed (630 Hz for
345+
// 160 Hz), and it did not converge back to the full read even at 2048
346+
// windows. 27x faster is not worth silently redrawing the author's carve.
335347
let frames = 0;
336348
for (let start = 0; start + FRAME <= n; start += HOP) {
337349
// Goertzel-free naive DFT would be O(n^2); use a real FFT via recursion on
338350
// a copied frame. FRAME is a power of two so the radix-2 split is exact.
339-
const re = new Float64Array(FRAME);
340-
const im = new Float64Array(FRAME);
351+
im.fill(0);
341352
for (let i = 0; i < FRAME; i++) re[i] = (padded[start + i] ?? 0) * window[i]!;
342353
fft(re, im);
343354
for (let k = 0; k < bins; k++) acc[k]! += re[k]! * re[k]! + im[k]! * im[k]!;
@@ -601,9 +612,13 @@ export function analyseCarveDynamics(
601612

602613
const times: number[] = [];
603614
const perBand = bands.map(() => [] as number[]);
615+
// Reused across windows, as in powerSpectrum. `re` is fully overwritten
616+
// below; only `im` has to be cleared. The hop here is already bounded by
617+
// POINT_BUDGET, so there is nothing to stride.
618+
const re = new Float64Array(FRAME);
619+
const im = new Float64Array(FRAME);
604620
for (let start = 0; start < voice.length; start += hop) {
605-
const re = new Float64Array(FRAME);
606-
const im = new Float64Array(FRAME);
621+
im.fill(0);
607622
for (let i = 0; i < FRAME; i++) re[i] = (voice[start + i] ?? 0) * window[i]!;
608623
fft(re, im);
609624
const power: number[] = [];

packages/core/src/audioFx.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ describe("normalizeAudioFxParams", () => {
6969
expect(v.gain).toBe(0);
7070
});
7171

72+
it("treats a blank or missing value as absent rather than as zero", () => {
73+
// `Number(null)`, `Number("")`, `Number(false)` and `Number([])` are all 0
74+
// and all finite, so these used to clamp to 0 instead of falling back. Zero
75+
// is a legal setting for most of these knobs, so nothing downstream could
76+
// tell: a compressor whose threshold arrived as null sat at 0 dB and never
77+
// engaged, silently, rather than at its declared -24 dB.
78+
const def = defaultAudioFxParams("compressor").threshold;
79+
expect(def).not.toBe(0);
80+
for (const blank of [null, undefined, "", " ", false, [], {}]) {
81+
expect(
82+
normalizeAudioFxParams("compressor", { threshold: blank as unknown as number }).threshold,
83+
`${JSON.stringify(blank)} was read as a number`,
84+
).toBe(def);
85+
}
86+
// A string that really does spell a number still counts — that is how the
87+
// panel's inputs arrive.
88+
expect(
89+
normalizeAudioFxParams("compressor", { threshold: "-30" as unknown as number }).threshold,
90+
).toBe(-30);
91+
});
92+
7293
it("falls back to the default for an unrecognised enum value", () => {
7394
expect(normalizeAudioFxParams("saturate", { type: "sawtooth" }).type).toBe("tanh");
7495
expect(normalizeAudioFxParams("saturate", { type: "atan" }).type).toBe("atan");

packages/core/src/audioFx.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,20 @@ export function normalizeAudioFxParams(
761761
out[p.key] = ok ? (raw as string) : p.default;
762762
continue;
763763
}
764-
const n = typeof raw === "number" ? raw : Number(raw);
764+
// Only a number, or a string that actually spells one. `Number(null)`,
765+
// `Number("")`, `Number(false)` and `Number([])` are all 0 and all pass
766+
// Number.isFinite, so a missing or blanked value used to clamp to 0 rather
767+
// than fall back to the declared default — and 0 is a legal value for most
768+
// of these knobs, so nothing downstream could tell. A compressor whose
769+
// threshold arrived as null sat at 0 dB and never engaged, silently,
770+
// instead of at its -24 dB default. `numberOrNull` in audioAutomation.ts
771+
// already guards exactly this.
772+
const n =
773+
typeof raw === "number"
774+
? raw
775+
: typeof raw === "string" && raw.trim() !== ""
776+
? Number(raw)
777+
: Number.NaN;
765778
out[p.key] = Number.isFinite(n) ? Math.min(p.max, Math.max(p.min, n)) : p.default;
766779
}
767780
return out;

packages/producer/src/services/render/stages/audioStage.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,27 @@ describe("runAudioStage", () => {
122122
const result = await runAudioStage(makeInput());
123123
expect(result.hasAudio).toBe(false);
124124
expect(result.audioError).toMatch(/Audio FX failed for track bgm/);
125-
expect(result.audioFailures).toBeUndefined();
125+
// And it is classified. This used to come back undefined, so the warning
126+
// policy — which reads owner, retryability, reason and stage off this list
127+
// — described the FATAL failure with strictly less detail than a single
128+
// dropped track gets.
129+
expect(result.audioFailures).toEqual([
130+
{
131+
stage: "internal",
132+
reason: "internal",
133+
owner: "system",
134+
retryable: false,
135+
detail: "Audio FX failed for track bgm: browser launch failed",
136+
},
137+
]);
138+
});
139+
140+
it("bounds the synthesised failure's detail", async () => {
141+
// `detail` is contractually bounded diagnostic text; an ffmpeg-flavoured
142+
// message can run to tens of kilobytes.
143+
processCompositionAudioMock.mockRejectedValue(new Error("x".repeat(5_000)));
144+
const result = await runAudioStage(makeInput());
145+
expect(result.audioFailures?.[0]?.detail.length).toBe(2_000);
126146
});
127147

128148
it("lets an abort keep its own shape rather than becoming an audio error", async () => {

0 commit comments

Comments
 (0)