/**************************************************************************/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/input/input_enums.h"33#include "core/typedefs.h"34#include "core/variant/variant.h"3536/**37* Multi-Platform abstraction for accessing to MIDI.38*/3940class MIDIDriver {41static MIDIDriver *singleton;42static uint8_t last_received_message;4344protected:45// Categories of message for parser logic.46enum class MessageCategory {47Data,48Voice,49SysExBegin,50SystemCommon, // excluding System Exclusive Begin/End51SysExEnd,52RealTime,53};5455// Convert midi data to InputEventMIDI and send it to Input.56// p_data_len is the length of the buffer passed at p_data, this must be57// at least equal to the data required by the passed message type, but58// may be larger. Only the required data will be read.59static void send_event(int p_device_index, uint8_t p_status,60const uint8_t *p_data = nullptr, size_t p_data_len = 0);6162class Parser {63public:64Parser() = default;65Parser(int p_device_index) :66device_index{ p_device_index } {}67virtual ~Parser() = default;6869// Push a byte of MIDI stream. Any completed messages will be70// forwarded to MIDIDriver::send_event.71void parse_fragment(uint8_t p_fragment);7273static MessageCategory category(uint8_t p_midi_fragment);7475// If the byte is a Voice Message status byte return the contained76// channel number, otherwise zero.77static uint8_t channel(uint8_t p_status_byte);7879// If the byte is a status byte for a message with a fixed number of80// additional data bytes, return the number expected, otherwise zero.81static size_t expected_data(uint8_t p_status_byte);82static size_t expected_data(MIDIMessage p_msg_type);8384// If the fragment is a status byte return the message type85// represented, otherwise MIDIMessage::NONE.86static MIDIMessage status_to_msg_enum(uint8_t p_status_byte);8788private:89int device_index = 0;9091static constexpr size_t DATA_BUFFER_SIZE = 2;9293uint8_t status_byte = 0;94uint8_t data_buffer[DATA_BUFFER_SIZE] = { 0 };95size_t received_data_len = 0;96bool skipping_sys_ex = false;97};9899PackedStringArray connected_input_names;100101public:102static MIDIDriver *get_singleton();103104MIDIDriver();105virtual ~MIDIDriver() = default;106107virtual Error open() = 0;108virtual void close() = 0;109110PackedStringArray get_connected_inputs() const;111};112113114