InputEventMIDI
Inherits:InputEvent<Resource<RefCounted<Object
Represents a MIDI message from a MIDI device, such as a musical keyboard.
Description
InputEventMIDI stores information about messages fromMIDI (Musical Instrument Digital Interface) devices. These may include musical keyboards, synthesizers, and drum machines.
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.
By default, Godot does not detect MIDI devices. You need to callOS.open_midi_inputs(), first. You can check which devices are detected withOS.get_connected_midi_inputs(), and close the connection withOS.close_midi_inputs().
func_ready():OS.open_midi_inputs()print(OS.get_connected_midi_inputs())func_input(input_event):ifinput_eventisInputEventMIDI:_print_midi_info(input_event)func_print_midi_info(midi_event):print(midi_event)print("Channel ",midi_event.channel)print("Message ",midi_event.message)print("Pitch ",midi_event.pitch)print("Velocity ",midi_event.velocity)print("Instrument ",midi_event.instrument)print("Pressure ",midi_event.pressure)print("Controller number: ",midi_event.controller_number)print("Controller value: ",midi_event.controller_value)
publicoverridevoid_Ready(){OS.OpenMidiInputs();GD.Print(OS.GetConnectedMidiInputs());}publicoverridevoid_Input(InputEventinputEvent){if(inputEventisInputEventMidimidiEvent){PrintMIDIInfo(midiEvent);}}privatevoidPrintMIDIInfo(InputEventMidimidiEvent){GD.Print(midiEvent);GD.Print($"Channel {midiEvent.Channel}");GD.Print($"Message {midiEvent.Message}");GD.Print($"Pitch {midiEvent.Pitch}");GD.Print($"Velocity {midiEvent.Velocity}");GD.Print($"Instrument {midiEvent.Instrument}");GD.Print($"Pressure {midiEvent.Pressure}");GD.Print($"Controller number: {midiEvent.ControllerNumber}");GD.Print($"Controller value: {midiEvent.ControllerValue}");}
Note: Godot does not support MIDI output, so there is no way to emit MIDI messages from Godot. Only MIDI input is supported.
Note: On the Web platform, using MIDI input requires a browser permission to be granted first. This permission request is performed when callingOS.open_midi_inputs(). MIDI input will not work until the user accepts the permission request.
Tutorials
Properties
| ||
| ||
| ||
| ||
| ||
| ||
| ||
|
Property Descriptions
intget_channel()
The MIDI channel of this message, ranging from0
to15
. MIDI channel9
is reserved for percussion instruments.
intget_controller_number()
The unique number of the controller, ifmessage is@GlobalScope.MIDI_MESSAGE_CONTROL_CHANGE, otherwise this is0
. This value can be used to identify sliders for volume, balance, and panning, as well as switches and pedals on the MIDI device. See theGeneral MIDI specification for a small list.
intget_controller_value()
The value applied to the controller. Ifmessage is@GlobalScope.MIDI_MESSAGE_CONTROL_CHANGE, this value ranges from0
to127
, otherwise it is0
. See alsocontroller_value.
intget_instrument()
The instrument (also calledprogram orpreset) used on this MIDI message. This value ranges from0
to127
.
To see what each value means, refer to theGeneral MIDI's instrument list. Keep in mind that the list is off by 1 because it does not begin from 0. A value of0
corresponds to the acoustic grand piano.
MIDIMessagemessage =0
🔗
MIDIMessageget_message()
Represents the type of MIDI message (see theMIDIMessage enum).
For more information, see theMIDI message status byte list chart.
intget_pitch()
The pitch index number of this MIDI message. This value ranges from0
to127
.
On a piano, themiddle C is60
, followed by aC-sharp (61
), then aD (62
), and so on. Each octave is split in offsets of 12. See the "MIDI note number" column of thepiano key frequency chart a full list.
intget_pressure()
The strength of the key being pressed. This value ranges from0
to127
.
Note: For many devices, this value is always0
. Other devices such as musical keyboards may simulate pressure by changing thevelocity, instead.
intget_velocity()
The velocity of the MIDI message. This value ranges from0
to127
. For a musical keyboard, this corresponds to how quickly the key was pressed, and is rarely above110
in practice.
Note: Some MIDI devices may send a@GlobalScope.MIDI_MESSAGE_NOTE_ON message with0
velocity and expect it to be treated the same as a@GlobalScope.MIDI_MESSAGE_NOTE_OFF message. If necessary, this can be handled with a few lines of code:
func_input(event):ifeventisInputEventMIDI:ifevent.message==MIDI_MESSAGE_NOTE_ONandevent.velocity>0:print("Note pressed!")