import { useEffect, useRef } from 'react' import { bendFor, PITCH_RANGES } from '../../lib/sync' import { Fader } from '../mixer/Controls' import type { Deck } from './useDeck' interface Props { deck: Deck /** This deck is the tempo master (the one SYNC follows). */ master: boolean synced: boolean /** Why SYNC cannot engage (no grid on a deck), else null. */ syncBlocked: string | null onSync: () => void onMaster: () => void /** Moving the pitch fader — or bending — by hand takes the deck out of SYNC. */ onManualPitch: () => void /** This deck's beats against the other deck's, in beats (-0.5..0.5); null * when either has no grid or is not playing. Read every frame. */ phase: () => number | null } const lit = 'is-selected text-text' const unlit = 'btn-glass text-muted' /** * A deck's tempo row in performance mode (decided 2026-11-02): keylock, SYNC, * MASTER, the phase meter, the pitch fader with its range, and the tempo as * heard. The pitch fader is the deck's SPEED — the grid's BPM is still edited * in the header and Grid mode. */ export function DeckTempo({ deck, master, synced, syncBlocked, onSync, onMaster, onManualPitch, phase }: Props) { const { pitch, setPitch, pitchRange, changeRange, rate, setBend, keylock, setKeylock, keylockAvailable, activeMarker, track } = deck const bpm = activeMarker ? activeMarker.bpm * rate : null const pct = pitch * pitchRange * 100 const nextRange = PITCH_RANGES[(PITCH_RANGES.indexOf(pitchRange) + 1) % PITCH_RANGES.length] return (
{ if (b !== 1) onManualPitch() setBend(b) }} /> { setPitch(Math.abs(v) < 0.01 ? 0 : v) onManualPitch() }} label="Pitch" describe={() => `Pitch ${fmtPct(pct)} (range ±${Math.round(pitchRange * 100)}%) — double-click to reset`} className="min-w-16 flex-1" />
{bpm ? bpm.toFixed(2) : '—'} {fmtPct(pct)}
) } const fmtPct = (p: number) => `${p > 0 ? '+' : p < 0 ? '−' : '±'}${Math.abs(p).toFixed(2)}%` /** Where this deck's beats fall against the other deck's: centre = in phase, * right = ahead. Drawn every frame straight to the DOM. DRAG it to bend the * deck's speed — right faster, left slower — and let go to spring back, the * way a DJ nudges a platter to bring the beats together. */ function PhaseMeter({ phase, onBend, disabled, }: { phase: () => number | null onBend: (bend: number) => void disabled: boolean }) { const markRef = useRef(null) const dragX = useRef(null) const release = () => { if (dragX.current == null) return dragX.current = null onBend(1) } const read = useRef(phase) read.current = phase useEffect(() => { let raf = 0 const tick = () => { const el = markRef.current const p = read.current() if (el) { el.style.opacity = p == null ? '0' : '1' if (p != null) { el.style.left = `${(p + 0.5) * 100}%` el.style.background = Math.abs(p) < 0.03 ? 'var(--color-mint)' : 'var(--color-gold)' } } raf = requestAnimationFrame(tick) } raf = requestAnimationFrame(tick) return () => cancelAnimationFrame(raf) }, []) return (
{ if (disabled || e.button !== 0) return e.preventDefault() e.currentTarget.setPointerCapture(e.pointerId) dragX.current = e.clientX }} onPointerMove={(e) => dragX.current != null && onBend(bendFor(e.clientX - dragX.current, e.shiftKey))} onPointerUp={release} onPointerCancel={release} onLostPointerCapture={release} className={`well relative h-3 w-20 shrink-0 touch-none overflow-hidden rounded-full ${disabled ? '' : 'cursor-ew-resize'}`} title="Phase: where this deck's beats fall against the other deck's (centre = together). Drag to bend the speed — right faster, left slower (Shift: finer)" >
) }