midi_driver.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*************************************************************************/
  2. /* midi_driver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. MIDIDriver *MIDIDriver::singleton = NULL;
  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.instance();
  43. if (length >= 1) {
  44. event->set_channel(data[0] & 0xF);
  45. event->set_message(data[0] >> 4);
  46. }
  47. switch (event->get_message()) {
  48. case MIDI_MESSAGE_AFTERTOUCH:
  49. if (length >= 3) {
  50. event->set_pitch(data[1]);
  51. event->set_pressure(data[2]);
  52. }
  53. break;
  54. case MIDI_MESSAGE_CONTROL_CHANGE:
  55. if (length >= 3) {
  56. event->set_controller_number(data[1]);
  57. event->set_controller_value(data[2]);
  58. }
  59. break;
  60. case MIDI_MESSAGE_NOTE_ON:
  61. case MIDI_MESSAGE_NOTE_OFF:
  62. case MIDI_MESSAGE_PITCH_BEND:
  63. if (length >= 3) {
  64. event->set_pitch(data[1]);
  65. event->set_velocity(data[2]);
  66. }
  67. break;
  68. case MIDI_MESSAGE_PROGRAM_CHANGE:
  69. if (length >= 2) {
  70. event->set_instrument(data[1]);
  71. }
  72. break;
  73. case MIDI_MESSAGE_CHANNEL_PRESSURE:
  74. if (length >= 2) {
  75. event->set_pressure(data[1]);
  76. }
  77. break;
  78. }
  79. InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
  80. id->parse_input_event(event);
  81. }
  82. PoolStringArray MIDIDriver::get_connected_inputs() {
  83. PoolStringArray list;
  84. return list;
  85. }
  86. MIDIDriver::MIDIDriver() {
  87. set_singleton();
  88. }