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 from MIDI (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 call OS.open_midi_inputs, first. You can check which devices are detected with OS.get_connected_midi_inputs, and close the connection with OS.close_midi_inputs.

GDScriptC#

  1. func _ready():
  2. OS.open_midi_inputs()
  3. print(OS.get_connected_midi_inputs())
  4. func _input(input_event):
  5. if input_event is InputEventMIDI:
  6. _print_midi_info(input_event)
  7. func _print_midi_info(midi_event):
  8. print(midi_event)
  9. print("Channel ", midi_event.channel)
  10. print("Message ", midi_event.message)
  11. print("Pitch ", midi_event.pitch)
  12. print("Velocity ", midi_event.velocity)
  13. print("Instrument ", midi_event.instrument)
  14. print("Pressure ", midi_event.pressure)
  15. print("Controller number: ", midi_event.controller_number)
  16. print("Controller value: ", midi_event.controller_value)
  1. public override void _Ready()
  2. {
  3. OS.OpenMidiInputs();
  4. GD.Print(OS.GetConnectedMidiInputs());
  5. }
  6. public override void _Input(InputEvent inputEvent)
  7. {
  8. if (inputEvent is InputEventMidi midiEvent)
  9. {
  10. PrintMIDIInfo(midiEvent);
  11. }
  12. }
  13. private void PrintMIDIInfo(InputEventMidi midiEvent)
  14. {
  15. GD.Print(midiEvent);
  16. GD.Print($"Channel {midiEvent.Channel}");
  17. GD.Print($"Message {midiEvent.Message}");
  18. GD.Print($"Pitch {midiEvent.Pitch}");
  19. GD.Print($"Velocity {midiEvent.Velocity}");
  20. GD.Print($"Instrument {midiEvent.Instrument}");
  21. GD.Print($"Pressure {midiEvent.Pressure}");
  22. GD.Print($"Controller number: {midiEvent.ControllerNumber}");
  23. GD.Print($"Controller value: {midiEvent.ControllerValue}");
  24. }

Note: Godot does not support MIDI output, so there is no way to emit MIDI messages from Godot. Only MIDI input is supported.

Tutorials

Properties

int

channel

0

int

controller_number

0

int

controller_value

0

int

instrument

0

MIDIMessage

message

0

int

pitch

0

int

pressure

0

int

velocity

0


Property Descriptions

int channel = 0 🔗

  • void set_channel(value: int)

  • int get_channel()

The MIDI channel of this message, ranging from 0 to 15. MIDI channel 9 is reserved for percussion instruments.


int controller_number = 0 🔗

  • void set_controller_number(value: int)

  • int get_controller_number()

The unique number of the controller, if message is @GlobalScope.MIDI_MESSAGE_CONTROL_CHANGE, otherwise this is 0. 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 General MIDI specification for a small list.


int controller_value = 0 🔗

  • void set_controller_value(value: int)

  • int get_controller_value()

The value applied to the controller. If message is @GlobalScope.MIDI_MESSAGE_CONTROL_CHANGE, this value ranges from 0 to 127, otherwise it is 0. See also controller_value.


int instrument = 0 🔗

  • void set_instrument(value: int)

  • int get_instrument()

The instrument (also called program or preset) used on this MIDI message. This value ranges from 0 to 127.

To see what each value means, refer to the General MIDI’s instrument list. Keep in mind that the list is off by 1 because it does not begin from 0. A value of 0 corresponds to the acoustic grand piano.


MIDIMessage message = 0 🔗

Represents the type of MIDI message (see the MIDIMessage enum).

For more information, see the MIDI message status byte list chart.


int pitch = 0 🔗

  • void set_pitch(value: int)

  • int get_pitch()

The pitch index number of this MIDI message. This value ranges from 0 to 127.

On a piano, the middle C is 60, followed by a C-sharp (61), then a D (62), and so on. Each octave is split in offsets of 12. See the “MIDI note number” column of the piano key frequency chart a full list.


int pressure = 0 🔗

  • void set_pressure(value: int)

  • int get_pressure()

The strength of the key being pressed. This value ranges from 0 to 127.

Note: For many devices, this value is always 0. Other devices such as musical keyboards may simulate pressure by changing the velocity, instead.


int velocity = 0 🔗

  • void set_velocity(value: int)

  • int get_velocity()

The velocity of the MIDI message. This value ranges from 0 to 127. For a musical keyboard, this corresponds to how quickly the key was pressed, and is rarely above 110 in practice.

Note: Some MIDI devices may send a @GlobalScope.MIDI_MESSAGE_NOTE_ON message with 0 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:

  1. func _input(event):
  2. if event is InputEventMIDI:
  3. if event.message == MIDI_MESSAGE_NOTE_ON and event.velocity > 0:
  4. print("Note pressed!")

User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next