Repository navigation
Expand file tree
/
Copy pathableton.js
More file actions
261 lines (230 loc) · 8.73 KB
/
Copy pathableton.js
File metadata and controls
261 lines (230 loc) · 8.73 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* ableton.js — Manages the ableton-js connection and exposes a live state object.
*/
import { Ableton } from "ableton-js";
import { parseCuePoints, mergeMetadata, findActiveSong, findActiveSection } from "./setlist.js";
import { loadMeta, clearCache } from "./storage.js";
const BROADCAST_INTERVAL_MS = 100;
export class AbletonBridge {
constructor(onStateChange) {
this.onStateChange = onStateChange;
this.ableton = new Ableton({
clientPortFile: "ableton-js-client.port",
serverPortFile: "ableton-js-server.port",
logger: {
debug: () => {},
info: () => {},
warn: () => {},
error: console.error,
},
});
this.setlist = [];
this.rawCuePoints = [];
this.connected = false;
this.state = {
connected: false,
isPlaying: false,
tempo: 120,
currentTime: 0,
activeSongIndex: -1,
activeSectionIndex: -1,
setlist: [],
projectName: "",
};
this._lastBroadcast = 0;
this._pendingBroadcast = null;
this._lastStopCheck = 0;
this._firedStops = new Set();
this._lastSeenTime = 0;
this._listenersAttached = false;
}
async start() {
await this.ableton.start();
console.log("[ableton] Started, waiting for connection…");
this.ableton.on("connect", async () => {
console.log("[ableton] Connected");
this.connected = true;
this._firedStops.clear();
this._lastSeenTime = 0;
await this._onConnect();
});
this.ableton.on("disconnect", () => {
console.log("[ableton] Disconnected");
this.connected = false;
this.state.connected = false;
this.state.isPlaying = false;
this._broadcastNow();
});
this.ableton.on("error", (e) => {
console.error("[ableton] Error:", e?.message || e);
});
}
async _onConnect() {
try {
const [tempo, isPlaying, projectName] = await Promise.all([
this.ableton.song.get("tempo"),
this.ableton.song.get("is_playing"),
this.ableton.song.get("name").catch(() => "Untitled"),
]);
await this._loadSetlist();
this.state.connected = true;
this.state.tempo = tempo;
this.state.isPlaying = isPlaying;
this.state.projectName = projectName;
// Ableton fires "connect" on every reconnect (e.g. if the Live set is
// reopened). Without this guard, addListener would stack a new
// listener each time, causing broadcasts/auto-stops to fire multiple
// times per tick and leaking memory over a long session.
if (!this._listenersAttached) {
this._listenersAttached = true;
this.ableton.song.addListener("is_playing", (val) => {
this.state.isPlaying = val;
this._broadcastNow();
});
this.ableton.song.addListener("tempo", (val) => {
this.state.tempo = val;
this._broadcastNow();
});
this.ableton.song.addListener("current_song_time", (val) => {
this.state.currentTime = val;
this._updateActivePosition(val);
this._checkAutoStop(val);
this._throttledBroadcast();
});
}
this._broadcastNow();
} catch (err) {
console.error("[ableton] Error on connect:", err.message);
}
}
async _loadSetlist() {
try {
await clearCache();
const cueObjects = await this.ableton.song.get("cue_points");
this.rawCuePoints = cueObjects;
const parsed = parseCuePoints(cueObjects);
const meta = await loadMeta();
this.setlist = mergeMetadata(parsed, meta);
this.state.setlist = this._serializeSetlist();
console.log(`[ableton] Loaded ${this.setlist.length} songs`);
} catch (err) {
console.error("[ableton] Failed to load setlist:", err.message);
this.setlist = [];
this.state.setlist = [];
}
}
_serializeSetlist() {
return this.setlist.map((song) => ({
id: song.id,
name: song.name,
time: song.time,
duration: song.duration,
notes: song.notes,
color: song.color,
excluded: song.excluded,
sections: song.sections.map((s) => ({ id: s.id, name: s.name, time: s.time })),
stopMarkerCount: song.stopMarkers.length,
// Present only when a [NEXTMARKER] cue moved the real start point
// later than the song's own cue (i.e. skips lead-in silence).
introSkip: song.startCue.time > song.time ? song.startCue.time : null,
}));
}
_updateActivePosition(currentTime) {
this.state.activeSongIndex = findActiveSong(this.setlist, currentTime);
this.state.activeSectionIndex = findActiveSection(
this.setlist[this.state.activeSongIndex], currentTime
);
}
_checkAutoStop(currentTime) {
// If the playhead has jumped backwards (rewind, loop, manual seek),
// "un-fire" any stop markers ahead of the new position so they can
// trigger again on the next pass instead of being silently skipped
// for the rest of the session.
if (currentTime < this._lastSeenTime - 0.5) {
for (const key of [...this._firedStops]) {
if (parseFloat(key) >= currentTime) this._firedStops.delete(key);
}
}
this._lastSeenTime = currentTime;
if (!this.state.isPlaying) return;
const now = Date.now();
if (now - this._lastStopCheck < 50) return;
this._lastStopCheck = now;
for (const song of this.setlist) {
for (const stop of song.stopMarkers) {
const key = stop.time.toFixed(4);
if (!this._firedStops.has(key) && currentTime >= stop.time) {
this._firedStops.add(key);
console.log(`[ableton] Auto-stop at ${stop.time.toFixed(2)}s`);
this.ableton.song.stopPlaying().catch(() => {});
}
}
}
}
_throttledBroadcast() {
const now = Date.now();
if (now - this._lastBroadcast >= BROADCAST_INTERVAL_MS) {
this._lastBroadcast = now;
this.onStateChange({ ...this.state });
} else if (!this._pendingBroadcast) {
const delay = BROADCAST_INTERVAL_MS - (now - this._lastBroadcast);
this._pendingBroadcast = setTimeout(() => {
this._pendingBroadcast = null;
this._lastBroadcast = Date.now();
this.onStateChange({ ...this.state });
}, delay);
}
}
_broadcastNow() {
this.state.setlist = this._serializeSetlist();
this.onStateChange({ ...this.state });
}
/* ── Transport ───────────────────────────────────────────────────────────── */
async play() { await this.ableton.song.startPlaying(); }
async stop() { await this.ableton.song.stopPlaying(); }
async continuePlaying() { await this.ableton.song.continuePlaying(); }
async jumpToNextCue() { await this.ableton.song.jumpToNextCue(); }
async jumpToPrevCue() { await this.ableton.song.jumpToPrevCue(); }
async setTempo(bpm) {
const val = parseFloat(bpm);
if (!Number.isFinite(val)) throw new Error("Invalid tempo value");
const clamped = Math.min(999, Math.max(20, val));
await this.ableton.song.set("tempo", clamped);
}
async jumpToSong(songId) {
const song = this.setlist.find((s) => s.id === songId);
if (!song?.cueObject) throw new Error("Song not found: " + songId);
// If a [NEXTMARKER] cue was found inside this song, jump there instead
// of the song's own start — skips any lead-in silence before the music
// actually begins.
const target = song.startCue?.cueObject || song.cueObject;
await target.jump();
}
async jumpToSection(songId, sectionId) {
const song = this.setlist.find((s) => s.id === songId);
if (!song) throw new Error("Song not found: " + songId);
const section = song.sections.find((s) => s.id === sectionId);
if (!section?.cueObject) throw new Error("Section not found: " + sectionId);
await section.cueObject.jump();
}
/* ── Setlist meta ────────────────────────────────────────────────────────── */
async reloadSetlist() {
await this._loadSetlist();
this._broadcastNow();
}
updateSongMeta(songId, updates) {
const song = this.setlist.find((s) => s.id === songId);
if (!song) return;
Object.assign(song, updates);
this._broadcastNow();
}
reorderSetlist(orderedIds) {
const map = new Map(this.setlist.map((s) => [s.id, s]));
const reordered = orderedIds.map((id) => map.get(id)).filter(Boolean);
for (const song of this.setlist) {
if (!orderedIds.includes(song.id)) reordered.push(song);
}
this.setlist = reordered;
this._broadcastNow();
}
}