midi_driver.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* midi_driver.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. #include "midi_driver.h"
  31. #include "core/input/input.h"
  32. uint8_t MIDIDriver::last_received_message = 0x00;
  33. MIDIDriver *MIDIDriver::singleton = nullptr;
  34. MIDIDriver *MIDIDriver::get_singleton() {
  35. return singleton;
  36. }
  37. MIDIDriver::MIDIDriver() {
  38. singleton = this;
  39. }
  40. MIDIDriver::MessageCategory MIDIDriver::Parser::category(uint8_t p_midi_fragment) {
  41. if (p_midi_fragment >= 0xf8) {
  42. return MessageCategory::RealTime;
  43. } else if (p_midi_fragment >= 0xf0) {
  44. // System Exclusive begin/end are specified as System Common Category
  45. // messages, but we separate them here and give them their own categories
  46. // as their behavior is significantly different.
  47. if (p_midi_fragment == 0xf0) {
  48. return MessageCategory::SysExBegin;
  49. } else if (p_midi_fragment == 0xf7) {
  50. return MessageCategory::SysExEnd;
  51. }
  52. return MessageCategory::SystemCommon;
  53. } else if (p_midi_fragment >= 0x80) {
  54. return MessageCategory::Voice;
  55. }
  56. return MessageCategory::Data;
  57. }
  58. MIDIMessage MIDIDriver::Parser::status_to_msg_enum(uint8_t p_status_byte) {
  59. if (p_status_byte & 0x80) {
  60. if (p_status_byte < 0xf0) {
  61. return MIDIMessage(p_status_byte >> 4);
  62. } else {
  63. return MIDIMessage(p_status_byte);
  64. }
  65. }
  66. return MIDIMessage::NONE;
  67. }
  68. size_t MIDIDriver::Parser::expected_data(uint8_t p_status_byte) {
  69. return expected_data(status_to_msg_enum(p_status_byte));
  70. }
  71. size_t MIDIDriver::Parser::expected_data(MIDIMessage p_msg_type) {
  72. switch (p_msg_type) {
  73. case MIDIMessage::NOTE_OFF:
  74. case MIDIMessage::NOTE_ON:
  75. case MIDIMessage::AFTERTOUCH:
  76. case MIDIMessage::CONTROL_CHANGE:
  77. case MIDIMessage::PITCH_BEND:
  78. case MIDIMessage::SONG_POSITION_POINTER:
  79. return 2;
  80. case MIDIMessage::PROGRAM_CHANGE:
  81. case MIDIMessage::CHANNEL_PRESSURE:
  82. case MIDIMessage::QUARTER_FRAME:
  83. case MIDIMessage::SONG_SELECT:
  84. return 1;
  85. default:
  86. return 0;
  87. }
  88. }
  89. uint8_t MIDIDriver::Parser::channel(uint8_t p_status_byte) {
  90. if (category(p_status_byte) == MessageCategory::Voice) {
  91. return p_status_byte & 0x0f;
  92. }
  93. return 0;
  94. }
  95. void MIDIDriver::send_event(int p_device_index, uint8_t p_status,
  96. const uint8_t *p_data, size_t p_data_len) {
  97. const MIDIMessage msg = Parser::status_to_msg_enum(p_status);
  98. ERR_FAIL_COND(p_data_len < Parser::expected_data(msg));
  99. Ref<InputEventMIDI> event;
  100. event.instantiate();
  101. event->set_device(p_device_index);
  102. event->set_channel(Parser::channel(p_status));
  103. event->set_message(msg);
  104. switch (msg) {
  105. case MIDIMessage::NOTE_OFF:
  106. case MIDIMessage::NOTE_ON:
  107. event->set_pitch(p_data[0]);
  108. event->set_velocity(p_data[1]);
  109. break;
  110. case MIDIMessage::AFTERTOUCH:
  111. event->set_pitch(p_data[0]);
  112. event->set_pressure(p_data[1]);
  113. break;
  114. case MIDIMessage::CONTROL_CHANGE:
  115. event->set_controller_number(p_data[0]);
  116. event->set_controller_value(p_data[1]);
  117. break;
  118. case MIDIMessage::PROGRAM_CHANGE:
  119. event->set_instrument(p_data[0]);
  120. break;
  121. case MIDIMessage::CHANNEL_PRESSURE:
  122. event->set_pressure(p_data[0]);
  123. break;
  124. case MIDIMessage::PITCH_BEND:
  125. event->set_pitch((p_data[1] << 7) | p_data[0]);
  126. break;
  127. // QUARTER_FRAME, SONG_POSITION_POINTER, and SONG_SELECT not yet implemented.
  128. default:
  129. break;
  130. }
  131. Input::get_singleton()->parse_input_event(event);
  132. }
  133. void MIDIDriver::Parser::parse_fragment(uint8_t p_fragment) {
  134. switch (category(p_fragment)) {
  135. case MessageCategory::RealTime:
  136. // Real-Time messages are single byte messages that can
  137. // occur at any point and do not interrupt other messages.
  138. // We pass them straight through.
  139. MIDIDriver::send_event(device_index, p_fragment);
  140. break;
  141. case MessageCategory::SysExBegin:
  142. status_byte = p_fragment;
  143. skipping_sys_ex = true;
  144. break;
  145. case MessageCategory::SysExEnd:
  146. status_byte = 0;
  147. skipping_sys_ex = false;
  148. break;
  149. case MessageCategory::Voice:
  150. case MessageCategory::SystemCommon:
  151. skipping_sys_ex = false; // If we were in SysEx, assume it was aborted.
  152. received_data_len = 0;
  153. status_byte = 0;
  154. ERR_FAIL_COND(expected_data(p_fragment) > DATA_BUFFER_SIZE);
  155. if (expected_data(p_fragment) == 0) {
  156. // No data bytes needed, post it now.
  157. MIDIDriver::send_event(device_index, p_fragment);
  158. } else {
  159. status_byte = p_fragment;
  160. }
  161. break;
  162. case MessageCategory::Data:
  163. // We don't currently process SysEx messages, so ignore their data.
  164. if (!skipping_sys_ex) {
  165. const size_t expected = expected_data(status_byte);
  166. if (received_data_len < expected) {
  167. data_buffer[received_data_len] = p_fragment;
  168. received_data_len++;
  169. if (received_data_len == expected) {
  170. MIDIDriver::send_event(device_index, status_byte,
  171. data_buffer, expected);
  172. received_data_len = 0;
  173. // Voice messages can use 'running status', sending further
  174. // messages without resending their status byte.
  175. // For other messages types we clear the cached status byte.
  176. if (category(status_byte) != MessageCategory::Voice) {
  177. status_byte = 0;
  178. }
  179. }
  180. }
  181. }
  182. break;
  183. }
  184. }
  185. PackedStringArray MIDIDriver::get_connected_inputs() const {
  186. return connected_input_names;
  187. }