Files
hyperframes/packages/core/src/runtime/audioAutomationVolume.ts
T
Miguel Ángel cfff4493df fix(core): a clip's volume lane stays smooth in preview while the page is busy (#6057)
* fix(core): a clip's volume lane stays smooth in preview while the page is busy

A clip with a volume automation lane had two writers on one gain in
preview: the Web Audio transport scheduled the lane as ramps on the
clip's gain, and the per-tick media sync wrote the same lane's value onto
that gain about 60 times a second. Each write landed whenever the main
thread got to it, so a busy page stepped the gain and the clip sounded
choppy. The lane now has its own gain node, like a group's fader beside
its mute gain, and the per-tick volume of a routed clip no longer folds
the lane in.

* fix(core): name the transport's ownership of a routed clip's lane instead of commenting it

* chore(core): raise the unpacked size budget for the volume lane's own gain

The preview fix gives a clip's volume lane its own gain node and tears it
down with the clip, which takes core to 3935950 bytes unpacked, 283 over
the budget main set at its exact size.

* fix(core): volume lanes follow rate changes and edits mid-play

* fix(core): shorten the clip stages comment

* fix(core): point the volume lane comments at the code they name
2026-10-22 15:12:46 +00:00

58 lines
1.9 KiB
TypeScript

/**
* The volume lane's gain for an element at a moment in its clip.
*
* Read per tick for a clip that sounds through `el.volume`. A clip the Web Audio
* transport routes plays its lane scheduled on a gain of its own
* (`attachClipStages`), out of reach of the per-tick gain write.
*/
import {
HF_AUDIO_AUTOMATION_ATTR,
parseAutomation,
sampleAutomationLane,
VOLUME_TARGET,
type HfAutomationLane,
} from "../audioAutomation.js";
/**
* Parsed lanes by the attribute text they came from. Reparsing on every tick
* would run the JSON parser 60 times a second per track for a value that only
* changes when the author edits it.
*/
const cache = new Map<string, HfAutomationLane | null>();
const CACHE_LIMIT = 64;
/** The lane for `target` inside a `data-automation` value, or null when absent or unreadable. */
export function laneFromAttr(raw: string, target: string = VOLUME_TARGET): HfAutomationLane | null {
const key = `${target}\0${raw}`;
const hit = cache.get(key);
if (hit !== undefined) return hit;
let lane: HfAutomationLane | null = null;
try {
lane = parseAutomation(raw).lanes.find((l) => l.target === target) ?? null;
} catch {
// Unreadable automation plays the track flat rather than silencing it.
lane = null;
}
if (cache.size > CACHE_LIMIT) cache.clear();
cache.set(key, lane);
return lane;
}
/**
* Gain the lane asks for at `relTime` clip-local seconds, or null when the
* element has no volume lane and the caller should fall back to its own rules.
*/
export function elementVolumeLaneGain(
el: { getAttribute?(name: string): string | null },
relTime: number,
): number | null {
const raw =
(typeof el.getAttribute === "function" ? el.getAttribute(HF_AUDIO_AUTOMATION_ATTR) : null) ??
"";
if (!raw) return null;
const lane = laneFromAttr(raw);
if (!lane || lane.points.length === 0) return null;
return sampleAutomationLane(lane, relTime);
}