/
mec_mididevice.cpp
418 lines (354 loc) · 13.1 KB
/
mec_mididevice.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "mec_mididevice.h"
#include "mec_log.h"
#include "../mec_voice.h"
#ifdef __linux__
#include <alsa/asoundlib.h>
extern unsigned int portInfo(snd_seq_t *seq, snd_seq_port_info_t *pinfo, unsigned int type, int portNumber);
#endif
namespace mec {
#ifdef __linux__
#include <alsa/asoundlib.h>
// Imported from RtMidi library.
bool findMidiPortId(unsigned &result, const std::string &portName, bool outputPort) {
snd_seq_t *seq;
if (snd_seq_open(&seq, "default", SND_SEQ_OPEN_INPUT, 0) < 0)
return false;
result = 0;
bool success = false;
const unsigned int type = outputPort ? SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE : SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ;
snd_seq_addr_t addr;
if (snd_seq_parse_address(seq, &addr, portName.c_str()) >= 0) {
snd_seq_port_info_t *info;
snd_seq_port_info_alloca(&info);
unsigned count = portInfo(seq, info, type, -1);
for (unsigned i = 0; i < count; ++i) {
portInfo(seq, info, type, i);
if (memcmp(&addr, snd_seq_port_info_get_addr(info), sizeof(addr)) == 0) {
result = i;
success = true;
break;
}
}
}
snd_seq_close(seq);
return success;
}
#else
bool findMidiPortId(unsigned &result, const std::string &portName, bool outputPort)
{
RtMidiOut out;
RtMidiIn in;
RtMidi &rt = outputPort ? (RtMidi&)out : (RtMidi&)in;
for (unsigned i = 0; i < rt.getPortCount(); i++) {
if (portName.compare(rt.getPortName(i)) == 0) {
result = i;
return true;
}
}
return false;
}
#endif // __linux__
////////////////////////////////////////////////
MidiDevice::MidiDevice(ICallback &cb) :
active_(false), callback_(cb) {
}
MidiDevice::~MidiDevice() {
deinit();
}
void MidiDeviceInCallback(double deltatime, std::vector<unsigned char> *message, void *userData) {
MidiDevice *self = static_cast<MidiDevice *>(userData);
self->midiCallback(deltatime, message);
}
RtMidiIn::RtMidiCallback MidiDevice::getMidiCallback() {
return MidiDeviceInCallback;
}
bool MidiDevice::init(void *arg) {
Preferences prefs(arg);
if (active_) {
deinit();
}
active_ = false;
bool found = false;
std::string input_device = prefs.getString("input device");
if (!input_device.empty()) {
try {
midiInDevice_.reset(new RtMidiIn(RtMidi::Api::UNSPECIFIED,"MEC MIDI IN DEVICE"));
} catch (RtMidiError &error) {
midiInDevice_.reset();
LOG_0("MidiDevice RtMidiIn ctor error:" << error.what());
return false;
}
mpeMode_ = prefs.getBool("mpe", true);
pitchbendRange_ = (float) prefs.getDouble("pitchbend range", 48.0);
unsigned port;
if (findMidiPortId(port, input_device.c_str(), false)) {
try {
midiInDevice_->openPort(port,"MIDI IN");
found = true;
LOG_1("Midi input opened :" << input_device);
} catch (RtMidiError &error) {
LOG_0("Midi input open error:" << error.what());
midiInDevice_.reset();
return false;
}
} else {
LOG_0("Input device not found : [" << input_device << "]");
LOG_0("available devices:");
for (unsigned i = 0; i < midiInDevice_->getPortCount(); i++) {
LOG_0("[" << midiInDevice_->getPortName(i) << "]");
}
midiInDevice_.reset();
return false;
}
midiInDevice_->ignoreTypes(true, true, true);
midiInDevice_->setCallback(getMidiCallback(), this);
} //midi input
std::string output_device = prefs.getString("output device");
if (!output_device.empty()) {
bool virt = prefs.getBool("virtual output", false);
try {
midiOutDevice_.reset(new RtMidiOut(RtMidi::Api::UNSPECIFIED, "MEC MIDI OUT DEVICE"));
} catch (RtMidiError &error) {
midiOutDevice_.reset();
LOG_0("MidiDevice RtMidiOut ctor error:" << error.what());
return false;
}
if (virt) {
try {
midiOutDevice_->openVirtualPort("MIDI OUT");
LOG_0("Midi virtual output created :" << output_device);
virtualOpen_ = true;
} catch (RtMidiError &error) {
LOG_0("Midi virtual output create error:" << error.what());
virtualOpen_ = false;
midiOutDevice_.reset();
return false;
}
} else {
found = false;
unsigned port;
if (findMidiPortId(port, input_device.c_str(), true)) {
try {
midiOutDevice_->openPort(port,"MIDI OUT");
LOG_0("Midi output opened :" << output_device);
found = true;
} catch (RtMidiError &error) {
LOG_0("Midi output create error:" << error.what());
midiOutDevice_.reset();
return false;
}
} else {
LOG_0("Output device not found : [" << output_device << "]");
LOG_0("available devices : ");
for (unsigned i = 0; i < midiOutDevice_->getPortCount(); i++) {
LOG_0("[" << midiOutDevice_->getPortName(i) << "]");
}
midiOutDevice_.reset();
}
}
} // midi output
active_ = midiInDevice_ || midiOutDevice_;
LOG_0("MidiDevice::init - complete");
return active_;
}
bool MidiDevice::process() {
return queue_.process(callback_);
}
void MidiDevice::deinit() {
LOG_0("MidiDevice::deinit");
if (midiInDevice_) midiInDevice_->cancelCallback();
midiInDevice_.reset();
active_ = false;
}
bool MidiDevice::isActive() {
return active_;
}
bool MidiDevice::midiCallback(double, std::vector<unsigned char> *message) {
int status = 0, data1 = 0, data2 = 0; //data3 = 0;
unsigned int n = message->size();
if (n > 3) LOG_0("midiCallback unexpect midi size" << n);
status = (int) message->at(0);
if (n > 1) data1 = (int) message->at(1);
if (n > 2) data2 = (int) message->at(2);
// if (n > 3) data3 = (int) message->at(3);
int ch = status & 0x0F;
int type = status & 0xF0;
VoiceData &touch = touches_[ch];
MecMsg msg;
switch (type) {
case 0x90: {
// note on (+note off if vel =0)
if (mpeMode_) {
if (touch.active_ || data2 == 0) {
// touch.x_ = 0.0f;
touch.y_ = 0.0f;
touch.z_ = 0.0f;
touch.active_ = false;
// assumption: callback handler wants note to be touch finish position
msg.type_ = MecMsg::TOUCH_OFF;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
touch.startNote_ = 0.0f;
}
if (data2 > 0) {
touch.startNote_ = (float) data1;
touch.note_ = (float) data1;
touch.x_ = 0.0f;
touch.y_ = 0.0f;
touch.z_ = float(data2) / 127.0f;
touch.active_ = true;
msg.type_ = MecMsg::TOUCH_ON;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
}
} else {
// not mpe mode, just send thru, allow multi notes on 1 channel
touch.startNote_ = (float) data1;
touch.note_ = (float) data1;
touch.x_ = 0.0f;
touch.y_ = 0.0f;
touch.z_ = float(data2) / 127.0f;
touch.active_ = true;
msg.type_ = MecMsg::TOUCH_ON;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
}
break;
}
case 0x80: {
// note off
if (mpeMode_) {
// touch.note_ == data1 !!
// touch.x_ = 0.0f;
touch.y_ = 0.0f;
touch.z_ = float(data2) / 127.0f;
touch.active_ = false;
// assumption: callback handler wants note to be touch finish position
msg.type_ = MecMsg::TOUCH_OFF;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
touch.startNote_ = 0.0f;
} else {
touch.note_ = (float) data1;
touch.x_ = 0.0f;
touch.y_ = 0.0f;
touch.z_ = float(data2) / 127.0f;
touch.active_ = true;
msg.type_ = MecMsg::TOUCH_OFF;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
touch.startNote_ = 0.0f;
}
break;
}
case 0xB0: {
// CC
float v = float(data2) / 127.0f;
if (mpeMode_ && data1 == 74) {
if (touch.active_) {
touch.y_ = v;
msg.type_ = MecMsg::TOUCH_CONTINUE;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
}
} else {
msg.type_ = MecMsg::CONTROL;
msg.data_.control_.controlId_ = data1;
msg.data_.control_.value_ = v;
queue_.addToQueue(msg);
}
break;
}
case 0xD0: {
// channel pressure
float v = float(data1) / 127.0f;
if (mpeMode_) {
if (touch.active_) {
touch.z_ = v;
msg.type_ = MecMsg::TOUCH_CONTINUE;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
}
} else {
msg.type_ = MecMsg::CONTROL;
msg.data_.control_.controlId_ = type;
msg.data_.control_.value_ = v;
queue_.addToQueue(msg);
}
break;
}
case 0xE0: {
// PB
float pb = (float) ((data2 << 7) + data1);
float v = (pb / 8192.0f) - 1.0f; // -1.0 to 1.0
if (mpeMode_) {
if (touch.active_) {
touch.note_ = touch.startNote_ + (v * pitchbendRange_);
touch.x_ = v;
msg.type_ = MecMsg::TOUCH_CONTINUE;
msg.data_.touch_.touchId_ = ch;
msg.data_.touch_.note_ = touch.note_;
msg.data_.touch_.x_ = touch.x_;
msg.data_.touch_.y_ = touch.y_;
msg.data_.touch_.z_ = touch.z_;
queue_.addToQueue(msg);
}
} else {
msg.type_ = MecMsg::CONTROL;
msg.data_.control_.controlId_ = type;
msg.data_.control_.value_ = v;
queue_.addToQueue(msg);
}
break;
}
default: {
// everything else;
//ignore
break;
}
} //switch
return true;
}
bool MidiDevice::send(const MidiMsg &m) {
if (midiOutDevice_ == nullptr || !isOutputOpen()) return false;
std::vector<unsigned char> msg;
for (int i = 0; i < m.size; i++) {
msg.push_back(m.data[i]);
}
try {
midiOutDevice_->sendMessage(&msg);
} catch (RtMidiError &error) {
LOG_0("MidiDevice output write error:" << error.what());
return false;
}
return true;
}
} //namespace