import { useRef, useState } from 'react' interface Props { bpm: number | null locked: boolean hasGrid: boolean onSetBpm: (bpm: number) => void onNudgeBpm: (delta: number) => void onHalve: () => void onDouble: () => void onNudge: (deltaMs: number) => void onSetHere: () => void onReset: () => void onToggleLock: () => void onDeleteGrid: () => void /** Detect BPM + first beat and build the grid (sets hotcue 1 + anchor). */ onAnalyze: () => void analyzing: boolean } const BTN = 'flex flex-1 items-center justify-center rounded border border-line bg-ink-850 py-1 text-xs ' + 'font-semibold text-text transition-colors hover:border-accent disabled:opacity-30 ' + 'disabled:hover:border-line' /** * Beatgrid / tempo panel (Traktor grid-control equivalent): editable BPM, BPM * nudge (fine ±0.01 / coarse ±0.25), half/double, tap tempo, grid-phase nudge * (±1/±10 ms), set beat 1 at the playhead, reset to the loaded values, lock, * and delete grid. */ export function GridControls({ bpm, locked, hasGrid, onSetBpm, onNudgeBpm, onHalve, onDouble, onNudge, onSetHere, onReset, onToggleLock, onDeleteGrid, onAnalyze, analyzing, }: Props) { const [editing, setEditing] = useState(false) const [val, setVal] = useState('') const tapsRef = useRef([]) const commit = () => { const n = parseFloat(val) if (isFinite(n) && n > 0) onSetBpm(Math.round(n * 1000) / 1000) setEditing(false) } const tap = () => { const now = performance.now() const taps = tapsRef.current if (taps.length && now - taps[taps.length - 1] > 2000) taps.length = 0 // reset after a pause taps.push(now) if (taps.length > 8) taps.shift() if (taps.length >= 2) { const first = taps[0] const avg = (now - first) / (taps.length - 1) onSetBpm(Math.round((60000 / avg) * 1000) / 1000) } } return (
{/* BPM readout / editor */}
{editing ? ( setVal(e.target.value)} onBlur={commit} onKeyDown={(e) => { if (e.key === 'Enter') commit() if (e.key === 'Escape') setEditing(false) }} className="w-28 rounded bg-ink-850 py-0.5 text-center text-2xl font-semibold tabular-nums text-text outline-none" /> ) : ( )}
BPM
{/* BPM nudge — coarse ±0.25 (outer) / fine ±0.01 (inner) */}
BPM
{/* halve / double / tap / reset */}
{/* grid-phase nudge + set beat 1 */}
Grid
{/* lock / delete grid */}
{/* analyze: detect BPM + first beat, set hotcue 1 + grid anchor */}
) }