/**************************************************************************/1/* midi_driver.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/typedefs.h"33#include "core/variant/variant.h"3435/**36* Multi-Platform abstraction for accessing to MIDI.37*/3839class MIDIDriver {40static MIDIDriver *singleton;41static uint8_t last_received_message;4243protected:44// Categories of message for parser logic.45enum class MessageCategory {46Data,47Voice,48SysExBegin,49SystemCommon, // excluding System Exclusive Begin/End50SysExEnd,51RealTime,52};5354// Convert midi data to InputEventMIDI and send it to Input.55// p_data_len is the length of the buffer passed at p_data, this must be56// at least equal to the data required by the passed message type, but57// may be larger. Only the required data will be read.58static void send_event(int p_device_index, uint8_t p_status,59const uint8_t *p_data = nullptr, size_t p_data_len = 0);6061class Parser {62public:63Parser() = default;64Parser(int p_device_index) :65device_index{ p_device_index } {}66virtual ~Parser() = default;6768// Push a byte of MIDI stream. Any completed messages will be69// forwarded to MIDIDriver::send_event.70void parse_fragment(uint8_t p_fragment);7172static MessageCategory category(uint8_t p_midi_fragment);7374// If the byte is a Voice Message status byte return the contained75// channel number, otherwise zero.76static uint8_t channel(uint8_t p_status_byte);7778// If the byte is a status byte for a message with a fixed number of79// additional data bytes, return the number expected, otherwise zero.80static size_t expected_data(uint8_t p_status_byte);81static size_t expected_data(MIDIMessage p_msg_type);8283// If the fragment is a status byte return the message type84// represented, otherwise MIDIMessage::NONE.85static MIDIMessage status_to_msg_enum(uint8_t p_status_byte);8687private:88int device_index = 0;8990static constexpr size_t DATA_BUFFER_SIZE = 2;9192uint8_t status_byte = 0;93uint8_t data_buffer[DATA_BUFFER_SIZE] = { 0 };94size_t received_data_len = 0;95bool skipping_sys_ex = false;96};9798PackedStringArray connected_input_names;99100public:101static MIDIDriver *get_singleton();102103MIDIDriver();104virtual ~MIDIDriver() = default;105106virtual Error open() = 0;107virtual void close() = 0;108109PackedStringArray get_connected_inputs() const;110};111112113