Refined traktor timing fix

This commit is contained in:
patben8 committed 2026-10-01 10:54:27 +02:00
1 parent 2c02019bb2
commit 34b18893ef
11 files changed
+489 -20

No files matched your search

+10 -2
View File
@@ -29,12 +29,20 @@ describe('buildBeatGrid', () => {
expect(buildBeatGrid([{ start: Infinity, bpm: 120 }])).toBeNull()
})
it('sorts unsorted input and clamps negative starts', () => {
it('sorts unsorted input and keeps negative starts', () => {
const g = grid([
{ start: 10, bpm: 60 },
{ start: -5, bpm: 120 },
])
expect(g.markers.map((m) => m.start)).toEqual([0, 10])
expect(g.markers.map((m) => m.start)).toEqual([-5, 10])
})
it('a marker before the audio starts keeps its phase', () => {
// A Traktor MP3 grid anchored inside the header frame: 30 ms before the
// decoded audio. Clamping it to 0 would put every beat 30 ms late.
const g = grid([{ start: -0.03, bpm: 120 }])
expect(g.snapToBeat(0.5)).toBeCloseTo(0.47, 9)
expect(g.timeOfBeat(1)).toBeCloseTo(0.47, 9)
})
it('collapses markers at the same instant', () => {
+7 -2
View File
@@ -277,7 +277,8 @@ export class BeatGrid {
*
* Tolerant by design: accepts `undefined` (so a frontend running against an
* older backend degrades to "no grid" rather than throwing), unsorted input,
* duplicate positions, negative starts, and non-finite or out-of-range BPMs.
* duplicate positions and non-finite or out-of-range BPMs. Negative starts are
* kept as they are (see below).
*/
export function buildBeatGrid(markers?: readonly GridMarker[] | null): BeatGrid | null {
if (!markers || markers.length === 0) return null
@@ -286,7 +287,11 @@ export function buildBeatGrid(markers?: readonly GridMarker[] | null): BeatGrid
if (!m || !Number.isFinite(m.bpm) || !Number.isFinite(m.start)) continue
if (m.bpm <= 0) continue
clean.push({
start: Math.max(0, m.start),
// NOT clamped at 0. A marker before the decoded audio's start is a real
// phase — Traktor anchors MP3 grids inside the header frame, ~51 ms
// before the audio Konduktor decodes — and clamping it would move every
// beat of the grid late by that much.
start: m.start,
bpm: Math.min(MAX_BPM, Math.max(MIN_BPM, m.bpm)),
})
}