-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMyAbletonLink.cpp
142 lines (123 loc) · 3.27 KB
/
MyAbletonLink.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
#include "MyAbletonLink.h"
#include <algorithm>
#include <cmath>
using namespace std;
MyAbletonLink::MyAbletonLink()
: link_(nullptr),
quantum_(4.0),
isNumPeersChanged_(false),
numPeers_(0),
isTempoChanged_(false),
tempo_(0.0)
// , npc(nullptr), tc(nullptr)
{
}
MyAbletonLink::~MyAbletonLink(){
if(link_ != nullptr){
link_->enable(false);
delete link_;
}
}
void MyAbletonLink::setup(double tempo){
if(link_ != nullptr){
link_->enable(false);
delete link_;
}
link_ = new ableton::Link(tempo);
link_->setNumPeersCallback([this](std::size_t peers){
isNumPeersChanged_ = true;
numPeers_ = static_cast<int>(peers);
});
link_->setTempoCallback([this](const double bpm){
isTempoChanged_ = true;
tempo_ = bpm;
});
link_->enable(true);
}
void MyAbletonLink::setTempo(double bpm){
if (link_ == nullptr){
return;
}
auto state = link_->captureAppSessionState();
const auto time = link_->clock().micros();
state.setTempo(bpm, time);
link_->commitAppSessionState(state);
}
double MyAbletonLink::tempo(){
if(link_ == nullptr){
return 0.0;
}
return link_->captureAppSessionState().tempo();
}
void MyAbletonLink::setQuantum(double quantum){
this->quantum_ = fmin(fmax(quantum, 2.0), 16.0);
}
double MyAbletonLink::quantum(){
return quantum_;
}
void MyAbletonLink::forceBeatAtTime(double beat) {
if(link_ == nullptr){
return;
}
auto state = link_->captureAppSessionState();
const auto time = link_->clock().micros();
state.forceBeatAtTime(beat, time, quantum_);
link_->commitAppSessionState(state);
}
void MyAbletonLink::requestBeatAtTime(double beat) {
if(link_ == nullptr){
return;
}
auto state = link_->captureAppSessionState();
const auto time = link_->clock().micros();
state.requestBeatAtTime(beat, time, quantum_);
link_->commitAppSessionState(state);
}
void MyAbletonLink::enable(bool bEnable){
if(link_ == nullptr){
return;
}
link_->enable(bEnable);
}
bool MyAbletonLink::isEnabled() const{
if(link_ == nullptr){
return false;
}
return link_->isEnabled();
}
std::size_t MyAbletonLink::numPeers(){
if(link_ == nullptr){
return 0;
}
return link_->numPeers();
}
MyAbletonLink::Status MyAbletonLink::update(){
Status status;
if(link_ == nullptr){
return status;
}
auto state = link_->captureAppSessionState();
const auto time = link_->clock().micros();
status.beat = state.beatAtTime(time, quantum_);
status.phase = state.phaseAtTime(time, quantum_);
status.quantam = quantum_;
status.tempo = state.tempo();
status.time = std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
status.numPeers = static_cast<int>(link_->numPeers());
// if (isNumPeersChanged && npc != nullptr) {
// isNumPeersChanged = false;
// npc(static_cast<int>(numPeers_));
// }
// if (isTempoChanged && tc != nullptr) {
// isTempoChanged = false;
// tc(static_cast<double>(tempo_));
// }
return status;
}
//void MyAbletonLink::setNumPeersCallback(numPeersCallback cb) {
// npc = cb;
//}
//
//void MyAbletonLink::setTempoCallback(tempoCallback cb) {
// tc = cb;
//}