Skip to content

Commit

Permalink
Move MIDI's global constants into InputEventMIDI
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickeon committed Jan 2, 2024
1 parent 13a0d6e commit bba002f
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 46 deletions.
35 changes: 28 additions & 7 deletions core/input/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "core/input/input_map.h"
#include "core/input/shortcut.h"
#include "core/os/keyboard.h"
#include "core/os/midi_driver.h"
#include "core/os/os.h"

const int InputEvent::DEVICE_ID_EMULATION = -1;
Expand Down Expand Up @@ -1661,11 +1662,11 @@ int InputEventMIDI::get_channel() const {
return channel;
}

void InputEventMIDI::set_message(const MIDIMessage p_message) {
void InputEventMIDI::set_message(const Message p_message) {
message = p_message;
}

MIDIMessage InputEventMIDI::get_message() const {
InputEventMIDI::Message InputEventMIDI::get_message() const {
return message;
}

Expand Down Expand Up @@ -1724,19 +1725,19 @@ String InputEventMIDI::as_text() const {
String InputEventMIDI::to_string() {
String ret;
switch (message) {
case MIDIMessage::NOTE_ON:
case Message::MESSAGE_NOTE_ON:
ret = vformat("Note On: channel=%d, pitch=%d, velocity=%d", channel, pitch, velocity);
break;
case MIDIMessage::NOTE_OFF:
case Message::MESSAGE_NOTE_OFF:
ret = vformat("Note Off: channel=%d, pitch=%d, velocity=%d", channel, pitch, velocity);
break;
case MIDIMessage::PITCH_BEND:
case Message::MESSAGE_PITCH_BEND:
ret = vformat("Pitch Bend: channel=%d, pitch=%d", channel, pitch);
break;
case MIDIMessage::CHANNEL_PRESSURE:
case Message::MESSAGE_CHANNEL_PRESSURE:
ret = vformat("Channel Pressure: channel=%d, pressure=%d", channel, pressure);
break;
case MIDIMessage::CONTROL_CHANGE:
case Message::MESSAGE_CONTROL_CHANGE:
ret = vformat("Control Change: channel=%d, controller_number=%d, controller_value=%d", channel, controller_number, controller_value);
break;
default:
Expand All @@ -1763,6 +1764,26 @@ 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);

BIND_ENUM_CONSTANT(MESSAGE_NONE);
BIND_ENUM_CONSTANT(MESSAGE_NOTE_OFF);
BIND_ENUM_CONSTANT(MESSAGE_NOTE_ON);
BIND_ENUM_CONSTANT(MESSAGE_AFTERTOUCH);
BIND_ENUM_CONSTANT(MESSAGE_CONTROL_CHANGE);
BIND_ENUM_CONSTANT(MESSAGE_PROGRAM_CHANGE);
BIND_ENUM_CONSTANT(MESSAGE_CHANNEL_PRESSURE);
BIND_ENUM_CONSTANT(MESSAGE_PITCH_BEND);
BIND_ENUM_CONSTANT(MESSAGE_SYSTEM_EXCLUSIVE);
BIND_ENUM_CONSTANT(MESSAGE_QUARTER_FRAME);
BIND_ENUM_CONSTANT(MESSAGE_SONG_POSITION_POINTER);
BIND_ENUM_CONSTANT(MESSAGE_SONG_SELECT);
BIND_ENUM_CONSTANT(MESSAGE_TUNE_REQUEST);
BIND_ENUM_CONSTANT(MESSAGE_TIMING_CLOCK);
BIND_ENUM_CONSTANT(MESSAGE_START);
BIND_ENUM_CONSTANT(MESSAGE_CONTINUE);
BIND_ENUM_CONSTANT(MESSAGE_STOP);
BIND_ENUM_CONSTANT(MESSAGE_ACTIVE_SENSING);
BIND_ENUM_CONSTANT(MESSAGE_SYSTEM_RESET);

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
32 changes: 29 additions & 3 deletions core/input/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,35 @@ class InputEventPanGesture : public InputEventGesture {
};

class InputEventMIDI : public InputEvent {
private:
GDCLASS(InputEventMIDI, InputEvent);

public:
enum Message {
MESSAGE_NONE = (int)MIDIMessage::NONE,
MESSAGE_NOTE_OFF = (int)MIDIMessage::NOTE_OFF,
MESSAGE_NOTE_ON = (int)MIDIMessage::NOTE_ON,
MESSAGE_AFTERTOUCH = (int)MIDIMessage::AFTERTOUCH,
MESSAGE_CONTROL_CHANGE = (int)MIDIMessage::CONTROL_CHANGE,
MESSAGE_PROGRAM_CHANGE = (int)MIDIMessage::PROGRAM_CHANGE,
MESSAGE_CHANNEL_PRESSURE = (int)MIDIMessage::CHANNEL_PRESSURE,
MESSAGE_PITCH_BEND = (int)MIDIMessage::PITCH_BEND,
MESSAGE_SYSTEM_EXCLUSIVE = (int)MIDIMessage::SYSTEM_EXCLUSIVE,
MESSAGE_QUARTER_FRAME = (int)MIDIMessage::QUARTER_FRAME,
MESSAGE_SONG_POSITION_POINTER = (int)MIDIMessage::SONG_POSITION_POINTER,
MESSAGE_SONG_SELECT = (int)MIDIMessage::SONG_SELECT,
MESSAGE_TUNE_REQUEST = (int)MIDIMessage::TUNE_REQUEST,
MESSAGE_TIMING_CLOCK = (int)MIDIMessage::TIMING_CLOCK,
MESSAGE_START = (int)MIDIMessage::START,
MESSAGE_CONTINUE = (int)MIDIMessage::CONTINUE,
MESSAGE_STOP = (int)MIDIMessage::STOP,
MESSAGE_ACTIVE_SENSING = (int)MIDIMessage::ACTIVE_SENSING,
MESSAGE_SYSTEM_RESET = (int)MIDIMessage::SYSTEM_RESET,
};

private:
int channel = 0;
MIDIMessage message = MIDIMessage::NONE;
Message message = Message::MESSAGE_NONE;
int pitch = 0;
int velocity = 0;
int instrument = 0;
Expand All @@ -526,8 +551,8 @@ class InputEventMIDI : public InputEvent {
void set_channel(const int p_channel);
int get_channel() const;

void set_message(const MIDIMessage p_message);
MIDIMessage get_message() const;
void set_message(const Message p_message);
Message get_message() const;

void set_pitch(const int p_pitch);
int get_pitch() const;
Expand All @@ -552,6 +577,7 @@ class InputEventMIDI : public InputEvent {

InputEventMIDI() {}
};
VARIANT_ENUM_CAST(InputEventMIDI::Message);

class InputEventShortcut : public InputEvent {
GDCLASS(InputEventShortcut, InputEvent);
Expand Down
20 changes: 10 additions & 10 deletions core/os/midi_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,57 +51,57 @@ void MIDIDriver::receive_input_packet(uint64_t timestamp, uint8_t *data, uint32_
if (data[0] >= 0xF0) {
// channel does not apply to system common messages
event->set_channel(0);
event->set_message(MIDIMessage(data[0]));
event->set_message(InputEventMIDI::Message(data[0]));
last_received_message = data[0];
} else if ((data[0] & 0x80) == 0x00) {
// running status
event->set_channel(last_received_message & 0xF);
event->set_message(MIDIMessage(last_received_message >> 4));
event->set_message(InputEventMIDI::Message(last_received_message >> 4));
param_position = 0;
} else {
event->set_channel(data[0] & 0xF);
event->set_message(MIDIMessage(data[0] >> 4));
event->set_message(InputEventMIDI::Message(data[0] >> 4));
param_position = 1;
last_received_message = data[0];
}
}

switch (event->get_message()) {
case MIDIMessage::AFTERTOUCH:
case InputEventMIDI::Message::MESSAGE_AFTERTOUCH:
if (length >= 2 + param_position) {
event->set_pitch(data[param_position]);
event->set_pressure(data[param_position + 1]);
}
break;

case MIDIMessage::CONTROL_CHANGE:
case InputEventMIDI::Message::MESSAGE_CONTROL_CHANGE:
if (length >= 2 + param_position) {
event->set_controller_number(data[param_position]);
event->set_controller_value(data[param_position + 1]);
}
break;

case MIDIMessage::NOTE_ON:
case MIDIMessage::NOTE_OFF:
case InputEventMIDI::Message::MESSAGE_NOTE_ON:
case InputEventMIDI::Message::MESSAGE_NOTE_OFF:
if (length >= 2 + param_position) {
event->set_pitch(data[param_position]);
event->set_velocity(data[param_position + 1]);
}
break;

case MIDIMessage::PITCH_BEND:
case InputEventMIDI::Message::MESSAGE_PITCH_BEND:
if (length >= 2 + param_position) {
event->set_pitch((data[param_position + 1] << 7) | data[param_position]);
}
break;

case MIDIMessage::PROGRAM_CHANGE:
case InputEventMIDI::Message::MESSAGE_PROGRAM_CHANGE:
if (length >= 1 + param_position) {
event->set_instrument(data[param_position]);
}
break;

case MIDIMessage::CHANNEL_PRESSURE:
case InputEventMIDI::Message::MESSAGE_CHANNEL_PRESSURE:
if (length >= 1 + param_position) {
event->set_pressure(data[param_position]);
}
Expand Down
57 changes: 38 additions & 19 deletions doc/classes/@GlobalScope.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2529,62 +2529,81 @@
<constant name="JOY_AXIS_MAX" value="10" enum="JoyAxis">
The maximum number of game controller axes: OpenVR supports up to 5 Joysticks making a total of 10 axes.
</constant>
<constant name="MIDI_MESSAGE_NONE" value="0" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_NONE" value="0" enum="MIDIMessage" is_deprecated="true">
Enum value which doesn't correspond to any MIDI message. This is used to initialize [enum MIDIMessage] properties with a generic state.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_NONE].
</constant>
<constant name="MIDI_MESSAGE_NOTE_OFF" value="8" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_NOTE_OFF" value="8" enum="MIDIMessage" is_deprecated="true">
MIDI note OFF message. Not all MIDI devices send this event; some send [constant MIDI_MESSAGE_NOTE_ON] with zero velocity instead. See the documentation of [InputEventMIDI] for information of how to use MIDI inputs.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_NOTE_OFF].
</constant>
<constant name="MIDI_MESSAGE_NOTE_ON" value="9" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_NOTE_ON" value="9" enum="MIDIMessage" is_deprecated="true">
MIDI note ON message. Some MIDI devices send this event with velocity zero instead of [constant MIDI_MESSAGE_NOTE_OFF], but implementations vary. See the documentation of [InputEventMIDI] for information of how to use MIDI inputs.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_NOTE_ON].
</constant>
<constant name="MIDI_MESSAGE_AFTERTOUCH" value="10" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_AFTERTOUCH" value="10" enum="MIDIMessage" is_deprecated="true">
MIDI aftertouch message. This message is most often sent by pressing down on the key after it "bottoms out".
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_AFTERTOUCH].
</constant>
<constant name="MIDI_MESSAGE_CONTROL_CHANGE" value="11" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_CONTROL_CHANGE" value="11" enum="MIDIMessage" is_deprecated="true">
MIDI control change message. This message is sent when a controller value changes. Controllers include devices such as pedals and levers.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_CONTROL_CHANGE].
</constant>
<constant name="MIDI_MESSAGE_PROGRAM_CHANGE" value="12" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_PROGRAM_CHANGE" value="12" enum="MIDIMessage" is_deprecated="true">
MIDI program change message. This message sent when the program patch number changes.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_PROGRAM_CHANGE].
</constant>
<constant name="MIDI_MESSAGE_CHANNEL_PRESSURE" value="13" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_CHANNEL_PRESSURE" value="13" enum="MIDIMessage" is_deprecated="true">
MIDI channel pressure message. This message is most often sent by pressing down on the key after it "bottoms out". This message is different from polyphonic after-touch as it indicates the highest pressure across all keys.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_CHANNEL_PRESSURE].
</constant>
<constant name="MIDI_MESSAGE_PITCH_BEND" value="14" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_PITCH_BEND" value="14" enum="MIDIMessage" is_deprecated="true">
MIDI pitch bend message. This message is sent to indicate a change in the pitch bender (wheel or lever, typically).
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_PITCH_BEND].
</constant>
<constant name="MIDI_MESSAGE_SYSTEM_EXCLUSIVE" value="240" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_SYSTEM_EXCLUSIVE" value="240" enum="MIDIMessage" is_deprecated="true">
MIDI system exclusive message. This has behavior exclusive to the device you're receiving input from. Getting this data is not implemented in Godot.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_SYSTEM_EXCLUSIVE].
</constant>
<constant name="MIDI_MESSAGE_QUARTER_FRAME" value="241" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_QUARTER_FRAME" value="241" enum="MIDIMessage" is_deprecated="true">
MIDI quarter frame message. Contains timing information that is used to synchronize MIDI devices. Getting this data is not implemented in Godot.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_QUARTER_FRAME].
</constant>
<constant name="MIDI_MESSAGE_SONG_POSITION_POINTER" value="242" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_SONG_POSITION_POINTER" value="242" enum="MIDIMessage" is_deprecated="true">
MIDI song position pointer message. Gives the number of 16th notes since the start of the song. Getting this data is not implemented in Godot.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_SONG_POSITION_POINTER].
</constant>
<constant name="MIDI_MESSAGE_SONG_SELECT" value="243" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_SONG_SELECT" value="243" enum="MIDIMessage" is_deprecated="true">
MIDI song select message. Specifies which sequence or song is to be played. Getting this data is not implemented in Godot.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_SONG_SELECT].
</constant>
<constant name="MIDI_MESSAGE_TUNE_REQUEST" value="246" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_TUNE_REQUEST" value="246" enum="MIDIMessage" is_deprecated="true">
MIDI tune request message. Upon receiving a tune request, all analog synthesizers should tune their oscillators.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_TUNE_REQUEST].
</constant>
<constant name="MIDI_MESSAGE_TIMING_CLOCK" value="248" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_TIMING_CLOCK" value="248" enum="MIDIMessage" is_deprecated="true">
MIDI timing clock message. Sent 24 times per quarter note when synchronization is required.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_TIMING_CLOCK].
</constant>
<constant name="MIDI_MESSAGE_START" value="250" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_START" value="250" enum="MIDIMessage" is_deprecated="true">
MIDI start message. Start the current sequence playing. This message will be followed with Timing Clocks.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_START].
</constant>
<constant name="MIDI_MESSAGE_CONTINUE" value="251" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_CONTINUE" value="251" enum="MIDIMessage" is_deprecated="true">
MIDI continue message. Continue at the point the sequence was stopped.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_CONTINUE].
</constant>
<constant name="MIDI_MESSAGE_STOP" value="252" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_STOP" value="252" enum="MIDIMessage" is_deprecated="true">
MIDI stop message. Stop the current sequence.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_STOP].
</constant>
<constant name="MIDI_MESSAGE_ACTIVE_SENSING" value="254" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_ACTIVE_SENSING" value="254" enum="MIDIMessage" is_deprecated="true">
MIDI active sensing message. This message is intended to be sent repeatedly to tell the receiver that a connection is alive.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_ACTIVE_SENSING].
</constant>
<constant name="MIDI_MESSAGE_SYSTEM_RESET" value="255" enum="MIDIMessage">
<constant name="MIDI_MESSAGE_SYSTEM_RESET" value="255" enum="MIDIMessage" is_deprecated="true">
MIDI system reset message. Reset all receivers in the system to power-up status. It should not be sent on power-up itself.
[i]Deprecated.[/i] Use [constant InputEventMIDI.MESSAGE_SYSTEM_RESET].
</constant>
<constant name="OK" value="0" enum="Error">
Methods that return [enum Error] return [constant OK] when no error occurred.
Expand Down
Loading

0 comments on commit bba002f

Please sign in to comment.