feat(core): the levelling script — "Even Out Levels" (#3180)

* 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>
This commit is contained in:
Vance Ingalls
2026-08-13 07:42:32 -07:00
committed by GitHub
co-authored by Claude Opus 5
parent 818a7b281b
commit 71066fc25b
10 changed files with 718 additions and 0 deletions
+6
View File
@@ -104,6 +104,12 @@
"types": "./dist/audioFxEq.d.ts",
"environments": ["browser", "bun", "node"]
},
"./audio-leveller": {
"source": "./src/audioLeveller.ts",
"runtime": "./dist/audioLeveller.js",
"types": "./dist/audioLeveller.d.ts",
"environments": ["browser", "bun", "node"]
},
"./audio-fx-presets": {
"source": "./src/audioFxPresets.ts",
"runtime": "./dist/audioFxPresets.js",
+10
View File
@@ -118,6 +118,12 @@
"import": "./src/audioFxEq.ts",
"types": "./src/audioFxEq.ts"
},
"./audio-leveller": {
"bun": "./src/audioLeveller.ts",
"node": "./dist/audioLeveller.js",
"import": "./src/audioLeveller.ts",
"types": "./src/audioLeveller.ts"
},
"./audio-fx-presets": {
"bun": "./src/audioFxPresets.ts",
"node": "./dist/audioFxPresets.js",
@@ -418,6 +424,10 @@
"import": "./dist/audioFxEq.js",
"types": "./dist/audioFxEq.d.ts"
},
"./audio-leveller": {
"import": "./dist/audioLeveller.js",
"types": "./dist/audioLeveller.d.ts"
},
"./audio-fx-presets": {
"import": "./dist/audioFxPresets.js",
"types": "./dist/audioFxPresets.d.ts"
+8
View File
@@ -832,6 +832,11 @@ export interface HfAudioFxNode {
* effect type and its bands stay ordinary filters underneath.
*/
fromEq?: string;
/**
* Set on the gain stage the leveller writes, so re-running replaces it rather
* than stacking a second one — the same contract `fromCarve` has.
*/
fromLeveller?: boolean;
/** Absent means enabled — chain files written before the field existed still load. */
enabled?: boolean;
params?: HfAudioFxParamValues;
@@ -884,6 +889,7 @@ export function parseAudioFxChain(json: string): HfAudioFxChain {
fromPreset?: unknown;
label?: unknown;
fromEq?: unknown;
fromLeveller?: unknown;
};
if (typeof node.type !== "string" || !BY_ID.has(node.type)) {
throw new AudioFxChainError(`Node ${i} has unknown effect type: ${String(node.type)}`);
@@ -900,6 +906,7 @@ export function parseAudioFxChain(json: string): HfAudioFxChain {
: {}),
...(typeof node.label === "string" && node.label ? { label: node.label } : {}),
...(typeof node.fromEq === "string" && node.fromEq ? { fromEq: node.fromEq } : {}),
...(node.fromLeveller === true ? { fromLeveller: true as const } : {}),
enabled: node.enabled !== false,
params: normalizeAudioFxParams(
node.type,
@@ -926,6 +933,7 @@ export function serializeAudioFxChain(chain: HfAudioFxChain): string {
...(node.fromPreset ? { fromPreset: node.fromPreset } : {}),
...(node.label ? { label: node.label } : {}),
...(node.fromEq ? { fromEq: node.fromEq } : {}),
...(node.fromLeveller === true ? { fromLeveller: true } : {}),
...(node.enabled === false ? { enabled: false } : {}),
params: normalizeAudioFxParams(node.type, node.params),
})),
+266
View File
@@ -0,0 +1,266 @@
import { describe, expect, it } from "vitest";
import {
HF_AUDIO_FX_CHAIN_VERSION,
parseAudioFxChain,
serializeAudioFxChain,
type HfAudioFxChain,
} from "./audioFx.js";
import { sampleAutomationLane } from "./audioAutomation.js";
import { applyAudioFxPreset, getAudioFxPreset } from "./audioFxPresets.js";
import {
analyseLevelling,
levellerProfile,
levellingResult,
levellingSummary,
removeLevelling,
} from "./audioLeveller.js";
const SR = 48000;
const empty = (): HfAudioFxChain => ({ version: HF_AUDIO_FX_CHAIN_VERSION, nodes: [] });
/** A tone whose amplitude changes per section, so the levelling has real work. */
function uneven(sections: { seconds: number; amp: number }[]): Float32Array {
const total = sections.reduce((n, s) => n + Math.floor(SR * s.seconds), 0);
const out = new Float32Array(total);
let at = 0;
for (const section of sections) {
const n = Math.floor(SR * section.seconds);
for (let i = 0; i < n; i += 1) {
out[at + i] = section.amp * Math.sin((2 * Math.PI * 300 * (at + i)) / SR);
}
at += n;
}
return out;
}
const at = (points: { t: number; v: number }[], t: number) =>
sampleAutomationLane({ target: "fx.n1.gain", points }, t);
describe("measuring", () => {
it("lifts a quiet passage and leaves the loud one alone", () => {
// Loud, then 18 dB down, then loud again.
const points = analyseLevelling(
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
{ seconds: 3, amp: 0.5 },
]),
SR,
);
expect(points.length).toBeGreaterThan(0);
// Mid-quiet-section, well past the attack.
expect(at(points, 5.5)).toBeGreaterThan(3);
// Mid-loud-section, well past the release.
expect(Math.abs(at(points, 2.5))).toBeLessThan(2);
});
it("finds nothing to do on a track that is already even", () => {
// A script that always writes something teaches an author it is doing
// nothing; saying "already even" is the useful answer.
expect(analyseLevelling(uneven([{ seconds: 6, amp: 0.4 }]), SR)).toEqual([]);
});
it("leaves room tone alone rather than lifting it into the mix", () => {
// Deliberately NOT digital silence: a real pause is quiet but finite, and
// that is the case the floor exists for. Absolute zero would be skipped by
// the isFinite check alone and prove nothing.
const points = analyseLevelling(
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.0008 },
{ seconds: 3, amp: 0.5 },
]),
SR,
1,
);
// ~56 dB down: a pause with a noise floor, not a quiet passage. Gain
// applied here is gain applied to the room.
expect(Math.abs(at(points, 5.5))).toBeLessThan(1.5);
});
it("targets a level the track reaches, not its loudest instant", () => {
// Mostly quiet with one loud burst. Against the PEAK the whole body of the
// track reads as "quiet" and gets hauled up; against a level the track
// actually sustains, the body is already the target and barely moves.
const points = analyseLevelling(
uneven([
{ seconds: 6, amp: 0.08 },
{ seconds: 1, amp: 0.8 },
{ seconds: 5, amp: 0.08 },
]),
SR,
1,
);
expect(Math.abs(at(points, 3))).toBeLessThan(3);
});
it("never asks for more correction than it can justify", () => {
// ~30 dB below the loud section: still well above the silence floor, so it
// IS a passage to lift — and at full strength the raw ask is over 25 dB,
// which is more gain than any quiet passage should be given.
const points = analyseLevelling(
uneven([
{ seconds: 3, amp: 0.6 },
{ seconds: 4, amp: 0.019 },
]),
SR,
1,
);
expect(points.some((p) => p.v > 6)).toBe(true);
for (const p of points) expect(Math.abs(p.v)).toBeLessThanOrEqual(12);
});
it("starts at the clip's start, so the lane does not slide in from nowhere", () => {
const points = analyseLevelling(
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]),
SR,
);
expect(points[0]?.t).toBe(0);
});
it("handles an empty track without inventing a lane", () => {
expect(analyseLevelling(new Float32Array(0), SR)).toEqual([]);
expect(analyseLevelling(uneven([{ seconds: 1, amp: 0.4 }]), 0)).toEqual([]);
});
});
describe("strength", () => {
it("corrects more the further it is turned up", () => {
const track = uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]);
const gentle = at(analyseLevelling(track, SR, 0.1), 5.5);
const strong = at(analyseLevelling(track, SR, 1), 5.5);
expect(strong).toBeGreaterThan(gentle);
});
it("still leaves some of the performance in at full strength", () => {
// Driving a track to a flat line removes the performance along with the
// inconsistency, so even 1.0 corrects most rather than all of it.
expect(levellerProfile(1).correction).toBeLessThan(1);
expect(levellerProfile(0).correction).toBeGreaterThan(0);
});
});
describe("what it writes", () => {
it("rides a gain node, because a volume lane can only attenuate", () => {
// VOLUME_RANGE is 0..1 and normaliseEnvelope clamps into it, so a volume
// lane cannot lift a quiet passage at all. This is the whole reason the
// script writes a node instead of only a lane.
const result = levellingResult(
empty(),
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]),
SR,
);
expect(result).not.toBeNull();
const node = result!.chain.nodes[0]!;
expect(node.type).toBe("gain");
expect(node.label).toBe("Even Out Levels");
expect(node.params?.gain).toBe(0);
expect(result!.automation.lanes[0]!.target).toBe(`fx.${node.id}.gain`);
});
it("returns nothing when there is nothing to correct", () => {
expect(levellingResult(empty(), uneven([{ seconds: 6, amp: 0.4 }]), SR)).toBeNull();
});
it("replaces its own stage instead of stacking a second one", () => {
const track = uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]);
const once = levellingResult(empty(), track, SR)!;
const twice = levellingResult(once.chain, track, SR)!;
expect(twice.chain.nodes.filter((n) => n.fromLeveller)).toHaveLength(1);
// Same node id, so the lane it already wrote still addresses the right stage.
expect(twice.chain.nodes[0]!.id).toBe(once.chain.nodes[0]!.id);
});
it("goes in FRONT of a trailing limiter, never after it", () => {
// The likely sequence: apply Clean Voice, then even out the levels. Clean
// Voice ends in a Peak Ceiling, and up to 12 dB of lift landing after that
// ceiling means loud material at -1 dBFS goes over full scale and the
// render shears it flat. A ceiling with something after it is not a ceiling.
const voiced = applyAudioFxPreset(empty(), getAudioFxPreset("voice-clean")!);
expect(voiced.nodes[voiced.nodes.length - 1]!.type).toBe("limiter");
const result = levellingResult(
voiced,
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]),
SR,
)!;
const types = result.chain.nodes.map((n) => n.type);
expect(types[types.length - 1]).toBe("limiter");
expect(types[types.length - 2]).toBe("gain");
expect(result.chain.nodes.find((n) => n.fromLeveller)).toBeTruthy();
});
it("leaves hand-added effects alone", () => {
const chain: HfAudioFxChain = {
version: HF_AUDIO_FX_CHAIN_VERSION,
nodes: [{ type: "reverb", id: "mine", enabled: true }],
};
const result = levellingResult(
chain,
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]),
SR,
)!;
expect(result.chain.nodes.map((n) => n.id)).toContain("mine");
expect(result.chain.nodes).toHaveLength(2);
});
it("survives the attribute round trip", () => {
const result = levellingResult(
empty(),
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]),
SR,
)!;
const back = parseAudioFxChain(serializeAudioFxChain(result.chain));
// Without fromLeveller surviving, re-running stacks a second gain stage.
expect(back.nodes.filter((n) => n.fromLeveller)).toHaveLength(1);
expect(back.nodes[0]!.label).toBe("Even Out Levels");
});
it("names the lane it takes away with it", () => {
const result = levellingResult(
empty(),
uneven([
{ seconds: 3, amp: 0.5 },
{ seconds: 3, amp: 0.06 },
]),
SR,
)!;
const removed = removeLevelling(result.chain);
expect(removed.chain.nodes).toHaveLength(0);
// An orphaned lane keeps driving a parameter that is no longer there.
expect(removed.removedTarget).toBe(result.automation.lanes[0]!.target);
});
});
describe("what it says", () => {
it("describes the moves rather than listing numbers", () => {
expect(
levellingSummary([
{ t: 0, v: 0 },
{ t: 1, v: 4.2 },
]),
).toMatch(/lifting quiet parts/);
expect(levellingSummary([])).toMatch(/Already even/);
});
});
+247
View File
@@ -0,0 +1,247 @@
/**
* "Even Out Levels" — the first of the adaptive scripts.
*
* A preset cannot fix inconsistent loudness, because the right correction
* depends on the recording. So this measures the track and writes an automation
* lane that lifts the quiet passages toward the loud ones, exactly as the carve
* measures a voice and writes its bands.
*
* It is NOT loudness normalisation. Platform targets (-14 LUFS and friends) are
* ITU-R BS.1770 — K-weighted and gated — and this is plain windowed RMS, so
* calling it LUFS would be a claim the measurement does not support. It is
* named for what it does: evening out.
*
* ## Why the lane rides a `gain` node
*
* The obvious home is the track's volume lane, and that cannot work: volume is
* 0..1 and `normaliseEnvelope` clamps every keyframe into it, so a volume lane
* can only ever attenuate. Lifting a quiet passage needs a `gain` node, which
* spans -60..+12 dB — which is what the audio skill means when it calls `gain`
* "what an automation lane rides when a track has to move".
*/
import {
fxAutomationTarget,
type HfAutomation,
type HfAutomationPoint,
} from "./audioAutomation.js";
import {
HF_AUDIO_FX_CHAIN_VERSION,
mintAudioFxNodeId,
normalizeAudioFxParams,
type HfAudioFxChain,
type HfAudioFxNode,
} from "./audioFx.js";
/** One window per emitted point; the hop keeps the lane inside its budget. */
const FRAME = 4096;
const POINT_BUDGET = 400;
/** Below this, a window is silence rather than a quiet passage worth lifting. */
const FLOOR_BELOW_PEAK_DB = 42;
/** How far a correction may go. Beyond this, lifting a whisper only lifts the room. */
const MAX_LIFT_DB = 12;
const MAX_CUT_DB = 12;
/** Ignore differences smaller than this — they are not audible and they add points. */
const SNAP_DB = 0.4;
/** Attack/release in seconds, so a correction rides the phrase and not the syllable. */
const ATTACK_S = 0.35;
const RELEASE_S = 0.9;
export interface HfLevellerSettings {
/** 0 = leave it alone, 1 = as even as this can make it. */
strength: number;
}
export const DEFAULT_LEVELLER: HfLevellerSettings = { strength: 0.5 };
export interface HfLevellerProfile {
/** How much of the measured difference to correct. */
correction: number;
}
/**
* One knob to a profile, the shape `carveProfile` established.
*
* At full strength this still corrects only most of the difference: driving a
* track to a flat line removes the performance along with the inconsistency.
*/
export function levellerProfile(strength: number): HfLevellerProfile {
const s = Number.isFinite(strength) ? Math.min(1, Math.max(0, strength)) : 0.5;
return { correction: Number((0.25 + s * 0.6).toFixed(3)) };
}
/** Level in dB of one window, or -Infinity for silence. */
function windowDb(samples: Float32Array, from: number, count: number): number {
let sum = 0;
let n = 0;
for (let i = from; i < from + count; i += 1) {
const s = samples[i];
if (s === undefined) break;
sum += s * s;
n += 1;
}
if (n === 0) return Number.NEGATIVE_INFINITY;
const rms = Math.sqrt(sum / n);
return rms > 0 ? 20 * Math.log10(rms) : Number.NEGATIVE_INFINITY;
}
/**
* Measure a track and return the gain moves that even it out, in dB against
* clip-local seconds. Empty when there is nothing worth correcting.
*/
export function analyseLevelling(
samples: Float32Array,
sampleRate: number,
strength = DEFAULT_LEVELLER.strength,
): HfAutomationPoint[] {
if (samples.length === 0 || sampleRate <= 0) return [];
const profile = levellerProfile(strength);
const hop = Math.max(FRAME, Math.ceil(samples.length / POINT_BUDGET));
const levels: number[] = [];
const times: number[] = [];
for (let start = 0; start < samples.length; start += hop) {
levels.push(windowDb(samples, start, FRAME));
times.push((start + FRAME / 2) / sampleRate);
}
const speaking = levels.filter((d) => Number.isFinite(d));
if (speaking.length === 0) return [];
const peak = Math.max(...speaking);
const floor = peak - FLOOR_BELOW_PEAK_DB;
/**
* The target is a level the track ALREADY REACHES — the 80th percentile of
* its speaking windows — not an absolute one.
*
* Anchoring it to an absolute figure means an already-even track gets pulled
* bodily up or down to meet it, which is a volume change wearing a
* levelling label. Against a level the track reaches, its loud passages
* correct to roughly nothing and only the quiet ones move, which is what
* evening out means. It is a percentile rather than the peak so one loud
* word cannot set the target for the whole track.
*/
const sorted = [...speaking].sort((a, b) => a - b);
const target = sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * 0.8))] ?? peak;
const attack = 1 - Math.exp(-(hop / sampleRate) / ATTACK_S);
const release = 1 - Math.exp(-(hop / sampleRate) / RELEASE_S);
let applied = 0;
const raw: HfAutomationPoint[] = [];
levels.forEach((db, i) => {
// Silence is left alone. Lifting a pause only lifts the room with it.
const wanted =
Number.isFinite(db) && db > floor
? Math.max(-MAX_CUT_DB, Math.min(MAX_LIFT_DB, (target - db) * profile.correction))
: 0;
applied += (wanted > applied ? attack : release) * (wanted - applied);
const v = Math.abs(applied) < SNAP_DB ? 0 : Number(applied.toFixed(1));
raw.push({ t: Number((times[i] ?? 0).toFixed(3)), v });
});
// Keep only the moves. A run of equal values is held by keeping the last of
// the run, or interpolation would slide across a passage that is steady.
const points: HfAutomationPoint[] = [];
let lastKept = 0;
let keptIndex = -1;
raw.forEach((pt, i) => {
if (i !== raw.length - 1 && Math.abs(pt.v - lastKept) < SNAP_DB) return;
if (i > 0 && keptIndex !== i - 1) {
const prev = raw[i - 1];
if (prev) points.push(prev);
}
points.push(pt);
lastKept = pt.v;
keptIndex = i;
});
// A lane of zeroes is not a correction, it is a lane that says nothing. An
// author who runs this on an even track should be told there was nothing to
// do, not handed an inert envelope to wonder about.
if (points.length === 0 || points.every((p) => p.v === 0)) return [];
// A lane's first point has to sit at the clip's start, or everything before
// it is drawn from wherever the first move happens to be.
if ((points[0]?.t ?? 0) > 0) points.unshift({ t: 0, v: points[0]?.v ?? 0 });
return points;
}
/**
* The chain and lane for "Even Out Levels".
*
* Returns nothing when the track needs no correcting — a script that always
* writes something teaches an author that it is doing nothing.
*/
export function levellingResult(
chain: HfAudioFxChain,
samples: Float32Array,
sampleRate: number,
strength = DEFAULT_LEVELLER.strength,
): { chain: HfAudioFxChain; automation: HfAutomation } | null {
const points = analyseLevelling(samples, sampleRate, strength);
if (points.length === 0) return null;
const existing = chain.nodes.find((n) => n.fromLeveller);
const id = existing?.id ?? mintAudioFxNodeId(chain);
const node: HfAudioFxNode = {
type: "gain",
id,
fromLeveller: true,
label: "Even Out Levels",
enabled: true,
// Seeded at 0 dB: the lane is what moves it, and a non-zero seed would be
// heard for the instant before the first ramp is scheduled.
params: normalizeAudioFxParams("gain", { gain: 0 }),
};
/**
* In FRONT of a trailing limiter, not after it.
*
* The likely sequence is "apply Clean Voice, then even out the levels", and
* Clean Voice ends in a Peak Ceiling. Appending would put up to 12 dB of lift
* AFTER the ceiling that exists to bound the chain — so every quiet-to-loud
* transition leaves residual lift on loud material sitting at -1 dBFS, and
* the render shears it flat. A ceiling that something is added after is not
* a ceiling.
*/
const insertAt =
!existing && chain.nodes[chain.nodes.length - 1]?.type === "limiter"
? chain.nodes.length - 1
: chain.nodes.length;
return {
chain: {
version: HF_AUDIO_FX_CHAIN_VERSION,
nodes: existing
? chain.nodes.map((n) => (n.fromLeveller ? node : n))
: [...chain.nodes.slice(0, insertAt), node, ...chain.nodes.slice(insertAt)],
},
automation: {
version: 1,
lanes: [{ target: fxAutomationTarget(id, "gain"), points }],
},
};
}
/** Drop the leveller and say which lane went with it. */
export function removeLevelling(chain: HfAudioFxChain): {
chain: HfAudioFxChain;
removedTarget: string | null;
} {
const node = chain.nodes.find((n) => n.fromLeveller);
return {
chain: { ...chain, nodes: chain.nodes.filter((n) => !n.fromLeveller) },
removedTarget: node?.id ? fxAutomationTarget(node.id, "gain") : null,
};
}
/** What the module says when it is closed. */
export function levellingSummary(points: readonly HfAutomationPoint[]): string {
const moves = points.filter((p) => p.v !== 0);
if (moves.length === 0) return "Already even — nothing to do";
const lift = Math.max(...moves.map((p) => p.v));
const cut = Math.min(...moves.map((p) => p.v));
const parts: string[] = [];
if (lift > 0) parts.push(`lifting quiet parts up to ${lift.toFixed(1)} dB`);
if (cut < 0) parts.push(`holding loud parts down ${Math.abs(cut).toFixed(1)} dB`);
return parts.join(", ");
}
@@ -41,6 +41,7 @@ import {
import {
automatedTargetsOf,
automationAttrValue,
withLane,
HF_AUDIO_AUTOMATION_ATTR,
HF_AUDIO_AUTOMATION_DATA_KEY,
readPanelAutomation,
@@ -48,6 +49,7 @@ import {
withoutLane,
withSeededLane,
} from "./propertyPanelAutomation";
import { levellingResult, removeLevelling } from "@hyperframes/core/audio-leveller";
import type { DomEditSelection } from "./domEditingTypes";
import { useLivePlayheadTime } from "../../hooks/useLivePlayheadTime";
import { usePlayerStore } from "../../player";
@@ -487,6 +489,63 @@ export function AudioFxGroup({
* on this one. The bands replace any previous carve output but leave
* hand-added effects alone, so re-analysing does not discard other work.
*/
/**
* Measure THIS track and write the levelling lane.
*
* Same shape as the carve below it — decode offline, lock the rack while it
* works, write once — but it listens to the track it is on rather than to a
* voice above it, so it needs no source picker.
*/
const runLeveller = async (): Promise<void> => {
const el = element.element;
const src = el?.getAttribute("src");
const doc = el?.ownerDocument;
if (!src || !doc) return;
setAnalysing(true);
try {
const Ctor =
window.OfflineAudioContext ??
(window as unknown as { webkitOfflineAudioContext?: typeof OfflineAudioContext })
.webkitOfflineAudioContext;
if (!Ctor) return;
const res = await fetch(new URL(src, doc.baseURI).href);
const buffer = await new Ctor(1, 1, DECODE_SAMPLE_RATE).decodeAudioData(
await res.arrayBuffer(),
);
const result = levellingResult(chain, buffer.getChannelData(0), buffer.sampleRate);
if (!result) return;
await onSetAttributeQuiet(HF_AUDIO_FX_ATTR, serializeAudioFxChain(result.chain));
// Merged by target, never written wholesale: the script describes its own
// lane only, and replacing the attribute would take the carve's lanes and
// the volume lane with it.
const lane = result.automation.lanes[0];
if (lane) {
void onSetAttributeQuiet(
HF_AUDIO_AUTOMATION_ATTR,
automationAttrValue(withLane(automation, lane)) || null,
);
}
} catch {
// A track whose audio cannot be fetched or decoded simply gets no
// levelling, the same way an unreadable carve source is skipped.
} finally {
setAnalysing(false);
}
};
const removeLeveller = (): void => {
const { chain: next, removedTarget } = removeLevelling(chain);
void onSetAttributeQuiet(HF_AUDIO_FX_ATTR, serializeAudioFxChain(next));
// The lane goes with the node. An orphan keeps driving a parameter that is
// no longer in the graph.
if (removedTarget) {
void onSetAttributeQuiet(
HF_AUDIO_AUTOMATION_ATTR,
automationAttrValue(withoutLane(automation, removedTarget)) || null,
);
}
};
const analyse = async (active: HfCarveSettings | null = carve): Promise<void> => {
if (!active?.sources.length) return;
const doc = element.element?.ownerDocument;
@@ -664,6 +723,9 @@ export function AudioFxGroup({
onCarveChange={(next) => void setCarve(next)}
onCarvePreview={(next) => onSetAttributeLive(HF_AUDIO_CARVE_ATTR, JSON.stringify(next))}
sourceOptions={sourceOptions}
onLevel={() => void runLeveller()}
onRemoveLevel={removeLeveller}
levelled={chain.nodes.some((n) => n.fromLeveller)}
carvedAgainstBy={carvedAgainstBy}
analysing={analysing}
/>
@@ -0,0 +1,60 @@
import { describe, expect, it } from "vitest";
import type { HfAutomation } from "@hyperframes/core/audio-automation";
import { automationAttrValue, withLane, withoutLane } from "./propertyPanelAutomation.js";
const carved = (): HfAutomation => ({
version: 1,
lanes: [
{ target: "fx.n1.gain", points: [{ t: 0, v: -6 }] },
{ target: "fx.n2.gain", points: [{ t: 0, v: -9 }] },
{ target: "volume", points: [{ t: 0, v: 0.8 }] },
],
});
/**
* A script hands back a whole `HfAutomation` that describes only its OWN lane.
* Writing that to the attribute would take everything else with it — the
* carve's per-band lanes and the track's volume lane — which is silent, total,
* and only noticed later when the mix has quietly lost its ducking.
*/
describe("withLane", () => {
it("keeps every lane it was not asked about", () => {
const next = withLane(carved(), { target: "fx.n9.gain", points: [{ t: 0, v: 3 }] });
expect(next.lanes.map((l) => l.target).sort()).toEqual([
"fx.n1.gain",
"fx.n2.gain",
"fx.n9.gain",
"volume",
]);
expect(next.lanes.find((l) => l.target === "volume")?.points).toEqual([{ t: 0, v: 0.8 }]);
});
it("replaces a lane rather than adding a second one for the same target", () => {
// Re-running a script must not leave two lanes fighting over one parameter.
const once = withLane(carved(), { target: "fx.n9.gain", points: [{ t: 0, v: 3 }] });
const twice = withLane(once, { target: "fx.n9.gain", points: [{ t: 0, v: 5 }] });
expect(twice.lanes.filter((l) => l.target === "fx.n9.gain")).toHaveLength(1);
expect(twice.lanes.find((l) => l.target === "fx.n9.gain")?.points).toEqual([{ t: 0, v: 5 }]);
expect(twice.lanes).toHaveLength(4);
});
it("does not mutate what it was given", () => {
const before = carved();
withLane(before, { target: "fx.n9.gain", points: [{ t: 0, v: 3 }] });
expect(before.lanes).toHaveLength(3);
});
});
describe("withoutLane", () => {
it("takes one lane and leaves the rest", () => {
// A node removed without its lane leaves an orphan driving a parameter that
// is no longer in the graph.
const next = withoutLane(carved(), "fx.n1.gain");
expect(next.lanes.map((l) => l.target)).toEqual(["fx.n2.gain", "volume"]);
});
it("empties the attribute when the last lane goes", () => {
const one: HfAutomation = { version: 1, lanes: [{ target: "fx.n1.gain", points: [] }] };
expect(automationAttrValue(withoutLane(one, "fx.n1.gain"))).toBe("");
});
});
@@ -6,6 +6,7 @@
* attribute the same way.
*/
import type { HfAutomationLane } from "@hyperframes/core/audio-automation";
import {
HF_AUDIO_AUTOMATION_ATTR,
HF_AUDIO_AUTOMATION_DATA_KEY,
@@ -73,6 +74,21 @@ export function withoutLane(automation: HfAutomation, target: string): HfAutomat
return { version: 1, lanes: automation.lanes.filter((lane) => lane.target !== target) };
}
/**
* Replace one lane, leaving every other lane alone.
*
* A script that hands back a whole `HfAutomation` describes only its OWN lane.
* Writing that wholesale would take the carve's lanes and the volume lane with
* it, so what the script produces has to be merged in by target rather than
* swapped for what is already there.
*/
export function withLane(automation: HfAutomation, lane: HfAutomationLane): HfAutomation {
return {
version: 1,
lanes: [...automation.lanes.filter((l) => l.target !== lane.target), lane],
};
}
/** The attribute value for an automation set; empty when nothing is automated. */
export function automationAttrValue(automation: HfAutomation): string {
return automation.lanes.length > 0 ? serializeAutomation(automation) : "";
@@ -69,6 +69,9 @@ function mount(overrides: Partial<Parameters<typeof FxSection>[0]> = {}) {
automatedTargets={overrides.automatedTargets}
onAutomateParam={overrides.onAutomateParam}
onRemoveParamAutomation={overrides.onRemoveParamAutomation}
onLevel={overrides.onLevel}
onRemoveLevel={overrides.onRemoveLevel}
levelled={overrides.levelled}
/>,
);
return { host, onChainChange, onChainPreview, onCarveChange };
@@ -388,6 +391,22 @@ describe("FxSection chain", () => {
expect(next.nodes.find((n) => n.label === "Middle")!.params!.gain).toBe(0);
});
it("offers levelling, and offers to take it away once it is there", () => {
const onLevel = vi.fn();
const onRemoveLevel = vi.fn();
const { host } = mount({ onLevel, onRemoveLevel });
click(host.querySelector(".hf-fx-add"));
click(byText(host, ".hf-fx-add-composite", "Even Out Levels"));
expect(onLevel).toHaveBeenCalledTimes(1);
const already = mount({ onLevel, onRemoveLevel, levelled: true });
click(already.host.querySelector(".hf-fx-add"));
// The same control, because adding a second levelling stage is never what
// an author means by pressing it twice.
click(byText(already.host, ".hf-fx-add-composite", "Remove levelling"));
expect(onRemoveLevel).toHaveBeenCalledTimes(1);
});
it("cannot move the ends past themselves", () => {
const { host } = mount({ chain: chainOf("peaking", "reverb") });
const ups = host.querySelectorAll('.hf-fx-move[title="Move up"]');
@@ -649,6 +649,12 @@ export interface FxSectionProps {
onRemoveParamAutomation?(nodeId: string, paramKey: string): void;
/** Delete every lane belonging to a node that is being removed. */
onRemoveNodeAutomation?(nodeId: string): void;
/** 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;
/** Structural edits and gesture-end writes; this is the one that persists. */
onChainChange(chain: HfAudioFxChain): void;
/** Continuous updates while a control is being dragged. */
@@ -686,6 +692,9 @@ export function FxSection({
sourceOptions,
analysing,
disabled,
onLevel,
onRemoveLevel,
levelled,
}: FxSectionProps) {
// Falls back to the persisting write when no preview handler is supplied, which
// keeps the control working rather than going dead.
@@ -908,6 +917,21 @@ export function FxSection({
<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);
}}
>
{levelled ? "Remove levelling" : "Even Out Levels"}
</button>
) : null}
<button
type="button"
// Not hf-fx-add-item: Tone is a composite over several filters,