Debian Codegraph @

core/os/midi_driver.h

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