8000 fix(editor): anchor the bar ruler to the song start, not where master-0 plays by johannboehme · Pull Request #52 · 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
8000
8000
24 changes: 13 additions & 11 deletions frontend/src/editor/components/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
candidateIdxNearestStart,
} from "../match-snap";
import {
effectiveBeatPhaseS,
arrBeatPhaseS,
effectiveBeatsPerBar,
effectiveBarOffsetBeats,
} from "../selectors/timing";
Expand Down Expand Up @@ -241,7 +241,12 @@ export function Timeline({
const snapMode = useEditorStore((s) => s.ui.snapMode);
const lanesLocked = useEditorStore((s) => s.ui.lanesLocked);
const bpm = useEditorStore((s) => s.jobMeta?.bpm?.value ?? null);
const beatPhase = useEditorStore((s) => effectiveBeatPhaseS(s.jobMeta));
// Master beat grid anchored into the arr-time canvas axis (beat 0
// relative to the first played segment) — the same anchor the BeatRuler
// draws, so drag/loop snapping lands on the visible bar lines.
const arrBeatPhase = useEditorStore((s) =>
arrBeatPhaseS(s.jobMeta, s.arrangementSegments),
);
const beatsPerBar = useEditorStore((s) => effectiveBeatsPerBar(s.jobMeta));
const barOffsetBeats = useEditorStore((s) =>
effectiveBarOffsetBeats(s.jobMeta),
Expand Down Expand Up @@ -1101,8 +1106,8 @@ export function Timeline({

// Build the snap context for this drag. `extraCandidates` is set during
// a clip-move so MATCH mode can snap the cam to its alternative offsets.
// `beatPhase` is master-time (per `effectiveBeatPhaseS`); the snap
// routines work in whichever time-domain the caller projects it into.
// Callers pass `arrBeatPhase` (already in the arr-time canvas axis), so
// the snap routines match the BeatRuler's bar lines directly.
function buildSnapCtx(
phaseDomainPhase: number,
extraCandidates?: number[],
Expand All @@ -1127,8 +1132,7 @@ export function Timeline({
function snapped(t: number, e: { shiftKey: boolean }, candPositions?: number[]): number {
if (e.shiftKey || snapMode === "off") return t;
const arrT = masterToView(t);
const arrPhase = masterToArr(beatPhase, arrangementSegments);
const snappedT = snapTime(arrT, snapMode, buildSnapCtx(arrPhase, candPositions));
const snappedT = snapTime(arrT, snapMode, buildSnapCtx(arrBeatPhase, candPositions));
return viewToMaster(snappedT);
}

Expand All @@ -1142,8 +1146,7 @@ export function Timeline({
candPositions?: number[],
): number {
if (e.shiftKey || snapMode === "off") return arrT;
const arrPhase = masterToArr(beatPhase, arrangementSegments);
return snapTime(arrT, snapMode, buildSnapCtx(arrPhase, candPositions));
return snapTime(arrT, snapMode, buildSnapCtx(arrBeatPhase, candPositions));
}

// ─── Multi-touch pinch-zoom + 2-finger pan ─────────────────────────
Expand Down Expand Up @@ -1444,13 +1447,12 @@ export function Timeline({
Math.min(trimOutArr - len, arrAtPointer - drag.offset),
);
// Loop bounds live in arr-time; snap against the master-bar-grid
// projected into arr-time so a long-form arrangement still snaps
// anchored into arr-time so a long-form arrangement still snaps
// to the song's bar lines.
const arrPhase = masterToArr(beatPhase, arrangementSegments);
const newStart =
e.shiftKey || snapMode === "off"
? newStartRaw
: snapTime(newStartRaw, snapMode, buildSnapCtx(arrPhase));
: snapTime(newStartRaw, snapMode, buildSnapCtx(arrBeatPhase));
// OP-1 tape feel: don't yank the playhead while dragging — the
// store defers the wrap to the OLD loop.end. The active element
// keeps playing through that point and wraps to the new loop's
Expand Down
18 changes: 9 additions & 9 deletions frontend/src/editor/components/timeline/BeatRuler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import { useEffect, useRef } from "react";
import { useEditorStore } from "../../store";
import {
effectiveBeatPhaseS,
arrBeatPhaseS,
effectiveBeatsPerBar,
effectiveBarOffsetBeats,
} from "../../selectors/timing";
import { arrToMaster, masterToArr } from "../../arrangement-time";
import { arrToMaster } from "../../arrangement-time";
import { buildRulerTicks } from "./beat-ruler-ticks";

interface BeatRulerProps {
Expand Down Expand Up @@ -43,15 +43,15 @@ export function BeatRuler({
}: BeatRulerProps) {
const canvasRef = useRef<HTMLCanvasElement | null>(null);
const bpm = useEditorStore((s) => s.jobMeta?.bpm?.value ?? null);
// The selectors return master-time. The ruler renders against the
// arr-time canvas (`viewStartS`/`viewEndS` are arr-time), so project
// master-bar phase into arr-time once. With single-take's
// whole-master segment this is Identity; long-form gets one master-
// bar grid running continuously across the song instead of a
// separate phase per chunk.
// The ruler renders against the arr-time canvas (`viewStartS`/`viewEndS`
// are arr-time), so the master beat grid is anchored into arr-time via
// `arrBeatPhaseS` (beat 0 relative to the first played segment). With
// single-take's whole-master `[0, dur]` segment this is Identity;
// long-form gets one master-bar grid running continuously from the
// start of the song instead of a separate phase per chunk.
const arrSegments = useEditorStore((s) => s.arrangementSegments);
const beatPhase = useEditorStore((s) =>
masterToArr(effectiveBeatPhaseS(s.jobMeta), arrSegments),
arrBeatPhaseS(s.jobMeta, arrSegments),
);
const beatsPerBar = useEditorStore((s) => effectiveBeatsPerBar(s.jobMeta));
const barOffsetBeats = useEditorStore((s) =>
Expand Down
45 changes: 45 additions & 0 deletions frontend/src/editor/selectors/timing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
effectiveBeatsPerBar,
effectiveBarOffsetBeats,
effectiveBarPhaseS,
arrBeatPhaseS,
} from "./timing";
import type { JobMeta } from "../store";
import type { Segment } from "../types";

const baseMeta: JobMeta = {
id: "j",
Expand Down Expand Up @@ -160,6 +162,49 @@ describe("effectiveBarOffsetBeats", () => {
});
});

describe("arrBeatPhaseS", () => {
const meta148: JobMeta = {
...baseMeta,
bpm: { value: 148, confidence: 1, phase: 0.3, manualOverride: false },
};

it("returns the raw master phase when there are no segments", () => {
expect(arrBeatPhaseS(meta148, [])).toBeCloseTo(0.3, 6);
});

it("is identity for single-take's whole-master [0, dur] segment", () => {
// seg0.in === 0 → arr-anchor equals the master phase, exactly as the
// pre-projection direct-mode path behaved.
const segs: Segment[] = [{ in: 0, out: 275 }];
expect(arrBeatPhaseS(meta148, segs)).toBeCloseTo(0.3, 6);
});

it("anchors the grid to the first PLAYED segment, not where master-0 lands", () => {
// Long-form regression: the arrangement plays a mid-song chunk first
// (master 200..275) and the chunk covering master-0 last (0..50).
// The OLD `masterToArr(beatPhase)` projection mapped beat-0 (master
// ~0.3 s) into the LAST segment, anchoring bar 1 at arr ~75 s — "way
// back in the song". The correct anchor is relative to seg0.in (200),
// i.e. a large negative arr-time so the grid fills from arr 0 onward.
const segs: Segment[] = [
{ in: 200, out: 275 },
{ in: 50, out: 100 },
{ in: 0, out: 50 },
];
expect(arrBeatPhaseS(meta148, segs)).toBeCloseTo(0.3 - 200, 6);
// It must NOT be a large positive arr-time (the buggy behaviour).
expect(arrBeatPhaseS(meta148, segs)).toBeLessThan(0);
});

it("a first chunk that is the true song start still anchors bar 1 near arr 0", () => {
const segs: Segment[] = [
{ in: 0, out: 60 },
{ in: 120, out: 180 },
];
expect(arrBeatPhaseS(meta148, segs)).toBeCloseTo(0.3, 6);
});
});

describe("effectiveBarPhaseS", () => {
const meta120: JobMeta = {
...baseMeta,
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/editor/selectors/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* arr-time project the result through `masterToArr` at the call site.
*/
import type { JobMeta } from "../store";
import type { Segment } from "../types";

const DEFAULT_BEATS_PER_BAR = 4;

Expand All @@ -25,6 +26,37 @@ export function effectiveBeatPhaseS(
return phase + nudge;
}

/** Arrangement-time anchor of the master beat grid (beat 0 in arr-time).
*
* The editor keeps ONE master bar grid for every job (see
* `effectiveBeatPhaseS`). Consumers that draw the grid or snap to it on
* the arr-time canvas need beat 0 expressed in arr-time. Arr-time 0 is
* the start of the first PLAYED segment, where `arr = master - seg0.in`,
* so the anchor is simply `beatPhaseMaster - seg0.in`. That keeps the
* grid continuous from the start of the song the user assembled,
* whatever order the chunks play in and wherever the first one sits in
* the source recording.
*
* Do NOT project beat 0 through `masterToArr`: beat 0 is a single master
* instant (~0.3 s into the source). `masterToArr` returns the arr-time
* at which that instant is *played*, which is wherever the chunk
* covering master ~0 lands in the playback order. For a long-form
* arrangement whose first chunk starts late in the source (or whose
* master-0 chunk plays last), that anchors bar 1 far down the timeline —
* the "bar ruler starts way back in the song" bug.
*
* Empty `segments` → the raw master phase unchanged (single-take supplies
* a whole-master `[0, dur]` segment, so this only hits the pre-load case).
*/
export function arrBeatPhaseS(
meta: JobMeta | null | undefined,
segments: readonly Segment[],
): number {
const phase = effectiveBeatPhaseS(meta);
if (segments.length === 0) return phase;
return phase - segments[0].in;
}

/** Master-time of the audio-onset (where audible material begins). */
export function effectiveAudioStartS(
meta: JobMeta | null | undefined,
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/editor/store.ts
987B
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
effectiveBeatPhaseS,
effectiveBeatsPerBar,
effectiveBarOffsetBeats,
arrBeatPhaseS,
} from "./selectors/timing";
import type { Cut } from "../storage/jobs-db";
import type { FxKind, PunchFx } from "./fx/types";
Expand Down Expand Up @@ -2325,14 +2326,11 @@ export const useEditorStore = create<EditorState>()(
const s = get();
const mode = s.ui.snapMode;
if (mode === "off" || mode === "match") return t;
// beatPhase is the master-time of beat 0. For the snap result to
// land on the bar marker the BeatRuler renders, the anchor must
// be projected into the same axis as `t`. `masterToArr` is the
// bijection the BeatRuler itself uses.
const beatPhase = masterToArr(
effectiveBeatPhaseS(s.jobMeta),
s.arrangementSegments,
);
// For the snap result to land on the bar marker the BeatRuler
// renders, the beat-grid anchor must be in the same arr-time axis
// as `t`. `arrBeatPhaseS` is the exact anchor the BeatRuler uses
// (beat 0 relative to the first played segment).
const beatPhase = arrBeatPhaseS(s.jobMeta, s.arrangementSegments);
return snapTime(t, mode, {
bpm: s.jobMeta?.bpm?.value ?? null,
beatPhase,
Expand Down
Loading
0