Implemented track loudness analysis

This commit is contained in:
patben8 committed 2026-10-19 14:07:54 +02:00
1 parent 5c9387f136
commit d70a7c5de2
28 files changed
+1732 -144

No files matched your search

+72 -26
View File
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { keepPreviousData, useQuery, useQueryClient } from '@tanstack/react-query'
import type { ColumnSizingState, SortingState, VisibilityState } from '@tanstack/react-table'
import { CapabilitiesContext, slotLabeller } from './lib/capabilities'
import { readOnlyNotice, removalNote, writeHint } from './lib/platformCopy'
@@ -13,6 +13,7 @@ import { confirmDiscardUnsaved } from './lib/unsaved'
import { askConfirm } from './lib/confirm'
import {
COLUMN_MENU,
MASTERING_COLUMNS,
DEFAULT_COLUMN_ORDER,
DEFAULT_COLUMN_VISIBILITY,
} from './lib/trackColumns'
@@ -101,7 +102,9 @@ export default function App() {
// job, polled into the status bar. ONE at a time across both kinds — the
// backend refuses a second, since a grid run would move the beats a hotcue
// run is placing cues on.
const [analyzing, setAnalyzing] = useState<string[] | null>(null) // the Analyze dialog's selection
// The Analyze dialog's selection, and whose ids they are (a device's or a
// folder's loose files can only have their loudness measured).
const [analyzing, setAnalyzing] = useState<{ ids: string[]; origin: TrackOrigin } | null>(null)
const [cueBatch, setCueBatch] = useState<{ ids: string[]; withoutGrid: number } | null>(null)
const [batchJob, setBatchJob] = useState<{ id: string; kind: 'grid' | 'cues' | 'stems' } | null>(null)
// Convert to Stems: the dialog's selection, a finished run's Details, and a
@@ -143,6 +146,12 @@ export default function App() {
setColumnOrder(DEFAULT_COLUMN_ORDER)
setColumnSizing({})
}, [])
/** One checkbox row of the header's column chooser. */
const columnToggle = (id: string, label: string) => ({
label,
checked: columnVisibility[id] !== false,
onToggle: () => setColumnVisibility((v) => ({ ...v, [id]: v[id] === false })),
})
// Hydrate the saved layout once, tolerating partial / stale (new columns
// added since) data by merging against the current defaults.
@@ -563,7 +572,7 @@ export default function App() {
: source.name
const isAll = source.kind === 'all'
const tracks: Track[] = viewingFolder
const listed: Track[] = viewingFolder
? (folderView.data?.tracks ?? [])
: viewingDevice
? source.kind === 'device'
@@ -578,6 +587,30 @@ export default function App() {
: isAll
? (allTracks.data?.items ?? [])
: (playlistTracks.data ?? [])
// The Mastering columns' values: Konduktor's own store, keyed by the FILE
// (`/api/loudness`), joined into the rows — and only fetched while one of
// those columns is shown, since it stats every listed file. Keyed by a hash
// of the ids, so a list whose membership changes asks again; an analysis
// invalidates `['loudness']` itself.
const viewOrigin: TrackOrigin = viewingFolder ? 'folder' : viewingDevice ? 'device' : 'collection'
const masteringShown = MASTERING_COLUMNS.some((id) => columnVisibility[id] === true)
const listedIds = useMemo(() => listed.map((t) => t.id), [listed])
const listedKey = useMemo(() => {
let h = 5381
for (const id of listedIds) for (let i = 0; i < id.length; i++) h = ((h << 5) + h + id.charCodeAt(i)) | 0
return `${listedIds.length}:${h}`
}, [listedIds])
const loudness = useQuery({
queryKey: ['loudness', viewOrigin, listedKey],
queryFn: () => api.loudness(viewOrigin, listedIds),
enabled: masteringShown && listedIds.length > 0,
placeholderData: keepPreviousData,
})
const tracks: Track[] = useMemo(() => {
const values = masteringShown ? loudness.data?.values : undefined
if (!values) return listed
return listed.map((t) => (values[t.id] ? { ...t, loudness: values[t.id] } : t))
}, [listed, loudness.data, masteringShown])
// The deck holds a SNAPSHOT of its track, taken when it was loaded. When the
// view it came from refetches (Analyze wrote its key, Edit Tags its title),
// follow the fresh copy so the deck's KEY readout and header are current.
@@ -729,6 +762,7 @@ export default function App() {
if (!job.result) return
invalidateTrackLists(qc)
qc.invalidateQueries({ queryKey: ['facets'] })
qc.invalidateQueries({ queryKey: ['loudness'] })
const cancelled = job.state === 'cancelled'
const plural = (n: number, w: string) => `${n} ${w}${n === 1 ? '' : 's'}`
let touched: string[]
@@ -744,18 +778,21 @@ export default function App() {
g.bpm ? `${plural(g.bpm, 'BPM')} (downbeat kept)` : '',
g.phase ? `${plural(g.phase, 'grid')} at its BPM` : '',
r.key.set ? plural(r.key.set, 'key') : '',
r.loudness?.measured ? `loudness of ${r.loudness.measured}` : '',
].filter(Boolean)
const skipped = [
g.existing ? `${g.existing} with a grid` : '',
g.locked ? `${g.locked} locked` : '',
g.flexible ? `${g.flexible} multi-tempo` : '',
r.key.existing ? `${r.key.existing} with a key` : '',
r.loudness?.existing ? `${r.loudness.existing} already measured` : '',
].filter(Boolean)
parts = [
`${cancelled ? 'Cancelled — analyzed' : 'Analyzed'} ${plural(r.analysed.length, 'track')}` +
(wrote.length ? `: ${wrote.join(', ')}` : ''),
skipped.length ? `skipped ${skipped.join(', ')}` : '',
r.key.none ? `no key found in ${r.key.none}` : '',
r.loudness?.none ? `nothing to measure in ${r.loudness.none} (silent or under 3 s)` : '',
]
} else {
const r = job.result as unknown as CueBatchResult
@@ -958,11 +995,22 @@ export default function App() {
setBatchCancelling(false)
setBatchJob({ id: job.id, kind: 'grid' })
}
// Offered where ANY of BPM / Grid / Key can be stored; the dialog disables
// the rest with a reason.
// Offered for ANY selection: loudness goes to Konduktor's own store, so it
// can always be measured (decided 2026-10-19). The dialog disables BPM /
// Grid / Key with a reason where they cannot be stored.
const canAnalyzeGrid = !!capabilities.data?.grid.editable
const canAnalyzeKey = !!capabilities.data?.tracks.key_writable
const canAnalyze = canEdit && !viewingDevice && (canAnalyzeGrid || canAnalyzeKey)
const analyzeOrigin = (ids: string[]): TrackOrigin =>
viewingDevice ? 'device' : viewingFolder && ids.some((id) => !folderFiles[id]) ? 'folder' : 'collection'
/** Why BPM / Grid / Key cannot be analysed for these tracks at all, if so. */
const analyzeRefusal = (origin: TrackOrigin): string | null =>
origin === 'device'
? 'A device is read-only: only its loudness can be measured'
: origin === 'folder'
? 'Add these files to your collection first'
: !canEdit
? ((capabilities.data && readOnlyNotice(capabilities.data)) ?? 'This library is read-only')
: null
const startCueBatch = (ids: string[]) => {
const byId = new Map(tracks.map((t) => [t.id, t]))
@@ -1273,16 +1321,14 @@ export default function App() {
},
]
: []),
...(canAnalyze
? [
{
label: 'Analyze…',
hint: batchJob ? 'busy' : menu.ids.length > 1 ? String(menu.ids.length) : undefined,
disabled: !!batchJob,
onClick: gated(() => setAnalyzing(menu.ids)),
},
]
: []),
// No "add it first" detour for loose files: their loudness can be
// measured where they are; the dialog explains the rest.
{
label: 'Analyze…',
hint: batchJob ? 'busy' : menu.ids.length > 1 ? String(menu.ids.length) : undefined,
disabled: !!batchJob,
onClick: () => setAnalyzing({ ids: menu.ids, origin: analyzeOrigin(menu.ids) }),
},
// The deck's ✨ Auto covers a single loaded track, with its
// existing cues shown; this is the same template over the selection.
...(canAutoCue
@@ -1356,12 +1402,11 @@ export default function App() {
items={[
{ heading: 'Columns' },
// Default-visible unless explicitly hidden, as in the Columns menu.
...COLUMN_MENU.map((c) => ({
label: c.label,
checked: columnVisibility[c.id] !== false,
onToggle: () =>
setColumnVisibility((v) => ({ ...v, [c.id]: v[c.id] === false })),
})),
...COLUMN_MENU.filter((c) => !c.group).map((c) => columnToggle(c.id, c.label)),
{
label: 'Mastering',
submenu: COLUMN_MENU.filter((c) => c.group === 'mastering').map((c) => columnToggle(c.id, c.label)),
},
{ separator: true as const },
{ label: 'Reset to defaults', onClick: resetColumns },
]}
@@ -1411,10 +1456,11 @@ export default function App() {
)}
{analyzing && (
<AnalyzeDialog
ids={analyzing}
gridEditable={canAnalyzeGrid}
keyWritable={canAnalyzeKey}
onRun={(opts) => runAnalysis(analyzing, opts)}
ids={analyzing.ids}
origin={analyzing.origin}
gridWhy={analyzeRefusal(analyzing.origin) ?? (canAnalyzeGrid ? null : "This library's beatgrids cannot be edited")}
keyWhy={analyzeRefusal(analyzing.origin) ?? (canAnalyzeKey ? null : 'This library cannot store a key')}
onRun={(opts) => runAnalysis(analyzing.ids, opts)}
onClose={() => setAnalyzing(null)}
onError={onError}
/>