Skip to content

Commit 07a126d

Browse files
vanceingallsclaude
andauthored
fix(core): preview audio fades play as ramps on the audio clock, so cuts no longer click (#6397)
* fix(core): preview audio fades play as ramps on the audio clock, so cuts no longer click data-fade-in / data-fade-out were folded into one volume per 50 ms sync tick and applied as a step (el.volume, or gain.value in the Web Audio transport), so a 20 ms fade never played and each clip started and stopped at near full level: a click at every cut. The export applies the fades (ffmpeg afade) and does not click. The transport now books each clip's fades on a gain of its own when it schedules the clip, for both decoded buffers and captured media elements: a linear ramp from where the clip starts sounding up to its fade-in, and down to silence at its end. The sync tick hands the transport the level without the fade (onElementVolume's new fifth argument, sent only where a fade applies), so a booked fade is not applied twice. The plain el.volume fallback keeps its stepped fade. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(core): one doc block on setElementVolume Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(core): keep the comment share where it was Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(core): booked fades hold through pre-roll, follow rate changes, skip unbounded ends Co-Authored-By: Claude Opus 5.5 <[email protected]> * chore: raise the producer unpacked budget to the measured size plus 1% Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0390f3f commit 07a126d

6 files changed

Lines changed: 232 additions & 13 deletions

File tree

‎packages/core/src/runtime/init.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,8 +3643,8 @@ export function initSandboxRuntimeModular(): void {
36433643
cueAheadSeconds,
36443644
getCompositionDuration: () =>
36453645
getSafeTimelineDurationSeconds(state.capturedTimeline, 0, timingRevision),
3646-
onElementVolume: (el, _effectiveVolume, authorVolume, clipGain) =>
3647-
webAudio.setElementVolume(el, authorVolume, clipGain),
3646+
onElementVolume: (el, _effectiveVolume, authorVolume, clipGain, unfadedVolume) =>
3647+
webAudio.setElementVolume(el, authorVolume, clipGain, unfadedVolume),
36483648
isWebAudioOwned: (el) => webAudio.ownsElement(el),
36493649
isWebAudioRouted: (el) => webAudio.routesElement(el),
36503650
onAutoplayBlocked: () => {
@@ -5793,6 +5793,7 @@ export function initSandboxRuntimeModular(): void {
57935793
vol,
57945794
gen,
57955795
state.playbackRate,
5796+
clipDuration,
57965797
)
57975798
: Promise.resolve(null);
57985799
void capture.then((scheduled) => {

‎packages/core/src/runtime/media.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,23 @@ describe("syncRuntimeMedia", () => {
524524
expect(at(11.9)).toBeCloseTo(0.08, 5); // almost at the clip's end
525525
});
526526

527+
it("also hands a transport the level without the fade, so one that books fades on its own clock does not fade twice", () => {
528+
const clip = createMockClip({ start: 2, end: 12, duration: 10, volume: 0.8 });
529+
Object.defineProperty(clip.el, "readyState", { value: 4, writable: true });
530+
clip.fades = { fadeIn: 2, fadeOut: 1 };
531+
const onElementVolume = vi.fn();
532+
syncRuntimeMedia({
533+
clips: [clip],
534+
timeSeconds: 3,
535+
playing: true,
536+
playbackRate: 1,
537+
onElementVolume,
538+
});
539+
const [, , authorVolume, , unfadedVolume] = onElementVolume.mock.lastCall ?? [];
540+
expect(authorVolume).toBeCloseTo(0.4, 5); // halfway through the fade-in, as the tick steps it
541+
expect(unfadedVolume).toBeCloseTo(0.8, 5);
542+
});
543+
527544
it("never writes NaN or a negative volume for 0, negative, NaN, or longer-than-clip fades", () => {
528545
const cases: Array<{ fadeIn: number; fadeOut: number }> = [
529546
{ fadeIn: 0, fadeOut: 0 },

‎packages/core/src/runtime/media.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ export function syncRuntimeMedia(params: {
376376
effectiveVolume: number,
377377
authorVolume: number,
378378
clipGain: number,
379+
unfadedVolume?: number,
379380
) => void;
380381
/** Is THIS element owned by the Web Audio transport? Owned → mute it (transport
381382
* plays it); not owned → leave audible (HTMLMedia fallback). Per-element, not a
@@ -507,6 +508,7 @@ export function syncRuntimeMedia(params: {
507508
}
508509

509510
// Clip-local fade on top of the resolved level, matching render's afade-after-volume.
511+
const unfadedVolume = authorVolume;
510512
const fades = clip.fades ?? NO_FADES;
511513
if (fades.fadeIn > 0 || fades.fadeOut > 0) {
512514
authorVolume *= fadeGain(
@@ -529,7 +531,9 @@ export function syncRuntimeMedia(params: {
529531
const effectiveVolume = silencedByHidden ? 0 : clampVolume(authorVolume * userVol);
530532
if (Math.abs(el.volume - effectiveVolume) > 1e-6) el.volume = effectiveVolume;
531533
lastRuntimeAppliedVolume.set(el, effectiveVolume);
532-
params.onElementVolume?.(el, effectiveVolume, authorVolume, preFxGain);
534+
if (unfadedVolume === authorVolume)
535+
params.onElementVolume?.(el, effectiveVolume, authorVolume, preFxGain);
536+
else params.onElementVolume?.(el, effectiveVolume, authorVolume, preFxGain, unfadedVolume);
533537
// Mute only when force-muted or the transport owns this element; an unclaimed
534538
// track stays audible via the HTMLMedia fallback.
535539
if ((forceMuteAll || params.isWebAudioOwned?.(el)) && !el.muted) el.muted = true;

‎packages/core/src/runtime/webAudioTransport.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,3 +1721,126 @@ describe("WebAudioTransport", () => {
17211721
});
17221722
});
17231723
});
1724+
1725+
describe("WebAudioTransport clip fades on the audio clock", () => {
1726+
type Param = {
1727+
value: number;
1728+
setValueAtTime: ReturnType<typeof vi.fn>;
1729+
linearRampToValueAtTime: ReturnType<typeof vi.fn>;
1730+
cancelScheduledValues: ReturnType<typeof vi.fn>;
1731+
};
1732+
const param = (): Param => ({
1733+
value: 1,
1734+
setValueAtTime: vi.fn(),
1735+
linearRampToValueAtTime: vi.fn(),
1736+
cancelScheduledValues: vi.fn(),
1737+
});
1738+
/** A context whose gains record their automation; the element gain is the first one made. */
1739+
function setup(currentTime = 100) {
1740+
const { transport, mock, gen } = setupTransport(currentTime);
1741+
const gains: {
1742+
gain: Param;
1743+
connect: ReturnType<typeof vi.fn>;
1744+
disconnect: ReturnType<typeof vi.fn>;
1745+
}[] = [];
1746+
mock.ctx.createGain = vi.fn(() => {
1747+
const node = { gain: param(), connect: vi.fn(), disconnect: vi.fn() };
1748+
gains.push(node);
1749+
return node;
1750+
}) as never;
1751+
const booked = () => gains.find((g) => g.gain.setValueAtTime.mock.calls.length > 0)?.gain;
1752+
return { transport, mock, gen, gains, booked };
1753+
}
1754+
const faded = (fadeIn = "0.02", fadeOut = "0.02") =>
1755+
({
1756+
muted: false,
1757+
volume: 1,
1758+
paused: true,
1759+
addEventListener: () => {},
1760+
getAttribute: (name: string) =>
1761+
({ "data-playback-rate": "1", "data-fade-in": fadeIn, "data-fade-out": fadeOut })[name] ??
1762+
null,
1763+
}) as unknown as HTMLMediaElement;
1764+
1765+
it("a decoded clip ramps in over its fade-in from its start and out to silence at its end", async () => {
1766+
const { transport, gen, booked } = setup(100);
1767+
await transport.schedulePlayback(faded(), mockBuffer, 5, 0, () => 5, 1, gen, 1, 1);
1768+
const gain = booked();
1769+
expect(gain?.setValueAtTime.mock.calls).toEqual([
1770+
[0, 100],
1771+
[1, expect.closeTo(100.98, 9)],
1772+
]);
1773+
expect(gain?.linearRampToValueAtTime.mock.calls).toEqual([
1774+
[1, expect.closeTo(100.02, 9)],
1775+
[0, expect.closeTo(101, 9)],
1776+
]);
1777+
});
1778+
1779+
it("a clip started halfway through its fade-in starts at half level", async () => {
1780+
const { transport, gen, booked } = setup(100);
1781+
await transport.schedulePlayback(faded(), mockBuffer, 5, 0, () => 5.01, 1, gen, 1, 1);
1782+
const gain = booked();
1783+
expect(gain?.setValueAtTime.mock.calls[0]).toEqual([expect.closeTo(0.5, 9), 100]);
1784+
expect(gain?.linearRampToValueAtTime.mock.calls[0]).toEqual([1, expect.closeTo(100.01, 9)]);
1785+
});
1786+
1787+
it("a captured media element books its fades the same way", async () => {
1788+
const { transport, gen, booked } = setup(100);
1789+
await transport.scheduleMediaElementPlayback(faded(), 5, 0, 5, 1, gen, 1, 1);
1790+
expect(booked()?.linearRampToValueAtTime.mock.calls).toEqual([
1791+
[1, expect.closeTo(100.02, 9)],
1792+
[0, expect.closeTo(101, 9)],
1793+
]);
1794+
});
1795+
1796+
it("a clip scheduled ahead of its start holds its fade-in's first level until it starts", async () => {
1797+
const { transport, gen, booked } = setup(100);
1798+
await transport.schedulePlayback(faded(), mockBuffer, 5, 0, () => 4.9, 1, gen, 1, 1);
1799+
const gain = booked();
1800+
expect(gain?.value).toBe(0);
1801+
expect(gain?.setValueAtTime.mock.calls[0]).toEqual([0, expect.closeTo(100.1, 9)]);
1802+
});
1803+
1804+
it("a fade-out with no clip end books nothing, so the per-tick volume keeps the fade", async () => {
1805+
const { transport, gen, gains, booked } = setup(100);
1806+
const el = faded("0.02", "2");
1807+
await transport.scheduleMediaElementPlayback(el, 5, 0, 5, 1, gen, 1);
1808+
expect(booked()).toBeUndefined();
1809+
transport.setElementVolume(el, 0.4, 1, 0.8);
1810+
expect(gains[0]!.gain.value).toBeCloseTo(0.4, 9);
1811+
});
1812+
1813+
it("a rate change rebooks the fades on the new rate from where the clip is", async () => {
1814+
const { transport, mock, gen, booked } = setup(100);
1815+
await transport.scheduleMediaElementPlayback(faded("0.02", "1"), 5, 0, 5, 1, gen, 2, 10);
1816+
const gain = booked()!;
1817+
mock.ctx.currentTime = 101;
1818+
transport.setRate(1);
1819+
expect(gain.cancelScheduledValues).toHaveBeenCalled();
1820+
expect(gain.linearRampToValueAtTime.mock.calls.at(-1)).toEqual([
1821+
0,
1822+
expect.closeTo(101 + (10 - 2), 9),
1823+
]);
1824+
});
1825+
1826+
it("a clip without fades books nothing", async () => {
1827+
const { transport, gen, booked } = setup(100);
1828+
await transport.schedulePlayback(faded("", ""), mockBuffer, 5, 0, () => 5, 1, gen, 1, 1);
1829+
expect(booked()).toBeUndefined();
1830+
});
1831+
1832+
it("the per-tick volume leaves out the fade where it is booked, and keeps it where it is not", async () => {
1833+
const { transport, gen, gains } = setup(100);
1834+
const withFades = faded();
1835+
await transport.schedulePlayback(withFades, mockBuffer, 5, 0, () => 5, 1, gen, 1, 1);
1836+
const elementGain = gains[0]!.gain;
1837+
transport.setElementVolume(withFades, 0.4, 1, 0.8);
1838+
expect(elementGain.value).toBeCloseTo(0.8, 9);
1839+
1840+
const second = setup(100);
1841+
const plain = faded("", "");
1842+
await second.transport.schedulePlayback(plain, mockBuffer, 5, 0, () => 5, 1, second.gen, 1, 1);
1843+
second.transport.setElementVolume(plain, 0.4, 1, 0.8);
1844+
expect(second.gains[0]!.gain.value).toBeCloseTo(0.4, 9);
1845+
});
1846+
});

0 commit comments

Comments
 (0)