Improved cue and hotcue button press behaviour

This commit is contained in:
Ben committed 2026-07-08 13:26:36 +02:00
1 parent a0dfed9a9e
commit 73b793aee6
2 files changed
+138 -29

No files matched your search

+16 -5
View File
@@ -4,7 +4,8 @@ import { contrastText, cueColor } from '../lib/cues'
interface Props { interface Props {
cues: CuePoint[] cues: CuePoint[]
selectedSlot: number | null selectedSlot: number | null
onSlotClick: (slot: number) => void onSlotPress: (slot: number) => void
onSlotRelease: (slot: number) => void
} }
const SLOTS = [0, 1, 2, 3, 4, 5, 6, 7] // Traktor hotcue slots → labelled 1–8 const SLOTS = [0, 1, 2, 3, 4, 5, 6, 7] // Traktor hotcue slots → labelled 1–8
@@ -12,10 +13,12 @@ const SLOTS = [0, 1, 2, 3, 4, 5, 6, 7] // Traktor hotcue slots → labelled 1–
/** /**
* The 8 hotcue slots, colour-coded by cue type when assigned (gray when empty). * The 8 hotcue slots, colour-coded by cue type when assigned (gray when empty).
* Rendered as a flex-1 group inside the hotcue row; the parent supplies the row * Rendered as a flex-1 group inside the hotcue row; the parent supplies the row
* chrome and the trailing edit controls. Clicking a slot delegates to the parent * chrome and the trailing edit controls. Slots respond to press AND release
* (create when empty, jump/trigger when assigned). * (pointer capture, so a release outside the button still fires) so the parent
* can implement momentary "cue preview" (play while held). Press creates when
* empty and jumps/triggers when assigned; release ends any preview.
*/ */
export function HotcueBar({ cues, selectedSlot, onSlotClick }: Props) { export function HotcueBar({ cues, selectedSlot, onSlotPress, onSlotRelease }: Props) {
return ( return (
<div className="flex flex-1 items-stretch gap-px"> <div className="flex flex-1 items-stretch gap-px">
{SLOTS.map((slot) => { {SLOTS.map((slot) => {
@@ -25,7 +28,15 @@ export function HotcueBar({ cues, selectedSlot, onSlotClick }: Props) {
return ( return (
<button <button
key={slot} key={slot}
onClick={() => onSlotClick(slot)} onPointerDown={(e) => {
e.preventDefault()
// Capture so the matching release fires even if the pointer drifts
// off the button before it's let go.
e.currentTarget.setPointerCapture(e.pointerId)
onSlotPress(slot)
}}
onPointerUp={() => onSlotRelease(slot)}
onPointerCancel={() => onSlotRelease(slot)}
title={ title={
cue cue
? cue.name && cue.name !== 'n.n.' ? cue.name && cue.name !== 'n.n.'
+122 -24
View File
@@ -71,6 +71,10 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
const engagedRef = useRef(false) // scratch engine is dragging or coasting const engagedRef = useRef(false) // scratch engine is dragging or coasting
const pendingPlayRef = useRef(false) // a play was requested; start once ready const pendingPlayRef = useRef(false) // a play was requested; start once ready
const loadedIdRef = useRef<string | null>(null) // trackId whose buffer is loaded const loadedIdRef = useRef<string | null>(null) // trackId whose buffer is loaded
// Active momentary "cue preview": a hotcue OR the CUE button held while paused
// plays from its point and stops on release, unless `latched` (play pressed
// during the hold). `id` identifies the holder so its release matches.
const previewRef = useRef<{ id: string; start: number; latched: boolean } | null>(null)
const trackId = track?.id ?? null const trackId = track?.id ?? null
@@ -150,6 +154,7 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
setActiveBeats(null) setActiveBeats(null)
setCuePoint(0) setCuePoint(0)
loopInRef.current = null loopInRef.current = null
previewRef.current = null
}, [trackId]) }, [trackId])
// Analyse once per track; the decoded buffer feeds both the playback and // Analyse once per track; the decoded buffer feeds both the playback and
@@ -226,6 +231,18 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
const eng = playbackRef.current const eng = playbackRef.current
if (!eng || !eng.ready) return if (!eng || !eng.ready) return
getCtx() // ensure the context resumes on this user gesture getCtx() // ensure the context resumes on this user gesture
// Play pressed during a momentary cue preview → latch playback on so it
// keeps going after the hotcue button is released.
const pv = previewRef.current
if (pv && !pv.latched) {
pv.latched = true
if (!eng.playing) {
eng.play()
setPlaying(true)
}
setCurrent(eng.getPosition())
return
}
if (eng.playing) { if (eng.playing) {
eng.pause() eng.pause()
setPlaying(false) setPlaying(false)
@@ -236,10 +253,36 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
setCurrent(eng.getPosition()) setCurrent(eng.getPosition())
} }
// Start a momentary "cue preview" from `start` for holder `id`: play now, and
// remember to stop + return on release (see endPreview) unless it's latched.
const beginPreview = (id: string, start: number) => {
const eng = playbackRef.current
if (!eng || !eng.ready) return
previewRef.current = { id, start, latched: false }
eng.play()
setPlaying(true)
}
// End holder `id`'s momentary preview: stop and return to the point — unless a
// play press latched it during the hold, in which case playback continues.
const endPreview = (id: string) => {
const pv = previewRef.current
if (!pv || pv.id !== id) return
previewRef.current = null
if (pv.latched) return
const eng = playbackRef.current
if (!eng) return
eng.pause()
eng.seek(pv.start)
setPlaying(false)
setCurrent(pv.start)
}
// CUE (Traktor/CDJ-style): while playing, jump back to the cue point and // CUE (Traktor/CDJ-style): while playing, jump back to the cue point and
// pause; while paused and away from the cue, drop the cue at the playhead; // pause; while paused and away from the cue, drop the cue at the playhead;
// while paused and already at the cue, play from it. // while paused and already at the cue, play from it for as long as the button
const handleCue = () => { // is held (momentary preview — mirrors the hotcue buttons).
const onCuePress = () => {
const eng = playbackRef.current const eng = playbackRef.current
if (!eng || !eng.ready) return if (!eng || !eng.ready) return
getCtx() getCtx()
@@ -249,12 +292,17 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
setPlaying(false) setPlaying(false)
setCurrent(cuePoint) setCurrent(cuePoint)
} else if (Math.abs(current - cuePoint) > 0.03) { } else if (Math.abs(current - cuePoint) > 0.03) {
setCuePoint(current) // Drop the cue at the (snapped) playhead and park on it, so the next press
// is "at the cue" and previews rather than re-setting.
const t = snapTime(current)
setCuePoint(t)
eng.seek(t)
setCurrent(t)
} else { } else {
eng.play() beginPreview('cue', cuePoint)
setPlaying(true)
} }
} }
const onCueRelease = () => endPreview('cue')
// A library row's play button bumps `playRequest`: load (if needed) + play. // A library row's play button bumps `playRequest`: load (if needed) + play.
// If the requested track is already loaded and ready, start immediately; // If the requested track is already loaded and ready, start immediately;
@@ -458,20 +506,15 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
return best != null && bestDiff <= beats * 0.05 ? best : null return best != null && bestDiff <= beats * 0.05 ? best : null
} }
const onSlotClick = async (slot: number) => { // Hotcue slot pressed (mouse/touch down, or key down). Empty slots create a
// hotcue; assigned slots jump to their point. When jumping while PAUSED we
// also begin a momentary "cue preview" — play from the point for as long as
// the button is held (released in onSlotRelease). While already playing, a
// press just jumps, as before.
const onSlotPress = async (slot: number) => {
if (!track) return if (!track) return
const cue = hotcueAt(slot) const cue = hotcueAt(slot)
if (cue) { if (!cue) {
// A loop hotcue jumps to its start AND re-engages a loop of its length.
if (cue.type === LOOP_TYPE && cue.length > 0) {
seek(cue.start)
engagLoop(cue.start, cue.start + cue.length, beatsForLength(cue.length))
} else {
seek(cue.start)
}
setSelectedSlot(slot)
return
}
try { try {
// Setting a hotcue while a loop is active stores it as a loop hotcue. // Setting a hotcue while a loop is active stores it as a loop hotcue.
if (loopActive && loopRegion) { if (loopActive && loopRegion) {
@@ -485,7 +528,25 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
} catch (e) { } catch (e) {
onError?.((e as Error).message) onError?.((e as Error).message)
} }
return
} }
setSelectedSlot(slot)
const eng = playbackRef.current
if (!eng || !eng.ready) return
getCtx()
// A loop hotcue jumps to its start AND re-engages a loop of its length.
if (cue.type === LOOP_TYPE && cue.length > 0) {
seek(cue.start)
engagLoop(cue.start, cue.start + cue.length, beatsForLength(cue.length))
} else {
seek(cue.start)
}
// Paused at press → momentary preview: play while held.
if (!eng.playing) beginPreview(`hc:${slot}`, cue.start)
}
// Hotcue slot released (mouse/touch up, or key up). Ends its momentary preview.
const onSlotRelease = (slot: number) => endPreview(`hc:${slot}`)
const changeSelectedType = async (type: number) => { const changeSelectedType = async (type: number) => {
if (!track || selectedSlot == null) return if (!track || selectedSlot == null) return
@@ -520,29 +581,59 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
// Space → play/pause; 1–8 → the matching hotcue slot; Shift+1–8 → delete it. // Space → play/pause; 1–8 → the matching hotcue slot; Shift+1–8 → delete it.
// Digits are read from e.code (layout-/Shift-independent) and a ref holds the // Digits are read from e.code (layout-/Shift-independent) and a ref holds the
// latest handlers so the listener attaches once and never goes stale. // latest handlers so the listener attaches once and never goes stale.
const shortcutsRef = useRef({ toggle, onSlotClick, deleteHotcueSlot }) const shortcutsRef = useRef({
shortcutsRef.current = { toggle, onSlotClick, deleteHotcueSlot } toggle,
onSlotPress,
onSlotRelease,
deleteHotcueSlot,
onCuePress,
onCueRelease,
})
shortcutsRef.current = {
toggle,
onSlotPress,
onSlotRelease,
deleteHotcueSlot,
onCuePress,
onCueRelease,
}
useEffect(() => { useEffect(() => {
const onKey = (e: KeyboardEvent) => { const onKey = (e: KeyboardEvent) => {
// Ignore while typing in a field or with a non-Shift modifier held. // Ignore while typing in a field or with a non-Shift modifier held.
const t = e.target as HTMLElement | null const t = e.target as HTMLElement | null
if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable)) return if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable)) return
if (e.metaKey || e.ctrlKey || e.altKey) return if (e.metaKey || e.ctrlKey || e.altKey) return
if (e.repeat) return // held key auto-repeats — treat as one press+hold
if (e.code === 'Space') { if (e.code === 'Space') {
e.preventDefault() e.preventDefault()
shortcutsRef.current.toggle() shortcutsRef.current.toggle()
return return
} }
if (e.code === 'KeyC') {
e.preventDefault()
shortcutsRef.current.onCuePress()
return
}
const digit = e.code.match(/^(?:Digit|Numpad)([1-8])$/) const digit = e.code.match(/^(?:Digit|Numpad)([1-8])$/)
if (digit) { if (digit) {
e.preventDefault() e.preventDefault()
const slot = Number(digit[1]) - 1 const slot = Number(digit[1]) - 1
if (e.shiftKey) void shortcutsRef.current.deleteHotcueSlot(slot) if (e.shiftKey) void shortcutsRef.current.deleteHotcueSlot(slot)
else void shortcutsRef.current.onSlotClick(slot) else void shortcutsRef.current.onSlotPress(slot)
} }
} }
// keyup ends a held hotcue's momentary preview (release).
const onKeyRelease = (e: KeyboardEvent) => {
if (e.code === 'KeyC') shortcutsRef.current.onCueRelease()
const digit = e.code.match(/^(?:Digit|Numpad)([1-8])$/)
if (digit) shortcutsRef.current.onSlotRelease(Number(digit[1]) - 1)
}
window.addEventListener('keydown', onKey) window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey) window.addEventListener('keyup', onKeyRelease)
return () => {
window.removeEventListener('keydown', onKey)
window.removeEventListener('keyup', onKeyRelease)
}
}, []) }, [])
// ---- beatgrid --------------------------------------------------------- // ---- beatgrid ---------------------------------------------------------
@@ -631,10 +722,16 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
<div className="mt-auto flex items-center gap-2"> <div className="mt-auto flex items-center gap-2">
<button <button
onClick={handleCue} onPointerDown={(e) => {
e.preventDefault()
e.currentTarget.setPointerCapture(e.pointerId)
onCuePress()
}}
onPointerUp={onCueRelease}
onPointerCancel={onCueRelease}
disabled={!ready} disabled={!ready}
className="flex h-11 items-center justify-center rounded-xl border border-line bg-ink-850 px-4 text-sm font-bold tracking-wide text-gold transition-colors hover:border-gold disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:border-line" className="flex h-11 items-center justify-center rounded-xl border border-line bg-ink-850 px-4 text-sm font-bold tracking-wide text-gold transition-colors hover:border-gold disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:border-line"
title="Cue — set the cue point (paused) or jump back to it (playing)" title="Cue — set the cue point (paused), hold to preview from it, or jump back to it (playing)"
> >
CUE CUE
</button> </button>
@@ -737,7 +834,8 @@ export function PrepStrip({ track, playRequest = 0, onError }: Props) {
<HotcueBar <HotcueBar
cues={cueData?.cues ?? []} cues={cueData?.cues ?? []}
selectedSlot={selectedSlot} selectedSlot={selectedSlot}
onSlotClick={onSlotClick} onSlotPress={onSlotPress}
onSlotRelease={onSlotRelease}
/> />
<div className="flex w-28 items-center justify-center bg-ink-900 px-1"> <div className="flex w-28 items-center justify-center bg-ink-900 px-1">
{selectedCue && selectedCue.type === LOOP_TYPE ? ( {selectedCue && selectedCue.type === LOOP_TYPE ? (