-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcarabiner.cpp
373 lines (329 loc) · 13.1 KB
/
carabiner.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <string>
#include <set>
#include <mutex>
#include <gflags/gflags.h>
#include <ableton/Link.hpp>
extern "C" {
#include "mongoose.h"
}
// Validators for command-line arguments
static bool validatePort(const char* flagname, gflags::int32 value) {
if (value > 0 && value < 32768) {
return true; // Valid
}
std::cerr << flagname << " must be between 1 and 32767" << std::endl;
return false;
}
static bool validatePoll(const char* flagname, gflags::int32 value) {
if (value > 0 && value < 1001) {
return true; // Valid
}
std::cerr << flagname << " must be between 1 and 1000" << std::endl;
return false;
}
// Set up the command-line options supported by the program
DEFINE_int32(port, 17000, "TCP port on which to accept connections");
static const bool port_dummy = gflags::RegisterFlagValidator(&FLAGS_port, &validatePort);
DEFINE_int32(poll, 20, "Number of milliseconds between updates");
static const bool poll_dummy = gflags::RegisterFlagValidator(&FLAGS_poll, &validatePoll);
// Our interface to the Link session
ableton::Link linkInstance(120.);
// Keep track of open TCP connections, and those which need updates sent to them
std::set<struct mg_connection *> activeConnections, updatedConnections;
std::mutex updatedMutex;
// Keep track of whether we are synchronizing transport start/stop
bool syncStartStop = false;
// Send a message describing the current state of the Link session.
static void reportStatus(struct mg_connection *nc) {
const std::chrono::microseconds time = linkInstance.clock().micros();
const ableton::Link::SessionState sessionState = linkInstance.captureAppSessionState();
std::string playingState = sessionState.isPlaying()? "true" : "false";
std::string playingResponse = syncStartStop? (" :playing " + playingState) : "";
std::string response = "status { :peers " + std::to_string(linkInstance.numPeers()) +
" :bpm " + std::to_string(sessionState.tempo()) +
" :start " + std::to_string(sessionState.timeAtBeat(0.0, 4.0).count()) +
" :beat " + std::to_string(sessionState.beatAtTime(time, 4.0)) + playingResponse + " }";
mg_send(nc, response.data(), response.length());
}
// Process a request for the current link session status. Can simply remove the connection
// from the set of those which have current status updates, and one will be sent on the next
// poll.
static void handleStatus(std::string args, struct mg_connection *nc) {
updatedMutex.lock();
updatedConnections.erase(nc);
updatedMutex.unlock();
}
// Process a request to establish a specific tempo
static void handleBpm(std::string args, struct mg_connection *nc) {
std::stringstream ss(args);
double bpm;
ss >> bpm;
if (ss.fail() || (bpm < 20.0) || (bpm > 999.0)) {
// Unparsed bpm, report error
std::string response = "bad-bpm " + args;
mg_send(nc, response.data(), response.length());
std::cerr << "Failed to parse bpm: " << args << std::endl;
} else {
ableton::Link::SessionState sessionState = linkInstance.captureAppSessionState();
sessionState.setTempo(bpm, linkInstance.clock().micros());
linkInstance.commitAppSessionState(sessionState);
// No neeed to call reportStatus(nc) because if the BPM changed that will happen on its own.
}
}
// Process a request to query the SessionState to find out what beat occurs at a particular time
static void handleBeatAtTime(std::string args, struct mg_connection *nc) {
std::stringstream ss(args);
long when;
double quantum;
ss >> when;
if (ss.fail()) {
// Unparsed microsecond value, report error
std::string response = "bad-time " + args;
mg_send(nc, response.data(), response.length());
} else {
ss >> quantum;
if (ss.fail() || (quantum < 2.0) || (quantum > 16.0)) {
// Unparsed quantum value, report error
std::string response = "bad-quantum " + args;
mg_send(nc, response.data(), response.length());
} else {
ableton::Link::SessionState sessionState = linkInstance.captureAppSessionState();
double beat = sessionState.beatAtTime(std::chrono::microseconds(when), quantum);
std::string response = "beat-at-time { :when " + std::to_string(when) +
" :quantum " + std::to_string(quantum) +
" :beat " + std::to_string(beat) + " }";
mg_send(nc, response.data(), response.length());
}
}
}
// Process a request to query the current SessionState phase
static void handlePhaseAtTime(std::string args, struct mg_connection *nc) {
std::stringstream ss(args);
long when;
double quantum;
ss >> when;
if (ss.fail()) {
// Unparsed microsecond value, report error
std::string response = "bad-time " + args;
mg_send(nc, response.data(), response.length());
} else {
ss >> quantum;
if (ss.fail() || (quantum < 2.0) || (quantum > 16.0)) {
// Unparsed quantum value, report error
std::string response = "bad-quantum " + args;
mg_send(nc, response.data(), response.length());
} else {
ableton::Link::SessionState sessionState = linkInstance.captureAppSessionState();
double phase = sessionState.phaseAtTime(std::chrono::microseconds(when), quantum);
std::string response = "phase-at-time { :when " + std::to_string(when) +
" :quantum " + std::to_string(quantum) +
" :phase " + std::to_string(phase) + " }";
mg_send(nc, response.data(), response.length());
}
}
}
// Process a request to determine when a specific beat falls on the SessionState
static void handleTimeAtBeat(std::string args, struct mg_connection *nc) {
std::stringstream ss(args);
double beat;
double quantum;
ss >> beat;
if (ss.fail()) {
// Unparsed beat value, report error
std::string response = "bad-beat " + args;
mg_send(nc, response.data(), response.length());
} else {
ss >> quantum;
if (ss.fail() || (quantum < 2.0) || (quantum > 16.0)) {
// Unparsed quantum value, report error
std::string response = "bad-quantum " + args;
mg_send(nc, response.data(), response.length());
} else {
ableton::Link::SessionState sessionState = linkInstance.captureAppSessionState();
std::chrono::microseconds time = sessionState.timeAtBeat(beat, quantum);
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(time);
std::string response = "time-at-beat { :beat " + std::to_string(beat) +
" :quantum " + std::to_string(quantum) +
" :when " + std::to_string(micros.count()) + " }";
mg_send(nc, response.data(), response.length());
}
}
}
// Process a request to gracefully or forcibly realign the SessionState
static void handleAdjustBeatAtTime(std::string args, struct mg_connection *nc, bool force) {
std::stringstream ss(args);
double beat;
long when;
double quantum;
ss >> beat;
if (ss.fail()) {
// Unparsed beat value, report error
std::string response = "bad-beat " + args;
mg_send(nc, response.data(), response.length());
} else {
ss >> when;
if (ss.fail()) {
// Unparsed microsecond value, report error
std::string response = "bad-time " + args;
mg_send(nc, response.data(), response.length());
} else {
ss >> quantum;
if (ss.fail() || (quantum < 2.0) || (quantum > 16.0)) {
// Unparsed quantum value, report error
std::string response = "bad-quantum " + args;
mg_send(nc, response.data(), response.length());
} else {
ableton::Link::SessionState sessionState = linkInstance.captureAppSessionState();
if (force) {
sessionState.forceBeatAtTime(beat, std::chrono::microseconds(when), quantum);
} else {
sessionState.requestBeatAtTime(beat, std::chrono::microseconds(when), quantum);
}
linkInstance.commitAppSessionState(sessionState);
reportStatus(nc);
}
}
}
}
// Process a request to enable or disable start/stop sync
static void handleEnableStartStopSync(bool enable, std::string args, struct mg_connection *nc) {
syncStartStop = enable;
linkInstance.enableStartStopSync(enable);
reportStatus(nc);
}
// Process a request to start or stop the shared transport state
static void handleSetIsPlaying(bool playing, std::string args, struct mg_connection *nc) {
std::stringstream ss(args);
long when;
ss >> when;
if (ss.fail()) {
// Unparsed microsecond value, report error
std::string response = "bad-time " + args;
mg_send(nc, response.data(), response.length());
} else {
ableton::Link::SessionState sessionState = linkInstance.captureAppSessionState();
sessionState.setIsPlaying(playing, std::chrono::microseconds(when));
linkInstance.commitAppSessionState(sessionState);
reportStatus(nc);
}
}
// Check whether the packet contains the specified command; if so, remove the
// command prefix from the arguments.
static bool matchesCommand(std::string msg, std::string cmd, std::string& args) {
if (msg.substr(0, cmd.length()) == cmd) {
//std::cout << " is " << cmd << "command!" << std::endl;
args = msg.substr(cmd.length(), std::string::npos);
return true;
}
return false;
}
// When a packet has been received, identify the command it contains, and handle it appropriately.
static void processMessage(std::string msg, struct mg_connection *nc) {
//std::cout << std::endl << "received: " << msg << std::endl;
std::string args;
if (matchesCommand(msg, "bpm ", args)) {
handleBpm(args, nc);
} else if (matchesCommand(msg, "beat-at-time ", args)) {
handleBeatAtTime(args, nc);
} else if (matchesCommand(msg, "phase-at-time ", args)) {
handlePhaseAtTime(args, nc);
} else if (matchesCommand(msg, "time-at-beat ", args)) {
handleTimeAtBeat(args, nc);
} else if (matchesCommand(msg, "force-beat-at-time ", args)) {
handleAdjustBeatAtTime(args, nc, true);
} else if (matchesCommand(msg, "request-beat-at-time ", args)) {
handleAdjustBeatAtTime(args, nc, false);
} else if (matchesCommand(msg, "enable-start-stop-sync", args)) {
handleEnableStartStopSync(true, args, nc);
} else if (matchesCommand(msg, "disable-start-stop-sync", args)) {
handleEnableStartStopSync(false, args, nc);
} else if (matchesCommand(msg, "start-playing ", args)) {
handleSetIsPlaying(true, args, nc);
} else if (matchesCommand(msg, "stop-playing ", args)) {
handleSetIsPlaying(false, args, nc);
} else if (matchesCommand(msg, "status", args)) {
handleStatus(args, nc);
} else {
// Unrecognized input, report error
std::string response = "unsupported " + msg;
mg_send(nc, response.data(), response.length());
}
}
static void eventHandler(struct mg_connection *nc, int ev, void *p) {
struct mbuf *io = &nc->recv_mbuf;
(void) p;
bool needsUpdate;
switch (ev) {
case MG_EV_ACCEPT:
activeConnections.insert(nc);
break;
case MG_EV_CLOSE:
activeConnections.erase(nc);
break;
case MG_EV_POLL:
updatedMutex.lock();
needsUpdate = updatedConnections.insert(nc).second;
updatedMutex.unlock();
if (needsUpdate) {
// The connection was not already on the updated list, so send it an updated status
reportStatus(nc);
}
break;
case MG_EV_RECV:
processMessage(std::string(io->buf, io->len), nc);
mbuf_remove(io, io->len); // Discard message from recv buffer
break;
default:
break;
}
}
// Registered to be called by a Link thread whenever the session BPM changes; arranges for an update to
// be sent to all of our TCP clients.
void tempoCallback(double bpm) {
updatedMutex.lock();
updatedConnections.clear();
updatedMutex.unlock();
}
// Registered to be called by a Link thread whenever the number of peers changes; arranges for an update to
// be sent to all of our TCP clients.
void peersCallback(std::size_t numPeers) {
updatedMutex.lock();
updatedConnections.clear();
updatedMutex.unlock();
}
// Registered to be called by a Link thread whenever the transport state changes if we are synchronizing
// start/stop state; arranges for an update to be sent to all of our TCP clients.
void startStopCallback(bool isPlaying) {
updatedMutex.lock();
updatedConnections.clear();
updatedMutex.unlock();
}
int main(int argc, char* argv[]) {
gflags::SetUsageMessage("Bridge to an Ableton Link session. Sample usage:\n" + std::string(argv[0]) +
" --port 1234 --poll 10");
gflags::SetVersionString("1.0.0");
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (argc > 1) {
std::cerr << "Unrecognized argument, " << argv[1] << std::endl;
gflags::ShowUsageWithFlagsRestrict(argv[0], "carabiner.cpp");
return 1;
}
linkInstance.setTempoCallback(tempoCallback);
linkInstance.setNumPeersCallback(peersCallback);
linkInstance.setStartStopCallback(startStopCallback);
linkInstance.enableStartStopSync(syncStartStop);
linkInstance.enable(true);
struct mg_mgr mgr;
std::string port = "tcp://127.0.0.1:" + std::to_string(FLAGS_port);
mg_mgr_init(&mgr, NULL);
mg_bind(&mgr, port.c_str(), eventHandler);
std::cout << "Starting Carabiner on port " << port << std::endl;
for (;;) {
std::cout << "Link bpm: " << linkInstance.captureAppSessionState().tempo() <<
" Peers: " << linkInstance.numPeers() <<
" Connections: " << activeConnections.size() << " \r" << std::flush;
mg_mgr_poll(&mgr, FLAGS_poll);
}
mg_mgr_free(&mgr);
return 0;
}