midi_driver.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**************************************************************************/
  2. /* midi_driver.h */
  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. #ifndef MIDI_DRIVER_H
  31. #define MIDI_DRIVER_H
  32. #include "core/typedefs.h"
  33. #include "core/variant/variant.h"
  34. /**
  35. * Multi-Platform abstraction for accessing to MIDI.
  36. */
  37. class MIDIDriver {
  38. static MIDIDriver *singleton;
  39. static uint8_t last_received_message;
  40. protected:
  41. // Categories of message for parser logic.
  42. enum class MessageCategory {
  43. Data,
  44. Voice,
  45. SysExBegin,
  46. SystemCommon, // excluding System Exclusive Begin/End
  47. SysExEnd,
  48. RealTime,
  49. };
  50. // Convert midi data to InputEventMIDI and send it to Input.
  51. // p_data_len is the length of the buffer passed at p_data, this must be
  52. // at least equal to the data required by the passed message type, but
  53. // may be larger. Only the required data will be read.
  54. static void send_event(int p_device_index, uint8_t p_status,
  55. const uint8_t *p_data = nullptr, size_t p_data_len = 0);
  56. class Parser {
  57. public:
  58. Parser() = default;
  59. Parser(int p_device_index) :
  60. device_index{ p_device_index } {}
  61. virtual ~Parser() = default;
  62. // Push a byte of MIDI stream. Any completed messages will be
  63. // forwarded to MIDIDriver::send_event.
  64. void parse_fragment(uint8_t p_fragment);
  65. static MessageCategory category(uint8_t p_midi_fragment);
  66. // If the byte is a Voice Message status byte return the contained
  67. // channel number, otherwise zero.
  68. static uint8_t channel(uint8_t p_status_byte);
  69. // If the byte is a status byte for a message with a fixed number of
  70. // additional data bytes, return the number expected, otherwise zero.
  71. static size_t expected_data(uint8_t p_status_byte);
  72. static size_t expected_data(MIDIMessage p_msg_type);
  73. // If the fragment is a status byte return the message type
  74. // represented, otherwise MIDIMessage::NONE.
  75. static MIDIMessage status_to_msg_enum(uint8_t p_status_byte);
  76. private:
  77. int device_index = 0;
  78. static constexpr size_t DATA_BUFFER_SIZE = 2;
  79. uint8_t status_byte = 0;
  80. uint8_t data_buffer[DATA_BUFFER_SIZE] = { 0 };
  81. size_t received_data_len = 0;
  82. bool skipping_sys_ex = false;
  83. };
  84. PackedStringArray connected_input_names;
  85. public:
  86. static MIDIDriver *get_singleton();
  87. MIDIDriver();
  88. virtual ~MIDIDriver() = default;
  89. virtual Error open() = 0;
  90. virtual void close() = 0;
  91. PackedStringArray get_connected_inputs() const;
  92. };
  93. #endif // MIDI_DRIVER_H