midi_driver_coremidi.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifdef COREMIDI_ENABLED
  31. #include "midi_driver_coremidi.h"
  32. #include "core/string/print_string.h"
  33. #import <CoreAudio/HostTime.h>
  34. #import <CoreServices/CoreServices.h>
  35. void MIDIDriverCoreMidi::read(const MIDIPacketList *packet_list, void *read_proc_ref_con, void *src_conn_ref_con) {
  36. MIDIPacket *packet = const_cast<MIDIPacket *>(packet_list->packet);
  37. for (UInt32 i = 0; i < packet_list->numPackets; i++) {
  38. receive_input_packet(packet->timeStamp, packet->data, packet->length);
  39. packet = MIDIPacketNext(packet);
  40. }
  41. }
  42. Error MIDIDriverCoreMidi::open() {
  43. CFStringRef name = CFStringCreateWithCString(nullptr, "Godot", kCFStringEncodingASCII);
  44. OSStatus result = MIDIClientCreate(name, nullptr, nullptr, &client);
  45. CFRelease(name);
  46. if (result != noErr) {
  47. ERR_PRINT("MIDIClientCreate failed, code: " + itos(result));
  48. return ERR_CANT_OPEN;
  49. }
  50. result = MIDIInputPortCreate(client, CFSTR("Godot Input"), MIDIDriverCoreMidi::read, (void *)this, &port_in);
  51. if (result != noErr) {
  52. ERR_PRINT("MIDIInputPortCreate failed, code: " + itos(result));
  53. return ERR_CANT_OPEN;
  54. }
  55. int sources = MIDIGetNumberOfSources();
  56. for (int i = 0; i < sources; i++) {
  57. MIDIEndpointRef source = MIDIGetSource(i);
  58. if (source) {
  59. MIDIPortConnectSource(port_in, source, (void *)this);
  60. connected_sources.insert(i, source);
  61. }
  62. }
  63. return OK;
  64. }
  65. void MIDIDriverCoreMidi::close() {
  66. for (int i = 0; i < connected_sources.size(); i++) {
  67. MIDIEndpointRef source = connected_sources[i];
  68. MIDIPortDisconnectSource(port_in, source);
  69. }
  70. connected_sources.clear();
  71. if (port_in != 0) {
  72. MIDIPortDispose(port_in);
  73. port_in = 0;
  74. }
  75. if (client != 0) {
  76. MIDIClientDispose(client);
  77. client = 0;
  78. }
  79. }
  80. PackedStringArray MIDIDriverCoreMidi::get_connected_inputs() {
  81. PackedStringArray list;
  82. for (int i = 0; i < connected_sources.size(); i++) {
  83. MIDIEndpointRef source = connected_sources[i];
  84. CFStringRef ref = nullptr;
  85. char name[256];
  86. MIDIObjectGetStringProperty(source, kMIDIPropertyDisplayName, &ref);
  87. CFStringGetCString(ref, name, sizeof(name), kCFStringEncodingUTF8);
  88. CFRelease(ref);
  89. list.push_back(name);
  90. }
  91. return list;
  92. }
  93. MIDIDriverCoreMidi::MIDIDriverCoreMidi() {}
  94. MIDIDriverCoreMidi::~MIDIDriverCoreMidi() {
  95. close();
  96. }
  97. #endif // COREMIDI_ENABLED