diff --git a/src/lib/player/store.test.ts b/src/lib/player/store.test.ts index 824437c..0a6932f 100644 --- a/src/lib/player/store.test.ts +++ b/src/lib/player/store.test.ts @@ -356,6 +356,25 @@ it("a loop wrapping is a jump, and says so", async () => { expect(store.lastSeekAt()).toBeGreaterThan(stamped); }); +it("choosing a loop *ahead* of the playhead is a jump too, and says so", async () => { + // The structural rule cannot see this one. It reads a step backwards as the tell, because playback never + // runs backwards — but a forward step is exactly what playback looks like at the epsilon that rule uses, + // so the direction has to be stated by the gesture instead. The gesture that owed it is auditioning a + // candidate that lies ahead of the caret: the audio starts at A, and the row, never told, glides the whole + // distance to A at the length of its transition — a 250 ms slide nobody asked to watch. + const store = makeStore(); + store.playSongs(tracks, 0); + await vi.waitFor(() => expect(store.peekState()).toBe("playing")); + ctx.advance(2); + await vi.waitFor(() => expect(store.peekPosition()).toBeCloseTo(2 - 0.08, 1)); + const before = store.lastSeekAt(); + + expect(store.setLoop({ start: 8, end: 10 })).toBe(true); + + await vi.waitFor(() => expect(store.peekPosition()).toBeCloseTo(8, 0)); + expect(store.lastSeekAt()).toBeGreaterThan(before); +}); + it("position comes from the audio clock", async () => { const store = makeStore(); store.playSongs(tracks, 0); diff --git a/src/lib/player/store.ts b/src/lib/player/store.ts index d92f3f4..b87b12a 100644 --- a/src/lib/player/store.ts +++ b/src/lib/player/store.ts @@ -859,19 +859,31 @@ export function createPlayerStore(overrides: PlayerStoreOverrides = {}): PlayerS * region, and the ticker simply *discovering* a wrap it was not told about — which is the case a per-site * stamp would miss, because the write that shows it is the sample, not the wrap. * - * What it deliberately does not cover is a jump *forward* that is not a seek: those are rare, and the ones - * that exist go through `seek`, which stamps its own cause. Together the two make every discontinuity the - * row can see a stated one, which matters because the row's smoothing is a linear transition justified by - * the clock advancing linearly — see `WaveformRow`'s `--pb-smooth`. + * What it deliberately does not cover is a jump *forward*: the epsilon below is float noise, and a ticker + * sample is a legitimate forward step, so a forward rule could only ever fire on playback. The forward + * jumps that exist therefore state themselves instead — `seek` stamps its own cause, and so does + * `rearmLive` when it is handed a destination. Together the two make every discontinuity the row can see a + * stated one, which matters because the row's smoothing is a linear transition justified by the clock + * advancing linearly — see `WaveformRow`'s `--pb-smooth`. * * The epsilon is float and jitter noise, not a judgement about how big a jump counts: the transition being * skipped is 250 ms long and the two positions it would have glided between are within a frame of each * other, so a false positive here is invisible and a false positive on a real wrap is the bug. */ const CLOCK_BACKWARDS_S = 0.01; + /** + * Say that the clock's next change was placement rather than playback. + * + * Separate from the rule in `writeClock` because the gestures that need it are known somewhere other than + * the write they end up causing: a region press reaches the clock through `rearmLive` and the successor it + * arms, not through a single `writeClock` the press can stamp on the way. + */ + function markDiscontinuity(): void { + lastSeekAt.set(globalThis.performance?.now() ?? Date.now()); + } function writeClock(next: number): void { if (next < position.peek() - CLOCK_BACKWARDS_S) { - lastSeekAt.set(globalThis.performance?.now() ?? Date.now()); + markDiscontinuity(); } // The field's own setter, not `writeClock`: the rule above reads the previous position, and calling // through the rule to apply the rule is a stack overflow wearing an invariant. @@ -3151,6 +3163,13 @@ export function createPlayerStore(overrides: PlayerStoreOverrides = {}): PlayerS * `from` it is a jump, and a jump is heard wherever it is scheduled. */ function rearmLive(from?: number): void { + // A named destination is a jump, and a jump is a discontinuity whichever way it points. The backwards + // rule in `writeClock` cannot see a forward one, so the direction has to be stated by the gesture the way + // `seek` states its own. Stamped at the decision rather than at the writes it causes, because both of + // them — the paused `writeClock` below and the successor `beginPlayback` arms — belong to one press. + // Without it, choosing a detected candidate *ahead* of the playhead starts the audio at A while the + // caret spends the transition's length walking there: the sound of a choice nobody made. + if (from !== undefined) markDiscontinuity(); if (state.peek() !== "playing" || !live) { // Paused, there is no source to restart — but the playhead still has to be where the // next play begins, or the bar shows one place and the audio starts from another, and @@ -4350,7 +4369,7 @@ export function createPlayerStore(overrides: PlayerStoreOverrides = {}): PlayerS // Stamped here, at the one place the clock is moved by an act of will, and before anything else the // seek does: the row reads this when the position it is drawing changes, and the two have to agree on // which change was the jump. - lastSeekAt.set(globalThis.performance?.now() ?? Date.now()); + markDiscontinuity(); const cached = decoded.get(id); const buffer = cached?.buffer; const to = Math.min(target, buffer ? buffer.duration : target);