Skip to content

Commit

Permalink
Merge pull request #20154 from marcelofg55/midi_driver
Browse files Browse the repository at this point in the history
Added a new MIDIDriver class
  • Loading branch information
akien-mga authored Jul 24, 2018
2 parents 4080e7f + 7a5f9fc commit a501678
Show file tree
Hide file tree
Showing 28 changed files with 1,029 additions and 4 deletions.
5 changes: 5 additions & 0 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ String _OS::get_audio_driver_name(int p_driver) const {
return OS::get_singleton()->get_audio_driver_name(p_driver);
}

PoolStringArray _OS::get_connected_midi_inputs() {
return OS::get_singleton()->get_connected_midi_inputs();
}

void _OS::set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeable, int p_screen) {

OS::VideoMode vm;
Expand Down Expand Up @@ -1058,6 +1062,7 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_video_driver_name", "driver"), &_OS::get_video_driver_name);
ClassDB::bind_method(D_METHOD("get_audio_driver_count"), &_OS::get_audio_driver_count);
ClassDB::bind_method(D_METHOD("get_audio_driver_name", "driver"), &_OS::get_audio_driver_name);
ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &_OS::get_connected_midi_inputs);

ClassDB::bind_method(D_METHOD("get_screen_count"), &_OS::get_screen_count);
ClassDB::bind_method(D_METHOD("get_current_screen"), &_OS::get_current_screen);
Expand Down
2 changes: 2 additions & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class _OS : public Object {
virtual int get_audio_driver_count() const;
virtual String get_audio_driver_name(int p_driver) const;

virtual PoolStringArray get_connected_midi_inputs();

virtual int get_screen_count() const;
virtual int get_current_screen() const;
virtual void set_current_screen(int p_screen);
Expand Down
10 changes: 10 additions & 0 deletions core/global_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ VARIANT_ENUM_CAST(KeyList);
VARIANT_ENUM_CAST(KeyModifierMask);
VARIANT_ENUM_CAST(ButtonList);
VARIANT_ENUM_CAST(JoystickList);
VARIANT_ENUM_CAST(MidiMessageList);

void register_global_constants() {

Expand Down Expand Up @@ -458,6 +459,15 @@ void register_global_constants() {
BIND_GLOBAL_ENUM_CONSTANT(JOY_ANALOG_L2);
BIND_GLOBAL_ENUM_CONSTANT(JOY_ANALOG_R2);

// midi
BIND_GLOBAL_ENUM_CONSTANT(MIDI_MESSAGE_NOTE_OFF);
BIND_GLOBAL_ENUM_CONSTANT(MIDI_MESSAGE_NOTE_ON);
BIND_GLOBAL_ENUM_CONSTANT(MIDI_MESSAGE_AFTERTOUCH);
BIND_GLOBAL_ENUM_CONSTANT(MIDI_MESSAGE_CONTROL_CHANGE);
BIND_GLOBAL_ENUM_CONSTANT(MIDI_MESSAGE_PROGRAM_CHANGE);
BIND_GLOBAL_ENUM_CONSTANT(MIDI_MESSAGE_CHANNEL_PRESSURE);
BIND_GLOBAL_ENUM_CONSTANT(MIDI_MESSAGE_PITCH_BEND);

// error list

BIND_GLOBAL_ENUM_CONSTANT(OK);
Expand Down
119 changes: 119 additions & 0 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,3 +1080,122 @@ InputEventPanGesture::InputEventPanGesture() {

delta = Vector2(0, 0);
}
/////////////////////////////

void InputEventMIDI::set_channel(const int p_channel) {

channel = p_channel;
}

int InputEventMIDI::get_channel() const {
return channel;
}

void InputEventMIDI::set_message(const int p_message) {

message = p_message;
}

int InputEventMIDI::get_message() const {
return message;
}

void InputEventMIDI::set_pitch(const int p_pitch) {

pitch = p_pitch;
}

int InputEventMIDI::get_pitch() const {
return pitch;
}

void InputEventMIDI::set_velocity(const int p_velocity) {

velocity = p_velocity;
}

int InputEventMIDI::get_velocity() const {
return velocity;
}

void InputEventMIDI::set_instrument(const int p_instrument) {

instrument = p_instrument;
}

int InputEventMIDI::get_instrument() const {
return instrument;
}

void InputEventMIDI::set_pressure(const int p_pressure) {

pressure = p_pressure;
}

int InputEventMIDI::get_pressure() const {
return pressure;
}

void InputEventMIDI::set_controller_number(const int p_controller_number) {

controller_number = p_controller_number;
}

int InputEventMIDI::get_controller_number() const {
return controller_number;
}

void InputEventMIDI::set_controller_value(const int p_controller_value) {

controller_value = p_controller_value;
}

int InputEventMIDI::get_controller_value() const {
return controller_value;
}

String InputEventMIDI::as_text() const {

return "InputEventMIDI : channel=(" + itos(get_channel()) + "), message=(" + itos(get_message()) + ")";
}

void InputEventMIDI::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_channel", "channel"), &InputEventMIDI::set_channel);
ClassDB::bind_method(D_METHOD("get_channel"), &InputEventMIDI::get_channel);
ClassDB::bind_method(D_METHOD("set_message", "message"), &InputEventMIDI::set_message);
ClassDB::bind_method(D_METHOD("get_message"), &InputEventMIDI::get_message);
ClassDB::bind_method(D_METHOD("set_pitch", "pitch"), &InputEventMIDI::set_pitch);
ClassDB::bind_method(D_METHOD("get_pitch"), &InputEventMIDI::get_pitch);
ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMIDI::set_velocity);
ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMIDI::get_velocity);
ClassDB::bind_method(D_METHOD("set_instrument", "instrument"), &InputEventMIDI::set_instrument);
ClassDB::bind_method(D_METHOD("get_instrument"), &InputEventMIDI::get_instrument);
ClassDB::bind_method(D_METHOD("set_pressure", "pressure"), &InputEventMIDI::set_pressure);
ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventMIDI::get_pressure);
ClassDB::bind_method(D_METHOD("set_controller_number", "controller_number"), &InputEventMIDI::set_controller_number);
ClassDB::bind_method(D_METHOD("get_controller_number"), &InputEventMIDI::get_controller_number);
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);

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");
ADD_PROPERTY(PropertyInfo(Variant::INT, "velocity"), "set_velocity", "get_velocity");
ADD_PROPERTY(PropertyInfo(Variant::INT, "instrument"), "set_instrument", "get_instrument");
ADD_PROPERTY(PropertyInfo(Variant::INT, "pressure"), "set_pressure", "get_pressure");
ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_number"), "set_controller_number", "get_controller_number");
ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_value"), "set_controller_value", "get_controller_value");
}

InputEventMIDI::InputEventMIDI() {

channel = 0;
message = 0;
pitch = 0;
velocity = 0;
instrument = 0;
pressure = 0;
controller_number = 0;
controller_value = 0;
}
56 changes: 56 additions & 0 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ enum JoystickList {
JOY_ANALOG_R2 = JOY_AXIS_7,
};

enum MidiMessageList {
MIDI_MESSAGE_NOTE_OFF = 0x8,
MIDI_MESSAGE_NOTE_ON = 0x9,
MIDI_MESSAGE_AFTERTOUCH = 0xA,
MIDI_MESSAGE_CONTROL_CHANGE = 0xB,
MIDI_MESSAGE_PROGRAM_CHANGE = 0xC,
MIDI_MESSAGE_CHANNEL_PRESSURE = 0xD,
MIDI_MESSAGE_PITCH_BEND = 0xE,
};

/**
* Input Modifier Status
* for keyboard/mouse events.
Expand Down Expand Up @@ -525,4 +535,50 @@ class InputEventPanGesture : public InputEventGesture {

InputEventPanGesture();
};

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

int channel;
int message;
int pitch;
int velocity;
int instrument;
int pressure;
int controller_number;
int controller_value;

protected:
static void _bind_methods();

public:
void set_channel(const int p_channel);
int get_channel() const;

void set_message(const int p_message);
int get_message() const;

void set_pitch(const int p_pitch);
int get_pitch() const;

void set_velocity(const int p_velocity);
int get_velocity() const;

void set_instrument(const int p_instrument);
int get_instrument() const;

void set_pressure(const int p_pressure);
int get_pressure() const;

void set_controller_number(const int p_controller_number);
int get_controller_number() const;

void set_controller_value(const int p_controller_value);
int get_controller_value() const;

virtual String as_text() const;

InputEventMIDI();
};

#endif
107 changes: 107 additions & 0 deletions core/os/midi_driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*************************************************************************/
/* midi_driver.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "midi_driver.h"

#include "main/input_default.h"
#include "os/os.h"

MIDIDriver *MIDIDriver::singleton = NULL;
MIDIDriver *MIDIDriver::get_singleton() {

return singleton;
}

void MIDIDriver::set_singleton() {

singleton = this;
}

void MIDIDriver::receive_input_packet(uint64_t timestamp, uint8_t *data, uint32_t length) {

Ref<InputEventMIDI> event;
event.instance();

if (length >= 1) {
event->set_channel(data[0] & 0xF);
event->set_message(data[0] >> 4);
}

switch (event->get_message()) {
case MIDI_MESSAGE_AFTERTOUCH:
if (length >= 3) {
event->set_pitch(data[1]);
event->set_pressure(data[2]);
}
break;

case MIDI_MESSAGE_CONTROL_CHANGE:
if (length >= 3) {
event->set_controller_number(data[1]);
event->set_controller_value(data[2]);
}
break;

case MIDI_MESSAGE_NOTE_ON:
case MIDI_MESSAGE_NOTE_OFF:
case MIDI_MESSAGE_PITCH_BEND:
if (length >= 3) {
event->set_pitch(data[1]);
event->set_velocity(data[2]);
}
break;

case MIDI_MESSAGE_PROGRAM_CHANGE:
if (length >= 2) {
event->set_instrument(data[1]);
}
break;

case MIDI_MESSAGE_CHANNEL_PRESSURE:
if (length >= 2) {
event->set_pressure(data[1]);
}
break;
}

InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
id->parse_input_event(event);
}

PoolStringArray MIDIDriver::get_connected_inputs() {

PoolStringArray list;
return list;
}

MIDIDriver::MIDIDriver() {

set_singleton();
}
Loading

0 comments on commit a501678

Please sign in to comment.