midi_driver.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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(uint64_t timestamp, uint8_t *data, uint32_t length) {
  41. Ref<InputEventMIDI> event;
  42. event.instantiate();
  43. uint32_t param_position = 1;
  44. if (length >= 1) {
  45. if (data[0] >= 0xF0) {
  46. // channel does not apply to system common messages
  47. event->set_channel(0);
  48. event->set_message(MIDIMessage(data[0]));
  49. last_received_message = data[0];
  50. } else if ((data[0] & 0x80) == 0x00) {
  51. // running status
  52. event->set_channel(last_received_message & 0xF);
  53. event->set_message(MIDIMessage(last_received_message >> 4));
  54. param_position = 0;
  55. } else {
  56. event->set_channel(data[0] & 0xF);
  57. event->set_message(MIDIMessage(data[0] >> 4));
  58. param_position = 1;
  59. last_received_message = data[0];
  60. }
  61. }
  62. switch (event->get_message()) {
  63. case MIDIMessage::AFTERTOUCH:
  64. if (length >= 2 + param_position) {
  65. event->set_pitch(data[param_position]);
  66. event->set_pressure(data[param_position + 1]);
  67. }
  68. break;
  69. case MIDIMessage::CONTROL_CHANGE:
  70. if (length >= 2 + param_position) {
  71. event->set_controller_number(data[param_position]);
  72. event->set_controller_value(data[param_position + 1]);
  73. }
  74. break;
  75. case MIDIMessage::NOTE_ON:
  76. case MIDIMessage::NOTE_OFF:
  77. if (length >= 2 + param_position) {
  78. event->set_pitch(data[param_position]);
  79. event->set_velocity(data[param_position + 1]);
  80. }
  81. break;
  82. case MIDIMessage::PITCH_BEND:
  83. if (length >= 2 + param_position) {
  84. event->set_pitch((data[param_position + 1] << 7) | data[param_position]);
  85. }
  86. break;
  87. case MIDIMessage::PROGRAM_CHANGE:
  88. if (length >= 1 + param_position) {
  89. event->set_instrument(data[param_position]);
  90. }
  91. break;
  92. case MIDIMessage::CHANNEL_PRESSURE:
  93. if (length >= 1 + param_position) {
  94. event->set_pressure(data[param_position]);
  95. }
  96. break;
  97. default:
  98. break;
  99. }
  100. Input *id = Input::get_singleton();
  101. id->parse_input_event(event);
  102. }
  103. PackedStringArray MIDIDriver::get_connected_inputs() {
  104. PackedStringArray list;
  105. return list;
  106. }
  107. MIDIDriver::MIDIDriver() {
  108. set_singleton();
  109. }