forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi.cpp
216 lines (174 loc) · 4.14 KB
/
midi.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
#include "midi.hpp"
#include "rtmidi.hpp"
#include "bridge.hpp"
#include "gamepad.hpp"
#include "keyboard.hpp"
namespace rack {
static std::vector<int> driverIds;
static std::map<int, MidiDriver*> drivers;
////////////////////
// MidiDevice
////////////////////
void MidiInputDevice::subscribe(MidiInput *midiInput) {
subscribed.insert(midiInput);
}
void MidiInputDevice::unsubscribe(MidiInput *midiInput) {
// Remove MidiInput from subscriptions
auto it = subscribed.find(midiInput);
if (it != subscribed.end())
subscribed.erase(it);
}
void MidiInputDevice::onMessage(MidiMessage message) {
for (MidiInput *midiInput : subscribed) {
midiInput->onMessage(message);
}
}
////////////////////
// MidiDriver
////////////////////
////////////////////
// MidiIO
////////////////////
MidiIO::~MidiIO() {
// Because of polymorphic destruction, descendants must call this in their own destructor
// setDriverId(-1);
}
std::vector<int> MidiIO::getDriverIds() {
return driverIds;
}
std::string MidiIO::getDriverName(int driverId) {
auto it = drivers.find(driverId);
if (it == drivers.end())
return "";
return it->second->getName();
}
void MidiIO::setDriverId(int driverId) {
// Destroy driver
setDeviceId(-1);
if (driver) {
driver = NULL;
}
this->driverId = -1;
// Set driver
auto it = drivers.find(driverId);
if (it != drivers.end()) {
driver = it->second;
this->driverId = driverId;
}
}
std::string MidiIO::getChannelName(int channel) {
if (channel == -1)
return "All channels";
else
return stringf("Channel %d", channel + 1);
}
json_t *MidiIO::toJson() {
json_t *rootJ = json_object();
json_object_set_new(rootJ, "driver", json_integer(driverId));
std::string deviceName = getDeviceName(deviceId);
if (!deviceName.empty())
json_object_set_new(rootJ, "deviceName", json_string(deviceName.c_str()));
json_object_set_new(rootJ, "channel", json_integer(channel));
return rootJ;
}
void MidiIO::fromJson(json_t *rootJ) {
json_t *driverJ = json_object_get(rootJ, "driver");
if (driverJ)
setDriverId(json_integer_value(driverJ));
json_t *deviceNameJ = json_object_get(rootJ, "deviceName");
if (deviceNameJ) {
std::string deviceName = json_string_value(deviceNameJ);
// Search for device with equal name
for (int deviceId : getDeviceIds()) {
if (getDeviceName(deviceId) == deviceName) {
setDeviceId(deviceId);
break;
}
}
}
json_t *channelJ = json_object_get(rootJ, "channel");
if (channelJ)
channel = json_integer_value(channelJ);
}
////////////////////
// MidiInput
////////////////////
MidiInput::MidiInput() {
if (driverIds.size() >= 1) {
setDriverId(driverIds[0]);
}
}
MidiInput::~MidiInput() {
setDriverId(-1);
}
std::vector<int> MidiInput::getDeviceIds() {
if (driver) {
return driver->getInputDeviceIds();
}
return {};
}
std::string MidiInput::getDeviceName(int deviceId) {
if (driver) {
return driver->getInputDeviceName(deviceId);
}
return "";
}
void MidiInput::setDeviceId(int deviceId) {
// Destroy device
if (driver && this->deviceId >= 0) {
driver->unsubscribeInputDevice(this->deviceId, this);
}
this->deviceId = -1;
// Create device
if (driver && deviceId >= 0) {
driver->subscribeInputDevice(deviceId, this);
this->deviceId = deviceId;
}
}
void MidiInputQueue::onMessage(MidiMessage message) {
// Filter channel
if (channel >= 0) {
if (message.status() != 0xf && message.channel() != channel)
return;
}
// Push to queue
if ((int) queue.size() < queueMaxSize)
queue.push(message);
}
bool MidiInputQueue::shift(MidiMessage *message) {
if (!message)
return false;
if (!queue.empty()) {
*message = queue.front();
queue.pop();
return true;
}
return false;
}
////////////////////
// MidiOutput
////////////////////
MidiOutput::MidiOutput() {
}
MidiOutput::~MidiOutput() {
setDriverId(-1);
}
void MidiOutput::setDeviceId(int deviceId) {
// TODO
}
////////////////////
// midi
////////////////////
void midiDestroy() {
driverIds.clear();
for (auto &pair : drivers) {
delete pair.second;
}
drivers.clear();
}
void midiDriverAdd(int driverId, MidiDriver *driver) {
assert(driver);
driverIds.push_back(driverId);
drivers[driverId] = driver;
}
} // namespace rack