midi_driver_alsamidi.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**************************************************************************/
  2. /* midi_driver_alsamidi.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 ALSAMIDI_ENABLED
  31. #include "midi_driver_alsamidi.h"
  32. #include "core/os/os.h"
  33. #include <errno.h>
  34. MIDIDriverALSAMidi::InputConnection::InputConnection(int p_device_index,
  35. snd_rawmidi_t *p_rawmidi) :
  36. parser(p_device_index), rawmidi_ptr(p_rawmidi) {}
  37. void MIDIDriverALSAMidi::InputConnection::read() {
  38. int read_count;
  39. do {
  40. uint8_t buffer[32];
  41. read_count = snd_rawmidi_read(rawmidi_ptr, buffer, sizeof(buffer));
  42. if (read_count < 0) {
  43. if (read_count != -EAGAIN) {
  44. ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(read_count)));
  45. }
  46. } else {
  47. for (int i = 0; i < read_count; i++) {
  48. parser.parse_fragment(buffer[i]);
  49. }
  50. }
  51. } while (read_count > 0);
  52. }
  53. void MIDIDriverALSAMidi::thread_func(void *p_udata) {
  54. MIDIDriverALSAMidi *md = static_cast<MIDIDriverALSAMidi *>(p_udata);
  55. while (!md->exit_thread.is_set()) {
  56. md->lock();
  57. for (InputConnection &conn : md->connected_inputs) {
  58. conn.read();
  59. }
  60. md->unlock();
  61. OS::get_singleton()->delay_usec(1000);
  62. }
  63. }
  64. Error MIDIDriverALSAMidi::open() {
  65. void **hints;
  66. if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) {
  67. return ERR_CANT_OPEN;
  68. }
  69. lock();
  70. int device_index = 0;
  71. for (void **h = hints; *h != nullptr; h++) {
  72. char *name = snd_device_name_get_hint(*h, "NAME");
  73. if (name != nullptr) {
  74. snd_rawmidi_t *midi_in;
  75. int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK);
  76. if (ret >= 0) {
  77. // Get display name.
  78. snd_rawmidi_info_t *info;
  79. snd_rawmidi_info_malloc(&info);
  80. snd_rawmidi_info(midi_in, info);
  81. connected_input_names.push_back(snd_rawmidi_info_get_name(info));
  82. snd_rawmidi_info_free(info);
  83. connected_inputs.push_back(InputConnection(device_index, midi_in));
  84. // Only increment device_index for successfully connected devices.
  85. device_index++;
  86. }
  87. }
  88. if (name != nullptr) {
  89. free(name);
  90. }
  91. }
  92. snd_device_name_free_hint(hints);
  93. unlock();
  94. exit_thread.clear();
  95. thread.start(MIDIDriverALSAMidi::thread_func, this);
  96. return OK;
  97. }
  98. void MIDIDriverALSAMidi::close() {
  99. exit_thread.set();
  100. if (thread.is_started()) {
  101. thread.wait_to_finish();
  102. }
  103. for (const InputConnection &conn : connected_inputs) {
  104. snd_rawmidi_close(conn.rawmidi_ptr);
  105. }
  106. connected_inputs.clear();
  107. connected_input_names.clear();
  108. }
  109. void MIDIDriverALSAMidi::lock() const {
  110. mutex.lock();
  111. }
  112. void MIDIDriverALSAMidi::unlock() const {
  113. mutex.unlock();
  114. }
  115. MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
  116. exit_thread.clear();
  117. }
  118. MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
  119. close();
  120. }
  121. #endif // ALSAMIDI_ENABLED