Files
hyperframes/packages/studio/src/components/editor/propertyPanelFxSection.tsx
T
Vance IngallsandClaude Opus 5 6bcf739390 fix(studio): audition from the playhead while paused, and let an author out of a menu (#3190)
* 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 13:06:56 -07:00

885 lines
36 KiB
TypeScript

/**
* The FX section for an audio element: the chain, plus the voiceover carve.
*
* The carve is its own module — see `propertyPanelFxCarveModule.tsx` for why it
* is not an entry in the chain.
*/
import { useCallback, useEffect, useMemo, useRef, useState, type KeyboardEvent } from "react";
import {
defaultAudioFxParams,
getAudioFxDef,
HF_AUDIO_FX,
mintAudioFxNodeId,
type HfAudioFxChain,
type HfAudioFxGroup,
type HfAudioFxNode,
type HfAudioFxParam,
type HfAudioFxParamValues,
} from "@hyperframes/core/audio-fx";
import { DEFAULT_CARVE, type HfCarveSettings } from "@hyperframes/core/audio-carve";
import { applyAudioFxPreset, getAudioFxPreset } from "@hyperframes/core/audio-fx-presets";
import {
addAudioEq,
audioEqIds,
readAudioEqBands,
removeAudioEq,
setAudioEqBandGain,
} from "@hyperframes/core/audio-fx-eq";
import { EFFECT_COPY } from "@hyperframes/core/audio-fx-copy";
import { applyAudioFxProfile, getAudioFxProfile } from "@hyperframes/core/audio-fx-profiles";
import {
audioFxJobNode,
HF_AUDIO_FX_JOBS,
HF_AUDIO_FX_JOB_TYPES,
type HfAudioFxJob,
} from "@hyperframes/core/audio-fx-jobs";
import { FxParamRow } from "./propertyPanelFxControls.js";
import { FxPresetMenu } from "./propertyPanelFxPresetMenu.js";
import { FxEqModule } from "./propertyPanelFxEqModule.js";
import { FxCarveModule, type AudioTrackOption } from "./propertyPanelFxCarveModule.js";
import { FxNodeRow } from "./propertyPanelFxNodeRow.js";
export type { AudioTrackOption };
const GROUP_ORDER: HfAudioFxGroup[] = ["filter", "dynamics", "nonlinear", "time"];
const GROUP_LABEL: Record<HfAudioFxGroup, string> = {
filter: "Filters",
dynamics: "Dynamics",
nonlinear: "Non-linear",
time: "Time",
};
/**
* The one control over a whole preset: how much of it is applied.
*
* Not in the effect registry — a preset is not an effect — so the row is
* fabricated the same way the derived one-knob control is, and rendered by the
* ordinary controls.
*/
const PRESET_AMOUNT_PARAM: HfAudioFxParam = {
kind: "number",
key: "amount",
label: "Amount",
unit: "",
min: 0,
max: 1,
step: 0.01,
default: 1,
hint: "How much of this preset is applied. Automate it to bring the whole preset in or out over time.",
};
export interface FxSectionProps {
chain: HfAudioFxChain;
/** Targets this track already automates, as `fx.<nodeId>.<param>` strings. */
automatedTargets?: ReadonlySet<string>;
/**
* What each automated target is worth at the playhead, by the same key.
*
* An automated parameter's stored number is only the seed the lane replaced, so
* a rack that shows it stands still while the carve is audibly working. Absent,
* or missing a key, means there is no playhead over this clip and the stored
* value is the honest one.
*/
liveAutomationValues?: ReadonlyMap<string, number>;
/** Add a lane for one effect parameter, seeded at its current value. */
onAutomateParam?(nodeId: string, paramKey: string): void;
/** Delete one effect parameter's lane. */
onRemoveParamAutomation?(nodeId: string, paramKey: string): void;
/** Delete every lane belonging to a node that is being removed. */
onRemoveNodeAutomation?(nodeId: string): void;
/** Add a lane for a whole preset's amount, seeded where it sits now. */
onAutomatePreset?(presetId: string, amount: number): void;
/** Delete that lane. */
onRemovePresetAutomation?(presetId: string): void;
/** Presets whose amount a lane already drives. */
automatedPresets?: ReadonlySet<string>;
/** Measure this track and write the levelling lane. Absent when unavailable. */
onLevel?(): void;
/** Take the levelling stage and its lane back out. */
onRemoveLevel?(): void;
/** Whether a levelling stage is already on the track. */
levelled?: boolean;
/**
* Hover-audition of the levelling script: measure this track and play the
* result without persisting it, and put it back on `false`.
*
* Separate from `onChainPreview` because it is the one audition that cannot be
* synthesised from the chain in hand — the numbers do not exist until the
* audio has been decoded and measured.
*/
onAuditionLevel?(on: boolean): void;
/** Whether that measurement is running, so the button can say so. */
auditioningLevel?: boolean;
/**
* Start the transport for an audition, and stop it on the way out.
*
* An audition is written to the running graph, which is silent while the
* transport is paused — so without this, hovering a preset does nothing at all
* for a paused author.
*/
onAuditionTransport?(on: boolean): void;
/** Structural edits and gesture-end writes; this is the one that persists. */
onChainChange(chain: HfAudioFxChain): void;
/** Continuous updates while a control is being dragged. */
onChainPreview?(chain: HfAudioFxChain): void;
carve: HfCarveSettings | null;
/** Gesture-end write; this is the one that persists. */
onCarveChange(carve: HfCarveSettings | null): void;
/** Continuous updates while a carve slider is dragged. Without this every
* pointermove patched the source file and resynced the selection. */
onCarvePreview?(carve: HfCarveSettings): void;
/**
* Set when another track's carve listens to this one, naming it. The carve block
* is then not offered here at all: this track is the voice, not the bed.
*/
carvedAgainstBy?: string | null;
/** Other audio elements that could act as the carve source. */
sourceOptions: AudioTrackOption[];
analysing?: boolean;
disabled?: boolean;
}
export function FxSection({
chain,
automatedTargets,
liveAutomationValues,
onAutomateParam,
onRemoveParamAutomation,
onRemoveNodeAutomation,
onChainChange,
onChainPreview,
carve,
carvedAgainstBy,
onCarveChange,
onCarvePreview,
sourceOptions,
analysing,
disabled,
onLevel,
onRemoveLevel,
levelled,
onAuditionLevel,
auditioningLevel,
onAutomatePreset,
onRemovePresetAutomation,
automatedPresets,
onAuditionTransport,
}: FxSectionProps) {
const presetAutomated = automatedPresets ?? new Set<string>();
// Falls back to the persisting write when no preview handler is supplied, which
// keeps the control working rather than going dead.
const previewCarve = onCarvePreview ?? onCarveChange;
// Nothing to carve against means nothing to show — see the block below.
// Not offered on the voice another track is already carving against — that
// track is the far end of someone else's relationship, and a carve of its own
// could only name a source it must not.
const showCarve = !carvedAgainstBy && (sourceOptions.length > 0 || carve !== null);
const [adding, setAdding] = useState(false);
const [picking, setPicking] = useState(false);
const [openNode, setOpenNode] = useState<number | null>(0);
/**
* The add menu, with the jobs standing in for the effect they are made of.
*
* `peaking` is not offered as itself: picking it is picking a machine and
* leaving the real decision — which range — for afterwards. The jobs are that
* decision, already made. See `audioFxJobs.ts`.
*/
const grouped = useMemo(
() =>
GROUP_ORDER.map((g) => ({
group: g,
defs: HF_AUDIO_FX.filter((d) => d.group === g && !HF_AUDIO_FX_JOB_TYPES.has(d.id)),
jobs: HF_AUDIO_FX_JOBS.filter((job) => getAudioFxDef(job.type)?.group === g),
})),
[],
);
const mutate = useCallback(
(nodes: HfAudioFxNode[]) => onChainChange({ ...chain, nodes }),
[chain, onChainChange],
);
// Dragging a knob previews without persisting; releasing it commits once.
const previewNode = useCallback(
(index: number, params: HfAudioFxParamValues) =>
onChainPreview?.({
...chain,
nodes: chain.nodes.map((n, i) => (i === index ? { ...n, params } : n)),
}),
[chain, onChainPreview],
);
/**
* The chain as it is really stored, captured when an audition starts.
*
* Auditioning writes through the preview channel, which does not persist and
* does not come back as a new `chain` prop — so reverting has to remember what
* was there rather than read it back. Null means nothing is being auditioned,
* which is also what makes a stray leave a no-op instead of a write.
*/
const auditionBase = useRef<HfAudioFxChain | null>(null);
/**
* Play something without committing to it, and put it back on the way out.
*
* Hearing a preset before choosing it is the strongest affordance in this
* panel — see `plans/audio-fx-ux/README.md` §Decided. It costs nothing new:
* the preview channel a slider drag already uses rebuilds the running graph
* without touching the document.
*/
const audition = useCallback(
(make: ((base: HfAudioFxChain) => HfAudioFxChain) | null) => {
if (!onChainPreview) return;
if (make) {
auditionBase.current ??= chain;
onChainPreview(make(auditionBase.current));
// After the chain is in the graph, not before: starting the transport
// first plays a moment of the un-auditioned mix.
onAuditionTransport?.(true);
} else if (auditionBase.current) {
// Stop before reverting, for the mirror of that reason — the last thing
// heard should be the preset, not a frame of the chain coming back.
onAuditionTransport?.(false);
onChainPreview(auditionBase.current);
auditionBase.current = null;
}
},
[chain, onChainPreview, onAuditionTransport],
);
/**
* The preview handler as of the last render, held rather than closed over.
*
* The teardown below must run on teardown and at no other time, so its deps
* have to be empty — and `onChainPreview` is an inline arrow in the group,
* which re-renders on every playhead tick to move the automation readouts. A
* dep on it made React tear down and re-run the effect on every one of those
* ticks, so an audition reverted itself about 30 times a second while the
* pointer was still on the button: the preset was heard for a frame during
* playback, which is the exact case the whole affordance exists for.
*/
const previewRef = useRef(onChainPreview);
previewRef.current = onChainPreview;
// Leaving by any route other than the pointer — the element deselected, the
// panel closed — would otherwise leave the audition playing over a chain the
// document does not have.
const transportRef = useRef(onAuditionTransport);
transportRef.current = onAuditionTransport;
useEffect(
() => () => {
if (auditionBase.current) {
transportRef.current?.(false);
previewRef.current?.(auditionBase.current);
}
},
[],
);
const applyPreset = useCallback(
(id: string) => {
const preset = getAudioFxPreset(id);
if (!preset) return;
// Appends. Stacking a character preset onto an already-cleaned voice is a
// real thing to want, and replacing silently would throw work away — so
// the destructive option is a separate gesture, not the default one.
const next = applyAudioFxPreset(chain, preset);
// The audition WAS this, so there is nothing to put back — and putting the
// old chain back over the write that just landed is a race the author
// hears as the preset arriving and then leaving again.
auditionBase.current = null;
onAuditionTransport?.(false);
mutate(next.nodes);
// Land on the first node the preset wrote, so the author can hear what
// arrived and immediately see what it is made of.
setOpenNode(next.nodes.findIndex((n) => n.fromPreset === preset.id));
setPicking(false);
},
[chain, mutate, onAuditionTransport],
);
/**
* One effect appended, at the values its module opens on.
*
* For most effects that is the registry's defaults. For the five with a
* derived knob it is NOT: the registry defaults are not a point on the
* profile's curve, so the module opened reading a strength it was not set to —
* a compressor arrived showing Evenness 0.67 with its make-up gain at 0 dB,
* which is the "quieter as you turn it up" bug the profiles exist to prevent,
* on the very first frame. Seeding through the profile puts the knob and the
* mechanism in agreement from the start.
*/
const withEffect = useCallback(
(base: HfAudioFxChain, type: string): HfAudioFxChain => ({
...base,
nodes: [
...base.nodes,
{
type,
id: mintAudioFxNodeId(base),
enabled: true,
params: getAudioFxProfile(type)
? applyAudioFxProfile(type, 0.5, defaultAudioFxParams(type))
: defaultAudioFxParams(type),
},
],
}),
[],
);
/** The same, for a job — an ordinary node that arrives already named and aimed. */
const withJob = useCallback(
(base: HfAudioFxChain, job: HfAudioFxJob): HfAudioFxChain => ({
...base,
nodes: [...base.nodes, audioFxJobNode(job, base)],
}),
[],
);
const addJob = useCallback(
(job: HfAudioFxJob) => {
auditionBase.current = null;
onAuditionTransport?.(false);
mutate(withJob(chain, job).nodes);
setOpenNode(chain.nodes.length);
setAdding(false);
},
[chain, mutate, withJob, onAuditionTransport],
);
const addEffect = useCallback(
(type: string) => {
auditionBase.current = null;
onAuditionTransport?.(false);
mutate(withEffect(chain, type).nodes);
setOpenNode(chain.nodes.length);
setAdding(false);
},
[chain, mutate, withEffect, onAuditionTransport],
);
const updateNode = useCallback(
(index: number, patch: Partial<HfAudioFxNode>) =>
mutate(chain.nodes.map((n, i) => (i === index ? { ...n, ...patch } : n))),
[chain.nodes, mutate],
);
/**
* How much of a preset is applied, 0..1.
*
* The switch and the lane are the same value, not two ways of silencing a
* preset: `presetAmount` drives the wet/dry blend the graph wraps the run in,
* so Off is amount 0 and a lane ramping 0 → 1 is the same control moving
* continuously. Writing `enabled` instead would take the nodes out of the
* graph, which a lane cannot do part-way and cannot do without a rebuild.
*
* On every node of the run because that is where the chain can hold it — see
* `HfAudioFxNode.presetAmount`.
*/
const setRunAmount = useCallback(
(items: { node: HfAudioFxNode; i: number }[], amount: number, persist = true) => {
const slots = new Set(items.map((item) => item.i));
const next = {
...chain,
nodes: chain.nodes.map((n, i) => (slots.has(i) ? { ...n, presetAmount: amount } : n)),
};
if (persist) mutate(next.nodes);
else onChainPreview?.(next);
},
[chain, mutate, onChainPreview],
);
/**
* Take a preset back out whole, lanes and all.
*
* Same contract as removing one node — an orphaned lane keeps driving a
* parameter that is no longer in the graph, and with ids minted lowest-free
* the next effect added inherits it.
*/
const removeRun = useCallback(
(items: { node: HfAudioFxNode; i: number }[]) => {
for (const { node } of items) {
if (node.id) onRemoveNodeAutomation?.(node.id);
}
const slots = new Set(items.map((item) => item.i));
mutate(chain.nodes.filter((_, i) => !slots.has(i)));
setOpenNode(null);
},
[chain.nodes, mutate, onRemoveNodeAutomation],
);
const removeNode = useCallback(
(index: number) => {
// The node's lanes go with it. `resolveAutomation` only hides an orphan at
// read time; left in the attribute, and with ids minted lowest-free, the
// next effect added takes the same id and inherits the dead envelope —
// arriving with its control disabled and "Automated" without the author
// ever automating it, and baked into the render.
const removedId = chain.nodes[index]?.id;
if (removedId) onRemoveNodeAutomation?.(removedId);
mutate(chain.nodes.filter((_, i) => i !== index));
setOpenNode(null);
},
[chain.nodes, mutate, onRemoveNodeAutomation],
);
// Open by default: the module is the carve's whole control surface now, and a
// collapsed card would hide the knob the author came here for.
const [carveOpen, setCarveOpen] = useState(true);
const carveNodes = useMemo(() => chain.nodes.filter((n) => n.fromCarve), [chain.nodes]);
/** Everything the author added, with the chain index every edit addresses. */
const handBuilt = useMemo(
() =>
chain.nodes
.map((node, i) => ({ node, i }))
// Carve and EQ bands belong to their own modules; showing them here too
// would put the same filter on screen twice with two ways to edit it.
.filter(({ node }) => !node.fromCarve && !node.fromEq),
[chain.nodes],
);
/**
* The hand-built list cut into runs, so a preset reads as one thing.
*
* Applying a preset drops five rows into the rack with nothing saying they
* arrived together — which is the same failure the carve module was built to
* fix, one level down. Consecutive only: a preset whose nodes have been pulled
* apart by a reorder is no longer a unit, and drawing a bracket around the gap
* would claim an adjacency the signal path does not have.
*/
const runs = useMemo(() => {
const out: { preset?: string; items: { node: HfAudioFxNode; i: number }[] }[] = [];
for (const item of handBuilt) {
const preset = item.node.fromPreset;
const last = out.at(-1);
if (last && last.preset === preset) last.items.push(item);
else out.push({ ...(preset ? { preset } : {}), items: [item] });
}
return out;
}, [handBuilt]);
const eqIds = useMemo(() => audioEqIds(chain), [chain]);
/**
* The number each row wears, counted over what the rack actually shows.
*
* Not the chain index: the carve's filters and an EQ's bands are inside their
* own modules, so counting raw nodes would leave the visible rack jumping from
* 02 to 07 and the numbers would look like a bug rather than a position.
*/
const positions = useMemo(() => {
const map = new Map<number, number>();
let at = (showCarve ? 1 : 0) + eqIds.length;
for (const { i } of handBuilt) map.set(i, ++at);
return map;
}, [handBuilt, eqIds.length, showCarve]);
const [openEq, setOpenEq] = useState<string | null>(null);
const addEq = useCallback(() => {
auditionBase.current = null;
onAuditionTransport?.(false);
const { chain: next, eqId } = addAudioEq(chain);
mutate(next.nodes);
setOpenEq(eqId);
setAdding(false);
}, [chain, mutate, onAuditionTransport]);
// Dragging a fader is heard immediately and written once on release, the same
// split every other control in the rack uses.
const previewEqBand = useCallback(
(eqId: string, band: string, gain: number) =>
onChainPreview?.(setAudioEqBandGain(chain, eqId, band, gain)),
[chain, onChainPreview],
);
const commitEqBand = useCallback(
(eqId: string, band: string, gain: number) =>
mutate(setAudioEqBandGain(chain, eqId, band, gain).nodes),
[chain, mutate],
);
const removeEq = useCallback(
(eqId: string) => {
for (const node of chain.nodes) {
if (node.fromEq === eqId && node.id) onRemoveNodeAutomation?.(node.id);
}
mutate(removeAudioEq(chain, eqId).nodes);
},
[chain, mutate, onRemoveNodeAutomation],
);
const moveNode = useCallback(
(index: number, delta: number) => {
const target = index + delta;
if (target < 0 || target >= chain.nodes.length) return;
const next = [...chain.nodes];
const [moved] = next.splice(index, 1);
next.splice(target, 0, moved!);
mutate(next);
setOpenNode(target);
},
[chain.nodes, mutate],
);
/**
* Escape closes whichever menu is open.
*
* The first thing anyone reaches for, and on a surface that covers the rack it
* is the one that needs no discovering. Bound on the section rather than the
* window: a keystroke aimed at the timeline is not aimed at this.
*/
const closeMenus = useCallback(
(event: KeyboardEvent) => {
if (event.key !== "Escape" || (!adding && !picking)) return;
// Stops the panel's own Escape handling from also firing — closing a menu
// and deselecting the clip on one keystroke loses the author their place.
event.stopPropagation();
audition(null);
onAuditionLevel?.(false);
setAdding(false);
setPicking(false);
},
[adding, picking, audition, onAuditionLevel],
);
return (
<div
className="hf-fx-section space-y-2"
// Focus lives on the buttons and menu items inside, so the keystroke
// bubbles to here without the section needing focus of its own.
onKeyDown={closeMenus}
>
<div className="hf-fx-chain space-y-1">
{/* The rack IS the signal path, and saying so costs two lines. Without
them the order reads as a list, which is the one reading that makes
"move up" look cosmetic — it is the most consequential control here. */}
<p className="hf-fx-term flex items-baseline gap-1.5 px-1.5 font-mono text-[9px] uppercase tracking-wide text-panel-text-4">
<span className="hf-fx-term-cap text-panel-text-1">In</span>
<span>this track</span>
</p>
{/* Carve leads the rack, which is also where its effects sit in the signal
path — corrective work before anything the author added. Present
whenever there is a voice for it to listen to, rather than appearing
only once it has already produced something: a control that materialises
after the fact cannot be the thing you reach for to start. */}
{showCarve ? (
<FxCarveModule
nodes={carveNodes}
carve={carve ?? { ...DEFAULT_CARVE }}
sourceOptions={sourceOptions}
automatedTargets={automatedTargets}
liveAutomationValues={liveAutomationValues}
open={carveOpen}
disabled={disabled}
analysing={analysing}
onToggleOpen={() => setCarveOpen((was) => !was)}
onCarveChange={onCarveChange}
onCarvePreview={previewCarve}
/>
) : null}
{eqIds.map((eqId) => (
<FxEqModule
key={eqId}
eqId={eqId}
bands={readAudioEqBands(chain, eqId)}
open={openEq === eqId}
disabled={disabled}
onToggleOpen={() => setOpenEq((was) => (was === eqId ? null : eqId))}
onPreview={(band, gain) => previewEqBand(eqId, band, gain)}
onCommit={(band, gain) => commitEqBand(eqId, band, gain)}
onRemove={() => removeEq(eqId)}
/>
))}
{handBuilt.length === 0 && eqIds.length === 0 ? (
<p className="hf-fx-empty py-1 text-[11px] text-panel-text-4">
{showCarve ? "No other effects on this track." : "No effects on this track."}
</p>
) : (
runs.map((run) => {
const rows = run.items.map(({ node, i }) => (
<FxNodeRow
// Keyed by id, as the carve module's list above already is.
// On `${type}-${index}` two effects of the same type keep their
// keys through a reorder, so React reuses each row where it
// stands — and the controls hold real state (a half-typed
// number, an in-flight drag), which then lands on whichever
// effect moved into that slot.
key={node.id ?? `${node.type}-${i}`}
node={node}
index={i}
position={positions.get(i)}
automatedTargets={automatedTargets}
liveAutomationValues={liveAutomationValues}
onAutomateParam={onAutomateParam}
onRemoveParamAutomation={onRemoveParamAutomation}
open={openNode === i}
last={i === chain.nodes.length - 1}
disabled={disabled}
onToggleOpen={() => setOpenNode(openNode === i ? null : i)}
onUpdate={updateNode}
onMove={moveNode}
onRemove={removeNode}
onPreview={previewNode}
/>
));
const preset = run.preset ? getAudioFxPreset(run.preset) : null;
if (!preset) return rows;
// On unless every node in it is bypassed: one switched back on means
// the preset is doing something, and the switch has to offer to stop
// it rather than claiming it has already stopped.
// How much of it is applied. Any node of the run carries it, and the
// first is the one the graph reads.
const amount = run.items[0]?.node.presetAmount;
const runAmount = typeof amount === "number" ? amount : 1;
const runOn = runAmount > 0;
return (
<div
key={`preset-${run.preset}-${run.items[0]?.i}`}
className="hf-fx-preset-run space-y-1 rounded-[4px] border border-dashed border-panel-border-input p-1"
data-fx-preset={run.preset}
>
<div className="hf-fx-preset-run-head flex min-h-6 items-center gap-1 px-0.5">
<span className="hf-fx-preset-run-label min-w-0 flex-1 truncate font-mono text-[9px] uppercase tracking-wide text-panel-text-4">
{preset.label}
</span>
{/* The whole preset, on or off. Partly-bypassed reads as off,
because "some of it is running" is not a state an author
set — it is one they arrived at, and the switch is how they
get back out of it. */}
<button
type="button"
className="hf-fx-preset-run-toggle rounded-[3px] border border-panel-border-input px-1.5 py-0.5 font-mono text-[9px] text-panel-text-4 hover:text-panel-text-0 disabled:opacity-40"
aria-pressed={runOn}
title={runOn ? `Switch ${preset.label} off` : `Switch ${preset.label} back on`}
disabled={disabled}
onClick={() => setRunAmount(run.items, runOn ? 0 : 1)}
>
{runOn ? "On" : "Off"}
</button>
<button
type="button"
className="hf-fx-preset-run-remove px-1 font-mono text-[11px] text-panel-text-4 hover:text-red-400 disabled:opacity-40"
title={`Remove ${preset.label}`}
disabled={disabled}
onClick={() => removeRun(run.items)}
>
&times;
</button>
</div>
{/* The same value the switch sets, so an author can put the
preset half in — and the lane below ramps it continuously. */}
<FxParamRow
param={PRESET_AMOUNT_PARAM}
value={runAmount}
disabled={disabled || presetAutomated.has(run.preset ?? "")}
automated={presetAutomated.has(run.preset ?? "")}
onChange={(_k, v) => setRunAmount(run.items, Number(v), false)}
onCommit={(_k, v) => setRunAmount(run.items, Number(v))}
onAutomate={
run.preset && onAutomatePreset && !presetAutomated.has(run.preset)
? () => onAutomatePreset(run.preset ?? "", runAmount)
: undefined
}
onRemoveAutomation={
run.preset && onRemovePresetAutomation && presetAutomated.has(run.preset)
? () => onRemovePresetAutomation(run.preset ?? "")
: undefined
}
/>
{rows}
</div>
);
})
)}
<p className="hf-fx-term hf-fx-term-out flex items-baseline gap-1.5 px-1.5 font-mono text-[9px] uppercase tracking-wide text-panel-text-4">
<span className="hf-fx-term-cap text-panel-text-1">Out</span>
<span>to mix</span>
</p>
</div>
{adding ? (
<div
className="hf-fx-add-menu space-y-1.5 rounded-[4px] border border-panel-border-input p-1.5"
// On the shelf, not on each button: moving between two of them passes
// through the gap, and a per-button leave would revert on the way.
onMouseLeave={() => {
audition(null);
onAuditionLevel?.(false);
}}
// The keyboard's version of leaving. Tabbing between two entries fires
// this and then the next one's focus, so it reverts and re-auditions.
onBlur={() => {
audition(null);
onAuditionLevel?.(false);
}}
>
<div className="hf-fx-add-group flex flex-wrap items-center gap-1">
<span className="hf-fx-add-group-label w-full font-mono text-[9px] uppercase tracking-wide text-panel-text-4">
Tone
</span>
{onLevel ? (
<button
type="button"
className="hf-fx-add-composite rounded-[3px] bg-panel-surface px-1.5 py-0.5 text-[10px] text-panel-text-1 hover:text-panel-text-0"
title="Listen to this track and even out its loud and quiet parts."
disabled={disabled || analysing}
onClick={() => {
if (levelled) onRemoveLevel?.();
else onLevel();
setAdding(false);
}}
// The one module here that cannot answer instantly: it has to
// decode the track and measure it before there is anything to
// hear. So it says it is working rather than doing nothing
// visible, and whoever handles this must drop a result that
// arrives after the pointer has gone.
onMouseEnter={
levelled
? undefined
: () => {
audition(null);
onAuditionLevel?.(true);
}
}
onFocus={levelled ? undefined : () => onAuditionLevel?.(true)}
>
{levelled ? "Remove levelling" : "Even Out Levels"}
{auditioningLevel ? <span className="hf-fx-add-working"> measuring</span> : null}
</button>
) : null}
<button
type="button"
// Not hf-fx-add-item: Tone is a composite over several filters,
// not an entry in the effect registry, and a count of the registry
// must not include it.
className="hf-fx-add-composite rounded-[3px] bg-panel-surface px-1.5 py-0.5 text-[10px] text-panel-text-1 hover:text-panel-text-0"
title="Bass, middle and treble on one set of faders."
// No audition of its own: a Tone module arrives with every band at
// 0 dB, so there is nothing to hear until a fader moves, and a
// hover that changes nothing teaches that hovering does nothing.
// It still has to call the neighbours' auditions off.
onMouseEnter={() => {
audition(null);
onAuditionLevel?.(false);
}}
onClick={addEq}
>
Tone (EQ)
</button>
</div>
{grouped.map(({ group, defs, jobs }) => (
<div key={group} className="hf-fx-add-group flex flex-wrap items-center gap-1">
<span className="hf-fx-add-group-label w-full font-mono text-[9px] uppercase tracking-wide text-panel-text-4">
{GROUP_LABEL[group]}
</span>
{jobs.map((job) => (
<button
key={job.id}
type="button"
// Same class as any other entry: a job IS an effect, and one
// that looked special would read as a preset rather than as
// the thing the author is about to add.
className="hf-fx-add-item rounded-[3px] bg-panel-surface px-1.5 py-0.5 text-[10px] text-panel-text-1 hover:text-panel-text-0"
title={job.does}
onClick={() => addJob(job)}
onMouseEnter={() => {
onAuditionLevel?.(false);
audition((base) => withJob(base, job));
}}
onFocus={() => audition((base) => withJob(base, job))}
>
{job.label}
</button>
))}
{defs.map((d) => (
<button
key={d.id}
type="button"
className="hf-fx-add-item rounded-[3px] bg-panel-surface px-1.5 py-0.5 text-[10px] text-panel-text-1 hover:text-panel-text-0"
// The menu that adds it has to call it what the rack will call
// it, or the author picks "High-pass" and a module named
// "Remove Rumble" appears. The registry's own description
// stays as the tooltip beside the plain one: the mechanism is
// taught here rather than withheld.
title={
EFFECT_COPY[d.id] ? `${EFFECT_COPY[d.id]?.does} (${d.label})` : d.description
}
onClick={() => addEffect(d.id)}
// Cancels the levelling audition as well as starting its own.
// The shelf's leave handler only fires on the way OUT of the
// menu, so sliding from Even Out Levels straight to here left a
// measurement in flight — and it landed on top of this one, a
// levelled version of the chain as it was, written through a
// channel the document never sees.
onMouseEnter={() => {
onAuditionLevel?.(false);
audition((base) => withEffect(base, d.id));
}}
onFocus={() => audition((base) => withEffect(base, d.id))}
>
{EFFECT_COPY[d.id]?.title ?? d.label}
</button>
))}
</div>
))}
</div>
) : null}
{picking ? (
<FxPresetMenu
onPick={applyPreset}
onAudition={
onChainPreview
? (id) => {
const preset = id ? getAudioFxPreset(id) : null;
audition(preset ? (base) => applyAudioFxPreset(base, preset) : null);
}
: undefined
}
/>
) : null}
{/* The buttons stay while their menu is open, and close it — an author who
opened one and changed their mind had no way back: picking something
was the only thing that set these false, so the only exits were adding
an effect they did not want or deselecting the clip. */}
<div className="flex gap-1">
<button
type="button"
className="hf-fx-preset w-full rounded-[4px] border border-dashed border-panel-border-input py-1 text-[11px] text-panel-text-4 hover:text-panel-text-0 disabled:opacity-40"
aria-expanded={picking}
disabled={disabled}
onClick={() => {
// Leaving the shelf by closing it is still leaving it, and an
// audition left playing is audible over a chain the document does
// not have.
if (picking) audition(null);
setPicking(!picking);
setAdding(false);
}}
>
{picking ? "Close" : "Presets"}
</button>
<button
type="button"
className="hf-fx-add w-full rounded-[4px] border border-dashed border-panel-border-input py-1 text-[11px] text-panel-text-4 hover:text-panel-text-0 disabled:opacity-40"
aria-expanded={adding}
disabled={disabled}
onClick={() => {
if (adding) {
audition(null);
onAuditionLevel?.(false);
}
setAdding(!adding);
setPicking(false);
}}
>
{adding ? "Close" : "Add effect"}
</button>
</div>
</div>
);
}