Skip to content
0.2.0-dev Download ZIP
105 lines ·6.9 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="InputEventMIDI" inherits="InputEvent" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 Represents a MIDI message from a MIDI device, such as a musical keyboard.
5 </brief_description>
6 <description>
7 InputEventMIDI stores information about messages from [url=https://en.wikipedia.org/wiki/MIDI]MIDI[/url] (Musical Instrument Digital Interface) devices. These may include musical keyboards, synthesizers, and drum machines.
8 MIDI messages can be received 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 is using.
9 By default, Godot does not detect MIDI devices. You need to call [method OS.open_midi_inputs], first. You can check which devices are detected with [method OS.get_connected_midi_inputs], and close the connection with [method OS.close_midi_inputs].
10 [codeblocks]
11 [gdscript]
12 func _ready():
13 OS.open_midi_inputs()
14 print(OS.get_connected_midi_inputs())
15
16 func _input(input_event):
17 if input_event is InputEventMIDI:
18 _print_midi_info(input_event)
19
20 func _print_midi_info(midi_event):
21 print(midi_event)
22 print("Channel ", midi_event.channel)
23 print("Message ", midi_event.message)
24 print("Pitch ", midi_event.pitch)
25 print("Velocity ", midi_event.velocity)
26 print("Instrument ", midi_event.instrument)
27 print("Pressure ", midi_event.pressure)
28 print("Controller number: ", midi_event.controller_number)
29 print("Controller value: ", midi_event.controller_value)
30 [/gdscript]
31 [csharp]
32 public override void _Ready()
33 {
34 OS.OpenMidiInputs();
35 GD.Print(OS.GetConnectedMidiInputs());
36 }
37
38 public override void _Input(InputEvent inputEvent)
39 {
40 if (inputEvent is InputEventMidi midiEvent)
41 {
42 PrintMIDIInfo(midiEvent);
43 }
44 }
45
46 private void PrintMIDIInfo(InputEventMidi midiEvent)
47 {
48 GD.Print(midiEvent);
49 GD.Print($"Channel {midiEvent.Channel}");
50 GD.Print($"Message {midiEvent.Message}");
51 GD.Print($"Pitch {midiEvent.Pitch}");
52 GD.Print($"Velocity {midiEvent.Velocity}");
53 GD.Print($"Instrument {midiEvent.Instrument}");
54 GD.Print($"Pressure {midiEvent.Pressure}");
55 GD.Print($"Controller number: {midiEvent.ControllerNumber}");
56 GD.Print($"Controller value: {midiEvent.ControllerValue}");
57 }
58 [/csharp]
59 [/codeblocks]
60 [b]Note:[/b] Godot does not support MIDI output, so there is no way to emit MIDI messages from Godot. Only MIDI input is supported.
61 [b]Note:[/b] On the Web platform, using MIDI input requires a browser permission to be granted first. This permission request is performed when calling [method OS.open_midi_inputs]. MIDI input will not work until the user accepts the permission request.
62 </description>
63 <tutorials>
64 <link title="MIDI Message Status Byte List">https://www.midi.org/specifications-old/item/table-2-expanded-messages-list-status-bytes</link>
65 <link title="Wikipedia General MIDI Instrument List">https://en.wikipedia.org/wiki/General_MIDI#Program_change_events</link>
66 <link title="Wikipedia Piano Key Frequencies List">https://en.wikipedia.org/wiki/Piano_key_frequencies#List</link>
67 </tutorials>
68 <members>
69 <member name="channel" type="int" setter="set_channel" getter="get_channel" default="0">
70 The MIDI channel of this message, ranging from [code]0[/code] to [code]15[/code]. MIDI channel [code]9[/code] is reserved for percussion instruments.
71 </member>
72 <member name="controller_number" type="int" setter="set_controller_number" getter="get_controller_number" default="0">
73 The unique number of the controller, if [member message] is [constant MIDI_MESSAGE_CONTROL_CHANGE], otherwise this is [code]0[/code]. This value can be used to identify sliders for volume, balance, and panning, as well as switches and pedals on the MIDI device. See the [url=https://en.wikipedia.org/wiki/General_MIDI#Controller_events]General MIDI specification[/url] for a small list.
74 </member>
75 <member name="controller_value" type="int" setter="set_controller_value" getter="get_controller_value" default="0">
76 The value applied to the controller. If [member message] is [constant MIDI_MESSAGE_CONTROL_CHANGE], this value ranges from [code]0[/code] to [code]127[/code], otherwise it is [code]0[/code]. See also [member controller_value].
77 </member>
78 <member name="instrument" type="int" setter="set_instrument" getter="get_instrument" default="0">
79 The instrument (also called [i]program[/i] or [i]preset[/i]) used on this MIDI message. This value ranges from [code]0[/code] to [code]127[/code].
80 To see what each value means, refer to the [url=https://en.wikipedia.org/wiki/General_MIDI#Program_change_events]General MIDI's instrument list[/url]. Keep in mind that the list is off by 1 because it does not begin from 0. A value of [code]0[/code] corresponds to the acoustic grand piano.
81 </member>
82 <member name="message" type="int" setter="set_message" getter="get_message" enum="MIDIMessage" default="0">
83 Represents the type of MIDI message (see the [enum MIDIMessage] enum).
84 For more information, see the [url=https://www.midi.org/specifications-old/item/table-2-expanded-messages-list-status-bytes]MIDI message status byte list chart[/url].
85 </member>
86 <member name="pitch" type="int" setter="set_pitch" getter="get_pitch" default="0">
87 The pitch index number of this MIDI message. This value ranges from [code]0[/code] to [code]127[/code].
88 On a piano, the [b]middle C[/b] is [code]60[/code], followed by a [b]C-sharp[/b] ([code]61[/code]), then a [b]D[/b] ([code]62[/code]), and so on. Each octave is split in offsets of 12. See the "MIDI note number" column of the [url=https://en.wikipedia.org/wiki/Piano_key_frequencies]piano key frequency chart[/url] a full list.
89 </member>
90 <member name="pressure" type="int" setter="set_pressure" getter="get_pressure" default="0">
91 The strength of the key being pressed. This value ranges from [code]0[/code] to [code]127[/code].
92 [b]Note:[/b] For many devices, this value is always [code]0[/code]. Other devices such as musical keyboards may simulate pressure by changing the [member velocity], instead.
93 </member>
94 <member name="velocity" type="int" setter="set_velocity" getter="get_velocity" default="0">
95 The velocity of the MIDI message. This value ranges from [code]0[/code] to [code]127[/code]. For a musical keyboard, this corresponds to how quickly the key was pressed, and is rarely above [code]110[/code] in practice.
96 [b]Note:[/b] Some MIDI devices may send a [constant MIDI_MESSAGE_NOTE_ON] message with [code]0[/code] velocity and expect it to be treated the same as a [constant MIDI_MESSAGE_NOTE_OFF] message. If necessary, this can be handled with a few lines of code:
97 [codeblock]
98 func _input(event):
99 if event is InputEventMIDI:
100 if event.message == MIDI_MESSAGE_NOTE_ON and event.velocity &gt; 0:
101 print("Note pressed!")
102 [/codeblock]
103 </member>
104 </members>
105</class>