diff --git a/node_2d.gd b/node_2d.gd index ec44987..5f602c4 100644 --- a/node_2d.gd +++ b/node_2d.gd @@ -3,30 +3,107 @@ extends Node2D # https://docs.godotengine.org/en/stable/classes/class_inputeventmidi.html # https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-midimessage +# if >0: print midi event to the console (1 for minimal logs, 2 for more logs) +# if =0: the program acts normally +const debug := 0 + +# load config values +var spin_channel_1 = Config.spin_channel_1 +var spin_note_1 = Config.spin_note_1 +var spin_channel_2 = Config.spin_channel_2 +var spin_note_2 = Config.spin_note_2 +var jog_channel = Config.jog_channel +var jog_note = Config.jog_note +var beat_channel = Config.beat_channel +var beat_note = Config.beat_note +var is_inverted = Config.is_inverted + +# init control values +var midi_beat: bool = false +var midi_jog: bool = false +var midi_turnleft: bool = false +var midi_turnright: bool = false +var left: int = 1 +var right: int = 127 + # Called when the node enters the scene tree for the first time. func _ready(): + + # fix midi values (the game adds one to the channel number) + spin_channel_1 -= 1 + spin_channel_2 -= 1 + jog_channel -= 1 + beat_channel -= 1 + + # handle inverted turntable (must invert left and right values) + if is_inverted: + left = 127 + right = 1 + print("inverted!") + + # connect to the midi turntable OS.open_midi_inputs() print(OS.get_connected_midi_inputs()) -func _unhandled_input(event): - if event is InputEventKey: - if event.keycode == KEY_ESCAPE: +func _unhandled_input(input_event): + if input_event is InputEventKey: + if input_event.keycode == KEY_ESCAPE: get_tree().quit() + if input_event is InputEventMIDI: + if debug > 0: + _print_midi_info(input_event) + if input_event.channel == beat_channel and ((input_event.pitch == beat_note and input_event.velocity == 127) or input_event.controller_number == 10): + midi_beat = true + if input_event.channel == beat_channel and ((input_event.pitch == beat_note and input_event.velocity == 0) or input_event.controller_number == 9): + midi_beat = false + if input_event.channel == jog_channel and ((input_event.pitch == jog_note and input_event.velocity == 127) or input_event.controller_number == spin_note_1): + midi_jog = true + if input_event.channel == jog_channel and ((input_event.pitch == jog_note and input_event.velocity == 0) or input_event.controller_number == spin_note_2): + midi_jog = false + if (input_event.channel == spin_channel_1 or input_event.channel == spin_channel_2) and input_event.controller_value == left: + midi_turnleft = true + if (input_event.channel == spin_channel_1 or input_event.channel == spin_channel_2) and input_event.controller_value == right: + midi_turnright = true + + +func _print_midi_info(midi_event: InputEventMIDI): + print(midi_event) + if debug > 1: + print("Channel " + str(midi_event.channel)) + print("Message " + str(midi_event.message)) + print("Pitch " + str(midi_event.pitch)) + print("Velocity " + str(midi_event.velocity)) + print("Instrument " + str(midi_event.instrument)) + print("Pressure " + str(midi_event.pressure)) + print("Controller number: " + str(midi_event.controller_number)) + print("Controller value: " + str(midi_event.controller_value)) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta): + + if debug == 0: + # process inputs + if Input.is_action_pressed("quit"): + # exit the software + get_tree().quit() + if Input.is_action_pressed("left") or midi_beat: + # trigger the left controller + print("left") + if Input.is_action_pressed("right") or midi_jog: + # trigger the right controller + print("right") + if midi_turnleft: + # trigger the left controller + print("turn left") + if midi_turnright: + # trigger the right controller + print("turn right") - # process inputs - if Input.is_action_pressed("quit"): - # exit the software - get_tree().quit() - if Input.is_action_pressed("left") or MIDIMessage.MIDI_MESSAGE_CONTROL_CHANGE == 1: - # trigger the left controller - print("left") - if Input.is_action_pressed("right") or MIDIMessage.MIDI_MESSAGE_CONTROL_CHANGE == 1: - # trigger the right controller - print("right") - right_turn + # reset the turn values before the next cycle + # needed because it can't be turned to false in the _unhandled_input function + # because apparently there is no MIDI command sended for that .___. + midi_turnleft = false + midi_turnright = false diff --git a/node_2d.tscn b/node_2d.tscn index 04acd3a..8db9ef8 100644 --- a/node_2d.tscn +++ b/node_2d.tscn @@ -1,122 +1,13 @@ -[gd_scene load_steps=4 format=3 uid="uid://chynesmvg3dl5"] +[gd_scene load_steps=5 format=3 uid="uid://chynesmvg3dl5"] [ext_resource type="Texture2D" uid="uid://pa6jmogeid3x" path="res://art/turn.PNG" id="1_58q2i"] +[ext_resource type="Script" path="res://node_2d.gd" id="1_r100h"] [ext_resource type="Texture2D" uid="uid://f8ibqo4nbqqk" path="res://art/bark.PNG" id="3_grsrk"] [sub_resource type="GDScript" id="GDScript_grk35"] -script/source = "extends Node2D - -# https://docs.godotengine.org/en/stable/classes/class_inputeventmidi.html -# https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-midimessage - -# if >0: print midi event to the console (1 for minimal logs, 2 for more logs) -# if =0: the program acts normally -const debug := 0 - -# load config values -var spin_channel_1 = Config.spin_channel_1 -var spin_note_1 = Config.spin_note_1 -var spin_channel_2 = Config.spin_channel_2 -var spin_note_2 = Config.spin_note_2 -var jog_channel = Config.jog_channel -var jog_note = Config.jog_note -var beat_channel = Config.beat_channel -var beat_note = Config.beat_note -var is_inverted = Config.is_inverted - -# init control values -var midi_beat: bool = false -var midi_jog: bool = false -var midi_turnleft: bool = false -var midi_turnright: bool = false -var left: int = 1 -var right: int = 127 - - -# Called when the node enters the scene tree for the first time. -func _ready(): - - # fix midi values (the game adds one to the channel number) - spin_channel_1 -= 1 - spin_channel_2 -= 1 - jog_channel -= 1 - beat_channel -= 1 - - # handle inverted turntable (must invert left and right values) - if is_inverted: - left = 127 - right = 1 - print(\"inverted!\") - - # connect to the midi turntable - OS.open_midi_inputs() - print(OS.get_connected_midi_inputs()) - - -func _unhandled_input(input_event): - if input_event is InputEventKey: - if input_event.keycode == KEY_ESCAPE: - get_tree().quit() - if input_event is InputEventMIDI: - if debug > 0: - _print_midi_info(input_event) - if input_event.channel == beat_channel and ((input_event.pitch == beat_note and input_event.velocity == 127) or input_event.controller_number == 10): - midi_beat = true - if input_event.channel == beat_channel and ((input_event.pitch == beat_note and input_event.velocity == 0) or input_event.controller_number == 9): - midi_beat = false - if input_event.channel == jog_channel and ((input_event.pitch == jog_note and input_event.velocity == 127) or input_event.controller_number == spin_note_1): - midi_jog = true - if input_event.channel == jog_channel and ((input_event.pitch == jog_note and input_event.velocity == 0) or input_event.controller_number == spin_note_2): - midi_jog = false - if (input_event.channel == spin_channel_1 or input_event.channel == spin_channel_2) and input_event.controller_value == left: - midi_turnleft = true - if (input_event.channel == spin_channel_1 or input_event.channel == spin_channel_2) and input_event.controller_value == right: - midi_turnright = true - - -func _print_midi_info(midi_event: InputEventMIDI): - print(midi_event) - if debug > 1: - print(\"Channel \" + str(midi_event.channel)) - print(\"Message \" + str(midi_event.message)) - print(\"Pitch \" + str(midi_event.pitch)) - print(\"Velocity \" + str(midi_event.velocity)) - print(\"Instrument \" + str(midi_event.instrument)) - print(\"Pressure \" + str(midi_event.pressure)) - print(\"Controller number: \" + str(midi_event.controller_number)) - print(\"Controller value: \" + str(midi_event.controller_value)) - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(_delta): - - if debug == 0: - # process inputs - if Input.is_action_pressed(\"quit\"): - # exit the software - get_tree().quit() - if Input.is_action_pressed(\"left\") or midi_beat: - # trigger the left controller - print(\"left\") - if Input.is_action_pressed(\"right\") or midi_jog: - # trigger the right controller - print(\"right\") - if midi_turnleft: - # trigger the left controller - print(\"turn left\") - if midi_turnright: - # trigger the right controller - print(\"turn right\") - - # reset the turn values before the next cycle - # needed because it can't be turned to false in the _unhandled_input function - # because apparently there is no MIDI command sended for that .___. - midi_turnleft = false - midi_turnright = false -" [node name="Node2D" type="Node2D"] -script = SubResource("GDScript_grk35") +script = ExtResource("1_r100h") [node name="left_turn" type="Sprite2D" parent="."] position = Vector2(270, 284)