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

fix(triage): anchor each sliced piece on its own downbeat (no bar-0 ruler) - #54

Merged
johannboehme merged 1 commit into
mainfrom
claude/affectionate-banzai-27993f
Jun 14, 2026
Merged

johannboehme merged 1 commit into
mainfrom
claude/affectionate-banzai-27993f

Conversation

@johannboehme
Copy link
Copy Markdown
Owner

What

When slicing a chunk into N-bar pieces (the SLICE TO BARS action), some pieces' bar rulers started at bar 0 instead of bar 1 when a piece was selected — at fractional BPMs this looked random across pieces.

Why it happened

The bar ruler numbers relative to a chunk's anchor (audioStartMs): bar 1 sits on the anchor, and any beats before the anchor render as bar 0.

In sliceChunkIntoBars two roundings combined:

  1. Cut points are rounded to whole ms: Math.round(originalAnchor + n·step).
  2. Each piece's anchor was then re-derived via anchorInRange, which returns the smallest grid boundary ≥ pieceStart using Math.ceil.

With a non-integer msPerBar (e.g. 130 BPM → 1846.15 ms) the rounded cut can land a hair past its exact grid line. The strict ceil then read it as "just past bar M" and returned bar M+1, placing the anchor a full bar into the piece. The piece's left edge fell before bar 1 → rendered as bar 0. Whether the rounding tipped over varied per piece, hence the mixed 0/1. At 120 BPM (msPerBar 2000, integral) it never triggered, so existing tests missed it.

The fix

Interior pieces start exactly on a cut, which is a downbeat of the original grid by construction — so the piece's bar-1 anchor is simply its start. Set anchor = pieceStart for those instead of round-tripping through anchorInRange. The leading partial (i === 0) may start mid-bar and keeps the original grid anchor.

const anchor =
  i === 0
    ? anchorInRange(originalAnchor, bpm, beatsPerBar, pieceStart)
    : pieceStart;

Testing

  • TDD: added a red test at 130 BPM asserting audioStartMs === startMs for every sliced piece (reproduced the full-bar overshoot: anchor 9231 vs piece start 7385), now green.
  • Full triage-store suite green (72 tests); tsc --noEmit clean.
  • Pre-existing unrelated failures confirmed on the base commit too: detectChunks "generous min-pause" test and the hardware-dependent WebGPU readback perf benchmark — not touched by this change.

🤖 Generated with Claude Code

…uler)

When slicing a chunk into N-bar pieces, some pieces' bar rulers started at
bar 0 instead of bar 1 — at fractional BPMs, seemingly at random.

Cut points are rounded to whole ms, so with a non-integer msPerBar a cut
can land a hair past its exact grid line. anchorInRange then resolved the
piece's anchor with a strict ceil and overshot by a full bar, placing the
grid anchor one bar INTO the piece. Since the bar ruler numbers relative to
the anchor (bar 1 = anchor), the piece's left edge fell before bar 1 and
rendered as bar 0. Whether the rounding tipped over the line varied per
piece, hence the mixed 0/1. At 120 BPM (msPerBar 2000, integral) it never
triggered, so existing tests missed it.

Interior pieces start exactly on a cut, which is a downbeat of the original
grid by construction — so the piece's bar-1 anchor is simply its start. Set
anchor = pieceStart for those instead of round-tripping through
anchorInRange. The leading partial (i === 0) may start mid-bar and keeps the
original grid anchor.

TDD: new red test at 130 BPM asserting audioStartMs === startMs for every
piece; full triage-store suite green, typecheck clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@johannboehme
johannboehme merged commit 6555eef into main Jun 14, 2026
1 check passed
@johannboehme
johannboehme deleted the claude/affectionate-banzai-27993f branch June 14, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0