Files
hyperframes/packages/core/src/audioFade.test.ts
839ae426dd feat(studio): typed audio values, clip fades, audible video drops (#4250)
* feat(studio): typed audio values, clip fades, audible video drops

Audio rows in the media inspector (Volume, Speed, Media start) take a
typed value: click the readout, type "-6 dB", "1.5x" or "2.5s", Enter.
Speed presets are hidden on <audio> clips.

Clip-edge fades: data-fade-in / data-fade-out in seconds, honoured by the
preview runtime (per-tick gain), the render mixer (afade after volume) and
Studio (Fade in / Fade out rows, and drag dots at the top corners of audio
clips and audible videos). Linear on both sides so preview and render
agree; fades that outrun the clip scale to meet inside it.

Dropped videos with an audio stream now land with data-has-audio="true"
instead of muted, so the mixer keeps their sound. The media metadata
endpoint reports hasAudio from the ffprobe call it already makes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(audio): one owner for fades, dB, and afade-after-volume

Illegal fade values become no-fade at fadeGain; typed inspector input
still returns null so the readout stays red. Master and group mixes
share one filter chain with volume then afade. Fade-out stays anchored
to the trimmed clip end.

* fix(studio): share audio helpers and bound typed media values

* fix(studio): reserve sibling fade duration when editing

---------

Co-authored-by: Jake Moran <jakemoran2011@gmail.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Miguel Angel Simon Sierra <miguel.sierra@heygen.com>
2026-09-21 18:45:08 -04:00

100 lines
3.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
clampFadesToDuration,
fadeGain,
formatFadeSeconds,
readElementFades,
readFadeSeconds,
} from "./audioFade.js";
describe("readFadeSeconds", () => {
it("reads a positive number and treats everything else as no fade", () => {
expect(readFadeSeconds("0.5")).toBe(0.5);
expect(readFadeSeconds("2")).toBe(2);
expect(readFadeSeconds(null)).toBe(0);
expect(readFadeSeconds("")).toBe(0);
expect(readFadeSeconds("-1")).toBe(0);
expect(readFadeSeconds("NaN")).toBe(0);
expect(readFadeSeconds("abc")).toBe(0);
});
it("reads both attributes off an element", () => {
const attrs: Record<string, string> = { "data-fade-in": "0.25", "data-fade-out": "1.5" };
expect(readElementFades({ getAttribute: (n) => attrs[n] ?? null })).toEqual({
fadeIn: 0.25,
fadeOut: 1.5,
});
expect(readElementFades({ getAttribute: () => null })).toEqual({ fadeIn: 0, fadeOut: 0 });
});
});
describe("clampFadesToDuration", () => {
it("leaves fades that fit alone", () => {
expect(clampFadesToDuration({ fadeIn: 1, fadeOut: 2 }, 10)).toEqual({ fadeIn: 1, fadeOut: 2 });
});
it("scales fades that outrun the clip so they meet inside it", () => {
expect(clampFadesToDuration({ fadeIn: 6, fadeOut: 6 }, 6)).toEqual({ fadeIn: 3, fadeOut: 3 });
expect(clampFadesToDuration({ fadeIn: 3, fadeOut: 1 }, 2)).toEqual({
fadeIn: 1.5,
fadeOut: 0.5,
});
});
it("keeps authored fades on a clip of unknown duration", () => {
expect(clampFadesToDuration({ fadeIn: 4, fadeOut: 4 }, Number.POSITIVE_INFINITY)).toEqual({
fadeIn: 4,
fadeOut: 4,
});
});
});
describe("fadeGain", () => {
const fades = { fadeIn: 2, fadeOut: 1 };
it("is unity with no fades", () => {
expect(fadeGain(0, 10, { fadeIn: 0, fadeOut: 0 })).toBe(1);
expect(fadeGain(10, 10, { fadeIn: 0, fadeOut: 0 })).toBe(1);
});
it("ramps linearly from silence over the fade-in", () => {
expect(fadeGain(0, 10, fades)).toBe(0);
expect(fadeGain(1, 10, fades)).toBe(0.5);
expect(fadeGain(2, 10, fades)).toBe(1);
expect(fadeGain(5, 10, fades)).toBe(1);
});
it("ramps linearly to silence over the fade-out, anchored at the clip end", () => {
expect(fadeGain(9, 10, fades)).toBe(1);
expect(fadeGain(9.5, 10, fades)).toBe(0.5);
expect(fadeGain(10, 10, fades)).toBe(0);
expect(fadeGain(11, 10, fades)).toBe(0);
});
it("multiplies where the two fades overlap on a short clip", () => {
// 2 s clip, both fades scaled to 1 s each: midpoint is the seam.
expect(fadeGain(1, 2, { fadeIn: 2, fadeOut: 2 })).toBe(1);
expect(fadeGain(0.5, 2, { fadeIn: 2, fadeOut: 2 })).toBe(0.5);
});
it("never applies a fade-out to a clip of unknown duration", () => {
expect(fadeGain(100, Number.POSITIVE_INFINITY, fades)).toBe(1);
expect(fadeGain(1, Number.POSITIVE_INFINITY, fades)).toBe(0.5);
});
it("treats 0, negative, and NaN fades as no fade", () => {
expect(fadeGain(0, 10, { fadeIn: 0, fadeOut: 0 })).toBe(1);
expect(fadeGain(0, 10, { fadeIn: -1, fadeOut: -4 })).toBe(1);
expect(fadeGain(0, 10, { fadeIn: Number.NaN, fadeOut: Number.NaN })).toBe(1);
});
it("returns a finite 0..1 gain for NaN elapsed and for fades longer than the clip", () => {
expect(fadeGain(Number.NaN, 10, fades)).toBe(1);
const overlapping = fadeGain(0.5, 2, { fadeIn: 10, fadeOut: 10 });
expect(overlapping).toBeGreaterThanOrEqual(0);
expect(overlapping).toBeLessThanOrEqual(1);
expect(Number.isFinite(overlapping)).toBe(true);
});
});
describe("formatFadeSeconds", () => {
it("writes short attribute text", () => {
expect(formatFadeSeconds(0)).toBe("0");
expect(formatFadeSeconds(1)).toBe("1");
expect(formatFadeSeconds(0.5)).toBe("0.5");
expect(formatFadeSeconds(0.333)).toBe("0.33");
expect(formatFadeSeconds(-2)).toBe("0");
});
});