diff options
Diffstat (limited to 'core/os/midi_driver.h')
-rw-r--r-- | core/os/midi_driver.h | 68 |
1 files changed, 61 insertions, 7 deletions
diff --git a/core/os/midi_driver.h b/core/os/midi_driver.h index cad3d8189e..ddce63f9c8 100644 --- a/core/os/midi_driver.h +++ b/core/os/midi_driver.h @@ -42,19 +42,73 @@ class MIDIDriver { static MIDIDriver *singleton; static uint8_t last_received_message; +protected: + // Categories of message for parser logic. + enum class MessageCategory { + Data, + Voice, + SysExBegin, + SystemCommon, // excluding System Exclusive Begin/End + SysExEnd, + RealTime, + }; + + // Convert midi data to InputEventMIDI and send it to Input. + // p_data_len is the length of the buffer passed at p_data, this must be + // at least equal to the data required by the passed message type, but + // may be larger. Only the required data will be read. + static void send_event(int p_device_index, uint8_t p_status, + const uint8_t *p_data = nullptr, size_t p_data_len = 0); + + class Parser { + public: + Parser() = default; + Parser(int p_device_index) : + device_index{ p_device_index } {} + virtual ~Parser() = default; + + // Push a byte of MIDI stream. Any completed messages will be + // forwarded to MIDIDriver::send_event. + void parse_fragment(uint8_t p_fragment); + + static MessageCategory category(uint8_t p_midi_fragment); + + // If the byte is a Voice Message status byte return the contained + // channel number, otherwise zero. + static uint8_t channel(uint8_t p_status_byte); + + // If the byte is a status byte for a message with a fixed number of + // additional data bytes, return the number expected, otherwise zero. + static size_t expected_data(uint8_t p_status_byte); + static size_t expected_data(MIDIMessage p_msg_type); + + // If the fragment is a status byte return the message type + // represented, otherwise MIDIMessage::NONE. + static MIDIMessage status_to_msg_enum(uint8_t p_status_byte); + + private: + int device_index = 0; + + static constexpr size_t DATA_BUFFER_SIZE = 2; + + uint8_t status_byte = 0; + uint8_t data_buffer[DATA_BUFFER_SIZE] = { 0 }; + size_t received_data_len = 0; + bool skipping_sys_ex = false; + }; + + PackedStringArray connected_input_names; + public: static MIDIDriver *get_singleton(); - void set_singleton(); + + MIDIDriver(); + virtual ~MIDIDriver() = default; virtual Error open() = 0; virtual void close() = 0; - virtual PackedStringArray get_connected_inputs(); - - static void receive_input_packet(int device_index, uint64_t timestamp, uint8_t *data, uint32_t length); - - MIDIDriver(); - virtual ~MIDIDriver() {} + PackedStringArray get_connected_inputs() const; }; #endif // MIDI_DRIVER_H |