Debian Codegraph @

core/os/midi_driver.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* midi_driver.cpp */
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 #include "midi_driver.h"
32
33 #include "core/input/input.h"
34
35 uint8_t MIDIDriver::last_received_message = 0x00;
36 MIDIDriver *MIDIDriver::singleton = nullptr;
37 MIDIDriver *MIDIDriver::get_singleton() {
38 return singleton;
39 }
40
41 MIDIDriver::MIDIDriver() {
42 singleton = this;
43 }
44
45 MIDIDriver::MessageCategory MIDIDriver::Parser::category(uint8_t p_midi_fragment) {
46 if (p_midi_fragment >= 0xf8) {
47 return MessageCategory::RealTime;
48 } else if (p_midi_fragment >= 0xf0) {
49 // System Exclusive begin/end are specified as System Common Category
50 // messages, but we separate them here and give them their own categories
51 // as their behavior is significantly different.
52 if (p_midi_fragment == 0xf0) {
53 return MessageCategory::SysExBegin;
54 } else if (p_midi_fragment == 0xf7) {
55 return MessageCategory::SysExEnd;
56 }
57 return MessageCategory::SystemCommon;
58 } else if (p_midi_fragment >= 0x80) {
59 return MessageCategory::Voice;
60 }
61 return MessageCategory::Data;
62 }
63
64 MIDIMessage MIDIDriver::Parser::status_to_msg_enum(uint8_t p_status_byte) {
65 if (p_status_byte & 0x80) {
66 if (p_status_byte < 0xf0) {
67 return MIDIMessage(p_status_byte >> 4);
68 } else {
69 return MIDIMessage(p_status_byte);
70 }
71 }
72 return MIDIMessage::NONE;
73 }
74
75 size_t MIDIDriver::Parser::expected_data(uint8_t p_status_byte) {
76 return expected_data(status_to_msg_enum(p_status_byte));
77 }
78
79 size_t MIDIDriver::Parser::expected_data(MIDIMessage p_msg_type) {
80 switch (p_msg_type) {
81 case MIDIMessage::NOTE_OFF:
82 case MIDIMessage::NOTE_ON:
83 case MIDIMessage::AFTERTOUCH:
84 case MIDIMessage::CONTROL_CHANGE:
85 case MIDIMessage::PITCH_BEND:
86 case MIDIMessage::SONG_POSITION_POINTER:
87 return 2;
88 case MIDIMessage::PROGRAM_CHANGE:
89 case MIDIMessage::CHANNEL_PRESSURE:
90 case MIDIMessage::QUARTER_FRAME:
91 case MIDIMessage::SONG_SELECT:
92 return 1;
93 default:
94 return 0;
95 }
96 }
97
98 uint8_t MIDIDriver::Parser::channel(uint8_t p_status_byte) {
99 if (category(p_status_byte) == MessageCategory::Voice) {
100 return p_status_byte & 0x0f;
101 }
102 return 0;
103 }
104
105 void MIDIDriver::send_event(int p_device_index, uint8_t p_status,
106 const uint8_t *p_data, size_t p_data_len) {
107 const MIDIMessage msg = Parser::status_to_msg_enum(p_status);
108 ERR_FAIL_COND(p_data_len < Parser::expected_data(msg));
109
110 Ref<InputEventMIDI> event;
111 event.instantiate();
112 event->set_device(p_device_index);
113 event->set_channel(Parser::channel(p_status));
114 event->set_message(msg);
115 switch (msg) {
116 case MIDIMessage::NOTE_OFF:
117 case MIDIMessage::NOTE_ON:
118 event->set_pitch(p_data[0]);
119 event->set_velocity(p_data[1]);
120 break;
121 case MIDIMessage::AFTERTOUCH:
122 event->set_pitch(p_data[0]);
123 event->set_pressure(p_data[1]);
124 break;
125 case MIDIMessage::CONTROL_CHANGE:
126 event->set_controller_number(p_data[0]);
127 event->set_controller_value(p_data[1]);
128 break;
129 case MIDIMessage::PROGRAM_CHANGE:
130 event->set_instrument(p_data[0]);
131 break;
132 case MIDIMessage::CHANNEL_PRESSURE:
133 event->set_pressure(p_data[0]);
134 break;
135 case MIDIMessage::PITCH_BEND:
136 event->set_pitch((p_data[1] << 7) | p_data[0]);
137 break;
138 // QUARTER_FRAME, SONG_POSITION_POINTER, and SONG_SELECT not yet implemented.
139 default:
140 break;
141 }
142 Input::get_singleton()->parse_input_event(event);
143 }
144
145 void MIDIDriver::Parser::parse_fragment(uint8_t p_fragment) {
146 switch (category(p_fragment)) {
147 case MessageCategory::RealTime:
148 // Real-Time messages are single byte messages that can
149 // occur at any point and do not interrupt other messages.
150 // We pass them straight through.
151 MIDIDriver::send_event(device_index, p_fragment);
152 break;
153
154 case MessageCategory::SysExBegin:
155 status_byte = p_fragment;
156 skipping_sys_ex = true;
157 break;
158
159 case MessageCategory::SysExEnd:
160 status_byte = 0;
161 skipping_sys_ex = false;
162 break;
163
164 case MessageCategory::Voice:
165 case MessageCategory::SystemCommon:
166 skipping_sys_ex = false; // If we were in SysEx, assume it was aborted.
167 received_data_len = 0;
168 status_byte = 0;
169 ERR_FAIL_COND(expected_data(p_fragment) > DATA_BUFFER_SIZE);
170 if (expected_data(p_fragment) == 0) {
171 // No data bytes needed, post it now.
172 MIDIDriver::send_event(device_index, p_fragment);
173 } else {
174 status_byte = p_fragment;
175 }
176 break;
177
178 case MessageCategory::Data:
179 // We don't currently process SysEx messages, so ignore their data.
180 if (!skipping_sys_ex) {
181 const size_t expected = expected_data(status_byte);
182 if (received_data_len < expected) {
183 data_buffer[received_data_len] = p_fragment;
184 received_data_len++;
185 if (received_data_len == expected) {
186 MIDIDriver::send_event(device_index, status_byte,
187 data_buffer, expected);
188 received_data_len = 0;
189 // Voice messages can use 'running status', sending further
190 // messages without resending their status byte.
191 // For other messages types we clear the cached status byte.
192 if (category(status_byte) != MessageCategory::Voice) {
193 status_byte = 0;
194 }
195 }
196 }
197 }
198 break;
199 }
200 }
201
202 PackedStringArray MIDIDriver::get_connected_inputs() const {
203 return connected_input_names;
204 }