Pianory 如何用精確拍數分數表示弱起與結尾小節
Exact Beat Fractions for Pickup and Closing Bars
Pianory 的課程時間軸以有理數四分音符表示拍位與時值,並要求分數約至最簡形式,以便統一表示等值拍數。比較分數時使用 BigInt 交叉相乘,避免大數超出 JavaScript 安全整數範圍後因捨入造成錯誤排序。驗證器只接受與弱起小節在相同拍號下恰好互補的結尾不完整小節;指定測試通過 8 項。
I build Pianory, a browser-based piano learning app. AI helped draft this article; I checked the claims against src/lib/domain/course-timeline.ts and its focused test suite.
A score import needs a stable representation of musical time. In Pianory's course timeline domain contract, beat positions and durations are rational quarter-note values, rather than floating-point estimates. The fraction helper requires a reduced numerator and denominator, so equivalent values have one canonical form.
Comparison uses BigInt cross-products. That matters when numerator times denominator exceeds JavaScript's safe-integer range: rounding could make two distinct beat positions appear equal or reverse their order. The focused tests include such a large-integer comparison and reject noncanonical fractions.
Pickup measures create a second edge case. An opening partial measure does not mean every later bar may be partial. The validator accepts a final partial bar when it exactly complements the opening pickup in the same meter. It rejects duration combinations that do not satisfy that relationship. That is a domain rule for a score timeline, not a claim that every imported MIDI file has perfect notation.
The design lesson is to make the time unit and equality rule explicit before wiring import or playback adapters. A versioned JSON score contract with canonical fractions gives tests a precise target. pnpm exec vitest run src/lib/domain/course-timeline.spec.ts passed 8 tests on 29 October 2026. This focused check verifies the domain model and tests only; I did not trace every production import or playback path for this article.
Relevant source: src/lib/domain/course-timeline.ts and src/lib/domain/course-timeline.spec.ts.
來源:DEV Community · dev.to