diff --git a/CLAUDE.md b/CLAUDE.md index deb4e84..0c19938 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1174,7 +1174,7 @@ Two independent apps that talk over HTTP: / Try again (`backend_restart`). The shell has no timeout of its own. Before ready it answers quit requests itself — nothing can be unsaved — and stops at ready so it never races `QuitGuard`), `SaveBar` (+ "Discard changes…" when - dirty, and the settings gear beside it, whose menu (or `?` anywhere) opens `ShortcutsDialog` — a KEYBOARD MAP (decided 2026-10-15): keys lit in the colour of what they do, a caption explaining the key pointed at or pressed (pressing explains, it never acts: a capture-phase listener keeps the deck's and table's keys from firing), holding or picking ⇧ / Cmd-Ctrl relights it with that layer, mouse gestures listed beneath. It shrinks to fit the dialog and drops the words on the keys when too small. Its content is `lib/shortcuts.ts`, written out BY HAND, so a new or changed shortcut in `PrepStrip` / `TrackTable` must be added there too — and `AppSettings` ("Konduktor Settings…": app-level prefs, each with its default in `APP_SETTING_DEFAULTS` and read through `useAppSetting`; today `expandPlaylistFolders` and `exportRootFolder`, both default off — the latter read by the backend at `run_export` and passed down as `ExportPayload.root_folder` — and `timeReadoutDecimals` (0–3, default 1: the deck's ELAPSED / REMAIN, truncated, never rounded). The Sidebar holds folder open state as per-id overrides of that setting, reset on a new library or a change of the setting, and carried to the new ids when a rename/move changes a folder's path id)), `Toolbar` (search/filters; columns are chosen by right-clicking the table header), + dirty, and the settings gear beside it, whose menu (or `?` anywhere) opens `ShortcutsDialog` — a KEYBOARD MAP (decided 2026-10-15): keys lit in the colour of what they do, a caption explaining the key pointed at or pressed (pressing explains, it never acts: a capture-phase listener keeps the deck's and table's keys from firing), holding or picking ⇧ / Cmd-Ctrl relights it with that layer, mouse gestures listed beneath. It shrinks to fit the dialog and drops the words on the keys when too small. Its content is `lib/shortcuts.ts`, written out BY HAND, so a new or changed shortcut in `lib/deckKeys.ts` / `TrackTable` must be added there too — and `AppSettings` ("Konduktor Settings…": app-level prefs, each with its default in `APP_SETTING_DEFAULTS` and read through `useAppSetting`; today `expandPlaylistFolders` and `exportRootFolder`, both default off — the latter read by the backend at `run_export` and passed down as `ExportPayload.root_folder` — and `timeReadoutDecimals` (0–3, default 1: the deck's ELAPSED / REMAIN, truncated, never rounded). The Sidebar holds folder open state as per-id overrides of that setting, reset on a new library or a change of the setting, and carried to the new ids when a rename/move changes a folder's path id)), `Toolbar` (search/filters; columns are chosen by right-clicking the table header), `TrackTable` (**the one track list** — All Tracks, playlists, exports and devices. TanStack Table + **virtualized** grid; per-row play button, configurable columns, header sorting, inline double-click editing, and @@ -1350,8 +1350,20 @@ Two independent apps that talk over HTTP: plus a half-size mono copy each on the main thread for the raw waveform — ~6 stereo-track units for a stem track (it was 5: four buffers plus a summed scratch copy), 1.5 for a plain track (it was 2). - - **Prep strip** (DJ deck across the top of the window): `PrepStrip` owns it, - in three rows. **Header**: cover, title/artist · album · genre, a Read-only + - **Prep strip** (DJ deck across the top of the window), in three rows. + **The deck is `components/deck/`** (split out 2026-11-02 for performance + mode's second deck): `useDeck` holds everything a deck DOES — engine, + transport, CUE, hotcues, memory cues, loops, grid editing, stems, + analysis — and takes its capabilities as a PARAMETER (each deck may hold + a different library's track), returning a `Deck` that `DeckHeader`, + `DeckMainWave` / `DeckOverview`, `DeckControls` and `DeckOverlays` (its + menus and dialogs, under the deck's own `CapabilitiesContext`) render. + `PrepStrip` lays one deck out and owns what decks SHARE: the main view's + zoom + raw toggle (`DeckView`, persisted) and the keyboard — + `lib/deckKeys.ts`, ONE window listener routing each key to the focused + deck (Alt/Option = the other deck, in performance mode; a key-up goes to + the deck its key-down went to). A deck's `persist` (loop size, cue bank) + and `leadsAmbient` (cover colours) are for deck A. **Header**: cover, title/artist · album · genre, a Read-only badge, the readout well (ELAPSED / REMAIN to tenths, KEY, and `BpmReadout` — click to type an exact BPM; with no grid, a typed or tapped BPM CREATES one at the playhead, and Set grid on a track with no BPM opens the readout to ask rather than failing), SNAP, Analyze, Auto hotcues. **Waveforms**: diff --git a/frontend/src/components/PrepStrip.tsx b/frontend/src/components/PrepStrip.tsx index cfff335..d92ea5e 100644 --- a/frontend/src/components/PrepStrip.tsx +++ b/frontend/src/components/PrepStrip.tsx @@ -1,47 +1,15 @@ -import { useEffect, useMemo, useRef, useState } from 'react' -import { useQuery, useQueryClient } from '@tanstack/react-query' -import { - api, - type AutoHotcuesResult, - type CuePoint, - type CueType, - type GridMarker, - type Track, - type TrackCues, - type StemInfo, - type TrackOrigin, - trackAudioUrl, - trackCuesFor, - trackStemsFor, - trackStemUrl, -} from '../api' -import { buildBeatGrid, GRID_EPS } from '../lib/beatgrid' -import { slotLabeller, useCaps } from '../lib/capabilities' -import { CUE_TYPE_LABELS, cueTypeColor } from '../lib/cues' -import { formatLoudness, keyColor } from '../lib/format' -import { readOnlyShort, readOnlyNotice } from '../lib/platformCopy' -import { analyzeStems, analyzeWaveform, type WaveColumn } from '../lib/waveform' -import type { RawWave } from '../lib/rawWave' -import { setAmbientFromArt } from '../lib/ambient' -import { Icon } from '../lib/icons' -import { DeckEngine } from '../lib/audio/deckEngine' -import { resumeAudio } from '../lib/audio/context' -import { invalidateTrackLists } from '../lib/trackQueries' -import { AutoCueDialog, eventLabel } from './AutoCueDialog' -import { TrackInfoDialog } from './TrackInfoDialog' -import { ContextMenu, type MenuItem } from './ContextMenu' -import { BpmReadout, GridEditStrip, TempoControls, TempoFold } from './GridControls' -import { HotcueBar } from './HotcueBar' -import { MemoryCueStrip } from './MemoryCueStrip' -import { ModeSwitch } from './ModeSwitch' -import { LoopControls, LOOP_SIZES, type LoopMode } from './LoopControls' -import { MainWaveform, MIN_SEC, MAX_SEC, DEFAULT_SEC, type StemLane } from './MainWaveform' -import { OverviewWaveform } from './OverviewWaveform' -import { useAppSetting } from './AppSettings' +import { useEffect, useRef, useState } from 'react' +import { useQuery } from '@tanstack/react-query' +import { api, type Track, type TrackOrigin } from '../api' +import { useCaps } from '../lib/capabilities' +import { useDeckKeys, type DeckKeyTargets } from '../lib/deckKeys' import { usePrefsWriter } from '../lib/prefs' - -/** Keys that mute stems 1-4 (Shift = solo), by `KeyboardEvent.code`. */ -const STEM_KEYS = ['KeyQ', 'KeyW', 'KeyE', 'KeyR'] +import { DeckControls } from './deck/DeckControls' +import { DeckHeader } from './deck/DeckHeader' +import { DeckOverlays } from './deck/DeckOverlays' +import { DeckMainWave, DeckOverview, type DeckView } from './deck/DeckWaves' +import { useDeck } from './deck/useDeck' +import { MIN_SEC, MAX_SEC, DEFAULT_SEC } from './MainWaveform' interface Props { /** The track currently loaded into the prep deck, if any. */ @@ -51,13 +19,7 @@ interface Props { onError?: (msg: string) => void /** Neutral/success feedback (e.g. Auto Hotcues result). */ onNotify?: (kind: 'success' | 'error', msg: string) => void - /** - * Which library the track belongs to, and so which endpoints serve its audio - * and cues: the loaded collection, a browsed DEVICE, or a browsed FOLDER of - * loose files. Every EDIT control is already inert for the last two — their - * capabilities say `writable: false`, and the deck gates on those — so this - * only needs to redirect the two READS. - */ + /** Which library the track belongs to (see `DeckOptions.origin`). */ origin?: TrackOrigin /** Bumped when something outside the deck (batch grid analysis) rewrote the * loaded track's cues/grid, so the deck re-reads them. */ @@ -69,136 +31,22 @@ interface Props { onEditTags?: (track: Track) => void } -/** mm:ss.f — tenths by default, as a CDJ does; `places` is the - * `timeReadoutDecimals` setting (0 drops the fraction). Truncates, never - * rounds, so the readout never shows a second before it has been reached. */ -function fmtTime(secs: number, places: number): string { - if (!isFinite(secs) || secs < 0) secs = 0 - const scale = 10 ** places - // The epsilon keeps e.g. 0.3 s from reading 0.299 at 3 places. - const units = Math.floor(secs * scale + 1e-6) - const whole = Math.floor(units / scale) - const m = Math.floor(whole / 60) - const s = whole % 60 - const frac = places > 0 ? `.${(units % scale).toString().padStart(places, '0')}` : '' - return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}${frac}` -} - /** - * Digit key -> bank slot: 1..9 map to slots 0..8 and 0 maps to slot 10. - * - * Ten digits is the physical ceiling, so a bank larger than that simply has no - * shortcut for its tail. Returns null when the key is not a digit or the slot is - * beyond this platform's bank — an 8-slot bank behaves exactly as before. + * The main waveform's zoom and raw/bars toggle, persisted to userprefs.json so + * they survive track switches (MainWaveform unmounts during each load) and app + * restarts. Hydrated once from the shared ['prefs'] query, then written through + * the shared, debounced prefs writer (as App's column layout is). One value for + * every deck: lanes at one zoom line their beats up. */ -const DIGIT_RE = /^(?:Digit|Numpad)([0-9])$/ - -function slotForDigit(code: string, slotCount: number): number | null { - const m = code.match(DIGIT_RE) - if (!m) return null - const slot = m[1] === '0' ? 9 : Number(m[1]) - 1 - return slot < slotCount ? slot : null -} - - -/** - * The DJ-style "prep strip" across the top of the window: transport controls on - * the left, waveforms on the right (a scrolling zoomable main view above a - * whole-track overview), plus loop + hotcue controls. Playback, loops and - * scratch all run in the deck's audio worklet (DeckEngine); the audio is - * decoded once for it and both waveform views. - */ -export function PrepStrip({ - track, - playRequest = 0, - onError, - onNotify, - origin = 'collection', - cuesRefresh = 0, - onPlayingChange, - onEditTags, -}: Props) { - const qc = useQueryClient() - const [playing, setPlaying] = useState(false) - useEffect(() => onPlayingChange?.(playing), [playing, onPlayingChange]) - const [previewing, setPreviewing] = useState(false) // momentary hold-to-play active - // CUE is held down (mouse OR the C key) — drives the button's pressed look, - // which the pointer's :active alone cannot, since the key never touches it. - const [cueHeld, setCueHeld] = useState(false) - const [current, setCurrent] = useState(0) - const timeDecimals = useAppSetting('timeReadoutDecimals') - const [duration, setDuration] = useState(0) - const [ready, setReady] = useState(false) - const [cols, setCols] = useState(null) - // The raw waveform's data: the mix's samples, or one per stem lane. - const [raw, setRaw] = useState(null) - const [stemRaws, setStemRaws] = useState(null) - // A stem track: its stems (from the FILE), their lanes, and which are muted - // — reset per track, as Traktor's stem deck does. There is no separate solo - // state: solo is a shortcut that SETS the mutes, so every stem stays freely - // mutable afterwards. - const [stems, setStems] = useState(null) - const [stemLanes, setStemLanes] = useState(null) - const [stemMuted, setStemMuted] = useState([]) - const [waveStatus, setWaveStatus] = useState<'loading' | 'ready' | 'error'>('loading') - const [cueData, setCueData] = useState(null) - // The beatgrid, rebuilt whenever the cue data is replaced (every edit returns - // a fresh TrackCues, so this stays in step automatically). Null = no grid. - const grid = useMemo(() => buildBeatGrid(cueData?.grid_markers), [cueData?.grid_markers]) - const [snap, setSnap] = useState(true) - const [selectedSlot, setSelectedSlot] = useState(null) - const [cuePoint, setCuePoint] = useState(0) // floating "CUE" point (frontend-only) - const [infoOpen, setInfoOpen] = useState(false) // the cover's track-info popup - const [secPerView, setSecPerView] = useState(DEFAULT_SEC) // main-waveform zoom (persisted) - const [rawWave, setRawWave] = useState(false) // main waveform: raw line instead of bars (persisted) - // ONE size, in beats, for beat loops and beat jump alike (persisted). - const [beatSize, setBeatSize] = useState(4) - const [loopMode, setLoopMode] = useState('beat') - // Grid mode swaps the hotcue pads for the grid-editing strip. - const [gridMode, setGridMode] = useState(false) - const [padMenu, setPadMenu] = useState<{ slot: number; x: number; y: number } | null>(null) - const [renamingSlot, setRenamingSlot] = useState(null) - // Hot / Memory: which bank the pads' place in the row shows (persisted). - const [bankMode, setBankMode] = useState<'hot' | 'memory'>('hot') - const [memoryMenu, setMemoryMenu] = useState<{ cue: CuePoint; x: number; y: number } | null>(null) - const [renamingMemory, setRenamingMemory] = useState(null) - - // Loop state (transient — persisted only when saved as a hotcue later). - const [loopRegion, setLoopRegion] = useState<{ start: number; end: number } | null>(null) - const [loopActive, setLoopActive] = useState(false) - const [activeBeats, setActiveBeats] = useState(null) - // The armed manual loop-in point: IN was pressed, OUT not yet. STATE, not a - // ref, because the IN button and the waveform both show it while it waits. - const [loopInPoint, setLoopInPoint] = useState(null) - const originalGridRef = useRef(null) - - // The deck's audio (created once, asynchronously: the worklet module loads - // first). playbackRef is set once it exists; getEngine() awaits it. - const playbackRef = useRef(null) - const enginePromiseRef = useRef | null>(null) - const wasPlayingRef = useRef(false) - const scratchRafRef = useRef(0) - const engagedRef = useRef(false) // the deck is scratching (dragging or coasting) - const pendingPlayRef = useRef(false) // a play was requested; start once ready - const loadedIdRef = useRef(null) // trackId whose buffer is loaded - const [gridBusy, setGridBusy] = useState(false) - // 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 - - // Main-waveform zoom (and the raw/bars toggle) persists to userprefs.json so it survives track switches - // (MainWaveform unmounts during each load) and app restarts. Hydrate once from - // the shared ['prefs'] query, then persist changes through the shared, - // debounced prefs writer (as App's column layout does). +function useDeckView(): DeckView { + const [secPerView, setSecPerView] = useState(DEFAULT_SEC) + const [rawWave, setRawWave] = useState(false) const prefsQuery = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }) - const zoomHydratedRef = useRef(false) + const hydratedRef = useRef(false) const writePrefs = usePrefsWriter() useEffect(() => { - if (zoomHydratedRef.current || !prefsQuery.data) return - zoomHydratedRef.current = true + if (hydratedRef.current || !prefsQuery.data) return + hydratedRef.current = true const z = (prefsQuery.data as { mainZoomSec?: unknown }).mainZoomSec if (typeof z === 'number' && isFinite(z)) { setSecPerView(Math.min(MAX_SEC, Math.max(MIN_SEC, z))) @@ -206,1618 +54,36 @@ export function PrepStrip({ if ((prefsQuery.data as { mainWaveRaw?: unknown }).mainWaveRaw === true) setRawWave(true) }, [prefsQuery.data]) useEffect(() => { - if (!zoomHydratedRef.current) return // don't clobber saved prefs pre-hydration + if (!hydratedRef.current) return // don't clobber saved prefs pre-hydration writePrefs({ mainZoomSec: secPerView, mainWaveRaw: rawWave }) }, [secPerView, rawWave, writePrefs]) - - // The loop/jump size persists the same way (survives track switches + - // restarts). `beatJumpBeats` is its name from when only beat jump had a size; - // it is read once so an upgrade keeps the user's choice. - const sizeHydratedRef = useRef(false) - useEffect(() => { - if (sizeHydratedRef.current || !prefsQuery.data) return - sizeHydratedRef.current = true - const prefs = prefsQuery.data as { beatSize?: unknown; beatJumpBeats?: unknown } - const b = prefs.beatSize ?? prefs.beatJumpBeats - if (typeof b === 'number' && LOOP_SIZES.includes(b)) setBeatSize(b) - }, [prefsQuery.data]) - useEffect(() => { - if (!sizeHydratedRef.current) return // don't clobber saved prefs pre-hydration - writePrefs({ beatSize }) - }, [beatSize, writePrefs]) - - // Hot / Memory bank, remembered the same way (decided 2026-10-16). - const bankHydratedRef = useRef(false) - useEffect(() => { - if (bankHydratedRef.current || !prefsQuery.data) return - bankHydratedRef.current = true - if ((prefsQuery.data as { cueBankMode?: unknown }).cueBankMode === 'memory') setBankMode('memory') - }, [prefsQuery.data]) - useEffect(() => { - if (!bankHydratedRef.current) return // don't clobber saved prefs pre-hydration - writePrefs({ cueBankMode: bankMode }) - }, [bankMode, writePrefs]) - - const getEngine = (): Promise => { - if (!enginePromiseRef.current) { - enginePromiseRef.current = DeckEngine.create().then((eng) => { - playbackRef.current = eng - eng.setOnEnded(() => setPlaying(false)) - // The deck hands itself back after a scratch's coast settles. - eng.setOnScratchDone((resumed) => scratchDoneRef.current(resumed)) - return eng - }) - enginePromiseRef.current.catch(() => { - enginePromiseRef.current = null - }) - } - return enginePromiseRef.current - } - - // Tear the deck's audio down when the strip itself unmounts (e.g. the - // collection picker early-returns and replaces the whole UI) — otherwise a - // remount would play a SECOND track over the orphaned one. The AudioContext - // is the app's and stays; this deck's nodes leave it. - useEffect(() => { - return () => { - const pending = enginePromiseRef.current - enginePromiseRef.current = null - playbackRef.current = null - void pending?.then((eng) => eng.dispose()) - } - }, []) - - // Reset transport + loop state when the loaded track changes. - useEffect(() => { - playbackRef.current?.pause() - setPlaying(false) - setCurrent(0) - setDuration(0) - setReady(false) - setSelectedSlot(null) - setPadMenu(null) - setRenamingSlot(null) - setMemoryMenu(null) - setRenamingMemory(null) - setLoopRegion(null) - setLoopActive(false) - setActiveBeats(null) - setCuePoint(0) - setLoopInPoint(null) - previewRef.current = null - setPreviewing(false) - setCueHeld(false) - setStemMuted([]) - }, [trackId]) - - // Recolour the whole app from this track's cover art. A device's art is not - // served (only its audio is), so a device track keeps the default look. - useEffect(() => { - void setAmbientFromArt(trackId && origin === 'collection' ? api.artUrl(trackId) : null) - }, [trackId, origin]) - - // Analyse once per track; the decoded buffer feeds both the playback and - // scratch engines (no re-decode) and both waveform views share the columns. - useEffect(() => { - if (!trackId) { - setCols(null) - setRaw(null) - setStemRaws(null) - setWaveStatus('loading') - return - } - let cancelled = false - setCols(null) - setRaw(null) - setStemRaws(null) - setStems(null) - setStemLanes(null) - setWaveStatus('loading') - // A stem FILE loads its four stems and plays their sum (decided: always, - // for every stem track); anything else loads as one buffer, as before. - const load = trackStemsFor(origin, trackId) - .catch(() => [] as StemInfo[]) - .then(async (found) => { - if (found.length) { - const res = await analyzeStems(found.map((_, k) => trackStemUrl(origin, trackId, k))) - return { cols: res.cols, audios: res.audios, lanes: res.lanes, stems: found, raw: null, raws: res.raws } - } - const res = await analyzeWaveform(trackAudioUrl(origin, trackId)) - return { cols: res.cols, audios: [res.audio], lanes: null, stems: null, raw: res.raw, raws: null } - }) - Promise.all([load, getEngine()]) - .then(([res, eng]) => { - if (cancelled) return - setCols(res.cols) - setRaw(res.raw) - setStemRaws(res.raws) - setStems(res.stems) - setStemLanes(res.lanes) - setWaveStatus('ready') - // The PCM moves to the audio thread here (transferred, not copied). - eng.load(res.audios) - setDuration(res.audios[0].duration) - setReady(true) - loadedIdRef.current = trackId - // A play was requested (library row button) → start now that it's ready. - if (pendingPlayRef.current) { - pendingPlayRef.current = false - eng.play() - setPlaying(true) - } - }) - .catch(() => { - if (!cancelled) setWaveStatus('error') - }) - return () => { - cancelled = true - } - }, [trackId, origin]) - - // Fetch beatgrid + cue markers for the loaded track. - useEffect(() => { - if (!trackId) { - setCueData(null) - return - } - let cancelled = false - setCueData(null) - ;trackCuesFor(origin, trackId) - .then((d) => { - if (cancelled) return - setCueData(d) - // Remember the grid as loaded, for "Reset". Flexible-grid editing is - // multi-step and destructive, so restoring exactly what Traktor had is - // the safety net. Set only here (keyed on trackId), never by an edit. - originalGridRef.current = d.grid_markers - }) - .catch(() => { - if (!cancelled) setCueData(null) - }) - return () => { - cancelled = true - } - }, [trackId, origin]) - - // Re-read after an outside edit. Deliberately NOT the effect above: that one - // resets `originalGridRef`, and "Reset" should still restore the grid the - // track had before the outside edit replaced it. - useEffect(() => { - if (!trackId || !cuesRefresh) return - let cancelled = false - api.trackCues(trackId).then((d) => !cancelled && setCueData(d)).catch(() => {}) - return () => { - cancelled = true - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [cuesRefresh]) - - const toggle = () => { - const eng = playbackRef.current - if (!eng || !eng.ready) return - resumeAudio() // resume 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 - setPreviewing(false) // becomes normal (green) playback - if (!eng.playing) { - eng.play() - setPlaying(true) - } - setCurrent(eng.getPosition()) - return - } - if (eng.playing) { - eng.pause() - setPlaying(false) - } else { - eng.play() - setPlaying(true) - } - 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) - setPreviewing(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 - setPreviewing(false) - 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 - // pause; while paused and away from the cue, drop the cue at the playhead; - // while paused and already at the cue, play from it for as long as the button - // is held (momentary preview — mirrors the hotcue buttons). - const onCuePress = () => { - const eng = playbackRef.current - if (!eng || !eng.ready) return - setCueHeld(true) - resumeAudio() - if (eng.playing) { - leaveLoopFor(cuePoint) - eng.pause() - eng.seek(cuePoint) - setPlaying(false) - setCurrent(cuePoint) - } else if (Math.abs(current - cuePoint) > 0.03) { - // 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 { - leaveLoopFor(cuePoint) - beginPreview('cue', cuePoint) - } - } - const onCueRelease = () => { - setCueHeld(false) - endPreview('cue') - } - - // A key-up that happens in another window never arrives, which would leave - // the button looking held; letting go of the window lets go of CUE. - useEffect(() => { - const letGo = () => setCueHeld(false) - window.addEventListener('blur', letGo) - return () => window.removeEventListener('blur', letGo) - }, []) - - // A library row's play button bumps `playRequest`: load (if needed) + play. - // If the requested track is already loaded and ready, start immediately; - // otherwise flag it and the analysis effect starts playback once ready. - // Only a CHANGE is a request: the deck unmounts behind the library picker, - // and remounting with a non-zero count must not start playback. - const seenPlayRequestRef = useRef(playRequest) - useEffect(() => { - if (playRequest === seenPlayRequestRef.current) return - seenPlayRequestRef.current = playRequest - pendingPlayRef.current = true - const eng = playbackRef.current - if (loadedIdRef.current === trackId && eng?.ready) { - pendingPlayRef.current = false - resumeAudio() - eng.play() - setPlaying(true) - setCurrent(eng.getPosition()) - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [playRequest]) - - // Follow the Web Audio clock while playing (high-res → smooth playhead). - useEffect(() => { - if (!playing) return - let raf = 0 - const tick = () => { - const eng = playbackRef.current - if (eng) setCurrent(eng.getPosition()) - raf = requestAnimationFrame(tick) - } - raf = requestAnimationFrame(tick) - return () => cancelAnimationFrame(raf) - }, [playing]) - - const seek = (t: number) => { - const eng = playbackRef.current - if (!eng) return - eng.seek(t) - setCurrent(t) - } - - // A cue jump that lands outside the engaged loop leaves it, Traktor-style. - // Without this the loop stays in force and Web Audio, started past a loop's - // end, wraps straight back into it. A jump inside the loop keeps it. - const leaveLoopFor = (t: number) => { - if (!loopActive || !loopRegion) return - if (t >= loopRegion.start && t < loopRegion.end) return - playbackRef.current?.setLoopEnabled(false) - setLoopActive(false) - } - - // User-driven seek from the waveforms: navigating away drops the active loop - // (otherwise it would just pull playback back into the loop region). - const seekManual = (t: number) => { - if (loopActive) { - playbackRef.current?.setLoopEnabled(false) - setLoopActive(false) - } - seek(t) - } - - // ---- beat jump / loop move ------------------------------------------ - // ‹ › (and ←/→): an engaged loop MOVES by its own length and keeps playing, - // Traktor-style; otherwise the playhead jumps `beatSize` beats. Both preserve - // sub-beat phase (pure translation, no grid snap), and seek() keeps the - // play/pause state. - const moveOrJump = (dir: -1 | 1) => { - const eng = playbackRef.current - if (!eng || !eng.ready) return - if (loopActive && loopRegion) { - const { start, end } = loopRegion - // A beat loop moves in BEATS, so it lands right across a tempo change; a - // manual loop has no beat length, so it moves by its duration. - let nStart: number - let nEnd: number - if (activeBeats != null && grid) { - nStart = grid.advanceBeats(start, dir * activeBeats) - nEnd = grid.advanceBeats(nStart, activeBeats) - } else { - const len = end - start - nStart = start + dir * len - nEnd = nStart + len - } - if (nStart < 0 || nEnd > duration) return // would leave the track - // The playhead travels with the loop, keeping its place inside it. The - // NEW loop is set first: the engine's seek honours the loop in force, so - // seeking first would wrap the playhead straight back into the old one. - const pos = eng.getPosition() + (nStart - start) - engagLoop(nStart, nEnd, activeBeats) - eng.seek(Math.min(nEnd - 0.001, Math.max(nStart, pos))) - setCurrent(eng.getPosition()) - return - } - if (!grid) return - // Phase is preserved in BEAT space, so a jump crossing a tempo change lands - // on the musically right beat rather than a fixed number of seconds away. - eng.seek(grid.advanceBeats(eng.getPosition(), dir * beatSize)) - setCurrent(eng.getPosition()) - } - - // ---- scratch ---------------------------------------------------------- - // The deck scratches itself: a drag moves its position, a release coasts to - // playing speed (or a stop) and it plays on BY ITSELF, then says so — so - // there is no gap between the coast and the music. React only mirrors it. - const scratchDone = (resumed: boolean) => { - cancelAnimationFrame(scratchRafRef.current) - engagedRef.current = false - const eng = playbackRef.current - if (eng) setCurrent(eng.getPosition()) - setPlaying(resumed) - } - const scratchDoneRef = useRef(scratchDone) - scratchDoneRef.current = scratchDone - - const runScratchRaf = () => { - cancelAnimationFrame(scratchRafRef.current) - const tick = () => { - const eng = playbackRef.current - if (!eng || !engagedRef.current) return - setCurrent(eng.getPosition()) - scratchRafRef.current = requestAnimationFrame(tick) - } - scratchRafRef.current = requestAnimationFrame(tick) - } - - const onScratchStart = () => { - const eng = playbackRef.current - if (!eng || !eng.ready) return - if (engagedRef.current) { - eng.scratchRegrab() // re-grab while coasting - return - } - wasPlayingRef.current = eng.playing - if (eng.playing) setPlaying(false) - eng.scratchBegin(eng.getPosition()) - engagedRef.current = true - runScratchRaf() - } - - const onScratchMove = (t: number) => { - if (engagedRef.current) playbackRef.current?.scratchTarget(t) - else setCurrent(t) - } - - const onScratchEnd = (t: number) => { - if (engagedRef.current) { - playbackRef.current?.scratchRelease(wasPlayingRef.current) - } else { - seek(t) - if (wasPlayingRef.current) { - playbackRef.current?.play() - setPlaying(true) - } - } - } - - // ---- loops ------------------------------------------------------------ - // Snap a time to the nearest beat when Snap is on (needs a beatgrid). - const snapTime = (t: number) => (snap && grid ? grid.snapToBeat(t) : Math.max(0, t)) - - const engagLoop = (start: number, end: number, beats: number | null) => { - const eng = playbackRef.current - if (!eng) return - eng.setLoop(start, end, true) - setLoopRegion({ start, end }) - setLoopActive(true) - setActiveBeats(beats) - setLoopInPoint(null) - setCurrent(eng.getPosition()) - } - - const setBeatLoop = (beats: number) => { - const eng = playbackRef.current - if (!eng || !eng.ready || !grid) return - // Pressing the size of the loop that's already playing disables it. - if (loopActive && activeBeats === beats) { - eng.setLoopEnabled(false) - setLoopActive(false) - setCurrent(eng.getPosition()) - return - } - // Resizing an active loop keeps its start locked; only a fresh loop starts - // at the current playhead. - const start = loopActive && loopRegion ? loopRegion.start : snapTime(eng.getPosition()) - // Measured from the loop's own start, so a loop spanning a tempo change - // still covers the right number of beats. - engagLoop(start, grid.advanceBeats(start, beats), beats) - } - - const loopIn = () => { - const eng = playbackRef.current - if (!eng || !eng.ready) return - setLoopInPoint(snapTime(eng.getPosition())) - } - - const loopOut = () => { - const eng = playbackRef.current - if (!eng || !eng.ready) return - const end = snapTime(eng.getPosition()) - // OUT closes an armed IN — or, with a manual loop already set, moves that - // loop's end to the playhead (Traktor's loop-out adjust). - const start = loopInPoint ?? (loopRegion && activeBeats == null ? loopRegion.start : null) - if (start == null || end <= start) return - engagLoop(start, end, null) - } - - const toggleLoop = () => { - const eng = playbackRef.current - if (!eng || !loopRegion) return - const next = !loopActive - eng.setLoopEnabled(next) - setLoopActive(next) - setCurrent(eng.getPosition()) - } - - // − / + (and Cmd/Ctrl+↓/↑): step the shared size, clamping at both ends. An - // engaged BEAT loop follows it — halving or doubling from its locked start, as - // on a CDJ. A manual loop has no beat size to follow, so it is left alone. - const stepSize = (dir: -1 | 1) => { - const i = LOOP_SIZES.indexOf(beatSize) - const next = LOOP_SIZES[Math.min(LOOP_SIZES.length - 1, Math.max(0, (i < 0 ? 7 : i) + dir))] - if (next === beatSize) return - setBeatSize(next) - if (loopActive && activeBeats != null) setBeatLoop(next) - } - - // The size key: a loop of exactly this size switches off; otherwise a loop of - // this size is set (from the playhead, or resized from an engaged loop's start). - const toggleSizeLoop = () => setBeatLoop(beatSize) - - // A beat loop's end is stored in seconds, so editing the tempo of a marker - // inside it would silently detune it. Re-derive the end whenever the grid - // changes, keeping the loop the beat length the user actually asked for. - useEffect(() => { - if (!grid || !loopActive || !loopRegion || activeBeats == null) return - const end = grid.advanceBeats(loopRegion.start, activeBeats) - if (Math.abs(end - loopRegion.end) < 1e-4) return - playbackRef.current?.setLoop(loopRegion.start, end, true) - setLoopRegion({ start: loopRegion.start, end }) - // Only the grid should trigger this; loop state changes set the end already. - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [grid]) - - // ---- capability-derived ----------------------------------------------- - const caps = useCaps() - // Edits are guarded at the point of INTENT, so the user gets the reason - // instead of a 422 from the adapter's backstop, and the deck stays fully - // usable for listening — which is most of its value on a library you cannot - // write. - // - // TWO gates, not one. `writable` is the library; `cues.editable` and - // `grid.editable` are the features. A platform can be writable overall while - // its cue or grid store is not implemented yet, so gating only on `writable` - // would re-expose these controls the moment that flips — which is exactly how - // this would regress unnoticed when Rekordbox writes land. - const canEditCues = caps.writable && caps.cues.editable - const canEditGrid = caps.writable && caps.grid.editable - const refuseCueEdit = () => { - if (canEditCues) return false - onError?.( - readOnlyNotice(caps) ?? `Cues cannot be edited on ${caps.save.app_name} libraries yet.`, - ) - return true - } - const refuseGridEdit = () => { - if (canEditGrid) return false - onError?.( - readOnlyNotice(caps) ?? - `The beatgrid cannot be edited on ${caps.save.app_name} libraries yet.`, - ) - return true - } - const slotCount = caps.cues.hotcue_slots - const slotLabel = slotLabeller(caps) - // Loops are a cue TYPE on some platforms and a separate bank on others; the - // dropdown only ever offers the point types. - const pointCueTypes = caps.cues.types.filter((t) => t !== 'loop') - - // ---- hotcues ---------------------------------------------------------- - const hotcueAt = (slot: number) => - cueData?.cues.find((c) => c.role === 'hotcue' && c.slot === slot) ?? null - - const applyCueEdit = (fresh: TrackCues) => { - setCueData(fresh) - invalidateTrackLists(qc) - } - - // Map a loop length (seconds) back to a preset beat count for the size - // highlight, snapping to the nearest preset when it's close (float tolerance). - // Needs the start as well as the length: under a marker list a duration - // alone no longer identifies a beat count. - const beatsForLoop = (start: number, length: number): number | null => { - if (!grid) return null - const beats = grid.beatsBetween(start, start + length) - let best: number | null = null - let bestDiff = Infinity - for (const s of LOOP_SIZES) { - const diff = Math.abs(s - beats) - if (diff < bestDiff) { - bestDiff = diff - best = s - } - } - return best != null && bestDiff <= beats * 0.05 ? best : null - } - - // 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 - const cue = hotcueAt(slot) - if (!cue) { - if (refuseCueEdit()) return - try { - // Setting a hotcue while a loop is active stores it as a loop hotcue. - if (loopActive && loopRegion) { - const len = loopRegion.end - loopRegion.start - applyCueEdit(await api.createCue(track.id, slot, loopRegion.start, 'loop', len)) - } else { - const t = snapTime(playbackRef.current?.getPosition() ?? current) - applyCueEdit(await api.createCue(track.id, slot, t, 'cue')) - } - setSelectedSlot(slot) - } catch (e) { - onError?.((e as Error).message) - } - return - } - setSelectedSlot(slot) - const eng = playbackRef.current - if (!eng || !eng.ready) return - resumeAudio() - enterCue(cue) - seek(cue.start) - // Paused at press → momentary preview: play while held. - if (!eng.playing) beginPreview(`hc:${slot}`, cue.start) - } - - // Before jumping to a cue: a loop cue engages a loop of its length — the new - // loop FIRST, so the seek is not wrapped back into the old one. The loop - // controls follow it: a beat-sized loop takes over the shared size (so −/+ - // resize THIS loop), and one of no preset size shows in MAN, where its toggle - // is. A point cue outside an engaged loop leaves it. - const enterCue = (cue: CuePoint) => { - if (cue.type === 'loop' && cue.length > 0) { - const beats = beatsForLoop(cue.start, cue.length) - engagLoop(cue.start, cue.start + cue.length, beats) - if (beats != null) { - setBeatSize(beats) - setLoopMode('beat') - } else { - setLoopMode('manual') - } - } else { - leaveLoopFor(cue.start) - } - } - - // Hotcue slot released (mouse/touch up, or key up). Ends its momentary preview. - const onSlotRelease = (slot: number) => endPreview(`hc:${slot}`) - - // Auto Hotcues: the button opens the slot-template dialog; the backend finds - // the track's structure on its beatgrid and places the template. Requires a - // beatgrid (button disabled otherwise) — every event is a bar on it. - const canAutoCue = grid != null - const [autoOpen, setAutoOpen] = useState(false) - const openAutoHotcues = () => { - if (!track || !canAutoCue) return - if (refuseCueEdit()) return - setAutoOpen(true) - } - const existingHotcues = useMemo(() => { - const m = new Map() - for (const c of cueData?.cues ?? []) if (c.role === 'hotcue' && c.slot != null) m.set(c.slot, c) - return m - }, [cueData]) - const autoHotcuesDone = (result: AutoHotcuesResult) => { - applyCueEdit(result.cues) - const by = (st: string) => result.outcomes.filter((o) => o.status === st) - const placed = by('placed').length - const missing = [...by('not_found'), ...by('out_of_range')].map((o) => eventLabel(o.event)) - const kept = by('occupied').length + by('protected').length - const dupes = by('duplicate').map( - (o) => `${slotLabel(o.slot)} (same beat as ${slotLabel(o.duplicate_of ?? o.slot)})`, - ) - const parts = [`Placed ${placed} hotcue${placed === 1 ? '' : 's'}`] - if (missing.length) parts.push(`not in this track: ${missing.join(', ')}`) - if (dupes.length) parts.push(`skipped ${dupes.join(', ')}`) - if (kept) parts.push(`${kept} occupied slot${kept === 1 ? '' : 's'} kept`) - onNotify?.(placed ? 'success' : 'error', parts.join(' · ')) - } - - const setSlotType = async (slot: number, type: CueType) => { - if (!track) return - if (refuseCueEdit()) return - try { - applyCueEdit(await api.setCueType(track.id, slot, type)) - } catch (e) { - onError?.((e as Error).message) - } - } - - // Renaming re-sets the slot with its own position, type and length plus the - // new name — the adapter's set_cue replaces a slot, and keeps a name only - // when called without one. An empty name clears it. - const renameSlot = async (slot: number, name: string) => { - setRenamingSlot(null) - const cue = hotcueAt(slot) - if (!track || !cue) return - if (refuseCueEdit()) return - try { - applyCueEdit(await api.createCue(track.id, slot, cue.start, cue.type, cue.length, name)) - } catch (e) { - onError?.((e as Error).message) - } - } - - // Only a fixed palette is offered: a platform with free RGB would need a - // picker of its own, and none that stores free RGB is writable yet. - const cuePalette = caps.cues.color === 'palette' ? caps.cues.palette : [] - const setSlotColor = async (slot: number, color: string | null) => { - if (!track) return - if (refuseCueEdit()) return - try { - applyCueEdit(await api.setCueColor(track.id, slot, color)) - } catch (e) { - onError?.((e as Error).message) - } - } - - const deleteHotcueSlot = async (slot: number) => { - const existing = hotcueAt(slot) - if (!track || !existing) return // nothing to remove in an empty slot - if (!existing.editable) return // the adapter would refuse it - if (refuseCueEdit()) return - try { - applyCueEdit(await api.deleteCue(track.id, slot)) - if (selectedSlot === slot) setSelectedSlot(null) - } catch (e) { - onError?.((e as Error).message) - } - } - - // ---- memory cues -------------------------------------------------------- - // Cues on no pad, listed in time order (MemoryCueStrip). Calling one is - // CDJ-style: jump there and make it the CUE point — paused stays paused, - // playing keeps playing; a memory loop also engages (decided 2026-10-16). - const showMemory = caps.cues.memory_cues && bankMode === 'memory' - const memoryPalette = caps.cues.memory_color === 'palette' ? caps.cues.memory_palette : [] - const memoryCount = cueData?.cues.filter((c) => c.role === 'memory' && c.editable).length ?? 0 - const memoryCap = caps.cues.max_memory_cues - const memoryAddRefusal = !canEditCues - ? (readOnlyNotice(caps) ?? `Cues cannot be edited on ${caps.save.app_name} libraries yet.`) - : memoryCap != null && memoryCount >= memoryCap - ? `${caps.save.app_name} keeps at most ${memoryCap} memory cues per track` - : null - - const callMemoryCue = (cue: CuePoint) => { - const eng = playbackRef.current - if (!eng || !eng.ready) return - resumeAudio() - enterCue(cue) - seek(cue.start) - setCuePoint(cue.start) - } - - // + Memory: at the (snapped) playhead, or the engaged loop as a memory loop. - const addMemoryCue = async () => { - if (!track) return - if (memoryAddRefusal) { - onError?.(memoryAddRefusal) - return - } - try { - if (loopActive && loopRegion) { - const len = loopRegion.end - loopRegion.start - applyCueEdit(await api.addMemoryCue(track.id, loopRegion.start, 'loop', len)) - } else { - const t = snapTime(playbackRef.current?.getPosition() ?? current) - applyCueEdit(await api.addMemoryCue(track.id, t, 'cue')) - } - } catch (e) { - onError?.((e as Error).message) - } - } - - const editMemoryCue = async (edit: () => Promise) => { - if (!track || refuseCueEdit()) return - try { - applyCueEdit(await edit()) - } catch (e) { - onError?.((e as Error).message) - } - } - - const memoryMenuItems = (cue: CuePoint): MenuItem[] => { - const id = cue.id - const locked = !cue.editable || id == null - const items: MenuItem[] = [] - // Its type as one-click choices, as on a pad — where the platform has more - // than one point type (Traktor); a loop stays a loop. - if (cue.type === 'loop') { - items.push({ heading: 'Memory loop' }) - } else if (pointCueTypes.length > 1) { - items.push({ heading: 'Memory cue · Type' }) - for (const t of pointCueTypes) { - const color = cueTypeColor(t) - items.push({ - label: CUE_TYPE_LABELS[t], - icon: ( - - ), - hint: t === cue.type ? '✓' : undefined, - disabled: locked, - onClick: () => - t !== cue.type && - id != null && - void editMemoryCue(() => api.setMemoryCueType(track!.id, id, t)), - }) - } - } else { - items.push({ heading: 'Memory cue' }) - } - if (memoryPalette.length > 0) { - items.push({ separator: true }) - items.push({ heading: 'Colour' }) - items.push({ - swatches: memoryPalette, - selected: cue.color, - disabled: locked, - onPick: (color) => - color !== cue.color && - id != null && - void editMemoryCue(() => api.setMemoryCueColor(track!.id, id, color)), - }) - } - if (locked) items.push({ heading: 'Managed by the DJ app — not editable here' }) - items.push({ separator: true }) - items.push({ - label: 'Rename…', - disabled: locked, - onClick: () => !refuseCueEdit() && setRenamingMemory(id), - }) - items.push({ - label: 'Delete', - danger: true, - disabled: locked, - onClick: () => id != null && void editMemoryCue(() => api.deleteMemoryCue(track!.id, id)), - }) - return items - } - - const renameMemoryCue = (cue: CuePoint, name: string) => { - setRenamingMemory(null) - if (cue.id == null) return - const id = cue.id - void editMemoryCue(() => api.renameMemoryCue(track!.id, id, name || null)) - } - - // ---- beatgrid --------------------------------------------------------- - // Which marker the grid controls act on is derived from the playhead — there - // is no separate selection state to keep in sync. - const activeMarkerIndex = grid ? grid.markerIndexAt(current) : -1 - const activeMarker = activeMarkerIndex >= 0 ? grid!.markers[activeMarkerIndex] : null - // ~4px of the current zoom, so "on the marker" stays usable at 2s and 64s/view. - // ~4 px of the view either side, so "at a marker" stays a marker-sized - // target at deep zoom instead of 4% of a 0.25 s view. - const markerHitSec = Math.max(0.001, secPerView / 250) - const atMarker = !!grid && grid.isOnMarker(current, markerHitSec) - const beforeFirstMarker = !!grid && current < grid.markers[0].start - GRID_EPS - - const playheadNow = () => playbackRef.current?.getPosition() ?? current - - const editMarker = async (index: number, patch: { bpm?: number; start?: number }) => { - if (!track || index < 0) return - if (refuseGridEdit()) return - try { - applyCueEdit(await api.setGridMarker(track.id, index, patch)) - } catch (e) { - onError?.((e as Error).message) - } - } - const setBpm = (bpm: number) => - editMarker(activeMarkerIndex, { bpm: Math.round(bpm * 1000) / 1000 }) - const nudgeBpm = (delta: number) => activeMarker && setBpm(activeMarker.bpm + delta) - // /2 and x2 retempo the governing marker only, like every other tempo control - // in this panel — a section can be octave-wrong on its own. - const halveBpm = () => activeMarker && setBpm(activeMarker.bpm / 2) - const doubleBpm = () => activeMarker && setBpm(activeMarker.bpm * 2) - // A beat is the governing marker's own beat: moving a marker by whole beats - // shifts which beat is the downbeat without changing the phase. - const nudgeGridBeats = (beats: number) => { - if (activeMarker) nudgeGrid((beats * 60000) / activeMarker.bpm) - } - const nudgeGrid = (deltaMs: number) => { - if (!activeMarker) return - editMarker(activeMarkerIndex, { start: Math.max(0, activeMarker.start + deltaMs / 1000) }) - } - // With no grid, a tempo from TAP or the typed readout CREATES one, with its - // first marker at an anchor captured when the gesture began: the first tap - // (a beat, when tapping along), or where the playhead stood when Set grid - // asked for a BPM. The Grid-mode nudges fix the phase. While that create is - // in flight, later tempos wait for it rather than creating a second grid. - const tapAnchorRef = useRef(0) - const gridAnchorRef = useRef(null) // Set grid's, while the readout asks - const gridCreateRef = useRef | null>(null) - const [bpmEntryRequest, setBpmEntryRequest] = useState(0) // bump → readout opens for typing - const tapStart = () => { - tapAnchorRef.current = playheadNow() - } - const commitBpm = async (bpm: number, anchor: number) => { - if (!track) return - // This closure predates the grid it is waiting for, so it cannot use - // activeMarkerIndex — but a grid created here has exactly one marker. - if (gridCreateRef.current) { - if (await gridCreateRef.current) editMarker(0, { bpm }) - return - } - if (markerCount > 0) return setBpm(bpm) - if (refuseGridEdit()) return - const create = api - .addGridMarker(track.id, anchor, bpm) - .then((cues) => (applyCueEdit(cues), true)) - .catch((e) => (onError?.((e as Error).message), false)) - .finally(() => (gridCreateRef.current = null)) - gridCreateRef.current = create - await create - } - // Adds a marker at the playhead — and creates the grid when there is none. - // Uses the raw playhead, not snapTime: a marker defines where beats are. - // A first marker needs a tempo: the track's own BPM when it has one (passed - // explicitly — Rekordbox does not fall back to it), else the readout opens - // to ask, and the marker lands where the playhead is NOW once it is typed. - const addMarkerHere = async () => { - if (!track) return - if (refuseGridEdit()) return - if (markerCount === 0 && !track.bpm) { - gridAnchorRef.current = playheadNow() - setBpmEntryRequest((n) => n + 1) - return - } - try { - applyCueEdit( - await api.addGridMarker(track.id, playheadNow(), markerCount === 0 ? (track.bpm ?? undefined) : undefined), - ) - } catch (e) { - onError?.((e as Error).message) - } - } - const deleteMarkerHere = async () => { - if (!track || activeMarkerIndex < 0) return - if (refuseGridEdit()) return - try { - applyCueEdit(await api.deleteGridMarker(track.id, activeMarkerIndex)) - } catch (e) { - onError?.((e as Error).message) - } - } - // Seeking between markers IS how the user changes which marker is active. - const seekToMarker = (index: number) => { - if (!grid || index < 0 || index >= grid.count) return - seekManual(grid.markers[index].start) - } - const prevMarker = () => seekToMarker(grid ? grid.prevMarkerIndex(current, markerHitSec) : -1) - const nextMarker = () => seekToMarker(grid ? grid.nextMarkerIndex(current, markerHitSec) : -1) - const resetGrid = async () => { - const o = originalGridRef.current - if (!track || !o) return - if (refuseGridEdit()) return - try { - applyCueEdit(await api.replaceGridMarkers(track.id, o)) - } catch (e) { - onError?.((e as Error).message) - } - } - const toggleLock = async () => { - if (!track) return - if (refuseGridEdit()) return - try { - applyCueEdit(await api.setGridLock(track.id, !cueData?.grid_locked)) - } catch (e) { - onError?.((e as Error).message) - } - } - const deleteGrid = async () => { - if (!track) return - if (refuseGridEdit()) return - try { - applyCueEdit(await api.deleteGrid(track.id)) - } catch (e) { - onError?.((e as Error).message) - } - } - // Analyze: the backend detects BPM + first beat (setting the grid anchor and, - // on Traktor, hotcue 1) and the key where the library stores one. The key - // lands on the track: `applyCueEdit` refetches the lists, and App hands the - // deck the fresh copy. - const runAnalyzeGrid = async () => { - if (!track || gridBusy) return - setGridBusy(true) - try { - if (canEditGrid) { - applyCueEdit(await api.autoGrid(track.id)) - onNotify?.('success', caps.tracks.key_writable - ? 'Analyzed — set BPM, grid and key, measured loudness' - : 'Analyzed — set BPM and grid, measured loudness') - } else { - // Nothing here can take a grid (a read-only library, a device, a loose - // file) — but loudness goes to Konduktor's own store, so measure that. - const l = await api.measureLoudness(origin, track.id) - onNotify?.('success', `Measured loudness — ${formatLoudness(l.lufs, 'LUFS')}, ${formatLoudness(l.true_peak, 'dBTP', true)}`) - } - qc.invalidateQueries({ queryKey: ['loudness'] }) - } catch (e) { - onError?.((e as Error).message) - } finally { - setGridBusy(false) - } - } - - // ---- keyboard shortcuts ---------------------------------------------- - // Space → play/pause; 1–8 → the matching hotcue slot; Shift+1–8 → delete it. - // ←/→ → beat jump, or move an engaged loop; Shift+←/→ → step between grid - // markers; Cmd/Ctrl+↑/↓ → the loop/jump size, resizing an engaged beat loop - // (Shift+↑/↓ is the track table's extend-selection). - // 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. - // ---- stems: mute / solo ------------------------------------------------------ - // What each stem plays at: a muted stem is 0. Both engines follow it (the - // scratch engine re-sums). - const stemLevels = useMemo(() => (stems ?? []).map((_, k) => (stemMuted[k] ? 0 : 1)), [stems, stemMuted]) - const stemLevelKey = stemLevels.join(',') - useEffect(() => { - if (!stems) return - playbackRef.current?.setGains(stemLevels) - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [stemLevelKey]) - // Solo = "mute every stem but this one". Soloing a stem that is already the - // only one playing brings them all back (so solo twice undoes it). - const toggleStem = (k: number, solo: boolean) => { - if (!stems || k >= stems.length) return - setStemMuted((m) => { - if (!solo) return stems.map((_, i) => (i === k ? !m[i] : !!m[i])) - const alone = stems.every((_, i) => (i === k ? !m[i] : !!m[i])) - return stems.map((_, i) => (alone ? false : i !== k)) - }) - } - // The ring on a lane button: that stem is the only one playing. - const soloed = stemLevels.filter((l) => l > 0).length === 1 && stemLevels.length > 1 - ? stemLevels.findIndex((l) => l > 0) - : null - const lanes: StemLane[] | null = - stems && stemLanes - ? stems.map((st, k) => ({ - data: stemLanes[k], - raw: stemRaws?.[k] ?? null, - name: st.name, - color: st.color, - audible: stemLevels[k] > 0, - solo: soloed === k, - shortcut: STEM_KEYS[k] ? STEM_KEYS[k].slice(3) : '', - })) - : null - - const shortcutsRef = useRef({ - toggle, - onSlotPress, - onSlotRelease, - deleteHotcueSlot, - onCuePress, - onCueRelease, - moveOrJump, - stepSize, - prevMarker, - nextMarker, - slotCount, - toggleStem, - }) - shortcutsRef.current = { - toggle, - onSlotPress, - onSlotRelease, - deleteHotcueSlot, - onCuePress, - onCueRelease, - moveOrJump, - stepSize, - prevMarker, - nextMarker, - slotCount, - toggleStem, - } - useEffect(() => { - const onKey = (e: KeyboardEvent) => { - // Ignore while typing in a field or with a non-Shift modifier held. - const t = e.target as HTMLElement | null - if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable)) return - // Cmd (macOS) / Ctrl (Windows, Linux), either accepted as Cmd/Ctrl+A is. - // Repeats allowed: holding it sweeps through the sizes. - if ((e.metaKey || e.ctrlKey) && !e.altKey && !e.shiftKey && - (e.code === 'ArrowUp' || e.code === 'ArrowDown')) { - e.preventDefault() - shortcutsRef.current.stepSize(e.code === 'ArrowUp' ? 1 : -1) - 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') { - e.preventDefault() - shortcutsRef.current.toggle() - return - } - if (e.code === 'KeyC') { - e.preventDefault() - shortcutsRef.current.onCuePress() - return - } - // Q W E R: mute a stem; Shift+ solos it (a stem track only — elsewhere - // there is nothing to toggle, and the keys stay free). - const stemKey = STEM_KEYS.indexOf(e.code) - if (stemKey >= 0) { - e.preventDefault() - shortcutsRef.current.toggleStem(stemKey, e.shiftKey) - return - } - if (e.code === 'ArrowLeft') { - e.preventDefault() - if (e.shiftKey) shortcutsRef.current.prevMarker() - else shortcutsRef.current.moveOrJump(-1) - return - } - if (e.code === 'ArrowRight') { - e.preventDefault() - if (e.shiftKey) shortcutsRef.current.nextMarker() - else shortcutsRef.current.moveOrJump(1) - return - } - const slot = slotForDigit(e.code, shortcutsRef.current.slotCount) - if (slot != null) { - e.preventDefault() - if (e.shiftKey) void shortcutsRef.current.deleteHotcueSlot(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 slot = slotForDigit(e.code, shortcutsRef.current.slotCount) - if (slot != null) shortcutsRef.current.onSlotRelease(slot) - } - window.addEventListener('keydown', onKey) - window.addEventListener('keyup', onKeyRelease) - return () => { - window.removeEventListener('keydown', onKey) - window.removeEventListener('keyup', onKeyRelease) - } - }, []) - - const showWaves = track && cols && waveStatus === 'ready' - const activeLoop = loopActive && loopRegion ? loopRegion : null - const markerCount = grid?.count ?? 0 - const subtitle = track - ? [track.artist ?? 'Unknown artist', track.album, track.genre].filter(Boolean).join(' · ') - : '' - - // The pad's right-click menu: its type (as one-click choices, the current one - // ticked), its colour where the platform has a palette, Rename, Delete. A cue the adapter will not edit gets the same menu - // disabled, so the reason is visible instead of the menu silently not opening. - const padMenuItems = (slot: number): MenuItem[] => { - const cue = hotcueAt(slot) - if (!cue) return [] - const locked = !cue.editable - const items: MenuItem[] = [] - if (cue.type === 'loop') { - items.push({ heading: `Hotcue ${slotLabel(slot)} · Loop` }) - } else if (pointCueTypes.length > 1) { - items.push({ heading: `Hotcue ${slotLabel(slot)} · Type` }) - for (const t of pointCueTypes) { - const color = cueTypeColor(t) - items.push({ - label: CUE_TYPE_LABELS[t], - icon: ( - - ), - hint: t === cue.type ? '✓' : undefined, - disabled: locked, - onClick: () => t !== cue.type && void setSlotType(slot, t), - }) - } - } else { - items.push({ heading: `Hotcue ${slotLabel(slot)}` }) - } - if (cuePalette.length > 0) { - items.push({ separator: true }) - items.push({ heading: 'Colour' }) - items.push({ - swatches: cuePalette, - selected: cue.color, - disabled: locked, - onPick: (color) => color !== cue.color && void setSlotColor(slot, color), - }) - } - if (locked) items.push({ heading: 'Managed by the DJ app — not editable here' }) - items.push({ separator: true }) - items.push({ - label: 'Rename…', - disabled: locked, - onClick: () => !refuseCueEdit() && setRenamingSlot(slot), - }) - items.push({ - label: 'Delete', - danger: true, - disabled: locked, - onClick: () => void deleteHotcueSlot(slot), - }) - return items - } - - const DIVIDER = - // Rendered twice — inline on a wide row, inside TempoFold on a narrow one. - const tempoProps = { - bpm: activeMarker?.bpm ?? null, - locked: cueData?.grid_locked ?? false, - lockable: caps.grid.lockable, - flexible: markerCount > 1, - markerIndex: activeMarkerIndex, - onTapStart: tapStart, - onTapBpm: (bpm: number) => commitBpm(bpm, tapAnchorRef.current), - onNudgeBpm: nudgeBpm, - onHalve: halveBpm, - onDouble: doubleBpm, - onToggleLock: toggleLock, - } - - return ( -
- {/* ---- Header: the track, its readouts, the analysis actions ---- */} -
- setInfoOpen(true) : undefined} - /> -
- {track ? ( - <> -
- - {track.title ?? 'Untitled'} - - {/* The deck stays usable for listening on a read-only library, - so say which it is rather than leaving the edit controls - looking live. The controls report the reason when pressed. */} - {readOnlyShort(caps) && ( - - {readOnlyShort(caps)} - - )} -
-
- {subtitle} -
- - ) : ( -
No track loaded
- )} -
- -
-
- ELAPSED - {fmtTime(current, timeDecimals)} -
- -
- REMAIN - −{fmtTime(Math.max(0, duration - current), timeDecimals)} -
- -
- KEY - - {track?.key || '—'} - -
- - commitBpm(bpm, gridAnchorRef.current ?? playheadNow())} - onClose={() => (gridAnchorRef.current = null)} - openRequest={bpmEntryRequest} - /> -
- - - - -
- - {/* ---- Waveforms ---- */} - {!track ? ( -
- Load a track to prep it -
- ) : waveStatus === 'error' ? ( -
- Could not load audio — file may be missing or an unsupported format. -
- ) : ( - <> -
- {showWaves ? ( - - ) : ( -
- Analysing waveform… -
- )} -
-
- {showWaves && ( - - )} -
- - )} - - {/* ---- Controls: transport · loop/jump · pads or grid · tempo ---- - A container, so the row gives way in stages as the window narrows - instead of the pads sliding under the tempo group: the pads drop - their names first (each pad queries its own width, see HotcueBar), - then below 77rem the tempo group folds into one BPM button. That is - the row's measured width just before the pad bank would hit its - minimum (with a lockable grid's extra Lock button, and the Hot / Mem - toggle's ~3rem with its gap), so move it with the controls. */} -
- - - {DIVIDER} - stepSize(-1)} - onBigger={() => stepSize(1)} - loopActive={loopActive} - sizeLit={loopActive && activeBeats === beatSize} - onToggleSize={toggleSizeLoop} - canToggle={loopRegion != null} - inArmed={loopInPoint != null} - manualSet={loopRegion != null && activeBeats == null} - onLoopIn={loopIn} - onLoopOut={loopOut} - onToggleLoop={toggleLoop} - onMove={moveOrJump} - /> - {DIVIDER} - {caps.cues.memory_cues && !gridMode && ( - { - setBankMode(m) - setPadMenu(null) - setMemoryMenu(null) - setRenamingSlot(null) - setRenamingMemory(null) - }} - /> - )} - {gridMode ? ( - - ) : showMemory ? ( - setMemoryMenu({ cue, x, y })} - onAdd={() => void addMemoryCue()} - addRefusal={memoryAddRefusal} - renamingId={renamingMemory} - onRenameCommit={renameMemoryCue} - onRenameCancel={() => setRenamingMemory(null)} - /> - ) : ( - { - setSelectedSlot(slot) - if (hotcueAt(slot)) setPadMenu({ slot, x, y }) - }} - renamingSlot={renamingSlot} - onRenameCommit={renameSlot} - onRenameCancel={() => setRenamingSlot(null)} - /> - )} - {DIVIDER} - { - setGridMode(m === 'grid') - setPadMenu(null) - setRenamingSlot(null) - setMemoryMenu(null) - setRenamingMemory(null) - }} - /> - - - - -
- - {memoryMenu && ( - setMemoryMenu(null)} - /> - )} - {padMenu && ( - setPadMenu(null)} - /> - )} - {infoOpen && track && ( - setInfoOpen(false)} - onEditTags={ - onEditTags - ? () => { - setInfoOpen(false) - onEditTags(track) - } - : undefined - } - /> - )} - {autoOpen && track && ( - setAutoOpen(false)} - onDone={autoHotcuesDone} - onError={(msg) => onError?.(msg)} - /> - )} -
- ) + return { secPerView, setSecPerView, rawWave, setRawWave } } /** - * The loaded track's sleeve. Until it loads — and for a track with no art — a - * swatch of the ambient colours stands in, so the header never has a hole in it. - * With a track loaded it is a button that opens the track-info popup. + * The DJ-style "prep strip" across the top of the window: the track and its + * readouts, a scrolling zoomable main waveform above a whole-track overview, + * then transport, loop and hotcue controls. The deck itself — state, audio, + * every action — is `useDeck` (components/deck/); this lays it out and routes + * the keyboard to it. Playback, loops and scratch run in the deck's audio + * worklet (lib/audio/). */ -function CoverThumb({ trackId, onOpen }: { trackId: string | null; onOpen?: () => void }) { - const [failed, setFailed] = useState(null) - const showArt = trackId != null && failed !== trackId +export function PrepStrip(props: Props) { + const caps = useCaps() + const view = useDeckView() + const deck = useDeck({ ...props, caps, secPerView: view.secPerView }) + + const keys = useRef({ focused: null, other: null }) + keys.current = { focused: deck.shortcuts, other: null } + useDeckKeys(keys) + return ( - +
+ + + + + +
) } diff --git a/frontend/src/components/deck/DeckControls.tsx b/frontend/src/components/deck/DeckControls.tsx new file mode 100644 index 0000000..4f1ba64 --- /dev/null +++ b/frontend/src/components/deck/DeckControls.tsx @@ -0,0 +1,232 @@ +import { Icon } from '../../lib/icons' +import { GridEditStrip, TempoControls, TempoFold } from '../GridControls' +import { HotcueBar } from '../HotcueBar' +import { LoopControls } from '../LoopControls' +import { MemoryCueStrip } from '../MemoryCueStrip' +import { ModeSwitch } from '../ModeSwitch' +import type { Deck } from './useDeck' + +const DIVIDER = + +/** + * The deck's controls row: transport · loop/jump · pads or grid · tempo. + * + * A container, so the row gives way in stages as it narrows instead of the + * pads sliding under the tempo group: the pads drop their names first (each pad + * queries its own width, see HotcueBar), then below 77rem the tempo group folds + * into one BPM button. That is the row's measured width just before the pad + * bank would hit its minimum (with a lockable grid's extra Lock button, and the + * Hot / Mem toggle's ~3rem with its gap), so move it with the controls. + */ +export function DeckControls({ deck }: { deck: Deck }) { + const { + caps, + track, + toggle, + ready, + previewing, + playing, + onCuePress, + onCueRelease, + cueHeld, + loopMode, + setLoopMode, + grid, + beatSize, + stepSize, + loopActive, + activeBeats, + toggleSizeLoop, + loopRegion, + loopInPoint, + loopIn, + loopOut, + toggleLoop, + moveOrJump, + gridMode, + setGridMode, + bankMode, + setBankMode, + setPadMenu, + setMemoryMenu, + setRenamingSlot, + setRenamingMemory, + activeMarkerIndex, + markerCount, + activeMarker, + atMarker, + beforeFirstMarker, + originalGridRef, + prevMarker, + nextMarker, + nudgeGrid, + nudgeGridBeats, + addMarkerHere, + deleteMarkerHere, + deleteGrid, + resetGrid, + showMemory, + cueData, + cuePoint, + callMemoryCue, + addMemoryCue, + memoryAddRefusal, + renamingMemory, + renameMemoryCue, + slotCount, + slotLabel, + selectedSlot, + setSelectedSlot, + onSlotPress, + onSlotRelease, + hotcueAt, + renamingSlot, + renameSlot, + tempoProps, + } = deck + return ( +
+ + + {DIVIDER} + stepSize(-1)} + onBigger={() => stepSize(1)} + loopActive={loopActive} + sizeLit={loopActive && activeBeats === beatSize} + onToggleSize={toggleSizeLoop} + canToggle={loopRegion != null} + inArmed={loopInPoint != null} + manualSet={loopRegion != null && activeBeats == null} + onLoopIn={loopIn} + onLoopOut={loopOut} + onToggleLoop={toggleLoop} + onMove={moveOrJump} + /> + {DIVIDER} + {caps.cues.memory_cues && !gridMode && ( + { + setBankMode(m) + setPadMenu(null) + setMemoryMenu(null) + setRenamingSlot(null) + setRenamingMemory(null) + }} + /> + )} + {gridMode ? ( + + ) : showMemory ? ( + setMemoryMenu({ cue, x, y })} + onAdd={() => void addMemoryCue()} + addRefusal={memoryAddRefusal} + renamingId={renamingMemory} + onRenameCommit={renameMemoryCue} + onRenameCancel={() => setRenamingMemory(null)} + /> + ) : ( + { + setSelectedSlot(slot) + if (hotcueAt(slot)) setPadMenu({ slot, x, y }) + }} + renamingSlot={renamingSlot} + onRenameCommit={renameSlot} + onRenameCancel={() => setRenamingSlot(null)} + /> + )} + {DIVIDER} + { + setGridMode(m === 'grid') + setPadMenu(null) + setRenamingSlot(null) + setMemoryMenu(null) + setRenamingMemory(null) + }} + /> + + + + +
+ ) +} diff --git a/frontend/src/components/deck/DeckHeader.tsx b/frontend/src/components/deck/DeckHeader.tsx new file mode 100644 index 0000000..73c40da --- /dev/null +++ b/frontend/src/components/deck/DeckHeader.tsx @@ -0,0 +1,182 @@ +import { useState, type ReactNode } from 'react' +import { api } from '../../api' +import { keyColor } from '../../lib/format' +import { Icon } from '../../lib/icons' +import { readOnlyNotice, readOnlyShort } from '../../lib/platformCopy' +import { useAppSetting } from '../AppSettings' +import { BpmReadout } from '../GridControls' +import type { Deck } from './useDeck' + +/** mm:ss.f — tenths by default, as a CDJ does; `places` is the + * `timeReadoutDecimals` setting (0 drops the fraction). Truncates, never + * rounds, so the readout never shows a second before it has been reached. */ +function fmtTime(secs: number, places: number): string { + if (!isFinite(secs) || secs < 0) secs = 0 + const scale = 10 ** places + // The epsilon keeps e.g. 0.3 s from reading 0.299 at 3 places. + const units = Math.floor(secs * scale + 1e-6) + const whole = Math.floor(units / scale) + const m = Math.floor(whole / 60) + const s = whole % 60 + const frac = places > 0 ? `.${(units % scale).toString().padStart(places, '0')}` : '' + return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}${frac}` +} + +/** The deck's header: the track, its readouts, the analysis actions — and + * `actions`, anything the layout adds after them. */ +export function DeckHeader({ deck, actions }: { deck: Deck; actions?: ReactNode }) { + const { + caps, + track, + origin, + subtitle, + setInfoOpen, + current, + duration, + activeMarker, + canEditGrid, + commitBpm, + gridAnchorRef, + playheadNow, + bpmEntryRequest, + snap, + setSnap, + runAnalyzeGrid, + gridBusy, + openAutoHotcues, + canAutoCue, + } = deck + const timeDecimals = useAppSetting('timeReadoutDecimals') + return ( +
+ setInfoOpen(true) : undefined} + /> +
+ {track ? ( + <> +
+ + {track.title ?? 'Untitled'} + + {/* The deck stays usable for listening on a read-only library, + so say which it is rather than leaving the edit controls + looking live. The controls report the reason when pressed. */} + {readOnlyShort(caps) && ( + + {readOnlyShort(caps)} + + )} +
+
+ {subtitle} +
+ + ) : ( +
No track loaded
+ )} +
+ +
+
+ ELAPSED + {fmtTime(current, timeDecimals)} +
+ +
+ REMAIN + −{fmtTime(Math.max(0, duration - current), timeDecimals)} +
+ +
+ KEY + + {track?.key || '—'} + +
+ + commitBpm(bpm, gridAnchorRef.current ?? playheadNow())} + onClose={() => (gridAnchorRef.current = null)} + openRequest={bpmEntryRequest} + /> +
+ + + + + {actions} +
+ ) +} + +/** + * The loaded track's sleeve. Until it loads — and for a track with no art — a + * swatch of the ambient colours stands in, so the header never has a hole in it. + * With a track loaded it is a button that opens the track-info popup. + */ +function CoverThumb({ trackId, onOpen }: { trackId: string | null; onOpen?: () => void }) { + const [failed, setFailed] = useState(null) + const showArt = trackId != null && failed !== trackId + return ( + + ) +} diff --git a/frontend/src/components/deck/DeckOverlays.tsx b/frontend/src/components/deck/DeckOverlays.tsx new file mode 100644 index 0000000..6f5a3f0 --- /dev/null +++ b/frontend/src/components/deck/DeckOverlays.tsx @@ -0,0 +1,81 @@ +import { api } from '../../api' +import { CapabilitiesContext } from '../../lib/capabilities' +import { AutoCueDialog } from '../AutoCueDialog' +import { ContextMenu } from '../ContextMenu' +import { TrackInfoDialog } from '../TrackInfoDialog' +import type { Deck } from './useDeck' + +/** The deck's menus and dialogs (all portalled, so where this renders does not + * matter). Under the deck's OWN capabilities: the track-info dialog reads + * them from context. */ +export function DeckOverlays({ deck }: { deck: Deck }) { + const { + caps, + track, + origin, + onError, + onEditTags, + memoryMenu, + setMemoryMenu, + memoryMenuItems, + padMenu, + setPadMenu, + padMenuItems, + infoOpen, + setInfoOpen, + autoOpen, + setAutoOpen, + slotCount, + slotLabel, + existingHotcues, + autoHotcuesDone, + } = deck + return ( + + {memoryMenu && ( + setMemoryMenu(null)} + /> + )} + {padMenu && ( + setPadMenu(null)} + /> + )} + {infoOpen && track && ( + setInfoOpen(false)} + onEditTags={ + onEditTags + ? () => { + setInfoOpen(false) + onEditTags(track) + } + : undefined + } + /> + )} + {autoOpen && track && ( + setAutoOpen(false)} + onDone={autoHotcuesDone} + onError={(msg) => onError?.(msg)} + /> + )} + + ) +} diff --git a/frontend/src/components/deck/DeckWaves.tsx b/frontend/src/components/deck/DeckWaves.tsx new file mode 100644 index 0000000..af8f6e0 --- /dev/null +++ b/frontend/src/components/deck/DeckWaves.tsx @@ -0,0 +1,112 @@ +import { MainWaveform } from '../MainWaveform' +import { OverviewWaveform } from '../OverviewWaveform' +import type { Deck } from './useDeck' + +/** The main view's zoom and raw/bars toggle — shared by every deck's lane + * (so beats line up between them), owned by the deck area. */ +export interface DeckView { + secPerView: number + setSecPerView: (sec: number) => void + rawWave: boolean + setRawWave: (raw: boolean) => void +} + +/** The scrolling main waveform, in its well — or what stands in for it: no + * track, a load error, the analysis running. */ +export function DeckMainWave({ deck, view, empty = 'Load a track to prep it' }: { deck: Deck; view: DeckView; empty?: string }) { + const { + track, + waveStatus, + showWaves, + cols, + raw, + current, + duration, + cueData, + cuePoint, + grid, + activeMarkerIndex, + gridMode, + activeLoop, + loopActive, + loopRegion, + loopInPoint, + seekManual, + onScratchStart, + onScratchMove, + onScratchEnd, + lanes, + toggleStem, + } = deck + if (!track) { + return ( +
+ {empty} +
+ ) + } + if (waveStatus === 'error') { + return ( +
+ Could not load audio — file may be missing or an unsupported format. +
+ ) + } + return ( +
+ {showWaves && cols ? ( + + ) : ( +
+ Analysing waveform… +
+ )} +
+ ) +} + +/** The whole-track overview, framing the main view's range. Nothing without a + * playable track (the main well says why). */ +export function DeckOverview({ deck, view }: { deck: Deck; view: DeckView }) { + const { track, waveStatus, showWaves, cols, current, duration, cueData, cuePoint, activeLoop, seekManual } = deck + if (!track || waveStatus === 'error') return null + return ( +
+ {showWaves && cols && ( + + )} +
+ ) +} diff --git a/frontend/src/components/deck/useDeck.tsx b/frontend/src/components/deck/useDeck.tsx new file mode 100644 index 0000000..c4e257a --- /dev/null +++ b/frontend/src/components/deck/useDeck.tsx @@ -0,0 +1,1412 @@ +// One deck's state and behaviour: audio engine, transport, CUE, hotcues, +// memory cues, loops, beatgrid editing, stems and analysis — everything the +// prep deck DOES, without how it looks. Moved out of PrepStrip (2026-11-02, +// performance mode phase 2) so a second deck is a second call: each deck gets +// its own track, origin and capabilities, and the components in this folder +// render whichever deck they are handed. +// +// Capabilities arrive as a parameter, not from context: in performance mode +// each deck may hold a track from a different library (a device's, a loose +// file), and the hook runs in a component that holds both. + +import { useEffect, useMemo, useRef, useState } from 'react' +import { useQuery, useQueryClient } from '@tanstack/react-query' +import { + api, + type AutoHotcuesResult, + type Capabilities, + type CuePoint, + type CueType, + type GridMarker, + type Track, + type TrackCues, + type StemInfo, + type TrackOrigin, + trackAudioUrl, + trackCuesFor, + trackStemsFor, + trackStemUrl, +} from '../../api' +import { buildBeatGrid, GRID_EPS } from '../../lib/beatgrid' +import { slotLabeller } from '../../lib/capabilities' +import { CUE_TYPE_LABELS, cueTypeColor } from '../../lib/cues' +import { formatLoudness } from '../../lib/format' +import { readOnlyNotice } from '../../lib/platformCopy' +import { analyzeStems, analyzeWaveform, type WaveColumn } from '../../lib/waveform' +import type { RawWave } from '../../lib/rawWave' +import { setAmbientFromArt } from '../../lib/ambient' +import { DeckEngine } from '../../lib/audio/deckEngine' +import { resumeAudio } from '../../lib/audio/context' +import { STEM_KEYS, type DeckShortcuts } from '../../lib/deckKeys' +import { invalidateTrackLists } from '../../lib/trackQueries' +import { eventLabel } from '../AutoCueDialog' +import type { MenuItem } from '../ContextMenu' +import { LOOP_SIZES, type LoopMode } from '../LoopControls' +import type { StemLane } from '../MainWaveform' +import { usePrefsWriter } from '../../lib/prefs' + +export interface DeckOptions { + /** The track loaded into this deck, if any. */ + track: Track | null + /** What this deck's library can do (capsFor its origin). */ + caps: Capabilities + /** Seconds across the main waveform — the shared zoom. */ + secPerView: number + /** Bumped by the library's per-row play button to load + auto-play. */ + playRequest?: number + onError?: (msg: string) => void + /** Neutral/success feedback (e.g. Auto Hotcues result). */ + onNotify?: (kind: 'success' | 'error', msg: string) => void + /** + * Which library the track belongs to, and so which endpoints serve its audio + * and cues: the loaded collection, a browsed DEVICE, or a browsed FOLDER of + * loose files. Every EDIT control is already inert for the last two — their + * capabilities say `writable: false`, and the deck gates on those — so this + * only needs to redirect the two READS. + */ + origin?: TrackOrigin + /** Bumped when something outside the deck (batch grid analysis) rewrote the + * loaded track's cues/grid, so the deck re-reads them. */ + cuesRefresh?: number + /** Reports whether the deck is playing, so the library can light its row. */ + onPlayingChange?: (playing: boolean) => void + /** Opens Edit Tags for the loaded track — passed only where the library + * will accept the edit, so the cover popup offers it only then. */ + onEditTags?: (track: Track) => void + /** Persist this deck's loop size and cue bank (deck A; default true). */ + persist?: boolean + /** Recolour the app from this deck's cover art (default true). */ + leadsAmbient?: boolean +} + +export function useDeck({ + track, + caps, + secPerView, + playRequest = 0, + onError, + onNotify, + origin = 'collection', + cuesRefresh = 0, + onPlayingChange, + onEditTags, + persist = true, + leadsAmbient = true, +}: DeckOptions) { + const qc = useQueryClient() + const [playing, setPlaying] = useState(false) + useEffect(() => onPlayingChange?.(playing), [playing, onPlayingChange]) + const [previewing, setPreviewing] = useState(false) // momentary hold-to-play active + // CUE is held down (mouse OR the C key) — drives the button's pressed look, + // which the pointer's :active alone cannot, since the key never touches it. + const [cueHeld, setCueHeld] = useState(false) + const [current, setCurrent] = useState(0) + const [duration, setDuration] = useState(0) + const [ready, setReady] = useState(false) + const [cols, setCols] = useState(null) + // The raw waveform's data: the mix's samples, or one per stem lane. + const [raw, setRaw] = useState(null) + const [stemRaws, setStemRaws] = useState(null) + // A stem track: its stems (from the FILE), their lanes, and which are muted + // — reset per track, as Traktor's stem deck does. There is no separate solo + // state: solo is a shortcut that SETS the mutes, so every stem stays freely + // mutable afterwards. + const [stems, setStems] = useState(null) + const [stemLanes, setStemLanes] = useState(null) + const [stemMuted, setStemMuted] = useState([]) + const [waveStatus, setWaveStatus] = useState<'loading' | 'ready' | 'error'>('loading') + const [cueData, setCueData] = useState(null) + // The beatgrid, rebuilt whenever the cue data is replaced (every edit returns + // a fresh TrackCues, so this stays in step automatically). Null = no grid. + const grid = useMemo(() => buildBeatGrid(cueData?.grid_markers), [cueData?.grid_markers]) + const [snap, setSnap] = useState(true) + const [selectedSlot, setSelectedSlot] = useState(null) + const [cuePoint, setCuePoint] = useState(0) // floating "CUE" point (frontend-only) + const [infoOpen, setInfoOpen] = useState(false) // the cover's track-info popup + // ONE size, in beats, for beat loops and beat jump alike (persisted). + const [beatSize, setBeatSize] = useState(4) + const [loopMode, setLoopMode] = useState('beat') + // Grid mode swaps the hotcue pads for the grid-editing strip. + const [gridMode, setGridMode] = useState(false) + const [padMenu, setPadMenu] = useState<{ slot: number; x: number; y: number } | null>(null) + const [renamingSlot, setRenamingSlot] = useState(null) + // Hot / Memory: which bank the pads' place in the row shows (persisted). + const [bankMode, setBankMode] = useState<'hot' | 'memory'>('hot') + const [memoryMenu, setMemoryMenu] = useState<{ cue: CuePoint; x: number; y: number } | null>(null) + const [renamingMemory, setRenamingMemory] = useState(null) + + // Loop state (transient — persisted only when saved as a hotcue later). + const [loopRegion, setLoopRegion] = useState<{ start: number; end: number } | null>(null) + const [loopActive, setLoopActive] = useState(false) + const [activeBeats, setActiveBeats] = useState(null) + // The armed manual loop-in point: IN was pressed, OUT not yet. STATE, not a + // ref, because the IN button and the waveform both show it while it waits. + const [loopInPoint, setLoopInPoint] = useState(null) + const originalGridRef = useRef(null) + + // The deck's audio (created once, asynchronously: the worklet module loads + // first). playbackRef is set once it exists; getEngine() awaits it. + const playbackRef = useRef(null) + const enginePromiseRef = useRef | null>(null) + const wasPlayingRef = useRef(false) + const scratchRafRef = useRef(0) + const engagedRef = useRef(false) // the deck is scratching (dragging or coasting) + const pendingPlayRef = useRef(false) // a play was requested; start once ready + const loadedIdRef = useRef(null) // trackId whose buffer is loaded + const [gridBusy, setGridBusy] = useState(false) + // 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 + + // beatSize and the Hot / Memory bank persist to userprefs.json (survive + // track switches and restarts) — deck A's only, when there are two. Hydrate + // once from the shared ['prefs'] query, then write through the shared, + // debounced prefs writer. + const prefsQuery = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }) + const writePrefs = usePrefsWriter() + + // The loop/jump size persists the same way (survives track switches + + // restarts). `beatJumpBeats` is its name from when only beat jump had a size; + // it is read once so an upgrade keeps the user's choice. + const sizeHydratedRef = useRef(false) + useEffect(() => { + if (sizeHydratedRef.current || !prefsQuery.data) return + sizeHydratedRef.current = true + const prefs = prefsQuery.data as { beatSize?: unknown; beatJumpBeats?: unknown } + const b = prefs.beatSize ?? prefs.beatJumpBeats + if (typeof b === 'number' && LOOP_SIZES.includes(b)) setBeatSize(b) + }, [prefsQuery.data]) + useEffect(() => { + if (!sizeHydratedRef.current || !persist) return // don't clobber saved prefs pre-hydration + writePrefs({ beatSize }) + }, [beatSize, writePrefs, persist]) + + // Hot / Memory bank, remembered the same way (decided 2026-10-16). + const bankHydratedRef = useRef(false) + useEffect(() => { + if (bankHydratedRef.current || !prefsQuery.data) return + bankHydratedRef.current = true + if ((prefsQuery.data as { cueBankMode?: unknown }).cueBankMode === 'memory') setBankMode('memory') + }, [prefsQuery.data]) + useEffect(() => { + if (!bankHydratedRef.current || !persist) return // don't clobber saved prefs pre-hydration + writePrefs({ cueBankMode: bankMode }) + }, [bankMode, writePrefs, persist]) + + const getEngine = (): Promise => { + if (!enginePromiseRef.current) { + enginePromiseRef.current = DeckEngine.create().then((eng) => { + playbackRef.current = eng + eng.setOnEnded(() => setPlaying(false)) + // The deck hands itself back after a scratch's coast settles. + eng.setOnScratchDone((resumed) => scratchDoneRef.current(resumed)) + return eng + }) + enginePromiseRef.current.catch(() => { + enginePromiseRef.current = null + }) + } + return enginePromiseRef.current + } + + // Tear the deck's audio down when the strip itself unmounts (e.g. the + // collection picker early-returns and replaces the whole UI) — otherwise a + // remount would play a SECOND track over the orphaned one. The AudioContext + // is the app's and stays; this deck's nodes leave it. + useEffect(() => { + return () => { + const pending = enginePromiseRef.current + enginePromiseRef.current = null + playbackRef.current = null + void pending?.then((eng) => eng.dispose()) + } + }, []) + + // Reset transport + loop state when the loaded track changes. + useEffect(() => { + playbackRef.current?.pause() + setPlaying(false) + setCurrent(0) + setDuration(0) + setReady(false) + setSelectedSlot(null) + setPadMenu(null) + setRenamingSlot(null) + setMemoryMenu(null) + setRenamingMemory(null) + setLoopRegion(null) + setLoopActive(false) + setActiveBeats(null) + setCuePoint(0) + setLoopInPoint(null) + previewRef.current = null + setPreviewing(false) + setCueHeld(false) + setStemMuted([]) + }, [trackId]) + + // Recolour the whole app from this track's cover art. A device's art is not + // served (only its audio is), so a device track keeps the default look. + useEffect(() => { + if (!leadsAmbient) return + void setAmbientFromArt(trackId && origin === 'collection' ? api.artUrl(trackId) : null) + }, [trackId, origin, leadsAmbient]) + + // Analyse once per track; the decoded buffer feeds both the playback and + // scratch engines (no re-decode) and both waveform views share the columns. + useEffect(() => { + if (!trackId) { + setCols(null) + setRaw(null) + setStemRaws(null) + setWaveStatus('loading') + return + } + let cancelled = false + setCols(null) + setRaw(null) + setStemRaws(null) + setStems(null) + setStemLanes(null) + setWaveStatus('loading') + // A stem FILE loads its four stems and plays their sum (decided: always, + // for every stem track); anything else loads as one buffer, as before. + const load = trackStemsFor(origin, trackId) + .catch(() => [] as StemInfo[]) + .then(async (found) => { + if (found.length) { + const res = await analyzeStems(found.map((_, k) => trackStemUrl(origin, trackId, k))) + return { cols: res.cols, audios: res.audios, lanes: res.lanes, stems: found, raw: null, raws: res.raws } + } + const res = await analyzeWaveform(trackAudioUrl(origin, trackId)) + return { cols: res.cols, audios: [res.audio], lanes: null, stems: null, raw: res.raw, raws: null } + }) + Promise.all([load, getEngine()]) + .then(([res, eng]) => { + if (cancelled) return + setCols(res.cols) + setRaw(res.raw) + setStemRaws(res.raws) + setStems(res.stems) + setStemLanes(res.lanes) + setWaveStatus('ready') + // The PCM moves to the audio thread here (transferred, not copied). + eng.load(res.audios) + setDuration(res.audios[0].duration) + setReady(true) + loadedIdRef.current = trackId + // A play was requested (library row button) → start now that it's ready. + if (pendingPlayRef.current) { + pendingPlayRef.current = false + eng.play() + setPlaying(true) + } + }) + .catch(() => { + if (!cancelled) setWaveStatus('error') + }) + return () => { + cancelled = true + } + }, [trackId, origin]) + + // Fetch beatgrid + cue markers for the loaded track. + useEffect(() => { + if (!trackId) { + setCueData(null) + return + } + let cancelled = false + setCueData(null) + ;trackCuesFor(origin, trackId) + .then((d) => { + if (cancelled) return + setCueData(d) + // Remember the grid as loaded, for "Reset". Flexible-grid editing is + // multi-step and destructive, so restoring exactly what Traktor had is + // the safety net. Set only here (keyed on trackId), never by an edit. + originalGridRef.current = d.grid_markers + }) + .catch(() => { + if (!cancelled) setCueData(null) + }) + return () => { + cancelled = true + } + }, [trackId, origin]) + + // Re-read after an outside edit. Deliberately NOT the effect above: that one + // resets `originalGridRef`, and "Reset" should still restore the grid the + // track had before the outside edit replaced it. + useEffect(() => { + if (!trackId || !cuesRefresh) return + let cancelled = false + api.trackCues(trackId).then((d) => !cancelled && setCueData(d)).catch(() => {}) + return () => { + cancelled = true + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [cuesRefresh]) + + const toggle = () => { + const eng = playbackRef.current + if (!eng || !eng.ready) return + resumeAudio() // resume 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 + setPreviewing(false) // becomes normal (green) playback + if (!eng.playing) { + eng.play() + setPlaying(true) + } + setCurrent(eng.getPosition()) + return + } + if (eng.playing) { + eng.pause() + setPlaying(false) + } else { + eng.play() + setPlaying(true) + } + 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) + setPreviewing(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 + setPreviewing(false) + 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 + // pause; while paused and away from the cue, drop the cue at the playhead; + // while paused and already at the cue, play from it for as long as the button + // is held (momentary preview — mirrors the hotcue buttons). + const onCuePress = () => { + const eng = playbackRef.current + if (!eng || !eng.ready) return + setCueHeld(true) + resumeAudio() + if (eng.playing) { + leaveLoopFor(cuePoint) + eng.pause() + eng.seek(cuePoint) + setPlaying(false) + setCurrent(cuePoint) + } else if (Math.abs(current - cuePoint) > 0.03) { + // 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 { + leaveLoopFor(cuePoint) + beginPreview('cue', cuePoint) + } + } + const onCueRelease = () => { + setCueHeld(false) + endPreview('cue') + } + + // A key-up that happens in another window never arrives, which would leave + // the button looking held; letting go of the window lets go of CUE. + useEffect(() => { + const letGo = () => setCueHeld(false) + window.addEventListener('blur', letGo) + return () => window.removeEventListener('blur', letGo) + }, []) + + // A library row's play button bumps `playRequest`: load (if needed) + play. + // If the requested track is already loaded and ready, start immediately; + // otherwise flag it and the analysis effect starts playback once ready. + // Only a CHANGE is a request: the deck unmounts behind the library picker, + // and remounting with a non-zero count must not start playback. + const seenPlayRequestRef = useRef(playRequest) + useEffect(() => { + if (playRequest === seenPlayRequestRef.current) return + seenPlayRequestRef.current = playRequest + pendingPlayRef.current = true + const eng = playbackRef.current + if (loadedIdRef.current === trackId && eng?.ready) { + pendingPlayRef.current = false + resumeAudio() + eng.play() + setPlaying(true) + setCurrent(eng.getPosition()) + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [playRequest]) + + // Follow the Web Audio clock while playing (high-res → smooth playhead). + useEffect(() => { + if (!playing) return + let raf = 0 + const tick = () => { + const eng = playbackRef.current + if (eng) setCurrent(eng.getPosition()) + raf = requestAnimationFrame(tick) + } + raf = requestAnimationFrame(tick) + return () => cancelAnimationFrame(raf) + }, [playing]) + + const seek = (t: number) => { + const eng = playbackRef.current + if (!eng) return + eng.seek(t) + setCurrent(t) + } + + // A cue jump that lands outside the engaged loop leaves it, Traktor-style. + // Without this the loop stays in force and Web Audio, started past a loop's + // end, wraps straight back into it. A jump inside the loop keeps it. + const leaveLoopFor = (t: number) => { + if (!loopActive || !loopRegion) return + if (t >= loopRegion.start && t < loopRegion.end) return + playbackRef.current?.setLoopEnabled(false) + setLoopActive(false) + } + + // User-driven seek from the waveforms: navigating away drops the active loop + // (otherwise it would just pull playback back into the loop region). + const seekManual = (t: number) => { + if (loopActive) { + playbackRef.current?.setLoopEnabled(false) + setLoopActive(false) + } + seek(t) + } + + // ---- beat jump / loop move ------------------------------------------ + // ‹ › (and ←/→): an engaged loop MOVES by its own length and keeps playing, + // Traktor-style; otherwise the playhead jumps `beatSize` beats. Both preserve + // sub-beat phase (pure translation, no grid snap), and seek() keeps the + // play/pause state. + const moveOrJump = (dir: -1 | 1) => { + const eng = playbackRef.current + if (!eng || !eng.ready) return + if (loopActive && loopRegion) { + const { start, end } = loopRegion + // A beat loop moves in BEATS, so it lands right across a tempo change; a + // manual loop has no beat length, so it moves by its duration. + let nStart: number + let nEnd: number + if (activeBeats != null && grid) { + nStart = grid.advanceBeats(start, dir * activeBeats) + nEnd = grid.advanceBeats(nStart, activeBeats) + } else { + const len = end - start + nStart = start + dir * len + nEnd = nStart + len + } + if (nStart < 0 || nEnd > duration) return // would leave the track + // The playhead travels with the loop, keeping its place inside it. The + // NEW loop is set first: the engine's seek honours the loop in force, so + // seeking first would wrap the playhead straight back into the old one. + const pos = eng.getPosition() + (nStart - start) + engagLoop(nStart, nEnd, activeBeats) + eng.seek(Math.min(nEnd - 0.001, Math.max(nStart, pos))) + setCurrent(eng.getPosition()) + return + } + if (!grid) return + // Phase is preserved in BEAT space, so a jump crossing a tempo change lands + // on the musically right beat rather than a fixed number of seconds away. + eng.seek(grid.advanceBeats(eng.getPosition(), dir * beatSize)) + setCurrent(eng.getPosition()) + } + + // ---- scratch ---------------------------------------------------------- + // The deck scratches itself: a drag moves its position, a release coasts to + // playing speed (or a stop) and it plays on BY ITSELF, then says so — so + // there is no gap between the coast and the music. React only mirrors it. + const scratchDone = (resumed: boolean) => { + cancelAnimationFrame(scratchRafRef.current) + engagedRef.current = false + const eng = playbackRef.current + if (eng) setCurrent(eng.getPosition()) + setPlaying(resumed) + } + const scratchDoneRef = useRef(scratchDone) + scratchDoneRef.current = scratchDone + + const runScratchRaf = () => { + cancelAnimationFrame(scratchRafRef.current) + const tick = () => { + const eng = playbackRef.current + if (!eng || !engagedRef.current) return + setCurrent(eng.getPosition()) + scratchRafRef.current = requestAnimationFrame(tick) + } + scratchRafRef.current = requestAnimationFrame(tick) + } + + const onScratchStart = () => { + const eng = playbackRef.current + if (!eng || !eng.ready) return + if (engagedRef.current) { + eng.scratchRegrab() // re-grab while coasting + return + } + wasPlayingRef.current = eng.playing + if (eng.playing) setPlaying(false) + eng.scratchBegin(eng.getPosition()) + engagedRef.current = true + runScratchRaf() + } + + const onScratchMove = (t: number) => { + if (engagedRef.current) playbackRef.current?.scratchTarget(t) + else setCurrent(t) + } + + const onScratchEnd = (t: number) => { + if (engagedRef.current) { + playbackRef.current?.scratchRelease(wasPlayingRef.current) + } else { + seek(t) + if (wasPlayingRef.current) { + playbackRef.current?.play() + setPlaying(true) + } + } + } + + // ---- loops ------------------------------------------------------------ + // Snap a time to the nearest beat when Snap is on (needs a beatgrid). + const snapTime = (t: number) => (snap && grid ? grid.snapToBeat(t) : Math.max(0, t)) + + const engagLoop = (start: number, end: number, beats: number | null) => { + const eng = playbackRef.current + if (!eng) return + eng.setLoop(start, end, true) + setLoopRegion({ start, end }) + setLoopActive(true) + setActiveBeats(beats) + setLoopInPoint(null) + setCurrent(eng.getPosition()) + } + + const setBeatLoop = (beats: number) => { + const eng = playbackRef.current + if (!eng || !eng.ready || !grid) return + // Pressing the size of the loop that's already playing disables it. + if (loopActive && activeBeats === beats) { + eng.setLoopEnabled(false) + setLoopActive(false) + setCurrent(eng.getPosition()) + return + } + // Resizing an active loop keeps its start locked; only a fresh loop starts + // at the current playhead. + const start = loopActive && loopRegion ? loopRegion.start : snapTime(eng.getPosition()) + // Measured from the loop's own start, so a loop spanning a tempo change + // still covers the right number of beats. + engagLoop(start, grid.advanceBeats(start, beats), beats) + } + + const loopIn = () => { + const eng = playbackRef.current + if (!eng || !eng.ready) return + setLoopInPoint(snapTime(eng.getPosition())) + } + + const loopOut = () => { + const eng = playbackRef.current + if (!eng || !eng.ready) return + const end = snapTime(eng.getPosition()) + // OUT closes an armed IN — or, with a manual loop already set, moves that + // loop's end to the playhead (Traktor's loop-out adjust). + const start = loopInPoint ?? (loopRegion && activeBeats == null ? loopRegion.start : null) + if (start == null || end <= start) return + engagLoop(start, end, null) + } + + const toggleLoop = () => { + const eng = playbackRef.current + if (!eng || !loopRegion) return + const next = !loopActive + eng.setLoopEnabled(next) + setLoopActive(next) + setCurrent(eng.getPosition()) + } + + // − / + (and Cmd/Ctrl+↓/↑): step the shared size, clamping at both ends. An + // engaged BEAT loop follows it — halving or doubling from its locked start, as + // on a CDJ. A manual loop has no beat size to follow, so it is left alone. + const stepSize = (dir: -1 | 1) => { + const i = LOOP_SIZES.indexOf(beatSize) + const next = LOOP_SIZES[Math.min(LOOP_SIZES.length - 1, Math.max(0, (i < 0 ? 7 : i) + dir))] + if (next === beatSize) return + setBeatSize(next) + if (loopActive && activeBeats != null) setBeatLoop(next) + } + + // The size key: a loop of exactly this size switches off; otherwise a loop of + // this size is set (from the playhead, or resized from an engaged loop's start). + const toggleSizeLoop = () => setBeatLoop(beatSize) + + // A beat loop's end is stored in seconds, so editing the tempo of a marker + // inside it would silently detune it. Re-derive the end whenever the grid + // changes, keeping the loop the beat length the user actually asked for. + useEffect(() => { + if (!grid || !loopActive || !loopRegion || activeBeats == null) return + const end = grid.advanceBeats(loopRegion.start, activeBeats) + if (Math.abs(end - loopRegion.end) < 1e-4) return + playbackRef.current?.setLoop(loopRegion.start, end, true) + setLoopRegion({ start: loopRegion.start, end }) + // Only the grid should trigger this; loop state changes set the end already. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [grid]) + + // ---- capability-derived ----------------------------------------------- + // Edits are guarded at the point of INTENT, so the user gets the reason + // instead of a 422 from the adapter's backstop, and the deck stays fully + // usable for listening — which is most of its value on a library you cannot + // write. + // + // TWO gates, not one. `writable` is the library; `cues.editable` and + // `grid.editable` are the features. A platform can be writable overall while + // its cue or grid store is not implemented yet, so gating only on `writable` + // would re-expose these controls the moment that flips — which is exactly how + // this would regress unnoticed when Rekordbox writes land. + const canEditCues = caps.writable && caps.cues.editable + const canEditGrid = caps.writable && caps.grid.editable + const refuseCueEdit = () => { + if (canEditCues) return false + onError?.( + readOnlyNotice(caps) ?? `Cues cannot be edited on ${caps.save.app_name} libraries yet.`, + ) + return true + } + const refuseGridEdit = () => { + if (canEditGrid) return false + onError?.( + readOnlyNotice(caps) ?? + `The beatgrid cannot be edited on ${caps.save.app_name} libraries yet.`, + ) + return true + } + const slotCount = caps.cues.hotcue_slots + const slotLabel = slotLabeller(caps) + // Loops are a cue TYPE on some platforms and a separate bank on others; the + // dropdown only ever offers the point types. + const pointCueTypes = caps.cues.types.filter((t) => t !== 'loop') + + // ---- hotcues ---------------------------------------------------------- + const hotcueAt = (slot: number) => + cueData?.cues.find((c) => c.role === 'hotcue' && c.slot === slot) ?? null + + const applyCueEdit = (fresh: TrackCues) => { + setCueData(fresh) + invalidateTrackLists(qc) + } + + // Map a loop length (seconds) back to a preset beat count for the size + // highlight, snapping to the nearest preset when it's close (float tolerance). + // Needs the start as well as the length: under a marker list a duration + // alone no longer identifies a beat count. + const beatsForLoop = (start: number, length: number): number | null => { + if (!grid) return null + const beats = grid.beatsBetween(start, start + length) + let best: number | null = null + let bestDiff = Infinity + for (const s of LOOP_SIZES) { + const diff = Math.abs(s - beats) + if (diff < bestDiff) { + bestDiff = diff + best = s + } + } + return best != null && bestDiff <= beats * 0.05 ? best : null + } + + // 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 + const cue = hotcueAt(slot) + if (!cue) { + if (refuseCueEdit()) return + try { + // Setting a hotcue while a loop is active stores it as a loop hotcue. + if (loopActive && loopRegion) { + const len = loopRegion.end - loopRegion.start + applyCueEdit(await api.createCue(track.id, slot, loopRegion.start, 'loop', len)) + } else { + const t = snapTime(playbackRef.current?.getPosition() ?? current) + applyCueEdit(await api.createCue(track.id, slot, t, 'cue')) + } + setSelectedSlot(slot) + } catch (e) { + onError?.((e as Error).message) + } + return + } + setSelectedSlot(slot) + const eng = playbackRef.current + if (!eng || !eng.ready) return + resumeAudio() + enterCue(cue) + seek(cue.start) + // Paused at press → momentary preview: play while held. + if (!eng.playing) beginPreview(`hc:${slot}`, cue.start) + } + + // Before jumping to a cue: a loop cue engages a loop of its length — the new + // loop FIRST, so the seek is not wrapped back into the old one. The loop + // controls follow it: a beat-sized loop takes over the shared size (so −/+ + // resize THIS loop), and one of no preset size shows in MAN, where its toggle + // is. A point cue outside an engaged loop leaves it. + const enterCue = (cue: CuePoint) => { + if (cue.type === 'loop' && cue.length > 0) { + const beats = beatsForLoop(cue.start, cue.length) + engagLoop(cue.start, cue.start + cue.length, beats) + if (beats != null) { + setBeatSize(beats) + setLoopMode('beat') + } else { + setLoopMode('manual') + } + } else { + leaveLoopFor(cue.start) + } + } + + // Hotcue slot released (mouse/touch up, or key up). Ends its momentary preview. + const onSlotRelease = (slot: number) => endPreview(`hc:${slot}`) + + // Auto Hotcues: the button opens the slot-template dialog; the backend finds + // the track's structure on its beatgrid and places the template. Requires a + // beatgrid (button disabled otherwise) — every event is a bar on it. + const canAutoCue = grid != null + const [autoOpen, setAutoOpen] = useState(false) + const openAutoHotcues = () => { + if (!track || !canAutoCue) return + if (refuseCueEdit()) return + setAutoOpen(true) + } + const existingHotcues = useMemo(() => { + const m = new Map() + for (const c of cueData?.cues ?? []) if (c.role === 'hotcue' && c.slot != null) m.set(c.slot, c) + return m + }, [cueData]) + const autoHotcuesDone = (result: AutoHotcuesResult) => { + applyCueEdit(result.cues) + const by = (st: string) => result.outcomes.filter((o) => o.status === st) + const placed = by('placed').length + const missing = [...by('not_found'), ...by('out_of_range')].map((o) => eventLabel(o.event)) + const kept = by('occupied').length + by('protected').length + const dupes = by('duplicate').map( + (o) => `${slotLabel(o.slot)} (same beat as ${slotLabel(o.duplicate_of ?? o.slot)})`, + ) + const parts = [`Placed ${placed} hotcue${placed === 1 ? '' : 's'}`] + if (missing.length) parts.push(`not in this track: ${missing.join(', ')}`) + if (dupes.length) parts.push(`skipped ${dupes.join(', ')}`) + if (kept) parts.push(`${kept} occupied slot${kept === 1 ? '' : 's'} kept`) + onNotify?.(placed ? 'success' : 'error', parts.join(' · ')) + } + + const setSlotType = async (slot: number, type: CueType) => { + if (!track) return + if (refuseCueEdit()) return + try { + applyCueEdit(await api.setCueType(track.id, slot, type)) + } catch (e) { + onError?.((e as Error).message) + } + } + + // Renaming re-sets the slot with its own position, type and length plus the + // new name — the adapter's set_cue replaces a slot, and keeps a name only + // when called without one. An empty name clears it. + const renameSlot = async (slot: number, name: string) => { + setRenamingSlot(null) + const cue = hotcueAt(slot) + if (!track || !cue) return + if (refuseCueEdit()) return + try { + applyCueEdit(await api.createCue(track.id, slot, cue.start, cue.type, cue.length, name)) + } catch (e) { + onError?.((e as Error).message) + } + } + + // Only a fixed palette is offered: a platform with free RGB would need a + // picker of its own, and none that stores free RGB is writable yet. + const cuePalette = caps.cues.color === 'palette' ? caps.cues.palette : [] + const setSlotColor = async (slot: number, color: string | null) => { + if (!track) return + if (refuseCueEdit()) return + try { + applyCueEdit(await api.setCueColor(track.id, slot, color)) + } catch (e) { + onError?.((e as Error).message) + } + } + + const deleteHotcueSlot = async (slot: number) => { + const existing = hotcueAt(slot) + if (!track || !existing) return // nothing to remove in an empty slot + if (!existing.editable) return // the adapter would refuse it + if (refuseCueEdit()) return + try { + applyCueEdit(await api.deleteCue(track.id, slot)) + if (selectedSlot === slot) setSelectedSlot(null) + } catch (e) { + onError?.((e as Error).message) + } + } + + // ---- memory cues -------------------------------------------------------- + // Cues on no pad, listed in time order (MemoryCueStrip). Calling one is + // CDJ-style: jump there and make it the CUE point — paused stays paused, + // playing keeps playing; a memory loop also engages (decided 2026-10-16). + const showMemory = caps.cues.memory_cues && bankMode === 'memory' + const memoryPalette = caps.cues.memory_color === 'palette' ? caps.cues.memory_palette : [] + const memoryCount = cueData?.cues.filter((c) => c.role === 'memory' && c.editable).length ?? 0 + const memoryCap = caps.cues.max_memory_cues + const memoryAddRefusal = !canEditCues + ? (readOnlyNotice(caps) ?? `Cues cannot be edited on ${caps.save.app_name} libraries yet.`) + : memoryCap != null && memoryCount >= memoryCap + ? `${caps.save.app_name} keeps at most ${memoryCap} memory cues per track` + : null + + const callMemoryCue = (cue: CuePoint) => { + const eng = playbackRef.current + if (!eng || !eng.ready) return + resumeAudio() + enterCue(cue) + seek(cue.start) + setCuePoint(cue.start) + } + + // + Memory: at the (snapped) playhead, or the engaged loop as a memory loop. + const addMemoryCue = async () => { + if (!track) return + if (memoryAddRefusal) { + onError?.(memoryAddRefusal) + return + } + try { + if (loopActive && loopRegion) { + const len = loopRegion.end - loopRegion.start + applyCueEdit(await api.addMemoryCue(track.id, loopRegion.start, 'loop', len)) + } else { + const t = snapTime(playbackRef.current?.getPosition() ?? current) + applyCueEdit(await api.addMemoryCue(track.id, t, 'cue')) + } + } catch (e) { + onError?.((e as Error).message) + } + } + + const editMemoryCue = async (edit: () => Promise) => { + if (!track || refuseCueEdit()) return + try { + applyCueEdit(await edit()) + } catch (e) { + onError?.((e as Error).message) + } + } + + const memoryMenuItems = (cue: CuePoint): MenuItem[] => { + const id = cue.id + const locked = !cue.editable || id == null + const items: MenuItem[] = [] + // Its type as one-click choices, as on a pad — where the platform has more + // than one point type (Traktor); a loop stays a loop. + if (cue.type === 'loop') { + items.push({ heading: 'Memory loop' }) + } else if (pointCueTypes.length > 1) { + items.push({ heading: 'Memory cue · Type' }) + for (const t of pointCueTypes) { + const color = cueTypeColor(t) + items.push({ + label: CUE_TYPE_LABELS[t], + icon: ( + + ), + hint: t === cue.type ? '✓' : undefined, + disabled: locked, + onClick: () => + t !== cue.type && + id != null && + void editMemoryCue(() => api.setMemoryCueType(track!.id, id, t)), + }) + } + } else { + items.push({ heading: 'Memory cue' }) + } + if (memoryPalette.length > 0) { + items.push({ separator: true }) + items.push({ heading: 'Colour' }) + items.push({ + swatches: memoryPalette, + selected: cue.color, + disabled: locked, + onPick: (color) => + color !== cue.color && + id != null && + void editMemoryCue(() => api.setMemoryCueColor(track!.id, id, color)), + }) + } + if (locked) items.push({ heading: 'Managed by the DJ app — not editable here' }) + items.push({ separator: true }) + items.push({ + label: 'Rename…', + disabled: locked, + onClick: () => !refuseCueEdit() && setRenamingMemory(id), + }) + items.push({ + label: 'Delete', + danger: true, + disabled: locked, + onClick: () => id != null && void editMemoryCue(() => api.deleteMemoryCue(track!.id, id)), + }) + return items + } + + const renameMemoryCue = (cue: CuePoint, name: string) => { + setRenamingMemory(null) + if (cue.id == null) return + const id = cue.id + void editMemoryCue(() => api.renameMemoryCue(track!.id, id, name || null)) + } + + // ---- beatgrid --------------------------------------------------------- + // Which marker the grid controls act on is derived from the playhead — there + // is no separate selection state to keep in sync. + const activeMarkerIndex = grid ? grid.markerIndexAt(current) : -1 + const activeMarker = activeMarkerIndex >= 0 ? grid!.markers[activeMarkerIndex] : null + // ~4px of the current zoom, so "on the marker" stays usable at 2s and 64s/view. + // ~4 px of the view either side, so "at a marker" stays a marker-sized + // target at deep zoom instead of 4% of a 0.25 s view. + const markerHitSec = Math.max(0.001, secPerView / 250) + const atMarker = !!grid && grid.isOnMarker(current, markerHitSec) + const beforeFirstMarker = !!grid && current < grid.markers[0].start - GRID_EPS + + const playheadNow = () => playbackRef.current?.getPosition() ?? current + + const editMarker = async (index: number, patch: { bpm?: number; start?: number }) => { + if (!track || index < 0) return + if (refuseGridEdit()) return + try { + applyCueEdit(await api.setGridMarker(track.id, index, patch)) + } catch (e) { + onError?.((e as Error).message) + } + } + const setBpm = (bpm: number) => + editMarker(activeMarkerIndex, { bpm: Math.round(bpm * 1000) / 1000 }) + const nudgeBpm = (delta: number) => activeMarker && setBpm(activeMarker.bpm + delta) + // /2 and x2 retempo the governing marker only, like every other tempo control + // in this panel — a section can be octave-wrong on its own. + const halveBpm = () => activeMarker && setBpm(activeMarker.bpm / 2) + const doubleBpm = () => activeMarker && setBpm(activeMarker.bpm * 2) + // A beat is the governing marker's own beat: moving a marker by whole beats + // shifts which beat is the downbeat without changing the phase. + const nudgeGridBeats = (beats: number) => { + if (activeMarker) nudgeGrid((beats * 60000) / activeMarker.bpm) + } + const nudgeGrid = (deltaMs: number) => { + if (!activeMarker) return + editMarker(activeMarkerIndex, { start: Math.max(0, activeMarker.start + deltaMs / 1000) }) + } + // With no grid, a tempo from TAP or the typed readout CREATES one, with its + // first marker at an anchor captured when the gesture began: the first tap + // (a beat, when tapping along), or where the playhead stood when Set grid + // asked for a BPM. The Grid-mode nudges fix the phase. While that create is + // in flight, later tempos wait for it rather than creating a second grid. + const tapAnchorRef = useRef(0) + const gridAnchorRef = useRef(null) // Set grid's, while the readout asks + const gridCreateRef = useRef | null>(null) + const [bpmEntryRequest, setBpmEntryRequest] = useState(0) // bump → readout opens for typing + const tapStart = () => { + tapAnchorRef.current = playheadNow() + } + const commitBpm = async (bpm: number, anchor: number) => { + if (!track) return + // This closure predates the grid it is waiting for, so it cannot use + // activeMarkerIndex — but a grid created here has exactly one marker. + if (gridCreateRef.current) { + if (await gridCreateRef.current) editMarker(0, { bpm }) + return + } + if (markerCount > 0) return setBpm(bpm) + if (refuseGridEdit()) return + const create = api + .addGridMarker(track.id, anchor, bpm) + .then((cues) => (applyCueEdit(cues), true)) + .catch((e) => (onError?.((e as Error).message), false)) + .finally(() => (gridCreateRef.current = null)) + gridCreateRef.current = create + await create + } + // Adds a marker at the playhead — and creates the grid when there is none. + // Uses the raw playhead, not snapTime: a marker defines where beats are. + // A first marker needs a tempo: the track's own BPM when it has one (passed + // explicitly — Rekordbox does not fall back to it), else the readout opens + // to ask, and the marker lands where the playhead is NOW once it is typed. + const addMarkerHere = async () => { + if (!track) return + if (refuseGridEdit()) return + if (markerCount === 0 && !track.bpm) { + gridAnchorRef.current = playheadNow() + setBpmEntryRequest((n) => n + 1) + return + } + try { + applyCueEdit( + await api.addGridMarker(track.id, playheadNow(), markerCount === 0 ? (track.bpm ?? undefined) : undefined), + ) + } catch (e) { + onError?.((e as Error).message) + } + } + const deleteMarkerHere = async () => { + if (!track || activeMarkerIndex < 0) return + if (refuseGridEdit()) return + try { + applyCueEdit(await api.deleteGridMarker(track.id, activeMarkerIndex)) + } catch (e) { + onError?.((e as Error).message) + } + } + // Seeking between markers IS how the user changes which marker is active. + const seekToMarker = (index: number) => { + if (!grid || index < 0 || index >= grid.count) return + seekManual(grid.markers[index].start) + } + const prevMarker = () => seekToMarker(grid ? grid.prevMarkerIndex(current, markerHitSec) : -1) + const nextMarker = () => seekToMarker(grid ? grid.nextMarkerIndex(current, markerHitSec) : -1) + const resetGrid = async () => { + const o = originalGridRef.current + if (!track || !o) return + if (refuseGridEdit()) return + try { + applyCueEdit(await api.replaceGridMarkers(track.id, o)) + } catch (e) { + onError?.((e as Error).message) + } + } + const toggleLock = async () => { + if (!track) return + if (refuseGridEdit()) return + try { + applyCueEdit(await api.setGridLock(track.id, !cueData?.grid_locked)) + } catch (e) { + onError?.((e as Error).message) + } + } + const deleteGrid = async () => { + if (!track) return + if (refuseGridEdit()) return + try { + applyCueEdit(await api.deleteGrid(track.id)) + } catch (e) { + onError?.((e as Error).message) + } + } + // Analyze: the backend detects BPM + first beat (setting the grid anchor and, + // on Traktor, hotcue 1) and the key where the library stores one. The key + // lands on the track: `applyCueEdit` refetches the lists, and App hands the + // deck the fresh copy. + const runAnalyzeGrid = async () => { + if (!track || gridBusy) return + setGridBusy(true) + try { + if (canEditGrid) { + applyCueEdit(await api.autoGrid(track.id)) + onNotify?.('success', caps.tracks.key_writable + ? 'Analyzed — set BPM, grid and key, measured loudness' + : 'Analyzed — set BPM and grid, measured loudness') + } else { + // Nothing here can take a grid (a read-only library, a device, a loose + // file) — but loudness goes to Konduktor's own store, so measure that. + const l = await api.measureLoudness(origin, track.id) + onNotify?.('success', `Measured loudness — ${formatLoudness(l.lufs, 'LUFS')}, ${formatLoudness(l.true_peak, 'dBTP', true)}`) + } + qc.invalidateQueries({ queryKey: ['loudness'] }) + } catch (e) { + onError?.((e as Error).message) + } finally { + setGridBusy(false) + } + } + + // ---- stems: mute / solo ------------------------------------------------------ + // What each stem plays at: a muted stem is 0. Both engines follow it (the + // scratch engine re-sums). + const stemLevels = useMemo(() => (stems ?? []).map((_, k) => (stemMuted[k] ? 0 : 1)), [stems, stemMuted]) + const stemLevelKey = stemLevels.join(',') + useEffect(() => { + if (!stems) return + playbackRef.current?.setGains(stemLevels) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [stemLevelKey]) + // Solo = "mute every stem but this one". Soloing a stem that is already the + // only one playing brings them all back (so solo twice undoes it). + const toggleStem = (k: number, solo: boolean) => { + if (!stems || k >= stems.length) return + setStemMuted((m) => { + if (!solo) return stems.map((_, i) => (i === k ? !m[i] : !!m[i])) + const alone = stems.every((_, i) => (i === k ? !m[i] : !!m[i])) + return stems.map((_, i) => (alone ? false : i !== k)) + }) + } + // The ring on a lane button: that stem is the only one playing. + const soloed = stemLevels.filter((l) => l > 0).length === 1 && stemLevels.length > 1 + ? stemLevels.findIndex((l) => l > 0) + : null + const lanes: StemLane[] | null = + stems && stemLanes + ? stems.map((st, k) => ({ + data: stemLanes[k], + raw: stemRaws?.[k] ?? null, + name: st.name, + color: st.color, + audible: stemLevels[k] > 0, + solo: soloed === k, + shortcut: STEM_KEYS[k] ? STEM_KEYS[k].slice(3) : '', + })) + : null + + // The keyboard's handle on this deck (routed by lib/deckKeys.ts). + const shortcuts: DeckShortcuts = { + toggle, + onSlotPress, + onSlotRelease, + deleteHotcueSlot, + onCuePress, + onCueRelease, + moveOrJump, + stepSize, + prevMarker, + nextMarker, + slotCount, + toggleStem, + } + + const showWaves = track && cols && waveStatus === 'ready' + const activeLoop = loopActive && loopRegion ? loopRegion : null + const markerCount = grid?.count ?? 0 + const subtitle = track + ? [track.artist ?? 'Unknown artist', track.album, track.genre].filter(Boolean).join(' · ') + : '' + + // The pad's right-click menu: its type (as one-click choices, the current one + // ticked), its colour where the platform has a palette, Rename, Delete. A cue the adapter will not edit gets the same menu + // disabled, so the reason is visible instead of the menu silently not opening. + const padMenuItems = (slot: number): MenuItem[] => { + const cue = hotcueAt(slot) + if (!cue) return [] + const locked = !cue.editable + const items: MenuItem[] = [] + if (cue.type === 'loop') { + items.push({ heading: `Hotcue ${slotLabel(slot)} · Loop` }) + } else if (pointCueTypes.length > 1) { + items.push({ heading: `Hotcue ${slotLabel(slot)} · Type` }) + for (const t of pointCueTypes) { + const color = cueTypeColor(t) + items.push({ + label: CUE_TYPE_LABELS[t], + icon: ( + + ), + hint: t === cue.type ? '✓' : undefined, + disabled: locked, + onClick: () => t !== cue.type && void setSlotType(slot, t), + }) + } + } else { + items.push({ heading: `Hotcue ${slotLabel(slot)}` }) + } + if (cuePalette.length > 0) { + items.push({ separator: true }) + items.push({ heading: 'Colour' }) + items.push({ + swatches: cuePalette, + selected: cue.color, + disabled: locked, + onPick: (color) => color !== cue.color && void setSlotColor(slot, color), + }) + } + if (locked) items.push({ heading: 'Managed by the DJ app — not editable here' }) + items.push({ separator: true }) + items.push({ + label: 'Rename…', + disabled: locked, + onClick: () => !refuseCueEdit() && setRenamingSlot(slot), + }) + items.push({ + label: 'Delete', + danger: true, + disabled: locked, + onClick: () => void deleteHotcueSlot(slot), + }) + return items + } + + // Rendered twice — inline on a wide row, inside TempoFold on a narrow one. + const tempoProps = { + bpm: activeMarker?.bpm ?? null, + locked: cueData?.grid_locked ?? false, + lockable: caps.grid.lockable, + flexible: markerCount > 1, + markerIndex: activeMarkerIndex, + onTapStart: tapStart, + onTapBpm: (bpm: number) => commitBpm(bpm, tapAnchorRef.current), + onNudgeBpm: nudgeBpm, + onHalve: halveBpm, + onDouble: doubleBpm, + onToggleLock: toggleLock, + } + + return { + caps, + track, + origin, + onError, + onEditTags, + subtitle, + playing, + previewing, + cueHeld, + current, + duration, + ready, + cols, + raw, + stems, + lanes, + waveStatus, + showWaves, + cueData, + grid, + markerCount, + activeMarker, + activeMarkerIndex, + atMarker, + beforeFirstMarker, + originalGridRef, + snap, + setSnap, + selectedSlot, + setSelectedSlot, + cuePoint, + infoOpen, + setInfoOpen, + beatSize, + loopMode, + setLoopMode, + gridMode, + setGridMode, + padMenu, + setPadMenu, + renamingSlot, + setRenamingSlot, + bankMode, + setBankMode, + memoryMenu, + setMemoryMenu, + renamingMemory, + setRenamingMemory, + loopRegion, + loopActive, + activeBeats, + loopInPoint, + activeLoop, + gridBusy, + canEditCues, + canEditGrid, + slotCount, + slotLabel, + toggle, + onCuePress, + onCueRelease, + seekManual, + moveOrJump, + onScratchStart, + onScratchMove, + onScratchEnd, + stepSize, + toggleSizeLoop, + loopIn, + loopOut, + toggleLoop, + hotcueAt, + onSlotPress, + onSlotRelease, + renameSlot, + padMenuItems, + canAutoCue, + autoOpen, + setAutoOpen, + openAutoHotcues, + existingHotcues, + autoHotcuesDone, + showMemory, + memoryAddRefusal, + callMemoryCue, + addMemoryCue, + memoryMenuItems, + renameMemoryCue, + playheadNow, + commitBpm, + gridAnchorRef, + bpmEntryRequest, + tempoProps, + prevMarker, + nextMarker, + nudgeGrid, + nudgeGridBeats, + addMarkerHere, + deleteMarkerHere, + resetGrid, + deleteGrid, + runAnalyzeGrid, + toggleStem, + shortcuts, + } +} + +export type Deck = ReturnType diff --git a/frontend/src/lib/deckKeys.test.ts b/frontend/src/lib/deckKeys.test.ts new file mode 100644 index 0000000..c1733bd --- /dev/null +++ b/frontend/src/lib/deckKeys.test.ts @@ -0,0 +1,60 @@ +import { describe, expect, it } from 'vitest' +import { keyAction, slotForDigit, type KeyLike } from './deckKeys' + +const key = (code: string, mods: Partial = {}): KeyLike => ({ + code, + shiftKey: false, + altKey: false, + metaKey: false, + ctrlKey: false, + repeat: false, + ...mods, +}) +const eight = () => 8 + +describe('keyAction', () => { + it('maps the deck keys', () => { + expect(keyAction(key('Space'), eight, false)).toEqual({ action: { kind: 'toggle' }, toOther: false }) + expect(keyAction(key('KeyC'), eight, false)?.action).toEqual({ kind: 'cue' }) + expect(keyAction(key('KeyE', { shiftKey: true }), eight, false)?.action).toEqual({ kind: 'stem', index: 2, solo: true }) + expect(keyAction(key('ArrowLeft'), eight, false)?.action).toEqual({ kind: 'move', dir: -1 }) + expect(keyAction(key('ArrowRight', { shiftKey: true }), eight, false)?.action).toEqual({ kind: 'marker', dir: 1 }) + expect(keyAction(key('Digit3'), eight, false)?.action).toEqual({ kind: 'slot', slot: 2, remove: false }) + expect(keyAction(key('Numpad1', { shiftKey: true }), eight, false)?.action).toEqual({ kind: 'slot', slot: 0, remove: true }) + expect(keyAction(key('ArrowUp', { metaKey: true }), eight, false)?.action).toEqual({ kind: 'stepSize', dir: 1 }) + expect(keyAction(key('ArrowDown', { ctrlKey: true }), eight, false)?.action).toEqual({ kind: 'stepSize', dir: -1 }) + }) + + it('leaves other keys and modified keys alone', () => { + expect(keyAction(key('KeyZ'), eight, false)).toBeNull() + expect(keyAction(key('Space', { metaKey: true }), eight, false)).toBeNull() + expect(keyAction(key('ArrowUp', { metaKey: true, shiftKey: true }), eight, false)).toBeNull() + expect(keyAction(key('Digit9'), eight, false)).toBeNull() // beyond an 8-pad bank + }) + + it('treats an auto-repeat as one press, except the size sweep', () => { + expect(keyAction(key('Space', { repeat: true }), eight, false)).toBeNull() + expect(keyAction(key('ArrowUp', { metaKey: true, repeat: true }), eight, false)?.action.kind).toBe('stepSize') + }) + + it('Alt sends the key to the other deck — and does nothing with one deck', () => { + expect(keyAction(key('Space', { altKey: true }), eight, true)).toEqual({ action: { kind: 'toggle' }, toOther: true }) + expect(keyAction(key('ArrowUp', { altKey: true, metaKey: true }), eight, true)?.toOther).toBe(true) + expect(keyAction(key('Space', { altKey: true }), eight, false)).toBeNull() + }) + + it("reads the TARGET deck's bank size for digits", () => { + const banks = (toOther: boolean) => (toOther ? 4 : 8) + expect(keyAction(key('Digit6'), banks, true)?.action).toEqual({ kind: 'slot', slot: 5, remove: false }) + expect(keyAction(key('Digit6', { altKey: true }), banks, true)).toBeNull() + }) +}) + +describe('slotForDigit', () => { + it('maps 1..9 to 0..8 and 0 to 9', () => { + expect(slotForDigit('Digit1', 16)).toBe(0) + expect(slotForDigit('Digit9', 16)).toBe(8) + expect(slotForDigit('Digit0', 16)).toBe(9) + expect(slotForDigit('KeyA', 16)).toBeNull() + }) +}) diff --git a/frontend/src/lib/deckKeys.ts b/frontend/src/lib/deckKeys.ts new file mode 100644 index 0000000..76ad70f --- /dev/null +++ b/frontend/src/lib/deckKeys.ts @@ -0,0 +1,169 @@ +// The decks' keyboard shortcuts: ONE window listener for every deck, routing +// each key to the deck it is meant for. Keys act on the FOCUSED deck; in +// performance mode, holding Alt/Option sends them to the OTHER deck instead +// (decided 2026-11-02 — Shift and Cmd/Ctrl were already taken by the keys +// themselves). With one deck, Alt does nothing, as before. +// +// Space play / pause +// C CUE (held = preview) +// 1–9, 0 hotcue slot (held = preview); Shift+ deletes it +// Q W E R mute stem 1–4; Shift+ solos it +// ← / → beat jump, or move an engaged loop +// Shift+← / → step between grid markers +// Cmd/Ctrl+↑ / ↓ loop / jump size (Shift+↑/↓ is the track table's) +// +// Keys are read from `e.code` (layout- and Shift-independent). A new or changed +// key needs `lib/shortcuts.ts` (the keyboard map) updating by hand. + +import { useEffect, type RefObject } from 'react' + +/** Keys that mute stems 1-4 (Shift = solo), by `KeyboardEvent.code`. */ +export const STEM_KEYS = ['KeyQ', 'KeyW', 'KeyE', 'KeyR'] + +/** What a deck offers the keyboard (its latest handlers). */ +export interface DeckShortcuts { + slotCount: number + toggle(): void + onCuePress(): void + onCueRelease(): void + onSlotPress(slot: number): void | Promise + onSlotRelease(slot: number): void + deleteHotcueSlot(slot: number): void | Promise + toggleStem(index: number, solo: boolean): void + moveOrJump(dir: -1 | 1): void + prevMarker(): void + nextMarker(): void + stepSize(dir: -1 | 1): void +} + +export interface DeckKeyTargets { + focused: DeckShortcuts | null + /** The other deck, in performance mode (else null: Alt does nothing). */ + other: DeckShortcuts | null +} + +export type DeckKeyAction = + | { kind: 'stepSize'; dir: -1 | 1 } + | { kind: 'toggle' } + | { kind: 'cue' } + | { kind: 'stem'; index: number; solo: boolean } + | { kind: 'move'; dir: -1 | 1 } + | { kind: 'marker'; dir: -1 | 1 } + | { kind: 'slot'; slot: number; remove: boolean } + +export interface KeyLike { + code: string + shiftKey: boolean + altKey: boolean + metaKey: boolean + ctrlKey: boolean + repeat: boolean +} + +const DIGIT_RE = /^(?:Digit|Numpad)([0-9])$/ + +/** + * Digit key -> bank slot: 1..9 map to slots 0..8 and 0 maps to slot 10. + * + * Ten digits is the physical ceiling, so a bank larger than that simply has no + * shortcut for its tail. Returns null when the key is not a digit or the slot is + * beyond this platform's bank — an 8-slot bank behaves exactly as before. + */ +export function slotForDigit(code: string, slotCount: number): number | null { + const m = code.match(DIGIT_RE) + if (!m) return null + const slot = m[1] === '0' ? 9 : Number(m[1]) - 1 + return slot < slotCount ? slot : null +} + +/** + * What a key-down means, and whether it goes to the other deck. Null = not a + * deck key (or Alt with no other deck). `slotCount` is the TARGET deck's bank. + */ +export function keyAction( + e: KeyLike, + slotCount: (toOther: boolean) => number, + hasOther: boolean, +): { action: DeckKeyAction; toOther: boolean } | null { + const toOther = e.altKey + if (toOther && !hasOther) return null + const meta = e.metaKey || e.ctrlKey + // Cmd (macOS) / Ctrl (Windows, Linux), either accepted as Cmd/Ctrl+A is. + // Repeats allowed: holding it sweeps through the sizes. + if (meta && !e.shiftKey && (e.code === 'ArrowUp' || e.code === 'ArrowDown')) { + return { action: { kind: 'stepSize', dir: e.code === 'ArrowUp' ? 1 : -1 }, toOther } + } + if (meta) return null + if (e.repeat) return null // a held key auto-repeats — one press + hold + const done = (action: DeckKeyAction) => ({ action, toOther }) + if (e.code === 'Space') return done({ kind: 'toggle' }) + if (e.code === 'KeyC') return done({ kind: 'cue' }) + const stem = STEM_KEYS.indexOf(e.code) + if (stem >= 0) return done({ kind: 'stem', index: stem, solo: e.shiftKey }) + if (e.code === 'ArrowLeft' || e.code === 'ArrowRight') { + const dir = e.code === 'ArrowLeft' ? -1 : 1 + return done(e.shiftKey ? { kind: 'marker', dir } : { kind: 'move', dir }) + } + const slot = slotForDigit(e.code, slotCount(toOther)) + if (slot != null) return done({ kind: 'slot', slot, remove: e.shiftKey }) + return null +} + +export function runKeyAction(a: DeckKeyAction, sc: DeckShortcuts): void { + switch (a.kind) { + case 'stepSize': + return sc.stepSize(a.dir) + case 'toggle': + return sc.toggle() + case 'cue': + return sc.onCuePress() + case 'stem': + return sc.toggleStem(a.index, a.solo) + case 'move': + return sc.moveOrJump(a.dir) + case 'marker': + return a.dir < 0 ? sc.prevMarker() : sc.nextMarker() + case 'slot': + void (a.remove ? sc.deleteHotcueSlot(a.slot) : sc.onSlotPress(a.slot)) + } +} + +/** + * Attach the listener once; `targets` is read at each key event, so it always + * routes to the decks as they are now. A key-up goes to the deck its key-down + * went to, so letting go of Alt first cannot strand a held preview. + */ +export function useDeckKeys(targets: RefObject): void { + useEffect(() => { + const held = new Map() + const onKey = (e: KeyboardEvent) => { + // Ignore while typing in a field. + const t = e.target as HTMLElement | null + if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable)) return + const { focused, other } = targets.current + const r = keyAction(e, (toOther) => (toOther ? other : focused)?.slotCount ?? 0, other != null) + const sc = r && (r.toOther ? other : focused) + if (!r || !sc) return + e.preventDefault() + if (r.action.kind === 'cue' || (r.action.kind === 'slot' && !r.action.remove)) held.set(e.code, sc) + runKeyAction(r.action, sc) + } + // keyup ends a held CUE / hotcue's momentary preview (release). + const onKeyUp = (e: KeyboardEvent) => { + const sc = held.get(e.code) + if (!sc) return + held.delete(e.code) + if (e.code === 'KeyC') sc.onCueRelease() + else { + const slot = slotForDigit(e.code, sc.slotCount) + if (slot != null) sc.onSlotRelease(slot) + } + } + window.addEventListener('keydown', onKey) + window.addEventListener('keyup', onKeyUp) + return () => { + window.removeEventListener('keydown', onKey) + window.removeEventListener('keyup', onKeyUp) + } + }, [targets]) +}