8000 feat(transport): OP-1 style loop controls + snap-aware nav by johannboehme · Pull Request #13 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions frontend/src/editor/OffsetScheduler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,43 @@ describe("shouldRescheduleOnTick", () => {
}),
).toBe(true);
});

// After an OP-1 style loop-shift, the playhead can sit *outside* the new
// loop region but should keep playing until it reaches the OLD loop end —
// that's the "deferred wrap" point we keep separately. The scheduler must
// honour it: don't snap to the new loop region until the playhead has
// crossed the deferred wrap point.
describe("with pendingWrapAt (OP-1 deferred loop-shift)", () => {
test("does not trigger when videoTime is before pendingWrapAt — even if videoTime < loop.start", () => {
// loop just got shifted right [0,2] → [2,4]. Playhead still at 1,
// pendingWrapAt = old loop.end = 2. Must NOT reschedule yet.
expect(
shouldRescheduleOnTick({
videoTime: 1.0,
loop: { start: 2, end: 4 },
pendingWrapAt: 2,
}),
).toBe(false);
});

test("triggers exactly when videoTime crosses pendingWrapAt", () => {
expect(
shouldRescheduleOnTick({
videoTime: 2.01,
loop: { start: 2, end: 4 },
pendingWrapAt: 2,
}),
).toBe(true);
});

test("pendingWrapAt = null behaves like no override", () => {
expect(
shouldRescheduleOnTick({
videoTime: 2.05,
loop: { start: 1, end: 2 },
pendingWrapAt: null,
}),
).toBe(true);
});
});
});
9 changes: 9 additions & 0 deletions frontend/src/editor/OffsetScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,23 @@ export function clampLoopRegion(
* Should we re-seek + re-schedule the audio source? Triggers on:
* - playhead crossed past loop.end → wrap back to start
* - playhead jumped before loop.start (user scrubbed) → restart loop
*
* `pendingWrapAt` overrides the default trigger: while it's set (non-null)
* the only condition that matters is `videoTime >= pendingWrapAt`. Used by
* the OP-1 style loop-shift, where the loop region jumps ahead but the
* playhead must keep playing in the now-out-of-loop zone until it reaches
* the *old* loop end.
*/
export function shouldRescheduleOnTick({
videoTime,
loop,
pendingWrapAt,
}: {
videoTime: number;
loop: LoopRegion | null;
pendingWrapAt?: number | null;
}): boolean {
if (!loop) return false;
if (pendingWrapAt != null) return videoTime >= pendingWrapAt;
return videoTime >= loop.end || videoTime < loop.start;
}
105 changes: 63 additions & 42 deletions frontend/src/editor/components/TransportBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function TransportBar() {
const loop = useEditorStore((s) => s.playback.loop);
const setLoop = useEditorStore((s) => s.setLoop);
const seek = useEditorStore((s) => s.seek);
const stepByActiveSnap = useEditorStore((s) => s.stepByActiveSnap);
const shiftLoop = useEditorStore((s) => s.shiftLoop);
// Phone-sized viewports get an aggressively compacted transport bar:
// every transport stepper + Play + IN/OUT/LOOP collapses to xs
// (h-8 ≈ 28 px wide icon-only, no min-w) so the entire 8-button
Expand All @@ -54,6 +56,29 @@ export function TransportBar() {
seek(currentTime + deltaSec);
}

// IN/OUT semantics are modal: while a loop region is active they edit
// the loop boundaries (so the user can sculpt a practice loop with the
// playhead); otherwise they edit the trim/export region. Same logic in
// both the buttons and the I/O shortcuts.
function setInPointAtPlayhead() {
if (loop) {
const newStart = currentTime;
const newEnd = Math.max(loop.end, newStart + 1 / fps);
setLoop({ start: newStart, end: newEnd });
} else {
setTrim({ in: currentTime, out: trim.out });
}
}
function setOutPointAtPlayhead() {
if (loop) {
const newEnd = currentTime;
const newStart = Math.min(loop.start, newEnd - 1 / fps);
setLoop({ start: newStart, end: newEnd });
} else {
setTrim({ in: trim.in, out: currentTime });
}
}

// Keyboard shortcuts (skip when an input/textarea is focused)
useEffect(() => {
function onKey(e: KeyboardEvent) {
Expand All @@ -70,84 +95,80 @@ export function TransportBar() {
e.preventDefault();
setPlaying(!isPlaying);
break;
case "k":
case "K":
e.preventDefault();
setPlaying(!isPlaying);
break;
case "j":
case "J":
e.preventDefault();
step(-1);
break;
case "l":
case "L":
e.preventDefault();
step(1);
break;
case "ArrowLeft":
e.preventDefault();
step(e.shiftKey ? -1 : -1 / fps);
if (e.altKey) shiftLoop(-1);
else stepByActiveSnap(-1);
break;
case "ArrowRight":
e.preventDefault();
step(e.shiftKey ? 1 : 1 / fps);
if (e.altKey) shiftLoop(1);
else stepByActiveSnap(1);
break;
case "i":
case "I":
e.preventDefault();
setTrim({ in: currentTime, out: trim.out });
setInPointAtPlayhead();
break;
case "o":
case "O":
e.preventDefault();
setTrim({ in: trim.in, out: currentTime });
setOutPointAtPlayhead();
break;
}
}
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [currentTime, fps, isPlaying, setPlaying, setTrim, trim.in, trim.out, step]);
}, [
currentTime,
fps,
isPlaying,
loop,
setPlaying,
setLoop,
setTrim,
trim.in,
trim.out,
stepByActiveSnap,
shiftLoop,
]);

useRegisterShortcut({
id: "transport.playpause",
keys: ["Space", "K"],
keys: ["Space"],
description: "Play / pause",
group: "Transport",
icon: <PlayIcon />,
});
useRegisterShortcut({
id: "transport.skipback",
keys: ["J"],
description: "Skip back 1 second",
group: "Transport",
icon: <SkipBackIcon />,
});
useRegisterShortcut({
id: "transport.skipfwd",
keys: ["L"],
description: "Skip forward 1 second",
group: "Transport",
icon: <SkipFwdIcon />,
});
useRegisterShortcut({
id: "transport.framestep",
keys: ["←", "→"],
description: "Step one frame (hold Shift for ±1 second)",
description: "Step by snap target (frame, beat, bar, or match-point)",
group: "Transport",
icon: <ArrowKeysIcon />,
});
useRegisterShortcut({
id: "transport.loopshift",
keys: ["⌥←", "⌥→"],
description: "Shift loop region by its length (OP-1 style; playback continues)",
group: "Transport",
icon: <LoopIcon />,
});
useRegisterShortcut({
id: "transport.in",
keys: ["I"],
description: "Set IN-point at the playhead",
description: loop
? "Set loop in-point at the playhead"
: "Set trim in-point at the playhead",
group: "Transport",
icon: <InIcon />,
});
useRegisterShortcut({
id: "transport.out",
keys: ["O"],
description: "Set OUT-point at the playhead",
description: loop
? "Set loop out-point at the playhead"
: "Set trim out-point at the playhead",
group: "Transport",
icon: <OutIcon />,
});
Expand Down Expand Up @@ -232,18 +253,18 @@ export function TransportBar() {
<ChunkyButton
variant="secondary"
size={trimSize}
class="x x-first x-last">() => setTrim({ in: currentTime, out: trim.out })}
class="x x-first x-last">setInPointAtPlayhead}
iconLeft={<InIcon />}
aria-label="Set in-point at the playhead"
aria-label={loop ? "Set loop in-point at the playhead" : "Set in-point at the playhead"}
>
{!isNarrow && "IN"}
</ChunkyButton>
<ChunkyButton
variant="secondary"
size={trimSize}
class="x x-first x-last">() => setTrim({ in: trim.in, out: currentTime })}
class="x x-first x-last">setOutPointAtPlayhead}
iconLeft={<OutIcon />}
aria-label="Set out-point at the playhead"
aria-label={loop ? "Set loop out-point at the playhead" : "Set out-point at the playhead"}
>
{!isNarrow && "OUT"}
</ChunkyButton>
Expand Down
28 changes: 28 additions & 0 deletions frontend/src/editor/components/timeline/BeatRuler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function BeatRuler({
effectiveBarOffsetBeats(s.jobMeta),
);
const seek = useEditorStore((s) => s.seek);
const loop = useEditorStore((s) => s.playback.loop);

// Repaint whenever any of the dependencies change.
useEffect(() => {
Expand All @@ -71,6 +72,32 @@ export function BeatRuler({
ctx.fillRect(0, height - 1, contentWidthPx, 1);

const visibleS = viewEndS - viewStartS;

// Loop band on the ruler — same accent as the audio-lane loop tint
// so the user can see the loop region above the bar grid (matters
// most for the OP-1 style Alt-arrow loop-shift, where the playhead
// stays in the old region while the loop visually jumps along the
// bar grid). Drawn before the ticks so they remain readable.
if (loop && visibleS > 0) {
const pxPerSecRaw = contentWidthPx / Math.max(0.001, visibleS);
const xs = (loop.start - viewStartS) * pxPerSecRaw;
const xe = (loop.end - viewStartS) * pxPerSecRaw;
const xsClamped = Math.max(0, xs);
const xeClamped = Math.min(conte 9D3C ntWidthPx, xe);
if (xeClamped > xsClamped) {
ctx.fillStyle = "rgba(255,87,34,0.18)";
ctx.fillRect(xsClamped, 0, xeClamped - xsClamped, height - 1);
ctx.strokeStyle = "rgba(255,87,34,0.6)";
ctx.lineWidth = 1;
ctx.strokeRect(
xsClamped + 0.5,
0.5,
Math.max(0, xeClamped - xsClamped - 1),
height - 2,
);
}
}

if (!bpm || visibleS <= 0) return;

const pxPerSec = contentWidthPx / Math.max(0.001, visibleS);
Expand Down Expand Up @@ -164,6 +191,7 @@ export function BeatRuler({
viewEndS,
contentWidthPx,
height,
loop,
]);

function onClick(e: React.MouseEvent<HTMLCanvasElement>) {
Expand Down
Loading
Loading
0