-
Notifications
You must be signed in to change notification settings - Fork 12
/
Link.cpp
198 lines (159 loc) · 4.67 KB
/
Link.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
#include <ableton/Link.hpp>
#include <chrono>
#include <iostream>
#include "SC_Unit.h"
#include "SC_PlugIn.h"
static InterfaceTable *ft;
static ableton::Link *gLink = nullptr;
static float gTempo = 60.0;
// ========================================================================================================
//
// Link Interface for SuperCollider
//
// ========================================================================================================
struct LinkEnabler : public Unit {
};
extern "C" {
void LinkEnabler_Ctor(LinkEnabler* unit);
void LinkEnabler_next(LinkEnabler* unit, int inNumSamples);
}
void LinkEnabler_Ctor(LinkEnabler* unit) {
if (!gLink) {
gTempo = *IN(0);
gLink = new ableton::Link(gTempo);
gLink->enable(true);
} else {
//std::cout<<"Link already running"<<std::endl;
}
SETCALC(LinkEnabler_next);
}
void LinkEnabler_next(LinkEnabler* unit, int inNumSamples) {
}
struct LinkDisabler : public Unit {
};
extern "C" {
void LinkDisabler_Ctor(LinkDisabler* unit);
void LinkDisabler_next(LinkDisabler* unit, int inNumSamples);
}
void LinkDisabler_Ctor(LinkDisabler* unit) {
if (gLink) {
gLink->enable(false);
delete gLink;
gLink = nullptr;
} else {
//std::cout<<"Link not running"<<std::endl;
}
SETCALC(LinkDisabler_next);
}
void LinkDisabler_next(LinkDisabler* unit, int inNumSamples) {
}
struct Link : public Unit {
float mLastBeat;
};
extern "C" {
void Link_Ctor(Link* unit);
void Link_next(Link* unit, int inNumSamples);
}
void Link_Ctor(Link* unit) {
if (!gLink) {
Print("warn: Link not enabled!\n");
}
unit->mLastBeat = 0.0;
SETCALC(Link_next);
}
void Link_next(Link* unit, int inNumSamples) {
float* output = OUT(0);
if (gLink) {
const auto time = gLink->clock().micros();
auto timeline = gLink->captureAudioSessionState();
const auto beats = timeline.beatAtTime(time, 4);
*output = static_cast<float>(beats);
unit->mLastBeat = *output;
} else {
*output = unit->mLastBeat;
}
}
struct LinkTempo : public Unit {
double mCurTempo;
double mTempoCalc;
};
extern "C" {
void LinkTempo_Ctor(LinkTempo* unit);
void LinkTempo_next(LinkTempo* unit, int inNumSamples);
}
void LinkTempo_Ctor(LinkTempo* unit) {
if (!gLink) {
Print("Link not enabled! can't set Link Tempo!\n");
} else {
const auto timeline = gLink->captureAudioSessionState();
unit->mCurTempo = timeline.tempo();
unit->mTempoCalc = unit->mCurTempo - *IN(0);
}
SETCALC(LinkTempo_next);
}
void LinkTempo_next(LinkTempo* unit, int inNumSamples) {
if (gLink) {
auto timeline = gLink->captureAudioSessionState();
timeline.setTempo(unit->mCurTempo - (*IN(1) * unit->mTempoCalc), gLink->clock().micros());
gLink->commitAudioSessionState(timeline);
}
}
// ========================================================================================================
//
// Application Interface
//
// ========================================================================================================
extern "C" {
double GetLinkBeat();
double GetLinkBeatToTime(double beat);
float GetLinkTempo();
void SyncUnixTimeWithLink();
}
static long gDiff = 0;
void SyncUnixTimeWithLink() {
if (gLink) {
unsigned long diff = 0;
for(int i = 0; i < 10; i++) {
const auto time = gLink->clock().micros();
unsigned long since_epoch = std::chrono::duration_cast<std::chrono::microseconds>
(std::chrono::system_clock::now().time_since_epoch()).count() - 0;
diff += since_epoch - time.count();
}
gDiff = diff / 10;
}
}
double GetLinkBeat() {
double output = 0.0;
if (gLink) {
const auto time = gLink->clock().micros();
auto timeline = gLink->captureAppSessionState();
const auto beats = timeline.beatAtTime(time, 4);
output = beats;
}
return output;
}
double GetLinkBeatToTime(double beat) {
double output = 0.0;
if (gLink) {
auto timeline = gLink->captureAppSessionState();
auto time = timeline.timeAtBeat(beat, 4);
output = (time.count() + gDiff) * 1e-6;
}
return output;
}
float GetLinkTempo() {
float output = 0.0;
if (gLink) {
auto timeline = gLink->captureAppSessionState();
output = static_cast<float>(timeline.tempo());
}
return output;
}
// ========================================================================================================
PluginLoad(Link) {
ft = inTable;
DefineSimpleUnit(LinkEnabler);
DefineSimpleUnit(LinkDisabler);
DefineSimpleUnit(Link);
DefineSimpleUnit(LinkTempo);
}