Skip to content

Commit

Permalink
Move OS' MIDI methods into InputEventMIDI
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickeon committed Jan 5, 2024
1 parent 89cc635 commit 2b617d7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
16 changes: 16 additions & 0 deletions core/input/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,18 @@ int InputEventMIDI::get_controller_value() const {
return controller_value;
}

void InputEventMIDI::open_inputs() {
OS::get_singleton()->open_midi_inputs();
}

void InputEventMIDI::close_inputs() {
OS::get_singleton()->close_midi_inputs();
}

PackedStringArray InputEventMIDI::get_connected_inputs() {
return OS::get_singleton()->get_connected_midi_inputs();
}

String InputEventMIDI::as_text() const {
return vformat(RTR("MIDI Input on Channel=%s Message=%s"), itos(channel), itos((int64_t)message));
}
Expand Down Expand Up @@ -1763,6 +1775,10 @@ void InputEventMIDI::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_controller_value", "controller_value"), &InputEventMIDI::set_controller_value);
ClassDB::bind_method(D_METHOD("get_controller_value"), &InputEventMIDI::get_controller_value);

ClassDB::bind_static_method("InputEventMIDI", D_METHOD("open_inputs"), &InputEventMIDI::open_inputs);
ClassDB::bind_static_method("InputEventMIDI", D_METHOD("close_inputs"), &InputEventMIDI::close_inputs);
ClassDB::bind_static_method("InputEventMIDI", D_METHOD("get_connected_inputs"), &InputEventMIDI::get_connected_inputs);

ADD_PROPERTY(PropertyInfo(Variant::INT, "channel"), "set_channel", "get_channel");
ADD_PROPERTY(PropertyInfo(Variant::INT, "message"), "set_message", "get_message");
ADD_PROPERTY(PropertyInfo(Variant::INT, "pitch"), "set_pitch", "get_pitch");
Expand Down
4 changes: 4 additions & 0 deletions core/input/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ class InputEventMIDI : public InputEvent {
void set_controller_value(const int p_controller_value);
int get_controller_value() const;

static void open_inputs();
static void close_inputs();
static PackedStringArray get_connected_inputs();

virtual String as_text() const override;
virtual String to_string() override;

Expand Down
34 changes: 29 additions & 5 deletions doc/classes/InputEventMIDI.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<description>
InputEventMIDI allows receiving input events from MIDI (Musical Instrument Digital Interface) devices such as a piano.
MIDI signals can be sent over a 5-pin MIDI connector or over USB, if your device supports both be sure to check the settings in the device to see which output it's using.
To receive input events from MIDI devices, you need to call [method OS.open_midi_inputs]. You can check which devices are detected using [method OS.get_connected_midi_inputs].
To receive input events from MIDI devices, you need to call [method InputEventMIDI.open_inputs]. You can check which devices are detected using [method InputEventMIDI.get_connected_inputs].
[codeblocks]
[gdscript]
func _ready():
OS.open_midi_inputs()
print(OS.get_connected_midi_inputs())
InputEventMIDI.open_inputs()
print(InputEventMIDI.get_connected_inputs())

func _input(input_event):
if input_event is InputEventMIDI:
Expand All @@ -31,8 +31,8 @@
[csharp]
public override void _Ready()
{
OS.OpenMidiInputs();
GD.Print(OS.GetConnectedMidiInputs());
InputEventMIDI.OpenInputs();
GD.Print(InputEventMIDI.GetConnectedInputs());
}

public override void _Input(InputEvent @event)
Expand Down Expand Up @@ -64,6 +64,30 @@
<link title="Wikipedia General MIDI Instrument List">https://en.wikipedia.org/wiki/General_MIDI#Program_change_events</link>
<link title="Wikipedia Piano Key Frequencies List">https://en.wikipedia.org/wiki/Piano_key_frequencies#List</link>
</tutorials>
<methods>
<method name="close_inputs" qualifiers="static">
<return type="void" />
<description>
Shuts down system MIDI driver.
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
</description>
</method>
<method name="get_connected_inputs" qualifiers="static">
<return type="PackedStringArray" />
<description>
Returns an array of MIDI device names.
The returned array will be empty if the system MIDI driver has not previously been initialized with [method open_inputs].
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
</description>
</method>
<method name="open_inputs" qualifiers="static">
<return type="void" />
<description>
Initializes the singleton for the system MIDI driver.
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
</description>
</method>
</methods>
<members>
<member name="channel" type="int" setter="set_channel" getter="get_channel" default="0">
The MIDI channel of this input event. There are 16 channels, so this value ranges from 0 to 15. MIDI channel 9 is reserved for the use with percussion instruments, the rest of the channels are for non-percussion instruments.
Expand Down
12 changes: 6 additions & 6 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
Displays a modal dialog box using the host platform's implementation. The engine execution is blocked until the dialog is closed.
</description>
</method>
<method name="close_midi_inputs">
<method name="close_midi_inputs" is_deprecated="true">
<return type="void" />
<description>
Shuts down the system MIDI driver. Godot will no longer receive [InputEventMIDI]. See also [method open_midi_inputs] and [method get_connected_midi_inputs].
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
[i]Deprecated.[/i] Use [method InputEventMIDI.close_inputs].
</description>
</method>
<method name="crash">
Expand Down Expand Up @@ -220,11 +220,11 @@
Not to be confused with [method get_user_data_dir], which returns the [i]project-specific[/i] user data path.
</description>
</method>
<method name="get_connected_midi_inputs">
<method name="get_connected_midi_inputs" is_deprecated="true">
<return type="PackedStringArray" />
<description>
Returns an array of connected MIDI device names, if they exist. Returns an empty array if the system MIDI driver has not previously been initialized with [method open_midi_inputs]. See also [method close_midi_inputs].
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
[i]Deprecated.[/i] Use [method InputEventMIDI.get_connected_inputs].
</description>
</method>
<method name="get_data_dir" qualifiers="const">
Expand Down Expand Up @@ -632,11 +632,11 @@
[b]Note:[/b] If the user has disabled the recycle bin on their system, the file will be permanently deleted instead.
</description>
</method>
<method name="open_midi_inputs">
<method name="open_midi_inputs" is_deprecated="true">
<return type="void" />
<description>
Initializes the singleton for the system MIDI driver, allowing Godot to receive [InputEventMIDI]. See also [method get_connected_midi_inputs] and [method close_midi_inputs].
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
[i]Deprecated.[/i] Use [method InputEventMIDI.open_inputs].
</description>
</method>
<method name="read_string_from_stdin">
Expand Down

0 comments on commit 2b617d7

Please sign in to comment.