Files
hyperframes/packages/core/src/audioFxPresets.test.ts
T
Vance IngallsandClaude Opus 5 acdb11250c feat: switch a preset off, or ramp it, as one thing (#3189)
* 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>
2026-08-13 12:35:44 -07:00

312 lines
13 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
getAudioFxDef,
HF_AUDIO_FX_CHAIN_VERSION,
normalizeAudioFxParams,
parseAudioFxChain,
serializeAudioFxChain,
type HfAudioFxChain,
type HfAudioFxNumberParam,
} from "./audioFx.js";
import {
activeAudioFxPresetIds,
applyAudioFxPreset,
audioFxPresetNodes,
audioFxPresetsByFamily,
getAudioFxPreset,
HF_AUDIO_FX_PRESET_FAMILIES,
HF_AUDIO_FX_PRESETS,
} from "./audioFxPresets.js";
const empty = (): HfAudioFxChain => ({ version: HF_AUDIO_FX_CHAIN_VERSION, nodes: [] });
const need = (id: string) => {
const p = getAudioFxPreset(id);
if (!p) throw new Error(`no preset ${id}`);
return p;
};
/**
* The catalogue is hand-written numbers, which is exactly the kind of thing
* that rots quietly: a value outside its declared range does not throw, it gets
* clamped, and the preset then sounds like something nobody chose. These check
* the data itself rather than the machinery around it.
*/
describe("the catalogue is internally valid", () => {
it("has unique ids and a family the menu knows", () => {
const ids = HF_AUDIO_FX_PRESETS.map((p) => p.id);
expect(new Set(ids).size).toBe(ids.length);
for (const p of HF_AUDIO_FX_PRESETS) {
expect(HF_AUDIO_FX_PRESET_FAMILIES, `${p.id} sits on no shelf`).toContain(p.family);
expect(p.nodes.length, `${p.id} is empty`).toBeGreaterThan(0);
expect(p.label.length).toBeGreaterThan(0);
expect(p.description.length).toBeGreaterThan(0);
}
});
it("names only real effects", () => {
for (const p of HF_AUDIO_FX_PRESETS) {
for (const node of p.nodes) {
expect(getAudioFxDef(node.type), `${p.id} uses unknown effect "${node.type}"`).toBeTruthy();
}
}
});
it("names only parameters those effects declare", () => {
for (const p of HF_AUDIO_FX_PRESETS) {
for (const node of p.nodes) {
const keys = new Set((getAudioFxDef(node.type)?.params ?? []).map((x) => x.key));
for (const key of Object.keys(node.params ?? {})) {
expect(keys.has(key), `${p.id}: ${node.type} has no parameter "${key}"`).toBe(true);
}
}
}
});
it("sets every value inside its own declared range", () => {
// The one that actually catches typos. A value out of range is silently
// clamped, so without this a preset can ship sounding like nothing anyone
// chose and still pass every other test here.
for (const p of HF_AUDIO_FX_PRESETS) {
for (const node of p.nodes) {
const def = getAudioFxDef(node.type);
for (const [key, raw] of Object.entries(node.params ?? {})) {
const param = def?.params.find((x) => x.key === key);
if (!param) continue;
if (param.kind === "enum") {
expect(
param.options.some((o) => o.value === raw),
`${p.id}: ${node.type}.${key} = "${String(raw)}" is not one of its options`,
).toBe(true);
continue;
}
const n = param as HfAudioFxNumberParam;
expect(typeof raw, `${p.id}: ${node.type}.${key} is not a number`).toBe("number");
expect(
raw as number,
`${p.id}: ${node.type}.${key} = ${String(raw)} is below its minimum ${n.min}`,
).toBeGreaterThanOrEqual(n.min);
expect(
raw as number,
`${p.id}: ${node.type}.${key} = ${String(raw)} is above its maximum ${n.max}`,
).toBeLessThanOrEqual(n.max);
}
}
}
});
it("survives being written to an attribute and read back, TAGS AND ALL", () => {
// Comparing only types is what let `fromPreset` be silently dropped by the
// parser: every preset round-tripped its effects while losing the tag that
// lets it find its own nodes again. Compare what the rack actually needs.
for (const p of HF_AUDIO_FX_PRESETS) {
const chain = applyAudioFxPreset(empty(), p);
const back = parseAudioFxChain(serializeAudioFxChain(chain));
expect(
back.nodes.map((x) => ({
type: x.type,
id: x.id,
fromPreset: x.fromPreset,
label: x.label,
})),
`${p.id} did not round-trip`,
).toEqual(
chain.nodes.map((x) => ({
type: x.type,
id: x.id,
fromPreset: x.fromPreset,
label: x.label,
})),
);
}
});
it("round-trips how much of the preset is applied", () => {
// `presetAmount` is what the switch writes and what a lane ramps, so losing
// it on reload silently turns every part-applied or switched-off preset back
// on — the same failure `fromPreset` had, one field along. The INVARIANT: a
// new HfAudioFxNode field goes in BOTH parseAudioFxChain and
// serializeAudioFxChain.
const preset = HF_AUDIO_FX_PRESETS[0];
if (!preset) throw new Error("empty catalogue");
for (const amount of [0, 0.4, 1]) {
const chain = applyAudioFxPreset(empty(), preset);
const withAmount = {
...chain,
nodes: chain.nodes.map((n) => ({ ...n, presetAmount: amount })),
};
const back = parseAudioFxChain(serializeAudioFxChain(withAmount));
// 1 is the absent case — a fully applied preset should not grow a field in
// every chain that carries one — and reads back as fully applied.
const expected = amount === 1 ? undefined : amount;
expect(
back.nodes.map((n) => n.presetAmount),
`amount ${amount} did not survive`,
).toEqual(chain.nodes.map(() => expected));
}
});
it("refuses an amount outside the blend it drives", () => {
// Two gains in opposition: past 1 the dry leg goes negative rather than the
// preset simply getting louder.
const preset = HF_AUDIO_FX_PRESETS[0];
if (!preset) throw new Error("empty catalogue");
const chain = applyAudioFxPreset(empty(), preset);
const raw = JSON.parse(serializeAudioFxChain(chain)) as {
nodes: { presetAmount?: number }[];
};
raw.nodes = raw.nodes.map((n) => ({ ...n, presetAmount: 4 }));
const back = parseAudioFxChain(JSON.stringify(raw));
for (const node of back.nodes) expect(node.presetAmount).toBe(1);
});
it("names every node for the job it is doing", () => {
for (const p of HF_AUDIO_FX_PRESETS) {
for (const node of p.nodes) {
expect(node.label, `${p.id}: a ${node.type} node has no job name`).toBeTruthy();
}
}
});
it("never shows the same name twice in one chain", () => {
// The failure this exists to prevent: a rack reading "Shape One Range"
// twice, once cutting mud and once adding clarity, with nothing to tell
// them apart. Identical CONSECUTIVE nodes are exempt — a stacked pair is
// one stage built from two biquads, not two jobs.
for (const p of HF_AUDIO_FX_PRESETS) {
const seen = new Map<string, number>();
p.nodes.forEach((node, i) => {
const key = node.label ?? node.type;
const prev = seen.get(key);
const stackedPair =
prev === i - 1 && JSON.stringify(p.nodes[prev]?.params) === JSON.stringify(node.params);
expect(
prev === undefined || stackedPair,
`${p.id} shows "${key}" twice — an author cannot tell the two apart`,
).toBe(true);
seen.set(key, i);
});
}
});
it("ends anything that boosts with a ceiling", () => {
// A preset that adds presence and makeup gain can push the chain past full
// scale, and the render clamps what it is handed. Every preset that lifts
// has to hand the mix something bounded.
for (const p of HF_AUDIO_FX_PRESETS) {
const lifts = p.nodes.some((node) => {
const g = node.params?.["gain"];
const makeup = node.params?.["makeup"];
const out = node.params?.["output"];
return (
(typeof g === "number" && g > 0) ||
(typeof makeup === "number" && makeup > 0) ||
(typeof out === "number" && out > 0)
);
});
if (!lifts) continue;
const last = p.nodes[p.nodes.length - 1];
const bounded =
p.nodes.some((n) => n.type === "limiter") ||
// A band-limited character preset cannot run away: its own filters and
// soft clip cap it, and a limiter would change the effect.
p.family === "character";
expect(bounded, `${p.id} lifts level but never bounds it (ends on ${last?.type})`).toBe(true);
}
});
it("puts the limiter last wherever it has one", () => {
for (const p of HF_AUDIO_FX_PRESETS) {
const at = p.nodes.findIndex((n) => n.type === "limiter");
if (at === -1) continue;
expect(at, `${p.id}: a limiter that is not last is not a ceiling`).toBe(p.nodes.length - 1);
}
});
it("keeps every shelf stocked", () => {
for (const family of HF_AUDIO_FX_PRESET_FAMILIES) {
expect(audioFxPresetsByFamily(family).length, `${family} is empty`).toBeGreaterThan(0);
}
});
});
describe("applying a preset", () => {
it("writes ordinary nodes with their defaults filled in", () => {
const chain = applyAudioFxPreset(empty(), need("rumble-cut"));
expect(chain.nodes).toHaveLength(1);
const node = chain.nodes[0]!;
expect(node.type).toBe("highpass");
// Named 100 Hz; everything else comes from the effect, so the file holds a
// complete node rather than a partial one the graph has to guess at.
expect(node.params).toEqual({ ...normalizeAudioFxParams("highpass", {}), frequency: 100 });
expect(node.fromPreset).toBe("rumble-cut");
expect(node.label).toBe("Cut Rumble");
expect(node.enabled).toBe(true);
});
it("gives every node an id, because a lane addresses effects by id", () => {
const chain = applyAudioFxPreset(empty(), need("telephone"));
const ids = chain.nodes.map((n) => n.id);
expect(ids.every(Boolean)).toBe(true);
expect(new Set(ids).size).toBe(ids.length);
});
it("appends rather than replacing, so a character preset can stack on a clean voice", () => {
const voiced = applyAudioFxPreset(empty(), need("voice-clean"));
const both = applyAudioFxPreset(voiced, need("telephone"));
expect(both.nodes.length).toBe(voiced.nodes.length + need("telephone").nodes.length);
expect(activeAudioFxPresetIds(both)).toEqual(["voice-clean", "telephone"]);
// ids stay unique across the two batches
expect(new Set(both.nodes.map((n) => n.id)).size).toBe(both.nodes.length);
});
it("replaces the whole chain when asked", () => {
const voiced = applyAudioFxPreset(empty(), need("voice-clean"));
const only = applyAudioFxPreset(voiced, need("hall"), { replaceChain: true });
expect(activeAudioFxPresetIds(only)).toEqual(["hall"]);
expect(only.nodes).toHaveLength(need("hall").nodes.length);
});
it("re-applying swaps its own nodes instead of stacking a second copy", () => {
const once = applyAudioFxPreset(empty(), need("telephone"));
const twice = applyAudioFxPreset(once, need("telephone"));
expect(twice.nodes.length).toBe(once.nodes.length);
expect(activeAudioFxPresetIds(twice)).toEqual(["telephone"]);
});
it("re-applying keeps the preset's place in the signal order", () => {
// Order is audible: a telephone band before a limiter is a different sound
// from one after it. Re-applying must not quietly move the preset to the end.
const start = applyAudioFxPreset(empty(), need("telephone"));
const withTail = applyAudioFxPreset(start, need("hall"));
const again = applyAudioFxPreset(withTail, need("telephone"));
expect(again.nodes.map((n) => n.fromPreset)).toEqual([
...new Array(need("telephone").nodes.length).fill("telephone"),
...new Array(need("hall").nodes.length).fill("hall"),
]);
});
it("leaves hand-added effects alone when a preset is re-applied", () => {
const hand = { type: "reverb", id: "mine", params: normalizeAudioFxParams("reverb", {}) };
const chain: HfAudioFxChain = { version: HF_AUDIO_FX_CHAIN_VERSION, nodes: [hand] };
const once = applyAudioFxPreset(chain, need("voice-clean"));
const twice = applyAudioFxPreset(once, need("voice-clean"));
expect(twice.nodes.filter((n) => n.id === "mine")).toHaveLength(1);
expect(twice.nodes.filter((n) => n.fromPreset === "voice-clean")).toHaveLength(
need("voice-clean").nodes.length,
);
});
it("mints ids that cannot collide with what is already in the chain", () => {
const chain: HfAudioFxChain = {
version: HF_AUDIO_FX_CHAIN_VERSION,
nodes: [
{ type: "reverb", id: "n1" },
{ type: "delay", id: "n2" },
],
};
const made = audioFxPresetNodes(need("voice-clean"), chain);
for (const node of made) expect(["n1", "n2"]).not.toContain(node.id);
expect(new Set(made.map((n) => n.id)).size).toBe(made.length);
});
});