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;
282 if (message->size() == 0)
286 unsigned char channel = ((*message)[0] & 0x0f) + 1;
287 if (((*message)[0] & 0xf0) == 0x80)
291 if (mii->receivedNote)
292 mii->receivedNote(mii->context, mn);
295 else if (((*message)[0] & 0xf0) == 0x90)
297 unsigned char velocity = (*message)[2];
300 bool isNoteOn = (velocity != 0);
304 if (mii->receivedNote)
305 mii->receivedNote(mii->context, mn);
308 else if (((*message)[0] & 0xf0) == 0xb0)
312 if (mii->receivedController)
313 mii->receivedController(mii->context, mc);
316 else if (((*message)[0] & 0xf0) == 0xe0)
320 if (mii->receivedPitchBend)
321 mii->receivedPitchBend(mii->context, mp);
326 const char *hex =
"0123456789abcdef";
327 std::string messageHex;
328 for (
size_t i = 0; i < message->size(); ++i)
330 messageHex.push_back(hex[(*message)[i] >> 4]);
331 messageHex.push_back(hex[(*message)[i] & 0x0f]);
333 VUserLog(
"Warning: Received unknown message: 0x%s", messageHex.c_str());
350 throw RtError(
"No matching device found");
354 midiin->openPort(realizedDevice.
id,
"VuoMidiIn_make");
361 mii->
callbackQueue = dispatch_queue_create(
"vuo.midi.receive", NULL);
367 midiin->ignoreTypes(
true,
true,
true);
369 catch (RtError &error)
398 mii->receivedNote = receivedNote;
399 mii->receivedController = receivedController;
400 mii->receivedPitchBend = receivedPitchBend;
401 mii->context = context;
416 mii->receivedNote = NULL;
417 mii->receivedController = NULL;
436 #define setRealizedDevice(newDevice) \
437 realizedDevice->id = newDevice.id; \
438 realizedDevice->name = VuoText_make(newDevice.name);
466 __block
bool found =
false;
470 if (device.
id != -1 && device.
id == item.
id)
472 VDebugLog(
"Matched by ID: %s",json_object_to_json_string(VuoMidiInputDevice_getJson(item)));
473 setRealizedDevice(item);
485 VDebugLog(
"Matched by name: %s",json_object_to_json_string(VuoMidiInputDevice_getJson(item)));
486 setRealizedDevice(item);
537 __block
bool found =
false;
541 if (device.
id != -1 && device.
id == item.
id)
543 VDebugLog(
"Matched by ID: %s",json_object_to_json_string(VuoMidiOutputDevice_getJson(item)));
544 setRealizedDevice(item);
556 VDebugLog(
"Matched by name: %s",json_object_to_json_string(VuoMidiOutputDevice_getJson(item)));
557 setRealizedDevice(item);