midi_driver_coremidi.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**************************************************************************/
  2. /* midi_driver_coremidi.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "midi_driver_coremidi.h"
  31. #ifdef COREMIDI_ENABLED
  32. #include "core/string/print_string.h"
  33. #import <CoreAudio/HostTime.h>
  34. #import <CoreServices/CoreServices.h>
  35. Mutex MIDIDriverCoreMidi::mutex;
  36. bool MIDIDriverCoreMidi::core_midi_closed = false;
  37. MIDIDriverCoreMidi::InputConnection::InputConnection(int p_device_index, MIDIEndpointRef p_source) :
  38. parser(p_device_index), source(p_source) {}
  39. void MIDIDriverCoreMidi::read(const MIDIPacketList *packet_list, void *read_proc_ref_con, void *src_conn_ref_con) {
  40. MutexLock lock(mutex);
  41. if (!core_midi_closed) {
  42. InputConnection *source = static_cast<InputConnection *>(src_conn_ref_con);
  43. const MIDIPacket *packet = packet_list->packet;
  44. for (UInt32 packet_index = 0; packet_index < packet_list->numPackets; packet_index++) {
  45. for (UInt16 data_index = 0; data_index < packet->length; data_index++) {
  46. source->parser.parse_fragment(packet->data[data_index]);
  47. }
  48. packet = MIDIPacketNext(packet);
  49. }
  50. }
  51. }
  52. Error MIDIDriverCoreMidi::open() {
  53. ERR_FAIL_COND_V_MSG(client || core_midi_closed, FAILED,
  54. "MIDIDriverCoreMidi cannot be reopened.");
  55. CFStringRef name = CFStringCreateWithCString(nullptr, "Godot", kCFStringEncodingASCII);
  56. OSStatus result = MIDIClientCreate(name, nullptr, nullptr, &client);
  57. CFRelease(name);
  58. if (result != noErr) {
  59. ERR_PRINT("MIDIClientCreate failed, code: " + itos(result));
  60. return ERR_CANT_OPEN;
  61. }
  62. result = MIDIInputPortCreate(client, CFSTR("Godot Input"), MIDIDriverCoreMidi::read, (void *)this, &port_in);
  63. if (result != noErr) {
  64. ERR_PRINT("MIDIInputPortCreate failed, code: " + itos(result));
  65. return ERR_CANT_OPEN;
  66. }
  67. int source_count = MIDIGetNumberOfSources();
  68. int connection_index = 0;
  69. for (int i = 0; i < source_count; i++) {
  70. MIDIEndpointRef source = MIDIGetSource(i);
  71. if (source) {
  72. InputConnection *conn = memnew(InputConnection(connection_index, source));
  73. const OSStatus res = MIDIPortConnectSource(port_in, source, static_cast<void *>(conn));
  74. if (res != noErr) {
  75. memdelete(conn);
  76. } else {
  77. connected_sources.push_back(conn);
  78. CFStringRef nameRef = nullptr;
  79. char name[256];
  80. MIDIObjectGetStringProperty(source, kMIDIPropertyDisplayName, &nameRef);
  81. CFStringGetCString(nameRef, name, sizeof(name), kCFStringEncodingUTF8);
  82. CFRelease(nameRef);
  83. connected_input_names.push_back(name);
  84. connection_index++; // Contiguous index for successfully connected inputs.
  85. }
  86. }
  87. }
  88. return OK;
  89. }
  90. void MIDIDriverCoreMidi::close() {
  91. mutex.lock();
  92. core_midi_closed = true;
  93. mutex.unlock();
  94. for (InputConnection *conn : connected_sources) {
  95. MIDIPortDisconnectSource(port_in, conn->source);
  96. memdelete(conn);
  97. }
  98. connected_sources.clear();
  99. connected_input_names.clear();
  100. if (port_in != 0) {
  101. MIDIPortDispose(port_in);
  102. port_in = 0;
  103. }
  104. if (client != 0) {
  105. MIDIClientDispose(client);
  106. client = 0;
  107. }
  108. }
  109. MIDIDriverCoreMidi::~MIDIDriverCoreMidi() {
  110. close();
  111. }
  112. #endif // COREMIDI_ENABLED