8000 fix(triage): anchor each sliced piece on its own downbeat (no bar-0 ruler) by johannboehme · Pull Request #54 · 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
25 changes: 25 additions & 0 deletions frontend/src/local/triage/triage-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,31 @@ describe("triage-store · sliceChunkIntoBars", () => {
for (const p of pieces) expect((p.audioStartMs! - 1000) % 2000).toBe(0);
});

it("anchors each piece on its OWN downbeat even when msPerBar is fractional", () => {
// 130 BPM 4/4 → msPerBar 1846.15… (non-integer). Cuts get rounded to
// whole ms, so a cut can land a hair past its exact grid line; a strict
// ceil-based anchor search then overshoots by a full bar, putting the
// piece's bar-1 a bar into the piece (ruler shows bar 0 at the edge).
// Each interior piece must anchor exactly on its own start downbeat.
seed([
makeChunk({
id: "c1",
startMs: 0,
endMs: 20000,
audioStartMs: 0,
effectiveBpm: 130,
beatsPerBar: 4,
}),
]);
useTriageStore.getState().sliceChunkIntoBars("c1", 1);
const pieces = useTriageStore
.getState()
.chunks.sort((a, b) => a.startMs - b.startMs);
expect(pieces.length).toBeGreaterThan(2);
// Every piece's grid anchor sits at its own start — no full-bar overshoot.
for (const p of pieces) expect(p.audioStartMs).toBe(p.startMs);
});

it("is a no-op when the chunk is shorter than one interval", () => {
seed([
makeChunk({
Expand Down
13 changes: 12 additions & 1 deletion frontend/src/local/triage/triage-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,18 @@ export const useTriageStore = create<TriageState>((set, get) => ({
for (let i = 0; i < boundaries.length - 1; i++) {
const pieceStart = boundaries[i];
const pieceEnd = boundaries[i + 1];
const anchor = anchorInRange(originalAnchor, bpm, beatsPerBar, pieceStart);
// Interior pieces start exactly on a cut, which IS a downbeat of the
// original grid by construction — so the piece's own bar-1 anchor is
// its start. Don't round-trip through anchorInRange: cuts are rounded
// to whole ms, and with a fractional msPerBar a cut can land a hair
// past its grid line, making the strict ceil overshoot by a full bar
// (the anchor lands a bar into the piece, so its ruler reads bar 0 at
// the edge instead of 1). The leading partial (i === 0) may start
// mid-bar, so it keeps the original grid anchor.
const anchor =
i === 0
? anchorInRange(originalAnchor, bpm, beatsPerBar, pieceStart)
: pieceStart;
const pieceId =
i === 0 ? chunk.id : `${chunk.id}-s${i}-${Date.now().toString(36)}`;
if (i > 0) newIds.push(pieceId);
Expand Down
Loading
0