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