mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Grid and BPM can now be fully manual
This commit is contained in:
1 parent
2e04bcb9f6
commit
85f01f6723
3 files changed
+77
-30
No files matched your search
@@ -25,27 +25,51 @@ function markerTime(t: number): string {
|
||||
return `${m}:${s < 10 ? '0' : ''}${s.toFixed(3)}`
|
||||
}
|
||||
|
||||
/** The header readout's BPM cell. Click to type an exact tempo. */
|
||||
/** The header readout's BPM cell. Click to type an exact tempo — with no grid,
|
||||
* typing one creates it (the caller decides where its first marker goes). */
|
||||
export function BpmReadout({
|
||||
bpm,
|
||||
editable,
|
||||
onSetBpm,
|
||||
onClose,
|
||||
openRequest = 0,
|
||||
}: {
|
||||
/** Tempo of the marker governing the playhead; null without a grid. */
|
||||
bpm: number | null
|
||||
editable: boolean
|
||||
onSetBpm: (bpm: number) => void
|
||||
/** The editor closed, committed or not. */
|
||||
onClose?: () => void
|
||||
/** Bump to open the editor from elsewhere (Set grid on a track with no BPM). */
|
||||
openRequest?: number
|
||||
}) {
|
||||
const [editing, setEditing] = useState(false)
|
||||
const editingRef = useRef(false)
|
||||
const [val, setVal] = useState('')
|
||||
const commit = () => {
|
||||
const open = () => {
|
||||
setVal(bpm != null ? bpm.toFixed(3) : '')
|
||||
editingRef.current = true
|
||||
setEditing(true)
|
||||
}
|
||||
// Only a CHANGE opens it, so remounting with a non-zero count does not.
|
||||
const seenRequestRef = useRef(openRequest)
|
||||
useEffect(() => {
|
||||
if (openRequest === seenRequestRef.current) return
|
||||
seenRequestRef.current = openRequest
|
||||
open()
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [openRequest])
|
||||
const close = (commit: boolean) => {
|
||||
if (!editingRef.current) return // Enter commits; a blur as it unmounts must not again
|
||||
editingRef.current = false
|
||||
const n = parseFloat(val)
|
||||
if (isFinite(n) && n > 0) onSetBpm(Math.round(n * 1000) / 1000)
|
||||
if (commit && isFinite(n) && n > 0) onSetBpm(Math.round(n * 1000) / 1000)
|
||||
setEditing(false)
|
||||
onClose?.()
|
||||
}
|
||||
const label = (
|
||||
<span className="text-[10px] tracking-[0.08em] text-faint">
|
||||
BPM{editable && bpm != null ? ' ✎' : ''}
|
||||
BPM{editable ? ' ✎' : ''}
|
||||
</span>
|
||||
)
|
||||
if (editing) {
|
||||
@@ -55,26 +79,29 @@ export function BpmReadout({
|
||||
<input
|
||||
autoFocus
|
||||
value={val}
|
||||
placeholder={bpm == null ? 'BPM?' : undefined}
|
||||
onChange={(e) => setVal(e.target.value)}
|
||||
onBlur={commit}
|
||||
onBlur={() => close(true)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') commit()
|
||||
if (e.key === 'Escape') setEditing(false)
|
||||
if (e.key === 'Enter') close(true)
|
||||
if (e.key === 'Escape') close(false)
|
||||
e.stopPropagation() // Space/arrows must not drive the deck while typing
|
||||
}}
|
||||
className="w-[5.5rem] rounded-md bg-ink-800 px-1 font-mono text-[17px] font-semibold text-accent outline-none ring-1 ring-accent"
|
||||
className="w-[5.5rem] rounded-md bg-ink-800 px-1 font-mono text-[17px] font-semibold text-accent outline-none ring-1 ring-accent placeholder:text-faint"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<button
|
||||
onClick={() => {
|
||||
if (!editable || bpm == null) return
|
||||
setVal(bpm.toFixed(3))
|
||||
setEditing(true)
|
||||
}}
|
||||
title={editable && bpm != null ? 'Click to type an exact BPM' : undefined}
|
||||
onClick={() => editable && open()}
|
||||
title={
|
||||
!editable
|
||||
? undefined
|
||||
: bpm != null
|
||||
? 'Click to type an exact BPM'
|
||||
: 'Click to type a BPM — starts the beatgrid at the playhead'
|
||||
}
|
||||
className="flex flex-col justify-center gap-px rounded-r-xl px-4 text-left hover:bg-ink-850"
|
||||
>
|
||||
{label}
|
||||
|
||||
@@ -885,40 +885,53 @@ export function PrepStrip({
|
||||
if (!activeMarker) return
|
||||
editMarker(activeMarkerIndex, { start: Math.max(0, activeMarker.start + deltaMs / 1000) })
|
||||
}
|
||||
// Tap tempo retempos the governing marker, or — with no grid — creates one
|
||||
// at the tempo tapped, anchored where the run began (the first tap is a beat
|
||||
// when tapping along; the Grid-mode nudges fix the phase). While that create
|
||||
// is in flight, later taps wait for it rather than creating a second grid.
|
||||
// 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 tapCreateRef = useRef<Promise<boolean> | null>(null)
|
||||
const gridAnchorRef = useRef<number | null>(null) // Set grid's, while the readout asks
|
||||
const gridCreateRef = useRef<Promise<boolean> | null>(null)
|
||||
const [bpmEntryRequest, setBpmEntryRequest] = useState(0) // bump → readout opens for typing
|
||||
const tapStart = () => {
|
||||
tapAnchorRef.current = playheadNow()
|
||||
}
|
||||
const tapBpm = async (bpm: number) => {
|
||||
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 by a tap has exactly one marker.
|
||||
if (tapCreateRef.current) {
|
||||
if (await tapCreateRef.current) editMarker(0, { bpm })
|
||||
// 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, tapAnchorRef.current, bpm)
|
||||
.addGridMarker(track.id, anchor, bpm)
|
||||
.then((cues) => (applyCueEdit(cues), true))
|
||||
.catch((e) => (onError?.((e as Error).message), false))
|
||||
.finally(() => (tapCreateRef.current = null))
|
||||
tapCreateRef.current = create
|
||||
.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()))
|
||||
applyCueEdit(
|
||||
await api.addGridMarker(track.id, playheadNow(), markerCount === 0 ? (track.bpm ?? undefined) : undefined),
|
||||
)
|
||||
} catch (e) {
|
||||
onError?.((e as Error).message)
|
||||
}
|
||||
@@ -1182,7 +1195,13 @@ export function PrepStrip({
|
||||
</span>
|
||||
</div>
|
||||
<span aria-hidden className="my-2 w-px bg-line" />
|
||||
<BpmReadout bpm={activeMarker?.bpm ?? null} editable={canEditGrid} onSetBpm={setBpm} />
|
||||
<BpmReadout
|
||||
bpm={activeMarker?.bpm ?? null}
|
||||
editable={canEditGrid}
|
||||
onSetBpm={(bpm) => commitBpm(bpm, gridAnchorRef.current ?? playheadNow())}
|
||||
onClose={() => (gridAnchorRef.current = null)}
|
||||
openRequest={bpmEntryRequest}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
@@ -1374,7 +1393,7 @@ export function PrepStrip({
|
||||
flexible={markerCount > 1}
|
||||
markerIndex={activeMarkerIndex}
|
||||
onTapStart={tapStart}
|
||||
onTapBpm={tapBpm}
|
||||
onTapBpm={(bpm) => commitBpm(bpm, tapAnchorRef.current)}
|
||||
onNudgeBpm={nudgeBpm}
|
||||
onHalve={halveBpm}
|
||||
onDouble={doubleBpm}
|
||||
|
||||
Reference in new issue
Block a user