Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(timeline): stack overlapping audio tracks on their own rows

Ola Adebayo90484ec8ad83
diff --git a/src/components/ai-edition/v4/EditorShellV4.module.css b/src/components/ai-edition/v4/EditorShellV4.module.css
index 7962cfca0..6f3eb5bfa 100644
--- a/src/components/ai-edition/v4/EditorShellV4.module.css
+++ b/src/components/ai-edition/v4/EditorShellV4.module.css
@@ -1548,6 +1548,8 @@
gap: 5px;
}
/* The audio lane is taller than the effect lanes to give the waveform room. */
+/* Height is set inline from the row count — the lane grows only as far as the
+ tracks actually overlap. This is the single-row floor. */
.tlLaneAudio {
height: 32px;
}
diff --git a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
index 25c0f3a4e..4fa2d7684 100644
--- a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
@@ -372,9 +372,14 @@ describe("V4Timeline audio lane drag", () => {
origin: "user" as const,
});
+ function renderAudioTracks(tracks: Array<ReturnType<typeof makeTrack>>) {
+ return renderAudio({}, {}, tracks);
+ }
+
function renderAudio(
trackOverrides: Partial<ReturnType<typeof makeTrack>> = {},
props: { onAddVoiceover?: () => void } = {},
+ tracks?: Array<ReturnType<typeof makeTrack>>,
) {
const placeAudioTrack = vi.fn(
async (_id: string, _span: { startMs: number; endMs: number; offsetMs?: number }) => {
@@ -393,7 +398,7 @@ describe("V4Timeline audio lane drag", () => {
selection: null,
multiSelection: [],
clipSelection: null,
- audioTracks: [{ ...makeTrack(), ...trackOverrides }],
+ audioTracks: tracks ?? [{ ...makeTrack(), ...trackOverrides }],
selectedAudioTrackId: null,
selectAudioTrack,
placeAudioTrack,
@@ -403,7 +408,7 @@ describe("V4Timeline audio lane drag", () => {
updateAnnotationSpan: vi.fn(async () => undefined),
addZoom: vi.fn(async () => undefined),
};
- render(
+ const { container } = render(
<ShortcutsProvider>
<V4Timeline
tl={tl as unknown as ReturnType<typeof useTimeline>}
@@ -417,7 +422,16 @@ describe("V4Timeline audio lane drag", () => {
/>
</ShortcutsProvider>,
);
- return { pill: screen.getByTitle("vo"), placeAudioTrack, selectAudioTrack };
+ // `pill` is a getter: the multi-track cases render no "vo" pill, and an
+ // eager lookup would throw before their own assertions ran.
+ return {
+ get pill() {
+ return screen.getByTitle("vo");
+ },
+ container,
+ placeAudioTrack,
+ selectAudioTrack,
+ };
}
// 900px / 1800s = 0.5 px per second, so +90px is +180s.
@@ -457,6 +471,30 @@ describe("V4Timeline audio lane drag", () => {
expect(pill.querySelectorAll('[data-testid="audio-loop-mark"]')).toHaveLength(0);
});
+ it("stacks overlapping tracks on separate rows", () => {
+ // Three takes over the same stretch used to draw at the same height, one
+ // hiding the next — you could not tell which pill you were about to drag.
+ const { container } = renderAudioTracks([
+ { ...makeTrack(), id: "a", label: "a", startMs: 0, endMs: 60_000 },
+ { ...makeTrack(), id: "b", label: "b", startMs: 10_000, endMs: 70_000 },
+ { ...makeTrack(), id: "c", label: "c", startMs: 20_000, endMs: 80_000 },
+ ]);
+ const tops = ["a", "b", "c"].map((l) => (screen.getByTitle(l) as HTMLElement).style.top);
+ expect(new Set(tops).size).toBe(3);
+ // ...and the lane grew to hold them rather than clipping.
+ const lane = container.querySelector('[class*="tlLaneAudio"]') as HTMLElement;
+ expect(Number.parseInt(lane.style.height, 10)).toBeGreaterThan(60);
+ });
+
+ it("keeps non-overlapping tracks on one row", () => {
+ renderAudioTracks([
+ { ...makeTrack(), id: "a", label: "a", startMs: 0, endMs: 10_000 },
+ { ...makeTrack(), id: "b", label: "b", startMs: 20_000, endMs: 30_000 },
+ ]);
+ const tops = ["a", "b"].map((l) => (screen.getByTitle(l) as HTMLElement).style.top);
+ expect(new Set(tops).size).toBe(1);
+ });
+
it("selects the track on pointer-down before any movement", () => {
const { pill, selectAudioTrack } = renderAudio();
fireEvent.pointerDown(pill, { clientX: 0 });
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index de9235bca..d0f43c32e 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -33,7 +33,7 @@ import { ZOOM_DEPTH_SCALES } from "@/components/video-editor/types";
import { useScopedT } from "@/contexts/I18nContext";
import { useShortcuts } from "@/contexts/ShortcutsContext";
import { useAudioPeaks } from "@/hooks/useAudioPeaks";
-import { collapseTracksToPills } from "@/lib/ai-edition/document/audioTracks";
+import { collapseTracksToPills, packAudioTrackRows } from "@/lib/ai-edition/document/audioTracks";
import { createId } from "@/lib/ai-edition/document/ids";
import { setUiProbeScrubbing } from "@/lib/ai-edition/perf/uiFrameProbe";
import type { AxcutAudioTrack, AxcutClip } from "@/lib/ai-edition/schema";
@@ -135,6 +135,14 @@ const PILL_HANDLE_OUT_PX = PILL_HANDLE_PX + PILL_MOVE_GAP_PX;
const PILL_CONTENT_MIN_PX = 34;
/** Edge-snap radius while dragging a pill, in screen px. */
const PILL_SNAP_PX = 8;
+// One audio pill's height, and the vertical step between stacked rows. The lane
+// grows by a row for each track that overlaps one already placed — see
+// `packAudioTrackRows`.
+const AUDIO_ROW_HEIGHT_PX = 26;
+const AUDIO_ROW_GAP_PX = 3;
+// Breathing room above the first row and below the last, so a pill never sits
+// flush against the lane's rounded edge.
+const AUDIO_LANE_PAD_PX = 3;
// The size a newly created pill aims for (PILL_CREATE_PX) lives in
// timeline/newRegionDuration, because the keyboard shortcuts create regions too
// and they are handled in NewEditorShell, outside this component.
@@ -361,6 +369,8 @@ const AudioLanePill = memo(function AudioLanePill({
sourceEndSec,
spanSec,
loopWindowSec,
+ row,
+ rowHeight,
selected,
onStartDrag,
onSelect,
@@ -380,6 +390,10 @@ const AudioLanePill = memo(function AudioLanePill({
* together they say where the loop boundaries fall. */
spanSec: number;
loopWindowSec: number;
+ /** Which row of the audio lane this pill occupies, and how tall a row is —
+ * overlapping tracks are stacked rather than drawn on top of each other. */
+ row: number;
+ rowHeight: number;
selected: boolean;
onStartDrag: (e: ReactPointerEvent, track: AxcutAudioTrack, mode: "move" | "l" | "r") => void;
onSelect: (id: string) => void;
@@ -396,7 +410,13 @@ const AudioLanePill = memo(function AudioLanePill({
className={`${styles.lanePill} ${styles.laneAudio}${
selected ? ` ${styles.lanePillSel}` : ""
}`}
- style={{ left: `${leftPct}%`, width: `${widthPct}%`, minWidth: 3 }}
+ style={{
+ left: `${leftPct}%`,
+ width: `${widthPct}%`,
+ minWidth: 3,
+ top: AUDIO_LANE_PAD_PX + row * rowHeight,
+ height: AUDIO_ROW_HEIGHT_PX,
+ }}
// Body drag moves the track; it also selects and stops the .tlTracks scrub.
onPointerDown={(e) => onStartDrag(e, track, "move")}
onKeyDown={(e) => {
@@ -909,6 +929,11 @@ export function V4Timeline({
trimEnd: number;
} | null>(null);
const audioDragRef = useRef<typeof audioDrag>(null);
+ // The user-visible tracks and their lane rows. Packed from the STORED spans,
+ // not the live drag geometry: a pill that changed rows halfway through a drag
+ // would jump out from under the pointer.
+ const audioPills = useMemo(() => collapseTracksToPills(tl.audioTracks), [tl.audioTracks]);
+ const audioRows = useMemo(() => packAudioTrackRows(audioPills), [audioPills]);
// Drag an audio track: "move" slides the head (both edges together), "l"/"r"
// trim the in/out points. The left edge moves the head AND the in-point so the
@@ -1838,7 +1863,18 @@ export function V4Timeline({
lane — "Add audio" is a toolbar peer of the region tools now (and
has a keyboard shortcut), so an empty lane advertises the shortcut
that fills it rather than hiding until the first import. */}
- <div className={`${styles.tlLane} ${styles.tlLaneAudio}`}>
+ <div
+ className={`${styles.tlLane} ${styles.tlLaneAudio}`}
+ // Grows a row per overlapping track, so three voiceovers over
+ // the same stretch are three legible pills rather than one
+ // pile nobody can aim at.
+ style={{
+ height:
+ audioRows.rowCount * AUDIO_ROW_HEIGHT_PX +
+ (audioRows.rowCount - 1) * AUDIO_ROW_GAP_PX +
+ AUDIO_LANE_PAD_PX * 2,
+ }}
+ >
{tl.audioTracks.length === 0 ? (
<span
className={styles.laneEmpty}
@@ -1850,7 +1886,7 @@ export function V4Timeline({
// One pill per user-visible track: the document stores one
// clip-anchored fragment per clip the track covers, and the
// lane must not show a split take as two pills.
- collapseTracksToPills(tl.audioTracks).map((track) => {
+ audioPills.map((track) => {
const asset = tl.assets.find((a) => a.id === track.assetId);
const duration = asset?.durationSec ?? track.durationSec;
// While this track is being dragged, lay it out from the live
@@ -1872,6 +1908,8 @@ export function V4Timeline({
assetDurationSec={duration}
leftPct={pctOf(start)}
widthPct={pctOf(widthSec)}
+ row={audioRows.rowOf.get(track.id) ?? 0}
+ rowHeight={AUDIO_ROW_HEIGHT_PX + AUDIO_ROW_GAP_PX}
spanSec={widthSec}
loopWindowSec={Math.max(0, (duration || 0) - trimStart)}
sourceStartSec={trimStart}
diff --git a/src/lib/ai-edition/document/audioTracks.test.ts b/src/lib/ai-edition/document/audioTracks.test.ts
index 586e9be4e..49b061d6e 100644
--- a/src/lib/ai-edition/document/audioTracks.test.ts
+++ b/src/lib/ai-edition/document/audioTracks.test.ts
@@ -3,6 +3,7 @@ import { type AxcutAsset, type AxcutClip, createAudioTrack, createEmptyDocument
import {
anchorAudioTrackFragments,
collapseTracksToPills,
+ packAudioTrackRows,
patchAudioTrack,
removeAudioTrack,
resolveFadeSecs,
@@ -232,3 +233,47 @@ describe("resolveFadeSecs", () => {
expect(fadeOutSec).toBeCloseTo(0.8);
});
});
+
+describe("packAudioTrackRows", () => {
+ const t = (id: string, startMs: number, endMs: number) => ({ id, startMs, endMs });
+
+ it("keeps tracks that never overlap on one row", () => {
+ // The common case stays a single-line lane.
+ const { rowOf, rowCount } = packAudioTrackRows([
+ t("a", 0, 1000),
+ t("b", 1000, 2000),
+ t("c", 5000, 6000),
+ ]);
+ expect(rowCount).toBe(1);
+ expect([rowOf.get("a"), rowOf.get("b"), rowOf.get("c")]).toEqual([0, 0, 0]);
+ });
+
+ it("stacks tracks that overlap, so neither hides the other", () => {
+ const { rowOf, rowCount } = packAudioTrackRows([t("a", 0, 5000), t("b", 1000, 2000)]);
+ expect(rowCount).toBe(2);
+ expect(rowOf.get("a")).toBe(0);
+ expect(rowOf.get("b")).toBe(1);
+ });
+
+ it("reuses a row as soon as it frees up", () => {
+ // b overlaps a and goes to row 1; c starts after a ends, so it drops back
+ // to row 0 rather than opening a third row.
+ const { rowOf, rowCount } = packAudioTrackRows([
+ t("a", 0, 3000),
+ t("b", 1000, 9000),
+ t("c", 4000, 5000),
+ ]);
+ expect(rowCount).toBe(2);
+ expect(rowOf.get("c")).toBe(0);
+ });
+
+ it("treats touching tracks as non-overlapping", () => {
+ // One ending exactly where the next begins is a sequence, not a pile.
+ const { rowCount } = packAudioTrackRows([t("a", 0, 1000), t("b", 1000, 2000)]);
+ expect(rowCount).toBe(1);
+ });
+
+ it("always reports at least one row, even with nothing to place", () => {
+ expect(packAudioTrackRows([]).rowCount).toBe(1);
+ });
+});
diff --git a/src/lib/ai-edition/document/audioTracks.ts b/src/lib/ai-edition/document/audioTracks.ts
index abcf605ec..73c23866c 100644
--- a/src/lib/ai-edition/document/audioTracks.ts
+++ b/src/lib/ai-edition/document/audioTracks.ts
@@ -199,3 +199,40 @@ export function resolveFadeSecs(
const scale = Math.max(0, spanSec) / total;
return { fadeInSec: fadeInSec * scale, fadeOutSec: fadeOutSec * scale };
}
+
+/**
+ * Assign each track a ROW in the audio lane, so two tracks that overlap in time
+ * never sit on top of each other.
+ *
+ * Greedy first-fit over tracks in start order: a track takes the topmost row
+ * whose last occupant has already finished, and opens a new row only when every
+ * existing one is still busy. Tracks that do not overlap therefore keep sharing
+ * one row — the lane stays a single line for the common case, and grows only as
+ * far as the actual overlap demands.
+ *
+ * Stacking is the whole point: a lane that draws every track at the same height
+ * turns three voiceovers into one illegible pile where the user cannot tell
+ * which pill they are about to drag.
+ *
+ * Returns the row index per track id, plus how many rows the lane needs.
+ */
+export function packAudioTrackRows(tracks: Array<{ id: string; startMs: number; endMs: number }>): {
+ rowOf: Map<string, number>;
+ rowCount: number;
+} {
+ const ordered = [...tracks].sort((a, b) => a.startMs - b.startMs || a.endMs - b.endMs);
+ // The end of the last track placed in each row, in the same index order.
+ const rowEnds: number[] = [];
+ const rowOf = new Map<string, number>();
+ for (const track of ordered) {
+ let row = rowEnds.findIndex((end) => end <= track.startMs);
+ if (row === -1) {
+ row = rowEnds.length;
+ rowEnds.push(track.endMs);
+ } else {
+ rowEnds[row] = track.endMs;
+ }
+ rowOf.set(track.id, row);
+ }
+ return { rowOf, rowCount: Math.max(1, rowEnds.length) };
+}