midi_driver_alsamidi.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/string/print_string.h"
  34. #include <errno.h>
  35. MIDIDriverALSAMidi::MessageCategory MIDIDriverALSAMidi::msg_category(uint8_t msg_part) {
  36. if (msg_part >= 0xf8) {
  37. return MessageCategory::RealTime;
  38. } else if (msg_part >= 0xf0) {
  39. // System Exclusive begin/end are specified as System Common Category messages,
  40. // but we separate them here and give them their own categories as their
  41. // behavior is significantly different.
  42. if (msg_part == 0xf0) {
  43. return MessageCategory::SysExBegin;
  44. } else if (msg_part == 0xf7) {
  45. return MessageCategory::SysExEnd;
  46. }
  47. return MessageCategory::SystemCommon;
  48. } else if (msg_part >= 0x80) {
  49. return MessageCategory::Voice;
  50. }
  51. return MessageCategory::Data;
  52. }
  53. size_t MIDIDriverALSAMidi::msg_expected_data(uint8_t status_byte) {
  54. if (msg_category(status_byte) == MessageCategory::Voice) {
  55. // Voice messages have a channel number in the status byte, mask it out.
  56. status_byte &= 0xf0;
  57. }
  58. switch (status_byte) {
  59. case 0x80: // Note Off
  60. case 0x90: // Note On
  61. case 0xA0: // Polyphonic Key Pressure (Aftertouch)
  62. case 0xB0: // Control Change (CC)
  63. case 0xE0: // Pitch Bend Change
  64. case 0xF2: // Song Position Pointer
  65. return 2;
  66. case 0xC0: // Program Change
  67. case 0xD0: // Channel Pressure (Aftertouch)
  68. case 0xF1: // MIDI Time Code Quarter Frame
  69. case 0xF3: // Song Select
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. void MIDIDriverALSAMidi::InputConnection::parse_byte(uint8_t byte, MIDIDriverALSAMidi &driver,
  75. uint64_t timestamp) {
  76. switch (msg_category(byte)) {
  77. case MessageCategory::RealTime:
  78. // Real-Time messages are single byte messages that can
  79. // occur at any point.
  80. // We pass them straight through.
  81. driver.receive_input_packet(timestamp, &byte, 1);
  82. break;
  83. case MessageCategory::Data:
  84. // We don't currently forward System Exclusive messages so skip their data.
  85. // Collect any expected data for other message types.
  86. if (!skipping_sys_ex && expected_data > received_data) {
  87. buffer[received_data + 1] = byte;
  88. received_data++;
  89. // Forward a complete message and reset relevant state.
  90. if (received_data == expected_data) {
  91. driver.receive_input_packet(timestamp, buffer, received_data + 1);
  92. received_data = 0;
  93. if (msg_category(buffer[0]) != MessageCategory::Voice) {
  94. // Voice Category messages can be sent with "running status".
  95. // This means they don't resend the status byte until it changes.
  96. // For other categories, we reset expected data, to require a new status byte.
  97. expected_data = 0;
  98. }
  99. }
  100. }
  101. break;
  102. case MessageCategory::SysExBegin:
  103. buffer[0] = byte;
  104. skipping_sys_ex = true;
  105. break;
  106. case MessageCategory::SysExEnd:
  107. expected_data = 0;
  108. skipping_sys_ex = false;
  109. break;
  110. case MessageCategory::Voice:
  111. case MessageCategory::SystemCommon:
  112. buffer[0] = byte;
  113. received_data = 0;
  114. expected_data = msg_expected_data(byte);
  115. skipping_sys_ex = false;
  116. if (expected_data == 0) {
  117. driver.receive_input_packet(timestamp, &byte, 1);
  118. }
  119. break;
  120. }
  121. }
  122. int MIDIDriverALSAMidi::InputConnection::read_in(MIDIDriverALSAMidi &driver, uint64_t timestamp) {
  123. int ret;
  124. do {
  125. uint8_t byte = 0;
  126. ret = snd_rawmidi_read(rawmidi_ptr, &byte, 1);
  127. if (ret < 0) {
  128. if (ret != -EAGAIN) {
  129. ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(ret)));
  130. }
  131. } else {
  132. parse_byte(byte, driver, timestamp);
  133. }
  134. } while (ret > 0);
  135. return ret;
  136. }
  137. void MIDIDriverALSAMidi::thread_func(void *p_udata) {
  138. MIDIDriverALSAMidi *md = static_cast<MIDIDriverALSAMidi *>(p_udata);
  139. uint64_t timestamp = 0;
  140. while (!md->exit_thread.is_set()) {
  141. md->lock();
  142. InputConnection *connections = md->connected_inputs.ptrw();
  143. size_t connection_count = md->connected_inputs.size();
  144. for (size_t i = 0; i < connection_count; i++) {
  145. connections[i].read_in(*md, timestamp);
  146. }
  147. md->unlock();
  148. OS::get_singleton()->delay_usec(1000);
  149. }
  150. }
  151. Error MIDIDriverALSAMidi::open() {
  152. void **hints;
  153. if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) {
  154. return ERR_CANT_OPEN;
  155. }
  156. int i = 0;
  157. for (void **n = hints; *n != nullptr; n++) {
  158. char *name = snd_device_name_get_hint(*n, "NAME");
  159. if (name != nullptr) {
  160. snd_rawmidi_t *midi_in;
  161. int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK);
  162. if (ret >= 0) {
  163. connected_inputs.insert(i++, InputConnection(midi_in));
  164. }
  165. }
  166. if (name != nullptr) {
  167. free(name);
  168. }
  169. }
  170. snd_device_name_free_hint(hints);
  171. exit_thread.clear();
  172. thread.start(MIDIDriverALSAMidi::thread_func, this);
  173. return OK;
  174. }
  175. void MIDIDriverALSAMidi::close() {
  176. exit_thread.set();
  177. thread.wait_to_finish();
  178. for (int i = 0; i < connected_inputs.size(); i++) {
  179. snd_rawmidi_t *midi_in = connected_inputs[i].rawmidi_ptr;
  180. snd_rawmidi_close(midi_in);
  181. }
  182. connected_inputs.clear();
  183. }
  184. void MIDIDriverALSAMidi::lock() const {
  185. mutex.lock();
  186. }
  187. void MIDIDriverALSAMidi::unlock() const {
  188. mutex.unlock();
  189. }
  190. PackedStringArray MIDIDriverALSAMidi::get_connected_inputs() {
  191. PackedStringArray list;
  192. lock();
  193. for (int i = 0; i < connected_inputs.size(); i++) {
  194. snd_rawmidi_t *midi_in = connected_inputs[i].rawmidi_ptr;
  195. snd_rawmidi_info_t *info;
  196. snd_rawmidi_info_malloc(&info);
  197. snd_rawmidi_info(midi_in, info);
  198. list.push_back(snd_rawmidi_info_get_name(info));
  199. snd_rawmidi_info_free(info);
  200. }
  201. unlock();
  202. return list;
  203. }
  204. MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
  205. exit_thread.clear();
  206. }
  207. MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
  208. close();
  209. }
  210. #endif // ALSAMIDI_ENABLED