Beat jumping can now be done outside of the song range

This commit is contained in:
patben8 committed 2026-11-03 11:59:50 +01:00
1 parent 2a40106ea5
commit 5d1bfcf7f1
9 files changed
+126 -10

No files matched your search

+57
View File
@@ -128,6 +128,57 @@ describe('DeckCore transport', () => {
})
})
describe('DeckCore lead-in (performance mode)', () => {
it('a seek before the track plays silence, then the track exactly on time', () => {
const core = new DeckCore(SR, null)
const src = ramp(SR)
core.load(src)
core.setLeadIn(true)
const lead = 2 * QUANTUM + 37
core.seek(-lead / SR)
expect(core.positionSec).toBeCloseTo(-lead / SR, 9)
core.play()
const out = run(core, 6)
for (let i = 0; i < lead - 2; i++) expect(out[i]).toBeCloseTo(0, 6)
for (let i = lead + 2; i < out.length; i++) expect(out[i]).toBeCloseTo(src.stems[0][0][i - lead], 6)
})
it('without lead-in a seek stops at the start; turning it off brings the playhead back', () => {
const core = new DeckCore(SR, null)
core.load(ramp(SR))
core.seek(-0.5)
expect(core.positionSec).toBe(0)
core.setLeadIn(true)
core.seek(-0.5)
expect(core.positionSec).toBeCloseTo(-0.5, 9)
core.setLeadIn(false)
expect(core.positionSec).toBe(0)
})
it('a loop past the end loops silence and keeps playing; released, the deck stops', () => {
const core = new DeckCore(SR, null)
const len = QUANTUM * 4
core.load(ramp(len))
core.setLeadIn(true)
core.setLoop((len - 100) / SR, (len + 200) / SR, true)
core.seek((len - 100) / SR)
core.play()
const events: string[] = []
for (let b = 0; b < 20; b++) {
run(core, 1)
events.push(...core.events)
}
expect(events).toEqual([])
expect(core.playing).toBe(true)
core.setLoop((len - 100) / SR, (len + 200) / SR, false)
for (let b = 0; b < 5; b++) {
run(core, 1)
events.push(...core.events)
}
expect(events).toEqual(['ended'])
})
})
describe('DeckCore scratch', () => {
it('follows the drag, then coasts back to playing speed and plays on by itself', () => {
const core = new DeckCore(SR, null)
@@ -290,4 +341,10 @@ describe('extrapolate', () => {
const m = { pos: 9, time: 0, speed: 1, playing: true, scratching: false }
expect(extrapolate(m, 2.5, { start: 8, end: 10, on: true }, 100)).toBeCloseTo(9.5, 9)
})
it('runs on before the track and past its end inside a loop that reaches there', () => {
const lead = { pos: -1, time: 0, speed: 1, playing: true, scratching: false }
expect(extrapolate(lead, 0.4, { start: 0, end: 0, on: false }, 100)).toBeCloseTo(-0.6, 9)
const out = { pos: 99.5, time: 0, speed: 1, playing: true, scratching: false }
expect(extrapolate(out, 1, { start: 99, end: 101, on: true }, 100)).toBeCloseTo(100.5, 9)
})
})
+25 -1
View File
@@ -53,6 +53,8 @@ export const WARM_QUANTA = 10
const WARM_BUDGET = 384
/** Stem mute/solo ramp (s). */
const GAIN_TAU = 0.004
/** The furthest a lead-in reaches before the track (s). */
const LEAD_IN_SEC = 32
export class DeckCore {
private stems: Float32Array[][] = []
@@ -64,6 +66,9 @@ export class DeckCore {
private pos = 0
private rate = 1
private keylock = false
/** Performance mode: the playhead may sit BEFORE the track (silence until
* it starts, on the beat) — a beat jump or loop move past the start. */
private leadIn = false
private loopOn = false
private loopStart = 0
@@ -174,10 +179,29 @@ export class DeckCore {
}
seek(sec: number): void {
this.pos = Math.max(0, Math.min(sec * this.srcRate, this.len))
this.pos = Math.max(this.minPos, Math.min(sec * this.srcRate, this.maxPos))
this.needRewarm = true
}
setLeadIn(on: boolean): void {
this.leadIn = on
if (!on && this.pos < 0) {
this.pos = 0
this.needRewarm = true
}
}
/** How far before the track the playhead may go (with lead-in), in source
* samples: far beyond any beat jump (32 beats at 60 BPM). */
private get minPos(): number {
return this.leadIn ? -LEAD_IN_SEC * this.srcRate : 0
}
/** The end, or an engaged loop's end where it reaches past the track. */
private get maxPos(): number {
return this.loopOn ? Math.max(this.len, this.loopEnd) : this.len
}
setLoop(startSec: number, endSec: number, enabled: boolean): void {
const wasOn = this.loopOn
this.loopStart = startSec * this.srcRate
+14 -1
View File
@@ -14,6 +14,9 @@ import { audioContext, loadDeckWorklet, resumeAudio } from './context'
import type { DeckCommand, DeckInbound, DeckOutbound } from './deckProcessor.worklet'
import { extrapolate, type Loop, type Model } from './position'
/** As `deckCore`'s: the furthest a lead-in reaches before the track (s). */
const LEAD_IN_SEC = 32
export class DeckEngine {
readonly output: GainNode
private readonly ctx: AudioContext
@@ -22,6 +25,7 @@ export class DeckEngine {
private m: Model = { pos: 0, time: 0, speed: 0, playing: false, scratching: false }
private loop: Loop = { start: 0, end: 0, on: false }
private rate = 1
private leadIn = false
private _duration = 0
private _keylockAvailable = false
private onEnded: (() => void) | null = null
@@ -146,7 +150,8 @@ export class DeckEngine {
}
seek(t: number): void {
const sec = Math.max(0, Math.min(t, this._duration))
const end = this.loop.on ? Math.max(this._duration, this.loop.end) : this._duration
const sec = Math.max(this.leadIn ? -LEAD_IN_SEC : 0, Math.min(t, end))
this.m = { ...this.m, pos: sec, time: this.ctx.currentTime }
this.send({ op: 'seek', sec })
}
@@ -176,6 +181,14 @@ export class DeckEngine {
this.send({ op: 'rate', rate })
}
/** Performance mode: a seek may land before the track (silence until it
* starts) — and a loop may reach past its end. */
setLeadIn(on: boolean): void {
this.leadIn = on
if (!on && this.m.pos < 0) this.m = { ...this.m, pos: 0, time: this.ctx.currentTime }
this.send({ op: 'leadIn', on })
}
/** Keylock: keep the pitch while the rate changes. */
setKeylock(on: boolean): void {
this.send({ op: 'keylock', on }, false)
@@ -28,6 +28,7 @@ export type DeckCommand =
| { op: 'gains'; levels: number[] }
| { op: 'rate'; rate: number }
| { op: 'keylock'; on: boolean }
| { op: 'leadIn'; on: boolean }
| { op: 'scratchBegin'; sec: number }
| { op: 'scratchRegrab' }
| { op: 'scratchTarget'; sec: number }
@@ -109,6 +110,8 @@ class DeckProcessor extends AudioWorkletProcessor {
return core.setRate(c.rate)
case 'keylock':
return core.setKeylock(c.on)
case 'leadIn':
return core.setLeadIn(c.on)
case 'scratchBegin':
return core.scratchBegin(c.sec)
case 'scratchRegrab':
+2 -1
View File
@@ -23,5 +23,6 @@ export function extrapolate(m: Model, now: number, loop: Loop, duration: number)
if (loop.on && loop.end > loop.start && p >= loop.end && m.pos < loop.end) {
p = loop.start + ((p - loop.start) % (loop.end - loop.start))
}
return Math.min(p, duration)
// The end — or an engaged loop's, where it reaches past the track.
return Math.min(p, loop.on ? Math.max(duration, loop.end) : duration)
}
+3 -2
View File
@@ -65,10 +65,11 @@ export function phaseOffset(master: DeckTempo, follower: DeckTempo, octave: numb
return d
}
/** The follower's playhead with its beats on the master's (the smallest move). */
/** The follower's playhead with its beats on the master's (the smallest move;
* it may fall before the track — performance mode's lead-in). */
export function alignedPosition(follower: DeckTempo, offset: number, octave: number): number {
const g = follower.grid!
return Math.max(0, g.timeOfBeat(g.beatIndexAt(follower.pos) - offset / octave))
return g.timeOfBeat(g.beatIndexAt(follower.pos) - offset / octave)
}
/** The pitch ranges a deck offers (fraction of the tempo either way). */