Repository navigation
Expand file tree
/
Copy pathsetlist.js
More file actions
187 lines (166 loc) · 5.03 KB
/
Copy pathsetlist.js
File metadata and controls
187 lines (166 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* setlist.js — Parses Ableton cue points into a structured setlist
*
* Naming conventions supported:
* Song Title → song entry (plain name)
* [Song Title] → song entry (bracket notation)
* > Section Name → section within current song
* --- STOP --- → auto-stop marker
* [STOP] → auto-stop marker
* [NEXTMARKER] → the song's real starting point (skips lead-in
* silence) — jumping to the song jumps here instead
* // anything → comment, ignored
*/
import { randomUUID } from "crypto";
export function parseCuePoints(cuePoints) {
const setlist = [];
let currentSong = null;
let currentSongHasExplicitStart = false;
const sortedCues = [...cuePoints].sort((a, b) => a.raw.time - b.raw.time);
for (let i = 0; i < sortedCues.length; i++) {
const cue = sortedCues[i];
const name = (cue.raw.name || "").trim();
const time = cue.raw.time;
// Skip comments
if (name.startsWith("//")) continue;
// Stop marker
if (isStopMarker(name)) {
if (currentSong) {
currentSong.stopMarkers.push({ time, cueObject: cue });
}
continue;
}
// Real starting point of the current song (skips lead-in silence).
// Only the first one found after a song's own cue point is used.
if (isNextMarker(name)) {
if (currentSong && !currentSongHasExplicitStart) {
currentSong.startCue = { time, cueObject: cue };
currentSongHasExplicitStart = true;
}
continue;
}
// Section within current song
if (name.startsWith(">")) {
if (currentSong) {
const sectionName = name.slice(1).trim();
currentSong.sections.push({
id: randomUUID(),
name: sectionName,
time,
cueObject: cue,
});
}
continue;
}
// New song
const songName = cleanName(name);
currentSong = {
id: randomUUID(),
name: songName,
time,
cueObject: cue,
// Defaults to the song's own cue; overwritten if a [NEXTMARKER]
// cue is found before the next song entry.
startCue: { time, cueObject: cue },
sections: [],
stopMarkers: [],
// These get merged from storage (notes, color, excluded)
notes: "",
color: "",
excluded: false,
};
currentSongHasExplicitStart = false;
setlist.push(currentSong);
}
// Calculate durations
for (let i = 0; i < setlist.length; i++) {
const song = setlist[i];
const nextSongTime =
i + 1 < setlist.length ? setlist[i + 1].time : null;
song.duration = nextSongTime !== null ? nextSongTime - song.time : null;
}
return setlist;
}
function isStopMarker(name) {
const lower = name.toLowerCase();
return (
lower === "[stop]" ||
lower === "stop" ||
lower.includes("--- stop ---") ||
lower.startsWith("[stop]")
);
}
function isNextMarker(name) {
const lower = name.toLowerCase().replace(/\s+/g, "");
return lower === "[nextmarker]" || lower === "nextmarker";
}
function cleanName(name) {
// Remove surrounding brackets: [Song Title] → Song Title
return name.replace(/^\[(.+)\]$/, "$1").trim();
}
/**
* Merge saved metadata (notes, colors, order, excluded) onto a parsed setlist.
* meta = { songs: { [originalName]: { notes, color, excluded } }, order: [name, ...] }
*/
export function mergeMetadata(setlist, meta) {
if (!meta) return setlist;
// Apply per-song metadata
for (const song of setlist) {
const saved = meta.songs?.[song.name];
if (saved) {
song.notes = saved.notes ?? "";
song.color = saved.color ?? "";
song.excluded = saved.excluded ?? false;
}
}
// Apply custom order if saved
if (meta.order && meta.order.length > 0) {
const orderMap = new Map(meta.order.map((name, i) => [name, i]));
setlist.sort((a, b) => {
const ai = orderMap.has(a.name) ? orderMap.get(a.name) : 9999;
const bi = orderMap.has(b.name) ? orderMap.get(b.name) : 9999;
return ai - bi;
});
}
return setlist;
}
/**
* Format seconds as MM:SS
*/
export function formatTime(seconds) {
if (seconds == null || isNaN(seconds)) return "--:--";
const m = Math.floor(seconds / 60);
const s = Math.floor(seconds % 60);
return `${m}:${s.toString().padStart(2, "0")}`;
}
/**
* Given a setlist and current playhead time, find which song is active.
* Returns the song index or -1.
*/
export function findActiveSong(setlist, currentTime) {
if (!setlist.length) return -1;
let activeIdx = -1;
for (let i = 0; i < setlist.length; i++) {
if (currentTime >= setlist[i].time) {
activeIdx = i;
} else {
break;
}
}
return activeIdx;
}
/**
* Given a song and current playhead time, find active section index.
*/
export function findActiveSection(song, currentTime) {
if (!song?.sections?.length) return -1;
let activeIdx = -1;
for (let i = 0; i < song.sections.length; i++) {
if (currentTime >= song.sections[i].time) {
activeIdx = i;
} else {
break;
}
}
return activeIdx;
}