8000 fix(transport): L shortcut clamps loop region into trim by johannboehme · Pull Request #20 · 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
53 changes: 50 additions & 3 deletions frontend/src/editor/components/EditorShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ import { useEditorStore } from "../store";
const SIDE_PANEL_COLLAPSE_KEY = "editor.sidepanel.collapsed";
const EXPANDED_W = 380;

/** Tailwind `lg` breakpoint (= 1024px). The desktop and mobile layout
* branches BOTH receive the same React children (`videoArea`,
* `transport`, `timeline`, ...). Rendering them under a CSS-only
* `hidden lg:grid` / `lg:hidden` swap mounts each child TWICE in the
* React tree — burning a duplicate `<MasterAudio>` (= a second WebAudio
* ping-pong + AudioContext) and a duplicate `<TransportBar>` (=
* duplicate keydown listener → arrows step 2× the snap). We use
* this hook to render exactly ONE branch instead. */
const LG_MQ = "(min-width: 1024px)";
function useIsLargeViewport(): boolean {
const [large, setLarge] = useState(() => {
if (typeof window === "undefined") return true;
return window.matchMedia(LG_MQ).matches;
});
useEffect(() => {
if (typeof window === "undefined") return;
const mql = window.matchMedia(LG_MQ);
const MediaQueryListEvent) => setLarge(e.matches);
if (mql.addEventListener) {
mql.addEventListener("change", onChange);
return () => mql.removeEventListener("change", onChange);
}
const legacy = mql as unknown as {
addListener?: (cb: (ev: MediaQueryListEvent) => void) => void;
removeListener?: (cb: (ev: MediaQueryListEvent) => void) => void;
};
legacy.addListener?.(onChange);
return () => legacy.removeListener?.(onChange);
}, []);
return large;
}

function readCollapsed(): boolean {
if (typeof localStorage === "undefined") return false;
return localStorage.getItem(SIDE_PANEL_COLLAPSE_KEY) === "1";
Expand Down Expand Up @@ -43,6 +75,7 @@ export function EditorShell({
const [sheetOpen, setSheetOpen] = useState(false);
const [sideCollapsed, setSideCollapsed] = useState(readCollapsed);
const fxPanelOpen = useEditorStore((s) => s.ui.fxPanelOpen);
const isLarge = useIsLargeViewport();
// When the FX compartment is pulled out, the timeline's top corners
// go flat — the compartment merges with the timeline as a single
// "drawer pulled out of the timeline" silhouette.
Expand Down Expand Up @@ -77,9 +110,20 @@ export function EditorShell({
* expand-handle floats over the right edge as a small grab tab.
* No overflow-hidden here — the handle uses negative `right`
* to escape the px-3 padding and sit flush against the screen
* edge when collapsed. */}
* edge when collapsed.
*
* CRITICAL: rendered ONLY on lg+ via JS (not CSS-toggled).
* Mounting both branches via `hidden lg:grid` + `lg:hidden`
* duplicates `transport` / `videoArea` / `timeline` in the React
* tree — fine for static markup, fatal for nodes with effects.
* In particular it spawns TWO `<MasterAudio>` instances (= two
* parallel WebAudio ping-pong setups summed into the same
* destination, audible as constant phasing/comb-filtering across
* the whole loop) and TWO `<TransportBar>` keydown listeners
* (arrow-key snap navigation steps 2× the snap distance). */}
{isLarge && (
<div
className="relative flex-1 hidden lg:grid gap-3 px-3 pb-3 min-h-0 transition-[grid-template-columns] duration-200 ease-out"
className="relative flex-1 grid gap-3 px-3 pb-3 min-h-0 transition-[grid-template-columns] duration-200 ease-out"
style={{
gridTemplateColumns: sideCollapsed
? `1fr 0px`
Expand Down Expand Up @@ -126,6 +170,7 @@ export function EditorShell({
=> setSideCollapsed((c) => !c)}
/>
</div>
)}

{/* Tablet / mobile layout — scrollable column. Phones can't fit
* video + transport + FX + timeline + actions on one screen, so
Expand All @@ -140,7 +185,8 @@ export function EditorShell({
* FX pull-tab visible separation from the snap-button strip
* at the top of the timeline panel — without this the panels
* felt fused together. */}
<div className="flex-1 lg:hidden flex flex-col gap-2 px-2 pb-2 overflow-y-auto overflow-x-hidden min-h-0">
{!isLarge && (
<div className="flex-1 flex flex-col gap-2 px-2 pb-2 overflow-y-auto overflow-x-hidden min-h-0">
<div
className="relative aspect-video shrink-0 bg-sunken rounded-lg border border-rule shadow-panel overflow-hidden"
style={{ maxHeight: "40vh" }}
Expand All @@ -161,6 +207,7 @@ export function EditorShell({
<div className="h-full p-3">{sidePanel}</div>
</BottomSheet>
</div>
)}
</div>
);
}
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/editor/components/TransportBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,16 @@ export function TransportBar() {
setLoop(null);
return;
}
// Anchor inside trim — if the playhead sits before/after the trim
// region (e.g. fresh editor open with currentTime = 0 while the
// audio starts at trim.in = 6.4 s) we'd otherwise propose a region
// outside trim, which `setLoop` clamps to null and the L shortcut
// appears to do nothing. Clamping here keeps the action a real
// toggle from any playhead position.
const t = useEditorStore.getState().playback.currentTime;
setLoop({ start: t, end: Math.min(duration, t + 2) });
const start = Math.max(trim.in, Math.min(trim.out - 1 / fps, t));
const end = Math.min(trim.out, start + 2);
setLoop({ start, end });
}

// Keyboard shortcuts (skip when an input/textarea is focused)
Expand Down
34 changes: 34 additions & 0 deletions frontend/src/editor/store.test.ts
EBA0
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,40 @@ describe("useEditorStore", () => {
expect(useEditorStore.getState().playback.currentTime).toBeCloseTo(4.0, 6);
});

test("grid 1: t close-but-not-on snap (1.985s) advances to NEXT snap (2.0), not 4.0", () => {
// Regression for the eps fallback that used to fire whenever t was
// within step*0.01 of a snap, falsely doubling the step. The
// fallback now uses an FP-only tolerance so a real 15ms gap from
// the snap is treated as off-grid (target = nearest forward snap).
useEditorStore.getState().loadJob({
...baseJobMeta,
bpm: { value: 120, confidence: 1, phase: 0, manualOverride: false },
beatsPerBar: 4,
});
useEditorStore.getState().setSnapMode("1");
useEditorStore.getState().seek(1.985);
useEditorStore.getState().clearSeekRequest();
useEditorStore.getState().stepByActiveSnap(1);
expect(useEditorStore.getState().playback.currentTime).toBeCloseTo(2.0, 6);
});

test("grid 1: backward from on-grid t=2.0 lands on 0.0 (eps fallback still active for FP)", () => {
// Guards against shrinking eps to zero — the backward direction
// still needs the fallback because Math.round rounds half-toward-+∞
// and snapTime(probe=−step/2) returns the SAME tick (not the
// previous one). Without the fallback, ←-from-snap would no-op.
useEditorStore.getState().loadJob({
...baseJobMeta,
bpm: { value: 120, confidence: 1, phase: 0, manualOverride: false },
beatsPerBar: 4,
});
useEditorStore.getState().setSnapMode("1");
useEditorStore.getState().seek(2.0);
useEditorStore.getState().clearSeekRequest();
useEditorStore.getState().stepByActiveSnap(-1);
expect(useEditorStore.getState().playback.currentTime).toBeCloseTo(0.0, 6);
});

test("MATCH mode with selected clip: jumps to next/prev candidate position", () => {
useEditorStore.getState().loadJob(baseJobMeta, {
clips: [
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/editor/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,12 @@ export const useEditorStore = create<EditorState>()(
beatsPerBar,
barOffsetBeats: effectiveBarOffsetBeats(s.jobMeta),
});
const eps = step * 0.01;
// FP tolerance only — used to absorb snapTime's
// round-half-toward-+∞ asymmetry on negative half-tick probes.
// NOT a "near a snap" widener: a larger eps would falsely double
// the step when the user's playhead happens to land within ±N ms
// of a snap (e.g. via audio-mirror seek precision).
const eps = step * 1e-9;
let target = candidate;
if (Math.abs(target - t) < eps) target = candidate + direction * step;
s.seek(target);
Expand Down
Loading
0