midi_driver_alsamidi.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*************************************************************************/
  2. /* midi_driver_alsamidi.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. return 3;
  42. case 0xC0: // patch change
  43. case 0xD0: // channel pressure
  44. case 0xE0: // pitch bend
  45. return 2;
  46. }
  47. return 256;
  48. }
  49. void MIDIDriverALSAMidi::thread_func(void *p_udata) {
  50. MIDIDriverALSAMidi *md = (MIDIDriverALSAMidi *)p_udata;
  51. uint64_t timestamp = 0;
  52. uint8_t buffer[256];
  53. int expected_size = 255;
  54. int bytes = 0;
  55. while (!md->exit_thread) {
  56. int ret;
  57. md->lock();
  58. for (int i = 0; i < md->connected_inputs.size(); i++) {
  59. snd_rawmidi_t *midi_in = md->connected_inputs[i];
  60. do {
  61. uint8_t byte = 0;
  62. ret = snd_rawmidi_read(midi_in, &byte, 1);
  63. if (ret < 0) {
  64. if (ret != -EAGAIN) {
  65. ERR_PRINTS("snd_rawmidi_read error: " + String(snd_strerror(ret)));
  66. }
  67. } else {
  68. if (byte & 0x80) {
  69. // Flush previous packet if there is any
  70. if (bytes) {
  71. md->receive_input_packet(timestamp, buffer, bytes);
  72. bytes = 0;
  73. }
  74. expected_size = get_message_size(byte);
  75. }
  76. if (bytes < 256) {
  77. buffer[bytes++] = byte;
  78. // If we know the size of the current packet receive it if it reached the expected size
  79. if (bytes >= expected_size) {
  80. md->receive_input_packet(timestamp, buffer, bytes);
  81. bytes = 0;
  82. }
  83. }
  84. }
  85. } while (ret > 0);
  86. }
  87. md->unlock();
  88. OS::get_singleton()->delay_usec(1000);
  89. }
  90. }
  91. Error MIDIDriverALSAMidi::open() {
  92. void **hints;
  93. if (snd_device_name_hint(-1, "rawmidi", &hints) < 0)
  94. return ERR_CANT_OPEN;
  95. int i = 0;
  96. for (void **n = hints; *n != NULL; n++) {
  97. char *name = snd_device_name_get_hint(*n, "NAME");
  98. if (name != NULL) {
  99. snd_rawmidi_t *midi_in;
  100. int ret = snd_rawmidi_open(&midi_in, NULL, name, SND_RAWMIDI_NONBLOCK);
  101. if (ret >= 0) {
  102. connected_inputs.insert(i++, midi_in);
  103. }
  104. }
  105. if (name != NULL)
  106. free(name);
  107. }
  108. snd_device_name_free_hint(hints);
  109. mutex = Mutex::create();
  110. thread = Thread::create(MIDIDriverALSAMidi::thread_func, this);
  111. return OK;
  112. }
  113. void MIDIDriverALSAMidi::close() {
  114. if (thread) {
  115. exit_thread = true;
  116. Thread::wait_to_finish(thread);
  117. memdelete(thread);
  118. thread = NULL;
  119. }
  120. if (mutex) {
  121. memdelete(mutex);
  122. mutex = NULL;
  123. }
  124. for (int i = 0; i < connected_inputs.size(); i++) {
  125. snd_rawmidi_t *midi_in = connected_inputs[i];
  126. snd_rawmidi_close(midi_in);
  127. }
  128. connected_inputs.clear();
  129. }
  130. void MIDIDriverALSAMidi::lock() const {
  131. if (mutex)
  132. mutex->lock();
  133. }
  134. void MIDIDriverALSAMidi::unlock() const {
  135. if (mutex)
  136. mutex->unlock();
  137. }
  138. PoolStringArray MIDIDriverALSAMidi::get_connected_inputs() {
  139. PoolStringArray list;
  140. lock();
  141. for (int i = 0; i < connected_inputs.size(); i++) {
  142. snd_rawmidi_t *midi_in = connected_inputs[i];
  143. snd_rawmidi_info_t *info;
  144. snd_rawmidi_info_malloc(&info);
  145. snd_rawmidi_info(midi_in, info);
  146. list.push_back(snd_rawmidi_info_get_name(info));
  147. snd_rawmidi_info_free(info);
  148. }
  149. unlock();
  150. return list;
  151. }
  152. MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
  153. mutex = NULL;
  154. thread = NULL;
  155. exit_thread = false;
  156. }
  157. MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
  158. close();
  159. }
  160. #endif // ALSAMIDI_ENABLED