InputEventMIDI

Inherits: InputEvent < Resource < RefCounted < Object

Represents an input event from a MIDI device, such as a piano.

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 OS.open_midi_inputs. You can check which devices are detected using OS.get_connected_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: InputEventMIDI):
  8. print(midi_event)
  9. print("Channel " + str(midi_event.channel))
  10. print("Message " + str(midi_event.message))
  11. print("Pitch " + str(midi_event.pitch))
  12. print("Velocity " + str(midi_event.velocity))
  13. print("Instrument " + str(midi_event.instrument))
  14. print("Pressure " + str(midi_event.pressure))
  15. print("Controller number: " + str(midi_event.controller_number))
  16. print("Controller value: " + str(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 @event)
  7. {
  8. if (@event 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 that Godot does not currently support MIDI output, so there is no way to emit MIDI signals from Godot. Only MIDI input works.

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 ( int value )

  • int get_channel ( )

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.


int controller_number = 0

  • void set_controller_number ( int value )

  • int get_controller_number ( )

If the message is @GlobalScope.MIDI_MESSAGE_CONTROL_CHANGE, this indicates the controller number, otherwise this is zero. Controllers include devices such as pedals and levers.


int controller_value = 0

  • void set_controller_value ( int value )

  • int get_controller_value ( )

If the message is @GlobalScope.MIDI_MESSAGE_CONTROL_CHANGE, this indicates the controller value, otherwise this is zero. Controllers include devices such as pedals and levers.


int instrument = 0

  • void set_instrument ( int value )

  • int get_instrument ( )

The instrument of this input event. This value ranges from 0 to 127. Refer to the instrument list on the General MIDI wikipedia article to see a list of instruments, except that this value is 0-index, so subtract one from every number on that chart. A standard piano will have an instrument number of 0.


MIDIMessage message = 0

Returns a value indicating the type of message for this MIDI signal. This is a member of the MIDIMessage enum.

For MIDI messages between 0x80 and 0xEF, only the left half of the bits are returned as this value, as the other part is the channel (ex: 0x94 becomes 0x9). For MIDI messages from 0xF0 to 0xFF, the value is returned as-is.

Notes will return @GlobalScope.MIDI_MESSAGE_NOTE_ON when activated, but they might not always return @GlobalScope.MIDI_MESSAGE_NOTE_OFF when deactivated, therefore your code should treat the input as stopped if some period of time has passed.

Some MIDI devices may send @GlobalScope.MIDI_MESSAGE_NOTE_ON with zero velocity instead of @GlobalScope.MIDI_MESSAGE_NOTE_OFF.

For more information, see the note in velocity and the MIDI message status byte list chart linked above.


int pitch = 0

  • void set_pitch ( int value )

  • int get_pitch ( )

The pitch index number of this MIDI signal. This value ranges from 0 to 127. On a piano, middle C is 60, and A440 is 69, see the “MIDI note” column of the piano key frequency chart on Wikipedia for more information.


int pressure = 0

  • void set_pressure ( int value )

  • int get_pressure ( )

The pressure of the MIDI signal. This value ranges from 0 to 127. For many devices, this value is always zero.


int velocity = 0

  • void set_velocity ( int value )

  • int get_velocity ( )

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

Note: Some MIDI devices may send a @GlobalScope.MIDI_MESSAGE_NOTE_ON message with zero velocity and expect this to be treated the same as a @GlobalScope.MIDI_MESSAGE_NOTE_OFF message, but device implementations vary so Godot reports event data exactly as received. Depending on the hardware and the needs of the game/app, this MIDI quirk can be handled robustly with a couple lines of script (check for @GlobalScope.MIDI_MESSAGE_NOTE_ON with velocity zero).

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.