mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
272 lines
12 KiB
TypeScript
272 lines
12 KiB
TypeScript
import { createPortal } from 'react-dom'
|
|
import { useEffect, useState, type ReactNode } from 'react'
|
|
import { keepPreviousData, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { api, type AnalyzeOptions, type AnalyzePreview, type TrackOrigin } from '../api'
|
|
|
|
interface Props {
|
|
/** The selected tracks. */
|
|
ids: string[]
|
|
/** Whose ids they are: a device's or a folder's loose files can only have
|
|
* their loudness measured. */
|
|
origin: TrackOrigin
|
|
/** Why BPM and Grid cannot be analysed here; null = they can. */
|
|
gridWhy: string | null
|
|
/** Why a key cannot be stored here; null = it can. */
|
|
keyWhy: string | null
|
|
/** Why the DJ app's auto gain cannot be written here; null = it can. */
|
|
gainWhy: string | null
|
|
/** Opened for ONE option (the context menu's Mastering ▸ Auto gain… /
|
|
* Measure loudness): only it is ticked, and the remembered ticks are left
|
|
* as they were. */
|
|
preset?: keyof What
|
|
onRun: (opts: AnalyzeOptions) => Promise<void>
|
|
onClose: () => void
|
|
onError: (msg: string) => void
|
|
}
|
|
|
|
type What = Pick<AnalyzeOptions, 'bpm' | 'grid' | 'key' | 'loudness' | 'gain'>
|
|
const PREF_KEY = 'analyzeWhat'
|
|
// Loudness and gain off unless asked for once: they add a decode per track.
|
|
const DEFAULT_WHAT: What = { bpm: true, grid: true, key: true, loudness: false, gain: false }
|
|
|
|
/** The remembered ticks; anything malformed falls back to the default. */
|
|
function loadWhat(prefs: Record<string, unknown> | undefined): What {
|
|
const saved = prefs?.[PREF_KEY] as Partial<What> | undefined
|
|
if (!saved || typeof saved !== 'object') return DEFAULT_WHAT
|
|
const pick = (k: keyof What) => (typeof saved[k] === 'boolean' ? (saved[k] as boolean) : DEFAULT_WHAT[k])
|
|
return { bpm: pick('bpm'), grid: pick('grid'), key: pick('key'), loudness: pick('loudness'), gain: pick('gain') }
|
|
}
|
|
|
|
const plural = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`
|
|
|
|
/**
|
|
* The context menu's "Analyze…" (decided 2026-10-04): which of BPM / Grid /
|
|
* Key / Loudness to analyse, with the "already has one — replace?" questions
|
|
* folded in so there is never a second popup. BPM and Grid are independent
|
|
* halves of a beatgrid — BPM alone keeps the downbeat, Grid alone keeps the
|
|
* tempo — and the grid Replace tick only guards a FULL re-analysis (both
|
|
* ticked). Loudness goes to Konduktor's own store, so it is never disabled
|
|
* (decided 2026-10-19). Gain writes the DJ app's own auto gain from the
|
|
* loudness, only where a track has none unless Replace. The ticks are
|
|
* remembered; the Replace ticks never are — overwriting is a decision about
|
|
* THESE tracks.
|
|
*/
|
|
export function AnalyzeDialog({ ids, origin, gridWhy, keyWhy, gainWhy, preset, onRun, onClose, onError }: Props) {
|
|
const qc = useQueryClient()
|
|
const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs })
|
|
const [what, setWhat] = useState<What | null>(null)
|
|
const [replaceGrid, setReplaceGrid] = useState(false)
|
|
const [replaceKey, setReplaceKey] = useState(false)
|
|
const [replaceLoudness, setReplaceLoudness] = useState(false)
|
|
const [replaceGain, setReplaceGain] = useState(false)
|
|
const [busy, setBusy] = useState(false)
|
|
const gridEditable = gridWhy === null
|
|
const keyWritable = keyWhy === null
|
|
const gainWritable = gainWhy === null
|
|
|
|
// Hydrate once from prefs, masked by what can be stored here. Where loudness
|
|
// is ALL that can be done (a device, loose files), it is ticked.
|
|
useEffect(() => {
|
|
if (what || prefs.isPending) return
|
|
if (preset) {
|
|
setWhat({ bpm: false, grid: false, key: false, loudness: false, gain: false, [preset]: true })
|
|
return
|
|
}
|
|
const w = loadWhat(prefs.data as Record<string, unknown> | undefined)
|
|
setWhat({
|
|
bpm: w.bpm && gridEditable,
|
|
grid: w.grid && gridEditable,
|
|
key: w.key && keyWritable,
|
|
gain: w.gain && gainWritable,
|
|
loudness: w.loudness || (!gridEditable && !keyWritable && !gainWritable),
|
|
})
|
|
}, [what, preset, prefs.isPending, prefs.data, gridEditable, keyWritable, gainWritable])
|
|
|
|
const opts: AnalyzeOptions | null = what && {
|
|
...what,
|
|
replace_grid: what.bpm && what.grid && replaceGrid,
|
|
replace_key: what.key && replaceKey,
|
|
replace_loudness: what.loudness && replaceLoudness,
|
|
replace_gain: what.gain && replaceGain,
|
|
origin,
|
|
}
|
|
const preview = useQuery({
|
|
queryKey: ['analyze-preview', ids, opts],
|
|
queryFn: () => api.analyzePreview(ids, opts!),
|
|
enabled: !!opts && (opts.bpm || opts.grid || opts.key || opts.loudness || opts.gain),
|
|
placeholderData: keepPreviousData,
|
|
})
|
|
const p: AnalyzePreview | undefined = preview.data
|
|
const nothingTicked = !!what && !(what.bpm || what.grid || what.key || what.loudness || what.gain)
|
|
const count = nothingTicked ? 0 : (p?.tracks_to_analyze ?? 0)
|
|
|
|
useEffect(() => {
|
|
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && !busy && onClose()
|
|
window.addEventListener('keydown', onKey)
|
|
return () => window.removeEventListener('keydown', onKey)
|
|
}, [busy, onClose])
|
|
|
|
const run = async () => {
|
|
if (!opts || busy || count === 0) return
|
|
setBusy(true)
|
|
// A tick that was disabled here keeps what it was: analysing a device's
|
|
// loudness must not forget that the collection's keys are wanted. A
|
|
// one-option run (a preset) remembers nothing.
|
|
const saved = loadWhat(prefs.data as Record<string, unknown> | undefined)
|
|
if (!preset)
|
|
api
|
|
.patchPrefs({
|
|
[PREF_KEY]: {
|
|
bpm: gridEditable ? opts.bpm : saved.bpm,
|
|
grid: gridEditable ? opts.grid : saved.grid,
|
|
key: keyWritable ? opts.key : saved.key,
|
|
gain: gainWritable ? opts.gain : saved.gain,
|
|
loudness: opts.loudness,
|
|
},
|
|
})
|
|
.then((x) => qc.setQueryData(['prefs'], x))
|
|
.catch(() => {})
|
|
try {
|
|
await onRun(opts)
|
|
} catch (e) {
|
|
onError((e as Error).message)
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
const toggle = (k: keyof What, label: string, help: string, enabled: boolean, why: string) => (
|
|
<label
|
|
className={`flex items-start gap-3 rounded-md px-2 py-1.5 ${enabled ? 'cursor-pointer hover:bg-ink-800' : 'opacity-40'}`}
|
|
title={enabled ? undefined : why}
|
|
>
|
|
<input
|
|
type="checkbox"
|
|
className="mt-0.5"
|
|
checked={!!what?.[k]}
|
|
disabled={!enabled || !what}
|
|
onChange={(e) => setWhat((w) => w && { ...w, [k]: e.target.checked })}
|
|
/>
|
|
<span className="min-w-0">
|
|
<span className="block text-sm text-text">{label}</span>
|
|
<span className="block text-xs text-faint">{help}</span>
|
|
</span>
|
|
</label>
|
|
)
|
|
|
|
// What the grid ticks will do, in the dialog's own words.
|
|
const gridLines: ReactNode[] = []
|
|
if (p && what && (what.bpm || what.grid)) {
|
|
if (what.bpm && what.grid) {
|
|
if (p.with_grid > 0)
|
|
gridLines.push(
|
|
<div key="rg" className="flex items-center justify-between gap-3">
|
|
<span>
|
|
<span className="tabular-nums text-text">{p.with_grid}</span>{' '}
|
|
{p.with_grid === 1 ? 'already has a grid' : 'already have a grid'}
|
|
</span>
|
|
<label className="flex shrink-0 items-center gap-1.5 text-xs" title="Replacing discards any hand edits to those grids">
|
|
<input type="checkbox" checked={replaceGrid} onChange={(e) => setReplaceGrid(e.target.checked)} />
|
|
Replace {p.with_grid === 1 ? 'it' : 'them'}
|
|
</label>
|
|
</div>,
|
|
)
|
|
} else if (what.bpm) {
|
|
if (p.grid_bpm) gridLines.push(<div key="b">{plural(p.grid_bpm, 'grid')}: tempo re-detected, downbeat kept</div>)
|
|
if (p.grid_full) gridLines.push(<div key="bf">{plural(p.grid_full, 'track')} without a grid: analyzed in full</div>)
|
|
} else {
|
|
if (p.grid_phase) gridLines.push(<div key="g">{plural(p.grid_phase, 'track')}: downbeats found at the current BPM</div>)
|
|
if (p.grid_full) gridLines.push(<div key="gf">{plural(p.grid_full, 'track')} without a BPM: analyzed in full</div>)
|
|
}
|
|
if (p.grid_flexible)
|
|
gridLines.push(
|
|
<div key="fx" className="text-faint" title="One BPM or downbeat for the whole track would flatten its tempo changes">
|
|
{plural(p.grid_flexible, 'multi-tempo grid')} — skipped
|
|
</div>,
|
|
)
|
|
if (p.grid_locked) gridLines.push(<div key="lk" className="text-faint">{plural(p.grid_locked, 'locked grid')} — skipped</div>)
|
|
}
|
|
|
|
return createPortal(
|
|
<div
|
|
aria-modal="true"
|
|
className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 backdrop-blur-[3px] p-6"
|
|
onClick={() => !busy && onClose()}
|
|
>
|
|
<div className="w-full max-w-sm overflow-hidden glass-overlay" onClick={(e) => e.stopPropagation()}>
|
|
<div className="border-b border-line px-5 py-4">
|
|
<div className="text-[15px] font-semibold tracking-tight">Analyze</div>
|
|
<div className="text-xs text-muted">{plural(ids.length, 'track')} selected</div>
|
|
</div>
|
|
|
|
<div className="space-y-0.5 px-3 pt-3">
|
|
{toggle('bpm', 'BPM', 'Detect the tempo', gridEditable, gridWhy ?? '')}
|
|
{toggle('grid', 'Grid', 'Find where the downbeats fall', gridEditable, gridWhy ?? '')}
|
|
{toggle('key', 'Key', 'Detect the musical key', keyWritable, keyWhy ?? '')}
|
|
{toggle('loudness', 'Loudness', 'Measure LUFS, RMS, true peak and dynamics', true, '')}
|
|
{toggle('gain', 'Auto gain', "Set the DJ app's own gain from the loudness (measured first if needed)",
|
|
gainWritable, gainWhy ?? '')}
|
|
</div>
|
|
|
|
<div className={`space-y-1.5 px-5 py-3 text-sm text-muted ${preview.isFetching ? 'opacity-70' : ''}`}>
|
|
{gridLines}
|
|
{p && what?.key && p.with_key > 0 && (
|
|
<div className="flex items-center justify-between gap-3">
|
|
<span>
|
|
<span className="tabular-nums text-text">{p.with_key}</span>{' '}
|
|
{p.with_key === 1 ? 'already has a key' : 'already have a key'}
|
|
</span>
|
|
<label className="flex shrink-0 items-center gap-1.5 text-xs">
|
|
<input type="checkbox" checked={replaceKey} onChange={(e) => setReplaceKey(e.target.checked)} />
|
|
Replace {p.with_key === 1 ? 'it' : 'them'}
|
|
</label>
|
|
</div>
|
|
)}
|
|
{p && what?.loudness && p.with_loudness > 0 && (
|
|
<div className="flex items-center justify-between gap-3">
|
|
<span>
|
|
<span className="tabular-nums text-text">{p.with_loudness}</span> already measured
|
|
</span>
|
|
<label className="flex shrink-0 items-center gap-1.5 text-xs">
|
|
<input type="checkbox" checked={replaceLoudness} onChange={(e) => setReplaceLoudness(e.target.checked)} />
|
|
Measure again
|
|
</label>
|
|
</div>
|
|
)}
|
|
{p && what?.gain && p.with_gain > 0 && (
|
|
<div className="flex items-center justify-between gap-3">
|
|
<span>
|
|
<span className="tabular-nums text-text">{p.with_gain}</span>{' '}
|
|
{p.with_gain === 1 ? 'already has an auto gain' : 'already have an auto gain'}
|
|
</span>
|
|
<label className="flex shrink-0 items-center gap-1.5 text-xs"
|
|
title="Replacing puts Konduktor's measure in place of the DJ app's own">
|
|
<input type="checkbox" checked={replaceGain} onChange={(e) => setReplaceGain(e.target.checked)} />
|
|
Replace {p.with_gain === 1 ? 'it' : 'them'}
|
|
</label>
|
|
</div>
|
|
)}
|
|
{nothingTicked && <div className="text-faint">Tick what to analyze.</div>}
|
|
</div>
|
|
|
|
<div className="flex items-center justify-end gap-2 border-t border-line px-5 py-3">
|
|
<button
|
|
onClick={onClose}
|
|
disabled={busy}
|
|
className="rounded-md px-3 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
onClick={run}
|
|
disabled={busy || count === 0 || !p}
|
|
className="rounded-full btn-primary px-4 py-1.5 text-sm font-semibold disabled:opacity-40"
|
|
>
|
|
{count === 0 && p && !nothingTicked ? 'Nothing to analyze' : `Analyze${count ? ` ${count}` : ''}`}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>,
|
|
document.body,
|
|
)
|
|
}
|