12 #include <RtMidi/RtMidi.h>
17 #include <CoreMIDI/CoreMIDI.h>
30 "VuoMidiOutputDevice",
34 "VuoList_VuoMidiInputDevice",
35 "VuoList_VuoMidiOutputDevice",
37 "CoreAudio.framework",
57 RtMidiIn *midiin =
new RtMidiIn();
58 unsigned int portCount = midiin->getPortCount();
59 for (
unsigned int i = 0; i < portCount; ++i)
75 RtMidiOut *midiout =
new RtMidiOut();
76 unsigned int portCount = midiout->getPortCount();
77 for (
unsigned int i = 0; i < portCount; ++i)
90 if (message->messageID != kMIDIMsgSetupChanged)
111 VUserLog(
"Error: Couldn't register device change listener: %s", description);
127 VUserLog(
"Error: Unbalanced VuoMidi_use() / _disuse() calls.");
137 VUserLog(
"Error: Couldn't unregister device change listener: %s", description);
178 RtMidiOut *midiout = NULL;
183 throw RtError(
"No matching device found");
186 midiout =
new RtMidiOut();
187 midiout->openPort(realizedDevice.
id,
"VuoMidiOut_make");
191 catch (RtError &error)
211 RtMidiOut *midiout = (RtMidiOut *)mo;
212 std::vector<unsigned char> message;
216 midiout->sendMessage(&message);
227 RtMidiOut *midiout = (RtMidiOut *)mo;
228 std::vector<unsigned char> message;
229 message.push_back(0xB0 +
MIN(controller.
channel-1,15));
231 message.push_back(
MIN(controller.
value,127));
232 midiout->sendMessage(&message);
243 RtMidiOut *midiout = (RtMidiOut *)mo;
244 std::vector<unsigned char> message;
245 message.push_back(0xE0 +
MIN(pitchBend.
channel-1,15));
246 message.push_back(pitchBend.
value & 0x7f);
247 message.push_back((pitchBend.
value >> 7) & 0x7f);
248 midiout->sendMessage(&message);
259 RtMidiOut *midiout = (RtMidiOut *)mo;
283 unsigned char channel = ((*message)[0] & 0x0f) + 1;
284 if (((*message)[0] & 0xf0) == 0x80)
288 if (mii->receivedNote)
289 mii->receivedNote(mii->context, mn);
292 else if (((*message)[0] & 0xf0) == 0x90)
294 unsigned char velocity = (*message)[2];
297 bool isNoteOn = (velocity != 0);
301 if (mii->receivedNote)
302 mii->receivedNote(mii->context, mn);
305 else if (((*message)[0] & 0xf0) == 0xb0)
309 if (mii->receivedController)
310 mii->receivedController(mii->context, mc);
313 else if (((*message)[0] & 0xf0) == 0xe0)
317 if (mii->receivedPitchBend)
318 mii->receivedPitchBend(mii->context, mp);
323 const char *hex =
"0123456789abcdef";
324 std::string messageHex;
325 for (
size_t i = 0; i < message->size(); ++i)
327 messageHex.push_back(hex[(*message)[i] >> 4]);
328 messageHex.push_back(hex[(*message)[i] & 0x0f]);
330 VUserLog(
"Warning: Received unknown message: 0x%s", messageHex.c_str());
347 throw RtError(
"No matching device found");
351 midiin->openPort(realizedDevice.
id,
"VuoMidiIn_make");
358 mii->
callbackQueue = dispatch_queue_create(
"vuo.midi.receive", NULL);
364 midiin->ignoreTypes(
true,
true,
true);
366 catch (RtError &error)
395 mii->receivedNote = receivedNote;
396 mii->receivedController = receivedController;
397 mii->receivedPitchBend = receivedPitchBend;
398 mii->context = context;
413 mii->receivedNote = NULL;
414 mii->receivedController = NULL;
433 #define setRealizedDevice(newDevice) \
434 realizedDevice->id = newDevice.id; \
435 realizedDevice->name = VuoText_make(newDevice.name);
463 __block
bool found =
false;
467 if (device.
id != -1 && device.
id == item.
id)
469 VDebugLog(
"Matched by ID: %s",json_object_to_json_string(VuoMidiInputDevice_getJson(item)));
470 setRealizedDevice(item);
482 VDebugLog(
"Matched by name: %s",json_object_to_json_string(VuoMidiInputDevice_getJson(item)));
483 setRealizedDevice(item);
534 __block
bool found =
false;
538 if (device.
id != -1 && device.
id == item.
id)
540 VDebugLog(
"Matched by ID: %s",json_object_to_json_string(VuoMidiOutputDevice_getJson(item)));
541 setRealizedDevice(item);
553 VDebugLog(
"Matched by name: %s",json_object_to_json_string(VuoMidiOutputDevice_getJson(item)));
554 setRealizedDevice(item);