Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c095c7a
Add a cue survey of how a playlist's cues meet the grid and phrases
Nov 28, 2026
c38d1ca
Format the native crates with rustfmt
Nov 28, 2026
0d0b5e0
Clear the native clippy warnings
Nov 28, 2026
292c90c
Find and name a track's sections
Nov 28, 2026
79af27c
Save our sections as rekordbox phrases during analysis
Nov 28, 2026
0cd545a
Show a track's phrases on the deck overview, as a developer option
Nov 28, 2026
d71fc1d
Show phrases on the scrolling waveform too
Nov 28, 2026
f45ccc5
Stop one miscounted section from shifting every phrase after it
Nov 28, 2026
9d5577a
Let a track's last section be a drop
Nov 28, 2026
963f85d
Learn a DJ's cue style from tracks they have cued
Nov 28, 2026
f640cd9
Auto cue tracks in the style of a cue palette
Nov 28, 2026
d295dc2
Edit a track's phrases from the deck, as a developer option
Nov 28, 2026
dcc6e08
Score our sections against hand-set phrases
Nov 28, 2026
e142012
Drop the by-genre summary from phrase_score
Nov 28, 2026
fba1398
Find phrase starts with finer, per-group novelty on a 4-bar lattice
Nov 28, 2026
f1c749f
Label sections by a stricter drop, drums alone, and the intro's length
Nov 28, 2026
2d1e3d7
Let cue rules count runs of a kind, and keep the most precise cues
Nov 28, 2026
a49eb74
Clear memory or hot cues from tracks from their context menu
Nov 28, 2026
34f081f
Count hand-placed cues on the beat within a quarter of one, and score…
Nov 28, 2026
6e71693
Refactor cue style logic and enhance memory cue placement
Nov 29, 2026
e6b2b7f
Implement CueStyleCache for persistent storage of learnt cue styles a…
Nov 29, 2026
acedee4
fix decks not picking up new cues after auto place
Nov 29, 2026
1385242
replace any cues when auto cueing
Nov 29, 2026
80045b9
improve cue pallet dialog
Nov 29, 2026
3bf1a2c
group cue options and remove playlists context options
Nov 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions app/src/main/analysis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BrowserWindow, ipcMain } from 'electron'
import type { AnalysisFinished, AnalysisJob, AnalysisProgress } from '../preload/analysis'
import type { NativeModule } from './audio/native'
import type { AnalysisFinished, AnalysisJob, AnalysisProgress, SectionStart } from '../preload/analysis'
import type { NativeGrid, NativeModule } from './audio/native'
import type { Library } from './library'

const MAX_CONCURRENCY = 4
Expand All @@ -27,6 +27,28 @@ export class AnalysisQueue {
this.enqueue(jobs, concurrency)
)
ipcMain.handle('analysis:cancel', () => this.cancel())
ipcMain.handle('analysis:sections', (_e, id: string) => this.sections(id))
}

/** The sections our analysis finds on the track's own grid, without writing anything. */
private async sections(id: string): Promise<SectionStart[]> {
const native = this.native()
if (!native) throw new Error('The analyser could not be loaded')
const path = await this.library.call<string | null>('media.resolve', { kind: 'audio', id, size: '' })
const { beatgrid } = await this.library.call<{ beatgrid: NativeGrid | null }>('track.detail', { id })
if (!path || !beatgrid?.times.length) throw new Error('Analyse this track before finding its phrases')
const result = await native.analyseTrack(path, {
grid: false,
key: false,
sections: true,
keptGrid: beatgrid,
dynamic: false,
minBpm: 0,
maxBpm: 0,
highPrecision: false,
roundBpm: false
})
return result.sections as SectionStart[]
}

private enqueue(jobs: AnalysisJob[], concurrency: number): void {
Expand Down Expand Up @@ -80,6 +102,7 @@ export class AnalysisQueue {
const result = await native.analyseTrack(path, {
grid: options.grid,
key: options.key,
sections: options.phrases,
dynamic: options.gridMode === 'dynamic',
minBpm: options.minBpm,
maxBpm: options.maxBpm,
Expand All @@ -95,6 +118,7 @@ export class AnalysisQueue {
bpm: result.bpm,
beats,
key: result.key ?? null,
sections: result.sections,
waveform: waveform.toString('base64')
},
options: { grid: options.grid, key: options.key, overwrite: options.overwrite }
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/audio/native.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { app } from 'electron'
import { join, sep } from 'path'
import { statSync } from 'fs'

export interface NativeDeckEvent {
kind: 'loaded' | 'error'
Expand Down Expand Up @@ -67,9 +68,18 @@ export interface NativeEngine {
close(): void
}

export interface NativeGrid {
times: number[]
beats: number[]
bpms: number[]
}

export interface NativeTrackAnalysisOptions {
grid: boolean
key: boolean
sections: boolean
/** Sections are found on this grid, and no new grid is detected. */
keptGrid?: NativeGrid | null
dynamic: boolean
minBpm: number
maxBpm: number
Expand All @@ -89,6 +99,21 @@ export interface NativeTrackAnalysis {
key?: { tonic: number; minor: boolean } | null
/** Eight bytes per 1/150 s: peak and RMS of the whole signal, then the low, mid and high bands. */
waveform: Uint8Array
/** `firstBeat` indexes the kept grid when one was given, otherwise the beats above. */
sections: { firstBeat: number; kind: string }[]
}

export interface NativeCuedTrack {
path: string
grid: NativeGrid
/** `kind` is 0 for a memory cue, otherwise the hot cue's slot as rekordbox numbers it. */
cues: { kind: number; startMs: number }[]
}

export interface NativePlacedCue {
/** 0 for a memory cue, otherwise the hot cue's slot as rekordbox numbers it. */
kind: number
startMs: number
}

export interface NativeLibrary {
Expand All @@ -103,9 +128,13 @@ export interface NativeLibrary {
playlistTracks(id: string): Promise<unknown>
historyTracks(id: string): Promise<unknown>
trackDetail(id: string): Promise<unknown>
trackCues(ids: string[]): Promise<unknown>
mediaResolve(kind: string, id: string, size: string): Promise<unknown>
cueSetHot(id: string, kind: number, ms: number, endMs: number | null): Promise<unknown>
cueAddMemory(id: string, ms: number, endMs: number | null): Promise<unknown>
cuePlaceAuto(id: string, hot: boolean, kinds: number[], startsMs: number[]): Promise<unknown>
trackSetPhrases(id: string, body: string): Promise<unknown>
cueClear(ids: string[], memory: boolean, hot: boolean): Promise<unknown>
cueDelete(id: string, cueId: string): Promise<unknown>
cueSetColor(id: string, cueId: string, colorIndex: number | null): Promise<unknown>
onEvent(callback: (event: string, data: string) => void): void
Expand Down Expand Up @@ -164,6 +193,9 @@ export interface NativeModule {
RekordboxLibrary: new () => NativeLibrary
analyse(path: string, bands: boolean, quality: number): Promise<NativeWave | null>
analyseTrack(path: string, options: NativeTrackAnalysisOptions): Promise<NativeTrackAnalysis>
/** A cue style, as JSON for `placeCues`, learnt from the tracks' memory cues or, with `hot`, hot cues. */
learnCueStyle(tracks: NativeCuedTrack[], hot: boolean): Promise<string>
placeCues(path: string, grid: NativeGrid, style: string): Promise<NativePlacedCue[]>
}

/** Built by `npm run build:native` into resources/, which electron-builder unpacks from the asar. */
Expand All @@ -174,6 +206,12 @@ function addonPath(): string {
)
}

/** Changes whenever the addon is rebuilt, so results it learnt can be told from a newer build's. */
export function nativeBuild(): string {
const { size, mtimeMs } = statSync(addonPath())
return `${size}:${mtimeMs}`
}

export function loadNative(): NativeModule {
const module = { exports: {} as NativeModule }
process.dlopen(module, addonPath())
Expand Down
148 changes: 148 additions & 0 deletions app/src/main/autoCue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { BrowserWindow, ipcMain } from 'electron'
import type { AutoCueFinished, AutoCueProgress, AutoCueRequest, CueSort } from '../preload/autoCue'
import { isOwnCue } from '../shared/cues'
import { nativeBuild, type NativeCuedTrack, type NativeGrid, type NativeModule } from './audio/native'
import { CueStyleCache } from './cueStyleCache'
import type { Library } from './library'

const CONCURRENCY = 3

interface DetailCue {
kind: 'hot' | 'memory'
slot: number
start: number
comment: string
}

interface Detail {
cues: DetailCue[]
beatgrid: NativeGrid | null
}

const ofSort = (cue: DetailCue, sort: CueSort): boolean => cue.kind === sort

/**
* Learns a cue style from the palette's tracks, then places cues with it on other tracks a few at
* a time and writes them, marked automatic, in place of every cue of the sort they had. Progress
* goes to every window on `autoCue:progress`.
*/
export class AutoCue {
private progress: AutoCueProgress | null = null
private queue: string[] = []
private styles = new CueStyleCache()

constructor(
private library: Library,
private native: () => NativeModule | null
) {}

register(): void {
ipcMain.handle('autoCue:run', (_e, request: AutoCueRequest) => this.run(request))
ipcMain.handle('autoCue:learn', (_e, paletteIds: string[], sort: CueSort) => this.learn(paletteIds, sort))
ipcMain.handle('autoCue:cancel', () => this.cancel())
}

private addon(): NativeModule {
const native = this.native()
if (!native) throw new Error('The analyser could not be loaded')
return native
}

private async source(id: string): Promise<{ path: string; detail: Detail } | null> {
const path = await this.library.call<string | null>('media.resolve', { kind: 'audio', id, size: '' })
const detail = await this.library.call<Detail>('track.detail', { id })
return path && detail.beatgrid?.times.length ? { path, detail } : null
}

/**
* The style, as JSON, learnt from the DJ's own cues of `sort` on the palette's tracks, or kept
* from the last time it was learnt from the same tracks, cues and learner.
*/
private async learn(paletteIds: string[], sort: CueSort): Promise<string> {
const tracks: NativeCuedTrack[] = []
for (const id of paletteIds) {
const found = await this.source(id)
if (!found?.detail.beatgrid) continue
const cues = found.detail.cues
.filter((cue) => isOwnCue(cue) && ofSort(cue, sort))
.map((cue) => ({ kind: cue.slot, startMs: cue.start }))
if (cues.length) tracks.push({ path: found.path, grid: found.detail.beatgrid, cues })
}
if (!tracks.length) throw new Error(`No track in the cue palette has ${sort} cues to learn from`)
const learntFrom = CueStyleCache.fingerprint({ learner: nativeBuild(), tracks })
const kept = this.styles.style(sort, learntFrom)
if (kept) return kept
const style = await this.addon().learnCueStyle(tracks, sort === 'hot')
this.styles.keep(sort, learntFrom, style)
return style
}

private async run({ trackIds, paletteIds, sort }: AutoCueRequest): Promise<void> {
if (this.progress) throw new Error('Auto cue is already running')
this.progress = { learning: true, total: trackIds.length, done: 0, failed: 0 }
this.broadcast()
let style: string
try {
style = await this.learn(paletteIds, sort)
} catch (err) {
this.progress = { ...this.progress, learning: false, error: (err as Error).message }
this.broadcast()
this.progress = null
return
}
this.progress = { ...this.progress, learning: false }
this.queue = [...trackIds]
this.broadcast()
await Promise.all(Array.from({ length: CONCURRENCY }, () => this.work(style, sort)))
this.progress = null
}

private async work(style: string, sort: CueSort): Promise<void> {
for (let id = this.queue.shift(); id != null; id = this.queue.shift()) {
this.finish(await this.cue(id, style, sort))
}
}

private async cue(id: string, style: string, sort: CueSort): Promise<AutoCueFinished> {
try {
const found = await this.source(id)
if (!found?.detail.beatgrid) return { id, error: 'No beatgrid or audio file; analyse the track first' }
const placed = await this.addon().placeCues(found.path, found.detail.beatgrid, style)
const cues = await this.library.call<unknown[]>('cue.placeAuto', {
id,
hot: sort === 'hot',
kinds: placed.map((cue) => cue.kind),
startsMs: placed.map((cue) => cue.startMs)
})
return { id, cues }
} catch (err) {
return { id, error: (err as Error).message }
}
}

private finish(finished: AutoCueFinished): void {
if (!this.progress) return
this.progress = {
...this.progress,
done: this.progress.done + (finished.cues ? 1 : 0),
failed: this.progress.failed + (finished.error ? 1 : 0)
}
this.broadcast(finished)
}

/** Drops the tracks still waiting. Those already being cued finish and are saved. */
private cancel(): void {
if (!this.progress) return
this.progress = { ...this.progress, total: this.progress.total - this.queue.length }
this.queue = []
this.broadcast()
}

private broadcast(finished?: AutoCueFinished): void {
if (!this.progress) return
const progress: AutoCueProgress = { ...this.progress, ...(finished ? { finished } : {}) }
for (const win of BrowserWindow.getAllWindows()) {
if (!win.isDestroyed()) win.webContents.send('autoCue:progress', progress)
}
}
}
58 changes: 58 additions & 0 deletions app/src/main/cueStyleCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { app } from 'electron'
import { createHash } from 'crypto'
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'fs'
import { dirname, join } from 'path'
import type { CueSort } from '../preload/autoCue'

const FILE_NAME = 'cue-styles.json'

interface Learnt {
learntFrom: string
style: string
}

/**
* Learnt cue styles kept on disk, one per sort, each with a fingerprint of what it was learnt from,
* so a style is only learnt again once its palette, the cues on it or the learner change.
*/
export class CueStyleCache {
private loaded: Partial<Record<CueSort, Learnt>> | null = null

private get path(): string {
return join(app.getPath('userData'), FILE_NAME)
}

private get learnt(): Partial<Record<CueSort, Learnt>> {
this.loaded ??= this.read()
return this.loaded
}

static fingerprint(learntFrom: unknown): string {
return createHash('sha256').update(JSON.stringify(learntFrom)).digest('hex')
}

style(sort: CueSort, learntFrom: string): string | null {
const learnt = this.learnt[sort]
return learnt?.learntFrom === learntFrom ? learnt.style : null
}

keep(sort: CueSort, learntFrom: string, style: string): void {
this.learnt[sort] = { learntFrom, style }
try {
mkdirSync(dirname(this.path), { recursive: true })
const tmp = `${this.path}.${process.pid}.tmp`
writeFileSync(tmp, JSON.stringify(this.learnt))
renameSync(tmp, this.path)
} catch (err) {
console.error('[cue styles] failed to write', err)
}
}

private read(): Partial<Record<CueSort, Learnt>> {
try {
return JSON.parse(readFileSync(this.path, 'utf8')) as Partial<Record<CueSort, Learnt>>
} catch {
return {}
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import macIcon from '../../resources/icon-mac.png?asset'
import { AnalysisQueue } from './analysis'
import { AutoCue } from './autoCue'
import { AudioBridge } from './audio/bridge'
import { buildMenu, registerFiles } from './files'
import { handleMediaProtocol, registerMediaScheme } from './media'
Expand All @@ -19,6 +20,7 @@ process.env.EXPORTSTUDIO_FFMPEG ??= ffmpegBinary()
const library = new Library(() => audio.addon())
const audio = new AudioBridge(library)
const analysis = new AnalysisQueue(library, () => audio.addon())
const autoCue = new AutoCue(library, () => audio.addon())
registerMediaScheme()

function createWindow(): BrowserWindow {
Expand Down Expand Up @@ -76,6 +78,7 @@ app.whenReady().then(() => {
handleMediaProtocol(library)
audio.register()
analysis.register()
autoCue.register()
buildMenu()
registerFiles()
loadSettings()
Expand Down
Loading
Loading