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)
212 RtMidiOut *midiout = (RtMidiOut *)mo;
213 std::vector<unsigned char> message;
217 midiout->sendMessage(&message);
228 RtMidiOut *midiout = (RtMidiOut *)mo;
229 std::vector<unsigned char> message;
230 message.push_back(0xB0 +
MIN(controller.
channel-1,15));
232 message.push_back(
MIN(controller.
value,127));
233 midiout->sendMessage(&message);
244 RtMidiOut *midiout = (RtMidiOut *)mo;
245 std::vector<unsigned char> message;
246 message.push_back(0xE0 +
MIN(pitchBend.
channel-1,15));
247 message.push_back(pitchBend.
value & 0x7f);
248 message.push_back((pitchBend.
value >> 7) & 0x7f);
249 midiout->sendMessage(&message);
260 RtMidiOut *midiout = (RtMidiOut *)mo;
284 unsigned char channel = ((*message)[0] & 0x0f) + 1;
285 if (((*message)[0] & 0xf0) == 0x80)
289 if (mii->receivedNote)
290 mii->receivedNote(mii->context, mn);
293 else if (((*message)[0] & 0xf0) == 0x90)
295 unsigned char velocity = (*message)[2];
298 bool isNoteOn = (velocity != 0);
302 if (mii->receivedNote)
303 mii->receivedNote(mii->context, mn);
306 else if (((*message)[0] & 0xf0) == 0xb0)
310 if (mii->receivedController)
311 mii->receivedController(mii->context, mc);
314 else if (((*message)[0] & 0xf0) == 0xe0)
318 if (mii->receivedPitchBend)
319 mii->receivedPitchBend(mii->context, mp);
324 const char *hex =
"0123456789abcdef";
325 std::string messageHex;
326 for (
size_t i = 0; i < message->size(); ++i)
328 messageHex.push_back(hex[(*message)[i] >> 4]);
329 messageHex.push_back(hex[(*message)[i] & 0x0f]);
331 VUserLog(
"Warning: Received unknown message: 0x%s", messageHex.c_str());
348 throw RtError(
"No matching device found");
352 midiin->openPort(realizedDevice.
id,
"VuoMidiIn_make");
359 mii->
callbackQueue = dispatch_queue_create(
"vuo.midi.receive", NULL);
365 midiin->ignoreTypes(
true,
true,
true);
367 catch (RtError &error)
397 mii->receivedNote = receivedNote;
398 mii->receivedController = receivedController;
399 mii->receivedPitchBend = receivedPitchBend;
400 mii->context = context;
415 mii->receivedNote = NULL;
416 mii->receivedController = NULL;
435 #define setRealizedDevice(newDevice) \
436 realizedDevice->id = newDevice.id; \
437 realizedDevice->name = VuoText_make(newDevice.name);
465 __block
bool found =
false;
469 if (device.
id != -1 && device.
id == item.
id)
471 VDebugLog(
"Matched by ID: %s",json_object_to_json_string(VuoMidiInputDevice_getJson(item)));
472 setRealizedDevice(item);
484 VDebugLog(
"Matched by name: %s",json_object_to_json_string(VuoMidiInputDevice_getJson(item)));
485 setRealizedDevice(item);
536 __block
bool found =
false;
540 if (device.
id != -1 && device.
id == item.
id)
542 VDebugLog(
"Matched by ID: %s",json_object_to_json_string(VuoMidiOutputDevice_getJson(item)));
543 setRealizedDevice(item);
555 VDebugLog(
"Matched by name: %s",json_object_to_json_string(VuoMidiOutputDevice_getJson(item)));
556 setRealizedDevice(item);