midi_driver.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. void MIDIDriver::set_singleton() {
  38. singleton = this;
  39. }
  40. void MIDIDriver::receive_input_packet(int device_index, uint64_t timestamp, uint8_t *data, uint32_t length) {
  41. Ref<InputEventMIDI> event;
  42. event.instantiate();
  43. event->set_device(device_index);
  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(MIDIMessage(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(MIDIMessage(last_received_message >> 4));
  55. param_position = 0;
  56. } else {
  57. event->set_channel(data[0] & 0xF);
  58. event->set_message(MIDIMessage(data[0] >> 4));
  59. param_position = 1;
  60. last_received_message = data[0];
  61. }
  62. }
  63. switch (event->get_message()) {
  64. case MIDIMessage::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 MIDIMessage::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 MIDIMessage::NOTE_ON:
  77. case MIDIMessage::NOTE_OFF:
  78. if (length >= 2 + param_position) {
  79. event->set_pitch(data[param_position]);
  80. event->set_velocity(data[param_position + 1]);
  81. }
  82. break;
  83. case MIDIMessage::PITCH_BEND:
  84. if (length >= 2 + param_position) {
  85. event->set_pitch((data[param_position + 1] << 7) | data[param_position]);
  86. }
  87. break;
  88. case MIDIMessage::PROGRAM_CHANGE:
  89. if (length >= 1 + param_position) {
  90. event->set_instrument(data[param_position]);
  91. }
  92. break;
  93. case MIDIMessage::CHANNEL_PRESSURE:
  94. if (length >= 1 + param_position) {
  95. event->set_pressure(data[param_position]);
  96. }
  97. break;
  98. default:
  99. break;
  100. }
  101. Input *id = Input::get_singleton();
  102. id->parse_input_event(event);
  103. }
  104. PackedStringArray MIDIDriver::get_connected_inputs() {
  105. PackedStringArray list;
  106. return list;
  107. }
  108. MIDIDriver::MIDIDriver() {
  109. set_singleton();
  110. }