midi_driver_alsamidi.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "core/print_string.h"
  34. #include <errno.h>
  35. static int get_message_size(uint8_t message) {
  36. switch (message & 0xF0) {
  37. case 0x80: // note off
  38. case 0x90: // note on
  39. case 0xA0: // aftertouch
  40. case 0xB0: // continuous controller
  41. case 0xE0: // pitch bend
  42. case 0xF2: // song position pointer
  43. return 3;
  44. case 0xC0: // patch change
  45. case 0xD0: // channel pressure
  46. case 0xF1: // time code quarter frame
  47. case 0xF3: // song select
  48. return 2;
  49. case 0xF0: // SysEx start
  50. case 0xF4: // reserved
  51. case 0xF5: // reserved
  52. case 0xF6: // tune request
  53. case 0xF7: // SysEx end
  54. case 0xF8: // timing clock
  55. case 0xF9: // reserved
  56. case 0xFA: // start
  57. case 0xFB: // continue
  58. case 0xFC: // stop
  59. case 0xFD: // reserved
  60. case 0xFE: // active sensing
  61. case 0xFF: // reset
  62. return 1;
  63. }
  64. return 256;
  65. }
  66. void MIDIDriverALSAMidi::thread_func(void *p_udata) {
  67. MIDIDriverALSAMidi *md = (MIDIDriverALSAMidi *)p_udata;
  68. uint64_t timestamp = 0;
  69. uint8_t buffer[256];
  70. int expected_size = 255;
  71. int bytes = 0;
  72. while (!md->exit_thread.is_set()) {
  73. int ret;
  74. md->lock();
  75. for (int i = 0; i < md->connected_inputs.size(); i++) {
  76. snd_rawmidi_t *midi_in = md->connected_inputs[i];
  77. do {
  78. uint8_t byte = 0;
  79. ret = snd_rawmidi_read(midi_in, &byte, 1);
  80. if (ret < 0) {
  81. if (ret != -EAGAIN) {
  82. ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(ret)));
  83. }
  84. } else {
  85. if (byte & 0x80) {
  86. // Flush previous packet if there is any
  87. if (bytes) {
  88. md->receive_input_packet(timestamp, buffer, bytes);
  89. bytes = 0;
  90. }
  91. expected_size = get_message_size(byte);
  92. // After a SysEx start, all bytes are data until a SysEx end, so
  93. // we're going to end the command at the SES, and let the common
  94. // driver ignore the following data bytes.
  95. }
  96. if (bytes < 256) {
  97. buffer[bytes++] = byte;
  98. // If we know the size of the current packet receive it if it reached the expected size
  99. if (bytes >= expected_size) {
  100. md->receive_input_packet(timestamp, buffer, bytes);
  101. bytes = 0;
  102. }
  103. }
  104. }
  105. } while (ret > 0);
  106. }
  107. md->unlock();
  108. OS::get_singleton()->delay_usec(1000);
  109. }
  110. }
  111. Error MIDIDriverALSAMidi::open() {
  112. void **hints;
  113. if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) {
  114. return ERR_CANT_OPEN;
  115. }
  116. int i = 0;
  117. for (void **n = hints; *n != nullptr; n++) {
  118. char *name = snd_device_name_get_hint(*n, "NAME");
  119. if (name != nullptr) {
  120. snd_rawmidi_t *midi_in;
  121. int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK);
  122. if (ret >= 0) {
  123. connected_inputs.insert(i++, midi_in);
  124. }
  125. }
  126. if (name != nullptr) {
  127. free(name);
  128. }
  129. }
  130. snd_device_name_free_hint(hints);
  131. exit_thread.clear();
  132. thread.start(MIDIDriverALSAMidi::thread_func, this);
  133. return OK;
  134. }
  135. void MIDIDriverALSAMidi::close() {
  136. exit_thread.set();
  137. thread.wait_to_finish();
  138. for (int i = 0; i < connected_inputs.size(); i++) {
  139. snd_rawmidi_t *midi_in = connected_inputs[i];
  140. snd_rawmidi_close(midi_in);
  141. }
  142. connected_inputs.clear();
  143. }
  144. void MIDIDriverALSAMidi::lock() const {
  145. mutex.lock();
  146. }
  147. void MIDIDriverALSAMidi::unlock() const {
  148. mutex.unlock();
  149. }
  150. PoolStringArray MIDIDriverALSAMidi::get_connected_inputs() {
  151. PoolStringArray list;
  152. lock();
  153. for (int i = 0; i < connected_inputs.size(); i++) {
  154. snd_rawmidi_t *midi_in = connected_inputs[i];
  155. snd_rawmidi_info_t *info;
  156. snd_rawmidi_info_malloc(&info);
  157. snd_rawmidi_info(midi_in, info);
  158. list.push_back(snd_rawmidi_info_get_name(info));
  159. snd_rawmidi_info_free(info);
  160. }
  161. unlock();
  162. return list;
  163. }
  164. MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
  165. exit_thread.clear();
  166. }
  167. MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
  168. close();
  169. }
  170. #endif // ALSAMIDI_ENABLED