From 1e8c53e6936371e545450f633715bcb7a5aaf039 Mon Sep 17 00:00:00 2001 From: Benjamin Hardy Date: Wed, 8 Jul 2026 14:48:38 +0200 Subject: [PATCH] Implemented beat jumping --- frontend/src/components/BeatJumpControls.tsx | 79 ++++++++++++++++++++ frontend/src/components/PrepStrip.tsx | 70 ++++++++++++++++- 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 frontend/src/components/BeatJumpControls.tsx diff --git a/frontend/src/components/BeatJumpControls.tsx b/frontend/src/components/BeatJumpControls.tsx new file mode 100644 index 0000000..0bea6ed --- /dev/null +++ b/frontend/src/components/BeatJumpControls.tsx @@ -0,0 +1,79 @@ +/** Jump-size cycle, in beats. Fractional sizes allow fine micro-nudging. */ +export const BEAT_JUMP_SIZES = [0.25, 0.5, 1, 2, 4, 8, 16, 32] + +interface Props { + /** Current jump size (one of BEAT_JUMP_SIZES). */ + beats: number + /** Step the jump size down (-1) / up (+1) through BEAT_JUMP_SIZES. */ + onStep: (dir: -1 | 1) => void + /** Jump the playhead back (-1) / forward (+1) by `beats`. */ + onJump: (dir: -1 | 1) => void + /** Disabled when the loaded track has no beatgrid. */ + disabled: boolean +} + +const BTN = + 'flex h-7 w-7 shrink-0 items-center justify-center rounded border border-line bg-ink-850 ' + + 'text-xs font-semibold text-text transition-colors hover:border-accent disabled:opacity-30 ' + + 'disabled:hover:border-line' + +/** Beats-only readout: 0.25 → "1/4", 0.5 → "1/2", else the integer. */ +function sizeLabel(beats: number): string { + if (beats === 0.25) return '1/4' + if (beats === 0.5) return '1/2' + return String(beats) +} + +/** + * Beat-jump control cluster (Traktor/Serato equivalent): step the jump size, + * then jump the playhead forward/back by that many beats — phase-preserving, + * playback-only. Presentational + controlled; PrepStrip owns the size state and + * performs the actual seek. Disabled wholesale when the track is ungridded. + */ +export function BeatJumpControls({ beats, onStep, onJump, disabled }: Props) { + const atMin = beats <= BEAT_JUMP_SIZES[0] + const atMax = beats >= BEAT_JUMP_SIZES[BEAT_JUMP_SIZES.length - 1] + return ( +
+ + Jump + + + + {sizeLabel(beats)} + + + + + +
+ ) +} diff --git a/frontend/src/components/PrepStrip.tsx b/frontend/src/components/PrepStrip.tsx index fd3d667..86bb285 100644 --- a/frontend/src/components/PrepStrip.tsx +++ b/frontend/src/components/PrepStrip.tsx @@ -4,6 +4,7 @@ import { api, type Track, type TrackCues } from '../api' import { analyzeWaveform, type WaveColumn } from '../lib/waveform' import { ScratchEngine } from '../lib/scratchEngine' import { PlaybackEngine } from '../lib/playbackEngine' +import { BeatJumpControls, BEAT_JUMP_SIZES } from './BeatJumpControls' import { GridControls } from './GridControls' import { HotcueBar } from './HotcueBar' import { LoopControls } from './LoopControls' @@ -57,6 +58,7 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) const [selectedSlot, setSelectedSlot] = useState(null) const [cuePoint, setCuePoint] = useState(0) // floating "CUE" point (frontend-only) const [secPerView, setSecPerView] = useState(DEFAULT_SEC) // main-waveform zoom (persisted) + const [jumpBeats, setJumpBeats] = useState(4) // beat-jump size in beats (persisted) // Loop state (transient — persisted only when saved as a hotcue later). const [loopRegion, setLoopRegion] = useState<{ start: number; end: number } | null>(null) @@ -109,6 +111,26 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) } }, [secPerView]) + // Beat-jump size persists the same way (survives track switches + restarts). + const jumpHydratedRef = useRef(false) + const jumpSaveTimer = useRef(null) + useEffect(() => { + if (jumpHydratedRef.current || !prefsQuery.data) return + jumpHydratedRef.current = true + const b = (prefsQuery.data as { beatJumpBeats?: unknown }).beatJumpBeats + if (typeof b === 'number' && BEAT_JUMP_SIZES.includes(b)) setJumpBeats(b) + }, [prefsQuery.data]) + useEffect(() => { + if (!jumpHydratedRef.current) return // don't clobber saved prefs pre-hydration + if (jumpSaveTimer.current) window.clearTimeout(jumpSaveTimer.current) + jumpSaveTimer.current = window.setTimeout(() => { + api.patchPrefs({ beatJumpBeats: jumpBeats }).catch(() => {}) + }, 500) + return () => { + if (jumpSaveTimer.current) window.clearTimeout(jumpSaveTimer.current) + } + }, [jumpBeats]) + const getCtx = (): AudioContext => { if (!audioCtxRef.current) { audioCtxRef.current = new (window.AudioContext ?? @@ -360,6 +382,31 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) seek(t) } + // ---- beat jump -------------------------------------------------------- + // Step the jump size through BEAT_JUMP_SIZES, clamping at both ends (no wrap). + const stepJumpSize = (dir: -1 | 1) => { + setJumpBeats((b) => { + const i = BEAT_JUMP_SIZES.indexOf(b) + const next = Math.min(BEAT_JUMP_SIZES.length - 1, Math.max(0, (i < 0 ? 4 : i) + dir)) + return BEAT_JUMP_SIZES[next] + }) + } + + // Move the playhead exactly `jumpBeats` beats fwd/back, preserving sub-beat + // phase (pure translation, no grid snap). Like seekManual, jumping escapes an + // active loop. seek() clamps to [0, duration], preserves play/pause state. + const beatJump = (dir: -1 | 1) => { + const eng = playbackRef.current + const bpm = cueData?.bpm ?? null + if (!eng || !bpm || bpm <= 0) return + if (loopActive) { + eng.setLoopEnabled(false) + setLoopActive(false) + } + eng.seek(eng.getPosition() + dir * jumpBeats * (60 / bpm)) + setCurrent(eng.getPosition()) + } + // ---- scratch ---------------------------------------------------------- const finalizeScratch = () => { cancelAnimationFrame(scratchRafRef.current) @@ -627,6 +674,7 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) deleteHotcueSlot, onCuePress, onCueRelease, + beatJump, }) shortcutsRef.current = { toggle, @@ -635,6 +683,7 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) deleteHotcueSlot, onCuePress, onCueRelease, + beatJump, } useEffect(() => { const onKey = (e: KeyboardEvent) => { @@ -653,6 +702,16 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) shortcutsRef.current.onCuePress() return } + if (e.code === 'ArrowLeft') { + e.preventDefault() + shortcutsRef.current.beatJump(-1) + return + } + if (e.code === 'ArrowRight') { + e.preventDefault() + shortcutsRef.current.beatJump(1) + return + } const digit = e.code.match(/^(?:Digit|Numpad)([1-8])$/) if (digit) { e.preventDefault() @@ -678,6 +737,8 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) // ---- beatgrid --------------------------------------------------------- const gridBpm = cueData?.bpm ?? null const gridAnchor = cueData?.grid_anchor ?? null + // Beat jump needs a tempo to size beats; disabled on ungridded tracks. + const canBeatJump = ready && gridBpm != null && gridBpm > 0 const editGrid = async (patch: { bpm?: number; anchor?: number }) => { if (!track) return @@ -737,7 +798,7 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) const activeLoop = loopActive && loopRegion ? loopRegion : null return ( -
+
{/* Controls */}
@@ -817,6 +878,13 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props) {fmt(current)} / {fmt(duration)}
+ +
{/* Waveforms + loop/hotcue controls. */}