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(audio): make loop fill by itself, and mark where it repeats

Ola Adebayo16b0b3f1fa71
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index 7a9b73552..e6c08b9f1 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -1064,6 +1064,14 @@ export function NewEditorShell() {
}
if (tl.selection) {
void tl.removeRegion(tl.selection.kind, tl.selection.id);
+ return;
+ }
+ // An audio track is selected through its OWN channel, not `selection`
+ // (the two are mutually exclusive — see addAudioTrack), so it needs its
+ // own branch here or Delete does nothing on the one lane that looks
+ // exactly like every other.
+ if (tl.selectedAudioTrackId) {
+ void tl.removeAudioTrack(tl.selectedAudioTrackId);
}
};
diff --git a/src/components/ai-edition/RightPanes.tsx b/src/components/ai-edition/RightPanes.tsx
index 8d55c7eb1..be50c4202 100644
--- a/src/components/ai-edition/RightPanes.tsx
+++ b/src/components/ai-edition/RightPanes.tsx
@@ -2468,7 +2468,9 @@ export function AudioTrackPane({ tl }: { tl: TimelineApi }) {
<Toggle
checked={track.loop}
ariaLabel={ts("audioTrack.loop")}
- onChange={(v) => void tl.updateAudioTrack(track.id, { loop: v })}
+ // Fills the rest of the programme on the way on — see
+ // setAudioTrackLoop for why the toggle moves the edge for you.
+ onChange={(v) => void tl.setAudioTrackLoop(track.id, v)}
/>
</div>
<button
diff --git a/src/components/ai-edition/v4/EditorShellV4.module.css b/src/components/ai-edition/v4/EditorShellV4.module.css
index 0a72c761b..cfe219f03 100644
--- a/src/components/ai-edition/v4/EditorShellV4.module.css
+++ b/src/components/ai-edition/v4/EditorShellV4.module.css
@@ -1510,6 +1510,19 @@
cursor: pointer;
}
/* The label rides above the waveform (which is inset:0 behind it). */
+/* Where a looping track starts its file over. A hairline rather than a full
+ divider: it has to be legible against the waveform without reading as a cut,
+ which is what a solid line on a timeline means everywhere else. */
+.laneLoopMark {
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ width: 1px;
+ z-index: 1;
+ pointer-events: none;
+ background: color-mix(in srgb, var(--fg) 45%, transparent);
+}
+
.laneAudioLabel {
position: relative;
z-index: 1;
diff --git a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
index 2a7aaa555..4ae330eb2 100644
--- a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
@@ -428,6 +428,18 @@ describe("V4Timeline audio lane drag", () => {
expect(onAddVoiceover).toHaveBeenCalledTimes(1);
});
+ it("marks where a looping track starts its file over", () => {
+ // A 60s file under a 180s span repeats twice more after the first pass, so
+ // there are two boundaries to show — at a third and two thirds.
+ const { pill } = renderAudio({ loop: true, endMs: 100_000 + 180_000 });
+ expect(pill.querySelectorAll('[data-testid="audio-loop-mark"]')).toHaveLength(2);
+ });
+
+ it("draws no loop marks when the track fits inside its source", () => {
+ const { pill } = renderAudio({ loop: true });
+ expect(pill.querySelectorAll('[data-testid="audio-loop-mark"]')).toHaveLength(0);
+ });
+
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 58660d366..a292614a9 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -355,6 +355,8 @@ const AudioLanePill = memo(function AudioLanePill({
widthPct,
sourceStartSec,
sourceEndSec,
+ spanSec,
+ loopWindowSec,
selected,
onStartDrag,
onSelect,
@@ -370,6 +372,10 @@ const AudioLanePill = memo(function AudioLanePill({
* span, or the live window while an edge is being dragged. */
sourceStartSec: number;
sourceEndSec: number;
+ /** The pill's own length in seconds, and how much source one repeat plays —
+ * together they say where the loop boundaries fall. */
+ spanSec: number;
+ loopWindowSec: number;
selected: boolean;
onStartDrag: (e: ReactPointerEvent, track: AxcutAudioTrack, mode: "move" | "l" | "r") => void;
onSelect: (id: string) => void;
@@ -411,6 +417,19 @@ const AudioLanePill = memo(function AudioLanePill({
// clamps, so scaling by the track gain alone under-read a boosted output.
gain={audioGainScalar(track.gainDb) * outputGain}
/>
+ {/* Where the file starts over, so a looping bed reads as one deliberate
+ repeat rather than a mystery. Only drawn when the pill actually
+ outruns its source — otherwise there is nothing to repeat. */}
+ {track.loop && loopWindowSec > 0
+ ? Array.from({ length: Math.min(200, Math.ceil(spanSec / loopWindowSec) - 1) }, (_, i) => (
+ <span
+ key={`loop-${i + 1}`}
+ data-testid="audio-loop-mark"
+ className={styles.laneLoopMark}
+ style={{ left: `${(((i + 1) * loopWindowSec) / spanSec) * 100}%` }}
+ />
+ ))
+ : null}
<span className={styles.laneAudioLabel}>
<Music size={11} />
{label}
@@ -1776,6 +1795,8 @@ export function V4Timeline({
assetDurationSec={duration}
leftPct={pctOf(start)}
widthPct={pctOf(widthSec)}
+ spanSec={widthSec}
+ loopWindowSec={Math.max(0, (duration || 0) - trimStart)}
sourceStartSec={trimStart}
// A looping pill can outrun its file; the waveform draws
// the source it actually has.
diff --git a/src/lib/ai-edition/store/documentWriteAudit.test.ts b/src/lib/ai-edition/store/documentWriteAudit.test.ts
index cb4dc775d..6af03cd04 100644
--- a/src/lib/ai-edition/store/documentWriteAudit.test.ts
+++ b/src/lib/ai-edition/store/documentWriteAudit.test.ts
@@ -246,6 +246,12 @@ const DECLARED: WritePath[] = [
// setAudioTrackGain routes through), or delete it — one undo step apiece.
w("src/lib/ai-edition/store/useTimeline.ts", "placeAudioTrack", "save", "gesture"),
w("src/lib/ai-edition/store/useTimeline.ts", "removeAudioTrack", "save", "gesture"),
+ // Three exits, one gesture: the toggle writes the flag alone when there is
+ // nothing to fill, and the flag plus the filled span when there is. Either
+ // way it is one undo step (see setAudioTrackLoop).
+ w("src/lib/ai-edition/store/useTimeline.ts", "setAudioTrackLoop", "save", "gesture"),
+ w("src/lib/ai-edition/store/useTimeline.ts", "setAudioTrackLoop", "save", "gesture"),
+ w("src/lib/ai-edition/store/useTimeline.ts", "setAudioTrackLoop", "save", "gesture"),
w("src/lib/ai-edition/store/useTimeline.ts", "updateAudioTrack", "save", "gesture"),
// The round-2 defect: a background duration probe every freshly imported asset
// fires, because `addAsset` never populates `durationSec`.
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index e8b6a0976..7b77e9c46 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -1408,6 +1408,52 @@ describe("useTimeline audio tracks", () => {
expect(track).toMatchObject({ startMs: 9000, endMs: 10_000 });
});
+ it("turning loop on fills the rest of the programme, in one undo step", async () => {
+ // Looping only means anything when the span exceeds the source, so a toggle
+ // that changed nothing else did nothing at all. The sample timeline is one
+ // 0..10s clip and the asset is 30s, so the track is created 2..10 (clamped
+ // to the content) and filling is a no-op — place it short first.
+ const { result } = renderTimeline();
+ let id = "";
+ await act(async () => {
+ id = (await result.current.addAudioTrack("audio_1", 2)) ?? "";
+ });
+ await act(async () => {
+ await result.current.placeAudioTrack(id, { startMs: 2000, endMs: 4000 });
+ });
+ await act(async () => {
+ await result.current.setAudioTrackLoop(id, true);
+ });
+ const tracks = useProjectStore.getState().document?.audioTracks ?? [];
+ expect(tracks[0]).toMatchObject({ startMs: 2000, endMs: 10_000, loop: true });
+
+ // One step: the flag and the fill undo together.
+ act(() => {
+ expect(undo()).toBe(true);
+ });
+ const back = useProjectStore.getState().document?.audioTracks[0];
+ expect(back).toMatchObject({ endMs: 4000, loop: false });
+ });
+
+ it("turning loop off leaves the span alone", async () => {
+ // Shrinking back would throw away a length the user may have set by hand.
+ const { result } = renderTimeline();
+ let id = "";
+ await act(async () => {
+ id = (await result.current.addAudioTrack("audio_1", 2)) ?? "";
+ });
+ await act(async () => {
+ await result.current.setAudioTrackLoop(id, true);
+ });
+ const filled = useProjectStore.getState().document?.audioTracks[0]?.endMs;
+ await act(async () => {
+ await result.current.setAudioTrackLoop(id, false);
+ });
+ const track = useProjectStore.getState().document?.audioTracks[0];
+ expect(track?.loop).toBe(false);
+ expect(track?.endMs).toBe(filled);
+ });
+
it("removeAudioTrack deletes the track", async () => {
const { result } = renderTimeline();
let id = "";
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index e1c64e4c9..3119869d6 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -1393,6 +1393,45 @@ export function useTimeline() {
[saveDocument],
);
+ // Turning loop ON fills the rest of the programme with the track.
+ //
+ // Looping only means anything when the span EXCEEDS the source, so a toggle
+ // that changed nothing else did nothing at all — the user had to know to then
+ // drag the pill's right edge out, which is not a thing anyone guesses. Filling
+ // is what "loop" is for, it is one undo away, and the edge still trims it back
+ // to any length. Turning loop OFF deliberately leaves the span alone: shrinking
+ // it would throw away a length the user may have set by hand.
+ const setAudioTrackLoop = useCallback(
+ async (trackId: string, loop: boolean) => {
+ const doc = useProjectStore.getState().document;
+ if (!doc) return;
+ const fragments = doc.audioTracks.filter((t) => trackGroupId(t) === trackId);
+ const [pill] = collapseTracksToPills(fragments);
+ if (!pill) return;
+ const programmeEndMs = Math.round(
+ doc.timeline.clips.reduce((max, c) => Math.max(max, c.timelineEndSec), 0) * 1000,
+ );
+ // One write, so the fill and the flag are a single undo step.
+ const patched = patchAudioTrack(doc, trackId, { loop });
+ if (!loop || programmeEndMs <= pill.endMs) {
+ await saveDocument(patched, { history: true });
+ return;
+ }
+ const others = patched.audioTracks.filter((t) => trackGroupId(t) !== trackId);
+ const filled = anchorAudioTrackFragments(
+ { ...pill, loop, endMs: programmeEndMs },
+ doc.timeline.clips,
+ () => createId("audio"),
+ );
+ if (filled.length === 0) {
+ await saveDocument(patched, { history: true });
+ return;
+ }
+ await saveDocument({ ...doc, audioTracks: [...others, ...filled] }, { history: true });
+ },
+ [saveDocument],
+ );
+
const setAudioTrackGain = useCallback(
async (trackId: string, gainDb: number) => {
await updateAudioTrack(trackId, { gainDb });
@@ -1425,6 +1464,7 @@ export function useTimeline() {
addAudio,
removeAudioTrack,
updateAudioTrack,
+ setAudioTrackLoop,
placeAudioTrack,
setAudioTrackGain,
selectedAudioTrackId,