midi_driver.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* midi_driver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "midi_driver.h"
  31. #include "core/os/os.h"
  32. #include "main/input_default.h"
  33. uint8_t MIDIDriver::last_received_message = 0x00;
  34. MIDIDriver *MIDIDriver::singleton = NULL;
  35. MIDIDriver *MIDIDriver::get_singleton() {
  36. return singleton;
  37. }
  38. void MIDIDriver::set_singleton() {
  39. singleton = this;
  40. }
  41. void MIDIDriver::receive_input_packet(uint64_t timestamp, uint8_t *data, uint32_t length) {
  42. Ref<InputEventMIDI> event;
  43. event.instance();
  44. uint32_t param_position = 1;
  45. if (length >= 1) {
  46. if (data[0] >= 0xF0) {
  47. // channel does not apply to system common messages
  48. event->set_channel(0);
  49. event->set_message(data[0]);
  50. last_received_message = data[0];
  51. } else if ((data[0] & 0x80) == 0x00) {
  52. // running status
  53. event->set_channel(last_received_message & 0xF);
  54. event->set_message(last_received_message >> 4);
  55. param_position = 0;
  56. } else {
  57. event->set_channel(data[0] & 0xF);
  58. event->set_message(data[0] >> 4);
  59. param_position = 1;
  60. last_received_message = data[0];
  61. }
  62. }
  63. switch (event->get_message()) {
  64. case MIDI_MESSAGE_AFTERTOUCH:
  65. if (length >= 2 + param_position) {
  66. event->set_pitch(data[param_position]);
  67. event->set_pressure(data[param_position + 1]);
  68. }
  69. break;
  70. case MIDI_MESSAGE_CONTROL_CHANGE:
  71. if (length >= 2 + param_position) {
  72. event->set_controller_number(data[param_position]);
  73. event->set_controller_value(data[param_position + 1]);
  74. }
  75. break;
  76. case MIDI_MESSAGE_NOTE_ON:
  77. case MIDI_MESSAGE_NOTE_OFF:
  78. if (length >= 2 + param_position) {
  79. event->set_pitch(data[param_position]);
  80. event->set_velocity(data[param_position + 1]);
  81. if (event->get_message() == MIDI_MESSAGE_NOTE_ON && event->get_velocity() == 0) {
  82. // https://www.midi.org/forum/228-writing-midi-software-send-note-off,-or-zero-velocity-note-on
  83. event->set_message(MIDI_MESSAGE_NOTE_OFF);
  84. }
  85. }
  86. break;
  87. case MIDI_MESSAGE_PITCH_BEND:
  88. if (length >= 2 + param_position) {
  89. event->set_pitch((data[param_position + 1] << 7) | data[param_position]);
  90. }
  91. break;
  92. case MIDI_MESSAGE_PROGRAM_CHANGE:
  93. if (length >= 1 + param_position) {
  94. event->set_instrument(data[param_position]);
  95. }
  96. break;
  97. case MIDI_MESSAGE_CHANNEL_PRESSURE:
  98. if (length >= 1 + param_position) {
  99. event->set_pressure(data[param_position]);
  100. }
  101. break;
  102. }
  103. InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
  104. id->parse_input_event(event);
  105. }
  106. PoolStringArray MIDIDriver::get_connected_inputs() {
  107. PoolStringArray list;
  108. return list;
  109. }
  110. MIDIDriver::MIDIDriver() {
  111. set_singleton();
  112. }