MidiOutCoreMIDI.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #if defined(__APPLE__)
  2. #include "MidiOutCoreMIDI.hh"
  3. #include "PluggingController.hh"
  4. #include "PlugException.hh"
  5. #include "serialize.hh"
  6. #include "openmsx.hh"
  7. #include "StringOp.hh"
  8. #include <mach/mach_time.h>
  9. #include <cassert>
  10. #include <memory>
  11. namespace openmsx {
  12. // MidiOutMessageBuffer ======================================================
  13. void MidiOutMessageBuffer::recvMessage(
  14. const std::vector<uint8_t>& message, EmuTime::param /*time*/)
  15. {
  16. // TODO: It would be better to schedule events based on EmuTime.
  17. MIDITimeStamp abstime = mach_absolute_time();
  18. MIDIPacketList packetList;
  19. MIDIPacket *curPacket = MIDIPacketListInit(&packetList);
  20. curPacket = MIDIPacketListAdd(&packetList, sizeof(packetList),
  21. curPacket, abstime, message.size(), message.data());
  22. if (!curPacket) {
  23. fprintf(stderr, "Failed to package MIDI data\n");
  24. } else if (OSStatus status = sendPacketList(&packetList)) {
  25. fprintf(stderr, "Failed to send MIDI data (%d)\n", int(status));
  26. } else {
  27. //fprintf(stderr, "MIDI send OK: %02X\n", value);
  28. }
  29. }
  30. // MidiOutCoreMIDI ===========================================================
  31. void MidiOutCoreMIDI::registerAll(PluggingController& controller)
  32. {
  33. ItemCount numberOfEndpoints = MIDIGetNumberOfDestinations();
  34. for (ItemCount i = 0; i < numberOfEndpoints; i++) {
  35. MIDIEndpointRef endpoint = MIDIGetDestination(i);
  36. if (endpoint) {
  37. controller.registerPluggable(
  38. std::make_unique<MidiOutCoreMIDI>(endpoint));
  39. }
  40. }
  41. }
  42. MidiOutCoreMIDI::MidiOutCoreMIDI(MIDIEndpointRef endpoint_)
  43. : endpoint(endpoint_)
  44. {
  45. // Get a user-presentable name for the endpoint.
  46. CFStringRef midiDeviceName;
  47. OSStatus status = MIDIObjectGetStringProperty(
  48. endpoint, kMIDIPropertyDisplayName, &midiDeviceName);
  49. if (status) {
  50. status = MIDIObjectGetStringProperty(
  51. endpoint, kMIDIPropertyName, &midiDeviceName);
  52. }
  53. if (status) {
  54. name = "Nameless endpoint";
  55. } else {
  56. name = strCat(StringOp::fromCFString(midiDeviceName), " OUT");
  57. CFRelease(midiDeviceName);
  58. }
  59. }
  60. void MidiOutCoreMIDI::plugHelper(Connector& /*connector*/,
  61. EmuTime::param /*time*/)
  62. {
  63. // Create client.
  64. if (OSStatus status = MIDIClientCreate(CFSTR("openMSX"), nullptr, nullptr, &client)) {
  65. throw PlugException("Failed to create MIDI client (", status, ')');
  66. }
  67. // Create output port.
  68. if (OSStatus status = MIDIOutputPortCreate(client, CFSTR("Output"), &port)) {
  69. MIDIClientDispose(client);
  70. client = 0;
  71. throw PlugException("Failed to create MIDI port (", status, ')');
  72. }
  73. }
  74. void MidiOutCoreMIDI::unplugHelper(EmuTime::param /*time*/)
  75. {
  76. clearBuffer();
  77. // Dispose of the client; this automatically disposes of the port as well.
  78. if (OSStatus status = MIDIClientDispose(client)) {
  79. fprintf(stderr, "Failed to dispose of MIDI client (%d)\n", int(status));
  80. }
  81. port = 0;
  82. client = 0;
  83. }
  84. const std::string& MidiOutCoreMIDI::getName() const
  85. {
  86. return name;
  87. }
  88. std::string_view MidiOutCoreMIDI::getDescription() const
  89. {
  90. return "Sends MIDI events to an existing CoreMIDI destination.";
  91. }
  92. OSStatus MidiOutCoreMIDI::sendPacketList(MIDIPacketList *myPacketList)
  93. {
  94. return MIDISend(port, endpoint, myPacketList);
  95. }
  96. template<typename Archive>
  97. void MidiOutCoreMIDI::serialize(Archive& /*ar*/, unsigned /*version*/)
  98. {
  99. }
  100. INSTANTIATE_SERIALIZE_METHODS(MidiOutCoreMIDI);
  101. REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, MidiOutCoreMIDI, "MidiOutCoreMIDI");
  102. // MidiOutCoreMIDIVirtual ====================================================
  103. MidiOutCoreMIDIVirtual:: MidiOutCoreMIDIVirtual()
  104. : client(0)
  105. , endpoint(0)
  106. {
  107. }
  108. void MidiOutCoreMIDIVirtual::plugHelper(Connector& /*connector*/,
  109. EmuTime::param /*time*/)
  110. {
  111. // Create client.
  112. if (OSStatus status = MIDIClientCreate(CFSTR("openMSX"), nullptr, nullptr, &client)) {
  113. throw PlugException("Failed to create MIDI client (", status, ')');
  114. }
  115. // Create endpoint.
  116. if (OSStatus status = MIDISourceCreate(client, CFSTR("openMSX"), &endpoint)) {
  117. MIDIClientDispose(client);
  118. throw PlugException("Failed to create MIDI endpoint (", status, ')');
  119. }
  120. }
  121. void MidiOutCoreMIDIVirtual::unplugHelper(EmuTime::param /*time*/)
  122. {
  123. clearBuffer();
  124. if (OSStatus status = MIDIEndpointDispose(endpoint)) {
  125. fprintf(stderr, "Failed to dispose of MIDI port (%d)\n", int(status));
  126. }
  127. endpoint = 0;
  128. if (OSStatus status = MIDIClientDispose(client)) {
  129. fprintf(stderr, "Failed to dispose of MIDI client (%d)\n", int(status));
  130. }
  131. client = 0;
  132. }
  133. const std::string& MidiOutCoreMIDIVirtual::getName() const
  134. {
  135. static const std::string name("Virtual OUT");
  136. return name;
  137. }
  138. std::string_view MidiOutCoreMIDIVirtual::getDescription() const
  139. {
  140. return "Sends MIDI events from a newly created CoreMIDI virtual source.";
  141. }
  142. OSStatus MidiOutCoreMIDIVirtual::sendPacketList(MIDIPacketList *myPacketList)
  143. {
  144. return MIDIReceived(endpoint, myPacketList);
  145. }
  146. template<typename Archive>
  147. void MidiOutCoreMIDIVirtual::serialize(Archive& /*ar*/, unsigned /*version*/)
  148. {
  149. }
  150. INSTANTIATE_SERIALIZE_METHODS(MidiOutCoreMIDIVirtual);
  151. REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, MidiOutCoreMIDIVirtual, "MidiOutCoreMIDIVirtual");
  152. } // namespace openmsx
  153. #endif // defined(__APPLE__)