Files
hyperframes/packages/core/src/audioGain.ts
Miguel Ángel e282ff15cc fix(audio): raise the authoring gain ceiling and carry it through the probes (#3333)
* fix(audio): raise the authoring gain ceiling and carry it through the probes

Builds on #3328, which made the preview graph apply author gain and user volume
exactly once each. That ownership is now correct but everything is still clamped
to 1.0, so a clip authored above unity cannot be heard or rendered.

`HTMLMediaElement.volume` is spec-clamped to [0,1], so both timeline probes lost
a clip's authored gain the moment it also carried a fade: the probe seeded the
element at the clamped value and every sample read back at or below 0 dB, and
the mixer prefers probed keyframes over the static volume. Both probes now
shadow the accessor for their own duration and forward the clamped value to the
native setter, so the authored gain survives while nothing outside the probe
ever sees an illegal volume.

Measured on one 6 s composition, first 4 s: unity -32.8 LUFS, boosted-with-fade
-32.8 before and -27.0 after — +5.8 dB, exactly the gain the clip was authored
at.

One ceiling, defined once in `audioGain.ts` and reachable from both sides: the
render mixer imports it, and the page-serialized probe takes it as a parameter
rather than re-literalling it. User volume stays spec-clamped — it is a fader,
not a gain.

Also holds the percent volume slider above unity in both property panels. That
control tops out at 100%, so one touch would cap a boosted clip and drop up to
12 dB that now genuinely renders; the dB fader that can represent these levels
replaces it in the next PR.

* fix(audio): carry a static above-unity gain onto the preview gain node

Review follow-up.

`setElementVolume` receives the clip's author gain and clamped it to [0,1],
so a static `data-volume` above unity was capped on the WebAudio preview path
while the render honoured it — the exact preview/render divergence this
ceiling exists to close. Automation lanes hid it: they schedule ramps onto the
param directly and never pass through here. The master volume beside it stays
spec-clamped, because a user fader is not a gain.

Verified by mutation: restoring the [0,1] clamp reds the new case.

Also scope the leveller's rationale to this rung — `VOLUME_RANGE` still stops
at unity until the dB fader lands, so "both now span the same range" was
premature — and say why the GSAP-tracking fallback is unity-capped: it reads
back through `el.volume`, which the spec pins to [0,1], so it cannot observe an
above-unity value however wide the clamp gets.

* fix(audio): restore the live test files this branch overwrote, and uncap preview

Review blocker: three files were wholesale copies from the abandoned #3304
branch laid over a two-day-newer base, so they silently reverted work that had
landed in between. CI could not see it — deleted tests do not fail.

- `audioMixer.test.ts` was byte-identical to #3304's head: 1186 lines against a
  base of 1353. Gone with it were the `data-playback-start` fallthrough cases
  from #3322 — merged 54 minutes before this branch's own merge base — and all
  retiming coverage (`playbackRate` 7 to 0, `atempo` 5 to 0), the strict
  literal-timing table, and the zero-window cases.
- `mediaVolumeEnvelope.test.ts` dropped the trailing-garbage duration case and
  the plateau-retention case.
- `packages/core/package.json` rolled the package version back 0.8.3 to 0.7.109.

All three are restored from `main` with only this PR's additions re-applied on
top, and the subpath export is regenerated by the repo's own script rather than
hand-edited.

Also closes the preview/render split the same review raised. Two clamps had to
go, not one: `setElementVolume` capped the author gain at the transport, and
the first-tick branch in `syncRuntimeMedia` trusted `el.volume` — which is
spec-bound to [0,1] and so cannot represent a boost, opening a boosted clip at
0 dB for one tick before the steady-state branch took over. Both pinned by
tests, both verified by mutation.
2026-08-18 20:07:42 -04:00

111 lines
4.1 KiB
TypeScript

/**
* Authoring gain for a media clip.
*
* HTMLMediaElement.volume is limited to 0..1, but HyperFrames' Web Audio
* preview and FFmpeg render paths both support gain above unity. Keep the
* shared ceiling here so Studio, preview, and render cannot drift.
*/
export const MAX_AUDIO_GAIN_DB = 12;
export const MAX_AUDIO_GAIN = 10 ** (MAX_AUDIO_GAIN_DB / 20);
/** Studio fader coordinates. Unity is deliberately the physical midpoint. */
export const AUDIO_GAIN_FADER_MIN = -100;
export const AUDIO_GAIN_FADER_MAX = 100;
const MIN_AUDIO_GAIN_DB = -60;
export function clampAudioGain(value: number): number {
if (!Number.isFinite(value)) return 1;
return Math.max(0, Math.min(MAX_AUDIO_GAIN, value));
}
export function clampNativeMediaVolume(value: number): number {
if (!Number.isFinite(value)) return 1;
return Math.max(0, Math.min(1, value));
}
/**
* Serialize an authored gain for `data-volume`.
*
* The fader travels in dB, so its stops are irrational (position -70 is
* 10 ** (-42/20)). Rounding to two decimals — what the generic numeric
* attribute formatter does — collapses the whole bottom of the fader onto
* `"0"` (a hard mute) and makes the knob jump on release everywhere below
* unity. Six decimals round-trip every integer fader stop back to itself.
*/
export function formatAudioGain(gain: number): string {
return clampAudioGain(gain)
.toFixed(6)
.replace(/\.?0+$/, "");
}
/**
* Run `probe` with `el.volume` shadowed by an accessor that keeps the authored
* value instead of the spec's [0,1] clamp.
*
* `HTMLMediaElement.volume` cannot hold gain above unity, so a clip authored
* at `data-volume="1.95"` reads back as 1 the moment the probe seeds it — and
* a GSAP tween started from that seed fades from 0 dB rather than from the
* authored boost. Both the FFmpeg mixer and the Web Audio transport carry gain
* up to MAX_AUDIO_GAIN, so the clamp is a probe artefact, not a real ceiling.
* The native setter still receives the clamped value, so nothing outside the
* probe observes an out-of-range volume, and the shadow is removed afterwards.
*/
export function withUnclampedVolume<T>(el: HTMLMediaElement, probe: () => T): T {
// Guarded for non-DOM runtimes: the probe that calls this is also reachable
// from tests and tools that run outside a browser, where the clamped path is
// the right (and only) answer.
const descriptor =
typeof HTMLMediaElement === "undefined"
? undefined
: Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "volume");
const nativeGet = descriptor?.get;
const nativeSet = descriptor?.set;
if (!nativeGet || !nativeSet) return probe();
let authored = Number(nativeGet.call(el));
Object.defineProperty(el, "volume", {
configurable: true,
get: () => authored,
set: (value: number) => {
authored = Number(value);
nativeSet.call(el, clampNativeMediaVolume(authored));
},
});
try {
return probe();
} finally {
delete (el as unknown as Record<"volume", unknown>).volume;
nativeSet.call(el, clampNativeMediaVolume(authored));
}
}
export function audioFaderPositionToGain(position: number): number {
const safe = Math.max(AUDIO_GAIN_FADER_MIN, Math.min(AUDIO_GAIN_FADER_MAX, position));
if (safe === AUDIO_GAIN_FADER_MIN) return 0;
const db =
safe < 0
? (safe / Math.abs(AUDIO_GAIN_FADER_MIN)) * Math.abs(MIN_AUDIO_GAIN_DB)
: (safe / AUDIO_GAIN_FADER_MAX) * MAX_AUDIO_GAIN_DB;
return 10 ** (db / 20);
}
export function audioGainToFaderPosition(gain: number): number {
const safe = clampAudioGain(gain);
if (safe === 0) return AUDIO_GAIN_FADER_MIN;
const db = 20 * Math.log10(safe);
const position =
db < 0
? (db / Math.abs(MIN_AUDIO_GAIN_DB)) * Math.abs(AUDIO_GAIN_FADER_MIN)
: (db / MAX_AUDIO_GAIN_DB) * AUDIO_GAIN_FADER_MAX;
return Math.max(AUDIO_GAIN_FADER_MIN, Math.min(AUDIO_GAIN_FADER_MAX, position));
}
export function audioGainToText(gain: number): string {
const safe = clampAudioGain(gain);
if (safe === 0) return "-∞ dB";
const db = 20 * Math.log10(safe);
const rounded = Math.abs(db) < 0.05 ? 0 : db;
return (rounded > 0 ? "+" : "") + rounded.toFixed(1) + " dB";
}