mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented beat jumping
This commit is contained in:
1 parent
f52c19f14d
commit
1e8c53e693
2 files changed
+148
-1
No files matched your search
@@ -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 (
|
||||
<div className="flex items-center gap-1">
|
||||
<span className="mr-0.5 text-[10px] font-semibold uppercase tracking-wider text-faint">
|
||||
Jump
|
||||
</span>
|
||||
<button
|
||||
className={BTN}
|
||||
onClick={() => onStep(-1)}
|
||||
disabled={disabled || atMin}
|
||||
title="Smaller jump size"
|
||||
>
|
||||
−
|
||||
</button>
|
||||
<span className="w-9 text-center text-xs font-semibold tabular-nums text-text">
|
||||
{sizeLabel(beats)}
|
||||
</span>
|
||||
<button
|
||||
className={BTN}
|
||||
onClick={() => onStep(1)}
|
||||
disabled={disabled || atMax}
|
||||
title="Larger jump size"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
<span className="mx-0.5 w-px self-stretch bg-line" aria-hidden />
|
||||
<button
|
||||
className={BTN}
|
||||
onClick={() => onJump(-1)}
|
||||
disabled={disabled}
|
||||
title="Jump back (←)"
|
||||
>
|
||||
◀
|
||||
</button>
|
||||
<button
|
||||
className={BTN}
|
||||
onClick={() => onJump(1)}
|
||||
disabled={disabled}
|
||||
title="Jump forward (→)"
|
||||
>
|
||||
▶
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -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<number | null>(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<number | null>(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 (
|
||||
<div className="flex h-[23rem] shrink-0 items-stretch gap-px border-b border-line bg-ink-950">
|
||||
<div className="flex h-[25.5rem] shrink-0 items-stretch gap-px border-b border-line bg-ink-950">
|
||||
{/* Controls */}
|
||||
<div className="flex w-72 shrink-0 flex-col gap-3 bg-ink-900 px-4 py-3">
|
||||
<div className="min-w-0">
|
||||
@@ -817,6 +878,13 @@ export function PrepStrip({ track, playRequest = 0, onError, onNotify }: Props)
|
||||
{fmt(current)} / {fmt(duration)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<BeatJumpControls
|
||||
beats={jumpBeats}
|
||||
onStep={stepJumpSize}
|
||||
onJump={beatJump}
|
||||
disabled={!canBeatJump}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Waveforms + loop/hotcue controls. */}
|
||||
|
||||
Reference in new issue
Block a user